def readNL(f, **kwargs): nlr = NLReader(f) objlist=[] singlepoint=[] for o in nlr.object_list: if o.__tag__=="Fiducial" and o.attrib("Style")=="points": if o.points.shape[0]==1: singlepoint.append(o) continue d = o.getPoints()[:,3] if not all(d == d[0]): o.attributes["Style"]="spheres" objlist.append(o) if singlepoint: f = createElement("Fiducial", singlepoint[0].attributes) for o in singlepoint: p = o.points[0] n = o.name() f.setPoints(p, append=1) i = len(f.points)-1 f.point_labels[i]=n objlist.append(f) n = createElement("Nmpml", {"Name":"Doc"}) for o in objlist: n.newElement(o) return n
def _gmm2dat(atr, weights, means, covs): atr.update({"SampleType": "Group", "Name": "gmm"}) dat1 = createElement("Data", atr) dw = createElement("Data", {"SampleType": "generic", "Name": "weights"}) dw.datinit(weights) dat1.newElement(dw) dm = createElement("Data", {"SampleType": "generic", "Name": "means"}) dm.datinit(means) dat1.newElement(dm) dc = createElement("Data", {"SampleType": "generic", "Name": "covs"}) dc.datinit(covs) dat1.newElement(dc) return dat1
def writeDisplaySpec(cv): vpdoc = createElement('CellViewerDisplay', {}) vp=getCurrentVP(cv) attr = {'viewpoint':tuple(vp[0,:]), 'forward':tuple(vp[1,:]), 'up':tuple(vp[2,:]), 'extent':vp[3,0], 'depth':vp[3,1], 'background':cv.graph.clearcolor, 'slices':cv.graph.slices, } vpdoc.newElement(createElement('ViewerSettings', attr)) vpdoc.newElement(createElement('Filter', {'filter':cv.displayfilter})) cv.save(doc=vpdoc)
def gmmToSpatialField(doc, upathAbstract="/AbstractModel:class1LLdensity", edge = 5.0, minvalue=.01, maxvalue=.1): am = doc.getInstance(upathAbstract) bb = _getGMMBB(am, minvalue) origin = bb[0] rang = bb[1]-bb[0] rang = ceil(rang/edge).astype(int32) indexes = combinations([arange(rang[0]), arange(rang[1]), arange(rang[2])]) dat = indexes*edge + array(origin) ds = createElement("Data", {"Name":'points', "SampleType":"generic"}) ds.datinit(dat) am.run(ds) sfs = ds.getSubData('gmmout').getData() md = sfs.max() keep = nonzero(sfs>=minvalue*sfs.max())[0] if keep.shape[0]<indexes.shape[0]/2.0: indexes = indexes[keep, :] values = sfs[keep, 0] mindex = indexes.min(0) indexes -= mindex maxdex = indexes.max(0)+1 origin = tuple(array(origin) + edge*mindex) sfd = zeros(maxdex) sfd[indexes[:,0], indexes[:,1], indexes[:,2]]=values else: sfd = zeros((rang[0], rang[1], rang[2])) sfd[indexes[:,0], indexes[:,1], indexes[:,2]]=sfs[:,0] name = am.name()+"_sf" sf = doc.getElements('SpatialField', name) if sf: sf[0].sever() sf = createElement('SpatialField', {"Name":name}) ds = createElement('Data', {"Name":"sfd"}) sfd = sfd/sfd.max() sfd = sfd*am.attributes.get('total_weight', .07)/.07 ds.datinit(sfd, {"SampleType":"locus"}) sf.newElement(ds) for an in am.attributes: if an.startswith("meta_"): sf.setAttrib(an, am.attrib(an)) sf.setAttrib("Origin", origin) sf.setAttrib("mindensity", minvalue) sf.setAttrib("maxdensity", maxvalue) ang = sf.attrib('meta_directional_tuning') % 360 ang = ang *pi/180 c = ccbcv.dircolors._getAngleColor(ang) pycol=ccbcv.dircolors.convertColor(c, 'py') sf.setAttrib('color', pycol) sf.setAttrib("Edge", edge) doc.newElement(sf)
def qread(f, **kwargs): d=f.readlines() attr=nmpml.nameFromFile(f) abst=nmpml.createElement('AbstractModel', attr) for l in d: l = l.strip() if not l or l.startswith("#"): continue i = l.index("(") fn = l[:i] argstring = l[i+1:-1] args = eval("_kwargstodict(%s)" % argstring) mb=nmpml.createElement('MienBlock', {'Function':fn}) abst.newElement(mb) mb.setArguments(args) return nmpml.wrapInDocument([abst])
def subGroup(gui, els, attr=None): grp = els[0] if not attr: d = gui.askParam([{"Name": "Attribute", "Type": str}]) if not d: return attr = d[0] avs = {} for e in grp.elements: if e.__tag__ == "Group": subGroup(gui, [e], attr) else: av = e.attrib(attr) if av != None: if not av in avs: avs[av] = [] avs[av].append(e) if len(avs) < 2: return for av in avs: if len(avs[av]) > 1: gn = "%s_%s" % (attr, av) m = {"Name": gn, attr: av} ngrp = createElement("Group", m) grp.newElement(ngrp) for e in avs[av]: e.move(ngrp) consolidateTags(gui, [ngrp]) gui.update_all(object=grp, event="rebuild")
def getBestEgg(doc, elem="/Fiducial:std_av_transverse", newname="reg_transverse"): el = doc.getInstance(elem) arc = N.pi / 180 pts = el.getPoints()[:, :3] pts, axis, angle, centroid = _tobestplane(pts) size = _eucd(pts[:, :2], N.array([0, 0])) mai = size.argmax() ang = N.arctan2(pts[mai, 1], pts[mai, 0]) # maa, mia, xoff, yoff, ang, stretch, mid, slope guess = (360, 240, 20, 127, N.pi / 2, 1.6, 60.0, 0.006) # guess = (size[mai], size.min(), 0, 0, N.arctan2(pts[mai,1], pts[mai,0]), 1.6, 60.0, 0.006) print guess gpts = _angleScan(_eggyweg, guess, arc) try: egg = _eggFit(pts[:, :2], guess) print egg bfe = _angleScan(_eggyweg, egg, arc) except: print "No Solution" bfe = N.zeros((1, 2)) for i, out in enumerate([pts, gpts, bfe]): p = N.column_stack([out, N.zeros(out.shape[0])]) ne = nmp.createElement("Fiducial", {"Style": "points", "width": 10, "Name": str(i)}) _setDisplayGroup(doc, [ne], "projections") ne.setPoints(p) els = doc.getElements("Fiducial", str(i), depth=1) if els: els[0].sever() doc.newElement(ne)
def makeDBGroup(lfn): """For each file in the list, make a database group containing the metadata for these objects. Also, remove these metadata from the objects contained within the group. Attempt's to guess a database ID ane assign it as the name of the group as well""" PATH = "/CercalSystem/" for fn in lfn: doc = io.read(fn) m = db_getmetas(doc, False) if "meta_dbid" in m: name = m["meta_dbid"] del (m["meta_dbid"]) else: try: name = "%s_%s_%s" % (m["meta_length"][0].upper(), str(m["meta_class"]), str(m["meta_slide_number"])) except: name = "DBGroup" print "setting group %s in %s" % (name, fn) m["Name"] = name m["DBrecord"] = PATH m["DBurl"] = URL group = createElement("Group", m) els = doc.elements[:] doc.newElement(group) for e in els: e.move(group) del (m["Name"]) for e in group.getElements(): for k in m: if k in e.attributes: del (e.attributes[k]) for k in ["Color", "DisplayGroup"]: if k in e.attributes: del (e.attributes[k]) io.write(doc, fn)
def read(f, **kwargs): d=f.read() d=eval(d) attr=nmpml.nameFromFile(f) abst=nmpml.createElement('AbstractModel', attr) disable=[] for i, l in enumerate(d): fn, args, dis = l[:3] if dis: disable.append(l) mb=nmpml.createElement('MienBlock', {'Function':fn}) abst.newElement(mb) mb.setArguments(args) if disable: abst.setAttrib('disable', disable) return nmpml.wrapInDocument([abst])
def make_object(self): cobj = self.current_object if len(cobj["sections"])==0: #print "Object %s contains no points. Ignoring it" % str(cobj["attribs"]) pass else: attribs = cobj["attribs"] if len(cobj['sections'])==1 and not attribs.get('Axon'): if attribs.has_key("Resolution"): attribs["Style"] = "line" else: attribs["Style"] = "points" obj = createElement("Fiducial", attribs) obj.setPoints(cobj["sections"][0][1]) obj.addComment("Imported from Neurolucida file %s" % self.filename) #print "Loaded Fiducial object %s" % attribs["Name"] else: if len(cobj['sections'])==1 and len(cobj["sections"][0][1]) <3: print("WARNING: Found an 'Axon' with < 3 points. Assuming this is a mistake and discarding it.") return n = attribs.get("Name", "Cell") attribs["Name"]=n if n in self.cellnames: i=2 tn = "%s_%i" % (n, i) while tn in self.cellnames: i+=1 tn = "%s_%i" % (n, i) attribs["Name"]=tn self.cellnames.append(n) obj = createElement("Cell", attribs) obj.addComment("Imported from Neurolucida file %s" % self.filename) for s in range(len(cobj["sections"])): n= "section[%i]" % s sec = cobj["sections"][s] if sec[0]=='None': p="None" else: p = "section[%i]" % sec[0] s = createElement("Section", {"Name":n, "Parent":p}) s.setPoints(sec[1]) obj.newElement(s) #print "Loaded Cell object %s" % attribs["Name"] self.current_object = None self.object_list.append(obj)
def gmmToSpatialField(doc, upathAbstract='/AbstractModel:MixtureModel/', npts = 30, minvalue=.01, maxvalue=.1): am = doc.getInstance(upathAbstract) bb = _getGMMBB(am, minvalue) origin = bb[0][:3] rang = bb[1][:3] - origin edge = rang.max()/npts pts = [arange(origin[i], bb[1][i], edge) for i in range(3)] pts = combinations(pts) ds = createElement("Data", {"Name":'points', "SampleType":"generic"}) ds.datinit(pts) am.run(ds) sfs = ds.getSubData('gmmout').getData() pts = ((pts-origin)/edge).round().astype(int32) md = sfs[:,0].max() keep = nonzero(sfs[:,0]>=minvalue*md)[0] if keep.shape[0]<pts.shape[0]/2.0: pts = pts[keep, :] sfs = sfs[keep, :] mindex = pts.min(0) pts -= mindex origin = tuple(array(origin) + edge*mindex) maxdex = pts.max(0)+1 fshape = list(maxdex) + [sfs.shape[1]] sfd = zeros(fshape ) sfd[ pts[:,0], pts[:,1], pts[:,2], : ] = sfs sfd[...,0] = maxvalue*sfd[...,0]/md if sfd.shape[3] == 1: sfd = sfd[...,0] name = am.name()+"_sf" sf = doc.getElements('SpatialField', name) if sf: sf[0].sever() sf = createElement('SpatialField', {"Name":name}) ds = createElement('Data', {"Name":"sfd"}) sfd = sfd*am.attributes.get('total_weight', 1.0) ds.datinit(sfd, {"SampleType":"sfield"}) sf.newElement(ds) sf.setAttrib("Origin", origin) sf.setAttrib("colormap", 'hot') sf.setAttrib("colordimension", 1) sf.setAttrib("colorrange", (minvalue, maxvalue)) sf.setAttrib("mindensity", minvalue) sf.setAttrib("Edge", edge) doc.newElement(sf)
def planeRegressAndProject(doc, elems=[], newname="planarProjection"): """collect all the points defined by elems and calculate a planer regression. Then construct a new point fiducial named newname containing the projection of all the points onto the regression plane. """ a = N.row_stack([doc.getInstance(e).getPoints()[:, :3] for e in elems]) plane = _pregress(a) a = _pproject(a, plane) ne = nmp.createElement("Fiducial", {"Style": "points", "Name": newname}) ne.setPoints(a) _setDisplayGroup(doc, [ne], "projections") doc.newElement(ne)
def addTraces(dat, traces, hasDrug): doc = dat.xpath(True)[0] for i,tr in enumerate(traces): attrs = { 'Name':'Trace%i' % i, 'StartTime':0.0, 'SamplesPerSecond':1e6, # batlab records spike times in microseconds 'SampleType':'labeledevents', 'nsweeps':tr['nsweeps'], 'raw_data_offset':tr['raw_data_offset'], 'raw_data_shape':[tr['nsamples'], tr['nsweeps']], 'raw_data_samplerate':tr['samplerate_resp'], 'Drug':hasDrug, 'stimuli_active':map(bool, tr['stimulus']) } trdat = nmp.createElement('Data', attrs) dat.newElement(trdat) if tr['spikes']: spdat = [] for sweep in range(len(tr['spikes'])): for spike in range(len(tr['spikes'][sweep])): spdat.append([tr['spikes'][sweep][spike], sweep]) if not spdat: spdat = np.zeros((0,2)) trdat.datinit(np.array(spdat)) sfiles = [] for ch, s in enumerate(tr['stimulus']): for k in s: trdat.setAttrib('stim%i_%s' % (ch, k), s[k]) if k == 'file': ename = "/Data:Call%s" % (s[k][:s[k].index('.')],) sfiles.append((ename, s[k])) er = nmp.createElement("ElementReference", {'Name':'Stim%i' % ch, 'Data':ch, 'Target':ename}) trdat.newElement(er) for (upath, fname) in sfiles: if not doc.getInstance(upath, True): stimdat = nmp.forceGetPath(doc, upath) stimdat.setAttrib('SampleType','timeseries') stimdat.setAttrib('SamplesPerSecond',tr['samplerate_stim']) fname = os.path.join(callsdir, fname) sd = np.fromstring(open(fname).read(), np.int16) stimdat.datinit(sd)
def makeGroup(gui, els): m = {"Name": "GroupFromSelection"} ngrp = createElement("Group", m) gui.document.newElement(ngrp) for e in els: m = e.getInheritedAttributes() for k in m: e.setAttrib(k, m[k]) e.move(ngrp) consolidateTags(gui, [ngrp]) gui.update_all(object=gui.document, event="rebuild")
def labelAllLines(doc): """create a labeled point object containing the first point of each Cell and Fiducial in the document (other than the standard), labeled with the name of the object.""" els = _getElems(doc) ne = nmp.createElement("Fiducial", {"Style": "points", "Name": "objectlabels"}) labs = [(i, els[i].name()) for i in range(len(els))] pts = N.ones((len(els), 4)) for i in range(len(els)): pts[i, :3] = els[i].getPoints()[0, :3] ne.point_labels = dict(labs) ne.setPoints(pts) doc.newElement(ne)
def mixtureModel(ds, select=(None, None, None), minCenters=3, maxCenters=10, model="Gaussian_pk_Lk_Ck", name="MixtureModel", runs=5): centers = range(minCenters, maxCenters+1) dat = getSelection(ds, select) mod = _mmcall(dat, centers, model, False, runs) doc = ds.getInstance('/') try: mm = doc.getInstance("/AbstractModel:%s" % name) mmb = mm.getInstance("MienBlock:gmm") par = mmb.getElements("Parameters", depth=1)[0] except: mm = createElement("AbstractModel", {"Name":name}) mmb = createElement('MienBlock', {"Name":"gmm",'Function':'ccbcv.gmm.gmm'}) par= createElement("Parameters", {}) mmb.newElement(par) mm.newElement(mmb) doc.newElement(mm) mm.setAttrib('total_weight', 1.0) w, m, c = _mod2par(mod) args = {'weights':w, 'means':m, 'covs':c} par.setValue(args, override=True)
def densityEstimatesForClasses(doc, minCenters=12, maxCenters=20, model="Gaussian_pk_Lk_Ck", useDiameters=0): els = doc.getElements('Fiducial', {"Style":'spheres'}) if not els: return centers = range(minCenters, maxCenters+1) classes = {} metas = ['meta_class', 'meta_cercus', 'meta_length', 'meta_directional_tuning', 'meta_instar'] for e in els: if not any([m in e.getInheritedAttributes() for m in metas]): print('metadata not defined for %s. Skipping' % e.upath()) continue e.setAttrib('meta_directional_tuning', e.attrib('meta_directional_tuning', True) % 360, True) sig = tuple([(m, e.attrib(m, True)) for m in metas]) if not sig in classes: classes[sig]=[] classes[sig].append(e.getPoints()) for cl in classes: dat = row_stack(classes[cl]) print cl, len(classes[cl]), dat.shape[0] if not useDiameters: dat=dat[:,:3] mod = _mmcall(dat, centers, model, useDiameters) name = "density%i_%i_%s%s%sgmm" % (cl[0][1], int(cl[4][1]), cl[2][1][0].upper(), cl[1][1][0].upper(), cl[3][1]) try: mm = doc.getInstance("/AbstractModel:%s" % name) mmb = mm.getInstance("MienBlock:gmm") par = mmb.getElements("Parameters", depth=1)[0] except: mm = createElement("AbstractModel", {"Name":name}) for pair in cl: mm.setAttrib(pair[0], pair[1]) mmb = createElement('MienBlock', {"Name":"gmm",'Function':'ccbcv.gmm.gmm'}) par= createElement("Parameters", {}) mmb.newElement(par) mm.newElement(mmb) doc.newElement(mm) total_weight = CLASS_DENSITIES[cl[0][1]] mm.setAttrib('total_weight', total_weight) w, m, c = _mod2par(mod) args = {'weights':w, 'means':m, 'covs':c} par.setValue(args, override=True)
def _setDisplayGroup(doc, els, gn): grp = doc.getElements("Group", gn) if grp: grp = grp[0] else: col = self.graph.getNewColor() grp = nmp.createElement("Group", {"Name": gn, "color": (200, 200, 90)}) doc.newElement(grp) for o in els: if "DisplayGroup" in o.attributes: del (o.attributes["DisplayGroup"]) if "color" in o.attributes: del (o.attributes["color"]) o.move(grp)
def drawEgg( doc, newname="elipse", arc=1.0, maa=100, mia=50, xoff=0.0, yoff=0.0, ang=45, stretch=2.0, mid=0.0, slope=0.01 ): ang = ang * N.pi / 180 arc = arc * N.pi / 180 pts = _angleScan(_eggyweg, (maa, mia, xoff, yoff, ang, stretch, mid, slope), arc) pts = N.column_stack([pts, N.zeros(pts.shape[0])]) ne = nmp.createElement("Fiducial", {"Style": "points", "width": 5, "Name": newname}) _setDisplayGroup(doc, [ne], "projections") ne.setPoints(pts) els = doc.getElements("Fiducial", newname, depth=1) if els: els[0].sever() doc.newElement(ne)
def compressMap(fname): '''Opens a document fname which should contain appropriately meta-tagged afferent reconstructions of cercal system afferents. Groups the included varicosities (sphere fiducials) in such a way that each length/cercus/class group is represented by a single fiducial. Discards slide number metadata, and any elements that are not sphere or line fiducials. ''' bfn, ext = os.path.splitext(fname) nfn = bfn+"_compressed.nmpml" doc = io.read(fname) d2 = nmpml.blankDocument() lfids = doc.getElements('Fiducial', {'Style':'line'}) i = 1 for f in lfids: nf = nmpml.createElement('Fiducial', {'Style':'line', 'color':[255,255,255], 'meta_slide_number': 123, 'Name':'standard_outline_123_at1p344scale_line%i' % i}) i+=1 nf.setPoints(f.getPoints()) d2.newElement(nf) for side in ['left', 'right']: for length in ['long', 'medium', 'short']: for clas in range(1,14): fids = doc.getElements('Fiducial', {'Style':'spheres', 'meta_class':clas, 'meta_length':length, 'meta_cercus':side}) if fids: nf = nmpml.createElement('Fiducial', {'Style':'spheres', 'color':fids[0].attrib('color'), 'meta_class': clas, 'meta_length':length, 'meta_cercus':side, 'Name':'class%i%s%s' % (clas, length, side)}) for f in fids: nf.setPoints(f.getPoints(), True) d2.newElement(nf) io.write(d2, nfn)
def toPointCloud(doc, image, thresh=128, color=0, name="imagepoints"): '''Convert the image stack to a collection of points in a point fiducial. Points with value greater than threshold are reported. If the image stack is in color, only one color channel is used for the thresholding''' dat, h = getImageDataAndHeader(doc, image) mv = dat.max() head = {"Name":name, "Style":"points"} edge = array((h.get("PixelWidth", 1.0), h.get("PixelHeight", 1.0), h.get("StackSpacing", 1.0))); dat = dat[:,arange(dat.shape[1]-1,-1,-1),color,:] hits = nonzero(dat>thresh) vals = dat[hits] coords = column_stack(hits).astype(float32) coords*=edge pts = column_stack([coords, vals]) head['Name']=name doc.report("%i pixels visible" % (pts.shape[0],)) import mien.parsers.nmpml as nmp o = nmp.createElement("Fiducial", head) doc.newElement(o) d=o.setPoints(pts) doc.report("Wrote element at %s" % (o.upath(),))
def regularizeContour(doc, elem="/Fiducial:transverse", newname="regularized", arc=1.0, mode="max"): """ SWITCHVALUES(mode)=["max", "min", "mean", "skip"] """ el = doc.getInstance(elem) pts = el.getPoints()[:, :3] arc = N.pi * arc / 180.0 pts, axis, angle, centroid = _tobestplane(pts) pts = _regularize(pts, arc, mode) pts = _back23D(pts, axis, angle, centroid) ne = nmp.createElement("Fiducial", {"Style": "points", "width": 5, "Name": newname}) _setDisplayGroup(doc, [ne], "projections") ne.setPoints(pts) els = doc.getElements("Fiducial", newname, depth=1) if els: els[0].sever() doc.newElement(ne)
def doTestGMM(gui, els): am = els[0] import portfors.density as den bb =den._getGMMBB(am) a = gui.askParam([{'Name':'Data Dimension', 'Value':len(bb[0])}]) if not a: return pts = numpy.column_stack([numpy.linspace(bb[0][i], bb[1][i], 10) for i in range(a[0])]) ds = createElement("Data", {"Name":'points', "SampleType":"generic"}) ds.datinit(pts) am.run(ds) sfs = ds.getSubData('gmmout').getData() print "run successful" for i in range(10): if len(sfs.shape) == 1 or sfs.shape[1] == 1: print "%s -> %.2g" % (','.join(map(str, pts[i])), sfs[i]) else: print "%s -> %s" % (','.join(map(str, pts[i])), ','.join(map(str, sfs[i])))
def getBestElipse(doc, elem="/Fiducial:std_av_sagital", newname="reg_coronal"): el = doc.getInstance(elem) arc = N.pi / 180 pts = el.getPoints()[:, :3] rpts, axis, angle, centroid = _tobestplane(pts) pts = rpts size = _eucd(pts[:, :2], N.array([0, 0])) mai = size.argmax() ang = N.arctan2(pts[mai, 1], pts[mai, 0]) guess = (size[mai], size.min(), 0, 0, ang) egg = _elipseFit(pts[:, :2], guess) bfe = _angleScan(_elipse, egg, arc) bfe = _back23D(bfe, axis, angle, centroid) ne = nmp.createElement("Fiducial", {"Style": "points", "width": 10, "Name": newname}) _setDisplayGroup(doc, [ne], "projections") ne.setPoints(bfe) els = doc.getElements("Fiducial", newname, depth=1) if els: els[0].sever() doc.newElement(ne)
def getTests(dsheet=None): cells = getDsheet(dsheet) files = {} for c in cells: etrack = c[ mouseNoLab] mouse = numOrRan(etrack)[0] dfpath = os.path.join(basedir, pathprefix+str(mouse), pathprefix +etrack) pstpath = os.path.join(dfpath, pathprefix + etrack + '.pst') if os.path.isfile(pstpath): print "found data for cell %s" % c[cellIdLab] if not pstpath in files: files[pstpath] = [] files[pstpath].append(c) else: print "No data for %s, %s" % (c[cellIdLab], pstpath) doc = nmp.blankDocument() for fn in files: pstmeta, tests = parsePST.parse(fn) pstmeta[1] = 'Date: ' + pstmeta[1] for c in files[fn]: dat = nmp.addPath(doc, "/Data:Cell%s" % c[cellIdLab]) dat.setAttrib('SampleType', "group") for k in c: safek = k.replace('#','Num').replace(' ', '_').replace('+', 'and').replace('?', '') dat.setAttrib(safek, c[k]) dat.setAttrib('ExperimentMetaData', pstmeta) dat.setAttrib('RawTraceFilename', fn[:-4] + '.raw') for i, t in enumerate(c[testLab]): tids = numOrRan(t) for tid in tids: test = tests[tid-1] drug = bool(c[conditionLab][i]=='yes') tdat = nmp.createElement('Data', {'Name':'Test%i' % tid,'SampleType':'group', 'Drug':drug}) for k in test: if not k == 'traces': tdat.setAttrib(k, test[k]) dat.newElement(tdat) addTraces(tdat, test['traces'], drug) return doc
def gmmOverlapNumerical(doc, upathGMM1="/AbstractModel:class1LLdensity", upathGMM2="/AbstractModel:class2LLdensity", edge = 5.0, minvalue=.01): am1 = doc.getInstance(upathGMM1) am2 = doc.getInstance(upathGMM2) bb1 = _getGMMBB(am1, minvalue) bb2 = _getGMMBB(am2, minvalue) bb = (minimum(bb1[0], bb2[0]), maximum(bb1[1], bb2[1])) origin = bb[0] rang = bb[1]-bb[0] rang = ceil(rang/edge).astype(int32) indexes = combinations([arange(rang[0]), arange(rang[1]), arange(rang[2])]) dat = indexes*edge + array(origin) ds = createElement("Data", {"Name":'points', "SampleType":"generic"}) ds.datinit(dat) am1.run(ds) sfs1 = ds.getSubData('gmmout').getData() am2.run(ds) sfs2 = ds.getSubData('gmmout').getData() sfs1 = sfs1 / sfs1.sum() sfs2 = sfs2 / sfs2.sum() sfs = minimum(sfs2 ,sfs1) val = sfs.sum() print "Overlap is %.2g" % val
def toSpatialField(doc, image, mindens=.3, maxdens = 0.8, name="density"): '''Convert an image stack to a spatial field representing the image density in 3D. The result is stored in a Data element of type SpatialField at upath.''' dat, h = getImageDataAndHeader(doc, image) mv = dat.max() head = {"Origin":h.get("SpatialAnchor", (0.0,0,0)), "Edge":(h.get("PixelWidth", 1.0), h.get("PixelHeight", 1.0), h.get("StackSpacing", 1.0)), "mindensity":mindens, "maxdensity":maxdens, "MaxValue":mv, "Vertical":h.get("SpatialVertical", (0.0,1.0,0)), "Depth":h.get("SpatialDepth", (0.0,1.0,0))} dat = dat.astype(float32).sum(2)/mv dat = dat[:,arange(dat.shape[1]-1,-1,-1),:] head['Name']=name doc.report("%i pixels visible" % ((dat>mindens).sum(),)) import mien.parsers.nmpml as nmp o = nmp.createElement("SpatialField", head) doc.newElement(o) d=o.getData() d.datinit(dat) doc.report("Wrote element at %s" % (o.upath(),))
def download(self, event=None, record=None, append=True, refresh=True): if not record: ids = self.list(None) d = self.askParam([{"Name": "Which Record?", "Type": "List", "Value": ids}]) if not d: return record = d[0] df = self.rest.getFile(record) doc = deserialize(df) if not append: self.Parent.newDoc(doc) else: m = self.rest.getInfo(record)["metadata"] m = dict([("meta_" + k, m[k]) for k in m]) m["Name"] = record m["DBrecord"] = self.rest.path m["DBurl"] = self.rest.url m["color"] = (255, 255, 255) group = createElement("Group", m) self.Parent.document.newElement(group) for e in doc.elements[:]: e.move(group) if refresh: self.Parent.update_all(element=self.Parent.document, action="rebuild")
def _makeSubGroup(grp, attr): if attr in grp.attributes: return avs = {} for e in grp.elements: if e.__tag__ == "Group": _makeSubGroup(e, attr) else: av = e.attrib(attr) if av != None: if not av in avs: avs[av] = [] avs[av].append(e) if len(avs) < 2: return for av in avs: if len(avs[av]) > 1: gn = "%s_%s" % (attr, av) m = {"Name": gn, attr: av} ngrp = createElement("Group", m) grp.newElement(ngrp) for e in avs[av]: e.move(ngrp) _consolidateTags(ngrp)
def createElement(self, tag, attrs, cdata=None): o=createElement(tag, attrs, cdata) o._guiinfo={} return o