def test_hash_reversability_3d_valid(self): assert pm.deinterleave3(pm.interleave(100, 30, 50)) == (100, 30, 50) if getattr(sys, 'maxint', 0) and sys.maxint <= 2 ** 31 - 1: max_v = 0xff else: max_v = 0xffff for i in range(100): p1 = (random.randint(0, max_v), random.randint(0, max_v), random.randint(0, max_v)) print(p1, pm.deinterleave3(pm.interleave(*p1))) assert p1 == pm.deinterleave3(pm.interleave(*p1))
def from_zcurve(zpercentile, dim): SCALE = morton_scale(dim) zmorton = int(morton_large(dim) * zpercentile + 0.5) if dim == 2: values = pymorton.deinterleave2(zmorton) elif dim == 3: values = pymorton.deinterleave3(zmorton) prtcls = [v / SCALE for v in values] return prtcls
def to_zcurve(prctls): """ A mapping from R^n -> R based on the Morton z-curve """ dim = len(prctls) SCALE = morton_scale(dim) int_prctls = [int(math.floor(p * SCALE)) for p in prctls] m1 = pymorton.interleave(*int_prctls) int_prctls_back = pymorton.deinterleave2( m1) if dim == 2 else pymorton.deinterleave3(m1) assert all(i1 == i2 for i1, i2 in zip(int_prctls, int_prctls_back)) m2 = pymorton.interleave(*[SCALE - 1 for _ in range(dim)]) return m1 / m2
def from_zcurve(self, zvalue, dim): zpercentile = StatsConventions.normcdf(zvalue) SCALE = self.morton_scale(dim) zmorton = int( self.morton_large(dim)*zpercentile+0.5 ) if dim==2: values = pymorton.deinterleave2(zmorton) elif dim==3: values = pymorton.deinterleave3(zmorton) else: raise NotImplementedError('Only 2d or 3d') prtcls = [ v/SCALE for v in values ] return prtcls
def to_cube(self, zpercentile: float, dim): """ Space filling curve I -> I^n """ SCALE = self.morton_scale(dim) zmorton = int(self.morton_large(dim) * zpercentile + 0.5) if dim == 2: values = pymorton.deinterleave2(zmorton) elif dim == 3: values = pymorton.deinterleave3(zmorton) else: raise NotImplementedError('Only 2d or 3d') prtcls = [v / SCALE for v in values] return prtcls
def test_pymorton(self): # Test standard hashing assert (pm.interleave(100, 50) == pm.interleave2(100, 50)) assert (pm.interleave(10, 50, 40) == pm.interleave3(10, 50, 40)) assert ((100, 30) == pm.deinterleave2(pm.interleave(100, 30))) assert (pm.deinterleave2(pm.interleave(100, 30)) == (100, 30)) assert (pm.deinterleave3(pm.interleave(100, 30, 50)) == (100, 30, 50)) # Test geo-hashing lat, lng = 40.712014, -74.008164 latlng_morton = pm.interleave_latlng(lat, lng) assert('03023211232311330231120312032231' == \ latlng_morton) assert(pm.deinterleave_latlng(latlng_morton) == \ (40.712013971060514, -74.00816400535405))
def test_pymorton(self): # Test standard hashing assert(pm.interleave(100, 50) == pm.interleave2(100, 50)) assert(pm.interleave(10, 50, 40) == pm.interleave3(10, 50, 40)) assert((100, 30) == pm.deinterleave2(pm.interleave(100, 30))) assert(pm.deinterleave2(pm.interleave(100,30)) == (100, 30)) assert(pm.deinterleave3(pm.interleave(100, 30, 50)) == (100, 30, 50)) # Test geo-hashing lat, lng = 40.712014, -74.008164 latlng_morton = pm.interleave_latlng(lat, lng) assert('03023211232311330231120312032231' == \ latlng_morton) assert(pm.deinterleave_latlng(latlng_morton) == \ (40.712013971060514, -74.00816400535405))
def from_cube(self, prctls: List[float]) -> float: """ Inverse of space filling curve I^n -> I """ dim = len(prctls) SAFE = False # relic SCALE = self.morton_scale(dim) int_prctls = [int(math.floor(p * SCALE)) for p in prctls] m1 = pymorton.interleave(*int_prctls) if SAFE: int_prctls_back = pymorton.deinterleave2( m1) if dim == 2 else pymorton.deinterleave3(m1) assert all(i1 == i2 for i1, i2 in zip(int_prctls, int_prctls_back)) m2 = pymorton.interleave(*[SCALE - 1 for _ in range(dim)]) zpercentile = m1 / m2 return zpercentile
def to_zcurve(self, prctls: List[float] ): """ A mapping from R^n -> R based on the Morton z-curve """ SAFE = False dim = len(prctls) if dim==1: return self.to_zscores(prctls)[0] else: SCALE = self.morton_scale(dim) int_prctls = [ int(math.floor(p*SCALE)) for p in prctls ] m1 = pymorton.interleave(*int_prctls) if SAFE: int_prctls_back = pymorton.deinterleave2(m1) if dim==2 else pymorton.deinterleave3(m1) assert all( i1==i2 for i1,i2 in zip(int_prctls, int_prctls_back)) m2 = pymorton.interleave(*[ SCALE-1 for _ in range(dim) ]) zpercentile = m1/m2 return StatsConventions.norminv(zpercentile)
def test_hash_reversability_3d_valid(self): assert pm.deinterleave3(pm.interleave(100, 30, 50)) == (100, 30, 50)