def test_reversibility(self): """Assert coordinates_from_distance and distance_from_coordinates are inverse operations.""" N = 3 p = 5 hilbert_curve = hilbert.HilbertCurve(p, N) n_h = 2**(N * p) for h in range(n_h): x = hilbert_curve.coordinates_from_distance(h) h_test = hilbert_curve.distance_from_coordinates(x) self.assertEqual(h, h_test)
def hilbertTraversal(im): # if not square image, let's make it square # just squish it, that way we keep some image coords = [] if (imgIsSquare(im) == False): im = makeImgSquare(im) # then we need to find the closest power of 4 num_pix = im.shape[0] * im.shape[1] p = int(math.log(num_pix, 4)) hc = hilbert.HilbertCurve(p, 2) # should return flattened single array of coordinates hil_size = int(math.pow(4, p)) for i in range(hil_size): coord = hc.coordinates_from_distance(i) val = im[coord[0]][coord[1]] coords.append(val) # flatten in order of coords return np.asarray(coords, dtype=np.float32)
def hilbert_points(pprint, p): N = 2 # Number of Dimensions # pprint("P is: " + p) spacing = 2 pmax = 2 side = 2**pmax min_coord = 0 max_coord = spacing * (side - 1) cmin = min_coord - 0.5 cmax = max_coord + 0.5 dx = 0.5 * spacing offset = -3 hc = hilbert.HilbertCurve(pprint, p, N) for i in range(2, p, -1): offset += dx dx *= 2 sidep = 2**p npts = 2**(N * p) pts = [] for i in range(npts): pts.append(hc.coordinates_from_distance(i)) # pprint("pts var is: " + str(pts)) new_pts = [] for pt in pts: new_pts.append((spacing * (pt[0] * side / sidep) + offset, spacing * (pt[1] * side / sidep) + offset)) # Returns pts of defense relative to the castle return new_pts
def test_10590(self): """Assert that a 15 bit hilber integer is correctly transposed into a 3-d vector ... ABCDEFGHIJKLMNO 10590 (0b010100101011110) ADGJM X[0] = 0b01101 = 13 BEHKN X[1] = 0b10011 = 19 CFILO X[2] = 0b00110 = 6 """ p = 5 N = 3 hilbert_curve = hilbert.HilbertCurve(p, N) h = 10590 expected_x = [13, 19, 6] actual_x = hilbert_curve._hilbert_integer_to_transpose(h) self.assertEqual(actual_x, expected_x)
def test_13_19_6(self): """Assert that a 15 bit hilber integer is correctly recovered from its transposed 3-d vector ... ABCDEFGHIJKLMNO 10590 (0b010100101011110) ADGJM X[0] = 0b01101 = 13 BEHKN X[1] = 0b10011 = 19 CFILO X[2] = 0b00110 = 6 """ p = 5 N = 3 hilbert_curve = hilbert.HilbertCurve(p, N) x = [13, 19, 6] expected_h = 10590 actual_h = hilbert_curve._transpose_to_hilbert_integer(x) self.assertEqual(actual_h, expected_h)
import matplotlib.pyplot as plt import hilbert plt.figure(figsize=(10, 10)) N = 2 # number of dimensions p = 3 # number of iterations hc = hilbert.HilbertCurve(p, N) npts = 2**(N * p) pts = [] for i in range(npts): pts.append(hc.coordinates_from_distance(i)) connectors = range(3, npts, 4) for i in range(npts - 1): if i in connectors: color = 'grey' linestyle = '--' else: color = 'black' linestyle = '-' plt.plot((pts[i][0], pts[i + 1][0]), (pts[i][1], pts[i + 1][1]), color=color, linewidth=3, linestyle=linestyle) for i in range(npts): plt.scatter(pts[i][0], pts[i][1], 60, color='red') plt.text(pts[i][0] + 0.1, pts[i][1] + 0.1, str(i)) side = 2**p - 1 cmin = -0.5
def fragGPA(inputMatrix, gn, tol, rad_tol, bandwidth=0.4, nbands=20, hbands=5): ''' Description: Measures the GPA, for a set of filtered images (made with butterworth). As increases the band, crops the boundaries (avoiding patterns affected by boundaries) Input: inputMatrix - the matrix to be analyzed gn - Gradient Moment (i.e. G1) tol - GPA modulus tolerance (0.0-1.0) rad_tol - GPA phase tolerance (0-2*pi) bandwidth - 0.0-1.0 nbands - number of bands hbands - hilbert curve interpolator size (total of bands = 2^(2*hbands)) Output: glist - list of GPA results avgFreq - list of the band average imgList - list of filtered images interp - Scipy interpolator, for the GPA sequence layer - GPA frequency series transformed in matrix, via PCHIP interpolation and Hilbert curves gssa - GPA of layer (it uses flexible tolerance based on hbands) ''' glist = [] avgFreq = [] imgList = [] freq = np.fft.fft2(inputMatrix) sfreq = np.fft.fftshift(freq) seq = [[ 0.5 * float(i) / float(nbands) - bandwidth / 2.0, 0.5 * float(i) / float(nbands) + bandwidth / 2.0 ] for i in range(1, nbands + 1)] # Frequencies loop (to build the series) for fb in seq: avg = np.average(fb) fsfreq = filterFreq(sfreq, 3, fb) filtered = np.real(np.fft.ifft2(np.fft.ifftshift(fsfreq))).astype( np.float) filtered = crop_center(filtered, avg / 4.0, avg / 4.0) imgList.append(filtered) avgFreq.append(np.average(fb)) gaObject = ga(tol, rad_tol) gaObject.evaluate(filtered, [gn]) if (gn == "G1"): glist.append(gaObject.G1) elif (gn == "G2"): glist.append(gaObject.G2) elif (gn == "G3"): glist.append(gaObject.G3) elif (gn == "G4"): glist.append(gaObject.G4) # Starts transforming the series in a matrix dim = pow(2, hbands) interp = interpolate.PchipInterpolator(avgFreq, glist) xs = np.linspace(min(avgFreq), max(avgFreq), dim * dim) ys = interp(xs) hcurve = hilbert.HilbertCurve(hbands, 2) layer = np.array( [[ys[hcurve.distance_from_coordinates([i, j])] for i in range(dim)] for j in range(dim)]).astype(np.float) # Now measures the gradient moment gaObject = ga(1.0 / float(dim * dim), 1.0 / float(dim * dim)) gaObject.evaluate(layer, [gn]) if (gn == "G1"): gssa = gaObject.G1 elif (gn == "G2"): gssa = gaObject.G2 elif (gn == "G3"): gssa = gaObject.G3 elif (gn == "G4"): gssa = gaObject.G4 return np.array(glist), np.array(avgFreq), np.array( imgList), interp, layer, gssa
import hilbert # we can go from distance along a curve to coordinates # in 2 dimensions with p=1 there are only 4 locations on the curve # [0, 0], [0, 1], [1, 1], [1, 0] p = 1 N = 2 hilbert_curve = hilbert.HilbertCurve(p, N) for ii in range(4): print('coords(h={},p={},N={}) = {}'.format( ii, p, N, hilbert_curve.coordinates_from_distance(ii))) # due to the magic of arbitrarily large integers in # Python (https://www.python.org/dev/peps/pep-0237/) # these calculations can be done with absurd numbers p = 512 N = 10 hilbert_curve = hilbert.HilbertCurve(p, N) ii = 123456789101112131415161718192021222324252627282930 coords = hilbert_curve.coordinates_from_distance(ii) print('coords(h={},p={},N={}) = {}'.format(ii, p, N, coords))