"""This module provides classes that allow Numpy-type access
to VTK datasets and arrays. This is best described with some examples.

To normalize a VTK array:

import vtk
import vtk.numpy_interface.dataset_adapter as dsa
import vtk.numpy_interface.algorithms as algs

rt = vtk.vtkRTAnalyticSource()
rt.Update()
image = dsa.WrapDataObject(rt.GetOutput())
rtdata = image.PointData['RTData']
rtmin = algs.min(rtdata)
rtmax = algs.max(rtdata)
rtnorm = (rtdata - rtmin) / (rtmax - rtmin)
image.PointData.append(rtnorm, 'RTData - normalized')
print image.GetPointData().GetArray('RTData - normalized').GetRange()

To calculate gradient:

grad= algs.gradient(rtnorm)

To access subsets:

>>> grad[0:10]
VTKArray([[ 0.10729134,  0.03763443,  0.03136338],
       [ 0.02754352,  0.03886006,  0.032589  ],
       [ 0.02248248,  0.04127144,  0.03500038],
       [ 0.02678365,  0.04357527,  0.03730421],
       [ 0.01765099,  0.04571581,  0.03944477],
       [ 0.02344007,  0.04763837,  0.04136734],
       [ 0.01089381,  0.04929155,  0.04302051],
       [ 0.01769151,  0.05062952,  0.04435848],
       [ 0.002764  ,  0.05161414,  0.04534309],
       [ 0.01010841,  0.05221677,  0.04594573]])

>>> grad[:, 0]
VTKArray([ 0.10729134,  0.02754352,  0.02248248, ..., -0.02748174,
       -0.02410045,  0.05509736])

All of this functionality is also supported for composite datasets
even though their data arrays may be spread across multiple datasets.
We have implemented a VTKCompositeDataArray class that handles many
Numpy style operators and is supported by all algorithms in the
algorithms module.

This module also provides an API to access composite datasets.
For example:

mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, image.VTKObject)
mb.SetBlock(1e, image.VTKObject)
cds = dsa.WrapDataObject(mb)
for block in cds:
    print block

Note that this module implements only the wrappers for datasets
and arrays. The classes implement many useful operators. However,
to make best use of these classes, take a look at the algorithms
module.
"""
try:
    import numpy
except ImportError:
    raise RuntimeError("This module depends on the numpy module. Please make\
sure that it is installed properly.")

import itertools
import operator
import sys
from vtk import buffer_shared
from vtk.util import numpy_support
from vtk.vtkCommonDataModel import vtkDataObject
import weakref

if sys.hexversion < 0x03000000:
    izip = itertools.izip
else:
    izip = zip

def reshape_append_ones (a1, a2):
    """Returns a list with the two arguments, any of them may be
    processed.  If the arguments are numpy.ndarrays, append 1s to the
    shape of the array with the smallest number of dimensions until
    the arrays have the same number of dimensions. Does nothing if the
    arguments are not ndarrays or the arrays have the same number of
    dimensions.

    """
    l = [a1, a2]
    if (isinstance(a1, numpy.ndarray) and isinstance(a2, numpy.ndarray)):
        len1 = len(a1.shape)
        len2 = len(a2.shape)
        if (len1 == len2 or len1 == 0 or len2 == 0 or
            a1.shape[0] != a2.shape[0]):
            return l;
        elif (len1 < len2):
            d = len1
            maxLength = len2
            i = 0
        else:
            d = len2
            maxLength = len1
            i = 1
        while (d < maxLength):
            l[i] = numpy.expand_dims(l[i], d)
            d = d + 1
    return l

class ArrayAssociation :
    """Easy access to vtkDataObject.AttributeTypes"""
    POINT = vtkDataObject.POINT
    CELL  = vtkDataObject.CELL
    FIELD = vtkDataObject.FIELD
    ROW = vtkDataObject.ROW

class VTKObjectWrapper(object):
    """Superclass for classes that wrap VTK objects with Python objects.
    This class holds a reference to the wrapped VTK object. It also
    forwards unresolved methods to the underlying object by overloading
    __get__attr."""
    def __init__(self, vtkobject):
        self.VTKObject = vtkobject

    def __getattr__(self, name):
        "Forwards unknown attribute requests to VTK object."
        return getattr(self.VTKObject, name)

def _MakeObserver(numpy_array):
    "Internal function used to attach a numpy array to a vtk array"
    def Closure(caller, event):
        foo = numpy_array
    return Closure

def vtkDataArrayToVTKArray(array, dataset=None):
    "Given a vtkDataArray and a dataset owning it, returns a VTKArray."
    narray = numpy_support.vtk_to_numpy(array)

    # Make arrays of 9 components into matrices. Also transpose
    # as VTK store matrices in Fortran order
    shape = narray.shape
    if len(shape) == 2 and shape[1] == 9:
        narray = narray.reshape((shape[0], 3, 3)).transpose(0, 2, 1)

    return VTKArray(narray, array=array, dataset=dataset)

def numpyTovtkDataArray(array, name="numpy_array", array_type=None):
    """Given a numpy array or a VTKArray and a name, returns a vtkDataArray.
    The resulting vtkDataArray will store a reference to the numpy array
    through a DeleteEvent observer: the numpy array is released only when
    the vtkDataArray is destroyed."""
    if not array.flags.contiguous:
        array = array.copy()
    vtkarray = numpy_support.numpy_to_vtk(array, array_type=array_type)
    vtkarray.SetName(name)
    # This makes the VTK array carry a reference to the numpy array.
    vtkarray.AddObserver('DeleteEvent', _MakeObserver(array))
    return vtkarray

def _make_tensor_array_contiguous(array):
    if array is None:
        return None
    if array.flags.contiguous:
        return array
    array = numpy.asarray(array)
    size = array.dtype.itemsize
    strides = array.strides
    if len(strides) == 3 and strides[1]/size == 1 and strides[2]/size == 3:
        return array.transpose(0, 2, 1)
    return array

def _metaclass(mcs):
    """For compatibility between python 2 and python 3."""
    def decorator(cls):
        body = vars(cls).copy()
        body.pop('__dict__', None)
        body.pop('__weakref__', None)
        return mcs(cls.__name__, cls.__bases__, body)
    return decorator

class VTKArrayMetaClass(type):
    def __new__(mcs, name, parent, attr):
        """We overwrite numerical/comparison operators because we might need
        to reshape one of the arrays to perform the operation without
        broadcast errors. For instace:

        An array G of shape (n,3) resulted from computing the
        gradient on a scalar array S of shape (n,) cannot be added together without
        reshaping.
        G + expand_dims(S,1) works,
        G + S gives an error:
        ValueError: operands could not be broadcast together with shapes (n,3) (n,)

        This metaclass overwrites operators such that it computes this
        reshape operation automatically by appending 1s to the
        dimensions of the array with fewer dimensions.

        """
        def add_numeric_op(attr_name):
            """Create an attribute named attr_name that calls
            _numeric_op(self, other, op)."""
            def closure(self, other):
                return VTKArray._numeric_op(self, other, attr_name)
            closure.__name__ = attr_name
            attr[attr_name] = closure

        def add_default_numeric_op(op_name):
            """Adds '__[op_name]__' attribute that uses operator.[op_name]"""
            add_numeric_op("__%s__"%op_name)

        def add_reverse_numeric_op(attr_name):
            """Create an attribute named attr_name that calls
            _reverse_numeric_op(self, other, op)."""
            def closure(self, other):
                return VTKArray._reverse_numeric_op(self, other, attr_name)
            closure.__name__ = attr_name
            attr[attr_name] = closure

        def add_default_reverse_numeric_op(op_name):
            """Adds '__r[op_name]__' attribute that uses operator.[op_name]"""
            add_reverse_numeric_op("__r%s__"%op_name)

        def add_default_numeric_ops(op_name):
            """Call both add_default_numeric_op and add_default_reverse_numeric_op."""
            add_default_numeric_op(op_name)
            add_default_reverse_numeric_op(op_name)

        add_default_numeric_ops("add")
        add_default_numeric_ops("sub")
        add_default_numeric_ops("mul")
        if sys.hexversion < 0x03000000:
            add_default_numeric_ops("div")
        add_default_numeric_ops("truediv")
        add_default_numeric_ops("floordiv")
        add_default_numeric_ops("mod")
        add_default_numeric_ops("pow")
        add_default_numeric_ops("lshift")
        add_default_numeric_ops("rshift")
        add_numeric_op("and")
        add_default_numeric_ops("xor")
        add_numeric_op("or")

        add_default_numeric_op("lt")
        add_default_numeric_op("le")
        add_default_numeric_op("eq")
        add_default_numeric_op("ne")
        add_default_numeric_op("ge")
        add_default_numeric_op("gt")
        return type.__new__(mcs, name, parent, attr)

@_metaclass(VTKArrayMetaClass)
class VTKArray(numpy.ndarray):
    """This is a sub-class of numpy ndarray that stores a
    reference to a vtk array as well as the owning dataset.
    The numpy array and vtk array should point to the same
    memory location."""

    def _numeric_op(self, other, attr_name):
        """Used to implement numpy-style numerical operations such as __add__,
        __mul__, etc."""
        l = reshape_append_ones(self, other)
        return getattr(numpy.ndarray, attr_name)(l[0], l[1])

    def _reverse_numeric_op(self, other, attr_name):
        """Used to implement numpy-style numerical operations such as __add__,
        __mul__, etc."""
        l = reshape_append_ones(self, other)
        return getattr(numpy.ndarray, attr_name)(l[0], l[1])

    def __new__(cls, input_array, array=None, dataset=None):
        # Input array is an already formed ndarray instance
        # We first cast to be our class type
        obj = numpy.asarray(input_array).view(cls)
        obj.Association = ArrayAssociation.FIELD
        # add the new attributes to the created instance
        obj.VTKObject = array
        # if dataset:
        #     import weakref
        #     obj.DataSet = weakref.ref(dataset)
        obj.DataSet = dataset
        # Finally, we must return the newly created object:
        return obj

    def __array_finalize__(self,obj):
        # Copy the VTK array only if the two share data
        slf = _make_tensor_array_contiguous(self)
        obj2 = _make_tensor_array_contiguous(obj)

        self.VTKObject = None
        try:
            # This line tells us that they are referring to the same buffer.
            # Much like two pointers referring to same memory location in C/C++.
            if buffer_shared(slf, obj2):
                self.VTKObject = getattr(obj, 'VTKObject', None)
        except TypeError:
            pass

        self.Association = getattr(obj, 'Association', None)
        self.DataSet = getattr(obj, 'DataSet', None)

    def __getattr__(self, name):
        "Forwards unknown attribute requests to VTK array."
        try:
            o = self.__dict__["VTKObject"]
        except KeyError:
            o = None
        if o is None:
            raise AttributeError("'%s' object has no attribute '%s'" %
                                 (self.__class__.__name__, name))
        return getattr(o, name)

class VTKNoneArrayMetaClass(type):
    def __new__(mcs, name, parent, attr):
        """Simplify the implementation of the numeric/logical sequence API."""
        def _add_op(attr_name, op):
            """Create an attribute named attr_name that calls
            _numeric_op(self, other, op)."""
            def closure(self, other):
                return VTKNoneArray._op(self, other, op)
            closure.__name__ = attr_name
            attr[attr_name] = closure

        def _add_default_reverse_op(op_name):
            """Adds '__r[op_name]__' attribute that uses operator.[op_name]"""
            _add_op("__r%s__"%op_name, getattr(operator, op_name))

        def _add_default_op(op_name):
            """Adds '__[op_name]__' attribute that uses operator.[op_name]"""
            _add_op("__%s__"%op_name, getattr(operator, op_name))

        def _add_default_ops(op_name):
            """Call both add_default_numeric_op and add_default_reverse_numeric_op."""
            _add_default_op(op_name)
            _add_default_reverse_op(op_name)

        _add_default_ops("add")
        _add_default_ops("sub")
        _add_default_ops("mul")
        if sys.hexversion < 0x03000000:
            _add_default_ops("div")
        _add_default_ops("truediv")
        _add_default_ops("floordiv")
        _add_default_ops("mod")
        _add_default_ops("pow")
        _add_default_ops("lshift")
        _add_default_ops("rshift")
        _add_op("__and__", operator.and_)
        _add_op("__rand__", operator.and_)
        _add_default_ops("xor")
        _add_op("__or__", operator.or_)
        _add_op("__ror__", operator.or_)

        _add_default_op("lt")
        _add_default_op("le")
        _add_default_op("eq")
        _add_default_op("ne")
        _add_default_op("ge")
        _add_default_op("gt")
        return type.__new__(mcs, name, parent, attr)

@_metaclass(VTKNoneArrayMetaClass)
class VTKNoneArray(object):
    """VTKNoneArray is used to represent a "void" array. An instance
    of this class (NoneArray) is returned instead of None when an
    array that doesn't exist in a DataSetAttributes is requested.
    All operations on the NoneArray return NoneArray. The main reason
    for this is to support operations in parallel where one of the
    processes may be working on an empty dataset. In such cases,
    the process is still expected to evaluate a whole expression because
    some of the functions may perform bulk MPI communication. None
    cannot be used in these instances because it cannot properly override
    operators such as __add__, __sub__ etc. This is the main raison
    d'etre for VTKNoneArray."""

    def __getitem__(self, index):
        return NoneArray

    def _op(self, other, op):
        """Used to implement numpy-style numerical operations such as __add__,
        __mul__, etc."""
        return NoneArray

    def astype(self, dtype):
        """Implements numpy array's astype method."""
        return NoneArray

NoneArray = VTKNoneArray()

class VTKCompositeDataArrayMetaClass(type):
    def __new__(mcs, name, parent, attr):
        """Simplify the implementation of the numeric/logical sequence API."""
        def add_numeric_op(attr_name, op):
            """Create an attribute named attr_name that calls
            _numeric_op(self, other, op)."""
            def closure(self, other):
                return VTKCompositeDataArray._numeric_op(self, other, op)
            closure.__name__ = attr_name
            attr[attr_name] = closure

        def add_reverse_numeric_op(attr_name, op):
            """Create an attribute named attr_name that calls
            _reverse_numeric_op(self, other, op)."""
            def closure(self, other):
                return VTKCompositeDataArray._reverse_numeric_op(self, other, op)
            closure.__name__ = attr_name
            attr[attr_name] = closure

        def add_default_reverse_numeric_op(op_name):
            """Adds '__r[op_name]__' attribute that uses operator.[op_name]"""
            add_reverse_numeric_op("__r%s__"%op_name, getattr(operator, op_name))

        def add_default_numeric_op(op_name):
            """Adds '__[op_name]__' attribute that uses operator.[op_name]"""
            add_numeric_op("__%s__"%op_name, getattr(operator, op_name))

        def add_default_numeric_ops(op_name):
            """Call both add_default_numeric_op and add_default_reverse_numeric_op."""
            add_default_numeric_op(op_name)
            add_default_reverse_numeric_op(op_name)

        add_default_numeric_ops("add")
        add_default_numeric_ops("sub")
        add_default_numeric_ops("mul")
        if sys.hexversion < 0x03000000:
            add_default_numeric_ops("div")
        add_default_numeric_ops("truediv")
        add_default_numeric_ops("floordiv")
        add_default_numeric_ops("mod")
        add_default_numeric_ops("pow")
        add_default_numeric_ops("lshift")
        add_default_numeric_ops("rshift")
        add_numeric_op("__and__", operator.and_)
        add_reverse_numeric_op("__rand__", operator.and_)
        add_default_numeric_ops("xor")
        add_numeric_op("__or__", operator.or_)
        add_reverse_numeric_op("__ror__", operator.or_)

        add_default_numeric_op("lt")
        add_default_numeric_op("le")
        add_default_numeric_op("eq")
        add_default_numeric_op("ne")
        add_default_numeric_op("ge")
        add_default_numeric_op("gt")
        return type.__new__(mcs, name, parent, attr)

@_metaclass(VTKCompositeDataArrayMetaClass)
class VTKCompositeDataArray(object):
    """This class manages a set of arrays of the same name contained
    within a composite dataset. Its main purpose is to provide a
    Numpy-type interface to composite data arrays which are naturally
    nothing but a collection of vtkDataArrays. A VTKCompositeDataArray
    makes such a collection appear as a single Numpy array and support
    all array operations that this module and the associated algorithm
    module support. Note that this is not a subclass of a Numpy array
    and as such cannot be passed to native Numpy functions. Instead
    VTK modules should be used to process composite arrays.
    """

    def __init__(self, arrays = [], dataset = None, name = None,
                 association = ArrayAssociation.FIELD):
        """Construct a composite array given a container of
        arrays, a dataset, name and association. It is sufficient
        to define a container of arrays to define a composite array.
        It is also possible to initialize an array by defining
        the dataset, name and array association. In that case,
        the underlying arrays will be created lazily when they
        are needed. It is recommended to use the latter method
        when initializing from an existing composite dataset."""
        self._Arrays = arrays
        self.DataSet = dataset
        self.Name = name
        self.Association = association
        self.Initialized = False

    def __init_from_composite(self):
        if self.Initialized:
            return

        self.Initialized = True

        if self.DataSet is None or self.Name is None:
            return

        self._Arrays = []
        for ds in self.DataSet:
            self._Arrays.append(ds.GetAttributes(self.Association)[self.Name])

    def GetSize(self):
        "Returns the number of elements in the array."
        self.__init_from_composite()
        size = numpy.int64(0)
        for a in self._Arrays:
            try:
                size += a.size
            except AttributeError:
                pass
        return size

    size = property(GetSize)

    def GetArrays(self):
        """Returns the internal container of VTKArrays. If necessary,
        this will populate the array list from a composite dataset."""
        self.__init_from_composite()
        return self._Arrays

    Arrays = property(GetArrays)

    def __getitem__(self, index):
        """Overwritten to refer indexing to underlying VTKArrays.
        For the most part, this will behave like Numpy. Note
        that indexing is done per array - arrays are never treated
        as forming a bigger array. If the index is another composite
        array, a one-to-one mapping between arrays is assumed.
        """
        self.__init_from_composite()
        res = []
        if type(index) == VTKCompositeDataArray:
            for a, idx in izip(self._Arrays, index.Arrays):
                if a is not NoneArray:
                    res.append(a.__getitem__(idx))
                else:
                    res.append(NoneArray)
        else:
            for a in self._Arrays:
                if a is not NoneArray:
                    res.append(a.__getitem__(index))
                else:
                    res.append(NoneArray)
        return VTKCompositeDataArray(res, dataset=self.DataSet)

    def _numeric_op(self, other, op):
        """Used to implement numpy-style numerical operations such as __add__,
        __mul__, etc."""
        self.__init_from_composite()
        res = []
        if type(other) == VTKCompositeDataArray:
            for a1, a2 in izip(self._Arrays, other.Arrays):
                if a1 is not NoneArray and a2 is not NoneArray:
                    l = reshape_append_ones(a1, a2)
                    res.append(op(l[0],l[1]))
                else:
                    res.append(NoneArray)
        else:
            for a in self._Arrays:
                if a is not NoneArray:
                    l = reshape_append_ones(a, other)
                    res.append(op(l[0], l[1]))
                else:
                    res.append(NoneArray)
        return VTKCompositeDataArray(res, dataset=self.DataSet)

    def _reverse_numeric_op(self, other, op):
        """Used to implement numpy-style numerical operations such as __add__,
        __mul__, etc."""
        self.__init_from_composite()
        res = []
        if type(other) == VTKCompositeDataArray:
            for a1, a2 in izip(self._Arrays, other.Arrays):
                if a1 is not NoneArray and a2 is notNoneArray:
                    l = reshape_append_ones(a2,a1)
                    res.append(op(l[0],l[1]))
                else:
                    res.append(NoneArray)
        else:
            for a in self._Arrays:
                if a is not NoneArray:
                    l = reshape_append_ones(other, a)
                    res.append(op(l[0], l[1]))
                else:
                    res.append(NoneArray)
        return VTKCompositeDataArray(res, dataset=self.DataSet)

    def __str__(self):
        return self.Arrays.__str__()

    def astype(self, dtype):
        """Implements numpy array's as array method."""
        res = []
        if self is not NoneArray:
            for a in self.Arrays:
                if a is NoneArray:
                    res.append(NoneArray)
                else:
                    res.append(a.astype(dtype))
        return VTKCompositeDataArray(res, dataset = self.DataSet)

class DataSetAttributes(VTKObjectWrapper):
    """This is a python friendly wrapper of vtkDataSetAttributes. It
    returns VTKArrays. It also provides the dictionary interface."""

    def __init__(self, vtkobject, dataset, association):
        super(DataSetAttributes, self).__init__(vtkobject)
        # import weakref
        # self.DataSet = weakref.ref(dataset)
        self.DataSet = dataset
        self.Association = association

    def __getitem__(self, idx):
        """Implements the [] operator. Accepts an array name or index."""
        return self.GetArray(idx)

    def GetArray(self, idx):
        "Given an index or name, returns a VTKArray."
        if isinstance(idx, int) and idx >= self.VTKObject.GetNumberOfArrays():
            raise IndexError("array index out of range")
        vtkarray = self.VTKObject.GetArray(idx)
        if not vtkarray:
            vtkarray = self.VTKObject.GetAbstractArray(idx)
            if vtkarray:
                return vtkarray
            return NoneArray
        array = vtkDataArrayToVTKArray(vtkarray, self.DataSet)
        array.Association = self.Association
        return array

    def keys(self):
        """Returns the names of the arrays as a list."""
        kys = []
        narrays = self.VTKObject.GetNumberOfArrays()
        for i in range(narrays):
            name = self.VTKObject.GetAbstractArray(i).GetName()
            if name:
                kys.append(name)
        return kys

    def values(self):
        """Returns the arrays as a list."""
        vals = []
        narrays = self.VTKObject.GetNumberOfArrays()
        for i in range(narrays):
            a = self.VTKObject.GetAbstractArray(i)
            if a.GetName():
                vals.append(a)
        return vals

    def PassData(self, other):
        "A wrapper for vtkDataSet.PassData."
        try:
            self.VTKObject.PassData(other)
        except TypeError:
            self.VTKObject.PassData(other.VTKObject)

    def append(self, narray, name):
        """Appends a new array to the dataset attributes."""
        if narray is NoneArray:
            # if NoneArray, nothing to do.
            return

        if self.Association == ArrayAssociation.POINT:
            arrLength = self.DataSet.GetNumberOfPoints()
        elif self.Association == ArrayAssociation.CELL:
            arrLength = self.DataSet.GetNumberOfCells()
        else:
            if not isinstance(narray, numpy.ndarray):
                arrLength = 1
            else:
                arrLength = narray.shape[0]

        # Fixup input array length:
        if not isinstance(narray, numpy.ndarray) or numpy.ndim(narray) == 0: # Scalar input
            narray = narray * numpy.ones(arrLength)
        elif narray.shape[0] != arrLength: # Vector input
            components = 1
            for l in narray.shape:
                components *= l
            narray = narray.flatten() * numpy.ones((arrLength, components))

        shape = narray.shape

        if len(shape) == 3:
            # Array of matrices. We need to make sure the order  in memory is right.
            # If column order (c order), transpose. VTK wants row order (fortran
            # order). The deep copy later will make sure that the array is contiguous.
            # If row order but not contiguous, transpose so that the deep copy below
            # does not happen.
            size = narray.dtype.itemsize
            if (narray.strides[1]/size == 3 and narray.strides[2]/size == 1) or \
                (narray.strides[1]/size == 1 and narray.strides[2]/size == 3 and \
                 not narray.flags.contiguous):
                narray  = narray.transpose(0, 2, 1)

        # If array is not contiguous, make a deep copy that is contiguous
        if not narray.flags.contiguous:
            narray = narray.copy()

        # Flatten array of matrices to array of vectors
        if len(shape) == 3:
            narray = narray.reshape(shape[0], shape[1]*shape[2])

        # this handle the case when an input array is directly appended on the
        # output. We want to make sure that the array added to the output is not
        # referring to the input dataset.
        copy = VTKArray(narray)
        try:
            copy.VTKObject = narray.VTKObject
        except AttributeError: pass
        arr = numpyTovtkDataArray(copy, name)
        self.VTKObject.AddArray(arr)

class CompositeDataSetAttributes():
    """This is a python friendly wrapper for vtkDataSetAttributes for composite
    datsets. Since composite datasets themselves don't have attribute data, but
    the attribute data is associated with the leaf nodes in the composite
    dataset, this class simulates a DataSetAttributes interface by taking a
    union of DataSetAttributes associated with all leaf nodes."""

    def __init__(self, dataset, association):
        # import weakref
        # self.DataSet = weakref.ref(dataset)
        self.DataSet = dataset
        self.Association = association
        self.ArrayNames = []
        self.Arrays = {}

        # build the set of arrays available in the composite dataset. Since
        # composite datasets can have partial arrays, we need to iterate over
        # all non-null blocks in the dataset.
        self.__determine_arraynames()

    def __determine_arraynames(self):
        array_set = set()
        array_list = []
        for dataset in self.DataSet:
            dsa = dataset.GetAttributes(self.Association)
            for array_name in dsa.keys():
                if array_name not in array_set:
                    array_set.add(array_name)
                    array_list.append(array_name)
        self.ArrayNames = array_list

    def keys(self):
        """Returns the names of the arrays as a list."""
        return self.ArrayNames

    def __getitem__(self, idx):
        """Implements the [] operator. Accepts an array name."""
        return self.GetArray(idx)

    def append(self, narray, name):
        """Appends a new array to the composite dataset attributes."""
        if narray is NoneArray:
            # if NoneArray, nothing to do.
            return

        added = False
        if not isinstance(narray, VTKCompositeDataArray): # Scalar input
            for ds in self.DataSet:
                ds.GetAttributes(self.Association).append(narray, name)
                added = True
            if added:
                self.ArrayNames.append(name)
                # don't add the narray since it's a scalar. GetArray() will create a
                # VTKCompositeArray on-demand.
        else:
            for ds, array in izip(self.DataSet, narray.Arrays):
                if array is not None:
                    ds.GetAttributes(self.Association).append(array, name)
                    added = True
            if added:
                self.ArrayNames.append(name)
                self.Arrays[name] = weakref.ref(narray)

    def GetArray(self, idx):
        """Given a name, returns a VTKCompositeArray."""
        arrayname = idx
        if arrayname not in self.ArrayNames:
            return NoneArray
        if arrayname not in self.Arrays or self.Arrays[arrayname]() is None:
            array = VTKCompositeDataArray(
                dataset = self.DataSet, name = arrayname, association = self.Association)
            self.Arrays[arrayname] = weakref.ref(array)
        else:
            array = self.Arrays[arrayname]()
        return array

    def PassData(self, other):
        """Emulate PassData for composite datasets."""
        for this,that in zip(self.DataSet, other.DataSet):
            for assoc in [ArrayAssociation.POINT, ArrayAssociation.CELL]:
                this.GetAttributes(assoc).PassData(that.GetAttributes(assoc))

class CompositeDataIterator(object):
    """Wrapper for a vtkCompositeDataIterator class to satisfy
       the python iterator protocol. This iterator iterates
       over non-empty leaf nodes. To iterate over empty or
       non-leaf nodes, use the vtkCompositeDataIterator directly.
       """

    def __init__(self, cds):
        self.Iterator = cds.NewIterator()
        if self.Iterator:
            self.Iterator.UnRegister(None)
            self.Iterator.GoToFirstItem()

    def __iter__(self):
        return self

    def __next__(self):
        if not self.Iterator:
            raise StopIteration

        if self.Iterator.IsDoneWithTraversal():
            raise StopIteration
        retVal = self.Iterator.GetCurrentDataObject()
        self.Iterator.GoToNextItem()
        return WrapDataObject(retVal)

    def next(self):
        return self.__next__()

    def __getattr__(self, name):
        """Returns attributes from the vtkCompositeDataIterator."""
        return getattr(self.Iterator, name)

class MultiCompositeDataIterator(CompositeDataIterator):
    """Iterator that can be used to iterate over multiple
    composite datasets together. This iterator works only
    with arrays that were copied from an original using
    CopyStructured. The most common use case is to use
    CopyStructure, then iterate over input and output together
    while creating output datasets from corresponding input
    datasets."""
    def __init__(self, cds):
        CompositeDataIterator.__init__(self, cds[0])
        self.Datasets = cds

    def __next__(self):
        if not self.Iterator:
            raise StopIteration

        if self.Iterator.IsDoneWithTraversal():
            raise StopIteration
        retVal = []
        retVal.append(WrapDataObject(self.Iterator.GetCurrentDataObject()))
        if len(self.Datasets) > 1:
            for cd in self.Datasets[1:]:
                retVal.append(WrapDataObject(cd.GetDataSet(self.Iterator)))
        self.Iterator.GoToNextItem()
        return retVal

    def next(self):
        return self.__next__()

class DataObject(VTKObjectWrapper):
    """A wrapper for vtkDataObject that makes it easier to access FielData
    arrays as VTKArrays
    """

    def GetAttributes(self, type):
        """Returns the attributes specified by the type as a DataSetAttributes
         instance."""
        if type == ArrayAssociation.FIELD:
            return DataSetAttributes(self.VTKObject.GetFieldData(), self, type)
        return DataSetAttributes(self.VTKObject.GetAttributes(type), self, type)

    def GetFieldData(self):
        "Returns the field data as a DataSetAttributes instance."
        return DataSetAttributes(self.VTKObject.GetFieldData(), self, ArrayAssociation.FIELD)

    FieldData = property(GetFieldData, None, None, "This property returns the field data of a data object.")

class Table(DataObject):
    """A wrapper for vtkFielData that makes it easier to access RowData array as
    VTKArrays
    """
    def GetRowData(self):
        "Returns the row data as a DataSetAttributes instance."
        return self.GetAttributes(ArrayAssociation.ROW)

    RowData = property(GetRowData, None, None, "This property returns the row data of the table.")

class CompositeDataSet(DataObject):
    """A wrapper for vtkCompositeData and subclasses that makes it easier
    to access Point/Cell/Field data as VTKCompositeDataArrays. It also
    provides a Python type iterator."""

    def __init__(self, vtkobject):
        DataObject.__init__(self, vtkobject)
        self._PointData = None
        self._CellData = None
        self._FieldData = None
        self._Points = None

    def __iter__(self):
        "Creates an iterator for the contained datasets."
        return CompositeDataIterator(self)

    def GetNumberOfElements(self, assoc):
        """Returns the total number of cells or points depending
        on the value of assoc which can be ArrayAssociation.POINT or
        ArrayAssociation.CELL."""
        result = 0
        for dataset in self:
            result += dataset.GetNumberOfElements(assoc)
        return int(result)

    def GetNumberOfPoints(self):
        """Returns the total number of points of all datasets
        in the composite dataset. Note that this traverses the
        whole composite dataset every time and should not be
        called repeatedly for large composite datasets."""
        return self.GetNumberOfElements(ArrayAssociation.POINT)

    def GetNumberOfCells(self):
        """Returns the total number of cells of all datasets
        in the composite dataset. Note that this traverses the
        whole composite dataset every time and should not be
        called repeatedly for large composite datasets."""
        return self.GetNumberOfElements(ArrayAssociation.CELL)

    def GetAttributes(self, type):
        """Returns the attributes specified by the type as a
        CompositeDataSetAttributes instance."""
        return CompositeDataSetAttributes(self, type)

    def GetPointData(self):
        "Returns the point data as a DataSetAttributes instance."
        if self._PointData is None or self._PointData() is None:
            pdata = self.GetAttributes(ArrayAssociation.POINT)
            self._PointData = weakref.ref(pdata)
        return self._PointData()

    def GetCellData(self):
        "Returns the cell data as a DataSetAttributes instance."
        if self._CellData is None or self._CellData() is None:
            cdata = self.GetAttributes(ArrayAssociation.CELL)
            self._CellData = weakref.ref(cdata)
        return self._CellData()

    def GetFieldData(self):
        "Returns the field data as a DataSetAttributes instance."
        if self._FieldData is None or self._FieldData() is None:
            fdata = self.GetAttributes(ArrayAssociation.FIELD)
            self._FieldData = weakref.ref(fdata)
        return self._FieldData()

    def GetPoints(self):
        "Returns the points as a VTKCompositeDataArray instance."
        if self._Points is None or self._Points() is None:
            pts = []
            for ds in self:
                try:
                    _pts = ds.Points
                except AttributeError:
                    _pts = None

                if _pts is None:
                    pts.append(NoneArray)
                else:
                    pts.append(_pts)
            if len(pts) == 0 or all([a is NoneArray for a in pts]):
                cpts = NoneArray
            else:
                cpts = VTKCompositeDataArray(pts, dataset=self)
            self._Points = weakref.ref(cpts)
        return self._Points()

    PointData = property(GetPointData, None, None, "This property returns the point data of the dataset.")
    CellData = property(GetCellData, None, None, "This property returns the cell data of a dataset.")
    FieldData = property(GetFieldData, None, None, "This property returns the field data of a dataset.")
    Points = property(GetPoints, None, None, "This property returns the points of the dataset.")

class DataSet(DataObject):
    """This is a python friendly wrapper of a vtkDataSet that defines
    a few useful properties."""

    def GetPointData(self):
        "Returns the point data as a DataSetAttributes instance."
        return self.GetAttributes(ArrayAssociation.POINT)

    def GetCellData(self):
        "Returns the cell data as a DataSetAttributes instance."
        return self.GetAttributes(ArrayAssociation.CELL)

    PointData = property(GetPointData, None, None, "This property returns the point data of the dataset.")
    CellData = property(GetCellData, None, None, "This property returns the cell data of a dataset.")

class PointSet(DataSet):
    """This is a python friendly wrapper of a vtkPointSet that defines
    a few useful properties."""
    def GetPoints(self):
        """Returns the points as a VTKArray instance. Returns None if the
        dataset has implicit points."""
        if not self.VTKObject.GetPoints():
            return None
        return vtkDataArrayToVTKArray(
            self.VTKObject.GetPoints().GetData(), self)

    def SetPoints(self, pts):
        """Given a VTKArray instance, sets the points of the dataset."""
        from vtk.vtkCommonCore import vtkPoints
        if isinstance(pts, vtkPoints):
            p = pts
        else:
            pts = numpyTovtkDataArray(pts)
            p = vtkPoints()
            p.SetData(pts)
        self.VTKObject.SetPoints(p)

    Points = property(GetPoints, SetPoints, None, "This property returns the point coordinates of dataset.")

class PolyData(PointSet):
    """This is a python friendly wrapper of a vtkPolyData that defines
    a few useful properties."""

    def GetPolygons(self):
        """Returns the polys as a VTKArray instance."""
        if not self.VTKObject.GetPolys():
            return None
        return vtkDataArrayToVTKArray(
            self.VTKObject.GetPolys().GetData(), self)

    Polygons = property(GetPolygons, None, None, "This property returns the connectivity of polygons.")

class UnstructuredGrid(PointSet):
    """This is a python friendly wrapper of a vtkUnstructuredGrid that defines
    a few useful properties."""

    def GetCellTypes(self):
        """Returns the cell types as a VTKArray instance."""
        if not self.VTKObject.GetCellTypesArray():
            return None
        return vtkDataArrayToVTKArray(
            self.VTKObject.GetCellTypesArray(), self)

    def GetCellLocations(self):
        """Returns the cell locations as a VTKArray instance."""
        if not self.VTKObject.GetCellLocationsArray():
            return None
        return vtkDataArrayToVTKArray(
            self.VTKObject.GetCellLocationsArray(), self)

    def GetCells(self):
        """Returns the cells as a VTKArray instance."""
        if not self.VTKObject.GetCells():
            return None
        return vtkDataArrayToVTKArray(
            self.VTKObject.GetCells().GetData(), self)

    def SetCells(self, cellTypes, cellLocations, cells):
        """Given cellTypes, cellLocations, cells as VTKArrays,
        populates the unstructured grid data structures."""
        from vtk import VTK_ID_TYPE
        from vtk.vtkCommonDataModel import vtkCellArray
        cellTypes = numpyTovtkDataArray(cellTypes)
        cellLocations = numpyTovtkDataArray(cellLocations, array_type=VTK_ID_TYPE)
        cells = numpyTovtkDataArray(cells, array_type=VTK_ID_TYPE)
        ca = vtkCellArray()
        ca.SetCells(cellTypes.GetNumberOfTuples(), cells)
        self.VTKObject.SetCells(cellTypes, cellLocations, ca)

    CellTypes = property(GetCellTypes, None, None, "This property returns the types of cells.")
    CellLocations = property(GetCellLocations, None, None, "This property returns the locations of cells.")
    Cells = property(GetCells, None, None, "This property returns the connectivity of cells.")

def WrapDataObject(ds):
    """Returns a Numpy friendly wrapper of a vtkDataObject."""
    if ds.IsA("vtkPolyData"):
        return PolyData(ds)
    elif ds.IsA("vtkUnstructuredGrid"):
        return UnstructuredGrid(ds)
    elif ds.IsA("vtkPointSet"):
        return PointSet(ds)
    elif ds.IsA("vtkDataSet"):
        return DataSet(ds)
    elif ds.IsA("vtkCompositeDataSet"):
        return CompositeDataSet(ds)
    elif ds.IsA("vtkTable"):
        return Table(ds)


# script to create an animated plot from a simulation

# import data

# import packages
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

# +
# input

# data from a .txt file
file_data = "sin_expdecay.txt"

#set title and legth of the movie
title_movie = 'example3_20s_5dpi'
length_movie = 20 # in seconds

n_datapoint_per_interval = 5

# Decorate the plot
X_label = 'time [s]'
Y_label = 'Signal'
plot_title = 'Best plot ever!'

# +

def video_generator(file_data, X_label, Y_label, plot_title, movie_title, movie_length, n_datapoint_per_interval=1):
    """Create a video animation from a data set

    The video is saved in a .mp4 file in the same folder

    Parameters
    ----------
    file_data : .txt file
                file containing the data to plot, in the format of a np.array([x, y])
    X_label : str
              label of X axis
    Y_label : str
              label of y axis
    plot_title : str
                 title of the plot
    movie_title : str
                  name to give to the .mp4 file when saved
    movie_length : int
                   duration in seconds of the movie
    n_datapoint_per interval: int, optional
                              number of datapoints taken in each frame (default=1)
                              if n_datapoint_per interval = 1 : it takes as number of frames as the number of datapoints
                              if n_datapoint_per interval > 1 : there will be less frames,
                                                                the video will be less smooth
                                                                but it will take less time to create it
    """
    # load data
    [X,Y] = np.loadtxt(file_data)

    # set number of frames for creating the plot
    X_length = len(X)
    X_interval = X_length // n_datapoint_per_interval

    # set limits of the plot
    X_start = X[0]
    X_stop = X[-1]
    Y_gap = (np.amax(Y) - np.amin(Y)) * 0.1
    Y_start = np.amin(Y) - Y_gap
    Y_stop = np.amax(Y) + Y_gap

    # print(X_length, n_datapoint_per_interval, X_interval)

    # code with animation function of matplotlib

    # First set up the figure, the axis, and the plot element we want to animate
    fig = plt.figure()
    ax = plt.axes(xlim=(X_start, X_stop), ylim=(Y_start, Y_stop))
    ax.set_xlabel(X_label)
    ax.set_ylabel(Y_label)
    ax.set_title(plot_title)
    line, = ax.plot([], [], lw=2)

    # set movie_title
    title_movie = movie_title + '.mp4'

    # frames per second
    frames_per_seconds = (X_interval+1) // movie_length

    # initialization function: plot the background of each frame
    def init():
        line.set_data([], [])
        return line,

    # animation function.  This is called sequentially
    def animate(i):
        line.set_data(X[0:i*n_datapoint_per_interval], Y[0:i*n_datapoint_per_interval])
        return line,

    # call the animator.  blit=True means only re-draw the parts that have changed.
    anim = animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=X_interval+1, interval=200, blit=True)

    # save the animation as an mp4.  This requires ffmpeg or mencoder to be
    # installed.  The extra_args ensure that the x264 codec is used, so that
    # the video can be embedded in html5.  You may need to adjust this for
    # your system: for more information, see
    # http://matplotlib.sourceforge.net/api/animation_api.html
    anim.save(title_movie, fps=frames_per_seconds, extra_args=['-vcodec', 'libx264'])
    # fps: frames per second in the movie
    #      --> the higher the number of fps, the faster will be animation in the movie
    #       es. if frames = 1000 in FuncAnimation and fps = 100: the movie will last 10 seconds

    plt.show()
# -
video_generator(file_data, X_label, Y_label, plot_title, title_movie, length_movie, n_datapoint_per_interval)


from typing import Tuple, List

import streamlit as st
import plotly.graph_objects as go
import plotly.express as px
from plotly.express.colors import unlabel_rgb, label_rgb, n_colors, sequential
import numpy as np
from scipy.stats import norm

from ruins.core import Config, build_config, debug_view
from ruins.plotting import distribution_plot

_TRANSLATE_EN = dict(
    title='Uncertainty & Risk',
    introduction="""
This first playground app will illustrate how uncertainty and risk influence our everyday decisions.
It is quite important to understand the difference between [knightian uncertaintry](https://en.wikipedia.org/wiki/Knightian_uncertainty)
and risk at this simplified example, before we move to climate modeling and weather data.
Taking the whole earth system into account, these concepts apply and are of high importance,
but their interpretration is way more complicated.

DESCRIPTION OF THE SIMPLIFIED EXAMPLE
    """,
    event_1_desc="""
The first event has two possible outcomes, from which we know their expected return distributions,
but we lack knowledge about the probabilities of occurence. Use the slider below to adjust their
mean outcome and the deviations from this mean.
""",
    event_2_desc="""
In the first event, there was an uncalculatable uncertainty, as we can't predict the outcome. Instead of
throw the dice every time and risk being way off in terms of return, we can take a save route and make
an active decision for another event. The second event has only one outcome, from which we know that the
expected return is worse than the better outcome of the first event.
We trade off the posibility of very positive outcome at the cost of not being trapped into very bad outcomes.

But is that worth it?
"""
)

_TRANSLATE_DE = dict(
    title='Unsicherheit und Risiko',
    introduction="""
Dieses erste, vereinfachte Beispiel demonstriert wie sich Unsicherheit und Risiko auf
unsere alltäglichen Entscheidungen auswirkt. Es ist wichtig den Unterschied zwischen
[Knightsche Unsicherheit](https://de.wikipedia.org/wiki/Knightsche_Unsicherheit) anhand dieses stark vereinfachten
Beispiels zu erkunden und zu verstehen, bevor wir die Modelle und Daten der letzten Kapitel betrachten.
Betrachtet man das gesamte Erdsystem, sind Unsicherheit und Risiko für die Interpretierbarkeit der Daten
von fundamentaler Bedeutng, stellen sich jedoch in wesentlich komplexeren Zusammenhängen dar.

BESCHREIBUNG DES VEREINFACHTEN BEISPIELS
""",
    event_1_desc="""
Das erste Ereignis hat zwei verschiedene Ergebnisse. Für jedes kennen wir die drchschnittliche Erwartung und deren
Verteilung, allerdings haben wir keine Information über die Wahrscheinlichkeit, dass eines der Ergebnisse eintritt.
Benutze die Schieberegler um die Verteilungen der Ergebnisse anzupassen.
""",
    event_2_desc="""
Im ersten Ereignis mussten wir mit einer unbestimmbaren Unsicherheit umgehen, da wir das Ergebnis nicht vorhersagen konnten.
Anstatt hier die Würfel entscheiden zu lassen und ein schlechtes Ergebnis zu riskieren, können wir uns gänzlich
umentscheiden und Ereignis 2 eintreten lassen, das nur ein einziges Ergebnis hat. Allerdings ist das Ergebnis hier
schlechter als der bessere Ausgang des ersten Erignisses. Wir erkaufen uns die Sicherheit keine sehr schlechten Ergbnisse
zu haben damit, dass wir auch auf sehr positive Ergebnisse verzichten.

Aber lohnt sich das?
"""
)

def concept_explainer(config: Config, **kwargs):
    """Show an explanation, if it was not already shown.
    """
    # check if we saw the explainer already
    if config.has_key('uncertainty_playground_explainer'):
        return

    # get the container and a translation function
    container = kwargs['container'] if 'container' in kwargs else st
    t = config.translator(en=_TRANSLATE_EN, de=_TRANSLATE_DE)

    # place title and intro
    container.title(t('title'))
    container.markdown(t('introduction'), unsafe_allow_html=True)

    # check if the user wants to continue
    accept = container.button('WEITER' if config.lang == 'de' else 'CONTINUE')
    if accept:
        st.session_state.uncertainty_playground_explainer = True
        st.experimental_rerun()
    else:
        st.stop()

def _helper_plot(ev1: List[Tuple[float, float]], ev2: Tuple[float, float] = None, **kwargs) -> go.Figure:
    # create figure
    fig = go.Figure()

    # build the colorscale with enough colors
    cscale = getattr(sequential, kwargs.get('colorscale', 'Greens'))
    cmap = n_colors(unlabel_rgb(cscale[-1]), unlabel_rgb(cscale[-3]), len(ev1))

    # get a common x
    x = np.linspace(0, 10, 200)

    # iterate over all outcomes
    for i,outcome in enumerate(ev1):
        mu, std = outcome
        y = norm.pdf(x, loc=mu, scale=std)
        y_sum = y.sum()
        y /= y_sum

        # add the traces
        fig.add_trace(
            go.Scatter(x=x, y=y * 100, mode='lines', line=dict(color=label_rgb(cmap[i])), name=f'Outcome #{i + 1}', fill='tozerox')
        )
        fig.add_trace(
            go.Scatter(x=[mu, mu], y=[0, norm.pdf(mu, loc=mu, scale=std) / y_sum * 100], mode='lines', line=dict(color=label_rgb(cmap[i]), width=3, dash='dash'), name=f'Mean #{i + 1}')
        )

    # handle second event
    if ev2 is not None:
        mu, std = ev2
        y = norm.pdf(x, loc=mu, scale=std)
        y_sum = y.sum()
        y /= y_sum

        # add distribution
        fig.add_trace(
            go.Scatter(x=x, y=y * 100, mode='lines', line=dict(color='orange', width=2), name='Alternative Event', fill='tozerox')
        )
        # add mean
        fig.add_trace(
            go.Scatter(x=[mu, mu], y=[0, norm.pdf(mu, loc=mu, scale=std) / y_sum * 100], mode='lines', line=dict(color='orange', width=2, dash='dash'), name='Alternative Event mean value')
        )

    # adjust figure
    fig.update_layout(
        template='plotly_white',
        legend=dict(orientation='h')
    )

    return fig

def concept_graph(config: Config, expander_container=st.sidebar, **kwargs) -> go.Figure:
    """
    # TODO: document this
    """
    # get the container and translator
    container = kwargs['container'] if 'container' in kwargs else st
    t = config.translator(de=_TRANSLATE_DE, en=_TRANSLATE_EN)

    # ------------------------
    # First PDF
    if not config.has_key('concept_event_1'):
        container.markdown(t('event_1_desc'))
        l1, c1, r1 = container.columns(3)
        l2, c2, r2 = container.columns(3)

        # outcome 1
        l1.markdown('### Outcome 1')
        ou1_mu = c1.slider('Expected value of outcome #1', min_value=1., max_value=10., value=2.5)
        ou1_st = r1.slider('Certainty of outcome #1', min_value=0.1, max_value=3.0, value=0.5)

        # outcome 2
        l2.markdown('### Outcome 2')
        ou2_mu = c2.slider('Expected value of outcome #2', min_value=1., max_value=10., value=6.0)
        ou2_st = r2.slider('Certainty of outcome #2', min_value=0.1, max_value=3.0, value=0.4)

        ev1 = [(ou1_mu, ou1_st), (ou2_mu, ou2_st)]
        # add the continue button
        ev1_ok = container.button('WEITER' if config.lang=='de' else 'CONTINUE')
        if ev1_ok:
            st.session_state.concept_event_1 = ev1
            st.experimental_rerun()
        else:
            fig = distribution_plot({'outcomes': ev1, 'coloscale': 'Greens'})
            return fig
    else:
        ev1 = config['concept_event_1']
        ev1_new = []
        for i, out in enumerate(ev1):
            e = expander_container.expander(f'Outcome #{i + 1}', expanded=True)
            mu = e.slider(f'Expected value of outcome # {i + 1}', min_value=1., max_value=10., value=out[0])
            std = e.slider(f'Certainty of outcome # {i + 1}', min_value=0.1, max_value=2.0, value=out[1])
            ev1_new.append((mu, std, ))

    # ------------------------
    # add second event
    container.markdown(t('event_2_desc'))
    l, c, r = container.columns(3)

    # second event
    l.markdown('### Second event')
    e2_mu = c.slider('Expected value of alternative event', min_value=1., max_value=10., value=5.5)
    e2_st = r.slider('Certainty of alternative event', min_value=0.1, max_value=3.0, value=0.2)

    fig = distribution_plot({'outcomes': ev1_new, 'name': 'Original Event', 'colorscale': 'Greens'}, {'outcomes': [(e2_mu, e2_st)], 'name': 'Alternative Event', 'colorscale': 'Oranges'})
    return fig

def concept_playground(config: Config) -> None:
    """
    The concept playground demonstrates how knightian uncertainty
    is different from risk and how it influences everyday decisions.
    """
    # TODO: add the story mode stuff here

    # explainer
    concept_explainer(config)

    # show the graph

    fig = concept_graph(config)
    plot_area = st.empty()
    plot_area.plotly_chart(fig, use_container_width=True)

def main_app(**kwargs):
    """
    """
    # build the config and the dataManager from kwargs
    url_params = st.experimental_get_query_params()
    config, dataManager = build_config(url_params=url_params, **kwargs)

    # set page config and debug view
    st.set_page_config(page_title='Uncertainty Explorer', layout=config.layout)
    debug_view.debug_view(dataManager, config, debug_name='Initial Application State')

    # --------------------------
    # Main App

    # TODO: right now, we have only the playground here
    concept_playground(config)

if __name__ == '__main__':
    main_app()


"""
Feature extraction algorithms.

Each algorithm works on the HandwrittenData class. They have to be applied like
this:

>>> import hwrt.features
>>> from hwrt.handwritten_data import HandwrittenData
>>> data_json = '[[{"time": 123, "x": 45, "y": 67}]]'
>>> a = HandwrittenData(raw_data_id=2953, raw_data_json=data_json)
>>> feature_list = [StrokeCount(),
...                 ConstantPointCoordinates(strokes=4,
...                                          points_per_stroke=20,
...                                          fill_empty_with=0)]
>>> x = a.feature_extraction(feature_list)
"""

# Core Library modules
import abc
import logging
import sys
from itertools import combinations_with_replacement as combinations_wr
from typing import Any, Dict, List

# Third party modules
import numpy
from PIL import Image, ImageDraw

# Local modules
from . import geometry, handwritten_data, preprocessing, utils

logger = logging.getLogger(__name__)

def get_features(model_description_features: List[Dict[str, Any]]):
    """Get features from a list of dictionaries

    Parameters
    ----------
    model_description_features : List[Dict[str, Any]]

    Examples
    --------
    >>> l = [{'StrokeCount': None}, \
             {'ConstantPointCoordinates': \
              [{'strokes': 4}, \
               {'points_per_stroke': 81}, \
               {'fill_empty_with': 0}, \
               {'pen_down': False}] \
             } \
            ]
    >>> get_features(l)
    [StrokeCount, ConstantPointCoordinates
     - strokes: 4
     - points per stroke: 81
     - fill empty with: 0
     - pen down feature: False
     - pixel_env: 0
    ]
    """
    return utils.get_objectlist(
        model_description_features, config_key="features", module=sys.modules[__name__]
    )

def print_featurelist(feature_list: List):
    """
    Print the feature_list in a human-readable form.

    Parameters
    ----------
    feature_list : List
        feature objects
    """
    input_features = sum(n.get_dimension() for n in feature_list)
    print("## Features (%i)" % input_features)
    print("```")
    for algorithm in feature_list:
        print("* %s" % str(algorithm))
    print("```")

class Feature(metaclass=abc.ABCMeta):

    """Abstract class which defines which methods to implement for features."""

    @abc.abstractmethod
    def __call__(self, hwr_obj):
        """Get the features value for a given recording ``hwr_obj``."""
        assert isinstance(
            hwr_obj, handwritten_data.HandwrittenData
        ), "handwritten data is not of type HandwrittenData, but of %r" % type(hwr_obj)

    @abc.abstractmethod
    def get_dimension(self):
        """Return the length of the list which __call__ will return."""

# Only feature calculation classes follow

# Every feature class must have a __str__, __repr__ function so that error
# messages can help you to find and fix bugs in features.
# Every feature class must have a __call__ function which is used to get the
# features value(s) for a given recording.
# Every feature class must have a get_dimension function so that the total
# number of features can be calculated and checked for consistency.
#
# * __call__ must take exactly one argument of type HandwrittenData
# * __call__ must return a list of length get_dimension()
# * get_dimension must return a positive number
# * have a 'normalize' attribute that is either True or False

# Local features

class ConstantPointCoordinates(Feature):

    """Take the first ``points_per_stroke=20`` points coordinates of the first
       ``strokes=4`` strokes as features. This leads to
       :math:`2 \\cdot \\text{points_per_stroke} \\cdot \\text{strokes}`
       features.

       If ``points`` is set to 0, the first ``points_per_stroke`` point
       coordinates and the ``pen_down`` feature is used. This leads to
       :math:`3 \\cdot \\text{points_per_stroke}` features.

    Parameters
    ----------
    strokes : int
    points_per_stroke : int
    fill_empty_with : float
    pen_down : boolean
    pixel_env : int
        How big should the pixel map around the given point be?
    """

    normalize = False

    def __init__(
        self,
        strokes=4,
        points_per_stroke=20,
        fill_empty_with=0,
        pen_down=True,
        pixel_env=0,
        scaling_factor=32,
    ):
        self.strokes = strokes
        self.points_per_stroke = points_per_stroke
        self.fill_empty_with = fill_empty_with
        self.pen_down = pen_down
        self.pixel_env = pixel_env
        self.scaling_factor = scaling_factor

    def __repr__(self):
        return (
            "ConstantPointCoordinates\n"
            " - strokes: %i\n"
            " - points per stroke: %i\n"
            " - fill empty with: %i\n"
            " - pen down feature: %r\n"
            " - pixel_env: %i\n"
        ) % (
            self.strokes,
            self.points_per_stroke,
            self.fill_empty_with,
            self.pen_down,
            self.pixel_env,
        )

    def __str__(self):
        return (
            "constant point coordinates\n"
            " - strokes: %i\n"
            " - points per stroke: %i\n"
            " - fill empty with: %i\n"
            " - pen down feature: %r\n"
            " - pixel_env: %i\n"
        ) % (
            self.strokes,
            self.points_per_stroke,
            self.fill_empty_with,
            self.pen_down,
            self.pixel_env,
        )

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        if self.strokes > 0:
            if self.pixel_env > 0:
                return (
                    (2 + (1 + 2 * self.pixel_env) ** 2)
                    * self.strokes
                    * self.points_per_stroke
                )
            else:
                return 2 * self.strokes * self.points_per_stroke
        else:
            if self.pen_down:
                return 3 * self.points_per_stroke
            else:
                return 2 * self.points_per_stroke

    def _features_with_strokes(self, hwr_obj):
        """Calculate the ConstantPointCoordinates features for the case of
        a fixed number of strokes."""
        x = []
        img = Image.new(
            "L",
            (
                (int(hwr_obj.get_width() * self.scaling_factor) + 2),
                (int(hwr_obj.get_height() * self.scaling_factor) + 2),
            ),
            "black",
        )
        draw = ImageDraw.Draw(img, "L")
        pointlist = hwr_obj.get_pointlist()
        bb = hwr_obj.get_bounding_box()
        for stroke_nr in range(self.strokes):
            last_point = None
            # make sure that the current symbol actually has that many
            # strokes
            if stroke_nr < len(pointlist):
                for point_nr in range(self.points_per_stroke):
                    if point_nr < len(pointlist[stroke_nr]):
                        point = pointlist[stroke_nr][point_nr]
                        x.append(pointlist[stroke_nr][point_nr]["x"])
                        x.append(pointlist[stroke_nr][point_nr]["y"])
                        if last_point is None:
                            last_point = point
                        y_from = int(
                            (-bb["miny"] + last_point["y"]) * self.scaling_factor
                        )
                        x_from = int(
                            (-bb["minx"] + last_point["x"]) * self.scaling_factor
                        )
                        y_to = int((-bb["miny"] + point["y"]) * self.scaling_factor)
                        x_to = int((-bb["minx"] + point["x"]) * self.scaling_factor)
                        draw.line([x_from, y_from, x_to, y_to], fill="#ffffff", width=1)
                        if self.pixel_env > 0:
                            pix = img.load()
                            for x_offset in range(-self.pixel_env, self.pixel_env + 1):
                                for y_offset in range(
                                    -self.pixel_env, self.pixel_env + 1
                                ):
                                    xp = (
                                        int(
                                            (-bb["minx"] + point["x"])
                                            * self.scaling_factor
                                        )
                                        + x_offset
                                    )
                                    yp = (
                                        int(
                                            (-bb["miny"] + point["y"])
                                            * self.scaling_factor
                                        )
                                        + y_offset
                                    )
                                    xp = max(0, xp)
                                    yp = max(0, yp)
                                    x.append(pix[xp, yp])
                        last_point = point
                    else:
                        x.append(self.fill_empty_with)
                        x.append(self.fill_empty_with)
                        if self.pixel_env > 0:
                            for _ in range((1 + 2 * self.pixel_env) ** 2):
                                x.append(self.fill_empty_with)
            else:
                for _ in range(self.points_per_stroke):
                    x.append(self.fill_empty_with)
                    x.append(self.fill_empty_with)
                    if self.pixel_env > 0:
                        for _ in range((1 + 2 * self.pixel_env) ** 2):
                            x.append(self.fill_empty_with)
        del draw
        return x

    def _features_without_strokes(self, hwr_obj):
        """Calculate the ConstantPointCoordinates features for the case of
        a single (callapesed) stroke with pen_down features."""
        x = []
        for point in hwr_obj.get_pointlist()[0]:
            if len(x) >= 3 * self.points_per_stroke or (
                len(x) >= 2 * self.points_per_stroke and not self.pen_down
            ):
                break
            x.append(point["x"])
            x.append(point["y"])
            if self.pen_down:
                if "pen_down" not in point:
                    logger.error(
                        "The "
                        "ConstantPointCoordinates(strokes=0) "
                        "feature should only be used after "
                        "SpaceEvenly preprocessing step."
                    )
                else:
                    x.append(int(point["pen_down"]))
        if self.pen_down:
            while len(x) != 3 * self.points_per_stroke:
                x.append(self.fill_empty_with)
        else:
            while len(x) != 2 * self.points_per_stroke:
                x.append(self.fill_empty_with)
        return x

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        if self.strokes > 0:
            x = self._features_with_strokes(hwr_obj)
        else:
            x = self._features_without_strokes(hwr_obj)
        assert self.get_dimension() == len(
            x
        ), "Dimension of %s should be %i, but was %i" % (
            str(self),
            self.get_dimension(),
            len(x),
        )
        return x

class FirstNPoints(Feature):

    """Similar to the ``ConstantPointCoordinates`` feature, this feature takes
    the first ``n=81`` point coordinates. It also has the
    ``fill_empty_with=0`` to make sure that the dimension of this feature is
    always the same."""

    normalize = False

    def __init__(self, n=81):
        self.n = n

    def __repr__(self):
        return f"FirstNPoints\n - n: {self.n}\n"

    def __str__(self):
        return f"first n points\n - n: {self.n}\n"

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 2 * self.n

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        x = []
        pointlist = hwr_obj.get_pointlist()
        left = self.n
        for stroke in pointlist:
            for point in stroke:
                if left == 0:
                    break
                else:
                    left -= 1
                    x.append(point["x"])
                    x.append(point["y"])
        assert self.get_dimension() == len(
            x
        ), "Dimension of %s should be %i, but was %i" % (
            str(self),
            self.get_dimension(),
            len(x),
        )
        return x

# Global features

class Bitmap(Feature):

    """Get a fixed-size bitmap of the recording."""

    normalize = True

    def __init__(self, size=16):
        self.size = size

    def __repr__(self):
        return "Bitmap(%i x %i)" % (self.size, self.size)

    def __str__(self):
        return "Bitmap(%i x %i)" % (self.size, self.size)

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return self.size ** 2

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        feat = hwr_obj.get_bitmap(size=self.size).flatten()
        return list(feat)

class StrokeCount(Feature):

    """Stroke count as a 1 dimensional recording."""

    normalize = True

    def __repr__(self):
        return "StrokeCount"

    def __str__(self):
        return "stroke count"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        return [len(hwr_obj.get_pointlist())]

class Ink(Feature):

    """Ink as a 1 dimensional feature. It gives a numeric value for the amount
    of ink this would eventually have consumed.
    """

    normalize = True

    def __repr__(self):
        return "Ink"

    def __str__(self):
        return "ink"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        ink = 0.0
        # calculate ink used for this symbol
        # TODO: What about dots? What about speed?
        for stroke in hwr_obj.get_pointlist():
            last_point = None
            for point in stroke:
                if last_point is not None:
                    ink += preprocessing.euclidean_distance(last_point, point)
                last_point = point
        return [ink]

class AspectRatio(Feature):

    """Aspect ratio of a recording as a 1 dimensional feature."""

    normalize = True

    def __repr__(self):
        return "Aspect Ratio"

    def __str__(self):
        return "Aspect Ratio"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        width = float(hwr_obj.get_width() + 0.01)
        height = float(hwr_obj.get_height() + 0.01)
        return [width / height]

class Width(Feature):

    """Width of a recording as a 1 dimensional feature.

    .. note::

        This is the current width. So if the recording was scaled, this will
        not be the original width.
    """

    normalize = True

    def __repr__(self):
        return "Width"

    def __str__(self):
        return "Width"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        return [float(hwr_obj.get_width())]

class Height(Feature):

    """Height of a recording as a a 1 dimensional feature.

    .. note::

        This is the current hight. So if the recording was scaled, this will
        not be the original height.
    """

    normalize = True

    def __repr__(self):
        return "Height"

    def __str__(self):
        return "Height"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        return [float(hwr_obj.get_height())]

class Time(Feature):

    """The time in milliseconds it took to create the recording. This is a 1
    dimensional feature."""

    normalize = True

    def __repr__(self):
        return "Time"

    def __str__(self):
        return "Time"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        return [float(hwr_obj.get_time())]

class CenterOfMass(Feature):

    """Center of mass of a recording as a 2 dimensional feature."""

    normalize = True

    def __repr__(self):
        return "CenterOfMass"

    def __str__(self):
        return "Center of mass"

    def get_dimension(self):  # pylint: disable=R0201
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 2

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        xs = []
        ys = []
        for stroke in hwr_obj.get_pointlist():
            for point in stroke:
                xs.append(point["x"])
                ys.append(point["y"])
        return [float(sum(xs)) / len(xs), float(sum(ys)) / len(ys)]

class StrokeCenter(Feature):

    """Get the stroke center of mass coordinates as a 2 dimensional feature."""

    normalize = True

    def __init__(self, strokes=4):
        self.strokes = strokes

    def __repr__(self):
        return "StrokeCenter"

    def __str__(self):
        return "Stroke center"

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return self.strokes * 2

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        feature_vector = []
        for i, stroke in enumerate(hwr_obj.get_pointlist()):
            if i >= self.strokes:
                break
            xs = []
            ys = []
            for point in stroke:
                xs.append(point["x"])
                ys.append(point["y"])
            feature_vector.append(numpy.mean(xs))
            feature_vector.append(numpy.mean(ys))
        while len(feature_vector) < self.get_dimension():
            feature_vector.append(0)
        return feature_vector

class DouglasPeuckerPoints(Feature):

    """Get the number of points which are left after applying the Douglas
    Peucker line simplification algorithm.
    """

    normalize = True

    def __init__(self, epsilon=0.2):
        self.epsilon = epsilon

    def __repr__(self):
        return "DouglasPeuckerPoints"

    def __str__(self):
        return "DouglasPeucker Points"

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return 1

    def _stroke_simplification(self, pointlist):
        """The Douglas-Peucker line simplification takes a list of points as an
        argument. It tries to simplifiy this list by removing as many points
        as possible while still maintaining the overall shape of the stroke.
        It does so by taking the first and the last point, connecting them
        by a straight line and searchin for the point with the highest
        distance. If that distance is bigger than 'epsilon', the point is
        important and the algorithm continues recursively."""

        # Find the point with the biggest distance
        dmax = 0
        index = 0
        for i in range(1, len(pointlist)):
            d = geometry.perpendicular_distance(
                pointlist[i], pointlist[0], pointlist[-1]
            )
            if d > dmax:
                index = i
                dmax = d

        # If the maximum distance is bigger than the threshold 'epsilon', then
        # simplify the pointlist recursively
        if dmax >= self.epsilon:
            # Recursive call
            rec_results1 = self._stroke_simplification(pointlist[0:index])
            rec_results2 = self._stroke_simplification(pointlist[index:])
            result_list = rec_results1[:-1] + rec_results2
        else:
            result_list = [pointlist[0], pointlist[-1]]

        return result_list

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        dp_points = 0
        for stroke in hwr_obj.get_pointlist():
            points = self._stroke_simplification(stroke)
            dp_points += len(points)
        return [dp_points]

class StrokeIntersections(Feature):
    """Count the number of intersections which strokes in the recording have
       with each other in form of a symmetrical matrix for the first
       ``stroke=4`` strokes. The feature dimension is
       :math:`round(\\frac{\\text{strokes}^2}{2} + \\frac{\\text{strokes}}{2})`
       because the symmetrical part is discarded.

    =======   ======= ======= ======= ===
      -       stroke1 stroke2 stroke3
    -------   ------- ------- ------- ---
    stroke1     0        1      0     ...
    stroke2     1        2      0     ...
    stroke3     0        0      0     ...
    ...         ...      ...    ...   ...
    =======   ======= ======= ======= ===

    Returns values of upper triangular matrix (including diagonal)
    from left to right, top to bottom.

    ..warning

        This method has an error. It should probably not be used.
    """

    normalize = True

    def __init__(self, strokes=4):
        self.strokes = strokes

    def __repr__(self):
        return "StrokeIntersections"

    def __str__(self):
        return "StrokeIntersections"

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return int(round(float(self.strokes ** 2) / 2 + float(self.strokes) / 2))

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)

        pointlist = hwr_obj.get_pointlist()
        polygonal_chains = []

        # Make sure the dimension is correct
        for i in range(self.strokes):
            if i < len(pointlist):
                polygonal_chains.append(geometry.PolygonalChain(pointlist[i]))
            else:
                polygonal_chains.append(geometry.PolygonalChain([]))

        x = []
        for chainA, chainB in combinations_wr(polygonal_chains, 2):
            if chainA == chainB:
                x.append(chainA.count_selfintersections())
            else:
                x.append(chainA.count_intersections(chainB))

        assert self.get_dimension() == len(
            x
        ), "Dimension of %s should be %i, but was %i" % (
            str(self),
            self.get_dimension(),
            len(x),
        )
        return x

class ReCurvature(Feature):

    """Re-curvature is a 1 dimensional, stroke-global feature for a recording.
    It is the ratio
    :math:`\\frac{\\text{height}(s)}{\\text{length}(s)}`.
    If ``length(s) == 0``, then the re-curvature is defined to be 1.
    """

    normalize = True

    def __init__(self, strokes=4):
        assert strokes > 0, "This attribute has to be positive, but was %s" % str(
            strokes
        )
        self.strokes = strokes

    def __repr__(self):
        return "ReCurvature"

    def __str__(self):
        return "Re-curvature"

    def get_dimension(self):
        """Get the dimension of the returned feature. This equals the number
        of elements in the returned list of numbers."""
        return self.strokes

    def __call__(self, hwr_obj):
        super(self.__class__, self).__call__(hwr_obj)
        x = []
        for stroke in hwr_obj.get_pointlist():
            stroke_y = [point["y"] for point in stroke]
            height = max(stroke_y) - min(stroke_y)
            length = 0.0
            for last_point, point in zip(stroke, stroke[1:]):
                length += preprocessing.euclidean_distance(point, last_point)

            if length == 0:
                x.append(1)
            else:
                x.append(height / length)
            if len(x) == self.strokes:
                break
        while len(x) < self.strokes:
            x.append(0)
        assert self.get_dimension() == len(
            x
        ), "Dimension of %s should be %i, but was %i" % (
            str(self),
            self.get_dimension(),
            len(x),
        )
        return x


#!/usr/bin/env python3
"""Contains routines to test the solvers module"""

import os.path
import pytest
import numpy as np
import linsolver.solvers as solvers
import linsolver.io as io

ABSOLUTE_TOLERANCE = 1e-10
RELATIVE_TOLERANCE = 1e-10

TESTDATADIR = 'testdata'

TESTS = ['simple', 'needs_pivot', 'linearly_dependant', 'multiple']

def get_test_input(testname):
    "Reads the input for a given test."
    testinfile = os.path.join(TESTDATADIR, testname + '.in')
    aa, bb = io.read_input(testinfile)
    return aa, bb

def get_test_output(testname):
    "Reads the reference ouput for a given test."
    testoutfile = os.path.join(TESTDATADIR, testname + '.out')
    result = io.read_result(testoutfile)
    return result

@pytest.mark.parametrize("testname", TESTS)
def test_elimination(testname):
    "Tests elimination."
    aa, bb = get_test_input(testname)
    xx_expected = get_test_output(testname)
    if xx_expected is None:
        # Linear system of equations can not be solved -> expecting exception
        with pytest.raises(np.linalg.LinAlgError):
            solvers.gaussian_eliminate(aa, bb)
    else:
        xx_gauss = solvers.gaussian_eliminate(aa, bb)
        # Make sure, both vectors are row vectors so that they can be compared
        if len(xx_expected.shape) == 1:
            xx_expected.shape = (1, xx_expected.shape[0])
        xx_expected = xx_expected.transpose()
        xx_gauss.shape = (xx_gauss.shape[0], -1)
        assert np.allclose(xx_gauss, xx_expected, atol=ABSOLUTE_TOLERANCE,
                           rtol=RELATIVE_TOLERANCE)

if __name__ == '__main__':
    pytest.main()