field_array.py 1.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18

19
20
import numpy as np

21
from field_type import FieldType
22
23


theos's avatar
theos committed
24
class FieldArray(FieldType):
25

26
    def __init__(self, shape, dtype=np.float):
27
28
29
30
31
32
33
34
35
36
37
        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
38
39
40
    @property
    def dim(self):
        return reduce(lambda x, y: x*y, self.shape)
41
42
43
44
45

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
        hdf5_group['shape'] = self.shape
Theo Steininger's avatar
Theo Steininger committed
46
        hdf5_group.attrs['dtype'] = self.dtype.name
47
48
49
50
51
52

        return None

    @classmethod
    def _from_hdf5(cls, hdf5_group, loopback_get):
        result = cls(
Theo Steininger's avatar
Theo Steininger committed
53
54
            shape=hdf5_group['shape'][:],
            dtype=np.dtype(hdf5_group.attrs['dtype'])
55
56
            )
        return result