Skip to content

Commit

Permalink
Create a hash uniquely identifying a RimeSpecification
Browse files Browse the repository at this point in the history
  • Loading branch information
sjperkins committed Jan 29, 2024
1 parent b6fd6f6 commit 4f36818
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions africanus/experimental/rime/fused/specification.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
from hashlib import shake_256
from importlib import import_module
import inspect
import multiprocessing
Expand All @@ -11,7 +12,7 @@
from africanus.experimental.rime.fused import terms as term_mod
from africanus.experimental.rime.fused.transformers.core import Transformer
from africanus.experimental.rime.fused import transformers as transformer_mod
from africanus.util.patterns import LazyProxy
from africanus.util.patterns import freeze, LazyProxy


TERM_STRING_REGEX = re.compile("([A-Z])(pq|p|q)")
Expand Down Expand Up @@ -335,6 +336,8 @@ def __init__(self, specification, terms=None, transformers=None):
"process_pool": pool
}

hash_elements = list(v for k, v in global_kw.items() if k != "process_pool")

for cls, cfg in zip(term_types, term_cfgs):
if cfg == "pq":
cfg = "middle"
Expand All @@ -350,15 +353,15 @@ def __init__(self, specification, terms=None, transformers=None):
cls_kw = {}

if "configuration" not in init_sig.parameters:
raise RimeSpecification(
raise RimeSpecificationError(
f"{cls}.__init__{init_sig} must take a "
f"'configuration' argument and call "
f"super().__init__(configuration)")

for a, p in list(init_sig.parameters.items())[1:]:
if p.kind not in {p.POSITIONAL_ONLY,
p.POSITIONAL_OR_KEYWORD}:
raise RimeSpecification(
raise RimeSpecificationError(
f"{cls}.__init__{init_sig} may not contain "
f"*args or **kwargs")

Expand All @@ -371,6 +374,8 @@ def __init__(self, specification, terms=None, transformers=None):
f"Available args: {available_kw}")

term = cls(**cls_kw)
hash_elements.append(".".join((cls.__module__, cls.__name__)))
hash_elements.append(cfg)
terms.append(term)

term_type_set = set(term_types)
Expand All @@ -385,7 +390,7 @@ def __init__(self, specification, terms=None, transformers=None):

transformers = []

for cls in transformer_types.values():
for _, cls in sorted(transformer_types.items()):
init_sig = inspect.signature(cls.__init__)
cls_kw = {}

Expand All @@ -405,10 +410,12 @@ def __init__(self, specification, terms=None, transformers=None):
f"Available args: {available_kw}")

transformer = cls(**cls_kw)
hash_elements.append(".".join((cls.__module__, cls.__name__)))
transformers.append(transformer)

self.terms = terms
self.transformers = transformers
self.spec_hash = shake_256(str((freeze(hash_elements))).encode("utf-8")).hexdigest(16)

@staticmethod
def _finalise_pool(pool):
Expand Down

0 comments on commit 4f36818

Please sign in to comment.