Skip to content

fix: model identifier raises TypeError on 0-d array attributes #1784

Closed

Description

@Jammy2211

Overview

Putting a model instance into an af.Collection (e.g. fixing a lens to its truth by inserting the true al.Galaxy) makes the model identifier raise TypeError: iteration over a 0-d array at autofit/mapper/identifier.py:185 when an attribute of that instance is a 0-d NumPy (or JAX) array. Found 2026-11-14 in the ep_gnfw_slope science project, worked around there with model.replacing floats.

Reproduced on main (45e8cd3a):

inst = Profile(centre=np.array(0.5), slope=np.array(2.0))
model = af.Collection(lens=inst, g=af.Model(af.ex.Gaussian))
Identifier(model)   # TypeError: iteration over a 0-d array (identifier.py:185)

Cause: a 0-d array satisfies isinstance(value, Iterable) (it defines __iter__), so _add_value_to_hash_list tries to loop over it.

Plan

  • Hash a 0-d array as the scalar it holds (value.item()), so it goes through the existing float/int/bool branches.
  • A 0-d float array then hashes exactly like the equivalent Python float (same identifier as the model.replacing workaround); no existing identifier changes.
  • Covers NumPy and JAX (both expose ndim and .item()).
  • Add unit tests: identifier computes without error, is stable across two computations, and equals the plain-float identifier.

Tier: glance — effective level: supervised; merge mode: auto-merge on green if the Witness passes after a merge slot (shift worker: ends at PR-open).

Detailed implementation plan

Affected repositories

  • PyAutoFit (primary)

Work classification

Library

Branch survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/model-identifier-0d-array
Worktree root: ~/Code/PyAutoLabs-wt/model-identifier-0d-array/

Implementation steps

  1. autofit/mapper/identifier.py, Identifier._add_value_to_hash_list: before the __dict__ branch, if getattr(value, "ndim", None) == 0 and the value has .item, replace it with value.item() and recurse.
  2. test_autofit/mapper/test_identifier.py (or the existing identifier test module): reproducer with a plain class holding 0-d np.array attributes inside an af.Collection; assert no error, stable across two Identifier calls, equal to the float-valued model's identifier.

Key files

  • autofit/mapper/identifier.py — hashing walk
  • identifier unit tests

Witness

the identifier of a model holding an instance with a 0-d array attribute is computed without error and is identical across two runs

Activity

  1. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR opened: #1785 (shift 2026-11-18-pm2, supervised, glance → stops at PR-open, labelled decision-taken).

    Workspace impact: (iii) none. It is an internal hashing fix that changes no existing identifier, and smoke passed 213/0.

    Decision flagged: the PR covers only the NumPy 0-d crash. JAX arrays hash value-blind (['ArrayImpl']) and never crashed; fixing that changes existing identifiers, so it went to a separate judge draft.

  2. Jammy2211 commented on Nov 19, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped: #1785 merged 2026-11-19T08:19Z (99b04b1) by human /prm after shift 2026-11-18-pm2. 0-d NumPy arrays now hash as their scalar in the model identifier; no existing identifier changes. JAX-array and NumPy-scalar hashing follow up in draft/bug/autofit/model_identifier_ignores_jax_array_and_numpy_scalar_values.md. Pending release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions