bycycle-tools / bycycle

Cycle-by-cycle analysis of neural oscillations.

Home Page:https://bycycle-tools.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

apavlo89 opened this issue · comments

The code below seems to be infinitely running with no csv output while I get a multiple lines error repeated in the console that starts like this

RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

Any idea what is causing it? Here is an example code below

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from neurodsp.sim import sim_combined
from neurodsp.filt import filter_signal
from neurodsp.plts import plot_time_series

from bycycle.group import compute_features_2d
from bycycle.plts import plot_burst_detect_summary, plot_feature_categorical

pd.options.display.max_columns = 10

# import required module
from pathlib import Path
import glob
import mne
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import os
sns.set(font_scale=1.2)

raw = mne.io.read_raw_eeglab('D:/REEG MU HIGHLOW/EEGLAB/Post ICA/P1_ICA.set', preload=True, uint16_codec=None, verbose=None)
              # Select a subset of EEG channels
raw.pick_channels(['Cz'])  
                # # Load data
sf = raw.info['sfreq']
chan = raw.ch_names
                #ch_names = chan

                # Frequency band of interest
                
f_alpha = (8, 13)
                
                # Tuned burst detection parameters
threshold_kwargs = {'amp_fraction_threshold': .2,
                                    'amp_consistency_threshold': .5,
                                    'period_consistency_threshold': .5,
                                    'monotonicity_threshold': .8,
                                    'min_n_cycles': 3}
                
                # Compute features for each signal
compute_features_kwargs={'threshold_kwargs': threshold_kwargs}
                
df_features_list = compute_features_2d(raw, sf, f_alpha, compute_features_kwargs)
                # Only consider cycles that were identified to be in bursting regimes
df_features_burst = df_features_list[df_features_list['is_burst']]
df_features_burst.to_csv('D:/REEG MU HIGHLOW/Mu/csv cycle-by-cycle/P1_Cz_mucharacteristics.csv')

Hi @apavlo89, this looks like a windows specific interaction with the multiprocessing module. Can you try inserting:

if __name__ == '__main__':
    df_features_list = compute_features_2d(raw, sf, f_alpha, compute_features_kwargs)

I've now fixed that error with the following code - axis=None was particularly important:

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from neurodsp.sim import sim_combined
from neurodsp.filt import filter_signal
from neurodsp.plts import plot_time_series

from bycycle.group import compute_features_2d
from bycycle.plts import plot_burst_detect_summary, plot_feature_categorical

pd.options.display.max_columns = 10

# import required module
from pathlib import Path
import glob
import mne
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import os
sns.set(font_scale=1.2)

raw = mne.io.read_raw_eeglab('D:/REEG MU HIGHLOW/EEGLAB/Post ICA/P1_preICA.set', preload=True, uint16_codec=None, verbose=None)
              # Select a subset of EEG channels
raw.pick_channels(['Cz'])  

                # # Load data
data = raw._data * 1e6
sf = raw.info['sfreq']
chan = raw.ch_names
                #ch_names = chan
times = np.arange(data.size) / sf
                #times = np.arange(data.shape[1]) / sf
print(data.shape, chan, times)
                # Frequency band of interest
f_alpha = (8, 13)
                
                # Tuned burst detection parameters
threshold_kwargs = {'amp_fraction_threshold': .2,
                                    'amp_consistency_threshold': .5,
                                    'period_consistency_threshold': .5,
                                    'monotonicity_threshold': .8,
                                    'min_n_cycles': 3}
                
                # Compute features for each signal
compute_features_kwargs={'threshold_kwargs': threshold_kwargs}
                
df_features_list = compute_features_2d(data, sf, f_alpha, compute_features_kwargs, axis=None)

However I am having difficulty selecting bursts and then printing them out in CSV. I am trying multiple things but something about the way things are listed in df_features_list is causing issues... Any help on this would be appreciated

You can merge the list of dataframes using the flatten_dfs function. This will add a index column to the end of the merged dataframe specifying which signal index each row belongs to.

from bycycle.utils.dataframes import flatten_dfs
df_features = flatten_dfs(df_features_list, list(range(len(df_features_list))))
df_features.to_csv('features.csv')

Thank you, finally it works - It was doing my head in. This is the code I used

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from neurodsp.sim import sim_combined
from neurodsp.filt import filter_signal
from neurodsp.plts import plot_time_series

from bycycle.group import compute_features_2d
from bycycle.plts import plot_burst_detect_summary, plot_feature_categorical

pd.options.display.max_columns = 10

# import required module
from pathlib import Path
import glob
import mne
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import os
sns.set(font_scale=1.2)

raw = mne.io.read_raw_eeglab('D:/REEG MU HIGHLOW/EEGLAB/Post ICA/P1_preICA.set', preload=True, uint16_codec=None, verbose=None)
              # Select a subset of EEG channels
raw.pick_channels(['Cz'])  

                # # Load data
data = raw._data * 1e6
sf = raw.info['sfreq']
chan = raw.ch_names
                #ch_names = chan
times = np.arange(data.size) / sf
                #times = np.arange(data.shape[1]) / sf
print(data.shape, chan, times)
                # Frequency band of interest
f_alpha = (8, 13)
                
                # Tuned burst detection parameters
threshold_kwargs = {'amp_fraction_threshold': .2,
                                    'amp_consistency_threshold': .5,
                                    'period_consistency_threshold': .5,
                                    'monotonicity_threshold': .8,
                                    'min_n_cycles': 3}
                
                # Compute features for each signal
compute_features_kwargs={'threshold_kwargs': threshold_kwargs}
                
df_features_list = compute_features_2d(data, sf, f_alpha, compute_features_kwargs, axis=None)
# df_features_burst = pd.DataFrame(df_features_list)
                #Only consider cycles that were identified to be in bursting regimes

from bycycle.utils.dataframes import flatten_dfs
df_features = flatten_dfs(df_features_list, list(range(len(df_features_list))))
df_features = pd.DataFrame(df_features)

df_features_burst = df_features[df_features['is_burst']== True]

df_features_burst.to_csv('D:/REEG MU HIGHLOW/Mu/csv cycle-by-cycle/P1_Cz_mucharacteristics.csv')