[docs]deftest_between_sorted_single_time(self):# test single time, falling right on edgest=np.arange(100)bounds=[10,25]ind=num.between_sorted(t,bounds)assertnp.all(t[ind]==np.arange(int(np.ceil(bounds[0])),int(np.floor(bounds[1]+1))))# test single time in between edgesbounds=[10.4,25.2]ind=num.between_sorted(t,bounds)assertnp.all(t[ind]==np.arange(np.ceil(bounds[0]),np.floor(bounds[1])+1))# out of boundsind=num.between_sorted(t,[-5,800])assertnp.all(ind)
[docs]deftest_between_sorted_multiple_times(self):t=np.arange(100)# non overlapping rangesbounds=np.array([[10.4,25.2],[67.2,86.4]])ind_=np.logical_or(num.between_sorted(t,bounds[0]),num.between_sorted(t,bounds[1]))ind=num.between_sorted(t,bounds)assertnp.all(ind==ind_)# overlapping rangesbounds=np.array([[10.4,83.2],[67.2,86.4]])ind_=np.logical_or(num.between_sorted(t,bounds[0]),num.between_sorted(t,bounds[1]))ind=num.between_sorted(t,bounds)assertnp.all(ind==ind_)# one range contains the otherbounds=np.array([[10.4,83.2],[34,78]])ind_=np.logical_or(num.between_sorted(t,bounds[0]),num.between_sorted(t,bounds[1]))ind=num.between_sorted(t,bounds)assertnp.all(ind==ind_)# case when one range starts exactly where another stopsbounds=np.array([[10.4,83.2],[83.2,84]])ind=num.between_sorted(t,bounds)ind_=np.logical_or(num.between_sorted(t,bounds[0]),num.between_sorted(t,bounds[1]))assertnp.all(ind==ind_)
[docs]deftest_between_sorted_out_of_range(self):# np searchsorted was returning out of range index when the start time# was greater than the max or the end time lower than the minbounds=np.array([[-10.4,-3],[10.4,20],[34,78]])array=np.arange(30)assertnp.sum(num.between_sorted(array,bounds))==10
[docs]deftest_ismember2d_uuids(self):nb=20na=500np.random.seed(42)a=np.random.randint(0,nb+3,na)b=np.arange(nb)lia,locb=num.ismember(a,b)bb=np.random.randint(low=np.iinfo(np.int64).min,high=np.iinfo(np.int64).max,size=(nb,2),dtype=np.int64,)aa=np.zeros((na,2),dtype=np.int64)aa[lia,:]=bb[locb,:]lia_,locb_=num.ismember2d(aa,bb)assertnp.all(lia==lia_)&np.all(locb==locb_)bb[:,0]=0aa[:,0]=0# if the first column is equal, the distinction is to be made on the second\assertnp.unique(bb[:,1]).size==nblia_,locb_=num.ismember2d(aa,bb)assertnp.all(lia==lia_)&np.all(locb==locb_)
[docs]deftest_bincount_2d(self):# first test simple with indicesx=np.array([0,1,1,2,2,3,3,3])y=np.array([3,2,2,1,1,0,0,0])r,xscale,yscale=num.bincount2D(x,y,xbin=1,ybin=1)r_=np.zeros_like(r)# sometimes life would have been simpler in c:forix,iyinzip(x,y):r_[iy,ix]+=1self.assertTrue(np.all(np.equal(r_,r)))# test with negative valuesy=np.array([3,2,2,1,1,0,0,0])-5r,xscale,yscale=num.bincount2D(x,y,xbin=1,ybin=1)self.assertTrue(np.all(np.equal(r_,r)))# test unequal binsr,xscale,yscale=num.bincount2D(x/2,y/2,xbin=1,ybin=2)r_=np.zeros_like(r)forix,iyinzip(np.floor(x/2),np.floor((y/2+2.5)/2)):r_[int(iy),int(ix)]+=1self.assertTrue(np.all(r_==r))# test with weightsw=np.ones_like(x)*2r,xscale,yscale=num.bincount2D(x/2,y/2,xbin=1,ybin=2,weights=w)self.assertTrue(np.all(r_*2==r))# test aggregation instead of binningx=np.array([0,1,1,2,2,4,4,4])y=np.array([4,2,2,1,1,0,0,0])r,xscale,yscale=num.bincount2D(x,y)self.assertTrue(np.all(xscale==yscale)andnp.all(xscale==np.array([0,1,2,4])))# test aggregation on a fixed scaler,xscale,yscale=num.bincount2D(x+10,y+10,xbin=np.arange(5)+10,ybin=np.arange(3)+10)self.assertTrue(np.all(xscale==np.arange(5)+10))self.assertTrue(np.all(yscale==np.arange(3)+10))self.assertTrue(np.all(r.shape==(3,5)))