Distributions fix - #92
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the distributions ParaView plugin to avoid crashing when a distribution’s species name is missing by catching an exception during distribution-name construction (notably for ASTRA scenarios).
Changes:
- Wrap distribution name generation in a
try/exceptto handle missing species name fields and fall back to a default name. - Emit a warning when a distribution name cannot be derived from the IDS.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try: | ||
| dist_name = self._create_dist_name(dist) | ||
| except AttributeError: | ||
| logger.warning("distribution %d does not contain any names, using %d", i, i) | ||
| dist_name = f"distribution {i}" |
|
@SBlokhuizen I am aware you contract is cloed, but could you have a look if this seems ok to you? |
|
Hi Paulo, an It'd be better to handle that case in the _create_dist_name function instead of wrapping it in a try-except at the call side IMO |
|
Hi @maarten-ic thanks for the quick update. |
|
@maarten-ic |
|
Hi Paulo,
Because the method could (though it's probably not right now) be used from multiple places, therefore it's better to handle the exception inside the method. Regardless of the solution 😉
IMAS-ParaView already does this in multiple places, so for consistency I'd handle it here as well. See, for example, IMAS-ParaView/imas_paraview/ids_util.py Lines 144 to 150 in 2fba6cd (Thanks @SBlokhuizen ) |
Looks fine to me! |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (5)
imas_paraview/plugins/distributions.py:174
- Same fallback pattern here can be simplified with getattr to avoid exception-driven control flow and to handle absent state/name/label consistently.
try:
state_name = species.ion.state.name
except AttributeError: # DD3 has label instead of name
state_name = species.ion.state.label
result += f" State ({state_name})"
imas_paraview/plugins/distributions.py:181
- Prefer getattr-based fallback over try/except for neutral naming as well; it reduces duplication and avoids relying on AttributeError for normal DD3/DD4 differences.
try:
neutral_name = species.neutral.name
except AttributeError: # DD3 has label instead of name
neutral_name = species.neutral.label
result = f"Neutral ({neutral_name})"
imas_paraview/plugins/distributions.py:188
- Same as above: simplify the neutral state name fallback with getattr to avoid exception-driven branching and handle missing fields consistently.
try:
state_name = species.neutral.state.name
except AttributeError: # DD3 has label instead of name
state_name = species.neutral.state.label
result += f" State ({state_name})"
imas_paraview/plugins/distributions.py:166
- This change adds DD3
.labelfallbacks, but the existing unit test coverage (imas_paraview/tests/test_distributions.py:test_distribution_name) only exercises the DD4.namepath. Add a test that constructs a distribution where ion/neutral use.label(and.nameis absent) to prevent regressions.
try:
ion_name = species.ion.name
except AttributeError: # DD3 has label instead of name
ion_name = species.ion.label
imas_paraview/plugins/distributions.py:167
- Using try/except for attribute fallbacks makes this harder to read/maintain and still doesn’t handle missing/None values cleanly. Prefer getattr-based fallback (name -> label -> a safe default) without exceptions.
This issue also appears in the following locations of the same file:
- line 163
- line 170
- line 177
- line 184
try:
ion_name = species.ion.name
except AttributeError: # DD3 has label instead of name
ion_name = species.ion.label
result = f"Ion ({ion_name})"
|
Hi Paulo, the changes look fine to me! The CI fails because there is an linting/formatting issue, you can check the issue locally by running the following: You can read more about this in the documentation. If you need any help, please let me know! |
|
Thanks, this has been very helpful. I made some minor changes to make it more readable. I removed the |
|
Thanks Paulo! This looks ok to me, though |
captures an exception in the distributions IDS that would be triggered with some species did not have a name defined. This is particular relevant for ASTRA scenarios.