示例#1
0
def get_AS_intensity_vector(self,
                            feat,
                            bin_type='12bins',
                            atype='abs'):  #, TEST=True):
    """returns array with active state intensity [mg/sec] for 
        feeding, drinking and movement into 11 2hr bins; 
        starting at 15 after midnight: bin0 = 15.00-17.00 = 08-10 CT
        NB. intervals that: 
        -START in CT6-8, END in CT8-10,
        -START in CT28-30, END in CT30-32
        have been removed. this is part of the job of
        getting rid of first bin: 13-15 (1-3pm, CT6-8)
    """
    act = feat[0]
    AS = self.load('AS_timeSet')
    tbins = get_CT_bins(bin_type, self.experiment.BIN_SHIFT,
                        self.experiment.binTimeShift)

    if self.experiment.short_name == '2CFast':  # has a fast day
        if self.dayNumber in self.experiment.fastDayNumbers:
            if act == 'F':
                _, tend = self.experiment.get_food_removal_times()
                # zero food after fast begins
                idx0 = np.where(AS[:, 1] <= tend)[0]
                AS = AS[idx0]

    arr = np.zeros(len(tbins))
    b = 0
    for tbin in tbins:
        # get AS times in bins (strict). use tbin_eff if onset-based definition
        AS_times, _, _, _ = count_onsets_in_bin(I=AS, tbin=tbin)

        if len(AS_times) > 0:
            # get amounts for each AS, in bin
            if act in ['F', 'W']:
                amounts = self.get_amounts(act, I=AS, tbin=tbin)  # mg

            elif act == 'M':
                _amounts = self.get_move_distances_in_AS_or_Bout(
                    tbin=tbin, move_type='AS')
                # some AS have no move events, but F/W only. correct
                amounts = self.correct_AS_without_move(_amounts,
                                                       bin_type,
                                                       bin_num=b)
                # if self.mouseNumber == 7210 and self.dayNumber == 15 and b==1: stop

            np.testing.assert_allclose(len(amounts), len(AS_times), rtol=1e-5)

            arr[b] = get_intensity(amounts, AS_times,
                                   atype)  # avg intensity, mg/s
            # if self.mouseNumber == 7250 and self.dayNumber == 12 and b == 0: stop
        b += 1
    return arr
示例#2
0
def get_bout_vector(self, feat, bin_type='12bins'):
    """ returns array with (features, num_bins):
        counts, duration, size, intensity
    """
    tbins = my_utils.get_CT_bins(bin_type, self.experiment.BIN_SHIFT, self.experiment.binTimeShift)      
    arr = np.zeros(len(tbins))
    b = 0
    for tbin in tbins:
        arr[b] = self.get_bout_feature_value(feat, tbin, b)  
        # arr[b] = self.get_bout_feature_value(feat, bin_type, tbin=tbin)
        b +=1   
    # elif bin_type == 'LC':
    #     arr = self.get_bout_feature_value(feat, bin_type, tbin=tbins)
    return arr
示例#3
0
def _check_bout_counts_in_bins(self, act):
    
    tbins = my_utils.get_CT_bins(bin_type='12bins')
    B = self.load('%sB_timeSet' %act)
    cnt_diff = np.zeros(len(tbins))
    b = 0
    for tbin in tbins:
        B_times, B_cnt_eff, B_times_eff, tbin_eff = my_utils.count_onsets_in_bin(I=B, tbin=tbin)
        B_cnt = B_times.shape[0]
        if B_cnt != B_cnt_eff:
            cnt_diff[b] = B_cnt - B_cnt_eff
        b +=1

    return cnt_diff
示例#4
0
def get_device_firing_durations_in_AS_or_Bouts(self, EV, I, tbin):
    """ get all durations for each AS/Bouts(I), for specified tbin
    """
    if tbin is None:  # use 24h bin
        tbin = my_utils.get_CT_bins(bin_type='24H')

    _I = Intervals(I).intersect(Intervals(tbin))
    durs = np.zeros(_I.intervals.shape[0])
    c = 0
    for x in _I:
        durs[c] = Intervals(x).intersect(Intervals(EV)).measure()
        # if act == 'W':
        #    size[c] = Intervals(x).intersect(EV).intervals.shape[0] * coeff      # grams
        c += 1
    return durs
示例#5
0
def get_AS_vector(self, feat, bin_type='12bins'):
    """returns ASP, ASN, ASD in 12-2hr bins resp. 24H, DC , LC values
        starting at 13 after midnight: 
        bin0: 13.00-15.00 or CT06-08 
    """
    tbins = get_CT_bins(bin_type, self.experiment.BIN_SHIFT,
                        self.experiment.binTimeShift
                        )  # tbins do not have anything outside CT [6, 30]
    # arr = np.zeros(len(tbins))
    # b = 0
    vect = [self.get_AS_feature_value(feat, tbin) for tbin in tbins]
    # for tbin in tbins:
    #     arr[b] = self.get_AS_feature_value(feat, tbin)
    #     b +=1
    return vect
示例#6
0
def get_AS_in_cycle(self, cycle, AS_def='onset'):
    """ for within AS structure
    """
    AS = self.load('AS_timeSet')
    if cycle == '24H':
        return AS
    else:
        tbins = get_CT_bins(bin_type=cycle)
        AS_eff = list()
        for tbin in tbins:
            bin_start, bin_end = tbin
            for start, end in AS:
                if (start > bin_start and start < bin_end):
                    AS_eff.append([start, end])

    return np.array(AS_eff)
示例#7
0
def get_total_vector(self, feat, bin_type='12bins'):
    """returns FWM totals in 11 2hr bins vs. 24H, DC , LC values
        starting at 15 after midnight: 
        bin0: 15.00-17.00 or 08-10 CT
        h13-15 after midnight (1-3pm) or CT6-8 is excluded 
    """
    tbins = my_utils.get_CT_bins(bin_type, self.experiment.BIN_SHIFT, self.experiment.binTimeShift)       # tbins does not have anything outside CT [6, 30]
    act = feat[1]

    arr = np.zeros(len(tbins))
    b = 0
    for tbin in tbins:
        # get total in bin (strict)
        if act in ['F', 'W']:
            amounts = self.get_amounts(act=act, tbin=tbin)
            arr[b] = amounts.sum() / 1000    # grams
        elif act == 'M':
            dist = self.get_move_distances(tbin=tbin, move_type='AS')
            arr[b] = dist.sum() / 100.    # meters
        b +=1
    
    return arr
示例#8
0
def get_time_budgets(self, cycle, num_items):

    var = [
        'recording_start_stop_time', 'AS_timeSet', 'FB_timeSet', 'WB_timeSet',
        'MB_timeSet', 'IS_timeSet'
    ]
    rec, AS, F, W, M, IS = [self.load(x) for x in var]

    C = get_CT_bins(bin_type=cycle)  # ignoring maintenance time
    time = np.diff(C).sum()

    if cycle in self.experiment.cycles:

        if cycle == '24H':
            tas, tf, tw, tm, tis = [
                np.diff(x).sum() for x in [AS, F, W, M, IS]
            ]

        elif cycle in ['DC', 'LC']:
            AS, F, W, M, IS = [
                intersect_arrays(x, C) for x in [AS, F, W, M, IS]
            ]
            tas, tf, tw, tm, tis = [
                np.diff(x).sum() for x in [AS, F, W, M, IS]
            ]

        elif cycle in ['AS24H', 'ASDC', 'ASLC']:
            stop

        FWM_union = Intervals(M).union(Intervals(F)).union(Intervals(W))
        other = Intervals(AS).intersect(~FWM_union).measure()

    arr = np.array([tm, tf, tw, other, tis])
    assert arr.sum() - time < 300, '%s %s screwed' % (
        MD, cycle)  # allow 5 minutes differences

    return arr
示例#9
0
def get_position_bin_times(self, cycle=None, tbin_type=None, xbins=2, ybins=4):
    
    for varName in ['recording_start_stop_time', 'CT', 'CX', 'CY']:
        if not hasattr(self, varName):
            self.load(varName)
    
    # start_time, stop_time = getattr(self, 'recording_start_stop_time')

    # pull corrected loco T, X, Y during this day               
    M = np.vstack([self.CT, self.CX, self.CY])
    
    C = Cage()
    xlims, ylims = (C.CMXLower, C.CMXUpper), (C.CMYLower, C.CMYUpper)

    if tbin_type is not None:
        arr = get_CT_bins(tbin_type=tbin_type.strip('AS'))

        # initialize times for this mouseday and bins
        bin_times = np.zeros((arr.shape[0], ybins, xbins)) 

        if tbin_type == '12bins':
            b = 0
            for tstart, tend in arr: 
                pos_subset = pull_locom_tseries_subset(M, tstart, tend)
                bin_times[b] = total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, xbins=xbins, ybins=ybins)
                b += 1

        elif tbin_type == 'AS12bins':
            AS = self.load('AS_timeSet')
            b = 0
            for row in arr:
                AS_bin = Intervals(AS).intersect(Intervals(row)).intervals
                for tstart, tend in AS_bin:
                    pos_subset = pull_locom_tseries_subset(M, tstart, tend)
                    bin_times[b] += total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, xbins=xbins, ybins=ybins)
                b += 1

    else:
        # initialize times for this mouseday and cycle
        bin_times = np.zeros((ybins, xbins)) 

        if cycle == '24H':
            start_time, stop_time = get_CT_bins(tbin_type='3cycles')[0]
            pos_subset = pull_locom_tseries_subset(M, start_time, stop_time)    # does not change M
            # assert((pos_subset==M).all()), 'pos_subset differs from M'
            bin_times = total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, 
                                                xbins=xbins, ybins=ybins)
        elif cycle == 'IS':
            IS = self.load('IS_timeSet')
            for deltaT in IS:
                pos_subset = pull_locom_tseries_subset(M, deltaT[0], deltaT[1])
                bin_times += total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, 
                                                xbins=xbins, ybins=ybins)
        
        elif cycle == 'DC':
            DC_start, DC_end = get_CT_bins(tbin_type='3cycles')[1]
            pos_subset = pull_locom_tseries_subset(M, DC_start, DC_end)
            bin_times = total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, 
                                                xbins=xbins, ybins=ybins)

        elif cycle == 'LC':
            LC1, LC2 = get_CT_bins(tbin_type='3cycles')[2]
            for LC_start, LC_end in [LC1, LC2]:
                pos_subset = pull_locom_tseries_subset(M, LC_start, LC_end)
                btimes = total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, 
                                                    xbins=xbins, ybins=ybins)
                bin_times += btimes

        else:

            AS, IS = [self.load(x) for x in ['AS_timeSet', 'IS_timeSet']]
            
            if cycle == 'AS24H':
                for deltaT in AS:
                    pos_subset = pull_locom_tseries_subset(M, deltaT[0], deltaT[1])
                    bin_times += total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, 
                                                xbins=xbins, ybins=ybins)

            else:
                light_phase = cycle.strip('AS')       #DC, LC
                idx = 1 if light_phase == 'DC' else 2
                I = get_CT_bins(tbin_type='3cycles')[idx]
                I_AS = Intervals(AS).intersect(Intervals(I)).intervals
                for deltaT in I_AS:
                    pos_subset = pull_locom_tseries_subset(M, deltaT[0], deltaT[1])
                    bin_times += total_time_rectangle_bins(pos_subset, xlims=xlims, ylims=ylims, 
                                                xbins=xbins, ybins=ybins)
    
    print self
    print "Total bin times, %s, xbins=%d, ybins=%d:" %(cycle, xbins, ybins)
    print bin_times
    assert (bin_times.sum()>=0), "bins_times < 0 !!"
    if bin_times.sum() == 0:
        print "No Activity mouse %d day: %d cycle: %s" %(mouse.mouseNumber, MD.dayNumber, cycle)
    
    if cycle == '24H':      
        setattr(self, 'bin_times_24H_xbins%d_ybins%d' %(xbins, ybins), bin_times)
    return bin_times
示例#10
0
    def generate_position_density(self,
                                  cycle='24H',
                                  tbin_type=None,
                                  level='mouseday',
                                  xbins=12,
                                  ybins=24,
                                  days=None,
                                  err_type='sd',
                                  GENERATE=False,
                                  VERBOSE=False):
        """ returns position density data 
        """
        cstart = time.clock()

        if days is None:
            days_to_use = self.daysToUse
            num_md = self.num_mousedays
        else:
            days_to_use = days
            num_md, _ = self.count_mousedays(days=days_to_use)

        if tbin_type is not None:
            num_tbins = my_utils.get_CT_bins(
                tbin_type=tbin_type.strip('AS')).shape[0]
            md_data = np.zeros((num_md, num_tbins, ybins, xbins))
        else:
            md_data = np.zeros((num_md, ybins, xbins))

        text = my_utils.get_text(cycle, tbin_type)

        print "Exp: %s, %s, tbin_type: %s, level:%s, xbins=%d, ybins=%d, err_type: %s, %s days" % (
            self.short_name, self.generate_position_density.__name__, text,
            level, xbins, ybins, err_type, days_to_use)

        md_labels = np.zeros((num_md, 3), dtype=int)
        cnt = 0
        for group in self.groups:
            for mouse in group.individuals:
                for MD in mouse.mouse_days:
                    if MD.dayNumber in days_to_use:

                        if MD.ignored:
                            md_data[cnt] = np.nan

                        else:
                            bin_times = MD.generate_position_bin_times(
                                cycle, tbin_type, xbins, ybins, GENERATE)
                            if tbin_type is not None:
                                b = 0
                                for btimes in bin_times:
                                    if btimes.sum() >= 0:
                                        md_data[cnt, b] = btimes / btimes.sum()
                                    b += 1
                            else:
                                if bin_times.sum() >= 0:
                                    md_data[cnt] = bin_times / bin_times.sum()

                        md_labels[
                            cnt] = MD.groupNumber, MD.mouseNumber, MD.dayNumber

                        # if GENERATE and not VERBOSE:
                        #     my_utils.print_progress(cnt, self.num_md_ok)

                        cnt += 1

        cstop = time.clock()

        if GENERATE:
            if not VERBOSE:
                print

            dirname = self.derived_dir + \
                        'position_data/xbins%d_ybins%d/%s/bin_times/' %(xbins, ybins, text)   # copied from: MD.generate_position_bin_times
            print "binary output saved to: %s" % dirname
            print "..took %1.2f minutes" % ((cstop - cstart) / 60.)

        E = self
        data, labels = my_utils.get_2d_averages_errors(E, md_data, md_labels,
                                                       tbin_type, level,
                                                       err_type)

        return data, labels
示例#11
0
def draw_position_density_tbins(E,
                                data,
                                labels,
                                tbin_type,
                                level,
                                err_type,
                                xbins,
                                ybins,
                                fname,
                                rect_HB=None,
                                obs_rect=None,
                                plot_type='position_density',
                                ADD_SOURCE=False):

    C = Cage()

    # figure
    figsize, nrows, ncols = get_subplot_specs(E,
                                              num_subplots=data.shape[0],
                                              plot_type=plot_type,
                                              sub_type=tbin_type)

    fig, axes = plt.subplots(nrows, ncols, figsize=figsize)

    cmap = plt.get_cmap("viridis")
    vmin, vmax = .0001, 1.
    if (xbins, ybins) == (2, 4):
        vmin = 0.01
    extent = [C.CMXLower, C.CMXUpper, C.CMYLower, C.CMYUpper]

    percent_time_color = E.fcolors['other'][0]

    title, subtitles = get_figure_titles(E,
                                         labels=labels,
                                         level=level,
                                         tbin_type=tbin_type,
                                         err_type=err_type,
                                         plot_type=plot_type)

    data_ = data.swapaxes(0, 1)
    arr = my_utils.get_CT_bins(
        bin_type=tbin_type) / 3600 - 7  # CT in hours. num_tbins=arr.shape[0]
    row_labels = ['CT%d-%d' % (row[0], row[1]) for row in arr]
    column_labels = subtitles

    for r in xrange(nrows):
        # row labels
        top, bottom, left, right = [
            getattr(fig.subplotpars, x)
            for x in ['top', 'bottom', 'left', 'right']
        ]
        yy = top - .03 - (top - bottom) * r / nrows
        fig.text(left - .1, yy, row_labels[r], va='center', fontsize=8)

        for c in xrange(ncols):
            # column labels
            if r < 1:
                xx = left + .01 + (right - left) * c / ncols
                fig.text(xx, top + .03, column_labels[c], fontsize=8)

            bin_data = data_[r, c, :, :]

            try:
                ax = axes[r, c] if type(
                    axes) is np.ndarray else axes  # when just one axis

                Z3 = bin_data[:, :, 0]  # plt average only for the moment

                if np.isnan(Z3).all():
                    # ax.text(.5, .5, subtitles[r] + ' excluded',
                    #     fontsize=12, color='.5', ha='center', va='center')
                    ax.axis('off')

                else:
                    Z3_ = Z3.copy()
                    Z3_[Z3 <
                        1e-4] = 1e-4  # round small numbers to almost zero for plotting

                    im = ax.imshow(Z3_,
                                   interpolation='nearest',
                                   cmap=cmap,
                                   norm=LogNorm(vmin=vmin,
                                                vmax=vmax))  #, extent=extent)

                    # ax.set_title(subtitles[c])
                    set_position_density_layout(fig, ax, im)
                    if (xbins, ybins) == (2, 4):
                        draw_homebase_star(ax,
                                           rect_HB[c],
                                           color=percent_time_color)

                if r * c < 1:
                    hh, ll = ax.get_legend_handles_labels()

            except IndexError:
                ax.axis('off')
                continue

    set_colorbar(fig, ax, im)
    set_legend(fig, hh, ll, xbins, ybins, text_color=percent_time_color)

    typad = .15  #if level == 'group' else 0
    add_titles_notes(E,
                     fig,
                     title=title,
                     typad=typad,
                     titlesize=14,
                     TL_NOTE=False)

    plt.subplots_adjust(hspace=.2, wspace=.05)

    sypad = -.05 if level == 'group' else .05

    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE, sypad=sypad)
示例#12
0
def plot_position_density_panel2(experiment,
                                 cycle='24H',
                                 tbin_type=None,
                                 xbins=12,
                                 ybins=24,
                                 err_type='sd',
                                 plot_type='position_density',
                                 ADD_SOURCE=True):

    E = experiment

    text = cycle if tbin_type is None else tbin_type
    string = E.get_days_to_use_text()

    print "%s, %s, xbins:%d, ybins:%d, %s" % (
        plot_position_density_panel2.__name__, text, xbins, ybins, E.use_days)

    dirname_out = E.figures_dir + '%s/panel2/xbins%d_ybins%d/%s/%s_days/' % (
        plot_type, xbins, ybins, text, E.use_days)
    if not os.path.isdir(dirname_out): os.makedirs(dirname_out)

    # load data
    data, labels = E.generate_position_density(cycle=cycle,
                                               tbin_type=tbin_type,
                                               level='mouseday',
                                               xbins=xbins,
                                               ybins=ybins,
                                               err_type=err_type)
    new_data = data.swapaxes(0, 1)
    # new_data = data_.reshape(data_.shape[0], data_.shape[1], -1)      # tbins, mousedays, xbins, ybins, (avg, err)

    # tbins
    arr = my_utils.get_CT_bins(
        bin_type=tbin_type) / 3600 - 7  # CT in hours. num_tbins=arr.shape[0]
    tbins = ['CT%d-%d' % (row[0], row[1]) for row in arr]

    # nest
    rects_HB = np.array([None] * len(data))
    obs_rects = np.array([None] * len(data))

    condition = (xbins, ybins) == (2, 4)
    if condition:
        rects_HB, obs_rects = E.generate_homebase_data()

    for strain in range(E.num_strains):
        idx = labels[:, 0] == strain
        mouseNumbers = np.unique(labels[idx, 1])

        for b in xrange(new_data.shape[0]):
            bin_data = new_data[b].reshape(len(mouseNumbers), len(E.daysToUse),
                                           ybins, xbins, 2)

            fig_title = '%s Experiment\n%s, %s\n%s days: %s\ngroup%d: %s, bin %d: %s' % (
                E.short_name, plot_type.replace('_', ' ').title(), text,
                E.use_days.replace('_', ' ').upper(), string, strain,
                E.strain_names[strain], b, tbins[b])

            fname = dirname_out + '%s_%s_group%d_xbins%d_ybins%d_tbin%d' % (
                plot_type, text, strain, xbins, ybins, b)

            rect_HB, obs_rect = None, None
            if condition:
                idx_start, idx_end = my_utils.find_nonzero_runs(idx)[0]
                rect_HB = np.array(rects_HB[idx_start:idx_end]).reshape(
                    len(mouseNumbers), len(E.daysToUse))
                # obs_rect = np.array(obs_rects[idx_start:idx_end]).reshape(len(mouseNumbers), len(E.daysToUse))

            if tbin_type is not None:
                draw_position_density_tbins2(E,
                                             bin_data,
                                             mouseNumbers,
                                             tbin_type,
                                             err_type,
                                             xbins,
                                             ybins,
                                             fig_title,
                                             fname,
                                             rect_HB=rect_HB,
                                             obs_rect=obs_rect,
                                             plot_type=plot_type,
                                             ADD_SOURCE=ADD_SOURCE)
示例#13
0
def write_to_csv(experiment,
                 cycle='24H',
                 tbin_type=None,
                 AS_FLAG=False,
                 level='group',
                 xbins=12,
                 ybins=24,
                 err_type='sd',
                 plot_type='position_density'):

    E = experiment
    use_days = E.use_days + '_days'

    text = my_utils.get_text(cycle, tbin_type, AS_FLAG)

    print "%s, %s, %s, xbins:%d, ybins:%d, %s" % (
        write_to_csv.__name__, text, level, xbins, ybins, use_days)

    suffix = '%s/csv_files/xbins%d_ybins%d/%s/%s/%s_days/' % (
        plot_type, xbins, ybins, level, text, E.use_days)

    dirname = os.path.join(E.figures_dir, suffix)
    if not os.path.isdir(dirname): os.makedirs(dirname)
    fname = dirname + '%s_%s_%s_xbins%d_ybins%d.csv' % (plot_type, text, level,
                                                        xbins, ybins)

    # load data
    data, labels = E.generate_position_density(cycle=cycle,
                                               tbin_type=tbin_type,
                                               AS_FLAG=AS_FLAG,
                                               level=level,
                                               xbins=xbins,
                                               ybins=ybins,
                                               err_type=err_type)

    if tbin_type is not None:
        arr = my_utils.get_CT_bins(bin_type=tbin_type) / 3600 - 7
        tbins = [
            'bin%d: CT%d-%d' % (b, row[0], row[1]) for b, row in enumerate(arr)
        ]
        new_data = data[:, :, :, :,
                        0].reshape(data.shape[0], data.shape[1],
                                   -1)  # (num_mousedays, 4x2 raveled)
    else:
        new_data = data[:, :, :, 0].reshape(data.shape[0],
                                            -1)  # (num_mousedays, 4x2 raveled)

    # headers
    # string = ', BIN_SHIFTED: %dm' %(E.binTimeShift/60) if E.BIN_SHIFT else ''
    headers = [['Experiment: %s' % E.short_name],
               ['%s, xbins=%d, ybins=%d' % (plot_type, xbins, ybins)],
               ['%s, %s' % (level, text)], ['%s days' % E.use_days],
               ['days: %s' % E.daysToUse]]

    if level == 'group':
        sub_header = ['use_days', 'group']
        if tbin_type is not None:
            if (xbins, ybins) == (2, 4):
                sub_header += [
                    'tbins', 'Niche', 'TopLeft', '[]', '[]', '[]', '[]',
                    'Feeder', 'Lick'
                ]
            else:
                sub_header += ['tbins']
        else:
            sub_header += [
                'Niche', 'TopLeft', '[]', '[]', '[]', '[]', 'Feeder', 'Lick'
            ]

        with open(fname, 'wb') as csv_file:
            writer = csv.writer(csv_file, delimiter=",")
            for h in headers:
                writer.writerow(h)
            writer.writerow('\n')
            writer.writerow(sub_header)
            writer.writerow('\n')

            if tbin_type is not None:
                new_data_ = new_data.swapaxes(
                    0, 1)  # tbin, days, (raveled xbins, ybins)
                for b in xrange(len(new_data_)):
                    bin_data = new_data_[b]
                    c = 0
                    for row in bin_data:
                        row_text = [
                            use_days + ', %s' % text, labels[c], tbins[b]
                        ]
                        writer.writerow(
                            np.hstack([row_text, ['%1.5f' % r for r in row]]))
                        c += 1
                    writer.writerow('\n')

            else:
                c = 0
                for row in new_data:
                    row_text = [use_days + ', %s' % text, labels[c]]
                    writer.writerow(
                        np.hstack([row_text, ['%1.5f' % r for r in row]]))
                    c += 1
                writer.writerow('\n')

    elif level == 'mouse':
        sub_header = ['use_days', 'group', 'mouse']
        if tbin_type is not None:
            if (xbins, ybins) == (2, 4):
                sub_header += [
                    'tbins', 'Niche', 'TopRight', '[]', '[]', '[]', '[]',
                    'Feeder', 'Lick'
                ]
            else:
                sub_header += ['tbins']
        else:
            sub_header += [
                'Niche', 'TopLeft', '[]', '[]', '[]', '[]', 'Feeder', 'Lick'
            ]

        with open(fname, 'wb') as csv_file:
            writer = csv.writer(csv_file, delimiter=",")
            for h in headers:
                writer.writerow(h)
            writer.writerow('\n')
            writer.writerow(sub_header)
            writer.writerow('\n')

            for group_num in range(E.num_strains):
                idx_group = labels[:, 0] == group_num
                group_data = new_data[idx_group]
                group_labels = labels[idx_group]

                if tbin_type is not None:
                    group_data_ = group_data.swapaxes(
                        0, 1)  # tbin, days, (raveled xbins, ybins)
                    for b in xrange(len(group_data_)):
                        bin_data = group_data_[b]
                        c = 0
                        for row in bin_data:
                            row_text = [
                                use_days + ', %s' % text, group_labels[c, 0],
                                group_labels[c, 1], tbins[b]
                            ]
                            writer.writerow(
                                np.hstack(
                                    [row_text, ['%1.5f' % r for r in row]]))
                            c += 1
                        # stop
                        writer.writerow('\n')
                        writer.writerow(['', '', '', 'Mean:'] + [
                            '%1.5f' % r for r in np.nanmean(bin_data, axis=0)
                        ])
                        writer.writerow(
                            ['', '', '', 'sd:'] +
                            ['%1.5f' % r for r in np.nanstd(bin_data, axis=0)])
                        sem = np.nanstd(
                            bin_data, axis=0) / np.sqrt(bin_data.shape[0] - 1)
                        writer.writerow(['', '', '', 'sem:'] +
                                        ['%1.5f' % r for r in sem])
                        writer.writerow('\n')

                else:
                    c = 0
                    for row in group_data:

                        row_text = [
                            use_days + ', %s' % text, group_labels[c, 0],
                            group_labels[c, 1]
                        ]
                        writer.writerow(
                            np.hstack([row_text, ['%1.5f' % r for r in row]]))
                        c += 1

                    writer.writerow('\n')
                    writer.writerow(
                        ['', '', 'Mean:'] +
                        ['%1.5f' % r for r in np.nanmean(group_data, axis=0)])
                    writer.writerow(
                        ['', '', 'sd:'] +
                        ['%1.5f' % r for r in np.nanstd(group_data, axis=0)])
                    sem = np.nanstd(group_data,
                                    axis=0) / np.sqrt(group_data.shape[0] - 1)
                    writer.writerow(['', '', 'sem:'] +
                                    ['%1.5f' % r for r in sem])
                    writer.writerow('\n')

    elif level == 'mouseday':
        sub_header = ['', 'group', 'mouse', 'mouseday']
        if tbin_type is not None:
            if (xbins, ybins) == (2, 4):
                sub_header += [
                    'tbins', 'Niche', 'TopLeft', '[]', '[]', '[]', '[]',
                    'Feeder', 'Lick'
                ]
            else:
                sub_header += ['tbins']
        else:
            sub_header += [
                'Niche', 'TopLeft', '[]', '[]', '[]', '[]', 'Feeder', 'Lick'
            ]

        with open(fname, 'wb') as csv_file:
            writer = csv.writer(csv_file, delimiter=",")
            for h in headers:
                writer.writerow(h)
            writer.writerow('\n')
            writer.writerow(sub_header)
            writer.writerow('\n')

            for group_num in range(E.num_strains):
                idx_group = labels[:, 0] == group_num
                group_data = new_data[idx_group]
                group_labels = labels[idx_group]

                m_labels = np.unique(group_labels[:, 1])

                for mouse in m_labels:
                    idx_mouse = group_labels[:, 1] == mouse
                    m_data = group_data[idx_mouse]
                    m_labels = group_labels[idx_mouse]

                    if tbin_type is not None:

                        m_data_ = m_data.swapaxes(
                            0, 1)  # tbin, days, (raveled xbins, ybins)
                        for b in xrange(len(m_data_)):
                            bin_data = m_data_[b]
                            c = 0
                            for row in bin_data:
                                row_text = [
                                    use_days + ', %s' % text, m_labels[c, 0],
                                    m_labels[c, 1], m_labels[c, 2], tbins[b]
                                ]
                                writer.writerow(
                                    np.hstack(
                                        [row_text,
                                         ['%1.5f' % r for r in row]]))
                                c += 1
                            # stop
                            writer.writerow('\n')
                            writer.writerow(['', '', '', '', 'Mean:'] + [
                                '%1.5f' % r
                                for r in np.nanmean(bin_data, axis=0)
                            ])
                            writer.writerow(['', '', '', '', 'sd:'] + [
                                '%1.5f' % r
                                for r in np.nanstd(bin_data, axis=0)
                            ])
                            sem = np.nanstd(
                                bin_data,
                                axis=0) / np.sqrt(bin_data.shape[0] - 1)
                            writer.writerow(['', '', '', '', 'sem:'] +
                                            ['%1.5f' % r for r in sem])
                            writer.writerow('\n')

                    else:
                        c = 0
                        for row in m_data:
                            row_text = [
                                use_days + ', %s' % text, m_labels[c, 0],
                                m_labels[c, 1], m_labels[c, 2]
                            ]
                            writer.writerow(
                                np.hstack(
                                    [row_text, ['%1.5f' % r for r in row]]))
                            c += 1

                        writer.writerow('\n')
                        writer.writerow(
                            ['', '', '', 'Mean:'] +
                            ['%1.5f' % r for r in np.nanmean(m_data, axis=0)])
                        writer.writerow(
                            ['', '', '', 'sd:'] +
                            ['%1.5f' % r for r in np.nanstd(m_data, axis=0)])
                        sem = np.nanstd(m_data,
                                        axis=0) / np.sqrt(m_data.shape[0] - 1)
                        writer.writerow(['', '', '', 'sem:'] +
                                        ['%1.5f' % r for r in sem])
                        writer.writerow('\n')

    print "csv files saved to:\n%s" % fname
示例#14
0
def draw_position_density(E,
                          data,
                          labels,
                          cycle,
                          tbin_type,
                          level,
                          err_type,
                          xbins,
                          ybins,
                          fname,
                          rect_HB=None,
                          obs_rect=None,
                          plot_type='position_density',
                          ADD_SOURCE=False):

    # figure
    figsize, nrows, ncols, sharex, sharey = get_subplot_specs(
        num_subplots=data.shape[0], tbin_type=tbin_type)

    fig_title, subtitles = get_figure_titles(E,
                                             labels=labels,
                                             level=level,
                                             cycle=cycle,
                                             tbin_type=tbin_type,
                                             err_type=err_type,
                                             plot_type=plot_type)

    percent_time_color = E.fcolors['other'][0]

    if tbin_type is not None:
        arr = my_utils.get_CT_bins(bin_type=tbin_type.strip('AS')) / 3600 - 7

        for b in xrange(data.shape[1]):

            fig, axes = plt.subplots(nrows=nrows,
                                     ncols=ncols,
                                     figsize=figsize,
                                     sharex=sharex,
                                     sharey=sharey)

            for c in xrange(nrows * ncols):
                try:
                    ax = axes.flatten()[c] if type(
                        axes) is np.ndarray else axes  # when just one axis
                    Z3 = data[c, b, :, :, 0]
                    fig, ax = draw_it(E, fig, ax, Z3, c, level, subtitles,
                                      xbins, ybins, rect_HB, obs_rect)

                    if c < 1:
                        h, l = ax.get_legend_handles_labels()

                except IndexError:
                    ax.axis('off')
                    continue

            title = fig_title + '\nbin%d: CT%d-%d' % (b, arr[b, 0], arr[b, 1])
            fname1 = fname + '_tbin%d' % b
            finish_plot(E, fig, level, xbins, ybins, h, l, nrows, title,
                        fname1, ADD_SOURCE)

    else:

        fig, axes = plt.subplots(nrows, ncols, figsize=figsize)

        for c in xrange(nrows * ncols):

            try:
                ax = axes.flatten()[c] if type(
                    axes) is np.ndarray else axes  # when just one axis

                Z3 = data[c, :, :, 0]  # plt average only for the moment
                fig, ax = draw_it(E, fig, ax, Z3, c, level, subtitles, xbins,
                                  ybins, rect_HB, obs_rect)
                if c < 1:
                    h, l = ax.get_legend_handles_labels()

            except IndexError:
                ax.axis('off')
                continue

        finish_plot(E, fig, level, xbins, ybins, h, l, nrows, fig_title, fname,
                    ADD_SOURCE)