Ejemplo n.º 1
0
def index_coordinates_at_homebase(self, xbins=2, ybins=4):
    """breaks up CT, CX, CY into Move components and corresponding M_at_HB_Set vs M_out_HB_Set:
        nest, or homebase, is given as a rectangle in a 2 x 4 dicretization of cage.
        _at_HB: movemements that occur at nest location (will be part of IS) 
        _out_HB: outside homebase
        returns CT idx at nest
    """
    startTime = time.clock()

    C = Cage()

    rectangle = getattr(self, 'rect_HB')

    num_movements = self.CT.shape[0]

    idx_list = []
    for rect in rectangle:
        tl, tr, bl, br = self.map_xbins_ybins_to_cage(rectangle=rect,
                                                      xbins=xbins,
                                                      ybins=ybins)
        print "Homebase location in original coordinates top_left, top_right, bot_left, bot_right: "
        print tl, tr, bl, br

        if rect == (0, 0):
            # REMOVE NEST ENCLOSURE
            string = 'in Niche'
            # these are correct cage boundaries -/+something to allow for match
            # with gridcells in a 2x4 discretization
            less_than_x = C.nestRightX - 1.2
            greater_than_y = C.nestBottomY + 0.7
            # 200x faster
            idx = (self.CX < less_than_x) & (self.CY > greater_than_y)
            idx_list.append(idx)
        else:
            # REMOVE other HB
            string = 'at other HB'
            idx1 = (self.CX > tl[0]) & (self.CX < tr[0])
            idx2 = (self.CY < tl[1]) & (self.CY > bl[1])
            idx = idx1 & idx2
            idx_list.append(idx)

    if len(idx_list) == 1:
        idx = idx_list[0]
    else:
        idx = idx_list[0] | idx_list[1]

    stopTime = time.clock()
    formatter = (sum(idx), num_movements, 100. * sum(idx) / num_movements,
                 string, stopTime - startTime)
    print "indexed %d/%d (%1.1fpercent) movements %s, took: %fs" % formatter
    self.idx_at_HB = idx
    return idx  # when at nest
Ejemplo n.º 2
0
def map_xbins_ybins_to_cage(self, rectangle=(0, 0), xbins=2, ybins=4):
    """ converts a rectangle in xbins x ybins to corresponding rectangle in Cage coordinates 
        format is [[p1, p2], [p3, p4]] where pi = (cage_height_location, cage_length_location)
        ### THIS GIVES WRONG CAGE LOCATIONS for top bottom left right
        # # # xbins ybins do NOT reflect cage geometry perfectly    
    """
    C = Cage()
    L = C.CMXUpper - C.CMXLower
    H = C.CMYUpper - C.CMYLower
    delta_x = L / xbins
    delta_y = H / ybins
    h, l = rectangle
    coord_y = C.CMYUpper - delta_y * h
    coord_x = C.CMXLower + delta_x * l
    return ((coord_x, coord_y), (coord_x + delta_x, coord_y),
            (coord_x, coord_y - delta_y), (coord_x + delta_x,
                                           coord_y - delta_y))
Ejemplo n.º 3
0
def designate_homebase(self, xbins=2, ybins=4):
    """ Takes self.CT, self.CX, self.CY data and tries to find Mouse cage Nest
        returns list of tuples, each tuple the index in 2 x 4 discretization of cage
        (e.g. most mice will return [(0,0)])
    """
    C = Cage()
    tot = getattr(self, 'bin_times_24H_xbins%d_ybins%d' % (xbins, ybins))
    idx = np.unravel_index(tot.argmax(), tot.shape)
    max_time = tot.max()
    # set observed homebase
    self.obs_HB = np.array(self.map_obs_rectangle_to_cage(
        (self.NLX, self.NLY)))  #obs_ethel
    # if largest time at niche
    if idx == (0, 0):  # and (max_time / tot.sum()) > .5:
        print "designated single homebase: niche, ", idx
        self.rect_HB = [idx]
        return [idx]

    # if not niche, use domino
    idx_domino = self.max_domino(tot)
    self.rect_HB = idx_domino
    return idx_domino
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def backwardFix(self):
    """
	this is the backwardsFix method to fix the sliding path issue.
	"""
    C = Cage()

    nan = np.nan
    # Get the uncorrected position data
    uncorrectedX = self.uncorrectedX
    uncorrectedY = self.uncorrectedY
    deltaX = self.deltaX
    deltaY = self.deltaY

    print "backward fixing.."
    # Create empty arrays
    backwardX = nan * uncorrectedX
    backwardY = nan * uncorrectedY
    # Set the last values
    #backwardX[-1] = mouseCMXSleeping #uncorrectedX[-1]
    #backwardY[-1] = mouseCMYSleeping #uncorrectedY[-1]

    # Loop backward through uncorrected X positions
    for i in range(len(uncorrectedX) + 1, 0, -1):
        # Position placeholders, a=x,b=y
        if i == len(uncorrectedX) + 1:
            #presume they are sleeping at the end.
            a = C.mouseCMXSleeping
            b = C.mouseCMYSleeping
            # For the first iteration, use the last uncorrected x and y coordinates
            #a = uncorrectedX[-1]
            #b = uncorrectedY[-1]
        else:
            # Otherwise, use the last corrected position with delta x and y
            a = backwardX[i - 1] - deltaX[i - 2]
            b = backwardY[i - 1] - deltaY[i - 2]

        # Use the cage boundaries if the x position is in violation
        a = max(C.CMXLower, a)
        a = min(C.CMXUpper, a)

        # Use the cage boundaries if the y position is in violation
        b = max(C.mouseCMYAtPhoto, b)
        b = min(C.CMYUpper, b)

        # Apply the nest boundaries
        if a < C.nestRightX:
            b = min(b, C.nestTopY)
        if b > C.nestBottomY:
            a = max(a, C.nestLeftX)
        if a > C.enclosurePenaltyLowerBoundXMin:
            b = max(b, 1)

        # Update the x and y positions after all comparisons
        backwardX[i - 2] = a
        backwardY[i - 2] = b

    backwardX[-1] = backwardX[
        -2]  #a bad fix of a larger problem that the last entries of backward are super crazy
    backwardY[-1] = backwardY[-2]

    # Store the backward corrected position data
    self.backwardX = backwardX
    self.backwardY = backwardY

    #We assume you calculated it because you want to use it / go with it
    self.CT = self.uncorrectedT
    self.CX = self.backwardX
    self.CY = self.backwardY
Ejemplo n.º 6
0
def draw_position_density_tbins2(E,
                                 data,
                                 mouseNumbers,
                                 tbin_type,
                                 err_type,
                                 xbins,
                                 ybins,
                                 fig_title,
                                 fname,
                                 rect_HB=None,
                                 obs_rect=None,
                                 plot_type='position_density',
                                 ADD_SOURCE=False):

    C = Cage()

    # figure
    figsize = (6.4, 6.4)
    nrows, ncols = data.shape[0], data.shape[1]

    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]

    row_labels = ['M%d' % m for m in mouseNumbers]
    column_labels = ['D%d' % d for d in E.daysToUse]

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

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

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

                Z3 = data[r, c, :, :, 0]  # plt average only for the moment

                if np.isnan(Z3).all():
                    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)

                    set_position_density_layout(fig, ax, im)

                    if (xbins, ybins) == (2, 4):
                        draw_homebase_star(ax,
                                           rect_HB[r, 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)

    add_titles_notes(E,
                     fig,
                     title=fig_title,
                     titlesize=14,
                     typad=.08,
                     TL_NOTE=False)

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

    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE, sypad=.03)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
def draw_it(E, fig, ax, Z3, subplot_num, level, subtitles, xbins, ybins,
            rect_HB, obs_rect):

    C = Cage()

    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]

    if np.isnan(Z3).all():
        ax.text(.5,
                .5,
                subtitles[subplot_num] + '\nexcluded',
                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[subplot_num], fontsize=10)
        set_position_density_layout(fig, ax, im)

        if (xbins, ybins) == (2, 4):
            draw_percent_time(ax, Z3 / Z3.sum(), color=percent_time_color)
            if level == 'mouseday':
                draw_homebase_star(ax,
                                   rect_HB[subplot_num],
                                   obs_rect[subplot_num],
                                   color=percent_time_color)

        set_colorbar(fig, ax, im)

    # add some labeling
    if level == 'group':
        show_devices_labels(E, ax)

    elif level == 'mouseday':
        # show fast
        if E.short_name == '2CFast':
            if hasattr(E, 'fastDayNumbers'):
                day = int(subtitles[subplot_num][1:])
                if day in E.fastDayNumbers + E.reFeedDayNumbers:
                    text = 'FAST' if day in E.fastDayNumbers else 'REFEED'
                    ax.text(.15,
                            1.03,
                            text,
                            fontsize=14,
                            color=E.fcolors['F'][0],
                            ha='right',
                            va='bottom',
                            transform=ax.transAxes)

    return fig, ax