def local(self, ind, r): '''returns a space of the same type as self, in which all the points within radius r of point ind have value 1.0, and all other points are not defined.''' allpts=combinations([arange(0,r+1) for x in range(len(self.dims))]) allpts=allpts[(allpts**2).sum(1)<=r**2] allpts= quadrants(allpts) allpts+=ind dat=(allpts, ones((allpts.shape[0], len(self.rules)))) dat=self.inspace(dat) return self.new(dat,False)
def boundary(self, ind, r): '''like local, but return only the points on the boundary of the local''' allpts=combinations([arange(0,r+1) for x in range(len(self.dims))]) allpts=allpts[(allpts**2).sum(1)<=r**2] allpts=allpts[(allpts**2).sum(1)>(r-1)**2] allpts= quadrants(allpts) allpts+=ind dat=(allpts, ones((allpts.shape[0], len(self.rules)))) dat=self.inspace(dat) return self.new(dat,False)
def quadrants(pts): '''renders the locus pts symmetric around the origin (pts should contain strictly non-negative values)''' z=[pts] quad=combinations([arange(-1,2,2) for x in range(pts.shape[1])]) for t in quad: if all(t>0): continue z.append(pts*t) z=row_stack(z) return uniqueND(z)
def local(self, a=None): ls = self.space() xmin = max(self.location[0]-self.localsize, 0) xmax = min(self.location[0]+self.localsize+1, ls.shape[0]) ymin = max(self.location[1]-self.localsize, 0) ymax = min(self.location[1]+self.localsize+1, ls.shape[1]) lpts = combinations([arange(xmin, xmax), arange(ymin, ymax)]) ls[lpts[:,0], lpts[:,1]]=1.0 if a!=None: ls = ls*a return ls
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 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 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 allPoints(self): '''Return an array of indexes that could be used as self.ind if every point in the space was specified''' l=[] for d in self.dims: l.append(arange(d)) return combinations(l)