diff --git a/src/easyreflectometry/data/data_store.py b/src/easyreflectometry/data/data_store.py index fd8d8c70..948382d7 100644 --- a/src/easyreflectometry/data/data_store.py +++ b/src/easyreflectometry/data/data_store.py @@ -79,9 +79,10 @@ def __init__( model: Optional['Model'] = None, # delay type checking until runtime (quotes) x_label: str = 'x', y_label: str = 'y', + auto_background: bool = True, ): self._model = model - if y is not None and model is not None: + if y is not None and model is not None and auto_background: self._model.background = max(np.min(y), 1e-10) if x is None: @@ -123,7 +124,6 @@ def model(self) -> 'Model': # delay type checking until runtime (quotes) @model.setter def model(self, new_model: 'Model') -> None: self._model = new_model - self._model.background = max(np.min(self.y), 1e-10) @property def is_experiment(self) -> bool: diff --git a/src/easyreflectometry/project.py b/src/easyreflectometry/project.py index 56718800..6f7096c1 100644 --- a/src/easyreflectometry/project.py +++ b/src/easyreflectometry/project.py @@ -402,6 +402,12 @@ def _apply_resolution_function( """ model.resolution_function = PercentageFwhm(5.0) + @staticmethod + def _auto_set_background(experiment: DataSet1D) -> None: + """Set the model background to the minimum y-value of the experiment data.""" + if experiment.model is not None and len(experiment.y) > 0: + experiment.model.background = max(np.min(experiment.y), 1e-10) + def load_new_experiment(self, path: Union[Path, str]) -> None: new_experiment = load_as_dataset(str(path)) new_index = len(self._experiments) @@ -412,6 +418,7 @@ def load_new_experiment(self, path: Union[Path, str]) -> None: self._apply_experiment_metadata(path, new_experiment, f'Experiment {new_index}') new_experiment.model = self.models[model_index] + self._auto_set_background(new_experiment) self._experiments[new_index] = new_experiment self._with_experiments = True self._apply_resolution_function(new_experiment, self.models[model_index]) @@ -473,6 +480,7 @@ def load_all_experiments_from_file(self, path: Union[Path, str]) -> int: data_key=data_key, ) new_experiment.model = self.models[model_index] + self._auto_set_background(new_experiment) self._experiments[new_index] = new_experiment self._apply_resolution_function(new_experiment, self.models[model_index]) @@ -484,6 +492,7 @@ def load_experiment_for_model_at_index(self, path: Union[Path, str], index: Opti self._apply_experiment_metadata(path, experiment, f'Experiment {index}') experiment.model = self.models[index] + self._auto_set_background(experiment) self._experiments[index] = experiment self._with_experiments = True self._apply_resolution_function(experiment, self._models[index]) @@ -727,6 +736,7 @@ def _from_dict_extract_experiments(self, project_dict: dict) -> Dict[int, DataSe ye=project_dict['experiments'][key][2], xe=project_dict['experiments'][key][3], model=self._models[project_dict['experiments_models'][key]], + auto_background=False, ) return experiments diff --git a/tests/data/test_data_store.py b/tests/data/test_data_store.py index 84acf4f8..ea66f8ff 100644 --- a/tests/data/test_data_store.py +++ b/tests/data/test_data_store.py @@ -79,16 +79,17 @@ def test_model_property(self): # Then assert data.model == mock_model - def test_model_setter_updates_background(self): + def test_model_setter_does_not_update_background(self): # Given mock_model = Mock() + mock_model.background = 1e-8 # Original background value data = DataSet1D(x=[1, 2, 3, 4], y=[1, 2, 0.5, 3]) # When data.model = mock_model - # Then - assert mock_model.background == 0.5 + # Then - background should NOT be overwritten by model setter + assert mock_model.background == 1e-8 def test_is_experiment_property(self): # Given diff --git a/tests/test_measurement_comprehensive.py b/tests/test_measurement_comprehensive.py index e9bf6ffe..d6bb0c46 100644 --- a/tests/test_measurement_comprehensive.py +++ b/tests/test_measurement_comprehensive.py @@ -189,14 +189,15 @@ def test_data_points_iterator(self): expected = [(1, 10, 1, 0.1), (2, 20, 2, 0.2), (3, 30, 3, 0.3)] assert points == expected - def test_model_property_with_background_setting(self): - """Test that setting model updates background to minimum y value.""" + def test_model_property_setter_does_not_update_background(self): + """Test that setting model via setter does not overwrite background.""" dataset = DataSet1D(x=[1, 2, 3, 4], y=[5, 1, 8, 3]) mock_model = Mock() + mock_model.background = 1e-8 # Original value dataset.model = mock_model - assert mock_model.background == 1 # minimum of [5, 1, 8, 3] + assert mock_model.background == 1e-8 # background should NOT be changed by setter def test_repr_string_representation(self): """Test the string representation of DataSet1D."""