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
12 changes: 6 additions & 6 deletions src/easydynamics/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from easydynamics.settings.detailed_balance_settings import DetailedBalanceSettings
from easydynamics.utils.plotting import slicerplot_with_residuals
from easydynamics.utils.utils import _in_notebook
from easydynamics.utils.utils import verify_Q_index


class Analysis(AnalysisBase):
Expand Down Expand Up @@ -253,7 +254,7 @@ def calculate(
if Q_index is None:
return [analysis.calculate(energy=energy) for analysis in self.analysis_list]

Q_index = self._verify_Q_index(Q_index)
verify_Q_index(Q_index=Q_index, Q=self.Q)
return self.analysis_list[Q_index].calculate(energy=energy)

def fit(
Expand Down Expand Up @@ -291,7 +292,7 @@ def fit(
'No Q values available for fitting. Please check the experiment data.'
)

Q_index = self._verify_Q_index(Q_index)
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)

if fit_method == 'independent':
if Q_index is not None:
Expand Down Expand Up @@ -347,9 +348,8 @@ def plot_data_and_model(
InteractiveFigure
A Plopp InteractiveFigure containing the plot of the data and model.
"""

verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)
if Q_index is not None:
Q_index = self._verify_Q_index(Q_index)
return self.analysis_list[Q_index].plot_data_and_model(
plot_components=plot_components,
add_background=add_background,
Expand Down Expand Up @@ -627,8 +627,8 @@ def fix_energy_offset(self, Q_index: int | None = None) -> None:
Index of the Q value to fix the energy offset for. If None, fixes the energy offset for
all Q values.
"""
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)
if Q_index is not None:
Q_index = self._verify_Q_index(Q_index)
self.analysis_list[Q_index].fix_energy_offset()
else:
for analysis in self.analysis_list:
Expand All @@ -645,8 +645,8 @@ def free_energy_offset(self, Q_index: int | None = None) -> None:
Index of the Q value to free the energy offset for. If None, frees the energy offset
for all Q values.
"""
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)
if Q_index is not None:
Q_index = self._verify_Q_index(Q_index)
self.analysis_list[Q_index].free_energy_offset()
else:
for analysis in self.analysis_list:
Expand Down
8 changes: 5 additions & 3 deletions src/easydynamics/analysis/analysis1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from easydynamics.settings.detailed_balance_settings import DetailedBalanceSettings
from easydynamics.utils.detailed_balance import detailed_balance_factor
from easydynamics.utils.plotting import slicerplot_with_residuals
from easydynamics.utils.utils import verify_Q_index


class Analysis1d(AnalysisBase):
Expand Down Expand Up @@ -119,7 +120,8 @@ def __init__(
extra_parameters=extra_parameters,
)

self._Q_index = self._verify_Q_index(Q_index)
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)
self._Q_index = Q_index

if self._Q_index is not None and self.experiment is not None:
masked_energy = self.experiment.get_masked_energy(Q_index=self._Q_index)
Expand Down Expand Up @@ -158,8 +160,8 @@ def Q_index(self, value: int | None) -> None:
value : int | None
The Q index.
"""

self._Q_index = self._verify_Q_index(value)
verify_Q_index(Q_index=value, Q=self.Q, allow_none=True)
self._Q_index = value
self._on_Q_index_changed()

#############
Expand Down
31 changes: 0 additions & 31 deletions src/easydynamics/analysis/analysis_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,37 +494,6 @@ def _on_convolution_settings_changed(self) -> None:
For subclasses that implement convolution, this method can be overridden
"""

def _verify_Q_index(self, Q_index: int | None) -> int | None:
"""
Verify that the Q index is valid.

Parameters
----------
Q_index : int | None
The Q index to verify.

Raises
------
TypeError
If Q_index is not an integer or None.
IndexError
If the Q index is not valid.

Returns
-------
int | None
The verified Q index.
"""
if Q_index is None:
return None

if not isinstance(Q_index, int):
raise TypeError('Q_index must be an integer or None.')

if Q_index < 0 or (self.Q is not None and Q_index >= len(self.Q)):
raise IndexError('Q_index must be a valid index for the Q values.')
return Q_index

def _verify_energy(self, energy: sc.Variable | None) -> sc.Variable | None:
"""
Verify that the provided energy is the correct type.
Expand Down
13 changes: 2 additions & 11 deletions src/easydynamics/experiment/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from easydynamics.base_classes.easydynamics_base import EasyDynamicsBase
from easydynamics.utils.utils import _in_notebook
from easydynamics.utils.utils import verify_Q_index


class Experiment(EasyDynamicsBase):
Expand Down Expand Up @@ -239,11 +240,6 @@ def get_masked_energy(self, Q_index: int) -> sc.Variable | None:
Q_index : int
The Q index to get the masked energy values for.

Raises
------
IndexError
If Q_index is not a valid index for the Q values.

Returns
-------
sc.Variable | None
Expand All @@ -252,12 +248,7 @@ def get_masked_energy(self, Q_index: int) -> sc.Variable | None:
if self.binned_data is None:
return None

if (
not isinstance(Q_index, int)
or Q_index < 0
or (self.Q is not None and Q_index >= len(self.Q))
):
raise IndexError('Q_index must be a valid index for the Q values.')
verify_Q_index(Q_index, self.Q)

energy = self.binned_data.coords['energy']
_, _, _, mask = self._extract_x_y_weights_only_finite(Q_index=Q_index)
Expand Down
31 changes: 3 additions & 28 deletions src/easydynamics/sample_model/diffusion_model/delta_lorentz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from easydynamics.sample_model.diffusion_model.diffusion_model_base import DiffusionModelBase
from easydynamics.utils.utils import Numeric
from easydynamics.utils.utils import Q_type
from easydynamics.utils.utils import verify_Q_index

MINIMUM_WIDTH = 1e-10 # To avoid division by zero

Expand Down Expand Up @@ -587,22 +588,9 @@ def get_independent_variables(self, Q_index: int | None = None) -> list[Paramete
-------
list[Parameter]
List of independent variables in the model.

Raises
------
ValueError
If Q_index is not None and is not a valid index for the Q values in the model.
"""

if Q_index is not None and (
not isinstance(Q_index, int)
or Q_index < 0
or Q_index >= len(self._component_collections)
):
raise ValueError(
f'Q_index must be an integer between 0 and '
f'{len(self._component_collections) - 1}, or None.'
)
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)

variables = []
if self._allow_Q_variation['A_0'] is True:
Expand All @@ -629,22 +617,9 @@ def get_all_variables(self, Q_index: int | None = None) -> list[DescriptorNumber
-------
list[DescriptorNumber]
List of all variables in the model.

Raises
------
ValueError
If Q_index is not None and is not a valid index for the Q values in the model.
"""

if Q_index is not None and (
not isinstance(Q_index, int)
or Q_index < 0
or Q_index >= len(self._component_collections)
):
raise ValueError(
f'Q_index must be an integer between 0 and '
f'{len(self._component_collections) - 1}, or None.'
)
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)

variables = self.get_global_variables()
variables.extend(self.get_independent_variables(Q_index=Q_index))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from easydynamics.utils.utils import Numeric
from easydynamics.utils.utils import Q_type
from easydynamics.utils.utils import _validate_and_convert_Q
from easydynamics.utils.utils import verify_Q_index


class DiffusionModelBase(EasyDynamicsModelBase):
Expand Down Expand Up @@ -305,21 +306,8 @@ def get_independent_variables(self, Q_index: int | None = None) -> list[Paramete
-------
list[Parameter]
List of independent variables in the model.

Raises
------
ValueError
If Q_index is not None and is not a valid index for the Q values in the model.
"""
if Q_index is not None and (
not isinstance(Q_index, int)
or Q_index < 0
or Q_index >= len(self._component_collections)
):
raise ValueError(
f'Q_index must be an integer between 0 and '
f'{max(len(self._component_collections) - 1, 0)}, or None.'
)
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)

return []

Expand All @@ -337,22 +325,8 @@ def get_all_variables(self, Q_index: int | None = None) -> list[Parameter]:
-------
list[Parameter]
A list of all Parameters from the diffusion model.

Raises
------
ValueError
If Q_index is out of bounds for the number of ComponentCollections.
"""

if Q_index is not None and (
not isinstance(Q_index, int)
or Q_index < 0
or Q_index >= len(self._component_collections)
):
raise ValueError(
f'Q_index must be an integer between 0 and '
f'{max(len(self._component_collections) - 1, 0)}, or None.'
)
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)

variables = self.get_global_variables()
variables.extend(self.get_independent_variables(Q_index))
Expand Down Expand Up @@ -464,29 +438,16 @@ def get_component_collections(
The index of the desired ComponentCollection. If None, all ComponentCollections are
returned.

Raises
------
TypeError
If Q_index is not an int.
IndexError
If Q_index is out of bounds for the number of ComponentCollections.

Returns
-------
ComponentCollection | list[ComponentCollection]
The ComponentCollection at the specified Q index. If Q_index is None, a list of all
ComponentCollections is returned.
"""
verify_Q_index(Q_index=Q_index, Q=self.Q, allow_none=True)
if Q_index is None:
return self._component_collections

if not isinstance(Q_index, int):
raise TypeError(f'Q_index must be an int, got {type(Q_index).__name__}')
if Q_index < 0 or Q_index >= len(self._component_collections):
raise IndexError(
f'Q_index {Q_index} is out of bounds for component collections '
f'of length {len(self._component_collections)}'
)
return self._component_collections[Q_index]

# ------------------------------------------------------------------
Expand Down
42 changes: 4 additions & 38 deletions src/easydynamics/sample_model/instrument_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from easydynamics.utils.utils import Q_type
from easydynamics.utils.utils import _validate_and_convert_Q
from easydynamics.utils.utils import _validate_unit
from easydynamics.utils.utils import verify_Q_index


class InstrumentModel(NewBase):
Expand Down Expand Up @@ -395,13 +396,6 @@ def get_all_variables(self, Q_index: int | None = None) -> list[Parameter]:
The index of the Q value to get variables for. If None, get variables for all Q values.


Raises
------
TypeError
If Q_index is not an int or None.
IndexError
If Q_index is out of bounds for the Q values in the InstrumentModel.

Returns
-------
list[Parameter]
Expand All @@ -413,15 +407,10 @@ def get_all_variables(self, Q_index: int | None = None) -> list[Parameter]:
return []

self._ensure_energy_offsets_current()
verify_Q_index(Q_index=Q_index, Q=self._Q, allow_none=True)
if Q_index is None:
variables = [self._energy_offsets[i] for i in range(len(self._Q))]
else:
if not isinstance(Q_index, int):
raise TypeError(f'Q_index must be an int or None, got {type(Q_index).__name__}')
if Q_index < 0 or Q_index >= len(self._Q):
raise IndexError(
f'Q_index {Q_index} is out of bounds for Q of length {len(self._Q)}'
)
variables = [self._energy_offsets[Q_index]]

variables.extend(self._background_model.get_all_variables(Q_index=Q_index))
Expand Down Expand Up @@ -458,10 +447,6 @@ def get_energy_offset(
------
ValueError
If no Q values are set in the InstrumentModel.
IndexError
If Q_index is out of bounds.
TypeError
If Q_index is not an int or None.

Returns
-------
Expand All @@ -473,15 +458,10 @@ def get_energy_offset(
raise ValueError('No Q values are set in the InstrumentModel.')

self._ensure_energy_offsets_current()
verify_Q_index(Q_index=Q_index, Q=self._Q, allow_none=True)
if Q_index is None:
return self._energy_offsets

if not isinstance(Q_index, int):
raise TypeError(f'Q_index must be an int or None, got {type(Q_index).__name__}')

if Q_index < 0 or Q_index >= len(self._Q):
raise IndexError(f'Q_index {Q_index} is out of bounds for Q of length {len(self._Q)}')

return self._energy_offsets[Q_index]

def fix_energy_offset(self, Q_index: int | None = None) -> None:
Expand Down Expand Up @@ -531,27 +511,13 @@ def _fix_or_free_energy_offset(self, Q_index: int | None = None, fixed: bool = T
energy offsets for all Q values.
fixed : bool, default=True
Whether to fix (True) or free (False) the energy offset.

Raises
------
TypeError
If Q_index is not an int or None.
IndexError
If Q_index is out of bounds for the Q values in the InstrumentModel.
"""

self._ensure_energy_offsets_current()
verify_Q_index(Q_index=Q_index, Q=self._Q, allow_none=True)
if Q_index is None:
for offset in self._energy_offsets:
offset.fixed = fixed
else:
if not isinstance(Q_index, int):
raise TypeError(f'Q_index must be an int or None, got {type(Q_index).__name__}')

if Q_index < 0 or Q_index >= len(self._Q):
raise IndexError(
f'Q_index {Q_index} is out of bounds for Q of length {len(self._Q)}'
)
self._energy_offsets[Q_index].fixed = fixed

def _ensure_energy_offsets_current(self) -> None:
Expand Down
Loading
Loading