Skip to content

Commit

Permalink
Please pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardo-rodrigues committed Nov 26, 2024
1 parent 1147559 commit b6b6a37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 217 deletions.
238 changes: 23 additions & 215 deletions notebooks/ExampleDecFileParsingWithLark.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -58,105 +58,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"// Copyright (c) 2018-2024, Eduardo Rodrigues and Henry Schreiner.\n",
"//\n",
"// Distributed under the 3-clause BSD license, see accompanying file LICENSE\n",
"// or https://github.com/scikit-hep/decaylanguage for details.\n",
"\n",
"start : _NEWLINE* (line _NEWLINE+)* (\"End\" _NEWLINE+)?\n",
"?line : define | particle_def | pythia_def | jetset_def | ls_def | model_alias | alias | chargeconj | commands | decay | cdecay | copydecay | setlspw | setlsbw | changemasslimit | inc_factor\n",
"\n",
"pythia_def : LABEL_PYTHIA8_COMMANDS LABEL \":\" LABEL \"=\" (LABEL | SIGNED_NUMBER) // Pythia 8 commands\n",
"LABEL_PYTHIA8_COMMANDS : \"PythiaAliasParam\" | \"PythiaBothParam\" | \"PythiaGenericParam\"\n",
"\n",
"jetset_def : \"JetSetPar\" LABEL \"=\" SIGNED_NUMBER // Old Pythia 6 commands\n",
"\n",
"ls_def : LABEL_LINESHAPE LABEL // Choose a lineshape for a particle\n",
"LABEL_LINESHAPE : \"LSFLAT\" | \"LSNONRELBW\" | \"LSMANYDELTAFUNC\" // Lineshape flat | non-relativistic BW, spikes\n",
"\n",
"inc_factor: LABEL_INCLUDE_FACTOR LABEL BOOLEAN_INCLUDE_FACTOR // Presence of the birth/decay momentum factor and form-factor\n",
"LABEL_INCLUDE_FACTOR : \"IncludeBirthFactor\" | \"IncludeDecayFactor\"\n",
"BOOLEAN_INCLUDE_FACTOR : \"yes\" | \"no\"\n",
"\n",
"setlsbw : \"BlattWeisskopf\" LABEL SIGNED_NUMBER // Set Blatt-Weisskopf barrier factor for a lineshape\n",
"\n",
"setlspw : \"SetLineshapePW\" LABEL LABEL LABEL INT // Redefine Partial Wave for label -> label label\n",
"\n",
"cdecay : \"CDecay\" LABEL\n",
"\n",
"define : \"Define\" LABEL SIGNED_NUMBER\n",
"\n",
"particle_def: \"Particle\" LABEL SIGNED_NUMBER SIGNED_NUMBER // Set the mass and width of a particle (in GeV)\n",
"\n",
"alias : \"Alias\" LABEL LABEL\n",
"\n",
"chargeconj : \"ChargeConj\" LABEL LABEL\n",
"\n",
"changemasslimit : LABEL_CHANGE_MASS LABEL SIGNED_NUMBER // Set upper/lower mass cuts on a lineshape\n",
"LABEL_CHANGE_MASS : \"ChangeMassMin\" | \"ChangeMassMax\"\n",
"\n",
"?commands : global_photos\n",
"\n",
"global_photos : boolean_photos\n",
"\n",
"boolean_photos : \"yesPhotos\" -> yes\n",
" | \"noPhotos\" -> no\n",
"\n",
"decay : \"Decay\" particle _NEWLINE+ decayline* \"Enddecay\"\n",
"decayline : value particle* photos? model _NEWLINE+\n",
"value : SIGNED_NUMBER\n",
"photos : \"PHOTOS\"\n",
"\n",
"copydecay : \"CopyDecay\" label label\n",
"\n",
"label : LABEL\n",
"particle : LABEL // Add full particle parsing here\n",
"model_label : LABEL\n",
"\n",
"model_alias : \"ModelAlias\" model_label model\n",
"\n",
"model : (model_label | MODEL_NAME model_options?) _SEMICOLON+\n",
"model_options : (value | LABEL | _NEWLINE | _COMMA)+\n",
"\n",
"// We must set priorities here to use lalr - match model name above label, and label above something else\n",
"// This is supposed to be empty and will be filled via the `edit_terminals` functionality with a list of models\n",
"MODEL_NAME.2 : \"MODEL_NAME_PLACEHOLDER\"/\\b/\n",
"\n",
"// model : model_generic\n",
"// model_helamp : \"HELAMP\" (SIGNED_NUMBER SIGNED_NUMBER)+\n",
"\n",
"// Terminal definitions\n",
"// To use a fast parser, we need to avoid conflicts\n",
"\n",
"%import common.WS_INLINE\n",
"%import common.INT\n",
"%import common.SIGNED_NUMBER\n",
"\n",
"// Disregard comments, (multiple) newlines and whitespace in parser tree\n",
"_NEWLINE: ( /\\r?\\n[\\t ]*/ | COMMENT )\n",
"_SEMICOLON: /;/\n",
"_COMMA: /,/\n",
"_WS: WS_INLINE\n",
"\n",
"LABEL : /[a-zA-Z0-9\\/\\-+*_().'~]+/\n",
"COMMENT : /[#][^\\n]*/\n",
"\n",
"// We should ignore comments\n",
"%ignore COMMENT\n",
"\n",
"// Disregard spaces in text\n",
"%ignore WS_INLINE\n",
"\n"
]
}
],
"outputs": [],
"source": [
"print(grammar)"
]
Expand All @@ -170,47 +74,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# Example decay chain for testing purposes\n",
"# Considered by itself, this file in in fact incomplete,\n",
"# as there are no instructions on how to decay the anti-D0 and the D-!\n",
"\n",
"Decay D*+\n",
" 0.677 D0 pi+ VSS;\n",
" 0.307 D+ pi0 VSS;\n",
" 0.016 D+ gamma VSP_PWAVE;\n",
"Enddecay\n",
"\n",
"Decay D*-\n",
"0.6770 anti-D0 pi- VSS;\n",
"0.3070 D- pi0 VSS;\n",
"0.0160 D- gamma VSP_PWAVE;\n",
"Enddecay\n",
"\n",
"Decay D0\n",
"1.0 K- pi+ PHSP;\n",
"Enddecay\n",
"\n",
"Decay D+\n",
"1.0 K- pi+ pi+ pi0 PHSP;\n",
"Enddecay\n",
"\n",
"Decay pi0\n",
"0.988228297 gamma gamma PHSP;\n",
"0.011738247 e+ e- gamma PI0_DALITZ;\n",
"0.000033392 e+ e+ e- e- PHSP;\n",
"0.000000065 e+ e- PHSP;\n",
"Enddecay\n",
"\n"
]
}
],
"outputs": [],
"source": [
"print(dec_file)"
]
Expand All @@ -224,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"tags": []
},
Expand All @@ -249,7 +115,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -259,17 +125,9 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# of decays in file = 5\n"
]
}
],
"outputs": [],
"source": [
"def number_of_decays(parsed_file):\n",
" \"\"\"Returns the number of particle decays defined in the parsed .dec file.\"\"\"\n",
Expand All @@ -281,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -292,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -328,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -356,69 +214,33 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_decay(dec, final_state):\n",
" \"\"\"Pretty print of the decay modes of a given particle.\"\"\"\n",
" print(dec)\n",
" for fs in final_state:\n",
" print(\"%12g : %50s %15s\" % (fs[0], \" \".join(p for p in fs[1]), fs[2]))"
" print(f\"{fs[0]:12g} : {' '.join(p for p in fs[1]):50s} {fs[2]:15s}\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pi0\n",
" 0.988228 : gamma gamma PHSP\n",
" 0.0117382 : e+ e- gamma PI0_DALITZ\n",
" 3.3392e-05 : e+ e+ e- e- PHSP\n",
" 6.5e-08 : e+ e- PHSP\n"
]
}
],
"outputs": [],
"source": [
"print_decay(\"pi0\", decays[\"pi0\"])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"D*+\n",
" 0.677 : D0 pi+ VSS\n",
" 0.307 : D+ pi0 VSS\n",
" 0.016 : D+ gamma VSP_PWAVE\n",
"D*-\n",
" 0.677 : anti-D0 pi- VSS\n",
" 0.307 : D- pi0 VSS\n",
" 0.016 : D- gamma VSP_PWAVE\n",
"D0\n",
" 1 : K- pi+ PHSP\n",
"D+\n",
" 1 : K- pi+ pi+ pi0 PHSP\n",
"pi0\n",
" 0.988228 : gamma gamma PHSP\n",
" 0.0117382 : e+ e- gamma PI0_DALITZ\n",
" 3.3392e-05 : e+ e+ e- e- PHSP\n",
" 6.5e-08 : e+ e- PHSP\n"
]
}
],
"outputs": [],
"source": [
"for particle, decay_info in decays.items():\n",
" print_decay(particle, decay_info)"
Expand All @@ -433,25 +255,11 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'pydot'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_3649711/3333668827.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mlark\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtree\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpydot__tree_to_png\u001b[0m \u001b[0;31m# requires pydot\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m pydot__tree_to_png(\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mlist_of_decay_trees\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparsed_dec_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"decay.png\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrankdir\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"LR\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m )\n",
"\u001b[0;32m~/.local/lib/python3.8/site-packages/lark/tree.py\u001b[0m in \u001b[0;36mpydot__tree_to_png\u001b[0;34m(tree, filename, rankdir, **kwargs)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 183\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpydot__tree_to_png\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtree\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrankdir\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"LR\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 184\u001b[0;31m \u001b[0mgraph\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpydot__tree_to_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtree\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrankdir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 185\u001b[0m \u001b[0mgraph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite_png\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.8/site-packages/lark/tree.py\u001b[0m in \u001b[0;36mpydot__tree_to_graph\u001b[0;34m(tree, rankdir, **kwargs)\u001b[0m\n\u001b[1;32m 202\u001b[0m \"\"\"\n\u001b[1;32m 203\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 204\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpydot\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 205\u001b[0m \u001b[0mgraph\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpydot\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgraph_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'digraph'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrankdir\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrankdir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 206\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'pydot'"
]
}
],
"outputs": [],
"source": [
"from IPython.display import Image\n",
"from lark.tree import pydot__tree_to_png # requires pydot\n",
Expand All @@ -466,7 +274,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -480,7 +288,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions tests/dec/test_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,9 @@ def test_expand_decay_chains():
"D*- -> D- gamma",
],
}
for particle in output:
for particle, descriptors in output.items():
all_decays = p.expand_decay_modes(particle)
assert set(all_decays) == set(output[particle])
assert set(all_decays) == set(descriptors)


def test_Lark_DecayModelAliasReplacement_Transformer():
Expand Down

0 comments on commit b6b6a37

Please sign in to comment.