From 0a8895265828a13a31cb5f18cc991015d8bdd03e Mon Sep 17 00:00:00 2001 From: richard gowers Date: Wed, 15 Nov 2023 17:24:49 +0000 Subject: [PATCH] make Measurement model frozen allows hashing, for usage in sets/dicts etc --- cinnabar/measurements.py | 3 +++ cinnabar/tests/test_measurements.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cinnabar/measurements.py b/cinnabar/measurements.py index 12afda4..1800342 100644 --- a/cinnabar/measurements.py +++ b/cinnabar/measurements.py @@ -50,6 +50,9 @@ def __hash__(self): class Measurement(DefaultModel): """The free energy difference of moving from A to B""" + class Config: + frozen = True + labelA: Hashable labelB: Hashable DG: FloatQuantity['kilocalorie_per_mole'] diff --git a/cinnabar/tests/test_measurements.py b/cinnabar/tests/test_measurements.py index 4b754c9..acf6744 100644 --- a/cinnabar/tests/test_measurements.py +++ b/cinnabar/tests/test_measurements.py @@ -14,6 +14,33 @@ def test_ground(): assert g3 == g4 +def test_measurement_hash(): + m1 = cinnabar.Measurement( + labelA='foo', labelB='bar', + DG=0.1 * unit.kilocalorie_per_mole, + uncertainty=0.01 * unit.kilocalorie_per_mole, + computational=True, + ) + m1a = cinnabar.Measurement( + labelA='foo', labelB='bar', + DG=0.1 * unit.kilocalorie_per_mole, + uncertainty=0.01 * unit.kilocalorie_per_mole, + computational=True, + ) + m2 = cinnabar.Measurement( + labelA='foo', labelB='bar', + DG=0.11 * unit.kilocalorie_per_mole, + uncertainty=0.01 * unit.kilocalorie_per_mole, + computational=False, + ) + + thing = set([m1, m1a, m2]) + + assert len(thing) == 2 + assert m1 in thing + assert m2 in thing + + @pytest.mark.parametrize('Ki,uncertainty,dG,dG_uncertainty,label,temp', [ [100 * unit.nanomolar, 10 * unit.nanomolar, -9.55 * unit.kilocalorie_per_mole, 0.059 * unit.kilocalorie_per_mole, 'lig', 298.15 * unit.kelvin],