Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions MDMC/control/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ class Control:
- ``rescale_factor`` (`float`, optional, defaults to `1.`) applied to
the experimental data when calculating the FoM to ensure it is on
the same scale as the calculated observable
- ``auto_scale`` (`bool`, optional, defaults to `False`) set the
``rescale_factor`` automatically to minimise the FoM, if both
``rescale_factor`` and ``auto_scale`` are provided then a warning
is printed and ``auto_scale`` takes precedence
- ``auto_scale`` (str or :class:`AutoScale`, optional, defaults to `CONSTANT`)
The method for automatically setting the rescale factor.
See :class:`AutoScale` documentation for more information.
Comment thread
oerc0122 marked this conversation as resolved.
- ``use_FFT`` (`bool`, optional, defaults to `True`) whether to use
Fast Fourier Transforms in the calculation of dependent variables.
FFT speeds up calculation but places restrictions on spacing in the
Expand Down Expand Up @@ -261,7 +260,7 @@ class Control:
'reader':'GENERIC_READER',
'weight':0.5,
'resolution':{'gaussian':2.35}
'auto_scale':True}]
'auto_scale': 'minimise_fom'}]

Attributes
----------
Expand Down
37 changes: 37 additions & 0 deletions MDMC/refinement/FoM/ChiSquared_experror.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,43 @@ class ChiSquaredExpError(FigureOfMerit):
mathematical details.
"""

def _compute_unreduced(self, obs_pair: ObservablePair):
"""
Compute the unreduced FoM value for the given observable pair.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated.

Returns
-------
float
Unreduced FoM value.
"""
return np.sum((obs_pair.calculate_difference() / obs_pair.calculate_exp_errors()) ** 2)

def _minimise_factor(self, obs_pair: ObservablePair) -> float:
"""
Minimise the FoM factor for the given FoM type.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated

Returns
-------
float
Computed auto_scale factor to minimise the FoM.
"""
exp_errors = np.array(*obs_pair.exp_obs.errors.values())
exp_values = np.array(*obs_pair.exp_obs.dependent_variables.values())
MD_values = np.array(*obs_pair.MD_obs.dependent_variables.values())
return np.sum((MD_values / exp_errors) ** 2) / np.sum(
MD_values * exp_values / exp_errors**2,
)

def calculate_single_FoM(self, obs_pair: ObservablePair):
"""
Calculates the chi-squared figure of merit for a single
Expand Down
67 changes: 67 additions & 0 deletions MDMC/refinement/FoM/FoM_abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np

from MDMC.common.decorators import repr_decorator
from MDMC.refinement.FoM import AutoScale
from MDMC.trajectory_analysis.observables.obs import Observable


Expand Down Expand Up @@ -468,3 +469,69 @@ def calculate_single_FoM(self, obs_pair: ObservablePair) -> float:
"""

raise NotImplementedError

@abstractmethod
def _compute_unreduced(self, obs_pair: ObservablePair) -> float:
"""
Compute the unreduced FoM value for the given observable pair.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated.

Returns
-------
float
Unreduced FoM value.
"""

@abstractmethod
def _minimise_factor(self, obs_pair: ObservablePair) -> float:
"""
Minimise the FoM factor for the given FoM type.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated

Returns
-------
float
Computed auto_scale factor to minimise the FoM.
"""

def compute_rescale_factor(self, obs_pair: ObservablePair) -> float:
"""
Compute rescale factor for calculated observable to match experimental data.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated

Returns
-------
float
Computed rescale factor.
"""
dep_vars = np.array(*obs_pair.exp_obs.dependent_variables.values())

match obs_pair.auto_scale:
case AutoScale.CONSTANT:
fac = obs_pair.rescale_factor
case AutoScale.MINIMISE_FOM:
fac = self._minimise_factor(obs_pair)
case AutoScale.MATCH_MAXIMUM:
fac = max(obs.max() for obs in dep_vars)
case AutoScale.MATCH_ABS_MAXIMUM:
fac = max(np.abs(obs).max() for obs in dep_vars)
case AutoScale.MATCH_SUM:
fac = sum(obs.sum() for obs in dep_vars)
case AutoScale.MATCH_ABS_SUM:
fac = sum(np.abs(obs.sum()) for obs in dep_vars)
case _:
fac = 1.0

return fac
36 changes: 36 additions & 0 deletions MDMC/refinement/FoM/RSquared_noneerror.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ class RSquared_noneerror(FigureOfMerit):
simple linear scaling.
"""

def _compute_unreduced(self, obs_pair: ObservablePair):
"""
Compute the unreduced FoM value for the given observable pair.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated.

Returns
-------
float
Unreduced FoM value.
"""
return np.sum(obs_pair.calculate_difference() ** 2)

def _minimise_factor(self, obs_pair: ObservablePair) -> float:
"""
Minimise the FoM factor for the given FoM type.

Parameters
----------
obs_pair : ObservablePair
An ``ObservablePair`` for which the FoM is calculated

Returns
-------
float
Computed auto_scale factor to minimise the FoM.
"""
exp_values = np.array(*obs_pair.exp_obs.dependent_variables.values())
MD_values = np.array(*obs_pair.MD_obs.dependent_variables.values())
A = np.sum(MD_values * exp_values)
B = np.sum(exp_values**2)
return A / B

def calculate_single_FoM(self, obs_pair: ObservablePair):
# ignore line too long linting as it is necessary for LaTeX formatting
# pylint: disable=line-too-long
Expand Down
2 changes: 2 additions & 0 deletions MDMC/refinement/FoM/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"""A module for Figure of Merit calculation"""

from . import ChiSquared_experror, FoM_abs, FoM_factory, RSquared_noneerror
from .FoM_abs import AutoScale

__all__ = [
"ChiSquared_experror",
"FoM_abs",
"FoM_factory",
"RSquared_noneerror",
"AutoScale",
]
Binary file modified doc/_static/files/linux/mdmc.tar.gz
Binary file not shown.
Binary file modified doc/_static/files/osx-windows/mdmc.zip
Binary file not shown.
Loading
Loading