Commit d4a7807c authored by Daniel Böckenhoff's avatar Daniel Böckenhoff
Browse files

all doctest problems including points3d resolved

parent bb27a224
from .array import igrid
from . import core
from . import bases
from . import array
from . import lib
from .lib import *
# __all__ = ['core', 'points3D']
from .core import Tensors, TensorFields, TensorMaps
......
......@@ -4,8 +4,9 @@ Run doctests on module call
def runDoctests():
import doctest
import pathlib
for f in list(pathlib.Path(__file__).parent.glob('**/*.py')):
doctest.testfile(f.name) # , verbose=True, optionflags=doctest.ELLIPSIS)
parent = pathlib.Path(__file__).parent
for f in list(parent.glob('**/*.py')):
doctest.testfile(str(f.relative_to(parent))) # , verbose=True, optionflags=doctest.ELLIPSIS)
if __name__ == '__main__':
......
......@@ -14,6 +14,7 @@ import sympy.diffgeom
from six import string_types
import warnings
from . import manifold_3
from .manifold_3 import CARTESIAN, CYLINDER, SPHERICAL
from .manifold_3 import cartesian, cylinder, spherical
......@@ -91,7 +92,6 @@ def transform(array, base_old, base_new):
Examples:
Cylindrical coordinates
>>> import tfields
>>> print "TEST"
>>> cart = np.array([[0, 0, 0],
... [1, 0, 0],
... [1, 1, 0],
......@@ -104,17 +104,14 @@ def transform(array, base_old, base_new):
... [0, 0, 1]])
>>> cyl = tfields.bases.transform(cart, 'cartesian', 'cylinder')
>>> cyl
>>> import npTools
>>> pnp = npTools.Points3D(cart)
>>> pnp.transform('cylinder')
>>> pnp
Transform cylinder to spherical. No connection is defined so routing via
cartesian
>>> import numpy as np
>>> import tfields
>>> b = np.array([[5, np.arctan(4. / 3), 0]])
>>> newB = tfields.bases.transform(b, 'cylinder', 'spherical')
>>> newB = b.copy()
>>> tfields.bases.transform(b, 'cylinder', 'spherical')
>>> assert newB[0, 0] == 5
>>> assert round(newB[0, 1], 10) == round(b[0, 1], 10)
......
......@@ -305,7 +305,7 @@ class Tensors(AbstractNdarray):
for t in objects:
try:
bases.append(t.coordSys)
except:
except AttributeError:
pass
# get most frequent coordSys
coordSys = sorted(bases, key=Counter(bases).get, reverse=True)[0]
......@@ -351,7 +351,7 @@ class Tensors(AbstractNdarray):
Examples:
Initilaize using the mgrid notation
>>> npTools.Tensors.grid((0, 1, 2j), (3, 4, 2j), (6, 7, 2j))
>>> tfields.Tensors.grid((0, 1, 2j), (3, 4, 2j), (6, 7, 2j))
Tensors([[ 0., 3., 6.],
[ 0., 3., 7.],
[ 0., 4., 6.],
......@@ -361,7 +361,7 @@ class Tensors(AbstractNdarray):
[ 1., 4., 6.],
[ 1., 4., 7.]])
>>> npTools.Tensors.grid(np.linspace(3, 4, 2), np.linspace(0, 1, 2),
>>> tfields.Tensors.grid(np.linspace(3, 4, 2), np.linspace(0, 1, 2),
... np.linspace(6, 7, 2), iter_order=[1, 0, 2])
Tensors([[ 3., 0., 6.],
[ 3., 0., 7.],
......@@ -371,7 +371,7 @@ class Tensors(AbstractNdarray):
[ 3., 1., 7.],
[ 4., 1., 6.],
[ 4., 1., 7.]])
>>> npTools.Tensors.grid(np.linspace(0, 1, 2), np.linspace(3, 4, 2),
>>> tfields.Tensors.grid(np.linspace(0, 1, 2), np.linspace(3, 4, 2),
... np.linspace(6, 7, 2), iter_order=[2, 0, 1])
Tensors([[ 0., 3., 6.],
[ 0., 4., 6.],
......@@ -383,7 +383,7 @@ class Tensors(AbstractNdarray):
[ 1., 4., 7.]])
"""
inst = cls.__new__(cls, tfields.array.igrid(*base_vectors, **kwargs))
inst = cls.__new__(cls, tfields.lib.grid.igrid(*base_vectors, **kwargs))
return inst
@property
......@@ -590,6 +590,10 @@ class Tensors(AbstractNdarray):
if return_bool:
return np.array_equal(self, other)
return self == other
elif rtol is None:
rtol = 0.
elif atol is None:
atol = 0.
mask = np.isclose(self, other, rtol=rtol, atol=atol, equal_nan=equal_nan)
if return_bool:
return bool(np.all(mask))
......@@ -704,7 +708,7 @@ class Tensors(AbstractNdarray):
"""
coords = sympy.symbols('x y z')
with self.tmp_transform(coordSys):
with self.tmp_transform(coordSys or self.coordSys):
mask = tfields.getMask(self, cutExpression, coords=coords)
return mask
......@@ -736,7 +740,7 @@ class Tensors(AbstractNdarray):
"""
if len(self) == 0:
return self.copy()
mask = self.getMask(cutExpression, coordSys=coordSys)
mask = self.getMask(cutExpression, coordSys=coordSys or self.coordSys)
mask.astype(bool)
inst = self[mask].copy()
return inst
......@@ -765,9 +769,9 @@ class Tensors(AbstractNdarray):
def distances(self, other, metric='euclidean', **kwargs):
"""
Examples:
>>> p = tfields.Tensors.grid((0, 2, 3),
... (0, 2, 3),
... (0, 0, 1))
>>> p = tfields.Tensors.grid((0, 2, 3j),
... (0, 2, 3j),
... (0, 0, 1j))
>>> p[4,2] = 1
>>> p.distances(p)[0,0]
0.0
......@@ -825,7 +829,7 @@ class Tensors(AbstractNdarray):
>>> p = tfields.Tensors.grid((0, 1, 2j),
... (0, 1, 2j),
... (0, 1, 2j))
>>> p = tfields.Tensors([p, [0.5, 0.5, 0.5]])
>>> p = tfields.Tensors.merged(p, [[0.5, 0.5, 0.5]])
>>> [len(en) for en in p.epsilonNeighbourhood(0.9)]
[2, 2, 2, 2, 2, 2, 2, 2, 9]
......
......@@ -8,8 +8,6 @@ Collection of additional numpy functions
part of tfields library
"""
import numpy as np
from . import grid
from .grid import igrid
def convert_nan(array, value=0.):
......@@ -105,3 +103,6 @@ def index(ar, entry, rtol=0, atol=0, equal_nan=False, axis=None):
if __name__ == '__main__':
import doctest
doctest.testmod()
else:
from . import grid
from .grid import igrid
......@@ -22,39 +22,50 @@ def igrid(*base_vectors, **kwargs):
Initilaize using the mgrid notation
>>> import tfields
>>> tfields.igrid((0, 1, 2j), (3, 4, 2j), (6, 7, 2j))
Tensors([[ 0., 3., 6.],
[ 0., 3., 7.],
[ 0., 4., 6.],
[ 0., 4., 7.],
[ 1., 3., 6.],
[ 1., 3., 7.],
[ 1., 4., 6.],
[ 1., 4., 7.]])
>>> tfields.Tensors.grid(np.linspace(3, 4, 2), np.linspace(0, 1, 2),
... np.linspace(6, 7, 2), iter_order=[1, 0, 2])
Tensors([[ 3., 0., 6.],
[ 3., 0., 7.],
[ 4., 0., 6.],
[ 4., 0., 7.],
[ 3., 1., 6.],
[ 3., 1., 7.],
[ 4., 1., 6.],
[ 4., 1., 7.]])
>>> tfields.Tensors.grid(np.linspace(0, 1, 2), np.linspace(3, 4, 2),
... np.linspace(6, 7, 2), iter_order=[2, 0, 1])
Tensors([[ 0., 3., 6.],
[ 0., 4., 6.],
[ 1., 3., 6.],
[ 1., 4., 6.],
[ 0., 3., 7.],
[ 0., 4., 7.],
[ 1., 3., 7.],
[ 1., 4., 7.]])
array([[ 0., 3., 6.],
[ 0., 3., 7.],
[ 0., 4., 6.],
[ 0., 4., 7.],
[ 1., 3., 6.],
[ 1., 3., 7.],
[ 1., 4., 6.],
[ 1., 4., 7.]])
>>> import numpy as np
>>> tfields.igrid([3, 4], np.linspace(0, 1, 2),
... (6, 7, 2), iter_order=[1, 0, 2])
array([[ 3., 0., 6.],
[ 3., 0., 7.],
[ 4., 0., 6.],
[ 4., 0., 7.],
[ 3., 1., 6.],
[ 3., 1., 7.],
[ 4., 1., 6.],
[ 4., 1., 7.]])
>>> tfields.igrid(np.linspace(0, 1, 2), np.linspace(3, 4, 2),
... np.linspace(6, 7, 2), iter_order=[2, 0, 1])
array([[ 0., 3., 6.],
[ 0., 4., 6.],
[ 1., 3., 6.],
[ 1., 4., 6.],
[ 0., 3., 7.],
[ 0., 4., 7.],
[ 1., 3., 7.],
[ 1., 4., 7.]])
"""
iter_order = kwargs.pop('iter_order', np.arange(len(base_vectors)))
# ensure, that the third entry in base_vector of type tuple becomes a complex type
base_vectors = list(base_vectors)
for i, vector in enumerate(base_vectors):
if isinstance(vector, tuple):
if len(vector) == 3:
vector = list(vector)
vector[2] = complex(vector[2])
base_vectors[i] = tuple(vector)
# create the grid
if all([isinstance(val, tuple) for val in base_vectors]):
base_vectors = [slice(*base_vectors[index]) for index in iter_order]
obj = np.mgrid[base_vectors]
......@@ -96,10 +107,11 @@ def swap_columns(array, *index_tuples):
expects tuples with indices to swap as arguments
Examples:
>>> import tfields
>>> l = np.array([[3,3,3], [2,2,2], [1,1,1], [0, 0, 0]])
>>> tfields.array.grid.swap_columns(l, (1, 2), (0, 3))
>>> l = np.array([[3, 2, 1, 0], [6, 5, 4, 3]])
>>> tfields.lib.grid.swap_columns(l, (1, 2), (0, 3))
>>> l
array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
array([[0, 1, 2, 3],
[3, 4, 5, 6]])
"""
# test swap_indices type
......@@ -121,7 +133,7 @@ def swap_rows(array, *args):
Examples:
>>> import tfields
>>> l = [[3,3,3], [2,2,2], [1,1,1], [0, 0, 0]]
>>> tfields.array.swap_rows(l, (1, 2), (0, 3))
>>> tfields.lib.grid.swap_rows(l, (1, 2), (0, 3))
>>> l
[[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]
......@@ -137,12 +149,10 @@ def compare_permutations(permut1, permut2):
>>> import tfields
>>> a = [1, 2, 0, 4, 3]
>>> b = [0, 1, 2, 3, 4]
>>> si = tfields.array.grid.compare_permutations(a, b)
>>> si = tfields.lib.grid.compare_permutations(a, b)
>>> si
[(0, 2), (1, 2), (3, 4)]
>>> a = np.array(a)
>>> tfields.array.grid.swap_columns(a, *si)
>>> a = list(a)
>>> tfields.lib.grid.swap_rows(a, *si)
>>> a
[0, 1, 2, 3, 4]
>>> a == b
......
......@@ -127,28 +127,19 @@ class Points3D(tfields.Tensors):
[ 0., 1., -1.]])
Coordinate system is implemented. Default is cartesian
>>> p.transform(tfields.bases.CYLINDER); p
Points3D([[ 2.23606798, 1.10714872, 3. ],
[ 6.40312424, 0.89605538, 6. ],
[ 2.23606798, 1.10714872, -6. ],
[ 7.07106781, -2.35619449, -5. ],
[ 1. , 0. , -1. ],
[ 1. , 1.57079633, -1. ]])
>>> p.transform(tfields.bases.CARTESIAN); p
Points3D([[ 1.000000e+00, 2.000000e+00, 3.000000e+00],
[ 4.000000e+00, 5.000000e+00, 6.000000e+00],
[ 1.000000e+00, 2.000000e+00, -6.000000e+00],
[-5.000000e+00, -5.000000e+00, -5.000000e+00],
[ 1.000000e+00, 0.000000e+00, -1.000000e+00],
[ 6.123234e-17, 1.000000e+00, -1.000000e+00]])
Appending to the array with numpy.append
>>> a = tfields.Points3D([[1,2,3]])
>>> b = tfields.Points3D([[4,5,6]])
>>> c = np.append(a, b, 0)
>>> c
array([[1., 2., 3.],
[4., 5., 6.]])
>>> p_cart = p.copy()
>>> p.transform(tfields.bases.CYLINDER)
>>> assert p.equal(
... tfields.Points3D([[2.236, 1.107, 3.],
... [6.403, 0.896, 6.],
... [2.236, 1.107, -6.],
... [7.071, -2.356, -5.],
... [1. , 0. , -1.],
... [1. , 1.571, -1.]],
... coordSys=tfields.bases.CYLINDER),
... atol=1e-3)
>>> p.transform(tfields.bases.CARTESIAN)
>>> assert p.equal(p_cart, atol=1e-15)
"""
def __new__(cls, tensors, **kwargs):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment