Belle II Software  release-05-02-19
test_variables_utils.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import unittest
5 from variables.utils import create_aliases_for_selected
6 from variables import variables as vm
7 
8 
9 class TestVariableUtilities(unittest.TestCase):
10  """Test case for the variables.utils functions"""
11 
12 
13  _list_of_variables = ['M', 'p']
14 
15  def _expand_expected(self, expected):
16  """Expand the list of expected aliases by appending _{name} for each variable in the _list_of_variables"""
17  result = []
18  for p in expected:
19  if p:
20  result += [f"{p}_{e}" for e in self._list_of_variables]
21  else:
22  result += self._list_of_variables
23  return result
24 
25  def assertAliases(self, decaystring, expected, **argk):
26  """Make sure the aliases created for given decaystring are as expected
27  Also, pass any additional keyword arguments to the create_aliases_for_selected function
28  """
29  expected = self._expand_expected(expected)
30  actual = create_aliases_for_selected(self._list_of_variables, decaystring, **argk)
31  self.assertEqual(expected, actual, f"decaystring: {decaystring}, arguments: {argk}")
32 
33 
36 
38  """Make sure we get an error if the decaystring is not valid"""
39  with self.assertRaises(ValueError):
40  create_aliases_for_selected(["p"], "eeh <- ok")
41 
43  """Make sure we get an error if o particle is selected"""
44  with self.assertRaises(ValueError):
45  create_aliases_for_selected(["p"], "B0 -> pi0")
46 
48  """Make sure we get an error if the number of supplied prefixes doesn't
49  match the number of selected particles"""
50  with self.assertRaises(ValueError):
51  create_aliases_for_selected(["p"], "^B0 -> ^pi0", prefix="one")
52  with self.assertRaises(ValueError):
53  create_aliases_for_selected(["p"], "B0 -> ^pi0", prefix=["one", "two"])
54 
56  """Make sure we got an error if the supplied provided prefixes are not unique"""
57  with self.assertRaises(ValueError):
58  create_aliases_for_selected(["p"], "^B0 -> ^pi0", prefix=["mine", "mine"])
59 
61  """Check daughter can be selected for an specific named alias"""
62  self.assertAliases('B0 -> [^D0 -> pi+ K-] pi0', ['dzero'], prefix='dzero')
63 
65  """Check mother and daughter can be selected for an specific named alias"""
66  self.assertAliases('^B0 -> [^D0 -> pi+ ^K-] pi0', ['MyB', 'MyD', 'MyK'], prefix=['MyB', 'MyD', 'MyK'])
67 
69  """Check daughter can be selected w/o an specific named alias"""
70  self.assertAliases('B0 -> [^D0 -> ^pi+ ^K-] pi0', ['d0', 'd0_d0', 'd0_d1'], use_names=False)
71 
73  """Check daughter can be selected w/o an specific named alias"""
74  self.assertAliases('^B0 -> pi0 ^pi0', ['', 'd1'], use_names=False)
75  # also make sure we ignore ``use_relative_indices``
76  self.assertAliases('^B0 -> pi0 ^pi0', ['', 'd1'], use_names=False, use_relative_indices=True)
77 
79  """Check granddaughter can be selected for an automatic name alias"""
80  self.assertAliases('B0 -> [D0 -> ^pi+ K-] pi0', ['D0_pi'])
81 
83  """Check multiple granddaughters can be selected for automatic name aliases"""
84  self.assertAliases(
85  'B0 -> [D0 -> ^pi+ ^pi- ^pi0] ^pi0',
86  [
87  'D0_pi_0',
88  'D0_pi_1',
89  'D0_pi0',
90  'pi0',
91  ])
92 
93  def test_autoindex(self):
94  """ check decay-string-of-doom with automatic names """
95  self.assertAliases(
96  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
97  [
98  "",
99  "D0_0_pi_0",
100  "D0_0_pi_1",
101  "D_pi",
102  "D0_2_pi_0",
103  "D0_2_pi_1",
104  "K_pi_0",
105  "K_pi_1",
106  "pi_4",
107  "pi_5",
108  ])
109 
111  """ check decay-string-of-doom with automatic names and relative indexing"""
112  self.assertAliases(
113  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
114  [
115  "",
116  "D0_0_pi_0",
117  "D0_0_pi_1",
118  "D_pi",
119  "D0_1_pi_0",
120  "D0_1_pi_1",
121  "K_pi_0",
122  "K_pi_1",
123  "pi_0",
124  "pi_1",
125  ], use_relative_indices=True)
126 
128  """ check decay-string-of-doom with automatic names and forced indices"""
129  self.assertAliases(
130  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
131  [
132  "",
133  "D0_0_pi_0",
134  "D0_0_pi_1",
135  "D_1_pi_0",
136  "D0_2_pi_0",
137  "D0_2_pi_1",
138  "K_3_pi_0",
139  "K_3_pi_1",
140  "pi_4",
141  "pi_5",
142  ], always_include_indices=True)
143 
145  """ check decay-string-of-doom with automatic names, relative indexing and forced indices"""
146  self.assertAliases(
147  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
148  [
149  "",
150  "D0_0_pi_0",
151  "D0_0_pi_1",
152  "D_0_pi_0",
153  "D0_1_pi_0",
154  "D0_1_pi_1",
155  "K_0_pi_0",
156  "K_0_pi_1",
157  "pi_0",
158  "pi_1",
159  ], use_relative_indices=True, always_include_indices=True)
160 
161  def test_indexed(self):
162  """ check decay-string-of-doom w/o automatic names """
163  self.assertAliases(
164  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
165  [
166  "",
167  "d0_d0",
168  "d0_d1",
169  "d1_d0",
170  "d2_d0",
171  "d2_d1",
172  "d3_d0",
173  "d3_d1",
174  "d4",
175  "d5",
176  ], use_names=False)
177 
179  """ check decay-string-of-doom w/o automatic names and make sure relative indexing is **not** honored"""
180  self.assertAliases(
181  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
182  [
183  "",
184  "d0_d0",
185  "d0_d1",
186  "d1_d0",
187  "d2_d0",
188  "d2_d1",
189  "d3_d0",
190  "d3_d1",
191  "d4",
192  "d5",
193  ], use_names=False, use_relative_indices=True)
194 
195  def test_threedkp(self):
196  """ check if the indexing works with more than two ... """
197  self.assertAliases(
198  "B0 -> [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]]",
199  [
200  "D_0_K_pi",
201  "D_1_K_pi",
202  "D_2_K_pi",
203  ])
204 
205  def test_fourdkp(self):
206  """ check if the indexing works with more than two ... """
207  self.assertAliases(
208  "B0 -> [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]]",
209  [
210  "D_0_K_pi",
211  "D_1_K_pi",
212  "D_2_K_pi",
213  "D_3_K_pi",
214  ])
215  self.assertAliases(
216  "B0 -> [D+ -> [K+ -> pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [^K+ -> pi+]] [D+ -> [K+ -> ^pi+]]",
217  [
218  "D_1_K_pi",
219  "D_2_K",
220  "D_3_K_pi",
221  ])
222  self.assertAliases(
223  "B0 -> [D+ -> [K+ -> pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [^K+ -> pi+]] [D+ -> [K+ -> ^pi+]]",
224  [
225  "D_0_K_pi",
226  "D_1_K",
227  "D_2_K_pi",
228  ], use_relative_indices=True)
229 
231  """Test many many children"""
232  self.assertAliases(
233  "B0 -> e+:0 ^e+:1 ^e+:2 e+:3 ^e+:4 e+:5 ^e+:6 mu+:7 ^mu+:8 mu+:9 ^mu+:10 ^mu+:11 mu+:12",
234  [
235  "e_1",
236  "e_2",
237  "e_4",
238  "e_6",
239  "mu_8",
240  "mu_10",
241  "mu_11",
242  ])
243 
245  """Test many many children with relative indices"""
246  self.assertAliases(
247  "B0 -> e+:0 ^e+:1 ^e+:2 e+:3 ^e+:4 e+:5 ^e+:6 mu+:7 ^mu+:8 mu+:9 ^mu+:10 ^mu+:11 mu+:12",
248  [
249  "e_0",
250  "e_1",
251  "e_2",
252  "e_3",
253  "mu_0",
254  "mu_1",
255  "mu_2",
256  ], use_relative_indices=True)
257 
259  """Test many many children without names"""
260  for use_relative_indices in True, False:
261  for always_include_indices in True, False:
262  self.assertAliases(
263  "B0 -> e+:0 ^e+:1 ^e+:2 e+:3 ^e+:4 e+:5 ^e+:6 mu+:7 ^mu+:8 mu+:9 ^mu+:10 ^mu+:11 mu+:12",
264  [
265  "d1",
266  "d2",
267  "d4",
268  "d6",
269  "d8",
270  "d10",
271  "d11",
272  ], use_names=False,
273  use_relative_indices=use_relative_indices,
274  always_include_indices=always_include_indices,
275  )
276 
277  def test_inclusive(self):
278  """Select a decay with the inclusive particle marker"""
279  self.assertAliases("B0 -> ^Xsd e+:loose e-:loose", ["Xsd"])
280 
281  def test_zfinal(self):
282  """Print all aliases as a final check"""
283  vm.printAliases()
284 
285 
286 if __name__ == '__main__':
287  unittest.main()
test_variables_utils.TestVariableUtilities.test_indexed_relativeignored
def test_indexed_relativeignored(self)
Definition: test_variables_utils.py:178
test_variables_utils.TestVariableUtilities.test_named_daughter
def test_named_daughter(self)
Definition: test_variables_utils.py:60
test_variables_utils.TestVariableUtilities.test_autonamed_granddaughter
def test_autonamed_granddaughter(self)
Definition: test_variables_utils.py:78
test_variables_utils.TestVariableUtilities.test_unnamed_daughter
def test_unnamed_daughter(self)
Definition: test_variables_utils.py:68
test_variables_utils.TestVariableUtilities.test_threedkp
def test_threedkp(self)
Definition: test_variables_utils.py:195
test_variables_utils.TestVariableUtilities.test_autoindex_allindexed_relative
def test_autoindex_allindexed_relative(self)
Definition: test_variables_utils.py:144
test_variables_utils.TestVariableUtilities.assertAliases
def assertAliases(self, decaystring, expected, **argk)
Definition: test_variables_utils.py:25
test_variables_utils.TestVariableUtilities.test_unnamed_mother
def test_unnamed_mother(self)
Definition: test_variables_utils.py:72
test_variables_utils.TestVariableUtilities.test_autoindex_relative
def test_autoindex_relative(self)
Definition: test_variables_utils.py:110
test_variables_utils.TestVariableUtilities.test_no_particle_selected
def test_no_particle_selected(self)
Definition: test_variables_utils.py:42
test_variables_utils.TestVariableUtilities.test_ohsomany_indexed
def test_ohsomany_indexed(self)
Definition: test_variables_utils.py:258
test_variables_utils.TestVariableUtilities.test_autoindex_allindexed
def test_autoindex_allindexed(self)
Definition: test_variables_utils.py:127
test_variables_utils.TestVariableUtilities
Definition: test_variables_utils.py:9
test_variables_utils.TestVariableUtilities.test_ohsomany_autonamed_relative
def test_ohsomany_autonamed_relative(self)
Definition: test_variables_utils.py:244
test_variables_utils.TestVariableUtilities._expand_expected
def _expand_expected(self, expected)
Definition: test_variables_utils.py:15
test_variables_utils.TestVariableUtilities.test_indexed
def test_indexed(self)
Definition: test_variables_utils.py:161
variables.utils
Definition: utils.py:1
test_variables_utils.TestVariableUtilities._list_of_variables
list _list_of_variables
list of variables to test
Definition: test_variables_utils.py:13
test_variables_utils.TestVariableUtilities.test_multiple_autoname_granddaughters
def test_multiple_autoname_granddaughters(self)
Definition: test_variables_utils.py:82
test_variables_utils.TestVariableUtilities.test_prefixes_repeated
def test_prefixes_repeated(self)
Definition: test_variables_utils.py:55
test_variables_utils.TestVariableUtilities.test_autoindex
def test_autoindex(self)
Definition: test_variables_utils.py:93
test_variables_utils.TestVariableUtilities.test_fourdkp
def test_fourdkp(self)
Definition: test_variables_utils.py:205
test_variables_utils.TestVariableUtilities.test_zfinal
def test_zfinal(self)
Definition: test_variables_utils.py:281
test_variables_utils.TestVariableUtilities.test_number_of_prefix_mismatch
def test_number_of_prefix_mismatch(self)
Definition: test_variables_utils.py:47
test_variables_utils.TestVariableUtilities.test_wrong_decaystring
def test_wrong_decaystring(self)
to add to the tests here, please add a test_something_something for you favourite compicated decay st...
Definition: test_variables_utils.py:37
test_variables_utils.TestVariableUtilities.test_ohsomany_autonamed
def test_ohsomany_autonamed(self)
Definition: test_variables_utils.py:230
test_variables_utils.TestVariableUtilities.test_named_mother_and_daughters
def test_named_mother_and_daughters(self)
Definition: test_variables_utils.py:64
test_variables_utils.TestVariableUtilities.test_inclusive
def test_inclusive(self)
Definition: test_variables_utils.py:277