From cd978734f0b8701779c86c41eb8735b58e22b520 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Wed, 16 Oct 2024 12:13:56 +0200 Subject: [PATCH 01/12] Node/EdgeQuantumberTypes for use as dict-keys --- src/qrules/quantum_numbers.py | 32 ++++++++++++++++++++++++++++++++ src/qrules/solving.py | 6 ++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/qrules/quantum_numbers.py b/src/qrules/quantum_numbers.py index ba0d6129..a64a65af 100644 --- a/src/qrules/quantum_numbers.py +++ b/src/qrules/quantum_numbers.py @@ -127,6 +127,29 @@ class EdgeQuantumNumbers: EdgeQuantumNumbers.g_parity, ] +# for accessing the keys of the dicts in EdgeSettings +EdgeQuantumNumberTypes = Union[ + type[EdgeQuantumNumbers.pid], + type[EdgeQuantumNumbers.mass], + type[EdgeQuantumNumbers.width], + type[EdgeQuantumNumbers.spin_magnitude], + type[EdgeQuantumNumbers.spin_projection], + type[EdgeQuantumNumbers.charge], + type[EdgeQuantumNumbers.isospin_magnitude], + type[EdgeQuantumNumbers.isospin_projection], + type[EdgeQuantumNumbers.strangeness], + type[EdgeQuantumNumbers.charmness], + type[EdgeQuantumNumbers.bottomness], + type[EdgeQuantumNumbers.topness], + type[EdgeQuantumNumbers.baryon_number], + type[EdgeQuantumNumbers.electron_lepton_number], + type[EdgeQuantumNumbers.muon_lepton_number], + type[EdgeQuantumNumbers.tau_lepton_number], + type[EdgeQuantumNumbers.parity], + type[EdgeQuantumNumbers.c_parity], + type[EdgeQuantumNumbers.g_parity], +] + @frozen(init=False) class NodeQuantumNumbers: @@ -155,6 +178,15 @@ class NodeQuantumNumbers: ] """Type hint for quantum numbers of interaction nodes.""" +# for accessing the keys of the dicts in NodeSettings +NodeQuantumNumberTypes = Union[ + type[NodeQuantumNumbers.l_magnitude], + type[NodeQuantumNumbers.l_projection], + type[NodeQuantumNumbers.s_magnitude], + type[NodeQuantumNumbers.s_projection], + type[NodeQuantumNumbers.parity_prefactor], +] + def _to_optional_float(optional_float: float | None) -> float | None: if optional_float is None: diff --git a/src/qrules/solving.py b/src/qrules/solving.py index 77bec661..de0e85f6 100644 --- a/src/qrules/solving.py +++ b/src/qrules/solving.py @@ -33,7 +33,9 @@ from qrules.quantum_numbers import ( EdgeQuantumNumber, EdgeQuantumNumbers, + EdgeQuantumNumberTypes, NodeQuantumNumber, + NodeQuantumNumberTypes, ) from qrules.topology import MutableTransition, Topology @@ -50,7 +52,7 @@ class EdgeSettings: conservation_rules: set[GraphElementRule] = field(factory=set) rule_priorities: dict[GraphElementRule, int] = field(factory=dict) - qn_domains: dict[Any, list] = field(factory=dict) + qn_domains: dict[EdgeQuantumNumberTypes, list] = field(factory=dict) @implement_pretty_repr @@ -70,7 +72,7 @@ class NodeSettings: conservation_rules: set[Rule] = field(factory=set) rule_priorities: dict[Rule, int] = field(factory=dict) - qn_domains: dict[Any, list] = field(factory=dict) + qn_domains: dict[NodeQuantumNumberTypes, list] = field(factory=dict) interaction_strength: float = 1.0 From dcddd08deca4471c747b02d06da9b892d1d53882 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 11:24:02 +0200 Subject: [PATCH 02/12] added 'bare' quantum-numbers to nitpick-ignore --- docs/conf.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index d395ae1e..887ef19c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -259,6 +259,36 @@ nb_execution_show_tb = True nb_execution_timeout = -1 nb_output_stderr = "remove" +nitpik_temp_names = [ + "pid", + "mass", + "width", + "spin_magnitude", + "spin_projection", + "charge", + "isospin_magnutude", + "isospin_projection", + "strangeness", + "charmness", + "bottomness", + "topness", + "baryon_number", + "electron_lepton_number", + "muon_lepton_number", + "tau_lepton_number", + "parity", + "c_parity", + "g_parity", + "l_magnitude", + "l_projection", + "s_magnitude", + "s_projection", + "parity_prefactor", +] +nitpick_temp_patterns = [ + (r"py:(class|obj)", r"qrules\.quantum_numbers\." + name) + for name in nitpik_temp_names +] nitpick_ignore_regex = [ (r"py:(class|obj)", "json.encoder.JSONEncoder"), (r"py:(class|obj)", r"qrules\.topology\.EdgeType"), @@ -267,6 +297,7 @@ (r"py:(class|obj)", r"qrules\.topology\.NewNodeType"), (r"py:(class|obj)", r"qrules\.topology\.NodeType"), (r"py:(class|obj)", r"qrules\.topology\.VT"), + *nitpick_temp_patterns, ] nitpicky = True primary_domain = "py" From a758e471ee206721d5f086e16057ddf179913cd0 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 11:27:04 +0200 Subject: [PATCH 03/12] fixed typo in 'isospin_magnitude' --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 887ef19c..25348ebc 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -266,7 +266,7 @@ "spin_magnitude", "spin_projection", "charge", - "isospin_magnutude", + "isospin_magnitude", "isospin_projection", "strangeness", "charmness", From 4db3ee5526bda589f1e96e1d8147f8265e43387e Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 12:31:56 +0200 Subject: [PATCH 04/12] quantum-number-names are now determined programatically --- docs/conf.py | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 25348ebc..72c2aa0f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -3,6 +3,7 @@ import os import sys +import attrs from sphinx_api_relink.helpers import ( get_branch_name, get_execution_mode, @@ -11,6 +12,8 @@ set_intersphinx_version_remapping, ) +from qrules.quantum_numbers import EdgeQuantumNumbers, NodeQuantumNumbers + sys.path.insert(0, os.path.abspath(".")) from _extend_docstrings import extend_docstrings # noqa: PLC2701 @@ -259,35 +262,12 @@ nb_execution_show_tb = True nb_execution_timeout = -1 nb_output_stderr = "remove" -nitpik_temp_names = [ - "pid", - "mass", - "width", - "spin_magnitude", - "spin_projection", - "charge", - "isospin_magnitude", - "isospin_projection", - "strangeness", - "charmness", - "bottomness", - "topness", - "baryon_number", - "electron_lepton_number", - "muon_lepton_number", - "tau_lepton_number", - "parity", - "c_parity", - "g_parity", - "l_magnitude", - "l_projection", - "s_magnitude", - "s_projection", - "parity_prefactor", -] +nitpick_temp_eqdge_names = list(attrs.fields_dict(EdgeQuantumNumbers)) +nitpick_temp_node_names = list(attrs.fields_dict(NodeQuantumNumbers)) +nitpick_temp_names = [*nitpick_temp_eqdge_names, *nitpick_temp_node_names] nitpick_temp_patterns = [ (r"py:(class|obj)", r"qrules\.quantum_numbers\." + name) - for name in nitpik_temp_names + for name in nitpick_temp_names ] nitpick_ignore_regex = [ (r"py:(class|obj)", "json.encoder.JSONEncoder"), From dd4725c01de7d30d39ecaf48db7274bf41dc3ad0 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 13:30:57 +0200 Subject: [PATCH 05/12] debug raise --- docs/conf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 72c2aa0f..a7ad39ca 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -265,6 +265,7 @@ nitpick_temp_eqdge_names = list(attrs.fields_dict(EdgeQuantumNumbers)) nitpick_temp_node_names = list(attrs.fields_dict(NodeQuantumNumbers)) nitpick_temp_names = [*nitpick_temp_eqdge_names, *nitpick_temp_node_names] +raise ValueError(nitpick_temp_names) nitpick_temp_patterns = [ (r"py:(class|obj)", r"qrules\.quantum_numbers\." + name) for name in nitpick_temp_names @@ -279,7 +280,7 @@ (r"py:(class|obj)", r"qrules\.topology\.VT"), *nitpick_temp_patterns, ] -nitpicky = True +nitpicky = True() primary_domain = "py" project = "QRules" pygments_style = "sphinx" From cd2cd6f9b1f1401da4bf633161dc23135d1f80a3 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 14:01:50 +0200 Subject: [PATCH 06/12] new function for fetching NewType-attributes --- docs/conf.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index a7ad39ca..70821738 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,8 +2,8 @@ import os import sys +import typing -import attrs from sphinx_api_relink.helpers import ( get_branch_name, get_execution_mode, @@ -262,9 +262,15 @@ nb_execution_show_tb = True nb_execution_timeout = -1 nb_output_stderr = "remove" -nitpick_temp_eqdge_names = list(attrs.fields_dict(EdgeQuantumNumbers)) -nitpick_temp_node_names = list(attrs.fields_dict(NodeQuantumNumbers)) -nitpick_temp_names = [*nitpick_temp_eqdge_names, *nitpick_temp_node_names] + + +def pick_newtype_attrs(some_type: type) -> list: + return [attr for attr in dir(some_type) if type(attr) is typing.NewType] + + +nitpick_temp_edge_names = pick_newtype_attrs(EdgeQuantumNumbers) +nitpick_temp_node_names = pick_newtype_attrs(NodeQuantumNumbers) +nitpick_temp_names = [*nitpick_temp_edge_names, *nitpick_temp_node_names] raise ValueError(nitpick_temp_names) nitpick_temp_patterns = [ (r"py:(class|obj)", r"qrules\.quantum_numbers\." + name) From 06f3a4126ba6ff9a2a849310b20ac7ec1bb41b52 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 14:14:20 +0200 Subject: [PATCH 07/12] fixed function to compare types of attributes not names --- docs/conf.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 70821738..578aeb5b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -265,7 +265,11 @@ def pick_newtype_attrs(some_type: type) -> list: - return [attr for attr in dir(some_type) if type(attr) is typing.NewType] + return [ + attr + for attr in dir(some_type) + if type(getattr(some_type, attr)) is typing.NewType + ] nitpick_temp_edge_names = pick_newtype_attrs(EdgeQuantumNumbers) From 4fdb854cbd8d57c9ba3746a491e67965373f25c1 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 14:17:36 +0200 Subject: [PATCH 08/12] removed debug-raise, removed 'bool-call' --- docs/conf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 578aeb5b..ca4ecfc7 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -275,7 +275,6 @@ def pick_newtype_attrs(some_type: type) -> list: nitpick_temp_edge_names = pick_newtype_attrs(EdgeQuantumNumbers) nitpick_temp_node_names = pick_newtype_attrs(NodeQuantumNumbers) nitpick_temp_names = [*nitpick_temp_edge_names, *nitpick_temp_node_names] -raise ValueError(nitpick_temp_names) nitpick_temp_patterns = [ (r"py:(class|obj)", r"qrules\.quantum_numbers\." + name) for name in nitpick_temp_names @@ -290,7 +289,7 @@ def pick_newtype_attrs(some_type: type) -> list: (r"py:(class|obj)", r"qrules\.topology\.VT"), *nitpick_temp_patterns, ] -nitpicky = True() +nitpicky = True primary_domain = "py" project = "QRules" pygments_style = "sphinx" From 41e210fd01f1efb7e9817230b61ed5eebc7c6a8d Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 14:45:43 +0200 Subject: [PATCH 09/12] replaced other occurences of 'Any' with qn-types --- src/qrules/conservation_rules.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/qrules/conservation_rules.py b/src/qrules/conservation_rules.py index 0ddc8017..9da17f4b 100644 --- a/src/qrules/conservation_rules.py +++ b/src/qrules/conservation_rules.py @@ -55,8 +55,12 @@ from attrs.converters import optional from qrules.quantum_numbers import EdgeQuantumNumbers as EdgeQN +from qrules.quantum_numbers import ( + EdgeQuantumNumberTypes, + NodeQuantumNumberTypes, + arange, +) from qrules.quantum_numbers import NodeQuantumNumbers as NodeQN -from qrules.quantum_numbers import arange def _is_boson(spin_magnitude: float) -> bool: @@ -75,15 +79,18 @@ def __call__(self, qns: Any, /) -> bool: ... class EdgeQNConservationRule(Protocol): def __call__( - self, ingoing_edge_qns: list[Any], outgoing_edge_qns: list[Any], / + self, + ingoing_edge_qns: list[EdgeQuantumNumberTypes], + outgoing_edge_qns: list[EdgeQuantumNumberTypes], + /, ) -> bool: ... class ConservationRule(Protocol): def __call__( self, - ingoing_edge_qns: list[Any], - outgoing_edge_qns: list[Any], + ingoing_edge_qns: list[EdgeQuantumNumberTypes], + outgoing_edge_qns: list[EdgeQuantumNumberTypes], node_qns: Any, /, ) -> bool: ... @@ -93,7 +100,7 @@ def __call__( # __call__ method in a concrete version of the generic are still containing the # TypeVar types. See https://github.com/python/typing/issues/762 def additive_quantum_number_rule( - quantum_number: type, + quantum_number: Union[EdgeQuantumNumberTypes, NodeQuantumNumberTypes], ) -> Callable[[Any], EdgeQNConservationRule]: r"""Class decorator for creating an additive conservation rule. From 9fd8404e0f66ea46c8aaa2093dea1718909d2af8 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 14:51:05 +0200 Subject: [PATCH 10/12] Revert "replaced other occurences of 'Any' with qn-types" This reverts commit 41e210fd01f1efb7e9817230b61ed5eebc7c6a8d. --- src/qrules/conservation_rules.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/qrules/conservation_rules.py b/src/qrules/conservation_rules.py index 9da17f4b..0ddc8017 100644 --- a/src/qrules/conservation_rules.py +++ b/src/qrules/conservation_rules.py @@ -55,12 +55,8 @@ from attrs.converters import optional from qrules.quantum_numbers import EdgeQuantumNumbers as EdgeQN -from qrules.quantum_numbers import ( - EdgeQuantumNumberTypes, - NodeQuantumNumberTypes, - arange, -) from qrules.quantum_numbers import NodeQuantumNumbers as NodeQN +from qrules.quantum_numbers import arange def _is_boson(spin_magnitude: float) -> bool: @@ -79,18 +75,15 @@ def __call__(self, qns: Any, /) -> bool: ... class EdgeQNConservationRule(Protocol): def __call__( - self, - ingoing_edge_qns: list[EdgeQuantumNumberTypes], - outgoing_edge_qns: list[EdgeQuantumNumberTypes], - /, + self, ingoing_edge_qns: list[Any], outgoing_edge_qns: list[Any], / ) -> bool: ... class ConservationRule(Protocol): def __call__( self, - ingoing_edge_qns: list[EdgeQuantumNumberTypes], - outgoing_edge_qns: list[EdgeQuantumNumberTypes], + ingoing_edge_qns: list[Any], + outgoing_edge_qns: list[Any], node_qns: Any, /, ) -> bool: ... @@ -100,7 +93,7 @@ def __call__( # __call__ method in a concrete version of the generic are still containing the # TypeVar types. See https://github.com/python/typing/issues/762 def additive_quantum_number_rule( - quantum_number: Union[EdgeQuantumNumberTypes, NodeQuantumNumberTypes], + quantum_number: type, ) -> Callable[[Any], EdgeQNConservationRule]: r"""Class decorator for creating an additive conservation rule. From 9c066f5e2566da31e92d2ba7085125523db12686 Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 15:06:30 +0200 Subject: [PATCH 11/12] inlined name-lists, moved function --- docs/conf.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index ca4ecfc7..eb389b3d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -17,6 +17,15 @@ sys.path.insert(0, os.path.abspath(".")) from _extend_docstrings import extend_docstrings # noqa: PLC2701 + +def pick_newtype_attrs(some_type: type) -> list: + return [ + attr + for attr in dir(some_type) + if type(getattr(some_type, attr)) is typing.NewType + ] + + extend_docstrings() set_intersphinx_version_remapping({ "ipython": { @@ -264,17 +273,12 @@ nb_output_stderr = "remove" -def pick_newtype_attrs(some_type: type) -> list: - return [ - attr - for attr in dir(some_type) - if type(getattr(some_type, attr)) is typing.NewType - ] - - nitpick_temp_edge_names = pick_newtype_attrs(EdgeQuantumNumbers) nitpick_temp_node_names = pick_newtype_attrs(NodeQuantumNumbers) -nitpick_temp_names = [*nitpick_temp_edge_names, *nitpick_temp_node_names] +nitpick_temp_names = [ + *pick_newtype_attrs(EdgeQuantumNumbers), + *pick_newtype_attrs(NodeQuantumNumbers), +] nitpick_temp_patterns = [ (r"py:(class|obj)", r"qrules\.quantum_numbers\." + name) for name in nitpick_temp_names From 7d70d98a5484f64a419e1e9c3e386ca96997590d Mon Sep 17 00:00:00 2001 From: grayson-helmholz Date: Fri, 18 Oct 2024 16:53:39 +0200 Subject: [PATCH 12/12] removed redundant lists --- docs/conf.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index eb389b3d..4b308b81 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -273,8 +273,6 @@ def pick_newtype_attrs(some_type: type) -> list: nb_output_stderr = "remove" -nitpick_temp_edge_names = pick_newtype_attrs(EdgeQuantumNumbers) -nitpick_temp_node_names = pick_newtype_attrs(NodeQuantumNumbers) nitpick_temp_names = [ *pick_newtype_attrs(EdgeQuantumNumbers), *pick_newtype_attrs(NodeQuantumNumbers),