text:
"""
    Tests for direct function
"""

import numpy as np
import pytest

from cayenne.simulation import Simulation

@pytest.mark.parametrize("algorithm", ["direct", "tau_leaping", "tau_adaptive"])
@pytest.mark.usefixtures("setup_basic", "setup_large")
class TestSanitizeAlg:
    """
        Sanity checks on Simulation class where simulations are attempted.
    """

    def test_null(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        k = np.array([0.0, 0.0])
        sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim.simulate(algorithm=algorithm)
        assert sim.results.status_list[0] == -2

    def test_status_3(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        V_p = np.array([[0, 0], [0, 0], [0, 1]])
        X0 = np.array([10, 0, 0], dtype=np.int64)
        sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim.simulate(algorithm=algorithm, max_t=10, max_iter=100, chem_flag=True)
        assert sim.results.status_list[0] == 3

    def test_status_2(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        X0 = np.array([10, 0, 0], dtype=np.int64)
        sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim.simulate(algorithm=algorithm, max_t=1, max_iter=100, chem_flag=True)
        assert sim.results.status_list[0] == 2

    def test_reproduce(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim2 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim1.simulate(algorithm=algorithm)
        sim2.simulate(algorithm=algorithm)
        assert all(
            (i == j).all() for i, j in zip(sim1.results.t_list, sim2.results.t_list)
        )
        assert all(
            (i == j).all() for i, j in zip(sim1.results.x_list, sim2.results.x_list)
        )
        assert all(
            i == j for i, j in zip(sim1.results.status_list, sim2.results.status_list)
        )

    def test_reproduce_fail(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim2 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim1.simulate(algorithm=algorithm)
        sim2.simulate(algorithm=algorithm, seed=1)
        for i, j in zip(sim1.results.t_list, sim2.results.t_list):
            if i.shape[0] == j.shape[0]:
                assert not (
                    all(
                        (i == j).all()
                        for i, j in zip(sim1.results.t_list, sim2.results.t_list)
                    )
                )

    def test_incorrect_seed(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        with pytest.raises(TypeError):
            sim1.simulate(algorithm=algorithm, n_rep=2, seed=[1])
        with pytest.raises(TypeError):
            sim1.simulate(algorithm=algorithm, n_rep=2, seed=[1, 2, 3])

    def test_maxiter_type(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        with pytest.raises(TypeError):
            sim1.simulate(algorithm=algorithm, max_iter=100.0)

    def test_monotonic(self, algorithm, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim1.simulate(algorithm=algorithm)
        results = sim1.results
        for t_array in results.t_list:
            assert (np.diff(t_array) > 0).all()

    def test_long(self, algorithm, setup_long):
        species_names, rxn_names, V_r, V_p, X0, k = setup_long
        sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim1.simulate(
            algorithm=algorithm, max_t=1e5, max_iter=int(1e8), chem_flag=False
        )
        X_output = np.array([0, 0, X0.sum()])
        assert (sim1.results.final[-1] == X_output).all()

@pytest.mark.usefixtures("setup_basic", "setup_large")
class TestSanitize:
    """
        Sanity checks on Simulation class where instance creation fails.
    """

    def test_too_high_order(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        V_r = np.array([[2, 0], [2, 1], [0, 0]])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_neg_k(self, setup_large):
        species_names, rxn_names, V_r, V_p, X0, k = setup_large
        k = np.array([1, 1, -1, 1, -1])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_k_shape(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        k = np.array([[1], [0], [0]])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_Vp_Vr_shape(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        V_p = np.array([[0], [1], [0]])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_kdet_Vr_shape(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        k = np.array([1, 1, 1])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_X0_Vr_shape(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        X0 = np.array([100, 0, 0, 0])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        X0 = np.array([100, 0])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_X0_2d(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        X0 = np.array([[100, 0, 0]])
        print(X0.shape)
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        X0 = np.array([[100], [0], [0]])
        print(X0.shape)
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_Vp_neg(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        V_p = np.array([[0, 0], [-1, 0], [0, 1]])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_Vr_neg(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        V_r = np.array([[-1, 0], [0, 1], [0, 0]])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

    def test_X0_neg(self, setup_basic):
        species_names, rxn_names, V_r, V_p, X0, k = setup_basic
        X0 = np.array([-10, 0, 0])
        with pytest.raises(ValueError):
            Simulation(species_names, rxn_names, V_r, V_p, X0, k)

# def test_bifurcation(setup_bifurcation):
#     V_r, V_p, X0, k = setup_bifurcation
#     count_excitation = 0
#     n_runs = 10
#     deviation_tolerance = 0.05
#     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
#     sim1.simulate(
#         algorithm="tau_adaptive", max_t=150, max_iter=1000, chem_flag=True, n_rep=n_runs
#     )
#     for ind in range(n_runs):
#         x = sim1.results.x_list[ind]
#         xt = x[-1, :]
#         assert np.all(xt - np.array([0, 11, 0, 0]) == 0) or np.all(
#             xt - np.array([0, 0, 1, 10]) == 0
#         )
#         if np.all(xt - np.array([0, 11, 0, 0]) == 0):
#             count_excitation += 1
#     assert np.abs(count_excitation / n_runs - 0.5) < deviation_tolerance

class TestHOR:
    """
        Test the HOR method for various combinations of reactants.
    """

    @staticmethod
    def create_sim_inst(react_stoic):
        prod_stoic = np.random.randint(0, 5, react_stoic.shape)
        k = np.random.rand(react_stoic.shape[1])
        X = np.random.randint(0, 5, react_stoic.shape[0], dtype=np.int64)
        species_names = ["X"] * react_stoic.shape[0]
        rxn_names = ["R"] * react_stoic.shape[1]
        sim = Simulation(species_names, rxn_names, react_stoic, prod_stoic, X, k)
        return sim

    def test_hor1(self):
        # A ->
        react_stoic = np.array([[1, 0]])
        sim = self.create_sim_inst(react_stoic)
        assert sim.HOR == 1

    def test_hor2(self):
        # A ->
        # A ->
        # A ->
        react_stoic = np.array([[1, 1, 1], [0, 0, 0]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [1, 0])

    def test_hor3(self):
        # A + B ->
        # A ->
        react_stoic = np.array([[1, 1], [1, 0]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [2, 2])

    def test_hor4(self):
        # # A + B ->
        # # C ->
        react_stoic = np.array([[1, 0], [1, 0], [0, 1]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [2, 2, 1])

    def test_hor5(self):
        # A + A ->
        # A ->
        # B ->
        react_stoic = np.array([[2, 1, 0], [0, 0, 1]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [-2, 1])

    def test_hor6(self):
        # # A + A + B ->
        # # A + B ->
        # # B ->
        react_stoic = np.array([[2, 1, 0], [1, 1, 1]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [-32, 3])

    def test_hor7(self):
        # A + A + B ->
        # A + B + B ->
        react_stoic = np.array([[2, 1], [1, 2]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [-32, -32])

    def test_hor8(self):
        # # A + A + A ->
        # # A + A + B ->
        # # A + B ->
        react_stoic = np.array([[3, 2, 1], [0, 1, 1]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [-3, 3])

    def test_hor9(self):
        # A + A + A ->
        # B + B + B ->
        # A + A + B ->
        # A + B + B ->
        # A + B + C ->
        react_stoic = np.array([[3, 0, 2, 1, 1], [0, 3, 1, 2, 1], [0, 0, 0, 0, 1]])
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [-3, -3, 3])

    def test_hor10(self):
        # A + B ->
        # A + A + A ->
        # B + B ->
        # A + C ->
        # C + D + E ->
        react_stoic = np.array(
            [
                [1, 3, 0, 1, 0],
                [1, 0, 2, 0, 0],
                [0, 0, 0, 1, 1],
                [0, 0, 0, 0, 1],
                [0, 0, 0, 0, 1],
            ]
        )
        sim = self.create_sim_inst(react_stoic)
        assert np.all(sim.HOR == [-3, -2, 3, 3, 3])

class TestLoadModel:
    def test_correct_model_str(self, setup_00001_correct):
        sim = Simulation.load_model(setup_00001_correct, "ModelString")
        sim.simulate()
        assert sim.results.status_list == [2]
        sim.simulate(max_t=20.0, max_iter=5)
        assert sim.results.status_list == [1]

    def test_correct_model_file(self):
        sim = Simulation.load_model("tests/models/00001.txt", "ModelFile")
        sim.simulate()
        assert sim.results.status_list == [2]

max_stars_repo_name: Heuro-labs/pyssa

max_issues_repo_name: Heuro-labs/pyssa

max_forks_repo_name: Heuro-labs/pyssa


text:
import os
import argparse
import numpy as np

def parse_obj(obj_name):
    vertex_data = []
    flines = []
    with open(obj_name) as f:
        for line in f:
            if line.startswith("v "): # it is vertex line
                head, x, y, z = line.strip().split(" ")
                vertex_data.append([float(x), float(y), float(z)])
            if line.startswith("f"):
                flines.append(line)
    return np.array(vertex_data), flines

def get_center(vertex):
    return np.mean(vertex, axis=0)

def write_obj(path, vertex, faces):
    with open(path, "w") as f:
        for item in vertex:
            f.write("v "+str(item[0])+" "+str(item[1])+" "+str(item[2])+"\n")
        for line in faces:
            f.write(line)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--input_dir", type=str, default="smpl_stl")
    parser.add_argument("-o", "--output_dir", type=str, default="smpl_parts")
    parser.add_argument("--offset_path", type=str)
    args = parser.parse_args()

    input_dir = args.input_dir
    part_list = os.listdir(input_dir)
    write_dir = args.output_dir

    if not os.path.exists(write_dir):
        os.makedirs(write_dir)

    with open(args.offset_path,"w") as f:
        for part in part_list:
            vertex, faces = parse_obj(input_dir+"/"+part)
            center = get_center(vertex)
            f.write(part+" "+str(center[0])+" "+str(center[1])+" "+str(center[2])+"\n")
            vertex -= center
            write_obj(write_dir+"/"+part, vertex, faces)

ext: py

max_stars_repo_name: empriselab/CareURDF

max_issues_repo_name: empriselab/CareURDF

max_forks_repo_name: empriselab/CareURDF

reason: import numpy


text:
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

r"""
Parameters contributed from PyRobot
https://pyrobot.org/
https://github.com/facebookresearch/pyrobot

Please cite PyRobot if you use this noise model
"""

from typing import Any, List, Optional, Sequence, Tuple

import attr
import magnum as mn
import numpy as np
import scipy.stats
from attr._make import Attribute
from numpy import ndarray

from habitat_sim import bindings as hsim
from habitat_sim.agent.controls.controls import ActuationSpec, SceneNodeControl
from habitat_sim.registry import registry

@attr.s(auto_attribs=True, init=False, slots=True)
class _TruncatedMultivariateGaussian:
    mean: np.ndarray
    cov: np.ndarray

    def __init__(self, mean: Sequence, cov: Sequence) -> None:
        self.mean = np.array(mean)
        self.cov = np.array(cov)
        if len(self.cov.shape) == 1:
            self.cov = np.diag(self.cov)

        assert (
            np.count_nonzero(self.cov - np.diag(np.diagonal(self.cov))) == 0
        ), "Only supports diagonal covariance"

    def sample(
        self,
        truncation: Optional[
            List[Optional[Tuple[Optional[Any], Optional[Any]]]]
        ] = None,
    ) -> ndarray:
        if truncation is not None:
            assert len(truncation) == len(self.mean)

        sample = np.zeros_like(self.mean)
        for i in range(len(self.mean)):
            stdev = np.sqrt(self.cov[i, i])
            mean = self.mean[i]
            # Always truncate to 3 standard deviations
            a, b = -3, 3

            if truncation is not None and truncation[i] is not None:
                trunc = truncation[i]
                if trunc[0] is not None:
                    a = max((trunc[0] - mean) / stdev, a)
                if trunc[1] is not None:
                    b = min((trunc[1] - mean) / stdev, b)

            sample[i] = scipy.stats.truncnorm.rvs(a, b, mean, stdev)

        return sample

@attr.s(auto_attribs=True, slots=True)
class MotionNoiseModel:
    linear: _TruncatedMultivariateGaussian
    rotation: _TruncatedMultivariateGaussian

@attr.s(auto_attribs=True, slots=True)
class ControllerNoiseModel:
    linear_motion: MotionNoiseModel
    rotational_motion: MotionNoiseModel

@attr.s(auto_attribs=True, slots=True)
class RobotNoiseModel:
    ILQR: ControllerNoiseModel
    Proportional: ControllerNoiseModel
    Movebase: ControllerNoiseModel

    def __getitem__(self, key: str) -> ControllerNoiseModel:
        return getattr(self, key)

pyrobot_noise_models = {
    "LoCoBot": RobotNoiseModel(
        ILQR=ControllerNoiseModel(
            linear_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.014, 0.009], [0.006, 0.005]),
                _TruncatedMultivariateGaussian([0.008], [0.004]),
            ),
            rotational_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.003, 0.003], [0.002, 0.003]),
                _TruncatedMultivariateGaussian([0.023], [0.012]),
            ),
        ),
        Proportional=ControllerNoiseModel(
            linear_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.017, 0.042], [0.007, 0.023]),
                _TruncatedMultivariateGaussian([0.031], [0.026]),
            ),
            rotational_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.001, 0.005], [0.001, 0.004]),
                _TruncatedMultivariateGaussian([0.043], [0.017]),
            ),
        ),
        Movebase=ControllerNoiseModel(
            linear_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.074, 0.036], [0.019, 0.033]),
                _TruncatedMultivariateGaussian([0.189], [0.038]),
            ),
            rotational_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.002, 0.003], [0.0, 0.002]),
                _TruncatedMultivariateGaussian([0.219], [0.019]),
            ),
        ),
    ),
    "LoCoBot-Lite": RobotNoiseModel(
        ILQR=ControllerNoiseModel(
            linear_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.142, 0.023], [0.008, 0.008]),
                _TruncatedMultivariateGaussian([0.031], [0.028]),
            ),
            rotational_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.002, 0.002], [0.001, 0.002]),
                _TruncatedMultivariateGaussian([0.122], [0.03]),
            ),
        ),
        Proportional=ControllerNoiseModel(
            linear_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.135, 0.043], [0.007, 0.009]),
                _TruncatedMultivariateGaussian([0.049], [0.009]),
            ),
            rotational_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.002, 0.002], [0.002, 0.001]),
                _TruncatedMultivariateGaussian([0.054], [0.061]),
            ),
        ),
        Movebase=ControllerNoiseModel(
            linear_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.192, 0.117], [0.055, 0.144]),
                _TruncatedMultivariateGaussian([0.128], [0.143]),
            ),
            rotational_motion=MotionNoiseModel(
                _TruncatedMultivariateGaussian([0.002, 0.001], [0.001, 0.001]),
                _TruncatedMultivariateGaussian([0.173], [0.025]),
            ),
        ),
    ),
}

@attr.s(auto_attribs=True)
class PyRobotNoisyActuationSpec(ActuationSpec):
    r"""Struct to hold parameters for pyrobot noise model

    :property robot: Which robot to simulate noise for. Valid values are
        ``LoCoBot`` and ``LoCoBot-Lite``.
    :property controller: Which controller to simulate noise models for. Valid
        values are ``ILQR``, ``Proportional``; ``Movebase ILQR`` is the
        default.
    :property noise_multiplier: Multiplier on the noise amount, useful for
        ablating the effect of noise.

    -   https://pyrobot.org/
    -   https://github.com/facebookresearch/pyrobot

    Please cite PyRobot if you use this noise model.
    """
    robot: str = attr.ib(default="LoCoBot")

    @robot.validator  # noqa: F811
    def check_robot(self, attribute: Attribute, value: str) -> None:
        assert value in pyrobot_noise_models, f"{value} not a known robot"

    controller: str = attr.ib(default="ILQR")

    @controller.validator  # noqa: F811
    def check_controller(self, attribute: Attribute, value: str) -> None:
        assert value in [
            "ILQR",
            "Proportional",
            "Movebase",
        ], f"{value} not a known controller"

    noise_multiplier: float = 1.0

_X_AXIS = 0
_Y_AXIS = 1
_Z_AXIS = 2

def _noisy_action_impl(
    scene_node: hsim.SceneNode,
    translate_amount: float,
    rotate_amount: float,
    multiplier: float,
    model: MotionNoiseModel,
    motion_type: str,
) -> None:
    # Perform the action in the coordinate system of the node
    transform = scene_node.transformation
    move_ax = -transform[_Z_AXIS].xyz
    perp_ax = transform[_X_AXIS].xyz

    if motion_type == "rotational":
        translation_noise = multiplier * model.linear.sample()
    else:
        # The robot will always move a little bit.  This has to be defined based on the intended actuation
        # as otherwise small rotation amounts would be invalid.  However, pretty quickly, we'll
        # get to the truncation of 3 sigma
        trunc = [(-0.95 * np.abs(translate_amount), None), None]

        translation_noise = multiplier * model.linear.sample(trunc)

    # + EPS to make sure 0 is positive.  We multiply by the sign of the translation
    # as otherwise forward would overshoot on average and backward would undershoot, while
    # both should overshoot
    translation_noise *= np.sign(translate_amount + 1e-8)

    scene_node.translate_local(
        move_ax * (translate_amount + translation_noise[0])
        + perp_ax * translation_noise[1]
    )

    if motion_type == "linear":
        rot_noise = multiplier * model.rotation.sample()
    else:
        # The robot will always turn a little bit.  This has to be defined based on the intended actuation
        # as otherwise small rotation amounts would be invalid.  However, pretty quickly, we'll
        # get to the truncation of 3 sigma
        trunc = [(-0.95 * np.abs(np.deg2rad(rotate_amount)), None)]

        rot_noise = multiplier * model.rotation.sample(trunc)

    # Same deal with rotation about + EPS and why we multiply by the sign
    rot_noise *= np.sign(rotate_amount + 1e-8)

    scene_node.rotate_y_local(mn.Deg(rotate_amount) + mn.Rad(rot_noise))
    scene_node.rotation = scene_node.rotation.normalized()

@registry.register_move_fn(body_action=True)
class PyrobotNoisyMoveBackward(SceneNodeControl):
    def __call__(
        self, scene_node: hsim.SceneNode, actuation_spec: ActuationSpec
    ) -> None:
        assert isinstance(actuation_spec, PyRobotNoisyActuationSpec)
        _noisy_action_impl(
            scene_node,
            -actuation_spec.amount,
            0.0,
            actuation_spec.noise_multiplier,
            pyrobot_noise_models[actuation_spec.robot][
                actuation_spec.controller
            ].linear_motion,
            "linear",
        )

@registry.register_move_fn(body_action=True)
class PyrobotNoisyMoveForward(SceneNodeControl):
    def __call__(
        self, scene_node: hsim.SceneNode, actuation_spec: ActuationSpec
    ) -> None:
        assert isinstance(actuation_spec, PyRobotNoisyActuationSpec)
        _noisy_action_impl(
            scene_node,
            actuation_spec.amount,
            0.0,
            actuation_spec.noise_multiplier,
            pyrobot_noise_models[actuation_spec.robot][
                actuation_spec.controller
            ].linear_motion,
            "linear",
        )

@registry.register_move_fn(body_action=True)
class PyrobotNoisyTurnLeft(SceneNodeControl):
    def __call__(
        self, scene_node: hsim.SceneNode, actuation_spec: ActuationSpec
    ) -> None:
        assert isinstance(actuation_spec, PyRobotNoisyActuationSpec)
        _noisy_action_impl(
            scene_node,
            0.0,
            actuation_spec.amount,
            actuation_spec.noise_multiplier,
            pyrobot_noise_models[actuation_spec.robot][
                actuation_spec.controller
            ].rotational_motion,
            "rotational",
        )

@registry.register_move_fn(body_action=True)
class PyrobotNoisyTurnRight(SceneNodeControl):
    def __call__(
        self, scene_node: hsim.SceneNode, actuation_spec: ActuationSpec
    ) -> None:
        assert isinstance(actuation_spec, PyRobotNoisyActuationSpec)
        _noisy_action_impl(
            scene_node,
            0.0,
            -actuation_spec.amount,
            actuation_spec.noise_multiplier,
            pyrobot_noise_models[actuation_spec.robot][
                actuation_spec.controller
            ].rotational_motion,
            "rotational",
        )

max_stars_repo_name: vauduong/habitat-sim

max_issues_repo_name: vauduong/habitat-sim

max_forks_repo_name: vauduong/habitat-sim

reason: import numpy,from numpy,import scipy


text:
from keras.layers import Input, Embedding, Bidirectional, LSTM, Dropout, Dense, BatchNormalization, GaussianNoise, concatenate
from keras.preprocessing.sequence import pad_sequences
from keras import Model, backend as K
from keras.regularizers import l2
from nltk import word_tokenize
import tensorflow as tf
import numpy as np
import datetime
import json
import os

def load_word_mappings():
    word2index = json.loads(open("word2index.json", "r").read())
    return word2index

def initialize_gpu():
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    print("GPUs available: ", len(physical_devices))
    tf.config.experimental.set_memory_growth(physical_devices[0], True)

def Word2Vec(input_length):
    embedding_weights = np.load(open("weights_file.bin", "rb"))
    print("Weight shape: " + str(embedding_weights.shape))
    return Embedding(input_length=input_length, input_dim=embedding_weights.shape[0], output_dim=embedding_weights.shape[1], weights=[embedding_weights], trainable=False, mask_zero=True)

def get_data(sentences_path, scores_path):
    print("Loading training data...")
    sentences_file = open(sentences_path, "r", encoding='utf8')
    scores_file = open(scores_path, "r")
    tokenized_sentences = [word_tokenize(line) for line in sentences_file.readlines()]
    scores = [float(line.strip("\n")) for line in scores_file.readlines()]
    return tokenized_sentences, scores

def get_word_index(word2index, word):
    try:
        return word2index[word]
    except:
        return 2999999

def index_sentence_words(word2index, sentences):
    indexed_sentences = []
    for sentence in sentences:
        indexed_sentence = []
        for word in sentence:
            indexed_sentence.append(get_word_index(word2index, word))
        indexed_sentences.append(indexed_sentence)
    return indexed_sentences

def preprocess_sentences(word2index, sentences, max_sentence_len):
    indexed_sentences = index_sentence_words(word2index, sentences)
    padded_sentences = pad_sequences(indexed_sentences, maxlen=max_sentence_len, padding='post', value=0)
    return padded_sentences.tolist()

def r(y_true, y_pred):
    true_sub = y_true - K.mean(y_true)
    pred_sub = y_pred - K.mean(y_pred)
    return K.mean(K.sum(true_sub * pred_sub) / K.sqrt(K.sum(K.pow(true_sub, 2)) * K.sum(K.pow(pred_sub, 2))))

max_sentence_len = 52

print("Starting to load all data...")

train_sentences, y_train = get_data("./datasets/extended/sentences.train.txt", "./datasets/extended/scores.train.txt")
validation_sentences, y_validation = get_data("./datasets/extended/sentences.test.txt", "./datasets/extended/scores.test.txt")
word2index = load_word_mappings()

print("Processing data...")

x_train_padded = preprocess_sentences(word2index, train_sentences, max_sentence_len)
x_validation_padded = preprocess_sentences(word2index, validation_sentences, max_sentence_len)

print("Initializing training log...")

# Set the log directory and create the tensorboard callback for viewing analytics
log_dir = "./logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tb_callback = tf.keras.callbacks.TensorBoard(histogram_freq=1, log_dir=log_dir)

print("Generating model...")

dropout_rate = 0.3

inputs = Input(shape=(52, ))

embeddings = Word2Vec(max_sentence_len)(inputs)
embeddings = GaussianNoise(0.25)(embeddings)

x1 = LSTM(64, activation='tanh', recurrent_activation='sigmoid', recurrent_dropout=0, unroll=False, use_bias=True)(embeddings)
x1 = BatchNormalization()(x1)
x1 = Dropout(dropout_rate)(x1)
x1 = Dense(128, activation='relu', kernel_regularizer=l2(1e-4))(x1)
x1 = Dropout(dropout_rate)(x1)
x1 = Dense(128, activation='relu', kernel_regularizer=l2(1e-4))(x1)
x1 = Dropout(dropout_rate)(x1)
x1 = Dense(64, activation='relu', kernel_regularizer=l2(1e-4))(x1)
x1 = Dropout(dropout_rate)(x1)
x1 = Dense(32, activation='relu', kernel_regularizer=l2(1e-4))(x1)
x1 = Dropout(dropout_rate)(x1)
x1 = Dense(16, activation='relu', kernel_regularizer=l2(1e-4))(x1)
x1 = Dropout(dropout_rate)(x1)

x2 = Bidirectional(LSTM(32, activation='tanh', recurrent_activation='sigmoid', recurrent_dropout=0, unroll=False, use_bias=True))(embeddings)
x2 = BatchNormalization()(x2)
x2 = Dropout(dropout_rate)(x2)
x2 = Dense(128, activation='relu', kernel_regularizer=l2(1e-4))(x2)
x2 = Dropout(dropout_rate)(x2)
x2 = Dense(128, activation='relu', kernel_regularizer=l2(1e-4))(x2)
x2 = Dropout(dropout_rate)(x2)
x2 = Dense(64, activation='relu', kernel_regularizer=l2(1e-4))(x2)
x2 = Dropout(dropout_rate)(x2)
x2 = Dense(64, activation='relu', kernel_regularizer=l2(1e-4))(x2)
x2 = Dropout(dropout_rate)(x2)
x2 = Dense(32, activation='relu', kernel_regularizer=l2(1e-4))(x2)
x2 = Dropout(dropout_rate)(x2)

combined = concatenate([x1, x2])

x3 = Dense(48, activation='relu')(combined)
x3 = Dense(1, activation='relu')(x3)
# x3 = Dense(5, activation='softmax')(x3)
# x3 = Dense(1, activation='sigmoid')(x3)

optimizer = tf.keras.optimizers.Adam(lr=1e-4)

model = Model(inputs=inputs, outputs=x3)
# model.compile(loss=r_loss, optimizer=optimizer, metrics=[r, 'mean_squared_error'])
model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=[r, 'mean_squared_error'])
# model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy', tfa.metrics.CohenKappa(5, sparse_labels=True)])
# model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy', tfa.metrics.CohenKappa(2)])
model.summary()

tb_callback = tf.keras.callbacks.TensorBoard(histogram_freq=1, log_dir=log_dir)

timestamp_str = str(datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))
os.mkdir(f"./models/{timestamp_str}")
weights_filepath = f"./models/{timestamp_str}/C-LSTM.hdf5"

# Callback used to store the model as training goes along. Stores only the best performing model.
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=weights_filepath,
    save_weights_only=True,
    monitor='val_r',
    mode='max',
    save_best_only=True
)

print("Starting training...")
model.fit(x=x_train_padded, y=y_train, batch_size=16, epochs=60, verbose=1, shuffle=True, validation_data=(x_validation_padded, y_validation), callbacks=[tb_callback, model_checkpoint_callback])
# model.fit(x=x_train_padded, y=y_train, batch_size=32, epochs=25, verbose=1, shuffle=True, validation_data=(x_validation_padded, y_validation), callbacks=[tb_callback])

max_stars_repo_name: DanJSG/reflectiment

max_issues_repo_name: DanJSG/reflectiment

max_forks_repo_name: DanJSG/reflectiment


text:
import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import plotly.express as px

import os
import datetime as dt

plt.style.use('dark_background')
mpl.rcParams['figure.figsize'] = (12, 6)

data_path = '/home/sid/mystuff/myprogs/flirt/projects/product_analytics/customer_segmentation/data/raw'

dataset = os.path.join(data_path, 'online_retail.xlsx')

## need to install openpyxl first with get_ipython().getoutput("pip install openpyxl")

df = pd.read_excel(dataset, parse_dates=True, engine='openpyxl')
df.head()

df.shape

df.info()

df.describe().T

df['InvoiceDate'].describe()

## separate year and month from InvoiceDate
df['InvoiceYearMonth'] = df['InvoiceDate'].map(lambda d: 100 * d.year + d.month)

df.head()

df['Revenue'] = df['UnitPrice'] * df['Quantity']

## Monthly Revenue
df_revenue = df.groupby('InvoiceYearMonth')['Revenue'].sum().reset_index()

df_revenue

df['InvoiceYearMonth'] = pd.to_datetime(df['InvoiceYearMonth'],
                                                format='get_ipython().run_line_magic("Y%m')", "")

df_revenue['InvoiceYearMonth'] = pd.to_datetime(df_revenue['InvoiceYearMonth'],
                                                format='get_ipython().run_line_magic("Y%m')", "")

plt.plot(df_revenue['InvoiceYearMonth'],
         df_revenue['Revenue'], marker='o')
plt.show()

df_revenue['MonthlyGrowth'] = df_revenue['Revenue'].pct_change()

df_revenue.head()

plt.plot(df_revenue['InvoiceYearMonth'],
         df_revenue['MonthlyGrowth'], marker='o')
plt.show()

df.groupby('Country')['Revenue'].sum().sort_values(ascending=False).astype(int)

df_uk = df.query("Country=='United Kingdom'").reset_index(drop=True)
df_uk.head()

## monthly acive users in uk
df_monthly_active = df_uk.groupby('InvoiceYearMonth')['CustomerID'].nunique().reset_index()

df_monthly_active

plt.plot(df_monthly_active['InvoiceYearMonth'],
         df_monthly_active['CustomerID'], marker='o')
plt.show()

df_monthly_sales = df_uk.groupby('InvoiceYearMonth')['Quantity'].sum().reset_index()

df_monthly_sales

df_monthly_sales['Quantity'].mean()

df_monthly_order_avg = df_uk.groupby('InvoiceYearMonth')['Revenue'].mean().reset_index()

df_monthly_order_avg

df_uk.info()

tx_min_purchase = df_uk.groupby('CustomerID').InvoiceDate.min().reset_index()

tx_min_purchase.columns = ['CustomerID','MinPurchaseDate']

tx_min_purchase['MinPurchaseYearMonth'] = tx_min_purchase['MinPurchaseDate'].map(lambda date: 100*date.year + date.month)

tx_min_purchase

tx_uk = pd.merge(df_uk, tx_min_purchase, on='CustomerID')

tx_uk['MinPurchaseYearMonth'] = pd.to_datetime(tx_uk['MinPurchaseYearMonth'],
                                               format='get_ipython().run_line_magic("Y%m')", "")

tx_uk['UserType'] = 'New'
tx_uk.loc[tx_uk['InvoiceYearMonth']>tx_uk['MinPurchaseYearMonth'],'UserType'] = 'Existing'

tx_uk.UserType.value_counts()

tx_uk.head()

tx_user_type_revenue = tx_uk.groupby(['InvoiceYearMonth','UserType'])['Revenue'].sum().reset_index()

tx_user_type_revenue.query("InvoiceYearMonth get_ipython().getoutput("= 20101201 and InvoiceYearMonth != 20111201")")

tx_user_type_revenue = tx_user_type_revenue.query("InvoiceYearMonth get_ipython().getoutput("= 20101201 and InvoiceYearMonth != 20111201")")

tx_user_ratio = tx_uk.query("UserType == 'New'").groupby(['InvoiceYearMonth'])['CustomerID'].nunique()/tx_uk.query("UserType == 'Existing'").groupby(['InvoiceYearMonth'])['CustomerID'].nunique()
tx_user_ratio = tx_user_ratio.reset_index()
tx_user_ratio = tx_user_ratio.dropna()

tx_uk.query("UserType == 'New'").groupby(['InvoiceYearMonth'])['CustomerID'].nunique()

tx_uk.query("UserType == 'Existing'").groupby(['InvoiceYearMonth'])['CustomerID'].nunique()

tx_min_purchase['MinPurchaseYearMonth'] = pd.to_datetime(tx_min_purchase['MinPurchaseYearMonth'],
                                               format='get_ipython().run_line_magic("Y%m')", "")

tx_min_purchase.head()

unq_month_year =  tx_min_purchase.MinPurchaseYearMonth.unique()

unq_month_year

def generate_signup_date(year_month):
    signup_date = [el for el in unq_month_year if year_month >= el]
    return np.random.choice(signup_date)

tx_min_purchase['SignupYearMonth'] = tx_min_purchase.apply(lambda row: generate_signup_date(row['MinPurchaseYearMonth']),axis=1)

tx_min_purchase['InstallYearMonth'] = tx_min_purchase.apply(lambda row: generate_signup_date(row['SignupYearMonth']),axis=1)

tx_min_purchase.head()

channels = ['organic','inorganic','referral']

tx_min_purchase['AcqChannel'] = tx_min_purchase.apply(lambda x: np.random.choice(channels),axis=1)

tx_activation = tx_min_purchase[tx_min_purchase['MinPurchaseYearMonth'] == tx_min_purchase['SignupYearMonth']].groupby('SignupYearMonth').CustomerID.count()/tx_min_purchase.groupby('SignupYearMonth').CustomerID.count()
tx_activation = tx_activation.reset_index()

tx_activation_ch = tx_min_purchase[tx_min_purchase['MinPurchaseYearMonth'] == tx_min_purchase['SignupYearMonth']].groupby(['SignupYearMonth','AcqChannel']).CustomerID.count()/tx_min_purchase.groupby(['SignupYearMonth','AcqChannel']).CustomerID.count()
tx_activation_ch = tx_activation_ch.reset_index()

tx_activation_ch.head(10)

df_monthly_active = tx_uk.groupby('InvoiceYearMonth')['CustomerID'].nunique().reset_index()

tx_user_purchase = tx_uk.groupby(['CustomerID','InvoiceYearMonth'])['Revenue'].sum().astype(int).reset_index()

tx_user_purchase

tx_user_purchase.Revenue.sum()

tx_retention = pd.crosstab(tx_user_purchase['CustomerID'], tx_user_purchase['InvoiceYearMonth']).reset_index()

tx_retention.head()

months = tx_retention.columns[2:]

months

retention_array = []
for i in range(len(months)-1):
    retention_data = {}
    selected_month = months[i+1]
    prev_month = months[i]
    retention_data['InvoiceYearMonth'] = selected_month.strftime('get_ipython().run_line_magic("Y%m')", "")
    retention_data['TotalUserCount'] = tx_retention[selected_month].sum()
    retention_data['RetainedUserCount'] = tx_retention[(tx_retention[selected_month]>0) & (tx_retention[prev_month]>0)][selected_month].sum()
    retention_array.append(retention_data)

tx_retention = pd.DataFrame(retention_array)
tx_retention.head(10)

tx_retention['RetentionRate'] = tx_retention['RetainedUserCount']/tx_retention['TotalUserCount']

tx_retention['InvoiceYearMonth'] = pd.to_datetime(tx_retention['InvoiceYearMonth'],
                                               format='get_ipython().run_line_magic("Y%m')", "")
tx_retention.head()

tx_retention['ChurnRate'] =  1- tx_retention['RetentionRate']

tx_user_purchase.head()

tx_min_purchase.head()

tx_retention = pd.crosstab(tx_user_purchase['CustomerID'], tx_user_purchase['InvoiceYearMonth']).reset_index()

tx_retention = pd.merge(tx_retention,tx_min_purchase[['CustomerID','MinPurchaseYearMonth']],on='CustomerID')

tx_retention.head()

tx_retention.columns

new_column_names = [ 'm_' + str(column) for column in tx_retention.columns[:-1]]
new_column_names.append('MinPurchaseYearMonth')

tx_retention.columns = new_column_names

tx_retention

months

retention_array = []

for i in range(len(months)):
    retention_data = {}
    selected_month = months[i]
    prev_months = months[:i]
    next_months = months[i+1:]
    for prev_month in prev_months:
        retention_data[prev_month] = np.nan

    total_user_count = tx_retention[tx_retention.MinPurchaseYearMonth == selected_month].MinPurchaseYearMonth.count()
    retention_data['TotalUserCount'] = total_user_count
    retention_data[selected_month] = 1

    query = "MinPurchaseYearMonth == {}".format(selected_month)

    for next_month in next_months:
        new_query = query + " and {} > 0".format(str('m_' + str(next_month)))
        retention_data[next_month] = np.round(tx_retention.query(new_query)['m_' + str(next_month)].sum()/total_user_count,2)
    retention_array.append(retention_data)

max_stars_repo_name: sidd-hart/customer-segmentation

max_issues_repo_name: sidd-hart/customer-segmentation

max_forks_repo_name: sidd-hart/customer-segmentation


text:
import re
import torch
import numpy as np

from PIL import Image
from gensim.models.doc2vec import Doc2Vec
from torch.nn.functional import softmax
from bytes.create_data.bytes2img import hex2mat

def get_opcode_sequence(file):
    """
    Get opcode sequence from .asm file
    :param file: The name of .asm file
    :return: The opcode sequence
    """
    # Save opcode sequences in string arrays
    opcode_seq = []
    # Use regular expression to get all opcodes and combine opcodes into sequences
    p = re.compile(r'\s([a-fA-F0-9]{2}\s)+\s*([a-z]+)')
    with open(file, errors='ignore') as f:
        for line in f:
            # If a line starts with .text, it is a assembly instruction line
            if line.startswith(".text"):
                m = re.findall(p, line)
                if m:
                    opc = m[0][1]
                    if opc != "align":
                        opcode_seq.append(opc)
    return opcode_seq

def vectorize_asm(file, model_path):
    """
    Vectorize uploaded .asm file.
    :param file: the path of uploaded .asm file
    :param model_path: the path of Doc2Vec model
    :return: 1*256 vector
    """
    model = Doc2Vec.load(model_path)
    vector = model.infer_vector(get_opcode_sequence(file))

    return vector

def predict_file(file, file_type):
    """
    Predict uploaded file.
    :param file: the path of uploaded file
    :param file_type: the type of uploaded file (asm/bytes/bmp)
    :return: probability of each category
    """
    probability = []
    if file_type == "asm":
        # get Doc2Vec vector
        vector = vectorize_asm(file, "./display/model/doc2vec.model")
        vector = np.reshape(vector, (1, 1, 256))

        x = torch.from_numpy(vector).cuda()

        model = torch.load("./display/model/lstm_dpcnn.pth").cuda()

        model.eval()
        output = model(x).cpu()
        probability = softmax(output, dim=1)
        probability = probability.detach().numpy()
        probability = np.reshape(probability, 9).tolist()

        for i in range(9):
            if probability[i] < 1e-6:
                probability[i] = 0

    elif file_type == "bytes":
        # get n * width array
        mat = hex2mat(file)

        img = Image.fromarray(mat)
        # convert image to bit map
        img = img.convert("L")
        img = img.resize((224, 224))
        mat = np.asarray(img, dtype=np.float32)
        mat = np.reshape(mat, (1, 1, 224, 224))
        mat = np.repeat(mat, 3, axis=1)
        # get 1*3*256*256 tensor
        x = torch.from_numpy(mat).cuda()

        model = torch.load("./display/model/resnet.pth").cuda()

        model.eval()
        output = model(x).cpu()
        probability = softmax(output, dim=1)
        probability = probability.detach().numpy()
        probability = np.reshape(probability, 9).tolist()

        for i in range(9):
            if probability[i] < 1e-6:
                probability[i] = 0

    elif file_type == "bmp":
        image = Image.open(file)
        image = image.resize((224, 224))
        mat = np.asarray(image, dtype=np.float32)
        mat = np.reshape(mat, (1, 1, 224, 224))
        mat = np.repeat(mat, 3, axis=1)
        # get 1*3*256*256 tensor
        x = torch.from_numpy(mat).cuda()

        model = torch.load("./display/model/resnet.pth").cuda()

        model.eval()
        output = model(x).cpu()
        probability = softmax(output, dim=1)
        probability = probability.detach().numpy().tolist()[0]

        for i in range(9):
            if probability[i] < 1e-6:
                probability[i] = 0

    return probability

max_stars_repo_name: Chen-Junbao/MalwareClassification

max_issues_repo_name: Chen-Junbao/MalwareClassification

max_forks_repo_name: Chen-Junbao/MalwareClassification


text:
#!/usr/bin/env python
import glob,os

import numpy as np

from bunch import Bunch
import mygis as io
import load_data

def adjust_p(p,h,dz):
    '''Convert p [Pa] at elevation h [m] by shifting its elevation by dz [m]'''
    # p    in pascals
    # h,dz in meters
    # slp = p/(1 - 2.25577E-5*h)**5.25588
    # p=slp*(1 - 2.25577E-5*(h+dz))**5.25588
    p*=(1 - 2.25577E-5*(h+dz))**5.25588

def update_base(base,filename,nz):
    data=load_data.cols(filename)
    nz=min(data.shape[0]-1,nz)
    base.z=data[:nz,0]
    base.dz=np.diff(data[:nz+1,0]).reshape((1,nz,1))
    base.th=data[:nz,1].reshape((1,nz,1))
    base.qv=data[:nz,2].reshape((1,nz,1))/1000.0

def main():
    filename="init"
    nx,nz,ny=(20.,10.,19)
    dims=[nx,nz,ny]

    # po=np.log(100000.0)
    # p1=np.log(50000.0)
    # dp=(po-p1)/nz
    # p=np.exp(np.arange(po,p1,-dp)),
    lonmin=-110.0; lonmax=-100.0; dlon=(lonmax-lonmin)/nx
    latmin=35.0; latmax=45.0; dlat=(latmax-latmin)/ny

    base=Bunch(u=10.0,w=0.0,v=0.0,
               qv=0.0013,qc=0.0,
               p=100000.0,
               th=np.arange(273.0,300,(300-273.0)/nz),
               dz=400.0)
    base.z=np.arange(0,nz*base.dz,base.dz)
    if glob.glob("sounding.txt"):
        update_base(base,"sounding.txt",nz)
        nz=base.th.size
        dims=[nx,nz,ny]
    # base.p=base.p[:nz]

    u=np.zeros(dims,dtype="f")+base.u
    w=np.zeros(dims,dtype="f")+base.w
    v=np.zeros(dims,dtype="f")+base.v
    qv=np.zeros(dims,dtype="f")+base.qv
    # qv+=np.sin(np.arange(dims[1])/dims[1]*2*np.pi)+1
    qc=np.zeros(dims,dtype="f")+base.qc
    coscurve=np.cos(np.arange(dims[0])/dims[0]*2*np.pi+np.pi)+1
    # p-=coscurve.reshape((nx,1,1))*15000
    hgt=(coscurve*1000).reshape((nx,1)).repeat(ny,axis=1)

    lon=np.arange(lonmin,lonmax,dlon)
    lat=np.arange(latmin,latmax,dlat)
    lat,lon=np.meshgrid(lat,lon) #note that this appears "backwards" but these are C-style, fortran will be reversed
    dz=np.zeros(dims)+base.dz
    z=np.zeros(dims,dtype="f")+base.z.reshape((1,nz,1))+hgt.reshape((nx,1,ny))

    layer1=(dz[:,0,:]/2)
    z[:,0,:]+=layer1
    for i in range(1,int(nz)):
        z[:,i,:]=z[:,i-1,:]+(dz[:,i-1,:]+dz[:,i,:])/2.0

    p=np.zeros(dims,dtype="f")+base.p# .reshape((1,nz,1))
    adjust_p(p,0.0,z)
    th=np.zeros(dims,dtype="f")+base.th.reshape((1,nz,1))

    d3dname=("x","z","y")
    d2dname=("x","y")
    othervars=[Bunch(data=lat,name="XLAT", dims=d2dname,dtype="f",attributes=dict(units="deg",  description="Latitude")),
               Bunch(data=lon,name="XLONG",dims=d2dname,dtype="f",attributes=dict(units="deg",  description="Longitude")),
               Bunch(data=v,  name="v",    dims=d3dname,dtype="f",attributes=dict(units="m/s",  description="Horizontal (y) wind speed")),
               Bunch(data=w,  name="w",    dims=d3dname,dtype="f",attributes=dict(units="m/s",  description="Vertical wind speed")),
               Bunch(data=qv, name="qv",   dims=d3dname,dtype="f",attributes=dict(units="kg/kg",description="Water vapor mixing ratio")),
               Bunch(data=qc, name="qc",   dims=d3dname,dtype="f",attributes=dict(units="kg/kg",description="Cloud water mixing ratio")),
               Bunch(data=p,  name="p",    dims=d3dname,dtype="f",attributes=dict(units="Pa",   description="Pressure")),
               Bunch(data=th, name="th",   dims=d3dname,dtype="f",attributes=dict(units="K",    description="Potential temperature")),
               Bunch(data=dz, name="dz",   dims=d3dname,dtype="f",attributes=dict(units="m",    description="Layer thickness")),
               Bunch(data=z,  name="z",    dims=d3dname,dtype="f",attributes=dict(units="m",    description="Layer Height AGL")),
               Bunch(data=hgt,name="hgt",  dims=d2dname,dtype="f",attributes=dict(units="m",    description="Terrain Elevation"))
               ]
    print(filename)
    fileexists=glob.glob(filename) or glob.glob(filename+".nc")
    if fileexists:
        print("Removing : "+fileexists[0])
        os.remove(fileexists[0])
    print(u.shape)
    print(lat.shape)
    print(hgt.shape)
    print(lon.shape)
    io.write(filename,  u,varname="u", dims=d3dname,dtype="f",attributes=dict(units="m/s",description="Horizontal (x) wind speed"),
            extravars=othervars)

if __name__ == '__main__':
    main()

max_stars_repo_name: scrasmussen/icar

max_issues_repo_name: scrasmussen/icar

max_forks_repo_name: scrasmussen/icar


text:
"""
utils.py

"""
import numpy as np
import pandas as pd

def sum_squared_jump(tracks, pixel_size_um=1.0):
    """
    For each trajectory in a dataset, calculate the sum
    of squared jumps in um.

    args
    ----
        tracks          :   pandas.DataFrame, input trajectories
        pixel_size_um   :   float, size of pixels in um

    returns
    -------
        pandas.DataFrame, indexed by trajectory, with the columns
            "trajectory", "n_jumps", "sum_sq_jump"

    """
    # Work with a copy of the trajectories
    tracks = tracks.copy()

    # Exclude singlets
    tracks = track_length(tracks)
    tracks = tracks[tracks["track_length"] > 1]

    # Convert from pixels to um
    tracks[['y', 'x']] = tracks[['y', 'x']] * pixel_size_um

    # Sort
    tracks = tracks.sort_values(by=["trajectory", "frame"])
    n_tracks = tracks["trajectory"].nunique()

    # Calculate YX displacement vectors
    _T = np.asarray(tracks[['frame', 'trajectory', 'y', 'x',
        'track_length', 'track_length']])
    vecs = _T[1:,:] - _T[:-1,:]

    # Map trajectory indices back
    vecs[:,5] = _T[:-1,1]
    vecs[:,4] = _T[:-1,4]

    # Only consider points originating from the same trajectory
    # and from subsequent frames
    take = np.logical_and(vecs[:,1] == 0.0, vecs[:,0] == 1.0)
    vecs = vecs[take, :]

    # Calculate squared 2D radial jumps
    vecs[:,1] = vecs[:,2]**2 + vecs[:,3]**2

    # Format as pandas.DataFrame for groupby
    M = vecs.shape[0]
    df = pd.DataFrame(index=np.arange(M),
        columns=["sq_jump", "trajectory"])
    df["sq_jump"] = vecs[:,1]
    df["trajectory"] = vecs[:,5]
    df = df.join(
        df.groupby("trajectory").size().rename("n_jumps"),
        on="trajectory"
    )

    # Sum of squared displacements for each trajectory
    S = pd.DataFrame(index=np.arange(n_tracks),
        columns=["sum_sq_jump", "trajectory", "n_jumps"])
    S["sum_sq_jump"] = np.asarray(df.groupby("trajectory")["sq_jump"].sum())
    S["trajectory"] = np.asarray(df.groupby("trajectory").apply(lambda i: i.name))
    S["n_jumps"] = np.asarray(df.groupby("trajectory").size())

    return S

def track_length(tracks):
    """
    Calculate the number of points in each of a set of trajectories.

    args
    ----
        tracks      :   pandas.DataFrame

    returns
    -------
        same dataframe with "track_length" column

    """
    if "track_length" in tracks.columns:
        tracks = tracks.drop("track_length", axis=1)
    tracks = tracks.join(
        tracks.groupby("trajectory").size().rename("track_length"),
        on="trajectory"
    )
    return tracks

max_stars_repo_name: alecheckert/gibberdiff

max_issues_repo_name: alecheckert/gibberdiff

max_forks_repo_name: alecheckert/gibberdiff


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
import inspect
import itertools
import json
import numbers
import os
import shutil
import sys
import time
import uuid
from collections import namedtuple
from functools import partial

import numpy as np
from trident.backend import iteration_tools
from trident.data.dataset import ZipDataset
from trident.backend.common import to_list, addindent, get_time_suffix, format_time, get_terminal_size, get_session, \
    snake2camel, PrintException, unpack_singleton, enforce_singleton, OrderedDict, split_path, sanitize_path
from trident.backend.model import ModelBase, progress_bar
from trident.callbacks.visualization_callbacks import *
from trident.data.data_provider import *
from trident.misc.ipython_utils import *
from trident.misc.visualization_utils import tile_rgb_images, loss_metric_curve
from trident.backend.tensorspec import TensorSpec, assert_spec_compatibility

__all__ = ['load_torch_as_tf_model']

_session = get_session()
_backend = _session.backend
if _backend == 'pytorch':
    import torch
    import torch.nn as nn
    from trident.backend.pytorch_backend import *
    from trident.backend.pytorch_ops import *
    from trident.optims.pytorch_optimizers import *
elif _backend == 'tensorflow':
    import tensorflow as tf
    from trident.backend.tensorflow_backend import *
    from trident.backend.tensorflow_ops import *
    from trident.optims.tensorflow_optimizers import *

class _IncompatibleKeys(namedtuple('IncompatibleKeys', ['missing_keys', 'unexpected_keys'])):
    def __repr__(self):
        if not self.missing_keys and not self.unexpected_keys:
            return '<All keys matched successfully>'
        return super(_IncompatibleKeys, self).__repr__()

    __str__ = __repr__

def load_torch_as_tf_model(model:Layer,state_dict,strict=False):

        missing_keys = []
        unexpected_keys = []
        error_msgs = []

        # copy state_dict so _load_from_state_dict can modify it
        metadata = getattr(state_dict, '_metadata', None)
        state_dict = state_dict.copy()

        def load(module, prefix=''):
            local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
            load_as_tf_from_torch_state_dict(module,state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys,
                                         error_msgs)
            for name, child in module._modules.items():
                if child is not None:
                    load(child, prefix + name + '.')

        load(model)
        load = None  # break load->load reference cycle

        if strict:
            if len(unexpected_keys) > 0:
                error_msgs.insert(0, 'Unexpected key(s) in state_dict: {}. '.format(
                    ', '.join('"{}"'.format(k) for k in unexpected_keys)))
            if len(missing_keys) > 0:
                error_msgs.insert(0, 'Missing key(s) in state_dict: {}. '.format(
                    ', '.join('"{}"'.format(k) for k in missing_keys)))

        # if len(error_msgs) > 0:
        #     raise RuntimeError(
        #         'Error(s) in loading state_dict for {}:\n\t{}'.format(self.__class__.__name__, "\n\t".join(error_msgs)))
        return _IncompatibleKeys(missing_keys, unexpected_keys)

def load_as_tf_from_torch_state_dict(model:Layer,state_dict, prefix, local_metadata, strict,
                          missing_keys, unexpected_keys, error_msgs):
    r"""Copies parameters and buffers from :attr:`state_dict` into only
    this module, but not its descendants. This is called on every submodule
    in :meth:`~torch.nn.Module.load_state_dict`. Metadata saved for this
    module in input :attr:`state_dict` is provided as :attr:`local_metadata`.
    For state dicts without metadata, :attr:`local_metadata` is empty.
    Subclasses can achieve class-specific backward compatible loading using
    the version number at `local_metadata.get("version", None)`.

    .. note::
        :attr:`state_dict` is not the same object as the input
        :attr:`state_dict` to :meth:`~torch.nn.Module.load_state_dict`. So
        it can be modified.

    Arguments:
        model ():
        state_dict (dict): a dict containing parameters and
            persistent buffers.
        prefix (str): the prefix for parameters and buffers used in this
            module
        local_metadata (dict): a dict containing the metadata for this module.
            See
        strict (bool): whether to strictly enforce that the keys in
            :attr:`state_dict` with :attr:`prefix` match the names of
            parameters and buffers in this module
        missing_keys (list of str): if ``strict=True``, add missing keys to
            this list
        unexpected_keys (list of str): if ``strict=True``, add unexpected
            keys to this list
        error_msgs (list of str): error messages should be added to this
            list, and will be reported together in
            :meth:`~torch.nn.Module.load_state_dict`
    """
    for hook in model._load_state_dict_pre_hooks.values():
        hook(state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs)

    persistent_buffers = {k: v for k, v in model._buffers.items() if k not in model._non_persistent_buffers_set}
    local_name_params = itertools.chain(model._parameters.items(), persistent_buffers.items())
    local_state = {k: v for k, v in local_name_params if v is not None}

    #torch   out-filters  in-filters,, kernel
    # tf    kernel,in-filters, out-filters
    #torch=>tf   permute(2,3,1,0)

    for name, param in local_state.items():
        key = prefix + name
        if key in state_dict:
            input_param = state_dict[key]

            # Backward compatibility: loading 1-dim tensor from 0.3.* to version 0.4+
            if len(param.shape) == 0 and len(input_param.shape) == 1:
                input_param = input_param[0]

            if input_param.ndim>=3 and 'conv' in name:
                axis_order=list(range(ndim))[2:]
                axis_order.append(1)
                axis_order.append(0)
                permute_input_param=input_param.permute(*axis_order)
                if permute_input_param.shape != param.shape:
                    # local shape should match the one in checkpoint
                    error_msgs.append('size mismatch for {}: copying a param with shape {} from checkpoint, '
                                      'the shape in current model is {}.'
                                      .format(key, permute_input_param.shape, param.shape))
                    continue
                try:
                    param.assign(to_tensor(permute_input_param))

                except Exception as ex:
                    error_msgs.append('While copying the parameter named "{}", '
                                      'whose dimensions in the model are {} and '
                                      'whose dimensions in the checkpoint are {}, '
                                      'an exception occured : {}.'
                                      .format(key, numel(param), numel(permute_input_param), ex.args))
            else:

                if input_param.shape != param.shape:
                    # local shape should match the one in checkpoint
                    error_msgs.append('size mismatch for {}: copying a param with shape {} from checkpoint, '
                                      'the shape in current model is {}.'
                                      .format(key, input_param.shape, param.shape))
                    continue

                try:
                    param.assign(to_tensor(input_param))

                except Exception as ex:
                    error_msgs.append('While copying the parameter named "{}", '
                                      'whose dimensions in the model are {} and '
                                      'whose dimensions in the checkpoint are {}, '
                                      'an exception occured : {}.'
                                      .format(key, numel(param), numel(input_param), ex.args))
        elif strict:
            missing_keys.append(key)

    if strict:
        for key in state_dict.keys():
            if key.startswith(prefix):
                input_name = key[len(prefix):]
                input_name = input_name.split('.', 1)[0]  # get the name of param/buffer/child
                if input_name not in model._modules and input_name not in local_state:
                    unexpected_keys.append(key)


text:
# augmentation function

from random import randint
import numpy as np
import tensorflow as tf

def baseline(image, label):
    """
    Call this on minibatch at time of training
    This map function represents the baseline approach from MURA paper
    """
    image = tf.image.random_flip_left_right(image) #lateral inversion with P(0.5)
    image = tf.image.rot90(image, k=randint(0, 4)) #not 0-30 degrees, but 90 degree increments... so sue me!
    #TODO # rotatse up to 30 https://www.tensorflow.org/api_docs/python/tf/contrib/image/rotate
    # https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/random_rotation
    image = tf.clip_by_value(image, 0.0, 1.0) #ensure [0.0,1.0] img constraint
    return image, label

def vgg19_v1(image, labels):
    """
    Our first approach fort VGG19 model
    """
    pass

max_stars_repo_name: lvjianhuam/kshannon

max_issues_repo_name: lvjianhuam/kshannon

max_forks_repo_name: lvjianhuam/kshannon


text:
import os
import io
import uuid
import json
import numpy as np
from base64 import b64encode
from zipfile import ZipFile, ZIP_DEFLATED
from typing import Union, Tuple

def _read_file(filepath: str):
    '''This function reads file as str

    Parameters
    ----------
    filename : str
        filepath of the file to read as string

    Returns
    -------
    file_content : str
    '''

    with open(filepath, "r") as fp:
        return fp.read()

def plot_shell_mesh(node_coordinates: np.ndarray,
                    shell_node_indexes: np.ndarray,
                    field: Union[np.ndarray, None] = None,
                    is_element_field: bool = True,
                    fringe_limits: Union[Tuple[float, float], None] = None):
    ''' Plot a mesh

    Parameters
    ----------
    node_coordinates : np.ndarray
        array of node coordinates for elements
    shell_node_indexes : np.ndarray
        node indexes of shells
    field : Union[np.ndarray, None]
        Array containing a field value for every element or node
    is_element_field : bool
        if the specified field is for elements or nodes
    fringe_limits : Union[Tuple[float, float], None]
        limits for the fringe bar. Set by default to min and max.

    Returns
    -------
    html : str
        html code for plotting as string
    '''

    assert(node_coordinates.ndim == 2)
    assert(node_coordinates.shape[1] == 3)
    assert(shell_node_indexes.ndim == 2)
    assert(shell_node_indexes.shape[1] in [3, 4])
    if isinstance(field, np.ndarray):
        assert(field.ndim == 1)
        if is_element_field:
            assert(field.shape[0] == shell_node_indexes.shape[0])
        else:
            assert(field.shape[0] == node_coordinates.shape[0])

    # cast types correctly
    # the types MUST be float32
    node_coordinates = node_coordinates.astype(np.float32)
    if isinstance(field, np.ndarray):
        field = field.astype(np.float32)

    # distinguish tria and quads
    is_quad = shell_node_indexes[:, 2] != shell_node_indexes[:, 3]
    is_tria = np.logical_not(is_quad)

    # seperate tria and quads ... I know its sad :(
    tria_node_indexes = shell_node_indexes[is_tria][:, :3]
    quad_node_indexes = shell_node_indexes[is_quad]

    # we can only plot tria, therefore we need to split quads
    # into two trias
    quad_node_indexes_tria1 = quad_node_indexes[:, :3]
    # quad_node_indexes_tria2 = quad_node_indexes[:, [True, False, True, True]]
    quad_node_indexes_tria2 = quad_node_indexes[:, [0, 2, 3]]

    # assemble elements for plotting
    # This seems to take a lot of memory and you are right thinking this,
    # the issue is just in order to plot fringe values, we need to output
    # the element values at the 3 corner nodes. Since elements share nodes
    # we can not use the same nodes, thus we need to create multiple nodes
    # at the same position but with different fringe.
    nodes_xyz = np.concatenate([
        node_coordinates[tria_node_indexes].reshape((-1, 3)),
        node_coordinates[quad_node_indexes_tria1].reshape((-1, 3)),
        node_coordinates[quad_node_indexes_tria2].reshape((-1, 3))
    ])

    # fringe value and hover title
    if isinstance(field, np.ndarray):

        if is_element_field:
            n_shells = len(shell_node_indexes)
            n_tria = np.sum(is_tria)
            n_quads = n_shells - n_tria

            # split field according to elements
            field_tria = field[is_tria]
            field_quad = field[is_quad]

            # allocate fringe array
            node_fringe = np.zeros(
                (len(field_tria) + 2 * len(field_quad), 3), dtype=np.float32)

            # set fringe values
            node_fringe[:n_tria, 0] = field_tria
            node_fringe[:n_tria, 1] = field_tria
            node_fringe[:n_tria, 2] = field_tria

            node_fringe[n_tria:n_tria + n_quads, 0] = field_quad
            node_fringe[n_tria:n_tria + n_quads, 1] = field_quad
            node_fringe[n_tria:n_tria + n_quads, 2] = field_quad

            node_fringe[n_tria + n_quads:n_tria +
                        2 * n_quads, 0] = field_quad
            node_fringe[n_tria + n_quads:n_tria +
                        2 * n_quads, 1] = field_quad
            node_fringe[n_tria + n_quads:n_tria +
                        2 * n_quads, 2] = field_quad

            # flatty paddy
            node_fringe = node_fringe.flatten()
        else:
            # copy & paste ftw
            node_fringe = np.concatenate([
                field[tria_node_indexes].reshape((-1, 3)),
                field[quad_node_indexes_tria1].reshape((-1, 3)),
                field[quad_node_indexes_tria2].reshape((-1, 3))
            ])
            node_fringe = node_fringe.flatten()

        # element text
        node_txt = [str(entry) for entry in node_fringe.flatten()]
    else:
        node_fringe = np.zeros(len(nodes_xyz), dtype=np.float32)
        node_txt = [''] * len(nodes_xyz)

    # zip compression of data for HTML (reduces size)
    zdata = io.BytesIO()
    with ZipFile(zdata, 'w', compression=ZIP_DEFLATED) as zipFile:
        zipFile.writestr('/intensities', node_fringe.tostring())
        zipFile.writestr('/positions', nodes_xyz.tostring())
        zipFile.writestr('/text', json.dumps(node_txt))
    zdata = b64encode(zdata.getvalue()).decode('utf-8')

    # read html template
    _html_template = _read_file(os.path.join(
        os.path.dirname(__file__), 'resources', 'template.html'))

    # format html template file
    min_value = 0
    max_value = 0
    if fringe_limits:
        min_value = fringe_limits[0]
        max_value = fringe_limits[1]
    elif isinstance(field, np.ndarray):
        min_value = field.min()
        max_value = field.max()

    _html_div = _html_template.format(div_id=uuid.uuid4(),
                                      lowIntensity=min_value,
                                      highIntensity=max_value,
                                      zdata=zdata)

    # wrap it up with all needed js libraries
    _html_jszip_js = '<script type="text/javascript">%s</script>' % _read_file(
        os.path.join(os.path.dirname(__file__), 'resources', 'jszip.min.js'))
    _html_three_js = '<script type="text/javascript">%s</script>' % _read_file(
        os.path.join(os.path.dirname(__file__), 'resources', 'three.min.js'))
    _html_chroma_js = '<script type="text/javascript">%s</script>' % _read_file(
        os.path.join(os.path.dirname(__file__), 'resources', 'chroma.min.js'))
    _html_jquery_js = '<script type="text/javascript">%s</script>' % _read_file(
        os.path.join(os.path.dirname(__file__), 'resources', 'jquery.min.js'))

    return '''
<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8" />
        {_jquery_js}
        {_jszip_js}
        {_three_js}
        {_chroma_js}
    </head>
    <body>
        {_html_div}
    </body>
</html>'''.format(
        _html_div=_html_div,
        _jszip_js=_html_jszip_js,
        _three_js=_html_three_js,
        _chroma_js=_html_chroma_js,
        _jquery_js=_html_jquery_js)

max_stars_repo_name: sharkweek/lasso-python

max_issues_repo_name: sharkweek/lasso-python

max_forks_repo_name: sharkweek/lasso-python


text:
import cv2
import numpy as np

def create_orb_features(image: np.ndarray, max_features: int) -> tuple:
    """Создать ORB и применить к изображению.
    :param image: изображение для ORB.
    :param max_features: количество фич в ORB.
    :return: фичи, полученные с помощью ORB.
    """
    orb = cv2.ORB_create(max_features)
    image_orb_features = orb.detectAndCompute(image, None)
    return image_orb_features

def match_images(image_orb_features: tuple, template_orb_features: tuple, keep_percent: float) -> tuple:
    """Получить точки для find_homography.

    :param image_orb_features: orb фичи, полученнные из image_to_align с помощью ORB.
    :param template_orb_features: template фичи, полученные из template с помощью ORB.
    :param keep_percent: какой процент лучших фич оставить.
    :return: точки на image_orb_features и template_orb_features для find_homography.
    """
    (kps1, descs1) = image_orb_features
    (kps2, descs2) = template_orb_features

    matcher = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck=True)
    matches = matcher.match(descs1, descs2)
    matches = sorted(matches, key=lambda x: x.distance)

    number_of_matches_to_keep = int(len(matches) * keep_percent)
    matches = matches[:number_of_matches_to_keep]

    pts1 = np.zeros((len(matches), 2), dtype="float")
    pts2 = np.zeros((len(matches), 2), dtype="float")

    for (i, match_point) in enumerate(matches):
        pts1[i] = kps1[match_point.queryIdx].pt
        pts2[i] = kps2[match_point.trainIdx].pt

    return pts1, pts2

def find_homography(image_to_align: np.ndarray, template: np.ndarray, pts1: np.array, pts2: np.array):
    """Выровнять изображение.

    :param image_to_align: изображение для выравнивания.
    :param template: шаблон формы.
    :param pts1: точки на image_to_align.
    :param pts2: точки на template.
    :return: выравненное изображение.
    """

    (H, mask) = cv2.findHomography(pts1, pts2, method=cv2.RANSAC)

    (width, height) = template.shape[:2]
    aligned_image = cv2.warpPerspective(image_to_align, H, (height, width))

    return aligned_image

max_stars_repo_name: azarenkovgd/consents-recognition

max_issues_repo_name: azarenkovgd/consents-recognition

max_forks_repo_name: azarenkovgd/consents-recognition


text:
# -*- coding:utf-8 -*-

# Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the MIT License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MIT License for more details.

"""Initialization constructor."""
import numpy as np
from modnas.registry.construct import register
from modnas.core.param_space import ParamSpace

@register
class DefaultInitConstructor():
    """Constructor that initializes the architecture space."""

    def __init__(self, seed=None):
        self.seed = seed

    def __call__(self, model):
        """Run constructor."""
        ParamSpace().reset()
        seed = self.seed
        if seed:
            np.random.seed(seed)
        return model


text:
from flask import Flask, Response, render_template, redirect, url_for, request
import face_recognition
import cv2
import numpy as np
import os
import os.path
import pickle
from PIL import Image, ImageDraw

root_dir = os.getcwd()
models_dir = os.path.join(root_dir, 'models')

model_path = os.path.join(models_dir, 'knn.pkl')

app = Flask(__name__)

camera = cv2.VideoCapture(0)

def predict(X_frame, knn_clf=None, model_path=None, distance_threshold=0.4):

    if knn_clf is None and model_path is None:
        raise Exception("Must supply knn classifier either thourgh knn_clf or model_path")

    # Load a trained KNN model (if one was passed in)
    if knn_clf is None:
        with open(model_path, 'rb') as f:
            knn_clf = pickle.load(f)

    X_face_locations = face_recognition.face_locations(X_frame)

    # If no faces are found in the image, return an empty result.
    if len(X_face_locations) == 0:
        return []

    # Find encodings for faces in the test image
    faces_encodings = face_recognition.face_encodings(X_frame, known_face_locations=X_face_locations)

    # Use the KNN model to find the best matches for the test face
    closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=1)
    are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]

    # Predict classes and remove classifications that aren't within the threshold
    return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]

def show_prediction_labels_on_image(frame, predictions):

    pil_image = Image.fromarray(frame)
    draw = ImageDraw.Draw(pil_image)

    for name, (top, right, bottom, left) in predictions:
        # enlarge the predictions for the full sized image.
        top *= 2
        right *= 2
        bottom *= 2
        left *= 2
        # Draw a box around the face using the Pillow module
        draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))

        # There's a bug in Pillow where it blows up with non-UTF-8 text
        # when using the default bitmap font
        name = name.encode("UTF-8")

        # Draw a label with a name below the face
        text_width, text_height = draw.textsize(name)
        draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
        draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))

    # Remove the drawing library from memory as per the Pillow docs.
    del draw
    # Save image in open-cv format to be able to show it.

    opencvimage = np.array(pil_image)
    return opencvimage

def gen_frames(option=None):
    print(option)
    while True:
        success, frame = camera.read()  # read the camera frame

        if not success:
            break
        else:

            if option is not None:
                if option == 'face_rec':
                    process_this_frame = 29
                    img = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
                    process_this_frame = process_this_frame + 1
                    if process_this_frame % 30 == 0:
                        predictions = predict(img, model_path=model_path)
                    frame = show_prediction_labels_on_image(frame, predictions)

            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')  # concat frame one by one and show result

@app.route('/')
def index():
    # return 'Hello World'
    return render_template('web.html')

@app.route('/video_feed/<option>')
def video_feed(option):
    return Response(gen_frames(option), mimetype='multipart/x-mixed-replace; boundary=frame')

# @app.route('/face_rec')
# def face_rec():
#     return redirect(url_for('video_feed',option = 'face_rec'))
    # return Response(gen_frames(option='face_rec'), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.debug = True
    # app.run()
    app.run(host="0.0.0.0")

max_stars_repo_name: foonghk/face-recognition

max_issues_repo_name: foonghk/face-recognition

max_forks_repo_name: foonghk/face-recognition


text:
# <<BEGIN-copyright>>
# Copyright 2021, Lawrence Livermore National Security, LLC.
# See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# <<END-copyright>>

from xData import ancestry as ancestryModule
from xData import link as linkModule
from xData import xDataArray as arrayModule
from fudge import suites as suitesModule
from fudge import abstractClasses as abstractClassesModule
from . import covarianceSection as sectionModule, tokens as tokensModule

__metaclass__ = type

class parameterLink( linkModule.link ):
    """
    Establishes a link between one or more rows of a parameterCovariance and corresponding parameter(s).
    Supports linking to specific parameters inside a table or list.

    For example, if we have a 2x4 table::

      |A B C D|
      |E F G H|

    and wish to give a 4x4 covariance matrix for elements in the 2nd and 4th column of the table, we can create
    a parameterLink pointing to the table, with
    'matrixStartIndex=0',  'nParameters=4',  'parameterStartIndex=1',  'parameterStride=2'.

    The corresponding covariance matrix rows would then correspond to 'B, D, F, H'.
    """

    moniker = 'parameterLink'

    def __init__( self, label, link, nParameters=1, matrixStartIndex=0, parameterStartIndex=0, parameterStride=1, **kwargs ):
        """
        :param label:               unique label for this parameter link
        :param link:                reference to parameter, table or list
        :param nParameters:         number of parameters linked to
        :param matrixStartIndex:    row of the covariance matrix corresponding to the start of this batch of parameters
        :param parameterStartIndex: for skipping one or more parameters at the start of a table or list
        :param parameterStride:     for stepping over parameters. Stride must be an integer
        :return:
        """
        linkModule.link.__init__( self, link=link, **kwargs )
        self.label = label
        self.nParameters = int(nParameters)
        self.matrixStartIndex = int(matrixStartIndex)
        self.parameterStartIndex = int(parameterStartIndex)
        self.parameterStride = int(parameterStride)

    def toXMLList( self, indent = '', **kwargs ) :

        attrString = ' label="%s" href="%s"' % (self.label, self.build_href(**kwargs))
        if self.nParameters != 1: attrString += ' nParameters="%d"' % self.nParameters
        if self.matrixStartIndex != 0: attrString += ' matrixStartIndex="%d"' % self.matrixStartIndex
        if self.parameterStartIndex != 0: attrString += ' parameterStartIndex="%d"' % self.parameterStartIndex
        if self.parameterStride != 1: attrString += ' parameterStride="%d"' % self.parameterStride
        return [ '%s<%s%s/>' % ( indent, self.moniker, attrString ) ]

class parameters( suitesModule.suite ):

    moniker = 'parameters'

    def __init__( self ):
        suitesModule.suite.__init__( self, allowedClasses = (parameterLink,) )

    @property
    def nParameters(self):
        return sum( [link.nParameters for link in self] )

class parameterCovariance( suitesModule.suite ):
    """
    For storing unresolved resonance parameter covariances. Very similar to covariances for cross section, nubar, etc.:
    they require an energy grid + matrix.

    Each average parameter (e.g. elastic width, capture width, etc.) has its own averageParameterCovariance section.
    """

    moniker = 'parameterCovariance'

    def __init__(self, label, rowData=None, columnData=None):

        suitesModule.suite.__init__( self, [parameterCovarianceMatrix] )
        self.label = label
        self.rowData = rowData
        self.columnData = columnData

    @property
    def crossTerm( self ):
        return self.columnData is not None and self.columnData.link != self.rowData.link

    @property
    def evaluated(self):
        """
        Helper method to grab evaluated style
        FIXME method should be inherited, but abstractClasses.component defines methods that don't make sense for covariances
        """

        if not hasattr(self.getRootAncestor(), 'styles'):
            return self[0]  # For covarianceSection that is not part of a full covarianceSuite.

        evaluated = self.getRootAncestor().styles.getEvaluatedStyle()
        try:
            return self[evaluated.label]
        except IndexError:
            return self[0]

    def check( self, info ):
        """ check each section """

        from fudge import warning
        warnings = []
        for form in self:
            formWarnings = form.check(info)
            if formWarnings:
                warnings.append(warning.context("Form '%s':" % form.label, formWarnings))

        return warnings

    def toXMLList( self, indent='', **kwargs ):

        indent2 = indent + kwargs.get('incrementalIndent', '  ')

        xmlString = [indent + '<%s label="%s"' % (self.moniker, self.label)]
        if self.crossTerm: xmlString[0] += ' crossTerm="true"'
        xmlString[0] += '>'
        for dataPointer in ('rowData', 'columnData'):
            if getattr(self, dataPointer) is not None:
                xmlString.append(getattr(self, dataPointer).toXML(indent2, **kwargs))
        for form in self:
            xmlString += form.toXMLList(indent2, **kwargs)
        xmlString[-1] += '</%s>' % self.moniker
        return xmlString

    @classmethod
    def parseXMLNode( cls, element, xPath, linkData ):
        """Translate <section> element from xml."""

        xPath.append('%s[@label="%s"]' % (element.tag, element.get('label')))
        linkData['typeConversion'] = {'domainMin': float, 'domainMax': float}
        rowData_ = sectionModule.rowData.parseXMLNode(element[0], xPath, linkData)
        columnData_ = None
        if element[1].tag == "columnData":
            columnData_ = sectionModule.columnData.parseXMLNode(element[1], xPath, linkData)
        del linkData['typeConversion']
        section_ = cls(element.get('label'), rowData_, columnData_)
        start = 2 if (columnData_ is not None) else 1
        for form in element[start:]:
            formClass = {
                parameterCovarianceMatrix.moniker: parameterCovarianceMatrix,
            }.get(form.tag)
            if formClass is None:
                raise Exception("encountered unknown covariance matrix form '%s'" % form.tag)
            section_.add(formClass.parseXMLNode(form, xPath, linkData))
        xPath.pop()
        return section_

class parameterCovarianceMatrix( abstractClassesModule.form ):
    """
    Store covariances (or correlations, depending on 'type') between model parameters
    """

    moniker = 'parameterCovarianceMatrix'

    def __init__( self, label, matrix, parameters_=None, type='relativeCovariance' ):
        """

        :param label:
        :param type:        'relativeCovariance', 'absoluteCovariance' or 'correlation'
        :param parameters_: list of parameterLinks that relate rows/columns to parameters
        :param matrix:      xData.array instance containing the covariance or correlation
        :return:
        """

        ancestryModule.ancestry.__init__( self )
        self.label = label
        self.type = type
        if parameters_ is not None:
            self.parameters = parameters_
        else:
            self.parameters = parameters()
        self.parameters.setAncestor( self )
        matrix.setAncestor( self )
        self.matrix = matrix

    def check( self, info ):
        import numpy
        from fudge import warning
        warnings = []

        params = self.parameters.nParameters
        if self.matrix.shape != (params,params):
            warnings.append( warning.parameterCovarianceMismatch(params, self.matrix.shape) )

        else:
            matrix = self.matrix.constructArray()
            matrixWarnings = []

            params = []
            for parameterLink in self.parameters:
                for row in parameterLink.link.data:
                    params += row
            params = numpy.array(params)

            if self.type == tokensModule.absoluteToken:
                matrix = matrix / params / params[:,numpy.newaxis]

            uncertainty = numpy.sqrt(matrix.diagonal())
            for idx, unc in enumerate(uncertainty):
                if unc < info['minRelUnc']:
                    # FIXME should probably ignore fake resonances (e.g. with negative energies)
                    warnings.append( warning.varianceTooSmall( idx, unc**2, self ) )
                elif unc > info['maxRelUnc']:
                    warnings.append( warning.varianceTooLarge( idx, unc**2, self ) )

            vals, vecs = numpy.linalg.eigh( matrix )
            if min(vals) < info['negativeEigenTolerance']:
                matrixWarnings.append( warning.negativeEigenvalues( len(vals[vals<0]), min(vals), self ))
            minpos, maxpos = min(vals[vals>=0]),max(vals[vals>=0])
            # Check that the condition number of the matrix is reasonable
            if minpos/maxpos < info['eigenvalueRatioTolerance'] and matrix.size != 1:
                matrixWarnings.append( warning.badEigenvalueRatio( minpos/maxpos, self ) )

            if matrixWarnings:
                warnings.append( warning.context("Model parameter covariances", matrixWarnings ) )

        return warnings

    def convertUnits( self, unitMap ):

        #raise NotImplementedError()
        pass

    def fix( self, **kw ):
        '''assemble some useful info, to be handed down to children's fix() functions'''
        info = {}
        info['rowENDF_MFMT'] = None
        info['columnENDF_MFMT'] = None
        info.update( kw )
        return self.matrix.fix( **info )

    def toXMLList( self, indent = '', **kwargs ) :

        indent2 = indent + kwargs.get( 'incrementalIndent', '  ' )

        xmllist = [ '%s<%s label="%s" type="%s">' % (indent, self.moniker, self.label, self.type) ]
        xmllist += self.parameters.toXMLList( indent2, **kwargs )
        xmllist += self.matrix.toXMLList( indent2, **kwargs )
        xmllist[-1] += ('</%s>' % self.moniker)
        return xmllist

    @classmethod
    def parseXMLNode( cls, element, xPath, linkData ):

        xPath.append( element.tag )
        matrix_ = arrayModule.full.parseXMLNode( element.find( arrayModule.full.moniker ), xPath, linkData )
        PC = cls(element.get('label'), matrix_, type=element.get('type'))
        PC.parameters.parseXMLNode( element.find( parameters.moniker ), xPath, linkData )
        xPath.pop()
        return PC

class averageParameterCovariance(sectionModule.covarianceSection):
    """
    For storing unresolved resonance parameter covariances. Very similar to covariances for cross section, nubar, etc.:
    they require an energy grid + matrix.

    Each average parameter (e.g. elastic width, capture width, etc.) has its own averageParameterCovariance section.
    """

    moniker = 'averageParameterCovariance'


text:
import socket
from collision_test import Paddle, Ball
import numpy as np
import pickle
import pygame
import json

player_count = 2
Resolution = W, H = 400, 400
width = 0.1
paddle_speed = 1
ball_spawn = np.array(Resolution) / 2
normalized_ball_speed = 0.3
ball_bounds = np.array([[0, W], [0, H]], dtype="float64")
ball_radius = 3
fps = 60
speed_multiplier = 1.05
header_length = 64

def get_extreme_points():
    offset = W / 10
    yield np.array([[offset, 0], [offset, H]], dtype="float64")
    while True:
        yield np.array([[W - offset, 0], [W - offset, H]], dtype="float64")

def get_random_dir():
    angle = np.pi * np.random.random()
    angle *= 1 if np.random.randint(1) else -1
    return np.array([np.cos(angle), np.sin(angle)])

def get_ball_velocity():
    ball_dir = get_random_dir() * normalized_ball_speed
    return np.array([ball_dir[0] * W, ball_dir[1] * H])

class Network:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.player_limit = player_count
        self.connection_info = []
        self.ball = Ball(ball_spawn.copy(), get_ball_velocity(), ball_bounds.copy(), 3)
        self.points_getter = get_extreme_points()
        self.paddles = []

    def bind(self):
        self.socket.bind((self.host, self.port))
        return self

    def send_all(self, msg: bytes):
        for conn_inf in self.connection_info:
            conn_inf[0].send(msg)

    def get_header(self, msg: bytes):
        return str(len(msg)).ljust(header_length, " ").encode()

    def send_with_header(self, conn, msg: bytes):
        conn.send(self.get_header(msg) + msg)

    def send_all_with_header(self, msg: bytes):
        self.send_all(self.get_header(msg) + msg)

    def listen(self):
        # Waiting
        self.socket.listen(self.player_limit)
        for _ in range(self.player_limit):
            conn_inf = self.socket.accept()
            print(f'Connection from {conn_inf[1]} has been established')
            self.connection_info.append(conn_inf)

        # Initialize

        # - Initialize Paddles
        for conn_inf in self.connection_info:
            paddle_info = next(self.points_getter), width, paddle_speed, ball_spawn
            new_paddle = Paddle(*paddle_info)
            self.paddles.append(new_paddle)
            paddle_info_bytes = pickle.dumps([Resolution, fps, self.ball, new_paddle])
            self.send_with_header(conn_inf[0], paddle_info_bytes)

        # - Send other paddles.
        for index, conn_inf in enumerate(self.connection_info):
            self.send_with_header(conn_inf[0],
                                  pickle.dumps([self.paddles[i] for i in range(self.player_limit) if i != index]))

        # - Initialize Game Loop
        clock = pygame.time.Clock()
        collision_disabled = False

        # Game Loop

        while 1:
            # - Measure delta time.
            dt = clock.tick(fps) / 1000

            # - Send display info.
            for index, conn_inf in enumerate(self.connection_info):
                msg = json.dumps([tuple(self.ball.position), self.paddles[index].coordinate,
                                  [self.paddles[i].coordinate for i in range(self.player_limit) if
                                   i != index]]).encode()
                conn_inf[0].send(self.get_header(msg) + msg)

            # - Do ball update
            self.ball.update_pos(dt)
            self.ball.bound_check(1)
            if collision_disabled and self.ball.bound_check(0):
                self.ball.position = ball_spawn.copy()
                self.ball.velocity = get_ball_velocity()
                collision_disabled = False

            # - Get Key Strokes.
            for index, conn_inf in enumerate(self.connection_info):
                direction = int(conn_inf[0].recv(1)) - 1
                self.paddles[index].update(direction, dt)
                self.paddles[index].bound_check()

            # - Do Collisions
            if not collision_disabled:
                for paddle in self.paddles:
                    ball_in_front, paddle_hit = paddle.do_collision(self.ball, speed_multiplier)
                    if not ball_in_front:
                        collision_disabled = True
                    if paddle_hit:
                        collision_disabled = False

if __name__ == "__main__":
    Network(socket.gethostname(), 9000).bind().listen()


text:
from matplotlib import container
import numpy as np
from numpy.core.fromnumeric import partition
from numpy.random import default_rng
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from bubbleSort import bubbleSort
from quickSort import quickSort
from insertionSort import insertionSort
from selectionSort import selectionSort

# plt.style.use('dark_background')
# dark background breaks the whole program use safely

class TrackedArray():
    def __init__(self, arr):
        self.arr = np.copy(arr)
        self.reset()

    def reset(self):
        self.indices = []
        self.values = []
        self.access_type = []
        self.full_copies = []

    def track(self, key, access_type):
        self.indices.append(key)
        self.values.append(self.arr[key])
        self.access_type.append(access_type)
        self.full_copies.append(np.copy(self.arr))

    # to keep track of the activities
    # like which index was acessed or
    # what operation was performed
    # return type is either a list of tuples or a tuple
    def GetActivity(self, idx=None):
        if isinstance(idx, type(None)):
            return [(i, op) for (i, op) in zip(self.indices, self.access_type)]
        else:
            return (self.indices[idx], self.access_type[idx])

    def __getitem__(self, key):
        self.track(key, "get")
        return self.arr.__getitem__(key)

    def __setitem__(self, key, value):
        self.arr.__setitem__(key, value)
        self.track(key, "set")

    def __delitem__(self, key):
        self.track(key, "del")
        self.arr.__delitem__(key)

    def __len__(self):
        return self.arr.__len__()

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

    def __repr__(self):
        return self.arr.__repr__()

FPS = 60.0

N = 50

# creating an array and rounding it off
arr = np.round(np.linspace(1, 1000, N), 0)
# if a pseudorandom number generator is reinitialized with the same seed,
# it will produce the same sequence of numbers.
# np.random.seed(0)

# shuffling the array
np.random.shuffle(arr)
print(arr)
arr = TrackedArray(arr)

# function call for bubble sort
# bubbleSort(arr)
insertionSort(arr)
# function call for quick sort
#quickSort(arr, 0, len(arr)-1)

print(arr)

fig, ax = plt.subplots(figsize=(16, 8))

fig.suptitle(f"Sorting Algorithms")
container = ax.bar(np.arange(0, len(arr), 1), arr, align='edge', width=0.8)
ax.set(xlabel="Index", ylabel="Value")
ax.set_xlim([0, N])

def update(frame):
    for (rectangle, height) in zip(container.patches, arr.full_copies[frame]):
        rectangle.set_height(height)
        rectangle.set_color('#1f77b4')

    return (*container,)

ani = animation.FuncAnimation(fig, update, frames=range(len(arr.full_copies)),
                              blit=True, interval=1000./FPS, repeat=False)

container = ax.bar(np.arange(0, len(arr), 1),
                   align="edge", width=0.8, height=arr)
plt.show()

reason: import numpy,from numpy