def set(self, address): """ This parameter stores a biovars.Address object. """ if isinstance(address, basestring): address = self._p._dataset.experiment.variables.get(address) if address is None or isaddress(address): Param.set(self, address) else: Param.set(self, asaddress(address))
def initWithSegments_over_(self, segments, over, exclude=None, func=None, mp=False, **kwargs): """ kwargs: func=None: is initially applied to the whole data cube; can be string 'phase'--> x/abs(x) else: eval mp=True: use multiprocessing v=False verbose mode """ raise NotImplementedError("TODO: get this method out of here!") # interpret func if func == None: def func(x): return x elif func=='phase': def func(x): return x/abs(x) elif type(func)==str: func = eval(func) #logging.debug( 'func'+str(func) ) # start t0 = time.time() self._properties.update(kwargs) kwargs['out']='data' if exclude: over = over!=exclude else: over = asaddress(over) segs_sorted = over.sort(segments) slist = segs_sorted.keys() slist.sort() self._properties.update({'slist':slist, 'svar':over, 'avars': '???', 'address': '???'}) # determine shape seg1 = segs_sorted[slist[0]][0] data1 = seg1.subdata(**kwargs) shape = data1.shape # create empty array self._data = np.empty(shape + (len(slist),)) # collect data for i, s in enumerate(slist): segs = segs_sorted[s] assert len(segs) > 0, "No data for %s"%str(s) data = np.empty(shape + (len(segs),)) for j, seg in enumerate(segs): segdata = seg.subdata(**kwargs) if shape == segdata.shape: data[...,j] = segdata else: msg = "'{n}' shape {s1} does not match the shape {s}" msg.format(n=seg.name, s1=segdata.shape, s=shape) raise ValueError(msg) self._data[...,i] = data.mean(-1) # info #segInfo = ' '.join(['%s:%s'%(k, str(len(v)).rjust(3)) for k,v in segments.iteritems()]) print ' '.join([self.name[:10], 'over', over.name[:10], ": time %.1f s"%(time.time()-t0)]) return self
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()
def set_cov(self, covs): "list of variables" if covs: self._covs = _vars.asaddress(covs).keys() else: self._covs = []