Skip to content
Draft
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
98 changes: 98 additions & 0 deletions docs/docs/tutorials/DeltaLorentz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Diffusion Model\n",
"We support several standard models of diffusion. Here we show an example of Browniand Translational Diffusion, where the scattering is a Lorentzian with width ($\\Gamma$) given by $\\Gamma = D Q^2$, where $D$ is the diffusion coefficient (in m$^2$/s) and $Q$ is the momentum transfer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"from easydynamics.sample_model.diffusion_model.delta_lorentz import DeltaLorentz\n",
"\n",
"%matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d6e1984",
"metadata": {},
"outputs": [],
"source": [
"Q = np.linspace(0.5, 2, 7)\n",
"energy = np.linspace(-2, 2, 501)\n",
"scale = 1.0\n",
"mean_u_squared = 0.5\n",
"A_0 = 0.2\n",
"lorentzian_width = 0.2\n",
"\n",
"diffusion_model = DeltaLorentz(\n",
" scale=scale,\n",
" mean_u_squared=mean_u_squared,\n",
" A_0=A_0,\n",
" lorentzian_width=lorentzian_width,\n",
" allow_Q_variation={'A_0': True},\n",
" Q=Q,\n",
" lorentzian_name='Lorentzian',\n",
" delta_name='Delta function',\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "389aa3cd",
"metadata": {},
"outputs": [],
"source": [
"component_collections = diffusion_model.get_component_collections()\n",
"cmap = plt.cm.jet\n",
"nQ = len(component_collections)\n",
"plt.figure()\n",
"for Q_index in range(len(component_collections)):\n",
" color = cmap(Q_index / (nQ - 1))\n",
" y = component_collections[Q_index].evaluate(energy)\n",
" plt.plot(energy, y, label=f'Q={Q[Q_index]} Å^-1', color=color)\n",
"\n",
"plt.legend()\n",
"plt.show()\n",
"plt.xlabel('Energy (meV)')\n",
"plt.ylabel('Intensity (arb. units)')\n",
"plt.title('Delta-Lorentz Model')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
12 changes: 5 additions & 7 deletions docs/docs/tutorials/diffusion_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"source": [
"# Diffusion Model\n",
"We support several standard models of diffusion. Here we show an example of Browniand Translational Diffusion, where the scattering is a Lorentzian with width ($\\Gamma$) given by $\\Gamma = D Q^2$, where $D$ is the diffusion coefficient (in m$^2$/s) and $Q$ is the momentum transfer."
"We support several standard models of diffusion. Here we show an example of Brownian Translational Diffusion, where the scattering is a Lorentzian with width ($\\Gamma$) given by $\\Gamma = D Q^2$, where $D$ is the diffusion coefficient (in m$^2$/s) and $Q$ is the momentum transfer."
]
},
{
Expand Down Expand Up @@ -41,12 +41,10 @@
"diffusion_coefficient = 2.4e-9 # m^2/s\n",
"\n",
"diffusion_model = BrownianTranslationalDiffusion(\n",
" display_name='DiffusionModel',\n",
" scale=scale,\n",
" diffusion_coefficient=diffusion_coefficient,\n",
" display_name='DiffusionModel', scale=scale, diffusion_coefficient=diffusion_coefficient, Q=Q\n",
")\n",
"\n",
"component_collections = diffusion_model.create_component_collections(Q)\n",
"component_collections = diffusion_model.get_component_collections()\n",
"\n",
"\n",
"cmap = plt.cm.jet\n",
Expand Down Expand Up @@ -87,7 +85,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "easydynamics_newbase",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -101,7 +99,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/tutorials/tutorial0_more_advanced.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"id": "4c8e97b7",
"metadata": {},
"source": [
"As before, we createa an `Experiment` to hold the data, and rebin it."
"As before, we create an `Experiment` to hold the data, and rebin it."
]
},
{
Expand Down
8 changes: 8 additions & 0 deletions src/easydynamics/analysis/fit_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def get_parameter_names(self) -> list[str]:
modes = self._get_modes()

if isinstance(self.model, DiffusionModelBase):
# This needs to be generalised.
# TODO: Generalise this for different diffusion models and modes. # noqa TD002 TD003
if 'delta' in modes:
return [f'{self.parameter_name} area' for mode in modes]

return [f'{self.parameter_name} {mode}' for mode in modes]

return [self.parameter_name]
Expand Down Expand Up @@ -292,6 +297,9 @@ def _build_diffusion_callable(self, mode: str) -> Callable:
if mode == 'width':
return lambda x, **_: model.calculate_width(x)

if mode == 'delta':
return lambda x, **_: model.calculate_EISF(x) * model.scale.value

raise ValueError(f'Unknown diffusion mode: {mode}')

def _get_modes(self) -> list[str]:
Expand Down
6 changes: 6 additions & 0 deletions src/easydynamics/sample_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from easydynamics.sample_model.diffusion_model.brownian_translational_diffusion import (
BrownianTranslationalDiffusion,
)
from easydynamics.sample_model.diffusion_model.delta_lorentz import DeltaLorentz
from easydynamics.sample_model.diffusion_model.jump_translational_diffusion import (
JumpTranslationalDiffusion,
)
from easydynamics.sample_model.instrument_model import InstrumentModel
from easydynamics.sample_model.resolution_model import ResolutionModel
from easydynamics.sample_model.sample_model import SampleModel
Expand All @@ -26,10 +30,12 @@
'ComponentCollection',
'DampedHarmonicOscillator',
'DeltaFunction',
'DeltaLorentz',
'Exponential',
'ExpressionComponent',
'Gaussian',
'InstrumentModel',
'JumpTranslationalDiffusion',
'Lorentzian',
'Polynomial',
'ResolutionModel',
Expand Down
30 changes: 5 additions & 25 deletions src/easydynamics/sample_model/component_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
Raises
------
TypeError
If unit is not a string or sc.Unit, or if components is not a list of ModelComponent.
If components is not a list of ModelComponent.
"""
if components is None:
components = []
Expand Down Expand Up @@ -340,29 +340,6 @@ def free_all_parameters(self) -> None:
# Dunder methods
# ------------------------------------------------------------------

def __contains__(self, item: str | ModelComponent) -> bool:
"""
Check if a component with the given name or instance exists in the ComponentCollection.

Parameters
----------
item : str | ModelComponent
The component name or instance to check for.

Returns
-------
bool
True if the component exists, False otherwise.
"""

if isinstance(item, str):
# Check by component name
return any(comp.name == item for comp in self)
if isinstance(item, ModelComponent):
# Check by component instance
return any(comp is item for comp in self)
return False

def __repr__(self) -> str:
"""
Return a string representation of the ComponentCollection.
Expand All @@ -374,7 +351,10 @@ def __repr__(self) -> str:
"""
comp_names = ', '.join(c.name for c in self) or 'No components'

return f"<ComponentCollection name='{self.name}' | Components: {comp_names}>"
return (
f"ComponentCollection(name='{self.name}', unit='{self.unit}', \n"
f'Components: {comp_names})'
)

def to_dict(self) -> dict:
return {
Expand Down
Loading