def run(self): experiment = self.pipeline.experiment events = self.get_passed_object(experiment+'_events') tal_info = self.get_passed_object('tal_info') prob_pre = self.get_passed_object('prob_pre') prob_diff = self.get_passed_object('prob_diff') ctrl_prob_pre = self.get_passed_object('ctrl_prob_pre') ctrl_prob_diff0 = self.get_passed_object('ctrl_prob_diff0') ctrl_prob_diff250 = ctrl_prob_diff500 = ctrl_prob_diff1000 = None try: ctrl_prob_diff250 = self.get_passed_object('ctrl_prob_diff250') except: pass try: ctrl_prob_diff500 = self.get_passed_object('ctrl_prob_diff500') except: pass try: ctrl_prob_diff1000 = self.get_passed_object('ctrl_prob_diff1000') except: pass sessions = np.unique(events.session) self.pass_object('NUMBER_OF_SESSIONS', len(sessions)) self.pass_object('NUMBER_OF_ELECTRODES', len(tal_info)) session_data = [] all_durs_ev = np.array([]) all_amps_ev = np.array([]) all_freqs_ev = np.array([]) all_burfs_ev = np.array([]) all_durs = np.array([]) all_amps = np.array([]) all_freqs = np.array([]) all_burfs = np.array([]) session_summary_array = [] prob_array_idx = 0 for session in sessions: session_summary = SessionSummary() session_summary.sess_num = session session_events = events[events.session == session] n_sess_events = len(session_events) timestamps = session_events.mstime first_time_stamp = np.min(timestamps) last_time_stamp = np.max(timestamps) session_length = '%.2f' % ((last_time_stamp - first_time_stamp) / 60000.0) session_date = time.strftime('%d-%b-%Y', time.localtime(last_time_stamp/1000)) session_data.append([session, session_date, session_length]) session_name = 'Sess%02d' % session stim_tag = session_events[0].stimAnodeTag + '-' + session_events[0].stimCathodeTag isi_min = np.nanmin(session_events.isi) isi_max = np.nanmax(session_events.isi) isi_mid = (isi_max+isi_min) / 2.0 isi_halfrange = isi_max - isi_mid print 'Session =', session_name, ' StimTag =', stim_tag, ' ISI =', isi_mid, '+/-', isi_halfrange durs_ev = session_events.pulse_duration amps_ev = session_events.amplitude freqs_ev = session_events.pulse_frequency burfs_ev = session_events.burst_frequency all_durs_ev = np.hstack((all_durs_ev, durs_ev)) all_amps_ev = np.hstack((all_amps_ev, amps_ev)) all_freqs_ev = np.hstack((all_freqs_ev, freqs_ev)) all_burfs_ev = np.hstack((all_burfs_ev, burfs_ev)) durs = np.unique(durs_ev) amps = np.unique(amps_ev) freqs = np.unique(freqs_ev) burfs = np.unique(burfs_ev) session_summary.name = session_name session_summary.length = session_length session_summary.date = session_date session_summary.stimtag = stim_tag session_summary.isi_mid = isi_mid session_summary.isi_half_range = isi_halfrange session_prob_pre = prob_pre[prob_array_idx:prob_array_idx+n_sess_events] session_prob_diff = prob_diff[prob_array_idx:prob_array_idx+n_sess_events] session_ctrl_prob_pre = ctrl_prob_pre session_ctrl_prob_diff = ctrl_prob_diff1000 if durs[-1]==1000 else ctrl_prob_diff500 if durs[-1]==500 else ctrl_prob_diff250 all_durs = np.hstack((all_durs, durs)) all_amps = np.hstack((all_amps, amps)) all_freqs = np.hstack((all_freqs, freqs)) all_burfs = np.hstack((all_burfs, burfs)) ev_vals = None param_grid = None if experiment == 'PS1': ev_vals = [freqs_ev, durs_ev] param_grid = [freqs, durs] session_summary.constant_name = 'Amplitude' session_summary.constant_value = amps[0] session_summary.constant_unit = 'mA' session_summary.parameter1 = 'Pulse Frequency' session_summary.parameter2 = 'Duration' elif experiment == 'PS2': ev_vals = [freqs_ev, amps_ev] param_grid = [freqs, amps] session_summary.constant_name = 'Duration' session_summary.constant_value = durs[-1] session_summary.constant_unit = 'ms' session_summary.parameter1 = 'Pulse Frequency' session_summary.parameter2 = 'Amplitude' elif experiment == 'PS3': ev_vals = [freqs_ev, burfs_ev] param_grid = [freqs, burfs] session_summary.constant_name = 'Amplitude' session_summary.constant_value = amps[0] session_summary.constant_unit = 'mA' session_summary.parameter1 = 'Pulse Frequency' session_summary.parameter2 = 'Burst Frequency' delta_stats = DeltaStats(2, ev_vals, param_grid, session_prob_pre, session_prob_diff, session_ctrl_prob_pre, session_ctrl_prob_diff, 1.0/3.0) delta_stats.run() anova = anova_test(ev_vals, param_grid, session_prob_diff) if anova is not None: session_summary.anova_fvalues = anova[0] session_summary.anova_pvalues = anova[1] data_point_indexes_left = np.arange(1,len(param_grid[0])+2) data_point_indexes_right = np.arange(1,len(param_grid[1])+2) # computting y axis limits min_plot, max_plot = delta_stats.y_range() ylim = [min_plot-0.1*(max_plot-min_plot), max_plot+0.1*(max_plot-min_plot)] x_tick_labels = ['CONTROL'] + [x if x>0 else 'PULSE' for x in param_grid[0]] session_summary.plot_data_dict[(0,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_all[0], yerr=delta_stats.stdev_all[0], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(1,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_low[0], yerr=delta_stats.stdev_low[0], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(2,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_high[0], yerr=delta_stats.stdev_high[0], x_tick_labels=x_tick_labels, ylim=ylim) x_tick_labels = ['CONTROL'] + list(param_grid[1]) session_summary.plot_data_dict[(0,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_all[1], yerr=delta_stats.stdev_all[1], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(1,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_low[1], yerr=delta_stats.stdev_low[1], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(2,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_high[1], yerr=delta_stats.stdev_high[1], x_tick_labels=x_tick_labels, ylim=ylim) session_summary_array.append(session_summary) prob_array_idx += len(session_events) self.pass_object('SESSION_DATA', session_data) self.pass_object('session_summary_array', session_summary_array) isi_min = np.nanmin(events.isi) isi_max = np.nanmax(events.isi) isi_mid = (isi_max+isi_min) / 2.0 isi_halfrange = isi_max - isi_mid print 'ISI =', isi_mid, '+/-', isi_halfrange self.pass_object('CUMULATIVE_ISI_MID',isi_mid) self.pass_object('CUMULATIVE_ISI_HALF_RANGE',isi_halfrange) durs = np.unique(all_durs) amps = np.unique(all_amps) freqs = np.unique(all_freqs) burfs = np.unique(all_burfs) ctrl_prob_diff = ctrl_prob_diff1000 if durs[-1]==1000 else ctrl_prob_diff500 if durs[-1]==500 else ctrl_prob_diff250 ev_vals = None param_grid = None if experiment == 'PS1': ev_vals = [all_freqs_ev, all_durs_ev] param_grid = [freqs, durs] self.pass_object('CUMULATIVE_PARAMETER1', 'Pulse Frequency') self.pass_object('CUMULATIVE_PARAMETER2', 'Duration') elif experiment == 'PS2': ev_vals = [all_freqs_ev, all_amps_ev] param_grid = [freqs, amps] self.pass_object('CUMULATIVE_PARAMETER1', 'Pulse Frequency') self.pass_object('CUMULATIVE_PARAMETER2', 'Amplitude') elif experiment == 'PS3': ev_vals = [all_freqs_ev, all_burfs_ev] param_grid = [freqs, burfs] self.pass_object('CUMULATIVE_PARAMETER1', 'Pulse Frequency') self.pass_object('CUMULATIVE_PARAMETER2', 'Burst Frequency') delta_stats = DeltaStats(2, ev_vals, param_grid, prob_pre, prob_diff, ctrl_prob_pre, ctrl_prob_diff, 1.0/3.0) delta_stats.run() data_point_indexes_left = np.arange(1,len(param_grid[0])+2) data_point_indexes_right = np.arange(1,len(param_grid[1])+2) # computing y axis limits min_plot, max_plot = delta_stats.y_range() ylim = [min_plot-0.1*(max_plot-min_plot), max_plot+0.1*(max_plot-min_plot)] cumulative_plot_data_dict = OrderedDict() x_tick_labels = ['CONTROL'] + [x if x>0 else 'PULSE' for x in param_grid[0]] cumulative_plot_data_dict[(0,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_all[0], yerr=delta_stats.stdev_all[0], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(1,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_low[0], yerr=delta_stats.stdev_low[0], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(2,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_high[0], yerr=delta_stats.stdev_high[0], x_tick_labels=x_tick_labels, ylim=ylim) x_tick_labels = ['CONTROL'] + list(param_grid[1]) cumulative_plot_data_dict[(0,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_all[1], yerr=delta_stats.stdev_all[1], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(1,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_low[1], yerr=delta_stats.stdev_low[1], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(2,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_high[1], yerr=delta_stats.stdev_high[1], x_tick_labels=x_tick_labels, ylim=ylim) self.pass_object('cumulative_plot_data_dict', cumulative_plot_data_dict)
def run(self): subject_id = self.pipeline.subject_id experiment = self.pipeline.experiment PostStimBuff = 50 # buffer in ms to leave following stim offset w_dir = self.get_workspace_dir() paramsPS = deserialize_single_object_from_matlab_format(join(w_dir,'paramsPS.mat'),'params') bpFull = deserialize_single_object_from_matlab_format(join(w_dir,'bpFull.mat'),'bpFull') bp = deserialize_single_object_from_matlab_format(join(w_dir,'bp.mat'),'bp') Weights = deserialize_single_object_from_matlab_format(join(w_dir,'Weights.mat'),'Weights') ps_events = deserialize_single_object_from_matlab_format(join(w_dir,'PSEvents.mat'),'PSEvents') ps_events_size = len(ps_events) for ev in ps_events: ev.ISI = np.nan for i in xrange(1, ps_events_size): ev_curr = ps_events[i] ev_prev = ps_events[i-1] if ev_curr.session == ev_prev.session: if is_stim_event(ev_curr) and is_stim_event(ev_prev): ev_curr.ISI = ev_curr.mstime - ev_prev.mstime ps_events = [ev for ev in ps_events if is_stim_event(ev)] print 'stim events size = ', len(ps_events) ps_sessions = np.unique([e.session for e in ps_events]) # print 'ps_sessions=',ps_sessions self.pass_object('NUMBER_OF_SESSIONS',len(ps_sessions)) self.pass_object('NUMBER_OF_ELECTRODES',len(bp)) session_data = [] all_durs_ev = np.array([]) all_amps_ev = np.array([]) all_freqs_ev = np.array([]) all_burfs_ev = np.array([]) all_durs = np.array([]) all_amps = np.array([]) all_freqs = np.array([]) all_burfs = np.array([]) ProbPostAllSessions = np.array([]) ProbPreAllSessions = np.array([]) ProbDiffAllSessions = np.array([]) session_summary_array = [] for SessNum in ps_sessions: session_summary = SessionSummary() # object that contains all the report related information for a given session SessEv = [ev for ev in ps_events if ev.session == SessNum] timestamps = [ev.mstime for ev in SessEv] firstTimestamp = min(timestamps) lastTimestamp = max(timestamps) SessLength = (lastTimestamp-firstTimestamp)/60000.0 SessDate = time.strftime('%Y-%m-%d', time.localtime(lastTimestamp/1000)) session_data.append([SessNum, SessDate, SessLength]) SessName = 'Sess%02d' % SessNum StimTag = SessEv[0].stimAnodeTag + '-' + SessEv[0].stimCathodeTag #self.pipeline.add_object_to_pass('STIMTAG',StimTag) ISI_min = np.nanmin([ev.ISI for ev in SessEv]) ISI_max = np.nanmax([ev.ISI for ev in SessEv]) ISI_mid = (ISI_max+ISI_min) / 2.0 ISI_halfrange = ISI_max - ISI_mid print 'Session =', SessName, ' StimTag =', StimTag, ' ISI =', ISI_mid, '+/-', ISI_halfrange durs_ev = [s.pulse_duration for s in SessEv] amps_ev = [s.amplitude for s in SessEv] freqs_ev = [s.pulse_frequency for s in SessEv] burfs_ev = [s.burst_frequency for s in SessEv] all_durs_ev = np.hstack((all_durs_ev, durs_ev)) all_amps_ev = np.hstack((all_amps_ev, amps_ev)) all_freqs_ev = np.hstack((all_freqs_ev, freqs_ev)) all_burfs_ev = np.hstack((all_burfs_ev, burfs_ev)) durs = np.unique(durs_ev) amps = np.unique(amps_ev) freqs = np.unique(freqs_ev) burfs = np.unique(burfs_ev) session_summary.name = SessName session_summary.length = SessLength session_summary.date = SessDate session_summary.stimtag = StimTag session_summary.isi_mid = ISI_mid session_summary.isi_half_range = ISI_halfrange all_durs = np.hstack((all_durs, durs)) all_amps = np.hstack((all_amps, amps)) all_freqs = np.hstack((all_freqs, freqs)) all_burfs = np.hstack((all_burfs, burfs)) StimOnBin = np.ones(len(SessEv), dtype=np.int)*paramsPS.pow.onsetInd PreStimInds = npm.repmat(paramsPS.pow.baseBins-1,len(SessEv),1); PostStimBin = np.empty_like(StimOnBin, dtype=np.int) PostStimInds = np.empty((len(PostStimBin),len(paramsPS.pow.baseBins)), dtype=np.int) for iEv in xrange(len(SessEv)): dur = SessEv[iEv].pulse_duration if dur == -999: dur = durs[-1] PostStimBin[iEv] = indices(paramsPS.pow.timeBins[:,0], lambda x: x <= dur+PostStimBuff)[-1] PostStimInds[iEv,:] = range(PostStimBin[iEv], PostStimBin[iEv]+len(paramsPS.pow.baseBins)) DataMat_PostStim = np.empty((50,len(bp),len(SessEv))) DataMat_PreStim = np.empty((50,len(bp),len(SessEv))) workspace_dir = self.get_workspace_dir() for iElec in xrange(len(bp)): power_file_name = abspath(join(workspace_dir,'power',subject_id, SessName,'%d-%d_Pow_bin_zs.mat'%(bp[iElec].channel[0],bp[iElec].channel[1]))) print power_file_name bp_session_reader = MatlabIO() bp_session_reader.deserialize(power_file_name) PowMat = bp_session_reader.PowMat pattern_PostStim = np.empty((50,len(SessEv))) pattern_PreStim = np.empty((50,len(SessEv))) for iEv in xrange(len(SessEv)): pattern_PostStim[:, iEv] = np.nanmean(PowMat[:, PostStimInds[iEv,:],iEv],1) pattern_PreStim[:, iEv] = np.nanmean(PowMat[:, PreStimInds[iEv,:],iEv],1) DataMat_PostStim[:,iElec,:] = pattern_PostStim DataMat_PreStim[:,iElec,:] = pattern_PreStim DataMat_PostStim = DataMat_PostStim.reshape(50*len(bp),len(SessEv), order='F') DataMat_PreStim = DataMat_PreStim.reshape(50*len(bp),len(SessEv), order='F') W = np.ravel(Weights.MeanCV) # classifier Beta's # Beta_0 Weights.MeanIntercept ProbPost = logit(Weights.MeanIntercept, W, DataMat_PostStim) ProbPre = logit(Weights.MeanIntercept, W, DataMat_PreStim) ProbDiff = ProbPost - ProbPre ProbPostAllSessions = np.hstack((ProbPostAllSessions, ProbPost)) ProbPreAllSessions = np.hstack((ProbPreAllSessions, ProbPre)) ProbDiffAllSessions = np.hstack((ProbDiffAllSessions, ProbDiff)) ev_vals = None param_grid = None if experiment == 'PS1': ev_vals = [freqs_ev, durs_ev] param_grid = [freqs, durs] session_summary.constant_name = 'Amplitude' session_summary.constant_value = amps[0] session_summary.constant_unit = 'mA' session_summary.parameter1 = 'Pulse Frequency' session_summary.parameter2 = 'Duration' elif experiment == 'PS2': ev_vals = [freqs_ev, amps_ev] param_grid = [freqs, amps] session_summary.constant_name = 'Duration' session_summary.constant_value = durs[-1] session_summary.constant_unit = 'ms' session_summary.parameter1 = 'Pulse Frequency' session_summary.parameter2 = 'Amplitude' elif experiment == 'PS3': ev_vals = [freqs_ev, burfs_ev] param_grid = [freqs, burfs] session_summary.constant_name = 'Amplitude' session_summary.constant_value = amps[0] session_summary.constant_unit = 'mA' session_summary.parameter1 = 'Pulse Frequency' session_summary.parameter2 = 'Burst Frequency' delta_stats = DeltaStats(2, ev_vals, param_grid, ProbPre, ProbDiff, 1.0/3.0) delta_stats.run() data_point_indexes_left = np.arange(1,len(param_grid[0])+1) data_point_indexes_right = np.arange(1,len(param_grid[1])+1) # computting y axis limits min_plot, max_plot = delta_stats.y_range() ylim = [min_plot-0.1*(max_plot-min_plot), max_plot+0.1*(max_plot-min_plot)] x_tick_labels = [x if x>0 else 'PULSE' for x in param_grid[0]] session_summary.plot_data_dict[(0,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_all[0], yerr=delta_stats.stdev_all[0], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(1,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_low[0], yerr=delta_stats.stdev_low[0], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(2,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_high[0], yerr=delta_stats.stdev_high[0], x_tick_labels=x_tick_labels, ylim=ylim) x_tick_labels = param_grid[1] session_summary.plot_data_dict[(0,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_all[1], yerr=delta_stats.stdev_all[1], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(1,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_low[1], yerr=delta_stats.stdev_low[1], x_tick_labels=x_tick_labels, ylim=ylim) session_summary.plot_data_dict[(2,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_high[1], yerr=delta_stats.stdev_high[1], x_tick_labels=x_tick_labels, ylim=ylim) session_summary_array.append(session_summary) self.pass_object('SESSION_DATA',session_data) self.pass_object('session_summary_array',session_summary_array) ISI_min = np.nanmin([ev.ISI for ev in ps_events]) ISI_max = np.nanmax([ev.ISI for ev in ps_events]) ISI_mid = (ISI_max+ISI_min) / 2.0 ISI_halfrange = ISI_max - ISI_mid print 'ISI =', ISI_mid, '+/-', ISI_halfrange self.pass_object('CUMULATIVE_ISI_MID',ISI_mid) self.pass_object('CUMULATIVE_ISI_HALF_RANGE',ISI_halfrange) durs = np.unique(all_durs) amps = np.unique(all_amps) freqs = np.unique(all_freqs) burfs = np.unique(all_burfs) ev_vals = None param_grid = None if experiment == 'PS1': ev_vals = [all_freqs_ev, all_durs_ev] param_grid = [freqs, durs] self.pass_object('CUMULATIVE_PARAMETER1', 'Pulse Frequency') self.pass_object('CUMULATIVE_PARAMETER2', 'Duration') elif experiment == 'PS2': ev_vals = [all_freqs_ev, all_amps_ev] param_grid = [freqs, amps] self.pass_object('CUMULATIVE_PARAMETER1', 'Pulse Frequency') self.pass_object('CUMULATIVE_PARAMETER2', 'Amplitude') elif experiment == 'PS3': ev_vals = [all_freqs_ev, all_burfs_ev] param_grid = [freqs, burfs] self.pass_object('CUMULATIVE_PARAMETER1', 'Pulse Frequency') self.pass_object('CUMULATIVE_PARAMETER2', 'Burst Frequency') delta_stats = DeltaStats(2, ev_vals, param_grid, ProbPreAllSessions, ProbDiffAllSessions, 1.0/3.0) delta_stats.run() data_point_indexes_left = np.arange(1,len(param_grid[0])+1) data_point_indexes_right = np.arange(1,len(param_grid[1])+1) # computing y axis limits min_plot, max_plot = delta_stats.y_range() ylim = [min_plot-0.1*(max_plot-min_plot), max_plot+0.1*(max_plot-min_plot)] cumulative_plot_data_dict = OrderedDict() x_tick_labels = [x if x>0 else 'PULSE' for x in param_grid[0]] cumulative_plot_data_dict[(0,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_all[0], yerr=delta_stats.stdev_all[0], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(1,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_low[0], yerr=delta_stats.stdev_low[0], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(2,0)] = PlotData(x=data_point_indexes_left, y=delta_stats.mean_high[0], yerr=delta_stats.stdev_high[0], x_tick_labels=x_tick_labels, ylim=ylim) x_tick_labels = param_grid[1] cumulative_plot_data_dict[(0,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_all[1], yerr=delta_stats.stdev_all[1], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(1,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_low[1], yerr=delta_stats.stdev_low[1], x_tick_labels=x_tick_labels, ylim=ylim) cumulative_plot_data_dict[(2,1)] = PlotData(x=data_point_indexes_right, y=delta_stats.mean_high[1], yerr=delta_stats.stdev_high[1], x_tick_labels=x_tick_labels, ylim=ylim) self.pass_object('cumulative_plot_data_dict', cumulative_plot_data_dict)