text:
import numpy as np
import ctypes as ct
from typing import Tuple, List, Optional

from . import types as t
from . import mathctypes as mt
from . import bindingbase as bb

from . import plib

# /**
#  * Makes a mesh that represents the given plane (pose in center).
#  */
# void p_meshprimitives_plane(pCloud *out_points, pMeshIndices *out_indices,
#                             mat4 pose, float width, float height);
plib.p_meshprimitives_plane.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                        mt.mat4, bb.c_float, bb.c_float]

def plane(pose: mt.Array, width: float, height: float) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given plane (pose in center).

    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_plane(bb.ref(out_points), bb.ref(out_indices),
                                mt.cast_array_mat4(pose), width, height)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given plane (pose in center).
#  */
# void p_meshprimitives_raw_plane(pCloud *out_points, pCloud *out_opt_normals,
#                                 mat4 pose, float width, float height);
plib.p_meshprimitives_raw_plane.argtypes = [t.pCloud_p, t.pCloud_p,
                                            mt.mat4, bb.c_float, bb.c_float]

def raw_plane(pose: mt.Array, width: float, height: float) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given plane (pose in center).

    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_plane(bb.ref(out_points), bb.ref(out_opt_normals),
                                    mt.cast_array_mat4(pose), width, height)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given box (pose in center).
#  */
# void p_meshprimitives_box(pCloud *out_points, pMeshIndices *out_indices,
#                           mat4 pose, float width, float height, float extend);
plib.p_meshprimitives_box.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                      mt.mat4, bb.c_float, bb.c_float, bb.c_float]

def box(pose: mt.Array, width: float, height: float, extend: float) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given box (pose in center).

    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_box(bb.ref(out_points), bb.ref(out_indices),
                              mt.cast_array_mat4(pose), width, height, extend)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given box (pose in center).
#  */
# void p_meshprimitives_raw_box(pCloud *out_points, pCloud *out_opt_normals,
#                               mat4 pose, float width, float height, float extend);
plib.p_meshprimitives_raw_box.argtypes = [t.pCloud_p, t.pCloud_p,
                                          mt.mat4, bb.c_float, bb.c_float, bb.c_float]

def raw_box(pose: mt.Array, width: float, height: float, extend: float) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given box (pose in center).

    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_box(bb.ref(out_points), bb.ref(out_opt_normals),
                                  mt.cast_array_mat4(pose), width, height, extend)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given circle (pose in center).
#  * @param vertex_density=2.5f: defines the line length (circle arc length)
#  */
# void p_meshprimitives_circle(pCloud *out_points, pMeshIndices *out_indices,
#                              mat4 pose, float radius, float vertex_density);
plib.p_meshprimitives_circle.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                         mt.mat4, bb.c_float, bb.c_float]

def circle(pose: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given circle (pose in center).

    :param vertex_density=2.5f: defines the line length (circle arc length)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_circle(bb.ref(out_points), bb.ref(out_indices),
                                 mt.cast_array_mat4(pose), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given circle (pose in center).
#  * @param vertex_density=2.5f: defines the line length (circle arc length)
#  */
# void p_meshprimitives_raw_circle(pCloud *out_points, pCloud *out_opt_normals,
#                                  mat4 pose, float radius, float vertex_density);
plib.p_meshprimitives_raw_circle.argtypes = [t.pCloud_p, t.pCloud_p,
                                             mt.mat4, bb.c_float, bb.c_float]

def raw_circle(pose: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given circle (pose in center).

    :param vertex_density=2.5f: defines the line length (circle arc length)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_circle(bb.ref(out_points), bb.ref(out_opt_normals),
                                     mt.cast_array_mat4(pose), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given cylinder hull.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_cylinder_hull(pCloud *out_points, pMeshIndices *out_indices,
#                                     vec3 start_point, vec3 extend, float radius, float vertex_density);
plib.p_meshprimitives_cylinder_hull.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                                mt.vec3, mt.vec3, bb.c_float, bb.c_float]

def cylinder_hull(start_point: mt.Array, extend: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given cylinder hull.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_cylinder_hull(bb.ref(out_points), bb.ref(out_indices),
                                        mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                        radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given cylinder hull.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_cylinder_hull(pCloud *out_points, pCloud *out_opt_normals,
#                                         vec3 start_point, vec3 extend,
#                                         float radius, float vertex_density);
plib.p_meshprimitives_raw_cylinder_hull.argtypes = [t.pCloud_p, t.pCloud_p,
                                                    mt.vec3, mt.vec3,
                                                    bb.c_float, bb.c_float]

def raw_cylinder_hull(start_point: mt.Array, extend: mt.Array,
                      radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given cylinder hull.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_cylinder_hull(bb.ref(out_points), bb.ref(out_opt_normals),
                                            mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                            radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given cylinder.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_cylinder(pCloud *out_points, pMeshIndices *out_indices,
#                                vec3 start_point, vec3 extend, float radius, float vertex_density);
plib.p_meshprimitives_cylinder.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                           mt.vec3, mt.vec3, bb.c_float, bb.c_float]

def cylinder(start_point: mt.Array, extend: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given cylinder.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_cylinder(bb.ref(out_points), bb.ref(out_indices),
                                   mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                   radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given cylinder.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_cylinder(pCloud *out_points, pCloud *out_opt_normals,
#                                    vec3 start_point, vec3 extend,
#                                    float radius, float vertex_density);
plib.p_meshprimitives_raw_cylinder.argtypes = [t.pCloud_p, t.pCloud_p,
                                               mt.vec3, mt.vec3,
                                               bb.c_float, bb.c_float]

def raw_cylinder(start_point: mt.Array, extend: mt.Array,
                 radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given cylinder.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_cylinder(bb.ref(out_points), bb.ref(out_opt_normals),
                                       mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                       radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given cone part hull.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_cone_part_hull(pCloud *out_points, pMeshIndices *out_indices,
#                                      vec3 start_point, vec3 extend,
#                                      float start_radius, float end_radius, float vertex_density);
plib.p_meshprimitives_cone_part_hull.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                                 mt.vec3, mt.vec3,
                                                 bb.c_float, bb.c_float, bb.c_float]

def cone_part_hull(start_point: mt.Array, extend: mt.Array,
                   start_radius: float, end_radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given cone part hull.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_cone_part_hull(bb.ref(out_points), bb.ref(out_indices),
                                         mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                         start_radius, end_radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given cone part hull.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_cone_part_hull(pCloud *out_points, pCloud *out_opt_normals,
#                                          vec3 start_point, vec3 extend,
#                                          float start_radius, float end_radius, float vertex_density);
plib.p_meshprimitives_raw_cone_part_hull.argtypes = [t.pCloud_p, t.pCloud_p,
                                                     mt.vec3, mt.vec3,
                                                     bb.c_float, bb.c_float, bb.c_float]

def raw_cone_part_hull(start_point: mt.Array, extend: mt.Array,
                       start_radius: float, end_radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given cone part hull.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_cone_part_hull(bb.ref(out_points), bb.ref(out_opt_normals),
                                             mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                             start_radius, end_radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given cone.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_cone_hull(pCloud *out_points, pMeshIndices *out_indices,
#                                 vec3 apex_point, vec3 extend,
#                                 float opening_angle_rad, float vertex_density);
plib.p_meshprimitives_cone_hull.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                            mt.vec3, mt.vec3,
                                            bb.c_float, bb.c_float]

def cone_hull(start_point: mt.Array, extend: mt.Array,
              opening_angle_rad: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given cone.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_cone_hull(bb.ref(out_points), bb.ref(out_indices),
                                    mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                    opening_angle_rad, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given cone.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_cone_hull(pCloud *out_points, pCloud *out_opt_normals,
#                                     vec3 apex_point, vec3 extend,
#                                     float opening_angle_rad, float vertex_density);
plib.p_meshprimitives_raw_cone_hull.argtypes = [t.pCloud_p, t.pCloud_p,
                                                mt.vec3, mt.vec3,
                                                bb.c_float, bb.c_float]

def raw_cone_hull(start_point: mt.Array, extend: mt.Array,
                  opening_angle_rad: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given cone.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_cone_hull(bb.ref(out_points), bb.ref(out_opt_normals),
                                        mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                        opening_angle_rad, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given cone.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_cone(pCloud *out_points, pMeshIndices *out_indices,
#                            vec3 apex_point, vec3 extend, float opening_angle_rad, float vertex_density);
plib.p_meshprimitives_cone.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                       mt.vec3, mt.vec3, bb.c_float, bb.c_float]

def cone(start_point: mt.Array, extend: mt.Array, opening_angle_rad: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given cone.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_cone(bb.ref(out_points), bb.ref(out_indices),
                               mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                               opening_angle_rad, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given cone.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_cone(pCloud *out_points, pCloud *out_opt_normals,
#                                vec3 apex_point, vec3 extend,
#                                float opening_angle_rad, float vertex_density);
plib.p_meshprimitives_raw_cone.argtypes = [t.pCloud_p, t.pCloud_p,
                                           mt.vec3, mt.vec3,
                                           bb.c_float, bb.c_float]

def raw_cone(start_point: mt.Array, extend: mt.Array,
             opening_angle_rad: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given cone.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_cone(bb.ref(out_points), bb.ref(out_opt_normals),
                                   mt.cast_array_vec3(start_point), mt.cast_array_vec3(extend),
                                   opening_angle_rad, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given sphere.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_sphere(pCloud *out_points, pMeshIndices *out_indices,
#                              vec3 mid_point, float radius, float vertex_density);
plib.p_meshprimitives_sphere.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                         mt.vec3, bb.c_float, bb.c_float]

def sphere(mid_point: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given sphere.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_sphere(bb.ref(out_points), bb.ref(out_indices),
                                 mt.cast_array_vec3(mid_point), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given sphere.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_sphere(pCloud *out_points, pCloud *out_opt_normals,
#                                  vec3 mid_point, float radius, float vertex_density);
plib.p_meshprimitives_raw_sphere.argtypes = [t.pCloud_p, t.pCloud_p,
                                             mt.vec3, bb.c_float, bb.c_float]

def raw_sphere(mid_point: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given sphere.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_sphere(bb.ref(out_points), bb.ref(out_opt_normals),
                                     mt.cast_array_vec3(mid_point), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given half sphere hull in z direction.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_half_sphere_hull(pCloud *out_points, pMeshIndices *out_indices,
#                                        mat4 pose, float radius, float vertex_density);
plib.p_meshprimitives_half_sphere_hull.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                                   mt.mat4, bb.c_float, bb.c_float]

def half_sphere_hull(pose: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given half sphere hull in z direction.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_half_sphere_hull(bb.ref(out_points), bb.ref(out_indices),
                                           mt.cast_array_mat4(pose), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given half sphere hull in z direction.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_half_sphere_hull(pCloud *out_points, pCloud *out_opt_normals,
#                                            mat4 pose, float radius, float vertex_density);
plib.p_meshprimitives_raw_half_sphere_hull.argtypes = [t.pCloud_p, t.pCloud_p,
                                                       mt.mat4, bb.c_float, bb.c_float]

def raw_half_sphere_hull(pose: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given half sphere hull in z direction.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_half_sphere_hull(bb.ref(out_points), bb.ref(out_opt_normals),
                                               mt.cast_array_mat4(pose), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given half sphere in z direction.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_half_sphere(pCloud *out_points, pMeshIndices *out_indices,
#                                   mat4 pose, float radius, float vertex_density);
plib.p_meshprimitives_half_sphere.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                              mt.mat4, bb.c_float, bb.c_float]

def half_sphere(pose: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given half sphere in z direction.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_half_sphere(bb.ref(out_points), bb.ref(out_indices),
                                      mt.cast_array_mat4(pose), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given half sphere in z direction.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_half_sphere(pCloud *out_points, pCloud *out_opt_normals,
#                                       mat4 pose, float radius, float vertex_density);
plib.p_meshprimitives_raw_half_sphere.argtypes = [t.pCloud_p, t.pCloud_p,
                                                  mt.mat4, bb.c_float, bb.c_float]

def raw_half_sphere(pose: mt.Array, radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given half sphere in z direction.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_half_sphere(bb.ref(out_points), bb.ref(out_opt_normals),
                                          mt.cast_array_mat4(pose), radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given arrow (build by cone and cylinder).
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_arrow_custom(pCloud *out_points, pMeshIndices *out_indices,
#                                    vec3 apex_point, vec3 extend,
#                                    float arrowhead_opening_angle_rad, float arrowhead_length,
#                                    float radius, float vertex_density);
plib.p_meshprimitives_arrow_custom.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                               mt.vec3, mt.vec3,
                                               bb.c_float, bb.c_float,
                                               bb.c_float, bb.c_float]

def arrow_custom(apex_point: mt.Array, extend: mt.Array,
                 arrowhead_opening_angle_rad: float, arrowhead_length: float,
                 radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given arrow (build by cone and cylinder).

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_arrow_custom(bb.ref(out_points), bb.ref(out_indices),
                                       mt.cast_array_vec3(apex_point), mt.cast_array_vec3(extend),
                                       arrowhead_opening_angle_rad, arrowhead_length,
                                       radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given arrow (build by cone and cylinder).
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_arrow_custom(pCloud *out_points, pCloud *out_opt_normals,
#                                        vec3 apex_point, vec3 extend,
#                                        float arrowhead_opening_angle_rad, float arrowhead_length,
#                                        float radius, float vertex_density);
plib.p_meshprimitives_raw_arrow_custom.argtypes = [t.pCloud_p, t.pCloud_p,
                                                   mt.vec3, mt.vec3,
                                                   bb.c_float, bb.c_float,
                                                   bb.c_float, bb.c_float]

def raw_arrow_custom(apex_point: mt.Array, extend: mt.Array,
                     arrowhead_opening_angle_rad: float, arrowhead_length: float,
                     radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given arrow (build by cone and cylinder).

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_arrow_custom(bb.ref(out_points), bb.ref(out_opt_normals),
                                           mt.cast_array_vec3(apex_point), mt.cast_array_vec3(extend),
                                           arrowhead_opening_angle_rad, arrowhead_length,
                                           radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given arrow (build by cone and cylinder).
#  * The opening angle will be 30Â° and the arrowhead_length will be so, that cone_radius = 2*radius.
#  * The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_arrow(pCloud *out_points, pMeshIndices *out_indices,
#                             vec3 apex_point, vec3 extend,
#                             float radius, float vertex_density);
plib.p_meshprimitives_arrow.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                        mt.vec3, mt.vec3,
                                        bb.c_float, bb.c_float]

def arrow(apex_point: mt.Array, extend: mt.Array,
          radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given arrow (build by cone and cylinder).
    The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
    The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_arrow(bb.ref(out_points), bb.ref(out_indices),
                                mt.cast_array_vec3(apex_point), mt.cast_array_vec3(extend),
                                radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given arrow (build by cone and cylinder).
#  * The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
#  * The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_arrow(pCloud *out_points, pCloud *out_opt_normals,
#                                 vec3 apex_point, vec3 extend,
#                                 float radius, float vertex_density);
plib.p_meshprimitives_raw_arrow.argtypes = [t.pCloud_p, t.pCloud_p,
                                            mt.vec3, mt.vec3,
                                            bb.c_float, bb.c_float]

def raw_arrow(apex_point: mt.Array, extend: mt.Array,
              radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given arrow (build by cone and cylinder).
    The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
    The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_arrow(bb.ref(out_points), bb.ref(out_opt_normals),
                                    mt.cast_array_vec3(apex_point), mt.cast_array_vec3(extend),
                                    radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

# /**
#  * Makes a mesh that represents the given arrow (build by cone and cylinder).
#  * The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
#  * The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_arrow_swapped(pCloud *out_points, pMeshIndices *out_indices,
#                                     vec3 end_point, vec3 extend,
#                                     float radius, float vertex_density);
plib.p_meshprimitives_arrow_swapped.argtypes = [t.pCloud_p, t.pMeshIndices_p,
                                                mt.vec3, mt.vec3,
                                                bb.c_float, bb.c_float]

def arrow_swapped(apex_point: mt.Array, extend: mt.Array,
                  radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpMeshIndices]:
    '''
    Makes a mesh that represents the given arrow (build by cone and cylinder).
    The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
    The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_indices
    '''
    out_points = t.pCloud()
    out_indices = t.pMeshIndices()
    plib.p_meshprimitives_arrow_swapped(bb.ref(out_points), bb.ref(out_indices),
                                        mt.cast_array_vec3(apex_point), mt.cast_array_vec3(extend),
                                        radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pMeshIndices(out_indices)

# /**
#  * Makes a raw mesh (each 3 point normals = triangle) that represents the given arrow (build by cone and cylinder).
#  * The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
#  * The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.
#  * @param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
#  */
# void p_meshprimitives_raw_arrow_swapped(pCloud *out_points, pCloud *out_opt_normals,
#                                         vec3 end_point, vec3 extend,
#                                         float radius, float vertex_density);
plib.p_meshprimitives_raw_arrow_swapped.argtypes = [t.pCloud_p, t.pCloud_p,
                                                    mt.vec3, mt.vec3,
                                                    bb.c_float, bb.c_float]

def raw_arrow_swapped(apex_point: mt.Array, extend: mt.Array,
                      radius: float, vertex_density: float = 2.5) \
        -> Tuple[t.NpCloud, t.NpCloud]:
    '''
    Makes a raw mesh (each 3 point normals = triangle) that represents the given arrow (build by cone and cylinder).
    The opening angle will be 30° and the arrowhead_length will be so, that cone_radius = 2*radius.
    The cylinder length must be at least arrowhead_length/2, if not, the radius will be set to fit it.

    :param vertex_density=2.5f: defines the triangle sizes/widths (same as p_meshprimitives_circle)
    :return: out_points, out_opt_normals
    '''
    out_points = t.pCloud()
    out_opt_normals = t.pCloud()
    plib.p_meshprimitives_raw_arrow_swapped(bb.ref(out_points), bb.ref(out_opt_normals),
                                            mt.cast_array_vec3(apex_point), mt.cast_array_vec3(extend),
                                            radius, vertex_density)
    return t.cast_from_pCloud(out_points), t.cast_from_pCloud(out_opt_normals)

max_stars_repo_name: renehorstmann/pbase

max_issues_repo_name: renehorstmann/pbase

max_forks_repo_name: renehorstmann/pbase


text:
#!/usr/bin/env python3

"""Plots the stationary distribution.

Usage:
    plot_stationary_dist.py -h | --help
    plot_stationary_dist.py <file_name>... [options]

Options:
    -h --help                 Show this screen
    --x_lim=<x_lim>           Change the x-axis limits.
    --sep                     Whether to use a separate figure for each file.
"""

import matplotlib.pyplot as plt
import numpy as np
import scipy.misc as misc

from docopt import docopt

def get_mean(file_name):
    mean = None

    f = open(file_name, "r")
    for line in f:
        if "#" not in line:
            break
        if "means" in line:
            mean = float(line.split(" ")[-1])
            break
    f.close()

    if not mean:
        data = np.loadtxt(file_name)
        mean = np.argmax(data)

    return mean

def get_range(data_len, x_lim):
    x_lim = x_lim + [0, data_len][len(x_lim):]
    tick_step = max((x_lim[1] - x_lim[0]) // 10, 1)
    x_lim += [tick_step]
    return x_lim

def get_data(file_name):
    y = np.atleast_2d(np.loadtxt(file_name))
    x = np.arange(y.shape[1])

    return x, y

def get_analytic(mean, x_lim):
    x = np.arange(x_lim[0], x_lim[1])
    y = 1.0 / (misc.factorial(x)) * mean ** x * np.exp(-mean)

    return x, y

def plot(d, name, lim, sep_figs=False):
    if sep_figs:
        plt.figure(name)
        plt.bar(d[0], d[1][0] / np.sum(d[1][0]), label="Stochastic")
    else:
        plt.bar(d[0], d[1][0] / np.sum(d[1][0]), label=name)

    for i in range(1, len(d[1])):
        plt.plot(d[0], d[1][i]/np.sum(d[1][i]))

    plt.xlim(lim[0]-0.5, lim[1]+0.5)
    plt.gca().set_xticks(np.arange(lim[0], lim[1]+1, lim[2]))
    plt.title("Stationary dist.")
    plt.xlabel("# of molecules")
    plt.ylabel("Probability")
    plt.legend()

if __name__ == '__main__':

    # Get the command line arguments
    args = docopt(__doc__)
    if args["--x_lim"]:
        args["--x_lim"] = [int(x) for x in args["--x_lim"].split(",")]
    else:
        args["--x_lim"] = []

    redraw = True
    for name in args["<file_name>"]:
        data = get_data(name)
        limits = get_range(len(data[0]), args["--x_lim"])
        plot(data, name, limits, args["--sep"])

        if redraw:
            d_a = get_analytic(get_mean(name), limits)
            plt.plot(d_a[0], d_a[1], label="Analytical", color="black")
            if args["--sep"]:
                redraw = True
            else:
                redraw = False

    plt.show()

ext: py

max_stars_repo_name: BartoszBartmanski/StoSpa

max_issues_repo_name: BartoszBartmanski/StoSpa

max_forks_repo_name: BartoszBartmanski/StoSpa

reason: import numpy,import scipy


text:
import numpy as np
import itertools
import tensorflow as tf
from tensorflow.keras.datasets import mnist
import os

# change tensorflow level for removing warning message
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def test1():
    assert 1 == 0

def test2():
    a = tf.constant(np.array([1, 0, 0, 1]), dtype=tf.float32)

    result = tf.constant(np.array([3, 0, 0, 3]), dtype=tf.float32)
    assert tf.reduce_all(tf.equal(3 * a, result))

def test3():
    a = tf.constant(np.array([1, 2, 3, 4]), dtype=tf.float32)
    state_diag = tf.linalg.diag(a)

    result = tf.constant(np.array([[1, 0, 0, 0],
                                   [0, 2, 0, 0],
                                   [0, 0, 3, 0],
                                   [0, 0, 0, 4]]), dtype=tf.float32)
    assert tf.reduce_all(tf.equal(state_diag, result))

def test4():
    a = tf.constant(np.array([1, 2, 3, 4]), dtype=tf.float32)
    b = tf.constant(np.array([3, 1, 3, 1]), dtype=tf.float32)
    result = tf.maximum(0, a - b)
    answer = tf.constant(np.array([0, 1, 0, 3]), dtype=tf.float32)
    assert tf.reduce_all(tf.equal(answer, result))

max_stars_repo_name: kwonhyeokmin/ImageSimilarity

max_issues_repo_name: kwonhyeokmin/ImageSimilarity

max_forks_repo_name: kwonhyeokmin/ImageSimilarity

reason: import numpy


text:
from numpy import pi, e, array
import codecs
import urllib.request
import os.path
import os
import multiprocessing
from urllib.error import HTTPError

def download_html(data_directory, which="all"):
    """
    Download the html files with mass data.

    Parameters
    ----------
    data_directory : string
        Name of the folder of downloaded data.

    which : list, string
        A list of element names by their abbreviations, or a single
        element as a string.
    """
    element_names_list = ["H", "He", "Li", "Be", "B", "C", "N", "O", "F",
        "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca",
        "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga",
        "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo",
        "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I",
        "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu",
        "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W",
        "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po",
        "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am",
        "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db",
        "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc",
        "Lv", "Ts", "Og", "all"]

    element_names_dict = {"H":1, "He":2, "Li":3, "Be":4, "B":5, "C":6, "N":7,
        "O":8, "F":9, "Ne":10, "Na":11, "Mg":12, "Al":13, "Si":14, "P":15,
        "S":16, "Cl":17, "Ar":18, "K":19, "Ca":20, "Sc":21, "Ti":22, "V":23,
        "Cr":24, "Mn":25, "Fe":26, "Co":27, "Ni":28, "Cu":29, "Zn":30, "Ga":31,
        "Ge":32, "As":33, "Se":34, "Br":35, "Kr":36, "Rb":37, "Sr":38, "Y":39,
        "Zr":40, "Nb":41, "Mo":42, "Tc":43, "Ru":44, "Rh":45, "Pd":46, "Ag":47,
        "Cd":48, "In":49, "Sn":50, "Sb":51, "Te":52, "I":53, "Xe":54, "Cs":55,
        "Ba":56, "La":57, "Ce":58, "Pr":59, "Nd":60, "Pm":61, "Sm":62, "Eu":63,
        "Gd":64, "Tb":65, "Dy":66, "Ho":67, "Er":68, "Tm":69, "Yb":70, "Lu":71,
        "Hf":72, "Ta":73, "W":74, "Re":75, "Os":76, "Ir":77, "Pt":78, "Au":79,
        "Hg":80, "Tl":81, "Pb":82, "Bi":83, "Po":84, "At":85, "Rn":86, "Fr":87,
        "Ra":88, "Ac":89, "Th":90, "Pa":91, "U":92, "Np":93, "Pu":94, "Am":95,
        "Cm":96, "Bk":97, "Cf":98, "Es":99, "Fm":100, "Md":101, "No":102,
        "Lr":103, "Rf":104, "Db":105, "Sg":106, "Bh":107, "Hs":108, "Mt":109,
        "Ds":110, "Rg":111, "Cn":112, "Nh":113, "Fl":114, "Mc":115, "Lv":116,
        "Ts":117, "Og":118}

    if which == "all": which = element_names_list[:-1]

    if isinstance(which, str): which = [which]
    elif isinstance(which, list):
        if not set(which) < set(element_names_list):
            print("You have specified invalid elements. " +
            " Please use the two letter abbreviations, either in a list" +
            " or a single element as a string.")
            print(element_names_list)
            return
    else:
        print(f"Valid input types are {list} and {str}, got {type(which)}.")
        return

    if not os.path.isdir(data_directory): os.mkdir(data_directory)

    pool = multiprocessing.Pool()
    pool.map(fetch_data_in_parallel,
        ((i, which, element_names_dict, data_directory) for i in range(len(which))))

def fetch_data_in_parallel(index_list_dict_folder):
    """
    The delay between request and recieve makes fetching data for many
    elements slow. This function is parallelised with the built-in
    multiprocessing module for fetching mass data in parallel.

    Parameters
    ----------
    index_list_dict_folder : tuple
        Since pool.map only supports a single function argument, all
        objects needed inside this function is passed as a tuple.

        i : int
            Current index of the input list of elements to fetch.

        which : list
            A list of elements to fetch.

        element_names_dict : dictionary
            A dictionary where key = element name, value = element
            number.

        data_directory : string
            Name of the sub-directory where the mass data are stored.
    """
    i, which, element_names_dict, data_directory = index_list_dict_folder

    element_number = f"{element_names_dict[which[i]]:03d}"
    filepath = data_directory + "/" + element_number + "-" + which[i] + "-masses.html"
    element_number = f"{element_names_dict[which[i]]:02d}"

    if not os.path.isfile(filepath):
        urlname = "https://wwwndc.jaea.go.jp/cgi-bin/nucltab14?" + element_number
        try:
            urllib.request.urlretrieve(urlname, filepath)
            print(filepath + " downloaded")
        except HTTPError:
            print(f"Unable to download {urlname}. Invalid URL.")

def extract_mass_from_html(filename):
    """
    Read (pre-downloaded) html files from 'wwwndc.jaea.go.jp' with mass
    data.
    """
    data = {}
    names = []

    with codecs.open(filename, "r") as infile:

        for i in range(3): infile.readline()    # Skip non-interesting lines.
        infile.readline()[4:14]  # Extract the name of the element.
        infile.readline()
        categories = infile.readline()  # Extract the titles of the table.
        infile.readline()

        max_iterations = 1000   # Avoid infinite loop.
        iterations = 0

        while True: # Iterate through the file, line by line.
            content = infile.readline()
            content = content.split()

            if (content[0][0:4] == "----"): break   # End of file.
            if (iterations > max_iterations):
                print("Reached maximum number of iterations. The program did"
                    + " most likely not find the end of the file.")
                break

            name = ""
            reference_point = 0 # Index of list element reference point where to find value and name data.
            name_location_1 = 0 # Substring index of (partial or entire) name of the isotope.

            for elem in content:
                """
                Use '' as a point of reference.  Mass value is
                always the next element in the list 'content' from
                ''.
                """
                if (elem[-4:] == ""):
                    if (len(elem) > 4):
                        """
                        The name of the element is sometimes (partially
                        or entirely) in the same string as '', eg:
                        '98', so we extract all characters before
                        '<', which has index -4, if the length of elem
                        is greater than 4.
                        """
                        name = elem[0:-4]

                        for char in elem[:-1]:
                            """
                            Sometimes the entire name is in the same
                            string as '', eg: '...>Tc-102', and
                            the name is always preceeded by '>'.  We
                            look for '>' to extract only the name of the
                            isotope. If the name is only partially
                            contained with '' like '98', then
                            there is no nothing to strip away in front,
                            but we must make sure that the final '>' is
                            not mistaken as the character preceeding the
                            name, hence 'elem[:-1].
                            """
                            name_location_1 += 1

                            if (char == ">"):
                                name = elem[name_location_1:-4]
                                break

                    break   # Break the loop if we find ''.

                reference_point += 1

            try:
                """
                If the next element of 'content' can be converted to
                float, then it is the mass of the isotope.
                """
                value = float(content[reference_point+1])
            except ValueError:
                """
                If the next element of 'content' cannot be converted to
                float, then the line is not interesting and can be
                skipped entirely.
                """
                continue

            name_location_2 = 0 # Substring index of (partial or entire) name of the isotope.

            try:
                """
                If the previous element of 'content' can be converted to
                int, then that element is a part of the name of the
                isotope, ex. [..., ...H-, 1, ], and the rest of the
                name is located 2 steps back in the list.
                """
                name = int(content[reference_point-1])
                name = str(name)
                reference_point -= 2
            except ValueError:
                """
                If the previous element of 'content' cannot be converted
                to int, then the rest of the name is located 1 step back
                in the list.
                """
                reference_point -= 1

            for char in content[reference_point]:
                """
                Using '' to find the start of the name of the
                isotope.
                """
                name_location_2 += 1
                if (char == ">"): break

            name = content[reference_point][name_location_2:] + name
            name = name.replace("-", "")
            names.append(name)
            data[name] = value

            iterations += 1

        # for key in data:
        #     print(key, data[key])

        return data, names

class MassesClass(dict):
    def __init__(self, which="all", unit="kg", suppress_print=False):
        """
        Parameters
        ----------
        which : string, list
            A single string, or a list of strings, containing the
            abbreviated names of the elements you wish to download and
            load the mass data for. Note that the download only happens
            the first time you run the program, so it is probably the
            best to just leave this to the default value of 'all'. Input
            is case sensitive. Arbitrary ordering of input elements is
            allowed.

            Examples:
                which = 'all' - download and load all elements.
                which = 'U' - download and load mass data for uranium.
                which = ['H', 'He', 'Au'] - download and load mass data
                    for hydrogen, helium, and gold.
        """
        self.amu_to_kg = 1.66053904e-27     # 1 amu in kg.
        self.u_to_kg = self.amu_to_kg       # Alias.
        self.kg_to_amu = 1/self.amu_to_kg   # 1 kg in amu.
        self.kg_to_u = self.kg_to_amu       # Alias.

        self.amu_to_eV = 931.49410242e6     # 1 amu in eV/c**2.
        self.amu_to_ev = self.amu_to_eV     # Alias.
        self.u_to_eV = self.amu_to_eV       # Alias.
        self.u_to_ev = self.amu_to_eV       # Alias.
        self.eV_to_amu = 1/self.amu_to_eV   # 1 eV/c**2 in amu.
        self.ev_to_amu = self.eV_to_amu     # Alias.
        self.eV_to_u = self.eV_to_amu       # Alias.
        self.ev_to_u = self.eV_to_amu       # Alias.

        self.eV_to_kg = 1.782662e-36        # 1 eV/c**2 in kg.
        self.ev_to_kg = self.eV_to_kg       # Alias.
        self.kg_to_eV = 1/self.eV_to_kg  # 1 kg in eV/c**2.
        self.kg_to_ev = self.kg_to_eV       # Alias.

        self.mass_data_in_amu = {}
        self.mass_data_in_eV = {}
        self.mass_data_in_kg = {}
        self.names = []

        self.current_unit = ""

        # NUCLEAR DATA PART IS CURRENTLY NOT WORKING. MUST BE DEBUGGED.
        # mass_data_tmp = {}
        # names_tmp = []

        # data_directory = "nuclear_data/"

        # if __name__ == "__main__":
        #     """
        #     Does not work in an interactive session. Run the script
        #     directly before interactive usage.
        #     """
        #     download_html(data_directory, which)
        # else:
        #     if not os.path.isdir(data_directory):
        #         print("Mass data files from https://wwwndc.jaea.go.jp/NuC/"
        #          + " must be downloaded once first by directly running this script.")
        #         print(f"Script located at {os.path.dirname(os.path.realpath(__file__))}")
        #         return
        #     elif (len(os.listdir(data_directory)) == 0):
        #         print("Mass data files from https://wwwndc.jaea.go.jp/NuC/"
        #          + " must be downloaded once first by directly running this script.")
        #         print(f"Script located at {os.path.dirname(os.path.realpath(__file__))}")
        #         return

        # for filename in os.listdir(data_directory):
        #     if filename.endswith(".html"):
        #         mass_data_tmp, names_tmp = extract_mass_from_html(data_directory + filename)
        #         self.mass_data_in_amu.update(mass_data_tmp)
        #         self.names += names_tmp

        # for key in self.mass_data_in_amu:
        #     self.mass_data_in_eV[key] = self.mass_data_in_amu[key]*self.amu_to_eV
        #     self.mass_data_in_kg[key] = self.mass_data_in_amu[key]*self.amu_to_kg

        # self.names = sorted(self.names)
        self.set_unit(unit, suppress_print=True)

    def set_unit(self, unit, suppress_print=False):
        """
        Set the mass unit.

        Parameters
        ----------
        unit : string
            Valid inputs are: 'ev', 'eV', 'ev/c^2', 'ev/c**2', 'eV/c^2',
            'eV/c**2', 'amu', 'u', 'kg'. Defaults to 'ev'.
        """

        if (unit == "ev") or (unit == "eV") or (unit == "ev/c^2") or (unit == "ev/c**2") or (unit == "eV/c^2") or (unit == "eV/c**2"):
            self.current_unit = "eV"
            self.set_attributes(self.mass_data_in_eV)
            fac = self.kg_to_eV # Base unit of all other masses is kg.
            if not suppress_print:
                print(f"Unit set to 'eV/c**2'.")

        elif (unit == "amu") or (unit == "u"):
            self.current_unit = "amu"
            self.set_attributes(self.mass_data_in_amu)
            fac = self.kg_to_amu    # Base unit of all other masses is kg.
            if not suppress_print:
                print(f"Unit set to 'amu'.")

        elif (unit == "kg"):
            self.current_unit = "kg"
            self.set_attributes(self.mass_data_in_kg)
            fac = 1 # Base unit of all other masses is kg.
            if not suppress_print:
                print(f"Unit set to 'kg'.")

        else:
            print(f"Invalid unit. Use 'eV', 'amu', or 'kg'. Got {unit}.")
            return

        # Nuclear elements (mass of nuclei).
        self.alpha = 6.644657230e-27*fac        # Mass of alpha particle, base [kg].

        # Hadrons.
        self.p = 1.6726219e-27*fac              # mass of proton, base [kg].
        self.proton = self.p
        self.n = 1.67492749804e-27*fac          # mass of neutron, base [kg].
        self.neutron = self.n

        # Leptons.
        self.e   = 9.10938356e-31*fac           # mass of electron, base [kg].
        self.electron = self.e
        self.mu  = 1.8835315972221714e-28*fac   # mass of muon, base [kg].
        self.muon = self.mu
        self.tau = 3.16754080132e-27*fac        # mass of tau, base [kg].
        self.tauon = self.tau

        # Mesons.
        self.pion = array([2.4880613534e-28, 2.406176557092e-28, 2.4880613534e-28])*fac # pi minus, pi zero, pi plus masses, base [kg].
        self.pi = self.pion
        self.pi_m = self.pion[0]    # Alias.
        self.pim = self.pion[0]     # Alias.
        self.pi_z = self.pion[1]    # Alias.
        self.piz = self.pion[1]     # Alias.
        self.pi_0 = self.pion[1]    # Alias.
        self.pi0 = self.pion[1]     # Alias.
        self.pi_n = self.pion[1]    # Alias.
        self.pin = self.pion[1]     # Alias.
        self.pi_p = self.pion[2]    # Alias.
        self.pip = self.pion[2]     # Alias.

        self.kaon = array([8.80059228174e-28, 8.87138178976e-28, 8.80059228174e-28])*fac # K minus, K zero, K plus masses, base [kg].
        self.K_m = self.kaon[0]    # Alias.
        self.Km = self.kaon[0]     # Alias.
        self.K_z = self.kaon[1]    # Alias.
        self.Kz = self.kaon[1]     # Alias.
        self.K_0 = self.kaon[1]    # Alias.
        self.K0 = self.kaon[1]     # Alias.
        self.K_n = self.kaon[1]    # Alias.
        self.Kn = self.kaon[1]     # Alias.
        self.K_p = self.kaon[2]    # Alias.
        self.Kp = self.kaon[2]     # Alias.

        ## Astronomy.
        self.sol = 1.9891e30*fac               # mass of sun, base [kg]
        self.sun = self.sol
        self.earth = 5.972e24*fac              # mass of earth, base [kg]

    def print_isotopes(self, which="all"):
        """
        Print a given set of available isotopes.

        Parameters
        ----------
        which : string
            Choose element for which to list all available isotopes and
            masses. Defaults to all.
        """
        if (self.current_unit == "eV"):
            current_mass_data = self.mass_data_in_eV
        elif (self.current_unit == "amu"):
            current_mass_data = self.mass_data_in_amu
        elif (self.current_unit == "kg"):
            current_mass_data = self.mass_data_in_kg

        for name in self.names:
            if (name.startswith(which)) or (which == "all"):
                print(f"{name}: {current_mass_data[name]}")

    def set_attributes(self, *args, **kwargs):
        """
        Update class attributes with entries in input dictionary. Each
        key will be the name of the attribute and each value will be
        the value of the attribute.
        """
        super(MassesClass, self).__init__(*args, **kwargs)
        self.__dict__.update(self)

class LifetimeClass:

    # Leptons.
    mu = 2.1969811e-6   # Muon lifetime, [s].
    muon = mu

    tau = 2.903e-13     # Tau lifetime, [s].
    tauon = tau

    # Mesons.
    pion = array([2.6e-8, 8.4e-17, 2.6e-8])    # pi minus, pi zero, pi plus lifetimes, [s].
    pi = pion
    pi_m = pion[0]
    pi_z = pion[1]
    pi_n = pi_z
    pi_p = pion[2]

    def __init__(self):
        pass

class HalfLifeClass:

    Mo99 = 237513.6         # Half-life of molybdenum, [s].
    mo99 = Mo99             # Alias.
    Tc99 = 6661667078600    # Half-life of technetium, [s].
    tc99 = Tc99             # Alias.
    Tc99m = 216241200       # Half-life of first excited state of technetium, [s].
    tc99m = Tc99m           # Alias.

    def __init__(self):
        pass

class UnitsClass:
    """
    Class of non-SI units converted to the base SI equivalent.
    """
    curie = 3.7e10  # Unit of radioactivity, [1/s].
    ci = curie
    Ci = curie

    barn = 1e-28    # Unit for expressing the cross-sectional area of nuclear reactions, [m**2].
    b = barn

    def __init__(self):
        pass

m = MassesClass()
_m_internal = MassesClass(suppress_print=True)  # For use inside this file.
t = LifetimeClass()
hl = HalfLifeClass()
u = UnitsClass()

man = "constants.m for masses. constants.t for lifetimes. constants.u for units. constants.hl for half-lives."
manual = man

# constants
c        = 299792458                     # Speed of light in vacuum, [m/s]
h        = 6.62607004e-34                # Planck's constant, [m^2*kg/s]
hbar     = h/(2*pi)                      # reduced Planck's constant, [m^2*kg/s]
h_ev     = 4.135667696e-15               # Planck's constant, [eV*s]
hbar_ev  = h_ev/(2*pi)                   # Reduced Planck's constant, [eV*s]
kb       = 1.38064852e-23                # Boltzmann's constant, [m^2*kg/(k*s^2)]
kb_ev    = 8.6173303e-5                  # Boltzmann's constant, [ev/K]
G        = 6.67408e-11                   # gravitational constant, [m^3/(kg*s^2)]
g        = 9.807                         # gravitational acceleration, [m/s**2]
R        = 8.3144598                     # gas constant, [J/(K mol)]
pi       = pi                            # Ratio between circumference and diameter of a circle, [unitless].
e        = e                             # Eulers number, [unitless].
sigma    = 2*pi**5*kb**4/(15*c**2*h**3)  # Stefan-Boltzmanns constant, [W/(m^2*K^4)].
# constants, units
c_unit     = "speed of light m/s"
h_unit     = "planck constant m^2*kg/s"
hbar_unit  = "reduced planck constant m^2*kg/s"
kb_unit    = "boltzmann constant m^2*kg/(K*s^2) = J/K"
kb_ev_unit = "boltzmann constant ev/K"
G_unit     = "universal gravitational constant m^3/(kg*s^2)"
g_unit     = "gravitational acceleration m*s^-2"
R_unit     = "gas constant J/(K mol)"
pi_unit    = "ratio between circumference and diameter of a circle unitless"
e_unit     = "eulers number unitless"
sigma_unit = "stefan-boltzmann constant W/(m^2*K^4)"

# lengths
AU = 149597871                  # astronomical unit, [km]
ly = 9.4605284e15               # light year, [m]
pc = 3.08567758e16              # parsec, [m]
r_sol = 695508e3                # solar radius, [m]
r_earth = 6371e3                # earth radius, [m]

# lengths, units
AU_unit  = "km"
ly_unit = "m"
pc_unit = "m"
r_sol_unit = "m"
r_earth_unit = "m"

# energies
eV_J = 1.60217662e-19           # electron volt to joule, [J]
ev_j = eV_J # Alias.
ev_J = eV_J # Alias.
ev_joule = eV_J # Alias.

# energies, units
eV_J_unit = "J"

# unsorted
atm         = 101325                  # atmospheric pressure, [Pa]
mol         = 6.0221415e23            # mole
T_sol       = 1.57e7                  # core temp of the sun, [K]
L_sol       = 3.828e26                # solar luminosity, [W]
yr          = 31556926                # seconds in a year, [s]
ec          = 1.60217662e-19          # elementary charge, [coulomb]
alpha       = 0.0072973525693         # fine structure constant
mu_b        = ec*hbar/(2*_m_internal.electron)
# unsorted, units
atm_unit   = "atmospheric pressure [Pa]"
mol_unit   = "mole [unitless]"
T_sol_unit = "core temperature of the sun [K]"
L_sol_unit = "solar luminosity [W]"
yr_unit    = "seconds in a year [s]"
ec_unit    = "elementary charge [C]"
alpha_unit = "fine structure constant [unitless]"
mu_b_unit  = "bohr magneton [J/T]"

reason: from numpy


text:
import argparse
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense
from spectral_normalization_core import SNDense

parser = argparse.ArgumentParser()
parser.add_argument('--use_tpu', action='store_true')
pargs = parser.parse_args()

def get_session(tpu_cluster_resolver=None):
    if pargs.use_tpu and tpu_cluster_resolver is not None:
        config = tf.ConfigProto()
        config.allow_soft_placement = True
        cluster_spec = tpu_cluster_resolver.cluster_spec()
        if cluster_spec:
            config.cluster_def.CopyFrom(cluster_spec.as_cluster_def())
        return tf.Session(
            target=tpu_cluster_resolver.master(),
            config=config)

    config = tf.ConfigProto()
    config.allow_soft_placement = True
    return tf.Session(config=config)

def tpu_decorator(func):
    def wrapper(*args, **kwargs):
        tf.keras.backend.clear_session()
        if pargs.use_tpu:
            tpu_grpc_url = "grpc://" + os.environ["COLAB_TPU_ADDR"]
            tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu_grpc_url)
            tf.contrib.distribute.initialize_tpu_system(tpu_cluster_resolver)
            strategy = tf.contrib.distribute.TPUStrategy(tpu_cluster_resolver)
            kwargs['tpu_cluster_resolver'] = tpu_cluster_resolver
            with strategy.scope():
                return func(*args, **kwargs)
        return func(*args, **kwargs)
    return wrapper

def power_iteration(W, u, power_iter=1):
    W_T = tf.transpose(W)
    for i in range(power_iter):
        v = tf.nn.l2_normalize(tf.matmul(u, W))  # 1 x filters
        u = tf.nn.l2_normalize(tf.matmul(v, W_T))
    v_bar = tf.stop_gradient(v)
    u_bar = tf.stop_gradient(u)
    sigma_W = tf.squeeze(tf.matmul(tf.matmul(u_bar, W), tf.transpose(v_bar)))
    return u_bar, sigma_W, v_bar

def build_model(input_dim, output_dim):
    inputs = Input((input_dim,))
    outputs = SNDense(output_dim)(inputs)
    model = Model(inputs, outputs)
    return model

@tpu_decorator
def sv_estimation_test(input_dim, output_dim, tpu_cluster_resolver=None):
    model = build_model(input_dim, output_dim)
    W = model.layers[-1].kernel
    u = model.layers[-1].u
    x = tf.ones((1, input_dim), tf.float32)
    y = model(x)
    _, sigma_W, _ = power_iteration(W, u, power_iter=1)
    _, sigma_W_100, _ = power_iteration(W, u, power_iter=100)

    with get_session(tpu_cluster_resolver=tpu_cluster_resolver) as sess:
        sess.run(tf.global_variables_initializer())
        s = sess.run(sigma_W)
        s_100 = sess.run(sigma_W_100)

        W_np = sess.run(W)
        _u, sv, _vh = np.linalg.svd(W_np)

        print('[singular value estimation test]')
        print('power iteration with tensorflow')
        print('      1 iteration  : {:}'.format(s))
        print('    100 iteration  : {:}'.format(s_100))
        print('estimation by numpy: {:}'.format(sv[0]))
        print()

@tpu_decorator
def grad_test(input_dim, output_dim, tpu_cluster_resolver=None):
    model = build_model(input_dim, output_dim)
    x = tf.ones((1, input_dim), tf.float32)
    y = model(x)
    W = model.layers[-1].kernel
    u = model.layers[-1].u
    u_bar, sigma_W, v_bar = power_iteration(W, u, power_iter=1)
    W_bar = W / sigma_W

    grad = tf.gradients(ys=W_bar, xs=W)

    uv = tf.matmul(tf.transpose(u_bar), v_bar)
    grad_sigma_inv = -uv / sigma_W ** 2
    grad2 = 1 / sigma_W + grad_sigma_inv * tf.reduce_sum(W)

    with get_session(tpu_cluster_resolver=tpu_cluster_resolver) as sess:
        sess.run(tf.global_variables_initializer())
        g, = sess.run(grad)
        g2 = sess.run(grad2)
        print('[gradients calculation test]')
        print('gradients calculated by tf.gradients()')
        print(g)
        print('gradients calculated by exact solution')
        print(g2)
        print()

@tpu_decorator
def sv_assign_test(input_dim, output_dim, training=True, tpu_cluster_resolver=None):
    model = build_model(input_dim, output_dim)
    W = model.layers[-1].kernel
    u = model.layers[-1].u
    x = tf.ones((1, input_dim), tf.float32)
    y = model(x, training=training)
    update_ops = model.updates
    u_power_iter, _, _ = power_iteration(W, u, power_iter=1)

    with get_session(tpu_cluster_resolver=tpu_cluster_resolver) as sess:
        sess.run(tf.global_variables_initializer())
        u0 = sess.run(u)
        u1_power_iter = sess.run(u_power_iter)
        sess.run(update_ops)
        u1 = sess.run(u)

        print('[singular vector assign test when training = ' + str(training) + ']')
        print('initial singular vector')
        print(u0)
        print('assigned singular vector')
        print(u1)
        print('singular vector calculated outside of Model')
        print(u1_power_iter)
        print()

if __name__ == '__main__':
    input_dim = 10
    output_dim = 2
    print('input dim: 10')
    print('output dim: 2')

    sv_estimation_test(input_dim, output_dim)
    grad_test(input_dim, output_dim)
    sv_assign_test(input_dim, output_dim, training=True)
    sv_assign_test(input_dim, output_dim, training=False)


text:
import csv
import json
import numpy as np

import matplotlib.pyplot as plt

from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

from keras.models import Model
from keras import backend as K
from keras.layers import Input, Embedding, Dropout, Conv1D, GlobalMaxPooling1D, Concatenate, Dense

# = = = = = functions = = = = =

def visualize_doc_embeddings(my_doc_embs,my_colors,my_labels,my_name):
    my_pca = PCA(n_components=10)
    my_tsne = TSNE(n_components=2,perplexity=10) #https://lvdmaaten.github.io/tsne/
    doc_embs_pca = my_pca.fit_transform(my_doc_embs)
    doc_embs_tsne = my_tsne.fit_transform(doc_embs_pca)

    fig, ax = plt.subplots()

    for label in list(set(my_labels)):
        idxs = [idx for idx,elt in enumerate(my_labels) if elt==label]
        ax.scatter(doc_embs_tsne[idxs,0],
                   doc_embs_tsne[idxs,1],
                   c = my_colors[label],
                   label=str(label),
                   alpha=0.7,
                   s=40)

    ax.legend(scatterpoints=1)
    fig.suptitle('t-SNE visualization doc embeddings',fontsize=15)
    fig.set_size_inches(11,7)
    fig.savefig(my_name + '.pdf')

### fill the gap in the cnn_branch function below ###
### add a convolutional and a pooling layer ###
# conv layers: https://keras.io/layers/convolutional/
# pooling layers: https://keras.io/layers/pooling/
# layers can be combined by nesting them as: layer_b(parameters_b)(layer_a(parameters_a)(input))

def cnn_branch(n_filters,k_size,d_rate,my_input):
    doc_conv= Conv1D(filters=n_filters,kernel_size=k_size)(my_input)
    doc_pool= GlobalMaxPooling1D()(doc_conv)
    return Dropout(d_rate)(doc_pool)

# = = = = = parameters = = = = =

mfw_idx = 2 # index of the most frequent words in the dictionary
padding_idx = 0
oov_idx = 1

d = 30 # dimensionality of word embeddings
max_size = 60 # max allowed size of a document
nb_branches = 2
nb_filters = 50
filter_sizes = [3,4]
drop_rate = 0.3 # amount of dropout regularization
batch_size = 64
nb_epochs = 2
my_optimizer = 'adam'

# = = = = = loading data = = = = =

path ="/home/khaoula/Documents/MVA/ALTEGRAD/TP3/for_moodle (5)/for_moodle"
# load dictionary of word indexes (sorted by decreasing frequency across the corpus)
with open(path+'/data/word_to_index.json', 'r') as my_file:
    word_to_index = json.load(my_file)

# invert mapping (for sanity checking, later)
index_to_word = dict((v,k) for k,v in word_to_index.items())

with open(path+'/data/training.csv', 'r') as my_file:
    reader = csv.reader(my_file, delimiter=',')
    x_train = list(reader)

with open(path+'/data/test.csv', 'r') as my_file:
    reader = csv.reader(my_file, delimiter=',')
    x_test = list(reader)

with open(path+'/data/training_labels.txt', 'r') as my_file:
    y_train = my_file.read().splitlines()

with open(path+'/data/test_labels.txt', 'r') as my_file:
    y_test = my_file.read().splitlines()

# turn lists of strings into lists of integers
x_train = [[int(elt) for elt in sublist] for sublist in x_train]
x_test = [[int(elt) for elt in sublist] for sublist in x_test]

y_train = [int(elt) for elt in y_train]
y_test = [int(elt) for elt in y_test]

print('data loaded')

# = = some sanity checking = =

print('index of "the":',word_to_index['the']) # most frequent word
print('index of "movie":',word_to_index['movie']) # very frequent word
print('index of "elephant":',word_to_index['elephant']) # less frequent word

# reconstruct first review
rev = x_train[0]
print (' '.join([index_to_word[elt] if elt in index_to_word else 'OOV' for elt in rev]))
# compare it with the original review: https://www.imdb.com/review/rw2219371/?ref_=tt_urv

# = = = = = truncation and padding = = = = =

# truncate reviews longer than 'max_size'
x_train = [rev[:max_size] for rev in x_train]
x_test = [rev[:max_size] for rev in x_test]

# pad reviews shorter than 'max_size' with the special padding token
x_train = [rev+[padding_idx]*(max_size-len(rev)) if len(rev)<max_size else rev for rev in x_train]
x_test = [rev+[padding_idx]*(max_size-len(rev)) if len(rev)<max_size else rev for rev in x_test]

# all reviews should now be of size 'max_size'
assert max_size == list(set([len(rev) for rev in x_train]))[0] and max_size == list(set([len(rev) for rev in x_test]))[0]

print('truncation and padding done')

# = = = = = defining architecture = = = = =

# see guide to Keras' functional API: https://keras.io/getting-started/functional-api-guide/
# core layers: https://keras.io/layers/core/

doc_ints = Input(shape=(None,))

doc_wv = Embedding(input_dim=len(word_to_index)+2,output_dim=d,input_length=max_size)(doc_ints)### fill the gap (add an Embedding layer) https://keras.io/layers/embeddings/ ###

doc_wv_dr = Dropout(drop_rate)(doc_wv)

branch_outputs = []
for idx in range(nb_branches):
    branch_outputs.append(cnn_branch(nb_filters,filter_sizes[idx],drop_rate,doc_wv_dr))
    ### fill the gap (use the cnn_branch function) ###

concat = Concatenate()(branch_outputs) # branch output combination

preds = Dense(units=1,activation='sigmoid')(concat) ### fill the gap (add a Dense layer) https://keras.io/layers/core/ ###

model = Model(doc_ints,preds)

model.compile(optimizer=my_optimizer,
              loss='binary_crossentropy',
              metrics=['accuracy'])

### fill the gap (call the compile method on model) https://keras.io/models/sequential/ ###
### specific the loss, optimizer, and metrics arguments ###
### use the accuracy as metric ###

print('model compiled')

model.summary()

print('total number of model parameters:',model.count_params())

# = = = = = visualizing doc embeddings (before training) = = = = =

### fill the gap ###
### create a K.function 'get_doc_embedding()' that returns the inner representation of the model before the prediction layer ###
### this function should take as input the input of the model and return the output of the penultimate layer ###
### you can access the layers of the model with model.layers ###
### a specific layer can then be accessed via its index, e.g., model.layers[0] ###
### the indexes of the layers can be inferred by calling model.summary() ###
### the input and output of a given layer can be accessed via, e.g., model.layers[0].input and model.layers[0].output ###
### see: How can I obtain the output of an intermediate layer? here: https://keras.io/getting-started/faq/ ###

get_doc_embedding = K.function([model.layers[0].input],
                                  [model.layers[9].output])

n_plot = 1000
labels_plt = y_test[:n_plot]
doc_embs = get_doc_embedding([np.array(x_test[:n_plot]),0])[0]

print('plotting embeddings of first',n_plot,'documents')
visualize_doc_embeddings(doc_embs,['blue','red'],labels_plt,'before')

# = = = = = training = = = = =

### note: if you don't have a GPU, you may use a subset of x_train and y_train to speed up training ###

model.fit(np.array(x_train),
          y_train,
          batch_size = batch_size,
          epochs = nb_epochs,
          validation_data = (np.array(x_test), y_test))

# = = = = = visualizing doc embeddings (after training) = = = = =

get_doc_embedding = K.function([model.layers[0].input],
                                  [model.layers[9].output])

n_plot = 1000
labels_plt = y_test[:n_plot]
doc_embs = get_doc_embedding([np.array(x_test[:n_plot]),0])[0]

print('plotting embeddings of first',n_plot,'documents')
visualize_doc_embeddings(doc_embs,['blue','red'],labels_plt,'before')
### fill the gaps ###
### perform the same steps as before training and observe the changes ###

# = = = = = predictive text regions for the first branch = = = = =

### fill the gaps ###
### define a K.function 'get_region_embedding' that returns the feature maps of the first branch ###

get_region_embedding = K.function([model.layers[0].input,K.learning_phase()],
                                  [model.layers[3].output])
Model(inputs=model.inputs, outputs=model.layers[1].output)
my_review_text = 'Oh , god , this was such a disappointment ! Worst movie ever . Not worth the 15 bucks .'
tokens = my_review_text.lower().split()
my_review = [word_to_index[elt] for elt in tokens]

# extract regions (sliding window over text)
regions = []
regions.append(' '.join(tokens[:filter_sizes[0]]))
for i in range(filter_sizes[0], len(tokens)):
    regions.append(' '.join(tokens[(i-filter_sizes[0]+1):(i+1)]))

my_review = np.array([my_review])

reg_emb = get_region_embedding([my_review,0])[0][0,:,:]

### fill the gap ###
### compute the norms of the region embeddings ###
### you may use np.linalg.norm() https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html ###
norms = np.linalg.norm(reg_emb,axis=1)

print([list(zip(regions,norms))[idx] for idx in np.argsort(-norms).tolist()])

# = = = = = saliency map = = = = =

input_tensors = [model.input, K.learning_phase()]
saliency_input = model.layers[1].output### fill the gap (extract the rows of the embedding matrix from the model) ###
saliency_output = model.output ### fill the gap (get the probability distribution over classes from the model) ###

gradients = model.optimizer.get_gradients(saliency_output,saliency_input)

compute_gradients = K.function(inputs=input_tensors,outputs=gradients)

matrix = compute_gradients([my_review,0])[0][0,:,:]

### fill the gap (compute the magnitude of the partial derivatives) ###
### you may use np.absolute https://docs.scipy.org/doc/numpy/reference/generated/numpy.absolute.html ###
#to_plot = np.apply_along_axis(np.absolute,axis=0,arr=matrix)
to_plot= np.absolute(matrix)
fig, ax = plt.subplots()
heatmap = ax.imshow(to_plot, cmap=plt.cm.Blues, interpolation='nearest',aspect='auto')
ax.set_yticks(np.arange(len(tokens)))
ax.set_yticklabels(tokens)
ax.tick_params(axis='y', which='major', labelsize=32*10/len(tokens))
fig.colorbar(heatmap)
fig.set_size_inches(11,7)
fig.savefig('saliency_map.pdf',bbox_inches='tight')
fig.show()

max_stars_repo_name: khaoulabelahsen/MVA-AlteGrad

max_issues_repo_name: khaoulabelahsen/MVA-AlteGrad

max_forks_repo_name: khaoulabelahsen/MVA-AlteGrad


text:
"""
Miscellaneous utility functions.
"""

import numpy as np

def hom_to_aug(x_hom):
    """
    Converts a homogeneous vector to an (inhomogeneous) augmented vector.
    """
    # Convert to numpy array if necessary
    if not isinstance(x_hom, np.ndarray):
        x = np.array(x)

    # Divide through by the last element to get the augmented vector
    return x_hom / x_hom[-1]

def focal_length(W, theta):
    """
    Returns the focal length of the camera.

    Parameters :
        W      : sensor width
        theta  : field of view
    """
    return W / 2 / np.tan(theta / 2)

def clip_min_max(img, val_min, val_max):
    """
    Clips the signal at the specified limits.
    """
    img[img < val_min] = val_min
    img[img > val_max] = val_max

    return img

def get_patch(img, x, y, x_len, y_len):
    """
    Extracts a patch from an image with top-left corner at (x, y) and
    size x_len by y_len.
    """
    return img[x : x + x_len, y : y + y_len]

def get_patch_centered(img, x, y, radius):
    """
    Extracts a patch from an image centered at (x, y) with a given radius.
    """
    return img[x - radius : x + radius + 1, y - radius : y + radius + 1]

def get_mask(img, x, y, x_len, y_len):
    """
    Returns a binary mask with top-left corner at (x, y) and
    size x_len by y_len.
    """
    w = np.zeros(img.shape)
    w[x : x + x_len, y : y + y_len] = 1.0

    return w

max_stars_repo_name: pauljxtan/pystuff

max_issues_repo_name: pauljxtan/pystuff

max_forks_repo_name: pauljxtan/pystuff


text:
import numpy
import pandas as pd

from dataset import *

### ----------------------------------------------------------------------------
### Learning
### ----------------------------------------------------------------------------
import warnings
warnings.filterwarnings("ignore") # ignore warnings
from sklearn.ensemble import GradientBoostingRegressor

### ----------------------------------------------------------------------------
### Test
### ----------------------------------------------------------------------------
# grid search by models
from sklearn.metrics import mean_squared_error

## -- Rounding values
def round_for(values, dots=2):
	return numpy.array([round(value,dots) for value in values])

def _in_five(value):
	value = round(value,0)
	u  = value % 10
	ut = 5 if (3 <= u <= 7) else 0
	if (8 <= u):
		value += 10
	value = value - u + ut
	return value

def round_in_five(values):
	return numpy.array([_in_five(value) for value in values])

## -- Prediction
reg = GradientBoostingRegressor(n_estimators=100, loss='ls', random_state=0)
reg.fit(X_treino, Y_treino)
pred = reg.predict(X_teste)

print('normal  ', mean_squared_error(Y_teste, pred))
print('round(2)', mean_squared_error(Y_teste, round_for(pred, 2)))
print('round(0)', mean_squared_error(Y_teste, round_for(pred, 0)))
print('round_5 ', mean_squared_error(Y_teste, round_in_five(pred)))

max_stars_repo_name: leomaurodesenv/QuarentenaDados-Desafio

max_issues_repo_name: leomaurodesenv/QuarentenaDados-Desafio

max_forks_repo_name: leomaurodesenv/QuarentenaDados-Desafio


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  8 10:39:33 2018
"""
import plotly.express as px
import json
from datetime import datetime
from dash import Dash
from dash.dependencies import Input, State, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from sqlalchemy.sql import text
from sqlalchemy import create_engine
import pandas as pd
import numpy as np

from .utility import apply_layout_with_auth, load_object, save_object, get_postgres_sqlalchemy_uri

# global vars
_URL_BASE = '/dash/google_ads_ltv_optimizer/'
_ACCOUNT_VALUES = ('test')

# layout
layout = html.Div([
    html.H2('Select account to assess LTV model'),
    dcc.Dropdown(
        id='account-dropdown',
        options=[
            {'label': 'Hult', 'value':'hult'}
        ],
        value='hult'
    ),
    html.H2('LTV model metrics'),
    html.Div([
        html.H3('Pick model type:'),
        dcc.Dropdown(id='ltv-metrics-model-type-dropdown')
    ]),
    dcc.Graph(id='ltv-metrics-graph'),
    html.H2('LTV sensitivities'),
    dcc.Dropdown(id='sensitivities-date-dropdown'),
    html.Br(),
    dash_table.DataTable(
        id='ltv-sensitivities-table',
        columns=(
            [{'id': 'model_type', 'name': 'Model Type'},
             {'id': 'model_date', 'name': 'Create Date'},
             {'id': 'category', 'name': 'Feature Category'},
             {'id': 'category_count', 'name': 'Feature Count'},
             {'id': 'category_value', 'name': 'Feature Value'},
             {'id': 'sensitivity', 'name': 'Feature Sensitivity'}]
        ),
        editable=False
    ),
], style={'width': '500'})

# main initialization and callbacks
def Add_Dash(server):
    app = Dash(server=server, url_base_pathname=_URL_BASE)
    pgdb = create_engine(get_postgres_sqlalchemy_uri())
    apply_layout_with_auth(app, layout)

    @app.callback([Output('sensitivities-date-dropdown', 'options'), Output('sensitivities-date-dropdown', 'value')],
                  [Input('account-dropdown', 'value')])
    def update_date_dropdowns(account_value):
        statement = text("""
        select distinct model_date from {}.ltv_model_sensitivities order by model_date;
        """.format(account_value))
        with pgdb.connect() as con:
            rs = con.execute(statement)
            data = rs.fetchall()
            data = [
                {'label': datetime.strftime(d[0], "%Y-%m-%d"), 'value': datetime.strftime(d[0], "%Y-%m-%d")}
                for d in data
            ]
            default = data[0]['value']
        return [data, default]

    @app.callback([Output('ltv-metrics-model-type-dropdown', 'options'), Output('ltv-metrics-model-type-dropdown', 'value')],
                  [Input('account-dropdown', 'value')])
    def update_ltv_metrics_model_type_dropdown(account_value):
        statement = text("""
        select distinct model_type from {}.ltv_model_metrics;
        """.format(account_value))
        with pgdb.connect() as con:
            rs = con.execute(statement)
            data = rs.fetchall()
            data = [
                {'label': d[0], 'value': d[0]}
                for d in data
            ]
            print(data)
            default = data[0]['value']
        return [data, default]

    @app.callback(
        Output('ltv-metrics-graph', 'figure'),
        [Input('account-dropdown', 'value'), Input('ltv-metrics-model-type-dropdown', 'value')]
    )
    def get_ltv_metrics_graph(account_value, model_type):
        statement = text("""
        select * from {}.ltv_model_metrics where model_type='{}' order by model_type, model_date
        """.format(account_value, model_type))
        with pgdb.connect() as con:
            rs = con.execute(statement)
            data = rs.fetchall()
            keys = rs.keys()
            df = pd.DataFrame(data, columns=keys)

        df = pd.melt(
            df, id_vars=['model_date','model_type'],
            value_vars=['precision','recall','accuracy', 'auc']
        )
        df = df.rename(columns={'variable':'metric'})
        fig = px.scatter(
            df,
            x='model_date',
            y='value',
            color='metric',
        )
        for d in fig.data:
            d.update(mode='markers+lines')
        return fig

    @app.callback(
        Output('ltv-sensitivities-table', 'data'),
        [Input('account-dropdown','value'), Input('sensitivities-date-dropdown', 'value')]
    )
    def get_ltv_sensitivities_table(account_value, sensitivity_date):
        statement = text("""
        select
             category,
             category_value,
             category_count,
             ROUND(sensitivity::numeric, 3) sensitivity,
             model_type,
             model_date
        from {}.ltv_model_sensitivities where model_date = '{}'
        """.format(account_value, sensitivity_date)) # assume already ordered by batch process
        with pgdb.connect() as con:
            rs = con.execute(statement)
        data = rs.fetchall()
        keys = rs.keys()
        df = pd.DataFrame(data, columns=keys)
        print(df)
        return df.to_dict(orient='records')

    return app.server

max_stars_repo_name: jackchua/nga-ui

max_issues_repo_name: jackchua/nga-ui

max_forks_repo_name: jackchua/nga-ui


text:
import chainer
import numpy as np

from helper import ModelRetrieverHelper

class TestInputs(ModelRetrieverHelper):

    @ModelRetrieverHelper.case
    def test_input_order(self):
        xs = [chainer.Variable(np.ones((1, 3, 32, 32))) for _ in range(5)]
        y = xs[0]
        for x in xs[1:]:
            y = y + x
        for i in range(len(xs)):
            self.retriever.register_inputs(xs[i], name='in_' + str(i))
        self.retriever(y)
        j = self.retriever.generate_json_source()
        assert j['inputs'] == ['in_0', 'in_1', 'in_2', 'in_3', 'in_4']

    @ModelRetrieverHelper.case
    def test_unused_registerd_input_should_not_appear(self):
        xs = [chainer.Variable(np.ones((1, 3, 32, 32))) for _ in range(5)]
        y = xs[0]
        for x in xs[1:-1]:  # The last one is intentionally ignored
            y = y + x
        for i in range(len(xs)):
            self.retriever.register_inputs(xs[i], name='in_' + str(i))

        self.retriever(y)
        j = self.retriever.generate_json_source()
        assert j['inputs'] == ['in_0', 'in_1', 'in_2', 'in_3']

    @ModelRetrieverHelper.case
    def test_constants_should_not_appear_in_inputs(self):
        x = chainer.Variable(np.ones((1, 3, 32, 32)))
        c = chainer.Variable(np.ones((1, 3, 32, 32))) * 3
        y = x + c
        self.retriever.register_inputs(x, name='in')
        self.retriever(y)
        j = self.retriever.generate_json_source()
        assert j['inputs'] == ['in']
        assert sum(l['type'] == 'ConstantInput' for l in j['layers']) == 1

    @ModelRetrieverHelper.case
    def test_input_shouldnt_be_treated_as_constant_if_nothing_registered(self):
        x1 = chainer.Variable(np.ones((1, 3, 32, 32)))
        x2 = chainer.Variable(np.ones((1, 3, 32, 32)) * 2)
        y = x1 + x2
        self.retriever(y)
        j = self.retriever.generate_json_source()
        assert len(j['inputs']) == 2
        assert sum(l['type'] == 'input' for l in j['layers']) == 2
        assert sum(l['type'] == 'ConstantInput' for l in j['layers']) == 0

    @ModelRetrieverHelper.case
    def test_out_in_middle_of_already_read_graph(self):
        x = chainer.Variable(np.zeros((1, 3, 32, 32)))
        y1 = x + 1
        y2 = y1 * 2
        self.retriever(y2, name='y2')
        self.retriever(y1, name='y1')
        j = self.retriever.generate_json_source()
        assert len(j['outputs']) == 2
        y2_id, y2_name = j['outputs'][0]
        y1_id, y1_name = j['outputs'][1]
        assert y2_id == 'MulConstant-1-1' and y2_name == 'y2'
        assert y1_id == 'AddConstant-0-1' and y1_name == 'y1'

max_stars_repo_name: tkerola/chainer-trt

max_issues_repo_name: tkerola/chainer-trt

max_forks_repo_name: tkerola/chainer-trt