From 2c4bc500497f9be48fed295640ab409bf2e19f8c Mon Sep 17 00:00:00 2001 From: Andreas Pedersen <48797331+andped10@users.noreply.github.com> Date: Tue, 24 Sep 2024 13:22:07 +0200 Subject: [PATCH] data folder (#187) --- src/easyreflectometry/data/__init__.py | 7 + src/easyreflectometry/data/data_store.py | 129 ++++++++++++++++++ .../{data.py => data/measurement.py} | 0 src/easyreflectometry/measurement/data.py | 11 -- tests/test_data.py | 8 +- tests/test_fitting.py | 2 +- 6 files changed, 141 insertions(+), 16 deletions(-) create mode 100644 src/easyreflectometry/data/__init__.py create mode 100644 src/easyreflectometry/data/data_store.py rename src/easyreflectometry/{data.py => data/measurement.py} (100%) delete mode 100644 src/easyreflectometry/measurement/data.py diff --git a/src/easyreflectometry/data/__init__.py b/src/easyreflectometry/data/__init__.py new file mode 100644 index 00000000..3676d427 --- /dev/null +++ b/src/easyreflectometry/data/__init__.py @@ -0,0 +1,7 @@ +from .data_store import ProjectData +from .measurement import load + +__all__ = [ + load, + ProjectData, +] diff --git a/src/easyreflectometry/data/data_store.py b/src/easyreflectometry/data/data_store.py new file mode 100644 index 00000000..65965b8b --- /dev/null +++ b/src/easyreflectometry/data/data_store.py @@ -0,0 +1,129 @@ +__author__ = 'github.com/wardsimon' + +from collections.abc import Sequence +from typing import Optional +from typing import TypeVar +from typing import Union + +import numpy as np +from easyscience.Objects.core import ComponentSerializer +from easyscience.Utils.io.dict import DictSerializer + +from easyreflectometry.model import Model + +T = TypeVar('T') + + +class ProjectData(ComponentSerializer): + def __init__(self, name='DataStore', exp_data=None, sim_data=None): + self.name = name + if exp_data is None: + exp_data = DataStore(name='Exp Datastore') + if sim_data is None: + sim_data = DataStore(name='Sim Datastore') + self.exp_data = exp_data + self.sim_data = sim_data + + +class DataStore(Sequence, ComponentSerializer): + def __init__(self, *args, name='DataStore'): + self.name = name + self.items = list(args) + self.show_legend = False + + def __getitem__(self, i: int) -> T: + return self.items.__getitem__(i) + + def __len__(self) -> int: + return len(self.items) + + def __setitem__(self, key, value): + self.items[key] = value + + def __delitem__(self, key): + del self.items[key] + + def append(self, *args): + self.items.append(*args) + + def as_dict(self, skip: list = []) -> dict: + this_dict = super(DataStore, self).as_dict(self, skip=skip) + this_dict['items'] = [item.as_dict() for item in self.items if hasattr(item, 'as_dict')] + + @classmethod + def from_dict(cls, d): + items = d['items'] + del d['items'] + obj = cls.from_dict(d) + decoder = DictSerializer() + obj.items = [decoder.decode(item) for item in items] + return obj + + @property + def experiments(self): + return [self[idx] for idx in range(len(self)) if self[idx].is_experiment] + + @property + def simulations(self): + return [self[idx] for idx in range(len(self)) if self[idx].is_simulation] + + +class DataSet1D(ComponentSerializer): + def __init__( + self, + name: str = 'Series', + x: Optional[Union[np.ndarray, list]] = None, + y: Optional[Union[np.ndarray, list]] = None, + ye: Optional[Union[np.ndarray, list]] = None, + xe: Optional[Union[np.ndarray, list]] = None, + model: Optional[Model] = None, + x_label: str = 'x', + y_label: str = 'y', + ): + self._model = model + self._model.background = np.min(y) + + if x is None: + x = np.array([]) + if y is None: + y = np.array([]) + if ye is None: + ye = np.zeros_like(x) + if xe is None: + xe = np.zeros_like(x) + + self.name = name + if not isinstance(x, np.ndarray): + x = np.array(x) + if not isinstance(y, np.ndarray): + y = np.array(y) + + self.x = x + self.y = y + self.ye = ye + self.xe = xe + + self.x_label = x_label + self.y_label = y_label + + self._color = None + + @property + def model(self) -> Model: + return self._model + + @model.setter + def model(self, new_model: Model) -> None: + self._model = new_model + self._model.background = np.min(self.y) + + @property + def is_experiment(self) -> bool: + return self._model is not None + + @property + def is_simulation(self) -> bool: + return self._model is None + + def __repr__(self) -> str: + return "1D DataStore of '{:s}' Vs '{:s}' with {} data points".format(self.x_label, self.y_label, len(self.x)) diff --git a/src/easyreflectometry/data.py b/src/easyreflectometry/data/measurement.py similarity index 100% rename from src/easyreflectometry/data.py rename to src/easyreflectometry/data/measurement.py diff --git a/src/easyreflectometry/measurement/data.py b/src/easyreflectometry/measurement/data.py deleted file mode 100644 index f45bffc0..00000000 --- a/src/easyreflectometry/measurement/data.py +++ /dev/null @@ -1,11 +0,0 @@ -__author__ = 'github.com/arm61' - -from typing import TextIO -from typing import Union - -import scipp as sc -from orsopy.fileio import orso - - -def load(fname: Union[TextIO, str]) -> sc.DataGroup: - return orso.load_orso(fname) diff --git a/tests/test_data.py b/tests/test_data.py index ded2107e..57418adf 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -10,11 +10,11 @@ from orsopy.fileio import load_orso import easyreflectometry -from easyreflectometry.data import _load_orso -from easyreflectometry.data import _load_txt -from easyreflectometry.data import load +from easyreflectometry.data.measurement import _load_orso +from easyreflectometry.data.measurement import _load_txt +from easyreflectometry.data.measurement import load -PATH_STATIC = os.path.join(os.path.dirname(easyreflectometry.__file__), '..', '..', 'tests' , '_static') +PATH_STATIC = os.path.join(os.path.dirname(easyreflectometry.__file__), '..', '..', 'tests', '_static') class TestData(unittest.TestCase): diff --git a/tests/test_fitting.py b/tests/test_fitting.py index 727c90a1..09a367e4 100644 --- a/tests/test_fitting.py +++ b/tests/test_fitting.py @@ -7,7 +7,7 @@ import easyreflectometry from easyreflectometry.calculators import CalculatorFactory -from easyreflectometry.data import load +from easyreflectometry.data.measurement import load from easyreflectometry.fitting import Fitter from easyreflectometry.model import Model from easyreflectometry.model import PercentageFhwm