diff --git a/autofit/mapper/identifier.py b/autofit/mapper/identifier.py index db17dab20..117cd1c32 100644 --- a/autofit/mapper/identifier.py +++ b/autofit/mapper/identifier.py @@ -182,6 +182,13 @@ def _add_value_to_hash_list(self, value): elif isinstance(value, (str, int, bool)): self.hash_list.append(str(value)) elif isinstance(value, Iterable): + if getattr(value, "ndim", None) == 0: + # A 0-d NumPy array is Iterable by type but cannot be iterated: + # hash the scalar it holds, so it matches the equivalent Python + # value. NumPy scalars such as np.float64 are not Iterable and JAX + # arrays take the __dict__ branch above, so neither reaches here. + self.add_value_to_hash_list(value.item()) + return for value in value: self.add_value_to_hash_list(value) diff --git a/test_autofit/database/identifier/test_identifiers.py b/test_autofit/database/identifier/test_identifiers.py index cc179deb3..f4f154f2f 100644 --- a/test_autofit/database/identifier/test_identifiers.py +++ b/test_autofit/database/identifier/test_identifiers.py @@ -632,3 +632,27 @@ def test_assertion_name_does_not_change_identifier(): collection.add_assertion(a.sigma < b.sigma, name="ordered") assert Identifier(collection) == Identifier(_asserted_collection()) + + +class ZeroDimProfile: + def __init__(self, centre=0.0, slope=1.0): + self.centre = centre + self.slope = slope + + +def _zero_dim_model(centre, slope): + return af.Collection( + lens=ZeroDimProfile(centre=centre, slope=slope), + gaussian=af.Model(af.ex.Gaussian), + ) + + +def test_instance_with_0d_numpy_array_attribute(): + model = _zero_dim_model(np.array(0.5), np.array(2.0)) + + first = str(Identifier(model)) + second = str(Identifier(_zero_dim_model(np.array(0.5), np.array(2.0)))) + + assert first == second + assert first == str(Identifier(_zero_dim_model(0.5, 2.0))) + assert first != str(Identifier(_zero_dim_model(np.array(0.5), np.array(3.0))))