text:
import torch.nn as nn
import torch.nn.functional as F
import math
import torch
import torch.optim as optim
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
from copy import deepcopy
from sklearn.metrics import f1_score
import scipy.sparse as sp
from MRGraph.utils import *

class GraphConv(Module):
    def __init__(self, in_features, out_features, with_bias=True):
        super(GraphConv, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(torch.FloatTensor(in_features, out_features))
        if with_bias:
            self.bias = Parameter(torch.FloatTensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input, adj):
        if input.data.is_sparse:
            support = torch.spmm(input, self.weight)
        else:
            support = torch.mm(input, self.weight)
        output = torch.spmm(adj, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

class ResGraphConv(Module):
    def __init__(self, in_features, out_features, with_bias=True):
        super(ResGraphConv, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(torch.FloatTensor(in_features, out_features))

        identity = sp.eye(in_features)
        self.identity = sparse_mx_to_torch_sparse_tensor(identity)

        if with_bias:
            self.bias = Parameter(torch.FloatTensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input, adj):
        if input.data.is_sparse:
            support = torch.spmm(input, (self.weight+self.identity))
        else:
            support = torch.mm(input, (self.weight+self.identity))
        output = torch.spmm(adj, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

class DenseGraphConv(Module):
    def __init__(self, nblocks,in_features, out_features, with_bias=True):
        super(DenseGraphConv, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.nblocks=nblocks

        blocks =[]
        for i in range(nblocks):
            weight = Parameter(torch.FloatTensor(in_features, out_features))
            identity = sp.eye(in_features)
            identity = sparse_mx_to_torch_sparse_tensor(identity)
            weight = weight+identity
            blocks.append(weight)

        self.weight = blocks[0]
        self.blocks = blocks

        if with_bias:
            self.bias = Parameter(torch.FloatTensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input, adj):
        if input.data.is_sparse:
            support = torch.spmm(input, self.blocks[0])
            for i in range(self.nblocks-1):
                support = torch.spmm(support, self.blocks[i+1])
        else:
            support = torch.mm(input, self.blocks[0])
            for i in range(self.nblocks-1):
                support = torch.mm(support, self.blocks[i+1])
        output = torch.spmm(adj, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

class MomGraphConv(Module):
    def __init__(self, nblocks,nnodes,in_features, out_features, device, with_bias=True):
        super(MomGraphConv, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.nblocks=nblocks
        self.device=device
        self.velocity = torch.ones(nnodes,in_features)
#         self.velocity = velocity.to(self.device)
#         self.velocity = sparse_mx_to_torch_sparse_tensor(velocity)
        self.gamma = .1#gamma

        blocks =[]
        for i in range(nblocks):
            weight = Parameter(torch.FloatTensor(in_features, out_features))
#             weight = weight.to(self.device)
            blocks.append(weight)

        self.weight = blocks[0]
        self.blocks = blocks

        if with_bias:
            self.bias = Parameter(torch.FloatTensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input, adj):
#         print('device = ',self.device)
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        velocity = self.velocity.to(device)
#         print('before loop, velocity on cuda? ',velocity.is_cuda)
        if input.data.is_sparse:

            for i in range(self.nblocks):
                velocity = (1 - self.gamma) * torch.spmm(input, self.blocks[i]) + self.gamma * velocity
                x = velocity+input
        else:
            for i in range(self.nblocks):
                velocity = self.gamma * velocity
#                 print('velocity type: ', type(velocity))
#                 print('velocity shape: ', velocity.shape)
#                 print('is velocity cuda? ',velocity.is_cuda)
#                 print('velo')
                test = (1 - self.gamma) * torch.spmm(input, self.blocks[i])
#                 exit()
#                 print('test type: ',type(test))
#                 print('test shape: ',test.shape)
#                 print('is test cuda? ',test.is_cuda)
#                 print()
#                 velocity = velocity + (1 - self.gamma) * torch.spmm(input, self.blocks[i])
#                 exit()
                velocity= velocity + test
#                 exit()
                x = velocity+input
#                 exit()
        x = torch.spmm(adj,x)
        exit()
        if self.bias is not None:
            return x + self.bias
        else:
            return x

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

max_stars_repo_name: ZhuFanCheng/Thesis

max_issues_repo_name: ZhuFanCheng/Thesis

max_forks_repo_name: ZhuFanCheng/Thesis

reason: import scipy


text:
import scipy
import scipy.stats
import numpy as np

def costFunction(guess, params):
    """Function used to optimise parameters in sample generation.

    Parameters
    ----------
    guess: list
        List of 3 values used as H1, H2 and gamma respectively in this script.
        Must all be > 0.
        Must also be such that delta1 and delta2 > 0 too.
    params: list
        List of 6 objects.
            alpha1, alpha2: floats
            rho: float
            wu: weights to use in gauss legendre integration of inverse gamma distribution function
            xu, xv: x values to use in gauss legendre integration of inverse gamma distribution function
            a, b: limits of gauss legendre integration. Normally 0 and 1 respectively

    Returns
    ----------
    F: float
        function returns 0 when guess is correct. F represents absolute error
    """
    alpha1, alpha2, rho, wu, xu, xv, a, b = params
    H1 = guess[0]
    H2 = guess[1]
    gamma = guess[2]
    delta1 = alpha1-H1-gamma
    delta2 = alpha2-H2-gamma
    if (H1<0) or (H2<0) or (gamma<0) or (delta1<0) or (delta2<0):
        F   = 1e+6
    else:
        if rho >= 0:
            x1 = xu
            x2 = xv
        else:
            x1 = 1-xu
            x2 = 1-xv

        #EV represents gauss legendre integration of inverse cumulative normal distribution functions multiplied together.
        EV = sum(
            scipy.stats.gamma.ppf(x1, H1, scale=1) * scipy.stats.gamma.ppf(x2, H2, scale=1) * wu
        )*0.5*(b-a)

        F = abs(
            (EV - H1*H2 + gamma) / np.sqrt(alpha1*alpha2) - rho
        )

    return F

def generateSamples(mu=[1,1],sigma=[1,1],rho=0,size=10000,glPoints=25):
    """Function used to generate correlated bivariate gamma variables given mean, standard deviation of each and correlation

    Parameters
    ----------
    mu: list
        List of the means of two variables
    sigma: list
        List of the standard deviation of the two variables
    rho: float
        Correlation between both random variables
    size: integer
        Number of sampels to generate
    glPoints: integer
        Number of points to use in gausse legendre integration.
        Literature suggests 25 points provide a good balance in accuracy and speed.

    Returns
    ----------
    X: list
        List containing 2 elements.
        Each is an array of length size.
        The mean and standard deviation should match the specified inputs.
        The correlation between the elements should match the input correlation.
    """
    #converting mean and standard deviation to theta and k values.
    #assumes gamma distribution of the form f(x) = (1/(Gamma(k)*theta^k))*x^(k-1)*exp(-x/theta)
    theta = [
        (sigma[0]**2)/mu[0],
        (sigma[1]**2)/mu[1]
    ]

    k = [
        mu[0]/theta[0],
        mu[1]/theta[1]
    ]

    #generating gauss legendre integration params
    b, a = 1, 0
    xu, wu = np.polynomial.legendre.leggauss(glPoints)
    xu = 0.5*(xu + 1)*(b - a) + a
    if rho >= 0:
        xv = xu
    else:
        xv = (1-xu)

    #initial guess
    H = [k[0]/3, k[1]/3]
    gamma = min(H)
    x0 = [H[0], H[1], gamma]

    #optimise params
    paramsOptimisation = scipy.optimize.minimize(
        fun = costFunction,
        x0 = x0,
        args = [k[0],k[1],rho,wu,xu,xv,a,b],
        tol = 1e-12,
        method = 'Nelder-Mead'
    )

    optimalParams = paramsOptimisation.x

    #store optimal params
    H[0] = optimalParams[0]
    H[1] = optimalParams[1]
    gamma = optimalParams[2]
    delta = [
        k[0]-H[0]-gamma,
        k[1]-H[1]-gamma
    ]

    #generate uniform random variables
    U = np.random.uniform(size=size)
    Uz = np.random.uniform(size=size)
    Uw1 = np.random.uniform(size=size)
    Uw2 = np.random.uniform(size=size)

    if rho >= 0:
        V = U
    else:
        V = 1 - U

    #generate intermediate gamma variables
    TU = scipy.stats.gamma.ppf(U, H[0], scale=1)
    TV = scipy.stats.gamma.ppf(V, H[1], scale=1)
    Z = scipy.stats.gamma.ppf(Uz, gamma, scale=1)
    W1 = scipy.stats.gamma.ppf(Uw1, delta[0], scale=1)
    W2 = scipy.stats.gamma.ppf(Uw2, delta[1], scale=1)

    #generate final gamma variables
    X = [((TU + Z + W1)*theta[0]), ((TV + Z + W2)*theta[1])]

    return X

ext: py

max_stars_repo_name: blaiseem/bivariateGamma

max_issues_repo_name: blaiseem/bivariateGamma

max_forks_repo_name: blaiseem/bivariateGamma

reason: import numpy,import scipy


text:
'''

MPI STRONG AND WEAK SCALING
Tables and visualization

Tests strong and weak scaling for a given mesh or set of meshes.

'''

import argparse
import numpy as np
import sys
import matplotlib
from copy import deepcopy
import subprocess
from matplotlib.ticker import MultipleLocator, FuncFormatter

matplotlib.use("Agg")

from matplotlib import pyplot as plt
import os

def get_node_count(infile):
    '''
    Returns the node count of an AVS-UCD mesh.
    '''
    assert infile.split('.')[-1].lower() == 'inp','Must be an AVS-UCD mesh'

    with open(infile,'r') as f:
        header = f.readline().strip().split()

    return int(header[0])

def get_runtime(exe,runtime_file='._runtime'):
    '''
    Returns the runtime of an executable.
    '''
    with open(os.path.join(os.path.dirname(exe),runtime_file),'r') as rtf:
        elapsed_time = float(rtf.read())
    return elapsed_time

def strong_scaling(infile,mpi_nodes=[1,2,4,8,16,32,64],outtypes=['fehm','pflotran','tough2','hdf5'],imgout=None,view=True,exe=None):
    '''
    Generates a strong scaling graph and STDOUT table.

    :param infile: Mesh to test against
    :type infile: str
    :param mpi_nodes: A list containing the proc scaling
    :type mpi_nodes: list<int>
    :param outtypes: A list of the output formats to test on
    :type outtypes: list<str>
    :param imgout: Path to save scaling image to
    :type imgout: str
    :param view: View the image on screen or not
    :type view: bool
    :param exe: path to VORONOI executable
    :type exe: str
    '''

    # Set up initial parameters
    ext = infile.split('.')[-1].lower()

    #if otype is None: otype = 'fehm'
    if exe is None: exe = '../voronoi'
    outfile = '._temp'

    # Verify that the correct filetype is being passed in
    # Change to exclusively avs?
    assert ext in ['inp','lgi'],'Unsupported file format!'
    ext = 'avs' if ext == 'inp' else 'lg'

    # Construct the Matplotlib figure
    fig, ax = plt.subplots()

    total_runtimes = []

    colors = ['black','red','green','yellow']

    mesh_nodes = get_node_count(infile)
    for (k,otype) in enumerate(outtypes):
        runtimes = []
        cmds = ['mpiexec -np %d %s -%s %s -o %s -type %s' % (proc,exe,ext,infile,outfile,otype) for proc in mpi_nodes]
        for (ll,cmd) in enumerate(cmds):
            print(cmd)
            try:
                out = subprocess.check_output(cmd.split(),stderr=subprocess.STDOUT)
                if 'error' in out.lower():
                    print('ERROR')
                    raise Exception
            except subprocess.CalledProcessError as e:
                print('sdf')
                raise

            runtimes.append(get_runtime(exe))

        # Calculate the scaling efficiency as a function of:
        # e(N) = t(1) / (N * t(N)) * 100%
        scaling_efficiency = [100.*runtimes[0]/((i+1)*runtimes[i]) for i in range(len(runtimes))]
        total_runtimes.append(runtimes)

        ax.plot(mpi_nodes, scaling_efficiency, color=colors[k], linestyle='solid', marker='o', markerfacecolor=colors[k], markersize=3, label=otype.upper())

    plt.xticks(mpi_nodes,[("%.0f" % x)  for x in mpi_nodes])
    ax.set_xlabel("Number of processing elements")
    ax.set_ylabel("Parallel Efficiency (% of linear scaling)")
    ax.set_title("Parallel Scaling: Strong Scaling on $10^6$ Vertex Mesh")
    legend = ax.legend(loc='upper right', shadow=False, fontsize='medium')

    ax.grid(True)

    # Save the figure?
    if imgout is not None:
        plt.savefig(imgout,format='eps', dpi=1000)

    for j in range(len(mpi_nodes)):
        a += '  %2d     ' % mpi_nodes[j] + '    '.join(['%4.4f' % a[i][j] for i in range(len(a))])

    a = '''
   strong scaling efficiency
         meshinput.inp
===============================
   n         t(n)       E_fehm(n)
-------------------------------
   1        0.763s     1.0000
   2        0.763s     0.7540
   4        0.763s     0.7540
   8        0.763s     0.7540
   16       0.763s     0.7540
   32       0.763s     0.7540
   64       0.763s     0.7540'''

    # Show the figure?
    if view:
        plt.show()

strong_scaling("/scratch/sft/livingston/voronoi_lagrit/test/2D/mpi_test_1_delete/mesh.inp",imgout="TESTME.eps")

'''
if (__name__ == "__main__"):

    # Check this out for 'true' SM vis: https://github.com/nschloe/betterspy

    #applet_description =

    parser = argparse.ArgumentParser(description=applet_description,formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('filename',help='input filename (.stor; .lgi,.avs.)',type=str)
    parser.add_argument('-c','--cmap',help='Name of a PyPlot colormap (default: \'binary\')',type=str,default='binary')
    parser.add_argument('-s','--save',help='Filepath to save image (default: visualize only)',type=str)
    parser.add_argument('-v','--view',help='Flag to display image (default: true)',type=str,default='true')
    parser.add_argument('-b','--binary',help='Flag to view sparse matrix as binary or gradient (default: true)',type=str,default='true')
    args = parser.parse_args()

    if args.filename is None:
        parser.print_help()
        sys.exit()

    view = (args.view.lower()[0] == 't' or args.view.lower()[0] == 'y' or args.view.lower()[0] == '1')
    binary = (args.binary.lower()[0] == 't' or args.view.lower()[0] == 'y' or args.view.lower()[0] == '1')

    m = get_sparse_matrix(args.filename)
    #m = np.random.rand(100,100)*100.
    plot_sparse_matrix(m,outfile=args.save,binary=binary,cmap=args.cmap,viewfig=view)
'''

max_stars_repo_name: daniellivingston/voronoi

max_issues_repo_name: daniellivingston/voronoi

max_forks_repo_name: daniellivingston/voronoi

reason: import numpy


text:
import numpy as np
import math

def p(x, w):
    return 1./(1.+math.exp(-np.dot(x,w)))

samples = [[.5,.7,1.],[.1,.5,1.],[.3,.6,1.],[.2,.8,1.],
           [.17,.17,1.],[.2,.3,1.],[.3,.4,1.],[.05,.2,1.],
           [.2,.3,1.],[.8,.3,1.],[.5,.2,1.],[.7,.2,1.],
           [.9,.1,1.],[.8,.4,1.],[.6,.5,1.],[.5,.4,1.]]
labels = [0.,0.,0.,0.,0.,0.,0.,0.,0.,1.,1.,1.,1.,1.,1.,1.]

n = len(samples)
X = np.matrix(samples)
y = np.matrix(labels).T
wk = np.matrix([[.3], [.7], [-.02]]) # small random numbers

l = -0.001 # regularization coefficient
for _ in range(5):
    pk = np.matrix([p(xi,wk) for xi in samples]).T
    Vk = np.matrix(np.diag([pk[i,0]*(1.-pk[i,0]) for i in range(n)]))
    wk += np.linalg.inv(X.T*(Vk-l*np.matrix(np.identity(n)))*X)*(X.T*(y-pk) + l*wk)

print(wk)

max_stars_repo_name: ssloy/least-squares-course

max_issues_repo_name: ssloy/least-squares-course

max_forks_repo_name: ssloy/least-squares-course


text:
#!/usr/bin/env python3

import sys
import os
from time import time
import numpy as np
from wearebeautiful.utils import get_fast_bbox

import pymesh
import click

@click.command()
@click.argument("src_file", nargs=1)
def invert(src_file):

    mesh = pymesh.meshio.load_mesh(src_file);
    bbox = get_fast_bbox(mesh)

    print("model %s" % src_file)
    print(" vertices: %8s" % ("{:,}".format(mesh.num_vertices)))
    print("    faces: %8s" % ("{:,}".format(mesh.num_faces)))
    print("dimension: (%-.5f %-.5f %-.5f)" % (bbox[1][0] - bbox[0][0],
                                  bbox[1][1] - bbox[0][1],
                                  bbox[1][2] - bbox[0][2]))
    print("bbox: [%.4f,%.4f,%.4f - %.4f,%.4f,%.4f]" % (bbox[0][0],
                                                       bbox[0][1],
                                                       bbox[0][2],
                                                       bbox[1][0],
                                                       bbox[1][1],
                                                       bbox[1][2]))
    print("is manifold: ", mesh.is_manifold())
    print("is edge manifold: ", mesh.is_edge_manifold())
    print("is vertex manifold: ", mesh.is_vertex_manifold())
    print("is closed: ", mesh.is_closed())
    print("is oriented: ", mesh.is_oriented())
    print("attribute names: ", mesh.attribute_names)

def usage(command):
    with click.Context(command) as ctx:
        click.echo(command.get_help(ctx))

if __name__ == "__main__":
    print("dump_mesh.py running, made with love and netherlands's finest in Delft. <3\n")
    invert()
    sys.exit(0)

max_issues_repo_name: wearebeautiful/wearebeautiful-tools

max_forks_repo_name: wearebeautiful/wearebeautiful-tools


text:
#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取图片
img = cv2.imread('test01.jpg')

#高斯滤波
result = cv2.medianBlur(img, 3)

#显示图像
cv2.imshow("source img", img)
cv2.imshow("medianBlur", result)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()


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

import cv2
import numpy as np

from ikalog.scenes.scene import Scene
from ikalog.utils import *

# Tracker the control tower (or rainmaker)

class ObjectiveTracker(Scene):
    # 720p サイズでの値
    tower_width = 580
    tower_left = 1280 / 2 - tower_width / 2
    tower_top = 78
    tower_height = 88

    tower_line_top = 93
    tower_line_height = 5

    def tower_pos(self, context):
        img = context['engine']['frame'][self.tower_line_top:self.tower_line_top +
                                         self.tower_line_height, self.tower_left:self.tower_left + self.tower_width]
        img2 = cv2.resize(img, (self.tower_width, 100))
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        for i in range(2):
            img2[20:40, :, i] = cv2.resize(
                img_hsv[:, :, 0], (self.tower_width, 20))
            img2[40:60, :, i] = cv2.resize(
                img_hsv[:, :, 1], (self.tower_width, 20))
            img2[60:80, :, i] = cv2.resize(
                img_hsv[:, :, 2], (self.tower_width, 20))

        # ゲージのうち信頼できる部分だけでマスクする
        img3 = np.minimum(img, self.ui_tower_mask)

        # 白い部分にいまヤグラ/ホコがある
        img3_hsv = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
        white_mask_s = cv2.inRange(img3_hsv[:, :, 1], 0, 8)
        white_mask_v = cv2.inRange(img3_hsv[:, :, 2], 248, 256)
        white_mask = np.minimum(white_mask_s, white_mask_v)
        x_list = np.arange(self.tower_width)
        tower_x = np.extract(white_mask[3, :] > 128, x_list)
        tower_xPos = np.average(tower_x)

        # FixMe: マスクした関係が位置がずれている可能性があるので、適宜補正すべき

        xPos_pct = (tower_xPos - self.tower_width / 2) / \
            (self.tower_width * 0.86 / 2) * 100

        # あきらかにおかしい値が出たらとりあえず排除
        if xPos_pct < -120 or 120 < xPos_pct:
            xPos_pct = Nan

        return xPos_pct

    def match_no_cache(self, context):
        if self.is_another_scene_matched(context, 'GameTimerIcon') == False:
            return False

        if context['game']['rule'] is None:
            return None

        rule_id = IkaUtils.rule2id(context['game']['rule'])
        applicable_modes = ['yagura', 'hoko']
        if not (rule_id in applicable_modes):
            return None

        xPos_pct = self.tower_pos(context)

        if not 'tower' in context['game']:
            context['game']['tower'] = {
                'pos': 0,
                'max': 0,
                'min': 0,
            }

        if xPos_pct != xPos_pct:
            # 値がとれていない
            xPos_pct = context['game']['tower']['pos']

        new_pos = int(xPos_pct)
        # 現在位置から飛びすぎている場合、1秒間は無視する
        if abs(new_pos - context['game']['tower']['pos']) > 30:
            if self.matched_in(context, 1000, attr='_last_update_msec'):
                return False

        new_min = min(new_pos, context['game']['tower']['min'])
        new_max = max(new_pos, context['game']['tower']['max'])

        updated = (new_pos != context['game']['tower']['pos'])

        context['game']['tower']['pos'] = new_pos
        context['game']['tower']['min'] = new_min
        context['game']['tower']['max'] = new_max
        if updated:
            self._call_plugins('on_game_objective_position_update')

        self._last_update_msec = context['engine']['msec']
        return True

    def _init_scene(self):
        self.ui_tower_mask = cv2.imread('masks/ui_tower.png')
        self.ui_tower_mask = self.ui_tower_mask[
            self.tower_line_top:self.tower_line_top + self.tower_line_height, self.tower_left:self.tower_left + self.tower_width]
        self._last_update_msec = -100 * 1000

if __name__ == "__main__":
    target = cv2.imread(sys.argv[1])

max_stars_repo_name: joythegreat/IkaLog

max_issues_repo_name: joythegreat/IkaLog

max_forks_repo_name: joythegreat/IkaLog


text:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import numpy as np
from scipy.spatial import distance_matrix
import UI_MainWindow
import PCAGraph
import DataPreparation
import FileInput
import IndividualMetrics
from matplotlib.backends.backend_qt5agg import ( NavigationToolbar2QT as NavigationToolbar )
from matplotlib import backend_bases
import datetime
import PCA

class IndMetricsTab(QtWidgets.QWidget):
    def createTab(self, whichds):
        IndMetricsTab.itab = QtWidgets.QTabWidget()
        self.iIndex = self.addTab(IndMetricsTab.itab,
                                 "Individual metrics")
        IndMetricsTab.createGraph(self, whichds)

    def createGraph(self, whichds):

        #Widgets
        IndMetricsTab.tickBox = QtWidgets.QCheckBox()
        IndMetricsTab.tBoxLabel = QtWidgets.QLabel()
        IndMetricsTab.tBoxLabel.setText("Hide unselected tick marks")
        IndMetricsTab.comboBox = QtWidgets.QComboBox()
        for metric in UI_MainWindow.Ui_MainWindow.listOfMetrics:
            IndMetricsTab.comboBox.addItem(metric)
        IndMetricsTab.comboBox.activated[str].connect(lambda x: IndMetricsTab.metric_change(self, text=IndMetricsTab.comboBox.currentText()))
        IndMetricsTab.sampleBox = QtWidgets.QComboBox()
        IndMetricsTab.sampleBox.addItem("")
        for sample in UI_MainWindow.Ui_MainWindow.metrics[0].index:
            IndMetricsTab.sampleBox.addItem(str(sample))
        IndMetricsTab.sampleBox.activated[str].connect(lambda x: IndMetricsTab.sample_change(self, text=IndMetricsTab.sampleBox.currentText()))
        IndMetricsTab.tickBox.toggled.connect(IndMetricsTab.hideTickMarks)
        Undozoombutton = QtWidgets.QPushButton()
        Undozoombutton.clicked.connect(IndMetricsTab.home)
        Undozoombutton.setStyleSheet("background-color: rgb(240,240,240);padding: 3px;")
        Undozoombutton.setText("Undo zoom")
        UI_MainWindow.Ui_MainWindow.progress1.setValue(80)

        #Create layout
        vbox = QtWidgets.QVBoxLayout(IndMetricsTab.itab)
        hbox15 = QtWidgets.QHBoxLayout()
        hbox15.addStretch()
        hbox15.addWidget(IndMetricsTab.sampleBox)
        vbox.addLayout(hbox15)
        hbox17 = QtWidgets.QHBoxLayout()
        hbox17.addStretch()
        hbox17.addWidget(IndMetricsTab.tBoxLabel)
        hbox17.addWidget(IndMetricsTab.tickBox)
        vbox.addLayout(hbox17)
        hbox2 = QtWidgets.QHBoxLayout()
        hbox2.addStretch()
        plotlabel = QtWidgets.QLabel()
        plotlabel.setText(UI_MainWindow.Ui_MainWindow.element)
        boldfont = QtGui.QFont()
        boldfont.setBold(True)
        font = QtGui.QFont()
        font.setBold(True)
        font.setPointSize(14)
        plotlabel.setFont(font)
        try:
            indMetPlot
            indMetPlot.clear()
        except NameError:
            indMetPlot = None
        indMetPlot = IndividualMetrics.MyIndMetricsCanvas(UI_MainWindow.Ui_MainWindow.NumericMetrics[whichds],
                            UI_MainWindow.Ui_MainWindow.NumericMetrics[whichds], UI_MainWindow.Ui_MainWindow.element, False)
        IndMetricsTab.indMetPlot = indMetPlot
        IndMetricsTab.originalylim = indMetPlot.ax.get_ylim()
        IndMetricsTab.originalxlim = indMetPlot.ax.get_xlim()
        mpl_toolbar = NavigationToolbar(indMetPlot, IndMetricsTab.itab)
        IndMetricsTab.mpl_toolbar = mpl_toolbar
        plotvbox = QtWidgets.QVBoxLayout()
        plotvbox.addWidget(plotlabel)
        plotvbox.addWidget(indMetPlot)
        toolbarhbox = QtWidgets.QHBoxLayout()
        toolbarhbox.addWidget(mpl_toolbar)
        toolbarhbox.addStretch()
        toolbarhbox.addWidget(IndMetricsTab.comboBox)
        toolbarhbox.addStretch()
        toolbarhbox.addWidget(Undozoombutton)
        plotvbox.addLayout(toolbarhbox)
        hbox2.addLayout(plotvbox)
        hbox2.addStretch()
        vbox.addLayout(hbox2)

        #hbox3 =  QtWidgets.QHBoxLayout()
        #hbox3.addStretch()
        #hbox3.addWidget(IndMetricsTab.comboBox)
        #hbox3.addStretch()
        #vbox.addLayout(hbox3)
        vbox.setContentsMargins(30, 20, 30, 100)
        self.setCurrentIndex(self.iIndex)
        UI_MainWindow.Ui_MainWindow.EnableAnalysisButtons(self)

    def metric_change(self, text):
        UI_MainWindow.Ui_MainWindow.element = text
        whichds = 0
        for dataset in range(len(UI_MainWindow.Ui_MainWindow.NumericMetrics)):
                if UI_MainWindow.Ui_MainWindow.element in UI_MainWindow.Ui_MainWindow.NumericMetrics[dataset].columns:
                    whichds = dataset
                    break
        UI_MainWindow.Ui_MainWindow.removeTab(self, self.iIndex)
        IndMetricsTab.createTab(self, whichds)
        tableIndex = list(UI_MainWindow.Ui_MainWindow.metrics[0].index)
        IndMetricsTab.sampleBox.setCurrentIndex(tableIndex.index(UI_MainWindow.Ui_MainWindow.sampleSelected)+1)
        IndMetricsTab.comboBox.setCurrentIndex( UI_MainWindow.Ui_MainWindow.listOfMetrics.index(text))
        UI_MainWindow.Ui_MainWindow.progress1.setValue(100)

    def sample_change(self, text):
        if text in UI_MainWindow.Ui_MainWindow.metrics[0].index:
                UI_MainWindow.Ui_MainWindow.sampleSelected = text
                if UI_MainWindow.Ui_MainWindow.sampleSelected in IndividualMetrics.MyIndMetricsCanvas.table.index:
                        sIndex = IndividualMetrics.MyIndMetricsCanvas.table.index.tolist().index(UI_MainWindow.Ui_MainWindow.sampleSelected)
                elif type(UI_MainWindow.Ui_MainWindow.sampleSelected) == int:
                        sIndex = int(UI_MainWindow.Ui_MainWindow.sampleSelected)
                if hasattr(IndividualMetrics.MyIndMetricsCanvas,"ann"):
                        IndividualMetrics.MyIndMetricsCanvas.ann.remove()
                if hasattr(IndividualMetrics.MyIndMetricsCanvas,"lines"):
                    IndividualMetrics.MyIndMetricsCanvas.lines.remove()
                IndividualMetrics.MyIndMetricsCanvas.ax.plot(IndividualMetrics.MyIndMetricsCanvas.samplenames,  IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element], linestyle="-",marker='o', markerfacecolor = "dimgrey",color = "black")
                IndividualMetrics.MyIndMetricsCanvas.ax.plot(IndividualMetrics.MyIndMetricsCanvas.samplenames[sIndex], IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].loc[UI_MainWindow.Ui_MainWindow.sampleSelected], linestyle="none", marker='o', markerfacecolor='limegreen', markeredgecolor='darkgreen')
                if type(IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[0])!= datetime.datetime:
                    xlim = IndividualMetrics.MyIndMetricsCanvas.ax.get_xlim()
                    thisylim = IndividualMetrics.MyIndMetricsCanvas.ax.get_ylim()
                    yaxrange = abs(thisylim[1])-abs(thisylim[0])
                    svalue = (IndividualMetrics.MyIndMetricsCanvas.ax.get_xticks()[sIndex])
                    offsets = [svalue,IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[sIndex]+(yaxrange/6)]
                    stringify = "x:" + str(IndividualMetrics.MyIndMetricsCanvas.samplenames[sIndex]) + "\ny:" + str(IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[sIndex])
                    IndividualMetrics.MyIndMetricsCanvas.ann = IndividualMetrics.MyIndMetricsCanvas.ax.annotate(stringify, xy=(svalue,IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[sIndex]), xytext=(offsets[0], offsets[1]), color="k",
                            size=10,ha = 'center', va="center", bbox=dict(facecolor='white', edgecolor='blue', pad=3.0))
                else:
                    xlim = IndividualMetrics.MyIndMetricsCanvas.ax.get_xlim()
                    diffBetweenDpAndMin = IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[sIndex] - IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].min()
                    diffinDays = diffBetweenDpAndMin.days
                    yaxmin = IndividualMetrics.MyIndMetricsCanvas.ax.get_yticks().min()
                    yaxrange = IndividualMetrics.MyIndMetricsCanvas.ax.get_yticks().max() -yaxmin
                    ylabel = str(IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[sIndex]).split(" ")[0]
                    yVal = yaxmin + diffinDays
                    svalue = (IndividualMetrics.MyIndMetricsCanvas.ax.get_xticks()[sIndex])
                    offsets = [svalue,yVal+(yaxrange/6)]
                    stringify = "x:" + str(IndividualMetrics.MyIndMetricsCanvas.samplenames[sIndex]) + "\ny:" + str(ylabel)
                    IndividualMetrics.MyIndMetricsCanvas.ann = IndividualMetrics.MyIndMetricsCanvas.ax.annotate(stringify, xy=(svalue,IndividualMetrics.MyIndMetricsCanvas.table[IndividualMetrics.MyIndMetricsCanvas.element].iloc[sIndex]), xytext=(offsets[0], offsets[1]), color="k",
                        size=10,ha = 'center', va="center", bbox=dict(facecolor='white', edgecolor='blue', pad=3.0))
                #Have to check if hide tick marks are selected:
                if IndMetricsTab.tickBox.isChecked():
                    #Find index of selected sample:
                    samples = list(IndividualMetrics.MyIndMetricsCanvas.samplenames)
                    sIndex = samples.index(UI_MainWindow.Ui_MainWindow.sampleSelected)
                    labels = [""] * len(samples)
                    labels[sIndex] = UI_MainWindow.Ui_MainWindow.sampleSelected
                    IndividualMetrics.MyIndMetricsCanvas.ax.set_xticklabels(labels)

                IndividualMetrics.MyIndMetricsCanvas.fig.canvas.draw()
                samples = list(UI_MainWindow.Ui_MainWindow.metrics[0].index)
                IndMetricsTab.sampleBox.setCurrentIndex(samples.index(text)+1) #Because the first one is
                UI_MainWindow.Ui_MainWindow.progress1.setValue(100)

    def hideTickMarks(self):
        if IndMetricsTab.tickBox.isChecked():
            #Find index of selected sample:
            samples = list(IndividualMetrics.MyIndMetricsCanvas.samplenames)
            sIndex = samples.index(UI_MainWindow.Ui_MainWindow.sampleSelected)
            labels = [""] * len(samples)
            labels[sIndex] = UI_MainWindow.Ui_MainWindow.sampleSelected
            IndividualMetrics.MyIndMetricsCanvas.ax.set_xticklabels(labels)

        else:
            IndividualMetrics.MyIndMetricsCanvas.ax.set_xticklabels(IndividualMetrics.MyIndMetricsCanvas.samplenames)

        IndividualMetrics.MyIndMetricsCanvas.fig.canvas.draw()

    def home(self):
        IndividualMetrics.MyIndMetricsCanvas.ax.set_xlim(IndMetricsTab.originalxlim)
        IndividualMetrics.MyIndMetricsCanvas.ax.set_ylim(IndMetricsTab.originalylim)
        IndividualMetrics.MyIndMetricsCanvas.fig.canvas.draw()

max_stars_repo_name: marinaPauw/Assurance

max_issues_repo_name: marinaPauw/Assurance

max_forks_repo_name: marinaPauw/Assurance

reason: import numpy,from scipy


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

import numpy as np
import pandas as pd
import pytest
from modelstore.models.common import save_joblib
from modelstore.models.sklearn import (
    MODEL_JOBLIB,
    SKLearnManager,
    _feature_importances,
)
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from tests.models.utils import classification_data

# pylint: disable=protected-access
# pylint: disable=redefined-outer-name

@pytest.fixture
def sklearn_tree():
    params = {
        "n_estimators": 5,
        "max_depth": 4,
        "min_samples_split": 5,
        "n_jobs": 1,
    }
    return RandomForestRegressor(**params)

@pytest.fixture
def sklearn_logistic():
    return LogisticRegression()

@pytest.fixture
def sklearn_pipeline(sklearn_tree):
    return Pipeline(
        [
            ("scaler", StandardScaler()),
            ("regressor", sklearn_tree),
        ]
    )

@pytest.fixture
def sklearn_manager():
    return SKLearnManager()

@pytest.mark.parametrize(
    "model_type,expected",
    [
        (
            RandomForestRegressor,
            {"library": "sklearn", "type": "RandomForestRegressor"},
        ),
        (
            LogisticRegression,
            {"library": "sklearn", "type": "LogisticRegression"},
        ),
        (
            partial(
                Pipeline, steps=[("regressor", RandomForestRegressor(n_jobs=1))]
            ),
            {"library": "sklearn", "type": "Pipeline"},
        ),
    ],
)
def test_model_info(sklearn_manager, model_type, expected):
    res = sklearn_manager._model_info(model=model_type())
    assert expected == res

@pytest.mark.parametrize(
    "ml_library,should_match",
    [
        ("sklearn", True),
        ("xgboost", False),
    ],
)
def test_is_same_library(sklearn_manager, ml_library, should_match):
    assert (
        sklearn_manager._is_same_library({"library": ml_library})
        == should_match
    )

def test_model_data(sklearn_manager, sklearn_tree):
    labels = np.array([0, 1, 1, 0, 1])
    exp = {"labels": {"shape": [5], "values": {0: 2, 1: 3}}}
    res = sklearn_manager._model_data(model=sklearn_tree, y_train=labels)
    assert exp == res

def test_required_kwargs(sklearn_manager):
    assert sklearn_manager._required_kwargs() == ["model"]

def test_matches_with(sklearn_manager, sklearn_tree):
    assert sklearn_manager.matches_with(model=sklearn_tree)
    assert not sklearn_manager.matches_with(model="a-string-value")
    assert not sklearn_manager.matches_with(classifier=sklearn_tree)

def test_get_functions(sklearn_manager, sklearn_tree):
    assert len(sklearn_manager._get_functions(model=sklearn_tree)) == 1
    with pytest.raises(TypeError):
        sklearn_manager._get_functions(model="not-an-sklearn-model")

@pytest.mark.parametrize(
    "model_type",
    [
        RandomForestRegressor,
        LogisticRegression,
    ],
)
def test_get_params(sklearn_manager, model_type):
    try:
        result = sklearn_manager._get_params(model=model_type())
        json.dumps(result)
    except Exception as e:
        pytest.fail(f"Exception when dumping params: {str(e)}")

def test_get_params_from_pipeline(sklearn_manager):
    pipeline = Pipeline(
        steps=[
            (
                "preprocessor",
                ColumnTransformer(
                    transformers=[
                        ("cat", OneHotEncoder(), ["columns"]),
                    ],
                    remainder="passthrough",
                ),
            ),
            ("classifier", RandomForestRegressor()),
        ]
    )
    result = sklearn_manager._get_params(model=pipeline)
    assert result == {}

def test_feature_importances_tree_model(sklearn_tree, classification_data):
    X_train, y_train = classification_data
    df = pd.DataFrame(
        X_train, columns=[f"col_{i}" for i in range(X_train.shape[1])]
    )
    sklearn_tree.fit(df, y_train)
    exp = dict(zip(df, sklearn_tree.feature_importances_))
    res = _feature_importances(sklearn_tree, df)
    assert exp == res

def test_feature_importances_pipeline(sklearn_pipeline, classification_data):
    X_train, y_train = classification_data
    df = pd.DataFrame(
        X_train, columns=[f"col_{i}" for i in range(X_train.shape[1])]
    )
    sklearn_pipeline.fit(df, y_train)
    exp = {"regressor": sklearn_pipeline.steps[1][1].feature_importances_}
    res = _feature_importances(sklearn_pipeline, df)
    assert (exp["regressor"] == res["regressor"]).all()

def test_load_model(tmp_path, sklearn_manager, sklearn_tree):
    # Save the model to a tmp directory
    model_path = save_joblib(tmp_path, sklearn_tree, MODEL_JOBLIB)
    assert model_path == os.path.join(tmp_path, MODEL_JOBLIB)

    #  Load the model
    loaded_model = sklearn_manager.load(tmp_path, {})

    # Expect the two to be the same
    assert type(loaded_model) == type(sklearn_tree)
    assert loaded_model.get_params() == sklearn_tree.get_params()

max_stars_repo_name: cdknorow/modelstore

max_issues_repo_name: cdknorow/modelstore

max_forks_repo_name: cdknorow/modelstore


text:
from filter import QMFFilter
from util import *
import numpy as np
import scipy as sp
from skimage.measure import compare_mse, compare_psnr

def metric(im1, im2):
    '''
    Calculate the PSNR performance of two images
    MSE = \frac{1}{MN} \sum_{i=1}^{M} \sum_{j=1}^{N} [f(i,j) - g(i,j)]^2
    metric = 10 * log_{10}(255^2 / MSE)
    :param im1: the first input image
    :param im2: the second input image
    :return: metric
    '''
    MSE = compare_mse(im1, im2)
    psnr = compare_psnr(im1, im2, 255)
    return MSE, psnr

def im2col(mtx, block_size):
    '''

    :param mtx: the input image
    :param block_size:
    :return:
    '''
    mtx_shape = mtx.shape
    sx = mtx_shape[0] - block_size[0] + 1
    sy = mtx_shape[1] - block_size[1] + 1
    result = np.empty((block_size[0] * block_size[1], sx * sy))
    for i in range(sy):
        for j in range(sx):
            result[:, i * sx + j] = mtx[j:j + block_size[0], i:i + block_size[1]].ravel(order='F')
    return result

def subband_denoise(x_in, noise_sigma, win_size):
    '''
    Subband denoising function
    :param x_in: the input image
    :param noise_sigma: the standard deviation of noise signal.
    The noise is often considered as coming from Gaussian distribution
    :param win_size: the window size
    :return: the image after denoising procedure
    '''
    AA = np.pad(x_in, pad_width=((win_size, win_size),
                                 (win_size, win_size)),
                mode="reflect")
    AA = im2col(AA, [win_size * 2 + 1, win_size * 2 + 1])
    x_in_1d = np.reshape(x_in, -1)
    result_1d = np.zeros_like(AA)

    var_gaussian = np.mean(AA**2, 0) - noise_sigma ** 2
    var_gaussian = np.maximum(var_gaussian, 0)
    sigma_gaussian = np.sqrt(var_gaussian)
    judge = sigma_gaussian != 0

    BB = np.mean(AA, 0)
    y = x_in_1d[judge]

    for i in range(0, AA.shape[0]):
        temp_x = AA[i,:]
        temp_A = temp_x[judge]
        temp_BB = BB[judge]
        temp_sigma = sigma_gaussian[judge]
        temp_result = (y * temp_sigma**2 + temp_A * noise_sigma**2 -
                       temp_BB * noise_sigma**2) / (temp_sigma**2 + noise_sigma**2)
        result_1d_temp = np.zeros_like(result_1d[i,:])
        result_1d_temp[judge] = temp_result
        result_1d[i,:] = result_1d_temp

    result_1d = np.mean(result_1d, 0)
    result = np.reshape(result_1d, [x_in.shape[0], x_in.shape[1]])

    return result

def denoise_kde(x_in, wbase, mom, dwt_level, win_size):
    '''

    :param x_in:
    :param wbase:
    :param mom:
    :param dwt_level:
    :param win_size:
    :return:
    '''
    [nrow, ncol] = x_in.shape
    L = np.log2(ncol) - dwt_level
    qmf = QMFFilter(wbase, mom)
    [temp, coef] = norm_noise_2d(x_in, qmf)
    noise_sigma = 1 / coef

    wx = FWT2_PO(x_in, L, qmf)
    [n, J] = dyadlength(wx)
    ws = wx

    for j in range(J-1, L-1, -1):
        [t1, t2] = dyad2HH(j)
        ws[t1, t2] = subband_denoise(wx[t1, t2], noise_sigma, win_size)
        [t1, t2] = dyad2HL(j)
        ws[t1, t2] = subband_denoise(wx[t1, t2], noise_sigma, win_size)
        [t1, t2] = dyad2LH(j)
        ws[t1, t2] = subband_denoise(wx[t1, t2], noise_sigma, win_size)

    x_out = iwt_po(ws, L, qmf)

    return x_out

if __name__ == '__main__':


text:
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import socket
import time
import cv2
import numpy as np

sys.path.append("..")
from RobotController.RobotController import RobotController

# mapping list of chess classes in different modules
map_list = [0, 1, 2, 3, 4, 5, 6, 13, 8, 9, 10, 11, 7, 12]

def EncodeBoard(chessBoard: list) -> str:
    """
    param:
        chessBoard: List-> [List ->[int:x, int: y, int: class], [], ...]

    return:
        boardInfo: str: [x, y, c]#[x, y, c]#...
    """
    boardInfo = '#'.join([str(item) for item in chessBoard])
    return boardInfo

def EncodeAImv(chessBoard: list, AI_mv: str, winner: str, is_mate: str) -> str:
    """
    param:
        chessBoard: List-> [List ->[int:x, int: y, int: class], [], ...]
        AI_mv: str: [x_src, y_src, x_dst, y_dst]
        winner: str: 'r' or 'b'
        is_mate: str: 0 or 1 or 2,
                      0: continue，1: end，2: draw

    return:
        AI_mv_info str: #[is_mate, player]#[x_src, y_src, class_src]#[x_dst, y_dst, class_dst]
    """
    if AI_mv == (" " * 12):
        [x_src, y_src, x_dst, y_dst] = [-1, 0, -1, 0]
    else:  # e.g. [6, 2, 6, 4]
        [x_src, y_src, x_dst, y_dst] = map(int, AI_mv[1:-1].split(','))
    c = -1
    for item in chessBoard:
        if item[0] == x_src and item[1] == y_src:
            c = item[2]

    part_1 = '[' + is_mate + ', ' + winner + ']'
    part_2 = str([x_src, y_src, c])
    part_3 = str([x_dst, y_dst, c])
    AI_mv_info = '#' + part_1 + '#' + part_2 + '#' + part_3
    return AI_mv_info

def DecodeHumanMv(Human_mv: str) -> str:
    """
    param:
        Human_mv: str #[x, y, c]#[x, y, c]
    return:
        mv: list of int [x_src, y_src, x_dst, y_dst]
    """
    Human_mv = Human_mv.strip()  # str #[]#[]
    templist = Human_mv.split('#')[1:]  # list [[x, y, c], [x, y, c]]
    x_src = templist[0][1:-1].split(',')[0]
    y_src = templist[0][1:-1].split(',')[1]
    x_dst = templist[1][1:-1].split(',')[0]
    y_dst = templist[1][1:-1].split(',')[1]
    mv = '[' + x_src + ', ' + y_src + ', ' + x_dst + ', ' + y_dst + ']'
    print("检查Human move：", mv)
    return mv

def IsKillStep(chessboard: list, mv: list) -> bool:
    """
    chessboard: current chessboard info [[x, y, class], [], []...]
    mv: AI move info, [x_src, y_src, x_dst, y_dst]

    return:
        BOOL: true if this step is kill step, false if normal step.
    """
    for pc in chessboard:
        if mv[2] == pc[0] and mv[3] == pc[1]:
            return True
        else:
            continue

    return False

def MoveStr2List(mv: str) -> list:
    """
    param:
        mv: str [x_src, y_src, x_dst, y_dst]

    return:
        mv_list: List-> [int: x_src, y_src, x_dst, y_dst]
    """
    mv_list = []
    if mv != "            ":
        mv_list = list(map(int, mv[1:-1].split(",")))
    return mv_list

def WrapInfo(info: str) -> str:
    """
    param:
        info: "xxxx"
    return:
        wrapped_info: "<xxxx>"
    """
    wrapped_info = "<" + info + ">"
    return wrapped_info

class CentralControl:
    def __init__(self):

        self.currentPlayer = "r"  # "r" or "b"
        self.gameMode = "Human VS AI"  # Play or Watch
        self.chessBoard = []  # [[int:x, y, class],[],...]
        self.chessBoard_real = []  # real position of chess on the chessboard
        self.isMate = 0  # 1 if game is over

        # socket of perception
        self.socket_perception = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.HOST_perception = "192.168.8.22"
        self.PORT_perception = 6002
        # socket of chess engine
        self.socket_playing = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.HOST_playing = "192.168.8.155"
        self.PORT_playing = 6667
        # socket of webserver
        self.socket_web = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.HOST_web = "192.168.8.155"
        self.PORT_web = 8231

        self.robotController_r = RobotController("r")  # red robot
        self.robotController_r.RobotInit()
        print("红方机械臂初始化完成")
        self.robotController_b = RobotController("b")  # black robot
        self.robotController_b.RobotInit()
        print("黑方机械臂初始化完成")

    def SocketConnect(self, switch: str):
        """
        str: sting length is 3, switch[0] is for perception module，and
                            switch[1] is for chess engine module, and
                            switch[2] is for webserver module.
        """
        # perception module
        if switch[0] == "1":
            try:
                self.socket_perception.connect((self.HOST_perception, self.PORT_perception))
            except socket.error as arg:
                (err_msg, errno) = arg
                print("连接棋盘理解模块失败，error message: %s, errNO : %d" % (err_msg, errno))
            print("连接棋盘理解模块成功")
        else:
            print("跳过连接棋盘理解模块")

        # chess engine module
        if switch[1] == "1":
            try:
                self.socket_playing.connect((self.HOST_playing, self.PORT_playing))
            except socket.error as arg:
                (err_msg, errno) = arg
                print("连接对弈引擎模块失败，error message: %s, errNO : %d" % (err_msg, errno))
            print("连接对弈引擎模块成功")
        else:
            print("跳过连接对弈引擎模块")

        # webserver module
        if switch[2] == "1":
            self.socket_web.bind((self.HOST_web, self.PORT_web))
            self.socket_web.listen(1)
            print("等待webserver端连接...")
            self.socket_web, self.web_addr = self.socket_web.accept()
            print("web_conn: ", self.socket_web)
            print("web_addr: ", self.web_addr)
            print("连接WebServer端成功")
        else:
            print("跳过连接web模块")

    def GetChessboardInfo(self):
        """
        Take a picture for perception with camera to get the chessboard information.
        """
        capture = cv2.VideoCapture(2)
        encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 100]
        waitFrameNum = 10  # wait for AE becoming stable
        while waitFrameNum:
            _, frame = capture.read()
            waitFrameNum -= 1
        ret, frame = capture.read()
        result, imgencode = cv2.imencode('.jpg', frame, encode_param)
        data = np.array(imgencode)
        stringData = data.tobytes()

        self.socket_perception.send(str.encode(str(len(stringData)).ljust(16)))
        self.socket_perception.send(stringData)

        receive = self.socket_perception.recv(1024)
        if len(receive):
            print("Chess Perception Results:  ", str(receive, encoding='utf-8'))

        [boardStr, boardStr_real] = receive.decode('utf-8').split("&")
        board_recv = boardStr.split("#")
        board_recv_real = boardStr_real.split("#")
        print("检查打印board_recv_real: ", board_recv_real)

        # ideal chessboard info
        new_board = []
        for item in board_recv:
            new_board.append(list(map(int, item[1:-1].split(','))))
        self.chessBoard = new_board.copy()
        # real chessboard info
        new_board_real = []
        for item in board_recv_real:
            new_board_real.append(list(map(int, item[1:-1].split(','))))
        self.chessBoard_real = new_board_real.copy()

        print("读取棋盘状态完成，数据已经加载到self.chessBoard: ")
        print(self.chessBoard)
        print("打印真实坐标：")
        print(self.chessBoard_real)
        print('\n')

        time.sleep(1)
        capture.release()

    def NextMoveViaSocket(self, chessboard: list, player: str) -> (str, str):
        """
        param:
        chessboard: current chessboard status [[x,y,class], [], ...]
        player: str current player， 'r' or 'b'

        return:
        AI_mv_info: str #[0, r]#[x, y, c]#[]
        AI_mv: str [x_src, y_src, x_dst, y_dst]
        """

        boardInfo = EncodeBoard(chessboard)
        print("boardInfo is: ", boardInfo)

        self.socket_playing.send(player.encode('utf-8'))  # 'r' or 'b'
        print("player 已发送！")
        time.sleep(1)

        self.socket_playing.send(boardInfo.encode('utf-8'))
        print("boardInfo 已发送！")
        print(boardInfo)

        AI_mv = self.socket_playing.recv(12).decode('utf-8')  # [6, 2, 6, 4]
        print("AI_mv received: ", AI_mv, type(AI_mv))
        is_mate = self.socket_playing.recv(1).decode('utf-8')  # 1
        print("is_mate received: ", is_mate, type(is_mate))
        winner = self.socket_playing.recv(1).decode('utf-8')  # 'b'
        print("winner received: ", winner, type(winner))

        time.sleep(1)

        print("对弈引擎计算结束！")
        AI_mv_info = EncodeAImv(chessboard, AI_mv, winner, is_mate)
        print("AI_mv_info: ", AI_mv_info)
        return AI_mv_info, AI_mv

    def Ideal2Real(self, ideal_index: list) -> list:
        """
        param:
            ideal_index: [x_idl, y_idl]
        return:
            real_index: [x_real, y_real]

        """
        real_index = []
        for i in range(len(self.chessBoard)):
            if self.chessBoard[i][0] == ideal_index[0] and self.chessBoard[i][1] == ideal_index[1]:
                print("chess found: ", self.chessBoard[i])
                print("real position: ", [self.chessBoard_real[i][0], self.chessBoard_real[i][1]])
                real_index = [self.chessBoard_real[i][0], self.chessBoard_real[i][1]]
        if real_index:
            return real_index
        else:
            print("Chess not found, please check input!")

    def MakeMove(self, player: str, mv: str):
        """
        Make robot execute the move signal.
        mv: str [x_src, y_src, x_dst, y_dst]
        """

        move = MoveStr2List(mv)  # [6, 2, 6, 4]
        print("机械臂解析:", move, player)

        # change ideal position to real position
        pos1t = tuple(self.Ideal2Real([move[0], move[1]]))
        pos2t = (move[2] * 100, move[3] * 100)
        print("机械臂执行指令：", [pos1t, pos2t])
        # select robot
        if player == "r":
            cur_robot = self.robotController_b
        else:  # player == 'b'
            cur_robot = self.robotController_b
        # check the move type of robot, Eat or Move
        if IsKillStep(self.chessBoard, move):
            cur_robot.Eat(pos1t, pos2t)
            time.sleep(2)
        else:
            cur_robot.Move(pos1t, pos2t)
            time.sleep(2)

    # use human to replace robot
    def MakeMove_help(self, player: str, mv: str):
        move = MoveStr2List(mv)
        print("请人工移动棋子！！！！")
        print("行动方: ", player)
        print("行动指令: ", move)
        input("移动完毕请按回车确认...")

    # switch player
    def ChangeSide(self):
        if self.currentPlayer == "r":
            self.currentPlayer = "b"
            print("红方结束，轮到黑方下棋！")
        elif self.currentPlayer == "b":
            self.currentPlayer = "r"
            print("黑方结束，轮到红方下棋！")

    # AI VS AI
    def AI_VS_AI(self):
        print("进入AI VS AI分支...")

        while self.isMate != 1:

            # mv2UI str: #[0, r]#[x, y, c]#[x, y, c], mv2Robot str: [x, y, x, y]
            mv2UI, mv2Robot = self.NextMoveViaSocket(self.chessBoard, self.currentPlayer)
	    self.isMate = int(mv2UI[2])
            print("下棋方： ", self.currentPlayer)

            if mv2UI[9] != "-":
                self.MakeMove_help(self.currentPlayer, mv2Robot)

            self.GetChessboardInfo()

            mv2UI = WrapInfo(mv2UI.ljust(32))
            print("检查发给UI信息:", mv2UI)
            self.socket_web.send(mv2UI.encode('utf-8'))
            # switch player
            self.ChangeSide()
            try:
                data = self.socket_web.recv(11 + 2, 0x40)
                self.gameMode = data.decode('utf-8')[1:-1].strip()
                print("成功接收到模式转换信号！当前对弈模式：", self.gameMode)
                break
            except BlockingIOError as e:
                data = None
        # end of game
        print("Quit AI vs AI mode...")

    # Human VS AI
    def Human_VS_AI(self):
        print("进入Human VS AI分支...")
        # if game is not over yet
        while self.isMate != 1:
            if self.currentPlayer == "r":
                print("等待红方落子...")
                self.socket_web.send("<Waiting for the red>".encode('utf-8'))
                # receive red move info
                Human_mv = self.socket_web.recv(32 + 2).decode('utf-8')[1:-1]
                if Human_mv[0] != "#":
                    print("对弈模式改变！！离开人机，进入AI自对弈：", Human_mv)
                    central.gameMode = Human_mv
                    print("判断当前模式：", central.gameMode)
                    break
                # decode red move info
                mv2Robot_R = DecodeHumanMv(Human_mv)
                # red robot play
                self.MakeMove_help(self.currentPlayer, mv2Robot_R)
                # update chessboard info
                self.GetChessboardInfo()
                # switch player
                self.ChangeSide()
            # generate AI move via chess engine
            # mv2UI is str such as: #[0, r]#[x, y, c]#[x, y, c], mv2Robot str: [x, y, x, y]
            print("AI 思考中...请耐心等待...")
            mv2UI, mv2Robot_B = self.NextMoveViaSocket(self.chessBoard, self.currentPlayer)
            # update the value of self.isMate
            if mv2UI[2] == '1':
                central.isMate = 1
            # black robot play
            print("下棋方： ", self.currentPlayer)
            if mv2UI[9] != "-":
                self.MakeMove_help(self.currentPlayer, mv2Robot_B)
            # update chessboard
            self.GetChessboardInfo()
            # send move info to webserver
            mv2UI = WrapInfo(mv2UI.ljust(32))
            print("检查发给UI信息:", mv2UI)
            self.socket_web.send(mv2UI.encode('utf-8'))
            # switch player
            self.ChangeSide()
        # end of game
        print("Quit Human vs AI mode...")

    def Initialization(self, ini: str = "111"):
        """
        Initialize the game by building connection among all the modules.
        Initialize the chessboard after connections are established.
        """
        # establish connections
        self.SocketConnect(ini)
        print("等待接收UI端初始化信号...")
        init_signal = self.socket_web.recv(27 + 2).decode('utf-8')  # Request Initial Chess Board
        print("init signal: ", init_signal[1:-1])
        # get current chessboard info via perception module
        self.GetChessboardInfo()
        # send info to webserver
        self.socket_web.send(WrapInfo(EncodeBoard(central.chessBoard).ljust(512)).encode('utf-8'))
        init_ACK = self.socket_web.recv(10 + 2)  # Initial OK
        print("Initial ACK: ", init_ACK.decode('utf-8')[1:-1])

    def ChangeMode(self):
        """
        Change game mode by setting the content of self.gameMode
        """
        print("当前默认mode = ", self.gameMode, "\n接收对弈模式中...")
        # length of "Human VS AI" is 11
        self.gameMode = central.socket_web.recv(11 + 2).decode('utf-8')[1:-1].strip()
        print("当前对弈模式：", self.gameMode)

if __name__ == '__main__':
    # instantiate
    central = CentralControl()
    # initialize
    central.Initialization("111")
    # receive mode
    central.ChangeMode()
    while central.isMate != 1:
        # branch 1：AI VS AI
        while central.gameMode == "AI VS AI" and central.isMate != 1:
            central.AI_VS_AI()

        # branch 2：Human VS AI
        while central.gameMode == "Human VS AI" and central.isMate != 1:
            central.Human_VS_AI()
    print("对弈结束。请重新启动")
    time.sleep(10)

max_stars_repo_name: Ascend/samples

max_issues_repo_name: Ascend/samples

max_forks_repo_name: Ascend/samples


text:
import numpy as np
import collections

def hr(text='', ch='-', width=80):
    tl = len(text)
    if width-tl-2 < 0: return text
    ll = (width-tl-2)/2
    rl = width-tl-2-ll
    return '%s %s %s\n' % (ch*ll, text, ch*rl)
#end def

class material:
    def __init__(self, density, cost):
        self.density = density
        self.cost    = cost
#end class

class surface:
    unit_h = 0.005

    def __init__(self, S, h, m):
        self._S   = S
        self.h    = h
        self.m    = m

    def set_pcs(self, n): self._S *= n

    def S(self, scale=1, length=1):
        return self._S*scale**2*length

    def mass(self, scale=1, length=1):
        return self.S(scale, length)*self.h*self.m.density;

    def cost(self, scale=1, length=1):
        return self.S(scale, length)*self.h/self.unit_h*self.m.cost;

    def __str__(self):
        return '[%sm^2 x %sm], %st/m^3, %st, %sCr' % (self.S(),
                                                   self.h, self.m.density,
                                                   self.mass(), self.cost())
#end class

class volume:
    def __init__(self, vol, name, **kwargs):
        #main parameters
        self._V    = float(vol)
        self.name  = name
        #surface and subvolumes
        self._surface = kwargs.get('S', None)
        self._subvolumes = kwargs.get('V', [])
        #counterparts of this volume
        n = kwargs.get('N', 1.0)
        self.set_pcs(n)
        #cost, mass and density
        mat = kwargs.get('material', None)
        if isinstance(mat, material):
            self.d = mat.density
            self._cost = mat.cost
        else:
            self._cost = kwargs.get('C', 0.0)
            mass = float(kwargs.get('M', -1.0))
            d = float(kwargs.get('D', -1.0))
            #recalculate mass and density
            if d < 0 and mass < 0: d = 0.0
            elif d < 0:
                d = mass*n/self.V()
                self._cost = self._cost*n/self.V()
            elif mass > 0: print('volume: density was given; mass is ignored')
            self.d = d
    #end def

    def set_pcs(self, n):
        self._V *= n
        if self._surface is not None: self._surface.set_pcs(n)
        map(lambda sv: sv.set_pcs(n), self._subvolumes)
    #end def

    #volume and surface area
    def V(self, scale=1, length=1):
        vol = self.full_V(scale, length) - sum(sv.full_V(scale, length) for sv in self._subvolumes)
        assert vol>=0, \
        ("Combined volume of sub-volumes is greater than the volume of '%s'" % self.name)
        return vol
    #end def

    def full_V(self, scale=1, length=1):
        return self._V*scale**3*length

    def S(self, scale=1, length=1):
        if self._surface is None: return 0
        return self._surface.S(scale, length)

    def full_S(self, scale=1, length=1):
        return (self.S(scale, length) +
                sum(sv.full_S(scale, length) for sv in self._subvolumes))

    #cost
    def V_cost(self, scale=1, length=1):
        return self.V(scale, length)*self._cost

    def full_V_cost(self, scale=1, length=1):
        return (self.V_cost(scale, length) +
                sum(sv.full_V_cost(scale, length) for sv in self._subvolumes))

    def S_cost(self, scale=1, length=1):
        if self._surface is None: return 0
        return self._surface.cost(scale, length)

    def full_S_cost(self, scale=1, length=1):
        return (self.S_cost(scale, length) +
                sum(sv.full_S_cost(scale, length) for sv in self._subvolumes))

    def full_cost(self, scale=1, length=1):
        return self.full_V_cost(scale, length) + self.full_S_cost(scale, length)

    #mass
    def V_mass(self, scale=1, length=1):
        return self.V(scale, length)*self.d;

    def full_V_mass(self, scale=1, length=1):
        return (self.V_mass(scale, length) +
                sum(sv.full_V_mass(scale, length) for sv in self._subvolumes))

    def S_mass(self, scale=1, length=1):
        if self._surface is None: return 0
        return self._surface.mass(scale, length)

    def full_S_mass(self, scale=1, length=1):
        return (self.S_mass(scale, length) +
                sum(sv.full_S_mass(scale, length) for sv in self._subvolumes))

    def full_mass(self, scale=1, length=1):
        return self.full_V_mass(scale, length) + self.full_S_mass(scale, length)

    #representation
    def __str__(self):
        simple = self._surface is None and not self._subvolumes
        s  = ''
        s += '%s: %sm^3, %s%st, %sCr\n' % (self.name, self.full_V(),
                                           '%st/m^3 ' % self.d if simple else '',
                                           self.full_mass(), self.full_cost())
        if not simple:
            if self._surface is not None:
                s += '   surface: %s\n' % self._surface
            if self.V_mass() > 0 or self.V_cost() > 0:
                s += '   content: %sm^3, %st/m^3, %st, %sCr\n' % (self.V(), self.d,
                                                                  self.V_mass(), self.V_cost())
            if len(self._subvolumes) > 0:
                s += '   '+''.join(str(sv).replace('\n', '\n   ') for sv in self._subvolumes)
                s = s[:-3]
        return s
#end class

class part(collections.Iterable):
    _asymptote_slope     = 1.5
    _asymptote_intercept = 1e5
    _exponent_base       = 1.25

    def __init__(self, name, volumes, add_mass = 0, add_cost = 0, res_cost = 0, size = 1):
        self.name       = name
        self._volumes   = volumes
        self._add_mass  = add_mass
        self._add_cost  = add_cost
        self._res_cost  = res_cost
        self._size      = size
        self._spec_mass = np.array([self.V_mass(), self.S_mass(), 0, self._add_mass])
        self._init_mass = sum(self._spec_mass)
        self._weights   = self._spec_mass/self._init_mass
        self._spec_cost = np.array([sum(v.full_V_cost() for v in self._volumes),
                                    sum(v.full_S_cost() for v in self._volumes),
                                    0, self._add_cost])
        self._cost      = sum(self._spec_cost)
        self._cost_weights = self._spec_cost/self._cost
        self._cost     += res_cost
        print(self)
        print
    #end def

    def __iter__(self): return iter(self._volumes)

    @property
    def volumes(self):
        return [volume(self.volume(), self.name, V=self._volumes)]
    #end def

    def mass(self, scale=1, length=1):
        scale = scale/self._size
        w = self._spec_mass
        m = ((w[0]*scale + w[1])*scale + w[2])*scale*length
        if len(w) > 3: m += w[3]
        return m
    #end def

    def cost(self, scale=1, length=1):
        scale = scale/self._size
        w = self._spec_cost
        c = ((w[0]*scale + w[1])*scale + w[2])*scale*length
        if len(w) > 3: c += w[3]
        c += self._res_cost
        return c
    #end def

    @classmethod
    def entry_cost(cls, c):
        return c*cls._asymptote_slope + (1-cls._exponent_base**(-10*c/cls._asymptote_intercept))*cls._asymptote_intercept
    #end def

    def true_mass(self, scale=1, length=1):
        return self.V_mass(scale, length)+self.S_mass(scale, length)+self._add_mass
    #end def

    def volume(self, scale=1, length=1):
        return sum(v.full_V(scale, length) for v in self._volumes)

    def surface(self, scale=1, length=1):
        return sum(v.full_S(scale, length) for v in self._volumes)

    def V_mass(self, scale=1, length=1):
        return sum(v.full_V_mass(scale, length) for v in self._volumes)

    def S_mass(self, scale=1, length=1):
        return sum(v.full_S_mass(scale, length) for v in self._volumes)
    #end def

    def __str__(self):
        s  = '//'+hr(self.name, '=')
        s += '//'+'\n//'.join(str(v).replace('\n', '\n//') for v in self._volumes)[:-2]
        s += '//'+hr()
        s += '//Total volume:    %.3f m^3, %.6f t\n' % (self.volume(), self.V_mass())
        s += '//Total surface:   %.3f m^2, %.6f t\n' % (self.surface(), self.S_mass())
        s += '//Additional mass: %.6f t\n' % self._add_mass
        s += '//Additional cost: %.3f Cr\n' % self._add_cost
        s += '//Resources  cost: %.3f Cr\n' % self._res_cost
        s += 'entryCost = %d\n' % np.ceil(self.entry_cost(self._cost-self._res_cost))
        s += 'cost = %d\n' % np.ceil(self._cost)
        s += 'mass = %.6f\n' % self._init_mass
        s += 'specificMass = %s //weights: [ %s ]\n' % (', '.join(str(m) for m in self._spec_mass),
                                                        ', '.join(str(w) for w in self._weights))
        s += 'specificCost = %s //weights: [ %s ]\n' % (', '.join(str(m) for m in self._spec_cost),
                                                        ', '.join(str(w) for w in self._cost_weights))
        return s
    #end def

    def print_masses(self, _from=0.5, _to=4.0, step=0.5):
        for s in np.arange(_from, _to+step/2.0, step):
            print s, self.mass(s)
        print ''
#end class

max_stars_repo_name: allista/hangar

max_issues_repo_name: allista/hangar

max_forks_repo_name: allista/hangar


text:
import os
import sys
from time import time
from pickle import load, dump
from typing import List, Union
from numpy import argsort
from torch import no_grad
from torch.cuda import is_available as cuda_is_available
from transformers import DPRReader, DPRReaderTokenizer

class EnglishDPRRetriever(object):
    def __init__(self,
                dpr_fn:str,
                tokenizer_fn:str,
                tokenizer_max_len:int,
                ):

        self.dpr = DPRReader.from_pretrained(dpr_fn)
        self.tokenizer_max_len  = tokenizer_max_len
        self.tokenizer  = DPRReaderTokenizer.from_pretrained(tokenizer_fn, max_len=tokenizer_max_len)
        device = 'cuda' if cuda_is_available() else 'cpu'
        self.dpr.to(device)
        self.device = device

    def __call__(self,
                questions: str,
                passages: List[str],
                inner_batch_size: int=1,
                top: int=None):
        ''' Both "questions" and "passages" must be in English
            For each question, return the ids of the passages ordered by relevance
            up to top, if specified
        '''
        assert inner_batch_size > 0, f'Passage batch size ({inner_batch_size}) should be at least 1.'
        tokenizer = self.tokenizer
        model = self.dpr
        model.eval()

        if isinstance(questions, str):
            questions = [questions]

        retrieved_ids = []
        retrieved_relevances = []
        with no_grad():
            for question in questions:
                relevances = []
                for i in range(0, len(passages), inner_batch_size):
                    batch_passages = passages[i: i + inner_batch_size]
                    titles = [f'{len(relevances)}'] * len(batch_passages)

                    encoded_inputs = tokenizer(
                        questions=question,
                        titles=titles,
                        texts=batch_passages,
                        return_tensors='pt',
                        padding=True,
                        max_length=self.tokenizer_max_len,
                        truncation=True
                    )

                    for input_key in encoded_inputs:
                        encoded_inputs[input_key] = encoded_inputs[input_key].to(self.device)

                    outputs = model(return_dict=True, **encoded_inputs)
                    outputs = outputs.relevance_logits
                    relevances += outputs
                relevances = [r.item() for r in relevances]
                ids = argsort(relevances)[::-1]
                relevances = [relevances[index] for index in ids]
                retrieved_ids.append(ids[:top])
                retrieved_relevances.append(relevances[:top])
        assert len(retrieved_ids) == len(retrieved_relevances)

        return retrieved_ids, retrieved_relevances

# if __name__ == '__main__':
#     from time import time

#     question_ptbr = ["Qual a localidade do projeto?",
#                      "Qual o contexto da palha de trigo?",
#                      "Qual o efeito do Ômega na soja?"]

#     rank = rank_passages(question_ptbr[2:],\
#                         batch_size=10,\
#                         passages_file_name=f'{os.getcwd()}/tmp/reports_contents.pck',
#                         verbose=True)

#     print(len(rank))
#     for index in range(len(rank)):
#         print(rank[index]['question'])
#         for position in rank[index]['passage_rank']:
#             print(position['relevance'])
#             print(position['passage'])
#             print()

max_stars_repo_name: vitoryeso/tasks

max_issues_repo_name: vitoryeso/tasks

max_forks_repo_name: vitoryeso/tasks

reason: from numpy


text:
"""

                _loaders.py

Code for loading data into tensorflow datasets
"""
import numpy as np
import tensorflow as tf
#from PIL import Image
from patchwork._util import tiff_to_array
from patchwork._augment import augment_function
from patchwork._tasks import _rotate

import os

def _generate_imtypes(fps):
    """
    Input a list of filepaths and return an array mapping
    filetypes to an integer index:

        png 0
        jpg 1
        gif 2
        tif 3
    """
    imtypes = np.zeros(len(fps), dtype=np.int64)
    for i in range(len(fps)):
        t = fps[i].lower()
        if (".jpg" in t) or ("jpeg" in t):
            imtypes[i] = 1
        elif ".gif" in t:
            imtypes[i] = 2
        elif ".tif" in t:
            imtypes[i] = 3
    return imtypes

def _build_load_function(path, imshape, norm, num_channels, single_channel,
                         augment=False):
    """
    macro to build a function that handles all the loading. the
    load function takes in two inputs (file path and integer specifying file
    type) and returns the loaded image

    Note- this used to return a tf.function, but when used within
    a tensorflow Dataset I'd occasionally get "InvalidArgumentError:
        The graph couldn't be sorted in topological order.
        [Op:OptimizeDataset]" errors
    """
    if path.lower().endswith(".jpg"):
        decode = tf.io.decode_jpeg
    elif path.lower().endswith(".png"):
        decode = tf.io.decode_png
    elif path.lower().endswith(".gif"):
        decode = tf.io.decode_gif
    else:
        assert False, "unsupported file type"

    def _load_img(x,y):
        loaded = tf.io.read_file(x)
        img = decode(loaded)
        if single_channel:
            #resized = tf.concat(num_channels*[resized], -1)
            img = tf.concat(num_channels*[img], -1)
        # normalize
        img = tf.cast(img[:,:,:num_channels], tf.float32)/norm
        #img = tf.reshape(normed, (imshape[0], imshape[1], num_channels))
        if augment:
            img = augment_function(imshape, augment)(img)
        # TWO RESIZING OPERATIONS HERE
        # first: tf.image.resize(), which will resample the image if it
        # doesn't have the right dimensions
        img = tf.image.resize(img, imshape)
        # in some cases tf.image.resize() will return tensors of shape
        # [imshape[0], imshape[1], None], so now we'll specify the
        # channel dimension explicitly.
        img = tf.reshape(img, [imshape[0], imshape[1], num_channels])
        return img,y

    return _load_img

def _select(x,i):
    # convenience function to simplify code in the dual-input case.
    # if the config is a float or int, use it for both inputs
    if isinstance(x,float)|isinstance(x,int):
        return x
    # if the config is a container- return the ith element
    else:
        return x[i]

def _image_file_dataset(fps, ys=None, imshape=(256,256),
                 num_parallel_calls=None, norm=255,
                 num_channels=3, shuffle=False,
                 single_channel=False, augment=False):
    """
    Basic tool to load images into a tf.data.Dataset using
    PIL.Image or gdal instead of the tensorflow decode functions

    :fps: list of filepaths
    :ys: optional list of labels
    :imshape: constant shape to resize images to
    :num_parallel_calls: number of processes to use for loading
    :norm: value for normalizing images
    :num_channels: channel depth to truncate images to
    :shuffle: whether to shuffle the dataset
    :single_channel: if True, expect a single-channel input image and
        stack it num_channels times.
    :augment: optional, dictionary of augmentation parameters

    Returns tf.data.Dataset object with structure (x,y) if labels were passed,
        and (x) otherwise. images (x) are a 3D float32 tensor and labels
        should be a 0D int64 tensor
    """
    # PRE-BUILT DATASET CASE
    if isinstance(fps, tf.data.Dataset):
        ds = fps
        if augment:
            _aug = augment_function(imshape, augment)
            ds = ds.map(_aug, num_parallel_calls=num_parallel_calls)
    # DIRECTORY OF TFRECORD FILES CASE
    elif isinstance(fps, str):
        if augment:
            augment = augment_function(imshape, augment)
        ds = load_dataset_from_tfrecords(fps, imshape, num_channels,
                                         num_parallel_calls=num_parallel_calls,
                                         map_fn=augment)
    # LIST OF FILES CASE: list of filepaths (probably what almost
    # always will get used)
    elif isinstance(fps[0], str):
        if ys is None:
            no_labels = True
            ys = np.zeros(len(fps), dtype=np.int64)
        else:
            no_labels = False
        # get an integer index for each filepath
        #imtypes = _generate_imtypes(fps)
        ds = tf.data.Dataset.from_tensor_slices((fps, ys))
        # do the shuffling before loading so we can have a big queue without
        # taking up much memory
        if shuffle:
            ds = ds.shuffle(len(fps))
        _load_img = _build_load_function(fps[0], imshape, norm, num_channels,
                                         single_channel, augment)
        #ds = ds.map(lambda x,t,y: (_load_img(x),y),
        #            num_parallel_calls=num_parallel_calls)
        ds = ds.map(_load_img, num_parallel_calls=num_parallel_calls)
        # if no labels were passed, strip out the y.
        if no_labels:
            ds = ds.map(lambda x,y: x)

    else:
        assert False, "what are these inputs"

    return ds

def dataset(fps, ys = None, imshape=(256,256), num_channels=3,
                 num_parallel_calls=None, norm=255, batch_size=256,
                 augment=False, shuffle=False,
                 single_channel=False):
    """
    return a tf dataset that iterates over a list of images once

    :fps: list of filepaths
    :ys: array of corresponding labels
    :imshape: constant shape to resize images to
    :num_channels: channel depth of images
    :batch_size: just what you think it is
    :augment: augmentation parameters (or True for defaults, or False to disable)
    :shuffle: whether to shuffle the dataset
    :single_channel: if True, expect a single-channel input image and
        stack it num_channels times.

    Returns
    :ds: tf.data.Dataset object to iterate over data. The dataset returns
        (x,y) tuples unless unlab_fps is included, in which case the structure
        will be ((x, x_unlab), y)
    :num_steps: number of steps (for passing to tf.keras.Model.fit())
    """
    ds = _image_file_dataset(fps, ys=ys, imshape=imshape, num_channels=num_channels,
                      num_parallel_calls=num_parallel_calls, norm=norm,
                      shuffle=shuffle, single_channel=single_channel,
                      augment=augment)
    # CUSTOM DATASET CASE
    if isinstance(fps, tf.data.Dataset):
        num_steps = None
    # SINGLE-INPUT CASE (DEFAULT)
    elif isinstance(fps[0], str):
        num_steps = int(np.ceil(len(fps)/batch_size))

    # DUAL-INPUT CASE
    else:
        num_steps = int(np.ceil(len(fps[0])/batch_size))

    ds = ds.batch(batch_size)
    ds = ds.prefetch(1)

    return ds, num_steps

def stratified_training_dataset(fps, y, imshape=(256,256), num_channels=3,
                 num_parallel_calls=None, batch_size=256, mult=10,
                    augment=True, norm=255, single_channel=False):
    """
    Training dataset for DeepCluster.
    Build a dataset that provides stratified samples over labels

    :fps: list of strings containing paths to image files
    :y: array of cluster assignments- should have same length as fp
    :imshape: constant shape to resize images to
    :num_channels: channel depth of images
    :batch_size: just what you think it is
    :mult: not in paper; multiplication factor to increase
        number of steps/epoch. set to 1 to get paper algorithm
    :augment: augmentation parameters (or True for defaults, or False to disable)
    :single_channel: if True, expect a single-channel input image and
        stack it num_channels times.

    Returns
    :ds: tf.data.Dataset object to iterate over data
    :num_steps: number of steps (for passing to tf.keras.Model.fit())
    """
    # sample indices to use
    indices = np.arange(len(fps))
    K = y.max()+1
    samples_per_cluster = mult*int(len(fps)/K)

    sampled_indices = []
    sampled_labels = []
    # for each cluster
    for k in range(K):
        # find indices of samples assigned to it
        cluster_inds = indices[y == k]
        # only sample if at least one is assigned. note that
        # the deepcluster paper takes an extra step here.
        if len(cluster_inds) > 0:
            samps = np.random.choice(cluster_inds, size=samples_per_cluster,
                            replace=True)
            sampled_indices.append(samps)
            sampled_labels.append(k*np.ones(len(samps), dtype=np.int64))
    # concatenate sampled indices for each cluster
    sampled_indices = np.concatenate(sampled_indices, 0)
    sampled_labels = np.concatenate(sampled_labels, 0)
    # and shuffle their order together
    reorder = np.random.choice(np.arange(len(sampled_indices)),
                          size=len(sampled_indices), replace=False)
    sampled_indices = sampled_indices[reorder]
    sampled_labels = sampled_labels[reorder]
    # SINGLE-INPUT CASE
    if isinstance(fps[0], str):
        fps = np.array(fps)[sampled_indices]
    # DUAL-INPUT CASE
    else:
        fps = [np.array(fps[0])[sampled_indices],
                np.array(fps[1])[sampled_indices]]

    # NOW CREATE THE DATASET
    ds, _ = dataset(fps, ys=sampled_labels,
                      imshape=imshape, num_channels=num_channels,
                      num_parallel_calls=num_parallel_calls, norm=norm,
                      shuffle=False, single_channel=single_channel,
                      augment=augment, batch_size=batch_size)

    return ds

def _get_features(fcn, downstream_labels, avpool=False,
                  return_images=False, **input_config):
    """
    Load features into memory either from a dictionary mapping filepaths
    to labels, or a dataset that generates batches of (image, label) pairs

    :fcn: Keras fully-convolutional network
    :downstream_labels: dictionary mapping image file paths to labels, or a
        dataset returning image/label pairs
    :avpool: average-pool feature tensors before fitting linear model. if False, flatten instead.
    :return_images: if True, also return a big array of all the stacked-up
        raw images
    :input_config: kwargs for patchwork.loaders.dataset()

    Returns
    :acc: float; test accuracy
    :cm: 2D numpy array; confusion matrix
    """
    # if input is a dictionary, build the dataset
    if not isinstance(downstream_labels, tf.data.Dataset):
        X = list(downstream_labels.keys())
        Y = list(downstream_labels.values())
        # multiple input case
        if "," in X[0]:
            X0 = [x.split(",")[0] for x in X]
            X1 = [x.split(",")[1] for x in X]
            ds, num_steps = dataset([X0, X1], ys=Y,
                                shuffle=False,
                                **input_config)
        # single input case
        else:
            ds, num_steps = dataset(X, ys=Y, shuffle=False,
                                            **input_config)
    else:
        ds = downstream_labels
    # run labeled images through the network and flatten or average
    features = []
    labels = []
    images = []
    for x,y in ds:
        features.append(fcn(x).numpy())
        labels.append(y.numpy())
        if return_images: images.append(x)
    features = np.concatenate(features, 0)
    labels = np.concatenate(labels, 0)
    labels = np.array([str(l) for l in labels.ravel()])
    if return_images: images = np.concatenate(images, 0)

    if avpool:
        features = features.mean(axis=1).mean(axis=1)
    else:
        features = features.reshape(features.shape[0], -1)

    if return_images:
        return features, labels, images
    else:
        return features, labels

def _build_rotation_dataset(imfiles, imshape=(256,256), batch_size=256,
                      num_parallel_calls=None, norm=255,
                      num_channels=3, augment=False,
                      single_channel=False):
    """
    """
    ds = _image_file_dataset(imfiles, shuffle=True, augment=augment,
                             imshape=imshape, norm=norm,
                             single_channel=single_channel,
                             num_parallel_calls=num_parallel_calls)

    ds = ds.map(_rotate, num_parallel_calls=num_parallel_calls)
    ds = ds.batch(batch_size)
    ds = ds.prefetch(1)
    return ds

def _get_rotation_features(fcn, imfiles, avpool=True, **input_config):
    """
    Generate features without labels, using a self-supervised rotation task. See "Evaluating
    Self-Supervised Pretraining Without Using Labels" by Reed et al for details.

    Currently only supports single-input networks.

    :fcn: Keras fully-convolutional network
    :imfiles: list of image filepaths, or a dataset that generates images
    :avpool: average-pool feature tensors before fitting linear model. if False, flatten instead.
    :input_config: kwargs for patchwork.loaders.dataset()

    Returns
    :features: 2D array of flattened or averaged feature vectors
    :labels: 1D array of labels
    """
    inpt = {k:input_config[k] for k in input_config if k != "batch_size"}
    # if input is a dictionary, build the dataset
    if not isinstance(imfiles, tf.data.Dataset):
        ds = _image_file_dataset(imfiles, shuffle=False, augment=False, **inpt)
    else:
        ds = imfiles

    ds = ds.map(_rotate, num_parallel_calls=input_config.get("num_parallel_calls", None))
    ds = ds.batch(input_config.get("batch_size", 32))

    # run labeled images through the network and flatten or average
    features = []
    labels = []
    for x,y in ds:
        features.append(fcn(x).numpy())
        labels.append(y.numpy())
    features = np.concatenate(features, 0)
    labels = np.concatenate(labels, 0).ravel()

    if avpool:
        features = features.mean(axis=1).mean(axis=1)
    else:
        features = features.reshape(features.shape[0], -1)

    return features, labels

def _fixmatch_unlab_dataset(fps, weak_aug, str_aug, imshape=(256,256),
                            num_parallel_calls=None, norm=255,
                            num_channels=3, single_channel=False,
                            batch_size=64):
    """
    Macro to build the weak/strong augmented pairs for FixMatch
    semisupervised learning
    """
    _weakaug = augment_function(imshape, weak_aug)
    _strongaug = augment_function(imshape, str_aug)
    def aug_pair(x):
        return _weakaug(x), _strongaug(x)

    ds = _image_file_dataset(fps, imshape=imshape,
                             num_parallel_calls=num_parallel_calls,
                             norm=norm, num_channels=num_channels,
                             shuffle=True,
                             single_channel=single_channel,
                             augment=False)
    ds = ds.map(aug_pair, num_parallel_calls=num_parallel_calls)
    ds = ds.batch(batch_size)
    ds = ds.prefetch(1)
    return ds

def load_dataset_from_tfrecords(record_dir, imshape, num_channels,
                                shuffle=2048, num_parallel_calls=None,
                                map_fn=None, num_images=1, gzip=True):
    """
    Load a directory structure of tfrecord files (like you'd build with save_to_tfrecords)
    into a tensorflow dataset. Wrapper for tf.data.TFRecordDataset().

    Assumes tfrecord files are saved with .tfrecord or .snapshot.

    :record_dir: top-level directory containing record files
    :imshape: (H,W) dimensions of image
    :num_channels: number of channels
    :shuffle: if not False, size of shuffle queue
    :num_parallel_calls: number of parallel readers/mappers for loading and parsing
        the dataset
    :map_fn: function to map across dataset during loading (for example, for augmentation)
    :gzip: whether tfrecord was saved using GZIP compression
    """
    if gzip:
        comp = "GZIP"
    else:
        comp = "NONE"
    # single-image case: dataset returns a single tensor
    if num_images == 1:
        element_spec = tf.TensorSpec(shape=(imshape[0],imshape[1],num_channels),
                                     dtype=tf.float32)
    # multiple-image case: dataset returns a TUPLE of `num_images` tensors
    else:
        element_spec = tuple(tf.TensorSpec(shape=(imshape[0],imshape[1],num_channels),
                                     dtype=tf.float32) for
                         _ in range(num_images))
    # note that this function may change in the future
    ds = tf.data.experimental.load(record_dir, element_spec, compression=comp)
    # if a map function was included, map across the dataset
    if map_fn is not None:
        ds = ds.map(map_fn, num_parallel_calls=num_parallel_calls)
    if shuffle:
        ds = ds.shuffle(shuffle)
    return ds


text:
#!/usr/bin/env python
# encoding: utf-8
"""
getRef.py

Created by www on 11:32 pm, Sep 11, 2018
"""
import numpy as np
import sys
import os

def loadFile(inputFile, outputFile):
    o   = open(outputFile, 'w+')
    with open(inputFile) as f:
        for line in f:
            line    = line.strip()
            if not line:
                o.write('\n')
                continue
            info    = line.split()
            o.write('%s\t' % info[0])

    o.close()

def main():
    inputFile    = sys.argv[1]
    outputFile   = sys.argv[2]

    loadFile(inputFile, outputFile)

if __name__ == '__main__':
    main()

max_forks_repo_name: asdcid/Haplotig-separation


text:
#!/usr/bin/env python
"""Compute metrics for evaluating the 3D reconstruction"""

import argparse
import os
import sys

import numpy as np

from ..metrics import PerPixelMeanDepthError, Accuracy, Completeness,\
    VoxelMask, FiltersFactory, ReduceDensity
from ..pointcloud import Pointcloud, get_pointcloud

from .arguments import add_metrics_related_arguments, build_dataset,\
    add_dataset_related_arguments
from .slicing import frame_idxs_type

def build_filter_factory(scene, min_distance, output_directory=None):
    filters = []
    if scene.observation_mask is not None:
        filters.append(
            VoxelMask(scene.bbox, scene.observation_mask, output_directory)
        )

    if min_distance != -1:
        filters.append(ReduceDensity(min_distance, output_directory))

    return FiltersFactory(filters)

def build_metric(
    metric_name,
    borders,
    filter_factory,
    truncate,
    use_pc_from_depthmap
):
    return {
        "ppmde": PerPixelMeanDepthError(borders),
        "accuracy": Accuracy(
            filter_factory,
            truncate,
            borders,
            use_pc_from_depthmap
        ),
        "completeness": Completeness(
            filter_factory,
            truncate,
            borders,
            use_pc_from_depthmap
         )
    }[metric_name]

def main(argv=None):
    parser = argparse.ArgumentParser(
        description="Compute the 3D reconstruction metrics"
    )
    parser.add_argument(
        "dataset_directory",
        help="The dataset to load"
    )
    parser.add_argument(
        "predictions_directory",
        help="The directory containing the model's predictions"
    )
    parser.add_argument(
        "metric",
        nargs="+",
        choices=["ppmde", "accuracy", "completeness"],
        help="Choose a metric"
    )
    parser.add_argument(
        "--output_directory",
        default="/tmp/",
        help="The directory to save the predicted point cloud"
    )
    parser.add_argument(
        "--scene_idx",
        type=int,
        default=0,
        help="Choose the scene to compute the metrics for"
    )
    parser.add_argument(
        "--frame_idxs",
        type=frame_idxs_type,
        default=":",
        help=("Choose the frames that correspond to the ordered prediction "
              "files")
    )
    parser.add_argument(
        "--predicted_files_format",
        default="depth_%03d.npy",
        help="The format for the predicted file"
    )
    parser.add_argument(
        "--use_pc_from_depthmap",
        action="store_true",
        help=("Set when we want to estimate the gt point cloud from the depth "
              "images")
    )

    add_dataset_related_arguments(parser)
    add_metrics_related_arguments(parser)
    args = parser.parse_args(argv)

    dataset = build_dataset(
        args.dataset_type,
        args.dataset_directory,
        args.illumination_condition
    )

    # Get the scene and then get the frame idxs
    scene = dataset.get_scene(args.scene_idx)
    frame_idxs = np.arange(scene.n_images)[args.frame_idxs]

    # Create the predicted point cloud
    depthmaps = [
        os.path.join(
            args.predictions_directory,
            args.predicted_files_format % (i,)
        )
        for i in frame_idxs
    ]
    predicted_pointcloud = get_pointcloud(
        scene,
        frame_idxs,
        depthmaps,
        args.with_consistency_check,
        borders=args.borders,
        consistency_threshold=args.consistency_threshold,
        n_neighbors=args.n_neighbors
    )
    print "Saving predicted point-cloud for scene %d ..." % (args.scene_idx,)
    if not os.path.exists(args.output_directory):
        os.makedirs(args.output_directory)

    predicted_pointcloud.save_ply(
        os.path.join(
            args.output_directory,
            "predicted_pc_s_%d.ply" % (args.scene_idx,)
        )
    )

    # For every metric
    for mi in args.metric:
        metric = build_metric(
            mi,
            args.borders,
            build_filter_factory(
                scene,
                args.min_distance,
                output_directory=args.output_directory
            ),
            args.truncate,
            args.use_pc_from_depthmap
        )
        values, points = metric.compute(
            scene,
            frame_idxs,
            depthmaps,
            predicted_pointcloud
        )
        # Create and save a pointcloud with the filtered points, where each
        # point will be colored based on the value of the computed metric
        if points is None:
            pass
        else:
            Pointcloud(points).save_colored_ply(
                os.path.join(
                    args.output_directory,
                    "%s_pc.ply" % (mi)
                ),
                values
            )
        print mi, " mean: ", values.mean(), " median: ", np.median(values)

if __name__ == "__main__":
    main(sys.argv[1:])

max_stars_repo_name: paschalidoud/raynet

max_issues_repo_name: paschalidoud/raynet

max_forks_repo_name: paschalidoud/raynet