Exemplo n.º 1
0
def bootstrap(arr,estimator,a,n,axis=None):
    if axis is None:
        arr = arr.ravel()
        axis = 0 
    t = [along(estimator,axis,along(samplewr,axis,arr)) for i in xrange(n)]
    return array((along(lambda x: sap(x,a),0,t),
                  along(lambda x: sap(x,1-a),0,t))).T #can simplify on newer scipy package
Exemplo n.º 2
0
def plotB2reg(prefix=''):
    w=loadStanFit(prefix+'revE2B2LHregCa.fit')
    px=np.array(np.linspace(-0.5,0.5,101),ndmin=2)
    a1=np.array(w['ma'][:,4],ndmin=2).T+1
    a0=np.array(w['ma'][:,3],ndmin=2).T
    printCI(w,'ma')
    y=np.concatenate([sap(a0+a1*px,97.5,axis=0),sap(a0+a1*px[:,::-1],2.5,axis=0)])
    x=np.squeeze(np.concatenate([px,px[:,::-1]],axis=1))
    man=np.array([-0.4,-0.2,0,0.2,0.4])
    plt.plot(px[0,:],np.median(a0)+np.median(a1)*px[0,:],'red')
    #plt.plot([-1,1],[0.5,0.5],'grey')
    ax=plt.gca()
    ax.set_aspect(1)
    ax.add_patch(plt.Polygon(np.array([x,y]).T,alpha=0.2,fill=True,fc='red',ec='w'))
    y=np.concatenate([sap(a0+a1*px,75,axis=0),sap(a0+a1*px[:,::-1],25,axis=0)])
    ax.add_patch(plt.Polygon(np.array([x,y]).T,alpha=0.2,fill=True,fc='red',ec='w'))
    mus=[]
    for m in range(len(man)):
        mus.append(loadStanFit(prefix+'revE2B2LHC%d.fit'%m)['ma4']+man[m])
    mus=np.array(mus).T
    errorbar(mus,x=man)
    ax.set_xticks(man)
    plt.xlim([-0.5,0.5])
    plt.ylim([-0.6,0.8])
    plt.xlabel('Pivot Displacement')
    plt.ylabel('Perceived Displacemet')
Exemplo n.º 3
0
def pystanErrorbar(w, keys=None):
    """ plots errorbars for variables in fit
        fit - dictionary with data extracted from Pystan.StanFit instance 
    """
    kk = 0
    ss = []
    sls = []
    if keys is None: keys = w.keys()[:-1]
    for k in keys:
        d = w[k]
        if d.ndim == 1:
            ss.append(d)
            sls.append(k)
            continue
            #d=np.array(d,ndmin=2).T
        d = np.atleast_3d(d)
        for h in range(d.shape[2]):
            kk += 1
            figure(num=kk)
            #ppl.boxplot(plt.gca(),d[:,:,h],sym='')
            errorbar(d[:, :, h])
            plt.title(k)
    #ss=np.array(ss)
    for i in range(len(ss)):
        print(sls[i], ss[i].mean(),
              'CI [%.3f,%.3f]' % (sap(ss[i], 2.5), sap(ss[i], 97.5)))
Exemplo n.º 4
0
def plotB3reg():
    w=loadStanFit('revE2B3BHreg.fit')
    printCI(w,'mmu')
    printCI(w,'mr')
    for b in range(2):
        subplot(1,2,b+1)
        plt.title('')
        px=np.array(np.linspace(-0.5,0.5,101),ndmin=2)
        a0=np.array(w['mmu'][:,b],ndmin=2).T
        a1=np.array(w['mr'][:,b],ndmin=2).T
        y=np.concatenate([sap(a0+a1*px,97.5,axis=0),sap(a0+a1*px[:,::-1],2.5,axis=0)])
        x=np.squeeze(np.concatenate([px,px[:,::-1]],axis=1))
        plt.plot(px[0,:],np.median(a0)+np.median(a1)*px[0,:],'red')
        #plt.plot([-1,1],[0.5,0.5],'grey')
        ax=plt.gca()
        ax.set_aspect(1)
        ax.add_patch(plt.Polygon(np.array([x,y]).T,alpha=0.2,fill=True,fc='red',ec='w'))
        y=np.concatenate([sap(a0+a1*px,75,axis=0),sap(a0+a1*px[:,::-1],25,axis=0)])
        ax.add_patch(plt.Polygon(np.array([x,y]).T,alpha=0.2,fill=True,fc='red',ec='w'))
        man=np.array([-0.4,-0.2,0,0.2,0.4])
        mus=[]
        for m in range(len(man)):
            mus.append(loadStanFit('revE2B3BH%d.fit'%m)['mmu'][:,b])
        mus=np.array(mus).T
        errorbar(mus,x=man)
        ax.set_xticks(man)
        plt.xlim([-0.5,0.5])
        plt.ylim([-0.4,0.8])
        #plt.xlabel('Manipulated Displacement')
        if b==0:
            plt.ylabel('Perceived Displacemet')
            plt.gca().set_yticklabels([])
        subplot_annotate()
    plt.text(-1.1,-0.6,'Pivot Displacement',fontsize=8);
Exemplo n.º 5
0
def errorbar(y, clr=CLR, x=None, labels=None):
    ''' customized error bars
        y - NxM ndarray containing results of
            N simulations of M random variables
        x - array with M elements, position of the bars on x axis 
        clr - bar color
        labels - array with xtickslabels

        >>> errorbar(np.random.randn(1000,10)+1.96)    
    '''
    out = []
    d = np.array(y)
    if d.ndim < 2: d = np.array(y, ndmin=2).T
    if not x is None: x = np.array(x)
    if x is None or x.size == 0: x = np.arange(d.shape[1])
    elif x.size == 1: x = np.ones(d.shape[1]) * x[0]
    elif x.ndim != 1 or x.shape[0] != d.shape[1]:
        x = np.arange(0, d.shape[1])
    ax = plt.gca()
    for i in range(d.shape[1]):
        out.append([
            np.median(d[:, i]),
            sap(d[:, i], 2.5),
            sap(d[:, i], 97.5),
            sap(d[:, i], 25),
            sap(d[:, i], 75)
        ])
        _errorbar(out[-1], x=x[i], clr=clr)
    ax.set_xticks(x)
    if not labels is None: ax.set_xticklabels(labels)
    plt.xlim([np.floor(np.min(x) - 1), np.ceil(np.max(x) + 1)])
    return np.array(out)
Exemplo n.º 6
0
def errorbar(y,clr=CLR,x=None,labels=None):
    ''' customized error bars
        y - NxM ndarray containing results of
            N simulations of M random variables
        x - array with M elements, position of the bars on x axis 
        clr - bar color
        labels - array with xtickslabels

        >>> errorbar(np.random.randn(1000,10)+1.96)    
    '''
    out=[]
    d=np.array(y);
    if d.ndim<2: d=np.array(y,ndmin=2).T
    if not x is None: x=np.array(x)
    if x is None or x.size==0: x=np.arange(d.shape[1])
    elif x.size==1: x=np.ones(d.shape[1])*x[0]
    elif x.ndim!=1 or x.shape[0]!=d.shape[1]:
        x=np.arange(0,d.shape[1])
    ax=plt.gca()
    for i in range(d.shape[1]):
        out.append([np.median(d[:,i]),sap(d[:,i],2.5),sap(d[:,i],97.5),
                    sap(d[:,i],25),sap(d[:,i],75)])
        _errorbar(out[-1],x=x[i],clr=clr)
    ax.set_xticks(x)
    if not labels is None: ax.set_xticklabels(labels)
    plt.xlim([np.floor(np.min(x)-1),np.ceil(np.max(x)+1)])
    return np.array(out)
Exemplo n.º 7
0
def bootstrap(arr, estimator, a, n, axis=None):
    if axis is None:
        arr = arr.ravel()
        axis = 0
    t = [along(estimator, axis, along(samplewr, axis, arr)) for i in xrange(n)]
    return array((along(lambda x: sap(x, a), 0,
                        t), along(lambda x: sap(x, 1 - a), 0,
                                  t))).T  #can simplify on newer scipy package
Exemplo n.º 8
0
def _horebar(d,xs,clr):
    ''' code snippet for horizontal errorbar'''
    for i in range(d.shape[1]):
        x=xs[i]
        plt.plot([sap(d[:,i],2.5),sap(d[:,i],97.5)],[x,x],color=clr)
        plt.plot([sap(d[:,i],25),sap(d[:,i],75)],[x,x],
            color=clr,lw=3,solid_capstyle='round')
        plt.plot([np.median(d[:,i])],[x],mfc=clr,mec=clr,ms=8,marker='|',mew=2)
    plt.gca().set_yticks(xs)
Exemplo n.º 9
0
def stdval(q):
    if not isinstance(q, Uval):
        return 0.0

    # TODO: figure out how to handle asymmetric error distributions! Should
    # pay attention to where this function is actually used to think about
    # the semantics that'd be the most helpful.

    return 0.5 * (sap(q.d, 84.134) - sap(q.d, 15.866))
def confint(arr):
    """Returns the median in between the 95% HPD interval of the array.
    """
    res=[[],[],[]]
    #r=hpd(arr)
    r=(sap(arr,2.5),sap(arr,97.5))
    res[0]=r[0]
    res[1]=arr.mean(0)
    res[2]=r[1]
    return np.array(res)
Exemplo n.º 11
0
def plotB3fit(fit,suffix='',pars=['mu','mmu']):
    plt.figure(figsize=(6,3))
    D=fit.extract()[pars[0]]
    errorbar(D[:,:,X],x=0.9)
    plt.xlabel('Subject')
    errorbar(D[:,:,Y],x=1.1,clr=(1,0.5,0.5))
    plt.ylabel('Displacement in Degrees')
    if pars[1] is '': return
    d=fit.extract()[pars[1]]
    print 'X: %.3f, CI %.3f, %.3f'%(d[:,0].mean(), sap(d[:,0],2.5),sap(d[:,0],97.5))
    print  'Y: %.3f, CI %.3f, %.3f'%(d[:,1].mean(), sap(d[:,1],2.5),sap(d[:,1],97.5))
Exemplo n.º 12
0
def plotComp():
    #E=[]
    D=[]
    for i in [1,2]:
        if i==2:
            find= len(D)
            D.append(np.load('force.npy'))
        if i==2: w=loadStanFit('revE2B2LHregCa.fit')
        else: w=loadStanFit('revE1B1LH.fit')
        D.append(w['ma'][:,3])
        if i==2: 
            w=loadStanFit('revE%dB2WHreg.fit'%i)
            D.append((w['ma0']-0.5)/w['ma1'])
            printCI(D[-1])
            w=loadStanFit('revE%dB3BHreg.fit'%i)
        else:
            w=loadStanFit('revE1B3BH.fit')
        D.append(w['mmu'][:,0])
        if i==2:vpn=range(351,381); vpn.remove(369); vpn.remove(370)
        else: vpn=range(301,314)
        B3,B4,B5=loadDataB345(vpn,correct=False,exp=i)
        b4=plotB5([B5],[vpn],exps=[i])
        plt.close()
        D.append(b4[0])
        D.append(b4[1])
        #E.append(D)
    clr=(0.2, 0.5, 0.6)
    figure(size=3,aspect=0.5)#0.66)
    for i in range(len(D)):
        if i in [0,1,5,6,7]:
            dat=[D[i].mean(),sap(D[i],2.5),sap(D[i],97.5),sap(D[i],25),sap(D[i],75) ]
        elif i==find:dat=D[i]
        else:
            err= D[i][2]* stats.t.ppf(0.975,D[i][3])
            err2= D[i][2]* stats.t.ppf(0.75,D[i][3])
            dat=[D[i][1],D[i][1]-err, D[i][1]+err,D[i][1]-err2, D[i][1]+err2]
        inc=[1,2][int(i>3)]
        plt.plot([dat[1],dat[2]],[i+inc,i+inc],color=clr)
        plt.plot([dat[3],dat[4]],[i+inc,i+inc],
            color=clr,lw=3,solid_capstyle='round')
        plt.plot([dat[0]],[i+inc],mfc=clr,mec=clr,ms=8,marker='+',mew=2)
    ax=plt.gca()
    plt.ylim([0,len(D)+2])
    plt.xlabel('Perceived Displacement')
    plt.xlim([-0.05,0.4])
    #subplot_annotate(loc=[0.95,0.9])
    plt.grid()
    plt.ylabel('Bugs'+' '*15+'Darts',fontsize=14)
    ax.set_yticks(range(1,len(D)+2))
    ax.set_yticklabels(['Location Recall','Distance Bisection',
        'Static Recall','Static Localization','','Mouse Position','Location Recall',
        'Leave-Me-Alone','Distance Bisection','Static Recall',
                    'Static Localization'])
Exemplo n.º 13
0
def FD_bins(data):
        """
        Calculates the optimal number of bins for a histogram based on Freedman-Diaconis equation
        """
        from scipy.stats import scoreatpercentile as sap
        
        data = np.array(data)
        #Use the Freedman-Diaconis method to calculate the bin number
        IQR = sap(data,75) - sap(data,25)
        width = 2*IQR/len(data)**(1./3.)
        bins = np.ceil((max(data)-min(data))/width)
        
        return bins
Exemplo n.º 14
0
def convertNumpyToCsv(filename, csvName):
    #  data = io.loadmat("data/Patient_1/Patient_1_interictal_segment_42.mat")

    data = io.loadmat(filename)
    np.set_printoptions(threshold=np.nan)

    print "Preparing to output %s data to %s" % (filename, csvName)
    outputFile = open(csvName, "w")

    outputWriter = csv.writer(outputFile)

    #header row on csv
    l_channels = ["class", "latency", "MAD", "IQD"]
    for l_channel in data['channels'][0][0]:
        l_channels.append(str(l_channel[0]))
    #print "l_channels: " + str(l_channels)
    outputWriter.writerow(l_channels)

    #associated data information
    print "freq: " + str(data['freq'])
    dt = 1.0 / float(data['freq'])

    if "_ictal" in filename:
        print "latency: " + str(data['latency'])
        time = float(data['latency'])
        dclass = 1
    else:
        dclass = -1
        time = -1
        dt = 0

    buf = np.zeros((BUF_SIZE, np.transpose(data['data']).shape[1]))
    i = 0
    #Write the class (ictal vs interical), the latency of the sample, and the
    #amplitude from all sources:
    for l_row in np.transpose(data['data']):
        buf[i, :] = l_row
        row = np.zeros((l_row.shape[0] + 4))

        row[0] = dclass
        row[1] = time
        row[2] = np.mean(abs(buf[i] -
                             np.mean(buf[i, :])))  #Mean Absolute Difference
        row[3] = sap(buf, 75) - sap(
            buf, 25)  #Interquartile Difference (All channels)
        row[4:] = l_row
        outputWriter.writerow(row)
        time += dt
        i = (i + 1) % BUF_SIZE

    outputFile.close()
Exemplo n.º 15
0
def plot(figname='stan.png', dpi=300):
    from matusplotlib import figure, subplot, plt
    figure(size=3, aspect=0.3)
    il = [
        'dog', 'trolley', 'wallet', 'plane', 'resume', 'kitten', 'mean score',
        'median score'
    ]
    w = loadStanFit('schnall')
    S = np.load('S.npy')
    offset = np.array(w['c'][:, 0] / 2 + w['c'][:, -1] / 2, ndmin=2).T
    scale = np.array(np.abs(w['c'][:, 0] - w['c'][:, -1]) / 2, ndmin=2).T
    bp = np.linspace(-2.2, 2.2, 51)
    b = bp * np.median(scale) + np.median(offset)
    cs = np.median(w['c'], axis=0)
    d = np.median(-w['d'], axis=0)
    #xlm=[cs[0]-0.2,cs[-1]+0.2]
    xlm = b[[0, -1]]
    cls = []
    for i in range(cs.size):
        cls.append('$c_%d$' % i)
    tmp = np.median(-w['tbeta'], axis=0)

    for j in range(2):
        ax = subplot(1, 2, 1 + j)
        plt.plot(xlm, [-d[0], -d[0]])
        plt.xlim(xlm)
        ax.set_xticks(cs)
        ax.set_xticklabels(cls)
        plt.plot(tmp[0, :], -0.7 * np.ones(6), 'xg')
        plt.plot(tmp[1, :], -0.9 * np.ones(6), 'xr')
        #for k in range(tmp.shape[1]):
        #    plt.plot(tmp[0:,k],[-0.09,-0.11],'k',alpha=0.2)
        ds = np.zeros((len(S), 3)) * np.nan
        for i in range(len(S)):
            if j:
                wi = loadStanFit('schnSimBB%02d' % i)
                ds[i, :] = sap(wi['gg1'][:, 1] - wi['gg2'][:, 1],
                               [2.5, 50, 97.5])
            elif j == 0:
                wi = loadStanFit('schnSimOLR%02d' % i)
                ds[i, :] = sap(wi['d'], [2.5, 50, 97.5])
        plt.plot(b, ds[:, 1], 'k')
        temp = [list(b) + list(b)[::-1], list(ds[:, 0]) + list(ds[:, 2])[::-1]]
        ax.add_patch(
            plt.Polygon(xy=np.array(temp).T, alpha=0.2, fc='k', ec='k'))
        plt.ylim([[-2, 4], [-2, 4]][j])
        plt.ylabel('$c_u^\Delta$')
        plt.xlabel(['$c_u=-c_l$', '$c_u$'][j])
        plt.title(['OLRM', 'Beta-Binomial'][j])
        plt.grid(axis='x')
    plt.savefig(figname, bbox_inches='tight', dpi=dpi)
Exemplo n.º 16
0
def convertNumpyToCsv(filename, csvName):
#  data = io.loadmat("data/Patient_1/Patient_1_interictal_segment_42.mat")

  data = io.loadmat(filename)
  np.set_printoptions(threshold=np.nan)


  print "Preparing to output %s data to %s" % (filename, csvName)
  outputFile = open(csvName, "w")

  outputWriter = csv.writer(outputFile)

  #header row on csv
  l_channels = ["class","latency","MAD","IQD"]
  for l_channel in data['channels'][0][0]:
      l_channels.append(str(l_channel[0]))
  #print "l_channels: " + str(l_channels)
  outputWriter.writerow(l_channels)

  #associated data information
  print "freq: " + str(data['freq'])
  dt = 1.0/float(data['freq'])

  if "_ictal" in filename:
      print "latency: " + str(data['latency'])
      time = float(data['latency'])
      dclass = 1
  else:
      dclass = -1
      time = -1
      dt = 0

  buf = np.zeros((BUF_SIZE,np.transpose(data['data']).shape[1]))
  i = 0
  #Write the class (ictal vs interical), the latency of the sample, and the
  #amplitude from all sources:
  for l_row in np.transpose(data['data']):
      buf[i,:] = l_row
      row = np.zeros((l_row.shape[0]+4))

      row[0] = dclass
      row[1] = time
      row[2] = np.mean(abs(buf[i]-np.mean(buf[i,:]))) #Mean Absolute Difference
      row[3] = sap(buf,75)-sap(buf,25) #Interquartile Difference (All channels)
      row[4:] = l_row
      outputWriter.writerow(row)
      time += dt
      i = (i+1)%BUF_SIZE

  outputFile.close()
Exemplo n.º 17
0
def _select_sigma(X):
    """
    Returns the smaller of std(X, ddof=1) or normalized IQR(X) over axis 0.

    References
    ----------
    Silverman (1986) p.47
    """
#    normalize = norm.ppf(.75) - norm.ppf(.25)
    normalize = 1.349
#    IQR = np.subtract.reduce(percentile(X, [75,25],
#                             axis=axis), axis=axis)/normalize
    IQR = (sap(X, 75) - sap(X, 25))/normalize
    return np.minimum(np.std(X, axis=0, ddof=1), IQR)
Exemplo n.º 18
0
def _select_sigma(X):
    """
    Returns the smaller of std(X, ddof=1) or normalized IQR(X) over axis 0.

    References
    ----------
    Silverman (1986) p.47
    """
#    normalize = norm.ppf(.75) - norm.ppf(.25)
    normalize = 1.349
#    IQR = np.subtract.reduce(percentile(X, [75,25],
#                             axis=axis), axis=axis)/normalize
    IQR = (sap(X, 75) - sap(X, 25))/normalize
    return np.minimum(np.std(X, axis=0, ddof=1), IQR)
 def write_vals(self,saveTo=None):
     """ Saves estimated parameters to csv file. 
     
     Input:
     - m (Model)
     - saveTo (str) - path and name of csv file where results should be 
     saved. Defaults to mod.saveTo+'-posteriorValues.csv'.
     """
     m=self
     if saveTo==None:
         fname=m.saveTo+'-posteriorValues.csv'
     else:
         fname=saveTo
     f=open(fname,'w')
     f.write('\t'.join(['Parameter','mean','median','95% HPD','std'])+'\n')
     for v in m.parameters:
         trac=getattr(m,v+'s')
         hpdi=ut.hpd(trac,0.95)
         form='%.2f'
         if v.startswith('p') or v.startswith('k') or v.startswith('e'):
             form='%.2e'
         
         f.write('\t'.join([v,form%trac.mean(),form%sap(trac,50),('['+form+', '+form+']')%hpdi,form%trac.std()])+'\n')
     
     f.close()
     if saveTo==None:
         print "Saved posterior median and confidence intervals for each parameter, see "+ m.name+'-posterior_values.csv'
     else:
         print "Saved posterior median and confidence intervals for each parameter, see "+ saveTo
Exemplo n.º 20
0
def _horebar(d, xs, clr):
    ''' code snippet for horizontal errorbar'''
    for i in range(d.shape[1]):
        x = xs[i]
        plt.plot([sap(d[:, i], 2.5), sap(d[:, i], 97.5)], [x, x], color=clr)
        plt.plot([sap(d[:, i], 25), sap(d[:, i], 75)], [x, x],
                 color=clr,
                 lw=3,
                 solid_capstyle='round')
        plt.plot([np.median(d[:, i])], [x],
                 mfc=clr,
                 mec=clr,
                 ms=8,
                 marker='|',
                 mew=2)
    plt.gca().set_yticks(xs)
Exemplo n.º 21
0
def bestval(q):
    if not isinstance(q, Uval):
        return q

    # http://www.roma1.infn.it/~dagos/asymmetric/node2.html says that the
    # "best" value is the barycenter of the distribution, i.e. the mean of our
    # samples (right?), not the median. TODO: investigate.

    return sap(q.d, 50.000)
Exemplo n.º 22
0
    def fit(self, frame):
        cx, cy = centroid_m(frame)
        sky = median(frame)
        amp = sap(frame - sky, 99)
        bounds = [[0.8 * amp, 1.2 * amp], [0.2 * self.sx, 0.8 * self.sx],
                  [0.2 * self.sy, 0.8 * self.sy], [0.5, 20], [1, 25],
                  [0.8 * sky, 1.2 * sky], [-10, 10], [-10, 10]]

        self.pv0 = pv0 = [amp, cx, cy, 7, 6, sky, 0, 0]
        self.fit_result = minimize(self.chi2, pv0, (frame, ), bounds=bounds)
        return rt(*self.fit_result.x)
Exemplo n.º 23
0
def pystanErrorbar(w,keys=None):
    """ plots errorbars for variables in fit
        fit - dictionary with data extracted from Pystan.StanFit instance 
    """
    kk=0
    ss=[];sls=[]
    if keys is None: keys=w.keys()[:-1]
    for k in keys:
        d= w[k]
        if d.ndim==1:
            ss.append(d);sls.append(k)
            continue
            #d=np.array(d,ndmin=2).T
        d=np.atleast_3d(d)
        for h in range(d.shape[2]):
            kk+=1; figure(num=kk)
            #ppl.boxplot(plt.gca(),d[:,:,h],sym='')
            errorbar(d[:,:,h])
            plt.title(k)
    #ss=np.array(ss)
    for i in range(len(ss)):
        print sls[i], ss[i].mean(), 'CI [%.3f,%.3f]'%(sap(ss[i],2.5),sap(ss[i],97.5)) 
Exemplo n.º 24
0
def plotB2Wreg():
    w=loadStanFit('revE2B2WHreg.fit')
    a1=np.array(w['ma1'],ndmin=2).T
    a0=np.array(w['ma0'],ndmin=2).T
    px=np.array(np.linspace(-0.5,0.5,101),ndmin=2)
    printCI(w,'ma0');printCI(w,'ma1')
    y=np.concatenate([sap(a0+a1*px,97.5,axis=0),sap(a0+a1*px[:,::-1],2.5,axis=0)])
    x=np.squeeze(np.concatenate([px,px[:,::-1]],axis=1))
    plt.plot(px[0,:],np.median(a0)+np.median(a1)*px[0,:],'red')
    #plt.plot([-1,1],[0.5,0.5],'grey')
    ax=plt.gca()
    ax.add_patch(plt.Polygon(np.array([x,y]).T,alpha=0.2,fill=True,fc='red',ec='red'))
    y=np.concatenate([sap(a0+a1*px,75,axis=0),sap(a0+a1*px[:,::-1],25,axis=0)])
    ax.add_patch(plt.Polygon(np.array([x,y]).T,alpha=0.2,fill=True,fc='red',ec='red'))
    mus=[];man=np.array([-0.4,-0.2,0,0.2,0.4])
    for m in range(len(man)):
        mus.append(loadStanFit('revE2B2WBB%d.fit'%m)['mmu'])
    mus=np.array(mus).T
    errorbar(mus,x=man)
    ax.set_xticks(man)
    plt.xlim([-0.5,0.5])
    plt.ylim([0.4,0.56])
    plt.xlabel('Pivot Displacement')
    plt.ylabel('Prop. of Time in Wolfpack Quadrants')
Exemplo n.º 25
0
    def find_stars(self,
                   treshold=99.5,
                   maxn=10,
                   target_sky=None,
                   target_pix=None,
                   mf_size=6):
        filtered_flux = mf(self.reduced, mf_size)
        objects = filtered_flux > sap(filtered_flux, treshold)
        labels, nl = label(objects)
        fluxes = [self.reduced[labels == l].mean() for l in range(1, nl + 1)]
        fids = argsort(fluxes)[::-1] + 1
        maxn = min(maxn, nl)
        self.nstars = maxn

        sorted_labels = zeros_like(labels)
        for i, fid in enumerate(fids[:maxn]):
            sorted_labels[labels == fid] = i + 1
        cpix = flip(
            array([
                com(self.reduced - median(self.reduced), sorted_labels, i)
                for i in range(1, maxn + 1)
            ]), 1)

        if self._wcs and target_sky is not None:
            target_pix = array(target_sky.to_pixel(self._wcs))
            cpix = concatenate([atleast_2d(target_pix), cpix])
        elif target_pix is not None:
            cpix = concatenate([atleast_2d(target_pix), cpix])

        self._initialize_tables(self.nstars, self.napt)
        if self._wcs:
            csky = pd.DataFrame(self._wcs.all_pix2world(cpix, 0),
                                columns='RA Dec'.split())
            self.set_reference_stars(cpix, csky=csky)
        else:
            self.set_reference_stars(cpix)
Exemplo n.º 26
0
def ufmt(q):
    if not isinstance(q, Uval):
        return '%g' % q

    # Same potential issue re: median-taking as mentioned above.

    lo = sap(q.d, 15.866)
    md = sap(q.d, 50.000)
    hi = sap(q.d, 84.134)

    if hi == lo:
        return '%g' % lo

    # Deltas. Round to limited # of places because we don't actually know the
    # fourth moment of the thing we're trying to describe.

    from numpy import abs, ceil, floor, log10

    dh = hi - md
    dl = md - lo

    p = int(ceil(log10(dh)))
    rdh = round(dh * 10**(-p), uplaces) * 10**p
    p = int(ceil(log10(dl)))
    rdl = round(dl * 10**(-p), uplaces) * 10**p

    # The least significant place to worry about is the L.S.P. of one of the
    # deltas, which is also its M.S.P. since we've just rounded them. Any
    # precision in the datum beyond this point is false.

    lsp = int(floor(log10(min(rdh, rdl))))

    # We should round the datum since it might be something like 0.999+-0.1 and
    # we're about to try to decide what its most significant place is. Might get
    # -1 rather than 0.

    rmd = round(md, -lsp)

    if rmd == -0.:  # 0 = -0, too, but no problem there.
        rmd = 0.

    # The most significant place to worry about is the M.S.P. of any of the
    # datum or the deltas. rdl and rdl must be positive, but not necessarily
    # rmd.

    msp = int(floor(log10(max(abs(rmd), rdh, rdl))))

    # If we're not very large or very small, don't use scientific notation.

    if msp > -3 and msp < 3:
        srdh = '%.*f' % (-lsp, rdh)
        srdl = '%.*f' % (-lsp, rdl)

        if srdh == srdl:
            return '%.*fpm%s [%e %e %e]' % (-lsp, rmd, srdh, lo, md, hi)
        return '%.*fp%sm%s [%e %e %e]' % (-lsp, rmd, srdh, srdl, lo, md, hi)

    # Use scientific notation. Adjust values, then format.

    armd = rmd * 10**-msp
    ardh = rdh * 10**-msp
    ardl = rdl * 10**-msp
    prec = msp - lsp

    sardh = '%.*f' % (prec, ardh)
    sardl = '%.*f' % (prec, ardl)

    if sardh == sardl:
        return '(%.*fpm%s)e%d [%e %e %e]' % (prec, armd, sardh, msp, lo, md,
                                             hi)

    return '(%.*fp%sm%s)e%d [%e %e %e]' % (prec, armd, sardh, sardl, msp, lo,
                                           md, hi)
Exemplo n.º 27
0
 def _print(b):
     d = np.round([np.median(b), sap(b, 2.5),
                   sap(b, 97.5)], decimals).tolist()
     print(sfmt.format(d[0], decimals, d[1], decimals, d[2], decimals))
Exemplo n.º 28
0
        for s in srctable.readStream(os.path.join(d, p))[2]:
            ag.add(s.totflux_initial, s.totflux, s.totflux_uc)

            sc = np.abs((s.totflux - s.totflux_initial) / s.totflux_uc)
            if sc > worstscore:
                worstscore = sc
                worstinfo = d, p, s.ra, s.dec

    true, fitted, uc = ag.finish().T
    w = np.where(fitted > 0)
    y = np.log10(fitted[w] / true[w])
    # y = fitted / true - 1
    # y = (fitted - true) / uc
    from scipy.stats import scoreatpercentile as sap

    print "bounds:", d, sap(y, 2), sap(y, 98)
    print "25/75/iqr:", d, sap(y, 25), sap(y, 75), sap(y, 75) - sap(y, 25)
    print "median:", d, np.median(y)
    data.append((d, y))

import astutil

print "worst:", worstinfo[0], worstinfo[1], worstscore, astutil.fmtradec(worstinfo[2], worstinfo[3])

tagmap = {"flux-ne": "No effects", "flux-sq": "ATA-scale squint"}

p = om.RectPlot()
for tag, y in data:
    usetag = tagmap.get(tag, tag)
    values, edges = np.histogram(y, bins=21, normed=True)  # range=(-0.0925, 0.0125),
    edges = edges[:-1]  # convert to old-style
    def plotPosterior(self,name=None, colors=None):
        """Plots the posterior intervals for the dose-response curve, the beta 
        distribution of the second group and the correlation between the 
        parameters a and b from the beta distribution.

        Equivalent figure in article: Figure 3.

        Returns:
        - f (Figure)
        - ax1, ax2, ax3 (Axes) - axes from each of the panels"""
        if colors==None:
            colors=self.colors
        m=self
        d=m.d
        a2s=self.a2s
        b2s=self.b2s
        pi1_ci=m.pi1_ci
        pi2_ci=m.pi2_ci
        x2=m.x2
        prevfsize=rcParams['font.size']
        prevlsize=rcParams['axes.labelsize']
        rcParams.update({'font.size': 8})
        rcParams['axes.labelsize']='large'
        f=pl.figure(figsize=(8,1.95))
        f.subplots_adjust(wspace=0.45)
        ax1=f.add_subplot(131)
        ax1.set_xlabel(r'dose')
        ax1.set_ylabel(r'infection probability, $\pi$')
        ax1.fill_between(x2,pi1_ci[0,:],pi1_ci[2,:],facecolor=colors[0],lw=0,alpha=0.12)
        ax1.fill_between(x2,pi2_ci[0,:],pi2_ci[2,:],facecolor=colors[1],lw=0,alpha=0.12)
        ax1.plot(x2,pi1_ci[1,:],colors[0]+'-')
        ax1.plot(x2,pi2_ci[1,:],colors[1]+'-')
        if isinstance( m.d,df.DayData):
            ax1.plot(m.d.doses,m.d.response1/m.d.nhosts1.astype(float),colors[0]+'.',alpha=0.8)
            ax1.plot(m.d.doses,m.d.response2/m.d.nhosts2.astype(float),colors[1]+'.',alpha=0.8)
        ax1.set_xscale('log')
        ax1.plot(-1,0,colors[0]+'-',markersize=7,mew=2,label='Homogeneous')
        ax1.plot(-1,0,colors[1]+'-',mec=colors[1],markersize=7,mew=2,label='Heterogeneous')
        xl=[0.15*(d.doses[d.doses>0][0]),0.85*(d.doses[-1]*10)]
        x=xl[0]
        
        ax1.set_xlim(xl)
        ax1.set_ylim([-0.09,1.09])
        ax1.text(-0.25, 1.15, 'A', transform=ax1.transAxes,
              fontsize=12, fontweight='bold', va='top', ha='right')    
        
        ax2=f.add_subplot(132)
        x=np.arange(0,1,0.005)
        N=np.array([st.beta.pdf(x,a2s[i],b2s[i]) for i in range(len(a2s))])
        ax2.plot(x,sap(N,50),colors[1]+'-',label='Heterogeneous')
        ax2.fill_between(x,sap(N,2.5),sap(N,97.5),facecolor=colors[1], lw=0,alpha=0.2)
        ax2.set_xlabel(r'susceptibility, $x$')
        ax2.set_ylabel(r'$q(x)$')    
        ax2.text(-0.25, 1.15, 'B', transform=ax2.transAxes,
                  fontsize=12, fontweight='bold', va='top', ha='right')    
        #ax2.set_ylim([0,10])
        
        ax3=f.add_subplot(133)
        ax3.scatter(a2s,b2s,c=colors[1],edgecolor='grey',lw=0.1,alpha=0.5,s=2)
        ax3.set_xlabel('estimated a')
        ax3.set_ylabel('estimated b')   
        ax3.set_xlim([0.1,10])
        ax3.set_ylim([0.1,10])
        ax3.set_xscale('log')
        ax3.set_yscale('log')
        ax3.plot(sap(a2s,50),sap(b2s,50),'ro',mec='r',ms=3)
        ax3.text(-0.25, 1.15, 'C', transform=ax3.transAxes,
                  fontsize=12, fontweight='bold', va='top', ha='right')
        
        if name==None:
            name='-plotPosterior'
        f.savefig(m.saveTo+name+'.'+m.figFormat, bbox_inches='tight',dpi=600)
        rcParams.update({'font.size': prevfsize})
        rcParams['axes.labelsize']=prevlsize
        print "Plotted dose-response curve, beta distribution and a-b correlation, see "+ m.name+name+'.'+m.figFormat
        return f,ax1,ax2,ax3
Exemplo n.º 30
0
def calc_bandwidth(x):
    from scipy.stats import scoreatpercentile as sap
    normalize = 1.349
    IQR = (sap(x, 75) - sap(x, 25)) / normalize
    sigma = np.minimum(np.std(x, axis=0, ddof=1), IQR)
    return max(0.01, 0.9 * sigma * len(x)**(-0.2))
Exemplo n.º 31
0
def centroid_m(d):
    mask = (mf(d, 4) > sap(d, 95)) & (mf(d, 4) < sap(d, 99))
    labels, nl = label(mask)
    d = d - sap(d, 92)
    brightest_object = argmax([d[labels == i].mean() for i in range(nl + 1)])
    return cmass(d, labels, index=brightest_object)
Exemplo n.º 32
0
 def _print(b):
     d=np.round([np.median(b), sap(b,2.5),sap(b,97.5)],decimals).tolist()
     print sfmt.format(d[0],decimals,d[1],decimals,d[2],decimals)