def __init__(self): self.function = BlockParameter(name='function equation', attribute_type=ParameterType.STRING, defaultvalue='x') self.graph_domain = BlockParameter(name='range', attribute_type=ParameterType.NUMBER, defaultvalue=100)
class Plot(Block): family = 'Graph' name = 'Plot' def __init__(self): self.function = BlockParameter(name='function equation', attribute_type=ParameterType.STRING, defaultvalue='x') self.graph_domain = BlockParameter(name='range', attribute_type=ParameterType.NUMBER, defaultvalue=100) def input_params(self, data): self.function.set_value(data['function equation']) self.graph_domain.set_value(int(data['range'])) def execute(self): x = np.array(range(-self.graph_domain.value, self.graph_domain.value)) y = eval(self.function.value) plt.figure(figsize=(20, 10)) plt.plot(x, y) plt.xlabel('x-axis') plt.ylabel('y-axis') plt.title('Graph Equation: {}'.format(self.function.value)) plt.grid() return (None, 'GRAPH')
class Constant(Block): family = 'Constant' name = 'Constant' def __init__(self): self.num = BlockParameter( name='constant value', attribute_type=ParameterType.NUMBER, defaultvalue=10 # defaultvalue='' ) self.output = BlockOutput( name='output', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.NUMBER ) def input_params(self,data): self.num.set_value(int(data['constant value'])) def execute(self): value = self.num.value self.output.set_value(value) return (value,'STRING')
class OfflineDataProvider(Block): family = 'DataProvider' name = 'OfflineDataProvider' def __init__(self): self.eeg_data = BlockParameter( name='EEG File', attribute_type=ParameterType.FILE, defaultvalue='', description='Select only the ".eeg" file, but folder must contain ".vhdr" and ".vmrk" files' ) self.eeg_data_output = BlockOutput( name='EEGData', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EEGDATA ) def input_params(self,data): path = data['EEG File'].split('/') path = Block.FILE_BASE_PATH + '/' + '/'.join(path[1:]) vhdr_path = '.'.join(path.split('.')[:-1]) + '.vhdr' raw = mne.io.read_raw_brainvision(vhdr_path) self.eeg_data.set_value(raw) def execute(self): value = self.eeg_data.value value.load_data() self.eeg_data_output.set_value(value) return (value.get_data().shape,'STRING')
def __init__(self): self.eeg_data = BlockInput(name='EEGData', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EEGDATA) self.pre_stimulus_onset = BlockParameter( name='PreStimulus onset', attribute_type=ParameterType.NUMBER, defaultvalue='-0.2', description='') self.post_stimulus_onset = BlockParameter( name='PostStimulus onset', attribute_type=ParameterType.NUMBER, defaultvalue='0.5', description='') self.baseline_start_time = BlockParameter( name='Baseline Correction Start Time', attribute_type=ParameterType.STRING, defaultvalue='None', description='None means beginning of the data') self.baseline_end_time = BlockParameter( name='Baseline Correction End Time', attribute_type=ParameterType.STRING, defaultvalue='0', description='None means end of the data') self.event_id = BlockParameter(name='Event Id', attribute_type=ParameterType.STRING, defaultvalue='None', description='None means all the events') self.eeg_data_output = BlockOutput(name='Epochs', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EPOCHS)
class FeatureLabeling(Block): family = 'FeatureExtraction' name = 'FeatureLabeling' def __init__(self): self.feature_vector = BlockInput(name='FeatureVector', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EPOCHS) self.labels = BlockParameter( name='Labels', attribute_type=ParameterType.STRING_ARRAY, defaultvalue='', description= 'All comma separated event ids are considered as one class.<br>Event id will not be considered if not added' ) self.feature_vector_out = BlockOutput( name='FeatureVector', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.FEATUREVECTOR) def input_params(self, data): self.feature_vector.set_value(data['FeatureVector']) self.labels.set_value(data['Labels']) def execute(self): ''' Here every comma separated event ids are assigned same class. For exmaple if the block parameter is: - [ '2,4', '3', '5,6' ] - So classes are assigned like: - event id 2 and 4 are assigned class 0 - event id 3 is assigned class 1 - event id 5 and 5 are assigned class 2 ''' event_class_mp = self.event_class_mapping() features = [] for feature in self.feature_vector.value: class_id = feature.class_id if (class_id in event_class_mp.keys()): feature.set_class(event_class_mp[class_id]) features.append(feature) self.feature_vector_out.set_value(features) return (event_class_mp, 'STRING') def event_class_mapping(self): mapping = {} class_i = 0 for label in self.labels.value: event_ids = [int(x) for x in label.split(',')] for event_id in event_ids: mapping[event_id] = class_i class_i += 1 return mapping
def __init__(self): self.num = BlockParameter( name='constant value', attribute_type=ParameterType.NUMBER, defaultvalue=10 # defaultvalue='' ) self.output = BlockOutput( name='output', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.NUMBER )
def __init__(self): self.eeg_data = BlockParameter( name='EEG File', attribute_type=ParameterType.FILE, defaultvalue='', description='Select only the ".eeg" file, but folder must contain ".vhdr" and ".vmrk" files' ) self.eeg_data_output = BlockOutput( name='EEGData', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EEGDATA )
def __init__(self): self.model = BlockInput(name='Model', min_cardinality=0, max_cardinality=1, attribute_type=ParameterType.MODEL) self.feature_vector = BlockInput( name='FeatureVector', min_cardinality=0, max_cardinality=1, attribute_type=ParameterType.FEATUREVECTOR) self.optimizer = BlockParameter( name='Optimizer', attribute_type=ParameterType.STRING, defaultvalue='adam', description="Either, 'adam', 'adagrad', 'rmsprop'") self.test_size = BlockParameter( name='Test Size', attribute_type=ParameterType.NUMBER, defaultvalue='0.33', description="Test Train Split. Ratio of test size") self.epochs = BlockParameter(name='Number of Epochs', attribute_type=ParameterType.NUMBER, defaultvalue='100', description="") self.classes = BlockParameter( name='Number of Classes', attribute_type=ParameterType.NUMBER, defaultvalue='', description="Number of Classification classes") self.model_out = BlockOutput(name='Model', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.MODEL)
def __init__(self): self.feature_vector = BlockInput(name='FeatureVector', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EPOCHS) self.labels = BlockParameter( name='Labels', attribute_type=ParameterType.STRING_ARRAY, defaultvalue='', description= 'All comma separated event ids are considered as one class.<br>Event id will not be considered if not added' ) self.feature_vector_out = BlockOutput( name='FeatureVector', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.FEATUREVECTOR)
def __init__(self): self.eeg_data = BlockInput( name='EEGData', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EEGDATA ) self.channels = BlockParameter( name='Channels', attribute_type=ParameterType.STRING_ARRAY, defaultvalue='', description='Select channel' ) self.eeg_data_output = BlockOutput( name='EEGData', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EEGDATA )
def __init__(self): self.eeg_data = BlockInput(name='EEGData', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EEGDATA) self.low_cutoff_freq = BlockParameter( name='Low Cutoff Frequency', attribute_type=ParameterType.NUMBER, defaultvalue='None', description='If None the data are only low-passed') self.high_cutoff_freq = BlockParameter( name='High Cutoff Frequency', attribute_type=ParameterType.NUMBER, defaultvalue='None', description='If None the data are only high-passed') self.eeg_data_output = BlockOutput( name='EEGData', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EEGDATA)
def __init__(self): self.model = BlockInput(name='Model', min_cardinality=0, max_cardinality=1, attribute_type=ParameterType.MODEL) self.units = BlockParameter(name='Number of Units', attribute_type=ParameterType.NUMBER, defaultvalue='', description='Must be an Integer') self.activation = BlockParameter( name='Activation Function', attribute_type=ParameterType.STRING, defaultvalue='relu', description= "Either, 'sigmoid', 'relu', 'softmax', 'elu', 'leaky-relu', 'selu' or 'gelu'" ) self.dropout = BlockParameter( name='Dropout', attribute_type=ParameterType.STRING, defaultvalue='0', description="Fraction of the input units to drop") self.model_out = BlockOutput(name='Model', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.MODEL)
class ChannelSelection(Block): family = 'PreProcessing' name = 'ChannelSelection' def __init__(self): self.eeg_data = BlockInput( name='EEGData', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EEGDATA ) self.channels = BlockParameter( name='Channels', attribute_type=ParameterType.STRING_ARRAY, defaultvalue='', description='Select channel' ) self.eeg_data_output = BlockOutput( name='EEGData', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EEGDATA ) def input_params(self,data): self.channels.set_value(data['Channels']) self.eeg_data.set_value(data['EEGData']) def execute(self): value = self.eeg_data.value value = value.pick_channels(self.channels.value) self.eeg_data_output.set_value(value) return (value.get_data().shape,'STRING')
class Filter(Block): family = 'PreProcessing' name = 'Filter' def __init__(self): self.eeg_data = BlockInput(name='EEGData', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EEGDATA) self.low_cutoff_freq = BlockParameter( name='Low Cutoff Frequency', attribute_type=ParameterType.NUMBER, defaultvalue='None', description='If None the data are only low-passed') self.high_cutoff_freq = BlockParameter( name='High Cutoff Frequency', attribute_type=ParameterType.NUMBER, defaultvalue='None', description='If None the data are only high-passed') self.eeg_data_output = BlockOutput( name='EEGData', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EEGDATA) def input_params(self, data): self.eeg_data.set_value(data['EEGData']) if (data['Low Cutoff Frequency'] == 'None'): self.low_cutoff_freq.set_value(None) else: self.low_cutoff_freq.set_value(float(data['Low Cutoff Frequency'])) if (data['High Cutoff Frequency'] == 'None'): self.high_cutoff_freq.set_value(None) else: self.high_cutoff_freq.set_value( float(data['High Cutoff Frequency'])) def execute(self): raw = self.eeg_data.value raw.filter(self.low_cutoff_freq.value, self.high_cutoff_freq.value, fir_design='firwin') self.eeg_data_output.set_value(raw) return None
class NeuralNetworkLayer(Block): family = 'Classification' name = 'NeuralNetworkLayer' def __init__(self): self.model = BlockInput(name='Model', min_cardinality=0, max_cardinality=1, attribute_type=ParameterType.MODEL) self.units = BlockParameter(name='Number of Units', attribute_type=ParameterType.NUMBER, defaultvalue='', description='Must be an Integer') self.activation = BlockParameter( name='Activation Function', attribute_type=ParameterType.STRING, defaultvalue='relu', description= "Either, 'sigmoid', 'relu', 'softmax', 'elu', 'leaky-relu', 'selu' or 'gelu'" ) self.dropout = BlockParameter( name='Dropout', attribute_type=ParameterType.STRING, defaultvalue='0', description="Fraction of the input units to drop") self.model_out = BlockOutput(name='Model', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.MODEL) def input_params(self, data): act_func = [ 'sigmoid', 'relu', 'softmax', 'elu', 'leaky-relu', 'selu', 'gelu' ] if ('Model' not in data.keys()): self.model.set_value(None) else: self.model.set_value(data['Model']) self.units.set_value(int(data['Number of Units'])) self.activation.set_value(data['Activation Function']) self.dropout.set_value(float(data['Dropout'])) def execute(self): if (self.model.value == None): model = Sequential() else: model = self.model.value model.add(Dense(self.units.value, activation=self.activation.value)) model.add(Dropout(self.dropout.value)) self.model_out.set_value(model) return None
class EpochExtraction(Block): family = 'PreProcessing' name = 'EpochExtraction' def __init__(self): self.eeg_data = BlockInput(name='EEGData', min_cardinality=1, max_cardinality=1, attribute_type=ParameterType.EEGDATA) self.pre_stimulus_onset = BlockParameter( name='PreStimulus onset', attribute_type=ParameterType.NUMBER, defaultvalue='-0.2', description='') self.post_stimulus_onset = BlockParameter( name='PostStimulus onset', attribute_type=ParameterType.NUMBER, defaultvalue='0.5', description='') self.baseline_start_time = BlockParameter( name='Baseline Correction Start Time', attribute_type=ParameterType.STRING, defaultvalue='None', description='None means beginning of the data') self.baseline_end_time = BlockParameter( name='Baseline Correction End Time', attribute_type=ParameterType.STRING, defaultvalue='0', description='None means end of the data') self.event_id = BlockParameter(name='Event Id', attribute_type=ParameterType.STRING, defaultvalue='None', description='None means all the events') self.eeg_data_output = BlockOutput(name='Epochs', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.EPOCHS) def input_params(self, data): self.eeg_data.set_value(data['EEGData']) self.pre_stimulus_onset.set_value(float(data['PreStimulus onset'])) self.post_stimulus_onset.set_value(float(data['PostStimulus onset'])) if (data['Baseline Correction Start Time'] == 'None'): self.baseline_start_time.set_value(None) else: self.baseline_start_time.set_value( float(data['Baseline Correction Start Time'])) if (data['Baseline Correction End Time'] == 'None'): self.baseline_end_time.set_value(None) else: self.baseline_end_time.set_value( float(data['Baseline Correction End Time'])) if (data['Event Id'] == 'None'): self.event_id.set_value(None) else: self.event_id.set_value(int(data['Event Id'])) def execute(self): raw = self.eeg_data.value events, event_id = mne.events_from_annotations(raw) # https://mne.tools/stable/generated/mne.Epochs.html epochs = mne.Epochs(raw, events, event_id=self.event_id.value, tmin=self.pre_stimulus_onset.value, tmax=self.post_stimulus_onset.value, baseline=(self.baseline_start_time.value, self.baseline_end_time.value), preload=True) self.eeg_data_output.set_value(epochs) return (epochs.get_data().shape, 'STRING')
class NeuralNetworkClassifier(Block): family = 'Classification' name = 'NeuralNetworkClassifier' def __init__(self): self.model = BlockInput(name='Model', min_cardinality=0, max_cardinality=1, attribute_type=ParameterType.MODEL) self.feature_vector = BlockInput( name='FeatureVector', min_cardinality=0, max_cardinality=1, attribute_type=ParameterType.FEATUREVECTOR) self.optimizer = BlockParameter( name='Optimizer', attribute_type=ParameterType.STRING, defaultvalue='adam', description="Either, 'adam', 'adagrad', 'rmsprop'") self.test_size = BlockParameter( name='Test Size', attribute_type=ParameterType.NUMBER, defaultvalue='0.33', description="Test Train Split. Ratio of test size") self.epochs = BlockParameter(name='Number of Epochs', attribute_type=ParameterType.NUMBER, defaultvalue='100', description="") self.classes = BlockParameter( name='Number of Classes', attribute_type=ParameterType.NUMBER, defaultvalue='', description="Number of Classification classes") self.model_out = BlockOutput(name='Model', min_cardinality=1, max_cardinality=100, attribute_type=ParameterType.MODEL) def input_params(self, data): self.model.set_value(data['Model']) self.feature_vector.set_value(data['FeatureVector']) self.optimizer.set_value(data['Optimizer']) self.test_size.set_value(float(data['Test Size'])) self.epochs.set_value(int(data['Number of Epochs'])) self.classes.set_value(int(data['Number of Classes'])) def execute(self): model = self.model.value model.compile(loss='categorical_crossentropy', optimizer=self.optimizer.value, metrics=['accuracy']) X = [] Y = [] for datapoint in self.feature_vector.value: X.append(datapoint.features) Y.append(datapoint.class_id) X_train, X_test, y_train, y_test = train_test_split( np.array(X), np.array(Y), test_size=self.test_size.value, random_state=42) number_of_training_sample = X_train.shape print(number_of_training_sample) print(X_test.shape) print(set(y_train)) print(set(y_test)) y_train = keras.utils.to_categorical(y_train, self.classes.value) y_test = keras.utils.to_categorical(y_test, self.classes.value) # X_train = X_test = np.array(X) # y_test = y_train = keras.utils.to_categorical(np.array(Y),self.classes.value) history = model.fit(X_train, y_train, epochs=self.epochs.value, validation_data=(X_test, y_test)) self.model_out.set_value(model) validation_acc = history.history['val_accuracy'][-1] training_acc = history.history['accuracy'][-1] stdout = "<br>Training Accuracy: {}<br>Validation Accuracy: {}<br>No. of Training Samples: {}".format( training_acc, validation_acc, number_of_training_sample[0]) return (stdout, 'STRING')