text:
import math
import numpy as np

def quaternion_from_matrix(matrix):
    """Return quaternion from rotation matrix.
    >>> R = rotation_matrix(0.123, (1, 2, 3))
    >>> q = quaternion_from_matrix(R)
    >>> numpy.allclose(q, [0.0164262, 0.0328524, 0.0492786, 0.9981095])
    True
    """
    q = np.empty((4, ), dtype=np.float64)
    M = np.array(matrix, dtype=np.float64, copy=False)[:4, :4]
    t = np.trace(M)
    if t > M[3, 3]:
        q[3] = t
        q[2] = M[1, 0] - M[0, 1]
        q[1] = M[0, 2] - M[2, 0]
        q[0] = M[2, 1] - M[1, 2]
    else:
        i, j, k = 0, 1, 2
        if M[1, 1] > M[0, 0]:
            i, j, k = 1, 2, 0
        if M[2, 2] > M[i, i]:
            i, j, k = 2, 0, 1
        t = M[i, i] - (M[j, j] + M[k, k]) + M[3, 3]
        q[i] = t
        q[j] = M[i, j] + M[j, i]
        q[k] = M[k, i] + M[i, k]
        q[3] = M[k, j] - M[j, k]
    q *= 0.5 / math.sqrt(t * M[3, 3])
    return q

def pose_quaternion_from_matrix(matrix):
    """Return translation + quaternion(x,y,z,w)
    """
    if matrix.shape == (3, 4):
        matrix = np.concatenate((matrix, [[0, 0, 0, 1]]), axis=0)

    pose = matrix[:3, 3]
    quat = quaternion_from_matrix(matrix)
    return np.concatenate((pose, quat), axis=0)

class URKinematics():

    def __init__(self, robot_name):
        if robot_name == 'ur3':
            import ur3_ikfast as ur_ikfast
        elif robot_name == 'ur3e':
            import ur3e_ikfast as ur_ikfast
        elif robot_name == 'ur5':
            import ur5_ikfast as ur_ikfast
        elif robot_name == 'ur5e':
            import ur5e_ikfast as ur_ikfast
        elif robot_name == 'ur10':
            import ur10_ikfast as ur_ikfast
        elif robot_name == 'ur10e':
            import ur10e_ikfast as ur_ikfast
        else:
            raise Exception("Unsupported robot")

        self.kinematics = ur_ikfast.PyKinematics()
        self.n_joints = self.kinematics.getDOF()

    def forward(self, joint_angles, rotation_type='quaternion'):
        """
            Compute robot's forward kinematics for the specified robot
            joint_angles: list
            rotation_type: 'quaternion' or 'matrix'
            :return: if 'quaternion' then return a list of [x, y, z, w. qx, qy, qz]
                     if 'matrix' then a list of 12 values the 3x3 rotation matrix and
                     the 3 translational values
        """
        if isinstance(joint_angles, np.ndarray):
            joint_angles = joint_angles.tolist()

        ee_pose = self.kinematics.forward(joint_angles)
        ee_pose = np.asarray(ee_pose).reshape(3, 4)

        if rotation_type == 'matrix':
            return ee_pose
        elif rotation_type == 'quaternion':
            return pose_quaternion_from_matrix(ee_pose)

    def inverse(self, ee_pose, all_solutions=False, q_guess=np.zeros(6)):
        """ Compute robot's inverse kinematics for the specified robot
            ee_pose: list of 7 if quaternion [x, y, z, w, qx, qy, qz]
                     list of 12 if rotation matrix + translational values
            all_solutions: whether to return all the solutions found or just the best one
            q_guess:  if just one solution is request, this set of joint values will be use
                      to find the closest solution to this
            :return: list of joint angles
                     list of best joint angles if found
                     q_guess if no solution is found
        """
        pose = None
        if len(ee_pose) == 7:
            rot = np.roll(ee_pose[3:], 1)
            pose = np.concatenate((ee_pose[:3], rot), axis=0)
        else:
            pose = ee_pose
        joint_configs = self.kinematics.inverse(pose.reshape(-1).tolist())
        n_solutions = int(len(joint_configs)/self.n_joints)
        joint_configs = np.asarray(joint_configs).reshape(n_solutions, self.n_joints)

        if all_solutions:
            return joint_configs

        return best_ik_sol(joint_configs, q_guess)

def best_ik_sol(sols, q_guess, weights=np.ones(6)):
    """ Get best IK solution """
    valid_sols = []
    for sol in sols:
        test_sol = np.ones(6) * 9999.
        for i in range(6):
            for add_ang in [-2. * np.pi, 0, 2. * np.pi]:
                test_ang = sol[i] + add_ang
                if (abs(test_ang) <= 2. * np.pi
                        and abs(test_ang - q_guess[i]) <
                        abs(test_sol[i] - q_guess[i])):
                    test_sol[i] = test_ang
        if np.all(test_sol != 9999.):
            valid_sols.append(test_sol)
    if not valid_sols:
        return None
    best_sol_ind = np.argmin(
        np.sum((weights * (valid_sols - np.array(q_guess)))**2, 1))
    return valid_sols[best_sol_ind]


text:
import streamlit as st
import os
import time
import pandas as pd
import numpy as np
from data.auxiliary_functions import Auxiliary

# from auxiliary_functions import Auxiliary

class DataLoad:

    pass

    # @st.cache(allow_output_mutation=True)
    # def get_connection():
    #     return create_engine(DataLoad.postgres)
    #
    # def load_df(table='',filters=[],dimensions=[],measures=[], query=''):
    #     if query == '':
    #         query = Auxiliary.query_processing(table=table,
    #                                            filters=filters,
    #                                            measures=measures,
    #                                            dimensions=dimensions)
    #
    #     with st.spinner('Loading Data...'):
    #         time.sleep(0.05)
    #         # print(query)
    #         df = pd.read_sql_query(query, DataLoad.get_connection())
    #     return df
    #
    # # @st.cache(allow_output_mutation=True)
    # def dataframe_load_and_persist(loading_dict, index='', sortascending='True', process='series', feature=''):
    #     df = DataLoad.load_df(**loading_dict)
    #     if index != '':
    #         df.sort_values(by=index, ascending=sortascending, inplace=True)
    #         df.set_index(index, inplace=True)
    #     df = DataLoad.processing_df(df, process=process)
    #     return df
    #
    #
    # def processing_df(df, process, feature='', sort=''):
    #     if process == 'value':
    #         if not df.empty:
    #             value =  df[feature].iloc[0]
    #         else:
    #             value = 0
    #     elif process == 'diff':
    #         if not df.empty:
    #             try:
    #                 if sort!='':
    #                     df = df.sort_values(by=sort,ascending=False)
    #                 df = df[feature].pct_change()
    #                 df = df.sort_values()
    #                 value =  round(df.iloc[0]*100, 2)
    #             except:
    #                 value = 0
    #         else:
    #             value = 0
    #     elif process == 'series':
    #         value = df
    #     elif process == 'diffseries':
    #         try:
    #             df = df.sort_values(by=sort)
    #             df[feature] = df[feature].pct_change()
    #             value = df
    #         except:
    #             df[feature] = 0
    #             value = df
    #     return value
    #
    # def to_filter(filters, filter_key):
    #     test=[]
    #     for filter in filters:
    #         test.append(filter)
    #     if test:
    #         filter_dict = {filter_key:test}
    #     else:
    #         filter_dict= {}
    #     return filter_dict

ext: py

max_stars_repo_name: PavelFryblik/DeepLearningStreamlit

max_issues_repo_name: PavelFryblik/DeepLearningStreamlit

max_forks_repo_name: PavelFryblik/DeepLearningStreamlit

reason: import numpy


text:
import numpy as np
import tensorflow as tf
from CONSTANT import *
from utils.data_utils import ohe2cat
from .meta_model import MetaModel
from sklearn.metrics import roc_auc_score

class DnnModel(MetaModel):
    def __init__(self):
        super(DnnModel, self).__init__()
        #clear_session()
        self.max_length = None
        self.mean = None
        self.std = None

        self._model = None
        self.is_init = False

        self.name = 'dnn'
        self.type = 'nn'
        self.patience = 10

        self.not_gain_threhlod = 3

    def init_model(self,
                   num_classes,
                   **kwargs):
        self.num_classes = num_classes
        model_fn = self.model_fn
        self._model = tf.estimator.Estimator(model_fn=model_fn)
        self.is_init = True

    def epoch_train(self, dataloader, epoch):
        dataset_train = dataloader['train']
        train_input_fn = lambda: self.input_function(dataset_train, 'train')
        self._model.train(input_fn=train_input_fn, steps=100)

    def epoch_valid(self, dataloader):
        dataset_valid, label = dataloader['valid']
        valid_input_fn = lambda: self.input_function(dataset_valid, 'valid')
        valid_results = self._model.predict(input_fn=valid_input_fn)
        preds = [x['probabilities'] for x in valid_results]
        preds = np.array(preds)
        valid_auc = roc_auc_score(label, preds)
        return valid_auc

    def predict(self, dataloader, batch_size=32):
        dataset_test = dataloader['test']
        valid_input_fn = lambda: self.input_function(dataset_test, 'test')
        test_results = self._model.predict(input_fn=valid_input_fn)
        preds = [x['probabilities'] for x in test_results]
        preds = np.array(preds)
        return preds

    def model_fn(self, features, labels, mode):
        is_training = False
        keep_prob = 1
        if mode == tf.estimator.ModeKeys.TRAIN:
            is_training = True
            keep_prob = 0.8

        input_layer = features

        # Replace missing values by 0
        mask = tf.is_nan(input_layer)
        input_layer = tf.where(mask, tf.zeros_like(input_layer), input_layer)

        # Sum over time axis
        input_layer = tf.reduce_sum(input_layer, axis=1)
        mask = tf.reduce_sum(1 - tf.cast(mask, tf.float32), axis=1)

        # Flatten
        input_layer = tf.layers.flatten(input_layer)
        mask = tf.layers.flatten(mask)
        f = input_layer.get_shape().as_list()[1]  # tf.shape(input_layer)[1]

        # Build network
        x = tf.layers.batch_normalization(input_layer, training=is_training)
        x = tf.nn.dropout(x, keep_prob)
        x_skip = self.fc(x, 256, is_training)
        x = self.fc(x_skip, 256, is_training)
        x = tf.nn.dropout(x, keep_prob)
        x = self.fc(x, 256, is_training) + x_skip
        x_mid = self.fc(x, f, is_training)

        x = self.fc(x_mid, 256, is_training)
        for i in range(3):
            x = self.fc(x, 256, is_training)

        logits = tf.layers.dense(inputs=x, units=self.num_classes)
        sigmoid_tensor = tf.nn.sigmoid(logits, name="sigmoid_tensor")

        predictions = {

            "classes": sigmoid_tensor > 0.5,

            "probabilities": sigmoid_tensor
        }

        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

        loss_labels = tf.reduce_sum(sigmoid_cross_entropy_with_logits(labels=labels, logits=logits))
        loss_reconst = tf.reduce_sum(mask * tf.abs(tf.subtract(input_layer, x_mid)))
        loss = loss_labels + loss_reconst

        # Configure the Training Op (for TRAIN mode)
        if mode == tf.estimator.ModeKeys.TRAIN:
            optimizer = tf.train.AdamOptimizer()
            update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
            with tf.control_dependencies(update_ops):
                train_op = optimizer.minimize(
                    loss=loss,
                    global_step=tf.train.get_global_step())
            return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

        assert mode == tf.estimator.ModeKeys.EVAL
        eval_metric_ops = {
            "accuracy": tf.metrics.accuracy(
                labels=labels, predictions=predictions["classes"])}
        return tf.estimator.EstimatorSpec(
            mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

    def fc(self, x, out_dim, is_training):
        x = tf.layers.dense(inputs=x, units=out_dim)
        x = tf.layers.batch_normalization(x, training=is_training)
        x = tf.nn.relu(x)
        return x

    def input_function(self, dataset, mode):
        if mode == 'train':
            dataset = dataset.shuffle(buffer_size=100)
            dataset = dataset.repeat()

        dataset = dataset.batch(batch_size=128)

        iterator_name = 'iterator_' + mode

        if not hasattr(self, iterator_name):
            self.iterator = dataset.make_one_shot_iterator()
        iterator = dataset.make_one_shot_iterator()
        if mode == 'train':
            example, labels = iterator.get_next()
            return example, labels
        if mode == 'valid' or mode == 'test':
            example = iterator.get_next()
            return example

def sigmoid_cross_entropy_with_logits(labels, logits):
    labels = tf.cast(labels, dtype=tf.float32)
    relu_logits = tf.nn.relu(logits)
    exp_logits = tf.exp(- tf.abs(logits))
    sigmoid_logits = tf.log(1 + exp_logits)
    element_wise_xent = relu_logits - labels * logits + sigmoid_logits
    return tf.reduce_sum(element_wise_xent)

max_stars_repo_name: zichuan-scott-xu/automl-workflow

max_issues_repo_name: zichuan-scott-xu/automl-workflow

max_forks_repo_name: zichuan-scott-xu/automl-workflow


text:
import numpy as np
from sklearn.utils import shuffle
from hparams import HParams
import tensorflow as tf
import torch.utils.data
import torchvision.transforms as transforms
from torch.utils.data.distributed import DistributedSampler

hparams = HParams.get_hparams_by_name("efficient_vdvae")

def download_mnist_datasets():
    # Ignore labels
    (x_train, _), (x_test, _) = tf.keras.datasets.mnist.load_data()

    x_train = x_train[:, np.newaxis, :, :]  # (60000, 1, 28, 28)
    x_test = x_test[:, np.newaxis, :, :]  # (10000, 1,  28, 28)
    x_train, x_test = x_train / 255., x_test / 255.

    # make images of size 32x32
    x_train = np.pad(x_train, pad_width=((0, 0), (0, 0), (2, 2), (2, 2)))  # (60000, 1, 32, 32)
    x_test = np.pad(x_test, pad_width=((0, 0), (0, 0), (2, 2), (2, 2)))  # (60000, 1, 32, 32)

    x_train = shuffle(x_train)
    x_test = shuffle(x_test,
                     random_state=101)  # Fix this seed to not overlap val and test between train and inference runs

    x_val = x_test[:len(x_test) // 2]  # 5000
    x_test = x_test[len(x_test) // 2:]  # 5000

    x_val, x_test = torch.tensor(x_val), torch.tensor(x_test)
    x_val = torch.Tensor(x_val.size()).bernoulli_(x_val)  # fix binarization
    x_test = torch.Tensor(x_test.size()).bernoulli_(x_test)

    return x_train, x_val, x_test

class Binarize(object):
    def __call__(self, img):
        """
        :param numpy array

        :return: Tensor
        """
        img = torch.tensor(img)
        return torch.Tensor(img.size()).bernoulli_(img)

    def __repr__(self):
        return self.__class__.__name__ + '()'

transform = transforms.Compose([
    Binarize()
])

def make_toy_filenames(data):
    return [f'image_{i}' for i in range(data.shape[0])]

class mnist_dataset(torch.utils.data.Dataset):
    def __init__(self, images, mode):
        self.images = shuffle(images)
        self.mode = mode
        self.filenames = make_toy_filenames(images)

    def __getitem__(self, idx):
        if self.mode == 'train':
            img = self.images[idx]
            img = transform(img)
            return img
        elif self.mode in ['val', 'div_stats', 'test']:
            img = self.images[idx]
            return img
        elif self.mode == 'encode':
            filename = self.filenames[idx]
            img = self.images[idx]
            return img, filename
        else:
            raise ValueError(f'Unknown Mode {self.mode}')

    def __len__(self):
        if self.mode in ['train', 'test', 'encode']:
            return len(self.images)
        elif self.mode == 'val':
            return hparams.val.n_samples_for_validation
        elif self.mode == 'div_stats':
            return round(len(self.images) * hparams.synthesis.div_stats_subset_ratio)

def train_val_data_mnist(world_size, rank):
    train_images, val_images, _ = download_mnist_datasets()
    train_mnist = mnist_dataset(train_images, mode='train')
    train_sampler = DistributedSampler(train_mnist, num_replicas=world_size, rank=rank, shuffle=True, drop_last=True)
    train_loader = torch.utils.data.DataLoader(sampler=train_sampler,
                                               dataset=train_mnist,
                                               batch_size=hparams.train.batch_size // hparams.run.num_gpus,
                                               shuffle=False,
                                               pin_memory=True,
                                               num_workers=2,
                                               drop_last=True, prefetch_factor=3)

    val_mnist = mnist_dataset(val_images, mode='val')
    val_sampler = DistributedSampler(val_mnist, num_replicas=world_size, rank=rank, shuffle=True, drop_last=True)
    val_loader = torch.utils.data.DataLoader(sampler=val_sampler,
                                             dataset=val_mnist,
                                             batch_size=hparams.val.batch_size // hparams.run.num_gpus,
                                             shuffle=False,
                                             pin_memory=True,
                                             num_workers=2,
                                             drop_last=True, prefetch_factor=3)

    return train_loader, val_loader

def synth_mnist_data():
    _, _, test_images = download_mnist_datasets()
    synth_mnist = mnist_dataset(test_images, 'test')
    synth_loader = torch.utils.data.DataLoader(
        dataset=synth_mnist,
        batch_size=hparams.synthesis.batch_size,
        shuffle=True,
        pin_memory=True,
        num_workers=hparams.run.num_cpus,
        drop_last=True)
    return synth_loader

def encode_mnist_data():
    train_data, _, _ = download_mnist_datasets()
    encode_mnist = mnist_dataset(train_data, 'encode')
    encode_loader = torch.utils.data.DataLoader(
        dataset=encode_mnist,
        batch_size=hparams.synthesis.batch_size,
        shuffle=True,
        pin_memory=True,
        num_workers=hparams.run.num_cpus)
    return encode_loader

def stats_mnist_data():
    train_data, _, _ = download_mnist_datasets()
    stats_mnist = mnist_dataset(train_data, 'div_stats')
    stats_loader = torch.utils.data.DataLoader(
        dataset=stats_mnist,
        batch_size=hparams.synthesis.batch_size,
        shuffle=True,
        pin_memory=True,
        num_workers=hparams.run.num_cpus)
    return stats_loader

max_stars_repo_name: Rayhane-mamah/Efficient-VDVAE

max_issues_repo_name: Rayhane-mamah/Efficient-VDVAE

max_forks_repo_name: Rayhane-mamah/Efficient-VDVAE


text:
# MantaFlow fluid solver framework
# Copyright 2011 Tobias Pfaff, Nils Thuerey
#
# @author: Marie-Lena Eckert http://marielenaeckert.com/
#
#
# plot density and velocity fields
#

import math, time, sys
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from pylab import MaxNLocator

import uniio

# some global variables to reuse axes for all figures
didSetupV   = False
didSetupD   = False
didSetupI   = False

fsGlobal = (360*3.4/96, 720/96)

fV, axGlobalV = plt.subplots(1, 3, figsize=fsGlobal, dpi=96, facecolor='b')
fD, axGlobalD = plt.subplots(1, 3, figsize=fsGlobal, dpi=96)

XYVGlobal = 0
XYDGlobal = 0
XYIGlobal = 0
fI = 0
axGlobalI = 0

def setupV(gs):
	global didSetupV
	didSetupV = True

	global XYVGlobal

	x = np.linspace(0, gs[0], gs[0])
	y = np.linspace(0, gs[1], gs[1])
	ySquare = np.linspace(0, gs[0], gs[0])
	XYVGlobal = [np.meshgrid(x, y), np.meshgrid(x, ySquare), np.meshgrid(x, y)]

	for c in range(0,3):
		square = (c==2)
		gsY = gs[0] if square else gs[1]

		pos = [0.029+0.33*c,0.,0.3,1]
		axGlobalV[c].set_position(pos,'both')

		axGlobalV[c].get_yaxis().set_major_locator(MaxNLocator(integer=True))
		axGlobalV[c].get_xaxis().set_major_locator(MaxNLocator(integer=True))
		axGlobalV[c].set_aspect('equal')
		axGlobalV[c].xaxis.set_ticks(np.arange(0, gs[0]+1, math.ceil((gs[0]-1)/5)))
		if square: axGlobalV[c].yaxis.set_ticks(np.arange(0, gsY+2, math.ceil((gsY-1)/5)))
		else: axGlobalV[c].yaxis.set_ticks(np.arange(0, gsY+2, math.ceil((gsY-1)/3))+1)
		axGlobalV[c].set_xlim([0, gs[0]])
		axGlobalV[c].set_ylim([0, gsY+1])
		axGlobalV[c].spines['bottom'].set_color('#dddddd')
		axGlobalV[c].spines['top'].set_color('#dddddd')
		axGlobalV[c].spines['right'].set_color('#dddddd')
		axGlobalV[c].spines['left'].set_color('#dddddd')
		axGlobalV[c].tick_params(axis='x', colors='#dddddd')
		axGlobalV[c].tick_params(axis='y', colors='#dddddd')
		axGlobalV[c].yaxis.label.set_color('#dddddd')
		axGlobalV[c].xaxis.label.set_color('#dddddd')
		axGlobalV[c].set_axis_bgcolor('black')

def setupD(gs):
	global didSetupD
	didSetupD = True

	global XYDGlobal

	x = np.linspace(0, gs[0]+1, gs[0]+1)
	y = np.linspace(0, gs[1]+1, gs[1]+1)
	ySquare = np.linspace(0, gs[0]+1, gs[0]+1)
	XYDGlobal = [np.meshgrid(x, y), np.meshgrid(x, ySquare), np.meshgrid(x, y)]

	for c in range(0,3):
		square = (c==2)
		gsY = gs[0] if square else gs[1]

		pos = [0.029+0.33*c,0.,0.3,1]
		axGlobalD[c].set_position(pos,'both')
		axGlobalD[c].get_yaxis().set_major_locator(MaxNLocator(integer=True))
		axGlobalD[c].get_xaxis().set_major_locator(MaxNLocator(integer=True))
		axGlobalD[c].set_aspect('equal')
		axGlobalD[c].xaxis.set_ticks(np.arange(0, gs[0]+2, math.ceil((gs[0])/5)))
		if square: axGlobalD[c].yaxis.set_ticks(np.arange(0, gsY+2, math.ceil((gsY)/5)))
		else: axGlobalD[c].yaxis.set_ticks(np.arange(0, gsY+2, math.ceil((gsY-1)/3))+1)
		axGlobalD[c].set_xlim([0, gs[0]+1])
		axGlobalD[c].set_ylim([0, gsY+1])
		axGlobalD[c].spines['bottom'].set_color('#dddddd')
		axGlobalD[c].spines['top'].set_color('#dddddd')
		axGlobalD[c].spines['right'].set_color('#dddddd')
		axGlobalD[c].spines['left'].set_color('#dddddd')
		axGlobalD[c].tick_params(axis='x', colors='#dddddd')
		axGlobalD[c].tick_params(axis='y', colors='#dddddd')
		axGlobalD[c].yaxis.label.set_color('#dddddd')
		axGlobalD[c].xaxis.label.set_color('#dddddd')

def setupI(gs, numImgs):
	global didSetupI
	didSetupI = True

	global XYIGlobal
	global fI
	global axGlobalI

	# images
	fI, axGlobalI = plt.subplots(1, numImgs, figsize=(540*numImgs/96, 960/96), dpi=96)

	x = np.linspace(0, gs[0]+1, gs[0]+1)
	y = np.linspace(0, gs[1]+1, gs[1]+1)
	XYIGlobal = []

	for c in range(0,numImgs):
		XYIGlobal.append(np.meshgrid(x, y))

		pos = [0.017+c/numImgs,0.,1/numImgs-0.025,1]
		axGlobalI[c].set_position(pos,'both')
		axGlobalI[c].get_yaxis().set_major_locator(MaxNLocator(integer=True))
		axGlobalI[c].get_xaxis().set_major_locator(MaxNLocator(integer=True))
		axGlobalI[c].set_aspect('equal')
		axGlobalI[c].xaxis.set_ticks(np.arange(0, gs[0]+2, math.ceil((gs[0])/5)))
		axGlobalI[c].yaxis.set_ticks(np.arange(0, gs[1]+2, math.ceil((gs[1])/3)))
		axGlobalI[c].set_xlim([0, gs[0]+1])
		axGlobalI[c].set_ylim([0, gs[1]+1])

def adjustSubplots(image, c):
	if c==0:
		c2=c
	elif c==1:
		c2=2
	elif c==2:
		c2=1
		image = np.transpose(image, (1, 0, 2))

	return image, c2

def getVelCmps(image, c2):
	if c2==0 :
		U = image[:,:,0]
		V = image[:,:,1]
	elif c2==2:
		U = image[:,:,0]
		V = image[:,:,2]
	else:
		U = image[:,:,2]
		V = image[:,:,1]

	return U, V

def draw2DDensityNpy(filenameIn, scale, synthReal, negativeValues=False, dark=True, useColorMap=True):
	useMap = 'viridis' if synthReal else plt.cm.gnuplot2
	if not useColorMap: scale = 1
	colorMapToUse = useMap if useColorMap else 'gray'
	if dark: plt.style.use('dark_background')

	content = np.load(filenameIn)['data']

	shapeAdj = [content.shape[2], content.shape[1]]
	if not didSetupI: setupI(shapeAdj,content.shape[0])

	vmin = -scale if negativeValues else 0

	plots = []
	for c in range(content.shape[0]):
		image = content[c,:,:,0]

		plots.append(axGlobalI[c].pcolormesh(XYIGlobal[c][0], XYIGlobal[c][1],  image[:,:], cmap = colorMapToUse, vmin = vmin, vmax = scale))

	fI.savefig(filenameIn[:-3]+'jpg')
	for p in plots: p.remove()

def draw3DDensityGridNpy(filenameIn, scale, synthReal, negativeValues=False):
	plt.style.use('dark_background')
	useMap = 'viridis' if synthReal else plt.cm.gnuplot2

	content = np.load(filenameIn)['data']

	if not didSetupD: setupD(content.shape)

	vmin = -scale if negativeValues else 0

	plots = []
	for c in range(3):
		image = np.average(content, axis=c)

		image, c2 = adjustSubplots(image, c)

		plots.append(axGlobalD[c2].pcolormesh(XYDGlobal[c][0], XYDGlobal[c][1], image[:,:,0], cmap = useMap, vmin = vmin, vmax = scale))

	fD.savefig(filenameIn[:-3]+'jpg')
	for p in plots: p.remove()

def draw3DVelGridNpy(filenameIn, scale, synthReal):
	plt.style.use('dark_background')
	useMap = 'cool_r' if synthReal else 'spring'

	content = np.load(filenameIn)['data']

	if not didSetupV: setupV(content.shape)

	plots = []
	for c in range(3):
		image = np.average(content, axis=c)

		image, c2 = adjustSubplots(image, c)

		U, V = getVelCmps(image, c2)
		plots.append(axGlobalV[c2].quiver(XYVGlobal[c][0], XYVGlobal[c][1], U, V, np.arctan2(np.abs(V),np.abs(U)), cmap=useMap, headwidth=0, headlength=0, headaxislength=0, angles='xy', scale_units='xy', scale=scale, width=0.004))

	fV.savefig(filenameIn[:-3]+'jpg')
	for p in plots: p.remove()

max_stars_repo_name: tum-pbs/reconstructScalarFlows

max_issues_repo_name: tum-pbs/reconstructScalarFlows

max_forks_repo_name: tum-pbs/reconstructScalarFlows


text:
from asyncio import sleep
from contextlib import suppress
from multiprocessing import Process
from typing import AsyncIterator, List

import pytest
from _pytest.fixtures import fixture
from aiohttp import ClientSession
from arango.database import StandardDatabase

from resotocore.__main__ import run
from resotocore.config import ConfigValidation
from resotocore.db import EstimatedQueryCostRating
from resotocore.db.model import GraphUpdate
from resotocore.model.model import predefined_kinds, Kind, StringKind, ComplexKind, Property
from resotocore.model.typed_model import to_js
from resotocore.task.model import Subscription
from resotocore.util import rnd_str, AccessJson

# noinspection PyUnresolvedReferences
from tests.resotocore.db.graphdb_test import foo_kinds, test_db, create_graph, system_db, local_client
from resotoclient import ResotoClient as ApiClient
from resotoclient import models as rc
from networkx import MultiDiGraph

@fixture
async def client_session() -> AsyncIterator[ClientSession]:
    session = ClientSession()
    yield session
    await session.close()

def graph_to_json(graph: MultiDiGraph) -> List[rc.JsObject]:
    ga: List[rc.JsValue] = [{**node, "type": "node"} for _, node in graph.nodes(data=True)]
    for from_node, to_node, data in graph.edges(data=True):
        ga.append({"type": "edge", "from": from_node, "to": to_node, "edge_type": data["edge_type"]})
    return ga

@fixture
async def core_client(
    client_session: ClientSession, foo_kinds: List[Kind], test_db: StandardDatabase
) -> AsyncIterator[ApiClient]:
    """
    Note: adding this fixture to a test: a complete resotocore process is started.
          The fixture ensures that the underlying process has entered the ready state.
          It also ensures to clean up the process, when the test is done.
    """

    # wipe and cleanly import the test model
    test_db.collection("model").truncate()
    test_db.collection("model").insert_many([{"_key": elem.fqn, **to_js(elem)} for elem in foo_kinds])

    process = Process(
        target=run,
        args=(
            [
                "--graphdb-database",
                "test",
                "--graphdb-username",
                "test",
                "--graphdb-password",
                "test",
                "--debug",
                "--no-tls",
            ],
        ),
    )
    process.start()
    ready = False
    count = 10
    while not ready:
        await sleep(0.5)
        with suppress(Exception):
            async with client_session.get("http://localhost:8900/system/ready"):
                ready = True
                count -= 1
                if count == 0:
                    raise AssertionError("Process does not came up as expected")
    yield ApiClient("http://localhost:8900", None)
    # terminate the process
    process.terminate()
    process.join(5)
    # if it is still running, kill it
    if process.is_alive():
        process.kill()
        process.join()
    process.close()

g = "graphtest"

@pytest.mark.asyncio
async def test_system_api(core_client: ApiClient, client_session: ClientSession) -> None:
    assert core_client.ping() == "pong"
    assert core_client.ready() == "ok"
    # make sure we get redirected to the api docs
    async with client_session.get("http://localhost:8900", allow_redirects=False) as r:
        assert r.headers["location"] == "api-doc"
    # static api docs get served
    async with client_session.get("http://localhost:8900") as r:
        assert r.content_type == "text/html"

@pytest.mark.asyncio
async def test_model_api(core_client: ApiClient) -> None:

    # GET /model
    assert len((core_client.model()).kinds) >= len(predefined_kinds)

    # PATCH /model
    string_kind: rc.Kind = rc.Kind(fqn="only_three", runtime_kind="string", properties=None, bases=None)
    setattr(string_kind, "min_length", 3)
    setattr(string_kind, "max_length", 3)

    complex_kind: rc.Kind = rc.Kind(
        fqn="test_cpl",
        runtime_kind=None,
        properties=[{"description": None, "kind": "only_three", "name": "ot", "required": False, "synthetic": None}],
        bases=None,
    )
    setattr(complex_kind, "allow_unknown_props", False)

    update = core_client.update_model(
        [
            string_kind,
            complex_kind,
        ]
    )
    none_kind = rc.Kind(fqn="none", runtime_kind=None, properties=None, bases=None)
    assert (update.kinds.get("only_three") or none_kind).runtime_kind == "string"

@pytest.mark.asyncio
async def test_graph_api(core_client: ApiClient) -> None:
    # make sure we have a clean slate
    with suppress(Exception):
        core_client.delete_graph(g)

    # create a new graph
    graph = AccessJson(core_client.create_graph(g))
    assert graph.id == "root"
    assert graph.reported.kind == "graph_root"

    # list all graphs
    graphs = core_client.list_graphs()
    assert g in graphs

    # get one specific graph
    graph: AccessJson = AccessJson(core_client.get_graph(g))  # type: ignore
    assert graph.id == "root"
    assert graph.reported.kind == "graph_root"

    # wipe the data in the graph
    assert core_client.delete_graph(g, truncate=True) == "Graph truncated."
    assert g in core_client.list_graphs()

    # create a node in the graph
    uid = rnd_str()
    node = AccessJson(core_client.create_node("root", uid, {"identifier": uid, "kind": "child", "name": "max"}, g))
    assert node.id == uid
    assert node.reported.name == "max"

    # update a node in the graph
    node = AccessJson(core_client.patch_node(uid, {"name": "moritz"}, "reported", g))
    assert node.id == uid
    assert node.reported.name == "moritz"

    # get the node
    node = AccessJson(core_client.get_node(uid, g))
    assert node.id == uid
    assert node.reported.name == "moritz"

    # delete the node
    core_client.delete_node(uid, g)
    with pytest.raises(AttributeError):
        # node can not be found
        core_client.get_node(uid, g)

    # merge a complete graph
    merged = core_client.merge_graph(graph_to_json(create_graph("test")), g)
    assert merged == rc.GraphUpdate(112, 1, 0, 212, 0, 0)

    # batch graph update and commit
    batch1_id, batch1_info = core_client.add_to_batch(graph_to_json(create_graph("hello")), "batch1", g)
    assert batch1_info == rc.GraphUpdate(0, 100, 0, 0, 0, 0)
    assert batch1_id == "batch1"
    batch_infos = AccessJson.wrap_list(core_client.list_batches(g))
    assert len(batch_infos) == 1
    # assert batch_infos[0].id == batch1_id
    assert batch_infos[0].affected_nodes == ["collector"]  # replace node
    assert batch_infos[0].is_batch is True
    core_client.commit_batch(batch1_id, g)

    # batch graph update and abort
    batch2_id, batch2_info = core_client.add_to_batch(graph_to_json(create_graph("bonjour")), "batch2", g)
    assert batch2_info == rc.GraphUpdate(0, 100, 0, 0, 0, 0)
    assert batch2_id == "batch2"
    core_client.abort_batch(batch2_id, g)

    # update nodes
    update = [{"id": node["id"], "reported": {"name": "bruce"}} for _, node in create_graph("foo").nodes(data=True)]
    updated_nodes = core_client.patch_nodes(update, g)
    assert len(updated_nodes) == 113
    for n in updated_nodes:
        assert n.get("reported", {}).get("name") == "bruce"

    # create the raw search
    raw = core_client.search_graph_raw('id("3")', g)
    assert raw == {
        "query": "LET filter0 = (FOR m0 in graphtest FILTER m0._key == @b0  RETURN m0) "
        'FOR result in filter0 RETURN UNSET(result, ["flat"])',
        "bind_vars": {"b0": "3"},
    }

    # estimate the search
    cost = core_client.search_graph_explain('id("3")', g)
    assert cost.full_collection_scan is False
    assert cost.rating == rc.EstimatedQueryCostRating.simple

    # search list
    result_list = list(core_client.search_list('id("3") -[0:]->', g))
    assert len(result_list) == 11  # one parent node and 10 child nodes
    assert result_list[0].get("id") == "3"  # first node is the parent node

    # search graph
    result_graph = list(core_client.search_graph('id("3") -[0:]->', g))
    assert len(result_graph) == 21  # 11 nodes + 10 edges
    assert result_list[0].get("id") == "3"  # first node is the parent node

    # aggregate
    result_aggregate = core_client.search_aggregate("aggregate(reported.kind as kind: sum(1) as count): all", g)
    assert {r["group"]["kind"]: r["count"] for r in result_aggregate} == {
        "bla": 100,
        "cloud": 1,
        "foo": 11,
        "graph_root": 1,
    }

    # delete the graph
    assert core_client.delete_graph(g) == "Graph deleted."
    assert g not in core_client.list_graphs()

@pytest.mark.asyncio
async def test_subscribers(core_client: ApiClient) -> None:
    # provide a clean slate
    for subscriber in core_client.subscribers():
        core_client.delete_subscriber(subscriber.id)

    sub_id = rnd_str()

    # add subscription
    subscriber = core_client.add_subscription(sub_id, rc.Subscription("test"))
    assert subscriber.id == sub_id
    assert len(subscriber.subscriptions) == 1
    assert subscriber.subscriptions["test"] is not None

    # delete subscription
    subscriber = core_client.delete_subscription(sub_id, rc.Subscription("test"))
    assert subscriber.id == sub_id
    assert len(subscriber.subscriptions) == 0

    # update subscriber
    updated = core_client.update_subscriber(sub_id, [rc.Subscription("test"), rc.Subscription("rest")])
    assert updated is not None
    assert updated.id == sub_id
    assert len(updated.subscriptions) == 2

    # subscriber for message type
    assert core_client.subscribers_for_event("test") == [updated]
    assert core_client.subscribers_for_event("rest") == [updated]
    assert core_client.subscribers_for_event("does_not_exist") == []

    # get subscriber
    sub = core_client.subscriber(sub_id)
    assert sub is not None

@pytest.mark.asyncio
async def test_cli(core_client: ApiClient) -> None:
    # make sure we have a clean slate
    with suppress(Exception):
        core_client.delete_graph(g)
    core_client.create_graph(g)
    graph_update = graph_to_json(create_graph("test"))
    core_client.merge_graph(graph_update, g)

    # evaluate search with count
    result = core_client.cli_evaluate("search all | count kind", g)
    assert len(result) == 1
    parsed, to_execute = result[0]
    assert len(parsed.commands) == 2
    assert (parsed.commands[0].cmd, parsed.commands[1].cmd) == ("search", "count")
    assert len(to_execute) == 2
    assert (to_execute[0].get("cmd"), to_execute[1].get("cmd")) == ("execute_search", "aggregate_to_count")

    # execute search with count
    executed = list(core_client.cli_execute("search is(foo) or is(bla) | count kind", g))
    assert executed == ["cloud: 1", "foo: 11", "bla: 100", "total matched: 112", "total unmatched: 0"]

    # list all cli commands
    info = AccessJson(core_client.cli_info())
    assert len(info.commands) == 34

@pytest.mark.asyncio
async def test_config(core_client: ApiClient, foo_kinds: List[rc.Kind]) -> None:
    # make sure we have a clean slate
    for config in core_client.configs():
        core_client.delete_config(config)

    # define a config model
    model = core_client.update_configs_model(foo_kinds)
    assert "foo" in model.kinds
    assert "bla" in model.kinds
    # get the config model again
    get_model = core_client.get_configs_model()
    assert len(model.kinds) == len(get_model.kinds)

    # define config validation
    validation = rc.ConfigValidation("external.validated.config", external_validation=True)
    assert core_client.put_config_validation(validation) == validation

    # get the config validation
    assert core_client.get_config_validation(validation.id) == validation

    # put config
    cfg_id = rnd_str()

    # put a config with schema that is violated
    with pytest.raises(AttributeError) as ex:
        core_client.put_config(cfg_id, {"foo": {"some_int": "abc"}})
    assert "Expected type int32 but got str" in str(ex.value)

    # put a config with schema that is violated, but turn validation off
    core_client.put_config(cfg_id, {"foo": {"some_int": "abc"}}, validate=False)

    # set a simple state
    assert core_client.put_config(cfg_id, {"a": 1}) == {"a": 1}

    # patch config
    assert core_client.patch_config(cfg_id, {"a": 1}) == {"a": 1}
    assert core_client.patch_config(cfg_id, {"b": 2}) == {"a": 1, "b": 2}
    assert core_client.patch_config(cfg_id, {"c": 3}) == {"a": 1, "b": 2, "c": 3}

    # get config
    assert core_client.config(cfg_id) == {"a": 1, "b": 2, "c": 3}

    # list configs
    assert list(core_client.configs()) == [cfg_id]

    # delete config
    core_client.delete_config(cfg_id)
    assert list(core_client.configs()) == []

max_stars_repo_name: someengineering/resoto

max_issues_repo_name: someengineering/resoto

max_forks_repo_name: someengineering/resoto

reason: from networkx


text:
from multi_nonrigid.datagenerators import *
import numpy as np
import glob
import os

def test_select_index_atlas():
    indi = [1, 0, 0]

    index_select = select_index_atlas(indi)

    assert index_select == 0

    indi = [1, 1, 0]

    index_select = select_index_atlas(indi)

    assert index_select != 2

def test_image_generator():
    vol_names = glob.glob(os.path.join('test_nonrigid/test_data', '*.nii.gz'))
    age_atlas = np.load('test_nonrigid/test_data/atlas.npz')['Age']

    atlasses = np.load('test_nonrigid/test_data/atlas.npz')['atlas_vol']
    atlas_list = ['test/atlas_00001.npz', 'test/atlas_00002.npz', 'test/atlas_00003.npz', 'test/atlas_00001.npz', 'test/atlas_00002.npz']

    output = list(image_generator(vol_names, 1, atlasses, age_atlas, atlas_list, test=True))

    assert output[0][0].shape == (1,64,64,64,1)
    assert np.sum(output[0][0]) == 61504.0

    assert output[0][1].shape == (1,64,64,64,1)
    assert np.sum(output[0][1]) == 0.0

def test_datagenerator_nonrigid():
    vol_names = glob.glob(os.path.join('test_nonrigid/test_data', '*.nii.gz'))
    age_atlas = np.load('test_nonrigid/test_data/atlas.npz')['Age']
    atlasses = np.load('test_nonrigid/test_data/atlas.npz')['atlas_vol']
    atlas_list = ['test/atlas_00001.npz', 'test/atlas_00002.npz', 'test/atlas_00003.npz', 'test/atlas_00001.npz', 'test/atlas_00002.    npz']

    gen = image_generator(vol_names, 1, atlasses, age_atlas, atlas_list, test=True)

    output = list(datagenerator_nonrigid(gen))

    assert output[0][0][0].shape == (1,64,64,64,1)
    assert np.sum(output[0][0][0]) == 61504.0

    assert output[0][0][1].shape == (1,64,64,64,1)
    assert np.sum(output[0][0][1]) == 0.0

    assert output[0][1][0].shape == (1,64,64,64,1)
    assert np.sum(output[0][1][0]) == 0.0

    assert output[0][1][1].shape == (1,64,64,64,3)
    assert np.sum(output[0][1][1]) == 0.0

    gen = image_generator(vol_names, 1, atlasses, age_atlas, atlas_list, test=True)
    output = list(datagenerator_nonrigid(gen, diffeomorphic=True))

    assert output[0][1][2].shape == (1,64,64,64,3)
    assert np.sum(output[0][1][2]) == 0.0

max_stars_repo_name: wapbastiaansen/multi-atlas-seg-reg

max_issues_repo_name: wapbastiaansen/multi-atlas-seg-reg

max_forks_repo_name: wapbastiaansen/multi-atlas-seg-reg


text:
# coding=utf-8
# Copyright 2018 The TF-Agents Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Treat multiple non-batch environments as a single batch environment."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# pylint: disable=line-too-long
# multiprocessing.dummy provides a pure *multithreaded* threadpool that works
# in both python2 and python3 (concurrent.futures isn't available in python2).
#   https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.dummy
from multiprocessing import dummy as mp_threads
# pylint: enable=line-too-long

import numpy as np

import tensorflow as tf
from tf_agents.environments import py_environment
import gin.tf

@gin.configurable
class BatchedPyEnvironment(py_environment.PyEnvironment):
  """Batch together multiple py environments and act as a single batch.

  The environments should only access shared python variables using
  shared mutex locks (from the threading module).
  """

  def __init__(self, envs):
    """Batch together multiple (non-batched) py environments.

    The environments can be different but must use the same action and
    observation specs.

    Args:
      envs: List python environments (must be non-batched).

    Raises:
      ValueError: If envs is not a list or tuple, or is zero length, or if
        one of the envs is already batched.
      ValueError: If the action or observation specs don't match.
    """
    if not isinstance(envs, (list, tuple)):
      raise ValueError("envs must be a list or tuple.  Got: %s" % envs)
    batched_envs = [(i, env) for i, env in enumerate(envs) if env.batched]
    if batched_envs:
      raise ValueError(
          "Some of the envs are already batched: %s" % batched_envs)
    self._envs = envs
    self._num_envs = len(envs)
    self._action_spec = self._envs[0].action_spec()
    self._observation_spec = self._envs[0].observation_spec()
    self._time_step_spec = self._envs[0].time_step_spec()
    if any(env.action_spec() != self._action_spec for env in self._envs):
      raise ValueError(
          "All environments must have the same action spec.  Saw: %s" %
          [env.action_spec() for env in self._envs])
    if any(env.time_step_spec() != self._time_step_spec for env in self._envs):
      raise ValueError(
          "All environments must have the same time_step_spec.  Saw: %s" %
          [env.time_step_spec() for env in self._envs])
    # Create a multiprocessing threadpool for execution.
    self._pool = mp_threads.Pool(self._num_envs)

  @property
  def batched(self):
    return True

  @property
  def batch_size(self):
    return len(self._envs)

  @property
  def envs(self):
    return self._envs

  def observation_spec(self):
    return self._observation_spec

  def action_spec(self):
    return self._action_spec

  def time_step_spec(self):
    return self._time_step_spec

  def _reset(self):
    """Reset all environments and combine the resulting observation.

    Returns:
      Time step with batch dimension.
    """
    time_steps = self._pool.map(lambda env: env.reset(), self._envs)
    return stack_time_steps(time_steps)

  def _step(self, actions):
    """Forward a batch of actions to the wrapped environments.

    Args:
      actions: Batched action, possibly nested, to apply to the environment.

    Raises:
      ValueError: Invalid actions.

    Returns:
      Batch of observations, rewards, and done flags.
    """
    unstacked_actions = unstack_actions(actions)
    if len(unstacked_actions) != self.batch_size:
      raise ValueError(
          "Primary dimension of action items does not match "
          "batch size: %d vs. %d" % (len(unstacked_actions), self.batch_size))
    time_steps = self._pool.map(
        lambda env_action: env_action[0].step(env_action[1]),
        zip(self._envs, unstacked_actions))
    return stack_time_steps(time_steps)

  def close(self):
    """Send close messages to the external process and join them."""
    self._pool.map(lambda env: env.close(), self._envs)
    self._pool.close()
    self._pool.join()

# TODO(b/124447001): Factor these helper functions out into common utils.
def stack_time_steps(time_steps):
  """Given a list of TimeStep, combine to one with a batch dimension."""
  return fast_map_structure(lambda *arrays: np.stack(arrays), *time_steps)

def unstack_actions(batched_actions):
  """Returns a list of actions from potentially nested batch of actions."""
  flattened_actions = tf.nest.flatten(batched_actions)
  unstacked_actions = [
      tf.nest.pack_sequence_as(batched_actions, actions)
      for actions in zip(*flattened_actions)
  ]
  return unstacked_actions

def fast_map_structure(func, *structure):
  """List tf.nest.map_structure, but skipping the slow assert_same_structure."""
  flat_structure = [tf.nest.flatten(s) for s in structure]
  entries = zip(*flat_structure)
  return tf.nest.pack_sequence_as(structure[0], [func(*x) for x in entries])


text:
"""
Series of data are loaded from a .csv file, and their names are
displayed in a checkable list view. The user can select the series
it wants from the list and plot them on a matplotlib canvas.
Use the sample .csv file that comes with the script for an example
of data series.

[2016-11-06] Convert to QtQuick 2.0 - Qt.labs.controls 1.0
[2016-11-05] Convert to QtQuick 2.0 - QtQuick Controls 1.0
[2016-11-01] Update to PyQt5.6 and python 3.5

Frederic Collonval (fcollonval@gmail.com)

Inspired from the work of Eli Bendersky (eliben@gmail.com):
https://github.com/eliben/code-for-blog/tree/master/2009/pyqt_dataplot_demo

License: MIT License
Last modified: 2016-11-06
"""
import sys, os, csv
from PyQt5.QtCore import QAbstractListModel, QModelIndex, QObject, QSize, Qt, QUrl, QVariant, pyqtProperty, pyqtSlot, pyqtSignal
from PyQt5.QtGui import QGuiApplication, QColor, QImage, QPixmap
# from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType
from PyQt5.QtQuick import QQuickImageProvider

import matplotlib
matplotlib.use('Agg')
# from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
# from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
# from matplotlib.figure import Figure
import matplotlib.pyplot as plt

import numpy as np

sys.path.append('../backend')
from backend_qtquick5 import FigureCanvasQTAggToolbar, MatplotlibIconProvider

class DataSerie(object):

    def __init__(self, name, data, selected=False):
        self._name = name
        self._data = data
        self._selected = selected

    def name(self):
        return self._name

    def selected(self):
        return self._selected

    def data(self):
        return self._data

class DataSeriesModel(QAbstractListModel):

    # Define role enum
    SelectedRole = Qt.UserRole
    NameRole = Qt.UserRole + 1
    DataRole = Qt.UserRole + 2

    _roles = {
        SelectedRole : b"selected",
        NameRole : b"name",
        DataRole : b"data"
    }

    lengthDataChanged = pyqtSignal()

    def __init__(self, parent=None):
        QAbstractListModel.__init__(self, parent)

        self._data_series = list()
        self._length_data = 0

    @pyqtProperty(int, notify=lengthDataChanged)
    def lengthData(self):
        return self._length_data

    @lengthData.setter
    def lengthData(self, length):
        if self._length_data != length:
            self._length_data = length
            self.lengthDataChanged.emit()

    def roleNames(self):
        return self._roles

    def load_from_file(self, filename=None):
        self._data_series.clear()
        self._length_data = 0

        if filename:
            with open(filename, 'r') as f:
                for line in csv.reader(f):
                    series = DataSerie(line[0],
                                       [i for i in map(int, line[1:])])
                    self.add_data(series)

    def add_data(self, data_series):
        self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
        self._data_series.append(data_series)
        self.lengthData = max(self.lengthData, len(data_series.data()))
        self.endInsertRows()

    def rowCount(self, parent=QModelIndex()):
        return len(self._data_series)

    def data(self, index, role=Qt.DisplayRole):
        if(index.row() < 0 or index.row() >= len(self._data_series)):
            return QVariant()

        series = self._data_series[index.row()]

        if role == self.SelectedRole:
            return series.selected()
        elif role == self.NameRole:
            return series.name()
        elif role == self.DataRole:
            return series.data()

        return QVariant()

    def setData(self, index, value, role=Qt.EditRole):
        if(index.row() < 0 or index.row() >= len(self._data_series)):
            return False

        series = self._data_series[index.row()]

        if role == self.SelectedRole:
            series._selected = not value
            self.dataChanged.emit(index, index, [role,])
            return True

        return False

class Form(QObject):

    xFromChanged = pyqtSignal()
    xToChanged = pyqtSignal()
    legendChanged = pyqtSignal()
    statusTextChanged = pyqtSignal()
    stateChanged = pyqtSignal()

    def __init__(self, parent=None, data=None):
        QObject.__init__(self, parent)

        self._status_text = "Please load a data file"

        self._filename = ""
        self._x_from = 0
        self._x_to = 1
        self._legend = False

        # default dpi=80, so size = (480, 320)
        self._figure = None
        self.axes = None

        self._data = data

    @property
    def figure(self):
        return self._figure

    @figure.setter
    def figure(self, fig):
        self._figure = fig
        self._figure.set_facecolor('white')
        self.axes = self.figure.add_subplot(111)

        # Signal connection
        self.xFromChanged.connect(self._figure.canvas.draw_idle)
        self.xToChanged.connect(self._figure.canvas.draw_idle)
        self.legendChanged.connect(self._figure.canvas.draw_idle)
        self.stateChanged.connect(self._figure.canvas.draw_idle)

    @pyqtProperty('QString', notify=statusTextChanged)
    def statusText(self):
        return self._status_text

    @statusText.setter
    def statusText(self, text):
        if self._status_text != text:
            self._status_text = text
            self.statusTextChanged.emit()

    @pyqtProperty('QString')
    def filename(self):
        return self._filename

    @filename.setter
    def filename(self, filename):
        if filename:
            filename = QUrl(filename).toLocalFile()
            if filename != self._filename:
                self._filename = filename
                self._data.load_from_file(filename)
                self.statusText = "Loaded " + filename
                self.xTo = self._data.lengthData
                self.update_figure()

    @pyqtProperty(int, notify=xFromChanged)
    def xFrom(self):
        return self._x_from

    @xFrom.setter
    def xFrom(self, x_from):
        if self.figure is None:
            return

        x_from = int(x_from)
        if self._x_from != x_from:
            self._x_from = x_from
            self.axes.set_xlim(left=self._x_from)
            self.xFromChanged.emit()

    @pyqtProperty(int, notify=xToChanged)
    def xTo(self):
        return self._x_to

    @xTo.setter
    def xTo(self, x_to):
        if self.figure is None:
            return

        x_to = int(x_to)
        if self._x_to != x_to:
            self._x_to = x_to
            self.axes.set_xlim(right=self._x_to)
            self.xToChanged.emit()

    @pyqtProperty(bool, notify=legendChanged)
    def legend(self):
        return self._legend

    @legend.setter
    def legend(self, legend):
        if self.figure is None:
            return

        if self._legend != legend:
            self._legend = legend
            if self._legend:
                self.axes.legend()
            else:
                leg = self.axes.get_legend()
                if leg is not None:
                    leg.remove()
            self.legendChanged.emit()

    @pyqtProperty('QString', constant=True)
    def about(self):
        msg = __doc__
        return msg.strip()

    @pyqtSlot()
    def update_figure(self):
        if self.figure is None:
            return

        self.axes.clear()
        self.axes.grid(True)

        has_series = False

        for row in range(self._data.rowCount()):
            model_index = self._data.index(row, 0)
            checked = self._data.data(model_index, DataSeriesModel.SelectedRole)

            if checked:
                has_series = True
                name = self._data.data(model_index, DataSeriesModel.NameRole)
                values = self._data.data(model_index, DataSeriesModel.DataRole)

                self.axes.plot(range(len(values)), values, 'o-', label=name)

        self.axes.set_xlim((self.xFrom, self.xTo))
        if has_series and self.legend:
            self.axes.legend()

        self.stateChanged.emit()

def main():
    argv = sys.argv

    # Trick to set the style / not found how to do it in pythonic way
    argv.extend(["-style", "universal"])
    app = QGuiApplication(argv)

    qmlRegisterType(FigureCanvasQTAggToolbar, "Backend", 1, 0, "FigureToolbar")
    imgProvider = MatplotlibIconProvider()

    # !! You must specified the QApplication as parent of QQmlApplicationEngine
    # otherwise a segmentation fault is raised when exiting the app
    engine = QQmlApplicationEngine(parent=app)
    engine.addImageProvider("mplIcons", imgProvider)

    context = engine.rootContext()
    data_model = DataSeriesModel()
    context.setContextProperty("dataModel", data_model)
    mainApp = Form(data=data_model)
    context.setContextProperty("draw_mpl", mainApp)

    engine.load(QUrl('main.qml'))

    win = engine.rootObjects()[0]
    mainApp.figure = win.findChild(QObject, "figure").getFigure()

    rc = app.exec_()
    # There is some trouble arising when deleting all the objects here
    # but I have not figure out how to solve the error message.
    # It looks like 'app' is destroyed before some QObject
    sys.exit(rc)

if __name__ == "__main__":
    main()


text:
# encoding: utf-8

from __future__ import print_function
from __future__ import absolute_import
import numpy as np

from jaqs.trade import EventDrivenStrategy
from jaqs.trade import common, model

from jaqs.data import RemoteDataService
from jaqs.trade import EventBacktestInstance
from jaqs.trade import BacktestTradeApi
from jaqs.trade import PortfolioManager
import jaqs.trade.analyze as ana
import jaqs.util as jutil

from config_path import DATA_CONFIG_PATH, TRADE_CONFIG_PATH
data_config = jutil.read_json(DATA_CONFIG_PATH)
trade_config = jutil.read_json(TRADE_CONFIG_PATH)

result_dir_path = '../../output/dual_thrust'

class DualThrust(EventDrivenStrategy):
    def __init__(self):
        super(DualThrust, self).__init__()
        self.symbol = ''
        self.quote = None
        self.bufferCount = 0
        self.bufferSize = 0
        self.high_list = []
        self.close_list = []
        self.low_list = []
        self.open_list = []
        self.k1 = 0.0
        self.k2 = 0.0
        self.pos = 0
        self.Upper = 0.0
        self.Lower = 0.0
        self.output = False

    def init_from_config(self, props):
        super(DualThrust, self).init_from_config(props)
        self.symbol = props.get('symbol')
        self.init_balance = props.get('init_balance')
        self.bufferSize = props.get('buffersize')
        self.k1 = props.get('k1')
        self.k2 = props.get('k2')

        self.high_list = np.zeros(self.bufferSize)
        self.close_list = np.zeros(self.bufferSize)
        self.low_list = np.zeros(self.bufferSize)
        self.open_list = np.zeros(self.bufferSize)

        self.tick_size = props.get('tick_size', 1.0)

    def initialize(self):
        self.bufferCount += 1
        td = self.ctx.trade_date
        ds = self.ctx.data_api
        df, msg = ds.daily(symbol=self.symbol, start_date=td, end_date=td)

        self.open_list[0:self.bufferSize - 1] = self.open_list[1:self.bufferSize]
        self.open_list[-1] = df.open
        self.high_list[0:self.bufferSize - 1] = self.high_list[1:self.bufferSize]
        self.high_list[-1] = df.high
        self.close_list[0:self.bufferSize - 1] = self.close_list[1:self.bufferSize]
        self.close_list[-1] = df.close
        self.low_list[0:self.bufferSize - 1] = self.low_list[1:self.bufferSize]
        self.low_list[-1] = df.low

        HH = max(self.high_list[:-1])
        HC = max(self.close_list[:-1])
        LC = min(self.close_list[:-1])
        LL = min(self.low_list[:-1])

        Range = max(HH - LC, HC - LL)
        self.Upper = self.open_list[-1] + self.k1 * Range
        self.Lower = self.open_list[-1] - self.k2 * Range

    def on_cycle(self):
        pass

    def on_tick(self, quote):
        pass

    def buy(self, quote, price, size):
        self.ctx.trade_api.place_order(quote.symbol, 'Buy', price, size)

    def sell(self, quote, price, size):
        self.ctx.trade_api.place_order(quote.symbol, 'Sell', price, size)

    def on_bar(self, quote):
        if self.bufferCount <= self.bufferSize:
            return
        self.quote = quote.get(self.symbol)

        if self.quote.time > 90100 and self.quote.time <= 142800:
            if self.pos == 0:
                if self.quote.close > self.Upper:
                    self.buy(self.quote, self.quote.close, 1)
                elif self.quote.close < self.Lower:
                    self.sell(self.quote, self.quote.close, 1)

            elif self.pos < 0:
                if self.quote.close > self.Upper:
                    self.buy(self.quote, self.quote.close, 2)

            else:
                if self.quote.close < self.Lower:
                    self.sell(self.quote, self.quote.close, 2)
        elif self.quote.time > 142800:
            pass
            # self.cancel_all_orders()
            # self.liquidate(self.quote, 3, tick_size=self.tick_size, pos=self.pos)

    def on_trade(self, ind):
        print("\nStrategy on trade: ")
        print(ind)
        self.pos = self.ctx.pm.get_pos(ind.symbol)
        print(self.ctx.pm.get_trade_stat(ind.symbol))

    def on_order_status(self, ind):
        if self.output:
            print("\nStrategy on order status: ")
            print(ind)

    def on_task_rsp(self, rsp):
        if self.output:
            print("\nStrategy on task rsp: ")
            print(rsp)

    def on_order_rsp(self, rsp):
        if self.output:
            print("\nStrategy on order rsp: ")
            print(rsp)

    def on_task_status(self, ind):
        if self.output:
            print("\nStrategy on task ind: ")
            print(ind)

props = {
    "symbol": "rb1710.SHF",
    "start_date": 20170510,
    "end_date": 20170830,
    "buffersize": 7,
    "k1": 0.2,
    "k2": 0.2,
    "bar_type": "1M",
    "init_balance": 3e4,
    "commission_rate": 0.0
}
props.update(data_config)
props.update(trade_config)

def run_strategy():
    tapi = BacktestTradeApi()
    ins = EventBacktestInstance()

    ds = RemoteDataService()
    strat = DualThrust()
    pm = PortfolioManager()

    context = model.Context(data_api=ds, trade_api=tapi, instance=ins,
                            strategy=strat, pm=pm)

    ins.init_from_config(props)
    ins.run()
    ins.save_results(folder_path=result_dir_path)

def analyze():
    ta = ana.EventAnalyzer()

    ds = RemoteDataService()
    ds.init_from_config(data_config)

    ta.initialize(data_server_=ds, file_folder=result_dir_path)

    ta.do_analyze(result_dir=result_dir_path, selected_sec=props['symbol'].split(','))

if __name__ == "__main__":
    run_strategy()
    analyze()


text:
import csv
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics.classification import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score

def loaddata(filename,instanceCol):
    file_reader = csv.reader(open(filename,'r'), delimiter=',')
    x = []
    y = []
    for row in file_reader:
        x.append(row[0:len(row)-1])
        y.append(row[-1])
    print(np.asarray(x).shape)
    return np.array(x[1:]).astype((np.float32)), np.array(y[1:]).astype(np.int)

def fractal_modeldata(filename):
    print(filename)
    X, Y = loaddata(filename, 5)
    np.random.seed(13)
    indices = np.random.permutation(1120)
    test_size = int(0.9 * len(indices))
    X_train = X[indices[:-test_size]]
    Y_train = Y[indices[:-test_size]]
    X_test = X[indices[-test_size:]]
    Y_test = Y[indices[-test_size:]]
    # relu, sigmoid
    classifier = KNeighborsClassifier(n_neighbors=5)
    classifier.fit(X_train, Y_train)
    Y_pred = classifier.predict(X_test)
    print(classification_report(Y_test, Y_pred))
    fpr, tpr, thre = roc_curve(Y_test, Y_pred)
    print(accuracy_score(Y_test, Y_pred)*100)
    print(roc_auc_score(Y_test, Y_pred))
    return fpr, tpr, thre

if __name__ == '__main__':
    root = 'D:\\\MySourceCodes\\Projects-Python\\Steganalysis-By-Frame\\SteganalysisDatasets\\Dataset\Fractal\\'
    fractal_modeldata(root + 'noisywavlet-Features-steghide-100.csv')
    fractal_modeldata(root + 'noisywavlet-Features-steghide-71.csv')
    fractal_modeldata(root + 'noisywavlet-Features-steghide-42.csv')
    fractal_modeldata(root + 'noisywavlet-Features-steghide-21.csv')
    fractal_modeldata(root + 'noisywavlet-Features-steghide-7.csv')

max_stars_repo_name: AIKICo/Steganalysis-By-Frame

max_issues_repo_name: MohMehrnia/Steganalysis-By-Frame

max_forks_repo_name: MohMehrnia/Steganalysis-By-Frame


text:
"""
A wrapper for numpy ndarrays to store our additional annotations
"""
import numpy

class MlinspectNdarray(numpy.ndarray):
    """
    A wrapper for numpy ndarrays to store our additional annotations.
    See https://docs.scipy.org/doc/numpy-1.13.0/user/basics.subclassing.html
    """

    def __new__(cls, input_array, annotations=None):
        # Input array is an already formed ndarray instance
        # We first cast to be our class type
        obj = numpy.asarray(input_array).view(cls)
        # add the new attribute to the created instance
        obj.annotations = annotations
        # Finally, we must return the newly created object:
        return obj

    def __array_finalize__(self, obj):
        # pylint: disable=attribute-defined-outside-init
        # see InfoArray.__array_finalize__ for comments
        if obj is None:
            return
        self.info = getattr(obj, 'annotations', None)

max_stars_repo_name: stefan-grafberger/mlinspect-cidr

max_issues_repo_name: stefan-grafberger/mlinspect-cidr

max_forks_repo_name: stefan-grafberger/mlinspect-cidr


text:
from django import forms
from .models import Image
import cv2
import numpy as np
import os
import boto3
from botocore.exceptions import ClientError

class ImageUploadForm(forms.ModelForm):

    class Meta:
        model = Image
        fields = ['original_image']

max_stars_repo_name: TrushaT/Fun-With-Cartoonista

max_issues_repo_name: TrushaT/Fun-With-Cartoonista

max_forks_repo_name: TrushaT/Fun-With-Cartoonista


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Tests for functions/aerosols_202.py functions.

Created on Mon Feb  3 16:20:51 2020

@author: Caterina Mogno c.mogno@ed.ac.uk
"""

import numpy as np
import xarray as xr

import sys
sys.path.append('/exports/csce/datastore/geos/users/s1878599/python_code/')
from WRFChemToolkit.analysis import aerosols_202 as ar202

# Load test data.
data_path = '../../../sample_WRF_chem_out_202.nc'
ds = xr.open_dataset(data_path)

# --------------------------- CHEM-OPT = 202 -------------------------------

ar202.get_pm_species(ds)
ar202.get_pm_components(ds)
ar202.calculate_tot_pm(ds)
ar202.direct_pm(ds)

#TEST1: compare calculated pm25 with diagnostic variable PM2_5_DRY.
print('Testing pm2.5 from components')
np.testing.assert_allclose(
           ds.pm25_tot.values, ds.PM2_5_DRY.values,rtol=1e-06)

#TEST2: compare directed calculated pm25 with diagnostic variable PM2_5_DRY.
print('Testing direc pm2.5 calculation')
np.testing.assert_allclose(
           ds.pm25_dir_tot.values, ds.PM2_5_DRY.values,rtol=1e-06)

#TEST3: compare calculated pm10 with diagnostic variable PM10.
print('Testing pm10')
np.testing.assert_allclose(
           ds.pm10_tot.values, ds.PM10.values,rtol=1e-06)

print('All tests passed for chem_opt=202!')

max_stars_repo_name: catemgn/WRFChem-Toolkit

max_issues_repo_name: catemgn/WRFChem-Toolkit

max_forks_repo_name: catemgn/WRFChem-Toolkit


text:
# Let's begin by importing what we'll need (You'll probably be copying and
# pasting this a lot)

# The normal imports
import numpy as np
from numpy.random import randn
import pandas as pd

# Import the stats librayr from numpy
from scipy import stats

# These are the plotting modules adn libraries we'll use:
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

# Create a random normal-dist dataset
dataset1 = randn(100)

# Plot a histogram of the dataset, note bins=10 by default
plt.hist(dataset1)

# Lets make another dataset
dataset2 = randn(80)

# Plot
plt.hist(dataset2, color='indianred')

# We can use normed to plot on same plot

# Set normed=True for the plots to be normalized in order to comapre data sets with different number of observations
# Set alpha=0.5 for transperancy

plt.hist(dataset1, normed=True, color='indianred', alpha=0.5, bins=20)
plt.hist(dataset2, normed=True, alpha=0.5, bins=20)

# Make two more random normal dist data sets
data1 = randn(1000)
data2 = randn(1000)

# Can represent joint distributions using joint plots
sns.jointplot(data1, data2)

# Can also use hex bins for a more concise picture
sns.jointplot(data1, data2, kind='hex')

max_stars_repo_name: LucasHelal/data-science

max_issues_repo_name: LucasHelal/data-science

max_forks_repo_name: LucasHelal/data-science

reason: import numpy,from numpy,from scipy


text:
#!/usr/bin/env python

import gsidiag
import numpy as np

# Conventional diagnostic file

convobsfile = 'diag_conv_ges.2015102700'
diag_conv = gsidiag.GSIdiag(convobsfile, 'CONVENTIONAL', endian='big')
nobsall = diag_conv.nobs
print 'total number of obs = %d ' % nobsall

# print OmF stats for all ship ps obs used in assimilation
obtype = 'ps'
ship_ps_hx = diag_conv.get_data_conv('hx', obtype, used=1, code=180)
ship_ps_obs = diag_conv.get_data_conv('obs', obtype, used=1, code=180)
fitsq = ((ship_ps_hx - ship_ps_obs)**2).mean()
print 'total number of ship surface pressure obs used in assimilation = %d ' % len(ship_ps_obs)
print 'OmF for all of  ship surface pressure obs used in assimilation = %f ' % np.sqrt(fitsq)

# Radiance diagnostic file

radobsfile = 'diag_amsua_n15_ges.2016030100'
diag_rad = gsidiag.GSIdiag(radobsfile, 'RADIANCE', endian='big')
nobsall = diag_rad.nobs
print 'total number of obs = %d ' % nobsall

# print OmF stats for all used obs from channel 7 with reasonable oberr
ichan = 7
hx = diag_rad.get_data_rad('hx', ichan, used=1, oberr=1.e9)
obs = diag_rad.get_data_rad('obs', ichan, used=1, oberr=1.e9)
fitsq = ((hx - obs)**2).mean()

print '%d obs used for channel %d out of %d' % (len(obs), ichan, nobsall)
print 'RMS OmF for used obs for channel %d = %f' % (ichan, np.sqrt(fitsq))

sind = 6
print 'Sample info'
print 'obs[%d] = %f, hx[%d] = %f, biascorr[%d] = %f, sum(biaspred[%d]) = %f' % (sind, diag_rad.obs[sind], sind, diag_rad.hx[sind], sind, diag_rad.biascorr[sind], sind, diag_rad.biaspred[1:,sind].sum())

max_stars_repo_name: whpldgs/py-gsidiag

max_issues_repo_name: whpldgs/py-gsidiag

max_forks_repo_name: whpldgs/py-gsidiag


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

"""
Common plotting objects and functions.
"""
from .angle import Angle

import numpy as np
from astropy.wcs import WCS

class PlotConfig(object):
    """
    Stores plot references for overlay plotting

    Attributes:
        fig (matplotlib.pyplot.figure): plot figure
        ax (matplotlib.pyplot.axes): plot axes
        limits (Limits): plot limits
    """
    def __init__(self, fig, ax, limits):
        self.fig = fig
        self.ax = ax
        self.limits = limits

class Limits(object):
    """
    Stores the limits for the x-axis and y-axis of a plot
    """
    def __init__(self, xi, xf, yi, yf, check=False):
        """
        TODO
        """
        self.xi = xi
        self.xf = xf
        self.yi = yi
        self.yf = yf
        if check:
            self.check_limits()
            #TODO: rethink limits
        # self.lx = float(abs(xf - xi))
        # self.ly = float(abs(yf - yi))

    def area(self):
        return (self.xf.arcmin - self.xi.arcmin) * (self.yf.arcmin - self.yi.arcmin)

    def add_perimeter(self, radius):
        ret = Limits(
            # left handed coordinate system
            Angle.from_arcmin(self.xi.arcmin + radius.arcmin),
            Angle.from_arcmin(self.xf.arcmin - radius.arcmin),
            Angle.from_arcmin(self.yi.arcmin - radius.arcmin),
            Angle.from_arcmin(self.yf.arcmin + radius.arcmin)
                    )
        return ret

    def check_limits(self):
        """
        Confirms limits are reasonable

        Raises:
            Exception if the limits would cause errors in plotting code
        """
        if self.xi < self.xf:
            raise Exception("x_i, x_f are in left handed right ascension coordinates, so x_i > x_f")
        elif self.yi > self.yf:
            raise Exception("y_i, y_f are in declination coordinates, so y_i < y_f")

    def to_pixels(self, lens_map):
        """
        TODO
        :param lens_map:
        :return:
        """
        pix_xi, pix_yi = lens_map.world_to_image(self.xi, self.yi)
        pix_xf, pix_yf = lens_map.world_to_image(self.xf, self.yf)
        return Limits(pix_xi, pix_xf, pix_yi, pix_yf, check=False)

    @staticmethod
    def default(lens_map):
        """
        TODO
        :param lens_map:
        :return:
        """
        xi, yi = lens_map.image_to_world(0, 0)
        xf, yf = lens_map.image_to_world(lens_map.NX[0], lens_map.NX[0])
        return Limits(xi, xf, yi, yf)

def set_figure_size(fig, fig_size, limits):
    """
    Takes inputted figure and changes its size according to Lx and Ly given in
    inches.

    TODO

    Args:
        fig (matplotlib.figure.Figure): figure to size
        fig_size (int): total figure size
        lx (float): length of x axis
        ly (float): length of y axis
    """

    if limits.lx > limits.ly:
        fig.set_size_inches(fig_size, fig_size * (1.0 * limits.ly / limits.lx))
    else:
        fig.set_size_inches(fig_size * (1.0 * limits.lx / limits.ly), fig_size)

def make_wcs_axes(fig, limits, pix_limits, header):
    """
    TODO: might be able to move into PlotConfig

    Sets wcs and pixel axes for plotting maps and catalogs. Both sets of axes are
    contained in the current figure instance, and also returned for ease of use.

    Args:
        fig (matplotlib.figure.Figure): figure to add axes to
        lx (float): length of x axis
        ly (float): length of y axis
        header (astropy.io.fits.header.Header): fits header for axes projection
        pix_limits (list(float)): list of four limits for plot [x_min, x_max, y_min, y_max]

    Returns:
        (matplotlib.axes.Axes): axes for plotting
    """

    # Create axis, always in world coordinates
    wcs = WCS(header)
    viewport = [0.1, 0.1, 0.8, 0.8]
    ax = fig.add_axes(viewport, projection=wcs, label='wcs')
    ax.set_autoscale_on(False)
    ra = ax.coords['ra']
    dec = ax.coords['dec']

    # Set pixel limits on axis
    ax.set_xlim(pix_limits.xi - 0.5, pix_limits.xf - 0.5)
    ax.set_ylim(pix_limits.yi - 0.5, pix_limits.yf - 0.5)

    # Set labels
    ra.set_axislabel('Right Ascension (deg)')
    dec.set_axislabel('Declination (deg)')

    # Set x/y-axis unit formatter
    if 0.03 < limits.lx <= 0.3:
        ra.set_major_formatter('d.dd')
    elif limits.lx <= 0.03:
        ra.set_major_formatter('d.ddd')
    else:
        ra.set_major_formatter('d.d')
    if 0.03 < limits.ly <= 0.3:
        dec.set_major_formatter('d.dd')
    elif limits.ly <= 0.03:
        dec.set_major_formatter('d.ddd')
    else:
        dec.set_major_formatter('d.d')

    # max number of ticks
    num = 8
    if limits.lx > limits.ly:
        ra.set_ticks(number=num)
        dec.set_ticks(number=np.ceil(num * limits.ly / limits.lx))
    elif limits.lx < limits.ly:
        ra.set_ticks(number=np.ceil(num * limits.lx / limits.ly))
        dec.set_ticks(number=num)
    else:
        ra.set_ticks(number=num)
        dec.set_ticks(number=num)

    return ax

def check_limits(limits):
    """
    Confirms limits are reasonable

    Args:
        limits (optional list(float)): list of four plot limits [xi, xf, yi, yf], defaults to
            full file

    Raises:
        Exception if the limits would cause errors in plotting code
    """
    if len(limits) != 4:
        raise Exception("limits must be length 4 array [x_i, x_f, y_i, y_f]")
    elif limits[0] < limits[1]:
        raise Exception("x_i, x_f are in left handed right ascension coordinates, so x_i > x_f")
    elif limits[2] > limits[3]:
        raise Exception("y_i, y_f are in declination coordinates, so y_i < y_f")

reason: import numpy,from astropy


text:
import numpy as np

from layers import (
    FullyConnectedLayer, ReLULayer,
    ConvolutionalLayer, MaxPoolingLayer, Flattener,
    softmax_with_cross_entropy, l2_regularization, softmax
    )

class ConvNet:
    """
    Implements a very simple conv net

    Input -> Conv[3x3] -> Relu -> Maxpool[4x4] ->
    Conv[3x3] -> Relu -> MaxPool[4x4] ->
    Flatten -> FC -> Softmax
    """
    def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels):
        """
        Initializes the neural network

        Arguments:
        input_shape, tuple of 3 ints - image_width, image_height, n_channels
                                         Will be equal to (32, 32, 3)
        n_output_classes, int - number of classes to predict
        conv1_channels, int - number of filters in the 1st conv layer
        conv2_channels, int - number of filters in the 2nd conv layer
        """
        # TODO Create necessary layers
        #raise Exception("Not implemented!")

        image_width, image_height, image_channels = input_shape

        maxpool1_size = 4
        maxpool2_size = 4

        flattener_width = int(image_width / (maxpool1_size * maxpool2_size))
        flattener_height = int(image_width / (maxpool1_size * maxpool2_size))

        self.layers = [
            ConvolutionalLayer(in_channels=image_channels, out_channels=conv1_channels, filter_size=3, padding=1),
            ReLULayer(),
            MaxPoolingLayer(maxpool1_size, maxpool1_size),

            ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1),
            ReLULayer(),
            MaxPoolingLayer(maxpool2_size, maxpool2_size),

            Flattener(),
            FullyConnectedLayer(flattener_width * flattener_height * conv2_channels, n_output_classes)
        ]

    def compute_loss_and_gradients(self, X, y):
        """
        Computes total loss and updates parameter gradients on a batch of training examples
        Arguments:
          :param X, np array (batch_size, height, width, input_features) - input data
          :param y, np array of int (batch_size) - classes
        """

        assert X.ndim == 4
        assert y.ndim == 1
        assert X.shape[0] == y.shape[0]

        for _, param in self.params().items():
            param.reset_grad()

        # forward pass
        out = X
        for layer in self.layers:
            out = layer.forward(out)

        # backward pass
        loss, d_out = softmax_with_cross_entropy(out, y)
        for layer in reversed(self.layers):
            d_out = layer.backward(d_out)

        return loss

    def predict(self, X):
        # forward pass
        out = X
        for layer in self.layers:
            out = layer.forward(out)
        out = softmax(out)

        pred = np.argmax(out, axis=1)
        return pred  # y_hat

    def params(self):
        result = {}
        for index, layer in enumerate(self.layers):
            for name, param in layer.params().items():
                result['%s_%s' % (index, name)] = param

        return result


text:
import numpy as np
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

from Peak import Peak
from fitpeaks import ALLOW_NEGATIVE_PEAKS
from helper_functions import gaussian_width_fwhm

class CustomToolbar(NavigationToolbar):
    def __init__(self, figure_canvas, parent=None):
        self.Window = parent
        self.toolitems = (
            ('Home', 'Reset original view', 'home', 'home'),
            ('Back', 'Back to  previous view', 'back', 'back'),
            ('Forward', 'Forward to next view', 'forward', 'forward'),
            (None, None, None, None),
            ('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'),
            ('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
            ('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
            (None, None, None, None),
            ('Save', 'Save the figure', 'filesave', 'save_figure'),
            (None, None, None, None),
            ('Add Peak', 'Add peak manually', 'add_peak', 'add_peak_tool'),
        )
        NavigationToolbar.__init__(self, figure_canvas, parent=None)
        self._actions['add_peak_tool'].setCheckable(True)

    def add_peak_tool(self):
        if self._active == 'ADD':
            self._active = None
        else:
            self._active = 'ADD'
        if self._idPress is not None:
            self._idPress = self.canvas.mpl_disconnect(self._idPress)
            self.mode = ''

        if self._idRelease is not None:
            self._idRelease = self.canvas.mpl_disconnect(self._idRelease)
            self.mode = ''
        if self._active:
            self._idPress = self.canvas.mpl_connect('button_press_event', self.press_add_peak)
            self._idRelease = self.canvas.mpl_connect('button_release_event', self.release_add_peak)
            self.mode = 'add_peak_tool'
            self.canvas.widgetlock(self)
        else:
            self.canvas.widgetlock.release(self)

        self.set_message(self.mode)
        self._update_buttons_checked()

    def _update_buttons_checked(self):
        super(CustomToolbar, self)._update_buttons_checked()
        self._actions['add_peak_tool'].setChecked(self._active == 'ADD')

    def press_add_peak(self, event):
        if event.button == 1:
            if event.inaxes is not None:
                self.add_peak_event = event
                x = np.linspace(*self.canvas.figure.axes[0].get_xlim(), 400)
                self.draw_fit_line, = self.canvas.figure.axes[0].plot(x, gaussian_width_fwhm(x, event.xdata, 0,
                                                                                0) + event.ydata * self.Window.stackscale,
                                                                      c='k')
                self.peak_drawing_binding = self.canvas.mpl_connect('motion_notify_event', self.draw_peak_curve)
                self.canvas.draw()

    def release_add_peak(self, event):
        self.canvas.mpl_disconnect(self.peak_drawing_binding)
        fwhm = abs(self.add_peak_event.xdata - event.xdata)
        height = (event.ydata - self.add_peak_event.ydata) * self.Window.stackscale
        area = height * fwhm * 1.0645889
        if not ALLOW_NEGATIVE_PEAKS:
            area = abs(area)
        parameter = self.Window.parameters[np.abs(self.Window.parameters - self.add_peak_event.ydata).argmin()]
        peak = Peak(self.add_peak_event.xdata, area, fwhm, parameter, manual=True)
        self.Window.peaks.append(peak)
        self.draw_fit_line.remove()
        self.Window.plot_peak_positions()
        self.Window.show_initial_parameters()
        self.Window.canvas.draw()

    def draw_peak_curve(self, event):
        x, y = event.xdata, event.ydata
        if event.inaxes:
            dx = abs(x - self.add_peak_event.xdata)
            dy = (y - self.add_peak_event.ydata) * self.Window.stackscale
            if not ALLOW_NEGATIVE_PEAKS:
                dy = abs(dy)
            xstart, xend = self.canvas.figure.axes[0].get_xlim()
            y = gaussian_width_fwhm(np.linspace(xstart, xend, 400), self.add_peak_event.xdata, dy, dx)
            self.draw_fit_line.set_ydata(y + self.add_peak_event.ydata * self.Window.stackscale)
            # plt.draw()
            self.canvas.draw()

max_stars_repo_name: mediocrescientist/PeakFit

max_issues_repo_name: mediocrescientist/PeakFit

max_forks_repo_name: mediocrescientist/PeakFit


text:
#!/usr/bin/env python
'''Testing for tracking.py

@author: Zach Hafen
@contact: zachary.h.hafen@gmail.com
@status: Development
'''

import h5py
from mock import patch
import numpy.testing as npt
import os
import pytest
import shutil
import unittest

import galaxy_dive.galaxy_linker.linker as galaxy_linker
from linefinder import galaxy_link

import linefinder.config as config

########################################################################
# Useful global test variables
########################################################################

gal_linker_kwargs = {
    'length_scale': 'Rvir',

    'redshift': 0.16946003,
    'snum': 500,
    'hubble': 0.70199999999999996,
    'halo_data_dir': './tests/data/ahf_test_data',
    'mtree_halos_index': 600,
    'main_mt_halo_id': 0,
    'mt_length_scale' : 'Rvir',
    'halo_file_tag': 'smooth',

    'galaxy_cut': 0.1,
    'ids_to_return': [
        'gal_id',
        'host_gal_id',
        'mt_gal_id',
        'd_gal',
        'd_other_gal_scaled',
    ],
    'minimum_criteria': 'n_star',
    'minimum_value': 0,
}

ptrack_gal_linker_kwargs = {
    'length_scale': 'Rvir',
    'ids_to_return': [
        'gal_id',
        'host_gal_id',
        'mt_gal_id',
        'd_gal',
        'd_other_gal_scaled',
    ],
    'minimum_criteria': 'n_star',
    'minimum_value': 0,
    'mt_length_scale' : 'Rvir',

    'galaxy_cut': 0.1,

    'halo_data_dir': './tests/data/ahf_test_data',
    'out_dir': './tests/data/tracking_output',
    'tag': 'test',
    'mtree_halos_index': 600,
    'main_mt_halo_id': 0,
}

########################################################################

class TestParticleTrackGalaxyLinker( unittest.TestCase ):

    def setUp( self ):

        # Mock the code version so we don't repeatedly change test data
        patcher = patch( 'galaxy_dive.utils.utilities.get_code_version' )
        self.addCleanup( patcher.stop )
        self.mock_code_version = patcher.start()

        self.originalfile = './tests/data/tracking_output/ptracks_test.hdf5'
        self.savefile = './tests/data/tracking_output/galids_test.hdf5'

        if os.path.isfile( self.savefile ):
            os.system( 'rm {}'.format( self.savefile ) )

    ########################################################################

    def test_find_galaxies_for_particle_tracks( self ):

        particle_track_gal_linker = galaxy_link.ParticleTrackGalaxyLinker(
            **ptrack_gal_linker_kwargs )
        particle_track_gal_linker.find_galaxies_for_particle_tracks()

        f = h5py.File( self.savefile, 'r' )
        g = h5py.File( self.originalfile, 'r' )

        # What we expect (calculated using the positions of the particles in
        # snum 500 )
        particle_positions = g['P'][...][:, -1]
        gal_linker = galaxy_linker.GalaxyLinker(
            particle_positions, **gal_linker_kwargs )
        expected = gal_linker.find_ids()

        # Make the comparison
        for key in expected.keys():
            npt.assert_allclose(
                expected[key], f[key][...][:, -1],
                err_msg='Key {} failed'.format( key ), rtol=1e-4 )

        # What we expect (calculated using the positions of the particles in
        # snum 550 )
        gal_linker_kwargs_copy = dict( gal_linker_kwargs )
        gal_linker_kwargs_copy['snum'] = g['snum'][1]
        gal_linker_kwargs_copy['redshift'] = g['redshift'][1]
        particle_positions = g['P'][...][:, 1]
        gal_linker = galaxy_linker.GalaxyLinker(
            particle_positions, **gal_linker_kwargs_copy )

        # Make the comparison
        expected = gal_linker.find_ids()
        for key in expected.keys():
            npt.assert_allclose(
                expected[key],
                f[key][...][:, 1],
                err_msg="Key '{}' failed".format( key ),
                rtol=1e-4
            )

        # Make sure the main MT halo ID is the one we expect.
        assert f.attrs['main_mt_halo_id'] == 0

        # Make sure we have stored the data parameters too.
        for key in ptrack_gal_linker_kwargs.keys():
            if (key != 'ids_to_return') and (key != 'main_mt_halo_id'):
                assert \
                    ptrack_gal_linker_kwargs[key] == f['parameters'].attrs[key]

########################################################################

# Old test for parallelism
# class TestParticleTrackGalaxyLinkerParallel( unittest.TestCase ):
#
#     def setUp( self ):
#
#         # Mock the code version so we don't repeatedly change test data
#         patcher = patch( 'galaxy_dive.utils.utilities.get_code_version' )
#         self.addCleanup( patcher.stop )
#         self.mock_code_version = patcher.start()
#
#         self.originalfile = './tests/data/tracking_output/ptracks_test.hdf5'
#         self.savefile = './tests/data/tracking_output/galids_test_parallel.hdf5'
#
#         if os.path.isfile( self.savefile ):
#             os.system( 'rm {}'.format( self.savefile ) )
#
#     ########################################################################
#
#     def test_find_galaxies_for_particle_tracks_parallel( self ):
#
#         parallel_kwargs = dict( ptrack_gal_linker_kwargs )
#         parallel_kwargs['ptracks_tag'] = 'test'
#         parallel_kwargs['tag'] = 'test_parallel'
#         parallel_kwargs['n_processors'] = 2
#
#         particle_track_gal_linker = galaxy_link.ParticleTrackGalaxyLinker(
#             **parallel_kwargs )
#         particle_track_gal_linker.find_galaxies_for_particle_tracks()
#
#         expected = \
#             h5py.File( './tests/data/tracking_output/galids_test.hdf5', 'r' )
#         actual = h5py.File( self.savefile, 'r' )
#
#         for key in expected.keys():
#             if key != 'parameters':
#                 npt.assert_allclose( expected[key], actual[key] )

########################################################################

class TestParticleTrackGalaxyLinkerJug( unittest.TestCase ):

    def setUp( self ):

        # Switch dirs to allow running executables (which can't normally be
        # in the package dir)
        os.chdir( '..' )

        self.originalfile = './linefinder/tests/data/tracking_output/ptracks_test.hdf5'
        self.savefile = './linefinder/tests/data/tracking_output/galids_test_jug.hdf5'

        if os.path.isfile( self.savefile ):
            os.system( 'rm {}'.format( self.savefile ) )

    ########################################################################

    def tearDown( self ):

        # Switch back so we don't mess up other tests
        os.chdir( 'linefinder' )

        jugdata_dir = './tests/find_galaxies_for_ptracks_jugfile.jugdata'

        if os.path.isdir( jugdata_dir ):
            shutil.rmtree( jugdata_dir )

    ########################################################################

    @pytest.mark.slow
    def test_find_galaxies_for_particle_tracks_jug( self ):

        os.system( "jug execute ./linefinder/tests/find_galaxies_for_ptracks_jugfile.py &" )
        os.system( "jug execute ./linefinder/tests/find_galaxies_for_ptracks_jugfile.py" )

        expected = \
            h5py.File( './linefinder/tests/data/tracking_output/galids_test.hdf5', 'r' )
        actual = h5py.File( self.savefile, 'r' )

        for key in expected.keys():
            if key != 'parameters':
                npt.assert_allclose( expected[key], actual[key] )

max_stars_repo_name: zhafen/linefinder

max_issues_repo_name: zhafen/linefinder

max_forks_repo_name: zhafen/linefinder


text:
import tensorrt as trt   # tensorRT 7.0
import pycuda.driver as cuda
import pycuda.autoinit

import numpy as np
import cv2
import time
import os

# You can set the logger severity higher to suppress messages (or lower to display more messages).
# TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
TRT_LOGGER = trt.Logger(trt.Logger.ERROR)
EXPLICIT_BATCH = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)  # set batch size

# Simple helper data class that's a little nicer to use than a 2-tuple.
class HostDeviceMem(object):
    def __init__(self, host_mem, device_mem):
        self.host = host_mem
        self.device = device_mem

    def __str__(self):
        return "Host:\n" + str(self.host) + "\nDevice:\n" + str(self.device)

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

class RepVGGTrt(object):
    def __init__(self, onnx_path):
        self.onnx_path = onnx_path
        self.engine = self.build_engine_onnx(onnx_path)
        self.inputs, self.outputs, self.bindings, self.stream = self.allocate_buffers(self.engine)
        self.context = self.engine.create_execution_context()

        self.short_size = 256
        self.dst_w = 224
        self.dst_h = 224
        self.input_size = [self.dst_h, self.dst_w]

        self.long_size = 224

        self.mean = np.array([123.675, 116.28, 103.53])
        self.std = np.array([58.395, 57.12, 57.375])
        self.std_inv = 1 / self.std
        self.img_t = None

        self.result = dict()
        print('Mmclassification Trt Init Done.')

    def GiB(self, val):
        return val * 1 << 30

    def build_engine_onnx(self, model_file):
        with trt.Builder(TRT_LOGGER) as builder, builder.create_network(EXPLICIT_BATCH) as network, trt.OnnxParser(
                network, TRT_LOGGER) as parser:
            builder.max_workspace_size = self.GiB(1)
            builder.max_batch_size = 1
            # Load the Onnx model and parse it in order to populate the TensorRT network.
            with open(model_file, 'rb') as model:
                if not parser.parse(model.read()):
                    print('ERROR: Failed to parse the ONNX file.')
                    for error in range(parser.num_errors):
                        print(parser.get_error(error))
                    return None
            return builder.build_cuda_engine(network)

    def allocate_buffers(self, engine):
        inputs = []
        outputs = []
        bindings = []
        stream = cuda.Stream()

        for binding in engine:
            bindig_shape = tuple(engine.get_binding_shape(binding))
            # size = trt.volume(engine.get_binding_shape(binding)) * engine.max_batch_size  # engine.max_batch_size
            dtype = trt.nptype(engine.get_binding_dtype(binding))
            host_mem = cuda.pagelocked_empty(bindig_shape, dtype)
            # print('\tAllocate host buffer: host_mem -> {}, {}'.format(host_mem, host_mem.nbytes))  # host mem

            device_mem = cuda.mem_alloc(host_mem.nbytes)
            # print('\tAllocate device buffer: device_mem -> {}, {}'.format(device_mem, int(device_mem))) # device mem

            # print('\t# Append the device buffer to device bindings.......')
            bindings.append(int(device_mem))
            # print('\tbindings: ', bindings)

            # Append to the appropriate list.
            if engine.binding_is_input(binding):
                # print("this is the input!")
                # print('____HostDeviceMem(host_mem, device_mem)): {}, {}'.format(HostDeviceMem(host_mem, device_mem),type(HostDeviceMem(host_mem, device_mem))))
                inputs.append(HostDeviceMem(host_mem, device_mem))
            else:
                # print("This is the output!")
                outputs.append(HostDeviceMem(host_mem, device_mem))
            # print("----------------------end allocating one binding in the onnx model-------------------------")

        return inputs, outputs, bindings, stream

    def do_inference(self, context, bindings, inputs, outputs, stream, batch_size=1):
        # Transfer input data to the GPU.
        [cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
        # Run inference.
        context.execute_async(batch_size=batch_size, bindings=bindings, stream_handle=stream.handle)
        # Transfer predictions back from the GPU.
        [cuda.memcpy_dtoh_async(out.host, out.device, stream) for out in outputs]
        # Synchronize the stream
        stream.synchronize()
        # Return only the host outputs.
        return [out.host for out in outputs]

    def crop_img_long_size(self, cv_img):
        long_size = max(cv_img.shape[:2])

        pad_h = (long_size - cv_img.shape[0]) // 2
        pad_w = (long_size - cv_img.shape[1]) // 2
        img_input = np.ones((long_size, long_size, 3), dtype=np.uint8) * 0
        img_input[pad_h:cv_img.shape[0] + pad_h, pad_w:cv_img.shape[1] + pad_w, :] = cv_img
        img_input = cv2.resize(img_input, (self.input_size[1], self.input_size[0]), cv2.INTER_LINEAR)

        return img_input

    def crop_img_short_size(self, cv_img):
        # resize the short size
        h, w, _ = cv_img.shape
        if h >= w:
            h = int(h * self.short_size / w)
            w = int(self.short_size)
        else:
            w = int(w * self.short_size / h)
            h = int(self.short_size)

        cv_img = cv2.resize(cv_img, (w, h), cv2.INTER_LINEAR)

        # center crop
        y1 = max(0, int(round((h - self.input_size[1]) / 2.)))
        x1 = max(0, int(round((w - self.input_size[0]) / 2.)))
        y2 = min(h-1, y1 + self.input_size[1])
        x2 = min(w-1, x1 + self.input_size[0])

        cv_img = cv_img[y1:y2, x1:x2, :]
        return cv_img

    def padding_short_size(self, cv_img):
        ignore_resize = False
        long_side = max(self.input_size)

        w, h, _ = cv_img.shape
        if (w >= h and w == long_side) or (h >= w and h == long_side):
            ignore_resize = True
        else:
            if w > h:
                width = long_side
                height = int(long_side * h / w)
            else:
                height = long_side
                width = int(long_side * w / h)

        if not ignore_resize:
            cv_img = cv2.resize(cv_img, (height, width), cv2.INTER_LINEAR)

        long_size = max(cv_img.shape[:2])
        pad_h = (long_size - cv_img.shape[1]) // 2
        pad_w = (long_size - cv_img.shape[0]) // 2

        pad_t = pad_h
        pad_d = pad_h

        pad_l = pad_w
        pad_r = pad_w

        if (long_size - cv_img.shape[0]) % 2 != 0:
            pad_l = pad_l + 1
        if (long_size - cv_img.shape[1]) % 2 != 0:
            pad_t = pad_t + 1

        img_crop_padding = cv2.copyMakeBorder(cv_img,  pad_l, pad_r, pad_t, pad_d, cv2.BORDER_CONSTANT, value=[0, 0, 0])
        return img_crop_padding

    def onnx_input_process(self, cv_img):
        # cv_img = self.crop_img_short_size(cv_img)
        # cv_img = self.crop_img_long_size(cv_img)
        cv_img = self.padding_short_size(cv_img)

        assert list(cv_img.shape[:2]) == self.input_size

        # normalize
        cv_img = cv_img.copy().astype(np.float32)
        self.mean = np.float64(self.mean.reshape(1, -1))
        self.std_inv = 1 / np.float64(self.std.reshape(1, -1))
        if True:
            cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB, cv_img)  # inplace
        cv2.subtract(cv_img, self.mean, cv_img)  # inplace
        cv2.multiply(cv_img, self.std_inv, cv_img)  # inplace

        self.img_t = cv_img.transpose(2, 0, 1)  # to C, H, W
        self.img_t = np.ascontiguousarray(self.img_t)

        self.img_t = np.expand_dims(self.img_t, axis=0)
        return self.img_t

    def trt_cv_img_preprocess(self, img_src, pagelocked_buffer):
        # Converts the input image to a CHW Numpy array
        trt_inputs = self.onnx_input_process(img_src)
        np.copyto(pagelocked_buffer, trt_inputs)
        return img_src

    def infer_cv_img(self, cv_img):
        self.trt_cv_img_preprocess(cv_img, self.inputs[0].host)
        trt_outputs = self.do_inference(self.context, bindings=self.bindings, inputs=self.inputs, outputs=self.outputs, stream=self.stream)
        for trt_output in trt_outputs:
            # scale back to origin img

            pred_score = np.max(trt_output, axis=1)[0]
            pred_label = np.argmax(trt_output, axis=1)[0]
            self.result.update({'pred_label': pred_label, 'pred_score': float(pred_score)})

        return self.result

def repvgg_cls_images_infer(images_dir, onnx_path):
    mm_classifier_trt = RepVGGTrt(onnx_path)
    image_names = [f for f in os.listdir(images_dir) if os.path.splitext(f)[-1] in ['.jpg', 'png']]
    for image_name in image_names:
        img_path = os.path.join(images_dir, image_name)
        img = cv2.imread(img_path)
        cls_result = mm_classifier_trt.infer_cv_img(img)
        print(cls_result)
        # print(image_net.CLASSES[cls_result['pred_label']])
        cv2.namedWindow("img", 0)
        cv2.imshow("img", img)
        if cls_result['pred_label'] != 1:
            # dst_img_path = img_path.replace(images_dir, check_dir)
            # shutil.copy(img_path, dst_img_path)
            wait_key = cv2.waitKey(0)
            if wait_key == 27:
                break

if __name__ == "__main__":
    onnx_path = "/home/liyongjing/Egolee_2021/programs/RepVGG-main/trained_model/RepVggB1-padding-short-size/RepVGG-B1-deploy_sim.onnx"
    images_dir = '/home/liyongjing/Egolee_2021/data/TrainData/train_fall_down/rep_vgg_format/val/person_normal'
    repvgg_cls_images_infer(images_dir, onnx_path)

max_stars_repo_name: yongjingli/RepVGG

max_issues_repo_name: yongjingli/RepVGG

max_forks_repo_name: yongjingli/RepVGG

reason: import numpy,import pycuda