Skip to content

Test: fix unimplemented device export test#67

Merged
mstimberg merged 6 commits intobrian-team:masterfrom
Utkarsha2811:fix-broken-export-test
Mar 26, 2026
Merged

Test: fix unimplemented device export test#67
mstimberg merged 6 commits intobrian-team:masterfrom
Utkarsha2811:fix-broken-export-test

Conversation

@Utkarsha2811
Copy link
Copy Markdown
Contributor

@Utkarsha2811 Utkarsha2811 commented Mar 14, 2026

Summary

This PR fixes a broken unit test for unsupported export objects and resolves NumPy 2.x / Python 3.13 compatibility issues caused by stricter array-to-scalar conversion rules.

Changes

Fix broken unit test for unsupported export objects

The test_ExportDevice_unsupported test was meant to check that the exporter raises a NotImplementedError when it sees a Brian2 object it does not support.

The test was broken for two reasons:
• It used PoissonInput as the unsupported object, but PoissonInput is actually supported by the exporter.
• The pytest.raises(NotImplementedError) block had been commented out, so the test was no longer checking the expected failure.

This PR fixes the test by:
• replacing PoissonInput with a minimal custom class, UnsupportedObject, that directly subclasses BrianObject
• re-enabling the pytest.raises(NotImplementedError) assertion
• removing old setup code that was only needed for PoissonInput, including the NeuronGroup, equation string, and run(1*ms) call
• moving the BrianObject import to the top of the file to follow normal Python style

Since UnsupportedObject has no exporter implementation, running a Network containing it with the exporter device now correctly raises NotImplementedError. The test now passes and checks the intended behavior properly.

Fix failures on NumPy 2.x / Python 3.13

NumPy 2.x is stricter about converting arrays into scalar values. A few places in the code were relying on older NumPy behavior where a 1-element array could be treated like a plain number. These cases now fail in CI on Python 3.13.

test_baseexport.py — test_spikegenerator
SpikeGeneratorGroup stores spike times as a 1D array. The test was calling float() directly on this array when comparing it to the original input.

In NumPy 2.x, float() only accepts 0D arrays, so this raises a TypeError.

Fixed by extracting the first element with [0] on both sides before calling float().

mdexport/expander.py — expand_initializer
When a variable is initialized for only part of a neuron group, for example group.v[0:5] = ..., the index is stored as a NumPy integer array.

The code was comparing this array directly to the strings 'True' and 'False' using == inside an or expression. In NumPy, this returns an array of booleans, not a single boolean value. Using that in Python or raises:

ValueError: The truth value of an array with more than one element is ambiguous

Fixed by checking isinstance(index, str) before doing the string comparisons, so they only run when index is actually a string.

plotting/morphology.py — _plot_morphology2D
After calling generate_coordinates(), Brian2 returns Soma.x, Soma.y, and Soma.diameter as 1-element 1D arrays instead of plain scalar values.

Matplotlib’s Circle eventually passes these values into an internal affine transform that requires real Python floats, which causes a ValueError.

Fixed by applying np.squeeze() before float(). This converts a 1-element 1D array into a 0D array, which float() can safely convert to a scalar.

Result
• restores a meaningful unit test for unsupported export objects
• fixes CI failures with NumPy 2.x
• improves compatibility with Python 3.13

Copy link
Copy Markdown
Member

@mstimberg mstimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch! I left two minor comments in the review.

Comment thread brian2tools/tests/test_baseexport.py Outdated
run(10 * ms)
run(1*ms)

from brian2.core.base import BrianObject
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this import to the top of the file?

Comment thread brian2tools/tests/test_baseexport.py Outdated
_ = PoissonInput(G, 'g', 1, 1 * Hz, 1)
# with pytest.raises(NotImplementedError):
run(10 * ms)
run(1*ms)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the lines above – those are not actually tested here (for PoissonInput, you needed a NeuronGroup, but here you don't need anything else).

@Utkarsha2811
Copy link
Copy Markdown
Contributor Author

@mstimberg Thanks for the review, Looking into it.

@Utkarsha2811 Utkarsha2811 marked this pull request as draft March 17, 2026 16:56
@Utkarsha2811 Utkarsha2811 force-pushed the fix-broken-export-test branch from c0e9c85 to 9344604 Compare March 21, 2026 13:49
@Utkarsha2811
Copy link
Copy Markdown
Contributor Author

Hey @mstimberg,

I’ve updated the PR based on the review comments and also revised the PR description to reflect the changes more clearly. Please take a look when you get a chance and let me know what you think.

@Utkarsha2811 Utkarsha2811 requested a review from mstimberg March 21, 2026 17:01
@Utkarsha2811 Utkarsha2811 marked this pull request as ready for review March 21, 2026 17:01
@Utkarsha2811 Utkarsha2811 deleted the fix-broken-export-test branch March 21, 2026 17:42
@Utkarsha2811 Utkarsha2811 restored the fix-broken-export-test branch March 21, 2026 17:44
@Utkarsha2811 Utkarsha2811 reopened this Mar 21, 2026
Copy link
Copy Markdown
Member

@mstimberg mstimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, looks good to me, except for a small nitpick/suggestion (see below).

Comment thread brian2tools/plotting/morphology.py Outdated
Copy link
Copy Markdown
Member

@mstimberg mstimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mstimberg mstimberg merged commit 629ca03 into brian-team:master Mar 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants