Commit de2644d1 authored by Daniel Böckenhoff (Laptop)'s avatar Daniel Böckenhoff (Laptop)
Browse files

tfields.core doctest works

parent ed9ad5f7
......@@ -292,16 +292,15 @@ class Tensors(AbstractNdarray):
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 = tfields.Tensors([0, 1, 2])
>>> scalars.rank == 0
True
Initialize vectors
>>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> vectors.rank == 1
True
>>> vectors.dim == 3
......@@ -309,7 +308,7 @@ class Tensors(AbstractNdarray):
>>> assert vectors.coordSys == 'cartesian'
Initialize the Levi-Zivita Tensor
>>> matrices = Tensors([[[0, 0, 0], [0, 0, 1], [0, -1, 0]],
>>> matrices = tfields.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)
......@@ -320,7 +319,7 @@ class Tensors(AbstractNdarray):
True
Initializing in different start coordinate system
>>> cyl = Tensors([[5, np.arctan(4. / 3.), 42]], coordSys='cylinder')
>>> cyl = tfields.Tensors([[5, np.arctan(4. / 3.), 42]], coordSys='cylinder')
>>> assert cyl.coordSys == 'cylinder'
>>> cyl.transform('cartesian')
>>> assert cyl.coordSys == 'cartesian'
......@@ -331,24 +330,24 @@ class Tensors(AbstractNdarray):
Initialize with copy constructor keeps the coordinate system
>>> with vectors.tmp_transform('cylinder'):
... vect_cyl = Tensors(vectors)
... vect_cyl = tfields.Tensors(vectors)
... assert vect_cyl.coordSys == vectors.coordSys
>>> assert vect_cyl.coordSys == 'cylinder'
You can demand a special dimension.
>>> _ = Tensors([[1, 2, 3]], dim=3)
>>> _ = Tensors([[1, 2, 3]], dim=2) # doctest: +ELLIPSIS
>>> _ = tfields.Tensors([[1, 2, 3]], dim=3)
>>> _ = tfields.Tensors([[1, 2, 3]], dim=2) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Incorrect dimension: 3 given, 2 demanded.
The dimension argument (dim) becomes necessary if you want to initialize
an empty array
>>> _ = Tensors([]) # doctest: +ELLIPSIS
>>> _ = tfields.Tensors([]) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Empty tensors need dimension parameter 'dim'.
>>> Tensors([], dim=7)
>>> tfields.Tensors([], dim=7)
Tensors([], shape=(0, 7), dtype=float64)
"""
......@@ -421,12 +420,12 @@ class Tensors(AbstractNdarray):
Examples:
>>> import numpy as np
>>> from tfields import Tensors
>>> import tfields
>>> 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]])
>>> vecA = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> vecB = tfields.Tensors([[5, 4, 1]], coordSys=tfields.bases.cylinder)
>>> vecC = tfields.Tensors([[5, 4, 1]], coordSys=tfields.bases.cylinder)
>>> merge = tfields.Tensors.merged(vecA, vecB, vecC, [[2, 0, 1]])
>>> assert merge.coordSys == 'cylinder'
"""
......@@ -484,36 +483,41 @@ class Tensors(AbstractNdarray):
Examples:
Initilaize using the mgrid notation
>>> tfields.Tensors.grid((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.]])
>>> import tfields
>>> mgrid = tfields.Tensors.grid((0, 1, 2j), (3, 4, 2j), (6, 7, 2j))
>>> mgrid.equal([[0, 3, 6],
... [0, 3, 7],
... [0, 4, 6],
... [0, 4, 7],
... [1, 3, 6],
... [1, 3, 7],
... [1, 4, 6],
... [1, 4, 7]])
True
>>> lins = tfields.Tensors.grid(np.linspace(3, 4, 2), np.linspace(0, 1, 2),
... np.linspace(6, 7, 2), iter_order=[1, 0, 2])
>>> lins.equal([[3, 0, 6],
... [3, 0, 7],
... [4, 0, 6],
... [4, 0, 7],
... [3, 1, 6],
... [3, 1, 7],
... [4, 1, 6],
... [4, 1, 7]])
True
>>> lins2 = tfields.Tensors.grid(np.linspace(0, 1, 2),
... np.linspace(3, 4, 2),
... np.linspace(6, 7, 2),
... iter_order=[2, 0, 1])
>>> lins2.equal([[0, 3, 6],
... [0, 4, 6],
... [1, 3, 6],
... [1, 4, 6],
... [0, 3, 7],
... [0, 4, 7],
... [1, 3, 7],
... [1, 4, 7]])
True
"""
inst = cls.__new__(cls, tfields.lib.grid.igrid(*base_vectors, **kwargs))
......@@ -540,10 +544,10 @@ class Tensors(AbstractNdarray):
Examples:
>>> import numpy as np
>>> from tfields import Tensors
>>> import tfields
CARTESIAN to SPHERICAL
>>> t = Tensors([[1, 2, 2], [1, 0, 0], [0, 0, -1], [0, 0, 1], [0, 0, 0]])
>>> t = tfields.Tensors([[1, 2, 2], [1, 0, 0], [0, 0, -1], [0, 0, 1], [0, 0, 0]])
>>> t.transform('spherical')
r
......@@ -564,7 +568,7 @@ class Tensors(AbstractNdarray):
CARTESIAN to CYLINDER
>>> tCart = Tensors([[3, 4, 42], [1, 0, 0], [0, 1, -1], [-1, 0, 1], [0, 0, 0]])
>>> tCart = tfields.Tensors([[3, 4, 42], [1, 0, 0], [0, 1, -1], [-1, 0, 1], [0, 0, 0]])
>>> tCyl = tCart.copy()
>>> tCyl.transform('cylinder')
>>> assert tCyl.coordSys == 'cylinder'
......@@ -631,30 +635,23 @@ class Tensors(AbstractNdarray):
Args:
coordinate (int): coordinate index
Examples:
>>> import tfields
>>> p = tfields.Tensors([[1., 2., 3.], [4., 5., 6.], [1, 2, -6]])
>>> p.mirror(1)
>>> p
Tensors([[ 1., -2., 3.],
[ 4., -5., 6.],
[ 1., -2., -6.]])
>>> assert p.equal([[1, -2, 3], [4, -5, 6], [1, -2, -6]])
multiple coordinates can be mirrored. Eg. a point mirrorion would be
>>> p = tfields.Tensors([[1., 2., 3.], [4., 5., 6.], [1, 2, -6]])
>>> p.mirror([0,2])
>>> p
Tensors([[-1., 2., -3.],
[-4., 5., -6.],
[-1., 2., 6.]])
>>> assert p.equal([[-1, 2, -3], [-4, 5, -6], [-1, 2., 6.]])
You can give a condition as mask or as str.
The mirroring will only be applied to the points meeting the condition.
>>> import sympy
>>> x, y, z = sympy.symbols('x y z')
>>> p.mirror([0,2], y > 3)
>>> p
Tensors([[-1., 2., -3.],
[ 4., 5., 6.],
[-1., 2., 6.]])
>>> p.equal([[-1, 2, -3], [4, 5, 6], [-1, 2, 6]])
True
"""
if condition is None:
......@@ -737,6 +734,7 @@ class Tensors(AbstractNdarray):
Inspired by a speed argument @
stackoverflow.com/questions/14766194/testing-whether-a-numpy-array-contains-a-given-row
Examples:
>>> import tfields
>>> p = tfields.Tensors([[1,2,3], [4,5,6], [6,7,8]])
>>> p.contains([4,5,6])
True
......@@ -790,6 +788,7 @@ class Tensors(AbstractNdarray):
Returns:
array shape(len(self)): Indices of other points that are closest to own points
Examples:
>>> import tfields
>>> m = tfields.Tensors([[1,0,0], [0,1,0], [1,1,0], [0,0,1],
... [1,0,1]])
>>> p = tfields.Tensors([[1.1,1,0], [0,0.1,1], [1,0,1.1]])
......@@ -814,19 +813,25 @@ class Tensors(AbstractNdarray):
Returns: np.array of dtype bool with lenght of number of points in self.
This array is True, where cutExpression evaluates True.
Examples:
>>> import tfields
>>> import numpy
>>> import sympy
>>> x, y, z = sympy.symbols('x y z')
>>> p = tfields.Points3D([[1., 2., 3.], [4., 5., 6.], [1, 2, -6],
... [-5, -5, -5], [1,0,-1], [0,1,-1]])
>>> p.getMask(x > 0)
array([ True, True, True, False, True, False], dtype=bool)
>>> np.array_equal(p.getMask(x > 0),
... [True, True, True, False, True, False])
True
And combination
>>> p.getMask((x > 0) & (y < 3))
array([ True, False, True, False, True, False], dtype=bool)
>>> np.array_equal(p.getMask((x > 0) & (y < 3)),
... [True, False, True, False, True, False])
True
Or combination
>>> p.getMask((x > 0) | (y > 3))
array([ True, True, True, False, True, False], dtype=bool)
>>> np.array_equal(p.getMask((x > 0) | (y > 3)),
... [True, True, True, False, True, False])
True
"""
coords = sympy.symbols('x y z')
......@@ -842,19 +847,20 @@ class Tensors(AbstractNdarray):
use symbols x, y and z
coordSys (str): coordSys to evaluate the expression in.
Examples:
>>> import tfields
>>> import sympy
>>> x, y, z = sympy.symbols('x y z')
>>> p = tfields.Points3D([[1., 2., 3.], [4., 5., 6.], [1, 2, -6],
... [-5, -5, -5], [1,0,-1], [0,1,-1]])
>>> p.cut(x > 0)
Points3D([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 1., 2., -6.],
[ 1., 0., -1.]])
>>> p = tfields.Tensors([[1., 2., 3.], [4., 5., 6.], [1, 2, -6],
... [-5, -5, -5], [1,0,-1], [0,1,-1]])
>>> p.cut(x > 0).equal([[1, 2, 3],
... [4, 5, 6],
... [1, 2, -6],
... [1, 0, -1]])
True
combinations of cuts
>>> p.cut((x > 0) & (z < 0))
Points3D([[ 1., 2., -6.],
[ 1., 0., -1.]])
>>> p.cut((x > 0) & (z < 0)).equal([[1, 2, -6], [1, 0, -1]])
True
Returns:
copy of self with cut applied
......@@ -874,6 +880,7 @@ class Tensors(AbstractNdarray):
**kwargs:
... is forwarded to sp.spatial.distance.cdist
Examples:
>>> import tfields
>>> p = tfields.Tensors.grid((0, 2, 3j),
... (0, 2, 3j),
... (0, 0, 1j))
......@@ -882,8 +889,8 @@ class Tensors(AbstractNdarray):
0.0
>>> p.distances(p)[5,1]
1.4142135623730951
>>> p.distances([[0,1,2]])[-1]
array([ 3.])
>>> p.distances([[0,1,2]])[-1][0] == 3
True
"""
if issubclass(type(other), Tensors) and self.coordSys != other.coordSys:
......@@ -902,14 +909,17 @@ class Tensors(AbstractNdarray):
Examples:
>>> import tfields
>>> import numpy as np
>>> p = tfields.Tensors.grid((0, 2, 3),
... (0, 2, 3),
... (0, 0, 1))
>>> p[4,2] = 1
>>> dMin = p.minDistances()
>>> dMin
array([ 1. , 1. , 1. , 1. , 1.41421356,
1. , 1. , 1. , 1. ])
>>> expected = [1] * 9
>>> expected[4] = np.sqrt(2)
>>> np.array_equal(dMin, expected)
True
>>> dMin2 = p.minDistances(memory_saving=True)
>>> bool((dMin2 == dMin).all())
......@@ -942,6 +952,7 @@ class Tensors(AbstractNdarray):
Examples:
Create mesh grid with one extra point that will have 8 neighbours
within epsilon
>>> import tfields
>>> p = tfields.Tensors.grid((0, 1, 2j),
... (0, 1, 2j),
... (0, 1, 2j))
......@@ -1019,15 +1030,15 @@ class TensorFields(Tensors):
"""
In addition to the usual, also slice fields
Examples:
>>> from tfields import Tensors, TensorFields
>>> import tfields
>>> import numpy as np
>>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> scalarField = TensorFields(vectors, [42, 21, 10.5], [1, 2, 3])
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> scalarField = tfields.TensorFields(vectors, [42, 21, 10.5], [1, 2, 3])
Slicing
>>> sliced = scalarField[2:]
>>> assert isinstance(sliced, TensorFields)
>>> assert isinstance(sliced.fields[0], Tensors)
>>> assert isinstance(sliced, tfields.TensorFields)
>>> assert isinstance(sliced.fields[0], tfields.Tensors)
>>> assert sliced.fields[0].equal([10.5])
Picking
......@@ -1057,12 +1068,12 @@ class TensorFields(Tensors):
"""
In addition to the usual, also slice fields
Examples:
>>> from tfields import Tensors, TensorFields
>>> import tfields
>>> import numpy as np
>>> original = TensorFields([[0, 0, 0], [0, 0, 1], [0, -1, 0]],
... [42, 21, 10.5], [1, 2, 3])
>>> obj = TensorFields([[0, 0, 0], [0, 0, np.nan], [0, -1, 0]],
... [42, 22, 10.5], [1, -1, 3])
>>> original = tfields.TensorFields([[0, 0, 0], [0, 0, 1], [0, -1, 0]],
... [42, 21, 10.5], [1, 2, 3])
>>> obj = tfields.TensorFields([[0, 0, 0], [0, 0, np.nan], [0, -1, 0]],
... [42, 22, 10.5], [1, -1, 3])
>>> slice_obj = obj.copy()
>>> assert not obj.equal(original)
>>> obj[1] = original[1]
......@@ -1122,17 +1133,18 @@ class TensorMaps(TensorFields):
maps (array-like): indices indicating a connection between the
tensors at the respective index positions
Examples:
>>> from tfields import Tensors, TensorFields, TensorMaps
>>> scalars = Tensors([0, 1, 2])
>>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> maps = [TensorFields([[0, 1, 2], [0, 1, 2]], [42, 21]),
... TensorFields([[1], [2]], [-42, -21])]
>>> mesh = TensorMaps(vectors, scalars,
... maps=maps)
>>> import tfields
>>> import numpy as np
>>> scalars = tfields.Tensors([0, 1, 2])
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
>>> maps = [tfields.TensorFields([[0, 1, 2], [0, 1, 2]], [42, 21]),
... tfields.TensorFields([[1], [2]], [-42, -21])]
>>> mesh = tfields.TensorMaps(vectors, scalars,
... maps=maps)
>>> assert isinstance(mesh.maps, list)
>>> assert len(mesh.maps) == 2
>>> assert mesh.equal(TensorFields(vectors, scalars))
>>> assert mesh.equal(tfields.TensorFields(vectors, scalars))
>>> assert mesh.maps[0].fields[0].equal(maps[0].fields[0])
"""
......@@ -1187,10 +1199,10 @@ class TensorMaps(TensorFields):
Returns:
Mask for all vertices that are stale i.e. are not refered by maps
Examples:
>>> from tfields import Tensors, TensorFields, TensorMaps
>>> vectors = Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0], [4, 4, 4]])
>>> tm = TensorMaps(vectors, maps=[[[0, 1, 2], [0, 1, 2]],
... [[1, 1], [2, 2]]])
>>> import tfields
>>> vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0], [4, 4, 4]])
>>> tm = tfields.TensorMaps(vectors, maps=[[[0, 1, 2], [0, 1, 2]],
... [[1, 1], [2, 2]]])
>>> assert np.array_equal(tm.stale(), [False, False, False, True])
"""
......@@ -1259,24 +1271,25 @@ class TensorMaps(TensorFields):
Copy because self is immutable
Examples:
>>> import tfields
>>> m = tfields.TensorMaps([[0,0,0], [1,1,1], [2,2,2], [0,0,0],
... [3,3,3], [4,4,4], [5,5,5]],
... maps=[TensorFields([[0, 1, 2], [0, 1, 3],
... [3, 4, 5], [3, 4, 1],
... [3, 4, 6]],
... [1, 3, 5, 7, 9], [2, 4, 6, 8, 0])])
... maps=[tfields.TensorFields([[0, 1, 2], [0, 1, 3],
... [3, 4, 5], [3, 4, 1],
... [3, 4, 6]],
... [1, 3, 5, 7, 9],
... [2, 4, 6, 8, 0])])
>>> c = m.removed([True, True, True, False, False, False, False])
>>> c
TensorMaps([[ 0., 0., 0.],
[ 3., 3., 3.],
[ 4., 4., 4.],
[ 5., 5., 5.]])
>>> c.equal([[0, 0, 0],
... [3, 3, 3],
... [4, 4, 4],
... [5, 5, 5]])
True
>>> c.maps[0]
TensorFields([[0, 1, 2],
[0, 1, 3]])
>>> c.maps[0].fields
[TensorFields([[ 5., 6.],
[ 9., 0.]])]
>>> assert c.maps[0].fields[0].equal([5, 9])
>>> assert c.maps[0].fields[1].equal([6, 0])
"""
remove_condition = np.array(remove_condition)
......@@ -1299,13 +1312,13 @@ class TensorMaps(TensorFields):
"""
Examples:
>>> from tfields import TensorMaps
>>> import numpy as np
>>> m = TensorMaps([[1,2,3], [3,3,3], [0,0,0], [5,6,7]],
... maps=[[0, 1, 2], [1, 2, 3]])
... maps=[[[0, 1, 2], [1, 2, 3]]])
>>> from sympy.abc import x,y,z
>>> vertexMask = m.getMask(z < 6)
>>> faceMask = m.to_maps_masks(vertexMask)
>>> faceMask
array([ True, False], dtype=bool)
>>> assert np.array_equal(faceMask, [[True, False]])
Returns:
mask of faces with all vertices in mask
......@@ -1318,8 +1331,8 @@ class TensorMaps(TensorFields):
masks = []
for map_index in range(len(self.maps)):
map_delete_mask = np.full((len(self.maps[map_index]),), False, dtype=bool)
for i, mp in enumerate(self.maps[0]):
for index in set(mp):
for i, mp in enumerate(self.maps[map_index]):
for index in mp:
if index in delete_indices:
map_delete_mask[i] = True
break
......@@ -1330,7 +1343,7 @@ class TensorMaps(TensorFields):
if __name__ == '__main__': # pragma: no cover
import doctest
doctest.testmod()
doctest.run_docstring_examples(TensorMaps.cleaned, globals())
doctest.run_docstring_examples(TensorFields, globals())
doctest.run_docstring_examples(AbstractNdarray.copy, globals())
doctest.run_docstring_examples(TensorMaps.equal, globals())
# doctest.run_docstring_examples(TensorMaps.cleaned, globals())
# doctest.run_docstring_examples(TensorFields, globals())
# doctest.run_docstring_examples(AbstractNdarray.copy, globals())
# doctest.run_docstring_examples(TensorMaps.equal, globals())
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