Skip to content

Commit

Permalink
refinement section
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Nov 15, 2024
1 parent ff1980c commit b23a342
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
60 changes: 32 additions & 28 deletions src/easyreflectometry/summary/html_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,7 @@
<!-- Analysis -->
<tr>
<td><h3>Refinement</h3></td>
</tr>
<tr></tr>
<tr>
<td>Calculation engine</td>
<td>calculation_engine &mdash; https://www.cryspy.fr</td>
</tr>
<tr>
<td>Minimization engine</td>
<td>minimization_engine &mdash; https://lmfit.github.io/lmfit-py</td>
</tr>
<tr>
<td>Goodness-of-fit: reduced <i>&chi;</i><sup>2</sup></td>
<td>goodness_of_fit</td>
</tr>
<tr>
<td>No. of parameters: total, free, fixed</td>
<td>num_total_params,&nbsp;&nbsp;num_free_params,&nbsp;&nbsp;num_fixed_params</td>
</tr>
<tr>
<td>No. of constraints</td>
<td>0</td>
</tr>
<tr></tr>
refinement_section
</table>
Expand Down Expand Up @@ -155,3 +128,34 @@
<tr></tr>
"""

HTML_REFINEMENT_TEMPLATE = """
<tr>
<td><h3>Refinement</h3></td>
</tr>
<tr></tr>
<tr>
<td>Calculation engine</td>
<td>calculation_engine</td>
</tr>
<tr>
<td>Minimization engine</td>
<td>minimization_engine</td>
</tr>
<tr>
<td>Goodness-of-fit: reduced <i>&chi;</i><sup>2</sup></td>
<td>goodness_of_fit</td>
</tr>
<tr>
<td>No. of parameters: total, free, fixed</td>
<td>num_total_params, num_free_params, num_fixed_params</td>
</tr>
<tr>
<td>No. of constraints</td>
<td>num_constriants</td>
</tr>
<tr></tr>
"""
48 changes: 35 additions & 13 deletions src/easyreflectometry/summary/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .html_templates import HTML_CRYSTAL_DATA_TEMPLATE
from .html_templates import HTML_DATA_COLLECTION_TEMPLATE
from .html_templates import HTML_PROJECT_INFORMATION_TEMPLATE
from .html_templates import HTML_REFINEMENT_TEMPLATE
from .html_templates import HTML_TEMPLATE


Expand All @@ -21,14 +22,13 @@ def compile_html_summary(self):
def _set_project_information_section(self, html: str) -> None:
html_project = ''
if self._project.created:
html_project = HTML_PROJECT_INFORMATION_TEMPLATE
name = self._project._info['name']
short_description = self._project._info['short_description']
html_project = HTML_PROJECT_INFORMATION_TEMPLATE
html_project = html_project.replace('project_title', f'{name}')
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)

def _set_crystal_data_section(self, html: str) -> None:
Expand Down Expand Up @@ -101,15 +101,37 @@ def _set_data_collection_section(self, html: str) -> None:
html = html.replace('data_collection_section', '\n'.join(html_experiments))

def _set_refinement_section(self, html: str) -> None:
num_free_params = self._project.fittables.freeParamsCount
num_fixed_params = self._project.fittables.fixedParamsCount
html_refinement = HTML_REFINEMENT_TEMPLATE
num_free_params = 1 # count_free_parameters(self._project)
num_fixed_params = 2 # 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]

html = html.replace('calculation_engine', f'{self._project.status.calculator}')
html = html.replace('minimization_engine', f'{self._project.status.minimizer}')
html = html.replace('goodness_of_fit', f'{goodness_of_fit}')
html = html.replace('num_total_params', f'{num_params}')
html = html.replace('num_free_params', f'{num_free_params}')
html = html.replace('num_fixed_params', f'{num_fixed_params}')
# goodness_of_fit = self._project.status.goodnessOfFit
# goodness_of_fit = goodness_of_fit.split(' → ')[-1]
num_constraints = 0

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}')
# html = html.replace('goodness_of_fit', f'{goodness_of_fit}')
html_refinement = html_refinement.replace('num_total_params', f'{num_params}')
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)


def count_free_parameters(project: Project) -> int:
count = 0
parameters = project.parameters
for parameter in parameters:
if parameter.free:
count = count + 1
return count


def count_fixed_parameters(project: Project) -> int:
count = 0
parameters = project.parameters
for parameter in parameters:
if not parameter.free:
count = count + 1
return count
15 changes: 15 additions & 0 deletions tests/summary/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ def test_set_project_information_section(self, project: Project) -> None:
html
== '\n<tr>\n <td><h3>Project information</h3></td>\n</tr>\n\n<tr></tr>\n\n<tr>\n <th>Title</th>\n <th>Example Project</th>\n</tr>\n<tr>\n <td>Description</td>\n <td>reflectometry, 1D</td>\n</tr>\n<tr>\n <td>No. of experiments</td>\n <td>0</td>\n</tr>\n\n<tr></tr>\n'
)

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

# Then
html = summary._set_refinement_section(html)

# Expect
assert (
html
== '\n<tr>\n <td><h3>Refinement</h3></td>\n</tr>\n\n<tr></tr>\n\n<tr>\n <td>Calculation engine</td>\n <td>refnx</td>\n</tr>\n<tr>\n <td>Minimization engine</td>\n <td>LMFit_leastsq</td>\n</tr>\n<tr>\n <td>Goodness-of-fit: reduced <i>&chi;</i><sup>2</sup></td>\n <td>goodness_of_fit</td>\n</tr>\n<tr>\n <td>No. of parameters: total, free, fixed</td>\n <td>3, 1, 2</td>\n</tr>\n<tr>\n <td>No. of constraints</td>\n <td>num_constriants</td>\n</tr>\n\n<tr></tr>\n'
)

0 comments on commit b23a342

Please sign in to comment.