示例#1
0
    def to_multicanvas(self,
                       canvas,
                       sequential=False,
                       sans=False,
                       modnames=True):
        """ """
        c_in = []
        c_out = []
        creduced, cnull = self.descriptors[0].in_and_out_canvas(canvas)
        c_in.append(creduced)
        c_out.append(cnull)

        for desc in self.descriptors[1:]:
            if sequential:
                creduced, cnull = desc.in_and_out_canvas(cnull)

            else:
                creduced, cnull = desc.in_and_out_canvas(canvas)

            c_in.append(creduced)
            c_out.append(cnull)

        outnames = self.aliases
        if not sans:
            c_final = c_in

        else:
            c_final = c_out
            if modnames:
                outnames = ['sans %s' % name for name in outnames]

        mcout = MultiCanvas(canvii=c_final, names=outnames)
        mcout.set_colors(*self.colors, fillnull=True)
        return mcout
示例#2
0
    def to_multicanvas(self, canvas, sequential=False, sans=False,
                       modnames=True):
        """ """
        c_in = [] ; c_out = []
        creduced, cnull = self.descriptors[0].in_and_out_canvas(canvas)
        c_in.append(creduced) ; c_out.append(cnull)

        for desc in self.descriptors[1:] :
            if sequential:
                creduced, cnull = desc.in_and_out_canvas(cnull)
           
            else:
                creduced, cnull = desc.in_and_out_canvas(canvas)
                
            c_in.append(creduced) ; c_out.append(cnull)             
        
        outnames = self.aliases
        if not sans:
            c_final = c_in
            
        else:
            c_final = c_out
            if modnames:
                outnames = ['sans %s' % name for name in outnames]

        mcout = MultiCanvas(canvii=c_final, names=outnames)
        mcout.set_colors(*self.colors, fillnull=True)
        return mcout    
示例#3
0
    def run_main(self):
        """ Define a series of sub-functions so logging/traceback easier """
        
        def getPARAM(attr, null=None):
            """ Geattr on self.PARAMS with optional null to return empty lists
            or dicts instead of None """
            return getattr(self.PARAMS, attr, null)
                
        def mkfromrootdir(dirname):
            """ Make a directory relative to self.ARGS.root; log it """
            fullpath = op.join(self.ARGS.outroot, dirname)
            self.logmkdir(fullpath)
            return fullpath
     
               
        @continue_on_fail
        def MPLOT(mcattr, mcpltfcn, mcpltkwds, outdirectory):
            """ boilerplate reduction; if multiplot parameter, runs the plot."""
            
            if getPARAM(mcattr):
                mcpltfcn, mcpltkwds = getattr(mc, mcpltfcn), getPARAM(mcpltkwds, null={})
                mcpltfcn(**mcpltkwds)                    
                self.logsavefig(op.join(outdirectory, getPARAM(mcattr) ))                  
            
    
        summary = open(op.join(self.ARGS.outroot, SUMMARYFILE), 'a') #make method 

        stream = self.PARAMS.parameter_stream()
        paramspath = op.join(self.ARGS.outroot, PARAMSFILE)
        self.logstream(paramspath, '\n\n'.join(stream))
        
        # MULTICANVAS OPERATIONS
        #-----------------------
        self.LOGGER.info("Creating MultiCanvas...")
        if self.PARAMS.mapper is None:
            self.PARAMS.mapper = []
        mc = MultiCanvas.from_labeled(self.ARGS.image, 
                                      storecolors=self.PARAMS.storecolors, 
                                      ignore=self.PARAMS.ignore,
                                      mapper = self.PARAMS.mapper
                                      )
        
        def sumwrite(string, newlines='\n\n', indent=''):
            summary.write(indent + string + newlines)
        
        sumwrite(mc.__repr__())
        
        multidir = mkfromrootdir(self.PARAMS.multidir)
    
        MPLOT('multihist', 'hist', 'multihistkwds', multidir)
        MPLOT('multipie', 'pie', 'multipiekwds', multidir)
        MPLOT('multishow', 'show', 'multishowkwds', multidir)

    
        # CANVAS OPERATIONS
        #-----------------------        
        #Early exit if not canvas operations
        if not getattr(self.PARAMS, 'canvasdir', None):
            return


        @continue_on_fail
        def canvas_stats(canvas, name='STATS'):
            """ Writes a text stream of various canvas parameters. """
            head = '### %s ###'% name            
            sumwrite(head, newlines='\n')
            sumwrite('-' * len(head))

            sumwrite(canvas.__repr__())
            sumwrite("Image Resolution: %s"% str(canvas.rez), indent=_INDENT)
            sumwrite("Particle coverage: %.2f%%" % 
                     round(100*canvas.pixarea, _ROUND), indent=_INDENT)

            for attr in getPARAM('summary_attr'):
                val = getattr(canvas, attr)
                xmin, xmax, xmean = min(val), max(val), np.mean(val)
                sumwrite("%s (min, max, mean):   (%.2f - %.2f, %.2f)"
                         % (attr, xmin, xmax, xmean), indent=_INDENT)
            sumwrite('')

        @continue_on_fail
        def canvas_hist(canvas, attr, savepath=None, **histkwds):
            """ Histogram of canvas attribute """

            attr_array = getattr(canvas, attr)
            plt.hist(attr_array, **histkwds)
            plt.xlabel(attr)
            plt.ylabel('counts')
            if savepath:
                self.logsavefig(savepath)

        
        self.LOGGER.info("Creating Canvas List")

        # List of new canvas and canvas-by-canvas breakdown
        total_canvas = mc.to_canvas(mapcolors=True)  

        #X Don't mess w/ order, net canvas must be first (see below idx==0)
        ALLITEMS = [(getPARAM('canvasdir', null='Net Canvas'), total_canvas)]
        if getPARAM('canvas_by_canvas'):
            ALLITEMS.extend(mc.items())
            canbycandir = mkfromrootdir('canvas_by_canvas')
      

        # Stats of each canvas pairwise
        for idx, (name, canvas) in enumerate(ALLITEMS):
            if idx == 0:
                workingcanvasdir = mkfromrootdir(name)
            else:
                workingcanvasdir = mkfromrootdir('%s/%s' % (canbycandir, name))

            autocolor = None
            if getPARAM('autocolor'):
                if idx != 0:
                    autocolor = mc._request_plotcolors()[idx-1]

            # Set canvas background
            if getPARAM('canvas_background'):
                canvas = canvas.set_bg(getPARAM('canvas_background'))

            canvas_stats(canvas, name)
            
            # Color/ Gray/ Binary image  #Maybe refactor in future boilerplate
            if getPARAM('colorimage'):
                colorkwds = getPARAM('showkwds', null={})
                canvas.show(**colorkwds)
                self.logsavefig(op.join(workingcanvasdir, '%s_colored.png' % name))
                
            if getPARAM('grayimage'):
                graykwds = getPARAM('graykwds', null={})
                if 'cmap' not in graykwds:
                    graykwds['cmap'] = 'gray'
                canvas.show(**graykwds)
                self.logsavefig(op.join(workingcanvasdir, '%s_gray.png' % name))

                
            if getPARAM('binaryimage'):
                binarykwds = getPARAM('binarykwds', null={})
                binarykwds.update({'cmap':'pbinary'})
                canvas.show(**binarykwds)
                self.logsavefig(op.join(workingcanvasdir, '%s_binary.png' % name))


            # Scatter plots
            for (x,y) in getPARAM('scatter', null=[]):

                scatterkwds = getPARAM('scatterkwds')
                
                if autocolor and 'color' not in scatterkwds:       #Don't use update         
                    canvas.scatter(attr1=x, attr2=y, color=autocolor, **scatterkwds)
                else:
                    canvas.scatter(attr1=x, attr2=y, **scatterkwds)
                self.logsavefig(op.join(workingcanvasdir, '%s_scatter.png' % name))
                

            # Generate histograms
            
            for attr in getPARAM('summary_attr'):
                savepath = op.join(workingcanvasdir, '%s_hist.png'%name)
                histkwds = getPARAM('histkwds', null={})

                if autocolor and 'color' not in histkwds:
                    canvas_hist(canvas, attr, color=autocolor, 
                                savepath=savepath, **histkwds)
                else:
                    canvas_hist(canvas, attr, savepath=savepath, **histkwds)

        
        summary.close()
示例#4
0
    def run_main(self):
        """ Define a series of sub-functions so logging/traceback easier """
        def getPARAM(attr, null=None):
            """ Geattr on self.PARAMS with optional null to return empty lists
            or dicts instead of None """
            return getattr(self.PARAMS, attr, null)

        def mkfromrootdir(dirname):
            """ Make a directory relative to self.ARGS.root; log it """
            fullpath = op.join(self.ARGS.outroot, dirname)
            self.logmkdir(fullpath)
            return fullpath

        @continue_on_fail
        def MPLOT(mcattr, mcpltfcn, mcpltkwds, outdirectory):
            """ boilerplate reduction; if multiplot parameter, runs the plot."""

            if getPARAM(mcattr):
                mcpltfcn, mcpltkwds = getattr(mc,
                                              mcpltfcn), getPARAM(mcpltkwds,
                                                                  null={})
                mcpltfcn(**mcpltkwds)
                self.logsavefig(op.join(outdirectory, getPARAM(mcattr)))

        summary = open(op.join(self.ARGS.outroot, SUMMARYFILE),
                       'a')  #make method

        stream = self.PARAMS.parameter_stream()
        paramspath = op.join(self.ARGS.outroot, PARAMSFILE)
        self.logstream(paramspath, '\n\n'.join(stream))

        # MULTICANVAS OPERATIONS
        #-----------------------
        self.LOGGER.info("Creating MultiCanvas...")
        if self.PARAMS.mapper is None:
            self.PARAMS.mapper = []
        mc = MultiCanvas.from_labeled(self.ARGS.image,
                                      storecolors=self.PARAMS.storecolors,
                                      ignore=self.PARAMS.ignore,
                                      mapper=self.PARAMS.mapper)

        def sumwrite(string, newlines='\n\n', indent=''):
            summary.write(indent + string + newlines)

        sumwrite(mc.__repr__())

        multidir = mkfromrootdir(self.PARAMS.multidir)

        MPLOT('multihist', 'hist', 'multihistkwds', multidir)
        MPLOT('multipie', 'pie', 'multipiekwds', multidir)
        MPLOT('multishow', 'show', 'multishowkwds', multidir)

        # CANVAS OPERATIONS
        #-----------------------
        #Early exit if not canvas operations
        if not getattr(self.PARAMS, 'canvasdir', None):
            return

        @continue_on_fail
        def canvas_stats(canvas, name='STATS'):
            """ Writes a text stream of various canvas parameters. """
            head = '### %s ###' % name
            sumwrite(head, newlines='\n')
            sumwrite('-' * len(head))

            sumwrite(canvas.__repr__())
            sumwrite("Image Resolution: %s" % str(canvas.rez), indent=_INDENT)
            sumwrite("Particle coverage: %.2f%%" %
                     round(100 * canvas.pixarea, _ROUND),
                     indent=_INDENT)

            for attr in getPARAM('summary_attr'):
                val = getattr(canvas, attr)
                xmin, xmax, xmean = min(val), max(val), np.mean(val)
                sumwrite("%s (min, max, mean):   (%.2f - %.2f, %.2f)" %
                         (attr, xmin, xmax, xmean),
                         indent=_INDENT)
            sumwrite('')

        @continue_on_fail
        def canvas_hist(canvas, attr, savepath=None, **histkwds):
            """ Histogram of canvas attribute """

            attr_array = getattr(canvas, attr)
            plt.hist(attr_array, **histkwds)
            plt.xlabel(attr)
            plt.ylabel('counts')
            if savepath:
                self.logsavefig(savepath)

        self.LOGGER.info("Creating Canvas List")

        # List of new canvas and canvas-by-canvas breakdown
        total_canvas = mc.to_canvas(mapcolors=True)

        #X Don't mess w/ order, net canvas must be first (see below idx==0)
        ALLITEMS = [(getPARAM('canvasdir', null='Net Canvas'), total_canvas)]
        if getPARAM('canvas_by_canvas'):
            ALLITEMS.extend(mc.items())
            canbycandir = mkfromrootdir('canvas_by_canvas')

        # Stats of each canvas pairwise
        for idx, (name, canvas) in enumerate(ALLITEMS):
            if idx == 0:
                workingcanvasdir = mkfromrootdir(name)
            else:
                workingcanvasdir = mkfromrootdir('%s/%s' % (canbycandir, name))

            autocolor = None
            if getPARAM('autocolor'):
                if idx != 0:
                    autocolor = mc._request_plotcolors()[idx - 1]

            # Set canvas background
            if getPARAM('canvas_background'):
                canvas = canvas.set_bg(getPARAM('canvas_background'))

            canvas_stats(canvas, name)

            # Color/ Gray/ Binary image  #Maybe refactor in future boilerplate
            if getPARAM('colorimage'):
                colorkwds = getPARAM('showkwds', null={})
                canvas.show(**colorkwds)
                self.logsavefig(
                    op.join(workingcanvasdir, '%s_colored.png' % name))

            if getPARAM('grayimage'):
                graykwds = getPARAM('graykwds', null={})
                if 'cmap' not in graykwds:
                    graykwds['cmap'] = 'gray'
                canvas.show(**graykwds)
                self.logsavefig(op.join(workingcanvasdir,
                                        '%s_gray.png' % name))

            if getPARAM('binaryimage'):
                binarykwds = getPARAM('binarykwds', null={})
                binarykwds.update({'cmap': 'pbinary'})
                canvas.show(**binarykwds)
                self.logsavefig(
                    op.join(workingcanvasdir, '%s_binary.png' % name))

            # Scatter plots
            for (x, y) in getPARAM('scatter', null=[]):

                scatterkwds = getPARAM('scatterkwds')

                if autocolor and 'color' not in scatterkwds:  #Don't use update
                    canvas.scatter(attr1=x,
                                   attr2=y,
                                   color=autocolor,
                                   **scatterkwds)
                else:
                    canvas.scatter(attr1=x, attr2=y, **scatterkwds)
                self.logsavefig(
                    op.join(workingcanvasdir, '%s_scatter.png' % name))

            # Generate histograms

            for attr in getPARAM('summary_attr'):
                savepath = op.join(workingcanvasdir, '%s_hist.png' % name)
                histkwds = getPARAM('histkwds', null={})

                if autocolor and 'color' not in histkwds:
                    canvas_hist(canvas,
                                attr,
                                color=autocolor,
                                savepath=savepath,
                                **histkwds)
                else:
                    canvas_hist(canvas, attr, savepath=savepath, **histkwds)

        summary.close()