def _setup_efel(self): """Set up efel before extracting the feature""" import efel efel.reset() if self.threshold is not None: efel.setThreshold(self.threshold) if self.stimulus_current is not None: efel.setDoubleSetting('stimulus_current', self.stimulus_current) if self.interp_step is not None: efel.setDoubleSetting('interp_step', self.interp_step) if self.double_settings is not None: for setting_name, setting_value in self.double_settings.items(): efel.setDoubleSetting(setting_name, setting_value) if self.int_settings is not None: for setting_name, setting_value in self.int_settings.items(): efel.setIntSetting(setting_name, setting_value) if self.string_settings is not None: for setting_name, setting_value in self.string_settings.items(): efel.setStrSetting(setting_name, setting_value)
def calculate_score(self, responses): """Calculate the score""" efel_trace = self._construct_efel_trace(responses) if efel_trace is None: score = 250.0 else: import efel efel.reset() if self.threshold: efel.setThreshold(self.threshold) score = efel.getDistance( efel_trace, self.efel_feature_name, self.exp_mean, self.exp_std) efel.reset() logger.debug('Calculated score for %s: %f', self.name, score) return score
def FFun_for_Features(self, mod_t, features_data, feature, k, args={}): exp_mean = 0 exp_std = 0 # generating time trace for efel t_gen = frange( 0, self.option.run_controll_tstop + self.option.run_controll_dt, self.option.run_controll_dt) t = [] for n in t_gen: t.append(n) t = t[0:len(mod_t)] # converting list to numpy array for efel mod_t_np = np.array(mod_t) t_np = np.array(t) temp_fit = 0 trace = {} traces = [] trace['T'] = t_np trace['V'] = mod_t_np trace['stim_start'] = [self.option.stim_del] trace['stim_end'] = [self.option.stim_del + self.option.stim_dur] traces.append(trace) efel.setThreshold(self.thres) traces_results = self.get_efel_values(traces, feature) exp_mean = features_data[feature]["mean"][k] exp_std = features_data[feature]["std"][k] mod_result = traces_results[0][feature] if mod_result is not None and mod_result.size > 1 and ( feature == 'AP_rise_time' or feature == 'AP_amplitude' or feature == 'AP_duration_half_width' or feature == 'AP_begin_voltage' or feature == 'AP_rise_rate'): mod_result = scipy.mean(mod_result[1:]) elif mod_result is not None and mod_result.size > 1: mod_result = scipy.mean( mod_result ) #for features (AHP_depth, AP_duration_half_width) that gives a list as a result, the mean of the results is used elif mod_result is not None and mod_result.size != 0: # mod_result is a numpy array with one element, 0 element for AP1_amp or APlast_amp if no AP generated by the model mod_result = mod_result[0] if (mod_result == None or mod_result.size == 0) and (exp_mean != None and exp_std != None): temp_fit = 250 elif exp_mean == None and exp_std == None: temp_fit = 0 else: result = abs(exp_mean - mod_result) / exp_std temp_fit = result return temp_fit
def _setup_efel(self): """Set up efel before extracting the feature""" import efel efel.reset() if self.threshold is not None: efel.setThreshold(self.threshold) if self.stimulus_current is not None: efel.setDoubleSetting('stimulus_current', self.stimulus_current) if not efel.FeatureNameExists(self.efel_feature_name): raise ValueError("eFEL doesn't have a feature called %s" % self.efel_feature_name)
def calculate_score(self, responses): """Calculate the score""" efel_trace = self._construct_efel_trace(responses) import efel if self.threshold: efel.setThreshold(self.threshold) score = efel.getDistance( efel_trace, self.efel_feature_name, self.exp_mean, self.exp_std) logger.debug('Calculated score for %s: %f', self.name, score) return score
def _setup_efel(self): """Set up efel before extracting the feature""" import efel efel.reset() if self.threshold is not None: efel.setThreshold(self.threshold) if self.stimulus_current is not None: efel.setDoubleSetting('stimulus_current', self.stimulus_current) if self.interp_step is not None: efel.setDoubleSetting('interp_step', self.interp_step) if self.double_settings is not None: for setting_name, setting_value in self.double_settings.items(): efel.setDoubleSetting(setting_name, setting_value) if self.int_settings is not None: for setting_name, setting_value in self.int_settings.items(): efel.setIntSetting(setting_name, setting_value)
def _setup_efel(self): """Set up efel before extracting the feature""" import efel efel.reset() if self.threshold is not None: efel.setThreshold(self.threshold) if self.stimulus_current is not None: efel.setDoubleSetting('stimulus_current', self.stimulus_current) if self.interp_step is not None: efel.setDoubleSetting('interp_step', self.interp_step) if self.double_settings is not None: for setting_name, setting_value in self.double_settings.items(): efel.setDoubleSetting(setting_name, setting_value) if not efel.FeatureNameExists(self.efel_feature_name): raise ValueError("eFEL doesn't have a feature called %s" % self.efel_feature_name)
def get_allfeature_values(): """Get back all the feature names and value""" import efel efel.reset() import numpy all_featurenames = efel.getFeatureNames() soma_data = numpy.loadtxt(os.path.join(testdata_dir, 'testdata.txt')) soma_time = soma_data[:, 0] soma_voltage = soma_data[:, 1] db_data = numpy.loadtxt(os.path.join(testdata_dir, 'testdbdata.txt')) db_time = db_data[:, 0] db_voltage = db_data[:, 1] bac_data = numpy.loadtxt(os.path.join(testdata_dir, 'testbacdata.txt')) bac_time = bac_data[:, 0] bac_voltage = bac_data[:, 1] bap1_data = numpy.loadtxt(os.path.join(testdata_dir, 'testbap1data.txt')) bap1_time = bap1_data[:, 0] bap1_voltage = bap1_data[:, 1] bap2_data = numpy.loadtxt(os.path.join(testdata_dir, 'testbap2data.txt')) bap2_time = bap2_data[:, 0] bap2_voltage = bap2_data[:, 1] trace = {} trace_db = {} trace['T'] = soma_time trace['V'] = soma_voltage trace['stim_start'] = [700] trace['stim_end'] = [2700] trace['T;location_AIS'] = soma_time trace['V;location_AIS'] = soma_voltage trace['stim_start;location_AIS'] = [700] trace['stim_end;location_AIS'] = [2700] trace['T;location_epsp'] = bac_time trace['V;location_epsp'] = bac_voltage trace['stim_start;location_epsp'] = [295] trace['stim_end;location_epsp'] = [600] trace['T;location_dend1'] = bap1_time trace['V;location_dend1'] = bap1_voltage trace['stim_start;location_dend1'] = [295] trace['stim_end;location_dend1'] = [500] trace['T;location_dend2'] = bap2_time trace['V;location_dend2'] = bap2_voltage trace['stim_start;location_dend2'] = [295] trace['stim_end;location_dend2'] = [500] trace_db['T'] = db_time trace_db['V'] = db_voltage trace_db['stim_start'] = [419.995] trace_db['stim_end'] = [1419.995] bpap_featurenames = [ 'BPAPHeightLoc1', 'BPAPHeightLoc2', 'BPAPAmplitudeLoc1', 'BPAPAmplitudeLoc2'] bac_featurenames = [ 'BAC_width'] db_featurenames = [ 'depol_block'] soma_featurenames = all_featurenames[:] for feature_name in bpap_featurenames: soma_featurenames.remove(feature_name) for feature_name in bac_featurenames: soma_featurenames.remove(feature_name) for feature_name in db_featurenames: soma_featurenames.remove(feature_name) import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") feature_values = efel.getFeatureValues([trace], soma_featurenames)[0] with warnings.catch_warnings(): warnings.simplefilter("ignore") feature_values = dict( list(feature_values.items()) + list(efel.getFeatureValues( [trace_db], db_featurenames)[0].items())) with warnings.catch_warnings(): warnings.simplefilter("ignore") efel.setThreshold(-30) feature_values = dict( list(feature_values.items()) + list(efel.getFeatureValues( [trace], bpap_featurenames)[0].items())) with warnings.catch_warnings(): warnings.simplefilter("ignore") efel.setThreshold(-55) feature_values = dict( list(feature_values.items()) + list(efel.getFeatureValues( [trace], bac_featurenames)[0].items())) for feature_name in feature_values: if feature_values[feature_name] is not None: feature_values[feature_name] = list( feature_values[feature_name]) return feature_values
def get_allfeature_values(): """Get back all the feature names and value""" import efel efel.reset() import numpy all_featurenames = efel.getFeatureNames() soma_data = numpy.loadtxt(os.path.join(testdata_dir, 'testdata.txt')) soma_time = soma_data[:, 0] soma_voltage = soma_data[:, 1] bac_data = numpy.loadtxt(os.path.join(testdata_dir, 'testbacdata.txt')) bac_time = bac_data[:, 0] bac_voltage = bac_data[:, 1] bap1_data = numpy.loadtxt(os.path.join(testdata_dir, 'testbap1data.txt')) bap1_time = bap1_data[:, 0] bap1_voltage = bap1_data[:, 1] bap2_data = numpy.loadtxt(os.path.join(testdata_dir, 'testbap2data.txt')) bap2_time = bap2_data[:, 0] bap2_voltage = bap2_data[:, 1] trace = {} trace['T'] = soma_time trace['V'] = soma_voltage trace['stim_start'] = [700] trace['stim_end'] = [2700] trace['T;location_AIS'] = soma_time trace['V;location_AIS'] = soma_voltage trace['stim_start;location_AIS'] = [700] trace['stim_end;location_AIS'] = [2700] trace['T;location_epsp'] = bac_time trace['V;location_epsp'] = bac_voltage trace['stim_start;location_epsp'] = [295] trace['stim_end;location_epsp'] = [600] trace['T;location_dend1'] = bap1_time trace['V;location_dend1'] = bap1_voltage trace['stim_start;location_dend1'] = [295] trace['stim_end;location_dend1'] = [500] trace['T;location_dend2'] = bap2_time trace['V;location_dend2'] = bap2_voltage trace['stim_start;location_dend2'] = [295] trace['stim_end;location_dend2'] = [500] bpap_featurenames = [ 'BPAPHeightLoc1', 'BPAPHeightLoc2', 'BPAPAmplitudeLoc1', 'BPAPAmplitudeLoc2' ] bac_featurenames = ['BAC_width'] soma_featurenames = all_featurenames[:] for feature_name in bpap_featurenames: soma_featurenames.remove(feature_name) for feature_name in bac_featurenames: soma_featurenames.remove(feature_name) import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") feature_values = efel.getFeatureValues([trace], soma_featurenames)[0] with warnings.catch_warnings(): warnings.simplefilter("ignore") efel.setThreshold(-30) feature_values = dict( list(feature_values.items()) + list(efel.getFeatureValues([trace], bpap_featurenames)[0].items())) with warnings.catch_warnings(): warnings.simplefilter("ignore") efel.setThreshold(-55) feature_values = dict( list(feature_values.items()) + list(efel.getFeatureValues([trace], bac_featurenames)[0].items())) for feature_name in feature_values: if feature_values[feature_name] is not None: feature_values[feature_name] = list(feature_values[feature_name]) return feature_values
#*It should be used in conjunction with the HEKAv2 MATLAB script #Install and Import these scripts import scipy.io import efel import numpy as np from tkinter import filedialog from matplotlib import pyplot as plt #Enable EFEL Settings efel.setDoubleSetting( 'interp_step', 0.05) #Must be set to the sampling rate (20,000 Hz = 0.05) efel.setDoubleSetting('voltage_base_start_perc', 0.1) efel.setDoubleSetting('voltage_base_end_perc', 0.8) efel.setDerivativeThreshold(10) #Value can be changed, but normally set to 10 efel.setThreshold(0) #Create global variable trace_results = dict() #Opens a dialog box allowing the user to select the file and removes the extension from the file name & load in the file file_path = filedialog.askopenfilename() file = file_path[:-4] mat = scipy.io.loadmat(file_path) #Depending upon the number of cells present, split the data into a list of numpy arrays based on the number of experiments print('There are ' + str(int(mat['CellNum'])) + ' cells in this file.') if mat['CellNum'] == 1: Cell1 = list(np.hsplit(mat['C1data'], int(mat['C1Expts']))) print('Cell #1 has ' + str(int(mat['C1Expts'])) + ' experiments.')
def FFun_for_Features(self, mod_t, features_data, feature, k, args={}): exp_mean = 0 exp_std = 0 # generating time trace for efel t_gen = frange( 0, self.option.run_controll_tstop + self.option.run_controll_dt, self.option.run_controll_dt) t = [] for n in t_gen: t.append(n) t = t[0:len(mod_t)] # converting list to numpy array for efel mod_t_np = np.array(mod_t) t_np = np.array(t) temp_fit = 0 trace = {} traces = [] trace['T'] = t_np trace['V'] = mod_t_np trace['stim_start'] = [self.option.stim_del] trace['stim_end'] = [self.option.stim_del + self.option.stim_dur] traces.append(trace) efel.setThreshold(self.thres) ''' used_features=list(features) AP_num = efel.getFeatureValues(traces,['Spikecount']) for i in range (len(features)): if AP_num[0]['Spikecount'] == 0 and features_data[features[i]]["requires_spike"]=="True": used_features.remove(features[i]) temp_fit += 10 #MEnnyi legyen a bunti? if AP_num[0]['Spikecount'] <2 and "ISI" in features_data[features[i]]: used_features.remove(features[i]) temp_fit += 1 ''' #traces_results = efel.getFeatureValues(traces,[feature]) #list traces_results = self.get_efel_values(traces, feature) #print traces_results exp_mean = features_data[feature]["mean"][k] exp_std = features_data[feature]["std"][k] mod_result = traces_results[0][feature] #print feature #print "exp mean", exp_mean #print "exp std", exp_std #print "model", mod_result if mod_result is not None and mod_result.size > 1: mod_result = scipy.mean( mod_result ) #for features (AHP_depth, AP_duration_half_width) that gives a list as a result, the mean of the results is used elif mod_result is not None and mod_result.size != 0: # mod_result is a numpy array with one element, 0 element for AP1_amp or APlast_amp if no AP generated by the model mod_result = mod_result[0] if (mod_result == None or mod_result == 0 or mod_result.size == 0) and (exp_mean != None and exp_std != None): temp_fit = 250 elif exp_mean == None and exp_std == None: temp_fit = 0 #elif exp_mean != None and exp_std != None : else: result = abs(exp_mean - mod_result) / exp_std temp_fit = result #print temp_fit return temp_fit
from scipy.io import loadmat, matlab import efel import pandas as pd import numpy as np from tkinter import filedialog from matplotlib import pyplot as plt from csv import DictWriter import csv from os import chdir, getcwd #Enable EFEL Settings efel.setDoubleSetting('interp_step',0.05) #Must be set to the sampling rate (20,000 Hz = 0.05) efel.setDoubleSetting('voltage_base_start_perc',0.1) efel.setDoubleSetting('voltage_base_end_perc',0.8) efel.setDerivativeThreshold(10) #Value can be changed, but normally set to 10 efel.setThreshold(10) #Create global variable to store parameter information Results = list() trace_calc = dict() User_choices = dict() Cell = dict() #Function that loads nested mat structures into a dictionary instead of using dot notation to #extract individual pieces of data def load_mat(filename): """ This function should be called instead of direct scipy.io.loadmat as it cures the problem of not properly recovering python dictionaries from mat files. It calls the function check keys to cure all entries which are still mat-objects
def FFun_for_Features(self, mod_t, features_data, feature, k, args={}): exp_mean = 0 exp_std = 0 # generating time trace for efel t_gen = frange(0, self.option.run_controll_tstop + self.option.run_controll_dt, self.option.run_controll_dt) t = [] for n in t_gen: t.append(n) t = t[0:len(mod_t)] # converting list to numpy array for efel mod_t_np=np.array(mod_t) t_np=np.array(t) temp_fit=0 trace = {} traces=[] trace['T'] = t_np trace['V'] = mod_t_np trace['stim_start'] = [self.option.stim_del] trace['stim_end'] = [self.option.stim_del + self.option.stim_dur] traces.append(trace) efel.setThreshold(self.thres) ''' used_features=list(features) AP_num = efel.getFeatureValues(traces,['Spikecount']) for i in range (len(features)): if AP_num[0]['Spikecount'] == 0 and features_data[features[i]]["requires_spike"]=="True": used_features.remove(features[i]) temp_fit += 10 #MEnnyi legyen a bunti? if AP_num[0]['Spikecount'] <2 and "ISI" in features_data[features[i]]: used_features.remove(features[i]) temp_fit += 1 ''' #traces_results = efel.getFeatureValues(traces,[feature]) #list traces_results = self.get_efel_values(traces, feature) #print traces_results exp_mean = features_data[feature]["mean"][k] exp_std = features_data[feature]["std"][k] mod_result=traces_results[0][feature] #print feature #print "exp mean", exp_mean #print "exp std", exp_std #print "model", mod_result if mod_result is not None and mod_result.size > 1: mod_result = scipy.mean(mod_result) #for features (AHP_depth, AP_duration_half_width) that gives a list as a result, the mean of the results is used elif mod_result is not None and mod_result.size!=0: # mod_result is a numpy array with one element, 0 element for AP1_amp or APlast_amp if no AP generated by the model mod_result=mod_result[0] if (mod_result == None or mod_result == 0 or mod_result.size==0) and (exp_mean != None and exp_std != None): temp_fit=250 elif exp_mean == None and exp_std == None : temp_fit=0 #elif exp_mean != None and exp_std != None : else: result = abs(exp_mean - mod_result) / exp_std temp_fit = result #print temp_fit return temp_fit
def three_feature_sets_on_static_models(model, unit_test=False, challenging=False): ''' Conventions: variables ending with 15 refer to 1.5 current injection protocols. variables ending with 30 refer to 3.0 current injection protocols. Inputs: NML-DB models, a method designed to be called inside an iteration loop, where a list of models is iterated over, and on each iteration a new model is supplied to this method. Outputs: A dictionary of dataframes, for features sought according to: Druckman, EFEL, AllenSDK ''' ## # wrangle data in preperation for computing # Allen Features ## #import pdb; pdb.set_trace() if type(model) is type(DataTC()): model = model.dtc_to_model() if not hasattr(model, 'vm30'): model.inject_square_current(model.rheobase * 3.0) model.vm30 = model.get_membrane_potential() ['amplitude'] model.inject_square_current() model.vm15 = model.get_membrane_potential() times = np.array([float(t) for t in model.vm30.times]) volts = np.array([float(v) for v in model.vm30]) try: import asciiplotlib as apl fig = apl.figure() fig.plot(times, volts, label="V_{m} (mV), versus time (ms)", width=100, height=80) fig.show() except: pass ## # Allen Features ## #frame_shape,frame_dynamics,per_spike_info, meaned_features_overspikes all_allen_features30, allen_features30 = allen_format( volts, times, optional_vm=model.vm30) #if frame30 is not None: # frame30['protocol'] = 3.0 ## # wrangle data in preperation for computing # Allen Features ## times = np.array([float(t) for t in model.vm15.times]) volts = np.array([float(v) for v in model.vm15]) ## # Allen Features ## all_allen_features15, allen_features15 = allen_format( volts, times, optional_vm=model.vm15) ## # Get Druckman features, this is mainly handled in external files. ## #if model.ir_currents DMTNMLO = dm_test_interoperable.DMTNMLO() if hasattr(model, 'druckmann2013_input_resistance_currents') and not hasattr( model, 'allen'): DMTNMLO.test_setup(None, None, model=model) else: DMTNMLO.test_setup(None, None, model=model, ir_current_limited=True) dm_test_features = DMTNMLO.runTest() ## # Wrangle data to prepare for EFEL feature calculation. ## trace3 = {} trace3['T'] = [float(t) for t in model.vm30.times.rescale('ms')] trace3['V'] = [float(v) for v in model.vm30.magnitude] #temp_vm trace3['stimulus_current'] = [model.druckmann2013_strong_current] if not hasattr(model, 'allen'): trace3['stim_end'] = [trace3['T'][-1]] trace3['stim_start'] = [float(model.protocol['Time_Start'])] else: trace3['stim_end'] = [float(model.protocol['Time_End']) * 1000.0] trace3['stim_start'] = [float(model.protocol['Time_Start']) * 1000.0] traces3 = [ trace3 ] # Now we pass 'traces' to the efel and ask it to calculate the feature# values trace15 = {} trace15['T'] = [float(t) for t in model.vm15.times.rescale('ms')] trace15['V'] = [float(v) for v in model.vm15.magnitude] #temp_vm if not hasattr(model, 'allen'): trace15['stim_end'] = [trace15['T'][-1]] trace15['stim_start'] = [float(model.protocol['Time_Start'])] else: trace15['stim_end'] = [float(model.protocol['Time_End']) * 1000.0] trace15['stim_start'] = [float(model.protocol['Time_Start']) * 1000.0] trace15['stimulus_current'] = [model.druckmann2013_standard_current] trace15['stim_end'] = [trace15['T'][-1]] traces15 = [ trace15 ] # Now we pass 'traces' to the efel and ask it to calculate the feature# values ## # Compute # EFEL features (HBP) ## efel.reset() if len(threshold_detection(model.vm15, threshold=0)): #pass threshold = float( np.max(model.vm15.magnitude) - 0.5 * np.abs(np.std(model.vm15.magnitude))) print(len(threshold_detection(model.vm15, threshold=threshold))) print(threshold, 'threshold', np.max(model.vm15.magnitude), np.min(model.vm15.magnitude)) #efel_15 = efel.getMeanFeatureValues(traces15,list(efel.getFeatureNames()))# else: threshold = float( np.max(model.vm15.magnitude) - 0.2 * np.abs(np.std(model.vm15.magnitude))) efel.setThreshold(threshold) print(len(threshold_detection(model.vm15, threshold=threshold))) print(threshold, 'threshold', np.max(model.vm15.magnitude)) if np.min(model.vm15.magnitude) < 0: efel_15 = efel.getMeanFeatureValues(traces15, list(efel.getFeatureNames())) else: efel_15 = None efel.reset() if len(threshold_detection(model.vm30, threshold=0)): threshold = float( np.max(model.vm30.magnitude) - 0.5 * np.abs(np.std(model.vm30.magnitude))) print(len(threshold_detection(model.vm30, threshold=threshold))) print(threshold, 'threshold', np.max(model.vm30.magnitude), np.min(model.vm30.magnitude)) #efel_30 = efel.getMeanFeatureValues(traces3,list(efel.getFeatureNames())) else: threshold = float( np.max(model.vm30.magnitude) - 0.2 * np.abs(np.std(model.vm30.magnitude))) efel.setThreshold(threshold) print(len(threshold_detection(model.vm15, threshold=threshold))) print(threshold, 'threshold', np.max(model.vm15.magnitude)) if np.min(model.vm30.magnitude) < 0: efel_30 = efel.getMeanFeatureValues(traces3, list(efel.getFeatureNames())) else: efel_30 = None efel.reset() if hasattr(model, 'information'): return { 'model_id': model.name, 'model_information': model.information, 'efel_15': efel_15, 'efel_30': efel_30, 'dm': dm_test_features, 'allen_15': all_allen_features15, 'allen_30': all_allen_features30 } else: return { 'model_id': model.name, 'model_information': 'allen_data', 'efel_15': efel_15, 'efel_30': efel_30, 'dm': dm_test_features, 'allen_15': all_allen_features15, 'allen_30': all_allen_features30 }