Beispiel #1
0
    def _degree_histo(self,numfig=1):
        import pylab

        def degreehisto(a):
            return  numpy.histogram(a,bins=numpy.arange(0,numpy.max(a)))

        msg(1,"figure %(degree)s: node degree histograms " % self.pdf)
        pylab.figure(numfig)
        pylab.clf()
        pylab.axes(axisbg='w',frameon=False)  # bg not visible?
        barlegend = LegendContainer()

        n,bins = degreehisto(self.D['degree'])
        lines = pylab.bar(bins,n,align='center',color='black',width=1.0)
        barlegend.append(lines[0],'total')

        n,bins = degreehisto(self.D['indegree'])
        lines = pylab.bar(bins,n,align='center',color='yellow',width=2./3.)
        barlegend.append(lines[0],'in')

        n,bins = degreehisto(self.D['outdegree'])
        lines = pylab.bar(bins,n,align='center',color='red',width=1./3.)
        barlegend.append(lines[0],'out')

        pylab.legend(*barlegend.args())
        pylab.xlim(xmin=-0.5)
        pylab.xlabel('degree')
        pylab.ylabel('count')
        #pylab.title("Distribution of node degrees (including connections with bulk)")
        pylab.savefig(self.pdf['degree'])
Beispiel #2
0
 def _check_and_remap(self,densityList):
     """check that all densities are compatible with the reference
     density (densities are NOT saved)"""
     for k,dens in enumerate(densityList):
         if dens.map.shape != self.reference.map.shape:
             msg(1, "Must remap density "+str(dens)+" to the reference "+str(self.reference)+"\n")
             dens_remapped = hop.sitemap.remap_density(dens,self.reference) # copy
             densityList[k] = dens_remapped    # assignment (no idea why it has to be that clumsy)
Beispiel #3
0
    def run(self,Ntotal=500000,Nskip=1000,verbosity=None):
        """MCMC run multiple cycles of lebgth <Nskip> scans for a total of <Ntotal>.

        run(Ntotal=500000,Nskip=1000)

        Starts from the current configuration in state.
        Creates the collection of configurations states: one state every Nskip steps
        """
        set_verbosity(verbosity)

        Ntotal = int(Ntotal)
        Nskip = int(Nskip)
        Ncycle = int(Ntotal/Nskip)

        self.runparameters = {'Ntotal':Ntotal,'Nskip':Nskip,'Ncycle':Ncycle}
        state_list = []
        msg(1,"Running MCMC:\n\tNtotal = %(Ntotal)d\n\tSaving state every cycle of Nskip steps = %(Nskip)d\n\t"
            "Hence in total Ncycle cycles = %(Ncycle)d\n" % locals())
        for i in xrange(1,Ncycle+1):
            msg(3,"cycle %(i)4d/%(Ncycle)4d\r" % locals())
            self.sample(Nskip)
            state_list.append(self.statevector)
        msg(3,"\n")

        if len(self.states) == 0:
            self.states = numpy.array(state_list)
        else:
            msg(2,"Extending states from %d configurations by %d new ones." \
                    % (len(self.states), len(state_list)))
            self.states = numpy.concatenate((self.states, numpy.array(state_list)))
Beispiel #4
0
    def scan(self,cutoffs,merge=True):
        if not self.with_densities or not (self.bulkdensities and self.refbulkdensity):
            # need bulk densities because redrawing the map at a new
            # cutoff completely erases the manually inserted bulk
            # density
            raise ValueError("Densities (including bulk) are required for a scan.")
        if not merge:
            self.scanstats = {}    # indexed by cutoff, list of stats

        self._check_and_remap([self.refbulkdensity])
        self._check_and_remap(self.densities)
        self._check_and_remap(self.bulkdensities)

        try:
            iter(cutoffs)
        except TypeError:
            cutoffs = [cutoffs]
        for rhocut in cutoffs:
            msg(3, "rhocut = %5.2f: " % rhocut)
            r = self.reference
            r.map_sites(rhocut)
            r.site_insert_bulk(self.refbulkdensity)
            for k,d in enumerate(self.densities):
                msg(3,"%d/ref " % k)
                d.map_sites(rhocut)
                d.site_insert_bulk(self.bulkdensities[k])
                d.find_equivalence_sites_with(r)
                self.scanstats[rhocut] = {'reference':r.stats(),k:d.stats()}
            msg(3,"\n")
        self._scan2recarray()
        msg(3, "Scan complete. See scanstats and scanarrays attributes.")
Beispiel #5
0
    def save(self,filename='pscan.pickle'):
        """Save pscan object to pickle file.

        save(pscan.pickle)

        Load with

            import cPickle
            myPscan = cPickle.load(open('pscan.pickle'))
        """
        import cPickle
        fh = open(filename,'wb')
        try:
            cPickle.dump(self,fh,cPickle.HIGHEST_PROTOCOL)
        finally:
            fh.close()
        msg(3,"Wrote Pscan object to '%s'.\n" % filename)
Beispiel #6
0
def run(filename='hopgraph.pickle',Ntotal=500000,Nskip=1000,Nequil=Nequil_default):
    """Perform Markov Chain Monte Carlo on a model derived from the hopping graph."""

    h = hop.graph.HoppingGraph(filename=filename)
    M = MCMCsampler(h)

    msg(0,"MCMCsampler() for %s\n" % filename)

    if Nequil > 0:
        # burn in/flooding
        msg(1,"Running %(Nequil)d steps for equilibration...\n" % locals())
        M.sample(Nequil,record_iterations=False)

    # MCMC run
    M.run(Ntotal=Ntotal,Nskip=Nskip,verbosity=3)

    return M
Beispiel #7
0
    def save(self, filename='pscan.pickle'):
        """Save pscan object to pickle file.

        save(pscan.pickle)

        Load with

            import cPickle
            myPscan = cPickle.load(open('pscan.pickle'))
        """
        import cPickle
        fh = open(filename, 'wb')
        try:
            cPickle.dump(self, fh, cPickle.HIGHEST_PROTOCOL)
        finally:
            fh.close()
        msg(3, "Wrote Pscan object to '%s'.\n" % filename)
Beispiel #8
0
    def plot(self,filename=None,format='pdf',**kwargs):
        """Plot the heatmap and save to an image file.

          plot()             # display using windowing system
          plot('hm')         # --> hm.pdf
          plot('hm.png')     # --> hm.png
          plot('hm','png')   # --> hm.png

        By default a clustered heat map is constructed using R's heatmap.2
        function. If R cannot be found, an unclustered heat map is
        plotted. **kwargs can be used to customize the output.

        :Arguments:
        filename       name of the image file; may contain extension
                       If empty use the windowing system.
        format         eps,pdf,png... whatever matplotlib understands

        **kwargs for R:
        scale          Determines the coloring. Choose between 'none' (the
                       actual values in the heat map (possibly already normalized)),
                       'row' or 'column' (z-score across the dimension)
        N_colors       Number of color levels; default is 32.

        **kwargs for matplotlib:
           The kwargs are applied to the matplotlib.text() method and
           are typically used to set font properties. See the
           pylab/matplotlib documentation.
        """
        if filename:
            format = hop.utilities.fileextension(filename,default=format)
        labels = self.labels()
        try:
            try:
                import rpy
            except ImportError:
                from rpy2 import rpy_classic as rpy
                # http://www.mail-archive.com/[email protected]/msg01893.html
                rpy.set_default_mode(rpy.BASIC_CONVERSION)
            self._heatmap_R(labels,filename=filename,format=format,**kwargs)
        except ImportError:
            msg(0,"rpy package missing: cannot plot clustered heat map, defaulting to "
                "an unclustered heat map")
            self._heatmap_matplotlib(labels,filename=filename,format=format,**kwargs)
        if filename:
            msg(1,"Wrote image to file %s.\n" % self.filename(filename,ext=format))
Beispiel #9
0
def run(filename='hopgraph.pickle',
        Ntotal=500000,
        Nskip=1000,
        Nequil=Nequil_default):
    """Perform Markov Chain Monte Carlo on a model derived from the hopping graph."""

    h = hop.graph.HoppingGraph(filename=filename)
    M = MCMCsampler(h)

    msg(0, "MCMCsampler() for %s\n" % filename)

    if Nequil > 0:
        # burn in/flooding
        msg(1, "Running %(Nequil)d steps for equilibration...\n" % locals())
        M.sample(Nequil, record_iterations=False)

    # MCMC run
    M.run(Ntotal=Ntotal, Nskip=Nskip, verbosity=3)

    return M
Beispiel #10
0
    def _heatmap_matplotlib(self,labels,filename=None,format='pdf',**kwargs):
        """Plot a un-clustered heat map using matplotlib."""
        import pylab

        pylab.clf()
        pylab.imshow(self.heatmap,interpolation='nearest')
        pylab.axis('off')
        pylab.colorbar(pad=0.075)
        col_labels = [_add_x_ticklabel(n,label,**kwargs)
                      for n,label in enumerate(labels['columns'])]
        row_labels = [_add_y_ticklabel(n,label,**kwargs)
                      for n,label in enumerate(labels['observables'])]
        norm_labels = [_add_y_ticklabel(n,label,
                                       offset=self.heatmap.shape[1], # number of colums
                                       horizontalalignment='left',
                                       **kwargs)
                       for n,label in enumerate(labels['normalizations'])]
        if filename is not None:
            pylab.savefig(self.filename(filename,ext=format,set_default=True))
        else:
            msg(3,"Only display, no file written because filename == None.")
Beispiel #11
0
    def _occupancy_histo(self,numfig=1):
        import pylab

        msg(1,"figure %(occupancy)s: occupancy histograms" % self.pdf)
        pylab.figure(numfig)
        pylab.clf()
        pylab.axes(axisbg='w',frameon=False)  # bg not visible?
        barlegend = LegendContainer()

        n,bins = numpy.histogram(self.D['site_occupancy_kin_avg'],bins=numpy.arange(0.1,1.6,0.1))
        lines = pylab.bar(bins,n,align='edge',color='k',linewidth=0,width=0.1)
        barlegend.append(lines[0],'from kinetics')

        n,bins = numpy.histogram(self.D['site_occupancy_rho_avg'],bins=numpy.arange(0.1,1.6,0.1))
        lines = pylab.bar(bins,n,align='edge',color=(0.7,0.7,0.7),alpha=0.7,linewidth=0.1,width=0.1)
        barlegend.append(lines[0],'from density')

        pylab.legend(*barlegend.args())
        pylab.xlabel('occupancy')
        pylab.ylabel('count')
        pylab.savefig(self.pdf['occupancy'])
Beispiel #12
0
    def _lifetime_histo(self,numfig=1):
        import pylab

        msg(1,"figure %(lifetime)s: life time histogram" % self.pdf)
        pylab.figure(numfig)
        pylab.clf()
        pylab.axes(axisbg='w',frameon=False)  # bg not visible?
        barlegend = LegendContainer()

        n,bins=numpy.histogram(self.D['site_lifetime_avg'],
                               bins=numpy.concatenate((numpy.arange(0,1000,50),
                                                       [1000,5000,10000,20000])))
        xcutoff = 1000
        N_outliers = numpy.sum(n[bins > xcutoff])
        lines = pylab.bar(bins,n,align='edge',color='k',linewidth=0,width=50,
                          label='life time')
        barlegend.append(lines[0],'life time\n(%(N_outliers)d counts > '
                         '%(xcutoff)g)' % locals() )

        pylab.xlim(0,xcutoff)
        pylab.legend(*barlegend.args())
        pylab.xlabel('life time/ps')
        pylab.ylabel('count')
        pylab.savefig(self.pdf['lifetime'])
Beispiel #13
0
    def run(self, Ntotal=500000, Nskip=1000, verbosity=None):
        """MCMC run multiple cycles of lebgth <Nskip> scans for a total of <Ntotal>.

        run(Ntotal=500000,Nskip=1000)

        Starts from the current configuration in state.
        Creates the collection of configurations states: one state every Nskip steps
        """
        set_verbosity(verbosity)

        Ntotal = int(Ntotal)
        Nskip = int(Nskip)
        Ncycle = int(Ntotal / Nskip)

        self.runparameters = {
            'Ntotal': Ntotal,
            'Nskip': Nskip,
            'Ncycle': Ncycle
        }
        state_list = []
        msg(
            1,
            "Running MCMC:\n\tNtotal = %(Ntotal)d\n\tSaving state every cycle of Nskip steps = %(Nskip)d\n\t"
            "Hence in total Ncycle cycles = %(Ncycle)d\n" % locals())
        for i in xrange(1, Ncycle + 1):
            msg(3, "cycle %(i)4d/%(Ncycle)4d\r" % locals())
            self.sample(Nskip)
            state_list.append(self.statevector)
        msg(3, "\n")

        if len(self.states) == 0:
            self.states = numpy.array(state_list)
        else:
            msg(2,"Extending states from %d configurations by %d new ones." \
                    % (len(self.states), len(state_list)))
            self.states = numpy.concatenate(
                (self.states, numpy.array(state_list)))
Beispiel #14
0
 def r_dev_off():
     msg(1,"Interactive R display. Kill with hop.analysis.kill_R()")
     pass
Beispiel #15
0
    def _heatmap_R(self,labels,filename=None,format='pdf',**kwargs):
        """Plot a clustered heat map using R."""
        # Note on coloring: test if data is z-score normalized and if
        #  true, force scale='row' for the heat map so that this is
        #  reflected in the legend (in fact, the z-score is recomputed
        #  over the data for heatmap.2 coloring but it looks identical
        #  and in any case, the clustering is done on the original
        #  data --- which is NOT clear from the heatmap docs)
        try:
            from rpy import r, RException
        except ImportError:
            from rpy2.rpy_classic import r, RException

        hm_args = dict(scale='none',
                       margins=(10,10), # space for long labels
                       N_colors=32,
                       )
        hm_args.update(kwargs)
        N_colors = hm_args.pop('N_colors')  # N_colors not a true heatmap argument
        if filename is not None:
            interactive = False
            def r_format():
                r[format](filename)
            def r_dev_off():
                r.dev_off()
        else:
            interactive = True
            def r_format():
                try:
                    r.X11()
                except RException:
                    r.quartz()
            def r_dev_off():
                msg(1,"Interactive R display. Kill with hop.analysis.kill_R()")
                pass
        if self.normalization_method is 'zscore' and hm_args['scale'] is not 'row':
            hm_args['scale'] = 'row'
            msg(3,"Forcing scale='row' for z-score normalized heat map.\n")
            # (This only has the effect to put the label 'Row Z-score' in the graph)
        try:
            r.library('colorRamps')
            r_color = r.matlab_like(N_colors) # getting somewhat close to matplotlib 'jet'
        except RException:
            msg(1,"For matplotlib-like colors install the R-package 'colorRamps':\n"
                ">>> import rpy\n"
                ">>> rpy.r.install_packages('colorRamps',type='source')")
            r_color = r.topo_colors(N_colors)
        try:
            r.library('gplots')
            r_heatmap = r.heatmap_2
            hm_args.update(dict(key=r.TRUE, symkey=r.FALSE,
                                density_info='histogram', trace='none'))
        except RException:
            msg(1,"For heatmap with a legend install the R-package 'gplots' via \n"
                ">>> import rpy\n"
                ">>> rpy.r.install_packages('gplots',type='source')")
            r_heatmap = r.heatmap

        r_format()
        r_heatmap(self.heatmap,
                  labRow=labels['observables'],
                  labCol=labels['columns'],
                  col=r_color,
                  **hm_args)
        r_dev_off()
Beispiel #16
0
    def plot(self,fn=None,idens=0,functions='all',properties=None,fignumber=1):
        """Plot density statistics against rho_cut for reference (black) and density 0 (red).

        plot(filename,properties=<dict of dicts>)

        Plot various functions of the density cut-off rho_cut. Current
        functions are 'sites', 'volume', 'occupancy', or 'all'.

        Plots can be customized by using the properties dict. To
        change the ylim and add an title to the sites graph, use

           properties = {'sites': {ylim':(0,220),'title':'number of sites'}}

        :Arguments:
        fn            file name for the file; default is scan.pdf. Suffix determines file type.
        idens         number of density plot; the first one is 0 in self.scanarrays[].
        functions     list of function names or 'all'
        properties    dict1 of dicts; keys1: sites, volume, occupancy;
                      keys2: any matplotlib settable property, values2: appropriate values
        fignumber     pylab figure number
        """
        import pylab
        available_functions = ['sites', 'volume', 'occupancy']
        plot_functions = []
        if functions is 'all':
            plot_functions = available_functions
        else:
            for f in functions:
                if f in available_functions:
                    plot_functions.append(f)
                else:
                    raise ValueError('No function '+str(f)+' is available, only '+
                                     str(available_functions))
        props = self.__plot_properties_default.copy()
        if type(properties) is dict:
            for k,userparams in properties.items():
                try:
                    props[k].update(userparams)
                except KeyError:
                    props[k] = userparams

        r = self.scanarrays['reference']
        d = self.scanarrays[idens]

        pylab.figure(fignumber)
        pylab.clf()
        pylab.axes(axisbg='w',frameon=False)

        par = props['sites']
        pylab.subplot(221)
        pylab.plot(r.rho_cut,r.N_equivalence_sites,'ko',label="N_equiv")
        pylab.plot(r.rho_cut,r.N_sites,'kx',label="N_sites")
        pylab.plot(r.rho_cut,d.N_sites,'rx')
        pylab.ylim(par['ylim'])
        #pylab.legend()

        # refine err bar plots
        alpha = 1  # acts on the line not on the errorbar markers
        capsize=0

        par = props['volume']
        pylab.subplot(222)
        pylab.errorbar(r.rho_cut,r.site_volume_avg,yerr=r.site_volume_std,
                       color='k',capsize=capsize,alpha=alpha,label="volume")
        pylab.errorbar(r.rho_cut,d.site_volume_avg,yerr=d.site_volume_std,
                       color='r',capsize=capsize,alpha=alpha,label="volume")
        pylab.ylim(par['ylim'])
        pylab.ylabel(par['ylabel'])
        # [pylab.set(pylab.gca(), k, v) for k,v in par.items()]

        par = props['occupancy']
        pylab.subplot(223)
        pylab.errorbar(r.rho_cut,r.site_occupancy_rho_avg,yerr=r.site_occupancy_rho_std,
                       color='k',capsize=capsize,alpha=alpha)
        pylab.errorbar(r.rho_cut,d.site_occupancy_rho_avg,yerr=d.site_occupancy_rho_std,
                       color='r',capsize=capsize,alpha=alpha)
        #pylab.legend()
        pylab.ylim(par['ylim'])
        pylab.xlabel(par['xlabel'])
        pylab.ylabel(par['ylabel'])

        pylab.savefig(fn)
        msg(1, "Graph saved to %s\n" % fn)

        pylab.close(fignumber)