Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions autofit/mapper/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions test_autofit/database/identifier/test_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Loading