| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright © 2016, German Neuroinformatics Node (G-Node) |
| 3 |
# |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted under the terms of the BSD License. See |
| 8 |
# LICENSE file in the root of the Project. |
| 9 |
from six import ensure_str |
| 10 |
import numpy as np |
| 11 |
from ..datatype import DataType |
| 12 |
from .. import util |
| 13 |
|
| 14 |
|
| 15 |
class H5DataSet: |
| 16 |
|
| 17 |
def __init__(self, parent, name, dtype=None, shape=None, |
| 18 |
compression=False): |
| 19 |
self._parent = parent |
| 20 |
self.name = name |
| 21 |
if (dtype is None) or (shape is None): |
| 22 |
self.dataset = self._parent[name] |
| 23 |
else: |
| 24 |
maxshape = (None,) * len(shape) |
| 25 |
if dtype == DataType.String: |
| 26 |
dtype = util.vlen_str_dtype |
| 27 |
comprargs = dict() |
| 28 |
if compression: |
| 29 |
comprargs = {"compression": "gzip", "compression_opts": 6} |
| 30 |
self.dataset = self._parent.require_dataset( |
| 31 |
name, shape=shape, dtype=dtype, chunks=True, maxshape=maxshape, |
| 32 |
**comprargs |
| 33 |
) |
| 34 |
self.h5obj = self.dataset |
| 35 |
|
| 36 |
@classmethod |
| 37 |
def create_from_h5obj(cls, h5obj): |
| 38 |
parent = h5obj.parent |
| 39 |
name = h5obj.name.split("/")[-1] |
| 40 |
return cls(parent, name) |
| 41 |
|
| 42 |
def write_data(self, data, slc=None): |
| 43 |
if data is None: # py2compat |
| 44 |
data = np.full(self.shape, np.nan)[slc] |
| 45 |
if slc is None: |
| 46 |
self.dataset[:] = data |
| 47 |
else: |
| 48 |
self.dataset[slc] = data |
| 49 |
|
| 50 |
def read_data(self, slc=None): |
| 51 |
if slc is None: |
| 52 |
slc = slice(None, None, None) |
| 53 |
try: |
| 54 |
data = self.dataset[slc] |
| 55 |
except ValueError as ve_exc: |
| 56 |
# h5py throws ValueError for out-of-bounds index |
| 57 |
# Let's change it to IndexError |
| 58 |
raise IndexError(ve_exc) |
| 59 |
except TypeError as te_exc: |
| 60 |
# h5py 2.10 in Python2 throws TypeError for out-of-bounds index |
| 61 |
# Let's change it to IndexError |
| 62 |
raise IndexError(te_exc) |
| 63 |
if data.dtype == util.vlen_str_dtype: |
| 64 |
data = np.reshape(np.array(list(map(ensure_str, data.ravel())), dtype=object), data.shape) |
| 65 |
elif data.dtype.fields: |
| 66 |
data = self._convert_string_cols(data) |
| 67 |
return data |
| 68 |
|
| 69 |
@staticmethod |
| 70 |
def _convert_string_cols(data): |
| 71 |
str_cols = list() |
| 72 |
for field_name, (col_type, _) in data.dtype.fields.items(): |
| 73 |
if col_type == util.vlen_str_dtype: |
| 74 |
str_cols.append(field_name) |
| 75 |
|
| 76 |
def conv_row(row): |
| 77 |
for field in str_cols: |
| 78 |
row[field] = ensure_str(row[field]) |
| 79 |
if str_cols: |
| 80 |
if not data.shape: |
| 81 |
# single row |
| 82 |
conv_row(data) |
| 83 |
else: |
| 84 |
# multiple rows |
| 85 |
for row in data: |
| 86 |
conv_row(row) |
| 87 |
return data |
| 88 |
|
| 89 |
def set_attr(self, name, value): |
| 90 |
if value is None: |
| 91 |
if name in self.dataset.attrs: |
| 92 |
del self.dataset.attrs[name] |
| 93 |
else: |
| 94 |
self.dataset.attrs[name] = value |
| 95 |
|
| 96 |
def get_attr(self, name): |
| 97 |
attr = self.dataset.attrs.get(name) |
| 98 |
if isinstance(attr, bytes): |
| 99 |
attr = attr.decode() |
| 100 |
return attr |
| 101 |
|
| 102 |
@property |
| 103 |
def shape(self): |
| 104 |
return self.dataset.shape |
| 105 |
|
| 106 |
@shape.setter |
| 107 |
def shape(self, shape): |
| 108 |
self.dataset.resize(shape) |
| 109 |
|
| 110 |
@property |
| 111 |
def dtype(self): |
| 112 |
dtype = self.dataset.dtype |
| 113 |
if dtype == util.vlen_str_dtype: |
| 114 |
return DataType.String |
| 115 |
return dtype |
| 116 |
|
| 117 |
def __str__(self): |
| 118 |
return "<H5DataSet object: {}>".format(self.dataset.name) |