<commit_before># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Miscellaneous algorithms for 2D contours and 3D triangularized meshes handling

  .. testsetup::
    # Change directory to provide relative paths for doctests
    >>> import os
    >>> filepath = os.path.dirname(os.path.realpath( __file__ ))
    >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))
    >>> os.chdir(datadir)

"""
from __future__ import division
from builtins import zip

import os
import os.path as op
from warnings import warn

import numpy as np
from numpy import linalg as nla

from .. import logging
from ..external.six import string_types
from ..interfaces.base import (BaseInterface, traits, TraitedSpec, File,
                               BaseInterfaceInputSpec)

iflogger = logging.getLogger('interface')

# Ensure that tvtk is loaded with the appropriate ETS_TOOLKIT env var
old_ets = os.getenv('ETS_TOOLKIT')
os.environ['ETS_TOOLKIT'] = 'null'
have_tvtk = False
try:
    from tvtk.api import tvtk
    have_tvtk = True
except ImportError:
    iflogger.warning('tvtk wasn\'t found')
finally:
    if old_ets is not None:
        os.environ['ETS_TOOLKIT'] = old_ets
    else:
        del os.environ['ETS_TOOLKIT']


def no_tvtk():
    global have_tvtk
    return not have_tvtk


class TVTKBaseInterface(BaseInterface):

    """ A base class for interfaces using VTK """

    _redirect_x = True
    _vtk_version = (4, 0, 0)

    def __init__(self, **inputs):
        if no_tvtk():
            raise ImportError('This interface requires tvtk to run.')

        try:
            from tvtk.tvtk_classes.vtk_version import vtk_build_version
            self._vtk_version = tuple([int(v) for v in vtk_build_version.split('.')])
        except ImportError:
            iflogger.warning(
                'VTK version-major inspection using tvtk failed, assuming VTK == 4.0.')
        super(TVTKBaseInterface, self).__init__(**inputs)

    def version(self):
        return self._vtk_version


class WarpPointsInputSpec(BaseInterfaceInputSpec):
    points = File(exists=True, mandatory=True,
                  desc='file containing the point set')
    warp = File(exists=True, mandatory=True,
                desc='dense deformation field to be applied')
    interp = traits.Enum('cubic', 'nearest', 'linear', usedefault=True,
                         mandatory=True, desc='interpolation')
    out_points = File(name_source='points', name_template='%s_warped',
                      output_name='out_points', keep_extension=True,
                      desc='the warped point set')


class WarpPointsOutputSpec(TraitedSpec):
    out_points = File(desc='the warped point set')


class WarpPoints(TVTKBaseInterface):

    """<commit_msg>improved VTK version parsing<commit_after># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Miscellaneous algorithms for 2D contours and 3D triangularized meshes handling

  .. testsetup::
    # Change directory to provide relative paths for doctests
    >>> import os
    >>> filepath = os.path.dirname(os.path.realpath( __file__ ))
    >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))
    >>> os.chdir(datadir)

"""
from __future__ import division
from builtins import zip

import os
import os.path as op
from warnings import warn

import numpy as np
from numpy import linalg as nla

from .. import logging
from ..external.six import string_types
from ..interfaces.base import (BaseInterface, traits, TraitedSpec, File,
                               BaseInterfaceInputSpec)

iflogger = logging.getLogger('interface')

# Ensure that tvtk is loaded with the appropriate ETS_TOOLKIT env var
old_ets = os.getenv('ETS_TOOLKIT')
os.environ['ETS_TOOLKIT'] = 'null'
have_tvtk = False
try:
    from tvtk.api import tvtk
    have_tvtk = True
except ImportError:
    iflogger.warning('tvtk wasn\'t found')
finally:
    if old_ets is not None:
        os.environ['ETS_TOOLKIT'] = old_ets
    else:
        del os.environ['ETS_TOOLKIT']


def no_tvtk():
    global have_tvtk
    return not have_tvtk


class TVTKBaseInterface(BaseInterface):

    """ A base class for interfaces using VTK """

    _redirect_x = True
    _vtk_version = (4, 0, 0)

    def __init__(self, **inputs):
        if no_tvtk():
            raise ImportError('This interface requires tvtk to run.')

        try:
            from tvtk.tvtk_classes.vtk_version import vtk_build_version
            vsplits = vtk_build_version.split('.')
            self._vtk_version = tuple([int(vsplits[0]), int(vsplits[1])] + vsplits[2:])
        except ImportError:
            iflogger.warning(
                'VTK version-major inspection using tvtk failed, assuming VTK == 4.0.')
        super(TVTKBaseInterface, self).__init__(**inputs)

    def version(self):
        return self._vtk_version


class WarpPointsInputSpec(BaseInterfaceInputSpec):
    points = File(exists=True, mandatory=True,
                  desc='file containing the point set')
    warp = File(exists=True, mandatory=True,
                desc='dense deformation field to be applied')
    interp = traits.Enum('cubic', 'nearest', 'linear', usedefault=True,
                         mandatory=True, desc='interpolation')
    out_points = File(name_source='points', name_template='%s_warped',
                      output_name='out_points', keep_extension=True,
                      desc='the warped point set')


class WarpPointsOutputSpec(TraitedSpec):
    out_points = File(desc='the warped point set')


class WarpPoints(TVTKBaseInterface):

    """
{'fraction_non_alphanumeric': 0.10500352360817478, 'fraction_numerical': 0.004404510218463707, 'mean_word_length': 3.049215406562054, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4118943', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1700, 'n_words': 473}