core.py 16.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/usr/bin/env
# encoding: utf-8
"""
Author:     Daniel Boeckenhoff
Mail:       daniel.boeckenhoff@ipp.mpg.de

core of tfields library
contains numpy ndarray derived bases of the tfields package
"""
10
import tfields.bases
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from contextlib import contextmanager
from collections import Counter
np.seterr(all='warn', over='raise')


def rank(tensor):
    """
    Tensor rank
    """
    return len(tensor.shape) - 1


def dim(tensor):
    """
    Manifold dimension
    """
    if rank(tensor) == 0:
        return 1
    return tensor.shape[1]


class AbstractNdarray(np.ndarray):
    """
    All tensors and subclasses should derive from AbstractNdarray.
    AbstractNdarray implements all the inheritance specifics for np.ndarray
    Whene inheriting, three attributes are of interest:
        __slots__ (list of str): If you want to add attributes to
            your AbstractNdarray subclass, add the attribute name to __slots__
        __slotDefaults__ (list): if __slotDefaults__ is None, the
            defaults for the attributes in __slots__ will be None
            other values will be treaded as defaults to the corresponding
            arg at the same position in the __slots__ list.
        __slotDtype__ (list of types): for the conversion of the
            args in __slots__ to numpy arrays. None values mean no
            conversion.

    Args:
        array (array-like): input array
        **kwargs: arguments corresponding to __slots__
    TODO:
        equality check
    """
    __slots__ = []
    __slotDefaults__ = []
    __slotDtypes__ = []
    __slotSetters__ = []

59
    def __new__(cls, array, **kwargs):  # pragma: no cover
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        raise NotImplementedError("{clsType} type must implement '__new__'"
                                  .format(clsType=type(cls)))

    def __array_finalize__(self, obj):
        if obj is None:
            return
        for attr in self._iterSlots():
            setattr(self, attr, getattr(obj, attr, None))

    def __array_wrap__(self, out_arr, context=None):
        return np.ndarray.__array_wrap__(self, out_arr, context)

    @classmethod
    def _iterSlots(cls, skipCache=True):
        return [att for att in cls.__slots__ if att != '_cache']

    @classmethod
    def _updateSlotKwargs(cls, kwargs, skipCache=True):
        """
        set the defaults in kwargs according to __slotDefaults__
        and convert the kwargs according to __slotDtypes__
        """
        slotDefaults = cls.__slotDefaults__ + \
            [None] * (len(cls.__slots__) - len(cls.__slotDefaults__))
        slotDtypes = cls.__slotDtypes__ + \
            [None] * (len(cls.__slots__) - len(cls.__slotDtypes__))
        for attr, default, dtype in zip(cls.__slots__, slotDefaults, slotDtypes):
            if attr == '_cache':
                continue
            if attr not in kwargs:
                kwargs[attr] = default
            if dtype is not None:
                kwargs[attr] = np.array(kwargs[attr], dtype=dtype)

    def __setattr__(self, name, value):
        if name in self.__slots__:
            index = self.__slots__.index(name)
            try:
                setter = self.__slotSetters__[index]
            except IndexError:
                setter = None
            if setter is not None:
                value = setter(value)
        super(AbstractNdarray, self).__setattr__(name, value)

    def __reduce__(self):
        """
        important for pickling
        Examples:
            >>> from tempfile import NamedTemporaryFile
            >>> import pickle
111
            >>> import tfields
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

            Build a dummy scalar field
            >>> from tfields import Tensors, TensorFields
            >>> scalars = Tensors([0, 1, 2])
            >>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
            >>> scalarField = TensorFields(vectors, scalars, coordSys='cylinder')

            Save it and restore it
            >>> outFile = NamedTemporaryFile(suffix='.pickle')

            >>> pickle.dump(scalarField,
            ...             outFile)
            >>> _ = outFile.seek(0)

            >>> sf = pickle.load(outFile)
            >>> sf.coordSys == 'cylinder'
            True
            >>> sf.fields[0][2] == 2.
            True

        """
        # Get the parent's __reduce__ tuple
        pickled_state = super(AbstractNdarray, self).__reduce__()

        # Create our own tuple to pass to __setstate__
        new_state = pickled_state[2] + tuple([getattr(self, slot) for slot in
                                              self._iterSlots()])

        # Return a tuple that replaces the parent's __setstate__ tuple with our own
        return (pickled_state[0], pickled_state[1], new_state)

    def __setstate__(self, state):
        """
        important for unpickling
        """
        # Call the parent's __setstate__ with the other tuple elements.
        super(AbstractNdarray, self).__setstate__(state[0:-len(self._iterSlots())])

        # set the __slot__ attributes
        for i, slot in enumerate(reversed(self._iterSlots())):
            index = -(i + 1)
            setattr(self, slot, state[index])


class Tensors(AbstractNdarray):
    """
    Set of tensors with the same basis.
    TODO:
        all slot args should be protected -> _base
    Args:
        tensors: np.ndarray or AbstractNdarray subclass
    Examples:
        >>> from tfields import Tensors
        >>> import numpy as np

        Initialize a scalar range
        >>> scalars = Tensors([0, 1, 2])
        >>> scalars.rank == 0
        True

        Initialize vectors
        >>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
        >>> vectors.rank == 1
        True
        >>> vectors.dim == 3
        True
        >>> assert vectors.coordSys == 'cartesian'

        Initialize the Levi-Zivita Tensor
        >>> matrices = Tensors([[[0, 0, 0], [0, 0, 1], [0, -1, 0]],
        ...                     [[0, 0, -1], [0, 0, 0], [1, 0, 0]],
        ...                     [[0, 1, 0], [-1, 0, 0], [0, 0, 0]]])
        >>> matrices.shape == (3, 3, 3)
        True
        >>> matrices.rank == 2
        True
        >>> matrices.dim == 3
        True

        Initializing in different start coordinate system
        >>> cyl = Tensors([[5, np.arctan(4. / 3.), 42]], coordSys='cylinder')
        >>> assert cyl.coordSys == 'cylinder'
        >>> cyl.coordinateTransform('cartesian')
        >>> assert cyl.coordSys == 'cartesian'
        >>> cart = cyl
        >>> assert round(cart[0, 0], 10) == 3.
        >>> assert round(cart[0, 1], 10) == 4.
        >>> assert cart[0, 2] == 42

        Initialize with copy constructor keeps the coordinate system
        >>> with vectors.tempCoordSys('cylinder'):
        ...     vectCyl = Tensors(vectors)
        ...     assert vectCyl.coordSys == vectors.coordSys
        >>> assert vectCyl.coordSys == 'cylinder'

        You can demand a special dimension.
        >>> _ = Tensors([[1, 2, 3]], dim=3)
        >>> _ = Tensors([[1, 2, 3]], dim=2)  # doctest: +ELLIPSIS
        Traceback (most recent call last):
            ...
        ValueError: Incorrect dimension: 3 given, 2 demanded.

214
215
216
217
218
219
220
221
222
        The dimension argument (dim) becomes necessary if you want to initialize
        an empty array
        >>> _ = Tensors([])  # doctest: +ELLIPSIS
        Traceback (most recent call last):
            ...
        ValueError: Empty tensors need dimension parameter 'dim'.
        >>> Tensors([], dim=7)
        Tensors([], shape=(0, 7), dtype=float64)

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    """
    __slots__ = ['coordSys']
    __slotDefaults__ = ['cartesian']
    __slotSetters__ = [tfields.bases.getCoordSystemName]

    def __new__(cls, tensors, **kwargs):
        ''' copy constructor '''
        if issubclass(type(tensors), cls):
            obj = tensors.copy()
            coordSys = kwargs.pop('coordSys', None)
            if kwargs:
                raise AttributeError("In copy constructor only 'coordSys' "
                                     "attribute is supported. Kwargs {kwargs} "
                                     "are not consumed"
                                     .format(**locals()))
            if coordSys is not None:
                obj.coordinateTransform(coordSys)
            return obj

        dtype = kwargs.pop('dtype', np.float64)
        order = kwargs.pop('order', None)
        dim = kwargs.pop('dim', None)

        ''' process empty inputs '''
        if len(tensors) == 0:
            if dim is not None:
249
                tensors = np.empty((0, dim))
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
            else:
                raise ValueError("Empty tensors need dimension "
                                 "parameter 'dim'.")

        tensors = np.asarray(tensors, dtype=dtype, order=order)
        obj = tensors.view(cls)

        ''' check dimension(s) '''
        for d in obj.shape[1:]:
            if not d == obj.dim:
                raise ValueError("Dimensions are inconstistent.")
        if dim is not None:
            if dim != obj.dim:
                raise ValueError("Incorrect dimension: {obj.dim} given,"
                                 " {dim} demanded."
                                 .format(**locals()))

        ''' update kwargs with defaults from slots '''
        cls._updateSlotKwargs(kwargs)

        ''' set kwargs to slots attributes '''
        for attr in kwargs:
            if attr not in cls._iterSlots():
                raise AttributeError("Keywordargument {attr} not accepted "
                                     "for class {cls}".format(**locals()))
            setattr(obj, attr, kwargs[attr])

        return obj

    @classmethod
    def merged(cls, *objects, **kwargs):
        """
        Factory method
        Merges all tensor inputs to one tensor

        Examples:
            >>> import numpy as np
            >>> from tfields import Tensors
            >>> import tfields.bases
            >>> vecA = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
            >>> vecB = Tensors([[5, 4, 1]], coordSys=tfields.bases.cylinder)
            >>> vecC = Tensors([[5, 4, 1]], coordSys=tfields.bases.cylinder)
            >>> merge = Tensors.merged(vecA, vecB, vecC, [[2, 0, 1]])
            >>> assert merge.coordSys == 'cylinder'

        """

        ''' get most frequent coordSys or predefined coordSys '''
        coordSys = kwargs.get('coordSys', None)
        if coordSys is None:
            bases = []
            for t in objects:
                try:
                    bases.append(t.coordSys)
                except:
                    pass
            # get most frequent coordSys
            coordSys = sorted(bases, key=Counter(bases).get, reverse=True)[0]
            kwargs['coordSys'] = coordSys

        ''' transform all raw inputs to cls type with correct coordSys. Also
        automatically make a copy of those instances that are of the correct
        type already.'''
        objects = [cls(t, **kwargs) for t in objects]

        ''' check rank and dimension equality '''
        if not len(set([t.rank for t in objects])) == 1:
            raise TypeError("Tensors must have the same rank for merging.")
        if not len(set([t.dim for t in objects])) == 1:
            raise TypeError("Tensors must have the same dimension for merging.")

        ''' merge all objects '''
        remainingObjects = objects[1:] or []
        tensors = objects[0]

        for i, obj in enumerate(remainingObjects):
            tensors = np.append(tensors, obj, axis=0)
        return cls.__new__(cls, tensors, **kwargs)

    @property
    def rank(self):
        """
        Tensor rank
        """
        return rank(self)

    @property
    def dim(self):
        """
        Manifold dimension
        """
        return dim(self)

    def coordinateTransform(self, coordSys):
        """
        Args:
            coordSys (str)

        Examples:
            >>> import numpy as np
            >>> from tfields import Tensors

            CARTESIAN to SPHERICAL
            >>> t = Tensors([[1, 2, 2], [1, 0, 0], [0, 0, -1], [0, 0, 1], [0, 0, 0]])
            >>> t.coordinateTransform('spherical')

            r
            >>> assert t[0, 0] == 3

            phi
            >>> assert t[1, 1] == 0.
            >>> assert t[2, 1] == 0.

            theta is 0 at (0, 0, 1) and pi at (0, 0, -1)
            >>> assert round(t[1, 2], 10) == round(np.pi / 2, 10)
            >>> assert t[2, 2] == np.pi
            >>> assert t[3, 2] == 0.

            theta is defined 0 for R == 0
            >>> assert t[4, 0] == 0.
            >>> assert t[4, 2] == 0.


            CARTESIAN to CYLINDER
            >>> tCart = Tensors([[3, 4, 42], [1, 0, 0], [0, 1, -1], [-1, 0, 1], [0, 0, 0]])
            >>> tCyl = tCart.copy()
            >>> tCyl.coordinateTransform('cylinder')
            >>> assert tCyl.coordSys == 'cylinder'

            R
            >>> assert tCyl[0, 0] == 5
            >>> assert tCyl[1, 0] == 1
            >>> assert tCyl[2, 0] == 1
            >>> assert tCyl[4, 0] == 0

            Phi
            >>> assert round(tCyl[0, 1], 10) == round(np.arctan(4. / 3), 10)
            >>> assert tCyl[1, 1] == 0
            >>> assert round(tCyl[2, 1], 10) == round(np.pi / 2, 10)
            >>> assert tCyl[1, 1] == 0

            Z
            >>> assert tCyl[0, 2] == 42
            >>> assert tCyl[2, 2] == -1

            >>> tCyl.coordinateTransform('cartesian')
            >>> assert tCyl.coordSys == 'cartesian'
            >>> assert tCyl[0, 0] == 3

        """
        #           scalars                 empty             already there
        if self.rank == 0 or self.shape[0] == 0 or self.coordSys == coordSys:
            self.coordSys = coordSys
            return

        self[:] = tfields.bases.transform(self, self.coordSys, coordSys)
        self.coordSys = coordSys

    @contextmanager
409
    def tempCoordSys(self, coordSys):
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
410
411
412
413
414
415
416
        """
        Temporarily change the coordSys to another coordSys and change it back at exit
        This method is for cleaner code only.
        No speed improvements go with this.
        Args:
            see coordinateTransform
        Examples:
417
            >>> import tfields
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
418
419
420
421
422
423
424
            >>> p = tfields.Tensors([[1,2,3]], coordSys=tfields.bases.SPHERICAL)
            >>> with p.tempCoordSys(tfields.bases.CYLINDER):
            ...     assert p.coordSys == tfields.bases.CYLINDER
            >>> assert p.coordSys == tfields.bases.SPHERICAL

        """
        baseBefore = self.coordSys
425
        if baseBefore == coordSys:
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
            yield
        else:
            self.coordinateTransform(coordSys)

            yield

            self.coordinateTransform(baseBefore)


class TensorFields(Tensors):
    """
    Discrete Tensor Field

    Examples:
        >>> from tfields import Tensors, TensorFields
        >>> scalars = Tensors([0, 1, 2])
        >>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
        >>> scalarField = TensorFields(vectors, scalars)
        >>> scalarField.rank
        1
        >>> scalarField.fields[0].rank
        0
        >>> vectorField = TensorFields(vectors, vectors)
        >>> vectorField.fields[0].rank
        1
        >>> vectorField.fields[0].dim
        3
        >>> multiField = TensorFields(vectors, scalars, vectors)
        >>> multiField.fields[0].dim
        1
        >>> multiField.fields[1].dim
        3

    """
    __slots__ = ['coordSys', 'fields']

    def __new__(cls, tensors, *fields, **kwargs):
        obj = super(TensorFields, cls).__new__(cls, tensors, **kwargs)
        obj.fields = list(fields)
        return obj

    def coordinateTransform(self, coordSys):
        super(TensorFields, self).coordinateTransform(coordSys)
        for field in self.fields:
            field.coordinateTransform(coordSys)
471
        
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

class TensorMaps(TensorFields):
    """
    Args:
        tensors: see Tensors class
        *fields (Tensors): see TensorFields class
        **kwargs:
            coordSys ('str'): see Tensors class
            maps (array-like): indices indicating a connection between the
                tensors at the respective index positions
    Examples:
        >>> from tfields import Tensors, TensorMaps
        >>> scalars = Tensors([0, 1, 2])
        >>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
        >>> mesh = TensorMaps(vectors, scalars, maps=[[[0, 1, 2], [0, 1, 2]],
        ...                                           [[1, 1, 1], [2, 2, 2]]])
        >>> mesh.maps
        array([[[0, 1, 2],
                [0, 1, 2]],
        <BLANKLINE>
               [[1, 1, 1],
                [2, 2, 2]]])

    """
    __slots__ = ['coordSys', 'fields', 'maps']
    __slotDtypes__ = [None, None, int]


500
if __name__ == '__main__':  # pragma: no cover
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
501
502
503
    import doctest
    doctest.testmod()
    # doctest.run_docstring_examples(TensorMaps, globals())