Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fields to __repr__ #211

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions docs/source/loading_data/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,49 @@ as a summary:
Reading particle data
---------------------

To find out what particle properties are present in our snapshot, we can use
the instance of :obj:`swiftsimio.reader.SWIFTMetadata`, ``data.metadata``,
To find out what particle properties are present in our snapshot, we can print
the available particle types. For example:

.. code-block:: python

data

prints the available particle types (or, more generally, groups):

.. code-block:: python

SWIFT dataset at cosmo_volume_example.hdf5.
Available groups: gas, dark_matter, stars, black_holes

The properties available for a particle type can be similarly printed:

.. code-block:: python

data.dark_matter

gives:

.. code-block:: python

SWIFT dataset at cosmo_volume_example.hdf5.
Available fields: coordinates, masses, particle_ids, velocities

With compatible python interpreters, the available fields (and other attributes
such as functions) can be seen using the tab completion feature, for example
typing `>>> data.dark_matter.` at the command prompt and pressing tab twice
gives:

.. code-block:: python

data.dark_matter.coordinates data.dark_matter.masses
data.dark_matter.filename data.dark_matter.metadata
data.dark_matter.particle_ids data.dark_matter.generate_empty_properties()
data.dark_matter.group data.dark_matter.units
data.dark_matter.group_metadata data.dark_matter.velocities
data.dark_matter.group_name

The available fields can also be accessed programatically using the instance of
:obj:`swiftsimio.reader.SWIFTMetadata`, ``data.metadata``,
which contains several instances of
:obj:`swiftsimio.reader.SWIFTParticleTypeMetadata` describing what kinds of
fields are present in gas or dark matter:
Expand Down Expand Up @@ -303,4 +344,4 @@ between different metadata types, based upon a property stored in the HDF5 file,
and use the :obj:`swiftsimio.metadata.objects.SWIFTSnapshotMetadata`
class. If it is ``SOAP``, we use
:obj:`swiftsimio.metadata.objects.SWIFTSOAPMetadata`, which instructs
``swiftsimio`` to read slightly different properties from the HDF5 file.
``swiftsimio`` to read slightly different properties from the HDF5 file.
15 changes: 13 additions & 2 deletions swiftsimio/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ def generate_empty_properties(self):

return

def __str__(self):
"""
Prints out some more useful information, rather than just
the memory location.
"""
field_names = ", ".join(self.group_metadata.field_names)
return f"SWIFT dataset at {self.filename}. \nAvailable fields: {field_names}"

def __repr__(self):
return self.__str__()


class __SWIFTNamedColumnDataset(object):
"""
Expand Down Expand Up @@ -572,8 +583,8 @@ def __str__(self):
Prints out some more useful information, rather than just
the memory location.
"""

return f"SWIFT dataset at {self.filename}."
group_names = ", ".join(self.metadata.present_group_names)
return f"SWIFT dataset at {self.filename}. \nAvailable groups: {group_names}"

def __repr__(self):
return self.__str__()
Expand Down
Loading