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

121 lines
4.4 KiB
Python
Raw Normal View History

2022-07-14 15:14:26 +02:00
from sys import argv
2022-05-31 17:22:57 +02:00
from typing import Tuple
import h5py
2022-05-24 17:06:49 +02:00
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from cic import cic_from_radius
2022-07-14 15:14:26 +02:00
from paths import base_dir, vis_datafile, has_1024_simulations
2022-06-01 11:31:42 +02:00
from read_vr_files import read_velo_halo_particles
2022-05-24 17:06:49 +02:00
from readfiles import read_file
2022-07-14 15:25:27 +02:00
all_in_area = True
2022-05-24 17:06:49 +02:00
2022-05-31 17:22:57 +02:00
Coords = Tuple[float, float, float, float] # radius, X, Y, Z
2022-05-24 17:06:49 +02:00
2022-05-31 17:22:57 +02:00
def load_halo_data(waveform: str, resolution: int, halo_id: int, coords: Coords):
2022-05-24 17:06:49 +02:00
dir = base_dir / f"{waveform}_{resolution}_100"
2022-07-05 15:40:17 +02:00
df, meta = read_file(dir / "output_0004.hdf5")
2022-07-14 15:14:26 +02:00
df_halo, halo_lookup, unbound = read_velo_halo_particles(dir, skip_halo_particle_ids=all_in_area)
2022-05-24 17:06:49 +02:00
halo = df_halo.loc[halo_id]
2022-07-05 15:40:17 +02:00
if coords:
radius, X, Y, Z = coords
else:
radius = halo["R_size"]
X = halo["Xc"]
Y = halo["Yc"]
Z = halo["Zc"]
coords: Coords = radius, X, Y, Z
2022-05-30 19:12:59 +02:00
if all_in_area:
df = df[df["X"].between(X - radius, X + radius)]
df = df[df["Y"].between(Y - radius, Y + radius)]
halo_particles = df[df["Z"].between(Z - radius, Z + radius)]
else:
halo_particle_ids = halo_lookup[halo_id]
del halo_lookup
del unbound
halo_particles = df.loc[list(halo_particle_ids)]
2022-05-31 17:22:57 +02:00
return halo, halo_particles, meta, coords
2022-05-24 17:06:49 +02:00
def get_comp_id(ref_waveform: str, reference_resolution: int, comp_waveform: str, comp_resolution: int):
return f"{ref_waveform}_{reference_resolution}_100_{comp_waveform}_{comp_resolution}_100_velo.csv"
def map_halo_id(halo_id: int, ref_waveform: str, reference_resolution: int, comp_waveform: str, comp_resolution: int):
file = base_dir / "comparisons" / get_comp_id(ref_waveform, reference_resolution, comp_waveform, comp_resolution)
print("opening", file)
df = pd.read_csv(file)
mapping = {}
for index, line in df.iterrows():
mapping[int(line["ref_ID"])] = int(line["comp_ID"])
return mapping[halo_id]
def imsave(rho, file_name: str):
2022-05-24 17:06:49 +02:00
# ax.scatter(Xs, Ys)
# plt.show()
cmap = plt.cm.viridis
data = np.log(1.001 + rho)
norm = plt.Normalize(vmin=data.min(), vmax=data.max())
image = cmap(norm(data))
print(file_name)
plt.imsave(file_name, image)
def main():
2022-07-14 15:14:26 +02:00
resolutions = [128, 256, 512]
if has_1024_simulations:
resolutions.append(1024)
initial_halo_id = int(argv[1])
2022-05-24 17:06:49 +02:00
first_halo = True
rhos = {}
ref_waveform = "shannon"
ref_resolution = 128
2022-07-05 15:40:17 +02:00
coords = {}
for wf in waveforms:
coords[wf] = None
vmin = np.Inf
vmax = -np.Inf
2022-07-14 15:14:26 +02:00
with h5py.File(vis_datafile, "a") as vis_out:
halo_group = vis_out.create_group(str(initial_halo_id))
2022-07-05 15:40:17 +02:00
for waveform in waveforms:
2022-07-14 15:14:26 +02:00
for resolution in resolutions:
if first_halo:
assert ref_resolution == resolution
assert ref_waveform == waveform
halo_id = initial_halo_id
first_halo = False
else:
halo_id = map_halo_id(initial_halo_id, ref_waveform, ref_resolution, waveform, resolution)
2022-07-14 15:14:26 +02:00
halo, halo_particles, meta, image_coords = load_halo_data(waveform, resolution, halo_id,
coords[waveform])
2022-07-05 15:40:17 +02:00
if not coords[waveform]:
coords[waveform] = image_coords
print(coords[waveform])
2022-05-30 19:12:59 +02:00
print("mass", halo["Mvir"])
# print("sleep")
# sleep(100)
2022-07-05 15:40:17 +02:00
radius, X, Y, Z = coords[waveform]
rho, _ = cic_from_radius(
halo_particles.X.to_numpy(), halo_particles.Y.to_numpy(),
2022-05-31 17:22:57 +02:00
1000, X, Y, radius, periodic=False)
rhos[(waveform, resolution)] = rho
vmin = min(rho.min(), vmin)
vmax = max(rho.max(), vmax)
2022-07-14 15:14:26 +02:00
dataset_group = halo_group.create_group(f"{waveform}_{resolution}")
dataset_group.create_dataset("rho", data=rho, compression='gzip', compression_opts=5)
dataset_group.create_dataset("coords", data=coords[waveform])
dataset_group.create_dataset("mass", data=meta.particle_mass)
dataset_group.create_dataset("halo_id", data=halo_id)
imsave(rho, f"out_halo{initial_halo_id}_{waveform}_{resolution}_{halo_id}.png")
2022-07-14 15:14:26 +02:00
halo_group.create_dataset("vmin_vmax", data=[vmin, vmax])
2022-05-24 17:06:49 +02:00
if __name__ == '__main__':
main()