Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

199 make fitter a part of project #200

Merged
merged 29 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3e2d110
minor functionality and code cleaning
andped10 Oct 31, 2024
b670869
expose parameters
andped10 Nov 1, 2024
18ad31c
added method to fit data in DataSet1D formar
andped10 Nov 1, 2024
2506376
syntax fit
andped10 Nov 1, 2024
5279e2a
rename method
andped10 Nov 1, 2024
741c14a
ruff
andped10 Nov 4, 2024
2b91109
sample index in project
andped10 Nov 4, 2024
d0a1af1
est all indicies
andped10 Nov 4, 2024
8594cd3
indicies setters updated
andped10 Nov 4, 2024
8ea2ec5
bug indicies
andped10 Nov 4, 2024
60d326d
material index
andped10 Nov 4, 2024
b03455e
layer index
andped10 Nov 4, 2024
3965023
index to zero rather than none
andped10 Nov 5, 2024
49368a2
only parameters in project
andped10 Nov 6, 2024
574e770
load and save project with experimental data
andped10 Nov 8, 2024
5686d15
only be recursive on dicts
andped10 Nov 9, 2024
a9f1f33
enabling load and save
andped10 Nov 9, 2024
9b1d133
load fix when no experiments
andped10 Nov 11, 2024
f36d70d
default info updated
andped10 Nov 13, 2024
6b08462
name for add model
andped10 Nov 13, 2024
e631d20
renameelement added to collections
andped10 Nov 13, 2024
ce52a44
added most used materials to project
andped10 Nov 13, 2024
3d3b404
add material in get index of given material
andped10 Nov 13, 2024
063feb6
minor adjustments to model and sample
andped10 Nov 13, 2024
dd0e7fe
latest version of easyscience
andped10 Nov 13, 2024
d1d0b05
update adding element to collection
andped10 Nov 13, 2024
156f7be
ruff
andped10 Nov 13, 2024
1a23538
fitter as property
andped10 Nov 15, 2024
6d867f0
added tests
andped10 Nov 15, 2024
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
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
Loading