Skip to content

Commit

Permalink
count functionality back to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Nov 21, 2024
1 parent 334152e commit 0286c6e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 60 deletions.
21 changes: 0 additions & 21 deletions src/easyreflectometry/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 6 additions & 3 deletions src/easyreflectometry/summary/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}')
Expand Down
12 changes: 12 additions & 0 deletions src/easyreflectometry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 0 additions & 36 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0286c6e

Please sign in to comment.