| 1 |
""" |
| 2 |
A grammar producing rows from a table within an HDF5 file. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import h5py |
| 12 |
|
| 13 |
from gavo.grammars import common |
| 14 |
from gavo import base |
| 15 |
|
| 16 |
|
| 17 |
class AstropyHDF5TableIterator(common.RowIterator): |
| 18 |
"""A row iterator generating rawdicts from Astropy-serialised HDF5 |
| 19 |
tables. |
| 20 |
|
| 21 |
The table is assumed to contain record arrays; NULL values are properly |
| 22 |
handled through associated .mask columns. |
| 23 |
""" |
| 24 |
def _makeRowBuilder(self, sourceDS): |
| 25 |
"""returns a function that builds rawdicts from a h5py dataset |
| 26 |
sourceDS. |
| 27 |
|
| 28 |
It is aware of the astropy convention of adding boolean .mask |
| 29 |
columns and uses these to produced Nones. |
| 30 |
""" |
| 31 |
names, nullMap = [], {} |
| 32 |
|
| 33 |
for index, name in enumerate(sourceDS.dtype.names): |
| 34 |
if name.endswith(".mask"): |
| 35 |
nullMap[index] = name[:-5] |
| 36 |
names.append(None) |
| 37 |
|
| 38 |
else: |
| 39 |
names.append(name) |
| 40 |
|
| 41 |
if nullMap: |
| 42 |
def makeRow(row): |
| 43 |
res = dict(zip(names, row)) |
| 44 |
del res[None] |
| 45 |
for index, name in nullMap.items(): |
| 46 |
if row[index]: |
| 47 |
res[name] = None |
| 48 |
return res |
| 49 |
|
| 50 |
return makeRow |
| 51 |
|
| 52 |
else: |
| 53 |
return lambda row: dict(zip(names, row)) |
| 54 |
|
| 55 |
def _iterRows(self): |
| 56 |
hdf = h5py.File(self.sourceToken, "r") |
| 57 |
try: |
| 58 |
sourceDS = hdf[self.grammar.dataset] |
| 59 |
except KeyError: |
| 60 |
raise base.ReportableError(f"Dataset {self.grammar.dataset} not" |
| 61 |
f" found in {self.sourceToken}. The following datasets" |
| 62 |
" are visible in the root: "+(", ".join(hdf.keys()))) |
| 63 |
|
| 64 |
buildRow = self._makeRowBuilder(sourceDS) |
| 65 |
|
| 66 |
for row in sourceDS: |
| 67 |
yield buildRow(row) |
| 68 |
|
| 69 |
|
| 70 |
class VaexHDF5TableIterator(common.RowIterator): |
| 71 |
"""A row iterator generating rawdicts from Vaex-serialised HDF5 |
| 72 |
tables. |
| 73 |
|
| 74 |
Here, the columns come in separate arrays, much like FITS tables. |
| 75 |
""" |
| 76 |
_chunkSize = 10000 |
| 77 |
|
| 78 |
def _iterRows(self): |
| 79 |
hdf = h5py.File(self.sourceToken, "r") |
| 80 |
try: |
| 81 |
sourceDS = hdf[self.grammar.dataset] |
| 82 |
except KeyError: |
| 83 |
raise base.ReportableError(f"Dataset {self.grammar.dataset} not" |
| 84 |
f" found in {self.sourceToken}. Note that we want the" |
| 85 |
" parent of the columns group here. The following datasets" |
| 86 |
" are visible in the root: "+(", ".join(hdf.keys()))) |
| 87 |
|
| 88 |
cols = sourceDS["columns"] |
| 89 |
names = cols.keys() |
| 90 |
arrs = [c["data"] for c in cols.values()] |
| 91 |
|
| 92 |
for offset in range(0, arrs[0].shape[0], self._chunkSize): |
| 93 |
curArrs = [arr[offset:offset+self._chunkSize] |
| 94 |
for arr in arrs] |
| 95 |
for index in range(len(curArrs[0])): |
| 96 |
yield dict(zip(names, [arr[index] for arr in curArrs])) |
| 97 |
|
| 98 |
|
| 99 |
class HDF5Grammar(common.Grammar): |
| 100 |
"""a grammar for parsing single tables from HDF5 files. |
| 101 |
|
| 102 |
These result in typed records, i.e., values normally come in |
| 103 |
the types they are supposed to have. The keys in the rows are |
| 104 |
the column names as given in the HDF file. |
| 105 |
|
| 106 |
Regrettably, there are about as many conventions to serialise tables |
| 107 |
in HDF5 as there are programmes writing HDF5. This grammar supports |
| 108 |
a few styles; ask to have more included. |
| 109 |
|
| 110 |
Styles currently implemented: |
| 111 |
|
| 112 |
:astropy: |
| 113 |
The table comes as a record array. The grammar is aware of the astropy |
| 114 |
convention of using adding mask columns as name+".mask" and will turn |
| 115 |
masked values to Nones. |
| 116 |
:vaex: |
| 117 |
The table comes as a group with the columns as individual arrays |
| 118 |
in the group member's data dataset. Put the parent of the columns |
| 119 |
group into the dataset attribute here. |
| 120 |
|
| 121 |
This class is not intended for ingesting large HDF5 files, as it will only |
| 122 |
process a few thousand rows per second on usual hardware. Use |
| 123 |
`Element directgrammar`_ for large files. |
| 124 |
|
| 125 |
This requires h5py (python package: python3-h5py). |
| 126 |
""" |
| 127 |
name_ = "hdf5Grammar" |
| 128 |
|
| 129 |
_dataset = base.UnicodeAttribute("dataset", |
| 130 |
default=base.Undefined, |
| 131 |
description="The name of the HDF5 dataset/group containing the table." |
| 132 |
" At this point, only datasets that are children of root are" |
| 133 |
" supported.", |
| 134 |
copyable=True) |
| 135 |
_style = base.EnumeratedUnicodeAttribute("style", |
| 136 |
default="astropy", |
| 137 |
validValues=["astropy", "vaex"], |
| 138 |
description="Style of the table serialisation.", |
| 139 |
copyable=True) |
| 140 |
|
| 141 |
rowIterator = AstropyHDF5TableIterator |
| 142 |
|
| 143 |
def onElementComplete(self): |
| 144 |
if self.style=="vaex": |
| 145 |
self.rowIterator = VaexHDF5TableIterator |
| 146 |
|
| 147 |
|
| 148 |
if __name__=="__main__": |
| 149 |
f = h5py.File("/home/msdemlei/tmp/hdparse/hdex.hdf5", "r") |
| 150 |
table = f["testdata"] |
| 151 |
print(table.shape) |
| 152 |
print(table.dtype) |
| 153 |
for row in table: |
| 154 |
print(row) |