Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1d5643a
initial analysis class
henrikjacobsenfys Feb 3, 2026
5e460ce
make analysis_base
henrikjacobsenfys Feb 6, 2026
74629d8
test things in notebook
henrikjacobsenfys Feb 6, 2026
9af6322
reintroduce energy_offset in convolution. It's needed.
henrikjacobsenfys Feb 6, 2026
ba85d95
Progress on Analysis
henrikjacobsenfys Feb 6, 2026
1c77784
multiple parameters with same unique_name?????
henrikjacobsenfys Feb 8, 2026
8c37ee4
fitting and plotting for multiple Q
henrikjacobsenfys Feb 10, 2026
b477a38
analysis MWP
henrikjacobsenfys Feb 11, 2026
08cdef2
Add plotting of parameters and examples
henrikjacobsenfys Feb 12, 2026
02158a1
Update failing tests
henrikjacobsenfys Feb 12, 2026
b9d3fec
Instrument model (#94)
henrikjacobsenfys Feb 5, 2026
248773d
initial analysis class
henrikjacobsenfys Feb 3, 2026
ce9acf8
fix merge conflict
henrikjacobsenfys Feb 12, 2026
f16a7e3
Remove notebook
henrikjacobsenfys Feb 12, 2026
7a31120
Update notebook, remove unused file
henrikjacobsenfys Feb 12, 2026
10d085e
pixi run fix
henrikjacobsenfys Feb 12, 2026
4e3d1c4
add missing tests
henrikjacobsenfys Feb 12, 2026
703d824
More missing tests
henrikjacobsenfys Feb 12, 2026
e343767
test analysis_base
henrikjacobsenfys Feb 12, 2026
fb7682c
100% coverage of base
henrikjacobsenfys Feb 12, 2026
51194a5
Test analysis1d
henrikjacobsenfys Feb 16, 2026
0972807
Another test
henrikjacobsenfys Feb 16, 2026
d054345
More analysis1d tests
henrikjacobsenfys Feb 17, 2026
9c01ffc
linting
henrikjacobsenfys Feb 17, 2026
61f49bd
update component_collection among other things
henrikjacobsenfys Feb 18, 2026
5178525
Add a few more tests
henrikjacobsenfys Feb 18, 2026
ad0689b
fix failing test
henrikjacobsenfys Feb 18, 2026
1b31537
Update analyis example
henrikjacobsenfys Feb 22, 2026
9b4548c
analysis tests
henrikjacobsenfys Feb 23, 2026
21da9d3
Minor fixes and tests
henrikjacobsenfys Feb 23, 2026
5d01a44
one more test
henrikjacobsenfys Feb 23, 2026
2c16a51
more tests
henrikjacobsenfys Feb 23, 2026
60aefa2
Update docstrings for AnalysisBase
henrikjacobsenfys Feb 25, 2026
9f381a1
pixi run fix
henrikjacobsenfys Feb 25, 2026
5e46e46
pixi run fix
henrikjacobsenfys Feb 25, 2026
918d6b9
more docstring for analysis1d
henrikjacobsenfys Feb 25, 2026
c9d4770
finish analysis1d docstring
henrikjacobsenfys Feb 25, 2026
7385c9a
react to PR comments
henrikjacobsenfys Feb 25, 2026
9c48b80
Update analysis.py docstrings
henrikjacobsenfys Feb 25, 2026
f49abdb
Handle changes to experiment etc in analysis
henrikjacobsenfys Feb 25, 2026
6af16cd
fix updaters
henrikjacobsenfys Feb 25, 2026
ee21733
More tests and response to PR comments
henrikjacobsenfys Feb 27, 2026
057dc32
minor PR comments
henrikjacobsenfys Feb 27, 2026
315a1cd
fix test
henrikjacobsenfys Feb 27, 2026
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
352 changes: 352 additions & 0 deletions docs/docs/tutorials/analysis.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8643b10c",
"metadata": {},
"source": [
"# Analysis\n",
"It is time to analyse some data. We here show how to set up an Analysis object and use it to first fit an artificial vanadium measurement. Next, we use the fitted resolution to fit an artificial measurement of a model with diffusion and some elastic scattering. \n",
"\n",
"We extract and plot the relevant parameters. Finally, we show how to fit directly to the diffusion model.\n",
"\n",
"In the near future, it will be possible to fit the width and area of the Lorentzian to the diffusion model as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bca91d3c",
"metadata": {},
"outputs": [],
"source": [
"# Imports\n",
"from easydynamics.analysis.analysis import Analysis\n",
"from easydynamics.experiment import Experiment\n",
"from easydynamics.sample_model import BrownianTranslationalDiffusion\n",
"from easydynamics.sample_model import ComponentCollection\n",
"from easydynamics.sample_model import DeltaFunction\n",
"from easydynamics.sample_model import Gaussian\n",
"from easydynamics.sample_model import Lorentzian\n",
"from easydynamics.sample_model import Polynomial\n",
"from easydynamics.sample_model.background_model import BackgroundModel\n",
"from easydynamics.sample_model.instrument_model import InstrumentModel\n",
"from easydynamics.sample_model.resolution_model import ResolutionModel\n",
"from easydynamics.sample_model.sample_model import SampleModel\n",
"\n",
"%matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8deca9b6",
"metadata": {},
"outputs": [],
"source": [
"# Load the vanadium data\n",
"vanadium_experiment = Experiment('Vanadium')\n",
"vanadium_experiment.load_hdf5(filename='vanadium_data_example.h5')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6762faba",
"metadata": {},
"outputs": [],
"source": [
"# Example of Analysis with a simple sample model and instrument model\n",
"# The scattering from vanadium is purely elastic, so we model it with a\n",
"# delta function\n",
"delta_function = DeltaFunction(display_name='DeltaFunction', area=1)\n",
"sample_model = SampleModel(\n",
" components=delta_function,\n",
")\n",
"\n",
"# The resolution is in this case modeled as a Gaussian. However, we can\n",
"# add as many components as we like to the resolution model\n",
"res_gauss = Gaussian(width=0.1)\n",
"res_gauss.area.fixed = True\n",
"resolution_components = ComponentCollection()\n",
"resolution_components.append_component(res_gauss)\n",
"resolution_model = ResolutionModel(components=resolution_components)\n",
"\n",
"# The background model is created in the same way. In this case, we use\n",
"# a flat background\n",
"background_model = BackgroundModel(components=Polynomial(coefficients=[0.001]))\n",
"\n",
"# We combine the resolution abd background model into an instrument\n",
"# model. This model also contains a small energy offset to account for\n",
"# instrument misalignment.\n",
"\n",
"instrument_model = InstrumentModel(\n",
" resolution_model=resolution_model,\n",
" background_model=background_model,\n",
")\n",
"\n",
"# Collect everything into an analysis object.\n",
"vanadium_analysis = Analysis(\n",
" display_name='Vanadium Full Analysis',\n",
" experiment=vanadium_experiment,\n",
" sample_model=sample_model,\n",
" instrument_model=instrument_model,\n",
")\n",
"\n",
"# Let us first fit a single Q index and plot the data and model to see\n",
"# how it looks\n",
"fit_result_independent_single_Q = vanadium_analysis.fit(fit_method='independent', Q_index=5)\n",
"vanadium_analysis.plot_data_and_model(Q_index=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e98e3d65",
"metadata": {},
"outputs": [],
"source": [
"# It looks good, so let us fit all Q indices independently and plot the\n",
"# results\n",
"fit_result_independent_all_Q = vanadium_analysis.fit(fit_method='independent')\n",
"vanadium_analysis.plot_data_and_model()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "133e682e",
"metadata": {},
"outputs": [],
"source": [
"# Inspect the Parameters as a scipp Dataset\n",
"vanadium_analysis.parameters_to_dataset()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dfacdf24",
"metadata": {},
"outputs": [],
"source": [
"# Plot some of fitted parameters as a function of Q\n",
"vanadium_analysis.plot_parameters(names=['DeltaFunction area'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b6f9f316",
"metadata": {},
"outputs": [],
"source": [
"vanadium_analysis.plot_parameters(names=['Gaussian width'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "572664a0",
"metadata": {},
"outputs": [],
"source": [
"vanadium_analysis.plot_parameters(names=['energy_offset'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3609e6c1",
"metadata": {},
"outputs": [],
"source": [
"# Now it's time to look at the data we want to fit. We first load the\n",
"# data\n",
"diffusion_experiment = Experiment('Diffusion')\n",
"diffusion_experiment.load_hdf5(filename='diffusion_data_example.h5')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e685909a",
"metadata": {},
"outputs": [],
"source": [
"# Now we set up the model, similarly to how we set up the model for the\n",
"# vanadium data.\n",
"delta_function = DeltaFunction(display_name='DeltaFunction', area=0.2)\n",
"lorentzian = Lorentzian(display_name='Lorentzian', area=0.5, width=0.3)\n",
"component_collection = ComponentCollection(\n",
" components=[delta_function, lorentzian],\n",
")\n",
"\n",
"sample_model = SampleModel(\n",
" components=component_collection,\n",
")\n",
"\n",
"background_model = BackgroundModel(components=Polynomial(coefficients=[0.001]))\n",
"\n",
"instrument_model = InstrumentModel(\n",
" background_model=background_model,\n",
")\n",
"\n",
"diffusion_analysis = Analysis(\n",
" display_name='Diffusion Full Analysis',\n",
" experiment=diffusion_experiment,\n",
" sample_model=sample_model,\n",
" instrument_model=instrument_model,\n",
")\n",
"\n",
"# We need to hack in the resolution model from the vanadium analysis,\n",
"# since the setters and getters overwrite the model. This will be fixed\n",
"# asap.\n",
"diffusion_analysis.instrument_model._resolution_model = (\n",
" vanadium_analysis.instrument_model.resolution_model\n",
")\n",
"\n",
"# We fix all parameters of the resolution model.\n",
"diffusion_analysis.instrument_model.resolution_model.fix_all_parameters()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c66828eb",
"metadata": {},
"outputs": [],
"source": [
"# Let us see how good the starting parameters are\n",
"diffusion_analysis.plot_data_and_model()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a4b7572",
"metadata": {},
"outputs": [],
"source": [
"# Now we fit the data and plot the result. Looks good!\n",
"diffusion_analysis.fit(fit_method='independent')\n",
"diffusion_analysis.plot_data_and_model()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df14b5c4",
"metadata": {},
"outputs": [],
"source": [
"# Let us look at the most interesting fit parameters\n",
"diffusion_analysis.plot_parameters(names=['Lorentzian width', 'Lorentzian area'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb226c8f",
"metadata": {},
"outputs": [],
"source": [
"# It will be possible to fit this to a DiffusionModel, but that will\n",
"# come later."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c6d7808",
"metadata": {},
"outputs": [],
"source": [
"# Let us now fit directly to a diffusion model. We replace the\n",
"# Lorentzian with a Brownian translational diffusion model and keep the\n",
"# other parameters the same.\n",
"delta_function = DeltaFunction(display_name='DeltaFunction', area=0.2)\n",
"component_collection = ComponentCollection(\n",
" components=[delta_function],\n",
")\n",
"diffusion_model = BrownianTranslationalDiffusion(\n",
" display_name='Brownian Translational Diffusion', diffusion_coefficient=2.4e-9, scale=0.5\n",
")\n",
"\n",
"sample_model = SampleModel(\n",
" components=component_collection,\n",
" diffusion_models=diffusion_model,\n",
")\n",
"\n",
"background_model = BackgroundModel(components=Polynomial(coefficients=[0.001]))\n",
"\n",
"instrument_model = InstrumentModel(\n",
" background_model=background_model,\n",
")\n",
"\n",
"diffusion_model_analysis = Analysis(\n",
" display_name='Diffusion Full Analysis',\n",
" experiment=diffusion_experiment,\n",
" sample_model=sample_model,\n",
" instrument_model=instrument_model,\n",
")\n",
"\n",
"# We again need to hack in the resolution model from the vanadium\n",
"# analysis, since the setters and getters overwrite the model. This will\n",
"# be fixed asap.\n",
"diffusion_model_analysis.instrument_model._resolution_model = (\n",
" vanadium_analysis.instrument_model.resolution_model\n",
")\n",
"diffusion_model_analysis.instrument_model.resolution_model.fix_all_parameters()\n",
"\n",
"# Let us see how good the starting parameters are\n",
"diffusion_model_analysis.plot_data_and_model()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd04d359",
"metadata": {},
"outputs": [],
"source": [
"# We now fit all the data simultaneously to the diffusion model, then\n",
"# plot the result. Looks good.\n",
"diffusion_model_analysis.fit(fit_method='simultaneous')\n",
"diffusion_model_analysis.plot_data_and_model()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "842c1f01",
"metadata": {},
"outputs": [],
"source": [
"# Let us look at the fitted diffusion coefficient\n",
"diffusion_model.get_all_parameters()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "easydynamics_newbase",
"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.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading