Skip to content

Commit

Permalink
Merge pull request #211 from SWIFTSIM/add_fields_to_repr
Browse files Browse the repository at this point in the history
Add fields to __repr__
  • Loading branch information
kyleaoman authored Nov 30, 2024
2 parents c24ac52 + c6dc700 commit 18d246b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
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

0 comments on commit 18d246b

Please sign in to comment.