Skip to content

Commit

Permalink
experiments section
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Nov 18, 2024
1 parent efbc9e5 commit edb273e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
36 changes: 22 additions & 14 deletions src/easyreflectometry/summary/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def __init__(self, project: Project):

def compile_html_summary(self):
html = HTML_TEMPLATE
self._set_project_information_section(html)
self._set_sample_section(html)
self._set_experiments_section(html)
self._set_refinement_section(html)
html.replace('project_information_section', self._project_information_section())
html.replace('sample_section', self._sample_section())
html.replace('experiments_section', self._experiments_section())
html.replace('refinement_section', self._refinement_section())
return html

def _set_project_information_section(self, html: str) -> None:
def _project_information_section(self) -> None:
html_project = ''
if self._project.created:
html_project = HTML_PROJECT_INFORMATION_TEMPLATE
Expand All @@ -31,9 +31,9 @@ def _set_project_information_section(self, html: str) -> None:
html_project = html_project.replace('project_description', f'{short_description}')
# html_project = html_project.replace('num_phases', f'{self._project.status.phaseCount}')
html_project = html_project.replace('num_experiments', f'{len(self._project.experiments)}')
return html.replace('project_information_section', html_project)
return html_project

def _set_sample_section(self, html: str) -> None:
def _sample_section(self) -> None:
html_phases = []

for phase in self._project.dataBlocks:
Expand All @@ -59,9 +59,11 @@ def _set_sample_section(self, html: str) -> None:
html_phase = html_phase.replace('angle_gamma', f'{angle_gamma}')
html_phases.append(html_phase)

html = html.replace('crystal_data_section', '\n'.join(html_phases))
html_phases_str = '\n'.join(html_phases)

def _set_experiments_section(self, html: str) -> None:
return html_phases_str

def _experiments_section(self) -> None:
html_experiments = []

for idx, experiment in self._project.experiments.items():
Expand All @@ -72,7 +74,9 @@ def _set_experiments_section(self, html: str) -> None:
# radiation_type = radiation_type.replace('cwl', 'constant wavelength')
# radiation_type = radiation_type.replace('tof', 'time-of-flight')
num_data_points = len(experiment.x)
resolution_functions = experiment.ye
resolution_function = self._project.models[idx].resolution_function.as_dict()['smearing']
if resolution_function == 'PercentageFhwm':
resolution_function = resolution_function + self._project.models[idx].resolution_function.as_dict()['constant']
range_min = min(experiment.y)
range_max = max(experiment.y)
range_units = 'Å⁻¹'
Expand Down Expand Up @@ -101,14 +105,18 @@ def _set_experiments_section(self, html: str) -> None:
# html_experiment = html_experiment.replace('radiation_type', f'{radiation_type}')
html_experiment = html_experiment.replace('range_min', f'{range_min}')
html_experiment = html_experiment.replace('range_max', f'{range_max}')
html_experiment = html_experiment.replace('range_inc', f'{range_inc}')
# html_experiment = html_experiment.replace('range_inc', f'{range_inc}')
html_experiment = html_experiment.replace('range_units', f'{range_units}')
html_experiment = html_experiment.replace('num_data_points', f'{num_data_points}')
html_experiments.append(html_experiment)

html = html.replace('experiments_section', '\n'.join(html_experiments))
html_experiments_str = '\n'.join(html_experiments)

return html_experiments_str

def _set_refinement_section(self, html: str) -> None:
def _refinement_section(
self,
) -> None:
html_refinement = HTML_REFINEMENT_TEMPLATE
num_free_params = count_free_parameters(self._project)
num_fixed_params = count_fixed_parameters(self._project)
Expand All @@ -124,4 +132,4 @@ def _set_refinement_section(self, html: str) -> None:
html_refinement = html_refinement.replace('num_free_params', f'{num_free_params}')
html_refinement = html_refinement.replace('num_fixed_params', f'{num_fixed_params}')
html_refinement = html_refinement.replace('num_constraints', f'{num_constraints}')
return html.replace('refinement_section', html_refinement)
return html_refinement
15 changes: 7 additions & 8 deletions tests/summary/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import easyreflectometry
from easyreflectometry import Project
from easyreflectometry.summary import Summary
from easyreflectometry.summary.html_templates import HTML_TEMPLATE

PATH_STATIC = os.path.join(os.path.dirname(easyreflectometry.__file__), '..', '..', 'tests', '_static')

Expand Down Expand Up @@ -40,10 +39,9 @@ def test_set_project_information_section(self, project: Project) -> None:
# When
project._created = True
summary = Summary(project)
html = 'project_information_section'

# Then
html = summary._set_project_information_section(html)
html = summary._project_information_section()

# Expect
assert (
Expand All @@ -57,22 +55,23 @@ def test_set_experiments_section(self, project: Project) -> None:
fpath = os.path.join(PATH_STATIC, 'example.ort')
project.load_experiment_for_model_at_index(fpath)
summary = Summary(project)
html = 'experiment_section'

# Then
html = summary._set_experiments_section(html)
html = summary._experiments_section()

# Expect
assert html == ''
assert (
html
== '\n<tr>\n <th>Experiment datablock</th>\n <th>Experiment for Model 0</th>\n</tr>\n<tr>\n <td>Radiation probe</td>\n <td>radiation_probe</td>\n</tr>\n<tr>\n <td>Radiation type</td>\n <td>radiation_type</td>\n</tr>\n<tr>\n <td>Measured range: min, max, inc (Å⁻¹)</td>\n <td>9.26972e-08,&nbsp;&nbsp;1.1171,&nbsp;&nbsp;range_inc</td>\n</tr>\n<tr>\n <td>No. of data points</td>\n <td>408</td>\n</tr>\n\n<tr></tr>\n'
)

def test_set_refinement_section(self, project: Project) -> None:
# When
project._created = True
summary = Summary(project)
html = 'refinement_section'

# Then
html = summary._set_refinement_section(html)
html = summary._refinement_section()

# Expect
assert (
Expand Down

0 comments on commit edb273e

Please sign in to comment.