Edits paths (#79)

This commit is contained in:
Richard Stiskalek 2023-07-29 10:16:43 +02:00 committed by GitHub
parent 61077940f3
commit 5acc4684bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,17 @@ from warnings import warn
import numpy import numpy
def check_directory(path):
if not isdir(path):
raise IOError(f"Invalid directory `{path}`!")
def try_create_directory(fdir):
if not isdir(fdir):
makedirs(fdir)
warn(f"Created directory `{fdir}`.")
class Paths: class Paths:
""" """
Paths manager for CSiBORG and Quijote simulations. Paths manager for CSiBORG and Quijote simulations.
@ -48,11 +59,6 @@ class Paths:
self.borg_dir = borg_dir self.borg_dir = borg_dir
self.quijote_dir = quijote_dir self.quijote_dir = quijote_dir
@staticmethod
def _check_directory(path):
if not isdir(path):
raise IOError(f"Invalid directory `{path}`!")
@property @property
def srcdir(self): def srcdir(self):
""" """
@ -70,7 +76,7 @@ class Paths:
def srcdir(self, path): def srcdir(self, path):
if path is None: if path is None:
return return
self._check_directory(path) check_directory(path)
self._srcdir = path self._srcdir = path
@property @property
@ -90,7 +96,7 @@ class Paths:
def borg_dir(self, path): def borg_dir(self, path):
if path is None: if path is None:
return return
self._check_directory(path) check_directory(path)
self._borg_dir = path self._borg_dir = path
@property @property
@ -110,7 +116,7 @@ class Paths:
def quijote_dir(self, path): def quijote_dir(self, path):
if path is None: if path is None:
return return
self._check_directory(path) check_directory(path)
self._quijote_dir = path self._quijote_dir = path
@property @property
@ -130,7 +136,7 @@ class Paths:
def postdir(self, path): def postdir(self, path):
if path is None: if path is None:
return return
self._check_directory(path) check_directory(path)
self._postdir = path self._postdir = path
@property @property
@ -143,9 +149,7 @@ class Paths:
path : str path : str
""" """
fpath = join(self.postdir, "temp") fpath = join(self.postdir, "temp")
if not isdir(fpath): try_create_directory(fpath)
mkdir(fpath)
warn(f"Created directory `{fpath}`.", UserWarning, stacklevel=1)
return fpath return fpath
@staticmethod @staticmethod
@ -202,11 +206,9 @@ class Paths:
""" """
assert simname in ["csiborg", "quijote"] assert simname in ["csiborg", "quijote"]
if simname == "quijote": if simname == "quijote":
raise RuntimeError("Quijote FoF membership is in the FoF cats..") raise RuntimeError("Quijote FoF membership is in the FoF cats.")
fdir = join(self.postdir, "FoF_membership", ) fdir = join(self.postdir, "FoF_membership", )
if not isdir(fdir): try_create_directory(fdir)
mkdir(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
fout = join(fdir, f"fof_membership_{nsim}.npy") fout = join(fdir, f"fof_membership_{nsim}.npy")
if sorted: if sorted:
fout = fout.replace(".npy", "_sorted.npy") fout = fout.replace(".npy", "_sorted.npy")
@ -223,14 +225,14 @@ class Paths:
simname : str simname : str
Simulation name. Must be one of `csiborg` or `quijote`. Simulation name. Must be one of `csiborg` or `quijote`.
""" """
assert simname in ["csiborg", "quijote"]
if simname == "csiborg": if simname == "csiborg":
fdir = join(self.postdir, "FoF_membership", ) fdir = join(self.postdir, "FoF_membership", )
if not isdir(fdir): try_create_directory(fdir)
mkdir(fdir)
warn(f"Created directory `{fdir}`.", UserWarning)
return join(fdir, f"halo_catalog_{nsim}_FOF.txt") return join(fdir, f"halo_catalog_{nsim}_FOF.txt")
return join(self.quijote_dir, "Halos_fiducial", str(nsim)) elif simname == "quijote":
return join(self.quijote_dir, "Halos_fiducial", str(nsim))
else:
raise ValueError(f"Unknown simulation name `{simname}`.")
def mmain(self, nsnap, nsim): def mmain(self, nsnap, nsim):
""" """
@ -248,11 +250,9 @@ class Paths:
path : str path : str
""" """
fdir = join(self.postdir, "mmain") fdir = join(self.postdir, "mmain")
if not isdir(fdir): try_create_directory(fdir)
mkdir(fdir) return join(
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1) fdir, f"mmain_{str(nsim).zfill(5)}_{str(nsnap).zfill(5)}.npz")
return join(fdir,
f"mmain_{str(nsim).zfill(5)}_{str(nsnap).zfill(5)}.npz")
def initmatch(self, nsim, simname, kind): def initmatch(self, nsim, simname, kind):
""" """
@ -272,21 +272,17 @@ class Paths:
------- -------
path : str path : str
""" """
assert simname in ["csiborg", "quijote"]
assert kind in ["particles", "fit"] assert kind in ["particles", "fit"]
ftype = "npy" if kind == "fit" else "h5" ftype = "npy" if kind == "fit" else "h5"
if simname == "csiborg": if simname == "csiborg":
fdir = join(self.postdir, "initmatch") fdir = join(self.postdir, "initmatch")
if not isdir(fdir): elif simname == "quijote":
mkdir(fdir) fdir = join(self.quijote_dir, "initmatch")
warn(f"Created directory `{fdir}`.", UserWarning) else:
return join(fdir, f"{kind}_{str(nsim).zfill(5)}.{ftype}") raise ValueError(f"Unknown simulation name `{simname}`.")
fdir = join(self.quijote_dir, "initmatch") try_create_directory(fdir)
if not isdir(fdir):
mkdir(fdir)
warn(f"Created directory `{fdir}`.", UserWarning)
return join(fdir, f"{kind}_{str(nsim).zfill(5)}.{ftype}") return join(fdir, f"{kind}_{str(nsim).zfill(5)}.{ftype}")
def get_ics(self, simname): def get_ics(self, simname):
@ -303,7 +299,6 @@ class Paths:
------- -------
ids : 1-dimensional array ids : 1-dimensional array
""" """
assert simname in ["csiborg", "quijote", "quijote_full"]
if simname == "csiborg": if simname == "csiborg":
files = glob(join(self.srcdir, "ramses_out*")) files = glob(join(self.srcdir, "ramses_out*"))
files = [f.split("/")[-1] for f in files] # Only file names files = [f.split("/")[-1] for f in files] # Only file names
@ -315,10 +310,13 @@ class Paths:
ids.remove(5511) ids.remove(5511)
except ValueError: except ValueError:
pass pass
return numpy.sort(ids) elif simname == "quijote" or simname == "quijote_full":
files = glob(
"/mnt/extraspace/rstiskalek/Quijote/Snapshots_fiducial/*")
files = [int(f.split("/")[-1]) for f in files]
else:
raise ValueError(f"Unknown simulation name `{simname}`.")
files = glob("/mnt/extraspace/rstiskalek/Quijote/Snapshots_fiducial/*")
files = [int(f.split("/")[-1]) for f in files]
return numpy.sort(files) return numpy.sort(files)
def snapshots(self, nsim, simname, tonew=False): def snapshots(self, nsim, simname, tonew=False):
@ -339,15 +337,16 @@ class Paths:
------- -------
path : str path : str
""" """
assert simname in ["csiborg", "quijote"]
if simname == "csiborg": if simname == "csiborg":
fname = "ramses_out_{}" fname = "ramses_out_{}"
if tonew: if tonew:
fname += "_new" fname += "_new"
return join(self.postdir, "output", fname.format(nsim)) return join(self.postdir, "output", fname.format(nsim))
return join(self.srcdir, fname.format(nsim)) return join(self.srcdir, fname.format(nsim))
elif simname == "quijote":
return join(self.quijote_dir, "Snapshots_fiducial", str(nsim)) return join(self.quijote_dir, "Snapshots_fiducial", str(nsim))
else:
raise ValueError(f"Unknown simulation name `{simname}`.")
def get_snapshots(self, nsim, simname): def get_snapshots(self, nsim, simname):
""" """
@ -370,10 +369,12 @@ class Paths:
snaps = glob(join(simpath, "output_*")) snaps = glob(join(simpath, "output_*"))
# Take just the last _00XXXX from each file and strip zeros # Take just the last _00XXXX from each file and strip zeros
snaps = [int(snap.split("_")[-1].lstrip("0")) for snap in snaps] snaps = [int(snap.split("_")[-1].lstrip("0")) for snap in snaps]
else: elif simname == "quijote":
snaps = glob(join(simpath, "snapdir_*")) snaps = glob(join(simpath, "snapdir_*"))
snaps = [int(snap.split("/")[-1].split("snapdir_")[-1]) snaps = [int(snap.split("/")[-1].split("snapdir_")[-1])
for snap in snaps] for snap in snaps]
else:
raise ValueError(f"Unknown simulation name `{simname}`.")
return numpy.sort(snaps) return numpy.sort(snaps)
def snapshot(self, nsnap, nsim, simname): def snapshot(self, nsnap, nsim, simname):
@ -418,19 +419,14 @@ class Paths:
------- -------
path : str path : str
""" """
assert simname in ["csiborg", "quijote"]
if simname == "csiborg": if simname == "csiborg":
fdir = join(self.postdir, "particles") fdir = join(self.postdir, "particles")
if not isdir(fdir): elif simname == "quijote":
makedirs(fdir) fdir = join(self.quijote_dir, "Particles_fiducial")
warn(f"Created directory `{fdir}`.", UserWarning) else:
fname = f"parts_{str(nsim).zfill(5)}.h5" raise ValueError(f"Unknown simulation name `{simname}`.")
return join(fdir, fname)
fdir = join(self.quijote_dir, "Particles_fiducial") try_create_directory(fdir)
if not isdir(fdir):
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning)
fname = f"parts_{str(nsim).zfill(5)}.h5" fname = f"parts_{str(nsim).zfill(5)}.h5"
return join(fdir, fname) return join(fdir, fname)
@ -451,19 +447,13 @@ class Paths:
------- -------
path : str path : str
""" """
assert simname in ["csiborg", "quijote"]
if simname == "csiborg": if simname == "csiborg":
fdir = join(self.postdir, "structfit") fdir = join(self.postdir, "structfit")
if not isdir(fdir): elif simname == "quijote":
mkdir(fdir) fdir = join(self.quijote_dir, "structfit")
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1) else:
fname = f"out_{str(nsim).zfill(5)}_{str(nsnap).zfill(5)}.npy" raise ValueError(f"Unknown simulation name `{simname}`.")
return join(fdir, fname)
fdir = join(self.quijote_dir, "structfit")
if not isdir(fdir):
mkdir(fdir)
warn(f"Created directory `{fdir}`.", UserWarning)
fname = f"out_{str(nsim).zfill(5)}_{str(nsnap).zfill(5)}.npy" fname = f"out_{str(nsim).zfill(5)}_{str(nsnap).zfill(5)}.npy"
return join(fdir, fname) return join(fdir, fname)
@ -485,9 +475,8 @@ class Paths:
path : str path : str
""" """
fdir = join(self.postdir, "overlap") fdir = join(self.postdir, "overlap")
if not isdir(fdir): try_create_directory(fdir)
mkdir(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
fname = f"overlap_{str(nsim0).zfill(5)}_{str(nsimx).zfill(5)}.npz" fname = f"overlap_{str(nsim0).zfill(5)}_{str(nsimx).zfill(5)}.npz"
if smoothed: if smoothed:
fname = fname.replace("overlap", "overlap_smoothed") fname = fname.replace("overlap", "overlap_smoothed")
@ -520,11 +509,11 @@ class Paths:
fdir = join(self.postdir, "environment") fdir = join(self.postdir, "environment")
assert kind in ["density", "velocity", "potential", "radvel", assert kind in ["density", "velocity", "potential", "radvel",
"environment"] "environment"]
if not isdir(fdir): try_create_directory(fdir)
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
if in_rsp: if in_rsp:
kind = kind + "_rsp" kind = kind + "_rsp"
fname = f"{kind}_{MAS}_{str(nsim).zfill(5)}_grid{grid}.npy" fname = f"{kind}_{MAS}_{str(nsim).zfill(5)}_grid{grid}.npy"
if smooth_scale is not None and smooth_scale > 0: if smooth_scale is not None and smooth_scale > 0:
smooth_scale = float(smooth_scale) smooth_scale = float(smooth_scale)
@ -547,9 +536,7 @@ class Paths:
path : str path : str
""" """
fdir = join(self.postdir, "HMF") fdir = join(self.postdir, "HMF")
if not isdir(fdir): try_create_directory(fdir)
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
fname = f"halo_counts_{simname}_{str(nsim).zfill(5)}.npz" fname = f"halo_counts_{simname}_{str(nsim).zfill(5)}.npz"
return join(fdir, fname) return join(fdir, fname)
@ -579,9 +566,8 @@ class Paths:
assert simname in ["csiborg", "quijote"] assert simname in ["csiborg", "quijote"]
assert kind in ["dist", "bin_dist", "tot_counts"] assert kind in ["dist", "bin_dist", "tot_counts"]
fdir = join(self.postdir, "nearest_neighbour") fdir = join(self.postdir, "nearest_neighbour")
if not isdir(fdir): try_create_directory(fdir)
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
if nsim is not None: if nsim is not None:
if simname == "csiborg": if simname == "csiborg":
nsim = str(nsim).zfill(5) nsim = str(nsim).zfill(5)
@ -615,9 +601,8 @@ class Paths:
""" """
assert simname in ["csiborg", "quijote"] assert simname in ["csiborg", "quijote"]
fdir = join(self.postdir, "knn", "auto") fdir = join(self.postdir, "knn", "auto")
if not isdir(fdir): try_create_directory(fdir)
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
if nsim is not None: if nsim is not None:
if simname == "csiborg": if simname == "csiborg":
nsim = str(nsim).zfill(5) nsim = str(nsim).zfill(5)
@ -648,9 +633,8 @@ class Paths:
path : str path : str
""" """
fdir = join(self.postdir, "knn", "cross") fdir = join(self.postdir, "knn", "cross")
if not isdir(fdir): try_create_directory(fdir)
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
if nsims is not None: if nsims is not None:
assert isinstance(nsims, (list, tuple)) and len(nsims) == 2 assert isinstance(nsims, (list, tuple)) and len(nsims) == 2
nsim0 = str(nsims[0]).zfill(5) nsim0 = str(nsims[0]).zfill(5)
@ -680,9 +664,8 @@ class Paths:
path : str path : str
""" """
fdir = join(self.postdir, "tpcf", "auto") fdir = join(self.postdir, "tpcf", "auto")
if not isdir(fdir): try_create_directory(fdir)
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
if nsim is not None: if nsim is not None:
return join(fdir, f"{simname}_tpcf{str(nsim).zfill(5)}_{run}.p") return join(fdir, f"{simname}_tpcf{str(nsim).zfill(5)}_{run}.p")