def int_to_Hilbert_test(n, nD, doPrint=True): tups_per_line = 2**(int(log(80 / tuple_print_width(nD), 2))) linear_size = int((n + .5)**(1. / nD)) isCube = linear_size**nD == n if not isCube: print "(This is not a complete cube:)" visited = {} for i in range(n): pt = tuple(int_to_Hilbert(i, nD)) if doPrint: print pt, visited[pt] = True # Add pt to "visited" dictionary. j = Hilbert_to_int(pt) if j != i: print "BZZT, decodes to", j, if i > 0: # Check that we have stepped one unit, along one dim: nDiffs = sum([pt[k] != prev_pt[k] for k in range(nD)]) if nDiffs != 1: print "BZZT, different along", nDiffs, "dimensions", maxDiff = max([abs(pt[k] - prev_pt[k]) for k in range(nD)]) if maxDiff != 1: print "BZZT, max difference is", maxDiff, prev_pt = pt if (i + 1) % tups_per_line == 0: if doPrint: print if isCube: check_visited(visited, [], linear_size, nD)
def int_to_Hilbert_test( n, nD, doPrint=True ): tups_per_line = 2**( int( log( 80 / tuple_print_width( nD ), 2 ) ) ) linear_size = int( ( n + .5 ) ** (1./nD) ) isCube = linear_size ** nD == n if not isCube: print "(This is not a complete cube:)" visited = {} for i in range( n ): pt = tuple( int_to_Hilbert( i, nD ) ) if doPrint: print pt, visited[ pt ] = True # Add pt to "visited" dictionary. j = Hilbert_to_int( pt ) if j != i: print "BZZT, decodes to", j, if i > 0: # Check that we have stepped one unit, along one dim: nDiffs = sum( [ pt[k] != prev_pt[k] for k in range( nD ) ] ) if nDiffs != 1: print "BZZT, different along", nDiffs, "dimensions", maxDiff = max( [ abs(pt[k] - prev_pt[k]) for k in range( nD ) ] ) if maxDiff != 1: print "BZZT, max difference is", maxDiff, prev_pt = pt if ( i + 1 ) % tups_per_line == 0: if doPrint: print if isCube: check_visited( visited, [], linear_size, nD )
def get(self, h): h = long(h) woeid, ts = hilbert.int_to_Hilbert(h, 2) self.api_ok(self.generate_response(woeid, ts, h)) return
def get(self, h): h = long(h) addr_int, ts = hilbert.int_to_Hilbert(h, 2) addr = "%d.%d.%d.%d" % ((addr_int >> 24)&0xFF, (addr_int >> 16)&0xFF, (addr_int >> 8)&0xFF, addr_int&0xFF) self.api_ok(self.generate_response(addr, ts, h)) return
def get(self, h): h = long(h) x, y, z = hilbert.int_to_Hilbert(h, 3) x = x / self.factor y = y / self.factor x -= 180. y -= 90. self.api_ok(self.generate_response(x, y, z, h)) return
# If needed... def generateface2picsmapping(minimum_faces_per_person=1): lfw_people = fetch_lfw_people(min_faces_per_person=minimum_faces_per_person, resize=0.4) n_samples, h, w = lfw_people.images.shape X, y, target_names = lfw_people.data, lfw_people.target, lfw_people.target_names n_examples, n_features = X.shape face2pics = [] print(max(y)) for i in range((max(y)+1)): face2pics.append([target_names[i],[] ]) for i in range(len(y)): face2pics[y[i]][1].append(i) return face2pics if __name__ == "__main__": (X, X_nmf, y, nmf_components, min_faces_per_person, face2pics) = pickle.load(open( "nmfdata.p", "rb" ) ) n_examples, n_features = X_nmf.shape print(X_nmf[0]) i = Hilbert_to_int( float2IntVector(X_nmf[0] )) print(i) v2 = int2FloatVector(int_to_Hilbert( i, nD=n_features )) print(v2) i2 = Hilbert_to_int( float2IntVector(v2 )) print(i2) print('loss') print(i2-i)
# child_start_end_tests() int_to_Hilbert_test( 8, 1 ) print int_to_Hilbert_test( 64, 2 ) print int_to_Hilbert_test( 64, 3 ) print int_to_Hilbert_test( 4096, 3, doPrint=False ) # nChunks > nD print int_to_Hilbert_test( 4096, 4, doPrint=False ) print k = 10**12 # definitely a long for nD in [ 2, 3, 4, 5 ]: # Decode point ( k, 0, 0... ) to an int...how many digits?: pt = [ 0 ] * nD pt[0] = k x = Hilbert_to_int( pt ) print x, log( x, 10 ) # Double-check: pt2 = list( int_to_Hilbert( x, nD ) ) if pt2 != pt: print "BZZT! Encodes to ", pt2 # Encode k to an nD point: pt = int_to_Hilbert( k, nD ) print pt # Double-check: y = Hilbert_to_int( pt ) if y != k: print "BZZT! Decodes to", y
print int_to_Hilbert_test(64, 3) print "int_to_Hilbert_test( 81, 4)" print int_to_Hilbert_test(81, 4) print "weird" print int_to_Hilbert_test(256, 4) print "int_to_Hilbert_test( 4096, 3, doPrint=False ) # nChunks > nD" print int_to_Hilbert_test(4096, 3, doPrint=False) # nChunks > nD print "int_to_Hilbert_test( 4096, 4, doPrint=False )" print int_to_Hilbert_test(4096, 4, doPrint=False) k = 10**12 + 0.2 # definitely a long for nD in [2, 3, 4, 5, 20, 40, 2000]: # Decode point ( k, 0, 0... ) to an int...how many digits?: pt = [0] * nD pt[0] = k x = Hilbert_to_int(pt) print x, log(x, 10) # Double-check: pt2 = list(int_to_Hilbert(x, nD)) if pt2 != pt: print "BZZT! Encodes to ", pt2 # Encode k to an nD point: pt = int_to_Hilbert(k, nD) print pt # Double-check: y = Hilbert_to_int(pt) if y != k: print "BZZT! Decodes to", y
# If needed... def generateface2picsmapping(minimum_faces_per_person=1): lfw_people = fetch_lfw_people( min_faces_per_person=minimum_faces_per_person, resize=0.4) n_samples, h, w = lfw_people.images.shape X, y, target_names = lfw_people.data, lfw_people.target, lfw_people.target_names n_examples, n_features = X.shape face2pics = [] print(max(y)) for i in range((max(y) + 1)): face2pics.append([target_names[i], []]) for i in range(len(y)): face2pics[y[i]][1].append(i) return face2pics if __name__ == "__main__": (X, X_nmf, y, nmf_components, min_faces_per_person, face2pics) = pickle.load(open("nmfdata.p", "rb")) n_examples, n_features = X_nmf.shape print(X_nmf[0]) i = Hilbert_to_int(float2IntVector(X_nmf[0])) print(i) v2 = int2FloatVector(int_to_Hilbert(i, nD=n_features)) print(v2) i2 = Hilbert_to_int(float2IntVector(v2)) print(i2) print('loss') print(i2 - i)
def mk_int(i): (x, y, z)= hilbert.int_to_Hilbert(i, 3) rsp = flask.jsonify(x=x, y=y, z=z, i=i) return rsp