field_array.py 926 Bytes
Newer Older
1
2
# -*- coding: utf-8 -*-

3
4
import numpy as np

5
from field_type import FieldType
6
7


theos's avatar
theos committed
8
class FieldArray(FieldType):
9

10
    def __init__(self, shape, dtype=np.float):
11
12
13
14
15
16
17
18
19
20
21
        try:
            new_shape = tuple([int(i) for i in shape])
        except TypeError:
            new_shape = (int(shape), )
        self._shape = new_shape
        super(FieldArray, self).__init__(dtype=dtype)

    @property
    def shape(self):
        return self._shape

theos's avatar
theos committed
22
23
24
    @property
    def dim(self):
        return reduce(lambda x, y: x*y, self.shape)
25
26
27
28
29

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
        hdf5_group['shape'] = self.shape
Theo Steininger's avatar
Theo Steininger committed
30
        hdf5_group.attrs['dtype'] = self.dtype.name
31
32
33
34
35
36

        return None

    @classmethod
    def _from_hdf5(cls, hdf5_group, loopback_get):
        result = cls(
Theo Steininger's avatar
Theo Steininger committed
37
38
            shape=hdf5_group['shape'][:],
            dtype=np.dtype(hdf5_group.attrs['dtype'])
39
40
            )
        return result