From 71a02fbc975789bea4a7ddd104deaa054e9f6ba8 Mon Sep 17 00:00:00 2001 From: Mateusz Mroz Date: Mon, 13 May 2024 18:53:49 +0200 Subject: [PATCH] like previous --- examples/example_16/ob03235_2_full.yaml | 6 +-- examples/example_16/ulens_model_fit.py | 54 +++++++++++++++---------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/examples/example_16/ob03235_2_full.yaml b/examples/example_16/ob03235_2_full.yaml index c5bb512c..59b38c9a 100644 --- a/examples/example_16/ob03235_2_full.yaml +++ b/examples/example_16/ob03235_2_full.yaml @@ -33,13 +33,13 @@ starting_parameters: s: uniform 1.09 1.11 alpha: gauss 224.0 1.0 fit_constraints: - negative_blending_flux_sigma_mag: 20. + negative_blending_flux_sigma_mag: 20. #one can specify for which dataset soft constrain on blending flux should be applied: sigma dataset_number(s) - #negative_blending_flux_sigma_mag: 20. 1 3 + #negative_blending_flux_sigma_mag: 20. "OGLE I-band" 1 # Alternative sharp constraint: # no_negative_blending_flux: True #color constrains, where color=flux_S_dataset_k/flux_S_dataset_m: gauss mu sigma k m - color : gauss 25. 5. "OGLE I-band" "OB03235_MOA.txt" + color : gauss 25. 5. "OGLE I-band" 1 #Alternative for binary source models: #color source 1 : gauss 25. 5. "OGLE I-band" "OB03235_MOA.txt" #and/or diff --git a/examples/example_16/ulens_model_fit.py b/examples/example_16/ulens_model_fit.py index 66eed00b..6b9b1fb7 100644 --- a/examples/example_16/ulens_model_fit.py +++ b/examples/example_16/ulens_model_fit.py @@ -1483,12 +1483,15 @@ def _parse_fit_constraints_soft_blending(self, key, value): """ Check if soft fit constraint on blending flux are correctly defined. """ - if isinstance(value, (float, int)): + if isinstance(value, float): sigma = float(value) - sets = list(range(1, len(self._datasets) + 1)) + sets = list(range(len(self._datasets))) + else: + sigma = float(value.split()[0]) - sets = list(map(int, value.split()[1:])) + sets = list(map(self._get_no_of_dataset, + shlex.split(value, posix=False)[1:])) if len(sets) > len(self._datasets): raise ValueError( 'dataset number specified in negative_blending_flux_sigma_mag do not match with provided datasets') @@ -1515,20 +1518,17 @@ def _parse_fit_constraints_color(self, key, value): words[2] + " " + words[3] + " " + words[4]) if settings[2] < 0.: raise ValueError('sigma cannot be negative: ' + words[2]) + + settings[3] = self._get_no_of_dataset(settings[3]) + settings[4] = self._get_no_of_dataset(settings[4]) + if settings[3] == settings[4]: raise ValueError( "in " + key + " fluxes have to be from different datasets") - if isinstance(settings[3], str) and isinstance(settings[4], str): - settings[3] = settings[3].strip('"') - settings[4] = settings[4].strip('"') - settings[3] = self._get_no_of_dataset_by_lable(settings[3]) - settings[4] = self._get_no_of_dataset_by_lable(settings[4]) - - if isinstance(settings[3], int) and isinstance(settings[4], int): - if (0 >= settings[3] >= len(self._datasets)-1) or (0 >= settings[4] >= len(self._datasets)-1): - raise ValueError( - 'dataset specified in color prior do not match with provided datasets') + if (0 >= settings[3] >= len(self._datasets)-1) or (0 >= settings[4] >= len(self._datasets)-1): + raise ValueError( + 'dataset specified in color prior do not match with provided datasets') self._fit_constraints[key] = settings @@ -1569,24 +1569,36 @@ def _parse_fit_constraints_prior(self): if len(priors) > 0: self._priors = priors - def _get_no_of_dataset_by_lable(self, label): + def _get_no_of_dataset(self, label): """ Returns the index of a dataset with a specific label. Parameters ---------- label : str - Label of the dataset defined by MulensData.plot_properties['label']. + Label of the dataset defined by MulensData.plot_properties['label'] + or sequential index of the dataset Returns ------- int - Sequential index of the dataset [1,2,...,n_datasets] + Sequential index of the dataset from [0,1,...,n_datasets-1] + """ - for (i, dataset) in enumerate(self._datasets): - if dataset.plot_properties['label'] == label: - return i - return -99 + if '"' in label: + label = label.strip('"') + + try: + ind = int(label) + if 0 <= ind <= len(self._datasets)-1: + return ind + except: + for (i, dataset) in enumerate(self._datasets): + + if dataset.plot_properties['label'] == label: + return i + raise KeyError( + "Unrecognized dataset lable in fit_constraints/prior: " + label) def _read_prior_t_E_data(self): """ @@ -2244,7 +2256,7 @@ def _run_flux_checks_ln_prior(self, fluxes): if key in self._fit_constraints: for (i, dataset) in enumerate(self._datasets): - if i+1 in self._fit_constraints[key][1]: + if i in self._fit_constraints[key][1]: blend_index = ((i+1)*self._n_fluxes_per_dataset) - 1 if fluxes[blend_index] < 0.: sigma = self._fit_constraints[key][0]