Skip to content

Fix particleset repr - #3021

Open
erikvansebille wants to merge 2 commits into
Parcels-code:mainfrom
erikvansebille:fix_particleset_repr
Open

erikvansebille wants to merge 2 commits into
Parcels-code:mainfrom
erikvansebille:fix_particleset_repr

Conversation

@erikvansebille

Copy link
Copy Markdown
Member

Description

This PR (re-)implements ParticleSet.__repr__ and ParticleSetView.__repr__. It also adds tests and improves/cleans up formatting

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Describe how you used it (e.g., by pasting your prompt):

CoPilot has helped me with some of the changes to the ParticleSetView.__repr__

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Don't print empty attrs dictionaries

This branch has not been deployed

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Improve ParticleSet.__repr__

1 participant