Exemplo n.º 1
0
	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()
Exemplo n.º 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
Exemplo n.º 3
0
	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
Exemplo n.º 4
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')
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
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
    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()