text:
import numpy as np
from porespy.networks import regions_to_network
from porespy.networks import add_boundary_regions
from porespy.networks import label_boundary_cells
from porespy.networks import _net_dict
from porespy.tools import pad_faces
from porespy.filters import snow_partitioning
from porespy.metrics import region_surface_areas, region_interface_areas

def snow_dual(im,
              voxel_size=1,
              boundary_faces=['top', 'bottom', 'left', 'right', 'front', 'back'],
              marching_cubes_area=False):

    r"""
    Analyzes an image that has been partitioned into void and solid regions
    and extracts the void and solid phase geometry as well as network
    connectivity.

    Parameters
    ----------
    im : ND-array
        Binary image in the Boolean form with True’s as void phase and False’s
        as solid phase. It can process the inverted configuration of the
        boolean image as well, but output labelling of phases will be inverted
        and solid phase properties will be assigned to void phase properties
        labels which will cause confusion while performing the simulation.
    voxel_size : scalar
        The resolution of the image, expressed as the length of one side of a
        voxel, so the volume of a voxel would be **voxel_size**-cubed.  The
        default is 1, which is useful when overlaying the PNM on the original
        image since the scale of the image is alway 1 unit lenth per voxel.
    boundary_faces : list of strings
        Boundary faces labels are provided to assign hypothetical boundary
        nodes having zero resistance to transport process. For cubical
        geometry, the user can choose ‘left’, ‘right’, ‘top’, ‘bottom’,
        ‘front’ and ‘back’ face labels to assign boundary nodes. If no label is
        assigned then all six faces will be selected as boundary nodes
        automatically which can be trimmed later on based on user requirements.
    marching_cubes_area : bool
        If ``True`` then the surface area and interfacial area between regions
        will be using the marching cube algorithm. This is a more accurate
        representation of area in extracted network, but is quite slow, so
        it is ``False`` by default.  The default method simply counts voxels
        so does not correctly account for the voxelated nature of the images.

    Returns
    -------
    A dictionary containing all the void and solid phase size data, as well as
    the network topological information.  The dictionary names use the OpenPNM
    convention (i.e. 'pore.coords', 'throat.conns') so it may be converted
    directly to an OpenPNM network object using the ``update`` command.
    * ``net``: A dictionary containing all the void and solid phase size data,
        as well as the network topological information.  The dictionary names
        use the OpenPNM convention (i.e. 'pore.coords', 'throat.conns') so it
        may be converted directly to an OpenPNM network object using the
        ``update`` command.
    * ``im``: The binary image of the void space
    * ``dt``: The combined distance transform of the image
    * ``regions``: The void and solid space partitioned into pores and solids
        phases using a marker based watershed with the peaks found by the
        SNOW Algorithm.

    References
    ----------
    [1] Gostick, J. "A versatile and efficient network extraction algorithm
    using marker-based watershed segmenation".  Phys. Rev. E 96, 023307 (2017)

    [2] Khan, ZA et al.  "Dual network extraction algorithm to investigate
    multiple transport processes in porous materials: Image-based modeling
    of pore and grain-scale processes. Computers and Chemical Engineering.
    123(6), 64-77 (2019)

    """
    # -------------------------------------------------------------------------
    # SNOW void phase
    pore_regions = snow_partitioning(im, return_all=True)
    # SNOW solid phase
    solid_regions = snow_partitioning(~im, return_all=True)
    # -------------------------------------------------------------------------
    # Combined Distance transform of two phases.
    pore_dt = pore_regions.dt
    solid_dt = solid_regions.dt
    dt = pore_dt + solid_dt
    # Calculates combined void and solid regions for dual network extraction
    pore_regions = pore_regions.regions
    solid_regions = solid_regions.regions
    pore_region = pore_regions*im
    solid_region = solid_regions*~im
    solid_num = np.amax(pore_regions)
    solid_region = solid_region + solid_num
    solid_region = solid_region * ~im
    regions = pore_region + solid_region
    b_num = np.amax(regions)
    # -------------------------------------------------------------------------
    # Boundary Conditions
    regions = add_boundary_regions(regions=regions, faces=boundary_faces)
    # -------------------------------------------------------------------------
    # Padding distance transform to extract geometrical properties
    dt = pad_faces(im=dt, faces=boundary_faces)
    # -------------------------------------------------------------------------
    # Extract void,solid and throat information from image
    net = regions_to_network(im=regions, dt=dt, voxel_size=voxel_size)
    # -------------------------------------------------------------------------
    # Extract marching cube surface area and interfacial area of regions
    if marching_cubes_area:
        areas = region_surface_areas(regions=regions)
        interface_area = region_interface_areas(regions=regions, areas=areas,
                                                voxel_size=voxel_size)
        net['pore.surface_area'] = areas * voxel_size**2
        net['throat.area'] = interface_area.area
    # -------------------------------------------------------------------------
    # Find void to void, void to solid and solid to solid throat conns
    loc1 = net['throat.conns'][:, 0] < solid_num
    loc2 = net['throat.conns'][:, 1] >= solid_num
    loc3 = net['throat.conns'][:, 1] < b_num
    pore_solid_labels = loc1 * loc2 * loc3

    loc4 = net['throat.conns'][:, 0] >= solid_num
    loc5 = net['throat.conns'][:, 0] < b_num
    solid_solid_labels = loc4 * loc2 * loc5 * loc3

    loc6 = net['throat.conns'][:, 1] < solid_num
    pore_pore_labels = loc1 * loc6

    loc7 = net['throat.conns'][:, 1] >= b_num
    boundary_throat_labels = loc5 * loc7

    solid_labels = ((net['pore.label'] > solid_num) * ~
                    (net['pore.label'] > b_num))
    boundary_labels = net['pore.label'] > b_num
    b_sa = np.zeros(len(boundary_labels[boundary_labels == 1.0]))
    # -------------------------------------------------------------------------
    # Calculates void interfacial area that connects with solid and vice versa
    p_conns = net['throat.conns'][:, 0][pore_solid_labels]
    ps = net['throat.area'][pore_solid_labels]
    p_sa = np.bincount(p_conns, ps)
    s_conns = net['throat.conns'][:, 1][pore_solid_labels]
    s_pa = np.bincount(s_conns, ps)
    s_pa = np.trim_zeros(s_pa)  # remove pore surface area labels
    p_solid_surf = np.concatenate((p_sa, s_pa, b_sa))
    # -------------------------------------------------------------------------
    # Calculates interfacial area using marching cube method
    if marching_cubes_area:
        ps_c = net['throat.area'][pore_solid_labels]
        p_sa_c = np.bincount(p_conns, ps_c)
        s_pa_c = np.bincount(s_conns, ps_c)
        s_pa_c = np.trim_zeros(s_pa_c)  # remove pore surface area labels
        p_solid_surf = np.concatenate((p_sa_c, s_pa_c, b_sa))
    # -------------------------------------------------------------------------
    # Adding additional information of dual network
    net['pore.solid_void_area'] = (p_solid_surf * voxel_size**2)
    net['throat.void'] = pore_pore_labels
    net['throat.interconnect'] = pore_solid_labels
    net['throat.solid'] = solid_solid_labels
    net['throat.boundary'] = boundary_throat_labels
    net['pore.void'] = net['pore.label'] <= solid_num
    net['pore.solid'] = solid_labels
    net['pore.boundary'] = boundary_labels
    # -------------------------------------------------------------------------
    # label boundary cells
    net = label_boundary_cells(network=net, boundary_faces=boundary_faces)
    # -------------------------------------------------------------------------
    # assign out values to dummy dict

    temp = _net_dict(net)
    temp.im = im.copy()
    temp.dt = dt
    temp.regions = regions
    return temp


text:
import os
import sys
import tarfile
import collections
import torch.utils.data as data
import xml.etree.ElementTree as ET
from PIL import Image
import numpy as np
import torchvision.transforms as transforms
import cv2
import torch
from PIL import ImageFilter as IF
import PIL
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

class SRCNN(data.DataLoader):

    def __init__(self, root_dir, transform=None, upscale = 0):
        self.transform = transform
        dir = root_dir
        self.upscale_factor = upscale
        file_list_image = [file for file in os.listdir(dir) if file.endswith(".jpg")]
        self.images = [os.path.join(dir, x) for x in file_list_image]

    def __getitem__(self, index):
        """
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is a dictionary of the XML tree.
        """
        img = Image.open(self.images[index]).convert('RGB')

        crop = transforms.CenterCrop(
            (int(img.size[1] / self.upscale_factor) * self.upscale_factor,
             int(img.size[0] / self.upscale_factor) * self.upscale_factor))

        img = crop(img)
        out = img.filter(IF.GaussianBlur(1.3))  # .convert('YCbCr') 3채널 : RGB
        out = out.resize((int(out.size[0] / self.upscale_factor), int(out.size[1] / self.upscale_factor)))
        out = out.resize((int(out.size[0] * self.upscale_factor), int(out.size[1] * self.upscale_factor)))

        return self.transform(out), self.transform(img)

    def __len__(self):

        return len(self.images)

ext: py

max_stars_repo_name: DokyeongKwon/Pytorch

reason: import numpy


text:
import time
import cv2
import logging
import numpy as np
import config
from .decoder import decode

class Camera:
    def __init__(self):
        """
        The videoplayer object combines the tflite model with opencv2 camera reading function
        """

        self.cap = cv2.VideoCapture(int(config.analytic["source"]))

    # ================================
    def processFrame(self):
        """
        Processes frame by frame
        :return:
        """

        while True:
            logging.debug("Processing frame")
            ret, frame = self.cap.read()

            if config.analytic['graysend'] == "1":
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            frame = cv2.resize(frame, (config.analytic["send_width"], config.analytic["send_height"]))

            if config.analytic["flip"] == "1":
                frame = cv2.flip(frame, 0)

            if frame is not None:
                yield (decode(frame), None, None)


text:
"""

Creates main figure

"""
import numpy as np
import qcelemental as qcel
import plotly.graph_objects as go
from qcelemental.models import Molecule as qcel_mol

from ..layers.bonds import get_bonds
from ..layers.geometry import get_atoms
#from ..layers.measurements import get_angle, get_line
from ..layers.cube import get_cubes, cube_to_molecule, get_cubes, get_cube_trace
from .layouts import get_layout, get_range
from .widgets import get_buttons, get_slider

from ..molecule.shapes import rotation_matrix
from ..molecule.shapes import get_single_cylinder
from ..layers.bonds import get_bond_mesh

from ..advanced import cubeprop

class Figure():
    def __init__(self, surface="matte", figsize=None, **kwargs):

        self.fig = go.Figure()
        self.molecules = {}
        self.geometries = []
        self.surface = surface
        self.resolution = figsize
        self.min_range = 0.0
        self.max_range = 0.0

    def show(self):
        self.fig.show()

    def assert_range(self, geometry):
        self.min_range = self.min_range if self.min_range < np.min(geometry) else np.min(geometry)
        self.max_range = self.max_range if self.max_range > np.max(geometry) else np.max(geometry)

        range_layout = get_range(self.min_range, self.max_range)
        self.fig.update_layout(range_layout)

    def get_connectivity(self, molecule):

        mol_dict = molecule.dict()
        symbols = molecule.symbols
        geometry = molecule.geometry

        if not "connectivity" in mol_dict:
            return qcel.molutil.guess_connectivity(symbols,geometry)

        elif mol_dict["connectivity"] is None:
            return qcel.molutil.guess_connectivity(symbols,geometry)

        elif "connectivity" in mol_dict:
            return self.dict["connectivity"]

    #Basic Traces

    def add_molecule(self, name, molecule, style="ball_and_stick"):

        self.molecules[name] = molecule

        bonds = self.get_connectivity(molecule)
        bond_list = get_bonds(molecule.geometry, molecule.symbols, bonds, style, self.surface)
        atom_list = get_atoms(molecule.geometry, molecule.atomic_numbers, molecule.symbols, style, self.surface)

        #Add traces
        for bond in bond_list:
            self.fig.add_trace(bond)

        for atom in atom_list:
            self.fig.add_trace(atom)

        #Update layout
        self.fig.update_layout(get_layout(self.resolution))
        self.assert_range(molecule.geometry)

    def add_cube(self, file, iso=0.01, plot_geometry=True,
                 colorscale="portland", opacity=0.2, style="ball_and_stick"):
        """
        Adds an isosurface plot to the figure from a cube file.

        Parameters
        ----------
        file : str
            The path to the cube file
        iso : float, tuple, or list
            If a float is given, the single isosurface is plotted
            Otherwise, all isosurface plots can be navigated via a slider
        plot_geometry : boolean
            Plots bonds and atoms if True, only plots the isosurface(s) if False
        colorscale : str
            The color scheme
        opacity : float
            The degree of transparency for the isosurface(s)
        style : str
            How bonds and atoms are represented within the plot
        """

        geometry, symbols, atomic_numbers, spacing, origin, cube = cube_to_molecule(file)

        if plot_geometry is True:
            bonds = qcel.molutil.guess_connectivity(symbols, geometry)
            bond_list = get_bonds(geometry, symbols, bonds, style, self.surface)
            atom_list = get_atoms(geometry, atomic_numbers, symbols, style, self.surface)

            #Add traces
            for bond in bond_list:
                self.fig.add_trace(bond)
            for atom in atom_list:
                self.fig.add_trace(atom)

        #Single value of iso
        if type(iso) == float:
            trace, min_range, max_range = get_cube_trace(cube, spacing, origin, iso, colorscale, opacity)
            #Add traces
            self.fig.add_trace(trace)

        #Slider with multiple iso value
        elif type(iso) == list or type(iso) == tuple:
            geometry_traces = len(self.fig.data)
            for i, iso_i in enumerate(iso):
                if i == 0:
                    trace, min_range, max_range = get_cube_trace(cube, spacing, origin, iso_i, colorscale, opacity, visible=True)
                else:
                    trace, min_range, max_range = get_cube_trace(cube, spacing, origin, iso_i, colorscale, opacity, visible=False)

                self.fig.add_trace(trace)

            slider = get_slider(iso, geometry_traces)
            self.fig.update_layout(sliders=slider)

        #Update layout
        self.fig.update_layout(get_layout(self.resolution))
        self.assert_range([min_range, max_range])

    def add_measurement(self, mol_label, m,
                        line_width=20,
                        line_color='grey'):

        molecule = self.molecules[mol_label]
        measurement = molecule.measure(m)

        if len(m) == 2:
            line = get_line(str(round(measurement, 2)) ,molecule.geometry[m[0]], molecule.geometry[m[1]],
                    line_width,
                    line_color)

            self.fig.add_trace(line)

            # markers = go.Scatter3d(x=[molecule.geometry[m[0]][0],molecule.geometry[m[0]][0]],
            #                        y=[molecule.geometry[m[0]][1],molecule.geometry[m[0]][1]],
            #                        z=[molecule.geometry[m[0]][2],molecule.geometry[m[0]][2]])

            # self.fig.add_trace(markers)

            print(f"Distance between {molecule.symbols[m[0]]} and {molecule.symbols[m[1]]} is {measurement}")

        elif len(m) == 3:
            angle = get_angle(str(round(measurement,2)),
                    molecule.geometry[m[0]], molecule.geometry[m[1]], molecule.geometry[m[2]],
                    line_color)

            self.fig.add_trace(angle)

        elif len(m) == 4:
            print("Unable to add dihedral")

    def add_cubes(self, directory=".", iso=0.03, style="ball_and_stick", colorscale="portland", opacity=0.2):
        cubes, details = get_cubes(directory)
        geometry, symbols, atomic_numbers, spacing, origin, _ = cube_to_molecule(details[0]["name"]+".cube")
        bonds = qcel.molutil.guess_connectivity(symbols, geometry)

        bond_list = get_bonds(geometry, symbols, bonds, style, self.surface)
        atom_list = get_atoms(geometry, atomic_numbers, symbols, style, self.surface)

        for bond in bond_list:
            self.fig.add_trace(bond)
        for atom in atom_list:
            self.fig.add_trace(atom)

        geometry_traces = len(self.fig.data)

        cube_list = []
        min_list = []
        max_list = []
        for cube in cubes:
            trace, min_range, max_range = get_cube_trace(cube, spacing, origin, iso, colorscale,opacity, visible=False)
            cube_list.append(trace)
            min_list.append(min_range)
            max_list.append(max_range)

        for cube in cube_list:
            self.fig.add_traces(cube)

        button_list = get_buttons(details, geometry_traces, directory)

        self.fig.update_layout(updatemenus=[dict(showactive=True,
                                                buttons=button_list,
                                                font={"family": "Helvetica",
                                                      "size" : 18},
                                                borderwidth=0
            ),
        ])

        #Update layout
        self.fig.update_layout(get_layout(self.resolution))
        self.assert_range([min(min_list), max(max_list)])

    def add_trace(self, trace):
        """
        Maps directly to plotly's figure add trace
        """

        self.fig.add_trace(trace)

    #Psi4 Traces

    def add_density(self, name,
                    wfn,
                    what_density="Dt",
                    iso=0.08,
                    colorscale="blues",
                    opacity=0.2,
                    geometry=True,
                    alpha=0.0,
                    atol=5e-4,
                    style="ball_and_stick",
                    spacing=[0.2, 0.2, 0.2],
                    overage=[3.0, 3.0, 3.0]):

        """
        Adds density from wfn object

        """

        if geometry is True:
            molecule = qcel.models.Molecule.from_data(wfn.basisset().molecule().save_string_xyz())
            self.add_molecule("name"+"_geometry", molecule, style)

        D = spacing
        L = overage

        if what_density == "Dt":
            Density = wfn.Da_subset("AO")
            Density.axpy(1.0, wfn.Db_subset("AO"))

        if what_density == "Da":
            Density =  wfn.Da_subset("AO")

        if what_density == "Db":
            Density = wfn.Db_subset("AO")

        if what_density == "Ds":
            Density_a = wfn.Da_subset("AO")
            Density_b = wfn.Db_subset("AO")
            Density = Density_a - Density_b

        O, N =  cubeprop.build_grid(wfn, L, D)
        block, points, nxyz, npoints =  cubeprop.populate_grid(wfn, O, N, D)

        if what_density == "Da" or what_density == "Db":
            volume = cubeprop.compute_density(O, N, D, npoints, points, nxyz, block, [Density], what_density)

        if what_density == "Dt":
            volume = cubeprop.compute_density(O, N, D, npoints, points, nxyz, block, [Density], what_density)

        #trace, min_range, max_range = get_cube_trace(volume, spacing, O, iso, colorscale, opacity)
        #trace, min_range, max_range = get_cubes_surfaces([volume], spacing, O, iso, colorscale, opacity, alpha, atol)

        x, y, z = np.mgrid[:volume.shape[0], :volume.shape[1], :volume.shape[2]]
        x_r = x * spacing[0] + O[0]
        y_r = y * spacing[1] + O[1]
        z_r = z * spacing[2] + O[2]

        trace = go.Scatter3d(x=x_r,
                             y=y_r,
                             z=z_r)

        self.fig.add_trace(trace)
        self.fig.update_layout(get_layout(self.resolution))
        #self.assert_range([min_range, max_range])

    def add_orbital(self,
                    name,
                    wfn,
                    orbital,
                    iso=0.08,
                    colorscale="portland",
                    opacity=0.3,
                    alpha=0.0,
                    atol=5e-4,
                    style="ball_and_stick",
                    plot_geometry=True,
                    spacing=[0.2,0.2,0.2],
                    overage=[3.0,3.0,3.0]):

        if plot_geometry is True:
            molecule = qcel.models.Molecule.from_data(wfn.basisset().molecule().save_string_xyz())
            self.add_molecule("name"+"_geometry", molecule, style)

        D = spacing
        L = overage
        O, N = cubeprop.build_grid(wfn, L, D)

        C = wfn.Ca_subset("AO", "ALL") if orbital > 0 else wfn.Cb_subset("AO", "ALL") if orbital < 0 else print("Use integers to describe orbital values")
        orbital = np.abs(orbital) - 1

        block, points, nxyz, npoints = cubeprop.populate_grid(wfn, O, N, D)
        volume = cubeprop.compute_orbital(O, N, D, npoints, points, nxyz, block, C, orbital)
        trace, min_range, max_range = get_cube_trace(volume, spacing, O, iso, colorscale, opacity)
        #trace_pos, min_range, max_range = get_cubes_surfaces([volume], spacing, O, iso, colorscale, opacity, alpha, atol)
        #trace_neg, min_range, max_range = get_cubes_surfaces([volume], spacing, O, -iso, colorscale, opacity, alpha, atol)

        self.fig.add_trace(trace)
        self.fig.update_layout(get_layout(self.resolution))
        #self.assert_range([min_range, max_range])

        return volume

    def add_dipole(self,
                   wfn,
                   style = "ball_and_stick",
                   geometry = True):

        if geometry is True:
            molecule = qcel.models.Molecule.from_data(wfn.basisset().molecule().save_string_xyz())
            self.add_molecule("name"+"_geometry", molecule, style)

        r=0.08

        vec1 = np.array([0.1,0,0])
        vec2 = np.array([wfn.variables()["CURRENT DIPOLE X"],
                        wfn.variables()["CURRENT DIPOLE Y"],
                        wfn.variables()["CURRENT DIPOLE Z"]])

        length = np.linalg.norm(vec2 - vec1)
        R = rotation_matrix(np.array([0, 0, 1]), vec2 - vec1)

        cyl = get_single_cylinder(radius=r)
        cyl[:, 2] *= length
        cyl = R.dot(cyl.T).T
        cyl += vec1

        cylinder = get_bond_mesh(cyl, 0, ["H"], "matte")
        cylinder.color = "rgba(204, 0.0, 0.0, 1.0)"

        # line = go.Scatter3d(x=[0, wfn.variables()["CURRENT DIPOLE X"]],
        #                     y=[0, wfn.variables()["CURRENT DIPOLE Y"]],
        #                     z=[0, wfn.variables()["CURRENT DIPOLE Z"]])

        # cone_norm = np.linalg.norm([wfn.variables()["CURRENT DIPOLE X"],
        #                             wfn.variables()["CURRENT DIPOLE Y"],
        #                             wfn.variables()["CURRENT DIPOLE Z"]])

        cone = go.Cone(x=[wfn.variables()["CURRENT DIPOLE X"]],
                    y=[wfn.variables()["CURRENT DIPOLE Y"]],
                    z=[wfn.variables()["CURRENT DIPOLE Z"]],
                    u=[wfn.variables()["CURRENT DIPOLE X"]],
                    v=[wfn.variables()["CURRENT DIPOLE Y"]],
                    w=[wfn.variables()["CURRENT DIPOLE Z"]],
                    colorscale="reds",
                    showscale=False)

        self.fig.add_trace(cylinder)
        self.fig.add_trace(cone)

    def add_frequency(self,
                      name,
                      wfn,
                      frequency,
                      nframes = 3,
                      style="wireframe"):

        #NORMAL FREQUENCIES INDICES:
        vib_indices = []
        for i, j in enumerate(wfn.frequency_analysis["TRV"].dict()["data"]):
            if j == "V":
                vib_indices.append(i)

        #FREQUENCIES:
        freq = wfn.frequency_analysis["omega"].dict()["data"][vib_indices]

        #NORMAL COORDINATES
        molecule = qcel_mol.from_data(wfn.molecule().save_string_xyz())
        geometry = molecule.geometry
        normal_coords = wfn.frequency_analysis['x'].dict()["data"][:,vib_indices].T.reshape(len(vib_indices),len(geometry),3)

        #NEW COORDINTES
        normal_geometries = []
        normal_geometries.append(geometry)
        for i_normal in normal_coords:
            normal_geometries.append(geometry + i_normal)

        #ORIGINAL GEOMETRY
            bonds = self.get_connectivity(molecule)
            bond_list = get_bonds(molecule.geometry, molecule.symbols, bonds, style, self.surface)
            atom_list = get_atoms(molecule.geometry, molecule.atomic_numbers, molecule.symbols, style, self.surface)

            for bond in bond_list:
                self.fig.add_trace(bond)
            for atom in atom_list:
                self.fig.add_trace(atom)

        #GENERATION OF FRAMES
        movie_frames = []
        for i_frame in range(nframes):

            frame_data = []
            geometry_step = geometry + normal_coords[frequency]/(nframes - i_frame)

            bonds = self.get_connectivity(molecule)
            bond_list = get_bonds(geometry_step, molecule.symbols, bonds, style, self.surface)
            atom_list = get_atoms(geometry_step, molecule.atomic_numbers, molecule.symbols, style, self.surface)

            for bond in bond_list:
                #self.fig.add_trace(bond)
                frame_data.append(bond)

            for atom in atom_list:
                #self.fig.add_trace(atom)
                frame_data.append(atom)

            movie_frames.append(go.Frame(data=frame_data))

        #MAKE SURE ANIMATION IS CONTINIOUS
        movie_frames.reverse()
        movie_frames_inv = movie_frames.copy()
        movie_frames.reverse()

        #BUTTONS LAYOUT FOR ANIMATION
        #self.fig.add_trace(frame_data[0])

        self.fig.layout.update(
        #title=F"Frequency: {wfn.frequencies[frequency].real:.2f} 1/cm",
        updatemenus=[dict(
            type="buttons",
            buttons=[dict(  label="Play",
                            method="animate",
                            args=[None,
                                 {"frame": {"duration": 1, "redraw": True}}]),

                    dict (   label="Stop",
                             method="animate",
                             args=[[None],
                                   {"frame": {"duration": 1, "redraw": True},
                                    "mode" : "immediate",
                                    "transition" : {"duration" :0}}])
                    ])])

        self.fig.frames =  movie_frames + movie_frames_inv + movie_frames + movie_frames_inv

        return movie_frames + movie_frames_inv + movie_frames + movie_frames_inv

        #Update layout
        self.fig.update_layout(get_layout(self.resolution))
        self.assert_range(molecule.geometry)

max_stars_repo_name: VHchavez/molely

max_issues_repo_name: VHchavez/molely

max_forks_repo_name: VHchavez/molely


text:
# matching features of two images
import cv2
import cv
import sys
import scipy as sp
import numpy as np

img1_path = '/home/rolf/Dropbox/Robotica/World Perception/Assignment 6/Liftknop Fotos/up_template_empty.png'
img2_path =  '/home/rolf/Dropbox/Robotica/World Perception/Assignment 6/Liftknop Fotos/lift-front-6.jpg'

img1 = cv2.imread(img1_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
img2 = cv2.imread(img2_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
ratio1 = img2.shape[0] / 640.0
ratio2 = img2.shape[1] / 480.0
ratio = ratio1 if ratio1 < ratio2 else ratio2
img2 = cv2.resize(img2, (0,0), fx=ratio, fy=ratio)

# Laplacian Gradients
img1 = cv2.Laplacian(img1,cv2.CV_8UC4)
img2 = cv2.Laplacian(img2,cv2.CV_8UC4)

# Canny Edge Detection
#img1 = cv2.Canny(img1,100,200)
#img2 = cv2.Canny(img2, 100, 200)

# Hough lines
minLineLength = 50
maxLineGap = 10
#img1 = cv2.HoughLinesP(img1,1,np.pi/180,100,minLineLength,maxLineGap)
#img2 = cv2.HoughLinesP(img2,1,np.pi/180,100,minLineLength,maxLineGap)

# Convert to grayscale
#img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
#img2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

# Initiate SIFT detector
sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
k1, des1 = sift.detectAndCompute(img1,None)
k2, des2 = sift.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

sel_matches = []
for m,n in matches:
    if m.distance < 0.90*n.distance:
        sel_matches.append(m)

# #####################################
# visualization
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
view = sp.zeros((max(h1, h2), w1 + w2, 3), sp.uint8)
view[:h1, :w1, 0] = img1
view[:h2, w1:, 0] = img2
view[:, :, 1] = view[:, :, 0]
view[:, :, 2] = view[:, :, 0]

for m in sel_matches:
    # draw the keypoints
    # print m.queryIdx, m.trainIdx, m.distance
    color = tuple([int(sp.random.randint(0, 255)) for _ in xrange(3)])
    cv2.line(view, (int(k1[m.queryIdx].pt[0]), int(k1[m.queryIdx].pt[1])), (int(k2[m.trainIdx].pt[0] + w1), int(k2[m.trainIdx].pt[1])), color)

cv2.imshow("view", view)
cv2.waitKey()

max_stars_repo_name: MartienLagerweij/aidu

max_issues_repo_name: MartienLagerweij/aidu

max_forks_repo_name: MartienLagerweij/aidu

reason: import numpy,import scipy


text:
r"""
PARI Groups

See :pari:`polgalois` for the PARI documentation of these objects.
"""

from sage.libs.pari import pari
from sage.rings.all import Integer
from sage.groups.perm_gps.permgroup_named import TransitiveGroup

class PariGroup(object):
    def __init__(self, x, degree):
        """
        EXAMPLES::

            sage: PariGroup([6, -1, 2, "S3"], 3)
            PARI group [6, -1, 2, S3] of degree 3
            sage: R.<x> = PolynomialRing(QQ)
            sage: f = x^4 - 17*x^3 - 2*x + 1
            sage: G = f.galois_group(pari_group=True); G
            PARI group [24, -1, 5, "S4"] of degree 4
        """
        self.__x = pari(x)
        self.__degree = Integer(degree)

    def __repr__(self):
        return "PARI group %s of degree %s" % (self.__x, self.__degree)

    def __eq__(self, other):
        """
        Test equality.

        EXAMPLES::

            sage: R.<x> = PolynomialRing(QQ)
            sage: f1 = x^4 - 17*x^3 - 2*x + 1
            sage: f2 = x^3 - x - 1
            sage: G1 = f1.galois_group(pari_group=True)
            sage: G2 = f2.galois_group(pari_group=True)
            sage: G1 == G1
            True
            sage: G1 == G2
            False
        """
        return (isinstance(other, PariGroup) and
            (self.__x, self.__degree) == (other.__x, other.__degree))

    def __ne__(self, other):
        """
        Test inequality.

        EXAMPLES::

            sage: R.<x> = PolynomialRing(QQ)
            sage: f1 = x^4 - 17*x^3 - 2*x + 1
            sage: f2 = x^3 - x - 1
            sage: G1 = f1.galois_group(pari_group=True)
            sage: G2 = f2.galois_group(pari_group=True)
            sage: G1 != G1
            False
            sage: G1 != G2
            True
        """
        return not (self == other)

    def __pari__(self):
        """
        TESTS::

            sage: G = PariGroup([6, -1, 2, "S3"], 3)
            sage: pari(G)
            [6, -1, 2, S3]
        """
        return self.__x

    def degree(self):
        """
        Return the degree of ``self``.

        EXAMPLES::

            sage: R.<x> = PolynomialRing(QQ)
            sage: f1 = x^4 - 17*x^3 - 2*x + 1
            sage: G1 = f1.galois_group(pari_group=True)
            sage: G1.degree()
            4
        """
        return self.__degree

    def order(self):
        """
        Return the order of ``self``.

        EXAMPLES::

            sage: R.<x> = PolynomialRing(QQ)
            sage: f1 = x^4 - 17*x^3 - 2*x + 1
            sage: G1 = f1.galois_group(pari_group=True)
            sage: G1.order()
            24
        """
        return Integer(self.__x[0])

    cardinality = order

    def permutation_group(self):
        return TransitiveGroup(self.__degree, self.__x[2])

    _permgroup_ = permutation_group

max_stars_repo_name: bopopescu/sage

max_issues_repo_name: Ivo-Maffei/sage

max_forks_repo_name: dimpase/sage

reason: from sage


text:
import os
import glob
import numpy as np
import pandas as pd

train = pd.read_csv("DataSet/Train.csv", nrows=1000)

total = train.isnull().sum().sort_values(ascending=False)
percent = (train.isnull().sum()/train.isnull().count()).sort_values(ascending=False)
total_df = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])

print(list(total_df[total_df["Percent"] < 0.75]["Percent"].index))


text:
#!/usr/bin/env python
import os
import rospy
import rospkg

from python_qt_binding import loadUi
try:
    # Starting from Qt 5 QWidget is defined in QtWidgets and not QtGui anymore
    from python_qt_binding.QtWidgets import QWidget
except:
    from python_qt_binding.QtGui import QWidget
from python_qt_binding.QtCore import QTimer, Slot
from python_qt_binding.QtCore import pyqtSlot

import geometry_msgs.msg as geometry_msgs
import quadrotor_msgs.msg as quadrotor_msgs
import std_msgs.msg as std_msgs

import math
import numpy as np

class AutopilotWidget(QWidget):

    def __init__(self, parent):
        # Init QWidget
        super(AutopilotWidget, self).__init__(parent)
        self.setObjectName('Autopilot Widget')

        # set variables
        self._quad_namespace = None
        self._connected = False

        self._arm_bridge_pub = None
        self._start_pub = None
        self._land_pub = None
        self._off_pub = None
        self._force_hover_pub = None
        self._go_to_pose_pub = None
        self._perception_pub = None
        self._disable_perception_pub = None

        self._autopilot_feedback_sub = None
        self._autopilot_feedback = quadrotor_msgs.AutopilotFeedback()
        self._autopilot_feedback_stamp = rospy.Time.now()

        self._previous_autopilot_state = self._autopilot_feedback.OFF

        # load UI
        ui_file = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            '../../resource/autopilot_widget.ui')
        loadUi(ui_file, self)

        # Timer
        self._update_info_timer = QTimer(self)
        self._update_info_timer.timeout.connect(self.update_gui)
        self._update_info_timer.start(100)

        self.disconnect()

    def connect(self, quad_namespace):
        self._quad_namespace = quad_namespace

        self._arm_bridge_pub = rospy.Publisher(
            quad_namespace+'/bridge/arm', std_msgs.Bool, queue_size=1)
        self._start_pub = rospy.Publisher(
            quad_namespace+'/autopilot/start', std_msgs.Empty, queue_size=1)
        self._land_pub = rospy.Publisher(
            quad_namespace+'/autopilot/land', std_msgs.Empty, queue_size=1)
        self._off_pub = rospy.Publisher(
            quad_namespace+'/autopilot/off', std_msgs.Empty, queue_size=1)
        self._force_hover_pub = rospy.Publisher(
            quad_namespace+'/autopilot/force_hover', std_msgs.Empty,
            queue_size=1)
        self._go_to_pose_pub = rospy.Publisher(
            quad_namespace+'/autopilot/pose_command', geometry_msgs.PoseStamped,
            queue_size=1)
        self._perception_pub = rospy.Publisher(
            quad_namespace+'/autopilot/pose_command_with_perception', geometry_msgs.PoseStamped,
            queue_size=1)
        self._disable_perception_pub = rospy.Publisher(
            quad_namespace+'/autopilot/disable_perception', std_msgs.Empty,
            queue_size=1)

        self._autopilot_feedback_sub = rospy.Subscriber(
            quad_namespace+'/autopilot/feedback',
            quadrotor_msgs.AutopilotFeedback, self.autopilot_feedback_cb)

        self.button_arm_bridge.setEnabled(True)
        self.button_start.setEnabled(True)
        self.button_land.setEnabled(True)
        self.button_off.setEnabled(True)
        self.button_force_hover.setEnabled(True)
        self.button_go_to_pose.setEnabled(False)
        self.button_perception.setEnabled(False)
        self.button_disable_perception.setEnabled(False)

        self._connected = True

    def disconnect_pub_sub(self, pub):
        if pub is not None:
            pub.unregister()
            pub = None

    def disconnect(self):
        self.disconnect_pub_sub(self._autopilot_feedback_sub)

        self.disconnect_pub_sub(self._arm_bridge_pub)
        self.disconnect_pub_sub(self._start_pub)
        self.disconnect_pub_sub(self._land_pub)
        self.disconnect_pub_sub(self._off_pub)
        self.disconnect_pub_sub(self._force_hover_pub)
        self.disconnect_pub_sub(self._go_to_pose_pub)
        self.disconnect_pub_sub(self._perception_pub)
        self.disconnect_pub_sub(self._disable_perception_pub)

        self.button_arm_bridge.setEnabled(False)
        self.button_start.setEnabled(False)
        self.button_land.setEnabled(False)
        self.button_off.setEnabled(False)
        self.button_force_hover.setEnabled(False)
        self.button_go_to_pose.setEnabled(False)
        self.button_perception.setEnabled(False)
        self.button_disable_perception.setEnabled(False)

        self._connected = False

    def autopilot_feedback_cb(self, msg):
        self._autopilot_feedback = msg
        self._autopilot_feedback_stamp = rospy.Time.now()

    def update_gui(self):
        if (self._connected and self.autopilot_feedback_available()):
            # Autopilot status
            self.autopilot_state.setText(
                self.get_autopilot_state_name(
                    self._autopilot_feedback.autopilot_state))
            self.control_command_delay.setText('%.1f'
                % (self._autopilot_feedback.control_command_delay.to_sec() * 1000.0))
            self.control_computation_time.setText('%.1f'
                % (self._autopilot_feedback.control_computation_time.to_sec() * 1000.0))
            self.trajectory_execution_left_duration.setText('%.1f'
                % self._autopilot_feedback.trajectory_execution_left_duration.to_sec())
            self.trajectories_left_in_queue.setText(
                str(self._autopilot_feedback.trajectories_left_in_queue))

            # Low-Level status
            if (self._autopilot_feedback.low_level_feedback.battery_state ==
                    self._autopilot_feedback.low_level_feedback.BAT_GOOD):
                self.status_battery_voltage.setStyleSheet('QLabel { color : green; }')
                self.status_battery_state.setStyleSheet('QLabel { color : green; }')
            elif (self._autopilot_feedback.low_level_feedback.battery_state ==
                    self._autopilot_feedback.low_level_feedback.BAT_LOW):
                self.status_battery_voltage.setStyleSheet('QLabel { color : orange; }')
                self.status_battery_state.setStyleSheet('QLabel { color : orange; }')
            else:
                self.status_battery_voltage.setStyleSheet('QLabel { color : red; }')
                self.status_battery_state.setStyleSheet('QLabel { color : red; }')

            self.status_battery_state.setText(
                self.get_battery_state_name(
                    self._autopilot_feedback.low_level_feedback.battery_state))
            self.status_battery_voltage.setText(
                '%.2f' % self._autopilot_feedback.low_level_feedback.battery_voltage)
            self.status_control_mode.setText(
                self.get_control_mode_name(
                    self._autopilot_feedback.low_level_feedback.control_mode))

            # State Estimate
            self.state_est_frame_id.setText(
                'Frame ID: %s' % self._autopilot_feedback.state_estimate.header.frame_id)
            euler_angles = np.rad2deg(
                self.quat_to_euler_angles(
                    self._autopilot_feedback.state_estimate.pose.pose.orientation))
            self.state_est_position.setText('x:%+.2f  y:%+.2f  z:%+.2f' %
                (self._autopilot_feedback.state_estimate.pose.pose.position.x,
                 self._autopilot_feedback.state_estimate.pose.pose.position.y,
                 self._autopilot_feedback.state_estimate.pose.pose.position.z))
            self.state_est_velocity.setText('x:%+.2f  y:%+.2f  z:%+.2f' %
                (self._autopilot_feedback.state_estimate.twist.twist.linear.x,
                 self._autopilot_feedback.state_estimate.twist.twist.linear.y,
                 self._autopilot_feedback.state_estimate.twist.twist.linear.z))
            self.state_est_orientation.setText('r:%+.1f  p:%+.1f  h:%+.1f' %
                (euler_angles[0], euler_angles[1], euler_angles[2]))
            self.state_est_body_rates.setText('x:%+.2f  y:%+.2f  z:%+.2f' %
                (self._autopilot_feedback.state_estimate.twist.twist.angular.x / math.pi * 180.0,
                 self._autopilot_feedback.state_estimate.twist.twist.angular.y / math.pi * 180.0,
                 self._autopilot_feedback.state_estimate.twist.twist.angular.z / math.pi * 180.0))

            # Reference State
            self.ref_position.setText('x:%(x)+.2f  y:%(y)+.2f  z:%(z)+.2f' %
                {'x': self._autopilot_feedback.reference_state.pose.position.x,
                 'y': self._autopilot_feedback.reference_state.pose.position.y,
                 'z': self._autopilot_feedback.reference_state.pose.position.z})
            self.ref_velocity.setText('x:%(x)+.2f  y:%(y)+.2f  z:%(z)+.2f' %
                {'x': self._autopilot_feedback.reference_state.velocity.linear.x,
                 'y': self._autopilot_feedback.reference_state.velocity.linear.y,
                 'z': self._autopilot_feedback.reference_state.velocity.linear.z})
            self.ref_acc.setText('x:%(x)+.2f  y:%(y)+.2f  z:%(z)+.2f' %
                {'x': self._autopilot_feedback.reference_state.acceleration.linear.x,
                 'y': self._autopilot_feedback.reference_state.acceleration.linear.y,
                 'z': self._autopilot_feedback.reference_state.acceleration.linear.z})
            self.ref_heading.setText('%+.1f' % (self._autopilot_feedback.reference_state.heading / math.pi * 180.0))

            # Update go_to fields and enable go to pose button on if in HOVER
            if (self._autopilot_feedback.autopilot_state == self._autopilot_feedback.HOVER and
                    self._previous_autopilot_state == self._autopilot_feedback.HOVER):

                self.button_go_to_pose.setEnabled(True)
                self.button_perception.setEnabled(True)
                self.button_disable_perception.setEnabled(True)
                self.go_to_pose_x.setEnabled(True)
                self.go_to_pose_y.setEnabled(True)
                self.go_to_pose_z.setEnabled(True)
                self.go_to_pose_heading.setEnabled(True)

            else:

                self.button_go_to_pose.setDisabled(True)
                self.button_perception.setDisabled(True)
                self.button_disable_perception.setDisabled(True)
                self.go_to_pose_x.setDisabled(True)
                self.go_to_pose_y.setDisabled(True)
                self.go_to_pose_z.setDisabled(True)
                self.go_to_pose_heading.setDisabled(True)

                self.go_to_pose_x.setText('%.2f' % (self._autopilot_feedback.reference_state.pose.position.x))
                self.go_to_pose_y.setText('%.2f' % (self._autopilot_feedback.reference_state.pose.position.y))
                self.go_to_pose_z.setText('%.2f' % (self._autopilot_feedback.reference_state.pose.position.z))
                self.go_to_pose_heading.setText('%.0f' % (self._autopilot_feedback.reference_state.heading / math.pi * 180.0))

            self._previous_autopilot_state = self._autopilot_feedback.autopilot_state

        else:
            # Autopilot status
            self.autopilot_state.setText('Not Available')
            self.control_command_delay.setText('0.0')
            self.control_computation_time.setText('0.0')
            self.trajectory_execution_left_duration.setText('0.0')
            self.trajectories_left_in_queue.setText('0')

            # Low-Level status
            self.status_battery_voltage.setStyleSheet('QLabel { color : gray; }')
            self.status_battery_state.setStyleSheet('QLabel { color : gray; }')

            self.status_battery_state.setText('Not Available')
            self.status_battery_voltage.setText('Not Available')
            self.status_control_mode.setText('Not Available')

            # State Estimate
            self.state_est_frame_id.setText('Frame ID:')
            self.state_est_position.setText('Not Available')
            self.state_est_velocity.setText('Not Available')
            self.state_est_orientation.setText('Not Available')
            self.state_est_body_rates.setText('Not Available')

            # Reference State
            self.ref_position.setText('Not Available')
            self.ref_velocity.setText('Not Available')
            self.ref_acc.setText('Not Available')
            self.ref_heading.setText('Not Available')

    @Slot(bool)
    def on_button_connect_clicked(self):
        if(self._connected):
            self.disconnect()
            self.button_connect.setText('Connect')
        else:
            quad_namespace = self.namespace_text.text()
            self.connect(quad_namespace)
            self.button_connect.setText('Disconnect')

    @Slot(bool)
    def on_button_arm_bridge_clicked(self):
        arm_message = std_msgs.Bool(True)
        self._arm_bridge_pub.publish(arm_message)

    @Slot(bool)
    def on_button_start_clicked(self):
        start_message = std_msgs.Empty()
        self._start_pub.publish(start_message)

    @Slot(bool)
    def on_button_land_clicked(self):
        land_message = std_msgs.Empty()
        self._land_pub.publish(land_message)

    @Slot(bool)
    def on_button_off_clicked(self):
        start_message = std_msgs.Empty()
        self._off_pub.publish(start_message)

    @Slot(bool)
    def on_button_force_hover_clicked(self):
        force_hover_msg = std_msgs.Empty()
        self._force_hover_pub.publish(force_hover_msg)

    @Slot(bool)
    def on_button_go_to_pose_clicked(self):
        try:
            go_to_pose_msg = geometry_msgs.PoseStamped()
            go_to_pose_msg.pose.position.x = float(self.go_to_pose_x.text())
            go_to_pose_msg.pose.position.y = float(self.go_to_pose_y.text())
            go_to_pose_msg.pose.position.z = float(self.go_to_pose_z.text())

            heading = float(self.go_to_pose_heading.text()) / 180.0 * math.pi

            go_to_pose_msg.pose.orientation.w = math.cos(heading / 2.0)
            go_to_pose_msg.pose.orientation.z = math.sin(heading / 2.0)

            self._go_to_pose_pub.publish(go_to_pose_msg)
        except:
            rospy.logwarn("Could not read and send go to pose message!")

    @Slot(bool)
    def on_button_perception_clicked(self):
        try:
            go_to_pose_msg = geometry_msgs.PoseStamped()
            go_to_pose_msg.pose.position.x = float(self.go_to_pose_x.text())
            go_to_pose_msg.pose.position.y = float(self.go_to_pose_y.text())
            go_to_pose_msg.pose.position.z = float(self.go_to_pose_z.text())

            heading = float(self.go_to_pose_heading.text()) / 180.0 * math.pi

            go_to_pose_msg.pose.orientation.w = math.cos(heading / 2.0)
            go_to_pose_msg.pose.orientation.z = math.sin(heading / 2.0)

            self._perception_pub.publish(go_to_pose_msg)
        except:
            rospy.logwarn("Could not read and send go to pose message!")

    @Slot(bool)
    def on_button_disable_perception_clicked(self):
        disable_perception_msg = std_msgs.Empty()
        self._disable_perception_pub.publish(disable_perception_msg)

    def autopilot_feedback_available(self):
        if (rospy.Time.now() - self._autopilot_feedback_stamp).to_sec() <= 1.0:
            return True
        return False

    def get_autopilot_state_name(self, autopilot_state):
        if (autopilot_state == self._autopilot_feedback.START):
            return "START"
        if (autopilot_state == self._autopilot_feedback.HOVER):
            return "HOVER"
        if (autopilot_state == self._autopilot_feedback.LAND):
            return "LAND"
        if (autopilot_state == self._autopilot_feedback.EMERGENCY_LAND):
            return "EMERGENCY_LAND"
        if (autopilot_state == self._autopilot_feedback.BREAKING):
            return "BREAKING"
        if (autopilot_state == self._autopilot_feedback.GO_TO_POSE):
            return "GO_TO_POSE"
        if (autopilot_state == self._autopilot_feedback.VELOCITY_CONTROL):
            return "VELOCITY_CONTROL"
        if (autopilot_state == self._autopilot_feedback.REFERENCE_CONTROL):
            return "REFERENCE_CONTROL"
        if (autopilot_state == self._autopilot_feedback.TRAJECTORY_CONTROL):
            return "TRAJECTORY_CONTROL"
        if (autopilot_state == self._autopilot_feedback.COMMAND_FEEDTHROUGH):
            return "COMMAND_FEEDTHROUGH"
        if (autopilot_state == self._autopilot_feedback.RC_MANUAL):
            return "RC_MANUAL"
        return "OFF"

    def get_battery_state_name(self, battery_state):
        if (battery_state == self._autopilot_feedback.low_level_feedback.BAT_GOOD):
            return "Good"
        if (battery_state == self._autopilot_feedback.low_level_feedback.BAT_LOW):
            return "Low"
        if (battery_state == self._autopilot_feedback.low_level_feedback.BAT_CRITICAL):
            return "Critical"
        return "Invalid"

    def get_control_mode_name(self, control_mode):
        if (control_mode == self._autopilot_feedback.low_level_feedback.ATTITUDE):
            return "Attitude"
        if (control_mode == self._autopilot_feedback.low_level_feedback.BODY_RATES):
            return "Body Rates"
        if (control_mode == self._autopilot_feedback.low_level_feedback.ANGULAR_ACCELERATION):
            return "Angular Accelerations"
        if (control_mode == self._autopilot_feedback.low_level_feedback.ROTOR_THRUSTS):
            return "Rotor Thrusts"
        if (control_mode == self._autopilot_feedback.low_level_feedback.RC_MANUAL):
            return "RC_Manual"
        return "None"

    def quat_to_euler_angles(self, q):
        #  Computes the euler angles from a unit quaternion using the
        #  z-y-x convention.
        #  euler_angles = [roll pitch yaw]'
        #  A quaternion is defined as q = [qw qx qy qz]'
        #  where qw is the real part.

        euler_angles = np.zeros((3, 1))

        euler_angles[0] = np.arctan2(
            2*q.w*q.x + 2*q.y*q.z, q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z)
        euler_angles[1] = -np.arcsin(2*q.x*q.z - 2*q.w*q.y)
        euler_angles[2] = np.arctan2(
            2*q.w*q.z + 2*q.x*q.y, q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z)
        return euler_angles


text:
import matplotlib
matplotlib.rcParams['text.usetex']  = True
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
"""
    # ax.quiver([sx],[sy],[sz],[ex],[ey],[ez])
        sx[] = starting compoent of x cordinate
        sy[] = starting compoent of y cordinate
        sz[] = starting compoent of z cordinate
            sx0 = starting point of x[0]
            sy0 = starting point of y[0]
            sz0 = starting point of z[0]
        ex[] = end compoent of x cordinate
        ey[] = end compoent of y cordinate
        ez[] = end compoent of z cordinate
            ex0 = ending point of x[0]
            ey0 = ending point of y[0]
            ez0 = ending point of z[0]
"""
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
########
"""
x_hat = np.array([1,0,0])
y_hat = np.array([0,1,0])
z_hat = np.array([0,0,1])

v_hat = np.eye(3)
v_0 = np.zeros((3,3))

#ax.quiver([0,0,0],[0,0,0],[0,0,0],[1,2,3],[4,5,6],[3,3,3])
ax.quiver(v_0[0],v_0[1],v_0[2],v_hat[0],v_hat[1],v_hat[2])
"""
########
x = np.arange(0,2*np.pi,.2)
x,y = np.meshgrid(x,x)
z = np.zeros_like(x)

pix = np.array([0,(1/2)*np.pi,np.pi,(3/2)*np.pi,2*np.pi])
sx,sy = np.meshgrid(pix,pix)
#sx = sx.flatten()
#sy = sy.flatten()

sz = np.zeros_like(sx)
su = np.cos(sx)
sv = np.sin(sy)
sw = np.sqrt(su**2 + sv**2)

u = np.cos(x)
v = np.sin(y)
ax.quiver([0],[0],[0],[np.cos(0)],[np.sin(0)],[0])
ax.quiver(sx,sy,sz,su,sv,sw)
#ax.quiver(x[::3,::3],y[::3,::3],z[::3,::3],u[::3,::3],v[::3,::3],z[::3,::3],pivot='middle')
ax.scatter(sx,sy,sz,color='r',s=5)
#ax.plot(sx,sy,sz,'ro')

"""
ax.plot([0],[0],[0],'ro')
ax.plot([np.pi/2],[0],[0],'ro')
ax.plot([np.pi/2],[np.pi/2],[0],'ro')
ax.plot([np.pi/2],[np.pi],[0],'ro')
ax.plot([np.pi],[0],[0],'ro')
ax.plot([np.pi],[np.pi/2],[0],'ro')
ax.plot([np.pi],[np.pi],[0],'ro')
ax.plot([np.pi],[np.pi],[0],'ro')
ax.plot([0],[np.pi/2],[0],'ro')
ax.plot([0],[np.pi],[0],'ro')
ax.plot([0],[np.pi/2],[0],'ro')
#ax.plot([0,1],[0,0],[0,1],'ro')
"""
ax.set_xlim(xmin=-1, xmax=7)
ax.set_ylim(ymin=-1, ymax=7)
ax.set_zlim(zmin=-1, zmax=7)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

reason: import numpy,import sympy


text:
from tempfile import NamedTemporaryFile, TemporaryDirectory

import numpy as np
import pandas
import pyarrow
import pytest

from katana import TsubaError, do_all, do_all_operator
from katana.local import Graph
from katana.local.import_data import from_csr

def test_load(graph):
    assert graph.num_nodes() == 29946
    assert graph.num_edges() == 43072
    assert len(graph.loaded_node_schema()) == 17
    assert len(graph.loaded_edge_schema()) == 3

def test_write(graph):
    with TemporaryDirectory() as tmpdir:
        graph.write(tmpdir)
        old_graph = graph
        del graph
        graph = Graph(tmpdir)
    assert graph.num_nodes() == 29946
    assert graph.num_edges() == 43072
    assert len(graph.loaded_node_schema()) == 17
    assert len(graph.loaded_edge_schema()) == 3

    assert graph == old_graph

# TODO(amp): Reinstant this test once it matches the actual RDG semantics.
@pytest.mark.skip("Does not work. Underlying semantics may be wrong or different.")
def test_commit(graph):
    with TemporaryDirectory() as tmpdir:
        graph.path = tmpdir
        graph.write()
        del graph
        graph = Graph(tmpdir)
    assert graph.num_nodes() == 29092
    assert graph.num_edges() == 39283
    assert len(graph.loaded_node_schema()) == 17
    assert len(graph.loaded_edge_schema()) == 3

def test_get_edge_dest(graph):
    assert graph.get_edge_dest(0) == 8014
    assert graph.get_edge_dest(1) == 8014

def test_edge_data_frame(graph):
    edges = graph.edges()
    assert len(edges.columns) == 6
    assert len(edges) == 43072
    assert edges.at[0, "dest"] == 8014
    assert edges["dest"][1] == 8014
    assert edges["source"][0] == 0
    assert edges["source"][1] == 1
    assert edges["source"][19993] == 20000

def test_edge_data_frame_1_node(graph):
    edges = graph.edges(0)
    assert len(edges.columns) == 6
    assert len(edges) == 1
    assert edges.at[0, "dest"] == 8014
    assert edges["dest"][0] == 8014
    assert edges["source"][0] == 0

def test_edge_data_frame_selected_nodes(graph):
    edges = graph.edges([20000, 5000], properties={"classYear"})
    assert len(edges.columns) == 1 + 3
    assert len(edges) == len(graph.edge_ids(20000)) + len(graph.edge_ids(5000))
    assert edges.at[0, "id"] == 19993
    assert edges.at[1, "id"] == 5000
    assert edges.at[0, "dest"] == 9475
    assert edges.at[1, "dest"] == 9167
    assert edges["dest"][0] == 9475
    assert edges["dest"][1] == 9167
    assert edges["source"][0] == 20000
    assert edges["source"][1] == 5000

def test_edge_data_frame_simple_slice(graph):
    edges = graph.edges(slice(110, 112))
    assert len(edges) == 2
    assert edges.at[0, "dest"] == 8019
    assert edges.at[1, "dest"] == 8019
    assert edges["dest"][0] == 8019
    assert edges["dest"][1] == 8019
    assert edges["source"][0] == 110
    assert edges["source"][1] == 111

def test_edge_data_frame_complex_slice(graph):
    edges = graph.edges(slice(1010, 1020, 2))
    assert len(edges) == 5
    assert edges.at[0, "dest"] == 7993
    assert edges.at[1, "dest"] == 7993
    assert edges["dest"][0] == 7993
    assert edges["dest"][1] == 7993
    assert edges["source"][0] == 1010
    assert edges["source"][1] == 1012

def test_edge_data_frame_to_pandas(graph):
    edges = graph.edges().to_pandas()
    assert len(edges.columns) == 6
    assert len(edges) == 43072
    assert edges.at[0, "dest"] == 8014
    assert edges["dest"][1] == 8014
    assert edges["source"][0] == 0

def test_reachable_from_10(graph):
    reachable = []
    for eid in graph.edge_ids(10):
        reachable.append(graph.get_edge_dest(eid))
    assert reachable == [8015]

def test_nodes_count_edges(graph):
    total = 0
    for nid in graph:
        total += len(graph.edge_ids(nid))
    assert graph.num_edges() == total

def test_get_node_property_exception(graph):
    with pytest.raises(KeyError):
        graph.get_node_property("_mispelled")

def test_get_node_property_index_exception(graph):
    with pytest.raises(IndexError):
        graph.get_node_property(100)

def test_get_node_property(graph):
    prop1 = graph.get_node_property(3)
    assert prop1[10].as_py() is None
    # TODO re-enable this
    # prop2 = graph.get_node_property("length")
    # assert prop1 == prop2

def test_get_node_property_chunked(graph):
    prop1 = graph.get_node_property(4)
    assert isinstance(prop1, pyarrow.Array)
    prop2 = graph.get_node_property_chunked(4)
    assert isinstance(prop2, pyarrow.ChunkedArray)
    assert prop1 == prop2.chunk(0)

def test_remove_node_property(graph):
    graph.remove_node_property(10)
    assert len(graph.loaded_node_schema()) == 16
    graph.remove_node_property("length")
    assert len(graph.loaded_node_schema()) == 15
    assert graph.loaded_node_schema()[4].name == "email"

def test_add_node_property_exception(graph):
    t = pyarrow.table(dict(new_prop=[1, 2]))
    with pytest.raises(RuntimeError):
        # Should raise because new property isn't long enough for the node set
        graph.add_node_property(t)

def test_add_node_property(graph):
    t = pyarrow.table(dict(new_prop=range(graph.num_nodes())))
    graph.add_node_property(t)
    assert graph.get_node_property("new_prop") == pyarrow.array(range(graph.num_nodes()))

def test_add_node_property_kwarg(graph):
    graph.add_node_property(new_prop=range(graph.num_nodes()))
    assert graph.get_node_property("new_prop") == pyarrow.array(range(graph.num_nodes()))

def test_add_node_property_dataframe(graph):
    graph.add_node_property(pandas.DataFrame(dict(new_prop=range(graph.num_nodes()))))
    assert graph.get_node_property("new_prop") == pyarrow.array(range(graph.num_nodes()))

def test_upsert_node_property(graph):
    prop = graph.loaded_node_schema().names[0]
    t = pyarrow.table({prop: range(graph.num_nodes())})
    graph.upsert_node_property(t)
    assert len(graph.loaded_node_schema()) == 17
    assert graph.get_node_property_chunked(prop) == pyarrow.chunked_array([range(graph.num_nodes())])
    assert graph.get_node_property(prop) == pyarrow.array(range(graph.num_nodes()))

def test_get_edge_property(graph):
    prop1 = graph.get_edge_property(2)
    assert not prop1[10].as_py()
    # TODO re-enable this test
    # prop2 = graph.get_edge_property("creationDate")
    # assert prop1 == prop2

def test_get_edge_property_chunked(graph):
    prop1 = graph.get_edge_property(2)
    assert isinstance(prop1, pyarrow.Array)
    prop2 = graph.get_edge_property_chunked(2)
    assert isinstance(prop2, pyarrow.ChunkedArray)
    assert prop1 == prop2.chunk(0)

def test_remove_edge_property(graph):
    graph.remove_edge_property(2)
    assert len(graph.loaded_edge_schema()) == 2
    graph.remove_edge_property("classYear")
    assert len(graph.loaded_edge_schema()) == 1
    assert graph.loaded_edge_schema()[0].name == "creationDate"

def test_add_edge_property_exception(graph):
    t = pyarrow.table(dict(new_prop=[1, 2]))
    with pytest.raises(RuntimeError):
        # Should raise because new property isn't long enough for the node set
        graph.add_edge_property(t)

def test_add_edge_property(graph):
    t = pyarrow.table(dict(new_prop=range(graph.num_edges())))
    graph.add_edge_property(t)
    assert len(graph.loaded_edge_schema()) == 4
    assert graph.get_edge_property("new_prop") == pyarrow.array(range(graph.num_edges()))

def test_upsert_edge_property(graph):
    prop = graph.loaded_edge_schema().names[0]
    t = pyarrow.table({prop: range(graph.num_edges())})
    graph.upsert_edge_property(t)
    assert len(graph.loaded_edge_schema()) == 3
    assert graph.get_edge_property(prop) == pyarrow.array(range(graph.num_edges()))

def test_upsert_edge_property_dict(graph):
    prop = graph.loaded_edge_schema().names[0]
    graph.upsert_edge_property({prop: range(graph.num_edges())})
    assert len(graph.loaded_edge_schema()) == 3
    assert graph.get_edge_property(prop) == pyarrow.array(range(graph.num_edges()))

def test_from_csr():
    pg = from_csr(np.array([1, 1], dtype=np.uint32), np.array([1], dtype=np.uint64))
    assert pg.num_nodes() == 2
    assert pg.num_edges() == 1
    assert list(pg.edge_ids(0)) == [0]
    assert pg.get_edge_dest(0) == 1

def test_from_csr_int16():
    pg = from_csr(np.array([1, 1], dtype=np.int16), np.array([1], dtype=np.int16))
    assert pg.num_nodes() == 2
    assert pg.num_edges() == 1
    assert list(pg.edge_ids(0)) == [0]
    assert pg.get_edge_dest(0) == 1

def test_from_csr_k3():
    pg = from_csr(np.array([2, 4, 6]), np.array([1, 2, 0, 2, 0, 1]))
    assert pg.num_nodes() == 3
    assert pg.num_edges() == 6
    assert list(pg.edge_ids(2)) == [4, 5]
    assert pg.get_edge_dest(4) == 0
    assert pg.get_edge_dest(5) == 1

def test_load_invalid_path():
    with pytest.raises(TsubaError):
        Graph("non-existent")

def test_load_directory():
    with pytest.raises(TsubaError):
        Graph("/tmp")

def test_load_garbage_file():
    with NamedTemporaryFile(delete=True) as fi:
        fi.write(b"Test")
        fi.flush()
        with pytest.raises(TsubaError):
            Graph(fi.name)

def test_simple_algorithm(graph):
    @do_all_operator()
    def func_operator(g, prop, out, nid):
        t = 0
        for eid in g.edge_ids(nid):
            nid2 = g.get_edge_dest(eid)
            if prop.is_valid(nid2):
                t += prop[nid2]
        out[nid] = t

    g = graph
    prop = g.get_node_property("length")
    out = np.empty((g.num_nodes(),), dtype=int)

    do_all(g, func_operator(g, prop, out), "operator")

    g.add_node_property(pyarrow.table(dict(referenced_total_length=out)))

    oprop = g.get_node_property("referenced_total_length")

    assert oprop[0].as_py() == 0
    assert oprop[4].as_py() == 0
    assert oprop[-1].as_py() == 0

def test_types(graph):
    node_type_set = set()
    edge_type_set = set()
    for nid in range(0, graph.num_nodes()):
        node_type_set.add(graph.get_type_of_node(nid))
        for eid in graph.edge_ids(nid):
            edge_type_set.add(graph.get_type_of_edge(eid))

    assert len(node_type_set) == 11
    assert len(edge_type_set) == 15

    node_entity_type = graph.node_types._make_entity_type(17)
    edge_entity_type = graph.edge_types._make_entity_type(8)
    assert graph.does_node_have_type(0, node_entity_type)
    assert graph.does_edge_have_type(0, edge_entity_type)

    node_atomic_types = graph.node_types.atomic_types
    node_name_to_id_map = {name: node_atomic_types[name].type_id for name in node_atomic_types}
    assert node_name_to_id_map == {
        b"Tag": 12,
        b"Organisation": 8,
        b"City": 1,
        b"Comment": 2,
        b"University": 14,
        b"Forum": 6,
        b"Company": 3,
        b"Continent": 4,
        b"Country": 5,
        b"Place": 10,
        b"TagClass": 13,
        b"Person": 9,
        b"Message": 7,
        b"Post": 11,
    }
    assert graph.node_types.is_subtype_of(0, 1) is True

    edge_atomic_types = graph.edge_types.atomic_types
    edge_name_to_id_map = {name: edge_atomic_types[name].type_id for name in edge_atomic_types}
    assert edge_name_to_id_map == {
        b"KNOWS": 11,
        b"IS_SUBCLASS_OF": 10,
        b"REPLY_OF": 13,
        b"LIKES": 12,
        b"CONTAINER_OF": 1,
        b"WORK_AT": 15,
        b"HAS_TYPE": 7,
        b"HAS_CREATOR": 2,
        b"STUDY_AT": 14,
        b"IS_LOCATED_IN": 8,
        b"HAS_INTEREST": 3,
        b"HAS_TAG": 6,
        b"HAS_MEMBER": 4,
        b"HAS_MODERATOR": 5,
        b"IS_PART_OF": 9,
    }
    assert graph.edge_types.is_subtype_of(0, 1) is True

max_stars_repo_name: micah-white/katana

max_issues_repo_name: micah-white/katana

max_forks_repo_name: micah-white/katana


text:
import copy
import numpy as np
from jsonextended import edict
from ejplugins.utils import validate_against_schema

_element_schema = {
    "repeat_cell": {
        'type': 'object',
        'required': ['cell_vectors', 'color_bbox', 'centre', 'type', 'name', 'sites', 'bonds'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'repeat_cell'},
            'cell_vectors': {'type': 'object', 'required': ['a', 'b', 'c']},
            'centre': {'type': 'array', "items": {"type": "number"}, 'minItems': 3, 'maxItems': 3},
            'sites': {
                'type': 'array',
                'items': {
                    'type': "object",
                    'required': ["radius", "color_fill", "color_outline", "transparency", "label", "ccoord"],
                    'properties': {
                        "radius": {"type": "number", "minimum": 0.},
                        "transparency": {"type": "number", "minimum": 0., "maximum": 1.},
                        "label": {"type": "string"},
                        "ccoord": {"type": "array", "items": {"type": "number"}, 'minItems': 3, 'maxItems': 3}
                    }
                }
            },
            'bonds': {
                'type': 'array',
                'items': {
                    "type": "object",
                    'required': ["label", "coord_label", "radius", "include_periodic"],
                    'properties': {
                        "label": {"type": "string"},
                        "coord_label": {"type": "string"},
                        "max_dist": {"type": "number", "minimum": 0.},
                        "radius": {"type": "number", "minimum": 0.},
                        "include_periodic": {"type": "boolean"}
                    }
                }
            }
        }
    },
    # 'repeat_poly': {
    #     'type': 'object',
    #     'required': ['cell_vectors', 'centre', 'polys', 'type',
    #                  'color', 'solid'],
    #     'properties': {
    #         'type': {'type': 'string', 'pattern': 'repeat_poly'},
    #         'cell_vectors': {'required': ['a', 'b', 'c']},
    #         'polys': {'type': 'array'},
    #         'solid': {'type': 'boolean'}
    #     }
    # },
    'repeat_density': {
        'type': 'object',
        'required': ['name', 'dtype', 'cell_vectors', 'centre', 'dcube', 'type', 'color_bbox'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'repeat_density'},
            "name": {"type": "string"},
            "dtype": {"type": "string"},
            'cell_vectors': {'type': 'object', 'required': ['a', 'b', 'c']},
            'dcube': {'nddim': 3, 'ndtype': 'float'},
            'centre': {'type': 'array', "items": {"type": "number"}, 'minItems': 3, 'maxItems': 3},
        }
    },
}

_transform_schema = {
    "local_repeat": {
        'type': 'object',
        'required': ['repeats', 'type'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'local_repeat'},
        }
    },
    "local_align": {
        'type': 'object',
        'required': ['cvector', 'direction', 'type'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'local_align'},
        }
    },
    "translate_to": {
        'type': 'object',
        'required': ['centre', 'type'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'translate_to'},
        }
    },
    "slice": {
        'type': 'object',
        'required': ['type', 'centre', 'normal', 'lbound', 'ubound'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'slice'},
        }
    },
    "resize": {
        'type': 'object',
        'required': ['type', 'sfraction'],
        'properties': {
            'type': {'type': 'string', 'pattern': 'resize'},
            'sfraction': {'type': 'number'}
        }
    },
}

def get_schema(eltypes=None, trtypes=None):
    """

    Parameters
    ----------
    eltypes: list or None
        element schemas to allow (if None, allow all)
    trtypes: list or None
        transform schemas to allow (if None, allow all)

    Returns
    -------

    """

    eschemas = list(_element_schema.values()) if eltypes is None else [_element_schema[e] for e in eltypes]
    tschemas = list(_transform_schema.values()) if trtypes is None else [_transform_schema[e] for e in trtypes]

    vschema = {
        'type': 'object',
        'required': ['elements', 'transforms'],
        'properties': {
            'elements': {
                'type': 'array',
                'items': {
                    'type': 'object',
                    'required': ['transforms'],
                    'properties': {
                        'transforms': {
                            'type': 'array',
                            'items': {
                                # 'type': 'object',
                                # 'required': ['type'],
                                # 'oneOf': tschemas
                            },
                        },
                    },
                    # 'oneOf': eschemas
                },
            },
            'transforms': {
                'type': 'array',
                'items': {
                    # 'type': 'object',
                    # 'required': ['type'],
                    # 'oneOf': tschemas
                },

            }
        }
    }

    if len(tschemas) > 1:
        edict.indexes(vschema, ["properties", "transforms", "items"])["oneOf"] = tschemas
        edict.indexes(vschema,
                      ["properties", "elements", "items", "properties", "transforms", "items"])["oneOf"] = tschemas
    else:
        vschema["properties"]["transforms"]["items"] = tschemas[0]
        edict.indexes(vschema,
                      ["properties", "elements", "items", "properties", "transforms"])["items"] = tschemas[0]

    if len(eschemas) > 1:
        edict.indexes(vschema, ["properties", "elements", "items"])["oneOf"] = eschemas
    else:
        vschema["properties"]["elements"]["items"] = edict.merge(
            [vschema["properties"]["elements"]["items"], eschemas[0]], append=True)

    # edict.pprint(vschema, depth=None)
    return vschema

def process_vstruct(vstruct, eltypes=None, trtypes=None, deepcopy=False):
    """ validate the vstruct

    Parameters
    ----------
    vstruct: dict
    eltypes: list or str or None
        element schemas to allow (if None, allow all)
    trtypes: list or None
        transform schemas to allow (if None, allow all)
    deepcopy:
        deepcopy structure before returning

    Returns
    -------

    """
    if "elements" not in vstruct:
        vstruct = {"elements": [vstruct], "transforms": []}

    validate_against_schema(vstruct, get_schema(eltypes=eltypes, trtypes=trtypes))

    if deepcopy:
        return copy.deepcopy(vstruct)
    else:
        return vstruct

max_stars_repo_name: chrisjsewell/ipyatom

max_issues_repo_name: chrisjsewell/ipyatom

max_forks_repo_name: chrisjsewell/ipyatom


text:
import numpy as np

from ..search_setting._base import get_params, to_func

def eval_fitness(scores, sign):
    """
    Fitness is in proportion to difference from worst score.
    """
    scores = sign*scores
    scores = np.nanmax(scores) - scores
    scores /= np.nansum(scores)
    scores[np.isnan(scores)] = 0
    return scores

def crossover(fitness, param_crossover_proba, cv_results, start_index):
    parents_index = np.random.choice(len(fitness), size=2, replace=False, p=fitness) + start_index
    parents_params_base = cv_results["params"][parents_index[0]]
    parents_params_tgt = cv_results["params"][parents_index[1]]

    child_params = {}
    for key in parents_params_base.keys():
        if np.random.rand() < param_crossover_proba:
            child_params[key] = parents_params_tgt[key]
        else:
            child_params[key] = parents_params_base[key]
    return child_params

def mutate(params, param_mutation_proba, param_distributions):
    ret = {}
    for key in params.keys():
        if np.random.rand() < param_mutation_proba:
            ret.update(get_params(param_distributions, tgt_key=key))
        else:
            ret[key] = params[key]
    return ret

def gamin(obj, param_distributions, max_iter, iter_pergeneration, param_crossover_proba, param_mutation_proba, random_sampling_proba, cvsummarizer):
    it = 0
    param_crossover_proba = to_func(param_crossover_proba)
    param_mutation_proba = to_func(param_mutation_proba)
    random_sampling_proba = to_func(random_sampling_proba)

    while True:
        # Set GA params.
        population = []
        if it == 0:
            generaion = 0
            rsp = 1
            start_index = None
            fitness = None

        else:
            generaion = int((it+1) / iter_pergeneration)
            rsp = random_sampling_proba(generaion)
            start_index = it - iter_pergeneration
            fitness = eval_fitness(scores=np.array(cvsummarizer.cv_results_[cvsummarizer.score_summarizer_name+"_test_score"])[start_index:],
                                   sign=cvsummarizer.sign)

            if (fitness > 0).sum() < 2:
                # If there are not enough parents in this generaion, use old generation scores.
                fitness = eval_fitness(scores=np.array(cvsummarizer.cv_results_[cvsummarizer.score_summarizer_name+"_test_score"]),
                                       sign=cvsummarizer.sign)
                start_index = 0

                if (fitness > 0).sum() < 2:
                    # If there are not enough parents in all generaion, all next generation is random sample.
                    rsp = 1

        # Create population.
        for i in range(iter_pergeneration):
            if np.random.rand() < rsp:
                population.append(get_params(param_distributions, tgt_key=None))
            else:
                child = crossover(fitness=fitness, param_crossover_proba=param_crossover_proba(generaion),
                                  cv_results=cvsummarizer.cv_results_, start_index=start_index)
                child = mutate(params=child, param_mutation_proba=param_mutation_proba(generaion),
                               param_distributions=param_distributions)
                population.append(child)

        # Do search.
        for params in population:
            obj(params)
            it += 1
            if it >= max_iter:
                return

max_stars_repo_name: lucasiscovici/cvopt

max_issues_repo_name: lucasiscovici/cvopt

max_forks_repo_name: lucasiscovici/cvopt


text:
# Patchwise Road Segmentation for Aerial Images with CNN
# Emmanouil Angelis, Spyridon Angelopoulos, Georgios Touloupas
# Group 5: Google Maps Team
# Department of Computer Science, ETH Zurich, Switzerland
# Computational Intelligence Lab

# This script is used for postprocessing (model ensemble, CRFs)

import os
import shutil
import argparse
import glob
import pickle
import numpy as np
from scipy.ndimage.filters import gaussian_filter
from skimage.io import imread, imsave
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_softmax, create_pairwise_bilateral, create_pairwise_gaussian

PIXEL_DEPTH = 255
NUM_LABELS = 2
IMG_PATCH_SIZE = 16

def create_empty_dir(dir):
    os.makedirs(dir, exist_ok=True)
    shutil.rmtree(dir)
    os.makedirs(dir, exist_ok=True)

def expand_patches(softmax_outputs, axes=(0,1)):
    expanded = softmax_outputs.repeat(IMG_PATCH_SIZE, axis=axes[0]).repeat(IMG_PATCH_SIZE, axis=axes[1])
    return expanded

def img_float_to_uint8(img):
    rimg = img - np.min(img)
    rimg = (rimg/np.max(rimg)*PIXEL_DEPTH).round().astype(np.uint8)
    return rimg

def patch_to_label(patch):
    foreground_threshold = 0.25 # percentage of pixels > 1 required to assign a foreground label to a patch
    df = np.mean(patch)
    if df > foreground_threshold:
        return 1
    else:
        return 0

def calculate_patch_predictions(img):
    predictions = []
    for i in range(0, img.shape[0], IMG_PATCH_SIZE):
        row_predictions = []
        for j in range(0, img.shape[1], IMG_PATCH_SIZE):
            patch = img[i:i + IMG_PATCH_SIZE, j:j + IMG_PATCH_SIZE]
            row_predictions.append(patch_to_label(patch))
        predictions.append(row_predictions)
    return np.asarray(predictions)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--test_dir", type=str, default="test_set_images",
                        help="directory containing the test dataset")
    parser.add_argument("--softmax_outputs_dir", type=str, default="softmax_outputs",
                        help="directory containing the softmax outputs of different models for the test set")
    parser.add_argument("--ensemble_dir", type=str, default="predictions_ensemble",
                        help="directory to store the ensemble predictions")
    parser.add_argument("--gaussian_blur_sigma", type=int, default=7,
                        help="Gaussian blur radius to be applied on the ensemble probabilities")
    parser.add_argument("--crf_dir", type=str, default="predictions_crf",
                        help="directory to store the CRF predictions")
    parser.add_argument("--crf_inference_steps", type=int, default=1,
                        help="CRF inference steps")
    parser.add_argument("--gaussian_sxy", type=int, default=3,
                        help="Pairwise Gaussian potential sxy features")
    parser.add_argument("--bilateral_sxy", type=int, default=20,
                        help="Pairwise bilateral potential sxy features")
    parser.add_argument("--bilateral_srgb", type=int, default=20,
                        help="Pairwise bilateral potential srgb features")
    args = parser.parse_args()

    create_empty_dir(args.ensemble_dir)
    create_empty_dir(args.crf_dir)

    print("Loading test set")
    imgs = {}
    for test_img in glob.glob(os.path.join(args.test_dir, "*.png")):
        imgs[test_img.split("test_")[-1]] = imread(test_img)
    img = next(iter(imgs.values()))
    H = img.shape[0]
    W = img.shape[1]

    print("Loading softmax outputs for all models")
    models = {}
    for softmax_outputs_file in glob.glob(os.path.join(args.softmax_outputs_dir, "*.pkl")):
        f = open(softmax_outputs_file, "rb")
        softmax_outputs = pickle.load(f)
        f.close()
        new_softmax_outputs = {}
        for test_img, softmax_output in softmax_outputs.items():
            new_softmax_outputs[test_img.split("test_")[-1]] = np.asarray(softmax_output).reshape((H//IMG_PATCH_SIZE, W//IMG_PATCH_SIZE, NUM_LABELS)).transpose([2, 1, 0])
        models[softmax_outputs_file] = new_softmax_outputs

    print("Calculating ensemble predictions")
    ensemble_softmax_outputs = {}
    blurred_softmax_outputs = {}
    for softmax_outputs_file, softmax_outputs in models.items():
        for test_img, softmax_output in softmax_outputs.items():
            if test_img not in ensemble_softmax_outputs:
                ensemble_softmax_outputs[test_img] = []
            ensemble_softmax_outputs[test_img] += [softmax_output]
    for test_img, softmax_output in ensemble_softmax_outputs.items():
        ensemble_softmax_outputs[test_img] = expand_patches(np.asarray(softmax_output).mean(axis=0), axes=(1,2))
        imsave(args.ensemble_dir + "/softmax_ensemble_" + test_img, img_float_to_uint8(ensemble_softmax_outputs[test_img][1]))
        ensemble_prediction = ensemble_softmax_outputs[test_img].argmax(axis=0)
        imsave(args.ensemble_dir + "/prediction_test_" + test_img, img_float_to_uint8(ensemble_prediction))
        blurred_probabilities = gaussian_filter(ensemble_softmax_outputs[test_img][1], sigma=args.gaussian_blur_sigma)
        imsave(args.ensemble_dir + "/blurred_softmax_ensemble_" + test_img, img_float_to_uint8(blurred_probabilities))
        blurred_softmax_outputs[test_img] = np.asarray([1 - blurred_probabilities, blurred_probabilities])

    print("CRF postprocessing")
    for test_img, softmax_output in blurred_softmax_outputs.items():
        d = dcrf.DenseCRF2D(W, H, NUM_LABELS)
        # Get unary potentials (neg log probability)
        U = unary_from_softmax(softmax_output)
        U = np.ascontiguousarray(U)
        d.setUnaryEnergy(U)
        # This potential enforces more spatially consistent segmentations
        d.addPairwiseGaussian(sxy=args.gaussian_sxy,
                              compat=3,
                              kernel=dcrf.DIAG_KERNEL,
                              normalization=dcrf.NORMALIZE_SYMMETRIC)
        # This potential uses local color features to refine the segmentation
        d.addPairwiseBilateral(sxy=args.bilateral_sxy,
                               srgb=args.bilateral_srgb,
                               rgbim=imgs[test_img],
                               compat=5,
                               kernel=dcrf.DIAG_KERNEL,
                               normalization=dcrf.NORMALIZE_SYMMETRIC)
        # Run inference steps
        Q = d.inference(args.crf_inference_steps)
        crf_probabilities = np.asarray(Q).reshape((NUM_LABELS, H, W))
        imsave(args.crf_dir + "/pixelwise_probabilities_" + test_img, img_float_to_uint8(crf_probabilities[1]))
        # Pixelwise prediction
        crf_prediction = crf_probabilities.argmax(axis=0)
        imsave(args.crf_dir + "/pixelwise_prediction_" + test_img, img_float_to_uint8(crf_prediction))
        # Per patch prediction
        crf_prediction_patch = expand_patches(calculate_patch_predictions(crf_probabilities[1]))
        imsave(args.crf_dir + "/patch_probabilities_prediction_" + test_img, img_float_to_uint8(crf_prediction_patch))
        crf_probabilities_patch = expand_patches(calculate_patch_predictions(crf_prediction))
        imsave(args.crf_dir + "/prediction_test_" + test_img, img_float_to_uint8(crf_probabilities_patch))

max_stars_repo_name: gttm/eth-computational-intelligence-lab

max_issues_repo_name: gttm/eth-computational-intelligence-lab

max_forks_repo_name: gttm/eth-computational-intelligence-lab

reason: import numpy,from scipy


text:
#!/usr/bin/env python2
'''
Created on Apr 5, 2018

@author: sergei gasilov
'''

import Tkinter as tk
import tkFileDialog as filedialog
import numpy as np
import os
from ezufo_helpers.stitch_funcs import main_sti_mp, main_conc_mp, main_360_mp_depth1
import getpass

E=tk.E; W=tk.W

class tk_args():
    def __init__(self, e_input, e_output, e_tmpdir,
                    e_typ, e_ort, e_slices, e_flip, e_ipol,
                    e_reprows, e_gray256, e_hmin, e_hmax,
                    e_r1, e_r2, e_ax):

        self.args={}
        # directories
        self.args['input']=str(e_input.get())
        setattr(self,'input',self.args['input'])
        self.args['output']=str(e_output.get())
        setattr(self,'output',self.args['output'])
        self.args['tmpdir']=str(e_tmpdir.get())
        setattr(self,'tmpdir',self.args['tmpdir'])
        # parameters
        self.args['typ']=str(e_typ.get())
        setattr(self,'typ',self.args['typ'])
        self.args['slices']=str(e_slices.get())
        setattr(self,'slices',self.args['slices'])
        self.args['flip']=bool(int(e_flip.get()))
        setattr(self,'flip',self.args['flip'])
        self.args['ipol']=int(e_ipol.get())
        setattr(self,'ipol',self.args['ipol'])
        self.args['ort']=bool(int(e_ort.get()))
        setattr(self,'ort',self.args['ort'])
        # vert stitch with interp and normalization
        self.args['reprows']=int(e_reprows.get())
        setattr(self,'reprows',self.args['reprows'])
        self.args['gray256']=bool(int(e_gray256.get()))
        setattr(self,'gray256',self.args['gray256'])
        self.args['hmin']=float(e_hmin.get())
        setattr(self,'hmin',self.args['hmin'])
        self.args['hmax']=float(e_hmax.get())
        setattr(self,'hmax',self.args['hmax'])
        #simple vert stitch
        self.args['r2']=int(e_r2.get())
        setattr(self,'r2',self.args['r2'])
        self.args['r1']=int(e_r1.get())
        setattr(self,'r1',self.args['r1'])
        #hor stitch half acq mode
        self.args['ax']=int(e_ax.get())
        setattr(self,'ax',self.args['ax'])

class GUI:
    def __init__(self, A):
        self.A = A
        A.title("Stitches multiple images of the same type stored in subdirectories")
        r=0;

        #Select input directory
        self.input = os.getcwd()
        self.input_b = tk.Button(A,\
            text="Select Input directory with a 000,001,...00N subdirectories", \
            command=self.select_input)
        self.input_b.grid(row=r, column=0, columnspan=2)
        r+=1

        v = tk.StringVar(A, value=self.input)
        self.e_input = tk.Entry(A,textvariable=v, width=70)
        self.e_input.grid(row=r, column=0, columnspan=2, sticky=E)
        r+=1

        #Select temporary directory
        self.tmpdir = os.getcwd()
        self.tmpdir = os.path.join("/data", 'tmp-ezstitch-'+getpass.getuser() )
        self.tmpdir_b = tk.Button(A,\
            text="Select temporary directory - default value recommended", \
            command=self.select_tmpdir)
        self.tmpdir_b.grid(row=r, column=0, columnspan=2)
        self.tmpdir_b.configure(state=tk.DISABLED)
        r+=1

        v = tk.StringVar(A, value=self.tmpdir)
        self.e_tmpdir = tk.Entry(A,textvariable=v, width=70)
        self.e_tmpdir.grid(row=r, column=0, columnspan=2, sticky=E)
        r+=1

        #Select output directory
        cwd = os.getcwd() + '-stitched'
        #cwd = os.path.abspath(os.path.join(cwd, os.pardir))
        self.output = cwd
        self.output_b = tk.Button(A,\
            text="Directory to save stitched images", \
            command=self.select_output)
        self.output_b.grid(row=r, column=0, columnspan=2)
        r+=1

        v = tk.StringVar(A, value=self.output)
        self.e_output = tk.Entry(A,textvariable=v, width=70)
        self.e_output.grid(row=r, column=0, columnspan=2, sticky=E)
        r+=1

        #other parameters
        tk.Label(A, text="Type of images to stitch (e.g. sli, tomo, proj-pr, etc.)"\
                ,fg="darkorange3").grid(row=r);
        v = tk.StringVar(A, value='sli')
        self.e_typ = tk.Entry(A,textvariable=v);
        self.e_typ.grid(row=r, column=1, sticky=E)
        r+=1

        tmp="Stitch orthogonal sections"
        self.e_ort = tk.BooleanVar(A, value=True)
        tk.Checkbutton(A, text=tmp, fg="darkorange3", \
                        variable=self.e_ort).grid(row=r, column=0)
        r+=1

        tk.Label(A, text="Which images to be stitched: start,stop,step:"\
                ,fg="blue").grid(row=r);
        v = tk.StringVar(A, value='200,2000,200')
        self.e_slices = tk.Entry(A,textvariable=v);
        self.e_slices.grid(row=r, column=1, sticky=E)
        r+=1

        self.e_flip = tk.BooleanVar(A, value=False)
        tmp="Sample was moved downwards during scan"
        tk.Checkbutton(A, text=tmp, fg="blue", \
                variable=self.e_flip).grid(row=r, column=0)
        r+=1

        self.e_ipol = tk.IntVar(A, value=0)
        tmp="Interpolate overlapping regions and equalize intensity"
        tk.Radiobutton(A, text=tmp, fg="magenta4",\
                    variable=self.e_ipol, value=0).grid(row=r, column=0)
        r+=1

        tk.Label(A, text="Number of overlapping rows"\
                    ,fg="magenta4").grid(row=r);
        v = tk.IntVar(A, value=60)
        self.e_reprows = tk.Entry(A,textvariable=v);
        self.e_reprows.grid(row=r, column=1, sticky=E); r+=1

        ##########  Convert to 8 bit
        self.e_gray256 = tk.BooleanVar(A, value=False)
        tmp="Clip histogram and convert slices to 8bit before saving"
        tk.Checkbutton(A, text=tmp, fg="magenta4", \
                variable=self.e_gray256).grid(row=r, column=0)
        r+=1

        tk.Label(A, text="Min value in 32-bit histogram"\
                ,fg="magenta4").grid(row=r);
        v = tk.DoubleVar(A, value=-3e-4)
        self.e_hmin = tk.Entry(A,textvariable=v);
        self.e_hmin.grid(row=r, column=1, sticky=E); r+=1

        tk.Label(A, text="Max value in 32-bit histogram"\
                ,fg="magenta4").grid(row=r);
        v = tk.DoubleVar(A, value=2e-4)
        self.e_hmax = tk.Entry(A,textvariable=v);
        self.e_hmax.grid(row=r, column=1, sticky=E); r+=1

        tmp="Concatenate only"
        tk.Radiobutton(A, text=tmp, fg="darkgreen",\
                    variable=self.e_ipol, value=1).grid(row=r, column=0)
        r+=1

        f2 = tk.Frame(A)
        f2.grid(row=r, column=0)
        br1 = tk.Label(f2, text="First row",fg="darkgreen")
        self.e_r1 = tk.Entry(f2,textvariable=tk.IntVar(A, value=40),width=10);
        br2 = tk.Label(f2, text="Last row",fg="darkgreen")
        self.e_r2 = tk.Entry(f2,textvariable=tk.IntVar(A, value=440),width=10);
        br1.pack(side="left")
        self.e_r1.pack(side="left")
        br2.pack(side="left")
        self.e_r2.pack(side="left")
        r+=1

        tmp="Half acqusition mode"
        tk.Radiobutton(A, text=tmp, fg="brown",\
                    variable=self.e_ipol, value=2).grid(row=r, column=0)
        r+=1

        tk.Label(A, text="In which column the axis of rotation is"\
                    ,fg="brown").grid(row=r);
        v = tk.IntVar(A, value=245)
        self.e_ax = tk.Entry(A,textvariable=v);
        self.e_ax.grid(row=r, column=1, sticky=E); r+=1

        #launch buttons
        f1 = tk.Frame(A)
        b1 = tk.Button(f1, text='Stitch', fg="red3", command=self.stitch)
        b2 = tk.Button(f1, text='Delete output dir', command=self.rm_rec_dir)
        b2.configure(state=tk.DISABLED)
        b3 = tk.Button(f1, text='Help', command=self.printhelp)
        b4 = tk.Button(f1, text='Quit', command=self.clean_and_quit)
        f1.grid(row=r, column = 0, columnspan = 2)
        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")
        b4.pack(side="left")

    def rm_rec_dir(self):
        if os.path.exists(self.e_output.get()):
           os.system( 'rm -r {}'.format(self.e_output.get()) )
           print "Directory with reconstructed data was removed"

    def select_input(self):
        cwd = '/mnt/BMIT_data/BMIT-USERS-DATA'
        tmp = "Select directory with CT scans"
        self.output = filedialog.askdirectory(\
                  initialdir = cwd, title = tmp)
        self.e_input.delete(0, tk.END)
        self.e_input.insert(0, self.output)

    def select_output(self):
        cwd = '/mnt/BMIT_data/BMIT-USERS-DATA/rec'
        tmp = "Select output directory"
        self.output = filedialog.askdirectory(\
                  initialdir = cwd, title = tmp)
        self.e_output.delete(0, tk.END)
        self.e_output.insert(0, self.output)

    def select_tmpdir(self):
        tmp = "Select temporary directory"
        self.tmpdir = filedialog.askdirectory(\
                  initialdir = "/data", title = tmp)
        self.e_tmpdir.delete(0, tk.END)
        self.e_tmpdir.insert(0, self.tmpdir)

    def stitch(self):
        args=tk_args(self.e_input, self.e_output, self.e_tmpdir,
                self.e_typ, self.e_ort, self.e_slices, self.e_flip, self.e_ipol,
                self.e_reprows, self.e_gray256, self.e_hmin, self.e_hmax,
                self.e_r1,self.e_r2, self.e_ax)

        if os.path.exists(self.e_tmpdir.get()):
            os.system( 'rm -r {}'.format(self.e_tmpdir.get()) )

        if os.path.exists(self.e_output.get()):
            raise ValueError('Output directory exists')

        if self.e_ipol.get() == 0:
            main_sti_mp(args)
        elif self.e_ipol.get() == 1:
            main_conc_mp(args)
        else:
            main_360_mp_depth1(args)

    def clean_and_quit(self):
        if os.path.exists(self.e_tmpdir.get()):
           os.system( 'rm -r {}'.format(self.e_tmpdir.get()) )
           print "==== Directory with temporary data was removed. ==="
        self.A.quit()

    def printhelp(self):
        h="Stitches images vertically\n"
        h+="Directory structure is, f.i., Input/000, Input/001,...Input/00N\n"
        h+="Each 000, 001, ... 00N directory must have identical subdirectory \"Type\"\n"
        h+="Selected range of images from \"Type\" directory will be stitched vertically\n"
        h+="across all subdirectories in the Input directory"
        h+="to be added as options:\n"
        h+="(1) orthogonal reslicing, (2) interpolation, (3) horizontal stitching"
        print h

if __name__=="__main__":
    A = tk.Tk()
    gui = GUI(A)
    A.mainloop()


text:
from typing import Tuple

import numpy as np

from ...nlu.dto.nlu_response import NLUResponse

class PolicyPrediction:
    """
    Used to store policy model predictions and hidden values.
    """
    def __init__(self, probs, prediction, hidden_outs, cell_state):
        self.probs = probs
        self.prediction = prediction
        self.hidden_outs = hidden_outs
        self.cell_state = cell_state
        self.predicted_action_ix = np.argmax(probs)
        self.utterance_features = None

    def get_network_state(self) -> Tuple:
        return self.cell_state, self.hidden_outs

    def set_utterance_features(self, features: NLUResponse):
        self.utterance_features = features

    def get_utterance_features(self) -> NLUResponse:
        return self.utterance_features


text:
from pathlib import Path
import json

from PIL import Image
import numpy as np

from pycococreatortools import pycococreatortools

SAMPLE_IMAGE_PATH = Path(__file__).parent / "sample.png"
assert SAMPLE_IMAGE_PATH.exists()

raw_image_bitmask = Image.open(SAMPLE_IMAGE_PATH)
image_bitmask = np.asarray(raw_image_bitmask.convert("1")).astype(np.uint8)

annotation_infos = pycococreatortools.create_annotation_infos(
    1, 1, {"id": 1}, image_bitmask, raw_image_bitmask.size, tolerance=2, connectivity=1
)

print(f"Found {len(annotation_infos)} annotations in the image:")
print(json.dumps(annotation_infos, indent=4))

max_stars_repo_name: TathyaEarth/pycococreator

max_issues_repo_name: TathyaEarth/pycococreator

max_forks_repo_name: TathyaEarth/pycococreator