Skip to content

Commit

Permalink
Merge pull request #430 from European-XFEL/kd-size
Browse files Browse the repository at this point in the history
Implement .nbytes, .size_mb, and .size_gb for KeyData
  • Loading branch information
JamesWrigley authored Jul 25, 2023
2 parents 6a889bc + c970c73 commit e8ffdb7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/reading_files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ below, e.g.::

.. automethod:: data_counts

.. autoattribute:: nbytes

.. autoattribute:: size_mb

.. autoattribute:: size_gb

.. automethod:: ndarray

.. automethod:: series
Expand Down
15 changes: 15 additions & 0 deletions extra_data/keydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ def shape(self):
"""
return (sum(c.total_count for c in self._data_chunks),) + self.entry_shape

@property
def nbytes(self):
"""The number of bytes this data would take up in memory."""
return self.dtype.itemsize * np.prod(self.shape)

@property
def size_mb(self):
"""The size of the data in memory in megabytes."""
return self.nbytes / 1e6

@property
def size_gb(self):
"""The size of the data in memory in gigabytes."""
return self.nbytes / 1e9

@property
def source_file_paths(self):
paths = []
Expand Down
3 changes: 3 additions & 0 deletions extra_data/tests/test_keydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def test_get_keydata(mock_spb_raw_run):
'RAW-R0238-DA01-S00000.h5', 'RAW-R0238-DA01-S00001.h5'
}

data = xgm_beam_x.ndarray()
assert xgm_beam_x.nbytes == data.nbytes

def test_select_trains(mock_spb_raw_run):
run = RunDirectory(mock_spb_raw_run)
xgm_beam_x = run['SPB_XTD9_XGM/DOOCS/MAIN', 'beamPosition.ixPos.value']
Expand Down

0 comments on commit e8ffdb7

Please sign in to comment.