Skip to content

Commit 7d1775d

Browse files
SBouchard01pre-commit-ci[bot]lgarrison
authored
AbacusHOD: Moving mock dir creation to run_hod (#187)
* Moved mock_dir.mkdir to run_hod with a condition on write_to_disk (avoid creating empty folder) * Added a check in staging to make sure the subsample directory exists, and return an explicit error otherwise to avoid an "out of range" IndexError instead * Added check of the simulation directory (same as the subsample directory one, to make the raised error understandable) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove mkdir (duplicate of makedirs in gen_gal_cat) * hod: only try to create dirs once avoid repeated stat * hod: dir creation condition * pathlib --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lehman Garrison <lgarrison@flatironinstitute.org>
1 parent bb4de66 commit 7d1775d

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
Upcoming
5+
--------
6+
7+
Improvements
8+
~~~~~~~~~~~~
9+
- AbacusHOD: avoid unnecessary mock dir creation [#187]
10+
411
2.1.1 (2025-07-25)
512
------------------
613

abacusnbody/hod/GRAND_HOD.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
import os
3+
from pathlib import Path
34
import time
45
import warnings
56

@@ -1671,6 +1672,21 @@ def gen_gal_cat(
16711672
NFW_draw,
16721673
)
16731674

1675+
if write_to_disk and tracers:
1676+
if rsd:
1677+
rsd_string = '_rsd'
1678+
else:
1679+
rsd_string = ''
1680+
1681+
savedir = Path(savedir)
1682+
if fn_ext is None:
1683+
outdir = (savedir) / ('galaxies' + rsd_string)
1684+
else:
1685+
outdir = (savedir) / ('galaxies' + rsd_string + fn_ext)
1686+
1687+
# create directories if not existing
1688+
os.makedirs(outdir, exist_ok=True)
1689+
16741690
# how many galaxies were generated and write them to disk
16751691
for tracer in tracers.keys():
16761692
Ncent = HOD_dict[tracer]['Ncent']
@@ -1686,19 +1702,6 @@ def gen_gal_cat(
16861702
if verbose:
16871703
print('outputting galaxies to disk')
16881704

1689-
if rsd:
1690-
rsd_string = '_rsd'
1691-
else:
1692-
rsd_string = ''
1693-
1694-
if fn_ext is None:
1695-
outdir = (savedir) / ('galaxies' + rsd_string)
1696-
else:
1697-
outdir = (savedir) / ('galaxies' + rsd_string + fn_ext)
1698-
1699-
# create directories if not existing
1700-
os.makedirs(outdir, exist_ok=True)
1701-
17021705
# save to file
17031706
# outdict =
17041707
HOD_dict[tracer].pop('Ncent', None)

abacusnbody/hod/abacus_hod.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,18 @@ def staging(self):
258258
simname = Path(self.sim_name)
259259
sim_dir = Path(self.sim_dir)
260260
mock_dir = output_dir / simname / ('z%4.3f' % self.z_mock)
261-
# create mock_dir if not created
262-
mock_dir.mkdir(parents=True, exist_ok=True)
263261
subsample_dir = Path(self.subsample_dir) / simname / ('z%4.3f' % self.z_mock)
264262

263+
# Check if the simulation directory exists
264+
if not (sim_dir / simname).exists():
265+
raise FileNotFoundError(
266+
f'Simulation directory {sim_dir / simname} not found.'
267+
)
268+
269+
# Check if the subsample directory exists
270+
if not subsample_dir.exists():
271+
raise FileNotFoundError(f'Subsample directory {subsample_dir} not found.')
272+
265273
# load header to read parameters
266274
if self.halo_lc:
267275
halo_info_fns = [

0 commit comments

Comments
 (0)