text:
import unittest

import numpy as np
import scipy.sparse
from sklearn.utils.testing import assert_array_almost_equal

from ParamSklearn.implementations.OneHotEncoder import OneHotEncoder

dense1 = np.array([[0, 1, 0],
                   [0, 0, 0],
                   [1, 1, 0]])
dense1_1h = np.array([[1, 0, 0, 1, 1],
                     [1, 0, 1, 0, 1],
                     [0, 1, 0, 1, 1]])
dense1_1h_minimum_fraction = np.array([[0, 1, 0, 1, 1],
                                       [0, 1, 1, 0, 1],
                                       [1, 0, 0, 1, 1]])

# Including NaNs
dense2 = np.array([[0, np.NaN, 0],
                   [np.NaN, 0, 2],
                   [1, 1, 1],
                   [np.NaN, 0, 1]])
dense2_1h = np.array([[0, 1, 0, 1, 0, 0, 1, 0, 0],
                      [1, 0, 0, 0, 1, 0, 0, 0, 1],
                      [0, 0, 1, 0, 0, 1, 0, 1, 0],
                      [1, 0, 0, 0, 1, 0, 0, 1, 0]])

dense2_1h_minimum_fraction = np.array([[1, 0, 1, 0, 1, 0],
                                       [0, 1, 0, 1, 1, 0],
                                       [1, 0, 1, 0, 0, 1],
                                       [0, 1, 0, 1, 0, 1]])

dense2_partial_1h = np.array([[0., 1., 0., 1., 0., 0., 0.],
                              [1., 0., 0., 0., 1., 0., 2.],
                              [0., 0., 1., 0., 0., 1., 1.],
                              [1., 0., 0., 0., 1., 0., 1.]])

dense2_1h_minimum_fraction_as_sparse = np.array([[0, 0, 1, 0, 0, 0],
                                                 [0, 1, 0, 0, 1, 0],
                                                 [1, 0, 0, 1, 0, 1],
                                                 [0, 1, 0, 0, 0, 1]])

# All NaN slice
dense3 = np.array([[0, 1, np.NaN],
                   [1, 0, np.NaN]])
dense3_1h = np.array([[1, 0, 0, 1, 1],
                      [0, 1, 1, 0, 1]])

sparse1 = scipy.sparse.csc_matrix(([3, 2, 1, 1, 2, 3],
                                   ((1, 4, 5, 2, 3, 5),
                                    (0, 0, 0, 1, 1, 1))), shape=(6, 2))
sparse1_1h = scipy.sparse.csc_matrix(([1, 1, 1, 1, 1, 1],
                                      ((5, 4, 1, 2, 3, 5),
                                       (0, 1, 2, 3, 4, 5))), shape=(6, 6))
sparse1_paratial_1h = scipy.sparse.csc_matrix(([1, 1, 1, 1, 2, 3],
                                               ((5, 4, 1, 2, 3, 5),
                                                (0, 1, 2, 3, 3, 3))),
                                              shape=(6, 4))

# All zeros slice
sparse2 = scipy.sparse.csc_matrix(([2, 1, 0, 0, 0, 0],
                                   ((1, 4, 5, 2, 3, 5),
                                    (0, 0, 0, 1, 1, 1))), shape=(6, 2))
sparse2_1h = scipy.sparse.csc_matrix(([1, 1, 1, 1, 1, 1],
                                      ((5, 4, 1, 2, 3, 5),
                                       (0, 1, 2, 3, 3, 3))), shape=(6, 4))

sparse2_csr = scipy.sparse.csr_matrix(([2, 1, 0, 0, 0, 0],
                                      ((1, 4, 5, 2, 3, 5),
                                       (0, 0, 0, 1, 1, 1))), shape=(6, 2))
sparse2_csr_1h = scipy.sparse.csr_matrix(([1, 1, 1, 1, 1, 1],
                                         ((5, 4, 1, 2, 3, 5),
                                          (0, 1, 2, 3, 3, 3))), shape=(6, 4))

class OneHotEncoderTest(unittest.TestCase):
    def test_dense1(self):
        self.fit_then_transform(dense1_1h, dense1)
        self.fit_then_transform_dense(dense1_1h, dense1)

    def test_dense1_minimum_fraction(self):
        self.fit_then_transform(dense1_1h_minimum_fraction, dense1, minimum_fraction=0.5)
        self.fit_then_transform_dense(dense1_1h_minimum_fraction, dense1, minimum_fraction=0.5)

    def test_dense2(self):
        self.fit_then_transform(dense2_1h, dense2)
        self.fit_then_transform_dense(dense2_1h, dense2)

    def test_dense2_minimum_fraction(self):
        self.fit_then_transform(dense2_1h_minimum_fraction, dense2,
                                minimum_fraction=0.3)
        self.fit_then_transform_dense(dense2_1h_minimum_fraction, dense2,
                                      minimum_fraction=0.3)

    def test_dense2_with_non_sparse_components(self):
        self.fit_then_transform(dense2_partial_1h, dense2,
                                categorical_features=[True, True, False])
        self.fit_then_transform_dense(dense2_partial_1h, dense2,
                                      categorical_features=[True, True, False])

    # Minimum fraction is not too interesting here...
    def test_dense3(self):
        self.fit_then_transform(dense3_1h, dense3)
        self.fit_then_transform_dense(dense3_1h, dense3)

    def test_sparse1(self):
        self.fit_then_transform(sparse1_1h.todense(), sparse1)
        self.fit_then_transform_dense(sparse1_1h.todense(), sparse1)

    def test_sparse1_minimum_fraction(self):
        expected = np.array([[0, 1, 0, 0, 1, 1],
                             [0, 0, 1, 1, 0, 1]], dtype=float).transpose()
        self.fit_then_transform(expected, sparse1,
                                minimum_fraction=0.5)
        self.fit_then_transform_dense(expected, sparse1,
                                      minimum_fraction=0.5)

    def test_sparse1_with_non_sparse_components(self):
        self.fit_then_transform(sparse1_paratial_1h.todense(), sparse1,
                                categorical_features=[True, False])
        # This test does not apply here. The sparse matrix will be cut into a
        #  continouos and a categorical part, after one hot encoding only the
        #  categorical part is an array, the continuous part will still be a
        # sparse matrix. Therefore, the OHE will only return a sparse matrix
        #self.fit_then_transform_dense(sparse1_paratial_1h.todense(), sparse1,
        #                              categorical_features=[True, False])

    def test_sparse2(self):
        self.fit_then_transform(sparse2_1h.todense(), sparse2)
        self.fit_then_transform_dense(sparse2_1h.todense(), sparse2)

    def test_sparse2_minimum_fraction(self):
        expected = np.array([[0, 1, 0, 0, 1, 1],
                             [0, 0, 1, 1, 0, 1]], dtype=float).transpose()
        self.fit_then_transform(expected, sparse2,
                                minimum_fraction=0.5)
        self.fit_then_transform_dense(expected, sparse2,
                                      minimum_fraction=0.5)

    def test_sparse2_csr(self):
        self.fit_then_transform(sparse2_csr_1h.todense(), sparse2_csr)
        self.fit_then_transform_dense(sparse2_csr_1h.todense(), sparse2_csr)

    def test_sparse_on_dense2_minimum_fraction(self):
        sparse = scipy.sparse.csr_matrix(dense2)
        self.fit_then_transform(dense2_1h_minimum_fraction_as_sparse, sparse,
                                minimum_fraction=0.5)
        self.fit_then_transform_dense(dense2_1h_minimum_fraction_as_sparse, sparse,
                                      minimum_fraction=0.5)

    def fit_then_transform(self, expected, input, categorical_features='all',
                           minimum_fraction=None):
        # Test fit_transform
        ohe = OneHotEncoder(categorical_features=categorical_features,
                            minimum_fraction=minimum_fraction)
        transformation = ohe.fit_transform(input.copy())
        self.assertIsInstance(transformation, scipy.sparse.csr_matrix)
        assert_array_almost_equal(expected.astype(float),
                                  transformation.todense())

        # Test fit, and afterwards transform
        ohe2 = OneHotEncoder(categorical_features=categorical_features,
                             minimum_fraction=minimum_fraction)
        ohe2.fit(input.copy())
        transformation = ohe2.transform(input.copy())
        self.assertIsInstance(transformation, scipy.sparse.csr_matrix)
        assert_array_almost_equal(expected, transformation.todense())

    def fit_then_transform_dense(self, expected, input,
                                 categorical_features='all',
                                 minimum_fraction=None):
        ohe = OneHotEncoder(categorical_features=categorical_features,
                            sparse=False, minimum_fraction=minimum_fraction)
        transformation = ohe.fit_transform(input.copy())
        self.assertIsInstance(transformation, np.ndarray)
        assert_array_almost_equal(expected, transformation)

        ohe2 = OneHotEncoder(categorical_features=categorical_features,
                             sparse=False, minimum_fraction=minimum_fraction)
        ohe2.fit(input.copy())
        transformation = ohe2.transform(input.copy())
        self.assertIsInstance(transformation, np.ndarray)
        assert_array_almost_equal(expected, transformation)

    def test_transform_with_unknown_value(self):
        input = np.array(((0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5))).transpose()
        ohe = OneHotEncoder()
        ohe.fit(input)
        test_data = np.array(((0, 1, 2, 6), (0, 1, 6, 7))).transpose()
        output = ohe.transform(test_data).todense()
        self.assertEqual(5, np.sum(output))

        input = np.array(((0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5))).transpose()
        ips = scipy.sparse.csr_matrix(input)
        ohe = OneHotEncoder()
        ohe.fit(ips)
        test_data = np.array(((0, 1, 2, 6), (0, 1, 6, 7))).transpose()
        tds = scipy.sparse.csr_matrix(test_data)
        output = ohe.transform(tds).todense()
        self.assertEqual(3, np.sum(output))

max_stars_repo_name: automl/paramsklearn

max_issues_repo_name: automl/paramsklearn

max_forks_repo_name: automl/paramsklearn

reason: import numpy,import scipy


text:
from gbf_raid_bot.capture.region import Region
from gbf_raid_bot.utilities import ROOT_DIR

import numpy as np
import os
import cv2

class TemplateImage:
	def __init__(self, filename, process_img=None, scale=1.0):
		path = os.path.join(ROOT_DIR, filename)
		if not os.path.isfile(path):
			raise FileNotFoundError(path)

		img = cv2.imread(path, cv2.IMREAD_COLOR)
		if process_img is not None:
			img = process_img(img)
		self.filename = filename
		self.image = img
		self.scale = scale
		self.width, self.height = img.shape[:2][::-1]

	def match(self, img, threshold=0.8):
		res = cv2.matchTemplate(img, self.image, cv2.TM_CCOEFF_NORMED)
		loc = np.where(res >= threshold)
		zipped = zip(*loc[::-1])

		result = []
		for pt in zipped:
			region = Region(
				x=int(pt[0] / self.scale),
				y=int(pt[1] / self.scale),
				w=int(self.width / self.scale),
				h=int(self.height / self.scale)
			)
			result.append(region)

		return result

class Template:
	def __init__(self, filenames, name, process_img=None, scale=1.0):
		self.images = []
		self.name = name
		self.process_img = process_img
		self.scale = scale

		if isinstance(filenames, list):
			self.loadMultipleFiles(filenames)
		else:
			self.loadSingleFile(filenames)

	def loadMultipleFiles(self, filenames):
		for filename in filenames:
			self.loadSingleFile(filename)

	def loadSingleFile(self, filename):
		self.images.append(TemplateImage(filename, self.process_img, self.scale))

	def match(self, img, threshold=0.8):
		result = []
		for image in self.images:
			result.extend(image.match(img, threshold))
		return result

class TemplateList:
	def __init__(self, templates={}, process_img=None, scale=1.0):
		self.scale = scale
		self.process_img = process_img
		self.templates = {}
		for item in templates.items():
			self.put(*item)

	def get(self, name):
		return self.templates[name];

	def put(self, name, filenames):
		self.templates[name] = Template(filenames, name, self.process_img, self.scale)

	def match(self, img, threshold=0.8, features=None):
		result = {}
		if features is None:
			features = self.templates.keys()

		for name in features:
			template = self.templates[name]
			result[name] = template.match(img, threshold)

		return result

	def __getitem__(self, key):
		return self.templates[key]

	def __setitem__(self, key, item):
		self.templates[key] = item

	def __iter__(self):
		return self.templates.__iter__()

class TemplateMatcher:
	def __init__(self, templateList, image, threshold=0.8):
		self.templateList = templateList
		self.image = image
		self.threshold = threshold
		self.cache = {}

	def get(self, name):
		if name in self.cache:
			return self.cache[name]
		matches = self.templateList[name].match(self.image, self.threshold)
		self.cache[name] = matches
		return matches

	def some(self, names):
		result = {}
		for name in names:
			result[name] = self.get(name)
		return result

	def items(self):
		return self.cache.items()

	def __getitem__(self, key):
		return self.get(key)

ext: py

reason: import numpy


text:
from typing import Tuple, List

import numpy as np
from GPy.kern import Kern

from src.autoks.backend.kernel import create_1d_kernel
from src.autoks.core.covariance import Covariance
from src.datasets.dataset import Dataset

class KnownGPDataset(Dataset):

    def __init__(self,
                 covariance: Covariance,
                 noise_var: float,
                 n_pts: int = 100):
        super().__init__()
        self.covariance = covariance
        self.noise_var = noise_var
        self.n_pts = n_pts

    def load_or_generate_data(self) -> None:
        self.x, self.y = sample_gp(self.covariance.raw_kernel, self.n_pts, self.noise_var)

    def snr(self):
        # assume signal variance always = 1
        signal_variance = 1
        return signal_variance / self.noise_var

    def __repr__(self):
        return f'{self.__class__.__name__}('f'kernel={self.covariance.infix_full!r}, n=' \
            f'{self.n_pts!r}, SNR={self.snr() !r})'

def sample_gp(kernel: Kern,
              n_pts: int = 500,
              noise_var: float = 1) -> Tuple[np.ndarray, np.ndarray]:
    """Sample paths from a GP.

    :param kernel: the kernel from which to draw samples
    :param n_pts: number of data points
    :param noise_var: variance of additive gaussian noise
    :return:
    """
    x = np.random.uniform(0., 1., (n_pts, kernel.input_dim))

    # zero-mean
    prior_mean = np.zeros(n_pts)
    prior_cov = kernel.K(x, x)

    # Generate a sample path
    f_true = np.random.multivariate_normal(prior_mean, prior_cov)
    f_true = f_true[:, None]

    # additive Gaussian noise
    gaussian_noise_mean = 0
    gaussian_noise_std = np.sqrt(noise_var)
    gaussian_noise = np.random.normal(gaussian_noise_mean, gaussian_noise_std, (n_pts, 1))
    y = f_true + gaussian_noise

    return x, y

def cks_known_kernels() -> List[Kern]:
    """Duvenaud, et al., 2013 (Table 1)"""
    se1 = create_1d_kernel('SE', 0)
    se2 = create_1d_kernel('SE', 1)
    se3 = create_1d_kernel('SE', 2)
    se4 = create_1d_kernel('SE', 3)
    rq1 = create_1d_kernel('RQ', 0)
    rq2 = create_1d_kernel('RQ', 1)
    per1 = create_1d_kernel('PER', 0)
    lin1 = create_1d_kernel('LIN', 0)

    true_kernels = [se1 + rq1, lin1 * per1, se1 + rq2, se1 + se2 * per1 + se3,
                    se1 * se2, se1 * se2 + se2 * se3, (se1 + se2) * (se3 + se4)]
    return true_kernels


text:
"""
Glossário:

SCG = sistema de coordenadas global
SCL = sistema de coordenadas local

K = matriz de rigidez do elemento no SCG
k = matriz de rigidez do elemento no SCL

v = deslocamentos nodais do elemento no SCG
u = deslocamento nodais do elemento no SCL

F = forças nodais do elemento no SCG
Q = forças nodais do elemento no SCL

P = forças globais da estrutura
S = matriz de rigidez global da estrutura
d = deslocamentos globais nodais
"""

# Importação das bibliotecas utilizadas
import pathlib
import numpy as np
import matplotlib.pyplot as plt

class Truss_2D:
    def __init__(self, input_file):
        """
        Método construtor da classe. Lê o arquivo de entrada e monta/calcula as
        matrizes necessárias para a análise.

        Parâmetros

            [input_file]: Caminho do arquivo com os dados de entrada.
        """
        self.file_name = input_file
        # Leitura do arquivo de entrada
        with open(input_file, "r", encoding="utf-8") as inp_file:
            lines = inp_file.read().split("\n")

        # Armazena a quantidade de nós
        self.num_nodes = int(lines.pop(0))
        self.matrix_node_coords = np.zeros([self.num_nodes, 2])
        # Armazena as coordenadas dos nós
        for node in range(self.num_nodes):
            x, y = lines[node].split(",")
            self.matrix_node_coords[node, 0] = float(x)
            self.matrix_node_coords[node, 1] = float(y)
        # Exclui os dados já armazenados da lista
        lines = list(filter(lambda line: lines.index(
            line) >= self.num_nodes, lines))

        # Armazena a quantidade de apoios
        self.num_supports = int(lines.pop(0))
        self.matrix_supports = np.zeros([self.num_supports, 3], dtype=int)
        # Armazena os dados dos apoios (suportes)
        for support in range(self.num_supports):
            node, x_restrained, y_restrained = lines[support].split(",")
            self.matrix_supports[support, 0] = int(node) - 1
            self.matrix_supports[support, 1] = int(x_restrained)
            self.matrix_supports[support, 2] = int(y_restrained)
        # Exclui os dados já armazenados da lista
        lines = list(filter(lambda line: lines.index(
            line) >= self.num_supports, lines))

        # Armazena a quantidade de módulos de elasticidade longitudinal
        self.num_E = int(lines.pop(0))
        self.matrix_E = np.zeros([self.num_E])
        # Armazena os valores dos módulos de elasticidade longitudinal
        for modulus in range(self.num_E):
            self.matrix_E[modulus] = float(lines[modulus])
        # Exclui os dados já armazenados da lista
        lines = list(
            filter(lambda line: lines.index(line) >= self.num_E, lines))

        # Armazena a quantidade de áreas de seção transversal
        self.num_areas = int(lines.pop(0))
        self.matrix_areas = np.zeros([self.num_areas])
        # Armazena os valores das áreas
        for area in range(self.num_areas):
            self.matrix_areas[area] = float(lines[area])
        # Exclui os dados já armazenados da lista
        lines = list(filter(lambda line: lines.index(
            line) >= self.num_areas, lines))

        # Armazena o número de barras da treliça
        self.num_members = int(lines.pop(0))
        self.matrix_members = np.zeros([self.num_members, 4], dtype=int)
        # Armazena as informações das barras
        for member in range(self.num_members):
            initial_node, final_node, e_type, area_type = lines[member].split(
                ",")
            self.matrix_members[member, 0] = int(initial_node) - 1
            self.matrix_members[member, 1] = int(final_node) - 1
            self.matrix_members[member, 2] = int(e_type) - 1
            self.matrix_members[member, 3] = int(area_type) - 1
        # Exclui os dados já armazenados da lista
        lines = list(filter(lambda line: lines.index(
            line) >= self.num_members, lines))

        # Armazena o número de forças aplicadas
        self.num_forces = int(lines.pop(0))
        self.matrix_forces_location = np.zeros([self.num_forces], dtype=int)
        self.matrix_forces_magnitude = np.zeros([self.num_forces, 2])
        # Armazena os nós em que as forças são aplicadas e as magnitudes das forças
        for force in range(self.num_forces):
            node, force_x, force_y = lines[force].split(",")
            self.matrix_forces_location[force] = int(node) - 1
            self.matrix_forces_magnitude[force, 0] = float(force_x)
            self.matrix_forces_magnitude[force, 1] = float(force_y)

        # Determinação das matrizes necessárias para a análise
        self.get_structure_coord_numbers()
        self.calculate_structure_stiffness_matrix()
        self.assembly_node_load_vector()

    def get_structure_coord_numbers(self):
        """
        Monta a matriz com os números associados às coordenadas
        dos nós da estrutura.
        """

        # Montagem da matriz coluna dos números das coordenadas
        self.structure_coord_numbers = np.zeros([2*self.num_nodes], dtype=int)

        # Percorre os nós da estrutura
        for i in range(self.num_nodes):
            for j in range(2):
                self.structure_coord_numbers[2*i + j] = 2*i + j

    def calculate_structure_stiffness_matrix(self):
        """Calcula a matriz de rigidez da ESTRUTURA."""

        # Valores auxiliares
        matrix_members = self.matrix_members

        # Gerando as matrizes com zeros
        K = np.zeros([4, 4])
        S = np.zeros([2 * self.num_nodes, 2 * self.num_nodes])

        # Percorrendo todas as barras da estrutura
        for member in matrix_members:
            # Informações das barras
            # node_i = nó inicial da barra
            # node_f = nó final da barra
            node_i = member[0]
            node_f = member[1]
            material_type = member[2]
            cross_section_type = member[3]

            # Dados obtidos após a leitura da matriz de membros
            E = self.matrix_E[material_type]
            A = self.matrix_areas[cross_section_type]
            x_i = self.matrix_node_coords[node_i, 0]
            y_i = self.matrix_node_coords[node_i, 1]
            x_f = self.matrix_node_coords[node_f, 0]
            y_f = self.matrix_node_coords[node_f, 1]

            # Cálculo do comprimento da barra
            L = np.sqrt((x_f-x_i)**2 + (y_f-y_i)**2)
            # cs = cos e sn = sen
            cs = (x_f-x_i) / L
            sn = (y_f-y_i) / L

            # Montagem da matriz de rigidez do elemento no SCG (triplo produto matricial)
            self.assembly_K(E, A, L, cs, sn, K)
            # Montagem da matriz de rigidez da estrutura excluindo as coordenadas restringidas
            self.assembly_S(node_i, node_f, K, S)

        # Atribuição da matriz de rigidez da estrutura como atributo do objeto
        self.structure_stiffness_matrix = S

    def assembly_K(self, E, A, L, cs, sn, K):
        """
        Preenche a matriz de rigidez do ELEMENTO (K) a partir dos parâmetros
        recebidos e efetuando o triplo produto matricial

        Parâmetros:

            [E]: Módulo de elasticidade longitudinal
            [A]: Área da seção transversal da barra
            [L]: Comprimento da barra
            [cs]: Cosseno (matriz de rotação)
            [sn]: Seno (matriz de rotação)
            [K]: Matriz de rigidez do elemento a ser preenchida
        """
        axial_stiffness = E * A / (L)
        # z1, z2 e z3 representam os produtos entre a rigidez axial e os senos e cossenos
        z1 = axial_stiffness * cs ** 2
        z2 = axial_stiffness * sn ** 2
        z3 = axial_stiffness * cs * sn
        K[0, 0] = z1
        K[1, 0] = z3
        K[2, 0] = -z1
        K[3, 0] = -z3
        K[0, 1] = z3
        K[1, 1] = z2
        K[2, 1] = -z3
        K[3, 1] = -z2
        K[0, 2] = -z1
        K[1, 2] = -z3
        K[2, 2] = z1
        K[3, 2] = z3
        K[0, 3] = -z3
        K[1, 3] = -z2
        K[2, 3] = z3
        K[3, 3] = z2

    def assembly_S(self, node_i, node_f, K, S):
        """
        Preenche a matriz de rigidez da ESTRUTURA excluindo as linhas e colunas
        associadas a coordenadas restringidas pelos apoios.

        Parâmetros:

            [node_i]: Nó inicial do elemento
            [node_f]: Nó final do elemento
            [K]: Matriz de rigidez do elemento no SCG
            [S]: Matriz de rigidez da estrutura a ser preenchida
        """

        # Valores auxiliares
        coord_numbers = self.structure_coord_numbers

        # Percorre as linhas da matriz de rigidez do elemento
        for row_K in range(4):
            # Verifica se a linha está associada ao nó inicial ou final
            if row_K < 2:
                row_code_number_1 = 2 * node_i + row_K
            else:
                row_code_number_1 = 2 * node_f + (row_K - 2)
            row_S = coord_numbers[row_code_number_1]
            # Percorre as colunas da matriz de rigidez do elemento
            for column_K in range(4):
                if column_K < 2:
                    row_code_number_2 = 2 * node_i + column_K
                else:
                    row_code_number_2 = 2 * node_f + (column_K - 2)
                column_S = coord_numbers[row_code_number_2]
                # Adiciona o valor pertinente à matriz de rigidez da estrutura
                S[row_S, column_S] += K[row_K, column_K]

    def assembly_node_load_vector(self):
        """Monta o vetor de carregamentos nodais."""

        # Valores auxiliares
        coord_numbers = self.structure_coord_numbers

        # Preenchendo o vetor desejado com zeros
        self.node_load_vector = np.zeros([2 * self.num_nodes])

        # Percorre as linhas com os nós de aplicações de forças
        for row in range(self.num_forces):
            node = self.matrix_forces_location[row]
            row_coord_number = 2 * node
            N1 = coord_numbers[row_coord_number]
            N2 = coord_numbers[row_coord_number + 1]
            self.node_load_vector[N1] += self.matrix_forces_magnitude[row, 0]
            self.node_load_vector[N2] += self.matrix_forces_magnitude[row, 1]

    def solve_node_displacements(self):
        """Calcula os deslocamentos nodais pelo método Pênalti."""
        coord_numbers = self.structure_coord_numbers
        matrix_supports = self.matrix_supports

        _S = np.copy(self.structure_stiffness_matrix)
        _P = np.copy(self.node_load_vector)

        for support in range(self.num_supports):
            node = matrix_supports[support, 0]
            for i in range(2):
                row_coord_number = 2 * node + i
                N = coord_numbers[row_coord_number]
                if matrix_supports[support, i + 1] == 1:
                    for row_S in range(2 * self.num_nodes):
                        for column_S in range(2 * self.num_nodes):
                            if row_S == N or column_S == N:
                                _S[row_S, column_S] = 0
                    _S[N, N] = 1
                    _P[N] = 0

        node_displacements = np.linalg.solve(_S, _P)
        return node_displacements

    def calculate_member_forces(self):
        """Calcula os esforços normais nas barras."""

        # Valores auxiliares
        matrix_members = self.matrix_members

        # Vetores que armazenam esforços nas barras e reações de apoio
        self.member_forces = np.empty((0, 4))
        reaction_vector = np.zeros([2*self.num_nodes])

        # Para cada elemento
        for member in matrix_members:
            # Informações das barras
            node_i = member[0]
            node_f = member[1]
            material_type = member[2]
            cross_section_type = member[3]
            E = self.matrix_E[material_type]
            A = self.matrix_areas[cross_section_type]
            x_i = self.matrix_node_coords[node_i, 0]
            y_i = self.matrix_node_coords[node_i, 1]
            x_f = self.matrix_node_coords[node_f, 0]
            y_f = self.matrix_node_coords[node_f, 1]
            L = np.sqrt((x_f-x_i)**2 + (y_f-y_i)**2)
            # cs = cos e sn = sen
            cs = (x_f-x_i) / L
            sn = (y_f-y_i) / L

            # Matriz de transformação de rotação
            T = self.assembly_T(cs, sn)

            # Matriz de deslocamentos nodais do elemento no SCG
            v = self.assembly_v(node_i, node_f)
            # Matriz de deslocamentos nodais do elemento no SCL
            u = np.dot(T, v)

            # Matriz de rigidez do elemento
            k = self.assembly_k(E, A, L)

            # Vetor de forças de extremidade do elemento no SCL
            Q = np.dot(k, u)
            self.member_forces = np.append(
                self.member_forces, np.array([Q]), axis=0)

            # Vetor de forças de extremidade do elemento no SCG
            F = np.dot(T.T, Q)
            # Preenchimento das reações de apoio
            self.assembly_reaction_vector(
                node_i, node_f, F, reaction_vector)

        self.support_reactions = reaction_vector

    def assembly_T(self, cs, sn):
        """
        Preenche a matriz de transformação de coordenadas.

        Parâmetros:
            [cs]: Cosseno (matriz de rotação)
            [sn]: Seno (matriz de rotação)
        """
        T = np.zeros([4, 4])
        T[0, 0] = cs
        T[0, 1] = sn
        T[1, 0] = -sn
        T[1, 1] = cs
        T[2, 2] = cs
        T[2, 3] = sn
        T[3, 2] = -sn
        T[3, 3] = cs
        return T

    def assembly_v(self, node_i, node_f):
        """
        Preenche o vetor de deslocamentos nodais no SCL

        Parâmetros:

            [node_i]: Nó inicial do elemento
            [node_f]: Nó final do elemento
        """
        # Valores auxiliares
        node_displacements = self.solve_node_displacements()
        coord_numbers = self.structure_coord_numbers

        # Criação do vetor com todos os elementos iguais a zero
        v = np.zeros([4])

        # Percorre as coordenadas do nó inicial e verifica se o deslocamento é livre
        for i in range(2):
            coord_number = coord_numbers[2*node_i + i]
            v[i] = node_displacements[coord_number]

        # Percorre as coordenadas do nó final e verifica se o deslocamento é livre
        for j in range(2, 4):
            coord_number = coord_numbers[2*node_f + (j-2)]
            v[j] = node_displacements[coord_number]

        return v

    def assembly_k(self, E, A, L):
        """
        Preenche a matriz de rigidez do elemento no SCL.

        Parâmetros:

            [E]: Módulo de elasticidade longitudinal
            [A]: Área da seção transversal da barra
            [L]: Comprimento da barra
        """
        k = np.zeros([4, 4])
        axial_stiffness = E * A / (L)
        k[0, 0] = axial_stiffness
        k[0, 2] = -axial_stiffness
        k[2, 0] = -axial_stiffness
        k[2, 2] = axial_stiffness
        return k

    def assembly_reaction_vector(self, node_i, node_f, F, reaction_vector):
        """
        Preenche o vetor de reações de apoio.

        Parâmetros:

            [node_i]: Nó inicial do elemento
            [node_f]: Nó final do elemento
            [F]: Vetor de forças nas extremidades do elemento no SCG
            [reaction_vector]: Vetor de reações de apoio a ser preenchido
        """

        # Valores auxiliares
        coord_numbers = self.structure_coord_numbers
        matrix_supports = self.matrix_supports

        for support in range(self.num_supports):
            if node_i == matrix_supports[support, 0]:
                # Percorre as coordenadas do nó inicial e verifica se o deslocamento é livre
                for i in range(2):
                    coord_number = coord_numbers[2*node_i + i]
                    if matrix_supports[support, i+1]:
                        reaction_vector[coord_number] += F[i]
            if node_f == matrix_supports[support, 0]:
                # Percorre as coordenadas do nó final e verifica se o deslocamento é livre
                for i in range(2, 4):
                    coord_number = coord_numbers[2*node_f + (i-2)]
                    if matrix_supports[support, i+1-2]:
                        reaction_vector[coord_number] += F[i]

    def show_deformed_shape(self, scale_factor=50):
        coords = self.matrix_node_coords
        members = self.matrix_members[:,:2]
        node_displacements = np.copy(self.solve_node_displacements())

        # Rearranjo dos deslocamentos nodais para somar com a matriz de coordenadas
        node_displacements_matrix = np.reshape(node_displacements, (self.num_nodes, 2))

        # Cálculo das coordenadas da configuração deformada
        coords_deformed = np.copy(coords) + node_displacements_matrix*scale_factor

        plt.figure(figsize=(10,4))

        # Valor auxiliar para percorrer as linhas da matriz de apoios
        aux = -1

        # Plota os nós
        for node in range(self.num_nodes):
            # Verifica se há apoio no nó
            if node in self.matrix_supports[:, 0]:
                aux += 1
                # Verificação de apoio do segundo gênero
                if self.matrix_supports[aux, 1] == 1 and self.matrix_supports[aux, 2] == 1:
                    plt.plot(coords[node,0], coords[node,1], "bs")
                    plt.plot(coords_deformed[node,0], coords_deformed[node,1], "rs")
                # Verificação de apoio do primeiro gênero
                elif self.matrix_supports[aux, 1] == 1 or self.matrix_supports[aux, 2] == 1:
                    plt.plot(coords[node,0], coords[node,1], "b^")
                    plt.plot(coords_deformed[node,0], coords_deformed[node,1], "r^")
            else:
                plt.plot(coords[node,0], coords[node,1], "bo")
                plt.plot(coords_deformed[node,0], coords_deformed[node,1], "ro")

        # Plota as barras
        for member in range(self.num_members):
            p1, p2 = members[member, 0], members[member, 1]
            x1, y1 = coords[p1, 0], coords[p1, 1]
            x2, y2 = coords[p2, 0], coords[p2, 1]
            plt.plot([x1,x2],[y1,y2],'b-')
            x1_d, y1_d = coords_deformed[p1, 0], coords_deformed[p1, 1]
            x2_d, y2_d = coords_deformed[p2, 0], coords_deformed[p2, 1]
            plt.plot([x1_d,x2_d],[y1_d,y2_d],'r--')

        plt.legend(["Configuração indeformada", "Configuração deformada"], loc="best")

        plt.show()

    def show_results(self, save_file=False, output_file=""):
        """
        Exibe e salva os resultados da análise.

        Parâmetros:

            [save_file]: Salva os resultados em um arquivo de saída ou não
            [output_file]: Nome do arquivo de saída de resultados
        """

        # Nome do caso analisado
        case = self.file_name.with_suffix("").name
        if output_file == "":
            output_file = f"Resultados - {case}.txt"

        output_msg = "".center(75, '#') + '\n'
        output_msg += " INÍCIO DA ANÁLISE ".center(75, '#') + '\n'
        output_msg += "".center(75, '#') + '\n'
        output_msg += "".center(75, '-') + '\n'

        output_msg += f"Caso analisado: {case}\n"

        output_msg += "".center(75, '-') + '\n'

        # Informações gerais do caso
        output_msg += f"Número de nós: {self.num_nodes}\n"
        output_msg += f"Número de barras: {self.num_members}\n"
        output_msg += f"Número de barras: {self.num_members}\n"

        output_msg += "".center(75, '-') + '\n'

        # Coordenadas dos nós
        output_msg += " Nós ".center(75, '-') + '\n'
        output_msg += f"{'Nó'.center(4)} | {'Coordenada X'.center(14)} | {'Coordenada Y'.center(14)}\n"

        for i, node in enumerate(self.matrix_node_coords):
            node_number = f"{i + 1}".center(4)
            x = f"{node[0]:5.4E}".center(14)
            y = f"{node[1]:5.4E}".center(14)
            output_msg += ' | '.join([node_number, x, y]) + '\n'

        output_msg += "".center(75, '-') + '\n'

        # Barras da treliça
        output_msg += " Barras ".center(75, '-') + '\n'
        output_msg += f"{'Barra'.center(8)} | {'Nó inicial'.center(12)} | {'Nó final'.center(10)} | {'E'.center(10)} | {'A'.center(10)}\n"

        for i, member in enumerate(self.matrix_members):
            num_member = f"{i + 1}".center(8)
            node_i = f"{member[0] + 1}".center(12)
            node_f = f"{member[1] + 1}".center(10)
            material_type = member[2]
            cross_section_type = member[3]
            E = f"{self.matrix_E[material_type]:5.4E}".center(10)
            A = f"{self.matrix_areas[cross_section_type]:5.4E}".center(10)
            output_msg += ' | '.join([num_member,
                                     node_i, node_f, E, A]) + '\n'

        output_msg += "".center(75, '-') + '\n'

        # Apoios
        output_msg += " Apoios ".center(75, '-') + '\n'
        output_msg += f"{'Nó'.center(4)} | {'Restringe X'.center(13)} | {'Restringe Y'.center(13)}\n"

        for support in self.matrix_supports:
            node = f"{support[0] + 1}".center(4)
            x_has_support = "Sim".center(
                13) if support[1] else "Não".center(13)
            y_has_support = "Sim".center(
                13) if support[2] else "Não".center(13)
            output_msg += ' | '.join([node, x_has_support,
                                     y_has_support]) + '\n'

        output_msg += "".center(75, '-') + '\n'

        # Forças nodais
        output_msg += " Forças nodais ".center(75, '-') + '\n'
        output_msg += f"{'Nó'.center(4)} | {'Componente X'.center(14)} | {'Componente Y'.center(14)}\n"

        for i, node in enumerate(self.matrix_forces_location):
            node = f"{node + 1}".center(4)
            x_force = f"{self.matrix_forces_magnitude[i, 0]:5.4E}".center(14)
            y_force = f"{self.matrix_forces_magnitude[i, 1]:5.4E}".center(14)
            output_msg += ' | '.join([node, x_force, y_force]) + '\n'

        # Cálculo dos esforços normais nas barras
        self.calculate_member_forces()

        # Reações de apoio
        reactions = np.copy(self.support_reactions)
        supports = self.matrix_supports
        reaction_matrix = np.zeros([self.num_supports, 3])
        coord_numbers = self.structure_coord_numbers

        for support in range(self.num_supports):
            node = supports[support, 0]
            reaction_matrix[support, 0] = node
            for i in range(2):
                coord_number = coord_numbers[2*node + i]
                if supports[support, i+1]:
                    reaction_matrix[support, i+1] = reactions[coord_number]

        output_msg += "".center(75, '-') + '\n'

        output_msg += " Reações de apoio ".center(75, '-') + '\n'
        output_msg += f"{'Nó'.center(4)} | {'Componente X'.center(14)} | {'Componente Y'.center(14)}\n"

        for node in reaction_matrix:
            num_node = f"{int(node[0] + 1)}".center(4)
            x_reaction = f"{node[1]:5.4E}".center(14)
            y_reaction = f"{node[2]:5.4E}".center(14)
            output_msg += ' | '.join([num_node, x_reaction, y_reaction]) + '\n'

        # Deslocamentos nodais
        node_displacements = list(np.copy(self.solve_node_displacements()))
        node_displacements_matrix = np.zeros([self.num_nodes, 3])

        for i in range(self.num_nodes):
            node_displacements_matrix[i, 0] = i

        for i in range(self.num_nodes):
            for j in range(2):
                node_displacements_matrix[i, j+1] = node_displacements.pop(0)

        output_msg += "".center(75, '-') + '\n'

        output_msg += " Deslocamentos nodais ".center(75, '-') + '\n'
        output_msg += f"{'Nó'.center(4)} | {'Direção X'.center(18)} | {'Direção Y'.center(18)}\n"

        for node in node_displacements_matrix:
            num_node = f"{int(node[0] + 1)}".center(4)
            x_direction = f"{node[1]:5.4E}".center(18)
            y_direction = f"{node[2]:5.4E}".center(18)
            output_msg += ' | '.join([num_node,
                                     x_direction, y_direction]) + '\n'

        output_msg += "".center(75, '-') + '\n'

        # Esforços normais nas barras
        output_msg += " Esforço normal ".center(75, '-') + '\n'
        output_msg += f"{'Barra'.center(8)} | {'Esforço normal'.center(20)}\n"

        for member in range(self.num_members):
            num_member = f"{member + 1}".center(8)
            if self.member_forces[member, 0] > 0:
                axial_force = f"{abs(self.member_forces[member, 0]):5.4E} (Compressão)".center(
                    20)
            else:
                axial_force = f"{abs(self.member_forces[member, 0]):5.4E} (Tração)".center(
                    20)
            output_msg += ' | '.join([num_member, axial_force]) + '\n'

        output_msg += "".center(75, '-') + '\n'
        output_msg += "".center(75, '#') + '\n'
        output_msg += " FIM DA ANÁLISE ".center(75, '#') + '\n'
        output_msg += "".center(75, '#')

        # Salvamento dos resultados em um arquivo txt se save_file=True
        if save_file:
            with open(output_file, "w", encoding="utf-8") as out_file:
                out_file.write(output_msg)

        # Exibição dos resultados
        print(output_msg)

if __name__ == "__main__":
    # Caminhos para os arquivos de entrada de dados
    data_path = pathlib.Path(__file__).parent / "data/Exemplo Livro.txt"
    data_path_2 = pathlib.Path(__file__).parent / "data/Exemplo Lista Teoria 2.txt"
    # Criação da instância de um objeto e cálculo
    trelica_1 = Truss_2D(data_path)
    trelica_1.show_deformed_shape()
    # trelica_1.show_results(save_file=True)
    trelica_2 = Truss_2D(data_path_2)
    trelica_2.show_deformed_shape()
    # trelica_2.show_results(save_file=True)


text:
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from typing import Optional

import megengine._internal as mgb

from ..core import Tensor, wrap_io_tensor
from .elemwise import clamp

@wrap_io_tensor
def sum(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
    r"""Returns the sum of each row of the ``inp`` tensor in the given ``axis``.

    :param inp: The input tensor.
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced.
        Default: None
    :param keepdims: Whether the output tensor has ``axis`` retained or not.
        Default: False
    :return: The output tensor

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        data = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
        out = F.sum(data)
        print(out.numpy())

    .. testoutput::

        [21]

    """
    return mgb.opr.reduce_(inp, "SUM", axis, keepdims)

@wrap_io_tensor
def prod(inp: Tensor, axis: Optional[int] = None, keepdims=False) -> Tensor:
    r"""
    Returns the element product of input tensor along given *axis*.

    :param inp: The input tensor
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: ``None``
    :param keepdims: Whether the output tensor has *axis* retained or not. Default: ``False``
    :return: The output tensor

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        data = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
        out = F.prod(data)
        print(out.numpy())

    Outputs:

    .. testoutput::

        [720]

    """
    return mgb.opr.reduce_(inp, "PRODUCT", axis, keepdims)

@wrap_io_tensor
def mean(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
    """Returns the mean value of each row of the ``inp`` tensor in
    the given ``axis``. If axis is a list of dimensions,
    reduce over all of them.

    :param inp: The input tensor
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
    :param keepdims: Whether the output tensor has ``axis`` retained or not. Default: False

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        data = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
        out = F.mean(data)
        print(out.numpy())

    .. testoutput::

        [3.5]

    """
    return mgb.opr.mean(inp, axis, keepdims)

@wrap_io_tensor
def min(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
    r"""
    Returns the min value of input tensor along given *axis*.

    :param inp: The input tensor
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
    :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
    :return: The output tensor

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
        y = F.min(x)
        print(y.numpy())

    Outputs:

    .. testoutput::

        [1]

    """
    return mgb.opr.reduce_(inp, "MIN", axis, keepdims)

@wrap_io_tensor
def max(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
    r"""Returns the max value of the input tensor along given *axis*.

    :param inp: The input tensor
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
    :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
    :return: The output tensor

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
        y = F.max(x)
        print(y.numpy())

    .. testoutput::

        [6]

    """
    return mgb.opr.reduce_(inp, "MAX", axis, keepdims)

@wrap_io_tensor
def sqrt(inp: Tensor) -> Tensor:
    """
    Return a new tensor with the square-root of the elements of ``inp``

    :param inp: The input tensor
    :return: The computed tensor

    Examples:

    .. testcode::

        import numpy as np
        import megengine as mge
        import megengine.functional as F

        data = mge.tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3))
        out = F.sqrt(data)
        print(out.numpy())

    Outputs:

    .. testoutput::

        [[0.      1.     1.4142]
         [1.7321  2.     2.2361 ]]

    """

    return mgb.opr.sqrt(inp)

def norm(inp: Tensor, p: int = 2, axis: Optional[int] = None, keepdims=False):
    """Calculate ``p``-norm of input tensor along certain axis.

    :param inp: The input tensor
    :param p: power of value ``p`` applied to ``inp``. Default: 2
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
    :param keepdims: Whether the output tensor has ``axis`` retained or not. Default: False
    :return: The output tensor

    """
    if axis is None:
        inp = inp.reshape(-1)
    return (inp ** p).sum(axis=axis, keepdims=keepdims) ** (1.0 / p)

@wrap_io_tensor
def argmin(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
    r"""Returns the indices of the minimum values along an axis

    :param inp: The input tensor
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
    :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
    :return: The output tensor

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
        y = F.argmin(x)
        print(y.numpy())

    .. testoutput::

        [0]

    """
    return mgb.opr.argmin(inp, axis, keepdims)

@wrap_io_tensor
def argmax(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
    r"""Returns the indices of the maximum values along an axis

    :param inp: The input tensor
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
    :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
    :return: The output tensor

    Examples:

    .. testcode::

        import numpy as np
        from megengine import tensor
        import megengine.functional as F

        x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
        y = F.argmax(x)
        print(y.numpy())

    .. testoutput::

        [5]

    """
    return mgb.opr.argmax(inp, axis, keepdims)

def normalize(
    inp: Tensor, p: int = 2, axis: Optional[int] = None, eps: float = 1e-12
) -> Tensor:
    r"""Perform :math:`L_p` normalization of input tensor along certain axis.

    For a tensor :attr:`inp` of shape :math:`(n_0, ..., n_{dim}, ..., n_k)`, each
    :math:`n_{dim}` -element vector :math:`v` along dimension :attr:`axis` is transformed as:

    .. math::
        v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}.

    :param inp: the input tensor
    :param p: power of value ``p`` applied to ``inp``. Default: 2
    :param axis: The dimension to reduce. If None, all the dimensions will be reduced
        to calculate the norm. Default: None
    :param eps: a small value to avoid division by zero. Default: 1e-12
    :return: the normalized output tensor

    """
    if axis is None:
        return inp / clamp(norm(inp, p), lower=eps)
    else:
        return inp / clamp(norm(inp, p, axis, keepdims=True), lower=eps)

max_stars_repo_name: johnhany/MegEngine

max_issues_repo_name: johnhany/MegEngine

max_forks_repo_name: johnhany/MegEngine


text:
"""
Protein-Ligand Interaction Profiler - Analyze and visualize protein-ligand interactions in PDB files.
mp.py - Functions for parallel processing
"""

# Python Standard Library
from __future__ import division
from builtins import zip
import multiprocessing
import itertools
from numpy import asarray
from functools import partial

class SubProcessError(Exception):
    def __init__(self, e, exitcode=1):
        self.exitcode = exitcode
        super(SubProcessError, self).__init__(e)
    pass

def universal_worker(input_pair):
    """This is a wrapper function expecting a tiplet of function, single
       argument, dict of keyword arguments. The provided function is called
       with the appropriate arguments."""
    function, arg, kwargs = input_pair
    return function(arg, **kwargs)

def pool_args(function, sequence, kwargs):
    """Return a single iterator of n elements of lists of length 3, given a sequence of len n."""
    return zip(itertools.repeat(function), sequence, itertools.repeat(kwargs))

def parallel_fn(f):
    """Simple wrapper function, returning a parallel version of the given function f.
       The function f must have one argument and may have an arbitray number of
       keyword arguments. """

    def simple_parallel(func, sequence, **args):
        """ f takes an element of sequence as input and the keyword args in **args"""
        if 'processes' in args:
            processes = args.get('processes')
            del args['processes']
        else:
            processes = multiprocessing.cpu_count()

        pool = multiprocessing.Pool(processes)  # depends on available cores

        result = pool.map_async(universal_worker, pool_args(func, sequence, args))
        pool.close()
        pool.join()
        cleaned = [x for x in result.get() if x is not None]  # getting results
        cleaned = asarray(cleaned)
        return cleaned
    return partial(simple_parallel, f)

reason: from numpy


text:
from __future__ import division
import argparse
import logging
import numpy as np
import cv2
from os import makedirs
from os.path import join, isdir

from lib.log_helper import init_log, add_file_handler
from lib.bbox_helper import get_axis_aligned_bbox, cxy_wh_2_rect
from lib.benchmark_helper import load_dataset

from lib.pysot.utils import region
from cftracker.mosse import MOSSE
from cftracker.staple import Staple
from cftracker.dsst import DSST
from cftracker.samf import SAMF
from cftracker.kcf import KCF
from cftracker.csk import CSK
from cftracker.cn import CN
from cftracker.dat import DAT
from cftracker.eco import ECO
from cftracker.bacf import BACF
from cftracker.csrdcf import CSRDCF
from cftracker.ldes import LDES
from cftracker.mkcfup import MKCFup
from cftracker.strcf import STRCF
from cftracker.mccth_staple import MCCTHStaple
from cftracker.opencv_cftracker import OpenCVCFTracker

from lib.eco.config import vot16_deep_config,vot16_hc_config
from cftracker.config import ldes_config,dsst_config,csrdcf_config,staple_config,mkcf_up_config,mccth_staple_config
parser = argparse.ArgumentParser(description='Test')

parser.add_argument('--dataset', dest='dataset', default='VOT2016',
                    help='datasets')
parser.add_argument('-l', '--log', default="log_test_vot2016.txt", type=str, help='log file')
parser.add_argument('-v', '--visualization', dest='visualization', action='store_true',
                    help='whether visualize result',default=False)
parser.add_argument('--gt', action='store_true', help='whether use gt rect for davis (Oracle)')

def create_tracker(tracker_type):
    if tracker_type == 'MOSSE':
        tracker = MOSSE()
    elif tracker_type == 'CSK':
        tracker = CSK()
    elif tracker_type == 'CN':
        tracker = CN()
    elif tracker_type == 'DSST':
        tracker = DSST(dsst_config.DSSTConfig())
    elif tracker_type=='DSST-LP':
        tracker=DSST(dsst_config.DSSTLPConfig())
    elif tracker_type=='SAMF':
        tracker=SAMF()
    elif tracker_type == 'Staple':
        tracker = Staple(config=staple_config.StapleVOTConfig())
    #elif tracker_type=='Staple-CA':
    #    tracker=Staple(config=staple_config.StapleCAVOTConfig())
    elif tracker_type == 'KCF':
        tracker = KCF(features='hog', kernel='gaussian')
    elif tracker_type == 'DCF':
        tracker = KCF(features='hog', kernel='linear')
    elif tracker_type == 'DAT':
        tracker = DAT()
    elif tracker_type=='ECO-HC':
        tracker=ECO(config=vot16_hc_config.VOT16HCConfig())
    elif tracker_type=='ECO':
        tracker=ECO(config=vot16_deep_config.VOT16DeepConfig())
    elif tracker_type=='BACF':
        tracker=BACF()
    elif tracker_type=='CSRDCF':
        tracker=CSRDCF(csrdcf_config.CSRDCFConfig())
    elif tracker_type=='CSRDCF-LP':
        tracker=CSRDCF(csrdcf_config.CSRDCFLPConfig())
    elif tracker_type=='OPENCV_KCF':
        tracker=OpenCVCFTracker(name='KCF')
    elif tracker_type=='OPENCV_MOSSE':
        tracker=OpenCVCFTracker(name='MOSSE')
    elif tracker_type=='OPENCV-CSRDCF':
        tracker=OpenCVCFTracker(name='CSRDCF')
    elif tracker_type=='LDES':
        tracker=LDES(config=ldes_config.LDESVOTLinearConfig())
    elif tracker_type=='LDES-NoBGD':
        tracker=LDES(config=ldes_config.LDESVOTNoBGDLinearConfig())
    elif tracker_type=='MKCFup':
        tracker=MKCFup(config=mkcf_up_config.MKCFupConfig())
    elif tracker_type=='MKCFup-LP':
        tracker=MKCFup(config=mkcf_up_config.MKCFupLPConfig())
    elif tracker_type=='STRCF':
        tracker=STRCF()
    elif tracker_type=='MCCTH-Staple':
        tracker=MCCTHStaple(config=mccth_staple_config.MCCTHVOTConfig())
    else:
        raise NotImplementedError
    return tracker

def track_vot(tracker_type, video):

    regions = []  # result and states[1 init / 2 lost / 0 skip]
    image_files, gt = video['image_files'], video['gt']

    start_frame, end_frame, lost_times, toc = 0, len(image_files), 0, 0

    for f, image_file in enumerate(image_files):
        im = cv2.imread(image_file)
        tic = cv2.getTickCount()
        if f == start_frame:  # init
            tracker=create_tracker(tracker_type)
            if tracker_type=='LDES':
                tracker.init(im,gt[f])
                if tracker.polygon is True:
                    location=gt[f]
                else:
                    cx, cy, w, h = get_axis_aligned_bbox(gt[f])
                    target_pos = np.array([cx, cy])
                    target_sz = np.array([w, h])
                    location = cxy_wh_2_rect(target_pos, target_sz)

            else:
                cx, cy, w, h = get_axis_aligned_bbox(gt[f])
                target_pos = np.array([cx, cy])
                target_sz = np.array([w, h])
                location=cxy_wh_2_rect(target_pos,target_sz)
                tracker.init(im,((cx-w/2),(cy-h/2),(w),(h)))
            regions.append(1 if 'VOT' in args.dataset else gt[f])
        elif f > start_frame:
            location=tracker.update(im)

            if 'VOT' in args.dataset:
                b_overlap = region.vot_overlap(gt[f],location, (im.shape[1], im.shape[0]))
            else:
                b_overlap = 1
            if b_overlap:
                regions.append(location)
            else:  # lost
                regions.append(2)
                lost_times += 1
                start_frame = f + 5  # skip 5 frames
        else:  # skip
            regions.append(0)
        toc += cv2.getTickCount() - tic

        if args.visualization and f >= start_frame:  # visualization (skip lost frame)
            im_show = im.copy()
            if f == 0: cv2.destroyAllWindows()
            if gt.shape[0] > f:
                if len(gt[f]) == 8:
                    cv2.polylines(im_show, [np.array(gt[f], np.int).reshape((-1, 1, 2))], True, (0, 255, 0), 3)
                else:
                    cv2.rectangle(im_show, (gt[f, 0], gt[f, 1]), (gt[f, 0] + gt[f, 2], gt[f, 1] + gt[f, 3]), (0, 255, 0), 3)
            if len(location) == 8:
                location_int = np.int0(location)
                cv2.polylines(im_show, [location_int.reshape((-1, 1, 2))], True, (0, 255, 255), 3)
            else:
                location = [int(l) for l in location]
                cv2.rectangle(im_show, (location[0], location[1]),
                              (location[0] + location[2], location[1] + location[3]), (0, 255, 255), 3)
            cv2.putText(im_show, str(f), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
            cv2.putText(im_show, str(lost_times), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            cv2.imshow(video['name'], im_show)
            cv2.waitKey(1)
    toc /= cv2.getTickFrequency()

    # save result
    name = tracker_type

    video_path = join('test', args.dataset, name,
                          'baseline', video['name'])
    if not isdir(video_path): makedirs(video_path)
    result_path = join(video_path, '{:s}_001.txt'.format(video['name']))
    with open(result_path, "w") as fin:
        for x in regions:
            fin.write("{:d}\n".format(x)) if isinstance(x, int) else \
            fin.write(','.join([region.vot_float2str("%.4f", i) for i in x]) + '\n')

    logger.info('({:d}) Video: {:12s} Time: {:02.1f}s Speed: {:3.1f}fps Lost: {:d} Tracker:{}'.format(
        v_id, video['name'], toc, f / toc, lost_times,tracker_type))

    return lost_times, f / toc

def main():
    global args, logger, v_id
    args = parser.parse_args()
    init_log('global', logging.INFO)
    if args.log != "":
        add_file_handler('global', args.log, logging.INFO)

    logger = logging.getLogger('global')
    logger.info(args)

    # setup dataset
    dataset = load_dataset(args.dataset)

    total_lost = 0  # VOT
    speed_list = []

    trackers = ['STRCF']

    for tracker_type in trackers:

        for v_id, video in enumerate(dataset.keys(), start=1):
            lost, speed = track_vot(tracker_type,dataset[video])
            total_lost += lost
            speed_list.append(speed)

        logger.info('Total Lost: {:d}'.format(total_lost))

        logger.info('Mean Speed: {:.2f} FPS'.format(np.mean(speed_list)))

if __name__ == '__main__':
    main()


text:
import numpy as np

from games.collecto.util.consts import *
from games.collecto.util.remove_adjacent import _get_equal_neighbours

def get_board(random=False):
    return get_random_board() if random else get_static_board()

def get_static_board():
    return np.array([
        [R, O, Y, P, G, B, P],
        [B, Y, B, Y, O, R, O],
        [R, P, Y, P, R, P, G],
        [B, Y, B, E, O, B, O],
        [G, B, G, Y, R, G, R],
        [O, P, Y, O, G, P, G],
        [P, R, B, R, Y, G, O]
    ])

def get_random_board():
    balls = np.array([colour for colour in COLOURS*8])
    np.random.shuffle(balls)
    balls = np.insert(balls, 24, E)
    balls = np.reshape(balls, (-1, 7))

    equal_neighbours = _get_equal_neighbours(balls)
    while not len(equal_neighbours) == 0:
        loc = np.random.choice([i for i in range(48) if not i == 24])
        x, y = loc // 7, loc % 7
        dx, dy = equal_neighbours.pop()[1]
        balls[dx][dy], balls[x][y] = balls[x][y], balls[dx][dy]
        equal_neighbours = _get_equal_neighbours(balls)
    return balls

max_stars_repo_name: RensOliemans/muzero-general

max_issues_repo_name: RensOliemans/muzero-general

max_forks_repo_name: RensOliemans/muzero-general


text:
import numpy as np
import pandas as pd
import collections
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture
from sklearn.model_selection import KFold
from random import seed
from random import randrange
from scipy.stats import multivariate_normal

#Import Dataset and store features in X
dataset = pd.read_csv('GMM_Dtrain_10000.csv')
X = dataset.iloc[:, 0:2].values

# Define number of folds and list to store model orders
kf = KFold(n_splits = 10)
Order = []

# Run training routine multiple times to analyze rate of model order selection
for routine in range(1,30):
    Max_likelihood = []
    # Test model orders rangine from 1 to 6
    for order in range(1,7):
        score = 0
        # Perform 10-fold cross validation and compute log-likelihood in each case
        for train_index, test_index in kf.split(X):
            Xtrain = X[train_index]
            Xtest = X[test_index]
            model = GaussianMixture(n_components = order, init_params = 'random', max_iter = 3500, tol = 1e-9, n_init = 2)
            model.fit(Xtrain)
            score += model.score(Xtest)

        # Compute average score after cross validation
        avg_score = score / 10
        # Store average scores in a list
        Max_likelihood.append(avg_score)

    print(Max_likelihood)
    # Identify model order with maximum score
    Order.append(np.argmax(Max_likelihood) + 1)

# Plot a histogram showing frequency of model orders selected
plt.hist(Order, range = (1, 7))
plt.xlabel("Model Order")
plt.ylabel("Average Log Likelihood")
plt.title("Plot for Model Order Selection")
plt.show()

reason: import numpy,from scipy


text:
#!/usr/bin/env python3

import ev3dev.ev3 as ev3
import numpy as np
import math
import random

def sensor_model (m_t0, s_t, z_t, c_angle):
    resolution = m_t0.get_resolution()
    _x_centre = 0
    _x_left = 0
    _x_right = 0

    if valid_centre:
        for m_ver in range (5, x_centre, resolution):
            for m_hor in range (- m_ver * np.tan(10), m_ver * np.tan(10), resolution):
                x_mi = x_t + m_ver * np.cos(c_angle) - m_hor * np.cos(c_angle + 90)   # x coordinate of grid cell
                y_mi = y_t + m_ver * np.sin(c_angle) - m_hor * np.sin(c_angle + 90)   # y coordinate of grid cell
                p_mi = m_t0. get_value(x_mi, y_mi)         #probability value at (x_mi, y_mi)
                if p_mi > 0.5:
                    _x_centre = m_ver
                    break
            if p_mi > 0.5:
                break
        if p_mi > 0.5:
            p_centre = random.gauss(abs(x_centre - _x_centre), 2)
        else:
            p_centre = 1
    else:
        p_centre = 1           #increase x_ver or _x_centre until find an occupied cell
                               #then take this x_ver as _x_centre

    if valid_left:
        for m_hor in range (5, x_left, resolution):
            for m_ver in range (- m_hor * np.tan(10), m_hor * np.tan(10), resolution):
                x_mi = x_t + m_hor * np.cos(c_angle + 90) - m_ver * np.cos(c_angle + 180)
                y_mi = y_t + m_hor * np.sin(c_angle + 90) - m_ver * np.sin(c_angle + 180)
                p_mi = m_t0. get_value(x_mi, y_mi)
                if p_mi > 0.5:
                    _x_left = m_hor
                    break
            if p_mi > 0.5:
                break
        if p_mi > 0.5:
            p_left = random.gauss(abs(x_left - _x_left), 2)
        else:
            p_left = 1
    else:
        p_left = 1

    if valid_right:
        for m_hor in range (5, x_right, resolution):
            for m_ver in range (- m_hor * np.tan(10), m_hor * np.tan(10), resolution):
                x_mi = x_t + m_hor * np.cos(c_angle - 90) - m_ver * np.cos(c_angle)
                y_mi = y_t + m_hor * np.sin(c_angle - 90) - m_ver * np.sin(c_angle)
                p_mi = m_t0. get_value(x_mi, y_mi)
                if p_mi > 0.5:
                    _x_right = m_hor
                    break
            if p_mi > 0.5:
                break
        if p_mi > 0.5:
            p_right = random.gauss(abs(x_right - _x_right), 2)
        else:
            p_right = 1
    else:
        p_right = 1

    if valid_rot:
        _m_angle = angle_t - c_angle
        p_rot = np.gauss(abs(m_angle - _m_angle), 3)
    else:
        p_rot = 1

    p_tot = p_rot * p_centre * p_left * p_right

    return p_tot


text:
try:
    import networkx as nx
    from networkx.drawing.nx_agraph import graphviz_layout
    import matplotlib.pyplot as plt
    _DRAW_GRAPHS = True
except:
    _DRAW_GRAPHS = False

from . import schema
from .jsonutil import get_column
from .search import Search

class Inspector(object):
    """ Database introspection interface.
    """

    def __init__(self, interface):
        """
            Parameters
            ----------
            interface:
                :class:`Interface` Object
        """
        self._intf = interface
        self._get_json = interface._get_json
        self._auto = True
        self._tick = 30

        self.schemas = SchemasInspector(interface)

    def __call__(self):
        for name in ['experiment', 'assessor', 'scan', 'reconstruction']:
            self._resource_struct(name)

    def set_autolearn(self, auto=None, tick=None):
        """ Once in a while queries will persist additional
            information on the server. This information is available
            through the following methods of this class:

                - experiment_types
                - assessor_types
                - scan_types
                - reconstruction_types

            It is also transparently used in insert operations.

            Parameters
            ----------
            auto: boolean
                True to enable auto learn. False to disable.
            tick: int
                Every 'tick' seconds, if a query is issued, additional
                information will be persisted.

            See Also
            --------
            :func:`EObject.insert`
        """
        if auto is not None:
            self._auto = auto
        if tick is not None:
            self._tick = tick

    def datatypes(self, pattern='*', fields_pattern=None):
        """ Discovers the datatypes and datafields of the database.

            Parameters
            ----------
            pattern: string
                Pattern for the datatype. May include wildcards.
            fields_pattern: string
                - Pattern for the datafields -- may include wildcards.
                - If specified, datafields will be returned instead of
                  datatypes.

            Returns
            -------
            list : datatypes or datafields depending on the argument usage.
        """
        self._intf._get_entry_point()

        search_els = self._get_json('%s/search/elements?format=json' %
                                        self._intf._get_entry_point())

        if not fields_pattern and ('*' in pattern or '?' in pattern):
            return get_column(search_els , 'ELEMENT_NAME', pattern)

        else:
            fields = []
            for datatype in get_column(search_els , 'ELEMENT_NAME', pattern):
                fields.extend(self._datafields(datatype,
                                               fields_pattern or '*', True)
                              )

            return fields

    def _datafields(self, datatype, pattern='*', prepend_type=True):
        self._intf._get_entry_point()

        search_fds = self._get_json('%s/search/elements/%s?format=json'
                                        % (self._intf._get_entry_point(), datatype)
                                    )

        fields = get_column(search_fds, 'FIELD_ID', pattern)

        return ['%s/%s' % (datatype, field)
                if prepend_type else field
                for field in fields
                if '=' not in field and 'SHARINGSHAREPROJECT' not in field
                ]

    def experiment_types(self):
        """ Returns the datatypes used at the experiment level in this
            database.

            See Also
            --------
            :func:`Inspector.set_autolearn`
        """
        return self._resource_types('experiment')

    def assessor_types(self):
        """ Returns the datatypes used at the assessor level in this
            database.

            See Also
            --------
            :func:`Inspector.set_autolearn`
        """
        return self._resource_types('assessor')

    def reconstruction_types(self):
        """ Returns the datatypes used at the reconstruction level in this
            database.

            See Also
            --------
            :func:`Inspector.set_autolearn`
        """
        return self._resource_types('reconstruction')

    def scan_types(self):
        """ Returns the datatypes used at the scan level in this
            database.

            See Also
            --------
            :func:`Inspector.set_autolearn`
        """
        return self._resource_types('scan')

    def field_values(self, field_name):
        """ Look for the values a specific datafield takes in the database.
        """
        self._intf._get_entry_point()

        search_tbl = Search(field_name.split('/')[0],
                            [field_name], self._intf
                            )

        criteria = [('%s/ID' % field_name.split('/')[0], 'LIKE', '%'), 'AND']

        return list(set([val
                         for entry in search_tbl.where(criteria)
                         for val in entry.values()
                         ])
                    )

    def project_values(self):
        """ Look for the values a the project level in the database.

            .. note::
                Is equivalent to interface.select.projects().get()
        """
        self._intf._get_entry_point()

        return get_column(self._get_json(
                '%s/projects' % self._intf._entry), 'ID')

    def subject_values(self, project=None):
        """ Look for the values a the subject level in the database.

            .. note::
                Is equivalent to interface.select('//subjects').get()
        """
        self._intf._get_entry_point()

        uri = '%s/subjects?columns=ID' % self._intf._entry

        if project is not None:
            uri += '&project=%s' % project

        return get_column(self._get_json(uri), 'ID')

    def experiment_values(self, datatype, project=None):
        """ Look for the values a the experiment level for a given datatype
            in the database.

            .. note::
                The  datatype should be one of Inspector.experiment_types()

            Parameters
            ----------
            datatype: string
                An experiment type. eg: xnat:mrsessiondata
            project: string
                Optional. Restrict operation to a project.
        """
        self._intf._get_entry_point()

        uri = '%s/experiments?columns=ID' % self._intf._entry
        if datatype is not None:
            uri += '&xsiType=%s' % datatype
        if project is not None:
            uri += '&project=%s' % project

        return get_column(self._get_json(uri), 'ID')

    def assessor_values(self, experiment_type, project=None):
        """ Look for the values at the assessor level for a given experiment
            type in the database.

            .. note::
               The experiment type should be one of
               Inspector.experiment_types()

            .. warning::
                Depending on the number of elements the operation may
                take a while.

            Parameters
            ----------
            datatype: string
                An experiment type. eg: xnat:mrsessiondata
            project: string
                Optional. Restrict operation to a project.
        """
        return self._sub_experiment_values('assessor',
                                           project, experiment_type)

    def scan_values(self, experiment_type, project=None):
        """ Look for the values at the scan level for a given experiment
            type in the database.

            .. note::
               The experiment type should be one of
               Inspector.experiment_types()

            .. warning::
                Depending on the number of elements the operation may
                take a while.

            Parameters
            ----------
            datatype: string
                An experiment type.
            project: string
                Optional. Restrict operation to a project.
        """
        return self._sub_experiment_values('scan', project, experiment_type)

    def reconstruction_values(self, experiment_type, project=None):
        """ Look for the values at the reconstruction level for a given
            experiment type in the database.

            .. note::
               The experiment type should be one of
               Inspector.experiment_types()

            .. warning::
                Depending on the number of elements the operation may
                take a while.

            Parameters
            ----------
            datatype: string
                An experiment type.
            project: string
                Optional. Restrict operation to a project.
        """
        return self._sub_experiment_values('reconstruction',
                                           project, experiment_type)

    def structure(self):
        """ Displays the keywords structure used in XNAT REST API.
        """
        def traverse(coll, lvl):
            for key in schema.resources_tree[coll]:
                print('%s+%s' % (' ' * lvl, key.upper()))

                datatypes = set([
                        self._intf._struct[uri]
                        for uri in self._intf._struct.keys()
                        if uri.split('/')[-2] == key
                        ])

                if datatypes != set():
                    print('%s  %s' % (' ' * lvl, '-' * len(key)))

                for datatype in datatypes:
                    print('%s- %s' % (' ' * lvl, datatype))

                if key in schema.resources_tree.keys():
                    traverse(key, lvl + 4)

        print('- %s' % 'PROJECTS')
        traverse('projects', 4)

    def _sub_experiment_values(self, sub_exp, project, experiment_type):
        self._intf._get_entry_point()

        values = []

        column = '%s/%ss/%s/id' % \
            (experiment_type.lower(), sub_exp, sub_exp)

        sub_exps = '%s/experiments?columns=ID,%s' % (self._intf._entry,
                                                     column
                                                     )

        if project is not None:
            sub_exps += '&project=%s' % project

        values = get_column(self._get_json(sub_exps), column)

        return list(set(values))

    def _resource_struct(self, name):

        return self._intf._struct

    def _resource_types(self, name):
        return list(set(self._resource_struct(name).values()))

class GraphData(object):
    def __init__(self, interface):
        self._intf = interface
        self._struct = interface._struct

    # def link(self, subjects, fields):

    #     criteria = [('xnat:subjectData/SUBJECT_ID', '=', _id)
    #                 for _id in subjects
    #                 ]
    #     criteria += ['OR']
    #     # variables = ['xnat:subjectData/SUBJECT_ID'] + fields

    #     subject_id = 'xnat:subjectData/SUBJECT_ID'

    #     for field in fields:

    #         field_tbl = self._intf.select('xnat:subjectData',
    #                                       [subject_id, field]
    #                                       ).where(criteria)
    #         head = field_tbl.headers()
    #         head.remove('subject_id')
    #         head = head[0]
    #         possible = set(field_tbl.get(head))

    #         groups = {}

    #         for val in possible:
    #             groups[val] = field_tbl.where(**{head:val}
    #                                             ).select('subject_id')

    #     return groups

    def datatypes(self, pattern='*'):
        graph = nx.DiGraph()
        graph.add_node('datatypes')
        graph.labels = {'datatypes':'datatypes'}
        graph.weights = {'datatypes':100.0}

        datatypes = self._intf.inspect.datatypes(pattern)
        namespaces = set([dat.split(':')[0] for dat in datatypes])

        for ns in namespaces:
            graph.add_edge('datatypes', ns)
            graph.weights[ns] = 70.0

            for dat in datatypes:
                if dat.startswith(ns):
                    graph.add_edge(ns, dat)
                    graph.weights[dat] = 40.0

        return graph

    def rest_resource(self, name):
        resource_types = self._intf.inspect._resource_types(name)

        graph = nx.DiGraph()
        graph.add_node(name)
        graph.labels = {name : name}
        graph.weights = {name : 100.0}

        namespaces = set([exp.split(':')[0] for exp in resource_types])

        for ns in namespaces:
            graph.add_edge(name, ns)
            graph.weights[ns] = 70.0

            for exp in resource_types:
                if exp.startswith(ns):
                    graph.add_edge(ns, exp)
                    graph.weights[exp] = 40.0

        return graph

    def field_values(self, field_name):

        search_tbl = Search(field_name.split('/')[0],
                            [field_name], self._intf
                            )

        criteria = [('%s/ID' % field_name.split('/')[0], 'LIKE', '%'), 'AND']

        dist = {}

        for entry in search_tbl.where(criteria):
            for val in entry.values():
                dist.setdefault(val, 1.0)
                dist[val] += 1

        graph = nx.Graph()
        graph.add_node(field_name)
        graph.weights = dist
        graph.weights[field_name] = 100.0

        for val in dist.keys():
            graph.add_edge(field_name, val)

        return graph

    def architecture(self, with_datatypes=True):
        graph = nx.DiGraph()
        graph.add_node('projects')
        graph.labels = {'projects' : 'projects'}
        graph.weights = {'projects' : 100.0}

        def traverse(lkw, as_lkw):

            for key in schema.resources_tree[lkw]:
                as_key = '%s_%s' % (as_lkw, key)
                weight = (1 - len(as_key*2)/100.0) * 100
                graph.add_edge(as_lkw, as_key)
                graph.labels[as_key] = key
                graph.weights[as_key] = weight

                if with_datatypes:
                    for uri in self._struct.keys():
                        if uri.split('/')[-2] == key:
                            datatype = self._struct[uri]
                            graph.add_edge(as_key, datatype)
                            graph.weights[datatype] = 10
                            graph.labels[datatype] = datatype

                traverse(key, as_key)

        traverse('projects', 'projects')

        return graph

class PaintGraph(object):
    def __init__(self, interface):
        self._intf = interface
        self.get_graph = interface._get_graph

    def architecture(self, with_datatypes=True, save=None):
        graph = self.get_graph.architecture(with_datatypes)

        plt.figure(figsize=(8,8))
        pos = graphviz_layout(graph, prog='twopi', args='')

        # node_size = [(float(graph.degree(v)) * 5)**3 for v in graph]
        # node_size = [graph.weights[v] ** 2 for v in graph]
        # node_color = [float(graph.degree(v)) for v in graph]
        # node_color = [graph.weights[v] ** 2 for v in graph]

        cost = lambda v: float(graph.degree(v)) ** 3 + \
            graph.weights[v] ** 2

        costs = norm_costs([cost(v) for v in graph], 10000)

        nx.draw(graph, pos, labels=graph.labels,
                node_size=costs, node_color=costs,
                font_size=13, font_color='orange',
                font_weight='bold', with_labels=True
                )

        plt.axis('off')

        if save is not None:
            plt.savefig(save)

        plt.show()

    def experiments(self, save=None):
        graph = self.get_graph.rest_resource('experiments')
        self._draw_rest_resource(graph, save=None)

    def assessors(self, save=None):
        graph = self.get_graph.rest_resource('assessors')
        self._draw_rest_resource(graph, save=None)

    def reconstructions(self, save=None):
        graph = self.get_graph.rest_resource('reconstructions')
        self._draw_rest_resource(graph, save=None)

    def scans(self):
        graph = self.get_graph.rest_resource('scans')
        self._draw_rest_resource(graph)

    def _draw_rest_resource(self, graph, save=None):
        plt.figure(figsize=(8,8))
        pos = graphviz_layout(graph, prog='twopi', args='')

        cost = lambda v: float(graph.degree(v)) ** 3 + \
            graph.weights[v] ** 2

        node_size = [cost(v) for v in graph]
        # node_size = [graph.weights[v] ** 2 for v in graph]
        # node_color = [float(graph.degree(v)) for v in graph]
        node_color = [cost(v) for v in graph]

        nx.draw(graph, pos,
                node_size=node_size, node_color=node_color,
                font_size=13, font_color='green', font_weight='bold'
                )

        plt.axis('off')

        if save is not None:
            plt.savefig(save)

        plt.show()

    def datatypes(self, pattern='*', save=None):
        graph = self.get_graph.datatypes(pattern)

        plt.figure(figsize=(8,8))
        pos = graphviz_layout(graph, prog='twopi', args='')

        cost = lambda v: float(graph.degree(v)) ** 3 + \
            graph.weights[v] ** 2

        node_size = [cost(v) for v in graph]
        # node_size = [graph.weights[v] ** 2 for v in graph]
        # node_color = [float(graph.degree(v)) for v in graph]
        node_color = [cost(v) for v in graph]

        nx.draw(graph, pos,
                node_size=node_size, node_color=node_color,
                font_size=13, font_color='green', font_weight='bold',
                with_labels=True
                )

        plt.axis('off')

        if save is not None:
            plt.savefig(save)

        plt.show()

    def field_values(self, field_name, save=None):
        graph = self.get_graph.field_values(field_name)

        plt.figure(figsize=(8,8))
        pos = graphviz_layout(graph, prog='twopi', args='')

        cost = lambda v: graph.weights[v]

        graph.weights[field_name] = max([cost(v) for v in graph]) / 2.0

        costs = norm_costs([cost(v) for v in graph], 10000)

        nx.draw(graph, pos,
                node_size=costs, node_color=costs,
                font_size=13, font_color='black',
                font_weight='bold', with_labels=True
                )

        plt.axis('off')

        if save is not None:
            plt.savefig(save)

        plt.show()

def norm_costs(costs, norm=1000):
    max_cost = max(costs)

    return [ (cost / max_cost) * norm for cost in costs]

# class GraphDrawer(object):
#     def __init__(self, interface):
#         self._intf = interface

#     def datatypes(self, project):
#         self._intf.connection.set_strategy('offline')

#         experiments_types = self._intf.inspect.datatypes.experiments(project)

#         labels = {project:project, 'Experiments':'Experiments'}

#         g = nx.Graph()

#         g.add_edge(project, 'Experiments', {'weight':1})

#         for exp_type in experiments_types:
#             g.add_edge('Experiments', exp_type, {'weight':8})
#             labels[exp_type] = exp_type

#         pos = nx.graphviz_layout(g, prog='twopi', args='')

#         nx.draw_networkx_nodes(g, pos,
#                                nodelist=[project],
#                                node_color='green', alpha=0.7,
#                                node_size=2500, node_shape='s')

#         nx.draw_networkx_nodes(g, pos,
#                                nodelist=['Experiments'],
#                                node_color='blue', alpha=0.7,
#                                node_size=2000, node_shape='p')

#         nx.draw_networkx_nodes(g, pos,
#                                nodelist=experiments_types,
#                                node_color='red', alpha=0.7,
#                                node_size=1500, node_shape='o')

#         nx.draw_networkx_edges(g, pos, width=2, alpha=0.5,
#                                edge_color='black')

#         nx.draw_networkx_labels(g, pos, labels,
#                                 alpha=0.9, font_size=8,
#                                 font_color='black', font_weight='bold')

#         plt.axis('off')
#         plt.show()

#         self._intf.connection.revert_strategy()

class SchemasInspector(object):
    def __init__(self, interface):
        self._intf = interface

    def __call__(self):
        self._intf.manage.schemas()

        for xsd in self._intf.manage.schemas():
            print('-'*40)
            print(xsd.upper())
            print('-'*40)
            print()

            for datatype in schema.datatypes(
                self._intf.manage.schemas._trees[xsd]):
                print('[%s]' % datatype)
                print()

                for path in schema.datatype_attributes(
                    self._intf.manage.schemas._trees[xsd], datatype):
                    print(path)

                print()

    def look_for(self, element_name, datatype_name=None):
        paths = []
        self._intf.manage.schemas._init()

        if ':' in element_name:
            for root in self._intf.manage.schemas._trees.values():
                paths.extend(schema.datatype_attributes(root, element_name))
            return paths

        for xsd in self._intf.manage.schemas():
            # nsmap = self._intf.manage.schemas._trees[xsd].nsmap

            if datatype_name is not None:
                datatypes = [datatype_name]
            else:
                datatypes = schema.datatypes(
                    self._intf.manage.schemas._trees[xsd]
                    )

            for datatype in datatypes:
                for path in schema.datatype_attributes(
                    self._intf.manage.schemas._trees[xsd], datatype):
                    if element_name in path:
                        paths.append(path)

        return paths

reason: import networkx,from networkx


text:
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Test for silx.gui.hdf5 module"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "21/09/2017"

import unittest
import tempfile
import numpy
import shutil
from ..import rawh5

class TestNumpyFile(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.tmpDirectory = tempfile.mkdtemp()

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.tmpDirectory)

    def testNumpyFile(self):
        filename = "%s/%s.npy" % (self.tmpDirectory, self.id())
        c = numpy.random.rand(5, 5)
        numpy.save(filename, c)
        h5 = rawh5.NumpyFile(filename)
        self.assertIn("data", h5)
        self.assertEqual(h5["data"].dtype.kind, "f")

    def testNumpyZFile(self):
        filename = "%s/%s.npz" % (self.tmpDirectory, self.id())
        a = numpy.array(u"aaaaa")
        b = numpy.array([1, 2, 3, 4])
        c = numpy.random.rand(5, 5)
        d = numpy.array(b"aaaaa")
        e = numpy.array(u"i \u2661 my mother")
        numpy.savez(filename, a, b=b, c=c, d=d, e=e)
        h5 = rawh5.NumpyFile(filename)
        self.assertIn("arr_0", h5)
        self.assertIn("b", h5)
        self.assertIn("c", h5)
        self.assertIn("d", h5)
        self.assertIn("e", h5)
        self.assertEqual(h5["arr_0"].dtype.kind, "U")
        self.assertEqual(h5["b"].dtype.kind, "i")
        self.assertEqual(h5["c"].dtype.kind, "f")
        self.assertEqual(h5["d"].dtype.kind, "S")
        self.assertEqual(h5["e"].dtype.kind, "U")

    def testNumpyZFileContainingDirectories(self):
        filename = "%s/%s.npz" % (self.tmpDirectory, self.id())
        data = {}
        data['a/b/c'] = numpy.arange(10)
        data['a/b/e'] = numpy.arange(10)
        numpy.savez(filename, **data)
        h5 = rawh5.NumpyFile(filename)
        self.assertIn("a/b/c", h5)
        self.assertIn("a/b/e", h5)

def suite():
    test_suite = unittest.TestSuite()
    loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
    test_suite.addTest(loadTests(TestNumpyFile))
    return test_suite

if __name__ == '__main__':
    unittest.main(defaultTest='suite')

max_stars_repo_name: vincefn/silx

max_issues_repo_name: vincefn/silx

max_forks_repo_name: payno/silx


text:
import os
import warnings
warnings.simplefilter("ignore")
import numpy as np
import pandas as pd

STORAGE_DIR = os.path.dirname(os.path.realpath(__file__))

DATA_FIELDS = ['country code', 'postal_code', 'place_name',
               'state_name', 'state_code', 'county_name', 'county_code',
               'community_name', 'community_code',
               'latitude', 'longitude', 'accuracy']

class Nominatim(object):
    """Query geographical location from a city name or a postal code
    Parameters
    ----------
    country: str, default='fr'
       country conde. See the documentation for a list of supported countries.
    """
    def __init__(self):
        self._data_path, self._data = self._get_data()
        self._data_unique = self._index_postal_codes()

    @staticmethod
    def _get_data():
        data_path = os.path.join(STORAGE_DIR,'allCountries.txt')
        if os.path.exists(data_path):
            data = pd.read_csv(data_path,
                               dtype={'postal_code': str})
        return data_path, data

    def _index_postal_codes(self):
        """ Create a dataframe with unique postal codes """
        data_path_unique = self._data_path.replace('.txt', '-index.txt')

        if os.path.exists(data_path_unique):
            data_unique = pd.read_csv(data_path_unique,
                                      dtype={'postal_code': str})
        else:
            # group together places with the same postal code
            df_unique_cp_group = self._data.groupby('postal_code')
            data_unique = df_unique_cp_group[['latitude', 'longitude']].mean()
            valid_keys = set(DATA_FIELDS).difference(
                    ['place_name', 'lattitude', 'longitude', 'postal_code'])
            data_unique['place_name'] = df_unique_cp_group['place_name'].apply(str).apply(', '.join)  # noqa
            for key in valid_keys:
                data_unique[key] = df_unique_cp_group[key].first()
            data_unique = data_unique.reset_index()[DATA_FIELDS]
            data_unique.to_csv(data_path_unique, index=None)
        return data_unique

    def _normalize_postal_code(self, codes):
        """Normalize postal codes to the values contained in the database
        For instance, take into account only first letters when applicable.
        Takes in a pd.DataFrame
        """
        codes['postal_code'] = codes.postal_code.str.upper()

        return codes

    def query_postal_code(self, codes):
        """Get locations information from postal codes
        Parameters
        ----------
        codes: array, list or int
          an array of strings containing postal codes
        Returns
        -------
        df : pandas.DataFrame
          a pandas.DataFrame with the relevant information
        """
        if isinstance(codes, int):
            codes = str(codes)

        if isinstance(codes, str):
            codes = [codes]
            single_entry = True
        else:
            single_entry = False

        if not isinstance(codes, pd.DataFrame):
            codes = pd.DataFrame(codes, columns=['postal_code'])

        codes = self._normalize_postal_code(codes)
        response = pd.merge(codes, self._data_unique, on='postal_code',
                            how='left')
        if single_entry:
            response = response.iloc[0]
        return response

    def query_location(self, name):
        """Get locations information from a community/minicipality name"""
        pass

class GeoDistance(Nominatim):
    """ Distance calculation from a city name or a postal code
    Parameters
    ----------
    data_path: str
      path to the dataset
    error: str, default='ignore'
      how to handle not found elements. One of
      'ignore' (return NaNs), 'error' (raise an exception),
      'nearest' (find from nearest valid points)
    """
    def __init__(self, country='fr', errors='ignore'):
        super(GeoDistance, self).__init__()

    def query_postal_code(self, x, y):
        """ Get distance (in km) between postal codes
        Parameters
        ----------
        x: array, list or int
          a list  of postal codes
        y: array, list or int
          a list  of postal codes
        Returns
        -------
        d : array or int
          the calculated distances
        """
        if isinstance(x, int):
            x = str(x)

        if isinstance(y, int):
            y = str(y)

        if isinstance(x, str):
            x = [x]
            single_x_entry = True
        else:
            single_x_entry = False
        df_x = super(GeoDistance, self).query_postal_code(x)

        if isinstance(y, str):
            y = [y]
            single_y_entry = True
        else:
            single_y_entry = False

        df_y = super(GeoDistance, self).query_postal_code(y)

        x_coords = df_x[['latitude', 'longitude']].values
        y_coords = df_y[['latitude', 'longitude']].values

        if x_coords.shape[0] == y_coords.shape[0]:
            pass
        elif x_coords.shape[0] == 1:
            x_coords = np.repeat(x_coords, y_coords.shape[0], axis=0)
        elif y_coords.shape[0] == 1:
            y_coords = np.repeat(y_coords, x_coords.shape[0], axis=0)
        else:
            raise ValueError('x and y must have the same number of elements')

        dist = haversine_distance(x_coords, y_coords)
        if single_x_entry and single_y_entry:
            return dist[0]
        else:
            return dist

# Copied from geopy
# IUGG mean earth radius in kilometers, from
# https://en.wikipedia.org/wiki/Earth_radius#Mean_radius.  Using a
# sphere with this radius results in an error of up to about 0.5%.
EARTH_RADIUS = 6371.009

def haversine_distance(x, y):
    """Haversine (great circle) distance
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    Parameters
    ----------
    x : array, shape=(n_samples, 2)
      the first list of coordinates (degrees)
    y : array: shape=(n_samples, 2)
      the second list of coordinates (degress)
    Returns
    -------
    d : array, shape=(n_samples,)
      the distance between corrdinates (km)
    References
    ----------
    https://en.wikipedia.org/wiki/Great-circle_distance
    """
    x_rad = np.radians(x)
    y_rad = np.radians(y)

    d = y_rad - x_rad

    dlat, dlon = d.T
    x_lat = x_rad[:, 0]
    y_lat = y_rad[:, 0]

    a = np.sin(dlat/2.0)**2 + \
        np.cos(x_lat) * np.cos(y_lat) * np.sin(dlon/2.0)**2

    c = 2 * np.arcsin(np.sqrt(a))
    return EARTH_RADIUS * c