def plotBackDetectorStats(self, phADU=20., imgScale=1.):
		numCols = len(self.sigLvls)
		if len(self.sigLvls) < 3:
			numCols = 3
		fig = plt.figure(figsize=(imgScale*16.5, imgScale*9.5), dpi=80)
		gs = gridspec.GridSpec(3, numCols)
		gs.update(top=0.95, bottom=0.08, hspace=0.3, wspace=0.25)
				
		ll = self.loglikelihood2.copy()
		ll -= ll.mean()
		for n,s in enumerate(self.sigLvls):
			sd = self.sigEvents2[:,n].copy()
			(y, x) = PL.histogram(sd, bins=100)
			ax = fig.add_subplot(gs[1,n])
			ax.text(.9, .9, "Hist. of %.2f sig pix./frame"%(s), horizontalalignment='right', verticalalignment='top', transform=ax.transAxes)
			ax.set_xlabel("# sig. pixels")
			ax.set_ylabel("num. frames")
			for label in ax.xaxis.get_ticklabels():
				label.set_rotation(-30)
				label.set_fontsize(10)
			plt.plot(x[1:], y)

			sd = self.sigADUFTally2[:,n].copy()
			(y, x) = PL.histogram(sd, bins=100)
			ax = fig.add_subplot(gs[0,n])
			ax.text(.9, .9, "Hist. of %.2f sig. ADUs/frame"%(s), horizontalalignment='right', verticalalignment='top', transform=ax.transAxes)
			ax.set_xlabel("Total sig. ADUs")
			ax.set_ylabel("num. frames")
			for label in ax.xaxis.get_ticklabels():
				label.set_rotation(-30)
				label.set_fontsize(10)
			plt.plot(x[1:], y)

			if n < 2:
				ax = fig.add_subplot(gs[2,n])
				ax.text(.92, .92, "Num of sig. pix. vs Log-likelihood"%(s), horizontalalignment='right', verticalalignment='top', transform=ax.transAxes)
				ax.set_ylabel("Frame Log-likelihood")
				ax.set_xlabel("Num of %.2f sig. pix./frame"%(s))
				for label in ax.xaxis.get_ticklabels():
					label.set_rotation(-30)
					label.set_fontsize(10)
				plt.plot(self.sigEvents2[:,n], ll, 'r.')
		
		(y, x) = PL.histogram(ll, bins=100)
		ax = fig.add_subplot(gs[2,2])
		ax.set_xlabel("Log-likelihood")
		ax.set_ylabel("Num. frames")
		ax.set_yscale('log')
		for label in ax.xaxis.get_ticklabels():
			label.set_rotation(-30)
			label.set_fontsize(10)
		plt.plot(x[1:], y)

		plt.show()
示例#2
0
def histeq(im, nbr_bins=256):
    """对一副图像进行直方图均衡化"""
    imhist, bins = pylab.histogram(im.flatten(), nbr_bins, normed=True)
    cdf = imhist.cumsum()
    cdf = 255.0 * cdf / cdf[-1]
    im2 = pylab.interp(im.flatten(), bins[:-1], cdf)
    return im2.reshape(im.shape), cdf
示例#3
0
def circular_hist_analysis(img_piece, xbin=XBIN):
    """compute hist array of image piece """
    counts, centers = pylab.histogram(img_piece, xbin, normed=True)
    counts = np.array(counts)
    counts.shape = (len(centers) - 1, 1)
    counts = np.transpose(counts)
    return counts
示例#4
0
文件: img_tools.py 项目: auroua/test
def histeq(im, nbr_bins=256):
    """对一副图像进行直方图均衡化"""
    imhist, bins = pylab.histogram(im.flatten(), nbr_bins, normed=True)
    cdf = imhist.cumsum()
    cdf = 255.0 * cdf / cdf[-1]
    im2 = pylab.interp(im.flatten(), bins[:-1], cdf)
    return im2.reshape(im.shape), cdf
	def __init__(self, inarr, waveLength, detectorDistance, angAvg=[], filename="plot", writeDir="", runTag="", plotRings=False, cmax=-1, cmin=-1, imgScale=1., adj=[0,0]):
		self.inarr = inarr*(inarr>0)
		for i in range(len(inarr)):
			self.inarr[i] = self.inarr[i][::-1]
		self.filename = filename
		self.write_dir = writeDir
		self.runTag = runTag
		if (cmax == -1):
			self.cmax = self.inarr.max()
		else:
			self.cmax = cmax 

		if (cmin == -1):
			self.cmin = self.inarr.min()
		else:
			self.cmin = cmin 

		self.hist = PL.histogram(self.inarr, bins=300)
		self.imgScale = imgScale
		self.wavelength = waveLength
		self.detectorDistance = detectorDistance
		self.HIceQ = {}
		self.plotRings = plotRings
		self.angAvg = angAvg
		self.adj = adj
示例#6
0
def plotData(Data, nObsPlot=5000):
  ''' Plot data items, at most nObsPlot distinct points (for quick rendering)
  '''
  if type(Data) == bnpy.data.XData:
    PRNG = np.random.RandomState(nObsPlot)
    pIDs = PRNG.permutation(Data.nObs)[:nObsPlot]
    if Data.dim > 1:
      pylab.plot(Data.X[pIDs,0], Data.X[pIDs,1], 'k.')  
    else:
      hist, bin_edges = pylab.histogram(Data.X, bins=25)
      xs = bin_edges[:-1]
      ys = np.asarray(hist, dtype=np.float32) / np.sum(hist)
      pylab.bar(xs, ys, width=0.8*(bin_edges[1]-bin_edges[0]), color='k')
示例#7
0
文件: HRV.py 项目: LauraSR/tfg
    def hrvTriangIndex(self, rr, flag=None):
        """
        Function that computes the triangular index, that is, the total number 
        of intervals rr between the height of the histogram.
        """        
        if flag == None:
           flag = 0

        #Number of bins with fs = 128, recommendation of the ref.
        fs = 128.
        ts = 1/fs*1000. #ms

        #Bins computing
        x = np.arange(min(rr), max(rr), ts)

        #Number of bins for the histogram
        nhist = x.size

        #Histogram
        [N, X] = plt.histogram(rr,nhist)

        #Only the non-empty bins are taken into account
        ind = np.where(N != 0)
        N = N[ind]
        X = X[ind]

        #Histogram maximum
        yo = max(N)

        res = sum(N)*1./yo

        if flag:
        #Graphic representation
            plt.hist(rr,nhist)
            plt.title('HRVTriangIndex')
            plt.xlabel('Duracion intervalos RR [ms]')
            plt.ylabel('Numero de intervalos RR')
        
        return res
def bsa_dpmm(bf, gf0, sub, gfc, dmax, thq, ths, verbose=0):
    """
    Estimation of the population level model of activation density using 
    dpmm and inference
    
    Parameters
    ----------
    bf list of nipy.neurospin.spatial_models.hroi.HierarchicalROI instances
       representing individual ROIs
       let nr be the number of terminal regions across subjects
    gf0, array of shape (nr)
         the mixture-based prior probability 
         that the terminal regions are true positives
    sub, array of shape (nr)
         the subject index associated with the terminal regions
    gfc, array of shape (nr, coord.shape[1])
         the coordinates of the of the terminal regions
    dmax float>0:
         expected cluster std in the common space in units of coord
    thq = 0.5 (float in the [0,1] interval)
        p-value of the prevalence test
    ths=0, float in the rannge [0,nsubj]
        null hypothesis on region prevalence that is rejected during inference
    verbose=0, verbosity mode

    Returns
    -------
    crmap: array of shape (nnodes):
           the resulting group-level labelling of the space
    LR: a instance of sbf.LandmarkRegions that describes the ROIs found
        in inter-subject inference
        If no such thing can be defined LR is set to None
    bf: List of  nipy.neurospin.spatial_models.hroi.Nroi instances
        representing individual ROIs
    p: array of shape (nnodes):
       likelihood of the data under H1 over some sampling grid
    """
    dom = bf[0].domain
    n_subj = len(bf)
    
    crmap = -np.ones(dom.size, np.int)
    LR = None
    p = np.zeros(dom.size)
    if len(sub)<1:
        return crmap, LR, bf, p

    sub = np.concatenate(sub).astype(np.int) 
    gfc = np.concatenate(gfc)
    gf0 = np.concatenate(gf0)

    g0 = 1./dom.local_volume.sum()
    
    # prepare the DPMM
    dim = dom.em_dim
    g1 = g0
    prior_precision =  1./(dmax*dmax)*np.ones((1,dim))
    dof = 10
    burnin = 100
    nis = 1000
    # nis = number of iterations to estimate p
    
    #nii = 100
    ## nii = number of iterations to estimate q
    #p,q =  fc.fdp(gfc, 0.5, g0, g1, dof, prior_precision, 1-gf0,
    #              sub, burnin, coord, nis, nii)
    p, q =  dpmm(gfc, 0.5, g0, g1, dof, prior_precision, 1-gf0,
               sub, burnin, dom.coord, nis)
    
    if verbose:
        import matplotlib.pylab as mp
        mp.figure()
        mp.plot(1-gf0,q,'.')
        h1,c1 = mp.histogram((1-gf0),bins=100)
        h2,c2 = mp.histogram(q,bins=100)
        mp.figure()
        mp.bar(c1[:len(h1)],h1,width=0.005)
        mp.bar(c2[:len(h2)]+0.003,h2,width=0.005,color='r')
        print 'Number of candidate regions %i, regions found %i' % (
                    np.size(q), q.sum())

    from nipy.neurospin.graph.field import field_from_coo_matrix_and_data 
    Fbeta = field_from_coo_matrix_and_data(dom.topology, p)
    _, _, _, label = Fbeta.custom_watershed(0, g0)

    # append some information to the hroi in each subject
    for s in range(n_subj):
        bfs = bf[s]
        if bfs.k>0 :
            leaves = bfs.isleaf()
            us = -np.ones(bfs.k).astype(np.int)

            # set posterior proba
            lq = np.zeros(bfs.k)
            lq[leaves] = q[sub==s]
            bfs.set_roi_feature('posterior_proba', lq)

            # set prior proba
            lq = np.zeros(bfs.k)
            lq[leaves] = 1-gf0[sub==s]
            bfs.set_roi_feature('prior_proba', lq)

            pos = bfs.representative_feature('position', 'mean')
            midx = [np.argmin(np.sum((dom.coord-pos[k])**2,1))
                    for k in range(bfs.k)]
            j = label[np.array(midx)]
            us[leaves] = j[leaves]

            # when parent regions has similarly labelled children,
            # include it also
            us = bfs.make_forest().propagate_upward(us)
            bfs.set_roi_feature('label',us)
                        
    # derive the group-level landmarks
    # with a threshold on the number of subjects
    # that are represented in each one 
    LR, nl = sbf.build_LR(bf, thq, ths, dmax, verbose=verbose)

    # make a group-level map of the landmark position        
    crmap = _relabel_(label, nl)   
    
    return crmap, LR, bf, p
示例#9
0
def find_shock_in(clstr: cluster.ClusterObj):
    # Read in data
    # xray is smoothed x-ray map
    # temp is smoothed temperature map
    # res is the resolution map used for smoothing
    # res is rescaled by 1.25 because that was done for smoothing

    # xray=pf.getdata('obs_sim5_sm.fits')
    # temp=pf.getdata('Tmap_vor5_sm.fits')
    # res=pf.getdata('acisI_scale.fits')
    # res=1.25*res

    xray = pf.getdata(clstr.combined_signal)  # combined_signal
    # temp=pf.getdata('A3667_shocks_8p/A3667_Tmap_c8_xspec_mg_hybCF.fits')
    temp = pf.getdata(clstr.temperature_map_filename)  # temperature map
    thead = pf.getheader(clstr.temperature_map_filename)  # temperature map
    res = pf.getdata(clstr.scale_map_file)
    # res=pf.getdata('A3667_shocks_8p/A3667_c_kmap.fits')
    # data_dir='../shockfinder_new/Obs/observational_shockfinder/simulated/projected/spin_cluster/'

    # xray=pf.getdata('simulated/projections_XRayEmissivity_1_wvtbin_asm_rb.fits')
    # temp=pf.getdata('simulated/projections_Temperature_1_wvtbin_asm_rb.fits')
    # thead=pf.getheader('simulated/projections_Temperature_1_wvtbin_asm_rb.fits')
    # res=pf.getdata('simulated/sim_scale_rb.fits')

    xray[temp == 0] = 0

    res = 1.25 * res

    # The sizes of the arrays should be the same
    nx = temp.shape[0]
    ny = temp.shape[1]

    mach = np.zeros((nx, ny), dtype='d')
    angle = np.zeros((nx, ny), dtype='d')
    i_plus = np.zeros((nx, ny), dtype='i')
    i_minus = np.zeros((nx, ny), dtype='i')
    j_plus = np.zeros((nx, ny), dtype='i')
    j_minus = np.zeros((nx, ny), dtype='i')
    Tjump = np.zeros((nx, ny), dtype='d')
    Xjump = np.zeros((nx, ny), dtype='d')
    max_Tjump = np.ones((nx, ny), dtype='d')
    max_theta = np.zeros((nx, ny), dtype='d')
    Xyes = np.zeros((nx, ny))
    mask = np.ones((nx, ny))
    XT = np.zeros((nx, ny), dtype='d')
    i = np.zeros((nx, ny), dtype='i')
    j = np.zeros((nx, ny), dtype='i')
    for k in range(ny):
        i[:, k] = np.arange(nx)
    for k in range(nx):
        j[k:, ] = np.arange(ny)

    theta = 0.0
    while theta < np.pi:
        print(theta)
        # Calculate the plus and minus indices given the angle and resolution map
        i_plus = np.rint(i + res * sin(theta))
        i_minus = i + i - i_plus
        j_plus = np.rint(j + res * cos(theta))
        j_minus = j + j - j_plus
        i_plus = i_plus.astype(int)
        i_minus = i_minus.astype(int)
        j_plus = j_plus.astype(int)
        j_minus = j_minus.astype(int)

        # Make sure all of the indices are contained in the image
        # If any are not, set the index to zero
        i_plus[i_plus > nx - 1] = 0
        i_plus[i_plus < 0] = 0
        i_minus[i_minus > nx - 1] = 0
        i_minus[i_minus < 0] = 0
        j_plus[j_plus > ny - 1] = 0
        j_plus[j_plus < 0] = 0
        j_minus[j_minus > ny - 1] = 0
        j_minus[j_minus < 0] = 0

        # Calculate the temperature jump and X-ray jump if both temperatures are non-zero
        pix1 = temp[i_plus, j_plus] > 0.0
        pix2 = temp[i_minus, j_minus] > 0.0
        pix = pix1 * pix2
        print("temp = ", temp[i_minus[pix], j_minus[pix]])
        print("xray = ", xray[i_minus[pix], j_minus[pix]])
        Tjump[pix] = temp[i_plus[pix], j_plus[pix]] / temp[i_minus[pix],
                                                           j_minus[pix]]
        Xjump[pix] = xray[i_plus[pix], j_plus[pix]] / xray[i_minus[pix],
                                                           j_minus[pix]]

        # Assign maximum values to the arrays
        plus_pix = Tjump > max_Tjump
        minus_pix = 1.0 / Tjump > max_Tjump
        max_Tjump[plus_pix] = Tjump[plus_pix]
        max_theta[plus_pix] = theta
        max_Tjump[minus_pix] = 1.0 / Tjump[minus_pix]
        max_theta[minus_pix] = theta + np.pi

        # Test if the temperature and X-ray jumps are in the same direction
        # XT equals 1 when they are in the same direction and 0 when they are not
        # XT is only calculated for pixels which achieved a max Mach number for the current theta
        XT[plus_pix] = np.sign(
            (Tjump[plus_pix] - 1.0) * (Xjump[plus_pix] - 1.0)) * 0.5 + 0.5
        XT[minus_pix] = np.sign(
            (Tjump[minus_pix] - 1.0) * (Xjump[minus_pix] - 1.0)) * 0.5 + 0.5

        theta = theta + np.pi / 32.0

    mach[XT == 1.0] = 0.2 * (-7.0 + 8.0 * max_Tjump[XT == 1.0] +
                             4.0 * np.sqrt(4.0 - 7.0 * max_Tjump[XT == 1.0] +
                                           4.0 * max_Tjump[XT == 1.0]**2))
    angle[XT == 1.0] = max_theta[XT == 1.0] + 180. / np.pi
    # in degrees
    angle[XT != 1.0] = -1.0
    mach = np.sqrt(mach)
    angle[np.isnan(mach)] = -1.0
    mach[np.isnan(mach)] = 0.0
    mach[temp == 0.0] = np.nan
    angle[temp == 0.0] = np.nan

    for ii in range(0, nx):
        for jj in range(0, ny):
            if mach[ii, jj] > 5: mach[ii, jj] = 0

    # pf.writeto('A3667_hyb_strong_mach_new_rb.fits',mach,header=thead,clobber=True)
    # pf.writeto('A3667_hyb_strong_angle_new_rb.fits',angle,header=thead,clobber=True)

    pf.writeto(clstr.mach_map_filename, mach, header=thead, overwrite=True)
    pf.writeto(clstr.angle_map_filename, angle, header=thead, overwrite=True)

    # The code below was lifted from Sam's shockfinder
    # It is used to make a histogram of the Mach number

    # # Make histogram
    sa2d, mach2d = pl.histogram(mach, bins=50, range=[0.1, 4.0])
    # sa2d = 1.0*sa2d*(1.0*fov/pixels)**2

    fig = pl.figure(figsize=(6.0, 6.0), dpi=200)
    #
    pl.clf()
    pl.semilogy(mach2d[1:], sa2d / (mach2d[1] - mach2d[0]), 'k', ls='steps-')
    #
    pl.xlabel('Mach')
    # pl.ylabel(r'd(Surface Area [$Mpc^2$])/d(Mach)')
    pl.ylabel(r'pixels')
    pl.xlim(0.1, 4.0)
    pl.ylim(1e2, 1.5e6)
    # pl.legend(['2D Shocks'],loc='lower left')
    pl.savefig(clstr.mach_histogram_filename, dpi=200, bbox_inches='tight')
示例#10
0
def plotSpikeHist (include = ['allCells', 'eachPop'], timeRange = None, binSize = 5, overlay=True, graphType='line', yaxis = 'rate', 
    figSize = (10,8), saveData = None, saveFig = None, showFig = True): 
    ''' 
    Plot spike histogram
        - include (['all',|'allCells','allNetStims',|,120,|,'E1'|,('L2', 56)|,('L5',[4,5,6])]): List of data series to include. 
            Note: one line per item, not grouped (default: ['allCells', 'eachPop'])
        - timeRange ([start:stop]): Time range of spikes shown; if None shows all (default: None)
        - binSize (int): Size in ms of each bin (default: 5)
        - overlay (True|False): Whether to overlay the data lines or plot in separate subplots (default: True)
        - graphType ('line'|'bar'): Type of graph to use (line graph or bar plot) (default: 'line')
        - yaxis ('rate'|'count'): Units of y axis (firing rate in Hz, or spike count) (default: 'rate')
        - figSize ((width, height)): Size of figure (default: (10,8))
        - saveData (None|True|'fileName'): File name where to save the final data used to generate the figure;
            if set to True uses filename from simConfig (default: None)
        - saveFig (None|True|'fileName'): File name where to save the figure;
            if set to True uses filename from simConfig (default: None)
        - showFig (True|False): Whether to show the figure or not (default: True)

        - Returns figure handle
    '''

    print('Plotting spike histogram...')

    colorList = [[0.42,0.67,0.84], [0.90,0.76,0.00], [0.42,0.83,0.59], [0.90,0.32,0.00],
                [0.34,0.67,0.67], [0.90,0.59,0.00], [0.42,0.82,0.83], [1.00,0.85,0.00],
                [0.33,0.67,0.47], [1.00,0.38,0.60], [0.57,0.67,0.33], [0.5,0.2,0.0],
                [0.71,0.82,0.41], [0.0,0.2,0.5]] 

    
    # Replace 'eachPop' with list of pops
    if 'eachPop' in include: 
        include.remove('eachPop')
        for pop in sim.net.allPops: include.append(pop)

    # Y-axis label
    if yaxis == 'rate': yaxisLabel = 'Avg cell firing rate (Hz)'
    elif yaxis == 'count': yaxisLabel = 'Spike count'
    else:
        print 'Invalid yaxis value %s', (yaxis)
        return

    # time range
    if timeRange is None:
        timeRange = [0,sim.cfg.duration]

    histData = []

    # create fig
    fig,ax1 = subplots(figsize=figSize)
    fontsiz = 12
    
    # Plot separate line for each entry in include
    for iplot,subset in enumerate(include):
        cells, cellGids, netStimPops = getCellsInclude([subset])
        numNetStims = 0

        # Select cells to include
        if len(cellGids) > 0:
            try:
                spkinds,spkts = zip(*[(spkgid,spkt) for spkgid,spkt in zip(sim.allSimData['spkid'],sim.allSimData['spkt']) if spkgid in cellGids])
            except:
                spkinds,spkts = [],[]
        else: 
            spkinds,spkts = [],[]

        # Add NetStim spikes
        spkts, spkinds = list(spkts), list(spkinds)
        numNetStims = 0
        for netStimPop in netStimPops:
            cellStims = [cellStim for cell,cellStim in sim.allSimData['stims'].iteritems() if netStimPop in cellStim]
            if len(cellStims) > 0:
                lastInd = max(spkinds) if len(spkinds)>0 else 0
                spktsNew = [spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ]
                spkindsNew = [lastInd+1+i for i,cellStim in enumerate(cellStims) for spkt in cellStim[netStimPop]]
                spkts.extend(spktsNew)
                spkinds.extend(spkindsNew)
                numNetStims += len(cellStims)

        histo = histogram(spkts, bins = arange(timeRange[0], timeRange[1], binSize))
        histoT = histo[1][:-1]+binSize/2
        histoCount = histo[0] 

        histData.append(histoCount)

        if yaxis=='rate': histoCount = histoCount * (1000.0 / binSize) / (len(cellGids)+numNetStims) # convert to firing rate

        color = colorList[iplot%len(colorList)]

        if not overlay: 
            subplot(len(include),1,iplot+1)  # if subplot, create new subplot
            title (str(subset))
            color = 'blue'
   
        if graphType == 'line':
            plot (histoT, histoCount, linewidth=1.0, color = color)
        elif graphType == 'bar':
            bar(histoT, histoCount, width = binSize, color = color)

        xlabel('Time (ms)', fontsize=fontsiz)
        ylabel(yaxisLabel, fontsize=fontsiz) # add yaxis in opposite side
        ax1.set_xlim(timeRange)

    try:
        tight_layout()
    except:
        pass

    # Add legend
    if overlay:
        for i,subset in enumerate(include):
            plot(0,0,color=colorList[i%len(colorList)],label=str(subset))
        legend(fontsize=fontsiz, bbox_to_anchor=(1.04, 1), loc=2, borderaxespad=0.)
        maxLabelLen = min(10,max([len(str(l)) for l in include]))
        subplots_adjust(right=(0.9-0.012*maxLabelLen))


    # save figure data
    if saveData:
        figData = {'histData': histData, 'histT': histoT, 'include': include, 'timeRange': timeRange, 'binSize': binSize,
         'saveData': saveData, 'saveFig': saveFig, 'showFig': showFig}
    
        _saveFigData(figData, saveData, 'spikeHist')
 
    # save figure
    if saveFig: 
        if isinstance(saveFig, str):
            filename = saveFig
        else:
            filename = sim.cfg.filename+'_'+'spikeHist.png'
        savefig(filename)

    # show fig 
    if showFig: _showFigure()

    return fig
示例#11
0
def plotRaster (include = ['allCells'], timeRange = None, maxSpikes = 1e8, orderBy = 'gid', orderInverse = False, spikeHist = None, 
        spikeHistBin = 5, syncLines = False, figSize = (10,8), saveData = None, saveFig = None, showFig = True): 
    ''' 
    Raster plot of network cells 
        - include (['all',|'allCells',|'allNetStims',|,120,|,'E1'|,('L2', 56)|,('L5',[4,5,6])]): Cells to include (default: 'allCells')
        - timeRange ([start:stop]): Time range of spikes shown; if None shows all (default: None)
        - maxSpikes (int): maximum number of spikes that will be plotted  (default: 1e8)
        - orderBy ('gid'|'y'|'ynorm'|...): Unique numeric cell property to order y-axis by, e.g. 'gid', 'ynorm', 'y' (default: 'gid')
        - orderInverse (True|False): Invert the y-axis order (default: False)
        - spikeHist (None|'overlay'|'subplot'): overlay line over raster showing spike histogram (spikes/bin) (default: False)
        - spikeHistBin (int): Size of bin in ms to use for histogram (default: 5)
        - syncLines (True|False): calculate synchorny measure and plot vertical lines for each spike to evidence synchrony (default: False)
        - figSize ((width, height)): Size of figure (default: (10,8))
        - saveData (None|True|'fileName'): File name where to save the final data used to generate the figure; 
            if set to True uses filename from simConfig (default: None)
        - saveFig (None|True|'fileName'): File name where to save the figure (default: None)
            if set to True uses filename from simConfig (default: None)
        - showFig (True|False): Whether to show the figure or not (default: True)

        - Returns figure handle
    '''

    print('Plotting raster...')

    colorList = [[0.42,0.67,0.84], [0.90,0.76,0.00], [0.42,0.83,0.59], [0.90,0.32,0.00],
                [0.34,0.67,0.67], [0.90,0.59,0.00], [0.42,0.82,0.83], [1.00,0.85,0.00],
                [0.33,0.67,0.47], [1.00,0.38,0.60], [0.57,0.67,0.33], [0.5,0.2,0.0],
                [0.71,0.82,0.41], [0.0,0.2,0.5]] 

    # Select cells to include
    cells, cellGids, netStimPops = getCellsInclude(include)
    selectedPops = [cell['tags']['popLabel'] for cell in cells]+netStimPops
    popLabels = [pop for pop in sim.net.allPops if pop in selectedPops] # preserves original ordering
    popColors = {popLabel: colorList[ipop%len(colorList)] for ipop,popLabel in enumerate(popLabels)} # dict with color for each pop
    if len(cellGids) > 0:
        gidColors = {cell['gid']: popColors[cell['tags']['popLabel']] for cell in cells}  # dict with color for each gid
        try:
            spkgids,spkts = zip(*[(spkgid,spkt) for spkgid,spkt in zip(sim.allSimData['spkid'],sim.allSimData['spkt']) if spkgid in cellGids])
        except:
            print 'No spikes available to plot raster'
            return None
        spkgidColors = [gidColors[spkgid] for spkgid in spkgids]

    # Order by
    if len(cellGids) > 0:
        if orderBy not in cells[0]['tags']:  # if orderBy property doesn't exist or is not numeric, use gid
            orderBy = 'gid'
        elif not isinstance(cells[0]['tags'][orderBy], Number): 
            orderBy = 'gid'
        ylabelText = 'Cells (ordered by %s)'%(orderBy)   
    
        if orderBy == 'gid': 
            yorder = [cell[orderBy] for cell in cells]
        else:
            yorder = [cell['tags'][orderBy] for cell in cells]

        if orderInverse: yorder.reverse()

        sortedGids = {gid:i for i,(y,gid) in enumerate(sorted(zip(yorder,cellGids)))}
        spkinds = [sortedGids[gid]  for gid in spkgids]

    else:
        spkts = []
        spkinds = []
        spkgidColors = []
        ylabelText = ''

    # Add NetStim spikes
    spkts,spkgidColors = list(spkts), list(spkgidColors)
    numNetStims = 0
    for netStimPop in netStimPops:
        cellStims = [cellStim for cell,cellStim in sim.allSimData['stims'].iteritems() if netStimPop in cellStim]
        if len(cellStims) > 0:
            lastInd = max(spkinds) if len(spkinds)>0 else 0
            spktsNew = [spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ]
            spkindsNew = [lastInd+1+i for i,cellStim in enumerate(cellStims) for spkt in cellStim[netStimPop]]
            spkts.extend(spktsNew)
            spkinds.extend(spkindsNew)
            for i in range(len(spktsNew)): 
                spkgidColors.append(popColors[netStimPop])
            numNetStims += len(cellStims)
    if len(cellGids)>0 and numNetStims: 
        ylabelText = ylabelText + ' and NetStims (at the end)'
    elif numNetStims:
        ylabelText = ylabelText + 'NetStims'

    # Time Range
    if timeRange == [0,sim.cfg.duration]:
        pass
    elif timeRange is None:
        timeRange = [0,sim.cfg.duration]
    else:
        spkinds,spkts,spkgidColors = zip(*[(spkind,spkt,spkgidColor) for spkind,spkt,spkgidColor in zip(spkinds,spkts,spkgidColors) 
        if timeRange[0] <= spkt <= timeRange[1]])

    # Limit to maxSpikes
    if (len(spkts)>maxSpikes):
        print('  Showing only the first %i out of %i spikes' % (maxSpikes, len(spkts))) # Limit num of spikes
        if numNetStims: # sort first if have netStims
            spkts, spkinds, spkgidColors = zip(*sorted(zip(spkts, spkinds, spkgidColors)))
        spkts = spkts[:maxSpikes]
        spkinds = spkinds[:maxSpikes]
        spkgidColors = spkgidColors[:maxSpikes]
        timeRange[1] =  max(spkts)

    # Calculate spike histogram 
    if spikeHist:
        histo = histogram(spkts, bins = arange(timeRange[0], timeRange[1], spikeHistBin))
        histoT = histo[1][:-1]+spikeHistBin/2
        histoCount = histo[0]

    # Plot spikes
    fig,ax1 = subplots(figsize=figSize)
    fontsiz = 12
    
    if spikeHist == 'subplot':
        gs = gridspec.GridSpec(2, 1,height_ratios=[2,1])
        ax1=subplot(gs[0])
    ax1.scatter(spkts, spkinds, 10, linewidths=2, marker='|', color = spkgidColors) # Create raster  
    
    # Plot stats
    totalSpikes = len(spkts)   
    totalConnections = sum([len(cell['conns']) for cell in cells])   
    numCells = len(cells)
    firingRate = float(totalSpikes)/numCells/(timeRange[1]-timeRange[0])*1e3 if totalSpikes>0 else 0# Calculate firing rate 
    connsPerCell = totalConnections/float(numCells) if numCells>0 else 0 # Calculate the number of connections per cell
    
    # Plot synchrony lines 
    if syncLines: 
        for spkt in spkts:
            ax1.plot((spkt, spkt), (0, len(cells)+numNetStims), 'r-', linewidth=0.1)
        title('cells=%i syns/cell=%0.1f rate=%0.1f Hz sync=%0.2f' % (numCells,connsPerCell,firingRate,syncMeasure()), fontsize=fontsiz)
    else:
        title('cells=%i syns/cell=%0.1f rate=%0.1f Hz' % (numCells,connsPerCell,firingRate), fontsize=fontsiz)

    # Plot spike hist
    if spikeHist == 'overlay':
        ax2 = ax1.twinx()
        ax2.plot (histoT, histoCount, linewidth=0.5)
        ax2.set_ylabel('Spike count', fontsize=fontsiz) # add yaxis label in opposite side
    elif spikeHist == 'subplot':
        ax2=subplot(gs[1])
        plot (histoT, histoCount, linewidth=1.0)
        ax2.set_xlabel('Time (ms)', fontsize=fontsiz)
        ax2.set_ylabel('Spike count', fontsize=fontsiz)

    # Axis
    ax1.set_xlabel('Time (ms)', fontsize=fontsiz)
    ax1.set_ylabel(ylabelText, fontsize=fontsiz)
    ax1.set_xlim(timeRange)
    ax1.set_ylim(-1, len(cells)+numNetStims+1)    

    # Add legend
    for popLabel in popLabels:
        plot(0,0,color=popColors[popLabel],label=popLabel)
    legend(fontsize=fontsiz, bbox_to_anchor=(1.04, 1), loc=2, borderaxespad=0.)
    maxLabelLen = max([len(l) for l in popLabels])
    subplots_adjust(right=(0.9-0.012*maxLabelLen))

    # save figure data
    if saveData:
        figData = {'spkTimes': spkts, 'spkInds': spkinds, 'spkColors': spkgidColors, 'cellGids': cellGids, 'sortedGids': sortedGids, 'numNetStims': numNetStims, 
        'include': include, 'timeRange': timeRange, 'maxSpikes': maxSpikes, 'orderBy': orderBy, 'orderInverse': orderInverse, 'spikeHist': spikeHist,
        'syncLines': syncLines}

        _saveFigData(figData, saveData, 'raster')
 
    # save figure
    if saveFig: 
        if isinstance(saveFig, str):
            filename = saveFig
        else:
            filename = sim.cfg.filename+'_'+'raster.png'
        savefig(filename)

    # show fig 
    if showFig: _showFigure()

    return fig
def bsa_dpmm(Fbeta, bf, gf0, sub, gfc, coord, dmax, thq, ths, g0,verbose=0):
    """
    Estimation of the population level model of activation density using 
    dpmm and inference
    
    Parameters
    ----------
    Fbeta nipy.neurospin.graph.field.Field instance
          an  describing the spatial relationships
          in the dataset. nbnodes = Fbeta.V
    bf list of nipy.neurospin.spatial_models.hroi.Nroi instances
       representing individual ROIs
       let nr be the number of terminal regions across subjects
    gf0, array of shape (nr)
         the mixture-based prior probability 
         that the terminal regions are true positives
    sub, array of shape (nr)
         the subject index associated with the terminal regions
    gfc, array of shape (nr, coord.shape[1])
         the coordinates of the of the terminal regions
    dmax float>0:
         expected cluster std in the common space in units of coord
    thq = 0.5 (float in the [0,1] interval)
        p-value of the prevalence test
    ths=0, float in the rannge [0,nsubj]
        null hypothesis on region prevalence that is rejected during inference
    g0 = 1.0 (float): constant value of the uniform density
       over the (compact) volume of interest
    verbose=0, verbosity mode

    Returns
    -------
    crmap: array of shape (nnodes):
           the resulting group-level labelling of the space
    LR: a instance of sbf.Landmark_regions that describes the ROIs found
        in inter-subject inference
        If no such thing can be defined LR is set to None
    bf: List of  nipy.neurospin.spatial_models.hroi.Nroi instances
        representing individual ROIs
    p: array of shape (nnodes):
       likelihood of the data under H1 over some sampling grid
    """
    nvox = coord.shape[0]
    nsubj = len(bf)
    
    crmap = -np.ones(nvox, np.int)
    u = []
    LR = None
    p = np.zeros(nvox)
    if len(sub)<1:
        return crmap,LR,bf,p

    sub = np.concatenate(sub).astype(np.int) 
    gfc = np.concatenate(gfc)
    gf0 = np.concatenate(gf0)
    
    # prepare the DPMM
    g1 = g0
    prior_precision =  1./(dmax*dmax)*np.ones((1,3), np.float)
    dof = 100
    spatial_coords = coord
    burnin = 100
    nis = 300
    # nis = number of iterations to estimate p
    nii = 100
    # nii = number of iterations to estimate q

    p,q =  fc.fdp(gfc, 0.5, g0, g1, dof, prior_precision, 1-gf0,
                  sub, burnin, spatial_coords, nis, nii)
    
    if verbose:
        import matplotlib.pylab as mp
        mp.figure()
        mp.plot(1-gf0,q,'.')
        h1,c1 = mp.histogram((1-gf0),bins=100)
        h2,c2 = mp.histogram(q,bins=100)
        mp.figure()
        # We use c1[:len(h1)] to be independant of the change in np.hist
        mp.bar(c1[:len(h1)],h1,width=0.005)
        mp.bar(c2[:len(h2)]+0.003,h2,width=0.005,color='r')
        print 'Number of candidate regions %i, regions found %i' % (
                    np.size(q), q.sum())
    
    Fbeta.set_field(p)
    idx,depth, major,label = Fbeta.custom_watershed(0,g0)

    # append some information to the hroi in each subject
    for s in range(nsubj):
        bfs = bf[s]
        if bfs!=None:
            leaves = bfs.isleaf()
            us = -np.ones(bfs.k).astype(np.int)
            lq = np.zeros(bfs.k)
            lq[leaves] = q[sub==s]
            bfs.set_roi_feature('posterior_proba',lq)
            lq = np.zeros(bfs.k)
            lq[leaves] = 1-gf0[sub==s]
            bfs.set_roi_feature('prior_proba',lq)
                   
            #idx = bfs.feature_argmax('activation')
            #midx = [bfs.discrete_features['index'][k][idx[k]]
            #        for k in range(bfs.k)]
            pos = bfs.roi_features['position']
            midx = [np.argmin(np.sum((coord-pos[k])**2,1))  for k in range(bfs.k)]
            j = label[np.array(midx)]
            us[leaves] = j[leaves]

            # when parent regions has similarly labelled children,
            # include it also
            us = bfs.propagate_upward(us)
            bfs.set_roi_feature('label',us)
                        
    # derive the group-level landmarks
    # with a threshold on the number of subjects
    # that are represented in each one 
    LR,nl = infer_LR(bf, thq, ths,verbose=verbose)

    # make a group-level map of the landmark position
    crmap = -np.ones(np.shape(label))
    if nl!=None:
        aux = np.arange(label.max()+1)
        aux[0:np.size(nl)] = nl
        crmap[label>-1] = aux[label[label>-1]]
 
    return crmap, LR, bf, p
if not bFilename:
    sys.exit('Usage: python3 fpqSegmentAngleRose.py -I<inputfilename>')

#   get nodes and calculate angles from nodes
nodelist = fpq.getNodes(fn)
xnodelist = nodelist[0]
ynodelist = nodelist[1]
segangle = fpq.getSegAngles(xnodelist, ynodelist)
nSegs = len(segangle)

#   bin the data and find maximum per bin
nBins = int(round(360 / nBinWidth))
segangleDoubled = np.zeros(len(segangle))
segangleDoubled = np.copy(segangle)
segangleDoubled = np.concatenate([segangleDoubled, segangleDoubled + 180.0])
n, b = plt.histogram(segangleDoubled, nBins)
nMax = max(n)

#   plot the segment angle distribution
plt.figure(figsize=(xSize, ySize))
plt.subplot(111, projection='polar')
coll = fpq.rose(segangle,
                bins=nBins,
                bidirectional=True,
                eqarea=True,
                color=sColour)
plt.xticks(np.radians(range(0, 360, 45)),
           ['0', '45', '90', '135', '180', '215', '270', '315'])
plt.rgrids(range(0, int(round(nMax * 1.1)), int(round((nMax * 1.1) / 5))),
           angle=330)
plt.ylim(0, int(round(nMax * 1.1)))
示例#14
0
文件: hop.py 项目: domaubert/EMMA
    return m,dndm

# ========================================================
# ========================================================
if __name__ == "__main__":
    import sys

#    fib(int(sys.argv[1]))
    
    m,dndm=read_hmf("fmass/mf9.dat")
    vs=read_size("fmass/snap9.size")
    
    omegam=0.3
    h0=70.
    lbox=64.
    npart=256**3

    rhoc=3.*(h0*1e3/3.086e22)**2/8./np.pi/6.67e-11
    mpart=rhoc*(omegam)*(lbox/(h0/100.)*3.086e22)**3/2e30/npart*(h0/100)

    binm=np.logspace(8,15,128)
    h,b=plt.histogram(vs*mpart,binm)
    h=h/(lbox**3)/np.diff(binm)
    binok=(binm[:-1]+binm[1:])*0.5

    #plt.clf()
    plt.loglog(binok,h,marker='o',linestyle='None')
    plt.loglog(m,dndm)
    plt.show()
    
def javelin_modeling(time, cflux, lflux, suff='', nburn=1000, nchain=500, mods=None):
    """Do lag calculations with Javelin
    
    Parameters:
        time: [n] array of time axis
        cflux: [2,n] continuum flux and error
        lflux: [2,n] line flux and error
        suff: suffix for printing
        mods: javelin models if already calculated [cont_mod, cont_line_mod]. 
            In that case, this function just does the plotting. 
    """
    
    if mods is None:
        # write light curves to file so javelin can read them #
        irand = np.random.randint(100000)
        text = '\n'.join(['{} {} {}'.format(*x) for x in zip(time, cflux[0], cflux[1])])
        with open('tmp_c%d.dat'%irand, 'w') as fp: fp.write(text)
        text = '\n'.join(['{} {} {}'.format(*x) for x in zip(time, lflux[0], lflux[1])])
        with open('tmp_l%d.dat'%irand, 'w') as fp: fp.write(text)

        # continuum first #
        cont_lc = get_data(['tmp_c%d.dat'%irand])
        cont_mod = Cont_Model(cont_lc)
        cont_mod.do_mcmc(set_verbose=False)
        #cont_mod.show_hist();return 0,cont_mod,0

        # continuum and line #
        cont_line_lc = get_data(['tmp_c%d.dat'%irand, 'tmp_l%d.dat'%irand]) 
        cont_line_mod = Rmap_Model(cont_line_lc)



        llimit = [-100, 100]
        pmap,_ = cont_line_mod.do_map([-0.6, 1.5, 1.0, 0.1, .2], 
                                      fixed=[1,1,1,1,1], set_verbose=False)
        cont_line_mod.do_mcmc(conthpd=cont_mod.hpd, laglimit=[llimit], nburn=nburn, nchain=nchain,
                         #fixed=[1,1,1,1,1], p_fix=pmap, threads=20,set_verbose=False)
                         threads=30,set_verbose=False)

        os.system('rm tmp_c%d.dat tmp_l%d.dat'%(irand, irand))
    else:
        cont_mod, cont_line_mod = mods
    
    # plot the result  of javelin fit #
    chains = cont_line_mod.flatchain
    lag_javelin = plt.histogram(chains[:,2], 400, density=1)

    bins_cent = (lag_javelin[1][1:] + lag_javelin[1][:-1])/2
    bins_err  = (lag_javelin[1][1:] - lag_javelin[1][:-1])/2

    percentile = lambda l: '[{:.4}, {:.4}]'.format(
        *np.percentile(chains[:,2], [(100-l)/2, l+(100-l)/2]))
    text = '# percentiles: 68, 90, 99: {}, {}, {}'.format(*[percentile(x) for x in [68, 90, 99]])
    text += '\n# mean lag: {:.4} +{:.4} {:.4}'.format(
        np.percentile(chains[:,2],50),
        np.percentile(chains[:,2],68+16)-np.percentile(chains[:,2],50),
        np.percentile(chains[:,2],16)-np.percentile(chains[:,2],50))
    print(text)
    text += '\ndescriptor lag_javelin{0},+- lag_javelin_prob{0}\n'.format(suff)
    text += '\n'.join(['{:.4} {:.4} {:.4}'.format(*x) 
                       for x in zip(bins_cent, bins_err, lag_javelin[0])])
    return text, cont_mod, cont_line_mod, lag_javelin