Beispiel #1
0
    def statsForAddress(self, address, over, 
#                        exclude=None,
                        color=None, 
#                        _connector=None, 
                        **kwargs):
        """
        initializes a statsSegment with the mean for each value of 'over' variable for address
        kwargs: func: function that is applied to each segment.data before averaging
                    e.g. use abs to get Amplitude from wavelet data
                   -'phase' to get phase ( x/abs(x) - results in a complex number)
                **subdata kwargs
        """
        assert ('segmented' in self.properties) and self.properties['segmented']

        segments = address.filter(self.segments)
        
        if len(segments) == 0:
            return None
        properties = deepcopy(self.properties)
        if color:
            properties.update(color=color)
        s = StatsSegment(properties=properties, name=address.name)
        s.initWithSegments_over_(segments, over, **kwargs)
        
        return s
Beispiel #2
0
    def collectstats(self, cells, over, mask=None, mp=False, pool=None, 
                     save='ask', **kwargs):
        """
        cells: address; statistics are extracted for each valid combination of
               values in this address
        over: variable specifying the cases within the cells (usually subject)
        
        - adds statistics segment for all values of cells in address to self.stats
        - forwards statsForAddress_over_ kwargs
        
        kwargs:
        mp  False
            'outer' Spawn one process for each StatsSegment 
            True    do statsSegments serially but use multiple processes in
                    StatsSegment.initWithSegments_over_
                    
        **kwargs: 
                    
        """
        t0=time.time()
        msg = "--> Collecting Statistics for {0} over {1}, mp={2}"
        print msg.format(cells.name, over.name, str(mp))
        
        if len(self.stats) > 0:
            msg = "self.stats currently contains the following statistics: %s"%str(self.stats.keys())
            if ui.ask(title="Clear existing statistics?",
                      message=msg):
                self.stats = {}
        
        cells = biovars.asaddress(cells)
        if mask == None:
            all_segments = self.segments
        else:
            all_segments = mask.filter(self.segments)
        segs_sorted = cells.sort(all_segments)
        
        # progress bar
#        n = len(segs_sorted)
#        i = 0
#        prog_ttl = "Collecting statistics for {n} cells".format(n=n)
#        prog_msg = "{name} ({i} of {n}) done...".format(name='{name}', n=n, i='{i}')
#        prog = ui.progress(i_max=n,
#                           title=prog_ttl,
#                           message=prog_msg.format(name='', i=0))
        
        label_dic = cells.short_labels()
        
        for address, segs in segs_sorted.iteritems():
            label = label_dic[address]
#            color = cells.color_for_value(key)
            s = StatsSegment(self.properties, name=label)#, color=color)
            s.initWithSegments_over_(segs, over, **kwargs)

            # make sure label is a valid name
            if label[0].isalpha():
                valid_label = label
            else:
                valid_label = 'c' + label
            
            self.stats[valid_label] = s
            
            # close all files
            self._store.close()
        
#        # progress bar
#            if prog:
#                i += 1
#                prog.advance(prog_msg.format(name=valid_label, i=i))
#        if prog:
#            prog.terminate()


#        if mp=='outer':
#            for key, label in cells.dictionary.iteritems():
#                self.stats[label] = self.stats[label].recv()
        print '\n'.join(["---",
                         "Finished after %s"%(time.time()-t0),
                         "Dataset now contains stats: %s"%self.stats.keys()])
        
        # save the experiment
        if save == 'ask':
            msg = "Save Experiment with newly added statistics segments?"
            save = ui.ask(title="Save Experiment?", message=msg)
        if save:
            self.experiment.save()