Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app/services/experiment.py
CHANGED
|
@@ -120,9 +120,13 @@ def _input_sha256(sequence: str) -> str:
|
|
| 120 |
|
| 121 |
def deterministic_seed(sequence: str, parameters: dict | None = None) -> int:
|
| 122 |
"""Deterministic random seed derived from input + params so a rerun with
|
| 123 |
-
identical inputs reproduces identical stochastic behavior.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
payload = sequence.strip().upper() + repr(sorted((parameters or {}).items()))
|
| 125 |
-
return int.from_bytes(hashlib.sha256(payload.encode()).digest()[:8], "big")
|
| 126 |
|
| 127 |
|
| 128 |
def make_experiment_id() -> str:
|
|
|
|
| 120 |
|
| 121 |
def deterministic_seed(sequence: str, parameters: dict | None = None) -> int:
|
| 122 |
"""Deterministic random seed derived from input + params so a rerun with
|
| 123 |
+
identical inputs reproduces identical stochastic behavior.
|
| 124 |
+
|
| 125 |
+
Masked to the signed 64-bit range so the value always fits a Postgres
|
| 126 |
+
bigint column (2^63-1), regardless of the input hash.
|
| 127 |
+
"""
|
| 128 |
payload = sequence.strip().upper() + repr(sorted((parameters or {}).items()))
|
| 129 |
+
return int.from_bytes(hashlib.sha256(payload.encode()).digest()[:8], "big") & 0x7FFFFFFFFFFFFFFF
|
| 130 |
|
| 131 |
|
| 132 |
def make_experiment_id() -> str:
|
tests/test_experiment_provenance.py
CHANGED
|
@@ -154,6 +154,15 @@ class TestExperimentManager:
|
|
| 154 |
assert a == b
|
| 155 |
assert a != c
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
def test_fingerprint_fields(self, fake_sb):
|
| 158 |
fp = exp_svc.build_fingerprint("MGHHHH", {"fast_mode": True})
|
| 159 |
assert "git_commit" in fp
|
|
|
|
| 154 |
assert a == b
|
| 155 |
assert a != c
|
| 156 |
|
| 157 |
+
def test_deterministic_seed_fits_postgres_bigint(self, fake_sb):
|
| 158 |
+
# Regression: seed must never overflow a signed 64-bit bigint column
|
| 159 |
+
# (Postgres max is 2^63-1). The masking above guarantees it.
|
| 160 |
+
bigint_max = (1 << 63) - 1
|
| 161 |
+
for seq in ["MGHHHH", "FVNQHLCGSHLVEALYLVCGERGFFYTPKT", "MKWVTFISLL" * 5, "A" * 2000]:
|
| 162 |
+
for params in (None, {"fast_mode": True}, {"blast_params": {"database": "nr"}}):
|
| 163 |
+
seed = exp_svc.deterministic_seed(seq, params)
|
| 164 |
+
assert 0 <= seed <= bigint_max
|
| 165 |
+
|
| 166 |
def test_fingerprint_fields(self, fake_sb):
|
| 167 |
fp = exp_svc.build_fingerprint("MGHHHH", {"fast_mode": True})
|
| 168 |
assert "git_commit" in fp
|