Skip to content
Snippets Groups Projects
fielddata.py 58.5 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
# -*- coding: utf-8 -*-
# Copyright 2016 by Forschungszentrum Juelich GmbH
# Author: J. Caron
#
"""This module provides classes for storing vector and scalar 3D-field."""

import logging

import os

import tempfile

import abc
from numbers import Number

import numpy as np

from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib import patheffects

from PIL import Image

from scipy.ndimage.interpolation import zoom

from . import colors
from . import plottools

__all__ = ['VectorData', 'ScalarData']


class FieldData(object, metaclass=abc.ABCMeta):
    """Class for storing field data.

    Abstract base class for the representatio of magnetic or electric fields (see subclasses).
    Fields can be accessed as 3D numpy arrays via the `field` property or as a vector via
    `field_vec`. :class:`~.FieldData` objects support negation, arithmetic operators
    (``+``, ``-``, ``*``) and their augmented counterparts (``+=``, ``-=``, ``*=``), with numbers
    and other :class:`~.FieldData` objects of the same subclass, if their dimensions and grid
    spacings match. It is possible to load data from HDF5 or LLG (.txt) files or to save the data
    in these formats. Specialised plotting methods are also provided.

    Attributes
    ----------
    a: float
        The grid spacing in nm.
    field: :class:`~numpy.ndarray` (N=4)
        The field distribution for every 3D-gridpoint.

    """

    _log = logging.getLogger(__name__ + '.FieldData')

    @property
    def a(self):
        """The grid spacing in nm."""
        return self._a

    @a.setter
    def a(self, a):
        assert isinstance(a, Number), 'Grid spacing has to be a number!'
        assert a >= 0, 'Grid spacing has to be a positive number!'
        self._a = float(a)

    @property
    def shape(self):
        """The shape of the `field` (3D for scalar, 4D vor vector field)."""
        return self.field.shape

    @property
    def dim(self):
        """Dimensions (z, y, x) of the grid, only 3D coordinates, without components if present."""
        return self.shape[-3:]

    @property
    def field(self):
        """The field strength for every 3D-gridpoint (scalar: 3D, vector: 4D)."""
        return self._field

    @field.setter
    def field(self, field):
        assert isinstance(field, np.ndarray), 'Field has to be a numpy array!'
        assert 3 <= len(field.shape) <= 4, 'Field has to be 3- or 4-dimensional (scalar / vector)!'
        if len(field.shape) == 4:
            assert field.shape[0] == 3, 'A vector field has to have exactly 3 components!'
        self._field = field

    @property
    def field_amp(self):
        """The field amplitude (returns the field itself for scalar and the vector amplitude
        calculated via a square sum for a vector field."""
        if len(self.shape) == 4:
            return np.sqrt(np.sum(self.field ** 2, axis=0))
        else:
            return self.field

    @property
    def field_vec(self):
        """Vector containing the vector field distribution."""
        return np.reshape(self.field, -1)

    @field_vec.setter
    def field_vec(self, mag_vec):
        assert np.size(mag_vec) == np.prod(self.shape), \
            'Vector has to match field shape! {} {}'.format(mag_vec.shape, np.prod(self.shape))
        self.field = mag_vec.reshape((3,) + self.dim)

    def __init__(self, a, field):
        self._log.debug('Calling __init__')
        self.a = a
        self.field = field
        self._log.debug('Created ' + str(self))

    def __repr__(self):
        self._log.debug('Calling __repr__')
        return '%s(a=%r, field=%r)' % (self.__class__, self.a, self.field)

    def __str__(self):
        self._log.debug('Calling __str__')
        return '%s(a=%s, dim=%s)' % (self.__class__, self.a, self.dim)

    def __neg__(self):  # -self
        self._log.debug('Calling __neg__')
        return self.__class__(self.a, -self.field)

    def __add__(self, other):  # self + other
        self._log.debug('Calling __add__')
        assert isinstance(other, (FieldData, Number)), \
            'Only FieldData objects and scalar numbers (as offsets) can be added/subtracted!'
        if isinstance(other, Number):  # other is a Number
            self._log.debug('Adding an offset')
            return self.__class__(self.a, self.field + other)
        elif isinstance(other, FieldData):
            self._log.debug('Adding two FieldData objects')
            assert other.a == self.a, 'Added phase has to have the same grid spacing!'
            assert other.shape == self.shape, 'Added field has to have the same dimensions!'
            return self.__class__(self.a, self.field + other.field)

    def __sub__(self, other):  # self - other
        self._log.debug('Calling __sub__')
        return self.__add__(-other)

    def __mul__(self, other):  # self * other
        self._log.debug('Calling __mul__')
        assert isinstance(other, Number), 'FieldData objects can only be multiplied by numbers!'
        return self.__class__(self.a, self.field * other)

    def __truediv__(self, other):  # self / other
        self._log.debug('Calling __truediv__')
        assert isinstance(other, Number), 'FieldData objects can only be divided by numbers!'
        return self.__class__(self.a, self.field / other)

    def __floordiv__(self, other):  # self // other
        self._log.debug('Calling __floordiv__')
        assert isinstance(other, Number), 'FieldData objects can only be divided by numbers!'
        return self.__class__(self.a, self.field // other)

    def __radd__(self, other):  # other + self
        self._log.debug('Calling __radd__')
        return self.__add__(other)

    def __rsub__(self, other):  # other - self
        self._log.debug('Calling __rsub__')
        return -self.__sub__(other)

    def __rmul__(self, other):  # other * self
        self._log.debug('Calling __rmul__')
        return self.__mul__(other)

    def __iadd__(self, other):  # self += other
        self._log.debug('Calling __iadd__')
        return self.__add__(other)

    def __isub__(self, other):  # self -= other
        self._log.debug('Calling __isub__')
        return self.__sub__(other)

    def __imul__(self, other):  # self *= other
        self._log.debug('Calling __imul__')
        return self.__mul__(other)

    def __itruediv__(self, other):  # self /= other
        self._log.debug('Calling __itruediv__')
        return self.__truediv__(other)

    def __ifloordiv__(self, other):  # self //= other
        self._log.debug('Calling __ifloordiv__')
        return self.__floordiv__(other)

    def __getitem__(self, item):
        return self.__class__(self.a, self.field[item])

    def __array__(self, dtype=None):  # Used for numpy ufuncs, together with __array_wrap__!
        if dtype:
            return self.field.astype(dtype)
        else:
            return self.field

    def __array_wrap__(self, array, _=None):  # _ catches the context, which is not used.
        return type(self)(self.a, array)

    def copy(self):
        """Returns a copy of the :class:`~.FieldData` object

        Returns
        -------
        field_data: :class:`~.FieldData`
            A copy of the :class:`~.FieldData`.

        """
        self._log.debug('Calling copy')
        return self.__class__(self.a, self.field.copy())

    def get_mask(self, threshold=0):
        """Mask all pixels where the amplitude of the field lies above `threshold`.

        Parameters
        ----------
        threshold : float, optional
            A pixel only gets masked, if it lies above this threshold . The default is 0.

        Returns
        -------
        mask : :class:`~numpy.ndarray` (N=3, boolean)
            Mask of the pixels where the amplitude of the field lies above `threshold`.

        """
        self._log.debug('Calling get_mask')
        return np.where(self.field_amp > threshold, True, False)

    def plot_mask(self, title='Mask', threshold=0, **kwargs):
        """Plot the mask as a 3D-contour plot.

        Parameters
        ----------
        title: string, optional
            The title for the plot.
        threshold : float, optional
            A pixel only gets masked, if it lies above this threshold . The default is 0.

        Returns
        -------
        plot : :class:`mayavi.modules.vectors.Vectors`
            The plot object.

        """
        self._log.debug('Calling plot_mask')
        from mayavi import mlab
        mlab.figure(size=(750, 700))
        zzz, yyy, xxx = (np.indices(self.dim) + self.a / 2)
        zzz, yyy, xxx = zzz.T, yyy.T, xxx.T
        mask = self.get_mask(threshold=threshold).astype(int).T  # Transpose because of VTK order!
        extent = np.ravel(list(zip((0, 0, 0), mask.shape)))
        cont = mlab.contour3d(xxx, yyy, zzz, mask, contours=[1], **kwargs)
        mlab.outline(cont, extent=extent)
        mlab.axes(cont, extent=extent)
        mlab.title(title, height=0.95, size=0.35)
        mlab.orientation_axes()
        cont.scene.isometric_view()
        return cont

    def plot_contour3d(self, title='Field Distribution', contours=10, opacity=0.25, **kwargs):
        """Plot the field as a 3D-contour plot.

        Parameters
        ----------
        title: string, optional
            The title for the plot.
        contours: int, optional
            Number of contours which should be plotted.
        opacity: float, optional
            Defines the opacity of the contours. Default is 0.25.

        Returns
        -------
        plot : :class:`mayavi.modules.vectors.Vectors`
            The plot object.

        """
        self._log.debug('Calling plot_contour3d')
        from mayavi import mlab
        mlab.figure(size=(750, 700))
        zzz, yyy, xxx = (np.indices(self.dim) + self.a / 2)
        zzz, yyy, xxx = zzz.T, yyy.T, xxx.T
        field_amp = self.field_amp.T  # Transpose because of VTK order!
        if not isinstance(contours, (list, tuple, np.ndarray)):  # Calculate the contours:
            contours = list(np.linspace(field_amp.min(), field_amp.max(), contours))
        extent = np.ravel(list(zip((0, 0, 0), field_amp.shape)))
        cont = mlab.contour3d(xxx, yyy, zzz, field_amp, contours=contours,
                              opacity=opacity, **kwargs)
        mlab.outline(cont, extent=extent)
        mlab.axes(cont, extent=extent)
        mlab.title(title, height=0.95, size=0.35)
        mlab.orientation_axes()
        cont.scene.isometric_view()
        return cont

    @abc.abstractmethod
    def scale_down(self, n):
        """Scale down the field distribution by averaging over two pixels along each axis.

        Parameters
        ----------
        n : int, optional
            Number of times the field distribution is scaled down. The default is 1.

        Returns
        -------
        None

        Notes
        -----
        Acts in place and changes dimensions and grid spacing accordingly.
        Only possible, if each axis length is a power of 2!

        """
        pass

    @abc.abstractmethod
    def scale_up(self, n, order):
        """Scale up the field distribution using spline interpolation of the requested order.

        Parameters
        ----------
        n : int, optional
            Power of 2 with which the grid is scaled. Default is 1, which means every axis is
            increased by a factor of ``2**1 = 2``.
        order : int, optional
            The order of the spline interpolation, which has to be in the range between 0 and 5
            and defaults to 0.

        Returns
        -------
        None

        Notes
        -----
        Acts in place and changes dimensions and grid spacing accordingly.
        """
        pass

    @abc.abstractmethod
    def get_vector(self, mask):
        """Returns the field as a vector, specified by a mask.

        Parameters
        ----------
        mask : :class:`~numpy.ndarray` (N=3, boolean)
            Masks the pixels from which the entries should be taken.

        Returns
        -------
        vector : :class:`~numpy.ndarray` (N=1)
            The vector containing the field of the specified pixels.

        """
        pass

    @abc.abstractmethod
    def set_vector(self, vector, mask):
        """Set the field of the masked pixels to the values specified by `vector`.

        Parameters
        ----------
        mask : :class:`~numpy.ndarray` (N=3, boolean), optional
            Masks the pixels from which the field should be taken.
        vector : :class:`~numpy.ndarray` (N=1)
            The vector containing the field of the specified pixels.

        Returns
        -------
        None

        """
        pass

    @classmethod
    def from_signal(cls, signal):
        """Convert a :class:`~hyperspy.signals.Signal` object to a :class:`~.FieldData` object.

        Parameters
        ----------
        signal: :class:`~hyperspy.signals.Signal`
            The :class:`~hyperspy.signals.Signal` object which should be converted to FieldData.

        Returns
        -------
        magdata: :class:`~.FieldData`
            A :class:`~.FieldData` object containing the loaded data.

        Notes
        -----
        This method recquires the hyperspy package!

        """
        cls._log.debug('Calling from_signal')
        return cls(signal.axes_manager[0].scale, signal.data)

    @abc.abstractmethod
    def to_signal(self):
        """Convert :class:`~.FieldData` data into a HyperSpy signal.

        Returns
        -------
        signal: :class:`~hyperspy.signals.Signal`
            Representation of the :class:`~.FieldData` object as a HyperSpy Signal.

        Notes
        -----
        This method recquires the hyperspy package!

        """
        self._log.debug('Calling to_signal')
        try:  # Try importing HyperSpy:
            # noinspection PyUnresolvedReferences
            import hyperspy.api as hs
        except ImportError:
            self._log.error('This method recquires the hyperspy package!')
            return
        # Create signal:
        signal = hs.signals.BaseSignal(self.field)  # All axes are signal axes!
        # Set axes:
        signal.axes_manager[0].name = 'x-axis'
        signal.axes_manager[0].units = 'nm'
        signal.axes_manager[0].scale = self.a
        signal.axes_manager[1].name = 'y-axis'
        signal.axes_manager[1].units = 'nm'
        signal.axes_manager[1].scale = self.a
        signal.axes_manager[2].name = 'z-axis'
        signal.axes_manager[2].units = 'nm'
        signal.axes_manager[2].scale = self.a
        return signal


class VectorData(FieldData):

    """Class for storing vector ield data.

    Represents 3-dimensional vector field distributions with 3 components which are stored as a
    3-dimensional numpy array in `field`, but which can also be accessed as a vector via
    `field_vec`. :class:`~.VectorData` objects support negation, arithmetic operators
    (``+``, ``-``, ``*``) and their augmented counterparts (``+=``, ``-=``, ``*=``), withnumbers
    and other :class:`~.VectorData` objects, if their dimensions and grid spacings match. It is
    possible to load data from HDF5 or LLG (.txt) files or to save the data in these formats.
    Plotting methods are also provided.

    Attributes
    ----------
    a: float
        The grid spacing in nm.
    field: :class:`~numpy.ndarray` (N=4)
        The `x`-, `y`- and `z`-component of the vector field for every 3D-gridpoint
        as a 4-dimensional numpy array (first dimension has to be 3, because of the 3 components).

    """
    _log = logging.getLogger(__name__ + '.VectorData')

    def scale_down(self, n=1):
        """Scale down the field distribution by averaging over two pixels along each axis.

        Parameters
        ----------
        n : int, optional
            Number of times the field distribution is scaled down. The default is 1.

        Returns
        -------
        None

        Notes
        -----
        Acts in place and changes dimensions and grid spacing accordingly.
        Only possible, if each axis length is a power of 2!

        """
        self._log.debug('Calling scale_down')
        assert n > 0 and isinstance(n, int), 'n must be a positive integer!'
        self.a *= 2 ** n
        for t in range(n):
            # Pad if necessary:
            pz, py, px = self.dim[0] % 2, self.dim[1] % 2, self.dim[2] % 2
            if pz != 0 or py != 0 or px != 0:
                self.field = np.pad(self.field, ((0, 0), (0, pz), (0, py), (0, px)),
                                    mode='constant')
            # Create coarser grid for the vector field:
            shape_4d = (3, self.dim[0] // 2, 2, self.dim[1] // 2, 2, self.dim[2] // 2, 2)
            self.field = self.field.reshape(shape_4d).mean(axis=(6, 4, 2))

    def scale_up(self, n=1, order=0):
        """Scale up the field distribution using spline interpolation of the requested order.

        Parameters
        ----------
        n : int, optional
            Power of 2 with which the grid is scaled. Default is 1, which means every axis is
            increased by a factor of ``2**1 = 2``.
        order : int, optional
            The order of the spline interpolation, which has to be in the range between 0 and 5
            and defaults to 0.

        Returns
        -------
        None

        Notes
        -----
        Acts in place and changes dimensions and grid spacing accordingly.
        """
        self._log.debug('Calling scale_up')
        assert n > 0 and isinstance(n, int), 'n must be a positive integer!'
        assert 5 > order >= 0 and isinstance(order, int), \
            'order must be a positive integer between 0 and 5!'
        self.a /= 2 ** n
        self.field = np.array((zoom(self.field[0], zoom=2 ** n, order=order),
                               zoom(self.field[1], zoom=2 ** n, order=order),
                               zoom(self.field[2], zoom=2 ** n, order=order)))

    def pad(self, pad_values):
        """Pad the current field distribution with zeros for each individual axis.

        Parameters
        ----------
        pad_values : tuple of int
            Number of zeros which should be padded. Provided as a tuple where each entry
            corresponds to an axis. An entry can be one int (same padding for both sides) or again
            a tuple which specifies the pad values for both sides of the corresponding axis.

        Returns
        -------
        None

        Notes
        -----
        Acts in place and changes dimensions accordingly.
        """
        self._log.debug('Calling pad')
        assert len(pad_values) == 3, 'Pad values for each dimension have to be provided!'
        pv = np.zeros(6, dtype=np.int)
        for i, values in enumerate(pad_values):
            assert np.shape(values) in [(), (2,)], 'Only one or two values per axis can be given!'
            pv[2 * i:2 * (i + 1)] = values
        self.field = np.pad(self.field, ((0, 0), (pv[0], pv[1]), (pv[2], pv[3]), (pv[4], pv[5])),
                            mode='constant')

    def crop(self, crop_values):
        """Crop the current field distribution with zeros for each individual axis.

        Parameters
        ----------
        crop_values : tuple of int
            Number of zeros which should be cropped. Provided as a tuple where each entry
            corresponds to an axis. An entry can be one int (same cropping for both sides) or again
            a tuple which specifies the crop values for both sides of the corresponding axis.

        Returns
        -------
        None

        Notes
        -----
        Acts in place and changes dimensions accordingly.
        """
        self._log.debug('Calling crop')
        assert len(crop_values) == 3, 'Crop values for each dimension have to be provided!'
        cv = np.zeros(6, dtype=np.int)
        for i, values in enumerate(crop_values):
            assert np.shape(values) in [(), (2,)], 'Only one or two values per axis can be given!'
            cv[2 * i:2 * (i + 1)] = values
        cv *= np.resize([1, -1], len(cv))
        cv = np.where(cv == 0, None, cv)
        self.field = self.field[:, cv[0]:cv[1], cv[2]:cv[3], cv[4]:cv[5]]

    def get_vector(self, mask):
        """Returns the vector field components arranged in a vector, specified by a mask.

        Parameters
        ----------
        mask : :class:`~numpy.ndarray` (N=3, boolean)
            Masks the pixels from which the components should be taken.

        Returns
        -------
        vector : :class:`~numpy.ndarray` (N=1)
            The vector containing vector field components of the specified pixels.
            Order is: first all `x`-, then all `y`-, then all `z`-components.

        """
        self._log.debug('Calling get_vector')
        if mask is not None:
            return np.reshape([self.field[0][mask],
                               self.field[1][mask],
                               self.field[2][mask]], -1)
        else:
            return self.field_vec

    def set_vector(self, vector, mask=None):
        """Set the field components of the masked pixels to the values specified by `vector`.

        Parameters
        ----------
        mask : :class:`~numpy.ndarray` (N=3, boolean), optional
            Masks the pixels from which the components should be taken.
        vector : :class:`~numpy.ndarray` (N=1)
            The vector containing vector field components of the specified pixels.
            Order is: first all `x`-, then all `y-, then all `z`-components.

        Returns
        -------
        None

        """
        self._log.debug('Calling set_vector')
        assert np.size(vector) % 3 == 0, 'Vector has to contain all 3 components for every pixel!'
        count = np.size(vector) // 3
        if mask is not None:
            self.field[0][mask] = vector[:count]  # x-component
            self.field[1][mask] = vector[count:2 * count]  # y-component
            self.field[2][mask] = vector[2 * count:]  # z-component
        else:
            self.field_vec = vector

    def flip(self, axis='x'):
        """Flip/mirror the vector field around the specified axis.

        Parameters
        ----------
        axis: {'x', 'y', 'z'}, optional
            The axis around which the vector field is flipped.

        Returns
        -------
        magdata_flip: :class:`~.VectorData`
           A flipped copy of the :class:`~.VectorData` object.

        """
        self._log.debug('Calling flip')
        if axis == 'x':
            mag_x, mag_y, mag_z = self.field[:, :, :, ::-1]
            field_flip = np.array((-mag_x, mag_y, mag_z))
        elif axis == 'y':
            mag_x, mag_y, mag_z = self.field[:, :, ::-1, :]
            field_flip = np.array((mag_x, -mag_y, mag_z))
        elif axis == 'z':
            mag_x, mag_y, mag_z = self.field[:, ::-1, :, :]
            field_flip = np.array((mag_x, mag_y, -mag_z))
        else:
            raise ValueError("Wrong input! 'x', 'y', 'z' allowed!")
        return VectorData(self.a, field_flip)

    def rot90(self, axis='x'):
        """Rotate the vector field 90° around the specified axis (right hand rotation).

        Parameters
        ----------
        axis: {'x', 'y', 'z'}, optional
            The axis around which the vector field is rotated.

        Returns
        -------
        magdata_rot: :class:`~.VectorData`
           A rotated copy of the :class:`~.VectorData` object.

        """
        self._log.debug('Calling rot90')
        if axis == 'x':
            field_rot = np.zeros((3, self.dim[1], self.dim[0], self.dim[2]))
            for i in range(self.dim[2]):
                mag_x, mag_y, mag_z = self.field[:, :, :, i]
                mag_xrot, mag_yrot, mag_zrot = np.rot90(mag_x), np.rot90(mag_y), np.rot90(mag_z)
                field_rot[:, :, :, i] = np.array((mag_xrot, mag_zrot, -mag_yrot))
        elif axis == 'y':
            field_rot = np.zeros((3, self.dim[2], self.dim[1], self.dim[0]))
            for i in range(self.dim[1]):
                mag_x, mag_y, mag_z = self.field[:, :, i, :]
                mag_xrot, mag_yrot, mag_zrot = np.rot90(mag_x), np.rot90(mag_y), np.rot90(mag_z)
                field_rot[:, :, i, :] = np.array((mag_zrot, mag_yrot, -mag_xrot))
        elif axis == 'z':
            field_rot = np.zeros((3, self.dim[0], self.dim[2], self.dim[1]))
            for i in range(self.dim[0]):
                mag_x, mag_y, mag_z = self.field[:, i, :, :]
                mag_xrot, mag_yrot, mag_zrot = np.rot90(mag_x), np.rot90(mag_y), np.rot90(mag_z)
                field_rot[:, i, :, :] = np.array((mag_yrot, -mag_xrot, mag_zrot))
        else:
            raise ValueError("Wrong input! 'x', 'y', 'z' allowed!")
        return VectorData(self.a, field_rot)

    def get_slice(self, ax_slice=None, proj_axis='z'):
        """Extract a slice from the :class:`~.VectorData` object.

        Parameters
        ----------
        proj_axis : {'z', 'y', 'x'}, optional
            The axis, from which the slice is taken. The default is 'z'.
        ax_slice : None or int, optional
            The slice-index of the axis specified in `proj_axis`. Defaults to the center slice.

        Returns
        -------
        u_mag, v_mag, w_mag, submask : :class:`~numpy.ndarray` (N=2)
            The extracted vector field components in plane perpendicular to the `proj_axis` and
            the perpendicular component.

        """
        self._log.debug('Calling get_slice')
        # Find slice:
        assert proj_axis == 'z' or proj_axis == 'y' or proj_axis == 'x', \
            'Axis has to be x, y or z (as string).'
        if ax_slice is None:
            ax_slice = self.dim[{'z': 0, 'y': 1, 'x': 2}[proj_axis]] // 2
        if proj_axis == 'z':  # Slice of the xy-plane with z = ax_slice
            self._log.debug('proj_axis == z')
            u_mag = np.copy(self.field[0][ax_slice, ...])  # x-component
            v_mag = np.copy(self.field[1][ax_slice, ...])  # y-component
            w_mag = np.copy(self.field[2][ax_slice, ...])  # z-component
        elif proj_axis == 'y':  # Slice of the xz-plane with y = ax_slice
            self._log.debug('proj_axis == y')
            u_mag = np.copy(self.field[0][:, ax_slice, :])  # x-component
            v_mag = np.copy(self.field[2][:, ax_slice, :])  # z-component
            w_mag = np.copy(self.field[1][:, ax_slice, :])  # y-component
        elif proj_axis == 'x':  # Slice of the zy-plane with x = ax_slice
            self._log.debug('proj_axis == x')
            u_mag = np.swapaxes(np.copy(self.field[2][..., ax_slice]), 0, 1)  # z-component
            v_mag = np.swapaxes(np.copy(self.field[1][..., ax_slice]), 0, 1)  # y-component
            w_mag = np.swapaxes(np.copy(self.field[0][..., ax_slice]), 0, 1)  # x-component
        else:
            raise ValueError('{} is not a valid argument (use x, y or z)'.format(proj_axis))
        return u_mag, v_mag, w_mag

    def to_signal(self):
        """Convert :class:`~.VectorData` data into a HyperSpy signal.

        Returns
        -------
        signal: :class:`~hyperspy.signals.Signal`
            Representation of the :class:`~.VectorData` object as a HyperSpy Signal.

        Notes
        -----
        This method recquires the hyperspy package!

        """
        self._log.debug('Calling to_signal')
        signal = super().to_signal()
        # Set component axis:
        signal.axes_manager[3].name = 'x/y/z-component'
        signal.axes_manager[3].units = ''
        # Set metadata:
        signal.metadata.Signal.title = 'VectorData'
        # Return signal:
        return signal

    def save(self, filename, **kwargs):
        """Saves the VectorData in the specified format.

        The function gets the format from the extension:
            - hdf5 for HDF5.
            - EMD Electron Microscopy Dataset format (also HDF5).
            - llg format.
            - ovf format.
            - npy or npz for numpy formats.

        If no extension is provided, 'hdf5' is used. Most formats are
        saved with the HyperSpy package (internally the fielddata is first
        converted to a HyperSpy Signal.

        Each format accepts a different set of parameters. For details
        see the specific format documentation.

        Parameters
        ----------
        filename : str, optional
            Name of the file which the VectorData is saved into. The extension
            determines the saving procedure.

        """
        from .file_io.io_vectordata import save_vectordata
        save_vectordata(self, filename, **kwargs)

    def plot_quiver(self, ar_dens=1, log=False, scaled=True, scale=1., b_0=None,  # Only used here!
                    coloring='angle', cmap=None,  # Used here and plot_streamlines!
                    proj_axis='z', ax_slice=None, show_mask=True, bgcolor=None, axis=None,
                    figsize=None, **kwargs):
        """Plot a slice of the vector field as a quiver plot.

        Parameters
        ----------
        ar_dens: int, optional
            Number defining the arrow density which is plotted. A higher ar_dens number skips more
            arrows (a number of 2 plots every second arrow). Default is 1.
        log : boolean, optional
            The loratihm of the arrow length is plotted instead. This is helpful if only the
             direction of the arrows is important and the amplitude varies a lot. Default is False.
        scaled : boolean, optional
            Normalizes the plotted arrows in respect to the highest one. Default is True.
        scale: float, optional
            Additional multiplicative factor scaling the arrow length. Default is 1
            (no further scaling).
        b_0 : float, optional
            Saturation induction (saturation magnetisation times the vacuum permeability).
            If this is specified, a quiverkey is used to indicate the length of the longest arrow.
        coloring : {'angle', 'amplitude', 'uniform', matplotlib color}
            Color coding mode of the arrows. Use 'full' (default), 'angle', 'amplitude', 'uniform'
            (black or white, depending on `bgcolor`), or a matplotlib color keyword.
        cmap : string, optional
            The :class:`~matplotlib.colors.Colormap` which is used for the plot as a string.
            If not set, an appropriate one is used. Note that a subclass of
            :class:`~.colors.Colormap3D` should be used for angle encoding.
        proj_axis : {'z', 'y', 'x'}, optional
            The axis, from which a slice is plotted. The default is 'z'.
        ax_slice : int, optional
            The slice-index of the axis specified in `proj_axis`. Is set to the center of
            `proj_axis` if not specified.
        show_mask: boolean
            Default is True. Shows the outlines of the mask slice if available.
        bgcolor: {'white', 'black'}, optional
            Determines the background color of the plot.
        axis : :class:`~matplotlib.axes.AxesSubplot`, optional
            Axis on which the graph is plotted. Creates a new figure if none is specified.
        figsize : tuple of floats (N=2)
            Size of the plot figure.

        Returns
        -------
        axis: :class:`~matplotlib.axes.AxesSubplot`
            The axis on which the graph is plotted.

        Notes
        -----
        Uses :func:`~.plottools.format_axis` at the end. According keywords can also be given here.

        """
        self._log.debug('Calling plot_quiver')
        a = self.a
        if figsize is None:
            figsize = plottools.FIGSIZE_DEFAULT
        assert proj_axis == 'z' or proj_axis == 'y' or proj_axis == 'x', \
            'Axis has to be x, y or z (as string).'
        if ax_slice is None:
            ax_slice = self.dim[{'z': 0, 'y': 1, 'x': 2}[proj_axis]] // 2
        # Extract slice and mask:
        u_mag, v_mag = self.get_slice(ax_slice, proj_axis)[:2]
        submask = np.where(np.hypot(u_mag, v_mag) > 0, True, False)
        # Prepare quiver (select only used arrows if ar_dens is specified):
        dim_uv = u_mag.shape
        vv, uu = np.indices(dim_uv) + 0.5  # shift to center of pixel
        uu = uu[::ar_dens, ::ar_dens]
        vv = vv[::ar_dens, ::ar_dens]
        u_mag = u_mag[::ar_dens, ::ar_dens]
        v_mag = v_mag[::ar_dens, ::ar_dens]
        amplitudes = np.hypot(u_mag, v_mag)
        angles = np.angle(u_mag + 1j * v_mag, deg=True).tolist()
        # Calculate the arrow colors:
        if bgcolor is None:
            bgcolor = 'white'  # Default!
        cmap_overwrite = cmap
        if coloring == 'angle':
            self._log.debug('Encoding angles')
            hue = np.asarray(np.arctan2(v_mag, u_mag) / (2 * np.pi))
            hue[hue < 0] += 1
            cmap = colors.CMAP_CIRCULAR_DEFAULT
        elif coloring == 'amplitude':
            self._log.debug('Encoding amplitude')
            hue = amplitudes / amplitudes.max()
            if bgcolor == 'white':
                cmap = colors.cmaps['cubehelix_reverse']
            else:
                cmap = colors.cmaps['cubehelix_standard']
        elif coloring == 'uniform':
            self._log.debug('Automatic uniform color encoding')
            hue = amplitudes / amplitudes.max()
            if bgcolor == 'white':
                cmap = colors.cmaps['transparent_black']
            else:
                cmap = colors.cmaps['transparent_white']
        else:
            self._log.debug('Specified uniform color encoding')
            hue = np.zeros_like(u_mag)
            cmap = ListedColormap([coloring])
        if cmap_overwrite is not None:
            cmap = cmap_overwrite
        # If no axis is specified, a new figure is created:
        if axis is None:
            self._log.debug('axis is None')
            fig = plt.figure(figsize=figsize)
            axis = fig.add_subplot(1, 1, 1)
            tight = True
        else:
            tight = False
        axis.set_aspect('equal')
        # Take the logarithm of the arrows to clearly show directions (if specified):
        if log and np.any(amplitudes):  # If the slice is empty, skip!
            cutoff = 10
            amp = np.round(amplitudes, decimals=cutoff)
            min_value = amp[np.nonzero(amp)].min()
            u_mag = np.round(u_mag, decimals=cutoff) / min_value
            u_mag = np.log10(np.abs(u_mag) + 1) * np.sign(u_mag)
            v_mag = np.round(v_mag, decimals=cutoff) / min_value
            v_mag = np.log10(np.abs(v_mag) + 1) * np.sign(v_mag)
            amplitudes = np.hypot(u_mag, v_mag)  # Recalculate (used if scaled)!
        # Scale the amplitude of the arrows to the highest one (if specified):
        if scaled:
            u_mag /= amplitudes.max() + 1E-30
            v_mag /= amplitudes.max() + 1E-30
        # Plot quiver:
        # TODO: quiver does not work with matplotlib 2.0! FIX!
        quiv = axis.quiver(uu, vv, u_mag, v_mag, hue, cmap=cmap, clim=(0, 1), angles=angles,
                           pivot='middle', units='xy', scale_units='xy', scale=scale / ar_dens,
                           minlength=0.05, width=1*ar_dens, headlength=2, headaxislength=2,
                           headwidth=2, minshaft=2)
        axis.set_xlim(0, dim_uv[1])
        axis.set_ylim(0, dim_uv[0])
        # Determine colormap if necessary:
        if coloring == 'amplitude':
            cbar_mappable, cbar_label = quiv, 'amplitude'
        else:
            cbar_mappable, cbar_label = None, None
        # Change background color:
        axis.set_axis_bgcolor(bgcolor)
        # Show mask:
        if show_mask and not np.all(submask):  # Plot mask if desired and not trivial!
            vv, uu = np.indices(dim_uv) + 0.5  # shift to center of pixel
            mask_color = 'white' if bgcolor == 'black' else 'black'
            axis.contour(uu, vv, submask, levels=[0.5], colors=mask_color,
                         linestyles='dotted', linewidths=2)
        # Plot quiverkey if B_0 is specified):
        if b_0 and not log:  # The angles needed for log would break the quiverkey!
            label = '{:.3g} T'.format(amplitudes.max() * b_0)
            quiv.angles = 'uv'  # With a list of angles, the quiverkey would break!
            stroke = plottools.STROKE_DEFAULT
            txtcolor = 'w' if stroke == 'k' else 'k'
            edgecolor = stroke if stroke is not None else 'none'
            fontsize = kwargs.get('fontsize', None)
            if fontsize is None:
                fontsize = plottools.FONTSIZE_DEFAULT
            qk = plt.quiverkey(Q=quiv, X=0.88, Y=0.065, U=1, label=label, labelpos='W',
                               coordinates='axes', facecolor=txtcolor, edgecolor=edgecolor,
                               labelcolor=txtcolor, linewidth=0.5,
                               clip_box=axis.bbox, clip_on=True,
                               fontproperties={'size': kwargs.get('fontsize', fontsize)})
            if stroke is not None:
                qk.text.set_path_effects(
                    [patheffects.withStroke(linewidth=2, foreground=stroke)])
        # Return formatted axis:
        return plottools.format_axis(axis, sampling=a, cbar_mappable=cbar_mappable,
                                     cbar_label=cbar_label, tight_layout=tight, **kwargs)

    def plot_field(self, proj_axis='z', ax_slice=None, show_mask=True, bgcolor=None, axis=None,
                   figsize=None, **kwargs):
        """Plot a slice of the vector field as a color field imshow plot.

        Parameters
        ----------
        proj_axis : {'z', 'y', 'x'}, optional
            The axis, from which a slice is plotted. The default is 'z'.
        ax_slice : int, optional
            The slice-index of the axis specified in `proj_axis`. Is set to the center of
            `proj_axis` if not specified.
        show_mask: boolean
            Default is True. Shows the outlines of the mask slice if available.
        bgcolor: {'white', 'black'}, optional
            Determines the background color of the plot.
        axis : :class:`~matplotlib.axes.AxesSubplot`, optional
            Axis on which the graph is plotted. Creates a new figure if none is specified.
        figsize : tuple of floats (N=2)
            Size of the plot figure.

        Returns
        -------
        axis: :class:`~matplotlib.axes.AxesSubplot`
            The axis on which the graph is plotted.

        Notes
        -----
        Uses :func:`~.plottools.format_axis` at the end. According keywords can also be given here.

        """
        self._log.debug('Calling plot_field')
        a = self.a
        if figsize is None:
            figsize = plottools.FIGSIZE_DEFAULT
        assert proj_axis == 'z' or proj_axis == 'y' or proj_axis == 'x', \
            'Axis has to be x, y or z (as string).'
        if ax_slice is None:
            ax_slice = self.dim[{'z': 0, 'y': 1, 'x': 2}[proj_axis]] // 2
        # Extract slice and mask:
        u_mag, v_mag, w_mag = self.get_slice(ax_slice, proj_axis)
        submask = np.where(np.hypot(u_mag, v_mag) > 0, True, False)
        # If no axis is specified, a new figure is created:
        if axis is None:
            self._log.debug('axis is None')
            fig = plt.figure(figsize=figsize)
            axis = fig.add_subplot(1, 1, 1)
            tight = True
        else:
            tight = False
        axis.set_aspect('equal')
        # Determine 'z'-component for luminance (keep as gray if None):
        z_mag = w_mag
        if bgcolor == 'white':
            z_mag = np.where(submask, z_mag, np.max(np.hypot(u_mag, v_mag)))