Skip to content

Commit

Permalink
Merge branch 'develop' into 201-move-reporting-to-the-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Nov 15, 2024
2 parents 4c3a0d7 + e64c4b4 commit 779fdda
Show file tree
Hide file tree
Showing 27 changed files with 398 additions and 148 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
]
requires-python = ">=3.9,<3.13"
dependencies = [
'easyscience @ git+https://github.com/EasyScience/EasyScience.git@develop',
"easyscience>=1.2.0",
"scipp>=23.12.0",
"refnx>=0.1.15",
"refl1d>=0.8.14",
Expand Down
2 changes: 2 additions & 0 deletions src/easyreflectometry/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .data_store import DataSet1D
from .data_store import ProjectData
from .measurement import load
from .measurement import load_as_dataset

__all__ = [
load,
load_as_dataset,
ProjectData,
DataSet1D,
]
13 changes: 13 additions & 0 deletions src/easyreflectometry/data/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
from orsopy.fileio import Header
from orsopy.fileio import orso

from easyreflectometry.data import DataSet1D


def load_as_dataset(fname: Union[TextIO, str]) -> DataSet1D:
"""Load data from an ORSO .ort file as a DataSet1D."""
data_group = load(fname)
return DataSet1D(
x=data_group['coords']['Qz_0'].values,
y=data_group['data']['R_0'].values,
ye=data_group['data']['R_0'].variances,
xe=data_group['coords']['Qz_0'].variances,
)


def load(fname: Union[TextIO, str]) -> sc.DataGroup:
"""Load data from an ORSO .ort file.
Expand Down
23 changes: 17 additions & 6 deletions src/easyreflectometry/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import numpy as np
import scipp as sc
from easyscience.fitting import AvailableMinimizers
from easyscience.fitting.multi_fitter import MultiFitter as easyFitter
from easyscience.fitting import FitResults
from easyscience.fitting.multi_fitter import MultiFitter as EasyScienceMultiFitter

from easyreflectometry.data import DataSet1D
from easyreflectometry.model import Model


class Fitter:
class MultiFitter:
def __init__(self, *args: Model):
r"""A convinence class for the :py:class:`easyscience.Fitting.Fitting`
which will populate the :py:class:`sc.DataGroup` appropriately
Expand All @@ -26,7 +28,7 @@ def wrapped(*args, **kwargs):

self._fit_func = [func_wrapper(m.interface.fit_func, m.unique_name) for m in args]
self._models = args
self.easy_f = easyFitter(args, self._fit_func)
self.easy_science_multi_fitter = EasyScienceMultiFitter(args, self._fit_func)

def fit(self, data: sc.DataGroup, id: int = 0) -> sc.DataGroup:
"""
Expand All @@ -39,28 +41,37 @@ def fit(self, data: sc.DataGroup, id: int = 0) -> sc.DataGroup:
x = [data['coords'][f'Qz_{i}'].values for i in refl_nums]
y = [data['data'][f'R_{i}'].values for i in refl_nums]
dy = [1 / np.sqrt(data['data'][f'R_{i}'].variances) for i in refl_nums]
result = self.easy_f.fit(x, y, weights=dy)
result = self.easy_science_multi_fitter.fit(x, y, weights=dy)
new_data = data.copy()
for i, _ in enumerate(result):
id = refl_nums[i]
new_data[f'R_{id}_model'] = sc.array(
dims=[f'Qz_{id}'], values=self._fit_func[i](data['coords'][f'Qz_{id}'].values)
)
sld_profile = self.easy_f._fit_objects[i].interface.sld_profile(self._models[i].unique_name)
sld_profile = self.easy_science_multi_fitter._fit_objects[i].interface.sld_profile(self._models[i].unique_name)
new_data[f'SLD_{id}'] = sc.array(dims=[f'z_{id}'], values=sld_profile[1] * 1e-6, unit=sc.Unit('1/angstrom') ** 2)
new_data['attrs'][f'R_{id}_model'] = {'model': sc.scalar(self._models[i].as_dict())}
new_data['coords'][f'z_{id}'] = sc.array(
dims=[f'z_{id}'], values=sld_profile[0], unit=(1 / new_data['coords'][f'Qz_{id}'].unit).unit
)
return new_data

def fit_single_data_set_1d(self, data: DataSet1D) -> FitResults:
"""
Perform the fitting and populate the DataGroups with the result.
:param data: DataGroup to be fitted to and populated
:param method: Optimisation method
"""
return self.easy_science_multi_fitter.fit(x=[data.x], y=[data.y], weights=[data.ye])[0]

def switch_minimizer(self, minimizer: AvailableMinimizers) -> None:
"""
Switch the minimizer for the fitting.
:param minimizer: Minimizer to be switched to
"""
self.easy_f.switch_minimizer(minimizer)
self.easy_science_multi_fitter.switch_minimizer(minimizer)


def _flatten_list(this_list: list) -> list:
Expand Down
4 changes: 2 additions & 2 deletions src/easyreflectometry/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from easyscience.Objects.new_variable import Parameter
from easyscience.Objects.ObjectClasses import BaseObj

from easyreflectometry.parameter_utils import get_as_parameter
from easyreflectometry.parameter_utils import yaml_dump
from easyreflectometry.sample import BaseAssembly
from easyreflectometry.sample import Sample
from easyreflectometry.utils import get_as_parameter
from easyreflectometry.utils import yaml_dump

from .resolution_functions import PercentageFhwm
from .resolution_functions import ResolutionFunction
Expand Down
2 changes: 1 addition & 1 deletion src/easyreflectometry/model/model_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def add_model(self, model: Optional[Model] = None):
:param model: Model to add.
"""
if model is None:
model = Model(name='Model new', interface=self.interface)
model = Model(name='EasyModel added', interface=self.interface)
self.append(model)

def duplicate_model(self, index: int):
Expand Down
2 changes: 1 addition & 1 deletion src/easyreflectometry/model/resolution_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def smearing(self, q: Union[np.array, float]) -> np.array:
def as_dict(
self, skip: Optional[List[str]] = None
) -> dict[str, str]: # skip is kept for consistency of the as_dict signature
return {'smearing': 'LinearSpline', 'q_data_points': self.q_data_points, 'fwhm_values': self.fwhm_values}
return {'smearing': 'LinearSpline', 'q_data_points': list(self.q_data_points), 'fwhm_values': list(self.fwhm_values)}
Loading

0 comments on commit 779fdda

Please sign in to comment.