text:
# pylint: disable=W0212, W0104
'''
Unit tests for operators.py.

These tests should NOT require MPI.
'''

import unittest as ut
from unittest.mock import Mock, MagicMock
import numpy as np

from dynamite.operators import Operator, sigmax, sigmay, sigmaz
from dynamite.operators import sigma_plus, sigma_minus, identity, zero
from dynamite import msc_tools

# TODO: add test: get_length on identity should fail without explicit L

class Fundamental(ut.TestCase):

    def check_same(self, dnm, npy):
        '''
        Helper function to check that dynamite and numpy arrays are equal, and
        print the differences if not.
        '''
        self.assertTrue(np.all(dnm == npy),
                        msg = '\n\n'.join(['\ndnm:\n'+str(dnm), '\nnpy:\n'+str(npy)]))

    def test_sigmax(self):
        dnm = msc_tools.msc_to_numpy(sigmax().msc, (2,2))
        npy = np.array([[0, 1],
                        [1, 0]])
        self.check_same(dnm, npy)

    def test_sigmay(self):
        dnm = msc_tools.msc_to_numpy(sigmay().msc, (2,2))
        npy = np.array([[0, -1j],
                        [1j,  0]])
        self.check_same(dnm, npy)

    def test_sigmaz(self):
        dnm = msc_tools.msc_to_numpy(sigmaz().msc, (2,2))
        npy = np.array([[1,  0],
                        [0, -1]])
        self.check_same(dnm, npy)

    def test_identity(self):
        dnm = msc_tools.msc_to_numpy(identity().msc, (2,2))
        npy = np.array([[1, 0],
                        [0, 1]])
        self.check_same(dnm, npy)

    def test_zero(self):
        dnm = msc_tools.msc_to_numpy(zero().msc, (2,2))
        npy = np.array([[0, 0],
                        [0, 0]])
        self.check_same(dnm, npy)

    def test_sigma_plus(self):
        dnm = msc_tools.msc_to_numpy(sigma_plus().msc, (2,2))
        npy = np.array([[0, 2],
                        [0, 0]])
        self.check_same(dnm, npy)

    def test_sigma_minus(self):
        dnm = msc_tools.msc_to_numpy(sigma_minus().msc, (2,2))
        npy = np.array([[0, 0],
                        [2, 0]])
        self.check_same(dnm, npy)

    def test_antihermitian(self):
        dtype = np.dtype([('masks', np.int32),
                          ('signs', np.int32),
                          ('coeffs', np.complex128)])
        dnm = msc_tools.msc_to_numpy(np.array([(1, 1, 1)], dtype=dtype), (2,2))
        # -1j * sigmay
        npy = np.array([[0, -1],
                        [1,  0]])
        self.check_same(dnm, npy)

class UnaryBinary(ut.TestCase):
    '''
    Check the unary and binary operator methods.
    '''

    dtype = msc_tools.msc_dtype

    def check_same_msc(self, check, target):
        check = msc_tools.combine_and_sort(check)
        target = msc_tools.combine_and_sort(np.array(target, dtype=self.dtype))
        self.assertTrue(np.array_equal(check, target),
                        msg = '\ncheck:\n'+str(check) + '\ntarget:\n'+str(target))

    def test_sum_same(self):
        dnm = sigmay() + sigmay()
        self.check_same_msc(dnm.msc, [(1, 1, 2j)])

    def test_sum_diff_op(self):
        dnm = sigmax() + sigmay()
        self.check_same_msc(dnm.msc, [(1, 0, 1), (1, 1, 1j)])

    def test_sum_diff_site(self):
        dnm = sigmay(0) + sigmay(1)
        self.check_same_msc(dnm.msc, [(1, 1, 1j), (2, 2, 1j)])

    def test_sum_number(self):
        dnm = 2 + sigmax()
        self.check_same_msc(dnm.msc, [(0, 0, 2), (1, 0, 1)])

    def test_rsum_number(self):
        dnm = sigmax() + 2
        self.check_same_msc(dnm.msc, [(0, 0, 2), (1, 0, 1)])

    def test_product_same(self):
        dnm = sigmay() * sigmay()
        self.check_same_msc(dnm.msc, [(0, 0, 1)])

    def test_product_diff_op(self):
        dnm = sigmax() * sigmay()
        self.check_same_msc(dnm.msc, [(0, 1, 1j)])

    def test_product_diff_site(self):
        dnm = sigmay(0) * sigmay(1)
        self.check_same_msc(dnm.msc, [(3, 3, -1)])

    def test_product_1x2(self):
        dnm = sigmay(0) * (sigmax(1) + sigmay(1))
        self.check_same_msc(dnm.msc, [(3, 1, 1j), (3, 3, -1)])

    def test_product_2x2(self):
        dnm = (2*sigmax(0) + 3*sigmaz(0)) * (5*sigmax(1) + 7*sigmay(1))
        self.check_same_msc(dnm.msc, [(3, 0, 10),
                                      (3, 2, 14j),
                                      (2, 1, 15),
                                      (2, 3, 21j)])

    def test_product_1x2x2(self):
        dnm = sigmax(2) * (2*sigmax(0) + 3*sigmaz(0)) * (5*sigmax(1) + 7*sigmay(1))
        self.check_same_msc(dnm.msc, [(7, 0, 10),
                                      (7, 2, 14j),
                                      (6, 1, 15),
                                      (6, 3, 21j)])

    def test_product_num(self):
        dnm = 4 * sigmay()
        self.check_same_msc(dnm.msc, [(1, 1, 4j)])

    def test_rproduct_num(self):
        dnm = sigmay() * 4
        self.check_same_msc(dnm.msc, [(1, 1, 4j)])

    def test_hopping(self):
        ladder = 0.5*(sigma_plus(0)*sigma_minus(1) + sigma_minus(0)*sigma_plus(1))
        pauli  = sigmax(0)*sigmax(1) + sigmay(0)*sigmay(1)
        self.check_same_msc(ladder.msc, pauli.msc)

from dynamite.operators import op_sum, op_product

class Sums(ut.TestCase):

    dtype = msc_tools.msc_dtype

    def check_same(self, check, target):
        check = msc_tools.combine_and_sort(check)
        target = msc_tools.combine_and_sort(np.array(target, dtype=self.dtype))
        self.assertTrue(np.array_equal(check, target),
                        msg = '\ncheck:\n'+str(check) + '\ntarget:\n'+str(target))

    def test_empty(self):
        dnm = op_sum([])
        self.check_same(dnm.msc, [])

    def test_single(self):
        dnm = op_sum([sigmay()])
        self.check_same(dnm.msc, [(1, 1, 1j)])

    def test_same(self):
        dnm = op_sum([sigmay(), sigmay()])
        self.check_same(dnm.msc, [(1, 1, 2j)])

    def test_diff_op(self):
        dnm = op_sum([sigmax(), sigmay()])
        self.check_same(dnm.msc, [(1, 0, 1), (1, 1, 1j)])

    def test_diff_site(self):
        dnm = op_sum([sigmay(0), sigmay(1)])
        self.check_same(dnm.msc, [(1, 1, 1j), (2, 2, 1j)])

    def test_large_generator(self):
        dnm = op_sum((i+1)*sigmax(i) for i in range(0,15,2))
        self.check_same(dnm.msc, [(1, 0, 1),
                                  (4, 0, 3),
                                  (16, 0, 5),
                                  (64, 0, 7),
                                  (256, 0, 9),
                                  (1024, 0, 11),
                                  (4096, 0, 13),
                                  (16384, 0, 15)])

    def test_large_same(self):
        dnm = op_sum(sigmaz(0)*sigmaz(k) for k in range(1,5))
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 5, 1),
                                  (0, 9, 1),
                                  (0, 17, 1)])

    def test_index_sum(self):
        dnm = op_sum([sigmaz(i)*sigmaz(i+1) for i in range(4)])
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 6, 1),
                                  (0, 12, 1),
                                  (0, 24, 1)])

    def test_large_repeat_product(self):
        ops = [op_sum([sigmaz(i)*sigmaz(i+k) for i in range(5-k)]) for k in range(1,5)]
        dnm = op_sum(op*op for op in ops)
        self.check_same(dnm.msc, [(0, 0, 10),
                                  (0, 5,  2),
                                  (0, 10, 2),
                                  (0, 20, 2),
                                  (0, 17, 2),
                                  (0, 30, 4),
                                  (0, 15, 4),
                                  (0, 27, 4)])

class Products(ut.TestCase):

    dtype = msc_tools.msc_dtype

    def check_same(self, check, target):
        check = msc_tools.combine_and_sort(check)
        target = msc_tools.combine_and_sort(np.array(target, dtype=self.dtype))
        self.assertTrue(np.array_equal(check, target),
                        msg = '\ncheck:\n'+str(check) + '\ntarget:\n'+str(target))

    def test_empty(self):
        dnm = op_product([])
        self.check_same(dnm.msc, [(0, 0, 1)])

    def test_single(self):
        dnm = op_product([sigmay()])
        self.check_same(dnm.msc, [(1, 1, 1j)])

    def test_same(self):
        dnm = op_product([sigmay(), sigmay()])
        self.check_same(dnm.msc, [(0, 0, 1)])

    def test_diff_op(self):
        dnm = op_product([sigmax(), sigmay()])
        self.check_same(dnm.msc, [(0, 1, 1j)])

    def test_diff_site(self):
        dnm = op_product([sigmay(0), sigmay(1)])
        self.check_same(dnm.msc, [(3, 3, -1)])

from dynamite.operators import index_sum, index_product

class IndexSum(ut.TestCase):
    '''
    Tests for the index_sum function.
    '''

    dtype = msc_tools.msc_dtype

    def check_same(self, check, target):
        check = msc_tools.combine_and_sort(check)
        target = msc_tools.combine_and_sort(np.array(target, dtype=self.dtype))
        self.assertTrue(np.array_equal(check, target),
                        msg = '\ncheck:\n'+str(check) + '\ntarget:\n'+str(target))

    def test_zero(self):
        dnm = index_sum(zero(), size = 5)
        self.check_same(dnm.msc, [])

    def test_one(self):
        dnm = index_sum(sigmax(), size = 1)
        self.check_same(dnm.msc, [(1, 0, 1)])

    def test_one_shifted(self):
        dnm = index_sum(sigmax(), size = 1, start = 2)
        self.check_same(dnm.msc, [(4, 0, 1)])

    def test_single(self):
        dnm = index_sum(sigmax(), size = 5)
        self.check_same(dnm.msc, [(1, 0, 1),
                                  (2, 0, 1),
                                  (4, 0, 1),
                                  (8, 0, 1),
                                  (16, 0, 1)])

    def test_single_start1(self):
        dnm = index_sum(sigmax(), size = 5, start = 1)
        self.check_same(dnm.msc, [(2, 0, 1),
                                  (4, 0, 1),
                                  (8, 0, 1),
                                  (16, 0, 1),
                                  (32, 0, 1)])

    def test_single_wrap(self):
        dnm = index_sum(sigmax(), size = 5, boundary = 'closed')
        self.check_same(dnm.msc, [(1, 0, 1),
                                  (2, 0, 1),
                                  (4, 0, 1),
                                  (8, 0, 1),
                                  (16, 0, 1)])

    def test_twosite(self):
        dnm = index_sum(sigmaz(0)*sigmaz(1), size = 5)
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 6, 1),
                                  (0, 12, 1),
                                  (0, 24, 1)])

    def test_twosite_start1(self):
        dnm = index_sum(sigmaz(0)*sigmaz(1), size = 5, start = 1)
        self.check_same(dnm.msc, [(0, 6, 1),
                                  (0, 12, 1),
                                  (0, 24, 1),
                                  (0, 48, 1)])

    def test_twosite_wrap(self):
        dnm = index_sum(sigmaz(0)*sigmaz(1), size = 5, boundary = 'closed')
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 6, 1),
                                  (0, 12, 1),
                                  (0, 24, 1),
                                  (0, 17, 1)])

    def test_autosize_L(self):
        o = sigmaz(0)*sigmaz(1)
        o.L = 5
        dnm = index_sum(o)
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 6, 1),
                                  (0, 12, 1),
                                  (0, 24, 1)])

    def test_toobig(self):
        with self.assertRaises(ValueError):
            index_sum(sigmaz(0) + sigmaz(4), size = 3)

    def test_size_0(self):
        with self.assertRaises(ValueError):
            index_sum(sigmaz(0), size = 0)

class IndexProduct(ut.TestCase):
    '''
    Tests for the index_product function.
    '''

    dtype = msc_tools.msc_dtype

    def check_same(self, check, target):
        check = msc_tools.combine_and_sort(check)
        target = msc_tools.combine_and_sort(np.array(target, dtype=self.dtype))
        self.assertTrue(np.array_equal(check, target),
                        msg = '\ncheck:\n'+str(check) + '\ntarget:\n'+str(target))

    def test_zero(self):
        dnm = index_product(zero(), size = 5)
        self.check_same(dnm.msc, [])

    def test_one(self):
        dnm = index_product(sigmax(), size = 1)
        self.check_same(dnm.msc, [(1, 0, 1)])

    def test_one_shifted(self):
        dnm = index_product(sigmax(), size = 1, start = 2)
        self.check_same(dnm.msc, [(4, 0, 1)])

    def test_single(self):
        dnm = index_product(sigmax(), size = 5)
        self.check_same(dnm.msc, [(31, 0, 1)])

    def test_single_start1(self):
        dnm = index_product(sigmax(), size = 5, start = 1)
        self.check_same(dnm.msc, [(62, 0, 1)])

    def test_twosite(self):
        dnm = index_product(sigmaz(0) + sigmaz(1), size = 3)
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 5, 1),
                                  (0, 0, 1),
                                  (0, 6, 1)])

    def test_twosite_start1(self):
        dnm = index_product(sigmaz(0) + sigmaz(1), size = 3, start = 1)
        self.check_same(dnm.msc, [(0, 6, 1),
                                  (0, 10, 1),
                                  (0, 0, 1),
                                  (0, 12, 1)])

    def test_autosize_L(self):
        o = sigmaz(0) + sigmaz(1)
        o.L = 3
        dnm = index_product(o)
        self.check_same(dnm.msc, [(0, 3, 1),
                                  (0, 5, 1),
                                  (0, 0, 1),
                                  (0, 6, 1)])

    def test_toobig(self):
        with self.assertRaises(ValueError):
            index_product(sigmaz(0) + sigmaz(4), size = 3)

    def test_size_0(self):
        self.assertEqual(index_product(sigmaz(0), size=0), identity())

class WithBrackets(ut.TestCase):
    '''
    Test classmethod _with_brackets
    '''

    def test_string_parens(self):
        out = Operator._with_brackets('σx + σy', '()', False)
        self.assertEqual(out, '(σx + σy)')

    def test_string_square(self):
        out = Operator._with_brackets('σx + σy', '[]', False)
        self.assertEqual(out, '[σx + σy]')

    def test_string_none(self):
        out = Operator._with_brackets('σx + σy', '', False)
        self.assertEqual(out, 'σx + σy')

    def test_tex_parens(self):
        out = Operator._with_brackets(r'\sigma_{x} + \sigma_{y}', '()', True)
        self.assertEqual(out, r'\left(\sigma_{x} + \sigma_{y}\right)')

    def test_tex_square(self):
        out = Operator._with_brackets(r'\sigma_{x} + \sigma_{y}', '[]', True)
        self.assertEqual(out, r'\left[\sigma_{x} + \sigma_{y}\right]')

    def test_tex_none(self):
        out = Operator._with_brackets(r'\sigma_{x} + \sigma_{y}', '', True)
        self.assertEqual(out, r'\sigma_{x} + \sigma_{y}')

class Properties(ut.TestCase):

    def test_max_spin_idx(self):
        o = sigmaz(4)
        self.assertEqual(o.max_spin_idx, 4)

        # check that it works again, since we save it
        self.assertEqual(o.max_spin_idx, 4)

        # make sure this changes when we change MSC
        o.msc = np.array([(2, 0, 1)], dtype = o.msc.dtype)
        self.assertEqual(o.max_spin_idx, 1)

    def test_L(self):
        o = sigmaz(4)
        self.assertEqual(o.get_length(), 5)
        self.assertIs(o.L, None)

        o.L = 7
        self.assertEqual(o.get_length(), 7)
        self.assertIs(o.L, 7)

        # too small, operator would be off the end of chain
        with self.assertRaises(ValueError):
            o.L = 4

    def test_dim(self):
        o = sigmaz(4)
        self.assertEqual(o.dim, (32, 32))

        o.L = 6
        self.assertEqual(o.dim, (64, 64))

        from dynamite.subspaces import Full

        subspace = Mock()
        subspace.get_dimension = MagicMock(return_value = 7)
        o._subspaces.append((subspace, Full()))
        self.assertEqual(o.dim, (7, 64))

        o.L = 5
        self.assertEqual(o.dim, (7, 32))
        # this should have set the subspace dimension too
        self.assertEqual(o.left_subspace.L, 5)

        o._subspaces[1] = (subspace, subspace)
        self.assertEqual(o.dim, (7, 7))

    def test_nnz(self):
        o = sigmaz(0) + sigmaz(1) + sigmax(0) + sigmax(1)
        self.assertEqual(o.nnz, 3)

    def test_density(self):
        o = sigmaz(0) + sigmaz(1) + sigmax(0) + sigmax(1)
        o.L = 4
        self.assertEqual(o.density, 0.1875)

        o.right_subspace.get_dimension = MagicMock(return_value = 8)

        self.assertEqual(o.density, 0.375)

    def test_shell(self):
        o = Operator()
        self.assertEqual(o.shell, False)

        o.shell = True
        self.assertTrue(o.shell)

        with self.assertRaises(ValueError):
            o.shell = 'gpu'

        with self.assertRaises(ValueError):
            o.shell = 'crab'

    def test_right_subspace(self):

        from dynamite.subspaces import Subspace, Full
        subspace = Mock(spec=Subspace)

        o = sigmaz()
        o.L = 5
        o.add_subspace(Full(), subspace)
        self.assertEqual(o.right_subspace.L, 5)
        o.L = 6
        self.assertEqual(o.right_subspace.L, 6)

        with self.assertRaises(ValueError):
            o.subspace

    def test_left_subspace(self):

        from dynamite.subspaces import Subspace, Full
        subspace = Mock(spec=Subspace)

        o = sigmaz()
        o.L = 5
        o.add_subspace(subspace, Full())
        self.assertEqual(o.left_subspace.L, 5)
        o.L = 6
        self.assertEqual(o.left_subspace.L, 6)

        with self.assertRaises(ValueError):
            o.subspace

    def test_subspace(self):

        from dynamite.subspaces import Subspace
        subspace = Mock(spec=Subspace)

        o = sigmaz()
        o.L = 5
        o.add_subspace(subspace)

        self.assertIs(o.left_subspace, subspace)
        self.assertIs(o.right_subspace, subspace)
        self.assertIs(o.subspace, subspace)

        self.assertEqual(o.subspace.L, 5)
        o.L = 6
        self.assertEqual(o.subspace.L, 6)

    def test_brackets(self):
        o = sigmaz()
        o.string = 'str'
        o.tex = 'tex'

        o.brackets = '()'
        self.assertEqual(o.brackets, '()')
        self.assertEqual(o.with_brackets(which = 'string'), '(str)')
        self.assertEqual(o.with_brackets(which = 'tex'), r'\left(tex\right)')

        o.brackets = '[]'
        self.assertEqual(o.brackets, '[]')
        self.assertEqual(o.with_brackets(which = 'string'), '[str]')
        self.assertEqual(o.with_brackets(which = 'tex'), r'\left[tex\right]')

        o.brackets = ''
        self.assertEqual(o.brackets, '')
        self.assertEqual(o.with_brackets(which = 'string'), 'str')
        self.assertEqual(o.with_brackets(which = 'tex'), 'tex')

        with self.assertRaises(ValueError):
            o.brackets = '<>'

class MSC(ut.TestCase):
    '''
    Test operator MSC setters, getters, etc.
    '''
    # TODO: need to isolate dtype from backend for testing
    # probably can mock it
    dtype = msc_tools.msc_dtype

    def test_set_list(self):
        o = Operator()
        o.msc = [(1, 2, 3)]
        self.assertTrue(isinstance(o.msc, np.ndarray))
        self.assertTrue(o.msc.dtype == self.dtype)
        self.assertTrue(np.all(o.msc == np.array([(1, 2, 3)], dtype=self.dtype)))

    def test_set_array(self):
        o = Operator()
        o.msc = np.array([(1, 2, 3)], dtype=self.dtype)
        self.assertTrue(isinstance(o.msc, np.ndarray))
        self.assertTrue(o.msc.dtype == self.dtype)
        self.assertTrue(np.all(o.msc == np.array([(1, 2, 3)], dtype=self.dtype)))

    def test_reduce(self):
        o = Operator()

        o.msc = [(1, 2, 3), (1, 2, 6)]
        self.assertFalse(o.is_reduced)
        o.reduce_msc()
        self.assertTrue(o.is_reduced)
        self.assertTrue(np.all(o.msc == np.array([(1, 2, 9)], dtype=self.dtype)))

        o.msc = [(1, 2, 3), (4, 5, 6)]
        self.assertFalse(o.is_reduced)
        o.is_reduced = True
        self.assertTrue(o.is_reduced)

from dynamite.operators import from_bytes
class FromBytes(ut.TestCase):

    def test_simple(self):
        test_cases = [{
            'MSC' : np.array([(1, 5, -0.2j), (0, 1, 2)],
                             dtype = msc_tools.msc_dtype),
            'serial' : b'2\n32\n' + \
                           b'\x00\x00\x00\x01\x00\x00\x00\x00' + \
                           b'\x00\x00\x00\x05\x00\x00\x00\x01' + \
                           b'\x80\x00\x00\x00\x00\x00\x00\x00\xbf\xc9\x99\x99\x99\x99\x99\x9a' + \
                           b'@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
        }]

        for t in test_cases:
            op = from_bytes(t['serial'])
            self.assertTrue(np.array_equal(op.msc, t['MSC']))

if __name__ == '__main__':
    ut.main()

max_stars_repo_name: GregDMeyer/dynamite

max_issues_repo_name: GregDMeyer/dynamite

max_forks_repo_name: GregDMeyer/dynamite


text:
import argparse
import os
import shutil
import time

import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets

import numpy as np

def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
    torch.save(state, filename)
    if is_best:
        shutil.copyfile(filename, 'model_best.pth.tar')

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def adjust_learning_rate(optimizer, epoch, base_lr):
    """Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
    lr = max(base_lr * (0.5 ** (epoch // 20)), 1e-5)
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
    #if (epoch + 1) == 51 or (epoch + 1) == 101 or (epoch + 1) == 151:
    #    #lr = lr * 0.1
    #    for param_group in optimizer.param_groups:
    #        param_group['lr'] *= 0.1

def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res

def avg_class_acc(output, target, all, correct):
    num_cls = all.shape[0]
    for i in range(num_cls):
        idx = (target == i)
        all[i] += np.sum(idx)
        correct[i] += np.sum(   output[idx] == target[idx]  )
    return all, correct

ext: py

max_stars_repo_name: PatrickFeng/RPNet

reason: import numpy


text:
import numpy as np
import cv2 as cv
import json
from time import sleep

config = {}
with open('config.json',"r+") as config_file:
    config = json.loads(''.join(config_file.readlines()))
filePath = config['filepath']

cap = cv.VideoCapture(filePath)
h1=s1=v1=h2=s2=v2=0
def on_trackbar_h1(val):
    global h1
    h1=val
    print('h1=',h1)
def on_trackbar_s1(val):
    global s1
    s1=val
    print('s1=',s1)
def on_trackbar_v1(val):
    global v1
    v1=val
    print('v1=',v1)
def on_trackbar_h2(val):
    global h2
    h2=val
    print('h2=',h2)
def on_trackbar_s2(val):
    global s2
    s2=val
    print('s2=',s2)
def on_trackbar_v2(val):
    global v2
    v2=val
    print('v2=',v2)
if config['debug']=='True':
    cv.namedWindow("test", cv.WINDOW_AUTOSIZE);
    cv.createTrackbar("h1", "test" , 0, 179, on_trackbar_h1)
    cv.createTrackbar("s1", "test" , 0, 255, on_trackbar_s1)
    cv.createTrackbar("v1", "test" , 0, 255, on_trackbar_v1)
    cv.createTrackbar("h2", "test" , 0, 179, on_trackbar_h2)
    cv.createTrackbar("s2", "test" , 0, 255, on_trackbar_s2)
    cv.createTrackbar("v2", "test" , 0, 255, on_trackbar_v2)
lower1 = (config['teams'][0]['h'][0],config['teams'][0]['s'][0],config['teams'][0]['v'][0])
upper1 = (config['teams'][0]['h'][1],config['teams'][0]['s'][1],config['teams'][0]['v'][1])
lower2 = (config['teams'][1]['h'][0],config['teams'][1]['s'][0],config['teams'][1]['v'][0])
upper2 = (config['teams'][1]['h'][1],config['teams'][1]['s'][1],config['teams'][1]['v'][1])
grass_lower = (config['grass']['h'][0],config['grass']['s'][0],config['grass']['v'][0])
grass_upper = (config['grass']['h'][1],config['grass']['s'][1],config['grass']['v'][1])

def get_most_right(team_pos):
    most_right = (0, 0)
    for pos in team_pos:
        if pos[0] > most_right[0]:
            most_right = pos
    return most_right

def get_most_left(team_pos):
    most_left = (10000000, 0)
    for pos in team_pos:
        if pos[0] < most_left[0]:
            most_left = pos
    return most_left

def get_more_left(team_pos, selected_value):
    return [pos for pos in team_pos if pos[0]<selected_value]

def get_more_right(team_pos, selected_value):
    return [pos for pos in team_pos if pos[0]>selected_value]

while True:
    ret, frame = cap.read()
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    canvas = frame.copy()
    if config['debug']=='True':
        lower = (h1,s1,v1)
        upper = (h2,s2,v2)
        mask = cv.inRange(hsv, lower, upper)
    mask1 = cv.inRange(hsv, lower1, upper1)
    mask2 = cv.inRange(hsv, lower2, upper2)
    grass_mask = cv.inRange(hsv, grass_lower, grass_upper)
    contours1, _ = cv.findContours(mask1, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
    contours2, _ = cv.findContours(mask2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
    grass_contour = max(cv.findContours(grass_mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)[0], key = cv.contourArea)
    if(cv.contourArea(grass_contour)>config["grass_min_area"]):

        M = cv.moments(grass_contour)
        grass_center = (0, 0)
        if(M["m00"]!=0):
            grass_center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
            cv.circle(frame, grass_center, 2, (0,255,0), 10)
        left_team_pos = []
        for c in contours1:
            if(cv.contourArea(c)>config["min_player_area"]):
                M = cv.moments(c)
                if(M["m00"]!=0):
                    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
                    if(cv.pointPolygonTest(grass_contour,center,False)>0):
                        cv.circle(frame, center, 2, (0,0,255), 10)
                        left_team_pos.append(center)
        right_team_pos = []
        for c in contours2:
            if(cv.contourArea(c)>config["min_player_area"]):
                M = cv.moments(c)
                if(M["m00"]!=0):
                    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
                    if(cv.pointPolygonTest(grass_contour,center,False)>0):
                        cv.circle(frame, center, 2, (255,0,0), 10)
                        right_team_pos.append(center)

        most_right_def = get_most_right(right_team_pos)
        more_right_atk = get_more_right(left_team_pos, most_right_def[0])
        most_left_def = get_most_left(left_team_pos)
        more_left_atk = get_more_left(right_team_pos, most_left_def[0])

        for more_right_attacker in  more_right_atk:
            if most_right_def[0] < more_right_attacker[0]:
                cv.circle(frame, more_right_attacker, 2, (0, 0, 0), 20)      # black
        for more_left_attacker in  more_left_atk:
            if most_left_def[0] > more_left_attacker[0]:
                cv.circle(frame, more_left_attacker, 2, (255, 255, 255), 20) # grey
    if config['debug']=='True':
        cv.imshow('mask',mask)
    cv.imshow('frame',frame)
    if cv.waitKey(config["frame_delay"]) & 0xFF == ord('q'):
        break
cap.release()
cv.destroyAllWindows()


text:
import cv2
import numpy as np

capture = cv2.VideoCapture(0)

while True:
    _return, frame = capture.read()

    width = int(capture.get(3))
    height = int(capture.get(4))

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # Displaying only the blue color objects
    lower_blue = np.array([90, 50, 50])
    upper_blue = np.array([130, 255, 255])

    # Masking the portion which is not in lower OR upper blue
    # the mask return by inRange method tells us which portion to
    # keep i.e. the portion where colours lies between upper and
    # lower blue
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # We'll use the mask and apply it to our img and keep the part
    # which lies in the upper and lower blue of the mask. So we'll
    # compare pixel by pixel and see if the pixel is blue, if yes
    # then keep it else turn it to black
    result = cv2.bitwise_and(frame, frame, mask=mask)

    # https://stackoverflow.com/a/51905557 this post will help to understand why
    # frame is used 2 time.
    # quick answer => dst(I) = sur1(I) ^sur2(I), if mask(I) != 0,

    cv2.imshow('Frame', result)
    cv2.imshow('Mask', mask)

    if cv2.waitKey(1) == ord('q'):
        break

capture.release()
capture.destroyAllWindows()

# Converting 1 pixel to HSV, but be carefull since cvtColor method needs
# an image, so while passing 1 pixel pass it in the format of (rows, columns, channels)
bgr_color = np.array([[[255, 0, 0]]])
img = cv2.cvtColor(bgr_color, cv2.COLOR_BGR2HSV)
# img[0][0] # you'll get the one pixel


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

from config import config as cfg
import numpy as np

class GroupTrack():
    def __init__(self):
        self.old_frame = None
        self.previous_landmarks_set = None

        self.with_landmark = True
        self.thres=cfg.TRACE.pixel_thres
        self.alpha=cfg.TRACE.smooth
        self.iou_thres=cfg.TRACE.iou_thres

    def calculate(self, img, now_landmarks_set):

        if self.previous_landmarks_set is None or self.previous_landmarks_set.shape[0]==0:
            self.previous_landmarks_set=now_landmarks_set
            result = now_landmarks_set

        else:
            if self.previous_landmarks_set.shape[0]==0:
                return now_landmarks_set
            else:
                result=[]

                for i in range(now_landmarks_set.shape[0]):
                    not_in_flag = True
                    for j in range(self.previous_landmarks_set.shape[0]):
                        if self.iou(now_landmarks_set[i],self.previous_landmarks_set[j])>self.iou_thres:

                            result.append(self.smooth(now_landmarks_set[i],self.previous_landmarks_set[j]))
                            not_in_flag=False
                            break
                    if not_in_flag:
                        result.append(now_landmarks_set[i])

        result=np.array(result)
        self.previous_landmarks_set=result

        return result

    def iou(self,p_set0,p_set1):
        rec1=[np.min(p_set0[:,0]),np.min(p_set0[:,1]),np.max(p_set0[:,0]),np.max(p_set0[:,1])]
        rec2 = [np.min(p_set1[:, 0]), np.min(p_set1[:, 1]), np.max(p_set1[:, 0]), np.max(p_set1[:, 1])]

        # computing area of each rectangles
        S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1])
        S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1])

        # computing the sum_area
        sum_area = S_rec1 + S_rec2

        # find the each edge of intersect rectangle
        x1 = max(rec1[0], rec2[0])
        y1 = max(rec1[1], rec2[1])
        x2 = min(rec1[2], rec2[2])
        y2 = min(rec1[3], rec2[3])

        # judge if there is an intersect
        intersect = max(0, x2 - x1) * max(0, y2 - y1)

        return intersect / (sum_area - intersect)

    def smooth(self,now_landmarks,previous_landmarks):

        result=[]
        for i in range(now_landmarks.shape[0]):

            dis = np.sqrt(np.square(now_landmarks[i][0] - previous_landmarks[i][0]) + np.square(now_landmarks[i][1] - previous_landmarks[i][1]))

            if dis < self.thres:
                result.append(previous_landmarks[i])
            else:
                result.append(self.do_moving_average(now_landmarks[i], previous_landmarks[i]))

        return np.array(result)
    def do_moving_average(self,p_now,p_previous):
        p=self.alpha*p_now+(1-self.alpha)*p_previous
        return p


text:
from __future__ import division

import numpy as np
from .fourier_transform import FourierTransform, multiplex_for_tensor_fields

def make_fft_grid(input_grid, q=1, fov=1):
	from ..field import CartesianGrid, RegularCoords

	q = np.ones(input_grid.ndim, dtype='float') * q
	fov = np.ones(input_grid.ndim, dtype='float') * fov

	# Check assumptions
	if not input_grid.is_regular:
		raise ValueError('The input_grid must be regular.')
	if not input_grid.is_('cartesian'):
		raise ValueError('The input_grid must be cartesian.')

	delta = (2*np.pi / (input_grid.delta * input_grid.dims)) / q
	dims = (input_grid.dims * fov * q).astype('int')
	zero = delta * (-dims/2 + np.mod(dims, 2) * 0.5)

	return CartesianGrid(RegularCoords(delta, dims, zero))

class FastFourierTransform(FourierTransform):
	def __init__(self, input_grid, q=1, fov=1, shift=0):
		# Check assumptions
		if not input_grid.is_regular:
			raise ValueError('The input_grid must be regular.')
		if not input_grid.is_('cartesian'):
			raise ValueError('The input_grid must be cartesian.')

		self.input_grid = input_grid

		self.shape_in = input_grid.shape
		self.weights = input_grid.weights
		self.size = input_grid.size
		self.ndim = input_grid.ndim

		self.output_grid = make_fft_grid(input_grid, q, fov).shifted(shift)

		self.shape_out = self.output_grid.shape
		self.internal_shape = (self.shape_in * q).astype('int')

		cutout_start = (self.internal_shape / 2.).astype('int') - (self.shape_in / 2.).astype('int')
		cutout_end = cutout_start + self.shape_in
		self.cutout_input = [slice(start, end) for start, end in zip(cutout_start, cutout_end)]

		cutout_start = (self.internal_shape / 2.).astype('int') - (self.shape_out / 2.).astype('int')
		cutout_end = cutout_start + self.shape_out
		self.cutout_output = [slice(start, end) for start, end in zip(cutout_start, cutout_end)]

		center = input_grid.zero + input_grid.delta * (np.array(input_grid.dims) // 2)
		if np.allclose(center, 0):
			self.shift_input = 1
		else:
			self.shift_input = np.exp(-1j * np.dot(center, self.output_grid.coords))
			self.shift_input /= np.fft.ifftshift(self.shift_input.reshape(self.shape_out)).ravel()[0] # No piston shift (remove central shift phase)

		shift = np.ones(self.input_grid.ndim) * shift
		if np.allclose(shift, 0):
			self.shift_output = 1
		else:
			self.shift_output = np.exp(-1j * np.dot(shift, self.input_grid.coords))

	@multiplex_for_tensor_fields
	def forward(self, field):
		from ..field import Field

		f = np.zeros(self.internal_shape, dtype='complex')
		f[self.cutout_input] = (field.ravel() * self.weights * self.shift_output).reshape(self.shape_in)
		res = np.fft.fftshift(np.fft.fftn(np.fft.ifftshift(f)))
		res = res[self.cutout_output].ravel() * self.shift_input

		return Field(res, self.output_grid)

	@multiplex_for_tensor_fields
	def backward(self, field):
		from ..field import Field

		f = np.zeros(self.internal_shape, dtype='complex')
		f[self.cutout_output] = (field.ravel() / self.shift_input).reshape(self.shape_out)
		res = np.fft.fftshift(np.fft.ifftn(np.fft.ifftshift(f)))
		res = res[self.cutout_input].ravel() / self.weights / self.shift_output

		return Field(res, self.input_grid)

max_stars_repo_name: dskleingeld/hcipy

max_issues_repo_name: dskleingeld/hcipy

max_forks_repo_name: dskleingeld/hcipy


text:
# -*- coding: utf-8 -*-
"""
@author: Carlos Pedro Gonçalves, University of Lisbon
"""

import numpy as np
import scipy.linalg as lalg

# =============================================================================
# svect module for  basic quantum operations using Dirac's bra-ket notation
# and NumPy's matrices
# =============================================================================

# =============================================================================
# GENERAL FUNCTIONS FOR BASIS DEFINITION AND PROJECTORS
# =============================================================================

def generateStrings(size):
    # Function to generate the bitstrings of size 2^N
    # used as auxiliary function in the basis definition

    string_list = [] # list that will hold the binary strings
    num_strings = 2**size # the number of strings is a power of 2

    # Append the strings in sequence to the string list
    for i in range(0,num_strings):
        string_list.append(np.binary_repr(i,width=size))

    # Return the binary strings list
    return string_list

def basisDef(size):
    # Definition of the basis in terms of symbolic representation
    # using Dirac's bra-ket notation

    # Generate the binary strings
    basis = generateStrings(size)

    # Get Dirac's bra-ket notation
    for i in range(0,len(basis)):
        basis[i] = '|' + basis[i] + '>'

    # Return the basis list in Dirac's bra-ket notation
    return basis

def getKet(basis,amplitudes):
    # Function to return ket vector structure as comprised
    # of a basis structure and amplitudes, takes the basis in
    # Dirac's notation and the amplitudes list as inputs and
    # outputs a list comprised of the basis and the vector

    # Define the vector in the computational basis representation as a column
    # vector from the amplitudes
    vector = np.matrix(amplitudes)
    vector = vector.getT()

    # Return the ket vector structure as a list comprised of the basis
    # and the ket column vector
    return [basis,vector]

def showKet(ket):

    # Function that prints the ket vector on the Python console
    # using Dirac's bra-ket notation

    basis = ket[0] # the basis occupies position 0 in the ket vector structure
    vector = ket[1] # the column vector occupies position 1 in the ket vector structure
    lv = len(vector) # the length of the vector

    ket_vector = "|psi> = " # the ket vector in symbolic representation

    # Represent the ket vector in Dirac's notation
    for i in range(0,lv):
        amplitude = np.asscalar(vector[i])
        if amplitude != 0:
            ket_vector = ket_vector + str(amplitude) + basis[i] + '+'
    cutPoint = len(ket_vector) - 1
    ket_vector = ket_vector[:cutPoint]

    # Print the ket vector in Dirac's notation
    print(ket_vector)

def density(ket):
    # Function that returns the density matrix from a ket vector structure

    psi = ket[1] # get the column matrix for the ket
    psi_T = psi.getH() # get the bra as the conjugate transpose

    # Return the matrix as |psi><psi| using NumPy's dot product
    return np.matrix(np.dot(psi,psi_T))

def density_vect(vector):
    # Procedure that returns the density matrix directly
    # from a column vector (if one does not wish to work
    # with Dirac's notation)
    return np.matrix(np.dot(vector,vector.getH()))

# =============================================================================
# GENERAL UNITARY GATES' FUNCTIONS
# =============================================================================

def PauliX():
    # Function that returns Pauli's X matrix |0><1| + |1><0|
    return np.matrix([[0,1],[1,0]])

def PauliY():
    # Function that returns Pauli's Y matrix -i|0><1| + i|1><0|
    return np.matrix([[0,-1j],[1j,0]])

def PauliZ():
    # Function that returns Pauli's Z matrix |0><0| - |1><1|
    return np.matrix([[1,0],[0,-1]])

def unit():
    # Function that returns the unit operator I on single qubit Hilbert Space
    # I = |0><0| + |1><1|
    return np.matrix([[1,0],[0,1]])

def WHGate():
    # Function that returns the Walsh-Haddamard gate
    return (1 / np.sqrt(2)) * (PauliZ() + PauliX())

def PShift(phi):
    # Function that returns the phase shift gate
    return np.matrix([[1,0],[0,np.exp(1j * phi)]])

def gate2x2(omega, # angle for phase
            theta, # angle for rotation
            n, # unit vector
            delta_t, # computation's time interval
            delta_to # basic processing time
            ):
    # Function that returns the general 2x2 unitary gate
    # from the Hamiltonian parameters that define the phase
    # and rotation parts of the unitary gate

    # Get the main components of the U(2) gate
    angle1 = (omega * delta_t) / (2 * delta_to) # angle for phase transform
    angle2 = (theta * delta_t) / (2 * delta_to) # angle for rotation component
    phaseTransform = np.exp(1j * angle1) # phase transform
    ns = n[0] * PauliX() + n[1] * PauliY() + n[2] * PauliZ() # product for spin direction
    rotation = np.cos(angle2) * unit() - 1j * np.sin(angle2) * ns # rotation

    # Return U(2) gate
    return phaseTransform * rotation

def tensorProd(operators_list):
    # Function that returns the operator that results from
    # the tensor product of operators from a list
    operator = operators_list[0] # first operator in the list
    for i in range(1,len(operators_list)):
        # apply the tensor product
        operator = np.matrix(np.kron(operator,operators_list[i]))
    # Return the operator
    return operator

def transformKet(operator,ket):
    # Function for the unitary transformation of a ket vector
    # returns the new ket with the usual ket structure as a list
    # comprised by the basis and the transformed amplitudes
    return [ket[0],np.dot(operator,ket[1])]

def transformDensity(operator,density):
    # Function for the unitary transformation of a density matrix

    # Get the conjugate transpose of the main operator
    operatorT = operator.getH()

    # Apply the operator on the left
    density = np.dot(operator,density)
    # Apply the conjugate transpose on the right
    density = np.dot(density,operatorT)

    # Return the density
    return density

# =============================================================================
# PROJECTORS AND QUANTUM AVERAGES
# =============================================================================

def proj2x2(value):
    # Function that returns the projectors |0><0| and |0><1|
    # if the argument is set to False it returns the projector |0><0|
    # if the argument is set to True it returns the projector |1><1|
    # NOTE: the function only accepts a Boolean argument

    if type(value) != bool:
        print("Function argument must be Boolean!")
    else:
        if value == False:
            a0 = np.matrix([1,0]) # vector <0|
            b0 = a0.getT() # vector |0>
            return np.matrix(np.outer(b0,a0)) # return projector |0><0|
        else:
            a1 = np.matrix([0,1]) # vector <1|
            b1 = a1.getT() # vector |1>
            return np.matrix(np.outer(b1,a1)) # return projector |1><1|

def genProj(size):

    # Function that returns a projectors list for each element of
    # a computational basis of size N

    projList = [] # projectors list

    strings = generateStrings(size) # binary strings for the required size

    # For each string:
    for element in strings:
        # Setup the new projector list for applying the tensor product
        newProj = []
        # For each symbol in the element...
        for symbol in element:
            # ...if the symbol is 0...
            if symbol == '0':
                # ...append the projector |0><0|...
                newProj.append(proj2x2(False))
            #...otherwise...
            else:
                #... append the projector |1><1|
                newProj.append(proj2x2(True))
        # If there is more than one qubit...
        if size > 1:
            #... get the tensor product for the projectors list
            projector = tensorProd(newProj)
            #... append the projector to the projectors' list
            projList.append(projector)
        # Otherwise...
        else:
            #... append the projector to the projectors' list
            projList = projList + newProj
    # Return the projectors' list
    return projList

def entropy(density,printouts=False):
    # Function to extract the von Neumann entropy from a density matrix

    # Get the von Neumann entropy from the density
    logDensity = lalg.logm(density)
    prod = np.dot(density,logDensity)
    s = 0 - np.trace(prod) / np.log(2)

    # Print the results if asked for
    if printouts == True:
       print("\nRho: ")
       print(density)
       print("ln(Rho): ")
       print(logDensity)
       print("\nRho*ln(Rho): ")
       print(prod)
       print("\nVon Neumann Entropy: ", s)

    # Return the von Neumann entropy
    return np.real(s)

max_stars_repo_name: cpgoncalves/qneural

max_issues_repo_name: cpgoncalves/qneural

max_forks_repo_name: cpgoncalves/qneural

reason: import numpy,import scipy


text:
import os
import cv2
from PIL import Image
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib as mpl
from copy import deepcopy
from scipy.spatial.transform import Rotation as rot
import torch
import h5py
import json
import math
import copy
from random import random

from const import PELVIS, H36M_KPTS_15, H36M_PARTS_15, KPTS_17, BODY_PARTS_17, \
        KPTS_15, OPENPOSE_PARTS_15, RADIUS, K
from data_utils import fit_to_frame

DATASET_DIR = './dataset/'

PEOPLE3D_H = 480
PEOPLE3D_W = 640

TILE_SIZE = 200

GREEN = (0, 255, 0)
RED = (255, 0, 0)

def draw_keypoints(kpts_2d, h, w):
    img = np.zeros((h, w, 3), np.uint8)

    for kpt_idx, kpt in enumerate(kpts_2d):
        kpt = tuple(kpt[:2])
        print(kpt)
        if kpt_idx == 5:
            img = cv2.circle(img, kpt, radius=1, color=(0, 255, 0), thickness=-1)
        else:
            img = cv2.circle(img, kpt, radius=1, color=(0, 0, 255), thickness=-1)

    cv2.imshow('2d keypoints', img)
    cv2.waitKey(0)

def draw_txt(txt_path):
    kpts_2d = []
    with open(txt_path) as f:
        lines = [x[:-1] for x in f.readlines()][1:]
    for line_idx, line in enumerate(lines):
        if line_idx + 1 in KPTS_15:
            kpts_2d.append([float(x) for x in line.split(' ')])
    kpts_2d = np.array([x[:3] for x in kpts_2d], dtype=np.float32)
    draw_keypoints(kpts_2d, 480, 640)

def draw_openpose(json_fpath, img_path=None):
    if img_path is None:
        img = np.ones((250, 250, 3), dtype=np.uint8) * 255
    else:
        img = cv2.imread(img_path)

    with open(json_fpath) as fjson:
        data = json.load(fjson)
    pose_2d = np.array(data['people'][0]['pose_keypoints_2d'], dtype=np.float32)
    pose_2d = np.delete(pose_2d, np.arange(2, pose_2d.size, 3))
    pose_2d = pose_2d[:30]

    for part_idx, part in enumerate(OPENPOSE_PARTS_15):
        start_point = (pose_2d[part[0]*2], pose_2d[part[0]*2+1])
        end_point = (pose_2d[part[1]*2], pose_2d[part[1]*2+1])
        if part_idx in [0, 1, 2, 3, 7, 8, 9, 10]:
            img = cv2.line(img, start_point, end_point, (255, 0, 0), thickness=2)
        else:
            img = cv2.line(img, start_point, end_point, (0, 255, 0), thickness=2)

    for idx in range(int(pose_2d.shape[0] / 2)):
        coord = (pose_2d[idx*2], pose_2d[idx*2+1])
        img = cv2.circle(img, coord, radius=1, color=(0, 0, 255), thickness=2)

    cv2.imshow('2d keypoints', img)
    cv2.waitKey(0)

### USED IN MAIN ###

def prepare_orig_img(orig_img):
    bigger_dim_size = np.max(orig_img.shape)
    scale_factor = bigger_dim_size / float(TILE_SIZE)
    new_h = int(orig_img.shape[0] / scale_factor)
    new_w = int(orig_img.shape[1] / scale_factor)
    orig_img = cv2.resize(orig_img, (new_w, new_h))

    h, w, _ = orig_img.shape
    h_off = int((TILE_SIZE - h) / 2)
    w_off = int((TILE_SIZE - w) / 2)

    full_img = np.ones((TILE_SIZE, TILE_SIZE, 3),
            dtype=np.uint8) * 255
    full_img[h_off:h_off+h, w_off:w_off+w] = orig_img
    return full_img

def draw_pose_2d(pose_2d, img_size):

    def is_zero(kpt):
        return not np.any(kpt)

    pose_2d = pose_2d[:, :2]
    pose_2d = fit_to_frame(pose_2d, TILE_SIZE)

    img = np.ones((TILE_SIZE, TILE_SIZE, 3),
            dtype=np.uint8) * 255

    for part_idx, part in enumerate(OPENPOSE_PARTS_15):
        start_point = tuple(pose_2d[part[0]])
        end_point = tuple(pose_2d[part[1]])
        if is_zero(start_point) or is_zero(end_point):
            continue
        if part_idx in [0, 1, 2, 3, 7, 8, 9, 10]:
            img = cv2.line(img, start_point, end_point,
                    (0, 0, 255), thickness=2)
        else:
            img = cv2.line(img, start_point, end_point,
                    (0, 255, 0), thickness=2)

    for kpt in pose_2d:
        if is_zero(kpt):
            continue
        img = cv2.circle(img, tuple(kpt), radius=1,
                color=(255, 0, 0), thickness=2)

    return img

def create_grid(pose_2ds, img_paths):
    img_grid = np.zeros(
            (pose_2ds.shape[0] * 2,  TILE_SIZE, TILE_SIZE, 3),
            dtype=np.uint8)
    pose_2ds = copy.deepcopy(pose_2ds)
    pose_2ds = np.squeeze(pose_2ds, axis=3)
    pose_2ds = np.swapaxes(pose_2ds, 1, 2)

    for pose_idx, pose_2d in enumerate(pose_2ds):
        orig_img = cv2.imread(img_paths[pose_idx])
        img_size = max(orig_img.shape[0], orig_img.shape[1])
        orig_img = prepare_orig_img(orig_img)

        pose_2d_img = draw_pose_2d(pose_2d, img_size)
        img_grid[2*pose_idx] = orig_img
        img_grid[2*pose_idx+1] = pose_2d_img

    return img_grid

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

if __name__ == '__main__':
    kpts_path = '/home/kristijan/phd/datasets/PETA/openpose/TownCentre/5_52_keypoints.json'
    draw_openpose(kpts_path)

max_stars_repo_name: kristijanbartol/gender-classifier

max_issues_repo_name: kristijanbartol/gender-classifier

max_forks_repo_name: kristijanbartol/gender-classifier

reason: import numpy,from scipy


text:
import csv
import sys
import numpy as np
from PIL import Image
import cv2

filename = open(sys.argv[1], 'rb')

file = csv.reader(filename)

i = 1

for row in file:
    if row[0] == 'emotion':
        continue

    image = np.fromstring(row[1], dtype=int, sep=' ').reshape(48, 48)

    location = row[2] + "/"

    cv2.imwrite(location + str(i) + ".jpg", image)

    i += 1

filename.close()

max_stars_repo_name: maikelronnau/studies

max_issues_repo_name: maikelronnau/studies

max_forks_repo_name: maikelronnau/studies


text:
from __future__ import division, absolute_import, print_function

from numpy.testing import assert_equal, TestCase, run_module_suite
from numpy.core import ones
from numpy import matrix, diagonal, diag

class TestDot(TestCase):
    def test_matscalar(self):
        b1 = matrix(ones((3, 3), dtype=complex))
        assert_equal(b1*1.0, b1)

def test_diagonal():
    b1 = matrix([[1,2],[3,4]])
    diag_b1 = matrix([[1, 4]])

    assert_equal(b1.diagonal(), diag_b1)
    assert_equal(diagonal(b1), diag_b1)
    assert_equal(diag(b1), diag_b1)

if __name__ == "__main__":
    run_module_suite()

max_stars_repo_name: Garrett-R/numpy

max_issues_repo_name: Garrett-R/numpy

max_forks_repo_name: Garrett-R/numpy

reason: from numpy


text:
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from warnings import warn

import numpy as np
import matplotlib.collections as mcoll
import matplotlib.patches as mpatches
import matplotlib.lines as mlines

from ..utils import groupby_apply, to_rgba, resolution
from ..doctools import document
from .geom import geom

@document
class geom_dotplot(geom):
    """
    Dot plot

    {usage}

    Parameters
    ----------
    {common_parameters}
    stackdir : str (default: up)
        Direction in which to stack the dots. Options are
        :py:`['up', 'down', 'center', 'centerwhole']`
    stackratio : float (default: 1)
        How close to stack the dots. If value is less than 1,
        the dots overlap, if greater than 1 they are spaced.
    dotsize : float (default: 1)
        Diameter of dots relative to ``binwidth``.
    stackgroups : bool (default: False)
        If :py:`True`, the dots are stacked across groups.

    See Also
    --------
    plotnine.stats.stat_bindot
    """
    DEFAULT_AES = {'alpha': 1, 'color': 'black', 'fill': 'black'}
    REQUIRED_AES = {'x', 'y'}
    NON_MISSING_AES = {'size', 'shape'}
    DEFAULT_PARAMS = {'stat': 'bindot', 'position': 'identity',
                      'na_rm': False, 'stackdir': 'up', 'stackratio': 1,
                      'dotsize': 1, 'stackgroups': False}

    def setup_data(self, data):
        gp = self.params
        sp = self._stat.params

        # Issue warnings when parameters don't make sense
        if gp['position'] == 'stack':
            warn("position='stack' doesn't work properly with "
                 "geom_dotplot. Use stackgroups=True instead.")
        if (gp['stackgroups'] and
                sp['method'] == 'dotdensity' and
                sp['binpositions'] == 'bygroup'):
            warn("geom_dotplot called with stackgroups=TRUE and "
                 "method='dotdensity'. You probably want to set "
                 "binpositions='all'")

        if 'width' not in data:
            if sp['width']:
                data['width'] = sp['width']
            else:
                data['width'] = resolution(data['x'], False) * 0.9

        # Set up the stacking function and range
        if gp['stackdir'] in (None, 'up'):
            def stackdots(a):
                return a - .5
            stackaxismin = 0
            stackaxismax = 1
        elif gp['stackdir'] == 'down':
            def stackdots(a):
                return -a + .5
            stackaxismin = -1
            stackaxismax = 0
        elif gp['stackdir'] == 'center':
            def stackdots(a):
                return a - 1 - np.max(a-1)/2
            stackaxismin = -.5
            stackaxismax = .5
        elif gp['stackdir'] == 'centerwhole':
            def stackdots(a):
                return a - 1 - np.floor(np.max(a-1)/2)
            stackaxismin = -.5
            stackaxismax = .5

        # Fill the bins: at a given x (or y),
        # if count=3, make 3 entries at that x
        idx = [i for i, c in enumerate(data['count'])
               for j in range(int(c))]
        data = data.iloc[idx]
        data.reset_index(inplace=True, drop=True)
        # Next part will set the position of each dot within each stack
        # If stackgroups=TRUE, split only on x (or y) and panel;
        # if not stacking, also split by group
        groupvars = [sp['binaxis'], 'PANEL']
        if not gp['stackgroups']:
            groupvars.append('group')

        # Within each x, or x+group, set countidx=1,2,3,
        # and set stackpos according to stack function
        def func(df):
            df['countidx'] = range(1, len(df)+1)
            df['stackpos'] = stackdots(df['countidx'])
            return df

        # Within each x, or x+group, set countidx=1,2,3, and set
        # stackpos according to stack function
        data = groupby_apply(data, groupvars, func)

        # Set the bounding boxes for the dots
        if sp['binaxis'] == 'x':
            # ymin, ymax, xmin, and xmax define the bounding
            # rectangle for each stack. Can't do bounding box per dot,
            # because y position isn't real.
            # After position code is rewritten, each dot should have
            # its own bounding box.
            data['xmin'] = data['x'] - data['binwidth']/2
            data['xmax'] = data['x'] + data['binwidth']/2
            data['ymin'] = stackaxismin
            data['ymax'] = stackaxismax
            data['y'] = 0
        elif sp['binaxis'] == 'y':
            # ymin, ymax, xmin, and xmax define the bounding
            # rectangle for each stack. Can't do bounding box per dot,
            # because x position isn't real.
            # xmin and xmax aren't really the x bounds. They're just
            # set to the standard x +- width/2 so that dot clusters
            # can be dodged like other geoms.
            # After position code is rewritten, each dot should have
            # its own bounding box.
            def func(df):
                df['ymin'] = df['y'].min() - data['binwidth'][0]/2
                df['ymax'] = df['y'].max() + data['binwidth'][0]/2
                return df
            data = groupby_apply(data, 'group', func)
            data['xmin'] = data['x'] + data['width'] * stackaxismin
            data['xmax'] = data['x'] + data['width'] * stackaxismax

        return data

    @staticmethod
    def draw_group(data, panel_params, coord, ax, **params):
        data = coord.transform(data, panel_params)
        fill = to_rgba(data['fill'], data['alpha'])
        color = to_rgba(data['color'], data['alpha'])
        ranges = coord.range(panel_params)

        # For perfect circles the width/height of the circle(ellipse)
        # should factor in the dimensions of axes
        bbox = ax.get_window_extent().transformed(
            ax.figure.dpi_scale_trans.inverted())
        ax_width, ax_height = bbox.width, bbox.height

        factor = ((ax_width/ax_height) *
                  np.ptp(ranges.y)/np.ptp(ranges.x))
        size = data.loc[0, 'binwidth'] * params['dotsize']
        offsets = data['stackpos'] * params['stackratio']

        if params['binaxis'] == 'x':
            width, height = size, size*factor
            xpos, ypos = data['x'], data['y'] + height*offsets
        elif params['binaxis'] == 'y':
            width, height = size/factor, size
            xpos, ypos = data['x'] + width*offsets, data['y']

        circles = []
        for xy in zip(xpos, ypos):
            patch = mpatches.Ellipse(xy, width=width, height=height)
            circles.append(patch)

        coll = mcoll.PatchCollection(circles,
                                     edgecolors=color,
                                     facecolors=fill)
        ax.add_collection(coll)

    @staticmethod
    def draw_legend(data, da, lyr):
        """
        Draw a point in the box

        Parameters
        ----------
        data : dataframe
        params : dict
        lyr : layer

        Returns
        -------
        out : DrawingArea
        """
        key = mlines.Line2D([0.5*da.width],
                            [0.5*da.height],
                            alpha=data['alpha'],
                            marker='o',
                            markersize=da.width/2,
                            markerfacecolor=data['fill'],
                            markeredgecolor=data['color'])
        da.add_artist(key)
        return da


text:
import numpy as np

from pymoo.core.problem import Problem
from pymoo.problems.multi.zdt import ZDT

class ZDT1WithGradient(ZDT):

    def __init__(self, n_var=30, **kwargs):
        super().__init__(n_var, evaluation_of=["F", "dF"], **kwargs)

    def _calc_pareto_front(self, n_pareto_points=100):
        x = np.linspace(0, 1, n_pareto_points)
        return np.array([x, 1 - np.sqrt(x)]).T

    def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        g = 1 + 9.0 / (self.n_var - 1) * np.sum(x[:, 1:], axis=1)
        f2 = g * (1 - np.power((f1 / g), 0.5))

        out["F"] = np.column_stack([f1, f2])

        if "dF" in out:
            dF = np.zeros([x.shape[0], self.n_obj, self.n_var], dtype=float)
            dF[:, 0, 0], dF[:, 0, 1:] = 1, 0
            dF[:, 1, 0] = -0.5 * np.sqrt(g / x[:, 0])
            dF[:, 1, 1:] = ((9 / (self.n_var - 1)) * (1 - 0.5 * np.sqrt(x[:, 0] / g)))[:, None]
            out["dF"] = dF

class ZDT2WithGradient(ZDT):

    def __init__(self, n_var=30, **kwargs):
        super().__init__(n_var, evaluation_of=["F", "dF"], **kwargs)

    def _calc_pareto_front(self, n_pareto_points=100):
        x = np.linspace(0, 1, n_pareto_points)
        return np.array([x, 1 - np.power(x, 2)]).T

    def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        c = np.sum(x[:, 1:], axis=1)
        g = 1.0 + 9.0 * c / (self.n_var - 1)
        f2 = g * (1 - np.power((f1 * 1.0 / g), 2))

        out["F"] = np.column_stack([f1, f2])

        if "dF" in out:
            dF = np.zeros([x.shape[0], self.n_obj, self.n_var], dtype=float)

            dF[:, 0, 0], dF[:, 0, 1:] = 1, 0
            dF[:, 1, 0] = -2 * x[:, 0] / g
            dF[:, 1, 1:] = (9 / (self.n_var - 1)) * (1 + x[:, 0] ** 2 / g ** 2)[:, None]
            out["dF"] = dF

class ZDT3WithGradient(ZDT):

    def __init__(self, n_var=30, **kwargs):
        super().__init__(n_var, evaluation_of=["F", "dF"], **kwargs)

    def _calc_pareto_front(self, n_pareto_points=100):
        regions = [[0, 0.0830015349],
                   [0.182228780, 0.2577623634],
                   [0.4093136748, 0.4538821041],
                   [0.6183967944, 0.6525117038],
                   [0.8233317983, 0.8518328654]]

        pareto_front = np.array([]).reshape((-1, 2))
        for r in regions:
            x1 = np.linspace(r[0], r[1], int(n_pareto_points / len(regions)))
            x2 = 1 - np.sqrt(x1) - x1 * np.sin(10 * np.pi * x1)
            pareto_front = np.concatenate((pareto_front, np.array([x1, x2]).T), axis=0)
        return pareto_front

    def _evaluate(self, x, out, *args, **kwargs):

        f1 = x[:, 0]
        c = np.sum(x[:, 1:], axis=1)
        g = 1.0 + 9.0 * c / (self.n_var - 1)
        f2 = g * (1 - np.power(f1 * 1.0 / g, 0.5) - (f1 * 1.0 / g) * np.sin(10 * np.pi * f1))

        out["F"] = np.column_stack([f1, f2])

        if "dF" in out:
            dF = np.zeros([x.shape[0], self.n_obj, self.n_var], dtype=float)

            dF[:, 0, 0], dF[:, 0, 1:] = 1, 0
            dF[:, 1, 0] = -0.5 * np.sqrt(g / x[:, 0]) - np.sin(10 * np.pi * x[:, 0]) - 10 * np.pi * x[:, 0] * np.cos(
                10 * np.pi * x[:, 0])
            dF[:, 1, 1:] = (9 / (self.n_var - 1)) * (1 - 0.5 * np.sqrt(x[:, 0] / g))[:, None]
            out["dF"] = dF

class MySphere(Problem):

    def __init__(self):
        super().__init__(n_var=2, n_obj=1, n_constr=1, xl=-1, xu=+1, elementwise_evaluation=True)

    def _evaluate(self, x, out, *args, **kwargs):
        out["F"] = (x ** 2).sum()
        out["G"] = (x ** 2).sum()

class SphereWithGradientAndConstraint(Problem):

    def __init__(self):
        super().__init__(n_var=2, n_obj=1, n_constr=1, xl=-1, xu=+1, elementwise_evaluation=True, autograd=False)

    def _evaluate(self, x, out, *args, **kwargs):
        out["F"] = (x ** 2).sum()
        out["G"] = ((x - 2) ** 2).sum()

        if "dF" in out:
            out["dF"] = np.array(2 * x)[None, None, :]

        if "ddF" in out:
            out["ddF"] = np.array([[2, 0], [0, 2]])[None, None, :]

        if "dG" in out:
            out["dG"] = np.array(2 * (x - 2))[None, None, :]

        if "ddG" in out:
            out["ddG"] = np.array([[2, 0], [0, 2]])[None, None, :]

class AutomaticDifferentiationProblem(Problem):

    def __init__(self, func, n_var=2, **kwargs):
        super().__init__(n_var, n_obj=1, n_constr=0, xl=-10, xu=10, type_var=np.double, elementwise_evaluation=True,
                         evaluation_of=["F", "dF", "ddF"], **kwargs)
        self.func = func

    def _evaluate(self, x, out, *args, **kwargs):
        out["F"] = self.func(x)

        import numdifftools as nd
        out["dF"] = nd.Gradient(self.func)(x)[None, None, :]
        out["ddF"] = nd.Hessian(self.func)(x)[None, None, :]

max_stars_repo_name: jarreguit/pymoo

max_issues_repo_name: jarreguit/pymoo

max_forks_repo_name: jarreguit/pymoo


text:
import numpy as np

import pytest
from ....tests.helpers import ASTROPY_GE_04_INSTALLED

if not ASTROPY_GE_04_INSTALLED:
    pytest.skip()

from ....core.tests.test_link_helpers import check_link, check_using
from ....core import ComponentLink, ComponentID

from ..link_helpers import (Galactic_to_FK5, FK4_to_FK5, ICRS_to_FK5,
                            Galactic_to_FK4, ICRS_to_FK4, ICRS_to_Galactic)

# We now store for each class the expected result of the conversion of (45,50)
# from the input frame to output frame and then from the output frame to the
# input frame.
EXPECTED = {
    Galactic_to_FK5: [(238.23062386,27.96352696),(143.12136866,-7.76422226)],
    FK4_to_FK5: [(45.87780898,50.19529421),(44.12740884,49.80169907)],
    ICRS_to_FK5: [(45.00001315,49.99999788),(44.99998685,50.00000212)],
    Galactic_to_FK4: [(237.71557513,28.11113265),(143.52337155,-7.32105993)],
    ICRS_to_FK4: [(44.12742195,49.801697),(45.87779583,50.19529642)],
    ICRS_to_Galactic: [(143.12137717,-7.76422008),(238.23062019,27.96352359)],
}

lon1, lat1, lon2, lat2 = (ComponentID('lon_in'), ComponentID('lat_in'),
                          ComponentID('lon_out'), ComponentID('lat_out'))

@pytest.mark.parametrize(('conv_class', 'expected'),list(EXPECTED.items()))
def test_conversion(conv_class, expected):

    result = conv_class(lon1, lat1, lon2, lat2)

    assert len(result) == 4

    # Check links are correct
    check_link(result[0], [lon1, lat1], lon2)
    check_link(result[1], [lon1, lat1], lat2)
    check_link(result[2], [lon2, lat2], lon1)
    check_link(result[3], [lon2, lat2], lat1)

    # Check string representation
    assert str(result[0]) == "lon_out <- " + conv_class.__name__ + ".forward_1(lon_in, lat_in)"
    assert str(result[1]) == "lat_out <- " + conv_class.__name__ + ".forward_2(lon_in, lat_in)"
    assert str(result[2]) == "lon_in <- " + conv_class.__name__ + ".backward_1(lon_out, lat_out)"
    assert str(result[3]) == "lat_in <- " + conv_class.__name__ + ".backward_2(lon_out, lat_out)"

    # Check numerical accuracy

    x = np.array([45])
    y = np.array([50])

    check_using(result[0], (x, y), expected[0][0])
    check_using(result[1], (x, y), expected[0][1])
    check_using(result[2], (x, y), expected[1][0])
    check_using(result[3], (x, y), expected[1][1])


text:
import grpc
import os
import sys
import json

sys.path.append("/cache/proto/")

import numpy as np

from tqdm import tqdm
from sklearn import metrics
from persia.embedding.data import PersiaBatch

import inference_pb2
import inference_pb2_grpc

from data_generator import make_dataloader

def get_inference_stub():
    channel = grpc.insecure_channel("localhost:7070")
    stub = inference_pb2_grpc.InferenceAPIsServiceStub(channel)
    return stub

def infer(stub, model_name, model_input):
    input_data = {"batch": model_input}
    response = stub.Predictions(
        inference_pb2.PredictionsRequest(model_name=model_name, input=input_data)
    )
    try:
        prediction = response.prediction.decode("utf-8")
        prediction = prediction.splitlines()
        prediction = [x.strip() for x in prediction]
        prediction = [x.replace(",", "") for x in prediction]
        prediction = prediction[1:-1]
        prediction = [float(x) for x in prediction]
        return prediction
    except:
        exit(1)

if __name__ == "__main__":

    test_filepath = os.path.join(
        os.path.dirname(os.path.realpath(__file__)), "data/test.npz"
    )
    loader = make_dataloader(test_filepath, batch_size=1024)
    all_pred = []
    all_label = []

    for (non_id_type_feature, id_type_features, label) in tqdm(
        loader, desc="gen batch data..."
    ):
        batch_data = PersiaBatch(
            id_type_features,
            non_id_type_features=[non_id_type_feature],
            requires_grad=False,
        )
        model_input = batch_data.to_bytes()
        prediction = infer(get_inference_stub(), "adult_income", model_input)

        assert len(prediction) == len(
            label
        ), f"Missing results: prediction length({len(prediction)}) does not match label length({len(label)})"

        all_label.append(label.data)
        all_pred.append(prediction)

    all_pred, all_label = np.concatenate(all_pred), np.concatenate(all_label)

    fpr, tpr, th = metrics.roc_curve(all_label, all_pred)
    infer_auc = metrics.auc(fpr, tpr)

    print(f"infer_auc = {infer_auc}")

    assert (
        infer_auc > 0.8927
    ), f"infer error, expect infer_auc > 0.8927 but got {infer_auc}"

max_stars_repo_name: dumpmemory/PERSIA

max_issues_repo_name: PersiaML/Persia

max_forks_repo_name: PersiaML/Persia


text:
"""Extended Memory Network."""
import numpy as np
import tensorflow as tf

class EMN(object):
    """Implementation of E-MN model."""

    def __init__(self, config):
        """Init model."""
        self.word_dim = config['word_dim']
        self.vocab_num = config['vocab_num']
        self.pretrained_embedding = config['pretrained_embedding']
        self.video_feature_dim = config['video_feature_dim']
        self.video_feature_num = config['video_feature_num']
        self.memory_dim = config['memory_dim']
        self.answer_num = config['answer_num']

        self.video_feature = None
        self.question_encode = None
        self.answer_encode = None

        self.logit = None
        self.prediction = None
        self.loss = None
        self.acc = None

        self.train = None

    def build_inference(self):
        """Build inference graph."""
        with tf.name_scope('input'):
            self.video_feature = tf.placeholder(
                tf.float32, [None, self.video_feature_num, self.video_feature_dim], 'video_feature')
            self.question_encode = tf.placeholder(
                tf.int32, [None, None], 'question_encode')

        with tf.variable_scope('process_question'):
            if self.pretrained_embedding:
                embedding_matrix = tf.get_variable(
                    'embedding_matrix',
                    initializer=np.load(self.pretrained_embedding),
                    regularizer=tf.nn.l2_loss)
            else:
                embedding_matrix = tf.get_variable(
                    'embedding_matrix', [self.vocab_num, self.word_dim],
                    regularizer=tf.nn.l2_loss)
            question_embedding = tf.nn.embedding_lookup(
                embedding_matrix, self.question_encode, name='question_embedding')
            cell = tf.nn.rnn_cell.BasicLSTMCell(self.word_dim)
            _, question_state = tf.nn.dynamic_rnn(
                cell, question_embedding, dtype=tf.float32, scope='lstm')

        with tf.variable_scope('process_video'):
            cell_fw = tf.nn.rnn_cell.BasicLSTMCell(self.video_feature_dim)
            cell_bw = tf.nn.rnn_cell.BasicLSTMCell(self.video_feature_dim)
            batch_size = tf.shape(self.video_feature)[0]
            seq_len = tf.fill(
                [1, batch_size], tf.constant(self.video_feature_num, dtype=tf.int64))[0]
            bi_video, _ = tf.nn.bidirectional_dynamic_rnn(
                cell_fw, cell_bw, self.video_feature, seq_len, dtype=tf.float32, scope='bi_lstm')
            bi_video = (bi_video[0] + bi_video[1]) / 2

        with tf.variable_scope('memory'):
            A1 = tf.get_variable(
                'A1', [self.video_feature_dim, self.memory_dim],
                regularizer=tf.nn.l2_loss)
            C1A2 = tf.get_variable(
                'C1A2', [self.video_feature_dim, self.memory_dim],
                regularizer=tf.nn.l2_loss)
            C2A3 = tf.get_variable(
                'C2A3', [self.video_feature_dim, self.memory_dim],
                regularizer=tf.nn.l2_loss)
            C3 = tf.get_variable(
                'C3', [self.video_feature_dim, self.memory_dim],
                regularizer=tf.nn.l2_loss)
            B = tf.get_variable(
                'B', [self.word_dim, self.memory_dim],
                regularizer=tf.nn.l2_loss)

            u1 = tf.matmul(question_state.c, B, name='u1')

            with tf.name_scope('hop_1'):
                with tf.name_scope('in'):
                    mem_in = tf.reshape(bi_video, [-1, self.video_feature_dim])
                    mem_in = tf.matmul(mem_in, A1)
                    mem_in = tf.reshape(
                        mem_in, [-1, self.video_feature_num, self.memory_dim])
                    mem_in = tf.nn.tanh(mem_in)
                with tf.name_scope('out'):
                    mem_out = tf.reshape(
                        bi_video, [-1, self.video_feature_dim])
                    mem_out = tf.matmul(mem_out, C1A2)
                    mem_out = tf.reshape(
                        mem_out, [-1, self.video_feature_num, self.memory_dim])
                    mem_out = tf.nn.tanh(mem_out)
                weight, _ = self.attend(u1, mem_in)

            o1 = tf.reduce_sum(
                tf.expand_dims(weight, 2) * mem_out, 1, name='o1')
            u2 = tf.add(u1, o1, name='u2')

            with tf.name_scope('hop_2'):
                with tf.name_scope('in'):
                    mem_in = tf.reshape(bi_video, [-1, self.video_feature_dim])
                    mem_in = tf.matmul(mem_in, C1A2)
                    mem_in = tf.reshape(
                        mem_in, [-1, self.video_feature_num, self.memory_dim])
                    mem_in = tf.tanh(mem_in)
                with tf.name_scope('out'):
                    mem_out = tf.reshape(
                        bi_video, [-1, self.video_feature_dim])
                    mem_out = tf.matmul(mem_out, C2A3)
                    mem_out = tf.reshape(
                        mem_out, [-1, self.video_feature_num, self.memory_dim])
                    mem_out = tf.nn.tanh(mem_out)
                weight, _ = self.attend(u2, mem_in)

            o2 = tf.reduce_sum(
                tf.expand_dims(weight, 2) * mem_out, 1, name='o2')
            u3 = tf.add(u2, o2, name='u3')

            with tf.name_scope('hop_3'):
                with tf.name_scope('in'):
                    mem_in = tf.reshape(bi_video, [-1, self.video_feature_dim])
                    mem_in = tf.matmul(mem_in, C2A3)
                    mem_in = tf.reshape(
                        mem_in, [-1, self.video_feature_num, self.memory_dim])
                    mem_in = tf.tanh(mem_in)
                with tf.name_scope('out'):
                    mem_out = tf.reshape(
                        bi_video, [-1, self.video_feature_dim])
                    mem_out = tf.matmul(mem_out, C3)
                    mem_out = tf.reshape(
                        mem_out, [-1, self.video_feature_num, self.memory_dim])
                    mem_out = tf.nn.tanh(mem_out)
                weight, _ = self.attend(u3, mem_in)

            o3 = tf.reduce_sum(
                tf.expand_dims(weight, 2) * mem_out, 1, name='o3')
            fuse = tf.add(u3, o3, name='fuse')

        with tf.variable_scope('output'):
            W = tf.get_variable(
                'W', [self.memory_dim, self.answer_num],
                regularizer=tf.nn.l2_loss)
            b = tf.get_variable('b', [self.answer_num])
            self.logit = tf.nn.softmax(
                tf.nn.xw_plus_b(fuse, W, b), name='logit')
            self.prediction = tf.argmax(self.logit, axis=1, name='prediction')

    def build_loss(self, reg_coeff):
        """Compute loss and acc."""
        with tf.name_scope('answer'):
            self.answer_encode = tf.placeholder(
                tf.int64, [None], 'answer_encode')
            answer_one_hot = tf.one_hot(
                self.answer_encode, self.answer_num)
        with tf.name_scope('loss'):
            log_loss = tf.losses.log_loss(
                answer_one_hot, self.logit, scope='log_loss')
            reg_loss = tf.add_n(
                tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES), name='reg_loss')
            self.loss = log_loss + reg_coeff * reg_loss
        with tf.name_scope("acc"):
            correct = tf.equal(self.prediction, self.answer_encode)
            self.acc = tf.reduce_mean(tf.cast(correct, "float"))

    def build_train(self, learning_rate):
        """Add train operation."""
        with tf.variable_scope('train'):
            optimizer = tf.train.AdamOptimizer(learning_rate)
            self.train = optimizer.minimize(self.loss)

    def attend(self, target, sources, name=None):
        """Use target to attend on sources. `target` and `sources` should have equal dim.

        Args:
            target: [None, target_dim].
            sources: [None, source_num, source_dim].
        Returns:
            weight: [None, source_num].
            att: [None, source_dim].
        """
        with tf.name_scope(name, 'attend'):
            weight = tf.nn.softmax(tf.reduce_sum(
                tf.expand_dims(target, 1) * sources, 2))
            att = tf.reduce_sum(
                tf.expand_dims(weight, 2) * sources, 1)
            return weight, att


text:
from pandas import read_csv, DataFrame
from numpy import asarray, transpose, array, linalg, abs, cov, reshape
from sklearn.externals import joblib
from sklearn import mixture
from sklearn.metrics import silhouette_score
from operator import itemgetter
import sympy as sp

def get_dataset(path):
    data = read_csv(path)
    frequency = 5
    step = 60 / frequency
    nw = data.shape[0] / step
    stepFloat = data.shape[0] / nw
    return data, step, nw, stepFloat

def lagrangian_calculator(sample):
    sample = asarray(sample)
    lag = list()
    for j in range(0, sample.shape[1]):
        vector = sample[:, j]
        l = len(vector)
        lam = sp.symbols('lambda', real=True)
        g = -1
        for i in range(0, l):
            g += (vector[i]/(2*lam)) ** 2
        stationary_points = sp.solve(g, lam)
        lamb = max(stationary_points)
        vector = asarray(vector)
        lag.append(vector/(2 * lamb))
    return lag

def pca_calculator(data, step, stepFloat):
    data = asarray(data)
    pca = list()
    section_start = 0
    i = 0
    while section_start <= (data.shape[0]-step):
        section_end = section_start + step
        section = data[int(section_start):int(section_end), :]
        cov_mat = cov(transpose(section))
        eig_vals, eig_vecs = linalg.eig(cov_mat)
        eig_pairs = [(abs(eig_vals[i]), eig_vecs[:, i]) for i in range(len(eig_vals))]
        eig_pairs.sort(key=itemgetter(0))
        eig_pairs.reverse()
        pr = eig_pairs[0]
        pca.append(pr[1])
        section_start = int(i * stepFloat)
        i = i + 1

    return array(pca)

def gmm_calculator(data, n_components=2):
    data = asarray(data)
    model = mixture.GaussianMixture(n_components=n_components, covariance_type='full')
    model.fit(data.astype(float))
    return model

data, step, nw, stepFloat = get_dataset('.../wind.csv')
d = DataFrame(columns=['wind_speed', 'wind_vane'])
for i in range(0, data.shape[0]):
    if data['type'][i] == 'wind_speed':
        d = d.append({'wind_speed': data['value'][i]}, ignore_index=True)

j = 0
for i in range(0, data.shape[0]):
    if data['type'][i] == 'wind_vane':
        d['wind_vane'][j] = data['value'][i]
        j = j+1

lag = lagrangian_calculator(d)
pca = pca_calculator(lag, step, stepFloat)
model = gmm_calculator(pca, n_components=3)
cluster_labels = model.predict(pca.astype(float))
score = silhouette_score(pca.astype(float), array(cluster_labels))
print(score)
filename = 'model_wind(speed,vane).sav'
joblib.dump(model, filename)

reason: from numpy,import sympy


text:
import logging
import os

import click
import neptune.new as neptune
import numpy as np
import optuna
from unityagents import UnityEnvironment

from src.agents.maddpg import MADDPGAgent
from src.config.config import settings
from src.structs import EnvFeedback

@click.group(chain=True, invoke_without_command=True)
def run():
    logging.info("Running the ml-monitoring project")

@run.command("evaluate", short_help="Run the agent based on saved weights")
def evaluate():
    logging.info("Setting up the environment for evaluation")
    env, multi_agent, scores, brain_name = setup_environment(read_saved_model=True, no_graphics=False)

    env_info = env.reset(train_mode=False)[brain_name]
    start_state = env_info.vector_observations

    score = act_during_episode(multi_agent, env, start_state, brain_name, use_noise=False)
    print(f"Evaluation score = {score}")
    env.close()

@run.command("optimize", short_help='Train with hyperparameter optimization')
def optimize():
    study = optuna.create_study(direction='maximize')
    study.optimize(run_with_suggested_settings, n_trials=200, gc_after_trial=True, n_jobs=1)

def run_with_suggested_settings(trial):
    global settings
    settings.epochs_with_noise = trial.suggest_int('epochs_with_noise', 0, 1000)
    settings.actor_learning_rate = trial.suggest_loguniform('actor_learning_rate', 1e-6, 1e-1)
    settings.critic_learning_rate = trial.suggest_loguniform('critic_learning_rate', 1e-6, 1e-1)
    settings.gamma = trial.suggest_float('gamma', 0.5, 0.9999)
    settings.tau = trial.suggest_loguniform('tau', 1e-5, 1e-1)
    settings.update_freq = trial.suggest_int('update_freq', 0, 500)
    settings.actor_size_1 = trial.suggest_int('actor_size_1', 64, 1024)
    settings.actor_size_2 = trial.suggest_int('actor_size_2', 64, 1024)
    settings.critic_size_1 = trial.suggest_int('critic_size_1', 64, 1024)
    settings.critic_size_2 = trial.suggest_int('critic_size_2', 64, 1024)
    settings.batch_size = trial.suggest_int('batch_size', 12, 2048)
    settings.buffer_size = trial.suggest_int('buffer_size', 1e5, 1e8)
    settings.alpha = trial.suggest_float('alpha', float(0), float(1))
    settings.beta = trial.suggest_float('beta', float(0), float(1))
    settings.noise_std = trial.suggest_float('noise_std', float(0), float(1))

    return objective(log=True)

@run.command("train", short_help="Train the reinforcement learning model")
@click.option(
    "-l",
    "--log",
    help="Flag whether the experiment should be logged to neptune.ai",
    required=False
)
def train(log: bool):
    best_score = objective(log)
    print(f"Best score = {best_score}")

def objective(log: bool = True):
    best_average = 0
    env, multi_agent, scores, brain_name = setup_environment(read_saved_model=False, no_graphics=True)
    use_noise = True
    if log:
        neptune_log = log_to_neptune()

    for episode in range(settings.max_epoch):
        if episode > settings.epochs_with_noise:
            use_noise = False
        env_info = env.reset(train_mode=True)[brain_name]
        multi_agent.reset()
        start_state = env_info.vector_observations
        score = act_during_episode(multi_agent, env, start_state, brain_name, use_noise)

        if log:
            neptune_log['best_average'].log(np.mean(scores[-100:]))
            neptune_log['score'].log(score)
        scores.append(score)

        episode_score = np.mean(scores[-100:])

        if episode % 1 == 0:
            print(
                f"Ep: {episode} | Score: {score:.2f} | Max: {np.max(scores):.2f} "
                f"| Avg: {episode_score:.4f}")

        if episode_score > best_average:
            best_average = episode_score
        if episode % settings.checkpoint_every == 0:
            multi_agent.save()

    if log:
        neptune_log.stop()
    multi_agent.save()
    env.close()

    return best_average

def setup_environment(read_saved_model=False, no_graphics=True):
    env = UnityEnvironment(file_name=settings.unity_env_location, no_graphics=no_graphics)
    brain_name = env.brain_names[0]
    brain = env.brains[brain_name]

    env_info = env.reset(train_mode=True)[brain_name]

    state = env_info.vector_observations[0]

    multi_agent = MADDPGAgent(state_size=len(state), action_size=brain.vector_action_space_size,
                              read_saved_model=read_saved_model)

    scores = []

    return env, multi_agent, scores, brain_name

def act_during_episode(multi_agent, env, state, brain_name, use_noise):
    score = []
    while True:
        action = multi_agent.act(state, use_noise)

        env_info = env.step(action)[brain_name]

        env_response = EnvFeedback(state, action, env_info.rewards, env_info.vector_observations,
                                   env_info.local_done)

        multi_agent.step(env_response)

        score += [env_response.reward]
        state = env_response.next_state
        if any(env_response.done):
            break
    return np.max(np.sum(np.array(score), axis=0))

def log_to_neptune():
    neptune_run = neptune.init(
        project="wsz/RL-Tenis",
        api_token=os.getenv('NEPTUNE_TOKEN')
    )

    neptune_run['parameters'] = {
        'ACTOR_LEARNING_RATE': settings.actor_learning_rate,
        'CRITIC_LEARNING_RATE': settings.critic_learning_rate,
        'GAMMA': settings.gamma,
        'TAU': settings.tau,
        'BATCH_SIZE': settings.batch_size,
        'ACTOR_MID_1': settings.actor_size_1,
        'ACTOR_MID_2': settings.actor_size_2,
        'CRITIC_CONCAT_1': settings.critic_size_1,
        'CRITIC_CONCAT_2': settings.critic_size_2,
        'EPOCHS_WITH_NOISE': settings.epochs_with_noise,
        'BUFFER_SIZE': settings.buffer_size,
        'ALPHA': settings.alpha,
        'BETA': settings.beta,
        'NOISE_STD': settings.noise_std,
        'UPDATE_FREQ': settings.update_freq
    }
    return neptune_run

if __name__ == "__main__":
    run()

max_stars_repo_name: wszenic/rl-collaborative-agents

max_issues_repo_name: wszenic/rl-collaborative-agents

max_forks_repo_name: wszenic/rl-collaborative-agents


text:
from tensorflow.keras.models import model_from_json
from tensorflow.keras.optimizers import RMSprop
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
import numpy as np
from skimage import transform
import sys

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

json_file = open('cat_vs_dogs.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("cats_vs_dogs.h5")
print("Loaded model from disk")

loaded_model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.0001), metrics=['accuracy'])

def load(img):
        np_image = Image.open(img)
        real_image = Image.open("800.jpg")

        pixels1 = np_image.getdata()
        pixels2 = real_image.getdata()
        counter = 0
        #if len(pixels1) != len(pixels2):
         #   return ["not same picture"]
        #for i in range(len(pixels1)):
         #   if pixels1[i] != pixels2[i]:
          #      counter += 1
        #if counter > 15:
         #   return ["not original"]
        np_image = np.array(np_image).astype('float32') / 255
        np_image = transform.resize(np_image, (200, 200, 3))
        np_image = np.expand_dims(np_image, axis=0)

        return np_image

image = load(sys.argv[1])
if (image[0] == "not same picture"):
    print("Nope, this isnt a good image, please check size and format")
if (image[0] == "not original"):
    print("Nope, this is not my dog at all")
predictions = loaded_model.predict(image)
print(predictions)
image_path = sys.argv[1]
img = mpimg.imread(image_path)
plt.imshow(img)
if predictions[0][0] > 0.5:
    plt.title("%.2f" % (predictions[0][0]*100) + "% dog " + image_path)
else:
    plt.title("%.2f" % ((1-predictions[0][0])*100) + "% cat " + image_path)
plt.show()

max_stars_repo_name: PoCInnovation/PoCtf

max_issues_repo_name: PoCInnovation/PoCtf

max_forks_repo_name: PoCInnovation/PoCtf


text:
import numpy as np
from PIL import Image
from scipy.special import erf

from fluid import Fluid

import pandas as pd

def normalize(df):
    blacklist = ['Race', 'Year']
    result = df.copy()
    for feature_name in df.columns:
        if(feature_name in blacklist):
            continue
        max_value = df[feature_name].max()
        min_value = df[feature_name].min()
        result[feature_name] = (((df[feature_name] - min_value) / (max_value - min_value)) + 1)*2
    return result

data = pd.read_csv("datasets/data.csv", thousands=',')
data['n'] = data['n'].astype(int)
data['First'] = data['First'].astype(int)
data['Second'] = data['Second'].astype(int)
data['Third'] = data['Third'].astype(int)
data['Fourth'] = data['Fourth'].astype(int)
data['Fifth'] = data['Fifth'].astype(int)

normalized_data = normalize(data)

gb = data.groupby('Race')
groups = [gb.get_group(x) for x in gb.groups]
all = groups[0]
black = groups[1]

gb = normalized_data.groupby('Race')
groups = [gb.get_group(x) for x in gb.groups]
normalized_all = groups[0]
normalized_black = groups[1]

normalized_data["n"] = normalized_data["n"]*3

RESOLUTION = 1200, 1200 #1200, 1200
DURATION = 1200

inflow_velocities = normalized_data.sort_values("Race")["First"].tolist()
inflow_radii = normalized_data.sort_values("Race")["n"].tolist()

INFLOW_PADDING = 1
INFLOW_DURATION = 10
INFLOW_RADIUS = 3
INFLOW_VELOCITY = 3
INFLOW_COUNT = data.shape[0] #number of years in dataset, 2020-1967

print('Generating fluid solver, this may take some time.')
fluid = Fluid(RESOLUTION, 'dye')

center = np.floor_divide(RESOLUTION, 2)
r = np.min(center) - INFLOW_PADDING

points = np.linspace(-np.pi, np.pi, INFLOW_COUNT, endpoint=False)
points = tuple(np.array((np.cos(p), np.sin(p))) for p in points)
normals = tuple(-p for p in points)
points = tuple(r * p + center for p in points)

inflow_velocity = np.zeros_like(fluid.velocity)
inflow_dye = np.zeros(fluid.shape)

i = 0
for p, n in zip(points, normals):
    print(i)
    mask = np.linalg.norm(fluid.indices - p[:, None, None], axis=0) <= inflow_radii[i]
    inflow_velocity[:, mask] += n[:, None] * inflow_velocities[i]
    i+=1
    inflow_dye[mask] = 1

frames = []
for f in range(DURATION):
    print(f'Computing frame {f + 1} of {DURATION}.')
    if f <= INFLOW_DURATION:
        fluid.velocity += inflow_velocity
        fluid.dye += inflow_dye

    curl = fluid.step()[1]
    # Using the error function to make the contrast a bit higher.
    # Any other sigmoid function e.g. smoothstep would work.
    curl = (erf(curl * 2) + 1) / 4

    color = np.dstack((curl, np.ones(fluid.shape), fluid.dye))
    color = (np.clip(color, 0, 1) * 255).astype('uint8')
    frames.append(Image.fromarray(color, mode='HSV').convert('RGB'))

print('Saving simulation result.')
frames[0].save('output.gif', save_all=True, append_images=frames[1:], duration=20, loop=0)

max_stars_repo_name: Aryan-Poonacha/stable-fluids

max_issues_repo_name: Aryan-Poonacha/stable-fluids

max_forks_repo_name: Aryan-Poonacha/stable-fluids


text:
#!/usr/bin/env python

import rospy
import numpy as np
from std_msgs.msg import String, Int16
import time
import localization

class Digital_Cube(object):

    def __init__(self):
        self.height = 0
        self.connections = 0
        self.x = 0
        self.y = 0
        self.z = 0


text:
import numpy as np
import pandas as pd

def eclat_mine(df, minsup):
    frequent = {'support': [], 'itemset': []}
    prefix = []
    for col in df.columns:
        t_col = set(df[df[col] == 1].index)
        if len(t_col) >= minsup:
            prefix.append((set(col), t_col))
    eclat(prefix, minsup, frequent)
    return pd.DataFrame(frequent)

def eclat(prefix_class, minsup, frequent):
    '''
    Finds all itemsets with support greater or equal to `minsup`.
    Function performs recursively.

    Parameters
    ----------
    prefix_class: list(tuples)
        A list in which each element is a tuple that consist of an item and
        t(item).
        The first prefix class is actually the whole itemset supposed
        to be mined. Each element of the itemset shares the prefix class
        with the empty item.
        Notice that the second item of the tuple of each item in
        `prefix_class` should be all transactions in which the item appears.
        That is, it is supposed that the item appears at least once in the
        data set.
    minsup: int
        The minimum support threshold for which an itemset is frequent
        in the data base.
    frequent: pd.DataFrame
        Two columns: 'support', 'itemset'. Only frequent itemsets.

    Returns
    -------
        None. Only modifies `frequent`.
    '''

    for i in range(len(prefix_class)):
        first_item = prefix_class[i]
        frequent['support'].append(len(first_item[1]))
        frequent['itemset'].append(first_item[0])
        new_prefix_class = []

        for j in range(i+1, len(prefix_class)):
            second_item = prefix_class[j]
            t_union = first_item[1].intersection(second_item[1])
            sup_union = len(t_union)

            if sup_union >= minsup:
                uni_item = first_item[0].union(second_item[0])
                new_prefix_class.append((uni_item, t_union))
        if new_prefix_class:
            eclat(new_prefix_class, minsup, frequent)


text:
import os
import numpy as np
from demo_utils import plot_image
import svmbir

"""
This file demonstrates the generation of a Shepp-Logan phantom followed by sinogram projection and reconstruction using MBIR.
The phantom, sinogram, and reconstruction are then displayed.
"""

# Simulated image parameters
num_rows_cols = 256 # assumes a square image

# Simulated sinogram parameters
num_views = 144
tilt_angle = np.pi/2 # Tilt range of +-90deg

# Reconstruction parameters
T = 0.1
p = 1.1
sharpness = 0.0
snr_db = 40.0

# Display parameters
vmin = 1.0
vmax = 1.2

# Generate phantom with a single slice
phantom = svmbir.phantom.gen_shepp_logan(num_rows_cols, num_rows_cols)
phantom = np.expand_dims(phantom, axis=0)

# Generate array of view angles form -180 to 180 degs
angles = np.linspace(-tilt_angle, tilt_angle, num_views, endpoint=False)

# Generate sinogram by projecting phantom
sino = svmbir.project(angles, phantom, num_rows_cols )

# Determine resulting number of views, slices, and channels
(num_views, num_slices, num_channels) = sino.shape

# Perform MBIR reconstruction
recon = svmbir.recon(sino, angles, T=T, p=p, sharpness=sharpness, snr_db=snr_db)

# Compute Normalized Root Mean Squared Error
nrmse = svmbir.phantom.nrmse(recon[0], phantom[0])

# create output folder
os.makedirs('output', exist_ok=True)

# display phantom
plot_image(phantom[0], title='Shepp Logan Phantom', filename='output/shepp_logan_phantom.png', vmin=vmin, vmax=vmax)

# display sinogram
plot_image(np.squeeze(sino), title='Sinogram', filename='output/shepp_logan_sinogram.png')

# display reconstruction
title = f'Reconstruction with NRMSE={nrmse:.3f}.'
plot_image(recon[0], title=title, filename='output/shepp_logan_recon.png', vmin=vmin, vmax=vmax)

input("press Enter")


text:
# Authors: Robert Luke <mail@robertluke.net>
#
# License: BSD (3-clause)

from ._roi import glm_region_of_interest
from ._statsmodels import statsmodels_to_results
from ._glm_level_first import (RegressionResults, ContrastResults,
                               run_glm, compute_contrast, run_GLM,
                               read_glm)

max_stars_repo_name: AiAiHealthcare/mne-nirs

max_issues_repo_name: AiAiHealthcare/mne-nirs

max_forks_repo_name: AiAiHealthcare/mne-nirs

reason: import statsmodels