Skip to content

Commit

Permalink
Merge branch 'develop' into 181-extract-functionalit-for-creating-and…
Browse files Browse the repository at this point in the history
…-manipulating-a-project-should-be-moved-from-the-app-to-the-lib
  • Loading branch information
andped10 committed Sep 24, 2024
2 parents 06d70dc + 2c4bc50 commit fbcce69
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/easyreflectometry/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .data_store import ProjectData
from .measurement import load

__all__ = [
load,
ProjectData,
]
129 changes: 129 additions & 0 deletions src/easyreflectometry/data/data_store.py
Original file line number Diff line number Diff line change
@@ -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))
File renamed without changes.
11 changes: 0 additions & 11 deletions src/easyreflectometry/measurement/data.py

This file was deleted.

8 changes: 4 additions & 4 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fbcce69

Please sign in to comment.