text:
"""
Core visualization operations based on PyVista.

Actual implementation of _Renderer and _Projection classes.
"""

# Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr>
#          Eric Larson <larson.eric.d@gmail.com>
#          Guillaume Favelier <guillaume.favelier@gmail.com>
#          Joan Massich <mailsik@gmail.com>
#
# License: Simplified BSD

from contextlib import contextmanager
from inspect import signature
import os
import re
import sys
import warnings

import numpy as np

from ._abstract import _AbstractRenderer, Figure3D
from ._utils import (_get_colormap_from_array, _alpha_blend_background,
                     ALLOWED_QUIVER_MODES, _init_mne_qtapp)
from ...fixes import _point_data, _cell_data, _compare_version
from ...transforms import apply_trans
from ...utils import (copy_base_doc_to_subclass_doc, _check_option,
                      _require_version, _validate_type, warn)

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    import pyvista
    from pyvista import Plotter, PolyData, Line, close_all, UnstructuredGrid
    try:
        from pyvistaqt import BackgroundPlotter  # noqa
    except ImportError:
        from pyvista import BackgroundPlotter
    from pyvista.plotting.plotting import _ALL_PLOTTERS

from vtkmodules.vtkCommonCore import (
    vtkCommand, vtkLookupTable, VTK_UNSIGNED_CHAR)
from vtkmodules.vtkCommonDataModel import VTK_VERTEX, vtkPiecewiseFunction
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkFiltersCore import vtkCellDataToPointData, vtkGlyph3D
from vtkmodules.vtkFiltersGeneral import (
    vtkTransformPolyDataFilter, vtkMarchingContourFilter)
from vtkmodules.vtkFiltersHybrid import vtkPolyDataSilhouette
from vtkmodules.vtkFiltersSources import (
    vtkSphereSource, vtkConeSource, vtkCylinderSource, vtkArrowSource,
    vtkPlatonicSolidSource, vtkGlyphSource2D)
from vtkmodules.vtkImagingCore import vtkImageReslice
from vtkmodules.vtkRenderingCore import (
    vtkMapper, vtkActor, vtkCellPicker, vtkColorTransferFunction,
    vtkPolyDataMapper, vtkVolume, vtkCoordinate, vtkDataSetMapper)
from vtkmodules.vtkRenderingVolumeOpenGL2 import vtkSmartVolumeMapper
from vtkmodules.util.numpy_support import numpy_to_vtk
try:
    from vtkmodules.vtkCommonCore import VTK_VERSION
except Exception:  # some bad versions of VTK
    VTK_VERSION = '9.0'

VTK9 = _compare_version(VTK_VERSION, '>=', '9.0')

_FIGURES = dict()

class PyVistaFigure(Figure3D):
    """PyVista-based 3D Figure.

    .. note:: This class should not be instantiated directly via
              ``mne.viz.PyVistaFigure(...)``. Instead, use
              :func:`mne.viz.create_3d_figure`.

    See Also
    --------
    mne.viz.create_3d_figure
    """

    def __init__(self):
        pass

    def _init(self, plotter=None, show=False, title='PyVista Scene',
              size=(600, 600), shape=(1, 1), background_color='black',
              smooth_shading=True, off_screen=False, notebook=False,
              splash=False, multi_samples=None):
        self._plotter = plotter
        self.display = None
        self.background_color = background_color
        self.smooth_shading = smooth_shading
        self.notebook = notebook
        self.title = title
        self.splash = splash

        self.store = dict()
        self.store['window_size'] = size
        self.store['shape'] = shape
        self.store['off_screen'] = off_screen
        self.store['border'] = False
        self.store['multi_samples'] = multi_samples

        if not self.notebook:
            self.store['show'] = show
            self.store['title'] = title
            self.store['auto_update'] = False
            self.store['menu_bar'] = False
            self.store['toolbar'] = False
            self.store['update_app_icon'] = False
            self._plotter_class = BackgroundPlotter
            if 'app_window_class' in signature(BackgroundPlotter).parameters:
                from ._qt import _MNEMainWindow
                self.store['app_window_class'] = _MNEMainWindow
        else:
            self._plotter_class = Plotter

        self._nrows, self._ncols = self.store['shape']
        self._azimuth = self._elevation = None

    def _build(self):
        if self.plotter is None:
            if not self.notebook:
                out = _init_mne_qtapp(
                    enable_icon=hasattr(self._plotter_class, 'set_icon'),
                    splash=self.splash)
                # replace it with the Qt object
                if self.splash:
                    self.splash = out[1]
                    app = out[0]
                else:
                    app = out
                self.store['app'] = app
            plotter = self._plotter_class(**self.store)
            plotter.background_color = self.background_color
            self._plotter = plotter
        if self.plotter.iren is not None:
            self.plotter.iren.initialize()
        _process_events(self.plotter)
        _process_events(self.plotter)
        return self.plotter

    def _is_active(self):
        if self.plotter is None:
            return False
        return hasattr(self.plotter, 'ren_win')

class _Projection(object):
    """Class storing projection information.

    Attributes
    ----------
    xy : array
        Result of 2d projection of 3d data.
    pts : None
        Scene sensors handle.
    """

    def __init__(self, *, xy, pts, plotter):
        """Store input projection information into attributes."""
        self.xy = xy
        self.pts = pts
        self.plotter = plotter

    def visible(self, state):
        """Modify visibility attribute of the sensors."""
        self.pts.SetVisibility(state)
        self.plotter.render()

@copy_base_doc_to_subclass_doc
class _PyVistaRenderer(_AbstractRenderer):
    """Class managing rendering scene.

    Attributes
    ----------
    plotter: Plotter
        Main PyVista access point.
    name: str
        Name of the window.
    """

    def __init__(self, fig=None, size=(600, 600), bgcolor='black',
                 name="PyVista Scene", show=False, shape=(1, 1),
                 notebook=None, smooth_shading=True, splash=False,
                 multi_samples=None):
        from .._3d import _get_3d_option
        _require_version('pyvista', 'use 3D rendering', '0.32')
        multi_samples = _get_3d_option('multi_samples')
        # multi_samples > 1 is broken on macOS + Intel Iris + volume rendering
        if sys.platform == 'darwin':
            multi_samples = 1
        figure = PyVistaFigure()
        figure._init(
            show=show, title=name, size=size, shape=shape,
            background_color=bgcolor, notebook=notebook,
            smooth_shading=smooth_shading, splash=splash,
            multi_samples=multi_samples)
        self.font_family = "arial"
        self.tube_n_sides = 20
        self.antialias = _get_3d_option('antialias')
        self.depth_peeling = _get_3d_option('depth_peeling')
        self.smooth_shading = smooth_shading
        if isinstance(fig, int):
            saved_fig = _FIGURES.get(fig)
            # Restore only active plotter
            if saved_fig is not None and saved_fig._is_active():
                self.figure = saved_fig
            else:
                self.figure = figure
                _FIGURES[fig] = self.figure
        elif fig is None:
            self.figure = figure
        else:
            self.figure = fig

        # Enable off_screen if sphinx-gallery or testing
        if pyvista.OFF_SCREEN:
            self.figure.store['off_screen'] = True

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            # pyvista theme may enable depth peeling by default so
            # we disable it initially to better control the value afterwards
            with _disabled_depth_peeling():
                self.plotter = self.figure._build()
            self._hide_axes()
            self._enable_antialias()
            self._enable_depth_peeling()

        # FIX: https://github.com/pyvista/pyvistaqt/pull/68
        if not hasattr(self.plotter, "iren"):
            self.plotter.iren = None

        self.update_lighting()

    @property
    def _all_plotters(self):
        if self.figure.plotter is not None:
            return [self.figure.plotter]
        else:
            return list()

    @property
    def _all_renderers(self):
        if self.figure.plotter is not None:
            return self.figure.plotter.renderers
        else:
            return list()

    def _hide_axes(self):
        for renderer in self._all_renderers:
            renderer.hide_axes()

    def _update(self):
        for plotter in self._all_plotters:
            plotter.update()

    def _index_to_loc(self, idx):
        _ncols = self.figure._ncols
        row = idx // _ncols
        col = idx % _ncols
        return (row, col)

    def _loc_to_index(self, loc):
        _ncols = self.figure._ncols
        return loc[0] * _ncols + loc[1]

    def subplot(self, x, y):
        x = np.max([0, np.min([x, self.figure._nrows - 1])])
        y = np.max([0, np.min([y, self.figure._ncols - 1])])
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            self.plotter.subplot(x, y)

    def scene(self):
        return self.figure

    def update_lighting(self):
        # Inspired from Mayavi's version of Raymond Maple 3-lights illumination
        for renderer in self._all_renderers:
            lights = list(renderer.GetLights())
            headlight = lights.pop(0)
            headlight.SetSwitch(False)
            # below and centered, left and above, right and above
            az_el_in = ((0, -45, 0.7), (-60, 30, 0.7), (60, 30, 0.7))
            for li, light in enumerate(lights):
                if li < len(az_el_in):
                    light.SetSwitch(True)
                    light.SetPosition(_to_pos(*az_el_in[li][:2]))
                    light.SetIntensity(az_el_in[li][2])
                else:
                    light.SetSwitch(False)
                    light.SetPosition(_to_pos(0.0, 0.0))
                    light.SetIntensity(0.0)
                light.SetColor(1.0, 1.0, 1.0)

    def set_interaction(self, interaction):
        if not hasattr(self.plotter, "iren") or self.plotter.iren is None:
            return
        if interaction == "rubber_band_2d":
            for renderer in self._all_renderers:
                renderer.enable_parallel_projection()
            if hasattr(self.plotter, 'enable_rubber_band_2d_style'):
                self.plotter.enable_rubber_band_2d_style()
            else:
                from vtkmodules.vtkInteractionStyle import\
                    vtkInteractorStyleRubberBand2D
                style = vtkInteractorStyleRubberBand2D()
                self.plotter.interactor.SetInteractorStyle(style)
        else:
            for renderer in self._all_renderers:
                renderer.disable_parallel_projection()
            kwargs = dict()
            if interaction == 'terrain':
                kwargs['mouse_wheel_zooms'] = True
            getattr(self.plotter, f'enable_{interaction}_style')(**kwargs)

    def legend(self, labels, border=False, size=0.1, face='triangle',
               loc='upper left'):
        return self.plotter.add_legend(
            labels, size=(size, size), face=face, loc=loc)

    def polydata(self, mesh, color=None, opacity=1.0, normals=None,
                 backface_culling=False, scalars=None, colormap=None,
                 vmin=None, vmax=None, interpolate_before_map=True,
                 representation='surface', line_width=1.,
                 polygon_offset=None, **kwargs):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            rgba = False
            if color is not None and len(color) == mesh.n_points:
                if color.shape[1] == 3:
                    scalars = np.c_[color, np.ones(mesh.n_points)]
                else:
                    scalars = color
                scalars = (scalars * 255).astype('ubyte')
                color = None
                rgba = True
            if isinstance(colormap, np.ndarray):
                if colormap.dtype == np.uint8:
                    colormap = colormap.astype(np.float64) / 255.
                from matplotlib.colors import ListedColormap
                colormap = ListedColormap(colormap)
            if normals is not None:
                _point_data(mesh)["Normals"] = normals
                mesh.GetPointData().SetActiveNormals("Normals")
            else:
                _compute_normals(mesh)
            if 'rgba' in kwargs:
                rgba = kwargs["rgba"]
                kwargs.pop('rgba')
            smooth_shading = self.smooth_shading
            if representation == 'wireframe':
                smooth_shading = False  # never use smooth shading for wf
            actor = _add_mesh(
                plotter=self.plotter,
                mesh=mesh, color=color, scalars=scalars, edge_color=color,
                rgba=rgba, opacity=opacity, cmap=colormap,
                backface_culling=backface_culling,
                rng=[vmin, vmax], show_scalar_bar=False,
                smooth_shading=smooth_shading,
                interpolate_before_map=interpolate_before_map,
                style=representation, line_width=line_width, **kwargs,
            )

            if polygon_offset is not None:
                mapper = actor.GetMapper()
                mapper.SetResolveCoincidentTopologyToPolygonOffset()
                mapper.SetRelativeCoincidentTopologyPolygonOffsetParameters(
                    polygon_offset, polygon_offset)

            return actor, mesh

    def mesh(self, x, y, z, triangles, color, opacity=1.0, shading=False,
             backface_culling=False, scalars=None, colormap=None,
             vmin=None, vmax=None, interpolate_before_map=True,
             representation='surface', line_width=1., normals=None,
             polygon_offset=None, **kwargs):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            vertices = np.c_[x, y, z].astype(float)
            triangles = np.c_[np.full(len(triangles), 3), triangles]
            mesh = PolyData(vertices, triangles)
        return self.polydata(
            mesh=mesh,
            color=color,
            opacity=opacity,
            normals=normals,
            backface_culling=backface_culling,
            scalars=scalars,
            colormap=colormap,
            vmin=vmin,
            vmax=vmax,
            interpolate_before_map=interpolate_before_map,
            representation=representation,
            line_width=line_width,
            polygon_offset=polygon_offset,
            **kwargs,
        )

    def contour(self, surface, scalars, contours, width=1.0, opacity=1.0,
                vmin=None, vmax=None, colormap=None,
                normalized_colormap=False, kind='line', color=None):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            if colormap is not None:
                colormap = _get_colormap_from_array(colormap,
                                                    normalized_colormap)
            vertices = np.array(surface['rr'])
            triangles = np.array(surface['tris'])
            n_triangles = len(triangles)
            triangles = np.c_[np.full(n_triangles, 3), triangles]
            mesh = PolyData(vertices, triangles)
            _point_data(mesh)['scalars'] = scalars
            contour = mesh.contour(isosurfaces=contours)
            line_width = width
            if kind == 'tube':
                contour = contour.tube(radius=width, n_sides=self.tube_n_sides)
                line_width = 1.0
            actor = _add_mesh(
                plotter=self.plotter,
                mesh=contour,
                show_scalar_bar=False,
                line_width=line_width,
                color=color,
                rng=[vmin, vmax],
                cmap=colormap,
                opacity=opacity,
                smooth_shading=self.smooth_shading,
            )
            return actor, contour

    def surface(self, surface, color=None, opacity=1.0,
                vmin=None, vmax=None, colormap=None,
                normalized_colormap=False, scalars=None,
                backface_culling=False, polygon_offset=None):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            normals = surface.get('nn', None)
            vertices = np.array(surface['rr'])
            triangles = np.array(surface['tris'])
            triangles = np.c_[np.full(len(triangles), 3), triangles]
            mesh = PolyData(vertices, triangles)
        colormap = _get_colormap_from_array(colormap, normalized_colormap)
        if scalars is not None:
            _point_data(mesh)['scalars'] = scalars
        return self.polydata(
            mesh=mesh,
            color=color,
            opacity=opacity,
            normals=normals,
            backface_culling=backface_culling,
            scalars=scalars,
            colormap=colormap,
            vmin=vmin,
            vmax=vmax,
            polygon_offset=polygon_offset,
        )

    def sphere(self, center, color, scale, opacity=1.0,
               resolution=8, backface_culling=False,
               radius=None):
        from vtkmodules.vtkFiltersSources import vtkSphereSource
        factor = 1.0 if radius is not None else scale
        center = np.array(center, dtype=float)
        if len(center) == 0:
            return None, None
        _check_option('center.ndim', center.ndim, (1, 2))
        _check_option('center.shape[-1]', center.shape[-1], (3,))
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            sphere = vtkSphereSource()
            sphere.SetThetaResolution(resolution)
            sphere.SetPhiResolution(resolution)
            if radius is not None:
                sphere.SetRadius(radius)
            sphere.Update()
            geom = sphere.GetOutput()
            mesh = PolyData(center)
            glyph = mesh.glyph(orient=False, scale=False,
                               factor=factor, geom=geom)
            actor = _add_mesh(
                self.plotter,
                mesh=glyph, color=color, opacity=opacity,
                backface_culling=backface_culling,
                smooth_shading=self.smooth_shading
            )
            return actor, glyph

    def tube(self, origin, destination, radius=0.001, color='white',
             scalars=None, vmin=None, vmax=None, colormap='RdBu',
             normalized_colormap=False, reverse_lut=False):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            cmap = _get_colormap_from_array(colormap, normalized_colormap)
            for (pointa, pointb) in zip(origin, destination):
                line = Line(pointa, pointb)
                if scalars is not None:
                    _point_data(line)['scalars'] = scalars[0, :]
                    scalars = 'scalars'
                    color = None
                else:
                    scalars = None
                tube = line.tube(radius, n_sides=self.tube_n_sides)
                actor = _add_mesh(
                    plotter=self.plotter,
                    mesh=tube,
                    scalars=scalars,
                    flip_scalars=reverse_lut,
                    rng=[vmin, vmax],
                    color=color,
                    show_scalar_bar=False,
                    cmap=cmap,
                    smooth_shading=self.smooth_shading,
                )
        return actor, tube

    def quiver3d(self, x, y, z, u, v, w, color, scale, mode, resolution=8,
                 glyph_height=None, glyph_center=None, glyph_resolution=None,
                 opacity=1.0, scale_mode='none', scalars=None, colormap=None,
                 backface_culling=False, line_width=2., name=None,
                 glyph_width=None, glyph_depth=None, glyph_radius=0.15,
                 solid_transform=None, *, clim=None):
        _check_option('mode', mode, ALLOWED_QUIVER_MODES)
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            factor = scale
            vectors = np.c_[u, v, w]
            points = np.vstack(np.c_[x, y, z])
            n_points = len(points)
            cell_type = np.full(n_points, VTK_VERTEX)
            cells = np.c_[np.full(n_points, 1), range(n_points)]
            args = (cells, cell_type, points)
            if not VTK9:
                args = (np.arange(n_points) * 3,) + args
            grid = UnstructuredGrid(*args)
            if scalars is None:
                scalars = np.ones((n_points,))
            _point_data(grid)['scalars'] = np.array(scalars)
            _point_data(grid)['vec'] = vectors
            if mode == '2darrow':
                return _arrow_glyph(grid, factor), grid
            elif mode == 'arrow':
                alg = _glyph(
                    grid,
                    orient='vec',
                    scalars='scalars',
                    factor=factor
                )
                mesh = pyvista.wrap(alg.GetOutput())
            else:
                tr = None
                if mode == 'cone':
                    glyph = vtkConeSource()
                    glyph.SetCenter(0.5, 0, 0)
                    if glyph_radius is not None:
                        glyph.SetRadius(glyph_radius)
                elif mode == 'cylinder':
                    glyph = vtkCylinderSource()
                    if glyph_radius is not None:
                        glyph.SetRadius(glyph_radius)
                elif mode == 'oct':
                    glyph = vtkPlatonicSolidSource()
                    glyph.SetSolidTypeToOctahedron()
                else:
                    assert mode == 'sphere', mode  # guaranteed above
                    glyph = vtkSphereSource()
                if mode == 'cylinder':
                    if glyph_height is not None:
                        glyph.SetHeight(glyph_height)
                    if glyph_center is not None:
                        glyph.SetCenter(glyph_center)
                    if glyph_resolution is not None:
                        glyph.SetResolution(glyph_resolution)
                    tr = vtkTransform()
                    tr.RotateWXYZ(90, 0, 0, 1)
                elif mode == 'oct':
                    if solid_transform is not None:
                        assert solid_transform.shape == (4, 4)
                        tr = vtkTransform()
                        tr.SetMatrix(
                            solid_transform.astype(np.float64).ravel())
                if tr is not None:
                    # fix orientation
                    glyph.Update()
                    trp = vtkTransformPolyDataFilter()
                    trp.SetInputData(glyph.GetOutput())
                    trp.SetTransform(tr)
                    glyph = trp
                glyph.Update()
                geom = glyph.GetOutput()
                mesh = grid.glyph(orient='vec', scale=scale_mode == 'vector',
                                  factor=factor, geom=geom)
            actor = _add_mesh(
                self.plotter,
                mesh=mesh,
                color=color,
                opacity=opacity,
                scalars=None,
                colormap=colormap,
                show_scalar_bar=False,
                backface_culling=backface_culling,
                clim=clim,
            )
        return actor, mesh

    def text2d(self, x_window, y_window, text, size=14, color='white',
               justification=None):
        size = 14 if size is None else size
        position = (x_window, y_window)
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            actor = self.plotter.add_text(text, position=position,
                                          font_size=size,
                                          color=color,
                                          viewport=True)
            if isinstance(justification, str):
                if justification == 'left':
                    actor.GetTextProperty().SetJustificationToLeft()
                elif justification == 'center':
                    actor.GetTextProperty().SetJustificationToCentered()
                elif justification == 'right':
                    actor.GetTextProperty().SetJustificationToRight()
                else:
                    raise ValueError('Expected values for `justification`'
                                     'are `left`, `center` or `right` but '
                                     'got {} instead.'.format(justification))
        _hide_testing_actor(actor)
        return actor

    def text3d(self, x, y, z, text, scale, color='white'):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            kwargs = dict(
                points=np.array([x, y, z]).astype(float),
                labels=[text],
                point_size=scale,
                text_color=color,
                font_family=self.font_family,
                name=text,
                shape_opacity=0,
            )
            if ('always_visible'
                    in signature(self.plotter.add_point_labels).parameters):
                kwargs['always_visible'] = True
            actor = self.plotter.add_point_labels(**kwargs)
        _hide_testing_actor(actor)
        return actor

    def scalarbar(self, source, color="white", title=None, n_labels=4,
                  bgcolor=None, **extra_kwargs):
        if isinstance(source, vtkMapper):
            mapper = source
        elif isinstance(source, vtkActor):
            mapper = source.GetMapper()
        else:
            mapper = None
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            kwargs = dict(color=color, title=title, n_labels=n_labels,
                          use_opacity=False, n_colors=256, position_x=0.15,
                          position_y=0.05, width=0.7, shadow=False, bold=True,
                          label_font_size=22, font_family=self.font_family,
                          background_color=bgcolor, mapper=mapper)
            kwargs.update(extra_kwargs)
            actor = self.plotter.add_scalar_bar(**kwargs)
        _hide_testing_actor(actor)
        return actor

    def show(self):
        self.plotter.show()

    def close(self):
        _close_3d_figure(figure=self.figure)

    def get_camera(self):
        return _get_3d_view(self.figure)

    def set_camera(self, azimuth=None, elevation=None, distance=None,
                   focalpoint='auto', roll=None, reset_camera=True,
                   rigid=None, update=True):
        _set_3d_view(self.figure, azimuth=azimuth, elevation=elevation,
                     distance=distance, focalpoint=focalpoint, roll=roll,
                     reset_camera=reset_camera, rigid=rigid, update=update)

    def reset_camera(self):
        self.plotter.reset_camera()

    def screenshot(self, mode='rgb', filename=None):
        return _take_3d_screenshot(figure=self.figure, mode=mode,
                                   filename=filename)

    def project(self, xyz, ch_names):
        xy = _3d_to_2d(self.plotter, xyz)
        xy = dict(zip(ch_names, xy))
        # pts = self.fig.children[-1]
        pts = self.plotter.renderer.GetActors().GetLastItem()

        return _Projection(xy=xy, pts=pts, plotter=self.plotter)

    def _enable_depth_peeling(self):
        if not self.depth_peeling:
            return
        if not self.figure.store['off_screen']:
            for renderer in self._all_renderers:
                renderer.enable_depth_peeling()

    def _enable_antialias(self):
        """Enable it everywhere except Azure."""
        if not self.antialias:
            return
        # XXX for some reason doing this on Azure causes access violations:
        #     ##[error]Cmd.exe exited with code '-1073741819'
        # So for now don't use it there. Maybe has to do with setting these
        # before the window has actually been made "active"...?
        # For Mayavi we have an "on activated" event or so, we should look into
        # using this for Azure at some point, too.
        if self.figure._is_active():
            # macOS, Azure
            bad_system = (
                sys.platform == 'darwin' or
                os.getenv('AZURE_CI_WINDOWS', 'false').lower() == 'true')
            bad_system |= _is_mesa(self.plotter)
            if not bad_system:
                for renderer in self._all_renderers:
                    renderer.enable_anti_aliasing()
            for plotter in self._all_plotters:
                plotter.ren_win.LineSmoothingOn()

    def remove_mesh(self, mesh_data):
        actor, _ = mesh_data
        self.plotter.remove_actor(actor)

    @contextmanager
    def _disabled_interaction(self):
        if not self.plotter.renderer.GetInteractive():
            yield
        else:
            self.plotter.disable()
            try:
                yield
            finally:
                self.plotter.enable()

    def _actor(self, mapper=None):
        actor = vtkActor()
        if mapper is not None:
            actor.SetMapper(mapper)
        _hide_testing_actor(actor)
        return actor

    def _process_events(self):
        for plotter in self._all_plotters:
            _process_events(plotter)

    def _update_picking_callback(self,
                                 on_mouse_move,
                                 on_button_press,
                                 on_button_release,
                                 on_pick):
        add_obs = self.plotter.iren.add_observer
        add_obs(vtkCommand.RenderEvent, on_mouse_move)
        add_obs(vtkCommand.LeftButtonPressEvent, on_button_press)
        add_obs(vtkCommand.EndInteractionEvent, on_button_release)
        self.plotter.picker = vtkCellPicker()
        self.plotter.picker.AddObserver(
            vtkCommand.EndPickEvent,
            on_pick
        )
        self.plotter.picker.SetVolumeOpacityIsovalue(0.)

    def _set_mesh_scalars(self, mesh, scalars, name):
        # Catch:  FutureWarning: Conversion of the second argument of
        # issubdtype from `complex` to `np.complexfloating` is deprecated.
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            _point_data(mesh)[name] = scalars

    def _set_colormap_range(self, actor, ctable, scalar_bar, rng=None,
                            background_color=None):
        if rng is not None:
            mapper = actor.GetMapper()
            mapper.SetScalarRange(*rng)
            lut = mapper.GetLookupTable()
            lut.SetTable(numpy_to_vtk(ctable))
        if scalar_bar is not None:
            lut = scalar_bar.GetLookupTable()
            if background_color is not None:
                background_color = np.array(background_color) * 255
                ctable = _alpha_blend_background(ctable, background_color)
            lut.SetTable(numpy_to_vtk(ctable, array_type=VTK_UNSIGNED_CHAR))
            lut.SetRange(*rng)

    def _set_volume_range(self, volume, ctable, alpha, scalar_bar, rng):
        color_tf = vtkColorTransferFunction()
        opacity_tf = vtkPiecewiseFunction()
        for loc, color in zip(np.linspace(*rng, num=len(ctable)), ctable):
            color_tf.AddRGBPoint(loc, *(color[:-1] / 255.))
            opacity_tf.AddPoint(loc, color[-1] * alpha / 255.)
        color_tf.ClampingOn()
        opacity_tf.ClampingOn()
        prop = volume.GetProperty()
        prop.SetColor(color_tf)
        prop.SetScalarOpacity(opacity_tf)
        prop.ShadeOn()
        prop.SetInterpolationTypeToLinear()
        if scalar_bar is not None:
            lut = vtkLookupTable()
            lut.SetRange(*rng)
            lut.SetTable(numpy_to_vtk(ctable))
            scalar_bar.SetLookupTable(lut)

    def _sphere(self, center, color, radius):
        from vtkmodules.vtkFiltersSources import vtkSphereSource
        sphere = vtkSphereSource()
        sphere.SetThetaResolution(8)
        sphere.SetPhiResolution(8)
        sphere.SetRadius(radius)
        sphere.SetCenter(center)
        sphere.Update()
        mesh = pyvista.wrap(sphere.GetOutput())
        actor = _add_mesh(
            self.plotter,
            mesh=mesh,
            color=color
        )
        return actor, mesh

    def _volume(self, dimensions, origin, spacing, scalars,
                surface_alpha, resolution, blending, center):
        # Now we can actually construct the visualization
        grid = pyvista.UniformGrid()
        grid.dimensions = dimensions + 1  # inject data on the cells
        grid.origin = origin
        grid.spacing = spacing
        _cell_data(grid)['values'] = scalars

        # Add contour of enclosed volume (use GetOutput instead of
        # GetOutputPort below to avoid updating)
        if surface_alpha > 0 or resolution is not None:
            grid_alg = vtkCellDataToPointData()
            grid_alg.SetInputDataObject(grid)
            grid_alg.SetPassCellData(False)
            grid_alg.Update()
        else:
            grid_alg = None

        if surface_alpha > 0:
            grid_surface = vtkMarchingContourFilter()
            grid_surface.ComputeNormalsOn()
            grid_surface.ComputeScalarsOff()
            grid_surface.SetInputData(grid_alg.GetOutput())
            grid_surface.SetValue(0, 0.1)
            grid_surface.Update()
            grid_mesh = vtkPolyDataMapper()
            grid_mesh.SetInputData(grid_surface.GetOutput())
        else:
            grid_mesh = None

        mapper = vtkSmartVolumeMapper()
        if resolution is None:  # native
            mapper.SetScalarModeToUseCellData()
            mapper.SetInputDataObject(grid)
        else:
            upsampler = vtkImageReslice()
            upsampler.SetInterpolationModeToLinear()  # default anyway
            upsampler.SetOutputSpacing(*([resolution] * 3))
            upsampler.SetInputConnection(grid_alg.GetOutputPort())
            mapper.SetInputConnection(upsampler.GetOutputPort())
        # Additive, AverageIntensity, and Composite might also be reasonable
        remap = dict(composite='Composite', mip='MaximumIntensity')
        getattr(mapper, f'SetBlendModeTo{remap[blending]}')()
        volume_pos = vtkVolume()
        volume_pos.SetMapper(mapper)
        dist = grid.length / (np.mean(grid.dimensions) - 1)
        volume_pos.GetProperty().SetScalarOpacityUnitDistance(dist)
        if center is not None and blending == 'mip':
            # We need to create a minimum intensity projection for the neg half
            mapper_neg = vtkSmartVolumeMapper()
            if resolution is None:  # native
                mapper_neg.SetScalarModeToUseCellData()
                mapper_neg.SetInputDataObject(grid)
            else:
                mapper_neg.SetInputConnection(upsampler.GetOutputPort())
            mapper_neg.SetBlendModeToMinimumIntensity()
            volume_neg = vtkVolume()
            volume_neg.SetMapper(mapper_neg)
            volume_neg.GetProperty().SetScalarOpacityUnitDistance(dist)
        else:
            volume_neg = None
        return grid, grid_mesh, volume_pos, volume_neg

    def _silhouette(self, mesh, color=None, line_width=None, alpha=None,
                    decimate=None):
        mesh = mesh.decimate(decimate) if decimate is not None else mesh
        silhouette_filter = vtkPolyDataSilhouette()
        silhouette_filter.SetInputData(mesh)
        silhouette_filter.SetCamera(self.plotter.renderer.GetActiveCamera())
        silhouette_filter.SetEnableFeatureAngle(0)
        silhouette_mapper = vtkPolyDataMapper()
        silhouette_mapper.SetInputConnection(
            silhouette_filter.GetOutputPort())
        actor, prop = self.plotter.add_actor(
            silhouette_mapper, reset_camera=False, name=None,
            culling=False, pickable=False, render=False)
        if color is not None:
            prop.SetColor(*color)
        if alpha is not None:
            prop.SetOpacity(alpha)
        if line_width is not None:
            prop.SetLineWidth(line_width)
        _hide_testing_actor(actor)
        return actor

def _compute_normals(mesh):
    """Patch PyVista compute_normals."""
    if 'Normals' not in _point_data(mesh):
        mesh.compute_normals(
            cell_normals=False,
            consistent_normals=False,
            non_manifold_traversal=False,
            inplace=True,
        )

def _add_mesh(plotter, *args, **kwargs):
    """Patch PyVista add_mesh."""
    mesh = kwargs.get('mesh')
    if 'smooth_shading' in kwargs:
        smooth_shading = kwargs.pop('smooth_shading')
    else:
        smooth_shading = True
    # disable rendering pass for add_mesh, render()
    # is called in show()
    if 'render' not in kwargs:
        kwargs['render'] = False
    actor = plotter.add_mesh(*args, **kwargs)
    if smooth_shading and 'Normals' in _point_data(mesh):
        prop = actor.GetProperty()
        prop.SetInterpolationToPhong()
    _hide_testing_actor(actor)
    return actor

def _hide_testing_actor(actor):
    from . import renderer
    if renderer.MNE_3D_BACKEND_TESTING:
        actor.SetVisibility(False)

def _deg2rad(deg):
    return deg * np.pi / 180.

def _rad2deg(rad):
    return rad * 180. / np.pi

def _to_pos(azimuth, elevation):
    theta = azimuth * np.pi / 180.0
    phi = (90.0 - elevation) * np.pi / 180.0
    x = np.sin(theta) * np.sin(phi)
    y = np.cos(phi)
    z = np.cos(theta) * np.sin(phi)
    return x, y, z

def _mat_to_array(vtk_mat):
    e = [vtk_mat.GetElement(i, j) for i in range(4) for j in range(4)]
    arr = np.array(e, dtype=float)
    arr.shape = (4, 4)
    return arr

def _3d_to_2d(plotter, xyz):
    # https://vtk.org/Wiki/VTK/Examples/Cxx/Utilities/Coordinate
    coordinate = vtkCoordinate()
    coordinate.SetCoordinateSystemToWorld()
    xy = list()
    for coord in xyz:
        coordinate.SetValue(*coord)
        xy.append(coordinate.GetComputedLocalDisplayValue(plotter.renderer))
    xy = np.array(xy, float).reshape(-1, 2)  # in case it's empty
    return xy

def _close_all():
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        close_all()
    _FIGURES.clear()

def _get_camera_direction(focalpoint, position):
    x, y, z = position - focalpoint
    r = np.sqrt(x * x + y * y + z * z)
    theta = np.arccos(z / r)
    phi = np.arctan2(y, x)
    return r, theta, phi

def _get_3d_view(figure):
    position = np.array(figure.plotter.camera_position[0])
    focalpoint = np.array(figure.plotter.camera_position[1])
    _, theta, phi = _get_camera_direction(focalpoint, position)
    azimuth, elevation = _rad2deg(phi), _rad2deg(theta)
    return (figure.plotter.camera.GetRoll(),
            figure.plotter.camera.GetDistance(),
            azimuth, elevation, focalpoint)

def _set_3d_view(figure, azimuth=None, elevation=None, focalpoint='auto',
                 distance=None, roll=None, reset_camera=True, rigid=None,
                 update=True):
    rigid = np.eye(4) if rigid is None else rigid
    position = np.array(figure.plotter.camera_position[0])
    bounds = np.array(figure.plotter.renderer.ComputeVisiblePropBounds())
    if reset_camera:
        figure.plotter.reset_camera(render=False)

    # focalpoint: if 'auto', we use the center of mass of the visible
    # bounds, if None, we use the existing camera focal point otherwise
    # we use the values given by the user
    if isinstance(focalpoint, str):
        _check_option('focalpoint', focalpoint, ('auto',),
                      extra='when a string')
        focalpoint = (bounds[1::2] + bounds[::2]) * 0.5
    elif focalpoint is None:
        focalpoint = np.array(figure.plotter.camera_position[1])
    else:
        focalpoint = np.asarray(focalpoint)

    # work in the transformed space
    position = apply_trans(rigid, position)
    focalpoint = apply_trans(rigid, focalpoint)
    _, theta, phi = _get_camera_direction(focalpoint, position)

    if azimuth is not None:
        phi = _deg2rad(azimuth)
    if elevation is not None:
        theta = _deg2rad(elevation)

    # set the distance
    if distance is None:
        distance = max(bounds[1::2] - bounds[::2]) * 2.0

    # Now calculate the view_up vector of the camera.  If the view up is
    # close to the 'z' axis, the view plane normal is parallel to the
    # camera which is unacceptable, so we use a different view up.
    if elevation is None or 5. <= abs(elevation) <= 175.:
        view_up = [0, 0, 1]
    else:
        view_up = [0, 1, 0]

    position = [
        distance * np.cos(phi) * np.sin(theta),
        distance * np.sin(phi) * np.sin(theta),
        distance * np.cos(theta)]

    figure._azimuth = _rad2deg(phi)
    figure._elevation = _rad2deg(theta)

    # restore to the original frame
    rigid = np.linalg.inv(rigid)
    position = apply_trans(rigid, position)
    focalpoint = apply_trans(rigid, focalpoint)
    view_up = apply_trans(rigid, view_up, move=False)
    figure.plotter.camera_position = [
        position, focalpoint, view_up]
    # We need to add the requested roll to the roll dictated by the
    # transformed view_up
    if roll is not None:
        figure.plotter.camera.SetRoll(figure.plotter.camera.GetRoll() + roll)

    if update:
        figure.plotter.update()
        _process_events(figure.plotter)

def _set_3d_title(figure, title, size=16):
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=FutureWarning)
        figure.plotter.add_text(title, font_size=size, color='white',
                                name='title')
    figure.plotter.update()
    _process_events(figure.plotter)

def _check_3d_figure(figure):
    _validate_type(figure, PyVistaFigure, 'figure')

def _close_3d_figure(figure):
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=FutureWarning)
        # copy the plotter locally because figure.plotter is modified
        plotter = figure.plotter
        # close the window
        plotter.close()  # additional cleaning following signal_close
        _process_events(plotter)
        # free memory and deregister from the scraper
        plotter.deep_clean()  # remove internal references
        _ALL_PLOTTERS.pop(plotter._id_name, None)
        _process_events(plotter)

def _take_3d_screenshot(figure, mode='rgb', filename=None):
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=FutureWarning)
        _process_events(figure.plotter)
        return figure.plotter.screenshot(
            transparent_background=(mode == 'rgba'),
            filename=filename)

def _process_events(plotter):
    if hasattr(plotter, 'app'):
        with warnings.catch_warnings(record=True):
            warnings.filterwarnings('ignore', 'constrained_layout')
            plotter.app.processEvents()

def _add_camera_callback(camera, callback):
    camera.AddObserver(vtkCommand.ModifiedEvent, callback)

def _arrow_glyph(grid, factor):
    glyph = vtkGlyphSource2D()
    glyph.SetGlyphTypeToArrow()
    glyph.FilledOff()
    glyph.Update()

    # fix position
    tr = vtkTransform()
    tr.Translate(0.5, 0., 0.)
    trp = vtkTransformPolyDataFilter()
    trp.SetInputConnection(glyph.GetOutputPort())
    trp.SetTransform(tr)
    trp.Update()

    alg = _glyph(
        grid,
        scale_mode='vector',
        scalars=False,
        orient='vec',
        factor=factor,
        geom=trp.GetOutputPort(),
    )
    mapper = vtkDataSetMapper()
    mapper.SetInputConnection(alg.GetOutputPort())
    return mapper

def _glyph(dataset, scale_mode='scalar', orient=True, scalars=True, factor=1.0,
           geom=None, tolerance=0.0, absolute=False, clamping=False, rng=None):
    if geom is None:
        arrow = vtkArrowSource()
        arrow.Update()
        geom = arrow.GetOutputPort()
    alg = vtkGlyph3D()
    alg.SetSourceConnection(geom)
    if isinstance(scalars, str):
        dataset.active_scalars_name = scalars
    if isinstance(orient, str):
        dataset.active_vectors_name = orient
        orient = True
    if scale_mode == 'scalar':
        alg.SetScaleModeToScaleByScalar()
    elif scale_mode == 'vector':
        alg.SetScaleModeToScaleByVector()
    else:
        alg.SetScaleModeToDataScalingOff()
    if rng is not None:
        alg.SetRange(rng)
    alg.SetOrient(orient)
    alg.SetInputData(dataset)
    alg.SetScaleFactor(factor)
    alg.SetClamping(clamping)
    alg.Update()
    return alg

@contextmanager
def _disabled_depth_peeling():
    try:
        from pyvista import global_theme
    except Exception:  # workaround for older PyVista
        from pyvista import rcParams
        depth_peeling = rcParams['depth_peeling']
    else:
        depth_peeling = global_theme.depth_peeling
    depth_peeling_enabled = depth_peeling["enabled"]
    depth_peeling["enabled"] = False
    try:
        yield
    finally:
        depth_peeling["enabled"] = depth_peeling_enabled

def _is_mesa(plotter):
    # MESA (could use GPUInfo / _get_gpu_info here, but it takes
    # > 700 ms to make a new window + report capabilities!)
    # CircleCI's is: "Mesa 20.0.8 via llvmpipe (LLVM 10.0.0, 256 bits)"
    gpu_info = plotter.ren_win.ReportCapabilities()
    gpu_info = re.findall("OpenGL renderer string:(.+)\n", gpu_info)
    gpu_info = ' '.join(gpu_info).lower()
    is_mesa = 'mesa' in gpu_info.split()
    if is_mesa:
        # Try to warn if it's ancient
        version = re.findall("mesa ([0-9.]+) .*", gpu_info)
        if version:
            version = version[0]
            if _compare_version(version, '<', '18.3.6'):
                warn(f'Mesa version {version} is too old for translucent 3D '
                     'surface rendering, consider upgrading to 18.3.6 or '
                     'later.')
        else:
            raise RuntimeError
    return is_mesa


text:
#include "tensorflow/compiler/tf2xla/type_util.h"
#include "tensorflow/compiler/tf2xla/xla_helpers.h"
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/array4d.h"
#include "tensorflow/core/framework/kernel_def_builder.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/lib/math/math_util.h"

namespace tensorflow {
namespace {

// We implement bilinear interpolation by upsampling followed by convolution.
// The basic idea is as follows. To scale from NxN to RxR:
//
//    1. S := (N - 1) /  gcd(N-1, R-1)
//    2. k := (R - 1) /  gcd(N-1, R-1)
//    3. Convolution(kxk, stride=S, lhs_dilation=k, padding=k-1)
//
// For example, to Scale from 7x7 -> 15x15:
//
//    1. S := (7-1) / gcd(7-1, 15-1) = 6 / gcd(6, 14) = 6 / 2 = 3
//    2. k := (15 - 1) / gcd(7-1, 15-1) = 14 / gcd(6, 14) = 14 / 2 = 7
//    3. Convolution(7x7, stride=3, lhs_dilation=3, padding=2)
//
//
// The 7x7 -> 15x15 case is much too large to write out in full as an
// example. The smallest interesting example is 3x3 -> 4x4.
//
// S := 2
// k := 3
//
// 00 03 06    00 00 00 00 00 00 00 00 00 00 00      00 02 04 06
// 09 12 15 -> 00 00 00 00 00 00 00 00 00 00 00   -> 06 08 10 12
// 18 21 24    00 00 00 00 00 03 00 00 06 00 00      12 14 16 18
//             00 00 00 00 00 00 00 00 00 00 00      18 20 22 24
//             00 00 00 00 00 00 00 00 00 00 00
//             00 00 09 00 00 12 00 00 15 00 00
//             00 00 00 00 00 00 00 00 00 00 00
//             00 00 00 00 00 00 00 00 00 00 00
//             00 00 18 00 00 21 00 00 24 00 00
//             00 00 00 00 00 00 00 00 00 00 00
//             00 00 00 00 00 00 00 00 00 00 00
//
// with the following convolutional kernel, with stride [2, 2]:
//       1 2 3 2 1
//       2 4 6 4 2
// 1/9 * 3 6 9 6 3
//       2 4 6 4 2
//       1 2 3 2 1

// Computes the size of the convolutional kernel and stride to use when resizing
// from in_size to out_size.
struct ResizeConvolutionDims {
  // Size of the kernel to use.
  std::vector<int64> kernel_size;

  // Stride of the convolution to use.
  std::vector<int64> stride;
};
ResizeConvolutionDims ComputeResizeConvolutionParameters(
    gtl::ArraySlice<int64> in_size, gtl::ArraySlice<int64> out_size) {
  CHECK_EQ(in_size.size(), out_size.size());
  int num_spatial_dims = in_size.size();
  ResizeConvolutionDims dims;
  dims.kernel_size.resize(num_spatial_dims);
  dims.stride.resize(num_spatial_dims);
  for (int i = 0; i < num_spatial_dims; ++i) {
    if (in_size[i] == 1) {
      // We must handle input size 1 specially because XLA convolution does
      // not allow stride 0.
      dims.stride[i] = dims.kernel_size[i] = 1;
    } else if (out_size[i] == 1) {
      // If in_size[i] > 1 but out_size[i] == 1, then we slice out the first
      // entry before resizing.
      dims.stride[i] = dims.kernel_size[i] = 1;
    } else {
      int64 gcd = MathUtil::GCD(static_cast<uint64>(in_size[i] - 1),
                                static_cast<uint64>(out_size[i] - 1));
      dims.stride[i] = (in_size[i] - 1) / gcd;
      dims.kernel_size[i] = (out_size[i] - 1) / gcd;
    }
  }
  return dims;
}

xla::ComputationDataHandle MakeBilinearResizeKernel(
    xla::ComputationBuilder* builder, gtl::ArraySlice<int64> kernel_size,
    int64 channels) {
  // Form a 2D convolution kernel like:
  //       1 2 3 2 1
  //       2 4 6 4 2
  // 1/9 * 3 6 9 6 3
  //       2 4 6 4 2
  //       1 2 3 2 1
  // by multiplying two 1D kernels of the form:
  // 1/3 * [1 2 3 2 1]
  auto make_1d_kernel = [](int64 n) {
    std::vector<float> kernel(n * 2 - 1);
    for (int64 i = 0; i < n; ++i) {
      float v = i + 1;
      kernel[i] = v;
      kernel[n * 2 - 2 - i] = v;
    }
    return kernel;
  };

  // Form a block diagonal kernel where each channel interacts only with itself.
  xla::Array4D<float> diag(1, 1, channels, channels, 0.0f);
  for (int i = 0; i < channels; ++i) {
    diag(0, 0, i, i) = 1.0f / (kernel_size[0] * kernel_size[1]);
  }
  return builder->Mul(
      builder->ConstantR1<float>(make_1d_kernel(kernel_size[0])),
      builder->Mul(builder->ConstantR1<float>(make_1d_kernel(kernel_size[1])),
                   builder->ConstantR4FromArray4D(diag),
                   /*broadcast_dimensions=*/{1}),
      /*broadcast_dimensions=*/{0});
}

class ResizeBilinearOp : public XlaOpKernel {
 public:
  explicit ResizeBilinearOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
    OP_REQUIRES_OK(ctx, ctx->GetAttr("align_corners", &align_corners_));
    OP_REQUIRES(
        ctx, align_corners_ == true,
        errors::Unimplemented(
            "ResizeBilinear with align_corners=False is not yet implemented"));
  }

  void Compile(XlaOpKernelContext* ctx) override {
    xla::ComputationBuilder* b = ctx->builder();

    TensorShape input_shape = ctx->InputShape(0);
    OP_REQUIRES(ctx, input_shape.dims() == 4,
                errors::InvalidArgument("input must be 4-dimensional",
                                        input_shape.DebugString()));
    const int64 batch = input_shape.dim_size(0);
    const std::vector<int64> in_size = {input_shape.dim_size(1),
                                        input_shape.dim_size(2)};
    const int64 channels = input_shape.dim_size(3);
    OP_REQUIRES(ctx, in_size[0] > 0 && in_size[1] > 0,
                errors::InvalidArgument("input size must be positive, got [",
                                        in_size[0], ",", in_size[1], "]"));

    std::vector<int64> out_size;
    OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(1, &out_size));
    OP_REQUIRES(ctx, out_size.size() == 2,
                errors::InvalidArgument("output size must be length 2, got ",
                                        out_size.size()));
    OP_REQUIRES(ctx, out_size[0] > 0 && out_size[1] > 0,
                errors::InvalidArgument("output size must be positive, got [",
                                        out_size[0], ",", out_size[1], "]"));

    const int num_spatial_dims = 2;

    xla::ComputationDataHandle input = ctx->Input(0);

    // If in_size[i] > 1 and out_size[i] == 1, slice out the first input in
    // dimension i.
    std::vector<int64> slice_size = in_size;
    bool slice_input = false;
    for (int i = 0; i < num_spatial_dims; ++i) {
      if (in_size[i] > 1 && out_size[i] == 1) {
        // If in_size[i] > 1 but out_size[i] == 1, then we slice out the first
        // entry before resizing.
        slice_input = true;
        slice_size[i] = 1;
      }
    }
    if (slice_input) {
      input = b->Slice(input, {0, 0, 0, 0},
                       {batch, slice_size[0], slice_size[1], channels},
                       {1, 1, 1, 1});
    }

    // Output is always type float.
    input = b->ConvertElementType(input, xla::F32);

    // Picture for a 1x3 to 1x4 resize:
    // stride = 2, kernel size = 3
    // Input:
    // 3 6 9
    // Input with dilation and padding:
    // 0 0 3 0 0 6 0 0 9 0 0
    // Convolution kernel:
    // 1/3 * [1 2 3 2 1]
    // Output:
    // 3 5 7 9
    xla::ConvolutionDimensionNumbers dnums;
    dnums.set_input_batch_dimension(0);
    dnums.set_output_batch_dimension(0);
    dnums.set_input_feature_dimension(3);
    dnums.set_output_feature_dimension(3);
    for (int i = 0; i < num_spatial_dims; ++i) {
      dnums.add_input_spatial_dimensions(1 + i);
      dnums.add_output_spatial_dimensions(1 + i);
      dnums.add_kernel_spatial_dimensions(i);
    }
    dnums.set_kernel_input_feature_dimension(num_spatial_dims);
    dnums.set_kernel_output_feature_dimension(num_spatial_dims + 1);

    ResizeConvolutionDims dims =
        ComputeResizeConvolutionParameters(in_size, out_size);
    xla::ComputationDataHandle kernel =
        MakeBilinearResizeKernel(b, dims.kernel_size, channels);
    xla::ComputationDataHandle output = b->ConvGeneralDilated(
        input, kernel, dims.stride,
        /*padding=*/
        {{dims.kernel_size[0] - 1, dims.kernel_size[0] - 1},
         {dims.kernel_size[1] - 1, dims.kernel_size[1] - 1}},
        /*lhs_dilation=*/dims.kernel_size,
        /*rhs_dilation=*/{1, 1}, dnums);

    // Add broadcasts to handle expanding from a size == 1 dimension to a
    // size > 1 dimension.
    for (int i = 0; i < num_spatial_dims; ++i) {
      if (in_size[i] == 1 && out_size[i] > 1) {
        output = b->Add(output, b->ConstantR1<float>(out_size[i], 0),
                        /*broadcast_dimensions=*/{1 + i});
      }
    }

    ctx->SetOutput(0, output);
  }

 private:
  bool align_corners_;
};

REGISTER_XLA_OP(Name("ResizeBilinear").CompileTimeConstInput("size"),
                ResizeBilinearOp);

class ResizeBilinearGradOp : public XlaOpKernel {
 public:
  explicit ResizeBilinearGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
    OP_REQUIRES_OK(ctx, ctx->GetAttr("align_corners", &align_corners_));
    OP_REQUIRES(
        ctx, align_corners_ == true,
        errors::Unimplemented("ResizeBilinearGrad with align_corners=False is "
                              "not yet implemented"));

    DataType output_dtype;
    OP_REQUIRES_OK(ctx, ctx->GetAttr("T", &output_dtype));
    OP_REQUIRES_OK(ctx, DataTypeToPrimitiveType(output_dtype, &output_type_));
  }

  void Compile(XlaOpKernelContext* ctx) override {
    xla::ComputationBuilder* b = ctx->builder();

    TensorShape input_shape = ctx->InputShape(1);
    OP_REQUIRES(ctx, input_shape.dims() == 4,
                errors::InvalidArgument("input must be 4-dimensional",
                                        input_shape.DebugString()));
    const int64 batch = input_shape.dim_size(0);
    const std::vector<int64> in_size = {input_shape.dim_size(1),
                                        input_shape.dim_size(2)};
    const int64 channels = input_shape.dim_size(3);
    OP_REQUIRES(ctx, in_size[0] > 0 && in_size[1] > 0,
                errors::InvalidArgument("input size must be positive, got [",
                                        in_size[0], ",", in_size[1], "]"));

    TensorShape grad_shape = ctx->InputShape(0);
    OP_REQUIRES(ctx, grad_shape.dims() == 4,
                errors::InvalidArgument("gradient must be 4-dimensional",
                                        grad_shape.DebugString()));
    const int64 grad_batch = grad_shape.dim_size(0);
    const std::vector<int64> grad_size = {grad_shape.dim_size(1),
                                          grad_shape.dim_size(2)};
    const int64 grad_channels = grad_shape.dim_size(3);
    OP_REQUIRES(ctx, batch == grad_batch,
                errors::InvalidArgument(
                    "activations and gradients must have the same batch size (",
                    batch, " vs. ", grad_batch, ")"));
    OP_REQUIRES(ctx, grad_size[0] > 0 && grad_size[1] > 0,
                errors::InvalidArgument("gradient size must be positive, got [",
                                        grad_size[0], ",", grad_size[1], "]"));
    OP_REQUIRES(
        ctx, channels == grad_channels,
        errors::InvalidArgument(
            "activations and gradients must have the same number of channels (",
            channels, " vs. ", grad_channels, ")"));

    const int num_spatial_dims = 2;

    xla::ComputationDataHandle grad = ctx->Input(0);

    ResizeConvolutionDims dims =
        ComputeResizeConvolutionParameters(in_size, grad_size);

    // To form the backward convolution, we keep the kernel unchanged (it is
    // already symmetric) and swap the roles of strides and LHS dilation.
    xla::ConvolutionDimensionNumbers dnums;
    dnums.set_input_batch_dimension(0);
    dnums.set_output_batch_dimension(0);
    dnums.set_input_feature_dimension(3);
    dnums.set_output_feature_dimension(3);
    for (int i = 0; i < num_spatial_dims; ++i) {
      dnums.add_input_spatial_dimensions(1 + i);
      dnums.add_output_spatial_dimensions(1 + i);
      dnums.add_kernel_spatial_dimensions(i);
    }
    dnums.set_kernel_input_feature_dimension(num_spatial_dims);
    dnums.set_kernel_output_feature_dimension(num_spatial_dims + 1);
    xla::ComputationDataHandle kernel =
        MakeBilinearResizeKernel(b, dims.kernel_size, channels);

    // Broadcast the input kernel where the forward op expanded from a size == 1
    // dimension to a size > 1 dimension. This has the effect of summing the
    // gradient contributions in that dimension.
    for (int i = 0; i < num_spatial_dims; ++i) {
      if (in_size[i] == 1 && grad_size[i] > 1) {
        kernel = b->Add(kernel, b->ConstantR1<float>(grad_size[i], 0),
                        /*broadcast_dimensions=*/{i});
      }
    }

    xla::ComputationDataHandle output = b->ConvGeneralDilated(
        grad, kernel, /*window_strides=*/dims.kernel_size,
        /*padding=*/
        {{dims.kernel_size[0] - 1, dims.kernel_size[0] - 1},
         {dims.kernel_size[1] - 1, dims.kernel_size[1] - 1}},
        /*lhs_dilation=*/dims.stride,
        /*rhs_dilation=*/{1, 1}, dnums);

    // If in_size[i] > 1 and grad_size[i] == 1, pad the output in dimension i.
    // Opposite of the slice performed by the forward op.
    xla::PaddingConfig padding = xla::MakeNoPaddingConfig(4);
    bool pad_output = false;
    for (int i = 0; i < num_spatial_dims; ++i) {
      if (in_size[i] > 1 && grad_size[i] == 1) {
        pad_output = true;
        padding.mutable_dimensions(1 + i)->set_edge_padding_high(in_size[i] -
                                                                 1);
      }
    }
    if (pad_output) {
      output = b->Pad(output, b->ConstantR0<float>(0.0f), padding);
    }

    output = b->ConvertElementType(output, output_type_);
    ctx->SetOutput(0, output);
  }

 private:
  bool align_corners_;
  xla::PrimitiveType output_type_;
};

REGISTER_XLA_OP(Name("ResizeBilinearGrad"), ResizeBilinearGradOp);

}  // namespace
}  // namespace tensorflow

redpajama_set_name: RedPajamaGithub


text:
[![Build Status](https://travis-ci.org/jlareau/HASS.svg?branch=master)](https://travis-ci.org/jlareau/HASS)
[![GitHub issues](https://img.shields.io/github/issues/jlareau/HASS.svg)](https://github.com/jlareau/HASS/issues)
My Home Assistant configuration


text:
/**
 * @author Gregory Shimansky
 */
/*
 * JVMTI extensions API
 */

#include "jvmti_internal.h"
#include "jvmti_utils.h"
#include "open/vm_util.h"
#include "cxxlog.h"
#include "suspend_checker.h"

struct JvmtiExtension
{
    JvmtiExtension *next;
    jvmtiExtensionFunctionInfo info;
};

static jvmtiParamInfo jvmtiGetNCAIEnvironmentParams[] =
{
    {
        const_cast<char*>("ncai_env_ptr"),
        JVMTI_KIND_OUT,
        JVMTI_TYPE_CVOID,
        JNI_FALSE
    },
    {
        const_cast<char*>("version"),
        JVMTI_KIND_IN,
        JVMTI_TYPE_JINT,
        JNI_FALSE
    }
};

static jvmtiError jvmtiGetNCAIEnvironmentErrors[] =
{ // Universal errors are excluded according to specification
    JVMTI_ERROR_NONE,
};

static JvmtiExtension jvmti_extension_list[] =
{
    {
        NULL,
        {
            jvmtiGetNCAIEnvironment,
            const_cast<char*>("org.apache.harmony.vm.GetExtensionEnv"),
            const_cast<char*>("Returns the reference to the NCAI function table"),
            sizeof(jvmtiGetNCAIEnvironmentParams) / sizeof(jvmtiParamInfo),
            jvmtiGetNCAIEnvironmentParams,
            sizeof(jvmtiGetNCAIEnvironmentErrors) / sizeof(jvmtiError),
            jvmtiGetNCAIEnvironmentErrors
        }
    }

};

static const jint extensions_number = sizeof(jvmti_extension_list) / sizeof(JvmtiExtension);

static void free_allocated_extension_array(jvmtiExtensionFunctionInfo *array,
                                           jint number)
{
    for (int iii = 0; iii <= number; iii++)
    {
        if (array[iii].id)
            _deallocate((unsigned char *)array[iii].id);
        if (array[iii].short_description)
            _deallocate((unsigned char *)array[iii].short_description);
        if (array[iii].errors)
            _deallocate((unsigned char *)array[iii].errors);

        if (array[iii].params)
        {
            for (int jjj = 0; jjj < array[iii].param_count; jjj++)
            {
                if (array[iii].params[jjj].name)
                    _deallocate((unsigned char *)array[iii].params[jjj].name);
            }

            _deallocate((unsigned char *)array[iii].params);
        }
    }
    _deallocate((unsigned char *)array);
}

/*
 * Get Extension Functions
 *
 * Returns the set of extension functions.
 *
 * REQUIRED Functionality.
 */
jvmtiError JNICALL
jvmtiGetExtensionFunctions(jvmtiEnv* env,
                           jint* extension_count_ptr,
                           jvmtiExtensionFunctionInfo** extensions)
{
    TRACE2("jvmti.extension", "GetExtensionFunctions called");
    SuspendEnabledChecker sec;
    // check environment, phase, pointers
    jvmtiError errorCode;

    /*
     * Check given env & current phase.
     */
    jvmtiPhase phases[] = {JVMTI_PHASE_ONLOAD, JVMTI_PHASE_LIVE};

    CHECK_EVERYTHING();

    if (NULL == extension_count_ptr)
        return JVMTI_ERROR_NULL_POINTER;

    if (NULL == extensions)
        return JVMTI_ERROR_NULL_POINTER;

    *extension_count_ptr = extensions_number;

    if (0 == extensions_number)
    {
        *extensions = NULL;
        return JVMTI_ERROR_NONE;
    }

    jvmtiExtensionFunctionInfo *array;
    size_t arr_size = sizeof(jvmtiExtensionFunctionInfo)*extensions_number;
    errorCode = _allocate(arr_size, (unsigned char **)&array);

    if (JVMTI_ERROR_NONE != errorCode)
        return errorCode;

    memset(array, 0, arr_size);

    JvmtiExtension *ex = jvmti_extension_list;
    for (int iii = 0; iii < extensions_number; iii++)
    {
        jvmtiExtensionFunctionInfo *info = &ex->info;

        array[iii].func = info->func;
        array[iii].param_count = info->param_count;
        array[iii].error_count = info->error_count;

        errorCode = _allocate(strlen(info->id) + 1,
            (unsigned char **)&(array[iii].id));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_array(array, iii);
            return errorCode;
        }

        errorCode = _allocate(strlen(info->short_description) + 1,
            (unsigned char **)&(array[iii].short_description));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_array(array, iii);
            return errorCode;
        }

        errorCode = _allocate(info->param_count * sizeof(jvmtiParamInfo),
            (unsigned char **)&(array[iii].params));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_array(array, iii);
            return errorCode;
        }

        errorCode = _allocate(info->error_count * sizeof(jvmtiError),
            (unsigned char **)&(array[iii].errors));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_array(array, iii);
            return errorCode;
        }

        memset(array[iii].params, 0,
            info->param_count * sizeof(jvmtiParamInfo));

        for (int jjj = 0; jjj < info->param_count; jjj++)
        {
            array[iii].params[jjj] = info->params[jjj];

            errorCode = _allocate(strlen(info->params[jjj].name) + 1,
                (unsigned char **)&(array[iii].params[jjj].name));
            if (JVMTI_ERROR_NONE != errorCode)
            {
                array[iii].params[jjj].name = NULL;
                free_allocated_extension_array(array, iii);
                return errorCode;
            }

            strcpy(array[iii].params[jjj].name, info->params[jjj].name);
        }

        strcpy(array[iii].id, info->id);
        strcpy(array[iii].short_description, info->short_description);
        memcpy(array[iii].errors, info->errors,
            info->error_count * sizeof(jvmtiError));

        ex = ex->next;
    }

    *extensions = array;
    return JVMTI_ERROR_NONE;
}

struct JvmtiExtensionEvent
{
    JvmtiExtensionEvent *next;
    jvmtiExtensionEventInfo info;
};

static JvmtiExtensionEvent *jvmti_exntension_event_list = NULL;

static const jint extensions_events_number = 0;

static void free_allocated_extension_event_array(jvmtiExtensionEventInfo *array,
                                                 jint number)
{
    for (jint iii = 0; iii <= number; iii++)
    {
        if (array[iii].id)
            _deallocate((unsigned char *)array[iii].id);
        if (array[iii].short_description)
            _deallocate((unsigned char *)array[iii].short_description);

        if (array[iii].params)
        {
            for (jint jjj = 0; jjj < array[iii].param_count; jjj++)
            {
                if (array[iii].params[jjj].name)
                    _deallocate((unsigned char *)array[iii].params[jjj].name);
            }

            _deallocate((unsigned char *)array[iii].params);
        }
    }
    _deallocate((unsigned char *)array);
}

/*
 * Get Extension Events
 *
 * Returns the set of extension events.
 *
 * REQUIRED Functionality.
 */
jvmtiError JNICALL
jvmtiGetExtensionEvents(jvmtiEnv* env,
                        jint* extension_count_ptr,
                        jvmtiExtensionEventInfo** extensions)
{
    TRACE2("jvmti.extension", "GetExtensionEvents called");
    SuspendEnabledChecker sec;
    // check environment, phase, pointers
    jvmtiError errorCode;

    /*
     * Check given env & current phase.
     */
    jvmtiPhase phases[] = {JVMTI_PHASE_ONLOAD, JVMTI_PHASE_LIVE};

    CHECK_EVERYTHING();

    if (NULL == extension_count_ptr)
        return JVMTI_ERROR_NULL_POINTER;

    if (NULL == extensions)
        return JVMTI_ERROR_NULL_POINTER;

    *extension_count_ptr = extensions_events_number;

    if (0 == extensions_events_number)
    {
        *extensions = NULL;
        return JVMTI_ERROR_NONE;
    }

    jvmtiExtensionEventInfo *array;
    size_t arr_size = sizeof(jvmtiExtensionEventInfo)*extensions_events_number;
    errorCode = _allocate(arr_size, (unsigned char **)&array);

    if (JVMTI_ERROR_NONE != errorCode)
        return errorCode;

    memset(array, 0, arr_size);

    JvmtiExtensionEvent *ex = jvmti_exntension_event_list;
    for (int iii = 0; iii < extensions_events_number; iii++)
    {
        jvmtiExtensionEventInfo *info = &ex->info;

        array[iii].extension_event_index = info->extension_event_index;
        array[iii].param_count = info->param_count;

        errorCode = _allocate(strlen(info->id) + 1,
            (unsigned char **)&(array[iii].id));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_event_array(array, iii);
            return errorCode;
        }

        errorCode = _allocate(strlen(info->short_description) + 1,
            (unsigned char **)&(array[iii].short_description));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_event_array(array, iii);
            return errorCode;
        }

        errorCode = _allocate(info->param_count * sizeof(jvmtiParamInfo),
            (unsigned char **)&(array[iii].params));
        if (JVMTI_ERROR_NONE != errorCode)
        {
            free_allocated_extension_event_array(array, iii);
            return errorCode;
        }

        memset(array[iii].params, 0,
            info->param_count * sizeof(jvmtiParamInfo));

        for (int jjj = 0; jjj < info->param_count; jjj++)
        {
            array[iii].params[jjj] = info->params[jjj];

            errorCode = _allocate(strlen(info->params[jjj].name) + 1,
                (unsigned char **)&(array[iii].params[jjj].name));
            if (JVMTI_ERROR_NONE != errorCode)
            {
                array[iii].params[jjj].name = NULL;
                free_allocated_extension_event_array(array, iii);
                return errorCode;
            }

            strcpy(array[iii].params[jjj].name, info->params[jjj].name);
        }

        strcpy(array[iii].id, info->id);
        strcpy(array[iii].short_description, info->short_description);

        ex = ex->next;
    }

    *extensions = array;
    return JVMTI_ERROR_NONE;
}

jvmtiError TIEnv::allocate_extension_event_callbacks_table()
{
    return _allocate(extensions_events_number * sizeof(jvmtiExtensionEvent),
        (unsigned char **)&extension_event_table);
}

/*
 * Set Extension Event Callback
 *
 * Sets the callback function for an extension event and enables
 * the event. Or, if the callback is NULL, disables the event.
 * Note that unlike standard events, setting the callback and
 * enabling the event are a single operation.
 *
 * REQUIRED Functionality.
 */
jvmtiError JNICALL
jvmtiSetExtensionEventCallback(jvmtiEnv* env,
                               jint extension_event_index,
                               jvmtiExtensionEvent callback)
{
    TRACE2("jvmti.extension", "SetExtensionEventCallback called");
    SuspendEnabledChecker sec;
    /*
     * Check given env & current phase.
     */
    jvmtiPhase phases[] = {JVMTI_PHASE_ONLOAD, JVMTI_PHASE_LIVE};

    CHECK_EVERYTHING();

    if (extension_event_index > extensions_events_number)
        return JVMTI_ERROR_ILLEGAL_ARGUMENT;

    TIEnv *ti_env = (TIEnv *)env;
    ti_env->extension_event_table[extension_event_index] = callback;

    return JVMTI_ERROR_NONE;
}


text:
namespace mojo {

template <typename E>
inline size_t GetSerializedSize_(const Array<E>& input);

// Because ValidateParams requires explicit argument specification, the
// argument-dependent loopup technique used to omit namespace when calling
// Serialize_() doesn't seem to work. Therefore, this function is named
// differently from those Serialize_() overloads.
template <typename ValidateParams, typename E, typename F>
inline void SerializeArray_(Array<E> input, internal::Buffer* buf,
                            internal::Array_Data<F>** output);

template <typename E, typename F>
inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output);

namespace internal {

template <typename E, typename F, bool move_only = IsMoveOnlyType<E>::value>
struct ArraySerializer;

template <typename E, typename F> struct ArraySerializer<E, F, false> {
  MOJO_COMPILE_ASSERT(sizeof(E) == sizeof(F), wrong_array_serializer);
  static size_t GetSerializedSize(const Array<E>& input) {
    return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E));
  }
  template <bool element_is_nullable, typename ElementValidateParams>
  static void SerializeElements(
      Array<E> input, Buffer* buf, Array_Data<F>* output) {
    MOJO_COMPILE_ASSERT(!element_is_nullable,
                        Primitive_type_should_be_non_nullable);
    MOJO_COMPILE_ASSERT(
        (IsSame<ElementValidateParams, NoValidateParams>::value),
        Primitive_type_should_not_have_array_validate_params);

    memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E));
  }
  static void DeserializeElements(
      Array_Data<F>* input, Array<E>* output) {
    std::vector<E> result(input->size());
    memcpy(&result[0], input->storage(), input->size() * sizeof(E));
    output->Swap(&result);
  }
};

template <> struct ArraySerializer<bool, bool, false> {
  static size_t GetSerializedSize(const Array<bool>& input) {
    return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8);
  }
  template <bool element_is_nullable, typename ElementValidateParams>
  static void SerializeElements(
      Array<bool> input, Buffer* buf, Array_Data<bool>* output) {
    MOJO_COMPILE_ASSERT(!element_is_nullable,
                        Primitive_type_should_be_non_nullable);
    MOJO_COMPILE_ASSERT(
        (IsSame<ElementValidateParams, NoValidateParams>::value),
        Primitive_type_should_not_have_array_validate_params);

    // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy?
    for (size_t i = 0; i < input.size(); ++i)
      output->at(i) = input[i];
  }
  static void DeserializeElements(
      Array_Data<bool>* input, Array<bool>* output) {
    Array<bool> result(input->size());
    // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy?
    for (size_t i = 0; i < input->size(); ++i)
      result.at(i) = input->at(i);
    output->Swap(&result);
  }
};

template <typename H> struct ArraySerializer<ScopedHandleBase<H>, H, true> {
  static size_t GetSerializedSize(const Array<ScopedHandleBase<H> >& input) {
    return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H));
  }
  template <bool element_is_nullable, typename ElementValidateParams>
  static void SerializeElements(Array<ScopedHandleBase<H> > input,
                                Buffer* buf,
                                Array_Data<H>* output) {
    MOJO_COMPILE_ASSERT(
        (IsSame<ElementValidateParams, NoValidateParams>::value),
        Handle_type_should_not_have_array_validate_params);

    for (size_t i = 0; i < input.size(); ++i) {
      output->at(i) = input[i].release();  // Transfer ownership of the handle.
      MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
          !element_is_nullable && !output->at(i).is_valid(),
          VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
          MakeMessageWithArrayIndex(
              "invalid handle in array expecting valid handles",
              input.size(), i));
    }
  }
  static void DeserializeElements(
      Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) {
    Array<ScopedHandleBase<H> > result(input->size());
    for (size_t i = 0; i < input->size(); ++i)
      result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i)));
    output->Swap(&result);
  }
};

template <typename S> struct ArraySerializer<S, typename S::Data_*, true> {
  static size_t GetSerializedSize(const Array<S>& input) {
    size_t size = sizeof(Array_Data<typename S::Data_*>) +
        input.size() * sizeof(internal::StructPointer<typename S::Data_>);
    for (size_t i = 0; i < input.size(); ++i)
      size += GetSerializedSize_(input[i]);
    return size;
  }
  template <bool element_is_nullable, typename ElementValidateParams>
  static void SerializeElements(Array<S> input,
                                Buffer* buf,
                                Array_Data<typename S::Data_*>* output) {
    for (size_t i = 0; i < input.size(); ++i) {
      typename S::Data_* element;
      SerializeCaller<S, ElementValidateParams>::Run(
          input[i].Pass(), buf, &element);
      output->at(i) = element;
      MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
          !element_is_nullable && !element,
          VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
          MakeMessageWithArrayIndex(
              "null in array expecting valid pointers", input.size(), i));
    }
  }
  static void DeserializeElements(
      Array_Data<typename S::Data_*>* input, Array<S>* output) {
    Array<S> result(input->size());
    for (size_t i = 0; i < input->size(); ++i) {
      S element;
      Deserialize_(input->at(i), &element);
      result[i] = element.Pass();
    }
    output->Swap(&result);
  }

 private:
  template <typename T, typename Params>
  struct SerializeCaller {
    static void Run(T input, Buffer* buf, typename T::Data_** output) {
      MOJO_COMPILE_ASSERT((IsSame<Params, NoValidateParams>::value),
                          Struct_type_should_not_have_array_validate_params);

      Serialize_(input.Pass(), buf, output);
    }
  };

  template <typename T, typename Params>
  struct SerializeCaller<Array<T>, Params> {
    static void Run(Array<T> input,
                    Buffer* buf,
                    typename Array<T>::Data_** output) {
      SerializeArray_<Params>(input.Pass(), buf, output);
    }
  };
};

template <> struct ArraySerializer<String, String_Data*, false> {
  static size_t GetSerializedSize(const Array<String>& input) {
    size_t size = sizeof(Array_Data<String_Data*>) +
        input.size() * sizeof(internal::StringPointer);
    for (size_t i = 0; i < input.size(); ++i)
      size += GetSerializedSize_(input[i]);
    return size;
  }
  template <bool element_is_nullable, typename ElementValidateParams>
  static void SerializeElements(
      Array<String> input,
      Buffer* buf,
      Array_Data<String_Data*>* output) {
    MOJO_COMPILE_ASSERT(
        (IsSame<ElementValidateParams,
                ArrayValidateParams<0, false, NoValidateParams> >::value),
        String_type_has_unexpected_array_validate_params);

    for (size_t i = 0; i < input.size(); ++i) {
      String_Data* element;
      Serialize_(input[i], buf, &element);
      output->at(i) = element;
      MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
          !element_is_nullable && !element,
          VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
          MakeMessageWithArrayIndex(
              "null in array expecting valid strings", input.size(), i));
    }
  }
  static void DeserializeElements(
      Array_Data<String_Data*>* input, Array<String>* output) {
    Array<String> result(input->size());
    for (size_t i = 0; i < input->size(); ++i)
      Deserialize_(input->at(i), &result[i]);
    output->Swap(&result);
  }
};

}  // namespace internal

template <typename E>
inline size_t GetSerializedSize_(const Array<E>& input) {
  if (!input)
    return 0;
  typedef typename internal::WrapperTraits<E>::DataType F;
  return internal::ArraySerializer<E, F>::GetSerializedSize(input);
}

template <typename ValidateParams, typename E, typename F>
inline void SerializeArray_(Array<E> input, internal::Buffer* buf,
                            internal::Array_Data<F>** output) {
  if (input) {
    MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
        ValidateParams::expected_num_elements != 0 &&
            input.size() != ValidateParams::expected_num_elements,
        internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER,
        internal::MakeMessageWithExpectedArraySize(
            "fixed-size array has wrong number of elements",
            input.size(), ValidateParams::expected_num_elements));

    internal::Array_Data<F>* result =
        internal::Array_Data<F>::New(input.size(), buf);
    if (result) {
      internal::ArraySerializer<E, F>::template SerializeElements<
          ValidateParams::element_is_nullable,
          typename ValidateParams::ElementValidateParams>(
              internal::Forward(input), buf, result);
    }
    *output = result;
  } else {
    *output = NULL;
  }
}

template <typename E, typename F>
inline void Deserialize_(internal::Array_Data<F>* input, Array<E>* output) {
  if (input) {
    internal::ArraySerializer<E, F>::DeserializeElements(input, output);
  } else {
    output->reset();
  }
}

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_


text:
/*
 * File: app/view/MonitoringMachineNetOutChartForm.js
 */

Ext.define('webapp.view.MonitoringMachineNetOutChartForm', {
    extend: 'Ext.form.Panel',
    alias: 'widget.monitoringmachinenetoutchartform',

    requires: [
		'Ext.chart.CartesianChart'
    ],

    itemId: 'monitoringMachineNetOutChartForm',
    bodyPadding: 10,
	layout: 'fit',
	height:300,
    items: [
		{
			xtype: 'cartesian',
			autoRender: true,
			itemId: 'machineNetOutChart',
			store: {type:'MonitoringStore'},
			theme: 'Yellow',
			legend: {
                docked: 'right'
            },
			axes: [
				{
					type: 'category',
					fields: [
						'monDtString'
					],
					grid: true,
					label: {
						rotate: {
							degrees: -45
						}
					},
					position: 'bottom'
				},
				{
					type: 'numeric',
					fields: [],
					grid: true,
					maximum: 500,
					minimum: 0,
					position: 'left'
				}
			],
			series: []
		}
	]
});


text:
<?php

declare(strict_types=1);

namespace spec\Sylius\Bundle\ThemeBundle\Locator;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ThemeBundle\Locator\ResourceLocatorInterface;
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException;
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;

final class BundleResourceLocatorSpec extends ObjectBehavior
{
    function let(Filesystem $filesystem, KernelInterface $kernel): void
    {
        $this->beConstructedWith($filesystem, $kernel);
    }

    function it_implements_resource_locator_interface(): void
    {
        $this->shouldImplement(ResourceLocatorInterface::class);
    }

    function it_locates_bundle_resource(
        Filesystem $filesystem,
        KernelInterface $kernel,
        ThemeInterface $theme,
        BundleInterface $childBundle,
        BundleInterface $parentBundle
    ): void {
        $kernel->getBundle('ParentBundle', false)->willReturn([$childBundle, $parentBundle]);

        $childBundle->getName()->willReturn('ChildBundle');
        $parentBundle->getName()->willReturn('ParentBundle');

        $theme->getPath()->willReturn('/theme/path');

        $filesystem->exists('/theme/path/ChildBundle/views/index.html.twig')->shouldBeCalled()->willReturn(false);
        $filesystem->exists('/theme/path/ParentBundle/views/index.html.twig')->shouldBeCalled()->willReturn(true);

        $this->locateResource('@ParentBundle/Resources/views/index.html.twig', $theme)->shouldReturn('/theme/path/ParentBundle/views/index.html.twig');
    }

    function it_throws_an_exception_if_resource_can_not_be_located(
        Filesystem $filesystem,
        KernelInterface $kernel,
        ThemeInterface $theme,
        BundleInterface $childBundle,
        BundleInterface $parentBundle
    ): void {
        $kernel->getBundle('ParentBundle', false)->willReturn([$childBundle, $parentBundle]);

        $childBundle->getName()->willReturn('ChildBundle');
        $parentBundle->getName()->willReturn('ParentBundle');

        $theme->getName()->willReturn('theme/name');
        $theme->getPath()->willReturn('/theme/path');

        $filesystem->exists('/theme/path/ChildBundle/views/index.html.twig')->shouldBeCalled()->willReturn(false);
        $filesystem->exists('/theme/path/ParentBundle/views/index.html.twig')->shouldBeCalled()->willReturn(false);

        $this->shouldThrow(ResourceNotFoundException::class)->during('locateResource', ['@ParentBundle/Resources/views/index.html.twig', $theme]);
    }

    function it_throws_an_exception_if_resource_path_does_not_start_with_an_asperand(ThemeInterface $theme): void
    {
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateResource', ['ParentBundle/Resources/views/index.html.twig', $theme]);
    }

    function it_throws_an_exception_if_resource_path_contains_two_dots_in_a_row(ThemeInterface $theme): void
    {
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateResource', ['@ParentBundle/Resources/views/../views/index.html.twig', $theme]);
    }

    function it_throws_an_exception_if_resource_path_does_not_contain_resources_dir(ThemeInterface $theme): void
    {
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateResource', ['@ParentBundle/views/Resources.index.html.twig', $theme]);
    }
}


text:
package org.librairy.boot.storage.system.column;

import org.librairy.boot.storage.system.column.domain.PartColumn;
import org.librairy.boot.storage.system.column.repository.BaseColumnRepository;
import org.librairy.boot.storage.system.column.repository.PartColumnRepository;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by cbadenes on 22/12/15.
 */
public class PartUnifiedColumnRepositoryTest extends BaseColumnRepositoryTest<PartColumn> {

    @Autowired
    PartColumnRepository repository;

    @Override
    public BaseColumnRepository<PartColumn> getRepository() {
        return repository;
    }

    @Override
    public PartColumn getEntity() {
        PartColumn column = new PartColumn();
        column.setUri("parts/72ce5395-6268-439a-947e-802229e7f022");
        column.setCreationTime("2015-12-21T16:18:59Z");
        column.setSense("background");
        column.setContent("Miniopterus aelleni is a bat in the genus Miniopterus found in the Comoro Islands and Madagascar. It is a small, brown bat, with a forearm length of 35 to 41 mm (1.4 to 1.6 in). The long tragus (a projection in the outer ear) has a broad base and a blunt or rounded tip. The uropatagium (tail membrane) is sparsely haired. The palate is flat and there are distinct diastemata (gaps) between the upper canines and premolars. Populations of this species were previously included in Miniopterus manavi, but recent molecular studies revealed that M");
        column.setTokens("Miniopterus aelleni be a bat in the genus Miniopterus find in the Comoro Islands and Madagascar . It be a small , brown bat , with a forearm length of 35 to 41 mm ( 1.4 to 1.6 in ) . The long tragus ( a projection in the outer ear ) have a broad base and a blunt or round tip . The uropatagium ( tail membrane ) be sparsely haired . The palate be flat and there be distinct diastema ( gap ) between the upper canine and premolar . Populations of this specie be previously include in Miniopterus manavi , but recent molecular study reveal that M ");
        return column;
    }
}


text:
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp

#pragma once

#ifndef NAZARA_ABSTRACTATLAS_HPP
#define NAZARA_ABSTRACTATLAS_HPP

#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <unordered_map>

namespace Nz
{
	class AbstractImage;
	class Image;

	class NAZARA_UTILITY_API AbstractAtlas
	{
		public:
			AbstractAtlas() = default;
			virtual ~AbstractAtlas();

			virtual void Clear() = 0;
			virtual void Free(SparsePtr<const Rectui> rects, SparsePtr<unsigned int> layers, unsigned int count) = 0;
			virtual AbstractImage* GetLayer(unsigned int layerIndex) const = 0;
			virtual std::size_t GetLayerCount() const = 0;
			virtual UInt32 GetStorage() const = 0;
			virtual bool Insert(const Image& image, Rectui* rect, bool* flipped, unsigned int* layerIndex) = 0;

			// Signals:
			NazaraSignal(OnAtlasCleared, const AbstractAtlas* /*atlas*/);
			NazaraSignal(OnAtlasLayerChange, const AbstractAtlas* /*atlas*/, AbstractImage* /*oldLayer*/, AbstractImage* /*newLayer*/);
			NazaraSignal(OnAtlasRelease, const AbstractAtlas* /*atlas*/);
	};
}

#endif // NAZARA_ABSTRACTATLAS_HPP


text:
README of cab42
===============

* (c) Adam Brengesjö <ca.brengesjo@gmail.com> 2011-2012
* Licensed under the Apache License, Version 2.0 (the "License")

Summary
-------

CAB42 is a graphical tool to quickly setup and build multiple CAB
files with diferent configurations for Windows Mobile smart devices.

About
-----

I use CAB42 to build Microsoft Mobile .CAB files with different
profiles and settings.
Also, cabwiz.exe is a PITA to configure when you want to dynamically include
files in various directories relative to your output as all files must be
explicitly be included with a absolute file path.

Installing
----------

See the INSTALL file

Usage
-----

    $ cab42 build MyProject.c42

Overwrite variables

    $ cab42 build MyProject.c42 --set-variable Version 2.1

this sets the project's $(Version) variable to "2.1"

Defining a new variable

    $ branch_name="$(git symbolic-ref HEAD 2>/dev/null)"
    $ branch_name=${branch_name##refs/heads/}
    $ cab42 build MyProject.c42 --set-variable GitBranch ${branch_name}

This example will define a new variable `GitBranch` with the value
set to the currently checked-out branch name (empty if not inside a git
repository).

**Information tab**

Define variables that CAB42 should use. Remember that you need to define where
the variables are to be used under the Includes tab (see FAQ 1).

**Output tab**

**Includes tab**

Define rules for how each file should be included/parsed. That includes saying
what XML-tags in a file that should be replaced by what values (or variable
values).

**Profiles tab**

Enter the values of the variables.

FAQ
---

**Why do I have to write a value as $(NameOfVariable)?**

To support that $(NameOfVariable)-foo-$(NameOfOtherVariable) also works.
Same syntax is used in output-filename, eg.

    PreCom $(Version)$(ReleaseNamePart)-$(ServerAddress) ($(Profile)) - Debuglevel $(DebugLevel)

**What if I have several ServerAddress tags with different absolute path?**

Then you must match these in Includes tab -> Edit Rule (for the file) -> check Modify XML contents and change the XML tag values.


text:
Mirabilis
============================================================
br.com.mirabilis
author: Rodrigo Simões Rosa
mail: rodrigosimoesrosa@gmail.com

### PopupBase.


text:
<?php

namespace Widop\Twitter\Rest\Statuses;

use Widop\Twitter\Options\OptionBag;
use Widop\Twitter\Options\OptionInterface;
use Widop\Twitter\Rest\AbstractPostRequest;

/**
 * Statuses retweet request.
 *
 * @link https://dev.twitter.com/docs/api/1.1/post/statuses/retweet/%3Aid
 *
 * @method string       getId()                        Gets the tweet ID to retweet.
 * @method null         setId(string $id)              Sets the tweet ID to retweet.
 * @method boolean|null getTrimUser()                  Checks if the user should be trimmed.
 * @method null         setTrimUser(boolean $trimUser) Sets if the user should be trimmed.
 *
 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
 */
class StatusesRetweetRequest extends AbstractPostRequest
{
    /**
     * Creates a statuses retweet request.
     *
     * @param string $id The tweet identifier.
     */
    public function __construct($id)
    {
        parent::__construct();

        $this->setId($id);
    }

    /**
     * {@inheritdoc}
     */
    protected function configureOptionBag(OptionBag $optionBag)
    {
        $optionBag
            ->register('id', OptionInterface::TYPE_PATH)
            ->register('trim_user', OptionInterface::TYPE_POST);
    }

    /**
     * {@inheritdoc}
     */
    protected function validateOptionBag(OptionBag $optionBag)
    {
        if (!isset($optionBag['id'])) {
            throw new \RuntimeException('You must provide an id.');
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function getPath()
    {
        return '/statuses/retweet/:id.json';
    }
}