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
80 changes: 77 additions & 3 deletions docs/docs/tutorials/tutorial0_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"\n",
"import easydynamics as edyn\n",
"import easydynamics.sample_model as sm\n",
"from easydynamics.analysis.analysis import Analysis\n",
"from easydynamics.analysis import Analysis\n",
"from easydynamics.analysis import ParameterAnalysis\n",
"from easydynamics.analysis.parameter_analysis import FitBinding\n",
"\n",
"# Make the plots interactive\n",
"%matplotlib widget"
Expand Down Expand Up @@ -322,7 +324,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(f'The reduced chi-squared value for Q_index=5 is: {fit_result_all_Q[5].reduced_chi}')\n",
"print(f'The reduced chi-squared value for Q_index=5 is: {fit_result_all_Q[5].reduced_chi2}')\n",
"\n",
"print(f'The minimizer engine is: {fit_result_all_Q[5].minimizer_engine}')"
]
Expand Down Expand Up @@ -368,7 +370,79 @@
"id": "842c1f01",
"metadata": {},
"source": [
"It will soon be possible to use **EasyDynamics** to fit these parameters to e.g. a polynomial."
"The final step in this tutorial is to fit the are of the `Gaussian` to a straight line. For this, we use the `ParameterAnalysis` class. We create a `Polynomial` with two coefficients for the fit function. We create a `FitBinding`, telling the class we want to fit the parameter named `Gaussian area` with the fit function that we define."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75db3d4c",
"metadata": {},
"outputs": [],
"source": [
"fit_func = sm.Polynomial(coefficients=[3.7, -0.5], display_name='Straight line')\n",
"\n",
"binding = FitBinding(parameter_name='Gaussian area', model=fit_func)\n",
"\n",
"parameter_analysis = ParameterAnalysis(\n",
" parameters=analysis,\n",
" bindings=[binding],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "01f45034",
"metadata": {},
"source": [
"Let us plot the start guess using the `plot()` method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd3cf4a6",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.plot()"
]
},
{
"cell_type": "markdown",
"id": "634bebdc",
"metadata": {},
"source": [
"It looks decent, so we can fit and plot again:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5ba8985",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.fit()\n",
"parameter_analysis.plot()"
]
},
{
"cell_type": "markdown",
"id": "dc33728c",
"metadata": {},
"source": [
"To see the parameters we can use the `get_all_parameters()` method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f18e2944",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.get_all_parameters()"
]
}
],
Expand Down
60 changes: 59 additions & 1 deletion docs/docs/tutorials/tutorial0_more_advanced.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,65 @@
"metadata": {},
"outputs": [],
"source": [
"analysis.plot_parameters(names=['Gaussian area', 'Lorentzian area', 'DHO area'])"
"analysis.plot_parameters(names=['Gaussian area', 'DHO area', 'DHO center'])"
]
},
{
"cell_type": "markdown",
"id": "0eadbd91",
"metadata": {},
"source": [
"With apologies for the lack of creativity, these all appear like straight lines. We can fit them individually or all together using `ParameterAnalysis`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb2f0e06",
"metadata": {},
"outputs": [],
"source": [
"gauss_fit_func = sm.Polynomial(\n",
" coefficients=[3.7, -0.5], unit='1/angstrom', display_name='Gauss area fit'\n",
")\n",
"dho_area_fit_func = sm.Polynomial(\n",
" coefficients=[2.0, 0.12], unit='1/angstrom', display_name='DHO area fit'\n",
")\n",
"dho_center_fit_func = sm.Polynomial(\n",
" coefficients=[1.1, 0.2], unit='1/angstrom', display_name='DHO center fit'\n",
")\n",
"\n",
"binding1 = edyn.FitBinding(parameter_name='Gaussian area', model=gauss_fit_func)\n",
"\n",
"binding2 = edyn.FitBinding(parameter_name='DHO area', model=dho_area_fit_func)\n",
"\n",
"binding3 = edyn.FitBinding(parameter_name='DHO center', model=dho_center_fit_func)\n",
"\n",
"parameter_analysis = edyn.ParameterAnalysis(\n",
" parameters=analysis,\n",
" bindings=[binding1, binding2, binding3],\n",
")\n",
"\n",
"parameter_analysis.plot()"
]
},
{
"cell_type": "markdown",
"id": "32bc1efc",
"metadata": {},
"source": [
"The start guesses look reasonable, so we fit:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5548093",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.fit()\n",
"parameter_analysis.plot()"
]
}
],
Expand Down
125 changes: 121 additions & 4 deletions docs/docs/tutorials/tutorial1_brownian.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"import pooch\n",
"\n",
"from easydynamics.analysis.analysis import Analysis\n",
"from easydynamics.analysis.parameter_analysis import FitBinding\n",
"from easydynamics.analysis.parameter_analysis import ParameterAnalysis\n",
"from easydynamics.experiment import Experiment\n",
"from easydynamics.sample_model import BrownianTranslationalDiffusion\n",
"from easydynamics.sample_model import ComponentCollection\n",
Expand Down Expand Up @@ -460,7 +462,7 @@
"id": "45daa848",
"metadata": {},
"source": [
"The fit looks good, so now we want to look at the most interesting fit parameters: the width and area of the Lorentzian. In later versions of EasyDynamics it will be possible to fit them to e.g. a DiffusionModel."
"The fit looks good, so now we want to look at the most interesting fit parameters: the width and area of the Lorentzian. "
]
},
{
Expand All @@ -470,7 +472,6 @@
"metadata": {},
"outputs": [],
"source": [
"# Let us look at the most interesting fit parameters\n",
"diffusion_analysis.plot_parameters(names=['Lorentzian width', 'Lorentzian area'])"
]
},
Expand All @@ -485,7 +486,105 @@
"$$\n",
"where $\\Gamma(Q) = D Q^2$ and $D$ is the diffusion coefficient. $S$ is an overall scale.\n",
"\n",
"In addition to this diffusion model, there is still the elastic incoherent scattering.\n",
"To fit the Brownian translational diffusion model to the data, we use the `ParameterAnalysis`. This time, we wish to fit the `Lorentzian`, and we wish to fit both its area (scale, $S$) and width to the diffusion model. We do this by creating a `FitBinding`, saying we want to fit both the area an width of the component called `Lorentzian`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c1ab6b0",
"metadata": {},
"outputs": [],
"source": [
"brownian_diffusion_model = BrownianTranslationalDiffusion(\n",
" display_name='Brownian Translational Diffusion', diffusion_coefficient=2.4e-9, scale=0.5\n",
")\n",
"\n",
"binding = FitBinding(\n",
" parameter_name='Lorentzian',\n",
" model=brownian_diffusion_model,\n",
" modes=['area', 'width'],\n",
")\n",
"\n",
"parameter_analysis = ParameterAnalysis(\n",
" parameters=diffusion_analysis,\n",
" bindings=[binding],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "53dc12dc",
"metadata": {},
"source": [
"We first plot the start guess to see if it's reasonable."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7074c64",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57b76d06",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.plot(names=['Polynomial_c0'])"
]
},
{
"cell_type": "markdown",
"id": "64babb01",
"metadata": {},
"source": [
"This looks pretty good. Let's fit and plot again:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26c418ef",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.fit()\n",
"parameter_analysis.plot()"
]
},
{
"cell_type": "markdown",
"id": "81d30f55",
"metadata": {},
"source": [
"And we can get the parameters of the diffusion model:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c47bcd4",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.get_all_parameters()"
]
},
{
"cell_type": "markdown",
"id": "fc2f8434",
"metadata": {},
"source": [
"This is a good result, but we can do better. Now that we know that the quasielastic scattering is well described by a model of diffusion, we can fit this model directly to the data, fitting all $Q$ simultaneously.\n",
"\n",
"In addition to this diffusion model, we will still fit the elastic incoherent scattering.\n",
"\n",
"We create a new `SampleModel` which as a `DeltaFunction` component for the elastic incoherent scattering and a `BrownianTranslationalDiffusion` diffusion model to describe the rest. We also create a new `BackgroundModel` and `InstrumentModel`."
]
Expand Down Expand Up @@ -593,7 +692,7 @@
"id": "bbeec088",
"metadata": {},
"source": [
"It does not make sense to plot the diffusion parameters, but we can display them (with uncertainties) like this."
"It does not make sense to plot the diffusion parameters, as they are just two numbers with uncertainties, but we can display them (with uncertainties) like this."
]
},
{
Expand All @@ -605,6 +704,24 @@
"source": [
"diffusion_model.get_all_parameters()"
]
},
{
"cell_type": "markdown",
"id": "fc9bf6b1",
"metadata": {},
"source": [
"For reference, here are the diffusion parameters found from fitting the width and scale of the fitted Lorentzians. Notice that the error bars when fitting everything simultaneously are a lot lower than the two-step fit."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0af81fa8",
"metadata": {},
"outputs": [],
"source": [
"parameter_analysis.get_all_parameters()"
]
}
],
"metadata": {
Expand Down
Loading
Loading