Skip to content
Snippets Groups Projects
Commit 5f279c64 authored by Martin Bauer's avatar Martin Bauer
Browse files

pytest and coverage improvements

- notebooks are considered in coverage analysis now
   -> custom notebook runner plugin adapted
- added images for documentation
parent 6e7e2fc5
Branches
Tags
No related merge requests found
......@@ -326,7 +326,7 @@ class SerialDataHandling(DataHandling):
cellData[name] = tuple(field)
else:
for i in range(fSize):
cellData["%s[%d]" % (name, i)] = np.ascontiguousarray(field[...,i])
cellData["%s[%d]" % (name, i)] = np.ascontiguousarray(field[..., i])
else:
assert False
imageToVTK(fullFileName, cellData=cellData)
......
......@@ -121,6 +121,11 @@ display_animation_func = None
def disp(*args, **kwargs):
from IPython import get_ipython
ipython = get_ipython()
if not ipython:
return
if not display_animation_func:
raise Exception("Call set_display_mode first")
return display_animation_func(*args, **kwargs)
......
......@@ -3,7 +3,6 @@ import matplotlib.animation as animation
from IPython.display import HTML
from tempfile import NamedTemporaryFile
import base64
from IPython import get_ipython
import sympy as sp
......@@ -150,6 +149,11 @@ display_animation_func = None
def disp(*args, **kwargs):
from IPython import get_ipython
ipython = get_ipython()
if not ipython:
return
if not display_animation_func:
raise Exception("Call set_display_mode first")
return display_animation_func(*args, **kwargs)
......@@ -174,10 +178,14 @@ def setDisplayMode(mode):
raise Exception("Unknown mode. Available modes 'imageupdate', 'video' and 'window' ")
ipython = get_ipython()
if ipython:
setDisplayMode('imageupdate')
ipython.magic("config InlineBackend.rc = { }")
ipython.magic("matplotlib inline")
plt.rc('figure', figsize=(16, 6))
sp.init_printing()
def activateIPython():
from IPython import get_ipython
ipython = get_ipython()
if ipython:
setDisplayMode('imageupdate')
ipython.magic("config InlineBackend.rc = { }")
ipython.magic("matplotlib inline")
plt.rc('figure', figsize=(16, 6))
sp.init_printing()
activateIPython()
......@@ -2,42 +2,56 @@ from pyevtk.vtk import VtkFile, VtkImageData
from pyevtk.hl import _addDataToFile, _appendDataToFile
def imageToVTK(path, origin=(0.0, 0.0, 0.0), spacing=(1.0, 1.0, 1.0), cellData=None, pointData=None):
"""Patched version of same pyevtk function that also support vector data"""
assert cellData != None or pointData != None
def imageToVTK(path, cellData, origin=(0.0, 0.0, 0.0), spacing=(1.0, 1.0, 1.0)):
"""
Writes numpy data to VTK
Numpy arrays have to be contiguous in memory - if this is a problem call :func:`numpy.ascontiguousarray` first
Patched version of same pyevtk function that also supports vector-valued data
:param path: path with file name, without file ending (.vtk) where data should be stored
:param cellData: dictionary, mapping name of the data to a 3D numpy array, or to a 3-tuple of 3D numpy arrays
in case of vector-valued data
:param origin: 3-tuple describing the origin of the field in 3D
:param spacing: 3-tuple describing the grid spacing in x,y, z direction
:returns path to file that was written
Example:
>>> from tempfile import TemporaryDirectory
>>> import os
>>> import numpy as np
>>> with TemporaryDirectory() as tmp_dir:
... path = os.path.join(tmp_dir, 'out')
... size = (20, 20, 20)
... resFile = imageToVTK(path, cellData = {'someScalarField': np.zeros(size),
... 'someVectorField': (np.zeros(size), np.ones(size), np.zeros(size))
... })
"""
# Extract dimensions
start = (0, 0, 0)
end = None
if cellData:
keys = list(cellData.keys())
data = cellData[keys[0]]
if hasattr(data, 'shape'):
end = data.shape
elif isinstance(data, tuple):
shapes = set(d.shape for d in data)
if len(shapes) > 1:
raise ValueError("All components have to have the same shape")
end = shapes.pop()
elif pointData:
keys = list(pointData.keys())
data = pointData[keys[0]]
if hasattr(data, 'shape'):
end = data.shape
end = (end[0] - 1, end[1] - 1, end[2] - 1)
# Added for vector support...
elif data[0].ndim == 3 and data[1].ndim == 3 and data[0].ndim == 3:
keys = list(pointData.keys())
data = pointData[keys[0]]
end = data[0].shape
end = (end[0] - 1, end[1] - 1, end[2] - 1)
keys = list(cellData.keys())
data = cellData[keys[0]]
if hasattr(data, 'shape'):
end = data.shape
elif isinstance(data, tuple):
shapes = set(d.shape for d in data)
if len(shapes) > 1:
raise ValueError("All components have to have the same shape")
end = shapes.pop()
# Write data to file
w = VtkFile(path, VtkImageData)
w.openGrid(start=start, end=end, origin=origin, spacing=spacing)
w.openPiece(start=start, end=end)
_addDataToFile(w, cellData, pointData)
_addDataToFile(w, cellData, pointData=None)
w.closePiece()
w.closeGrid()
_appendDataToFile(w, cellData, pointData)
_appendDataToFile(w, cellData, pointData=None)
w.save()
return w.getFileName()
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