Repository navigation
Fix particleset repr - #3021
Open
erikvansebille wants to merge 2 commits into
Open
Fix particleset repr#3021erikvansebille wants to merge 2 commits into
erikvansebille wants to merge 2 commits into
Conversation
Improving readability and extending unit tests
erikvansebille
commented
Oct 29, 2026
| def __getitem__(self, index): | ||
| """Get a single particle by index.""" | ||
| return ParticleSetView(self._data, index=index, pclass=self._pclass) | ||
| return ParticleSetView(self._data, index=index, pclass=self._pclass, time_interval=self.fieldset.time_interval) |
Member
Author
There was a problem hiding this comment.
This is needed in order to print full dates (using parcels.float_to_datelike) for e.g. print(pset[0]). If we don't add the time-Interval, these Particle.t could only be printed as floats.
Do you agree the date-printing is worth the extra complexity of adding another argument to ParticleSetView.__init__?
| time_string = "not_yet_set" if pview.t is None or np.isnan(pview.t) else f"{pview.t:f}" | ||
| out = f"P[{pview.particle_id}]: time={time_string}, z={pview.z:f}, y={pview.y:f}, x={pview.x:f}" | ||
| vars = [v.name for v in pview._pclass.variables if v.to_write is True and v.name not in ["z", "y", "x", "time"]] | ||
| from parcels._core.utils.time import float_to_datelike |
Member
Author
There was a problem hiding this comment.
Need to import here because otherwise a circular import?
E ImportError: cannot import name 'float_to_datelike' from partially initialized module 'parcels._core.utils.time' (most likely due to a circular import) (/Users/erik/Codes/parcels/src/parcels/_core/utils/time.py)
| """Return a pretty repr for Variable""" | ||
| return f"Variable(name={var._name!r}, dtype={var.dtype!r}, initial={var.initial!r}, to_write={var.to_write!r}, attrs={var.attrs!r})" | ||
| out = f"Variable(name={var._name!r}, dtype={np.dtype(var.dtype).name!r}, initial={var.initial!r}, to_write={var.to_write!r}" | ||
| if getattr(var, "attrs", None): |
Member
Author
There was a problem hiding this comment.
Don't print empty attrs dictionaries
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR (re-)implements
ParticleSet.__repr__andParticleSetView.__repr__. It also adds tests and improves/cleans up formattingChecklist
mainfor normal development,v3-supportfor v3 support)AI Disclosure
CoPilot has helped me with some of the changes to the
ParticleSetView.__repr__