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 = self.pipeline.subject
        task = self.pipeline.task

        events = self.get_passed_object(task + '_events')
        math_events = self.get_passed_object(task + '_math_events')
        intr_events = self.get_passed_object(task + '_intr_events')
        rec_events = self.get_passed_object(task + '_rec_events')
        all_events = self.get_passed_object(task + '_all_events')
        channels = self.get_passed_object('channels')
        tal_info = self.get_passed_object('tal_info')

        sessions = np.unique(events.session)

        self.pass_object('NUMBER_OF_SESSIONS', len(sessions))
        self.pass_object('NUMBER_OF_ELECTRODES', len(channels))

        session_data = []
        session_summary_array = []

        positions = np.unique(events.serialpos)
        first_recall_counter = np.zeros(positions.size, dtype=int)
        total_list_counter = 0

        irt_within_cat = []
        irt_between_cat = []

        cumulative_n_items_from_stim = 0
        cumulative_n_recalls_from_stim = 0
        cumulative_n_intr_from_stim = 0

        cumulative_n_items_from_nonstim = 0
        cumulative_n_recalls_from_nonstim = 0
        cumulative_n_intr_from_nonstim = 0

        for session in sessions:
            session_summary = SessionSummary()

            session_events = events[events.session == session]
            n_sess_events = len(session_events)

            session_rec_events = rec_events[rec_events.session == session]

            session_all_events = all_events[all_events.session == session]
            timestamps = session_all_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

            print 'Session =', session_name

            session_summary.number = session
            session_summary.name = session_name
            session_summary.length = session_length
            session_summary.date = session_date
            session_summary.n_words = len(session_events)
            session_summary.n_correct_words = np.sum(session_events.recalled)
            session_summary.pc_correct_words = 100*session_summary.n_correct_words / float(session_summary.n_words)

            positions = np.unique(session_events.serialpos)
            prob_recall = np.empty_like(positions, dtype=float)
            for i,pos in enumerate(positions):
                pos_events = session_events[session_events.serialpos == pos]
                prob_recall[i] = np.sum(pos_events.recalled) / float(len(pos_events))

            session_summary.prob_recall = prob_recall

            lists = np.unique(session_events.list)
            n_lists = len(lists)
            prob_first_recall = np.zeros(len(positions), dtype=float)
            session_irt_within_cat = []
            session_irt_between_cat = []
            session_summary.n_recalls_per_list = np.zeros(n_lists, dtype=np.int)
            session_summary.n_intr_per_list = np.zeros(n_lists, dtype=np.int)
            session_summary.n_stims_per_list = np.zeros(n_lists, dtype=np.int)
            session_summary.is_stim_list = np.zeros(n_lists, dtype=np.bool)
            items_per_list = np.zeros(n_lists, dtype=np.int)

            for lst in lists:
                list_events = session_all_events[session_all_events.list == lst]
                list_rec_events = session_rec_events[(session_rec_events.list == lst) & (session_rec_events.intrusion == 0)]
                list_intr_events = session_rec_events[(session_rec_events.list == lst) & (session_rec_events.intrusion >= 4)]
                list_word_events = list_events[list_events.type=='WORD']

                #session_summary.n_recalls_per_list[lst-1] = len(list_rec_events)
                #session_summary.n_intr_per_list[lst-1] = len(list_intr_events)
                session_summary.n_recalls_per_list[lst-1] = np.sum(list_word_events.recalled)
                session_summary.n_stims_per_list[lst-1] = np.sum(list_events.type=='STIM')
                session_summary.is_stim_list[lst-1] = session_events[session_events.list == lst][0].stimList
                for ie in list_intr_events:
                    session_summary.n_intr_per_list[ie.intrusion-1] += 1

                items_per_list[lst-1] = np.sum(list_events.type=='WORD')

                if list_rec_events.size > 0:
                    list_events = session_events[session_events.list == lst]
                    tmp = np.where(list_events.itemno == list_rec_events[0].itemno)[0]
                    if tmp.size > 0:
                        first_recall_idx = tmp[0]
                        prob_first_recall[first_recall_idx] += 1
                        first_recall_counter[first_recall_idx] += 1
                if task == 'RAM_CatFR3':
                    # list_rec_events = session_rec_events[session_rec_events.list == lst]
                    for i in xrange(1,len(list_rec_events)):
                        cur_ev = list_rec_events[i]
                        prev_ev = list_rec_events[i-1]
                        # if (cur_ev.intrusion == 0) and (prev_ev.intrusion == 0):
                        dt = cur_ev.mstime - prev_ev.mstime
                        if cur_ev.category == prev_ev.category:
                            session_irt_within_cat.append(dt)
                        else:
                            session_irt_between_cat.append(dt)

            prob_first_recall /= float(n_lists)
            total_list_counter += n_lists

            n_items_from_stim = np.sum(items_per_list[session_summary.is_stim_list])
            n_recalls_from_stim = np.sum(session_summary.n_recalls_per_list[session_summary.is_stim_list])
            n_intr_from_stim = np.sum(session_summary.n_intr_per_list[session_summary.is_stim_list])

            cumulative_n_items_from_stim += n_items_from_stim
            cumulative_n_recalls_from_stim += n_recalls_from_stim
            cumulative_n_intr_from_stim += n_intr_from_stim

            nonstim_list_mask = ~session_summary.is_stim_list
            nonstim_list_mask[0:3] = False
            n_items_from_nonstim = np.sum(items_per_list[nonstim_list_mask])
            n_recalls_from_nonstim = np.sum(session_summary.n_recalls_per_list[nonstim_list_mask])
            n_intr_from_nonstim = np.sum(session_summary.n_intr_per_list[nonstim_list_mask])

            cumulative_n_items_from_nonstim += n_items_from_nonstim
            cumulative_n_recalls_from_nonstim += n_recalls_from_nonstim
            cumulative_n_intr_from_nonstim += n_intr_from_nonstim

            session_summary.n_correct_stim = n_recalls_from_stim
            session_summary.n_total_stim = n_items_from_stim
            session_summary.pc_from_stim = 100 * n_recalls_from_stim / float(n_items_from_stim)

            session_summary.n_correct_nonstim = n_recalls_from_nonstim
            session_summary.n_total_nonstim = n_items_from_nonstim
            session_summary.pc_from_nonstim = 100 * n_recalls_from_nonstim / float(n_items_from_nonstim)

            session_summary.n_stim_intr = n_intr_from_stim
            session_summary.pc_from_stim_intr = 100 * n_intr_from_stim / float(n_items_from_stim)

            session_summary.n_nonstim_intr = n_intr_from_nonstim
            session_summary.pc_from_nonstim_intr = 100 * n_intr_from_nonstim / float(n_items_from_nonstim)

            session_summary.chisqr,session_summary.pvalue,_ = proportions_chisquare([n_recalls_from_stim, n_recalls_from_nonstim], [n_items_from_stim, n_items_from_nonstim])
            session_summary.chisqr_intr,session_summary.pvalue_intr,_ = proportions_chisquare([n_intr_from_stim, n_intr_from_nonstim], [n_items_from_stim, n_items_from_nonstim])

            session_summary.irt_within_cat = sum(session_irt_within_cat) / len(session_irt_within_cat) if session_irt_within_cat else 0.0
            session_summary.irt_between_cat = sum(session_irt_between_cat) / len(session_irt_between_cat) if session_irt_between_cat else 0.0

            irt_within_cat += session_irt_within_cat
            irt_between_cat += session_irt_between_cat

            session_summary.prob_first_recall = prob_first_recall

            if math_events is not None:
                session_math_events = math_events[math_events.session == session]
                session_summary.n_math = len(session_math_events)
                session_summary.n_correct_math = np.sum(session_math_events.iscorrect)
                session_summary.pc_correct_math = 100*session_summary.n_correct_math / float(session_summary.n_math)
                session_summary.math_per_list = session_summary.n_math / float(n_lists)

            session_intr_events = intr_events[intr_events.session == session]

            session_summary.n_pli = np.sum(session_intr_events.intrusion > 0)
            session_summary.pc_pli = 100*session_summary.n_pli / float(n_sess_events)
            session_summary.n_eli = np.sum(session_intr_events.intrusion == -1)
            session_summary.pc_eli = 100*session_summary.n_eli / float(n_sess_events)

            session_summary_array.append(session_summary)


        self.pass_object('SESSION_DATA', session_data)
        self.pass_object('session_summary_array', session_summary_array)

        cumulative_summary = SessionSummary()
        cumulative_summary.n_words = len(events)
        cumulative_summary.n_correct_words = np.sum(events.recalled)
        cumulative_summary.pc_correct_words = 100*cumulative_summary.n_correct_words / float(cumulative_summary.n_words)

        cumulative_summary.irt_within_cat = sum(irt_within_cat) / len(irt_within_cat) if irt_within_cat else 0.0
        cumulative_summary.irt_between_cat = sum(irt_between_cat) / len(irt_between_cat) if irt_between_cat else 0.0

        positions = np.unique(events.serialpos)
        prob_recall = np.empty_like(positions, dtype=float)
        for i,pos in enumerate(positions):
            pos_events = events[events.serialpos == pos]
            prob_recall[i] = np.sum(pos_events.recalled) / float(len(pos_events))
        cumulative_summary.prob_recall = prob_recall

        prob_first_recall = first_recall_counter / float(total_list_counter)
        cumulative_summary.prob_first_recall = prob_first_recall

        cumulative_summary.n_correct_stim = cumulative_n_recalls_from_stim
        cumulative_summary.n_total_stim = cumulative_n_items_from_stim
        cumulative_summary.pc_from_stim = 100 * cumulative_n_recalls_from_stim / float(cumulative_n_items_from_stim)

        cumulative_summary.n_correct_nonstim = cumulative_n_recalls_from_nonstim
        cumulative_summary.n_total_nonstim = cumulative_n_items_from_nonstim
        cumulative_summary.pc_from_nonstim = 100 * cumulative_n_recalls_from_nonstim / float(cumulative_n_items_from_nonstim)

        cumulative_summary.n_stim_intr = cumulative_n_intr_from_stim
        cumulative_summary.pc_from_stim_intr = 100 * cumulative_n_intr_from_stim / float(cumulative_n_items_from_stim)

        cumulative_summary.n_nonstim_intr = cumulative_n_intr_from_nonstim
        cumulative_summary.pc_from_nonstim_intr = 100 * cumulative_n_intr_from_nonstim / float(cumulative_n_items_from_nonstim)

        cumulative_summary.chisqr,cumulative_summary.pvalue,_ = proportions_chisquare([cumulative_n_recalls_from_stim, cumulative_n_recalls_from_nonstim], [cumulative_n_items_from_stim, cumulative_n_items_from_nonstim])
        cumulative_summary.chisqr_intr,cumulative_summary.pvalue_intr,_ = proportions_chisquare([cumulative_n_intr_from_stim, cumulative_n_intr_from_nonstim], [cumulative_n_items_from_stim, cumulative_n_items_from_nonstim])

        if math_events is not None:
            cumulative_summary.n_math = len(math_events)
            cumulative_summary.n_correct_math = np.sum(math_events.iscorrect)
            cumulative_summary.pc_correct_math = 100*cumulative_summary.n_correct_math / float(cumulative_summary.n_math)
            cumulative_summary.math_per_list = cumulative_summary.n_math / float(total_list_counter)

        cumulative_summary.n_pli = np.sum(intr_events.intrusion > 0)
        cumulative_summary.pc_pli = 100*cumulative_summary.n_pli / float(len(events))
        cumulative_summary.n_eli = np.sum(intr_events.intrusion == -1)
        cumulative_summary.pc_eli = 100*cumulative_summary.n_eli / float(len(events))

        self.pass_object('cumulative_summary', cumulative_summary)
    def run(self):
        subject = self.pipeline.subject
        task = self.pipeline.task

        events = self.get_passed_object(task + '_events')
        math_events = self.get_passed_object(task + '_math_events')
        intr_events = self.get_passed_object(task + '_intr_events')
        rec_events = self.get_passed_object(task + '_rec_events')
        all_events = self.get_passed_object(task + '_all_events')
        monopolar_channels = self.get_passed_object('monopolar_channels')
        bipolar_pairs = self.get_passed_object('bipolar_pairs')
        loc_info = self.get_passed_object('loc_info')

        ttest = self.get_passed_object('ttest')

        xval_output = self.get_passed_object('xval_output')
        perm_test_pvalue = self.get_passed_object('pvalue')

        sessions = np.unique(events.session)

        self.pass_object('NUMBER_OF_SESSIONS', len(sessions))
        self.pass_object('NUMBER_OF_ELECTRODES', len(monopolar_channels))

        session_data = []
        session_summary_array = []
        session_ttest_data = []

        positions = np.unique(events.serialpos)
        first_recall_counter = np.zeros(positions.size, dtype=int)
        total_list_counter = 0

        irt_within_cat = []
        irt_between_cat = []

        for session in sessions:
            session_summary = SessionSummary()

            session_events = events[events.session == session]
            n_sess_events = len(session_events)

            session_rec_events = rec_events[rec_events.session == session]

            session_all_events = all_events[all_events.session == session]
            timestamps = session_all_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

            print 'Session =', session_name

            session_summary.number = session
            session_summary.name = session_name
            session_summary.length = session_length
            session_summary.date = session_date
            session_summary.n_words = len(session_events)
            session_summary.n_correct_words = np.sum(session_events.recalled)
            session_summary.pc_correct_words = 100*session_summary.n_correct_words / float(session_summary.n_words)

            positions = np.unique(session_events.serialpos)
            prob_recall = np.empty_like(positions, dtype=float)
            for i,pos in enumerate(positions):
                pos_events = session_events[session_events.serialpos == pos]
                prob_recall[i] = np.sum(pos_events.recalled) / float(len(pos_events))

            session_summary.prob_recall = prob_recall

            lists = np.unique(session_events.list)
            n_lists = len(lists)
            prob_first_recall = np.zeros(len(positions), dtype=float)
            session_irt_within_cat = []
            session_irt_between_cat = []
            for lst in lists:
                list_rec_events = session_rec_events[(session_rec_events.list == lst) & (session_rec_events.intrusion == 0)]
                if list_rec_events.size > 0:
                    list_events = session_events[session_events.list == lst]
                    tmp = np.where(list_events.itemno == list_rec_events[0].itemno)[0]
                    if tmp.size > 0:
                        first_recall_idx = tmp[0]
                        prob_first_recall[first_recall_idx] += 1
                        first_recall_counter[first_recall_idx] += 1
                if task == 'RAM_CatFR1':
                    # list_rec_events = session_rec_events[session_rec_events.list == lst]
                    for i in xrange(1,len(list_rec_events)):
                        cur_ev = list_rec_events[i]
                        prev_ev = list_rec_events[i-1]
                        # if (cur_ev.intrusion == 0) and (prev_ev.intrusion == 0):
                        dt = cur_ev.mstime - prev_ev.mstime
                        if cur_ev.category == prev_ev.category:
                            session_irt_within_cat.append(dt)
                        else:
                            session_irt_between_cat.append(dt)
            prob_first_recall /= float(n_lists)
            total_list_counter += n_lists

            session_summary.irt_within_cat = sum(session_irt_within_cat) / len(session_irt_within_cat) if session_irt_within_cat else 0.0
            session_summary.irt_between_cat = sum(session_irt_between_cat) / len(session_irt_between_cat) if session_irt_between_cat else 0.0

            irt_within_cat += session_irt_within_cat
            irt_between_cat += session_irt_between_cat

            session_summary.prob_first_recall = prob_first_recall

            if math_events is not None:
                session_math_events = math_events[math_events.session == session]
                session_summary.n_math = len(session_math_events)
                session_summary.n_correct_math = np.sum(session_math_events.iscorrect)
                session_summary.pc_correct_math = 100*session_summary.n_correct_math / float(session_summary.n_math)
                session_summary.math_per_list = session_summary.n_math / float(n_lists)

            session_intr_events = intr_events[intr_events.session == session]

            session_summary.n_pli = np.sum(session_intr_events.intrusion > 0)
            session_summary.pc_pli = 100*session_summary.n_pli / float(n_sess_events)
            session_summary.n_eli = np.sum(session_intr_events.intrusion == -1)
            session_summary.pc_eli = 100*session_summary.n_eli / float(n_sess_events)

            session_xval_output = xval_output[session]

            session_summary.auc = '%.2f' % (100*session_xval_output.auc)
            session_summary.fpr = session_xval_output.fpr
            session_summary.tpr = session_xval_output.tpr
            session_summary.pc_diff_from_mean = (session_xval_output.low_pc_diff_from_mean, session_xval_output.mid_pc_diff_from_mean, session_xval_output.high_pc_diff_from_mean)

            session_summary_array.append(session_summary)

            # ttest_data = [list(a) for a in zip(bipolar_pairs.eType,  bipolar_pairs.tagName, ttest[session][1], ttest[session][0])]
            session_ttest = ttest[session]
            if isinstance(session_ttest,tuple):
                if ('Das Volumetric Atlas Location' in loc_info) or ('Freesurfer Desikan Killiany Surface Atlas Location' in loc_info):
                    session_ttest_data.append([[None, None, None, None, np.nan, np.nan]])
                else:
                    session_ttest_data.append([[None, None, np.nan, np.nan]])
            else:
                ttest_data = make_ttest_table(bipolar_pairs, loc_info, session_ttest)
                ttest_data.sort(key=itemgetter(-2))
                ttest_data = format_ttest_table(ttest_data)
                session_ttest_data.append(ttest_data)

        self.pass_object('SESSION_DATA', session_data)
        self.pass_object('session_summary_array', session_summary_array)
        self.pass_object('session_ttest_data', session_ttest_data)

        cumulative_summary = SessionSummary()
        cumulative_summary.n_words = len(events)
        cumulative_summary.n_correct_words = np.sum(events.recalled)
        cumulative_summary.pc_correct_words = 100*cumulative_summary.n_correct_words / float(cumulative_summary.n_words)

        cumulative_summary.irt_within_cat = sum(irt_within_cat) / len(irt_within_cat) if irt_within_cat else 0.0
        cumulative_summary.irt_between_cat = sum(irt_between_cat) / len(irt_between_cat) if irt_between_cat else 0.0

        positions = np.unique(events.serialpos)
        prob_recall = np.empty_like(positions, dtype=float)
        for i,pos in enumerate(positions):
            pos_events = events[events.serialpos == pos]
            prob_recall[i] = np.sum(pos_events.recalled) / float(len(pos_events))
        cumulative_summary.prob_recall = prob_recall

        prob_first_recall = first_recall_counter / float(total_list_counter)
        cumulative_summary.prob_first_recall = prob_first_recall

        if math_events is not None:
            cumulative_summary.n_math = len(math_events)
            cumulative_summary.n_correct_math = np.sum(math_events.iscorrect)
            cumulative_summary.pc_correct_math = 100*cumulative_summary.n_correct_math / float(cumulative_summary.n_math)
            cumulative_summary.math_per_list = cumulative_summary.n_math / float(total_list_counter)

        cumulative_summary.n_pli = np.sum(intr_events.intrusion > 0)
        cumulative_summary.pc_pli = 100*cumulative_summary.n_pli / float(len(events))
        cumulative_summary.n_eli = np.sum(intr_events.intrusion == -1)
        cumulative_summary.pc_eli = 100*cumulative_summary.n_eli / float(len(events))

        cumulative_xval_output = xval_output[-1]

        cumulative_summary.auc = '%.2f' % (100*cumulative_xval_output.auc)
        cumulative_summary.fpr = cumulative_xval_output.fpr
        cumulative_summary.tpr = cumulative_xval_output.tpr
        cumulative_summary.pc_diff_from_mean = (cumulative_xval_output.low_pc_diff_from_mean, cumulative_xval_output.mid_pc_diff_from_mean, cumulative_xval_output.high_pc_diff_from_mean)
        cumulative_summary.perm_AUCs = self.get_passed_object('perm_AUCs')
        cumulative_summary.perm_test_pvalue = ('= %.3f' % perm_test_pvalue) if perm_test_pvalue>=0.001 else '\leq 0.001'
        cumulative_summary.jstat_thresh = '%.3f' % cumulative_xval_output.jstat_thresh
        cumulative_summary.jstat_percentile = '%.2f' % (100.0*cumulative_xval_output.jstat_quantile)

        self.pass_object('cumulative_summary', cumulative_summary)

        # cumulative_ttest_data = [list(a) for a in zip(bipolar_pairs.eType, bipolar_pairs.tagName, ttest[-1][1], ttest[-1][0])]
        cumulative_ttest_data = make_ttest_table(bipolar_pairs, loc_info, ttest[-1])
        cumulative_ttest_data.sort(key=itemgetter(-2))
        cumulative_ttest_data = format_ttest_table(cumulative_ttest_data)

        self.pass_object('cumulative_ttest_data', cumulative_ttest_data)

        ttable_format, ttable_header = make_ttest_table_header(loc_info)
        self.pass_object('ttable_format', ttable_format)
        self.pass_object('ttable_header', ttable_header)
Example #4
0
    def run(self):
        subject = self.pipeline.subject
        experiment = self.pipeline.experiment

        monopolar_channels = self.get_passed_object('monopolar_channels')
        xval_output = self.get_passed_object('xval_output')

        ps_table = self.get_passed_object('ps_table')

        sessions = sorted(ps_table.session.unique())

        self.pass_object('NUMBER_OF_SESSIONS', len(sessions))
        self.pass_object('NUMBER_OF_ELECTRODES', len(monopolar_channels))

        thresh = xval_output[-1].jstat_thresh

        self.pass_object('AUC', xval_output[-1].auc)

        param1_name = param2_name = None
        param1_unit = param2_unit = None
        const_param_name = const_unit = None
        if experiment == 'PS1':
            param1_name = 'Pulse_Frequency'
            param2_name = 'Duration'
            param1_unit = 'Hz'
            param2_unit = 'ms'
            const_param_name = 'Amplitude'
            const_unit = 'mA'
        elif experiment == 'PS2':
            param1_name = 'Pulse_Frequency'
            param2_name = 'Amplitude'
            param1_unit = 'Hz'
            param2_unit = 'mA'
            const_param_name = 'Duration'
            const_unit = 'ms'
        elif experiment == 'PS3':
            param1_name = 'Burst_Frequency'
            param2_name = 'Pulse_Frequency'
            param1_unit = 'Hz'
            param2_unit = 'Hz'
            const_param_name = 'Duration'
            const_unit = 'ms'

        self.pass_object('param1_name', param1_name.replace('_', ' '))
        self.pass_object('param1_unit', param1_unit)

        self.pass_object('param2_name', param2_name.replace('_', ' '))
        self.pass_object('param2_unit', param2_unit)

        self.pass_object('const_param_name', const_param_name)
        self.pass_object('const_unit', const_unit)

        session_data = []
        session_summary_array = []

        anova_param1_sv = dict()
        anova_param2_sv = dict()
        anova_param12_sv = dict()

        anova_significance = dict()

        for session in sessions:
            ps_session_table = ps_table[ps_table.session==session]

            session_summary = SessionSummary()

            session_summary.sess_num = session

            first_time_stamp = ps_session_table.mstime.min()
            last_time_stamp = ps_session_table.mstime.max()
            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_anode_tag = ps_session_table.stimAnodeTag.values[0].upper()
            stim_cathode_tag = ps_session_table.stimCathodeTag.values[0].upper()
            stim_tag = stim_anode_tag + '-' + stim_cathode_tag
            sess_loc_tag = ps_session_table.Region.values[0]
            roi = '{\em locTag not found}' if sess_loc_tag is None else sess_loc_tag

            isi_min = ps_session_table.isi.min()
            isi_max = ps_session_table.isi.max()
            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

            session_summary.name = session_name
            session_summary.length = session_length
            session_summary.date = session_date
            session_summary.stimtag = stim_tag
            session_summary.region_of_interest = roi
            session_summary.isi_mid = isi_mid
            session_summary.isi_half_range = isi_halfrange
            session_summary.const_param_value = ps_session_table[const_param_name].unique().max()

            ps_session_low_table = pd.DataFrame(ps_session_table[ps_session_table['prob_pre']<thresh])

            session_summary.plots = delta_plot_data(ps_session_low_table, param1_name, param2_name, param2_unit)

            if sess_loc_tag is not None and not (sess_loc_tag in anova_param1_sv):
                anova_param1_sv[sess_loc_tag] = []
                anova_param2_sv[sess_loc_tag] = []
                anova_param12_sv[sess_loc_tag] = []

            anova = anova_test(ps_session_low_table, param1_name, param2_name)
            if anova is not None:
                session_summary.anova_fvalues = anova[0]
                session_summary.anova_pvalues = anova[1]

                if anova[1][0] < 0.05: # first param significant
                    if stim_tag in anova_significance:
                        anova_significance[stim_tag][0] = True
                    else:
                        anova_significance[stim_tag] = np.array([True, False, False], dtype=np.bool)
                    param1_ttest_table = ttest_one_param(ps_session_low_table, param1_name)
                    if len(param1_ttest_table) > 0:
                        if sess_loc_tag is not None:
                            anova_param1_sv[sess_loc_tag].append(param1_ttest_table)
                        session_summary.param1_ttest_table = format_ttest_table(param1_ttest_table)

                if anova[1][1] < 0.05: # second param significant
                    if stim_tag in anova_significance:
                        anova_significance[stim_tag][1] = True
                    else:
                        anova_significance[stim_tag] = np.array([False, True, False], dtype=np.bool)
                    param2_ttest_table = ttest_one_param(ps_session_low_table, param2_name)
                    if len(param2_ttest_table) > 0:
                        if sess_loc_tag is not None:
                            anova_param2_sv[sess_loc_tag].append(param2_ttest_table)
                        session_summary.param2_ttest_table = format_ttest_table(param2_ttest_table)

                if anova[1][2] < 0.05: # interaction is significant
                    if stim_tag in anova_significance:
                        anova_significance[stim_tag][2] = True
                    else:
                        anova_significance[stim_tag] = np.array([False, False, True], dtype=np.bool)
                    param12_ttest_table = ttest_interaction(ps_session_low_table, param1_name, param2_name)
                    if len(param12_ttest_table) > 0:
                        if sess_loc_tag is not None:
                            anova_param12_sv[sess_loc_tag].append(param12_ttest_table)
                        session_summary.param12_ttest_table = format_ttest_table(param12_ttest_table)

            session_summary_array.append(session_summary)

        self.pass_object('SESSION_DATA', session_data)
        self.pass_object('session_summary_array', session_summary_array)

        joblib.dump(anova_param1_sv, self.get_path_to_resource_in_workspace(subject + '-' + experiment + '-anova_%s_sv.pkl'%param1_name))
        joblib.dump(anova_param2_sv, self.get_path_to_resource_in_workspace(subject + '-' + experiment + '-anova_%s_sv.pkl'%param2_name))
        joblib.dump(anova_param12_sv, self.get_path_to_resource_in_workspace(subject + '-' + experiment + '-anova_%s-%s_sv.pkl'%(param1_name,param2_name)))

        if len(anova_significance) > 0:
            joblib.dump(anova_significance, self.get_path_to_resource_in_workspace(subject + '-' + experiment + '-anova_significance.pkl'))

        isi_min = ps_table.isi.min()
        isi_max = ps_table.isi.max()
        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)

        ps_low_table = ps_table[ps_table['prob_pre']<thresh]

        cumulative_plots = delta_plot_data(ps_low_table, param1_name, param2_name, param2_unit)
        self.pass_object('cumulative_plots', cumulative_plots)

        cumulative_anova_fvalues = cumulative_anova_pvalues = None
        cumulative_param1_ttest_table = cumulative_param2_ttest_table = cumulative_param12_ttest_table = None
        anova = anova_test(ps_low_table, param1_name, param2_name)
        if anova is not None:
            cumulative_anova_fvalues = anova[0]
            cumulative_anova_pvalues = anova[1]
            if anova[1][0] < 0.05:
                param1_ttest_table = ttest_one_param(ps_low_table, param1_name)
                if len(param1_ttest_table) > 0:
                    cumulative_param1_ttest_table = format_ttest_table(param1_ttest_table)

            if anova[1][1] < 0.05:
                param2_ttest_table = ttest_one_param(ps_low_table, param2_name)
                if len(param2_ttest_table) > 0:
                    cumulative_param2_ttest_table = format_ttest_table(param2_ttest_table)

            if anova[1][2] < 0.05:
                param12_ttest_table = ttest_interaction(ps_low_table, param1_name, param2_name)
                if len(param12_ttest_table) > 0:
                    cumulative_param12_ttest_table = format_ttest_table(param12_ttest_table)

        self.pass_object('CUMULATIVE_ANOVA_FVALUES', cumulative_anova_fvalues)
        self.pass_object('CUMULATIVE_ANOVA_PVALUES', cumulative_anova_pvalues)

        self.pass_object('CUMULATIVE_PARAM1_TTEST_TABLE', cumulative_param1_ttest_table)
        self.pass_object('CUMULATIVE_PARAM2_TTEST_TABLE', cumulative_param2_ttest_table)
        self.pass_object('CUMULATIVE_PARAM12_TTEST_TABLE', cumulative_param12_ttest_table)
Example #5
0
    def run(self):
        subject = self.pipeline.subject
        task = self.pipeline.task

        events = self.get_passed_object(task + '_events')
        math_events = self.get_passed_object(task + '_math_events')
        intr_events = self.get_passed_object(task + '_intr_events')
        rec_events = self.get_passed_object(task + '_rec_events')
        all_events = self.get_passed_object(task + '_all_events')
        monopolar_channels = self.get_passed_object('monopolar_channels')
        bipolar_pairs = self.get_passed_object('bipolar_pairs')
        loc_info = self.get_passed_object('loc_info')

        ttest = self.get_passed_object('ttest')

        xval_output = self.get_passed_object('xval_output')
        perm_test_pvalue = self.get_passed_object('pvalue')

        sessions = np.unique(events.session)

        self.pass_object('NUMBER_OF_SESSIONS', len(sessions))
        self.pass_object('NUMBER_OF_ELECTRODES', len(monopolar_channels))

        session_data = []
        session_summary_array = []
        session_ttest_data = []

        positions = np.unique(events.serialpos)
        first_recall_counter = np.zeros(positions.size, dtype=int)
        total_list_counter = 0

        irt_within_cat = []
        irt_between_cat = []

        for session in sessions:
            session_summary = SessionSummary()

            session_events = events[events.session == session]
            n_sess_events = len(session_events)

            session_rec_events = rec_events[rec_events.session == session]

            session_all_events = all_events[all_events.session == session]
            timestamps = session_all_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

            print 'Session =', session_name

            session_summary.number = session
            session_summary.name = session_name
            session_summary.length = session_length
            session_summary.date = session_date
            session_summary.n_words = len(session_events)
            session_summary.n_correct_words = np.sum(session_events.recalled)
            session_summary.pc_correct_words = 100 * session_summary.n_correct_words / float(
                session_summary.n_words)

            positions = np.unique(session_events.serialpos)
            prob_recall = np.empty_like(positions, dtype=float)
            for i, pos in enumerate(positions):
                pos_events = session_events[session_events.serialpos == pos]
                prob_recall[i] = np.sum(pos_events.recalled) / float(
                    len(pos_events))

            session_summary.prob_recall = prob_recall

            lists = np.unique(session_events.list)
            n_lists = len(lists)
            prob_first_recall = np.zeros(len(positions), dtype=float)
            session_irt_within_cat = []
            session_irt_between_cat = []
            for lst in lists:
                list_rec_events = session_rec_events[
                    (session_rec_events.list == lst)
                    & (session_rec_events.intrusion == 0)]
                if list_rec_events.size > 0:
                    list_events = session_events[session_events.list == lst]
                    tmp = np.where(
                        list_events.itemno == list_rec_events[0].itemno)[0]
                    if tmp.size > 0:
                        first_recall_idx = tmp[0]
                        prob_first_recall[first_recall_idx] += 1
                        first_recall_counter[first_recall_idx] += 1
                if task == 'RAM_CatFR1':
                    # list_rec_events = session_rec_events[session_rec_events.list == lst]
                    for i in xrange(1, len(list_rec_events)):
                        cur_ev = list_rec_events[i]
                        prev_ev = list_rec_events[i - 1]
                        # if (cur_ev.intrusion == 0) and (prev_ev.intrusion == 0):
                        dt = cur_ev.mstime - prev_ev.mstime
                        if cur_ev.category == prev_ev.category:
                            session_irt_within_cat.append(dt)
                        else:
                            session_irt_between_cat.append(dt)
            prob_first_recall /= float(n_lists)
            total_list_counter += n_lists

            session_summary.irt_within_cat = sum(session_irt_within_cat) / len(
                session_irt_within_cat) if session_irt_within_cat else 0.0
            session_summary.irt_between_cat = sum(
                session_irt_between_cat) / len(
                    session_irt_between_cat
                ) if session_irt_between_cat else 0.0

            irt_within_cat += session_irt_within_cat
            irt_between_cat += session_irt_between_cat

            session_summary.prob_first_recall = prob_first_recall

            if math_events is not None:
                session_math_events = math_events[math_events.session ==
                                                  session]
                session_summary.n_math = len(session_math_events)
                session_summary.n_correct_math = np.sum(
                    session_math_events.iscorrect)
                session_summary.pc_correct_math = 100 * session_summary.n_correct_math / float(
                    session_summary.n_math)
                session_summary.math_per_list = session_summary.n_math / float(
                    n_lists)

            session_intr_events = intr_events[intr_events.session == session]

            session_summary.n_pli = np.sum(session_intr_events.intrusion > 0)
            session_summary.pc_pli = 100 * session_summary.n_pli / float(
                n_sess_events)
            session_summary.n_eli = np.sum(session_intr_events.intrusion == -1)
            session_summary.pc_eli = 100 * session_summary.n_eli / float(
                n_sess_events)

            session_xval_output = xval_output[session]

            session_summary.auc = '%.2f' % (100 * session_xval_output.auc)
            session_summary.fpr = session_xval_output.fpr
            session_summary.tpr = session_xval_output.tpr
            session_summary.pc_diff_from_mean = (
                session_xval_output.low_pc_diff_from_mean,
                session_xval_output.mid_pc_diff_from_mean,
                session_xval_output.high_pc_diff_from_mean)

            session_summary_array.append(session_summary)

            # ttest_data = [list(a) for a in zip(bipolar_pairs.eType,  bipolar_pairs.tagName, ttest[session][1], ttest[session][0])]
            session_ttest = ttest[session]
            if isinstance(session_ttest, tuple):
                if ('Das Volumetric Atlas Location' in loc_info) or (
                        'Freesurfer Desikan Killiany Surface Atlas Location'
                        in loc_info):
                    session_ttest_data.append(
                        [[None, None, None, None, np.nan, np.nan]])
                else:
                    session_ttest_data.append([[None, None, np.nan, np.nan]])
            else:
                ttest_data = make_ttest_table(bipolar_pairs, loc_info,
                                              session_ttest)
                ttest_data.sort(key=itemgetter(-2))
                ttest_data = format_ttest_table(ttest_data)
                session_ttest_data.append(ttest_data)

        self.pass_object('SESSION_DATA', session_data)
        self.pass_object('session_summary_array', session_summary_array)
        self.pass_object('session_ttest_data', session_ttest_data)

        cumulative_summary = SessionSummary()
        cumulative_summary.n_words = len(events)
        cumulative_summary.n_correct_words = np.sum(events.recalled)
        cumulative_summary.pc_correct_words = 100 * cumulative_summary.n_correct_words / float(
            cumulative_summary.n_words)

        cumulative_summary.irt_within_cat = sum(irt_within_cat) / len(
            irt_within_cat) if irt_within_cat else 0.0
        cumulative_summary.irt_between_cat = sum(irt_between_cat) / len(
            irt_between_cat) if irt_between_cat else 0.0

        positions = np.unique(events.serialpos)
        prob_recall = np.empty_like(positions, dtype=float)
        for i, pos in enumerate(positions):
            pos_events = events[events.serialpos == pos]
            prob_recall[i] = np.sum(pos_events.recalled) / float(
                len(pos_events))
        cumulative_summary.prob_recall = prob_recall

        prob_first_recall = first_recall_counter / float(total_list_counter)
        cumulative_summary.prob_first_recall = prob_first_recall

        if math_events is not None:
            cumulative_summary.n_math = len(math_events)
            cumulative_summary.n_correct_math = np.sum(math_events.iscorrect)
            cumulative_summary.pc_correct_math = 100 * cumulative_summary.n_correct_math / float(
                cumulative_summary.n_math)
            cumulative_summary.math_per_list = cumulative_summary.n_math / float(
                total_list_counter)

        cumulative_summary.n_pli = np.sum(intr_events.intrusion > 0)
        cumulative_summary.pc_pli = 100 * cumulative_summary.n_pli / float(
            len(events))
        cumulative_summary.n_eli = np.sum(intr_events.intrusion == -1)
        cumulative_summary.pc_eli = 100 * cumulative_summary.n_eli / float(
            len(events))

        cumulative_xval_output = xval_output[-1]

        cumulative_summary.auc = '%.2f' % (100 * cumulative_xval_output.auc)
        cumulative_summary.fpr = cumulative_xval_output.fpr
        cumulative_summary.tpr = cumulative_xval_output.tpr
        cumulative_summary.pc_diff_from_mean = (
            cumulative_xval_output.low_pc_diff_from_mean,
            cumulative_xval_output.mid_pc_diff_from_mean,
            cumulative_xval_output.high_pc_diff_from_mean)
        cumulative_summary.perm_AUCs = self.get_passed_object('perm_AUCs')
        cumulative_summary.perm_test_pvalue = (
            '= %.3f' %
            perm_test_pvalue) if perm_test_pvalue >= 0.001 else '\leq 0.001'
        cumulative_summary.jstat_thresh = '%.3f' % cumulative_xval_output.jstat_thresh
        cumulative_summary.jstat_percentile = '%.2f' % (
            100.0 * cumulative_xval_output.jstat_quantile)

        self.pass_object('cumulative_summary', cumulative_summary)

        # cumulative_ttest_data = [list(a) for a in zip(bipolar_pairs.eType, bipolar_pairs.tagName, ttest[-1][1], ttest[-1][0])]
        cumulative_ttest_data = make_ttest_table(bipolar_pairs, loc_info,
                                                 ttest[-1])
        cumulative_ttest_data.sort(key=itemgetter(-2))
        cumulative_ttest_data = format_ttest_table(cumulative_ttest_data)

        self.pass_object('cumulative_ttest_data', cumulative_ttest_data)

        ttable_format, ttable_header = make_ttest_table_header(loc_info)
        self.pass_object('ttable_format', ttable_format)
        self.pass_object('ttable_header', ttable_header)
    def run(self):
        subject = self.pipeline.subject
        experiment = self.pipeline.experiment

        channels = self.get_passed_object('channels')
        tal_info = self.get_passed_object('tal_info')
        loc_info = self.get_passed_object('loc_info')
        xval_output = self.get_passed_object('xval_output')

        ps_table = self.get_passed_object('ps_table')

        sessions = sorted(ps_table.session.unique())

        self.pass_object('NUMBER_OF_SESSIONS', len(sessions))
        self.pass_object('NUMBER_OF_ELECTRODES', len(channels))

        thresh = xval_output[-1].jstat_thresh

	self.pass_object('AUC', xval_output[-1].auc)

        param1_name = param2_name = None
        const_param_name = const_unit = None
        if experiment == 'PS1':
            param1_name = 'Pulse Frequency'
            param2_name = 'Duration'
            const_param_name = 'Amplitude'
            const_unit = 'mA'
        elif experiment == 'PS2':
            param1_name = 'Pulse Frequency'
            param2_name = 'Amplitude'
            const_param_name = 'Duration'
            const_unit = 'ms'
        elif experiment == 'PS3':
            param1_name = 'Burst Frequency'
            param2_name = 'Pulse Frequency'
            const_param_name = 'Duration'
            const_unit = 'ms'

        self.pass_object('CUMULATIVE_PARAMETER1', param1_name)
        self.pass_object('CUMULATIVE_PARAMETER2', param2_name)

        session_data = []
        session_summary_array = []

        for session in sessions:
            ps_session_table = ps_table[ps_table.session==session]

            session_summary = SessionSummary()

            session_summary.sess_num = session

            first_time_stamp = ps_session_table.mstime.min()
            last_time_stamp = ps_session_table.mstime.max()
            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_anode_tag = ps_session_table.stimAnodeTag.values[0]
            stim_cathode_tag = ps_session_table.stimCathodeTag.values[0]
            stim_tag = stim_anode_tag + '-' + stim_cathode_tag
            if loc_info[stim_tag] != '':
                stim_tag += ' (%s)' % loc_info[stim_anode_tag]

            isi_min = ps_session_table.isi.min()
            isi_max = ps_session_table.isi.max()
            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

            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_summary.parameter1 = param1_name
            session_summary.parameter2 = param2_name
            session_summary.constant_name = const_param_name
            session_summary.constant_value = ps_session_table[const_param_name].unique()[0]
            session_summary.constant_unit = const_unit

            anova = anova_test(ps_session_table, param1_name, param2_name)
            if anova is not None:
                session_summary.anova_fvalues = anova[0]
                session_summary.anova_pvalues = anova[1]
                joblib.dump(anova, self.get_path_to_resource_in_workspace(subject + '-' + experiment + '-anova.pkl'))

            session_summary.plots = delta_plot_data(ps_session_table[ps_session_table['prob_pre']<thresh], param1_name, param2_name)

            session_summary_array.append(session_summary)

        self.pass_object('SESSION_DATA', session_data)
        self.pass_object('session_summary_array', session_summary_array)

        isi_min = ps_table.isi.min()
        isi_max = ps_table.isi.max()
        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)

        cumulative_plots = delta_plot_data(ps_table[ps_table['prob_pre']<thresh], param1_name, param2_name)

        self.pass_object('cumulative_plots', cumulative_plots)
Example #7
0
    def run(self):
        subject = self.pipeline.subject
        task = self.pipeline.task

        events = self.get_passed_object(task + '_events')
        math_events = self.get_passed_object(task + '_math_events')
        intr_events = self.get_passed_object(task + '_intr_events')
        rec_events = self.get_passed_object(task + '_rec_events')
        all_events = self.get_passed_object(task + '_all_events')
        channels = self.get_passed_object('monopolar_channels')
        tal_info = self.get_passed_object('bipolar_pairs')

        sessions = np.unique(events.session)

        self.pass_object('NUMBER_OF_SESSIONS', len(sessions))
        self.pass_object('NUMBER_OF_ELECTRODES', len(channels))

        session_data = []
        session_summary_array = []

        positions = np.unique(events.serialpos)
        first_recall_counter = np.zeros(positions.size, dtype=int)
        total_list_counter = 0

        irt_within_cat = []
        irt_between_cat = []

        cumulative_n_items_from_stim = 0
        cumulative_n_recalls_from_stim = 0
        cumulative_n_intr_from_stim = 0

        cumulative_n_items_from_nonstim = 0
        cumulative_n_recalls_from_nonstim = 0
        cumulative_n_intr_from_nonstim = 0

        for session in sessions:
            session_summary = SessionSummary()

            session_events = events[events.session == session]
            n_sess_events = len(session_events)

            session_rec_events = rec_events[rec_events.session == session]

            session_all_events = all_events[all_events.session == session]
            timestamps = session_all_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

            print 'Session =', session_name

            session_summary.number = session
            session_summary.name = session_name
            session_summary.length = session_length
            session_summary.date = session_date
            session_summary.n_words = len(session_events)
            session_summary.n_correct_words = np.sum(session_events.recalled)
            session_summary.pc_correct_words = 100*session_summary.n_correct_words / float(session_summary.n_words)

            positions = np.unique(session_events.serialpos)
            prob_recall = np.empty_like(positions, dtype=float)
            for i,pos in enumerate(positions):
                pos_events = session_events[session_events.serialpos == pos]
                prob_recall[i] = np.sum(pos_events.recalled) / float(len(pos_events))

            session_summary.prob_recall = prob_recall

            lists = np.unique(session_events.list)
            n_lists = len(lists)
            prob_first_recall = np.zeros(len(positions), dtype=float)
            session_irt_within_cat = []
            session_irt_between_cat = []
            session_summary.n_recalls_per_list = np.zeros(n_lists, dtype=np.int)
            session_summary.n_intr_per_list = np.zeros(n_lists, dtype=np.int)
            session_summary.n_stims_per_list = np.zeros(n_lists, dtype=np.int)
            session_summary.is_stim_list = np.zeros(n_lists, dtype=np.bool)
            items_per_list = np.zeros(n_lists, dtype=np.int)

            for lst in lists:
                list_events = session_all_events[session_all_events.list == lst]
                list_rec_events = session_rec_events[(session_rec_events.list == lst) & (session_rec_events.intrusion == 0)]
                list_intr_events = session_rec_events[(session_rec_events.list == lst) & (session_rec_events.intrusion >= 4)]
                list_word_events = list_events[list_events.type=='WORD']

                #session_summary.n_recalls_per_list[lst-1] = len(list_rec_events)
                #session_summary.n_intr_per_list[lst-1] = len(list_intr_events)
                session_summary.n_recalls_per_list[lst-1] = np.sum(list_word_events.recalled)
                session_summary.n_stims_per_list[lst-1] = np.sum(list_events.type=='STIM')
                session_summary.is_stim_list[lst-1] = session_events[session_events.list == lst][0].stimList
                for ie in list_intr_events:
                    session_summary.n_intr_per_list[ie.intrusion-1] += 1

                items_per_list[lst-1] = np.sum(list_events.type=='WORD')

                if list_rec_events.size > 0:
                    list_events = session_events[session_events.list == lst]
                    tmp = np.where(list_events.itemno == list_rec_events[0].itemno)[0]
                    if tmp.size > 0:
                        first_recall_idx = tmp[0]
                        prob_first_recall[first_recall_idx] += 1
                        first_recall_counter[first_recall_idx] += 1
                if task == 'RAM_CatFR3':
                    # list_rec_events = session_rec_events[session_rec_events.list == lst]
                    for i in xrange(1,len(list_rec_events)):
                        cur_ev = list_rec_events[i]
                        prev_ev = list_rec_events[i-1]
                        # if (cur_ev.intrusion == 0) and (prev_ev.intrusion == 0):
                        dt = cur_ev.mstime - prev_ev.mstime
                        if cur_ev.category == prev_ev.category:
                            session_irt_within_cat.append(dt)
                        else:
                            session_irt_between_cat.append(dt)

            prob_first_recall /= float(n_lists)
            total_list_counter += n_lists

            n_items_from_stim = np.sum(items_per_list[session_summary.is_stim_list])
            n_recalls_from_stim = np.sum(session_summary.n_recalls_per_list[session_summary.is_stim_list])
            n_intr_from_stim = np.sum(session_summary.n_intr_per_list[session_summary.is_stim_list])

            cumulative_n_items_from_stim += n_items_from_stim
            cumulative_n_recalls_from_stim += n_recalls_from_stim
            cumulative_n_intr_from_stim += n_intr_from_stim

            nonstim_list_mask = ~session_summary.is_stim_list
            nonstim_list_mask[0:3] = False
            n_items_from_nonstim = np.sum(items_per_list[nonstim_list_mask])
            n_recalls_from_nonstim = np.sum(session_summary.n_recalls_per_list[nonstim_list_mask])
            n_intr_from_nonstim = np.sum(session_summary.n_intr_per_list[nonstim_list_mask])

            cumulative_n_items_from_nonstim += n_items_from_nonstim
            cumulative_n_recalls_from_nonstim += n_recalls_from_nonstim
            cumulative_n_intr_from_nonstim += n_intr_from_nonstim

            session_summary.n_correct_stim = n_recalls_from_stim
            session_summary.n_total_stim = n_items_from_stim
            session_summary.pc_from_stim = 100 * n_recalls_from_stim / float(n_items_from_stim)

            session_summary.n_correct_nonstim = n_recalls_from_nonstim
            session_summary.n_total_nonstim = n_items_from_nonstim
            session_summary.pc_from_nonstim = 100 * n_recalls_from_nonstim / float(n_items_from_nonstim)

            session_summary.n_stim_intr = n_intr_from_stim
            session_summary.pc_from_stim_intr = 100 * n_intr_from_stim / float(n_items_from_stim)

            session_summary.n_nonstim_intr = n_intr_from_nonstim
            session_summary.pc_from_nonstim_intr = 100 * n_intr_from_nonstim / float(n_items_from_nonstim)

            session_summary.chisqr,session_summary.pvalue,_ = proportions_chisquare([n_recalls_from_stim, n_recalls_from_nonstim], [n_items_from_stim, n_items_from_nonstim])
            session_summary.chisqr_intr,session_summary.pvalue_intr,_ = proportions_chisquare([n_intr_from_stim, n_intr_from_nonstim], [n_items_from_stim, n_items_from_nonstim])

            session_summary.irt_within_cat = sum(session_irt_within_cat) / len(session_irt_within_cat) if session_irt_within_cat else 0.0
            session_summary.irt_between_cat = sum(session_irt_between_cat) / len(session_irt_between_cat) if session_irt_between_cat else 0.0

            irt_within_cat += session_irt_within_cat
            irt_between_cat += session_irt_between_cat

            session_summary.prob_first_recall = prob_first_recall

            if math_events is not None:
                session_math_events = math_events[math_events.session == session]
                session_summary.n_math = len(session_math_events)
                session_summary.n_correct_math = np.sum(session_math_events.iscorrect)
                session_summary.pc_correct_math = 100*session_summary.n_correct_math / float(session_summary.n_math)
                session_summary.math_per_list = session_summary.n_math / float(n_lists)

            session_intr_events = intr_events[intr_events.session == session]

            session_summary.n_pli = np.sum(session_intr_events.intrusion > 0)
            session_summary.pc_pli = 100*session_summary.n_pli / float(n_sess_events)
            session_summary.n_eli = np.sum(session_intr_events.intrusion == -1)
            session_summary.pc_eli = 100*session_summary.n_eli / float(n_sess_events)

            session_summary_array.append(session_summary)


        self.pass_object('SESSION_DATA', session_data)
        self.pass_object('session_summary_array', session_summary_array)

        cumulative_summary = SessionSummary()
        cumulative_summary.n_words = len(events)
        cumulative_summary.n_correct_words = np.sum(events.recalled)
        cumulative_summary.pc_correct_words = 100*cumulative_summary.n_correct_words / float(cumulative_summary.n_words)

        cumulative_summary.irt_within_cat = sum(irt_within_cat) / len(irt_within_cat) if irt_within_cat else 0.0
        cumulative_summary.irt_between_cat = sum(irt_between_cat) / len(irt_between_cat) if irt_between_cat else 0.0

        positions = np.unique(events.serialpos)
        prob_recall = np.empty_like(positions, dtype=float)
        for i,pos in enumerate(positions):
            pos_events = events[events.serialpos == pos]
            prob_recall[i] = np.sum(pos_events.recalled) / float(len(pos_events))
        cumulative_summary.prob_recall = prob_recall

        prob_first_recall = first_recall_counter / float(total_list_counter)
        cumulative_summary.prob_first_recall = prob_first_recall

        cumulative_summary.n_correct_stim = cumulative_n_recalls_from_stim
        cumulative_summary.n_total_stim = cumulative_n_items_from_stim
        cumulative_summary.pc_from_stim = 100 * cumulative_n_recalls_from_stim / float(cumulative_n_items_from_stim)

        cumulative_summary.n_correct_nonstim = cumulative_n_recalls_from_nonstim
        cumulative_summary.n_total_nonstim = cumulative_n_items_from_nonstim
        cumulative_summary.pc_from_nonstim = 100 * cumulative_n_recalls_from_nonstim / float(cumulative_n_items_from_nonstim)

        cumulative_summary.n_stim_intr = cumulative_n_intr_from_stim
        cumulative_summary.pc_from_stim_intr = 100 * cumulative_n_intr_from_stim / float(cumulative_n_items_from_stim)

        cumulative_summary.n_nonstim_intr = cumulative_n_intr_from_nonstim
        cumulative_summary.pc_from_nonstim_intr = 100 * cumulative_n_intr_from_nonstim / float(cumulative_n_items_from_nonstim)

        cumulative_summary.chisqr,cumulative_summary.pvalue,_ = proportions_chisquare([cumulative_n_recalls_from_stim, cumulative_n_recalls_from_nonstim], [cumulative_n_items_from_stim, cumulative_n_items_from_nonstim])
        cumulative_summary.chisqr_intr,cumulative_summary.pvalue_intr,_ = proportions_chisquare([cumulative_n_intr_from_stim, cumulative_n_intr_from_nonstim], [cumulative_n_items_from_stim, cumulative_n_items_from_nonstim])

        if math_events is not None:
            cumulative_summary.n_math = len(math_events)
            cumulative_summary.n_correct_math = np.sum(math_events.iscorrect)
            cumulative_summary.pc_correct_math = 100*cumulative_summary.n_correct_math / float(cumulative_summary.n_math)
            cumulative_summary.math_per_list = cumulative_summary.n_math / float(total_list_counter)

        cumulative_summary.n_pli = np.sum(intr_events.intrusion > 0)
        cumulative_summary.pc_pli = 100*cumulative_summary.n_pli / float(len(events))
        cumulative_summary.n_eli = np.sum(intr_events.intrusion == -1)
        cumulative_summary.pc_eli = 100*cumulative_summary.n_eli / float(len(events))

        self.pass_object('cumulative_summary', cumulative_summary)