diff --git a/datahandling/serial_datahandling.py b/datahandling/serial_datahandling.py index 888b82ffd0aa32e3c0f152297759fd137ea4e175..758b074f8d7f8aaf5df887e6f1c1b972cd9f8b66 100644 --- a/datahandling/serial_datahandling.py +++ b/datahandling/serial_datahandling.py @@ -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) diff --git a/display_utils.py b/display_utils.py index ef49d810752a369e7131b7f72f350b41ca6e705e..1c4248c76dbc5c15a6a7d7d978c09acc5f786d0f 100644 --- a/display_utils.py +++ b/display_utils.py @@ -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) diff --git a/jupytersetup.py b/jupytersetup.py index 1e46d707c03d505675fd11cee90a69136c5e82de..d11f81f631b251d0acb995bf006fed411af0e594 100644 --- a/jupytersetup.py +++ b/jupytersetup.py @@ -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() diff --git a/vtk.py b/vtk.py index 2b5408180bb1ea9f6bfc62edea57d9b70610a348..caf9ca1aa411e065856315f196f51af8d7d2801c 100644 --- a/vtk.py +++ b/vtk.py @@ -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()