1
0
Fork 0
mirror of https://github.com/Findus23/halo_comparison.git synced 2024-09-19 16:03:50 +02:00
halo_comparison/HilbertCurvesIndexing.py

182 lines
5.2 KiB
Python
Raw Permalink Normal View History

2022-07-27 13:37:57 +02:00
"""
originally created by Oliver Hahn
in HilbertCurvesIndexing.ipynb
"""
from pathlib import Path
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
2022-10-27 16:34:11 +02:00
from numpy.random import SeedSequence, Generator, PCG64
2022-07-27 13:37:57 +02:00
# dictionary containing the first order hilbert curves
from utils import figsize_from_page_fraction
# from matplotlib import rc
# # rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
# ## for Palatino and other serif fonts use:
# rc('font',**{'family':'serif','serif':['Times New Roman']})
# rc('text', usetex=True)
# rc('axes', titlesize=24)
# rc('axes', labelsize=20)
# rc('axes', axisbelow=False)
# rc('lines',linewidth=2)
# # lines.markersize : 10
# rc('xtick', labelsize=16)
# rc('xtick.major',size=10)
# rc('xtick.minor',size=5)
# rc('xtick',direction='in')
# rc('ytick', labelsize=16)
# rc('ytick.major',size=10)
# rc('ytick.minor',size=5)
# rc('ytick',direction='in')
# rc('legend',fontsize='x-large')
base_shape = {
"u": [np.array([0, 1]), np.array([1, 0]), np.array([0, -1])],
"d": [np.array([0, -1]), np.array([-1, 0]), np.array([0, 1])],
"r": [np.array([1, 0]), np.array([0, 1]), np.array([-1, 0])],
"l": [np.array([-1, 0]), np.array([0, -1]), np.array([1, 0])],
}
2022-07-27 13:37:57 +02:00
def hilbert_curve(order, orientation):
"""
Recursively creates the structure for a hilbert curve of given order
"""
if order > 1:
if orientation == "u":
return (
hilbert_curve(order - 1, "r")
+ [np.array([0, 1])]
+ hilbert_curve(order - 1, "u")
+ [np.array([1, 0])]
+ hilbert_curve(order - 1, "u")
+ [np.array([0, -1])]
+ hilbert_curve(order - 1, "l")
)
elif orientation == "d":
return (
hilbert_curve(order - 1, "l")
+ [np.array([0, -1])]
+ hilbert_curve(order - 1, "d")
+ [np.array([-1, 0])]
+ hilbert_curve(order - 1, "d")
+ [np.array([0, 1])]
+ hilbert_curve(order - 1, "r")
)
elif orientation == "r":
return (
hilbert_curve(order - 1, "u")
+ [np.array([1, 0])]
+ hilbert_curve(order - 1, "r")
+ [np.array([0, 1])]
+ hilbert_curve(order - 1, "r")
+ [np.array([-1, 0])]
+ hilbert_curve(order - 1, "d")
)
2022-07-27 13:37:57 +02:00
else:
return (
hilbert_curve(order - 1, "d")
+ [np.array([-1, 0])]
+ hilbert_curve(order - 1, "l")
+ [np.array([0, -1])]
+ hilbert_curve(order - 1, "l")
+ [np.array([1, 0])]
+ hilbert_curve(order - 1, "u")
)
2022-07-27 13:37:57 +02:00
else:
return base_shape[orientation]
# test the functions
# if __name__ == '__main__':
# order = 8
# curve = hilbert_curve(order, 'u')
# curve = np.array(curve) * 4
# cumulative_curve = np.array([np.sum(curve[:i], 0) for i in range(len(curve)+1)])
# # plot curve using plt
# plt.plot(cumulative_curve[:, 0], cumulative_curve[:, 1])
# draw curve using turtle graphics
# tt.setup(1920, 1000)
# tt.pu()
# tt.goto(-950, -490)
# tt.pd()
# tt.speed(0)
# for item in curve:
# tt.goto(tt.pos()[0] + item[0], tt.pos()[1] + item[1])
# tt.done()
order = 6
curve = hilbert_curve(order, "u")
2022-07-27 13:37:57 +02:00
curve = np.array(curve) * 4
cumulative_curve_int = np.array([np.sum(curve[:i], 0) for i in range(len(curve) + 1)])
cumulative_curve = (
np.array([np.sum(curve[:i], 0) for i in range(len(curve) + 1)]) + 2
) / 2 ** (order + 2)
2022-07-27 13:37:57 +02:00
# plot curve using plt
N = 2 ** (2 * order)
sublevel = order - 4
cmap = cm.get_cmap("jet")
2022-07-27 13:37:57 +02:00
fig = plt.figure(figsize=figsize_from_page_fraction(height_to_width=1))
t = {}
sublevel = 7
for i in range(2 ** (2 * sublevel)):
il = i * N // (2 ** (2 * sublevel))
ir = (i + 1) * N // 2 ** (2 * sublevel)
plt.plot(
cumulative_curve[il : ir + 1, 0],
cumulative_curve[il : ir + 1, 1],
lw=0.5,
c=cmap(i / 2 ** (2 * sublevel)),
)
plt.xlabel("$x$")
plt.ylabel("$y$")
2022-07-27 13:37:57 +02:00
plt.tight_layout()
2022-08-01 14:03:46 +02:00
plt.savefig(Path(f"~/tmp/hilbert_indexcolor.eps").expanduser())
2022-07-27 13:37:57 +02:00
order = 6
curve = hilbert_curve(order, "u")
2022-07-27 13:37:57 +02:00
curve = np.array(curve) * 4
cumulative_curve_int = np.array([np.sum(curve[:i], 0) for i in range(len(curve) + 1)])
cumulative_curve = (
np.array([np.sum(curve[:i], 0) for i in range(len(curve) + 1)]) + 2
) / 2 ** (order + 2)
2022-07-27 13:37:57 +02:00
# plot curve using plt
N = 2 ** (2 * order)
sublevel = order - 4
cmap = cm.get_cmap("jet")
2022-07-27 13:37:57 +02:00
2022-10-27 16:34:11 +02:00
s = 10 # arbitrary lenght of each sequence drawn for every point
2022-07-27 13:37:57 +02:00
fig = plt.figure(figsize=figsize_from_page_fraction(height_to_width=1))
t = {}
sublevel = 7
for i in range(2 ** (2 * sublevel)):
2022-10-27 16:34:11 +02:00
pcg = PCG64(1234)
pcg = pcg.advance(i * s)
rng = Generator(pcg)
2022-07-27 13:37:57 +02:00
il = i * N // (2 ** (2 * sublevel))
ir = (i + 1) * N // 2 ** (2 * sublevel)
2022-10-27 16:34:11 +02:00
random_value = rng.random()
plt.plot(
cumulative_curve[il : ir + 1, 0],
cumulative_curve[il : ir + 1, 1],
lw=0.5,
2022-10-27 16:34:11 +02:00
c=cmap(random_value),
)
plt.xlabel("$x$")
plt.ylabel("$y$")
2022-07-27 13:37:57 +02:00
plt.tight_layout()
2022-08-01 14:03:46 +02:00
plt.savefig(Path(f"~/tmp/hilbert_indexcolor_scrambled.eps").expanduser())
2022-07-27 13:37:57 +02:00
plt.show()