-
Notifications
You must be signed in to change notification settings - Fork 25
Description
I've been thinking about the problem Andrey identified with the new TwoFlavor, ThreeFlavor and FourFalvor classes we want to introduce in version 2.0. The issues is with comparing `flavors' across the cases with different numbers i.e. how do you compare NU_E_BAR from TwoFlavor with NU_E_BAR from ThreeFlavor or compare NU_X from TwoFlavor with NU_MU from FourFlavor, and return True in both cases even though the numerical values of the enumerations can be different. The solution I came up with is to overload the comparison operators for the *Flavor classes. To reduce the size of the code I re-assigned the numerical values and added a couple of extra short methods. Overloading the comparison operators allows a user to compare flavors from different schemes but it doesn't solve the problem that the numerical values are different. What do people think? Is this is a problem we need to fix? Anyway, the solution is below. Now this code returns True for all cases
f1 = TwoFlavor.NU_X
f2 = ThreeFlavor.NU_TAU
print(f2==f1)
print(f1==f2)
f1 = FourFlavor.NU_TAU
f2 = TwoFlavor.NU_X
print(f2==f1)
print(f1==f2)Here's the relevant parts of neutrino.py
class TwoFlavor(IntEnum):
"""Enumeration of CCSN Neutrino flavors."""
NU_E = 0
NU_E_BAR = 1
NU_X = 2
NU_X_BAR = 3
def to_tex(self):
"""LaTeX-compatible string representations of flavor."""
if '_BAR' in self.name:
return r'$\overline{{\nu}}_{0}$'.format(self.name[3].lower())
return r'$\{0}$'.format(self.name.lower())
@property
def is_electron(self):
"""Return ``True`` for ``TwoFlavor.NU_E`` and ``TwoFlavor.NU_E_BAR``."""
return self.value in (TwoFlavor.NU_E.value, TwoFlavor.NU_E_BAR.value)
@property
def is_neutrino(self):
"""Return ``True`` for ``TwoFlavor.NU_E`` and ``TwoFlavor.NU_X``."""
return self.value in (TwoFlavor.NU_E.value, TwoFlavor.NU_X.value)
@property
def is_antineutrino(self):
return self.value in (TwoFlavor.NU_E_BAR.value, TwoFlavor.NU_X_BAR.value)
def __eq__(self, other):
if isinstance(other,ThreeFlavor) == True or isinstance(other,FourFlavor) == True:
return other == self
class ThreeFlavor(IntEnum):
"""Enumeration of CCSN Neutrino flavors."""
NU_E = 0
NU_E_BAR = 1
NU_MU = 2
NU_MU_BAR = 3
NU_TAU = 4
NU_TAU_BAR = 5
def to_tex(self):
"""LaTeX-compatible string representations of flavor."""
if '_BAR' in self.name:
return r'$\overline{{\nu}}_{0}$'.format(self.name[3].lower())
return r'$\{0}$'.format(self.name.lower())
@property
def is_electron(self):
"""Return ``True`` for ``ThreeFlavor.NU_E`` and ``ThreeFlavor.NU_E_BAR``."""
return self.value in (ThreeFlavor.NU_E.value, ThreeFlavor.NU_E_BAR.value)
@property
def is_muon(self):
"""Return ``True`` for ``ThreeFlavor.NU_MU``, ``ThreeFlavor.NU_MU_BAR``."""
return self.value in (ThreeFlavor.NU_MU.value, ThreeFlavor.NU_MU_BAR.value)
@property
def is_tauon(self):
"""Return ``True`` for ``ThreeFlavor.NU_TAU``, ``ThreeFlavor.NU_TAU_BAR``."""
return self.value in (ThreeFlavor.NU_TAU.value, ThreeFlavor.NU_TAU_BAR.value)
@property
def is_neutrino(self):
"""Return ``True`` for ``ThreeFlavor.NU_E`` and ``ThreeFlavor.NU_MU`` and ``ThreeFlavor.NU_TAU``."""
return self.value in (ThreeFlavor.NU_E.value, ThreeFlavor.NU_MU.value, ThreeFlavor.NU_TAU.value)
@property
def is_antineutrino(self):
return self.value in (ThreeFlavor.NU_E_BAR.value, ThreeFlavor.NU_MU_BAR.value, ThreeFlavor.NU_TAU_BAR.value)
def __eq__(self, other):
if isinstance(other,FourFlavor) == True:
return other == self
f = self.value
if isinstance(other,TwoFlavor) == True and self.is_tauon == True:
f -= 2
if f == other.value:
return True
else:
return False
class FourFlavor(IntEnum):
"""Enumeration of CCSN Neutrino flavors."""
NU_E = 0
NU_E_BAR = 1
NU_MU = 2
NU_MU_BAR = 3
NU_TAU = 4
NU_TAU_BAR = 5
NU_S = 6
NU_S_BAR = 7
def to_tex(self):
"""LaTeX-compatible string representations of flavor."""
if '_BAR' in self.name:
return r'$\overline{{\nu}}_{0}$'.format(self.name[3].lower())
return r'$\{0}$'.format(self.name.lower())
@property
def is_electron(self):
"""Return ``True`` for ``FourFlavor.NU_E`` and ``FourFlavor.NU_E_BAR``."""
return self.value in (FourFlavor.NU_E.value, FourFlavor.NU_E_BAR.value)
@property
def is_muon(self):
"""Return ``True`` for ``FourFlavor.NU_MU``, ``FourFlavor.NU_MU_BAR``."""
return self.value in (FourFlavor.NU_MU.value, FourFlavor.NU_MU_BAR.value)
@property
def is_tauon(self):
"""Return ``True`` for ``FourFlavor.NU_TAU``, ``FourFlavor.NU_TAU_BAR``."""
return self.value in (FourFlavor.NU_TAU.value, FourFlavor.NU_TAU_BAR.value)
@property
def is_sterile(self):
"""Return ``True`` for ``FourFlavor.NU_S``, ``FourFlavor.NU_S_BAR``."""
return self.value in (FourFlavor.NU_S.value, FourFlavor.NU_S_BAR.value)
@property
def is_neutrino(self):
"""Return ``True`` for ``FourFlavor.NU_E`` and ``FourFlavor.NU_MU`` and ``FourFlavor.NU_TAU`` and ``FourFlavor.NU_S``."""
return self.value in (FourFlavor.NU_E.value, FourFlavor.NU_MU.value, FourFlavor.NU_TAU.value, FourFlavor.NU_S.value)
@property
def is_antineutrino(self):
return self.value in (FourFlavor.NU_E_BAR.value, FourFlavor.NU_MU_BAR.value, FourFlavor.NU_TAU_BAR.value, FourFlavor.NU_S_BAR.value)
def __eq__(self, other):
f = self.value
if isinstance(other,TwoFlavor) == True and self.is_tauon == True:
f -= 2
if f == other.value:
return True
else:
return False