Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Daniel Boeckenhoff
tfields
Commits
8521b7e0
Commit
8521b7e0
authored
Jul 27, 2020
by
dboe
Browse files
removed all relative imports in favour of absolute
parent
78b25499
Pipeline
#79531
failed with stages
in 1 minute and 58 seconds
Changes
11
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
8521b7e0
...
...
@@ -32,6 +32,8 @@ clean:
rm
-rf
.tox
rm
-rf
.pytest_cache
rm
-rf
*
.egg-info
rm
-rf
*
/__pycache__
rm
-rf
*
/
**
/__pycache__
pre-commit clean
publish
:
...
...
tests/test_mesh3D.py
View file @
8521b7e0
...
...
@@ -5,7 +5,7 @@ import unittest
import
sympy
# NOQA: F401
import
os
import
sys
from
.test_core
import
TensorMaps_Check
from
tests
.test_core
import
TensorMaps_Check
THIS_DIR
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
os
.
path
.
join
(
os
.
getcwd
(),
os
.
path
.
expanduser
(
__file__
))))
sys
.
path
.
append
(
os
.
path
.
normpath
(
os
.
path
.
join
(
THIS_DIR
)))
...
...
tfields/__init__.py
View file @
8521b7e0
...
...
@@ -4,19 +4,14 @@ __author__ = """Daniel Böckenhoff"""
__email__
=
"dboe@ipp.mpg.de"
__version__
=
"0.3.2"
from
.
import
core
# NOQA
from
.
import
bases
# NOQA
from
.
import
lib
# NOQA
# methods:
from
.core
import
dim
,
rank
# NOQA
from
.mask
import
evalf
# NOQA
from
.lib
import
*
# NOQA
from
tfields.core
import
dim
,
rank
# NOQA
from
tfields.mask
import
evalf
# NOQA
# classes:
from
.core
import
Tensors
,
TensorFields
,
TensorMaps
,
Container
,
Maps
# NOQA
from
.points3D
import
Points3D
# NOQA
from
.mesh3D
import
Mesh3D
# NOQA
from
.triangles3D
import
Triangles3D
# NOQA
from
.planes3D
import
Planes3D
# NOQA
from
.bounding_box
import
Node
# NOQA
from
tfields
.core
import
Tensors
,
TensorFields
,
TensorMaps
,
Container
,
Maps
# NOQA
from
tfields
.points3D
import
Points3D
# NOQA
from
tfields
.mesh3D
import
Mesh3D
# NOQA
from
tfields
.triangles3D
import
Triangles3D
# NOQA
from
tfields
.planes3D
import
Planes3D
# NOQA
from
tfields
.bounding_box
import
Node
# NOQA
tfields/bases/__init__.py
View file @
8521b7e0
...
...
@@ -7,7 +7,7 @@ Mail: daniel.boeckenhoff@ipp.mpg.de
part of tfields library
Tools for sympy coordinate transformation
"""
from
.bases
import
*
# NOQA
from
.
import
manifold_3
# NOQA
from
.manifold_3
import
CARTESIAN
,
CYLINDER
,
SPHERICAL
# NOQA
from
.manifold_3
import
cartesian
,
cylinder
,
spherical
# NOQA
from
tfields.bases
.bases
import
get_coord_system
,
get_coord_system_name
,
lambdified_trafo
,
transform
# NOQA
from
tfields.bases
import
manifold_3
# NOQA
from
tfields.bases
.manifold_3
import
CARTESIAN
,
CYLINDER
,
SPHERICAL
# NOQA
from
tfields.bases
.manifold_3
import
cartesian
,
cylinder
,
spherical
# NOQA
tfields/bases/bases.py
View file @
8521b7e0
...
...
@@ -51,7 +51,7 @@ def get_coord_system_name(base):
return
str
(
base
)
def
lambdified
T
rafo
(
base_old
,
base_new
):
def
lambdified
_t
rafo
(
base_old
,
base_new
):
"""
Args:
base_old (sympy.CoordSystem)
...
...
@@ -64,13 +64,13 @@ def lambdifiedTrafo(base_old, base_new):
Transform cartestian to cylinder or spherical
>>> a = np.array([[3,4,0]])
>>> trafo = tfields.bases.lambdified
T
rafo(tfields.bases.cartesian,
... tfields.bases.cylinder)
>>> trafo = tfields.bases.lambdified
_t
rafo(tfields.bases.cartesian,
...
tfields.bases.cylinder)
>>> new = np.concatenate([trafo(*coords).T for coords in a])
>>> assert new[0, 0] == 5
>>> trafo = tfields.bases.lambdified
T
rafo(tfields.bases.cartesian,
... tfields.bases.spherical)
>>> trafo = tfields.bases.lambdified
_t
rafo(tfields.bases.cartesian,
...
tfields.bases.spherical)
>>> new = np.concatenate([trafo(*coords).T for coords in a])
>>> assert new[0, 0] == 5
...
...
@@ -128,17 +128,17 @@ def transform(array, base_old, base_new):
raise
ValueError
(
"Trafo not found."
)
# very fast trafos in numpy only
short
T
rafo
=
None
short
_t
rafo
=
None
try
:
short
T
rafo
=
getattr
(
base_old
,
'to_{base_new.name}'
.
format
(
**
locals
()))
short
_t
rafo
=
getattr
(
base_old
,
'to_{base_new.name}'
.
format
(
**
locals
()))
except
AttributeError
:
pass
if
short
T
rafo
:
short
T
rafo
(
array
)
if
short
_t
rafo
:
short
_t
rafo
(
array
)
return
# trafo via lambdified sympy expressions
trafo
=
tfields
.
bases
.
lambdified
T
rafo
(
base_old
,
base_new
)
trafo
=
tfields
.
bases
.
lambdified
_t
rafo
(
base_old
,
base_new
)
with
warnings
.
catch_warnings
():
warnings
.
filterwarnings
(
'ignore'
,
message
=
"invalid value encountered in double_scalars"
)
array
[:]
=
np
.
concatenate
([
trafo
(
*
coords
).
T
for
coords
in
array
])
...
...
tfields/bases/manifold_3.py
View file @
8521b7e0
import
tfields
import
sympy
import
sympy.diffgeom
import
numpy
as
np
import
warnings
...
...
@@ -59,7 +60,7 @@ def cartesian_to_cylinder(array):
array
[:,
1
]
=
np
.
sign
(
y_vals
)
*
np
.
arccos
(
x_vals
/
array
[:,
0
])
np
.
seterr
(
divide
=
'warn'
,
invalid
=
'warn'
)
array
[:,
1
][
problem_phi_indices
]
=
np
.
pi
tfields
.
convert_nan
(
array
,
0.
)
# for cases like cartesian 0, 0, 1
tfields
.
lib
.
util
.
convert_nan
(
array
,
0.
)
# for cases like cartesian 0, 0, 1
cartesian
.
to_cylinder
=
cartesian_to_cylinder
...
...
@@ -139,7 +140,7 @@ def cartesian_to_spherical(array):
array
[:,
1
]
=
np
.
sign
(
array
[:,
1
])
*
np
.
arccos
(
array
[:,
0
]
/
np
.
sqrt
(
xy
))
array
[:,
1
][
problemPhiIndices
]
=
np
.
pi
tfields
.
convert_nan
(
array
,
0.
)
# for cases like cartesian 0, 0, 1
tfields
.
lib
.
util
.
convert_nan
(
array
,
0.
)
# for cases like cartesian 0, 0, 1
# array[:,1] = np.arctan2(array[:,1], array[:,0]) # phi for phi between 0, 2pi
# r
...
...
tfields/core.py
View file @
8521b7e0
...
...
@@ -2516,7 +2516,7 @@ class TensorMaps(TensorFields):
inst
=
self
if
duplicates
:
inst
=
self
.
copy
()
duplicates
=
tfields
.
duplicates
(
self
,
axis
=
0
)
duplicates
=
tfields
.
lib
.
util
.
duplicates
(
self
,
axis
=
0
)
for
tensor_index
,
duplicate_index
in
zip
(
range
(
self
.
shape
[
0
]),
duplicates
):
...
...
@@ -2527,7 +2527,8 @@ class TensorMaps(TensorFields):
for
f
in
range
(
len
(
self
.
maps
[
map_dim
])):
# face index
mp
=
np
.
array
(
self
.
maps
[
map_dim
],
dtype
=
int
)
if
tensor_index
in
mp
[
f
]:
index
=
tfields
.
index
(
mp
[
f
],
tensor_index
)
index
=
tfields
.
lib
.
util
.
index
(
mp
[
f
],
tensor_index
)
inst
.
maps
[
map_dim
][
f
][
index
]
=
duplicate_index
return
inst
.
removed
(
stale_mask
)
...
...
tfields/lib/__init__.py
View file @
8521b7e0
...
...
@@ -7,112 +7,8 @@ Mail: daniel.boeckenhoff@ipp.mpg.de
Collection of additional numpy functions
part of tfields library
"""
import
numpy
as
np
def
convert_nan
(
array
,
value
=
0.
):
"""
Replace all occuring NaN values by value
"""
nanIndices
=
np
.
isnan
(
array
)
array
[
nanIndices
]
=
value
def
view1D
(
ar
):
"""
https://stackoverflow.com/a/44999009/ @Divakar
"""
ar
=
np
.
ascontiguousarray
(
ar
)
voidDt
=
np
.
dtype
((
np
.
void
,
ar
.
dtype
.
itemsize
*
ar
.
shape
[
1
]))
return
ar
.
view
(
voidDt
).
ravel
()
def
argsort_unique
(
idx
):
"""
https://stackoverflow.com/a/43411559/ @Divakar
"""
n
=
idx
.
size
sidx
=
np
.
empty
(
n
,
dtype
=
int
)
sidx
[
idx
]
=
np
.
arange
(
n
)
return
sidx
def
duplicates
(
ar
,
axis
=
None
):
"""
View1D version of duplicate search
Speed up version after
https://stackoverflow.com/questions/46284660
\
/python-numpy-speed-up-2d-duplicate-search/46294916#46294916
Args:
ar (array_like): array
other args: see np.isclose
Examples:
>>> import tfields
>>> import numpy as np
>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> tfields.duplicates(a, axis=0)
array([0, 0, 2])
An empty sequence will not throw errors
>>> assert np.array_equal(tfields.duplicates([], axis=0), [])
Returns:
list of int: int is pointing to first occurence of unique value
"""
if
len
(
ar
)
==
0
:
return
np
.
array
([])
if
axis
!=
0
:
raise
NotImplementedError
()
sidx
=
np
.
lexsort
(
ar
.
T
)
b
=
ar
[
sidx
]
groupIndex0
=
np
.
flatnonzero
((
b
[
1
:]
!=
b
[:
-
1
]).
any
(
1
))
+
1
groupIndex
=
np
.
concatenate
(([
0
],
groupIndex0
,
[
b
.
shape
[
0
]]))
ids
=
np
.
repeat
(
range
(
len
(
groupIndex
)
-
1
),
np
.
diff
(
groupIndex
))
sidx_mapped
=
argsort_unique
(
sidx
)
ids_mapped
=
ids
[
sidx_mapped
]
grp_minidx
=
sidx
[
groupIndex
[:
-
1
]]
out
=
grp_minidx
[
ids_mapped
]
return
out
def
index
(
ar
,
entry
,
rtol
=
0
,
atol
=
0
,
equal_nan
=
False
,
axis
=
None
):
"""
Examples:
>>> import tfields
>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> tfields.index(a, [2, 3, 4], axis=0)
2
>>> a = np.array([[1, 0, 0], [2, 3, 4]])
>>> tfields.index(a, 4)
5
Returns:
list of int: indices of point occuring
"""
if
axis
is
None
:
ar
=
ar
.
flatten
()
elif
axis
!=
0
:
raise
NotImplementedError
()
for
i
,
part
in
enumerate
(
ar
):
isclose
=
np
.
isclose
(
part
,
entry
,
rtol
=
rtol
,
atol
=
atol
,
equal_nan
=
equal_nan
)
if
axis
is
not
None
:
isclose
=
isclose
.
all
()
if
isclose
:
return
i
if
__name__
==
'__main__'
:
import
doctest
doctest
.
testmod
()
else
:
from
.
import
grid
# NOQA
from
.grid
import
igrid
# NOQA
from
.
import
stats
# NOQA
from
.stats
import
mode
,
median
,
mean
# NOQA
from
.
import
symbolics
# NOQA
from
.
import
sets
# NOQA
from
.
import
util
# NOQA
from
tfields.lib
import
grid
# NOQA
from
tfields.lib
import
stats
# NOQA
from
tfields.lib
import
symbolics
# NOQA
from
tfields.lib
import
sets
# NOQA
from
tfields.lib
import
util
# NOQA
tfields/lib/grid.py
View file @
8521b7e0
...
...
@@ -54,9 +54,9 @@ def igrid(*base_vectors, **kwargs):
Initilaize using the mgrid notation
>>> import tfields
>>> import numpy as np
>>> assert np.array_equal(tfields.igrid((0, 1, 2j),
... (3, 4, 2j),
... (6, 7, 2j)),
>>> assert np.array_equal(tfields.
lib.grid.
igrid((0, 1, 2j),
...
(3, 4, 2j),
...
(6, 7, 2j)),
... [[ 0., 3., 6.],
... [ 0., 3., 7.],
... [ 0., 4., 6.],
...
...
@@ -66,8 +66,9 @@ def igrid(*base_vectors, **kwargs):
... [ 1., 4., 6.],
... [ 1., 4., 7.]])
>>> assert np.array_equal(tfields.igrid([3, 4], np.linspace(0, 1, 2),
... (6, 7, 2),
>>> assert np.array_equal(tfields.lib.grid.igrid([3, 4],
... np.linspace(0, 1, 2),
... (6, 7, 2),
... iter_order=[1, 0, 2]),
... [[ 3., 0., 6.],
... [ 3., 0., 7.],
...
...
@@ -77,10 +78,10 @@ def igrid(*base_vectors, **kwargs):
... [ 3., 1., 7.],
... [ 4., 1., 6.],
... [ 4., 1., 7.]])
>>> assert np.array_equal(tfields.igrid(np.linspace(0, 1, 2),
... np.linspace(3, 4, 2),
... np.linspace(6, 7, 2),
... iter_order=[2, 0, 1]),
>>> assert np.array_equal(tfields.
lib.grid.
igrid(np.linspace(0, 1, 2),
...
np.linspace(3, 4, 2),
...
np.linspace(6, 7, 2),
...
iter_order=[2, 0, 1]),
... [[ 0., 3., 6.],
... [ 0., 4., 6.],
... [ 1., 3., 6.],
...
...
@@ -131,12 +132,13 @@ def base_vectors(array, rtol=None, atol=None):
Examples:
>>> import tfields
>>> grid = tfields.igrid((3, 5, 5j))
>>> grid = tfields.
lib.grid.
igrid((3, 5, 5j))
>>> tfields.lib.grid.base_vectors(grid[:, 0])
(3.0, 5.0, 5j)
>>> grid2 = tfields.igrid((3, 5, 5j),
... (1, 2, 2j))
>>> grid_circle = tfields.igrid(*tfields.lib.grid.base_vectors(grid2))
>>> grid2 = tfields.lib.grid.igrid((3, 5, 5j),
... (1, 2, 2j))
>>> grid_circle = tfields.lib.grid.igrid(
... *tfields.lib.grid.base_vectors(grid2))
>>> assert tfields.Tensors(grid_circle).equal(grid2)
"""
...
...
tfields/lib/util.py
View file @
8521b7e0
...
...
@@ -3,6 +3,7 @@ Various utility functions
"""
import
itertools
from
six
import
string_types
import
numpy
as
np
def
pairwise
(
iterable
):
...
...
@@ -113,6 +114,102 @@ def multi_sort(array, *others, **kwargs):
return
tuple
(
cast_type
(
x
)
for
x
in
zip
(
*
method
(
zip
(
array
,
*
others
),
**
kwargs
)))
def
convert_nan
(
ar
,
value
=
0.
):
"""
Replace all occuring NaN values by value
"""
nanIndices
=
np
.
isnan
(
ar
)
ar
[
nanIndices
]
=
value
def
view1D
(
ar
):
"""
Delete duplicate columns of the input array
https://stackoverflow.com/a/44999009/ @Divakar
"""
ar
=
np
.
ascontiguousarray
(
ar
)
voidDt
=
np
.
dtype
((
np
.
void
,
ar
.
dtype
.
itemsize
*
ar
.
shape
[
1
]))
return
ar
.
view
(
voidDt
).
ravel
()
def
argsort_unique
(
idx
):
"""
https://stackoverflow.com/a/43411559/ @Divakar
"""
n
=
idx
.
size
sidx
=
np
.
empty
(
n
,
dtype
=
int
)
sidx
[
idx
]
=
np
.
arange
(
n
)
return
sidx
def
duplicates
(
ar
,
axis
=
None
):
"""
View1D version of duplicate search
Speed up version after
https://stackoverflow.com/questions/46284660
\
/python-numpy-speed-up-2d-duplicate-search/46294916#46294916
Args:
ar (array_like): array
other args: see np.isclose
Examples:
>>> import tfields
>>> import numpy as np
>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> tfields.lib.util.duplicates(a, axis=0)
array([0, 0, 2])
An empty sequence will not throw errors
>>> assert np.array_equal(tfields.lib.util.duplicates([], axis=0), [])
Returns:
list of int: int is pointing to first occurence of unique value
"""
if
len
(
ar
)
==
0
:
return
np
.
array
([])
if
axis
!=
0
:
raise
NotImplementedError
()
sidx
=
np
.
lexsort
(
ar
.
T
)
b
=
ar
[
sidx
]
groupIndex0
=
np
.
flatnonzero
((
b
[
1
:]
!=
b
[:
-
1
]).
any
(
1
))
+
1
groupIndex
=
np
.
concatenate
(([
0
],
groupIndex0
,
[
b
.
shape
[
0
]]))
ids
=
np
.
repeat
(
range
(
len
(
groupIndex
)
-
1
),
np
.
diff
(
groupIndex
))
sidx_mapped
=
argsort_unique
(
sidx
)
ids_mapped
=
ids
[
sidx_mapped
]
grp_minidx
=
sidx
[
groupIndex
[:
-
1
]]
out
=
grp_minidx
[
ids_mapped
]
return
out
def
index
(
ar
,
entry
,
rtol
=
0
,
atol
=
0
,
equal_nan
=
False
,
axis
=
None
):
"""
Examples:
>>> import tfields
>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> tfields.lib.util.index(a, [2, 3, 4], axis=0)
2
>>> a = np.array([[1, 0, 0], [2, 3, 4]])
>>> tfields.lib.util.index(a, 4)
5
Returns:
list of int: indices of point occuring
"""
if
axis
is
None
:
ar
=
ar
.
flatten
()
elif
axis
!=
0
:
raise
NotImplementedError
()
for
i
,
part
in
enumerate
(
ar
):
isclose
=
np
.
isclose
(
part
,
entry
,
rtol
=
rtol
,
atol
=
atol
,
equal_nan
=
equal_nan
)
if
axis
is
not
None
:
isclose
=
isclose
.
all
()
if
isclose
:
return
i
if
__name__
==
'__main__'
:
import
doctest
doctest
.
testmod
()
tfields/mesh3D.py
View file @
8521b7e0
...
...
@@ -380,8 +380,8 @@ class Mesh3D(tfields.TensorMaps):
"""
vertices
=
tfields
.
Tensors
.
grid
(
*
base_vectors
,
**
kwargs
)
base_vectors
=
tfields
.
grid
.
ensure_complex
(
*
base_vectors
)
base_vectors
=
tfields
.
grid
.
to_base_vectors
(
*
base_vectors
)
base_vectors
=
tfields
.
lib
.
grid
.
ensure_complex
(
*
base_vectors
)
base_vectors
=
tfields
.
lib
.
grid
.
to_base_vectors
(
*
base_vectors
)
fix_coord
=
None
for
coord
in
range
(
3
):
if
len
(
base_vectors
[
coord
])
>
1
:
...
...
@@ -446,8 +446,8 @@ class Mesh3D(tfields.TensorMaps):
if
not
len
(
base_vectors
)
==
3
:
raise
AttributeError
(
"3 base_vectors vectors required"
)
base_vectors
=
tfields
.
grid
.
ensure_complex
(
*
base_vectors
)
base_vectors
=
tfields
.
grid
.
to_base_vectors
(
*
base_vectors
)
base_vectors
=
tfields
.
lib
.
grid
.
ensure_complex
(
*
base_vectors
)
base_vectors
=
tfields
.
lib
.
grid
.
to_base_vectors
(
*
base_vectors
)
indices
=
[
0
,
-
1
]
coords
=
range
(
3
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment