Skip to content

Commit

Permalink
docs: update usage notebooks new sympy design
Browse files Browse the repository at this point in the history
  • Loading branch information
spflueger authored and redeboer committed Feb 17, 2021
1 parent 65c4f0b commit 0b2a27a
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 685 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.json
*.npy
*.pdf
*.pickle
*.png
*.svg
*.v2
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"itertools",
"jsonschema",
"jupyter",
"lineshape",
"mathbb",
"matplotlib",
"mkdir",
Expand Down
2 changes: 0 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ glob:
usage/1_create_model
usage/2_generate_data
usage/3_perform_fit
usage/sympy
usage/backends
```
43 changes: 36 additions & 7 deletions docs/usage/1_create_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we convert the {attr}`~expertsystem.reaction.Result.transitions` into an {class}`~expertsystem.amplitude.model.AmplitudeModel`. This can be done with the {func}`~expertsystem.generate_amplitudes` method."
"Next we convert the {attr}`~expertsystem.reaction.Result.transitions` into an {class}`~expertsystem.amplitude.helicity.HelicityModel`. This can be done with the {func}`~expertsystem.generate_amplitudes` method."
]
},
{
Expand All @@ -92,7 +92,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that we have to specify the dynamics for the resonances. We choose to use {class}`~expertsystem.amplitude.model.RelativisticBreitWigner` for all resonances:"
"The heart of the model is a sympy expression that contains the full description of the intensity model. Note two things:\n",
"1. The coefficients for the different amplitudes are **complex** valued.\n",
"2. By default there is no dynamics in the model and it still has to be specified.\n",
"\n",
"We choose to use {func}`~expertsystem.amplitude.dynamics.lineshape.relativistic_breit_wigner_with_ff` for all resonances. In the HelicityModel `~expertsystem.amplitude.dynamics.set_resonance_dynamics` is an convenience interface for replacing the dynamics for intermediate states."
]
},
{
Expand All @@ -101,16 +105,38 @@
"metadata": {},
"outputs": [],
"source": [
"from expertsystem.amplitude.dynamics import set_resonance_dynamics\n",
"from expertsystem.amplitude.dynamics.builder import (\n",
" create_relativistic_breit_wigner_with_ff,\n",
")\n",
"\n",
"for name in result.get_intermediate_particles().names:\n",
" model.dynamics.set_breit_wigner(name)\n",
"{name: type(dyn) for name, dyn in model.dynamics.items()}"
" set_resonance_dynamics(\n",
" model, name, create_relativistic_breit_wigner_with_ff\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's take another look at the parameters of the model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(model.parameters)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we can write the [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel), using the [io.write](expertsystem.io.write) function:"
"Finally, we can write the [HelicityModel](expertsystem.amplitude.helicity.HelicityModel) via `pickle` to file:"
]
},
{
Expand All @@ -119,14 +145,17 @@
"metadata": {},
"outputs": [],
"source": [
"es.io.write(model, \"amplitude_model_helicity.yml\")"
"import pickle\n",
"\n",
"with open(\"helicity_model.pickle\", \"wb\") as model_file:\n",
" pickle.dump(model, model_file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cool, that's it! We now have a recipe for an amplitude model with which to [generate data](./2_generate_data) and [perform a fit](./3_perform_fit)! In the [next steps](./2_generate_data), we will use use this [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel) as a fit model template for [tensorwaves](tensorwaves)."
"Cool, that's it! We now have a recipe for an amplitude model with which to [generate data](./2_generate_data) and [perform a fit](./3_perform_fit)! In the [next steps](./2_generate_data), we will use use this {class}`~expertsystem.amplitude.helicity.HelicityModel` as a fit model template for {mod}`tensorwaves`."
]
}
],
Expand Down
100 changes: 87 additions & 13 deletions docs/usage/2_generate_data.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"%config Completer.use_jedi = False"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -11,9 +27,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section, we will use the [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel) that we created with the expert system in [the previous step](./1_create_model) to generate a data sample via hit & miss Monte Carlo. We do this with the [tensorwaves.data.generate](tensorwaves.data.generate) module.\n",
"In this section, we will use the {class}`~expertsystem.amplitude.helicity.HelicityModel` that we created with the expert system in [the previous step](./1_create_model) to generate a data sample via hit & miss Monte Carlo. We do this with the {mod}`tensorwaves.data.generate` module.\n",
"\n",
"First, we {func}`~.expertsystem.io.load` an {class}`~expertsystem.amplitude.model.AmplitudeModel` that was created in the previous step:"
"First, we {func}`~.expertsystem.io.load` an {class}`~expertsystem.amplitude.helicity.HelicityModel` that was created in the previous step:"
]
},
{
Expand All @@ -22,9 +38,25 @@
"metadata": {},
"outputs": [],
"source": [
"from expertsystem import io\n",
"# TODO: pickling of the HelicityModel does not work, so we have to currently redo all steps from before...\n",
"import expertsystem as es\n",
"\n",
"model = io.load(\"amplitude_model_helicity.yml\")"
"result = es.generate_transitions(\n",
" initial_state=(\"J/psi(1S)\", [-1, +1]),\n",
" final_state=[\"gamma\", \"pi0\", \"pi0\"],\n",
" allowed_intermediate_particles=[\"f(0)\"],\n",
" allowed_interaction_types=\"strong and EM\",\n",
")\n",
"model = es.generate_amplitudes(result)\n",
"from expertsystem.amplitude.dynamics import set_resonance_dynamics\n",
"from expertsystem.amplitude.dynamics.builder import (\n",
" create_relativistic_breit_wigner_with_ff,\n",
")\n",
"\n",
"for name in result.get_intermediate_particles().names:\n",
" set_resonance_dynamics(\n",
" model, name, create_relativistic_breit_wigner_with_ff\n",
" )"
]
},
{
Expand All @@ -38,7 +70,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"An [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel) defines the kinematics, the particles involved in the reaction, the dynamics used for the model on which to perform the eventual optimization, etc."
"A {class}`~expertsystem.amplitude.helicity.HelicityModel` defines the kinematics, the particles involved in the reaction, the dynamics used for the model on which to perform the eventual optimization, etc."
]
},
{
Expand All @@ -47,11 +79,36 @@
"metadata": {},
"outputs": [],
"source": [
"# TODO: this part will be changed once the kinematics is ported to the expertsystem!\n",
"from tensorwaves.physics.helicity_formalism.kinematics import (\n",
" HelicityKinematics,\n",
" ParticleReactionKinematicsInfo,\n",
" SubSystem,\n",
")\n",
"\n",
"kin = HelicityKinematics(\n",
" ParticleReactionKinematicsInfo(\n",
" initial_state_names=[\n",
" x.name for x in model.kinematics.initial_state.values()\n",
" ],\n",
" final_state_names=[\n",
" x.name for x in model.kinematics.final_state.values()\n",
" ],\n",
" particles=model.particles,\n",
" fs_id_event_pos_mapping=dict(\n",
" {k: i for i, k in enumerate(model.kinematics.final_state.keys())}\n",
" ),\n",
" )\n",
")\n",
"kin.register_subsystem(SubSystem([[3, 4], [2]], [], []))\n",
"kin.register_subsystem(SubSystem([[3], [4]], [2], []))\n",
"kin.register_invariant_mass([2, 4])\n",
"\n",
"import pickle\n",
"\n",
"with open(\"kinematics.pickle\", \"wb\") as kin_file:\n",
" pickle.dump(kin, kin_file)\n",
"\n",
"kin = HelicityKinematics.from_model(model)\n",
"print(\"Initial state mass:\", kin.reaction_kinematics_info.initial_state_masses)\n",
"print(\"Final state masses:\", kin.reaction_kinematics_info.final_state_masses)\n",
"print(\"Involved particles:\", model.particles.names)"
Expand Down Expand Up @@ -110,9 +167,26 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"'Data samples' are more complicated than phase space samples in that they represent the intensity profile resulting from a reaction. You therefore need an [IntensityTF](tensorwaves.physics.helicity_formalism.amplitude.IntensityTF) object (or, more generally, a [Function](tensorwaves.interfaces.Function) instance) and a phase space over which to generate that intensity distribution. We call such a data sample an **intensity-based sample**.\n",
"'Data samples' are more complicated than phase space samples in that they represent the intensity profile resulting from a reaction. You therefore need an {class}`~tensorwaves.physics.amplitude.Intensity` object (or, more generally, a [Function](tensorwaves.interfaces.Function) instance) and a phase space over which to generate that intensity distribution. We call such a data sample an **intensity-based sample**.\n",
"\n",
"An intensity-based sample is generated with the function [generate_data](tensorwaves.data.generate.generate_data). Its usage is similar to [generate_phsp](tensorwaves.data.generate.generate_phsp), but now you have to give an [Intensity](tensorwaves.physics.amplitude.Intensity) in addition to the [Kinematics](tensorwaves.interfaces.Kinematics) object. An [Intensity](tensorwaves.physics.amplitude.Intensity) object can be created with the [SympyModel](tensorwaves.physics.amplitude.SympyModel) class:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorwaves.physics.amplitude import Intensity, SympyModel\n",
"\n",
"sympy_model = SympyModel(\n",
" expression=model.expression.full_expression,\n",
" parameters={k: v.value for k, v in model.parameters.items()},\n",
" variables={},\n",
")\n",
"\n",
"An intensity-based sample is generated with the function [generate_data](tensorwaves.data.generate.generate_data). Its usage is similar to [generate_phsp](tensorwaves.data.generate.generate_phsp), but now you have to give an [IntensityTF](tensorwaves.physics.helicity_formalism.amplitude.IntensityTF) in addition to the [Kinematics](tensorwaves.interfaces.Kinematics) object. An [IntensityTF](tensorwaves.physics.helicity_formalism.amplitude.IntensityTF) object can be created with the [IntensityBuilder](tensorwaves.physics.helicity_formalism.amplitude.IntensityBuilder) class:"
"intensity = Intensity(sympy_model)"
]
},
{
Expand All @@ -121,10 +195,10 @@
"metadata": {},
"outputs": [],
"source": [
"from tensorwaves.physics.helicity_formalism.amplitude import IntensityBuilder\n",
"import pickle\n",
"\n",
"builder = IntensityBuilder(model.particles, kin)\n",
"intensity = builder.create_intensity(model)"
"with open(\"sympy_model.pickle\", \"wb\") as model_file:\n",
" pickle.dump(sympy_model, model_file)"
]
},
{
Expand Down Expand Up @@ -177,7 +251,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The data set is just a [dict](dict) of kinematic variables (keys are the names, values is a list of computed values for each event). The numbers you see here are final state IDs as defined in the [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel) member of the [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel):"
"The data set is just a [dict](dict) of kinematic variables (keys are the names, values is a list of computed values for each event). The numbers you see here are final state IDs as defined in the {class}`~expertsystem.amplitude.helicity.HelicityModel` member of the {class}`~expertsystem.amplitude.helicity.HelicityModel`:"
]
},
{
Expand Down Expand Up @@ -301,7 +375,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In the [next step](./3_perform_fit), we will illustrate how to 'perform a fit' with [tensorwaves](tensorwaves) by optimizing the intensity model to these data samples."
"In the [next step](./3_perform_fit), we will illustrate how to 'perform a fit' with {mod}`tensorwaves` by optimizing the intensity model to these data samples."
]
}
],
Expand Down
Loading

0 comments on commit 0b2a27a

Please sign in to comment.