From d140c1572d3ab891231ab9cd2a9dbc5bc50a158b Mon Sep 17 00:00:00 2001 From: Ryan Lyttle Date: Fri, 10 Jan 2025 15:44:18 +0000 Subject: [PATCH 1/3] Made n_sub_intervals into a config class attribute Made n_sub_intervals an attribute for simulationConfigs and updated unit tests to include it. --- src/sorcha/ephemeris/simulation_driver.py | 4 +++- src/sorcha/utilities/sorchaConfigs.py | 4 ++++ tests/sorcha/test_sorchaConfigs.py | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sorcha/ephemeris/simulation_driver.py b/src/sorcha/ephemeris/simulation_driver.py index cb742c92..12e2b5f7 100644 --- a/src/sorcha/ephemeris/simulation_driver.py +++ b/src/sorcha/ephemeris/simulation_driver.py @@ -80,6 +80,8 @@ def create_ephemeris(orbits_df, pointings_df, args, sconfigs): nside : integer The nside value used for the HEALPIx calculations. Must be a power of 2 (1, 2, 4, ...) nside=64 is current default. + n_sub_intervals: int + Number of sub-intervals for the Lagrange interpolation (default: 101) Returns ------- @@ -109,7 +111,7 @@ def create_ephemeris(orbits_df, pointings_df, args, sconfigs): picket_interval = sconfigs.simulation.ar_picket obsCode = sconfigs.simulation.ar_obs_code nside = 2**sconfigs.simulation.ar_healpix_order - n_sub_intervals = 101 # configs["n_sub_intervals"] + n_sub_intervals = sconfigs.simulation.n_sub_intervals ephemeris_csv_filename = None if args.output_ephemeris_file and args.outpath: diff --git a/src/sorcha/utilities/sorchaConfigs.py b/src/sorcha/utilities/sorchaConfigs.py index db9c1e74..f7070d0c 100644 --- a/src/sorcha/utilities/sorchaConfigs.py +++ b/src/sorcha/utilities/sorchaConfigs.py @@ -77,6 +77,9 @@ class simulationConfigs: ar_healpix_order: int = None """the order of healpix which we will use for the healpy portions of the code.""" + n_sub_intervals: int = 101 + """Number of sub-intervals for the Lagrange ephemerides interpolation (default: 101)""" + _ephemerides_type: str = None """Simulation used for ephemeris input.""" @@ -111,6 +114,7 @@ def _validate_simulation_configs(self): self.ar_fov_buffer = cast_as_float(self.ar_fov_buffer, "ar_fov_buffer") self.ar_picket = cast_as_int(self.ar_picket, "ar_picket") self.ar_healpix_order = cast_as_int(self.ar_healpix_order, "ar_healpix_order") + self.n_sub_intervals = cast_as_int(self.n_sub_intervals, "n_sub_intervals") elif self._ephemerides_type == "external": # makes sure when these are not needed that they are not populated check_key_doesnt_exist(self.ar_ang_fov, "ar_ang_fov", "but ephemerides type is external") diff --git a/tests/sorcha/test_sorchaConfigs.py b/tests/sorcha/test_sorchaConfigs.py index 4d6f8bb3..7242330c 100644 --- a/tests/sorcha/test_sorchaConfigs.py +++ b/tests/sorcha/test_sorchaConfigs.py @@ -35,6 +35,7 @@ "ar_picket": 1, "ar_obs_code": "X05", "ar_healpix_order": 6, + "n_sub_intervals": 101 } correct_filters_read = {"observing_filters": "r,g,i,z,u,y", "survey_name": "rubin_sim"} @@ -261,7 +262,7 @@ def test_simulationConfigs_float(key_name): ) -@pytest.mark.parametrize("key_name", ["ar_picket", "ar_healpix_order"]) +@pytest.mark.parametrize("key_name", ["ar_picket", "ar_healpix_order","n_sub_intervals"]) def test_simulationConfigs_int(key_name): """ Tests that wrong inputs for simulationConfigs int attributes is caught correctly From b61130208574f048ac751788228e10eb531d6723 Mon Sep 17 00:00:00 2001 From: Ryan Lyttle Date: Fri, 10 Jan 2025 15:54:21 +0000 Subject: [PATCH 2/3] Update sorchaConfigs.py --- src/sorcha/utilities/sorchaConfigs.py | 37 +++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/sorcha/utilities/sorchaConfigs.py b/src/sorcha/utilities/sorchaConfigs.py index f7070d0c..a298ccc1 100644 --- a/src/sorcha/utilities/sorchaConfigs.py +++ b/src/sorcha/utilities/sorchaConfigs.py @@ -77,7 +77,7 @@ class simulationConfigs: ar_healpix_order: int = None """the order of healpix which we will use for the healpy portions of the code.""" - n_sub_intervals: int = 101 + n_sub_intervals: int = None """Number of sub-intervals for the Lagrange ephemerides interpolation (default: 101)""" _ephemerides_type: str = None @@ -114,7 +114,7 @@ def _validate_simulation_configs(self): self.ar_fov_buffer = cast_as_float(self.ar_fov_buffer, "ar_fov_buffer") self.ar_picket = cast_as_int(self.ar_picket, "ar_picket") self.ar_healpix_order = cast_as_int(self.ar_healpix_order, "ar_healpix_order") - self.n_sub_intervals = cast_as_int(self.n_sub_intervals, "n_sub_intervals") + self.n_sub_intervals = cast_as_int_or_set_default(self.n_sub_intervals, "n_sub_intervals", 101) elif self._ephemerides_type == "external": # makes sure when these are not needed that they are not populated check_key_doesnt_exist(self.ar_ang_fov, "ar_ang_fov", "but ephemerides type is external") @@ -1311,6 +1311,39 @@ def cast_as_bool_or_set_default(value, key, default): return default +def cast_as_int_or_set_default(value, key, default): + + # replaces PPGetBoolOrExit: checks to make sure the value can be cast as a bool. + """ + Checks to see if value can be cast as an int and if not set (equals None) gives default int. + + Parameters + ----------- + value : object attribute + value of the config file attribute + + key : string + The key being checked. + + default : int + default int if value is None + + Returns + ---------- + value as an int + """ + + if value is not None: + try: + int(value) + except ValueError: + logging.error(f"ERROR: expected an int for config parameter {key}. Check value in config file.") + sys.exit(f"ERROR: expected an int for config parameter {key}. Check value in config file.") + return value + elif value is None: + return default + + def PrintConfigsToLog(sconfigs, cmd_args): """ Prints all the values from the config file and command line to the log. From 81c25f9ad60cdff7dcc4e13ff8d079db0ca403c9 Mon Sep 17 00:00:00 2001 From: Ryan Lyttle Date: Fri, 10 Jan 2025 18:15:30 +0000 Subject: [PATCH 3/3] Renamed n_sub_intervals and changed how its default is set Renamed n_sub_intervals to ar_n_sub_intervals and set it's default value at the start instead of None. Also added the parameter to the log. --- src/sorcha/ephemeris/simulation_driver.py | 2 +- src/sorcha/utilities/sorchaConfigs.py | 38 ++--------------------- tests/data/test_PrintConfigsToLog.txt | 1 + tests/sorcha/test_sorchaConfigs.py | 4 +-- 4 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/sorcha/ephemeris/simulation_driver.py b/src/sorcha/ephemeris/simulation_driver.py index 12e2b5f7..0417522d 100644 --- a/src/sorcha/ephemeris/simulation_driver.py +++ b/src/sorcha/ephemeris/simulation_driver.py @@ -111,7 +111,7 @@ def create_ephemeris(orbits_df, pointings_df, args, sconfigs): picket_interval = sconfigs.simulation.ar_picket obsCode = sconfigs.simulation.ar_obs_code nside = 2**sconfigs.simulation.ar_healpix_order - n_sub_intervals = sconfigs.simulation.n_sub_intervals + n_sub_intervals = sconfigs.simulation.ar_n_sub_intervals ephemeris_csv_filename = None if args.output_ephemeris_file and args.outpath: diff --git a/src/sorcha/utilities/sorchaConfigs.py b/src/sorcha/utilities/sorchaConfigs.py index a298ccc1..e138d559 100644 --- a/src/sorcha/utilities/sorchaConfigs.py +++ b/src/sorcha/utilities/sorchaConfigs.py @@ -77,7 +77,7 @@ class simulationConfigs: ar_healpix_order: int = None """the order of healpix which we will use for the healpy portions of the code.""" - n_sub_intervals: int = None + ar_n_sub_intervals: int = 101 """Number of sub-intervals for the Lagrange ephemerides interpolation (default: 101)""" _ephemerides_type: str = None @@ -114,7 +114,7 @@ def _validate_simulation_configs(self): self.ar_fov_buffer = cast_as_float(self.ar_fov_buffer, "ar_fov_buffer") self.ar_picket = cast_as_int(self.ar_picket, "ar_picket") self.ar_healpix_order = cast_as_int(self.ar_healpix_order, "ar_healpix_order") - self.n_sub_intervals = cast_as_int_or_set_default(self.n_sub_intervals, "n_sub_intervals", 101) + self.ar_n_sub_intervals = cast_as_int(self.ar_n_sub_intervals, "ar_n_sub_intervals") elif self._ephemerides_type == "external": # makes sure when these are not needed that they are not populated check_key_doesnt_exist(self.ar_ang_fov, "ar_ang_fov", "but ephemerides type is external") @@ -1311,39 +1311,6 @@ def cast_as_bool_or_set_default(value, key, default): return default -def cast_as_int_or_set_default(value, key, default): - - # replaces PPGetBoolOrExit: checks to make sure the value can be cast as a bool. - """ - Checks to see if value can be cast as an int and if not set (equals None) gives default int. - - Parameters - ----------- - value : object attribute - value of the config file attribute - - key : string - The key being checked. - - default : int - default int if value is None - - Returns - ---------- - value as an int - """ - - if value is not None: - try: - int(value) - except ValueError: - logging.error(f"ERROR: expected an int for config parameter {key}. Check value in config file.") - sys.exit(f"ERROR: expected an int for config parameter {key}. Check value in config file.") - return value - elif value is None: - return default - - def PrintConfigsToLog(sconfigs, cmd_args): """ Prints all the values from the config file and command line to the log. @@ -1549,6 +1516,7 @@ def PrintConfigsToLog(sconfigs, cmd_args): pplogger.info("...the picket interval is: " + str(sconfigs.simulation.ar_picket)) pplogger.info("...the observatory code is: " + str(sconfigs.simulation.ar_obs_code)) pplogger.info("...the healpix order is: " + str(sconfigs.simulation.ar_healpix_order)) + pplogger.info("...the number of sub-intervals is: " + str(sconfigs.simulation.ar_n_sub_intervals)) else: pplogger.info("ASSIST+REBOUND Simulation is turned OFF.") diff --git a/tests/data/test_PrintConfigsToLog.txt b/tests/data/test_PrintConfigsToLog.txt index 49f5cf28..ad619826 100644 --- a/tests/data/test_PrintConfigsToLog.txt +++ b/tests/data/test_PrintConfigsToLog.txt @@ -224,6 +224,7 @@ sorcha.utilities.sorchaConfigs INFO ...the buffer around the FOV is: 0.2 sorcha.utilities.sorchaConfigs INFO ...the picket interval is: 1 sorcha.utilities.sorchaConfigs INFO ...the observatory code is: X05 sorcha.utilities.sorchaConfigs INFO ...the healpix order is: 6 +sorcha.utilities.sorchaConfigs INFO ...the number of sub-intervals is: 101 sorcha.utilities.sorchaConfigs INFO No lightcurve model is being applied. sorcha.utilities.sorchaConfigs INFO Output files will be saved in path: ./ with filestem testout sorcha.utilities.sorchaConfigs INFO Output files will be saved as format: csv diff --git a/tests/sorcha/test_sorchaConfigs.py b/tests/sorcha/test_sorchaConfigs.py index 7242330c..59bfcde7 100644 --- a/tests/sorcha/test_sorchaConfigs.py +++ b/tests/sorcha/test_sorchaConfigs.py @@ -35,7 +35,7 @@ "ar_picket": 1, "ar_obs_code": "X05", "ar_healpix_order": 6, - "n_sub_intervals": 101 + "ar_n_sub_intervals": 101 } correct_filters_read = {"observing_filters": "r,g,i,z,u,y", "survey_name": "rubin_sim"} @@ -262,7 +262,7 @@ def test_simulationConfigs_float(key_name): ) -@pytest.mark.parametrize("key_name", ["ar_picket", "ar_healpix_order","n_sub_intervals"]) +@pytest.mark.parametrize("key_name", ["ar_picket", "ar_healpix_order","ar_n_sub_intervals"]) def test_simulationConfigs_int(key_name): """ Tests that wrong inputs for simulationConfigs int attributes is caught correctly