diff --git a/beast/physicsmodel/model_grid.py b/beast/physicsmodel/model_grid.py index 22b1980e..9a03e66f 100644 --- a/beast/physicsmodel/model_grid.py +++ b/beast/physicsmodel/model_grid.py @@ -165,7 +165,10 @@ def make_spectral_grid( distance: float or list of float distances at which models should be shifted, specified as a - single number or as [min, max, step] + single number, as [min, max, step], or as an explicit list of + more than three values giving a (possibly non-uniform) grid. + An explicit grid of exactly three values cannot be expressed, + since three values are always read as [min, max, step]. 0 means absolute magnitude. @@ -226,14 +229,24 @@ def make_spectral_grid( # Construct the distances array. Turn single value into # 1-element list if single distance is given. + # A list of >3 values is used as an explicit (possibly non-uniform) + # distance grid, e.g. for variable-resolution distance sampling. + # NOTE: an explicit grid of exactly 3 values cannot be expressed + # (3 values are always interpreted as min, max, step) -- pad with a + # duplicate-free 4th value in that case. _distance = np.atleast_1d(distance) if len(_distance) == 3: mindist, maxdist, stepdist = _distance distances = np.arange(mindist, maxdist + stepdist, stepdist) elif len(_distance) == 1: distances = np.array(_distance) + elif len(_distance) > 3: + distances = np.sort(np.array(_distance, dtype=float)) else: - raise ValueError("distance needs to be (min, max, step) or single number") + raise ValueError( + "distance needs to be (min, max, step), a single number, " + "or an explicit list of >3 values" + ) # calculate the distances in pc if distance_unit == units.mag: diff --git a/beast/tools/tests/test_verify_beast_settings.py b/beast/tools/tests/test_verify_beast_settings.py index 75f8fc2b..ae43466f 100644 --- a/beast/tools/tests/test_verify_beast_settings.py +++ b/beast/tools/tests/test_verify_beast_settings.py @@ -20,6 +20,8 @@ class settings_mock: rv_prior_model = {"name": "flat"} fAs = [0.0, 1.0, 0.25] fA_prior_model = {"name": "flat"} + distances = [50.0, 70.0, 5.0] + distance_prior_model = {"name": "flat"} oiso = isochrone.MISTWeb() @@ -93,3 +95,128 @@ def test_verifyparams_allowwarnRV(): """Test: verify_beast_settings for case of warning with no exception.""" with pytest.warns(UserWarning, match="Note: rvs grid is single-valued."): verify_beast_settings.verify_input_format(settings_mock_allowwarnRV()) + + +class settings_mock_dist_explicit(settings_mock): + """Mock beast_settings w/ an explicit, non-uniform distance grid (>3 values)""" + + distances = [55.0, 58.0, 60.0, 61.0, 62.0, 63.0, 65.0, 70.0] + + +class settings_mock_dist_single(settings_mock): + """Mock beast_settings w/ a single distance""" + + distances = [60.0] + + +class settings_mock_dist_bare(settings_mock): + """Mock beast_settings w/ a single distance given as a bare number""" + + distances = 60.0 + + +class settings_mock_dist_int(settings_mock): + """Mock beast_settings w/ an integer distance grid""" + + distances = [50, 70, 5] + + +class settings_mock_dist_two(settings_mock): + """Mock beast_settings w/ an unusable two-element distance list""" + + distances = [50.0, 70.0] + + +class settings_mock_dist_dup(settings_mock): + """Mock beast_settings w/ duplicate values in an explicit distance grid""" + + distances = [55.0, 60.0, 60.0, 65.0, 70.0] + + +class settings_mock_dist_negative(settings_mock): + """Mock beast_settings w/ an unphysical negative distance""" + + distances = [-10.0, 55.0, 60.0, 65.0] + + +class settings_mock_dist_str(settings_mock): + """Mock beast_settings w/ a non-numeric entry in the distance grid""" + + distances = [55.0, 60.0, "65.0", 70.0] + + +class settings_mock_dist_none(settings_mock): + """Mock beast_settings w/ distances undefined""" + + distances = None + + +class settings_mock_dist_none_allowwarn(settings_mock_dist_none): + """Mock beast_settings w/ distances undefined and allow_warnings = True""" + + allow_warnings = True + + +def test_verifyparams_dist_explicit(): + """Test: an explicit >3-value distance grid passes verification.""" + with warnings.catch_warnings(record=True) as record: + verify_beast_settings.verify_input_format(settings_mock_dist_explicit()) + assert len(record) == 0 + + +def test_verifyparams_dist_single(): + """Test: a single-element distance list passes verification.""" + with warnings.catch_warnings(record=True) as record: + verify_beast_settings.verify_input_format(settings_mock_dist_single()) + assert len(record) == 0 + + +def test_verifyparams_dist_bare(): + """Test: a bare numeric distance passes verification.""" + with warnings.catch_warnings(record=True) as record: + verify_beast_settings.verify_input_format(settings_mock_dist_bare()) + assert len(record) == 0 + + +def test_verifyparams_dist_int(): + """Test: an integer-valued distance grid passes verification.""" + with warnings.catch_warnings(record=True) as record: + verify_beast_settings.verify_input_format(settings_mock_dist_int()) + assert len(record) == 0 + + +def test_verifyparams_dist_two(): + """Test: a two-element distance list is rejected.""" + with pytest.raises(ValueError, match="distances needs to be a single number"): + verify_beast_settings.verify_input_format(settings_mock_dist_two()) + + +def test_verifyparams_dist_duplicates(): + """Test: duplicate values in an explicit distance grid are rejected.""" + with pytest.raises(ValueError, match="distances explicit grid has duplicate"): + verify_beast_settings.verify_input_format(settings_mock_dist_dup()) + + +def test_verifyparams_dist_negative(): + """Test: a negative distance is rejected.""" + with pytest.raises(ValueError, match="distances min value not physical."): + verify_beast_settings.verify_input_format(settings_mock_dist_negative()) + + +def test_verifyparams_dist_str(): + """Test: a non-numeric distance entry is rejected.""" + with pytest.raises(TypeError, match="distances is not in the right format"): + verify_beast_settings.verify_input_format(settings_mock_dist_str()) + + +def test_verifyparams_dist_none(): + """Test: undefined distances raises, as the warning is turned into an error.""" + with pytest.raises(UserWarning) as exc: + verify_beast_settings.verify_input_format(settings_mock_dist_none()) + assert exc.value.args[0] == "distances is not defined." + + +def test_verifyparams_dist_none_allowwarn(): + """Test: undefined distances only warns when allow_warnings is set.""" + with pytest.warns(UserWarning, match="distances is not defined."): + verify_beast_settings.verify_input_format(settings_mock_dist_none_allowwarn()) diff --git a/beast/tools/verify_beast_settings.py b/beast/tools/verify_beast_settings.py index c97ab2d9..b84c53bd 100644 --- a/beast/tools/verify_beast_settings.py +++ b/beast/tools/verify_beast_settings.py @@ -44,6 +44,38 @@ def check_grid(param, param_name, param_lim): raise ValueError(param_name + " step value greater than (max-min)") +def check_distance(param, param_name, param_lim): + # check the distance grid, which make_spectral_grid accepts in three forms: + # [fixed number] a single distance + # [min, max, step] a uniform grid, built with np.arange + # [d0, d1, ..., dn], n + 1 > 3 an explicit, possibly non-uniform grid + # Three values are always read as [min, max, step], so an explicit grid + # must carry more than three values. Keep this in step with the parsing + # in beast.physicsmodel.model_grid.make_spectral_grid. + + # ints are accepted alongside floats here: a distance written as [60] or + # [50, 70, 5] is unambiguous and works downstream, so rejecting it would + # break settings files that run correctly today. + if not all( + isinstance(item, (int, float)) and not isinstance(item, bool) for item in param + ): + raise TypeError(param_name + " is not in the right format - list of floats.") + + if len(param) == 1: + verify_range(param, param_name, param_lim) + elif len(param) == 3: + check_grid(param, param_name, param_lim) + elif len(param) > 3: + verify_range(param, param_name, param_lim) + if len(set(param)) != len(param): + raise ValueError(param_name + " explicit grid has duplicate values.") + else: + raise ValueError( + param_name + " needs to be a single number, [min, max, step], or an" + " explicit list of more than three values." + ) + + def verify_one_input_format(param, param_name, param_format, param_lim): """ Test for an input parameter correctness of format and limits. @@ -65,6 +97,18 @@ def verify_one_input_format(param, param_name, param_format, param_lim): """ + if "distance" in param_format: + # a single distance may be given as a bare number rather than a list + if param is None: + warnings.warn(param_name + " is not defined.") + elif isinstance(param, (int, float)) and not isinstance(param, bool): + check_distance([param], param_name, param_lim) + elif isinstance(param, (list, tuple)): + check_distance(list(param), param_name, param_lim) + else: + raise TypeError(param_name + " is not in the right format - a list.") + return + if "list" in param_format: if not isinstance(param, list): if param is None: @@ -140,8 +184,9 @@ def verify_input_format(settings): settings.avs, settings.rvs, settings.fAs, + getattr(settings, "distances", None), ] - parameters_names = ["z", "obsfile", "astfile", "avs", "rvs", "fAs"] + parameters_names = ["z", "obsfile", "astfile", "avs", "rvs", "fAs", "distances"] param_format = [ "list_float", "str_file", @@ -149,6 +194,7 @@ def verify_input_format(settings): "list_float_grid", "list_float_grid", "list_float_grid", + "list_float_distance", ] print(settings.oiso.name) @@ -161,6 +207,7 @@ def verify_input_format(settings): [0.0, inf], [1.0, 7.0], [0.0, 1.0], + [0.0, inf], ] params_extra = [settings.logt] @@ -176,6 +223,7 @@ def verify_input_format(settings): [0.0, inf], [1.0, 7.0], [0.0, 1.0], + [0.0, inf], ] params_extra = [settings.logt] params_extra_names = ["logt"] @@ -190,6 +238,7 @@ def verify_input_format(settings): [0.0, inf], [1.0, 7.0], [0.0, 1.0], + [0.0, inf], ] params_extra = [settings.logmass] diff --git a/docs/beast_setup.rst b/docs/beast_setup.rst index ee26bddb..eea02146 100644 --- a/docs/beast_setup.rst +++ b/docs/beast_setup.rst @@ -51,7 +51,10 @@ General Parameters * ``n_subgrid``: number of sub-grids to use (1 means no subgrids), useful for when the physics model grid is too large to read into memory. * ``velocity`` : heliocentric velocity of a galaxy (e.g., -300 km/s for M31). -* ``distances``: distance grid range parameters. ``[min, max, step]``, or ``[fixed number]``. +* ``distances``: distance grid range parameters. ``[min, max, step]``, ``[fixed number]``, + or an explicit list of more than three values for a non-uniform grid (e.g. sampling + concentrated near the target distance). A list of exactly three values is always + interpreted as ``[min, max, step]``. * ``distance_unit``: specify magnitude (``units.mag``) or a length unit. * ``distance_prior_model``: specify a prior for distance parameter.