Пример #1
0
def _ax_utsStats(segments, sensor, 
                 plotMean=True,
                 all=False,
                 ax=None,
                 lineprops={},   # c, ls, lw; doct or list of dicts 
                                 # e.g. [{'c':'b'},{'c':'r'},{'c':'g'},'m','c'],
                 test_segment=False, p=.05, softStats=False, #testWindowFreq='max',
                 sem=None,        # 'sem' (float multiplier)
                 ylim=None,
                 plotLabel=False,
                 statsColorspace=None,
                 **kwargs):
    """
    takes a list of segments and plots one sensor to ax. 
    
    
    Arguments
    ---------
    
    sem: = None or float
        plot standard error of the mean (e.g., ``sem=2`` plots the mean +/- 2
        sem)
    test_segment:
        submit a test_segment to add to plot (efficient because usually 
        _ax_utsStats is called more than once for several segments     
    plottype:
        ``'mean'``: plots the mean for each stats segment
        ``'all'``:  plots all data traces contained in the stats, colors mark the 
        segment NOT MAINTAINED
    
	NOT IMPLEMENTED segments: submit mean segments
	
    """
    # prepare arguments
    if statsColorspace is None:
        statsColorspace = _cs.get_sig_white()
    if ax is None:
        logging.debug(" _ax_utsStats got None ax")
        ax = P.gca()
    if ylim is None:
        ylim = segments[0].properties.get('ylim', None)
    
    # prepare args
    segments = _basic_ops_.toTuple(segments)
    seg = segments[0]
    assert seg.ndim == 1
    if ylim is None:
        if all:
            minV, maxV = -7.5, 7.5
        else:
            minV, maxV = -1.5, 1.5
    else:
        minV, maxV = -ylim, ylim
    # t
#    tmin = -seg.t0; 
#    xaxis_length = len(seg)
#    tmax = ( float(xaxis_length)/seg.samplingrate ) - seg.t0
#    t = np.r_[tmin : tmax : xaxis_length*1j]

    # line properties
    if type(lineprops) == dict:
        lineprops = [deepcopy(lineprops) for i in range(len(segments))]
        # (need separate instance because properties are is modified later)
    
    #plot
    handlesForLegend=[]
    for s, lp in zip(segments, lineprops):
        line_properties = {'alpha':.2,
                           'zorder':-1}
        if ('color' not in lp) and s.color:
            lp['color']=s.color
        
        line_properties.update(lp)
        mean = s.mean().subdata(out='data', sensors=[sensor])[:,0] #data[:,sensor]
        t = s.t
        
        logging.debug("_ax_utsStats plotting '%s'"%s.name)
        if plotMean:
            line = ax.plot(t, mean, label=s.name, **lp)[0] # label=label
            line_properties.update({'color':line.get_color()})
        if all:
            #print "%s, %s"%(sensor, type(sensor))
            single_line = ax.plot(t[...,None], s.data[:,sensor], **line_properties)[0]
        if sem:
            #print "%s, %s"%(sensor, type(sensor))
            line_properties.update({'alpha':.2})
            sem_array = sp.stats.sem(s.data[:,sensor], axis=-1) * sem
            ax.fill_between(t, mean-sem_array, mean+sem_array, **line_properties)
        handlesForLegend.append(line)
    
    # p-bar
    if test_segment:
        imKwargs={'zorder': -100}
        statsColorspace.toKwargs(imKwargs)
        im = test_segment.P[:, [sensor]].T
#        l = len(im) 
#        im = np.ma.array(np.vstack([np.ones(l), im, np.ones(l)]), 
#                         mask=np.vstack([np.ones(l), im>p, np.ones(l)]))
        if softStats:
            pad = np.ones(im.shape)
            im = np.vstack((pad,im,im,im,pad))
            imKwargs['interpolation'] = 'bicubic'
        else:
            imKwargs['interpolation'] = 'nearest'
        
        extent = (t[0], t[-1]) + ax.get_ylim()
        
        ax.imshow(im, extent=extent, aspect='auto', **imKwargs)
    else:
        ax.set_xlim(t[0], t[-1])
    
    if plotLabel:
        ax.text(t[0]/2, maxV/2, plotLabel, horizontalalignment='center')
    return handlesForLegend
Пример #2
0
def array(segments, sensors=None, plotAllSubjects=False, plotCbar=True, test='nonparametric', p=.05, **kwargs):
    """
    NOT MAINTAINED
    plots tv plots to a rectangular grid instead of topographic spacing
     
    kwargs:
    sensors: List of sensor IDs
    test='nonparametric', None if len(segments) > 2
    
    """
    P.clf()
    # prepare args
    segments = _basic_ops_.toTuple(segments)
    kwargs['test']=test
    if sensors==None:
        sensors = range(len(segments[0].sensors))
    else:
        sensors = _basic_ops_.toTuple(sensors)
    statsColorspace = _cs.get_sig(p)
    # determine plotting grid
    nPlots = len(sensors) * (1+plotAllSubjects) + plotCbar
    nColumnsEst = np.sqrt( nPlots ) 
    nColumns    = int(nColumnsEst)
    if nColumns != nColumnsEst:
        nColumns += 1
    if nColumns % (1+plotAllSubjects) != 0:
        nColumns += 1
    nRows = int( nPlots / nColumns ) + (1- ( nPlots % nColumns == 0 ))
    logging.debug("plotting electrodes %s, fig shape (%s, %s)"%(str(sensors), nRows, nColumns))
#    fig = P.gcf()
    P.subplots_adjust(left=.05, bottom=.075, right=.99, top=.94, wspace=.25, hspace=.3)
    #grid = AxesGrid(    fig, 111,
    #                    nrows_ncols = (nRows, nColumns),
    #                    axes_pad = .01 )
    # plot
    kwargs['labelNames']=True
    for i,sensor in enumerate(sensors):
        kwargs['lastRow'] = ( i > (nRows-1) * nColumns)
        if plotAllSubjects:
            #axes = grid[ (i+1)*2 ] # 
            axes = P.subplot(nRows, nColumns, (i+1) * 2 -1 )
            kwargs["plotType"]='mean'
            _ax_utsStats(segments, sensor, statsColorspace=statsColorspace, **kwargs)
            axes.set_title( segments[0].sensors[sensor].name )
            #axes = grid[ (i+1)*2+1 ] #
            axes = P.subplot(nRows, nColumns, (i+1) * (1+plotAllSubjects) )
            kwargs["plotType"]='all'
            _ax_utsStats(segments, sensor, statsColorspace=statsColorspace, **kwargs)
            axes.set_title( ', '.join(( segments[0].sensors[sensor].name, "All Subjects")) )
        else:
            #axes = grid[i] #
            axes = P.subplot(nRows, nColumns, (i+1) )
            kwargs["plotType"]='mean'
            _ax_utsStats(segments, sensor, statsColorspace=statsColorspace, 
                            firstColumn=( i%nColumns==0 ),
                            **kwargs)
            axes.set_title( segments[0].sensors[sensor].name )
        kwargs['labelNames']=False
    if test!=None and plotCbar:
        #axes = grid[-1] #
        axes = P.subplot(nRows, nColumns, nRows*nColumns)
        pos = axes.get_position()
        newPos = [pos.xmin, pos.ymin+pos.width*.3, pos.width, pos.width*.15]
        axes.set_position(newPos)
        statsColorspace.toAxes_(axes)