Skip to content

Commit

Permalink
Adding some tests, breaking out ampgen style and dec style particles.
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 25, 2018
1 parent f85264f commit 0c4258b
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 29 deletions.
49 changes: 25 additions & 24 deletions decaylanguage/particle/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from .. import data
from ..data import open_text
from .regex import getname, getdec

def programmatic_name(name):
'Return a name safe to use as a variable name'
Expand All @@ -32,20 +33,6 @@ def programmatic_name(name):
.replace('~', 'bar'))


getname = re.compile(r'''
^ # Beginning of string
(?P<name> \w+? ) # One or more characters, non-greedy
(?:\( (?P<family> [udsctb][\w]*) \) )? # Optional family like (s)
(?:\( (?P<state> \d+ ) \) # Optional state in ()
(?= \*? \( ) )? # - lookahead for mass
(?P<star> \* )? # Optional star
(?:\( (?P<mass> \d+ ) \) )? # Optional mass in ()
(?P<bar> (bar|~) )? # Optional bar
(?P<charge> [0\+\-][+-]?) # Required 0, -, --, or +, ++
$ # End of string
''', re.VERBOSE)


class SpinType(IntEnum):
'The spin type of a particle'
Scalar = 1 # (0, 1)
Expand Down Expand Up @@ -240,7 +227,10 @@ def __le__(self, other):
return abs(self.val - .25) < abs(other.val - .25)

def __eq__(self, other):
return self.val == other.val
try:
return self.val == other.val
except AttributeError:
return self.val == other

def __hash__(self):
return hash(self.val)
Expand Down Expand Up @@ -438,9 +428,21 @@ def from_search(cls, **search_terms):
else:
raise RuntimeError("Found too many particles")

@classmethod
def from_dec(cls, name):
'Get a particle from a DecFile style name'

mat = getdec.match(name)
if mat is None:
return cls.from_search(Name=name)
mat = mat.groupdict()

return cls._from_group_dict(mat)


@classmethod
def from_string(cls, name):
'Get a particle from an AmpGen style name'
'Get a particle from an AmpGen (PDG) style name'

# Patch in common names
if name == 'Upsilon':
Expand All @@ -449,16 +451,10 @@ def from_string(cls, name):
# Forcable override
bar = False

if name.startswith('anti-'):
name = name[5:]
bar = True

if '~' in name:
name = name.replace('~','')
bar = True



mat = getname.match(name)
if mat is None:
return cls.from_search(Name=name, particle=False if bar else None)
Expand All @@ -467,8 +463,13 @@ def from_string(cls, name):
if bar:
mat['bar'] = 'bar'

if '_' in mat['name']:
mat['name'], mat['family'] = mat['name'].split('_')
return cls._from_group_dict(mat)

@classmethod
def _from_group_dict(cls, mat):

#if '_' in mat['name']:
# mat['name'], mat['family'] = mat['name'].split('_')

Par_mapping = {'++': 2, '+': 1, '0': 0, '-': -1, '--': 2}
particle = False if mat['bar'] is not None else (True if mat['charge'] == '0' else None)
Expand Down
27 changes: 27 additions & 0 deletions decaylanguage/particle/regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re

getname = re.compile(r'''
^ # Beginning of string
(?P<name> \w+? ) # One or more characters, non-greedy
(?:\( (?P<family> [udsctb][\w]*) \) )? # Optional family like (s)
(?:\( (?P<state> \d+ ) \) # Optional state in ()
(?= \*? \( ) )? # - lookahead for mass
(?P<star> \* )? # Optional star
(?:\( (?P<mass> \d+ ) \) )? # Optional mass in ()
(?P<bar> (bar|~) )? # Optional bar
(?P<charge> [0\+\-][+-]?) # Required 0, -, --, or +, ++
$ # End of string
''', re.VERBOSE)

getdec = re.compile(r'''
^ # Beginning of string
(?P<bar> (anti-) )? # Optional anti-
(?P<name> [a-zA-Z]+? ) # One or more characters, non-greedy
(?P<prime> '* ) # Optional prime(s)
(?: _ (?P<state> \d+ ) )? # Optional state in ()
(?: (?P<family> _[udsctb][\w]*) )? # Optional family like (s)
(?:\( (?P<mass> \d+ ) \) )? # Optional mass in ()
(?P<star> \*? ) # Optional star
(?P<charge> [0\+\-][+-]?) # Required 0, -, --, or +, ++
$ # End of string
''', re.VERBOSE)
24 changes: 23 additions & 1 deletion notebooks/DecReader.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,29 @@
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/henryiii/git/software/decaylanguage/decaylanguage/decay/decay.py:22: UserWarning: Graphvis not installed. Line display not available.\n warnings.warn(\"Graphvis not installed. Line display not available.\")\n"
]
},
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'lark'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-d7fb869b3c71>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mdecaylanguage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdec\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdec\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mlark\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mLark\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTransformer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTree\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/git/software/decaylanguage/decaylanguage/dec/dec.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[0;34m.\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mopen_text\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecay\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecay\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mDecay\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0menum\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mEnum\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/git/software/decaylanguage/decaylanguage/decay/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mamplitudechain\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mLS\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mamplitudechain\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mAmplitudeChain\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0m__all__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mLS\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mAmplitudeChain\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/git/software/decaylanguage/decaylanguage/decay/amplitudechain.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 29\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mlark\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mLark\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 30\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mampgentransform\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mAmpGenTransformer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mget_from_parser\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'lark'"
],
"output_type": "error"
}
],
"source": [
"from decaylanguage.dec import dec\n",
"from lark import Lark, Transformer, Tree"
Expand Down
61 changes: 61 additions & 0 deletions tests/test_dec_full.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from decaylanguage import data
from decaylanguage.particle import Particle, ParticleNotFound
from decaylanguage.dec import dec
from lark import Lark, Transformer, Tree

import pytest

class TreeToDec2(Transformer):
missing = set()
keyerrs = set()

def __init__(self, alias_dict):
self.alias_dict = alias_dict

def particle(self, items):
label, = items
if label in self.alias_dict:
label = self.alias_dict[label]
try:
return Particle.from_string(str(label))
except ParticleNotFound:
self.missing.add(str(label))
return str(label)
except KeyError:
self.keyerrs.add(str(label))
return str(label)

@pytest.mark.skip
def test_dec_full():
with data.open_text(data, 'DECAY_LHCB.DEC') as f:
txt = f.read()

with data.open_text(data, 'decfile.lark') as f:
grammar = f.read()

l = Lark(grammar, parser='lalr', lexer='standard') # , transformer = TreeToDec())

parsed = l.parse(txt)
assert bool(parsed)

transformed = dec.TreeToDec().transform(parsed)

define = dec.define(transformed)
pythia_def = dec.pythia_def(transformed)
alias = dec.alias(transformed)
chargeconj = dec.chargeconj(transformed)
global_photos = dec.global_photos(transformed)
decay = dec.decay(transformed)
cdecay = dec.cdecay(transformed)
setlspw = dec.setlspw(transformed)

for item in pythia_def:
print(item[0], ":", item[1], "=", item[2])


labelled = TreeToDec2(alias).transform(decay)

print(TreeToDec2.missing)
print(TreeToDec2.keyerrs)

assert len(TreeToDec2.missing) == 0
18 changes: 14 additions & 4 deletions tests/test_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,22 @@ def test_prop():

def test_ampgen_style_names():
assert Particle.from_string('pi+').val == 211
assert Particle.from_string('pi-').val == -211
assert Particle.from_string('K~*0').val == -313
assert Particle.from_string('K*(892)bar0').val == -313
assert Particle.from_string('a(1)(1260)+').val == 20213

# Direct comparison to integer works too
assert Particle.from_string('rho(1450)0') == 100113
assert Particle.from_string('rho(770)0') == 113

assert Particle.from_string('K(1)(1270)bar-') == -10323
assert Particle.from_string('K(1460)bar-') == -100321



def test_decfile_style_names():
assert Particle.from_string('anti-K*0').val == -313
assert Particle.from_string('a_1(1260)+').val == 20213
# "D'_1+"
# "D_2*+"
assert Particle.from_dec('anti-K*0').val == -313
assert Particle.from_dec('a_1(1260)+').val == 20213
#assert Particle.from_dec("D'_1+").val == 7
#assert Particle.from_dec("D_2*+").val == 8

0 comments on commit 0c4258b

Please sign in to comment.