Skip to content

Commit

Permalink
Merge pull request #37 from LSSTDESC/issue/36/create-unlinked-run-dir…
Browse files Browse the repository at this point in the history
…ectory

Fixed model error and changed code to set run without symlink
  • Loading branch information
raphaelshirley authored May 14, 2024
2 parents 0f8818d + 52ded5c commit b0c0c8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
44 changes: 26 additions & 18 deletions src/rail/estimation/algos/lephare.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ class LephareEstimator(CatEstimator):
"autoadapt will be run if that key is set in the config."
),
),
run_dir=Param(
str,
"None",
msg=(
"Override for the LEPHAREWORK directory. If None we load it "
"from the model which is set during the inform stage. This "
"is to facilitate manually moving intermediate files."
),
),
)

def __init__(self, args, comm=None):
Expand All @@ -178,21 +187,16 @@ def __init__(self, args, comm=None):
self.zmin = float(Z_STEP.split(",")[1])
self.zmax = float(Z_STEP.split(",")[2])
self.nzbins = int((self.zmax - self.zmin) / self.zstep)
self.run_dir = self.model["run_dir"]
_update_lephare_env(None, self.run_dir)

def _estimate_pdf(self, onesource):
"""Return the pdf of a single source.
CatEstimator.open_model(self, **self.config)

Do we want to resample on RAIL z grid?
"""
# Check this is the best way to access pdf
pdf = onesource.pdfmap[11] # 11 = Bayesian galaxy redshift
# return the PDF as an array alongside lephare native zgrid
return np.array(pdf.vPDF), np.array(pdf.xaxis)
if self.config["run_dir"] == "None":
self.run_dir = self.model["run_dir"]
else:
self.run_dir = self.config["run_dir"]
_update_lephare_env(None, self.run_dir)

def _process_chunk(self, start, end, data, first):
"""Process an individual chunk of sources using lephare
"""Process an individual chunk of sources using lephare.
Run the equivalent of zphota and get the PDF for every source.
"""
Expand Down Expand Up @@ -279,7 +283,7 @@ def _update_lephare_env(lepharedir, lepharework):
importlib.reload(lp)


def _set_run_dir(name=None):
def _set_run_dir(name=None, full_path=None):
"""Create a named run if it doesn't exist otherwise set it to existing.
lephare has the functionality to set a timed or named run. In general we
Expand All @@ -289,11 +293,15 @@ def _set_run_dir(name=None):
Parameters
==========
name : str
The name to set the run. We may want to use timestamped runs
The name to set the run. If not set we use a default timestamped run.
full_path : str
If set we create a run directory wherever the user sets it.
"""
try:
run_directory = lp.dm.create_new_run(descriptive_directory_name=name)
except FileExistsError:
if name is None:
run_directory = lp.dm.create_new_run()
elif full_path:
run_directory = full_path
else:
run_directory = os.path.realpath(f"{lp.dm.lephare_work_dir}/../{name}")
_update_lephare_env(lp.LEPHAREDIR, run_directory)
_update_lephare_env(lp.LEPHAREDIR, run_directory)
return run_directory
6 changes: 5 additions & 1 deletion tests/lephare/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ def test_informer_and_estimator(test_data_dir: str):
testFile = os.path.join(test_data_dir, "output_table_conv_test.hdf5")
traindata_io = tables_io.read(trainFile)
testdata_io = tables_io.read(testFile)
# Load the test params with a sparse redshift grid
lephare_config_file = os.path.join(test_data_dir, "lsst.para")
lephare_config = lp.read_config(lephare_config_file)
lp.data_retrieval.get_auxiliary_data(keymap=lephare_config)
lp.data_retrieval.get_auxiliary_data(
keymap=lephare_config, additional_files=["examples/output.para"]
)

inform_lephare = LephareInformer.make_stage(
name="inform_Lephare",
nondetect_val=np.nan,
model="lephare.pkl",
hdf5_groupname="",
lephare_config=lephare_config,
)

inform_lephare.inform(traindata_io)
Expand Down

0 comments on commit b0c0c8e

Please sign in to comment.