def setup_Q(self): self.Q = scipy.zeros((self.nperiods, self.ndists, self.ndists)) #D = self.D * self.Dmask for p in range(self.nperiods): for i, d1 in enumerate(self.dists): s1 = sum(d1) if s1 > 0: for j, d2 in enumerate(self.dists): s2 = sum(d2) xor = scipy.logical_xor(d1, d2) # only consider transitions between dists that are # 1 step (dispersal or extinction) apart if sum(xor) == 1: dest = scipy.nonzero(xor)[0] #prior = self.dist_priors[i] if s1 < s2: # dispersal rate = 0.0 for src in scipy.nonzero(d1)[0]: rate += self.D[p,src,dest] * \ self.Dmask[p,src,dest] # for each area in d1, add rate of # dispersal to dest else: # extinction rate = self.E[p,dest] #self.Q[i][j] = (prior * rate) self.Q[p,i,j] = rate self.set_Qdiag(p)
def alg(ring): # fill holes in binary objects and then see if anything got filled xor = scipy.logical_xor(ring, ndimage.binary_fill_holes(ring)) if xor.any(): return True else: return False
def alg2(ring): # fill holes in binary objects filled = ndimage.binary_fill_holes(ring) # substract original image with object \w holes from the filled-holes image to obtain holes only holes = scipy.logical_xor(ring, filled) # label all objects in the image to obtain the number of holes _, hole_count = ndimage.measurements.label(holes) return hole_count
def iter_dist_splits(dist): if sum(dist) == 1: yield (dist, dist) else: for i in scipy.nonzero(dist)[0]: x = scipy.zeros((len(dist),), dtype="i") x[i] = 1 x = tuple(x) yield (x, dist) yield (dist, x) y = tuple(scipy.array(scipy.logical_xor(dist, x), dtype="i")) yield (x, y) if sum(y) > 1: yield (y, x)
def iter_dist_splits(dist): if sum(dist) == 1: yield (dist, dist) else: for i in scipy.nonzero(dist)[0]: x = scipy.zeros((len(dist), ), dtype="i") x[i] = 1 x = tuple(x) yield (x, dist) yield (dist, x) y = tuple(scipy.array(scipy.logical_xor(dist, x), dtype="i")) yield (x, y) if sum(y) > 1: yield (y, x)
def extract_holes(ring): """ Check if an object in an image forms a ring with one single hole. @note if the number of holes equals 1, the supplied objects is either a ring (in 2D) or another shape with a single hole somewhere (low probability). If the number of holes equals 0, the supplied object is of a shape containing no holes (e.g. an not completely closed ring). If the number is higher than 1, the object contains multiple holes. @param ring a 2D array of type bool_ containing a single object @type ring ndarray @return an 2D array containing all holes labeled consecutively from 1 as first and the number of found holes as second arguments @rtype (ndarray, int) """ # fill the holes in the binary array (using ndim*2 connectedness) and xor with original image holes = scipy.logical_xor(ring, ndimage.binary_fill_holes(ring)) # find all objects (= holes in the original object) in the image return ndimage.measurements.label(holes)
#center=(100,100); radius1=200; radius2=205; siz=np.round(2*radius2*1.40); img1=np.zeros((siz,siz)); img2=np.zeros((siz,siz)); center=np.round([siz/2,siz/2]); rr1, cc1 = circle(center[0],center[1], radius1) img1[rr1, cc1] = 1 rr2, cc2 = circle(center[0],center[1], radius2) img2[rr2, cc2] = 1 circ=sc.logical_xor(img2,img1) circ.astype(int) plt.imshow(img1) plt.show() io.imshow(img2) io.imshow(circ) io.imsave('Circle_radius'+str(radius1)+'.png',circ) # #angles on the circle # indices=np.where(xCenterLine==1) ## randperm=np.random.permutation(len(indices[0])) # randperm=range(len(indices[0])) # # #consider 2% random points on the circle
#center=(100,100); radius1 = 200 radius2 = 205 siz = np.round(2 * radius2 * 1.40) img1 = np.zeros((siz, siz)) img2 = np.zeros((siz, siz)) center = np.round([siz / 2, siz / 2]) rr1, cc1 = circle(center[0], center[1], radius1) img1[rr1, cc1] = 1 rr2, cc2 = circle(center[0], center[1], radius2) img2[rr2, cc2] = 1 circ = sc.logical_xor(img2, img1) circ.astype(int) plt.imshow(img1) plt.show() io.imshow(img2) io.imshow(circ) io.imsave('Circle_radius' + str(radius1) + '.png', circ) # #angles on the circle # indices=np.where(xCenterLine==1) ## randperm=np.random.permutation(len(indices[0])) # randperm=range(len(indices[0])) # # #consider 2% random points on the circle # numPts=np.ceil(1.00*len(randperm))
def doTestEdtWithHalo(self, haloSz=0): if (isinstance(haloSz, int) or ((sys.version_info.major < 3) and isinstance(haloSz, long))): if (haloSz < 0): haloSz = 0 haloSz = sp.array((haloSz,)*3) subDirName = "doTestEdtWithHalo_%s" % ("x".join(map(str, haloSz)), ) outDir = self.createTmpDir(subDirName) #outDir = subDirName if (mpi.world != None): if ((mpi.world.Get_rank() == 0) and (not os.path.exists(outDir))): os.makedirs(subDirName) mpi.world.barrier() segDds = mango.zeros(self.imgShape, mtype="segmented", halo=haloSz) segDds.setAllToValue(segDds.mtype.maskValue()) mango.data.fill_ellipsoid(segDds, centre=self.centre, radius=(self.radius*1.05,)*3, fill=0) mango.data.fill_ellipsoid(segDds, centre=self.centre, radius=(self.radius,)*3, fill=1) mango.io.writeDds(os.path.join(outDir,"segmentedSphere.nc"), segDds) dtDds = mango.image.distance_transform_edt(segDds, 1) mango.io.writeDds(os.path.join(outDir,"distance_mapSphereEdt.nc"), dtDds) segDds.updateHaloRegions() dtDds.updateHaloRegions() segDds.setBorderToValue(segDds.mtype.maskValue()) dtDds.setBorderToValue(dtDds.mtype.maskValue()) slc = [] for d in range(len(haloSz)): slc.append(slice(haloSz[d], segDds.asarray().shape[d]-haloSz[d])) slc = tuple(slc) arr = dtDds.subd_h.asarray() sbeg = dtDds.subd_h.origin send = sbeg + dtDds.subd_h.shape coords = np.ogrid[sbeg[0]:send[0],sbeg[1]:send[1],sbeg[2]:send[2]] distDds = mango.copy(dtDds) distArr = distDds.subd_h.asarray() distArr[...] = \ sp.where( sp.logical_and(dtDds.subd_h.asarray() != dtDds.mtype.maskValue(), dtDds.subd_h.asarray() >= 0), self.radius - sp.sqrt( ((coords[0]-self.centre[0])**2) + ((coords[1]-self.centre[1])**2) + ((coords[2]-self.centre[2])**2) ), dtDds.subd_h.asarray() ) mango.io.writeDds(os.path.join(outDir,"distance_mapSphereDistRef.nc"), distDds) rootLogger.info("max diff = %s" % (np.max(sp.absolute(distArr - dtDds.subd_h.asarray())),)) self.assertTrue( sp.all( sp.absolute(distArr - dtDds.subd_h.asarray()) <= 1.0 ) ) self.assertTrue( sp.all( sp.logical_not(sp.logical_xor( segDds.asarray() == 1, dtDds.asarray() >= 0 )) ) ) self.assertTrue( sp.all( sp.logical_not(sp.logical_xor( segDds.asarray() == segDds.mtype.maskValue(), dtDds.asarray() == dtDds.mtype.maskValue() )) ) ) self.assertTrue( sp.all( sp.logical_not(sp.logical_xor( segDds.asarray() == 0, dtDds.asarray() == -1 )) ) ) self.assertTrue(sp.all(segDds.halo == dtDds.halo)) self.assertTrue(sp.all(segDds.shape == dtDds.shape)) self.assertTrue(sp.all(segDds.origin == dtDds.origin), "%s != %s" % (segDds.origin, dtDds.origin)) self.assertTrue(sp.all(segDds.mpi.shape == dtDds.mpi.shape))