From 0286c6e37e5f675bcfb7c7d4cc6cf75483dc4a58 Mon Sep 17 00:00:00 2001 From: Andreas Pedersen Date: Thu, 21 Nov 2024 11:29:54 +0100 Subject: [PATCH] count functionality back to utils --- src/easyreflectometry/project.py | 21 ------------ src/easyreflectometry/summary/summary.py | 9 +++-- src/easyreflectometry/utils.py | 12 +++++++ tests/test_project.py | 36 -------------------- tests/test_utils.py | 43 ++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 60 deletions(-) create mode 100644 tests/test_utils.py diff --git a/src/easyreflectometry/project.py b/src/easyreflectometry/project.py index a5ba11b3..4b3ee991 100644 --- a/src/easyreflectometry/project.py +++ b/src/easyreflectometry/project.py @@ -450,24 +450,3 @@ def _replace_collection(self, src_collection: BaseCollection, dst_collection: Ba def _timestamp_modification(self): self._info['modified'] = datetime.datetime.now().strftime('%d.%m.%Y %H:%M') - - def free_parameters_count(self) -> int: - count = 0 - # parameters = self.parameters - # no_parameters = len(self.parameters) - for i in range(len(self.parameters)): - if not self.parameters[i]._fixed: - count = count + 1 - # parameters[0].free - # for parameter in parameters: - # # if not parameter._fixed: - # count = count + 1 - return count - - # return sum(1 for parameter in self.parameters if parameter.free) - - # def fixed_parameters_count(self) -> int: - # return sum(1 for parameter in self.parameters if not parameter.free) - - # def parameter_user_constraints_count(self) -> int: - # return sum(len(parameter.user_constraints.keys()) for parameter in self.parameters if not parameter.free) diff --git a/src/easyreflectometry/summary/summary.py b/src/easyreflectometry/summary/summary.py index 139898b0..e36a7724 100644 --- a/src/easyreflectometry/summary/summary.py +++ b/src/easyreflectometry/summary/summary.py @@ -4,6 +4,9 @@ from xhtml2pdf import pisa from easyreflectometry import Project +from easyreflectometry.utils import count_fixed_parameters +from easyreflectometry.utils import count_free_parameters +from easyreflectometry.utils import count_parameter_user_constraints from .html_templates import HTML_DATA_COLLECTION_TEMPLATE from .html_templates import HTML_FIGURES_TEMPLATE @@ -162,12 +165,12 @@ def _experiments_section(self) -> str: def _refinement_section(self) -> str: html_refinement = HTML_REFINEMENT_TEMPLATE - num_free_params = self._project.free_parameters_count() - num_fixed_params = self._project.fixed_parameters_count() + num_free_params = count_free_parameters(self._project) + num_fixed_params = count_fixed_parameters(self._project) num_params = num_free_params + num_fixed_params # goodness_of_fit = self._project.status.goodnessOfFit # goodness_of_fit = goodness_of_fit.split(' → ')[-1] - num_constraints = self._project.parameter_user_constraints_count() + num_constraints = count_parameter_user_constraints(self._project) html_refinement = html_refinement.replace('calculation_engine', f'{self._project._calculator.current_interface_name}') html_refinement = html_refinement.replace('minimization_engine', f'{self._project.minimizer.name}') diff --git a/src/easyreflectometry/utils.py b/src/easyreflectometry/utils.py index 01850fb0..00138804 100644 --- a/src/easyreflectometry/utils.py +++ b/src/easyreflectometry/utils.py @@ -71,3 +71,15 @@ def collect_unique_names_from_dict(structure_dict: dict, unique_names: Optional[ if key == 'unique_name': unique_names.append(value) return unique_names + + +def count_free_parameters(project) -> int: + return sum(1 for parameter in project.parameters if parameter.free) + + +def count_fixed_parameters(project) -> int: + return sum(1 for parameter in project.parameters if not parameter.free) + + +def count_parameter_user_constraints(project) -> int: + return sum(len(parameter.user_constraints.keys()) for parameter in project.parameters if not parameter.free) diff --git a/tests/test_project.py b/tests/test_project.py index 990f63e5..0d626b1f 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -634,39 +634,3 @@ def test_parameters(self): # Expect assert len(parameters) == 14 assert isinstance(parameters[0], Parameter) - - def test_count_free_parameters(self): - # When - project = Project() - project.default_model() - project.parameters[0].free = True - - # Then - count = project.free_parameters_count() - - # Expect - assert count == 1 - - def test_count_fixed_parameters(self): - # When - project = Project() - project.default_model() - project.parameters[0].free = True - - # Then - count = project.fixed_parameters_count() - - # Expect - assert count == 13 - - def test_count_parameter_user_constraints(self): - # When - project = Project() - project.default_model() - project.parameters[0].user_constraints['name_other_parameter'] = 'constraint' - - # Then - count = project.parameter_user_constraints_count() - - # Expect - assert count == 1 diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..a5a2bbe7 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,43 @@ +from easyreflectometry import Project +from easyreflectometry.utils import count_fixed_parameters +from easyreflectometry.utils import count_free_parameters +from easyreflectometry.utils import count_parameter_user_constraints + + +def test_count_free_parameters(): + # When + project = Project() + project.default_model() + project.parameters[0].free = True + + # Then + count = count_free_parameters(project) + + # Expect + assert count == 1 + + +def test_count_fixed_parameters(): + # When + project = Project() + project.default_model() + project.parameters[0].free = True + + # Then + count = count_fixed_parameters(project) + + # Expect + assert count == 13 + + +def test_count_parameter_user_constraints(): + # When + project = Project() + project.default_model() + project.parameters[0].user_constraints['name_other_parameter'] = 'constraint' + + # Then + count = count_parameter_user_constraints(project) + + # Expect + assert count == 1