def _compute_distance(df, x, y, p, x_range, y_range, as_series=False): """ Compute an array of Hilbert distances from a pandas dataframe Parameters ---------- df: pd.DataFrame pandas dataframe containing the coordinate columns x Label of the column containing the x-coordinates y Label of the column containing the y-coordinates p: int Hilbert-curve p value x_range: tuple Start (x_range[0]) and stop (x_range[1]) range of Hilbert x scale in data coordinates y_range: tuple Start (y_range[0]) and stop (y_range[1]) range of Hilbert y scale in data coordinates as_series: bool (default False) Whether to return results as a pandas series with index matching df Returns ------- np.ndarray Of Hilbert distances """ side_length = 2**p x_coords = _data2coord(df[x], x_range, side_length) y_coords = _data2coord(df[y], y_range, side_length) res = hc.distance_from_coordinates(p, x_coords, y_coords) if as_series: res = pd.Series(res, index=df.index) return res
def _build_distance_grid(p): """ Build a (2 ** p) x (2 ** p) array containing the Hilbert distance of each discrete location in the grid Parameters ---------- p: int Hilbert curve order Returns ------- np.ndarray 2D array containing the Hilbert curve distances for a curve with order p """ side_length = int(2**p) distance_grid = np.zeros((side_length, side_length), dtype=np.int64) for i in range(side_length): for j in range(side_length): distance_grid[i, j] = (hc.distance_from_coordinates(p, i, j)) return distance_grid