Skip to content

Commit

Permalink
Merge pull request #128 from jacanchaplais/feature/pdg-from-name-127
Browse files Browse the repository at this point in the history
added from_name classmethod to PdgArray #127
  • Loading branch information
jacanchaplais authored May 12, 2023
2 parents 720f378 + 8fa94a4 commit 89e2584
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion graphicle/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,52 @@ class PdgArray(base.ArrayBase):
__mega_to_giga: float = field(init=False, repr=False)
_HANDLED_TYPES = field(init=False, repr=False)

@classmethod
def from_name(cls, names: ty.Union[str, ty.Iterable[str]]) -> "PdgArray":
"""Instantiates ``PdgArray`` using string representations of
particle names, instead of integer valued PDG codes.
.. versionadded:: 0.2.12
Parameters
----------
names: str or iterable[str]
String representation of particle names.
Returns
-------
PdgArray
Instance constructed from names.
Raises
------
ValueError
If ``names`` are not the appropriate type, or are not
recognised.
"""
if not isinstance(names, str) and isinstance(names, cla.Iterable):
names = list(names)
if not all(map(op.is_, map(type, names), it.repeat(str))):
raise ValueError(
"When passing an iterable as names, all elements must be "
"of type str."
)
elif not isinstance(names, cla.Iterable):
raise ValueError(
"names must be either a string, or iterable of strings."
)
try:
pdgs = (
_LOOKUP_TABLE.table["name"]
.reset_index()
.set_index("name")
.loc[names]
.values.reshape(-1)
)
except KeyError as e:
raise ValueError("Some names were not recognised.") from e
return cls(pdgs)

def __attrs_post_init__(self) -> None:
self.__mega_to_giga: float = 1.0e-3
self.dtype = self._data.dtype
Expand Down Expand Up @@ -1054,7 +1100,7 @@ def mask(
np.isin(data, target, assume_unique=False, invert=blacklist)
)

def __get_prop(self, field: str) -> base.VoidVector:
def __get_prop(self, field: str) -> base.AnyVector:
props = _LOOKUP_TABLE.properties(self.data, [field])[field]
return props # type: ignore

Expand Down

0 comments on commit 89e2584

Please sign in to comment.