Skip to content

Commit d52245a

Browse files
spfluegerredeboer
authored andcommitted
docs: update usage notebooks new sympy design
1 parent 646de8e commit d52245a

File tree

9 files changed

+159
-685
lines changed

9 files changed

+159
-685
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.json
44
*.npy
55
*.pdf
6+
*.pickle
67
*.png
78
*.svg
89
*.v2

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"itertools",
7474
"jsonschema",
7575
"jupyter",
76+
"lineshape",
7677
"mathbb",
7778
"matplotlib",
7879
"mkdir",

docs/usage.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,4 @@ glob:
1313
usage/1_create_model
1414
usage/2_generate_data
1515
usage/3_perform_fit
16-
usage/sympy
17-
usage/backends
1816
```

docs/usage/1_create_model.ipynb

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"cell_type": "markdown",
7676
"metadata": {},
7777
"source": [
78-
"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."
78+
"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."
7979
]
8080
},
8181
{
@@ -92,7 +92,11 @@
9292
"cell_type": "markdown",
9393
"metadata": {},
9494
"source": [
95-
"Note that we have to specify the dynamics for the resonances. We choose to use {class}`~expertsystem.amplitude.model.RelativisticBreitWigner` for all resonances:"
95+
"The heart of the model is a sympy expression that contains the full description of the intensity model. Note two things:\n",
96+
"1. The coefficients for the different amplitudes are **complex** valued.\n",
97+
"2. By default there is no dynamics in the model and it still has to be specified.\n",
98+
"\n",
99+
"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."
96100
]
97101
},
98102
{
@@ -101,16 +105,38 @@
101105
"metadata": {},
102106
"outputs": [],
103107
"source": [
108+
"from expertsystem.amplitude.dynamics import set_resonance_dynamics\n",
109+
"from expertsystem.amplitude.dynamics.builder import (\n",
110+
" create_relativistic_breit_wigner_with_ff,\n",
111+
")\n",
112+
"\n",
104113
"for name in result.get_intermediate_particles().names:\n",
105-
" model.dynamics.set_breit_wigner(name)\n",
106-
"{name: type(dyn) for name, dyn in model.dynamics.items()}"
114+
" set_resonance_dynamics(\n",
115+
" model, name, create_relativistic_breit_wigner_with_ff\n",
116+
" )"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"Now let's take another look at the parameters of the model"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": null,
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"list(model.parameters)"
107133
]
108134
},
109135
{
110136
"cell_type": "markdown",
111137
"metadata": {},
112138
"source": [
113-
"Finally, we can write the [AmplitudeModel](expertsystem.amplitude.model.AmplitudeModel), using the [io.write](expertsystem.io.write) function:"
139+
"Finally, we can write the [HelicityModel](expertsystem.amplitude.helicity.HelicityModel) via `pickle` to file:"
114140
]
115141
},
116142
{
@@ -119,14 +145,17 @@
119145
"metadata": {},
120146
"outputs": [],
121147
"source": [
122-
"es.io.write(model, \"amplitude_model_helicity.yml\")"
148+
"import pickle\n",
149+
"\n",
150+
"with open(\"helicity_model.pickle\", \"wb\") as model_file:\n",
151+
" pickle.dump(model, model_file)"
123152
]
124153
},
125154
{
126155
"cell_type": "markdown",
127156
"metadata": {},
128157
"source": [
129-
"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)."
158+
"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`."
130159
]
131160
}
132161
],

docs/usage/2_generate_data.ipynb

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"jupyter": {
8+
"source_hidden": true
9+
},
10+
"tags": [
11+
"remove-cell"
12+
]
13+
},
14+
"outputs": [],
15+
"source": [
16+
"%config Completer.use_jedi = False"
17+
]
18+
},
319
{
420
"cell_type": "markdown",
521
"metadata": {},
@@ -11,9 +27,9 @@
1127
"cell_type": "markdown",
1228
"metadata": {},
1329
"source": [
14-
"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",
30+
"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",
1531
"\n",
16-
"First, we {func}`~.expertsystem.io.load` an {class}`~expertsystem.amplitude.model.AmplitudeModel` that was created in the previous step:"
32+
"First, we {func}`~.expertsystem.io.load` an {class}`~expertsystem.amplitude.helicity.HelicityModel` that was created in the previous step:"
1733
]
1834
},
1935
{
@@ -22,9 +38,25 @@
2238
"metadata": {},
2339
"outputs": [],
2440
"source": [
25-
"from expertsystem import io\n",
41+
"# TODO: pickling of the HelicityModel does not work, so we have to currently redo all steps from before...\n",
42+
"import expertsystem as es\n",
2643
"\n",
27-
"model = io.load(\"amplitude_model_helicity.yml\")"
44+
"result = es.generate_transitions(\n",
45+
" initial_state=(\"J/psi(1S)\", [-1, +1]),\n",
46+
" final_state=[\"gamma\", \"pi0\", \"pi0\"],\n",
47+
" allowed_intermediate_particles=[\"f(0)\"],\n",
48+
" allowed_interaction_types=\"strong and EM\",\n",
49+
")\n",
50+
"model = es.generate_amplitudes(result)\n",
51+
"from expertsystem.amplitude.dynamics import set_resonance_dynamics\n",
52+
"from expertsystem.amplitude.dynamics.builder import (\n",
53+
" create_relativistic_breit_wigner_with_ff,\n",
54+
")\n",
55+
"\n",
56+
"for name in result.get_intermediate_particles().names:\n",
57+
" set_resonance_dynamics(\n",
58+
" model, name, create_relativistic_breit_wigner_with_ff\n",
59+
" )"
2860
]
2961
},
3062
{
@@ -38,7 +70,7 @@
3870
"cell_type": "markdown",
3971
"metadata": {},
4072
"source": [
41-
"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."
73+
"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."
4274
]
4375
},
4476
{
@@ -47,11 +79,36 @@
4779
"metadata": {},
4880
"outputs": [],
4981
"source": [
82+
"# TODO: this part will be changed once the kinematics is ported to the expertsystem!\n",
5083
"from tensorwaves.physics.helicity_formalism.kinematics import (\n",
5184
" HelicityKinematics,\n",
85+
" ParticleReactionKinematicsInfo,\n",
86+
" SubSystem,\n",
87+
")\n",
88+
"\n",
89+
"kin = HelicityKinematics(\n",
90+
" ParticleReactionKinematicsInfo(\n",
91+
" initial_state_names=[\n",
92+
" x.name for x in model.kinematics.initial_state.values()\n",
93+
" ],\n",
94+
" final_state_names=[\n",
95+
" x.name for x in model.kinematics.final_state.values()\n",
96+
" ],\n",
97+
" particles=model.particles,\n",
98+
" fs_id_event_pos_mapping=dict(\n",
99+
" {k: i for i, k in enumerate(model.kinematics.final_state.keys())}\n",
100+
" ),\n",
101+
" )\n",
52102
")\n",
103+
"kin.register_subsystem(SubSystem([[3, 4], [2]], [], []))\n",
104+
"kin.register_subsystem(SubSystem([[3], [4]], [2], []))\n",
105+
"kin.register_invariant_mass([2, 4])\n",
106+
"\n",
107+
"import pickle\n",
108+
"\n",
109+
"with open(\"kinematics.pickle\", \"wb\") as kin_file:\n",
110+
" pickle.dump(kin, kin_file)\n",
53111
"\n",
54-
"kin = HelicityKinematics.from_model(model)\n",
55112
"print(\"Initial state mass:\", kin.reaction_kinematics_info.initial_state_masses)\n",
56113
"print(\"Final state masses:\", kin.reaction_kinematics_info.final_state_masses)\n",
57114
"print(\"Involved particles:\", model.particles.names)"
@@ -110,9 +167,26 @@
110167
"cell_type": "markdown",
111168
"metadata": {},
112169
"source": [
113-
"'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",
170+
"'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",
171+
"\n",
172+
"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:"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": null,
178+
"metadata": {},
179+
"outputs": [],
180+
"source": [
181+
"from tensorwaves.physics.amplitude import Intensity, SympyModel\n",
182+
"\n",
183+
"sympy_model = SympyModel(\n",
184+
" expression=model.expression.full_expression,\n",
185+
" parameters={k: v.value for k, v in model.parameters.items()},\n",
186+
" variables={},\n",
187+
")\n",
114188
"\n",
115-
"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:"
189+
"intensity = Intensity(sympy_model)"
116190
]
117191
},
118192
{
@@ -121,10 +195,10 @@
121195
"metadata": {},
122196
"outputs": [],
123197
"source": [
124-
"from tensorwaves.physics.helicity_formalism.amplitude import IntensityBuilder\n",
198+
"import pickle\n",
125199
"\n",
126-
"builder = IntensityBuilder(model.particles, kin)\n",
127-
"intensity = builder.create_intensity(model)"
200+
"with open(\"sympy_model.pickle\", \"wb\") as model_file:\n",
201+
" pickle.dump(sympy_model, model_file)"
128202
]
129203
},
130204
{
@@ -177,7 +251,7 @@
177251
"cell_type": "markdown",
178252
"metadata": {},
179253
"source": [
180-
"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):"
254+
"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`:"
181255
]
182256
},
183257
{
@@ -301,7 +375,7 @@
301375
"cell_type": "markdown",
302376
"metadata": {},
303377
"source": [
304-
"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."
378+
"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."
305379
]
306380
}
307381
],

0 commit comments

Comments
 (0)