text:
import numpy as np
import unittest
import pytest

from desc.geometry import FourierRZCurve, FourierXYZCurve, FourierPlanarCurve
from desc.grid import LinearGrid

class TestRZCurve(unittest.TestCase):
    def test_length(self):
        c = FourierRZCurve()
        np.testing.assert_allclose(c.compute_length(grid=20), 10 * 2 * np.pi)

    def test_curvature(self):
        c = FourierRZCurve()
        np.testing.assert_allclose(c.compute_curvature(grid=20), 1 / 10)

    def test_torsion(self):
        c = FourierRZCurve()
        np.testing.assert_allclose(c.compute_torsion(grid=20), 0)

    def test_frenet(self):
        c = FourierRZCurve()
        T, N, B = c.compute_frenet_frame(grid=np.array([[0.0, 0.0, 0.0]]))
        np.testing.assert_allclose(T, np.array([[0, 1, 0]]), atol=1e-12)
        np.testing.assert_allclose(N, np.array([[-1, 0, 0]]), atol=1e-12)
        np.testing.assert_allclose(B, np.array([[0, 0, 1]]), atol=1e-12)

    def test_misc(self):
        c = FourierRZCurve()
        grid = LinearGrid(L=1, M=4, N=4)
        c.grid = grid
        assert grid.eq(c.grid)

        R, Z = c.get_coeffs(0)
        np.testing.assert_allclose(R, 10)
        np.testing.assert_allclose(Z, 0)
        c.set_coeffs(0, 5, 0)
        np.testing.assert_allclose(
            c.R_n,
            [
                5,
            ],
        )
        np.testing.assert_allclose(c.Z_n, [])

        s = c.copy()
        assert s.eq(c)

        c.change_resolution(5)
        with pytest.raises(ValueError):
            c.R_n = s.R_n
        with pytest.raises(ValueError):
            c.Z_n = s.Z_n

class TestXYZCurve(unittest.TestCase):
    def test_length(self):
        c = FourierXYZCurve()
        np.testing.assert_allclose(c.compute_length(grid=20), 2 * 2 * np.pi)

    def test_curvature(self):
        c = FourierXYZCurve()
        np.testing.assert_allclose(c.compute_curvature(grid=20), 1 / 2)

    def test_torsion(self):
        c = FourierXYZCurve()
        np.testing.assert_allclose(c.compute_torsion(grid=20), 0)

    def test_frenet(self):
        c = FourierXYZCurve()
        T, N, B = c.compute_frenet_frame(grid=np.array([[0.0, 0.0, 0.0]]))
        np.testing.assert_allclose(T, np.array([[0, 0, 1]]), atol=1e-12)
        np.testing.assert_allclose(N, np.array([[-1, 0, 0]]), atol=1e-12)
        np.testing.assert_allclose(B, np.array([[0, -1, 0]]), atol=1e-12)

    def test_misc(self):
        c = FourierXYZCurve()
        grid = LinearGrid(L=1, M=4, N=4)
        c.grid = grid
        assert grid.eq(c.grid)

        X, Y, Z = c.get_coeffs(0)
        np.testing.assert_allclose(X, 10)
        np.testing.assert_allclose(Y, 0)
        np.testing.assert_allclose(Z, 0)
        c.set_coeffs(0, 5, 2, 3)
        np.testing.assert_allclose(c.X_n, [0, 5, 2])
        np.testing.assert_allclose(c.Y_n, [0, 2, 0])
        np.testing.assert_allclose(c.Z_n, [2, 3, 0])

        s = c.copy()
        assert s.eq(c)

        c.change_resolution(5)
        with pytest.raises(ValueError):
            c.X_n = s.X_n
        with pytest.raises(ValueError):
            c.Y_n = s.Y_n
        with pytest.raises(ValueError):
            c.Z_n = s.Z_n

class TestPlanarCurve(unittest.TestCase):
    def test_length(self):
        c = FourierPlanarCurve()
        np.testing.assert_allclose(c.compute_length(grid=20), 2 * 2 * np.pi)

    def test_curvature(self):
        c = FourierPlanarCurve()
        np.testing.assert_allclose(c.compute_curvature(grid=20), 1 / 2)

    def test_torsion(self):
        c = FourierPlanarCurve()
        np.testing.assert_allclose(c.compute_torsion(grid=20), 0)

    def test_frenet(self):
        c = FourierPlanarCurve()
        T, N, B = c.compute_frenet_frame(grid=np.array([[0.0, 0.0, 0.0]]))
        np.testing.assert_allclose(T, np.array([[0, 0, -1]]), atol=1e-12)
        np.testing.assert_allclose(N, np.array([[-1, 0, 0]]), atol=1e-12)
        np.testing.assert_allclose(B, np.array([[0, 1, 0]]), atol=1e-12)

    def test_misc(self):
        c = FourierPlanarCurve()
        grid = LinearGrid(L=1, M=4, N=4)
        c.grid = grid
        assert grid.eq(c.grid)

        r = c.get_coeffs(0)
        np.testing.assert_allclose(r, 2)
        c.set_coeffs(0, 3)
        np.testing.assert_allclose(
            c.r_n,
            [
                3,
            ],
        )

        c.normal = [1, 2, 3]
        c.center = [3, 2, 1]
        np.testing.assert_allclose(np.linalg.norm(c.normal), 1)
        np.testing.assert_allclose(c.normal * np.linalg.norm(c.center), c.center[::-1])

        s = c.copy()
        assert s.eq(c)

        c.change_resolution(5)
        with pytest.raises(ValueError):
            c.r_n = s.r_n

max_stars_repo_name: PlasmaControl/DESC

max_issues_repo_name: PlasmaControl/DESC

max_forks_repo_name: ddudt/DESC


text:
import argparse

import os

import pandas as pd
import numpy as np
from tqdm import tqdm
from sklearn.model_selection import train_test_split

tqdm.pandas()

parser = argparse.ArgumentParser()
parse.add_argument("--mimic_dir", help="The directory contaning all the required MIMIC files (ADMISSIONS, PATIENTS, DIAGNOSES_ICD, PROCEDURES_ICD, NOTEEVENTS).")
parse.add_argument("--save_dir", help="The directory where you want to save the processed files.")
args = parser.parse_args()

# ## Load data and clean admission

raw_adm = pd.read_csv(args.mimic_dir + "ADMISSIONS.csv.gz")
raw_patients = pd.read_csv(args.mimic_dir + 'PATIENTS.csv.gz')
dicd = pd.read_csv(args.mimic_dir + 'DIAGNOSES_ICD.csv.gz')
picd = pd.read_csv(args.mimic_dir + 'PROCEDURES_ICD.csv.gz')
raw_notes = pd.read_csv(args.mimic_dir + "NOTEEVENTS.csv")

# First only keep the admission IDs that are in the notes as well
adm = raw_adm.copy()
adm_in_notes = notes.HADM_ID.unique()
adm = adm[adm.HADM_ID.isin(adm_in_notes)]

# ## Process notes dataframe
#
# - Add information about death and discharge time
# - Ensures to only keep notes that were written at least 24h before discharge
# - Sample negative notes by randomly selecting at most 4 notes per patient.
# - Recombine everything to get a balanced dataset

notes = raw_notes.copy()
categories_keep = ['Nursing', 'Physician ', 'Nursing/other']
notes = notes[notes.CATEGORY.isin(categories_keep)]

notes = notes.merge(
    adm[['HADM_ID', 'DISCHTIME', 'HOSPITAL_EXPIRE_FLAG']],
    on='HADM_ID', how='left'
)

# Time manipulation
notes.DISCHTIME = pd.to_datetime(notes.DISCHTIME)
notes.CHARTTIME = pd.to_datetime(notes.CHARTTIME)
notes.CHARTDATE = pd.to_datetime(notes.CHARTDATE) + pd.DateOffset(hours=23)

notes.CHARTTIME = notes.CHARTTIME.fillna(notes.CHARTDATE)

notes = notes[notes.CHARTTIME < notes.DISCHTIME - pd.DateOffset(hours=24)]

keep_cols = ['HADM_ID', 'SUBJECT_ID', 'TEXT', 'HOSPITAL_EXPIRE_FLAG']

pos_notes = notes.loc[notes.HOSPITAL_EXPIRE_FLAG == 1, keep_cols]

neg_notes = (
    notes
    .loc[:, keep_cols]
    .query("HOSPITAL_EXPIRE_FLAG == 0")
    .groupby("HADM_ID")
    .progress_apply(lambda df: df.sample(n=4) if df.shape[0] >= 4 else df)
    .reset_index(drop=True)
)

sampled_notes = pd.concat([pos_notes, neg_notes]).drop_duplicates()
sampled_notes.HOSPITAL_EXPIRE_FLAG.value_counts()

# ## Process text content and split data

def isolate(text, chars):
    for c in chars:
        text = text.replace(c, f" {c} ")
    return text

def replace(text, chars, new=""):
    for c in chars:
        text = text.replace(c, new)
    return text

def clean_text(text):
    text = replace(text, "[**")
    text = replace(text, "**]")
    text = isolate(text, "~!@#$%^&*()_+-={}:\";',./<>?\\|`'")
    text = text.lower()

    return text

sampled_notes.TEXT = sampled_notes.TEXT.progress_apply(clean_text)

subjects = sampled_notes[['SUBJECT_ID', "HOSPITAL_EXPIRE_FLAG"]].drop_duplicates()

train_subj, rest_subj = train_test_split(
    subjects,
    test_size=0.25,
    random_state=0,
    stratify=subjects.HOSPITAL_EXPIRE_FLAG
)

valid_subj, test_subj = train_test_split(
    rest_subj.SUBJECT_ID.values,
    test_size=0.6,
    random_state=1,
    stratify=rest_subj.HOSPITAL_EXPIRE_FLAG
)

train_subj = train_subj.SUBJECT_ID.values

train_notes = sampled_notes[sampled_notes.SUBJECT_ID.isin(train_subj)].reset_index(drop=True)
valid_notes = sampled_notes[sampled_notes.SUBJECT_ID.isin(valid_subj)].reset_index(drop=True)
test_notes = sampled_notes[sampled_notes.SUBJECT_ID.isin(test_subj)].reset_index(drop=True)

# ## Save

os.makedirs(args.save_dir, exist_ok=True)
train_notes.to_csv(os.path.join(args.save_dir, "train.csv"), index=False)
valid_notes.to_csv(os.path.join(args.save_dir, "valid.csv"), index=False)
test_notes.to_csv(os.path.join(args.save_dir, "test.csv"), index=False)

ext: py

reason: import numpy


text:
# convert numpy array to ply files
import sys

import numpy as np

class PLYWriter:
    def __init__(self,
                 num_vertices: int,
                 num_faces=0,
                 face_type="tri",
                 comment="created by PLYWriter"):
        assert num_vertices > 0, "num_vertices should be greater than 0"
        assert num_faces >= 0, "num_faces shouldn't be less than 0"
        assert face_type == "tri" or face_type == "quad", "Only tri and quad faces are supported for now"

        self.ply_supported_types = [
            'char', 'uchar', 'short', 'ushort', 'int', 'uint', 'float',
            'double'
        ]
        self.corresponding_numpy_types = [
            np.int8, np.uint8, np.int16, np.uint16, np.int32, np.uint32,
            np.float32, np.float64
        ]
        self.type_map = {}
        for i, ply_type in enumerate(self.ply_supported_types):
            self.type_map[ply_type] = self.corresponding_numpy_types[i]

        self.num_vertices = num_vertices
        self.num_vertex_channels = 0
        self.vertex_channels = []
        self.vertex_data_type = []
        self.vertex_data = []
        self.num_faces = num_faces
        self.num_face_channels = 0
        self.face_channels = []
        self.face_data_type = []
        self.face_data = []
        self.face_type = face_type
        if face_type == "tri":
            self.face_indices = -np.ones((self.num_faces, 3), dtype=np.int32)
        elif face_type == "quad":
            self.face_indices = -np.ones((self.num_faces, 4), dtype=np.int32)
        self.comment = comment

    def add_vertex_channel(self, key: str, type: str, data: np.array):
        if type not in self.ply_supported_types:
            print("Unknown type " + type + " detected, skipping this channel")
            return
        if data.ndim == 1:
            assert data.size == self.num_vertices, "The dimension of the vertex channel is not correct"
            self.num_vertex_channels += 1
            if key in self.vertex_channels:
                print("WARNING: duplicate key " + key + " detected")
            self.vertex_channels.append(key)
            self.vertex_data_type.append(type)
            self.vertex_data.append(self.type_map[type](data))
        else:
            num_col = data.size // self.num_vertices
            assert data.ndim == 2 and data.size == num_col * \
                self.num_vertices, "The dimension of the vertex channel is not correct"
            data.shape = (self.num_vertices, num_col)
            self.num_vertex_channels += num_col
            for i in range(num_col):
                item_key = key + "_" + str(i + 1)
                if item_key in self.vertex_channels:
                    print("WARNING: duplicate key " + item_key + " detected")
                self.vertex_channels.append(item_key)
                self.vertex_data_type.append(type)
                self.vertex_data.append(self.type_map[type](data[:, i]))

    def add_vertex_pos(self, x: np.array, y: np.array, z: np.array):
        self.add_vertex_channel("x", "float", x)
        self.add_vertex_channel("y", "float", y)
        self.add_vertex_channel("z", "float", z)

    # TODO active and refactor later if user feedback indicates the necessity for a compact the input list
    # pass ti vector/matrix field directly
    # def add_vertex_pos(self, pos):
    #     assert isinstance(pos, (np.ndarray, ti.Matrix))
    #     if not isinstance(pos, np.ndarray):
    #         pos = pos.to_numpy()
    #     dim = pos.shape[pos.ndim-1]
    #     assert dim == 2 or dim == 3, "Only 2D and 3D positions are supported"
    #     n = pos.size // dim
    #     assert n == self.num_vertices, "Size of the input is not correct"
    #     pos = np.reshape(pos, (n, dim))
    #     self.add_vertex_channel("x", "float", pos[:, 0])
    #     self.add_vertex_channel("y", "float", pos[:, 1])
    #     if(dim == 3):
    #         self.add_vertex_channel("z", "float", pos[:, 2])
    #     if(dim == 2):
    #         self.add_vertex_channel("z", "float", np.zeros(n))

    def add_vertex_normal(self, nx: np.array, ny: np.array, nz: np.array):
        self.add_vertex_channel("nx", "float", nx)
        self.add_vertex_channel("ny", "float", ny)
        self.add_vertex_channel("nz", "float", nz)

    # TODO active and refactor later if user feedback indicates the necessity for a compact the input list
    # pass ti vector/matrix field directly
    # def add_vertex_normal(self, normal):
    #     assert isinstance(normal, (np.ndarray, ti.Matrix))
    #     if not isinstance(normal, np.ndarray):
    #         normal = normal.to_numpy()
    #     dim = normal.shape[normal.ndim-1]
    #     assert dim == 3, "Only 3D normal is supported"
    #     n = normal.size // dim
    #     assert n == self.num_vertices, "Size of the input is not correct"
    #     normal = np.reshape(normal, (n, dim))
    #     self.add_vertex_channel("nx", "float", normal[:, 0])
    #     self.add_vertex_channel("ny", "float", normal[:, 1])
    #     self.add_vertex_channel("nz", "float", normal[:, 2])

    def add_vertex_color(self, r: np.array, g: np.array, b: np.array):
        self.add_vertex_channel("red", "float", r)
        self.add_vertex_channel("green", "float", g)
        self.add_vertex_channel("blue", "float", b)

    def add_vertex_alpha(self, alpha: np.array):
        self.add_vertex_channel("Alpha", "float", alpha)

    def add_vertex_rgba(self, r: np.array, g: np.array, b: np.array,
                        a: np.array):
        self.add_vertex_channel("red", "float", r)
        self.add_vertex_channel("green", "float", g)
        self.add_vertex_channel("blue", "float", b)
        self.add_vertex_channel("Alpha", "float", a)

    # TODO active and refactor later if user feedback indicates the necessity for a compact the input list
    # pass ti vector/matrix field directly
    # def add_vertex_color(self, color):
    #     assert isinstance(color, (np.ndarray, ti.Matrix))
    #     if not isinstance(color, np.ndarray):
    #         color = color.to_numpy()
    #     channels = color.shape[color.ndim-1]
    #     assert channels == 3 or channels == 4, "The dimension for color should be either be 3 (rgb) or 4 (rgba)"
    #     n = color.size // channels
    #     assert n == self.num_vertices, "Size of the input is not correct"
    #     color = np.reshape(color, (n, channels))
    #     self.add_vertex_channel("red", "float", color[:, 0])
    #     self.add_vertex_channel("green", "float", color[:, 1])
    #     self.add_vertex_channel("blue", "float", color[:, 2])
    #     if channels == 4:
    #         self.add_vertex_channel("Alpha", "float", color[:, 3])

    def add_vertex_id(self):
        self.add_vertex_channel("id", "int", np.arange(self.num_vertices))

    def add_vertex_piece(self, piece: np.array):
        self.add_vertex_channel("piece", "int", piece)

    def add_faces(self, indices: np.array):
        if self.face_type == "tri":
            vert_per_face = 3
        else:
            vert_per_face = 4
        assert vert_per_face * \
            self.num_faces == indices.size, "The dimension of the face vertices is not correct"
        self.face_indices = np.reshape(indices,
                                       (self.num_faces, vert_per_face))
        self.face_indices = self.face_indices.astype(np.int32)

    def add_face_channel(self, key: str, type: str, data: np.array):
        if type not in self.ply_supported_types:
            print("Unknown type " + type + " detected, skipping this channel")
            return
        if data.ndim == 1:
            assert data.size == self.num_faces, "The dimension of the face channel is not correct"
            self.num_face_channels += 1
            if key in self.face_channels:
                print("WARNING: duplicate key " + key + " detected")
            self.face_channels.append(key)
            self.face_data_type.append(type)
            self.face_data.append(self.type_map[type](data))
        else:
            num_col = data.size // self.num_faces
            assert data.ndim == 2 and data.size == num_col * \
                self.num_faces, "The dimension of the face channel is not correct"
            data.shape = (self.num_faces, num_col)
            self.num_face_channels += num_col
            for i in range(num_col):
                item_key = key + "_" + str(i + 1)
                if item_key in self.face_channels:
                    print("WARNING: duplicate key " + item_key + " detected")
                self.face_channels.append(item_key)
                self.face_data_type.append(type)
                self.face_data.append(self.type_map[type](data[:, i]))

    def add_face_id(self):
        self.add_face_channel("id", "int", np.arange(self.num_faces))

    def add_face_piece(self, piece: np.array):
        self.add_face_channel("piece", "int", piece)

    def sanity_check(self):
        assert "x" in self.vertex_channels, "The vertex pos channel is missing"
        assert "y" in self.vertex_channels, "The vertex pos channel is missing"
        assert "z" in self.vertex_channels, "The vertex pos channel is missing"
        if self.num_faces > 0:
            for idx in self.face_indices.flatten():
                assert idx >= 0 and idx < self.num_vertices, "The face indices are invalid"

    def print_header(self, path: str, format: str):
        with open(path, "w") as f:
            f.writelines([
                "ply\n", "format " + format + " 1.0\n",
                "comment " + self.comment + "\n"
            ])
            f.write("element vertex " + str(self.num_vertices) + "\n")
            for i in range(self.num_vertex_channels):
                f.write("property " + self.vertex_data_type[i] + " " +
                        self.vertex_channels[i] + "\n")
            if (self.num_faces != 0):
                f.write("element face " + str(self.num_faces) + "\n")
                f.write("property list uchar int vertex_indices\n")
                for i in range(self.num_face_channels):
                    f.write("property " + self.face_data_type[i] + " " +
                            self.face_channels[i] + "\n")
            f.write("end_header\n")

    def export(self, path):
        self.sanity_check()
        self.print_header(path, "binary_" + sys.byteorder + "_endian")
        with open(path, "ab") as f:
            for i in range(self.num_vertices):
                for j in range(self.num_vertex_channels):
                    f.write(self.vertex_data[j][i])
            if self.face_type == "tri":
                vert_per_face = np.uint8(3)
            else:
                vert_per_face = np.uint8(4)
            for i in range(self.num_faces):
                f.write(vert_per_face)
                for j in range(vert_per_face):
                    f.write(self.face_indices[i, j])
                for j in range(self.num_face_channels):
                    f.write(self.face_data[j][i])

    def export_ascii(self, path):
        self.sanity_check()
        self.print_header(path, "ascii")
        with open(path, "a") as f:
            for i in range(self.num_vertices):
                for j in range(self.num_vertex_channels):
                    f.write(str(self.vertex_data[j][i]) + " ")
                f.write("\n")
            if self.face_type == "tri":
                vert_per_face = 3
            else:
                vert_per_face = 4
            for i in range(self.num_faces):
                f.writelines([
                    str(vert_per_face) + " ",
                    " ".join(map(str, self.face_indices[i, :])), " "
                ])
                for j in range(self.num_face_channels):
                    f.write(str(self.face_data[j][i]) + " ")
                f.write("\n")

    def export_frame_ascii(self, series_num: int, path: str):
        # if path has ply ending
        last_4_char = path[-4:]
        if last_4_char == ".ply":
            path = path[:-4]

        real_path = path + "_" + "{0:0=6d}".format(series_num) + ".ply"
        self.export_ascii(real_path)

    def export_frame(self, series_num: int, path: str):
        # if path has ply ending
        last_4_char = path[-4:]
        if last_4_char == ".ply":
            path = path[:-4]

        real_path = path + "_" + "{0:0=6d}".format(series_num) + ".ply"
        self.export(real_path)

max_stars_repo_name: PragmaTwice/taichi

max_issues_repo_name: PragmaTwice/taichi

max_forks_repo_name: PragmaTwice/taichi


text:
import random
import unittest
from model import enformer
import numpy as np
import torch

class TestEnformer(unittest.TestCase):

    def test_enformer(self):
        model = enformer.Enformer(channels=1536, num_heads=8, num_transformer_layers=11)
        inputs = torch.Tensor(_get_random_input())
        outputs = model(inputs)
        self.assertEqual(outputs["human"].shape, torch.Size((1, enformer.TARGET_LENGTH, 5313)))
        self.assertEqual(outputs["mouse"].shape, torch.Size((1, enformer.TARGET_LENGTH, 1643)))

def _get_random_input():
    seq = "".join(
        [random.choice("ACGT") for _ in range(enformer.SEQUENCE_LENGTH)])
    return np.expand_dims(enformer.one_hot_encode(seq), 0).astype(np.float32)

if __name__ == "__main__":
    unittest.main()

max_stars_repo_name: boxiangliu/enformer-pytorch

max_issues_repo_name: boxiangliu/enformer-pytorch

max_forks_repo_name: boxiangliu/enformer-pytorch


text:
import numpy as np
import pickle, os, glob
import matplotlib.pyplot as plt
from multidag.common.cmd_args import cmd_args
from multidag.data_generator import Dataset
from multidag.dag_utils import count_accuracy
from multidag.model import LSEM
from multidag.model import G_DAG
from torch import FloatTensor
from copy import deepcopy

p = [32]#, 64, 128, 256] #, 512, 1024]
n_samples = [10, 20, 40, 80, 160, 320]
s0 = [40, 96, 224, 512, 1152, 2560]
s = [120, 288, 672, 1536, 3456, 7680]
d = [5, 6, 7, 8, 9, 10]
sizes = [1]#, 2, 4, 8, 16, 32]

for idx in range(len(p)):
    # group_size * sample_size * task * configs
    title = ['fdr', 'tpr', 'fpr', 'shd', 'nnz']
    t = {key: {n: [] for n in n_samples} for key in sizes}
    fdr = {key: {n: [] for n in n_samples} for key in sizes}
    tpr = {key: {n: [] for n in n_samples} for key in sizes}
    fpr = {key: {n: [] for n in n_samples} for key in sizes}
    shd = {key: {n: [] for n in n_samples} for key in sizes}
    nnz = {key: {n: [] for n in n_samples} for key in sizes}
    t_std = {key: {n: [] for n in n_samples} for key in sizes}
    fdr_std = deepcopy(fdr)
    tpr_std = deepcopy(tpr)
    fpr_std = deepcopy(fpr)
    shd_std = deepcopy(shd)
    nnz_std = deepcopy(nnz)
    for n in n_samples:
        db = Dataset(p=p[idx],
                     n=n,
                     K=cmd_args.K,
                     s0=s0[idx],
                     s=s[idx],
                     d=d[idx],
                     w_range=(0.5, 2.0), verbose=False)
        root = f'./saved_models/p-{p[idx]}_n-{n}_K-{cmd_args.K}_s-{s[idx]}_s0-{s0[idx]}_d-{d[idx]}_' \
               f'w_range_l-0.5_w_range_u-2.0/GOD/'
        for size in sizes:
            A = np.zeros((cmd_args.K, p[idx], p[idx]))
            for gg in range(int(cmd_args.K / 8 / size)):
                dir = os.path.join(root, f'{size}_{8*gg*size}-{8*(gg+1)*size}.pkl')
                with open(dir, 'rb') as handle:
                    tt, A_est = pickle.load(handle)
                A[gg*size*8:(gg+1)*size*8] = A_est
                t[size][n].extend(tt)
            for k in range(cmd_args.K):
                G_true = np.abs(np.sign(db.G[k]))
                G_est = np.abs(A[k])
                G_est[G_est < cmd_args.threshold] = 0
                G_est = np.sign(G_est)
                r = count_accuracy(G_true, G_est)
                fdr[size][n].append(r['fdr'])
                tpr[size][n].append(r['tpr'])
                fpr[size][n].append(r['fpr'])
                shd[size][n].append(r['shd'])
                nnz[size][n].append(r['nnz'])
    for size in sizes:
        for n in n_samples:
            temp_fdr = list(fdr[size][n])
            temp_tpr = list(tpr[size][n])
            temp_fpr = list(fpr[size][n])
            temp_shd = list(shd[size][n])
            temp_nnz = list(nnz[size][n])
            temp_t = list(t[size][n]).copy()
            t[size][n] = np.mean(temp_t)
            fdr[size][n] = np.mean(temp_fdr)
            tpr[size][n] = np.mean(temp_tpr)
            fpr[size][n] = np.mean(temp_fpr)
            shd[size][n] = np.mean(temp_shd)
            nnz[size][n] = np.mean(temp_nnz)
            t_std[size][n] = np.std(temp_t)
            fdr_std[size][n] = np.std(temp_fdr)
            tpr_std[size][n] = np.std(temp_tpr)
            fpr_std[size][n] = np.std(temp_fpr)
            shd_std[size][n] = np.std(temp_shd)
            nnz_std[size][n] = np.std(temp_nnz)
    t = np.array([[t[size][n] for n in n_samples] for size in sizes])
    fdr = np.array([[fdr[size][n] for n in n_samples] for size in sizes])
    tpr = np.array([[tpr[size][n] for n in n_samples] for size in sizes])
    fpr = np.array([[fpr[size][n] for n in n_samples] for size in sizes])
    shd = np.array([[shd[size][n] for n in n_samples] for size in sizes])
    nnz = np.array([[nnz[size][n] for n in n_samples] for size in sizes])

    t_std = np.array([[t_std[size][n] for n in n_samples] for size in sizes])
    fdr_std = np.array([[fdr_std[size][n] for n in n_samples] for size in sizes])
    tpr_std = np.array([[tpr_std[size][n] for n in n_samples] for size in sizes])
    fpr_std = np.array([[fpr_std[size][n] for n in n_samples] for size in sizes])
    shd_std = np.array([[shd_std[size][n] for n in n_samples] for size in sizes])
    nnz_std = np.array([[nnz_std[size][n] for n in n_samples] for size in sizes])
    print(f'### time for p = {p[idx]} ###')
    for k in range(len(sizes)):
        log = f'k={int(2 ** k)} & '
        for l in range(len(n_samples)):
            log += f'${t[k][l]:.2f}\pm{t_std[k][l]:.2f}$ & '
        print(log[:-2] + '\\\\')
    print(f'### fdr for p = {p[idx]} ###')
    for k in range(len(sizes)):
        log = f'k={int(2**k)} & '
        for l in range(len(n_samples)):
            log += f'${fdr[k][l]:.3f}\pm{fdr_std[k][l]:.3f}$ & '
        print(log[:-2] + '\\\\')
    print(f'### tpr for p = {p[idx]} ###')
    for k in range(len(sizes)):
        log = f'k={int(2**k)} & '
        for l in range(len(n_samples)):
            log += f'${tpr[k][l]:.3f}\pm{tpr_std[k][l]:.3f}$ & '
        print(log[:-2] + '\\\\')
    print(f'### fpr for p = {p[idx]} ###')
    for k in range(len(sizes)):
        log = f'k={int(2**k)} & '
        for l in range(len(n_samples)):
            log += f'${fpr[k][l]:.3f}\pm{fpr_std[k][l]:.3f}$ & '
        print(log[:-2] + '\\\\')
    print(f'### shd for p = {p[idx]} ###')
    for k in range(len(sizes)):
        log = f'k={int(2**k)} & '
        for l in range(len(n_samples)):
            log += f'${shd[k][l]:.0f}\pm{shd_std[k][l]:.0f}$ & '
        print(log[:-2] + '\\\\')
    print(f'### nnz for p = {p[idx]} ###')
    for k in range(len(sizes)):
        log = f'k={int(2**k)} & '
        for l in range(len(n_samples)):
            log += f'${nnz[k][l]:.0f}\pm{nnz_std[k][l]:.0f}$ & '
        print(log[:-2] + '\\\\')

max_stars_repo_name: xinshi-chen/MultiDAG

max_issues_repo_name: xinshi-chen/MultiDAG

max_forks_repo_name: xinshi-chen/MultiDAG


text:
# Author: Michał Bednarek PUT Poznan
# Comment: Script gor generating data from Mujoco simulation for deep learning
# models. Data saved as a pickle. Each sample is a MAX_ITER_PER_EP samples of squeezing.

import os
import pickle
from argparse import ArgumentParser

import numpy as np
from tqdm import tqdm

from environment import ManEnv

NUM_EPISODES = 1
MAX_ITER_PER_EP = 160
OPEN_CLOSE_DIV = 80
START_STEP = 40

def log_into_file(args):
    assert type(args.mujoco_model_paths) is list
    num_envs = len(args.mujoco_model_paths)
    current_env = 0

    env_spec = ManEnv.get_std_spec(args)
    env = ManEnv(**env_spec)

    os.makedirs(args.data_folder, exist_ok=True)
    path = os.path.join(args.data_folder, "{}.pickle".format(args.data_name))
    file = open(path, 'wb')
    data, stiffness = list(), list()

    for ep in tqdm(range(NUM_EPISODES * num_envs)):

        current_stiffness = env.reset()
        # print(current_stiffness)

        # start squeezing an object
        samples = list()

        for _ in range(START_STEP):
            readings, contact = env.step()
            if args.mask_contact and not contact:
                readings = np.zeros_like(readings)
            if readings is not None:
                samples.append(readings)
        env.close_hand()
        for i in range(MAX_ITER_PER_EP):
            env.render()

            # perform squeezing or loose a hand
            if i % OPEN_CLOSE_DIV == 0 and i > 0:
                env.toggle_grip()

            # gather readings and mask out data when there is no contact
            readings, contact = env.step()
            if args.mask_contact and not contact:
                readings = np.zeros_like(readings)
            if readings is not None:
                samples.append(readings)

        # add to a pickle (important to use array(), not asarray(), because it makes a copy!)
        samples = np.array(samples)
        data.append(samples)
        stiffness.append(current_stiffness)

        # change env number
        if (ep + 1) % NUM_EPISODES == 0 and num_envs > 1:
            current_env += 1
            if current_env > num_envs:
                current_env = 0
            env.load_env(current_env)

    # dump data
    pickle.dump({
        "data": data,
        "stiffness": stiffness
    }, file)
    file.close()
    print("Total number of samples: {0}".format(len(data)))

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--sim-step', type=int, default=7)
    parser.add_argument('--vis', type=bool, default=True)
    parser.add_argument('--mask-contact', type=bool, default=False)
    parser.add_argument('--sim-start', type=int, default=1)
    parser.add_argument('--data-folder', type=str, default="./data/dataset/testing_datasets")
    parser.add_argument('--data-name', type=str, default="dataset_all_shapes")
    parser.add_argument('--mujoco-model-paths', nargs="+", required=True)
    args, _ = parser.parse_known_args()
    log_into_file(args)


text:
"""
Example module to run Inner Ability calculator
"""
import numpy as np
from ia_calc import IALine, IARollUniqLine, IARollLegendLine, IARollOneLineCount
from ia_calc import SecThirdLineRankRoll

req_line = IALine('Unique', 'buff', 38)
ia_roll_uniq_line = IARollUniqLine()
sec_third_line_rank_roll = SecThirdLineRankRoll()

np.random.seed(50)

n_count_list = [IARollOneLineCount.get_count(req_line, sec_third_line_rank_roll, ia_roll_uniq_line, []) for _ in range(100)]

print(np.mean(n_count_list))
print(np.percentile(n_count_list, [5, 25, 50, 75, 95]))


text:
import json
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.metrics import accuracy_score, precision_score, \
    recall_score, f1_score, roc_auc_score, roc_curve, auc, \
    confusion_matrix, log_loss, cohen_kappa_score, \
    silhouette_score, adjusted_rand_score, \
    adjusted_mutual_info_score, mutual_info_score, \
    normalized_mutual_info_score, homogeneity_score, \
    completeness_score, v_measure_score
from sklearn.model_selection import cross_val_score, learning_curve
from os.path import join, exists, isfile
import joblib

def cluster_metrics(x, y, y_pred):
    #print("Sum of squared distances:",kmeans_model.inertia_)
    #External
    #teoricas
    ad_rand = adjusted_rand_score(y*1, y_pred)
    #labs
    '''
    adjusted_mutual_info = adjusted_mutual_info_score(y, y_pred)
    mutual_info = mutual_info_score(y, y_pred)
    normalized_mutual_info = normalized_mutual_info_score(y, y_pred)
    homogeneity = homogeneity_score(y, y_pred)
    completeness= completeness_score(y, y_pred)
    v_measure = v_measure_score(y, y_pred)
    '''

    #Internal
    silhouette = silhouette_score(x, y_pred)

    #metrics.cluster.contingency_matrix(x, y)
    attrs = ['ad_rand_score', 'silhouette']

    values = [ad_rand, silhouette]

    logs = {attrs[it]: val for it, val in enumerate(values)}
    print(logs)
    return logs

def evaluate(classifier, x_train, y_train, x_test, y_test, y_predict, y_predict_train=None, iterator=None):

    full_x = x_train.append(x_test, ignore_index=True)
    full_y = y_train.append(y_test, ignore_index=True)

    # Calculate
    accuracy = accuracy_score(y_test, y_predict)
    precision = precision_score(y_test, y_predict, average='micro')
    recall = recall_score(y_test, y_predict, average='micro')
    f1 = f1_score(y_test, y_predict, average='micro')
    kappa = cohen_kappa_score(y_test, y_predict)

    attrs = ['accuracy', 'precision', 'recall', 'f1', 'kappa']
    values = [accuracy, precision, recall, f1, kappa]
    if y_predict_train is not None:
        accuracyTrain = accuracy_score(y_train, y_predict_train)
        attrs.append('accuracyTrain')
        values.append(accuracyTrain)

    # Log results
    logs = {attrs[it]: val for it, val in enumerate(values)}

    # Print
    for attr, value in sorted(logs.items()):
        print("\t{}: {}".format(attr.upper(), value))

    return logs

def printResultsToJson(logs, dir):
    with open(join(dir, 'logs.json'), 'w') as outfile:
        json.dump(logs, outfile)

def saveModel(model, dir):
    joblib.dump(model, join(dir, 'model.joblib'))

def loadModel(model, dir):
    model = joblib.load(model, join(dir, 'model.joblib'))
    return model

def confMatrix(y_test, y_predict, dir, iterator=None):

    confusionMatrix = confusion_matrix(y_test, y_predict)

    # Calculate some parameters depending on TP, FP, FN, TN
    sensitivity = confusionMatrix[0, 0]/(confusionMatrix[0, 0]+confusionMatrix[1, 0])
    specificity = confusionMatrix[1, 1]/(confusionMatrix[1, 1]+confusionMatrix[0, 1])
    recall = confusionMatrix[1, 1]/(confusionMatrix[1, 1]+confusionMatrix[1, 0])
    print('Sensitivity: %f' % sensitivity)
    print('Specificity: %f' % specificity)
    print('recall ', recall)

    # fixme - find cleaner way to do this ?
    # Log results
    attrs = ['sensitivity', 'specificity']
    values = [sensitivity, specificity]
    logs = {attrs[it]: val for it, val in enumerate(values)}

    # Plot the confusion matrix
    fig, ax = plt.subplots(figsize=(4, 4))
    ax.matshow(confusionMatrix, cmap=plt.cm.Blues, alpha=0.4)
    for i in range(confusionMatrix.shape[0]):
        for j in range(confusionMatrix.shape[1]):
            ax.text(x=j, y=i,
                    s=confusionMatrix[i, j],
                    va='center', ha='center')
    plt.xlabel('Predicted label')
    plt.ylabel('True label')
    plt.title('Confusion Matrix')
    # plt.show()
    name = join(dir, 'conf-mat.png') if iterator==None else join(dir, 'conf-mat-{}.png'.format(iterator) )
    plt.savefig(name)
    print('Plotted confusion matrix')
    plt.close(fig=fig)
    return logs

def rocCurve(y_test, y_predict, dir, iterator=None):
    # Roc score (area under the curve)
    roc_auc_score_ = roc_auc_score(y_test, y_predict, )
    print('Roc auc score = ', roc_auc_score_)

    # Compute ROC curve and ROC area for each class
    fpr, tpr, thresholds = roc_curve(y_test, y_predict)
    roc_auc = auc(fpr, tpr)  # Area under the roc curve

    # Plot of a ROC curve for a specific class
    fig = plt.figure()
    plt.plot(fpr, tpr, color='darkorange',
             label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Roc curve')
    plt.legend(loc="lower right")
    # plt.show()
    name = join(dir, 'roc.png') if iterator==None else join(dir, 'roc-{}.png'.format(iterator))
    plt.savefig(name)
    print('Plotted roc curve')
    plt.close(fig=fig)

def plot_learning_curve(estimator, X, y, dir, ylim=None, cv=None,
                        n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    Parameters
    ----------
    estimator : object type that implements the "fit" and "predict" methods
        An object of that type which is cloned for each validation.

    title : string
        Title for the chart.

    X : array-like, shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples) or (n_samples, n_features), optional
        Target relative to X for classification or regression;
        None for unsupervised learning.

    ylim : tuple, shape (ymin, ymax), optional
        Defines minimum and maximum yvalues plotted.

    cv : int, cross-validation generator or an iterable, optional
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

        For integer/None inputs, if ``y`` is binary or multiclass,
        :class:`StratifiedKFold` used. If the estimator is not a classifier
        or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validators that can be used here.

    n_jobs : int or None, optional (default=None)
        Number of jobs to run in parallel.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    train_sizes : array-like, shape (n_ticks,), dtype float or int
        Relative or absolute numbers of training examples that will be used to
        generate the learning curve. If the dtype is float, it is regarded as a
        fraction of the maximum size of the training set (that is determined
        by the selected validation method), i.e. it has to be within (0, 1].
        Otherwise it is interpreted as absolute sizes of the training sets.
        Note that for classification the number of samples usually have to
        be big enough to contain at least one sample from each class.
        (default: np.linspace(0.1, 1.0, 5))
    """
    plt.figure()
    plt.title('learning curve')
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    # plt.show()

    plt.savefig(join(dir, 'learning.png'))
    print('Plotted learning curve')

def plotParamVariationPretty(x, y1, y2, y3, label1='', label2='', label3='', xlabel='', ylabel='', title='', path='.'):
    fig, ax1 = plt.subplots()
    ax1.grid(which='major', alpha=0.3)

    ax1.tick_params(axis='y', labelcolor='black')
    ax1.plot(x, y1, label=label1)
    ax1.plot(x, y2, label=label2)

    if y3 != None:
        ax2 = ax1.twinx()
        ax2.plot(x, y3, label=label3, color='green')
        ax2.tick_params(axis='y', labelcolor='green')

    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.title(title)

    print('----------------------------------------------------------------------------------------------------------')
    plt.legend()
    plt.savefig(path)
    plt.close(fig=fig)

def plotEvolutionaryPlots(x, y1, y2, y3=None, clf_family=None):
    if y3 == None:
        label3=None
    else:
        label3='cost'
    if clf_family == "knn_un":
        plotParamVariationPretty(x, y1, y2, y3, label1='recall', label2='precision', label3=label3,
                                 xlabel='Number of estimators', \
                                 ylabel='K value', title='KNN uniform weights - k tuning', path='./media/' + clf_family)

    if clf_family == "knn_dist":
        plotParamVariationPretty(x, y1, y2, y3, label1='recall', label2='precision', label3=label3, xlabel='K value', \
                                 ylabel='Performance', title='KNN distributed weights - K tunning',
                                 path='./media/' + clf_family)

    if clf_family == "rf_entr":
        plotParamVariationPretty(x, y1, y2, y3, label1='recall', label2='precision', label3=label3, xlabel='Depth', \
                                 ylabel='Performance', title='Random Forest entropy - depth tuning',
                                 path='./media/' + clf_family)

    if clf_family == "rf_gini":
        plotParamVariationPretty(x, y1, y2, y3, label1='recall', label2='precision', label3=label3, xlabel='Depth', \
                                 ylabel='Performance', title='Random Forest gini - depth tuning',
                                 path='./media/' + clf_family)

    if clf_family == "rf_gini_n":
        plotParamVariationPretty(x, y1, y2, y3, label1='recall', label2='precision', label3=label3,
                                 xlabel='nº estimators', \
                                 ylabel='Performance', title='Random Forest Gini - nº estimators tunning',
                                 path='./media/' + clf_family)

max_stars_repo_name: andre-cavalheiro/datascience-project

max_issues_repo_name: andre-cavalheiro/datascience-project

max_forks_repo_name: andre-cavalheiro/datascience-project


text:
from collections import Counter

import pytest
import numpy as np

from voyapt.statistics import (
    get_bin_midpoints,
    probability_density_function,
    probability_mass_function,
    pmf_reduce_to_univariate,
    monte_carlo,
)

def test_get_bin_midpoint():
    assert np.array_equal([1, 3, 5], get_bin_midpoints([0, 2, 4, 6]))

def test_probability_density_function_1d():
    pdf, edges = probability_density_function(np.random.random_sample(10000))

    assert (pdf * np.diff(edges)).sum() == pytest.approx(1.0)
    assert len(edges) == 1

def test_probability_density_function_2d():
    samples = np.random.random_sample(10000).reshape(-1, 2)
    pdf, edges = probability_density_function(samples)

    assert (
        pdf * np.outer(np.diff(edges[0]), np.diff(edges[1]))
    ).sum() == pytest.approx(1.0)
    assert len(edges) == 2

def test_probability_mass_function_1d():
    pmf, coords, edges = probability_mass_function(np.random.random_sample(10000))

    assert pmf.sum() == pytest.approx(1.0)
    assert len(coords) == 1
    assert len(edges) == 1

def test_probability_mass_function_2d():
    samples = np.random.random_sample(10000).reshape(-1, 2)
    pmf, coords, edges = probability_mass_function(samples)

    assert pmf.sum() == pytest.approx(1.0)
    assert len(coords) == 2
    assert len(edges) == 2

def test_pmf_reduce_to_univariate():
    samples = np.random.random_sample(10).reshape(-1, 2)
    pmf, coords, edges = probability_mass_function(samples)

    univariate_pmf, univariate_coords = pmf_reduce_to_univariate(
        pmf, coords, lambda x: np.sum(x, axis=1)
    )

    assert univariate_pmf.sum() == pytest.approx(1.0)
    assert (sorted(univariate_coords) == univariate_coords).all()

def test_monte_carlo_uniform():
    rng = np.random.default_rng()
    samples = rng.uniform(size=10)

    draws = monte_carlo(samples)

    assert len(draws) == 1e6
    assert set(draws) == set(samples)

def test_monte_carlo_probs():
    rng = np.random.default_rng()
    samples = rng.uniform(size=10)

    probs = samples / samples.sum()

    draws = monte_carlo(samples, probabilities=probs)

    assert len(draws) == 1e6
    assert set(draws) == set(samples)
    c = Counter(draws)
    for sample, prob in zip(samples, probs):
        assert c[sample] / 1e6 == pytest.approx(prob, rel=1e5)


text:
# Copyright 2019-2021 ETH Zurich and the DaCe authors. All rights reserved.
""" FPGA Tests for Auto Optimization """

import dace
import numpy as np
from dace.fpga_testing import fpga_test
from dace.transformation.interstate import FPGATransformSDFG
from dace.transformation.auto import auto_optimize as aopt
from dace.transformation.auto import fpga as fpga_auto_opt

N = dace.symbol('N')

@fpga_test()
def test_global_to_local():
    '''
    Tests global_to_local optimization
    :return:
    '''
    @dace.program
    def global_to_local(alpha: dace.float32, B: dace.float32[N]):
        tmp = alpha / 2
        return tmp * B

    size = 8

    alpha = 0.5
    B = np.random.rand(size).astype(np.float32)

    sdfg = global_to_local.to_sdfg()

    aopt.auto_optimize(sdfg, dace.DeviceType.FPGA)

    # Check that transformation has been actually applied
    # There should be only one transient among the sdfg arrays and it must have Local Storage Type
    candidate = None
    for name, array in sdfg.arrays.items():
        if array.transient:
            assert array.storage == dace.dtypes.StorageType.FPGA_Local
            candidate = name
            break

    assert candidate is not None

    # Check that all access nodes that refer to this container have also been updated
    for node, graph in sdfg.all_nodes_recursive():
        if isinstance(node, dace.sdfg.nodes.AccessNode):
            trace = dace.sdfg.utils.trace_nested_access(node, graph, graph.parent)

            for (_, acc_node), memlet_trace, state_trace, sdfg_trace in trace:
                if acc_node is not None and acc_node.data == candidate:
                    nodedesc = node.desc(graph)
                    assert nodedesc.storage == dace.dtypes.StorageType.FPGA_Local

    C = sdfg(alpha=alpha, B=B, N=size)
    ref = alpha / 2 * B
    assert np.allclose(ref, C)

    return sdfg

@fpga_test()
def test_rr_interleave():
    '''
        Tests RR interleaving of containers to memory banks
    '''
    @dace.program
    def rr_interleave(A: dace.float32[8], B: dace.float32[8], C: dace.float32[8]):
        return A + B + C

    A = np.random.rand(8).astype(np.float32)
    B = np.random.rand(8).astype(np.float32)
    C = np.random.rand(8).astype(np.float32)

    sdfg = rr_interleave.to_sdfg()
    sdfg.apply_transformations([FPGATransformSDFG])

    #specifically run the the interleave transformation
    allocated = fpga_auto_opt.fpga_rr_interleave_containers_to_banks(sdfg)

    # There will be 5 arrays (one is a temporary containing A + B)
    assert allocated == [2, 1, 1, 1]

    R = sdfg(A=A, B=B, C=C)
    assert np.allclose(A + B + C, R)

    return sdfg

if __name__ == "__main__":
    test_global_to_local(8)
    test_rr_interleave()


text:
import numpy as np
from nolearn.lasagne import BatchIterator

# This is the total number of EEG channels. The raw data is N x N_CHANNELS
N_CHANNELS = 32
# This is the number of events we need to predict. The training labels are N x N_EVENTS
N_EVENTS = 6

class GraspBatchIterator(BatchIterator):
    """supply blocks of time points and labels for training based on their INDEX"""

    def __init__(self, source, channels, sample_size, *args, **kwargs):
        super(GraspBatchIterator, self).__init__(*args, **kwargs)
        if channels is None:
            channels = np.arange(N_CHANNELS)
        self.channels = channels
        self.n_channels = len(channels)
        self.sample_size = sample_size
        self.set_source(source)
        self.Xbuf = np.zeros([self.batch_size, self.n_channels, sample_size], np.float32)
        self.Ybuf = np.zeros([self.batch_size, N_EVENTS], np.float32)

    def set_source(self, source):
        self.source = source
        if source is None:
            self.augmented = None
        else:
            self.augmented = self.augment(source)

    def augment(self, source):
        # This function pads the data at the beginning be `sample_size-1` points.
        # That way when we examine the point located at zero, we still have access
        # to SAMPLE_SIZE points at or before time zero.
        offset = self.sample_size-1
        augmented = np.zeros([self.n_channels, len(source)+offset], dtype=np.float32)
        augmented[:,offset:] = source.data.transpose()[self.channels]
        # This line is actually the result of a cut and paste error. This is what we
        # used for the contest however, so I'm leaving it in place. Also, fixing it
        # results in slightly better scores, weirdly enough! The commented out line
        # below is what was intended.
        augmented[:,:offset] = augmented[:,offset][:,None][::-1]
#         augmented[:,:offset] = augmented[:,offset][:,None]
        return augmented

    def transform(self, X_indices, y_indices):
        X_indices, y_indices = super(GraspBatchIterator, self).transform(X_indices, y_indices)
        [count] = X_indices.shape
        # Use preallocated space
        X = self.Xbuf[:count]
        Y = self.Ybuf[:count]
        for i, ndx in enumerate(X_indices):
            if ndx == -1:
                # If an index of -1 is passed in we pick a random index and train on that.
                ndx = np.random.randint(len(self.source.events))
            # Since we've padded the data by sample_size-1 points, the following starts
            # at nxd-(sample_size-1) and ends at ndx on the original data.
            augmented = self.augmented[:,ndx:ndx+self.sample_size]
            X[i] = augmented
            if y_indices is not None:
                Y[i] = self.source.events[ndx]
        Y = None if (y_indices is None) else Y
        return X, Y

max_stars_repo_name: huangshunliang/EEG-Grasp

max_issues_repo_name: huangshunliang/EEG-Grasp

max_forks_repo_name: huangshunliang/EEG-Grasp


text:
import sympy as sym

__all__ = ["propagate_uncertainty"]

def propagate_uncertainty(expr, values=None, uncertainties=None, d=sym.symbols('d', cls=sym.Function)):
    """
    propagate_uncertainty takes a sympy expression, and optionally dicts mapping variables to their values and uncertainties, and returns the propagated uncertainty

    >>> x, y = sym.symbols('x y')
    >>> propagate_uncertainty(x * y)
    sqrt(x**2*d(y)**2 + y**2*d(x)**2)
    >>> propagate_uncertainty(x * y, {x:2, y:3})
    sqrt(9*d(x)**2 + 4*d(y)**2)
    >>> propagate_uncertainty(x * y, {x:20, y:30}, {x:2, y:3})
    60*sqrt(2)
    """
    if values is None: values = {}
    if uncertainties is None: uncertainties = {}
    variables = list(expr.free_symbols)
    dv = (lambda v: sym.symbols('d(%s)' % v.name))
    # check that we aren't reusing variable names:
    for v in variables:
        if dv(v) in variables:
            raise ValueError('propagate_uncertainty cannot handle having both a variable named %s and a variable named %s' % (repr(str(v)), repr(str(dv(v)))))
    # calculate symbolic error, as per https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Simplification
    sym_error = sym.sqrt(sum(sym.diff(expr, v)**2 * dv(v)**2 for v in variables))
    # substitute in the uncertainties and the values
    # order of substitution does not matter because we use variables rather than
    #  functions for the differentials, and because we fail if any of the differential
    #  variable names are already used as variable names
    num_error = sym_error.subs(dict((dv(k), v) for k, v in uncertainties.items())).subs(values)
    # replace the remaining uncertainties with derivative functions, rather than custom-named variables
    # this allows the user to specify the form the differentials take
    #  without resulting in us substituting in arguments to the differential function
    #  (that is, we don't want to return 9*d(2)**2 + 4*d(3)**2 for propagate_uncertainty(x * y, {x:2, y:3})
    final_error = num_error.subs(dict((dv(k), d(k)) for k in variables))
    return final_error

if __name__ == "__main__":
    import doctest
    doctest.testmod()

max_stars_repo_name: JasonGross/uncertainty-propagation

max_issues_repo_name: JasonGross/uncertainty-propagation

max_forks_repo_name: JasonGross/uncertainty-propagation

reason: import sympy


text:
#!/usr/bin/env python3
# Create by ChunYing.Jia

import numpy as np
import matplotlib.pyplot as plt
import cv2

def resize_image(image, dist_size):
    """
    缩放图像
    :param image:
    :param dist_size:
    :return:
    """
    # 缩放前按照最小边裁剪裁剪
    h, w, m = image.shape
    mini = min(h, w)
    margin_h = (h - mini) / 2
    margin_w = (w - mini) / 2
    c_image = np.zeros((mini, mini, m))
    c_image[:, :, :] = image[margin_h:h-margin_h, margin_w:w-margin_w, :]
    # 如果想要收缩图像，那么使用重采样差值法效果最好；
    # 如果想要放大图像，那么最好使用3次差值法或者线性差值法（文档推荐的）
    # 这里是缩放所以采用 INTER_AREA
    # interpolation 简介：
    # interpolation - 插值方法。共有5种：
    # １）INTER_NEAREST - 最近邻插值法
    # ２）INTER_LINEAR - 双线性插值法（默认）
    # ３）INTER_AREA - 基于局部像素的重采样（resampling using pixel area relation）。
    # 对于图像抽取（image decimation）来说，这可能是一个更好的方法。
    # 但如果是放大图像时，它和最近邻法的效果类似。
    # ４）INTER_CUBIC - 基于4x4像素邻域的3次插值法
    # ５）INTER_LANCZOS4 - 基于8x8像素邻域的Lanczos插值
    r_image = cv2.resize(c_image, dist_size, interpolation=cv2.INTER_AREA)
    return r_image

def generate_random_list(image_shape):
    """
    生成随机序列，用来打散mark，同时也加密了mark
    :param image_shape:
    :return:
    """
    h_list = list(range(image_shape[0] // 2))
    np.random.shuffle(h_list)
    w_list = list(range(image_shape[1]))
    np.random.shuffle(w_list)
    return h_list, w_list

def shuffle_mark_image(mark_image, original_image, random_tuple):
    """
    通过随机数列打乱 make image
    :param mark_image:
    :param original_image:
    :param random_tuple:
    :return:shuffle_mark
    """

    origin_shape = original_image.shape
    mark_shape = (origin_shape[0] // 2, origin_shape[1], origin_shape[2])

    # 创建大小合适的mark_image
    fitted_mark_image = np.zeros(mark_shape, dtype="float32")
    mini_h = min(mark_shape[0], mark_image.shape[0])
    mini_w = min(mark_shape[1], mark_image.shape[1])
    fitted_mark_image[:mini_h, :mini_w, :] = mark_image[:mini_h, :mini_w, :]

    # 上半部分mark image 的 shuffle
    half_shuffle_mark = np.zeros(mark_shape, dtype="float32")
    mark_height_list, mark_width_list = random_tuple

    # encode mark
    for i in range(len(mark_height_list)):
        mi = mark_height_list[i]
        for j in range(len(mark_width_list)):
            mj = mark_width_list[j]
            half_shuffle_mark[i, j, :] = fitted_mark_image[mi, mj, :]

    # 上半部分mark image 的 shuffle
    shuffle_mark = np.zeros(origin_shape, dtype="float32")
    shuffle_mark[:mark_shape[0], :mark_shape[1], :] = half_shuffle_mark
    # symmetric 设置对称部分
    for i in range(len(mark_height_list)):
        for j in range(len(mark_width_list)):
            shuffle_mark[origin_shape[0] - i - 1, origin_shape[1] - j - 1, :] = half_shuffle_mark[i, j, :]
    return shuffle_mark

def add_watermarking(i_image_name, m_image_name, o_image_name, output_size, alpha=1.0, shuffle_list=None):
    """
    给图片添加水印
    :param i_image_name: original 输入 image name
    :param m_image_name: mark image
    :param o_image_name: marked 输出 image name
    :param output_size: 输出图片的大小
    :param alpha: 水印的清晰度, 默认高清
    :param shuffle_list: 用来打散mark的随机数列，默认为空，会随机生成，并返回
    :return: marked image
    """
    img_arr = cv2.imread(i_image_name)
    img_arr = resize_image(img_arr, output_size)
    mark_arr = cv2.imread(m_image_name)
    if shuffle_list is None:
        shuffle_list = generate_random_list(img_arr.shape)
    shuffled_mark = shuffle_mark_image(mark_arr, img_arr, shuffle_list)
    # 加 watermark
    # f_img_arr 是 img 的频域
    f_img_arr = np.fft.fft2(img_arr)
    fs_img_arr = np.fft.fftshift(f_img_arr)
    # f_out_img_arr 输出img的频域 ： f_img_arr + mark_encoded
    f_out_img_arr = fs_img_arr + alpha * shuffled_mark
    # fft 逆变换，输出空域的img arr
    fi_img_arr = np.fft.ifftshift(f_out_img_arr)
    out_img_arr = np.fft.ifft2(fi_img_arr)
    out_img_arr = np.abs(out_img_arr)

    # write to file
    cv2.imwrite(o_image_name, out_img_arr)

if __name__ == "__main__":
    original_image_name = './imgs/gl1.jpg'
    output_image_name = './imgs/encode_mark_img_4.jpg'
    watermark_image = './imgs/pnrmark.png'
    alpha = 1.0
    output_size = (176, 176)
    add_watermarking(original_image_name, watermark_image, output_image_name, output_size=output_size)

max_stars_repo_name: itaa/soja-box

max_issues_repo_name: Ghosque/soja-box

max_forks_repo_name: Ghosque/soja-box


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 29 15:19:36 2020

@author: Francesco Di Lauro
@mail: F.Di-Lauro@sussex.ac.uk
Copyright 2020 Francesco Di Lauro. All Rights Reserved.
See LICENSE file for details
"""
import numpy as np
from numschemesfp import FVM3
#from AnalyticalSolution import TPDFBMReflected, TPDFBMAbsorbed
from matplotlib import pyplot as plt

# Test with Dirichlet boundaries
#PDE = FokkerPlanck1D(N=1000,Nx=10000,Nt=200,leftBC="Dir", rightBC="Rob")

u = np.loadtxt('txtfiles/ER_0.txt')
distribution = np.load('txtfiles/1000_E-R_tau=1.000_gamma=5.000_k=8_distribution.npy')
SI = np.load('txtfiles/1000_E-R_tau=1.000_gamma=5.000_k=8_SI.npy')

xi =5e-3
N=1000
for i in range(len(distribution[0])):
    distribution[0][i]=5e-3
#tau=0.752
ERgamma = [5, 4.5, 7]
ERtau =[1, 1, 4]
ERk = [8.0,10.0,7.0]
taufv = [4,3,0.8]

gamma=ERgamma[0]

tauf = taufv[0]

x = np.linspace(0,1,1001)
t = np.linspace(0,tauf,1000, endpoint=True)

def CAPmodel(x):
    return C*(((x)**p)*((1-x)**p)*(a*(x/2.0) +1))

def diffusion(x):
    N=1000
    return (1.0/N)*(CAPmodel(x) + gamma*x)

def drift(x):

    drift = CAPmodel(x) - gamma*x
    return drift

def splinediff(x, SI, gamma):
    f2 = interp1d(x,SI/N, kind='cubic')
    return (1/N)*(f2(x)+gamma*x)
def splinedrift(x, SI, gamma):
    f2 = interp1d(x,SI/N, kind='cubic')
    return f2(x) -gamma*x

C,a,p = u[2]

xi =5e-3
N=1000

difflux = diffusion(x)
driftflux = drift(x)
resvec = FVM3(x,t,difflux,driftflux,xi)
fig,ax = plt.subplots(2,5, figsize=(6,4))
ax = ax.ravel()
fig.subplots_adjust(wspace=0.4,hspace=0.34)

splindiff = splinediff(x,SI,gamma)
splindrif= splinedrift(x,SI,gamma)
resvecfvm3spline = FVM3(x,t,splindiff,splindrif,xi)

for i,sub in enumerate(ax):
    #print(time[i])
    #weights = np.ones_like(distribution[i])/float(len(distribution[i]))
    if i!=0:
       # binsize = int((np.max(distribution[i]) - np.min(distribution[i]))*N)-1
        binsize2 = int((np.max(distribution[i]) - np.min(distribution[i]))*N)-1

    else:
        binsize2 = 99

    #weights = np.ones_like(distribution[i])/float(len(distribution[i]))
    weights2 = np.ones_like(distribution[i])/float(len(distribution[i]))

   # sub.hist(distribution[i], bins=binsize,  weights=weights,color='b', alpha=0.7, label = 'Sde')
    sub.hist(distribution[i], density=True,color='blue', alpha=0.7, label = 'Gillespie')

    #sub.plot(PDE.x,res/1000.0, color='k', label='Chang-Cooper')
    sub.plot(x,resvec[i*100], color='k', label='FVM3 Cap')
    sub.plot(x,resvecfvm3spline[i*100], linestyle = '--', color='r', label='FVM3 Cap')

    #sub.hist(distributionsde[i], bins=binsizesde, density=True,stacked=True, color='orange')
    sub.set_xticks([0,0.5,1], ['$0$', '$0.5$', '$1$'])
    sub.set_xlim(0,1)
    if i !=0:
        sub.set_ylim(0, 1.2*np.max(resvec[i*100][1:]))
    else:
        sub.set_ylim(0, 1.2*np.max(resvec[i*100]))
    sub.set_title('$t=%.2f$'%t[i*100], size=10)

for i in range(len(ax)):
    for tick in ax[i].xaxis.get_major_ticks():
        tick.label.set_fontsize(9)
for i in range(len(ax)):
    for tick in ax[i].yaxis.get_major_ticks():
        tick.label.set_fontsize(9)
ax[1].set_ylim(0,40)
ax[2].set_ylim(0,15)
ax[3].set_ylim(0,7)
ax[4].set_ylim(0,7)
ax[5].set_ylim(0,7)
ax[6].set_ylim(0,18)
ax[7].set_ylim(0,18)
ax[8].set_ylim(0,18)
ax[9].set_ylim(0,18)

plt.savefig("Images/Pde_ER_0.eps", format='eps')

import numpy as np
from numschemesfp import FVM3
#from AnalyticalSolution import TPDFBMReflected, TPDFBMAbsorbed
from matplotlib import pyplot as plt

# Test with Dirichlet boundaries
#PDE = FokkerPlanck1D(N=1000,Nx=10000,Nt=200,leftBC="Dir", rightBC="Rob")

def model_cap(u, k, N):
  return u[0]*(k**u[2]) * ((1-k)**u[2]) * (u[1]*(k-1/2.0)+1)
def diffusion(u,x,gamma):
    N=1000
    return (1.0/N)*(model_cap(u,x,len(x)-1) + gamma*x)

def drift(u,x,gamma):
    drift = model_cap(u,x,len(x)-1) - gamma*x
    return drift
from scipy.interpolate import interp1d

'''
Regular
'''

uv = np.loadtxt('txtfiles/Reg_0.txt')
distribution = np.load('txtfiles/1000_Reg_tau=2.500_gamma=8.000_k=7_distribution.npy')
counts = np.load('txtfiles/1000_Reg_tau=2.500_gamma=8.000_k=7_SI.npy')

xi =5e-3
N=1000
for i in range(len(distribution[0])):
    distribution[0][i]=5e-3
#tau=0.752

Reggamma = [7,6,8]
Regtau =[3.5,1,2.5]
Regk = [8,9,7]
taufv = [1.5,6,2]

gamma=Reggamma[2]

tauf = taufv[2]
tau = Regtau[2]

x = np.linspace(0,1,1001)
t = np.linspace(0,tauf,1000, endpoint=True)

SI = counts*Regtau[2]
'''
plt.plot(x,model_cap(u[2],x, SI.size-1), color='r',linestyle='--',alpha=0.7)
plt.plot(x,SI/N, color='b')
plt.plot(x,gamma*x)
'''

splindiff = splinediff(x,SI,gamma)
splindrif= splinedrift(x,SI,gamma)
resvecfvm3spline = FVM3(x,t,splindiff,splindrif,xi)

u = uv[2]
difflux = diffusion(u,x,gamma)
driftflux = drift(u,x,gamma)
resvec = FVM3(x,t,difflux,driftflux,xi)

fig,ax = plt.subplots(2,5, figsize=(6,4))
ax = ax.ravel()
fig.subplots_adjust(wspace=0.4,hspace=0.34)

for i,sub in enumerate(ax):
    #print(time[i])
    #weights = np.ones_like(distribution[i])/float(len(distribution[i]))
    sub.plot(x,resvec[i*100], color='k', label='FVM3 Cap')
    sub.plot(x,resvecfvm3spline[i*100], linestyle = '--',  color='red', label='FVM3 spline')

    if i!=0:
       # binsize = int((np.max(distribution[i]) - np.min(distribution[i]))*N)-1
        binsize = np.arange(np.min(distribution[i]), np.max(distribution[i])+1/N, 1/N )

    else:
        binsize = 99

    #weights = np.ones_like(distribution[i])/float(len(distribution[i]))
    weights = np.ones_like(distribution[i])/float(len(distribution[i]))

   # sub.hist(distribution[i], bins=binsize,  weights=weights,color='b', alpha=0.7, label = 'Sde')
    sub.hist(distribution[i],density=True,  color='blue', alpha=0.7, label = 'Gillespie')

    #sub.plot(PDE.x,res/1000.0, color='k', label='Chang-Cooper')

    #sub.hist(distributionsde[i], bins=binsizesde, density=True,stacked=True, color='orange')
    sub.set_xticks([0,0.5,1], ['$0$', '$0.5$', '$1$'])
    sub.set_xlim(0,1)

    sub.set_title('$t=%.2f$'%t[i*100], size=10)
for i in range(len(ax)):
    for tick in ax[i].xaxis.get_major_ticks():
        tick.label.set_fontsize(9)
for i in range(len(ax)):
    for tick in ax[i].yaxis.get_major_ticks():
        tick.label.set_fontsize(9)

ax[1].set_ylim(0,40)
ax[2].set_ylim(0,15)
ax[3].set_ylim(0,7)
ax[4].set_ylim(0,7)
ax[5].set_ylim(0,7)
ax[6].set_ylim(0,18)
ax[7].set_ylim(0,18)
ax[8].set_ylim(0,18)
ax[9].set_ylim(0,18)

plt.savefig("Images/Pde_Reg_2.eps", format='eps')

reason: import numpy,from scipy


text:
from striatum.core import Env
from gym.spaces import Discrete
from scipy.stats import norm

class MultiArmedBandit(Env):
    observation_space = None

    def __init__(self, n_arms, average_rewards=None, reward_noise_rv=norm()):
        self.action_space = Discrete(n_arms)
        self.n_arms = n_arms
        self.average_rewards = average_rewards or norm.rvs(size=n_arms)
        self.reward_noise_rv = reward_noise_rv

    def step(self, action):
        observation = None
        reward = self.average_rewards[action] + self.reward_noise_rv.rvs()
        done = False
        info = dict()
        return observation, reward, done, info

    def reset(self):
        pass

max_stars_repo_name: dsevero/striatum

max_issues_repo_name: dsevero/striatum

max_forks_repo_name: dsevero/striatum

reason: from scipy


text:
# Scores, metrics, diagnosis tools for model evaluations
# Author : Íngrid Munné-Collado
# Date: 04/06/2020

# libraries import
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, mean_absolute_error
from math import sqrt
import scores_metrics_diagnosis_tools as scores

# y_true= np.array([10,12,14,8,9,5,8,10,12,11,10,15])
# y_pred = np.array([12,14,15,10,7,4,5,8,12,14,13,8])

def forecast_error(ytrue, ypred):
    return ytrue - ypred

def MAPE(y_true, y_pred):
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

def RMSE(y_true, y_pred):
    return sqrt(mean_squared_error(y_true, y_pred))

def bias(y_true, y_pred):
    return np.mean(y_true - y_pred)

# error = forecast_error(y_true, y_pred)
# MAPE= MAPE(y_true, y_pred)
# RMSE= RMSE(y_true, y_pred)
# bias= bias(y_true, y_pred)
# mae = mean_absolute_error(y_true, y_pred)


text:
from typing import List

import numpy as np
import numpy.typing as npt
from nuplan.planning.metrics.abstract_metric import AbstractMetricBuilder
from nuplan.planning.metrics.metric_result import MetricStatistics, MetricStatisticsType, Statistic, TimeSeries
from nuplan.planning.metrics.utils import state_extractors
from nuplan.planning.simulation.history.simulation_history import SimulationHistory

class TimeToCollisionStatistics(AbstractMetricBuilder):

    def __init__(self, name: str, category: str, collision_circle_radius: float, time_step_size: float,
                 time_horizon: float) -> None:
        """
        Ego time to collision metric, reports the minimal time for a projected collision if agents proceed with
        zero acceleration.

        :param name: Metric name.
        :param category: Metric category.
        :param collision_circle_radius: Radius of the circle used for collision checking [m]
        :param time_step_size: Step size for the propagation of collision circles
        :param time_horizon: Time horizon for collision checking
        """

        self._name = name
        self._category = category
        self._collision_circle_radius = collision_circle_radius
        self._time_step_size = time_step_size
        self._time_horizon = time_horizon

    @property
    def name(self) -> str:
        """
        Returns the metric name.
        :return: the metric name.
        """

        return self._name

    @property
    def category(self) -> str:
        """
        Returns the metric category.
        :return: the metric category.
        """

        return self._category

    def compute_time_to_collision(self, history: SimulationHistory) -> npt.NDArray[np.float32]:
        """
        Computes an estimate of the minimal time to collision with other agents. Agents are simplified with circles
        which are propagated in a straight line with constant velocity until there is a collision with ego or the
        maximal time window is reached.

        :param history The scenario history
        :return: The minimal TTC for each sample, inf if no collision is found
        """
        ego_states = [sample.ego_state for sample in history.data]
        ego_velocities = state_extractors.extract_ego_velocity(history)

        all_tracks_poses = state_extractors.extract_tracks_poses(history)
        all_tracks_speed = state_extractors.extract_tracks_speed(history)

        # Default to be inf
        time_to_collision: npt.NDArray[np.float32] = np.asarray([np.inf] * len(history))
        for i, (ego_state, ego_speed, tracks_poses, tracks_speed) in enumerate(zip(ego_states,
                                                                                ego_velocities,
                                                                                all_tracks_poses,
                                                                                all_tracks_speed)):
            # Remain inf if we don't have any agents
            if len(tracks_poses) == 0:
                continue

            ego_dx = np.cos(ego_state.rear_axle.heading) * ego_speed * self._time_step_size
            ego_dy = np.sin(ego_state.rear_axle.heading) * ego_speed * self._time_step_size

            tracks_dxy = np.array([np.cos(tracks_poses[:, 2]) * tracks_speed * self._time_step_size,
                                   np.sin(tracks_poses[:, 2]) * tracks_speed * self._time_step_size]).T
            t = 0.0
            ego_pose = np.array([ego_state.rear_axle.x, ego_state.rear_axle.y])
            while t < self._time_horizon:
                tracks_poses[:, :2] += tracks_dxy
                ego_pose += (ego_dx, ego_dy)
                dist = np.linalg.norm(tracks_poses[:, :2] - ego_pose, axis=1)

                if np.min(dist) < 2 * self._collision_circle_radius:
                    break

                t += self._time_step_size
            time_to_collision[i] = t if t < self._time_horizon else np.inf
        return time_to_collision

    def compute(self, history: SimulationHistory) -> List[MetricStatistics]:
        """
        Returns the estimated metric.
        :param history: History from a simulation engine.
        :return: the estimated metric.
        """
        time_to_collision = self.compute_time_to_collision(history=history)
        statistics = {MetricStatisticsType.MIN: Statistic(name="min_time_to_collision", unit="seconds",
                                                          value=np.amin(time_to_collision)),
                      MetricStatisticsType.P90: Statistic(name="ego_p90_time_to_collision", unit="seconds",
                                                          value=np.percentile(time_to_collision, 90,
                                                                              interpolation='nearest')),
                      }

        time_stamps = state_extractors.extract_ego_time_point(history)
        time_series = TimeSeries(unit='seconds',
                                 time_stamps=list(time_stamps),
                                 values=list(time_to_collision))
        result = MetricStatistics(metric_computator=self.name,
                                  name="time_to_collision_statistics",
                                  statistics=statistics,
                                  time_series=time_series,
                                  metric_category=self.category)

        return [result]

max_stars_repo_name: MCZhi/nuplan-devkit

max_issues_repo_name: MCZhi/nuplan-devkit

max_forks_repo_name: MCZhi/nuplan-devkit


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@file          detection_pipeline.py
@brief         apply pitch detection with one pipeline
@author        paradigm
"""

import logging
import numpy as np

from .project_parser import ProjectParser
from .audio_preprocessor import AudioPreprocessor
from .pitch_classifier import PitchClassifier
from .stochastic_postprocessor import StochasticPostprocessor

class DetectionPipeline:
    """ pitch detection pipeline with file handling """

    def __init__(
        self,
        project_parser: ProjectParser,
        audio_preprocessor: AudioPreprocessor,
        pitch_classifier: PitchClassifier,
        stochastic_postprocessor: StochasticPostprocessor
    ):
        self.project_parser = project_parser
        self.audio_preprocessor = audio_preprocessor
        self.pitch_classifier = pitch_classifier
        self.stochastic_postprocessor = stochastic_postprocessor

    def transform(
        self, notes_org: str, notes_new: str, postproc: bool = True
    ) -> (np.ndarray, np.ndarray):
        """ load note file, extract audio, detect pitches, and write to new notes.txt """
        self.project_parser.load_note_file(notes_org)
        pitches_org = self.project_parser.dump_pitches()
        pitches_new = []
        # number of blocks per segment
        len_arr = []
        # all singable block features of the song
        X = []
        # divide audio into pitch segments
        for audio_segment in self.project_parser.process_audio():
            # get block ffts for the segment
            features = self.audio_preprocessor.transform(audio_segment)
            # save number of blocks
            len_arr.append(len(features))
            # append segment features to song features
            X.append(features)
        # predict all block features at once (increases performance a lot!)
        pitches_prob = self.pitch_classifier.predict(np.vstack(X))
        # calculate the segment indexes from length array
        idx_0 = 0
        for lenght in len_arr:
            idx_1 = idx_0 + lenght
            # sum up the pitch probabilities of the segment
            segment_prob = np.sum(pitches_prob[idx_0:idx_1], axis=0)
            # determine the pitch with the highest probability of the segment
            pitches_new.append(segment_prob.argmax())
            idx_0 = idx_1
        if postproc:
            # guess song key from pitch distribution
            key = self.stochastic_postprocessor.detect_key(pitches_new)
            logging.info("Song was written in key: %d", key)
            pitches_new[:] = self.stochastic_postprocessor.correct_pitches(key, pitches_new)
        self.project_parser.update_pitches(pitches_new)
        self.project_parser.save_note_file(notes_new)
        return np.array(pitches_org), np.array(pitches_new)


text:
import numpy as np
import scipy.stats as st
from niscv.basic.expectation import Expectation

def experiment(dim, size_est, sn, show, size_kn, ratio):
    results = []
    mean = np.zeros(dim)
    target = lambda x: st.multivariate_normal(mean=mean).pdf(x)
    fun = lambda x: x[:, 0] ** 2
    init_proposal = st.multivariate_normal(mean=mean, cov=4)
    grid_x = np.linspace(-5, 5, 200)
    exp = Expectation(dim, target, fun, init_proposal, size_est, sn=sn, show=show)
    exp.initial_estimation(size_kn, ratio, resample=True)
    results.extend([exp.result[-3], exp.result[-2], exp.result[-4]])
    if exp.show:
        exp.draw(grid_x, name='initial')

    exp.density_estimation(bw=1.0, factor='scott', local=False, gamma=0.3, df=0, alpha0=0.1)
    exp.nonparametric_estimation()
    results.extend([exp.result[-3], exp.result[-1]])
    if exp.show:
        exp.draw(grid_x, name='nonparametric')

    exp.regression_estimation()
    results.extend([exp.result[-1]])
    if exp.show:
        exp.draw(grid_x, name='regression')

    exp.likelihood_estimation(optimize=True, NR=True)
    return results

def main(sn, ratios):
    # np.random.seed(3033079628)
    results = []
    for ratio in ratios:
        print(ratio)
        result = experiment(dim=4, size_est=25000, sn=sn, show=False, size_kn=500, ratio=ratio)
        results.append(result)

    return np.array(results)

if __name__ == '__main__':
    X = np.array([1, 2, 3, 4, 5, 7, 10, 15])
    R = main(sn=True, ratios=X)
    n0 = 500 * X
    ESS = R[:, 0]
    RSS = R[:, 1]
    print(np.round(n0 / ESS, 0))
    print(np.round(n0 / RSS, 0))
    print(np.round(R[:, 2:].T, 6))

max_stars_repo_path: niscv_v1/drop/resampling ratio.py

max_stars_repo_name: IanFla/Importance-Sampling

max_issues_repo_path: niscv_v1/drop/resampling ratio.py

max_issues_repo_name: IanFla/Importance-Sampling

max_forks_repo_path: niscv_v1/drop/resampling ratio.py

max_forks_repo_name: IanFla/Importance-Sampling

reason: import numpy,import scipy


text:
import numpy as np
from math import pi
import time

from compyle.config import get_config
from compyle.api import declare, annotate
from compyle.parallel import Elementwise, Reduction
from compyle.array import get_backend, wrap
import compyle.array as carr

from nnps import NNPSCountingSort, NNPSRadixSort
from md_simple import integrate_step1, integrate_step2, \
        boundary_condition, MDSolverBase

@annotate
def calculate_force(i, x, y, z, fx, fy, fz, pe, nbr_starts, nbr_lengths, nbrs):
    start_idx = nbr_starts[i]
    length = nbr_lengths[i]
    for k in range(start_idx, start_idx + length):
        j = nbrs[k]
        if i == j:
            continue
        xij = x[i] - x[j]
        yij = y[i] - y[j]
        zij = z[i] - z[j]
        rij2 = xij * xij + yij * yij + zij * zij
        irij2 = 1.0 / rij2
        irij6 = irij2 * irij2 * irij2
        irij12 = irij6 * irij6
        pe[i] += (2 * (irij12 - irij6))
        f_base = 24 * irij2 * (2 * irij12 - irij6)

        fx[i] += f_base * xij
        fy[i] += f_base * yij
        fz[i] += f_base * zij

@annotate
def step_method1(i, x, y, z, vx, vy, vz, fx, fy, fz, pe, xmin, xmax,
                 ymin, ymax, zmin, zmax, m, dt, nbr_starts, nbr_lengths,
                 nbrs):
    integrate_step1(i, m, dt, x, y, z, vx, vy, vz, fx, fy, fz)
    boundary_condition(i, x, y, z, vx, vy, vz, fx, fy, fz, pe, xmin, xmax,
                       ymin, ymax, zmin, zmax)

@annotate
def step_method2(i, x, y, z, vx, vy, vz, fx, fy, fz, pe, xmin, xmax,
                 ymin, ymax, zmin, zmax, m, dt, nbr_starts, nbr_lengths,
                 nbrs):
    calculate_force(i, x, y, z, fx, fy, fz, pe, nbr_starts, nbr_lengths, nbrs)
    integrate_step2(i, m, dt, x, y, z, vx, vy, vz, fx, fy, fz)

class MDNNPSSolver(MDSolverBase):
    def __init__(self, num_particles, x=None, y=None, z=None,
                 vx=None, vy=None, vz=None,
                 xmax=100., ymax=100., zmax=100., dx=2., init_T=0.,
                 backend=None, use_count_sort=False):
        super().__init__(num_particles, x=x, y=y, z=z, vx=vx, vy=vy, vz=vz,
                         xmax=xmax, ymax=ymax, zmax=zmax, dx=dx, init_T=init_T,
                         backend=backend)
        self.nnps_algorithm = NNPSCountingSort \
            if use_count_sort else NNPSRadixSort
        self.nnps = self.nnps_algorithm(self.x, self.y, self.z, 3., 0.01,
                                        self.xmax, self.ymax, self.zmax,
                                        backend=self.backend)
        self.init_forces = Elementwise(calculate_force, backend=self.backend)
        self.step1 = Elementwise(step_method1, backend=self.backend)
        self.step2 = Elementwise(step_method2, backend=self.backend)

    def solve(self, t, dt, log_output=False):
        num_steps = int(t // dt)
        self.nnps.build()
        self.nnps.get_neighbors()
        self.init_forces(self.x, self.y, self.z, self.fx, self.fy, self.fz,
                         self.pe, self.nnps.nbr_starts,
                         self.nnps.nbr_lengths, self.nnps.nbrs)
        for i in range(num_steps):
            self.step1(self.x, self.y, self.z, self.vx, self.vy, self.vz,
                       self.fx, self.fy, self.fz,
                       self.pe, self.xmin, self.xmax, self.ymin, self.ymax,
                       self.zmin, self.zmax, self.m, dt, self.nnps.nbr_starts,
                       self.nnps.nbr_lengths, self.nnps.nbrs)
            self.nnps.build()
            self.nnps.get_neighbors()
            self.step2(self.x, self.y, self.z, self.vx, self.vy, self.vz,
                       self.fx, self.fy, self.fz,
                       self.pe, self.xmin, self.xmax, self.ymin, self.ymax,
                       self.zmin, self.zmax, self.m, dt, self.nnps.nbr_starts,
                       self.nnps.nbr_lengths, self.nnps.nbrs)

            if i % 100 == 0:
                self.post_step(i, log_output=log_output)

if __name__ == '__main__':
    from compyle.utils import ArgumentParser
    p = ArgumentParser()
    p.add_argument(
        '--use-count-sort', action='store_true', dest='use_count_sort',
        default=False, help='Use count sort instead of radix sort'
    )
    p.add_argument(
        '--show', action='store_true', dest='show',
        default=False, help='Show plot'
    )
    p.add_argument(
        '--log-output', action='store_true', dest='log_output',
        default=False, help='Log output'
    )

    p.add_argument('-n', action='store', type=int, dest='n',
                   default=100, help='Number of particles')

    p.add_argument('--tf', action='store', type=float, dest='t',
                   default=40., help='Final time')

    p.add_argument('--dt', action='store', type=float, dest='dt',
                   default=0.02, help='Time step')

    o = p.parse_args()

    solver = MDNNPSSolver(
        o.n,
        backend=o.backend,
        use_count_sort=o.use_count_sort)

    start = time.time()
    solver.solve(o.t, o.dt, log_output=o.log_output)
    end = time.time()
    print("Time taken for N = %i is %g secs" % (o.n, (end - start)))
    if o.log_output:
        solver.write_log('nnps_log.log')
    if o.show:
        solver.pull()
        solver.plot()

max_stars_repo_name: adityapb/compyle

max_issues_repo_name: aerorohit/compyle

max_forks_repo_name: aerorohit/compyle


text:
#import files which are needed
import numpy as np
import cv2
import os

#filename
fileName = 'captureVideoFile.avi'

#get default capture device to get images
capture = cv2.VideoCapture(0)

#define extension of the video which is being recorded
videoType = cv2.VideoWriter_fourcc(*'XVID')

#get video file as output and attach all to variable
outputVideoFle = cv2.VideoWriter(fileName, videoType , 20.0, (64, 48))

#to get continuous frame of images
while True:
	#create a frame with name My Image and show it
	ret, frame = capture.read()

	#scale down video size
	scaleDownVideo = cv2.resize(frame,(64,48),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
	#show output
	cv2.imshow('My image(Normal)', frame)
	cv2.imshow('My image(Scale Down)', scaleDownVideo)

	#store output file
	outputVideoFle.write(scaleDownVideo)

	#termination condition
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break

capture.release()
outputVideoFle.release()
cv2.destroyAllWindows()

max_stars_repo_name: siddhantjoshi/OpenCV

max_issues_repo_name: siddhantjoshi/OpenCV

max_forks_repo_name: siddhantjoshi/OpenCV


text:
import pandas as pd
import numpy as np
import re
import os, sys
import django

os.environ['DJANGO_SETTINGS_MODULE'] = 'gettingstarted.settings'
django.setup()

from authentication import models

def nursery_row_converter(row, listy):
    #convert pandas row to a dictionary
    #requires a list of columns and a row as a tuple
    count = 1
    pictionary = {}
    pictionary['Index'] = row[0]
    for item in listy:
        if item == 'Provenance':
            word = re.sub('N°','',row[count])
            pictionary[item] = re.sub('[\W\_]','',word)
        else:
            pictionary[item] = row[count]
        count += 1
    return pictionary

def convert_to_dict_list(table):
    dict_list = []
    listy = table.columns
    for i, row in enumerate(table.itertuples()):
        dict_list.append(nursery_row_converter(row, listy))

    return dict_list

def alteia_data_to_db(alteia_list):

    for i, data in enumerate(alteia_list):
        alteia_data = models.AlteiaData(
            plantation_code = data['Code'],
            cashew_tree_cover = data['Cashew_Tree'],
            )
        try:
            alteia_data.save()
        except Exception as e:
            print(e)

def clean_alteia_data():
    alteia_df = pd.read_excel("./new_data/alteia_df.xlsx", engine='openpyxl')
    alteia_list = convert_to_dict_list(alteia_df)
    alteia_data_to_db(alteia_list)

if __name__ == '__main__':

    clean_alteia_data()


text:
"""Demo for use yolo v3
"""
import os
import time
import cv2
import numpy as np
from model.yolo_model import YOLO
import HyperLPRLite as pr
from demo import drawRectBox
from MainUI import MainWindow

def process_image(img):
    """Resize, reduce and expand image.

    # Argument:
        img: original image.

    # Returns
        image: ndarray(416, 416, 3), processed image.
    """
    image = cv2.resize(img, (416, 416),
                       interpolation=cv2.INTER_CUBIC)
    image = np.array(image, dtype='float32')
    image /= 255.
    image = np.expand_dims(image, axis=0)   #(416,416,3) -> (1,416,416,3)

    return image

def get_classes(file):
    """Get classes name.

    # Argument:
        file: classes name for database.

    # Returns
        class_names: List, classes name.

    """
    with open(file) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]

    return class_names

def draw(image, boxes, scores, classes, all_classes):
    """Draw the boxes on the image.

    # Argument:
        image: original image.
        boxes: ndarray, boxes of objects.
        classes: ndarray, classes of objects.
        scores: ndarray, scores of objects.
        all_classes: all classes name.
    """
    for box, score, cl in zip(boxes, scores, classes):
        x, y, w, h = box

        top = max(0, np.floor(x + 0.5).astype(int))
        left = max(0, np.floor(y + 0.5).astype(int))
        right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
        bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))

        cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
        cv2.putText(image, '{0} {1:.2f}'.format(all_classes[cl], score),
                    (top, left - 6),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.6, (0, 0, 255), 1,
                    cv2.LINE_AA)

        #print('class: {0}, score: {1:.2f}'.format(all_classes[cl], score))
        #print('box coordinate x,y,w,h: {0}'.format(box))

    #print()
    return boxes

def detect_image(image, yolo, all_classes):
    """Use yolo v3 to detect images.

    # Argument:
        image: original image.
        yolo: YOLO, yolo model.
        all_classes: all classes name.

    # Returns:
        image: processed image.
    """

    pimage = process_image(image)

    start = time.time()
    boxes, classes, scores = yolo.predict(pimage, image.shape)
    end = time.time()

    #print('time: {0:.2f}s'.format(end - start))

    if boxes is not None:
        draw(image, boxes, scores, classes, all_classes)

    return image

def detect_video(video, yolo, all_classes):
    """Use yolo v3 to detect video.

    # Argument:
        video: video file.
        yolo: YOLO, yolo model.
        all_classes: all classes name.
    """
    video_path = os.path.join("videos", "test", video)
    if (os.path.exists(video_path) and video != ''):
        camera = cv2.VideoCapture(video_path)
    else:
        camera = cv2.VideoCapture(0)
        video = 'your_camera.mp4'
    res, frame = camera.read()
    if not res:
        print("file open failed and camera can not open")
    cv2.namedWindow("detection", 0)#cv2.WINDOW_AUTOSIZE
    # Prepare for saving the detected video
    sz = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fourcc = cv2.VideoWriter_fourcc(*'mpeg')

    vout = cv2.VideoWriter()
    vout.open(os.path.join("videos", "res", video), fourcc, 20, sz, True)

    while True:
        res, frame = camera.read()

        if not res:
            break

        image = detect_image(frame, yolo, all_classes)
        model = pr.LPR("model/cascade.xml", "model/model12.h5", "model/ocr_plate_all_gru.h5")
        for pstr, confidence, rect in model.SimpleRecognizePlateByE2E(image):
            if confidence > 0.7:
                image = drawRectBox(image, rect, pstr + "confidence" + str(round(confidence, 3)))
                print("plate_str:")
                print(pstr)
                print("plate_confidence")
                print(confidence)
        cv2.namedWindow("detection" ,0)
        cv2.resizeWindow("detection",1000,650)
        cv2.imshow("detection", image)
        cv2.moveWindow("detection",480,180)

        # Save the video frame by frame
        vout.write(image)

        if cv2.waitKey(60) & 0xff == 27:       #press ESC to quit
                break

    vout.release()
    camera.release()

max_stars_repo_name: lillin-bug/Vehicle-Detection

max_issues_repo_name: lillin-bug/Vehicle-Detection

max_forks_repo_name: lillin-bug/Vehicle-Detection


text:
import numpy as np
from txt2seq import cut_max_seq

def bootstrapping():
    event_file = '../data/ywsz/events/events.txt'

    with open(event_file, encoding='UTF-8') as fr_event:
        events = np.loadtxt(fr_event, dtype=np.str)
    total_events = events.shape[0]
    print('total events: ', total_events)
    ids = np.arange(1, total_events+1)
    test_size = 456

    # _, events_test, _, ids_test = train_test_split(events, ids, test_size=test_size, shuffle=False)
    events_test = events[: test_size]
    ids_test = ids[: test_size]
    print('{} test events, {} test ids'.format(len(events_test), len(ids_test)))
    reduce(events_test, ids_test, 'test')
    # reduce(events, ids, 'train')

def reduce(events, ids, mode='test'):
    entity_file = '../data/ywsz/events/entity_bert_crf.txt'
    with open(entity_file, encoding='UTF-8') as fr_entity:
        entity = np.loadtxt(fr_entity, dtype=np.str)
        event_ids = entity[:, 0]

    fw_output = '../data/ywsz/events/bootstraping_{}.txt'.format(mode)
    fr_entity = open(entity_file, 'r', encoding='UTF-8')
    with open(fw_output, 'w+', encoding='UTF-8') as fw:
        for i, event in zip(ids, events):
            event_id = str('{:05d}').format(i)
            event = event.strip()
            labels = ['O'] * len(event)

            idx = [index for index in range(len(entity)) if event_ids[index] == event_id]
            for id in idx:
                elements = entity[id]
                if mode == 'test' and elements[1] == 'BOOK':
                    continue
                item_type = elements[1]
                item_value = elements[2]
                start = int(elements[3])
                end = int(elements[4])
                if item_value == event[start: end]:
                    labels[start] = 'B-{}'.format(item_type)
                    for flag in range(start+1, end):
                        labels[flag] = 'I-{}'.format(item_type)
                else:
                    raise ValueError('cannot matching entity {} to event {}: {}'.format(
                        item_value, i, ''.join(event[start: end]))
                    )
            seqs = cut_max_seq()
            seqs.cut(event, labels)
            token_seq, label_seq = seqs.get_cut_seqs()
            for i, tokens in enumerate(token_seq):
                labels = label_seq[i]
                for token, label in zip(tokens, labels):
                    fw.write('{} {}\n'.format(token, label))
                fw.write('\n')

    fr_entity.close()

if __name__ == "__main__":
    bootstrapping()

max_stars_repo_name: NJUDCA/NER

max_issues_repo_name: NJUDCA/NER

max_forks_repo_name: NJUDCA/NER