Exemplo n.º 1
0
def mstate_preprocess(confobj, eeg, eeg_info_study_obj):
    """
    Preprocesses (normalization option, GFP peak extraction, average referencing) EEG to prepare data for microstate computation.

    Parameters
    ----------
    confobj : object of type MstConfiguration
        Contains the following attributes: subtract_column_mean_at_start (bool), debug (bool), use_gfp_peaks (bool), force_avgref (bool), set_gfp_all_1 (bool), use_smoothing (bool), gfp_type_smoothing (string),
        smoothing_window (int), use_fancy_peaks (bool), method_GFPpeak (string), original_nr_of_maps (int), seed_number (int), max_number_of_iterations (int), ERP (bool), correspondance_cutoff (double):
    eeg : array
        Shape ntf*nch, conatains the EEG data the microstate analysis is to be computed on.
    eeg_info_study_obj : object of type EegInfo
        Contains the following attributes: nch (number of channels), tf (number of time frames per epoch), sf (sampling frequency), chlist (channel list)

    Returns
    -------
    eeg: array
        Preprocessed EEG.
    gfp_peak_indices: list
        List of indices of the EEG that qualify as global field power peaks.
    gfp_curve: 1D array
        Global field power for each time frame.
    """

    #################
    # 2.)a Subtract mean across columns (the mean map) [must be done for each 2 sec segment] (not done by default)
    #################              

    if confobj.subtract_column_mean_at_start:
        if confobj.debug:
            print 'column mean subtracted, does yield different results from published algorithm'
        eeg=subtract_column_mean_epochwise(eeg, eeg_info_study_obj.TF)
         
    #################   
    # 3.) COMPUTE GFP (ln1 or ln2)
    #################

    gfp_curve = compute_gfp(eeg, confobj.method_GFPpeak)
    if confobj.debug:
        print 'GFP Curve computed'

    #################
    #4.) Compute GFP Peaks (if the whole EEG is taken (use_gfp_peaks = False) it just returns the indices for the whole EEG
    #################

    gfp_peak_indices, gfp_curve = compute_gfp_peaks(gfp_curve, confobj.use_gfp_peaks, confobj.use_smoothing, confobj.gfp_type_smoothing, confobj.smoothing_window, confobj.use_fancy_peaks)

    #################
    # 2.) AVG REF
    #################

    if confobj.force_avgref:
        if confobj.debug:
            print 'Forced average reference'
        eeg=TK_norm(eeg, gfp_peak_indices, eeg_info_study_obj.nch)
                                     
    #################
    #5.) Set all Maps to GFP=1 (sets the maps for each timeframe to gfp=1)
    #################

    if confobj.set_gfp_all_1:
        print "GFP all maps set to 1"
        eeg = set_gfp_all_1(eeg, gfp_curve)

    return eeg, gfp_peak_indices, gfp_curve
Exemplo n.º 2
0
def modmaps_preprocess(confobj, eeg, eeg_info_study_obj):
    """
    Preprocesses (normalization option, GFP peak extraction, average referencing) EEG to prepare data for modelmap computation.

    Parameters
    ----------
    confobj : object of type MstConfiguration
        Contains the following attributes: subtract_column_mean_at_start (bool), debug (bool), use_gfp_peaks (bool), force_avgref (bool), set_gfp_all_1 (bool), use_smoothing (bool), gfp_type_smoothing (string),
        smoothing_window (int), use_fancy_peaks (bool), method_GFPpeak (string), original_nr_of_maps (int), seed_number (int), max_number_of_iterations (int), ERP (bool), correspondance_cutoff (double):
    eeg : array
        Shape ntf*nch, conatains the EEG data the microstate modelmap analysis is to be computed on.
    eeg_info_study_obj : object of type EegInfo
        Contains the following attributes: nch (number of channels), tf (number of time frames per epoch), sf (sampling frequency), chlist (channel list)

    Returns
    -------
    eeg: array
        Preprocessed EEG.
    gfp_peak_indices: list
        List of indices of the EEG that qualify as global field power peaks.
    gfp_curve: 1D array
        Global field power for each time frame.
    """

    #################
    # 2.) Subtract mean across columns (the mean map) [must be done for each 2 sec segment] (not done by default)
    #################              

    if confobj.subtract_column_mean_at_start:
        if confobj.debug:
            print('column mean subtracted, does yield different results from published algorithm')
        eeg=subtract_column_mean_epochwise(eeg, eeg_info_study_obj.TF)
         
    #################   
    # 3.) COMPUTE GFP (ln1 or ln2)
    #################

    gfp_curve = compute_gfp(eeg, confobj.method_GFPpeak)
    if confobj.debug:
        print('GFP Curve computed')
        #numpy.savetxt("gfp_curve_{0}" .format(eeg[0,0]), gfp_curve)

    #################
    #4.) Compute GFP Peaks (if the whole EEG is taken (use_gfp_peaks = False) it just returns the indices for the whole EEG
    #################

    gfp_peak_indices_pre, gfp_curve = compute_gfp_peaks(gfp_curve, confobj.use_gfp_peaks, confobj.use_smoothing, confobj.gfp_type_smoothing, confobj.smoothing_window, confobj.use_fancy_peaks)
    
    #correction for artifical peaks at epoch transitions  
    #excludes all peaks that are divisible by 512: e.g. 0, 512, 1024 [beginning of an epoch if tf=512] or that are 1 smaller than a divisible: e.g. 511, 1023,... [ends of epochs if tf=511]
    gfp_peak_indices = numpy.array([x for x in gfp_peak_indices_pre if not x % eeg_info_study_obj.tf == 0 and not (x+1) % eeg_info_study_obj.tf == 0])
    
    #make sure that there are at least as many gfp peaks as confobj.original_nr_of_maps
    if not len(gfp_peak_indices)>=confobj.original_nr_of_maps:
        #add correct error type
        raise IndexError("Number of GFP peaks need to be larger than number of maps extracted. Number of maps extracted is set to:" + confobj.original_nr_of_maps + ". Only " + len(gfp_peak_indices) + " GFP Peak Indices are available for computation.")    

    if confobj.debug:
        print('GFP Peak Indices identified')
        #numpy.savetxt("gfp_peak_indices_{0}" .format(eeg[0,0]), gfp_peak_indices)
        #numpy.savetxt("gfp_peaks_{0}" .format(eeg[0,0]), eeg[gfp_peak_indices])

    #################
    # 5.) AVG REF (done by default)
    #################

    if confobj.force_avgref:
        eeg=TK_norm(eeg, gfp_peak_indices, eeg_info_study_obj.nch)
        if confobj.debug:
            print('Forced average reference')
            #numpy.savetxt("eeg_TKnorm_{0}" .format(eeg[0,0]), eeg)
                                            
    #################
    # 6.) Set all Maps to GFP=1 (sets the maps for each timeframe to gfp=1) (not done by default)
    #################

    if confobj.set_gfp_all_1:
        print("GFP all maps set to 1")
        eeg = set_gfp_all_1(eeg, gfp_curve)

    return eeg, gfp_peak_indices, gfp_curve

    #################
    # 6.) Compute vectornorm for all maps -- is done for meanmods as well ---> Test!
    #################

    normalize_maps(eeg, 'vector_norm_1')

    return eeg, gfp_peak_indices, gfp_curve
Exemplo n.º 3
0
def modmaps_preprocess(confobj, eeg, eeg_info_study_obj):
    """
    Preprocesses (normalization option, GFP peak extraction, average referencing) EEG to prepare data for modelmap computation.

    Parameters
    ----------
    confobj : object of type MstConfiguration
        Contains the following attributes: subtract_column_mean_at_start (bool), debug (bool), use_gfp_peaks (bool), force_avgref (bool), set_gfp_all_1 (bool), use_smoothing (bool), gfp_type_smoothing (string),
        smoothing_window (int), use_fancy_peaks (bool), method_GFPpeak (string), original_nr_of_maps (int), seed_number (int), max_number_of_iterations (int), ERP (bool), correspondance_cutoff (double):
    eeg : array
        Shape ntf*nch, conatains the EEG data the microstate modelmap analysis is to be computed on.
    eeg_info_study_obj : object of type EegInfo
        Contains the following attributes: nch (number of channels), tf (number of time frames per epoch), sf (sampling frequency), chlist (channel list)

    Returns
    -------
    eeg: array
        Preprocessed EEG.
    gfp_peak_indices: list
        List of indices of the EEG that qualify as global field power peaks.
    gfp_curve: 1D array
        Global field power for each time frame.
    """

    #################
    # 2.) Subtract mean across columns (the mean map) [must be done for each 2 sec segment] (not done by default)
    #################

    if confobj.subtract_column_mean_at_start:
        if confobj.debug:
            print(
                'column mean subtracted, does yield different results from published algorithm'
            )
        eeg = subtract_column_mean_epochwise(eeg, eeg_info_study_obj.TF)

    #################
    # 3.) COMPUTE GFP (ln1 or ln2)
    #################

    gfp_curve = compute_gfp(eeg, confobj.method_GFPpeak)
    if confobj.debug:
        print('GFP Curve computed')
        #numpy.savetxt("gfp_curve_{0}" .format(eeg[0,0]), gfp_curve)

    #################
    #4.) Compute GFP Peaks (if the whole EEG is taken (use_gfp_peaks = False) it just returns the indices for the whole EEG
    #################

    gfp_peak_indices_pre, gfp_curve = compute_gfp_peaks(
        gfp_curve, confobj.use_gfp_peaks, confobj.use_smoothing,
        confobj.gfp_type_smoothing, confobj.smoothing_window,
        confobj.use_fancy_peaks)

    #correction for artifical peaks at epoch transitions
    #excludes all peaks that are divisible by 512: e.g. 0, 512, 1024 [beginning of an epoch if tf=512] or that are 1 smaller than a divisible: e.g. 511, 1023,... [ends of epochs if tf=511]
    gfp_peak_indices = numpy.array([
        x for x in gfp_peak_indices_pre if not x %
        eeg_info_study_obj.tf == 0 and not (x + 1) % eeg_info_study_obj.tf == 0
    ])

    #make sure that there are at least as many gfp peaks as confobj.original_nr_of_maps
    if not len(gfp_peak_indices) >= confobj.original_nr_of_maps:
        #add correct error type
        raise IndexError(
            "Number of GFP peaks need to be larger than number of maps extracted. Number of maps extracted is set to:"
            + confobj.original_nr_of_maps + ". Only " + len(gfp_peak_indices) +
            " GFP Peak Indices are available for computation.")

    if confobj.debug:
        print('GFP Peak Indices identified')
        #numpy.savetxt("gfp_peak_indices_{0}" .format(eeg[0,0]), gfp_peak_indices)
        #numpy.savetxt("gfp_peaks_{0}" .format(eeg[0,0]), eeg[gfp_peak_indices])

    #################
    # 5.) AVG REF (done by default)
    #################

    if confobj.force_avgref:
        eeg = TK_norm(eeg, gfp_peak_indices, eeg_info_study_obj.nch)
        if confobj.debug:
            print('Forced average reference')
            #numpy.savetxt("eeg_TKnorm_{0}" .format(eeg[0,0]), eeg)

    #################
    # 6.) Set all Maps to GFP=1 (sets the maps for each timeframe to gfp=1) (not done by default)
    #################

    if confobj.set_gfp_all_1:
        print("GFP all maps set to 1")
        eeg = set_gfp_all_1(eeg, gfp_curve)

    return eeg, gfp_peak_indices, gfp_curve

    #################
    # 6.) Compute vectornorm for all maps -- is done for meanmods as well ---> Test!
    #################

    normalize_maps(eeg, 'vector_norm_1')

    return eeg, gfp_peak_indices, gfp_curve