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

102 lines
2.9 KiB
Python
Raw Normal View History

2022-05-31 17:22:57 +02:00
import sys
2022-05-23 16:16:57 +02:00
from pathlib import Path
from typing import Tuple
2022-05-23 16:16:57 +02:00
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.axes import Axes
from matplotlib.colors import LogNorm
from matplotlib.figure import Figure
from numba import njit
from readfiles import read_file
Extent = Tuple[float, float, float, float]
2022-05-23 16:16:57 +02:00
2022-05-31 17:22:57 +02:00
@njit(parallel=True)
def cic_deposit(X, Y, ngrid, periodic=True) -> np.ndarray:
2022-05-23 16:16:57 +02:00
rho = np.zeros((ngrid, ngrid))
for x, y in zip(X, Y):
2022-05-31 17:22:57 +02:00
if periodic:
x = np.fmod(1.0 + x, 1.0)
y = np.fmod(1.0 + y, 1.0)
else:
if not (0 < x < 1) or not (0 < y < 1):
continue
2022-05-23 16:16:57 +02:00
il = int(np.floor(x * ngrid))
ir = (il + 1) % ngrid
jl = int(np.floor(y * ngrid))
jr = (jl + 1) % ngrid
dx = x * ngrid - float(il)
dy = y * ngrid - float(jl)
rho[il, jl] += (1 - dx) * (1 - dy)
rho[il, jr] += (1 - dx) * dy
rho[ir, jl] += dx * (1 - dy)
rho[ir, jr] += dx * dy
rhomean = len(X) / ngrid ** 2
return rho / rhomean - 1
def cic_range(
X: np.ndarray, Y: np.ndarray,
ngrid: int,
xmin: float, xmax: float,
2022-05-31 17:22:57 +02:00
ymin: float, ymax: float, *args, **kwargs
) -> Tuple[np.ndarray, Extent]:
xrange = xmax - xmin
yrange = ymax - ymin
Xs = (X - xmin) / xrange
Ys = (Y - ymin) / yrange
2022-05-31 17:22:57 +02:00
print(Xs.min(), Xs.max())
print(Ys.min(), Ys.max())
print((60 - ymin) / yrange)
extent = (xmin, xmax, ymin, ymax)
2022-05-31 17:22:57 +02:00
rho = cic_deposit(Xs, Ys, ngrid, *args, **kwargs)
return rho, extent
def cic_from_radius(
X: np.ndarray, Y: np.ndarray,
ngrid: int,
x_center: float, y_center: float,
2022-05-31 17:22:57 +02:00
radius: float, *args, **kwargs
) -> Tuple[np.ndarray, Extent]:
2022-05-31 17:22:57 +02:00
return cic_range(
X, Y, ngrid,
x_center - radius, x_center + radius,
y_center - radius, y_center + radius,
*args, **kwargs
)
2022-06-03 10:40:34 +02:00
def plot_file(input_file: Path):
df_ref, _ = read_file(input_file)
2022-05-24 17:06:49 +02:00
fig: Figure = plt.figure()
ax: Axes = fig.gca()
print("start cic")
2022-06-03 10:33:16 +02:00
# rho, extent = cic_from_radius(df_ref.X.to_numpy(), df_ref.Y.to_numpy(), 2000, 48.85, 56.985, 2, periodic=False)
rho, extent = cic_from_radius(df_ref.X.to_numpy(), df_ref.Y.to_numpy(), 500, 56, 49.5, 5, periodic=False)
# rho, extent = cic_range(df_ref.X.to_numpy(), df_ref.Y.to_numpy(), 1000, 0, 100, 0, 100, periodic=False)
2022-05-24 17:06:49 +02:00
print("finished cic")
2022-05-31 17:22:57 +02:00
data = 1.1 + rho
i = ax.imshow(data.T, norm=LogNorm(), extent=extent, origin="lower")
2022-06-03 10:33:16 +02:00
ax.set_title(str(input_file.relative_to(input_file.parent.parent)))
2022-05-24 17:06:49 +02:00
fig.colorbar(i)
plt.show()
cmap = plt.cm.viridis
data = np.log(data)
norm = plt.Normalize(vmin=data.min(), vmax=data.max())
image = cmap(norm(data))
plt.imsave("out.png", image)
# ax.hist2d(df.X, df.Y, bins=500, norm=LogNorm())
# ax.hist2d(df2.X, df2.Y, bins=1000, norm=LogNorm())
2022-06-03 10:40:34 +02:00
if __name__ == '__main__':
plot_file(Path(sys.argv[1]))