text:
import os
import pytest
import sys

import numpy as np
from numpy.testing import assert_allclose
from pytest import raises as assert_raises
from scipy.sparse.linalg._svdp import _svdp
from scipy.sparse import csr_matrix, csc_matrix, coo_matrix

TOLS = {
    np.float32: 1e-4,
    np.float64: 1e-8,
    np.complex64: 1e-4,
    np.complex128: 1e-8,
}

_dtype_map = {
    "single": np.float32,
    "double": np.float64,
    "complex8": np.complex64,
    "complex16": np.complex128,
}

def is_complex_type(dtype):
    return np.dtype(dtype).char.isupper()

def is_32bit():
    return sys.maxsize <= 2**32  # (usually 2**31-1 on 32-bit)

_dtype_testing = []
for dtype in _dtype_map:
    if 'complex' in dtype and is_32bit():
        # PROPACK has issues w/ complex on 32-bit; see gh-14433
        marks = [pytest.mark.skip]
    elif 'complex' in dtype:
        marks = [pytest.mark.slow]  # type: ignore[list-item]
    else:
        marks = []
    _dtype_testing.append(pytest.param(dtype, marks=marks))
_dtype_testing = tuple(_dtype_testing)  # type: ignore[assignment]

def generate_matrix(constructor, n, m, f,
                    dtype=float, rseed=0, **kwargs):
    """Generate a random sparse matrix"""
    rng = np.random.RandomState(rseed)
    if is_complex_type(dtype):
        M = (- 5 + 10 * rng.rand(n, m)
             - 5j + 10j * rng.rand(n, m)).astype(dtype)
    else:
        M = (-5 + 10 * rng.rand(n, m)).astype(dtype)
    M[M.real > 10 * f - 5] = 0
    return constructor(M, **kwargs)

def assert_orthogonal(u1, u2, rtol, atol):
    """Check that the first k rows of u1 and u2 are orthogonal"""
    A = abs(np.dot(u1.conj().T, u2))
    assert_allclose(A, np.eye(u1.shape[1], u2.shape[1]), rtol=rtol, atol=atol)

def check_svdp(n, m, constructor, dtype, k, irl_mode, which, f=0.8):
    tol = TOLS[dtype]

    M = generate_matrix(np.asarray, n, m, f, dtype)
    Msp = constructor(M)

    u1, sigma1, vt1 = np.linalg.svd(M, full_matrices=False)
    u2, sigma2, vt2, _ = _svdp(Msp, k=k, which=which, irl_mode=irl_mode,
                               tol=tol)

    # check the which
    if which.upper() == 'SM':
        u1 = np.roll(u1, k, 1)
        vt1 = np.roll(vt1, k, 0)
        sigma1 = np.roll(sigma1, k)

    # check that singular values agree
    assert_allclose(sigma1[:k], sigma2, rtol=tol, atol=tol)

    # check that singular vectors are orthogonal
    assert_orthogonal(u1, u2, rtol=tol, atol=tol)
    assert_orthogonal(vt1.T, vt2.T, rtol=tol, atol=tol)

@pytest.mark.parametrize('ctor', (np.array, csr_matrix, csc_matrix))
@pytest.mark.parametrize('precision', _dtype_testing)
@pytest.mark.parametrize('irl', (True, False))
@pytest.mark.parametrize('which', ('LM', 'SM'))
def test_svdp(ctor, precision, irl, which):
    np.random.seed(0)
    dtype = _dtype_map[precision]
    n, m, k = 10, 20, 3
    if which == 'SM' and not irl:
        message = "`which`='SM' requires irl_mode=True"
        with assert_raises(ValueError, match=message):
            check_svdp(n, m, ctor, dtype, k, irl, which)
    else:
        if is_32bit() and is_complex_type(dtype):
            message = 'PROPACK complex-valued SVD methods not available '
            with assert_raises(TypeError, match=message):
                check_svdp(n, m, ctor, dtype, k, irl, which)
        else:
            check_svdp(n, m, ctor, dtype, k, irl, which)

def load_real(folder, precision, file='illc1850.coord'):
    dtype = _dtype_map[precision]
    path = os.path.join(folder, precision, 'Examples', file)
    # Coordinate Text File
    with open(path) as f:
        m, n, nnz = (int(val) for val in f.readline().split())
        coord = np.array([[float(val) for val in line.split()] for line in f])
    i = coord[:, 0].astype(int) - 1
    j = coord[:, 1].astype(int) - 1
    data = coord[:, 2].astype(dtype)
    A = coo_matrix((data, (i, j)))
    return A

def load_complex(folder, precision):
    file = 'mhd1280b.cua'

    dtype = _dtype_map[precision]
    path = os.path.join(folder, precision, 'Examples', file)
    with open(path, "r") as f:
        contents = f.readlines()
    file_metadata = contents[1].split()
    matrix_metadata = contents[2].split()
    datum_length = 15  # hard code rather than getting from contents[3]

    n_header = 4
    n_total, n_indptr, n_indices, n_data, _ = (int(n) for n in file_metadata)
    m, n, nnz, _ = (int(n) for n in matrix_metadata[1:])

    line_indptr = n_header
    line_indices = line_indptr + n_indptr
    line_data = line_indices + n_indices

    def _concatenate_lines(lines):
        return "".join([line.rstrip() for line in lines])

    indptr = _concatenate_lines(contents[line_indptr:line_indices])
    indptr = np.asarray([int(i) for i in indptr.split()])-1

    indices = _concatenate_lines(contents[line_indices:line_data])
    indices = np.asarray([int(i) for i in indices.split()])-1

    data = _concatenate_lines(contents[line_data:])
    data = np.asarray([float(data[i:i+datum_length])
                       for i in range(0, len(data), datum_length)])
    real, imag = data[::2], data[1::2]
    data = real + imag*1.0j
    data.astype(dtype)

    return csc_matrix((data, indices, indptr), (m, n))

def load_sigma(folder, precision="double", irl=False):
    dtype = _dtype_map[precision]
    s_name = "Sigma_IRL.ascii" if irl else "Sigma.ascii"
    path = os.path.join(folder, precision, 'Examples', 'Output', s_name)
    pydtype = {
        np.complex64: complex,
        np.complex128: complex,
        np.float32: float,
        np.float64: float,
    }[dtype]
    with open(path) as f:
        data = np.array([pydtype(line.split()[1]) for line in f]).astype(dtype)
    return data

def load_uv(folder, precision="double", uv="U", irl=False):
    dtype = _dtype_map[precision]
    uv_name = (uv + "_IRL.ascii") if irl else (uv + ".ascii")
    path = os.path.join(folder, precision, 'Examples', 'Output', uv_name)
    with open(path) as f:
        m, n = (int(val) for val in f.readline().split())
        if precision in {'single', 'double'}:
            data = np.array([dtype(val.strip()) for val in f], dtype=dtype)
        else:
            data = np.loadtxt(
                path, dtype=dtype, skiprows=1, delimiter='\n',
                converters={0: lambda s: complex(
                    *[float(n) for n in s.decode()[1:-1].split(',')])})
    return data.reshape((n, m)).T

@pytest.mark.parametrize('precision', _dtype_testing)
@pytest.mark.parametrize('irl', (False, True))
def test_examples(precision, irl):
    atol = {
        'single': 1e-3,
        'double': 1e-9,
        'complex8': 1e-4,
        'complex16': 1e-9,
    }[precision]

    path_prefix = os.path.dirname(__file__)
    relative_path = ['..', '_propack', 'PROPACK']
    folder = os.path.join(path_prefix, *relative_path)

    A = {
        'single': load_real,
        'double': load_real,
        'complex8': load_complex,
        'complex16': load_complex,
    }[precision](folder, precision)
    s_expected = load_sigma(folder, precision, irl=irl)
    u_expected = load_uv(folder, precision, "U", irl=irl)
    vh_expected = load_uv(folder, precision, "V", irl=irl).T

    k = len(s_expected)
    u, s, vh, _ = _svdp(A, k, irl_mode=irl, random_state=0)

    # Check singular values
    assert_allclose(s, s_expected.real, atol=atol)

    # complex example matrix has many repeated singular values, so check only
    # beginning non-repeated singular vectors to avoid permutations
    sv_check = 27 if precision in {'complex8', 'complex16'} else k
    u = u[:, :sv_check]
    u_expected = u_expected[:, :sv_check]
    vh = vh[:sv_check, :]
    vh_expected = vh_expected[:sv_check, :]
    s = s[:sv_check]

    assert_allclose(np.abs(u), np.abs(u_expected), atol=atol)
    assert_allclose(np.abs(vh), np.abs(vh_expected), atol=atol)

    # Check orthogonality of singular vectors
    assert_allclose(np.eye(u.shape[1]), u.conj().T @ u, atol=atol)
    assert_allclose(np.eye(vh.shape[0]), vh @ vh.conj().T, atol=atol)

    # Ensure the norm of the difference between the np.linalg.svd and
    # PROPACK reconstructed matrices is small
    u3, s3, vh3 = np.linalg.svd(A.todense())
    u3 = u3[:, :sv_check]
    s3 = s3[:sv_check]
    vh3 = vh3[:sv_check, :]
    A3 = u3 @ np.diag(s3) @ vh3
    recon = u @ np.diag(s) @ vh
    assert_allclose(np.linalg.norm(A3 - recon), 0, atol=atol)

@pytest.mark.parametrize('shifts', (None, -10, 0, 1, 10, 70))
@pytest.mark.parametrize('precision', ('single', 'double'))
def test_shifts(shifts, precision):
    np.random.seed(0)
    n, k = 70, 10
    A = np.random.random((n, n))
    if shifts is not None and ((shifts < 0) or (k > min(n-1-shifts, n))):
        with pytest.raises(ValueError):
            _svdp(A, k, shifts=shifts, kmax=5*k, irl_mode=True)
    else:
        _svdp(A, k, shifts=shifts, kmax=5*k, irl_mode=True)

@pytest.mark.slow
@pytest.mark.xfail()
def test_shifts_accuracy():
    np.random.seed(0)
    n, k = 70, 10
    A = np.random.random((n, n)).astype(np.double)
    u1, s1, vt1, _ = _svdp(A, k, shifts=None, which='SM', irl_mode=True)
    u2, s2, vt2, _ = _svdp(A, k, shifts=32, which='SM', irl_mode=True)
    # shifts <= 32 doesn't agree with shifts > 32
    # Does agree when which='LM' instead of 'SM'
    assert_allclose(s1, s2)

max_stars_repo_name: lorentzenchr/scipy

max_issues_repo_name: lorentzenchr/scipy

max_forks_repo_name: lorentzenchr/scipy

reason: import numpy,from numpy,from scipy


text:
import sys
import scipy as SP
import h5py
import pdb

def vec(M):
    return M.reshape((M.size, 1), order = 'F')

def to_list(x):
    if x is None:               r = []
    elif type(x) is not list:   r = [x]
    else:                       r = x
    return r

def smartAppend(table,name,value):
    """
    helper function for apppending in a dictionary
    """
    if name not in list(table.keys()):
        table[name] = []
    table[name].append(value)

def smartSum(x,key,value):
    """ create a new page in x if key is not a page of x
        otherwise add value to x[key] """
    if key not in list(x.keys()):
        x[key] = value
    else:   x[key]+=value

def dumpDictHdf5(RV,o):
    """ Dump a dictionary where each page is a list or an array """
    for key in list(RV.keys()):
        o.create_dataset(name=key,data=SP.array(RV[key]),chunks=True,compression='gzip')

def smartDumpDictHdf5(RV,o):
    """ Dump a dictionary where each page is a list or an array or still a dictionary (in this case, it iterates)"""
    for key in list(RV.keys()):
        if type(RV[key])==dict:
            g = o.create_group(key)
            smartDumpDictHdf5(RV[key],g)
        else:
            o.create_dataset(name=key,data=SP.array(RV[key]),chunks=True,compression='gzip')

ext: py

max_stars_repo_name: DenisSch/svca

max_issues_repo_name: DenisSch/svca

max_forks_repo_name: DenisSch/svca

reason: import scipy


text:
# -*- coding: utf-8 -*-
#Pregnant women1 with COVID-19, United States, January 22, 2020 - October 25, 2021
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MaxNLocator

#import csv file
df  = pd.read_csv("cases_of_covid19_among_pregnant_women_by_week_of_diagnosis_download.csv")
df1 = pd.read_csv("pregnancy_data_download.csv")
df2 = pd.read_csv("Covid-Vaccine-among-prengnant.csv")
#graph1 ********
dateDiag = df.iloc[2:92,0]
numCase  = df.iloc[2:92,1]

fig,axes = plt.subplots(1,1)

plt.bar(dateDiag, numCase)
plt.xticks(rotation=45)
axes.xaxis.set_major_locator(MaxNLocator(23))
plt.xlabel('week of Diagnosis')
plt.ylabel('Number of cases')
plt.title('Cases of COVID-19 among Pregnant Women by Week of Diagnosis*')
plt.savefig('by Week of Diagnosis.png', dpi=300, bbox_inches='tight')
plt.show()
#*****************************************
age        = df1.iloc[5:14,0]
numAgeCase = df1.iloc[5:14,1]

plt.bar(age, numAgeCase)
plt.xticks(rotation=45)
plt.xlabel('Age Group')
plt.ylabel('Number of Cases')
plt.title('Pregnant women with COVID-19 by age, United States')
plt.savefig('Pregnant women by age.png', dpi=300, bbox_inches='tight')
plt.show()
#***************************************
Race    = df1.iloc[16:21,0]
numRace = df1.iloc[16:21,1]

plt.bar(Race, numRace)
plt.xticks(rotation=45)
plt.xlabel('Race / Ethnicity')
plt.ylabel('Number of Cases')
plt.title('Pregnant women with COVID-19 by race/ethnicity, United States')
plt.savefig('By raceEthnicity', dpi=300, bbox_inches='tight')
plt.show()
#**************************************
Hospital = df1.iloc[30:33,0]
numHos   = df1.iloc[30:33,1]

plt.bar(Hospital, numHos)
plt.xlabel('Hospitalization Status')
plt.ylabel('Number of Cases')
plt.title('Pregnant women with COVID-19 by Hospitalization Status, United States')
plt.savefig('Hospitalization Status.png', dpi=300, bbox_inches='tight')
plt.show()
#***************************Vaccine in prengnancy 14 Dec 2020 - 30 Oct 2021
prior = [[0]]*34
priorAndDuring = [[0]]*34
During = [[0]]*34
dateVaccine = [[0]]*34
CDCVaccineData1 = df2.iloc[0:102]
n = 0
a = 0
b = 0
for i in range(0,102,3):
    prior[n]       = (CDCVaccineData1.iloc[i,4])
    dateVaccine[n] = CDCVaccineData1.iloc[i,0]
    n = n+1
for i in range(1,102,3):
    priorAndDuring[a] = CDCVaccineData1.iloc[i,4]
    a = a+1
for i in range(2,102,3):
    During[b] = CDCVaccineData1.iloc[i,4]
    b = b+1

prior1 = np.array(prior)
priorAndDuring1 = np.array(priorAndDuring)
During1 = np.array(During)

plt.bar(dateVaccine, prior1)
plt.bar(dateVaccine, priorAndDuring1, bottom = prior1)
plt.bar(dateVaccine, During1, bottom = prior1 + priorAndDuring1)
plt.xticks(rotation=45, fontsize = 7)
plt.xlabel('Date')
plt.ylabel('Vaccination Coverage(%)')
plt.title('Percent of Pregnant People Aged 18-49 Years \n Fully Vaccinated Covid-19,United States')
plt.legend(['Prior to Pregnancy','Prior to and During Pregnancy','During Pregnancy'])
plt.savefig('FullyVaccinatedCovid-19.png', dpi=300, bbox_inches='tight')
plt.show()
#************************************

reason: import numpy


text:
import pandas as pd
import numpy as np
from pandas import DataFrame
from glob import glob
import sys
from time import time
path = sys.argv[1]
mp_data=sys.argv[2]
l_files = glob(path +  '/*.csv')

t = pd.read_csv(l_files[0])
lc = t.columns[2:]

l_len = len(l_files)
ligands = DataFrame(columns = lc)

for l in l_files:
    t = pd.read_csv(l)
    ligands = ligands.append(t.ix[:, 2:], ignore_index = True)
    print(t.shape, t.ix[0, 2], l)

#print(ligands.shape)
#print(l_len)
p_files = glob('all_pocket' +'/*_pocVec.txt')
p_len = len(p_files)
proteins = DataFrame()

for i, p in enumerate(p_files):
    #print(i * 1.0 / p_len)
    f = open(p)
    t = f.read()
    t = t[:-1].replace('[ ', '').replace('[', '').replace('\n', '').split()
    t.insert(0, p.split('/')[-1].split('.')[0])
    t = DataFrame(np.array(t).reshape(1, -1))
    if t.shape == (1, 301):
        proteins = proteins.append(t, ignore_index=True)
    else:
        print(t.shape, p)

ligands.to_hdf(mp_data+'/ligands.h5', 'df')

proteins.to_hdf(mp_data+'/proteins.h5', 'df')

ligands = pd.read_hdf(mp_data+'/ligands.h5', 'df')
proteins = pd.read_hdf(mp_data+'/proteins.h5', 'df')

print(ligands.shape)
#print(l_len)
print(proteins.shape)

#print(p_len)
############################################
############################################
pos = DataFrame()
pos_list_used = []

ligands_n = DataFrame(columns = lc)
for i in range(proteins.shape[0]):
    protein = proteins.ix[i:i, 1:]
    pname = proteins.ix[:, 0][i].split('_')[0]
    ligand_n = ligands[ligands.ix[:, 0] == pname+'_ligand.pdb'].ix[:, 0:]
    print ligand_n.shape
    ##compound=bind(pname,pname)
    ligands_n = ligands_n.append(ligand_n, ignore_index = True)

'''
ligands_n = pd.DataFrame(np.repeat(ligands.values, proteins.shape[0], axis=0))

proteins = pd.DataFrame(np.array(proteins.values))

#proteins = pd.DataFrame(np.vstack((proteins.values, proteins.values)))
'''
print ligands_n.shape
ligand = ligands_n.ix[:, 1:]
protein = proteins.ix[:, 1:]
print ligand.shape
print protein.shape

for i in range(proteins.shape[0]):
    pname = proteins.ix[i, 0]
    pos_list_used.append(pname)

compound = DataFrame(np.hstack((protein, ligand)))
pos = pos.append(compound, ignore_index = True)

pos.to_hdf(mp_data+'/pos.h5', 'df')

pos_read = pd.read_hdf(mp_data+'/pos.h5', 'df')
pos_list_used = DataFrame(pos_list_used)
pos_list_used.to_csv(mp_data+'/pos_list.csv', index = False, header = ['name'])
pos_list_used.head()


text:
import numpy
from collections import namedtuple
Coordinate = namedtuple('Coordinate', 'x y')

def get_distance(first_loc, second_loc):
    distance_x = first_loc.x - second_loc.x
    distance_y = first_loc.y - second_loc.y
    return abs(distance_x) + abs(distance_y)

def closest_point(location, list_of_points):
    distances = [get_distance(location, x) for x in list_of_points]
    smallest_distance = min(distances)
    smallest_distances = []
    for i, distance in enumerate(distances):
        if distance == smallest_distance:
            smallest_distances.append(i)

    if len(smallest_distances) == 1:
        return smallest_distances[0]
    else:
        return -1

def within_range(location, list_of_points, range):
    return int(sum([get_distance(location, x) for x in list_of_points]) < range)

INPUT = open("advent2018_day06_input.txt", "r").read().split("\n")
ALTINPUT = """1, 1
1, 6
8, 3
3, 4
5, 5
8, 9""".split("\n")

INPUT_SHAPE = (380, 380)
PROCESSED_INPUT = [Coordinate(x=int(x.split(",")[0]), y=int(x.split(",")[1])) for x in INPUT]
RANGE = 10000

#INPUT_SHAPE = (10, 10)
#PROCESSED_INPUT = [Coordinate(x=int(x.split(",")[0]), y=int(x.split(",")[1])) for x in ALTINPUT]
#RANGE = 32

grid = numpy.full(INPUT_SHAPE, fill_value=-1, dtype=int)

infinite_points = set()

for i, item in numpy.ndenumerate(grid):
    # i is returned in format (y, x)
    coord = Coordinate(i[1], i[0])
    grid[coord.y, coord.x] = closest_point(coord, PROCESSED_INPUT)
    if coord.x == 0 or coord.y == 0 or coord.x == INPUT_SHAPE[1]-1 or coord.y == INPUT_SHAPE[0]-1:
        infinite_points.add(grid[coord.y, coord.x])

areas = {}
for i, item in enumerate(PROCESSED_INPUT):
    if i not in infinite_points:
        areas[i] = (grid == i).sum()

largest_area = max(areas, key=areas.get)
print("Max is index %d with area %d" % (largest_area, areas[largest_area]))

grid_two = numpy.zeros(INPUT_SHAPE, dtype=int)
for i, item in numpy.ndenumerate(grid):
    # i is returned in format (y, x)
    coord = Coordinate(i[1], i[0])
    grid_two[coord.y, coord.x] = within_range(coord, PROCESSED_INPUT, RANGE)

print("Size of area within range: %d" % grid_two.sum())


text:
from Control import Parameters as p
import numpy as np
import pandas as pd
import math

class PointUpdate:
    timestep = 1
    id_col = 'ID'
    prev_col = 'Prev_ID'
    sail_col = 'Sail'
    time_col = 'Time'
    x_col = 'x_loc'
    y_col = 'y_loc'
    rad_col = 'Radius'
    the_o_col = 'Theta_Origin'
    speed_col = 'Speed'
    dircount = 360
    m2knts = 1.943844
    pm_angle = 90
    pm_res = 0.5
    conv = 360 / (2 * np.pi)
    w_res = p.res
    cols = [id_col, prev_col, time_col, x_col, y_col, rad_col, the_o_col, speed_col, sail_col]

    def __init__(self, wind, polars, initial_x, initial_y):
        self.wind = wind   #as vector field
        self.ini_x = initial_x
        self.ini_y = initial_y
        self.polar = np.array(polars[1])
        self.sails = polars[0]

    def func(self, time):
        #function to increase the number of points in boundary as time increases
        t0 = 40
        t10 = 300
        return int((t10 - t0) * math.log(time, 10) + (t10 - t0))

    def winddata(self, data):
        #Retuns wind data at given location
        #converts direction to wind direction relative boat location
        x = int(data[0] // p.res[0])
        y = int(data[1] // p.res[1])

        u, v = self.wind[0][y, x], self.wind[1][y, x]
        t_w = 2 * np.pi - np.arctan2(v, u)
        s_w = int(np.sqrt(u ** 2 + v ** 2))
        #print(u, v, s_w, t_w)
        return t_w, s_w

    def bestpolar(self, k, angle):
        #Returns best polar given wind data
        #angle in degrees
        #Checks number of polars availible for checks
        #k refers to windspeed
        x1, x2 = math.floor(angle) % self.dircount, math.ceil(angle) % self.dircount
        if len(self.polar.shape) == 2:
            y1, y2 = self.polar[x1, k], self.polar[x2, k]
            pol = 0
        elif len(self.polar.shape) == 3:
            inx = np.argmax(self.polar[:, x1, k])
            y1, y2 = self.polar[inx, x1, k], self.polar[inx, x2, k]
            pol = inx
        else:
            print('Error with polar data')
            y1, y2 = 0, 0
            pol = 0

        #profprms a linear regression on two speeds given two angles
        if x1 == x2 or y1 == y2:
            boat_speed = y1
        else:
            coef = np.polyfit([x1, x2], [y1, y2], 1)
            boat_speed = coef[0] * angle + coef[1]
        return pol, boat_speed

    def UpdatePoints(self, maxt, res):
        count = 0
        time = 0
        #two lists, one storeing the previous iteration, the other all the data to store
        prevdata = []
        datahold = []

        #for loop to run through data
        for t in range(time, maxt, self.timestep):
            print(t)

            dataprev = prevdata
            prevdata = []
            if count == 0:
                #first iteration
                #[id_col, prev_col, time_col, x_col, y_col, rad_col, the_o_col, speed_col, sail_col]
                data = [count, count, t, self.ini_x, self.ini_y, 0, 0, 0, np.nan]
                prevdata.append(data)
                datahold.append(data)
                count += 1

            else:
                dat = np.array(dataprev)

                for dir_angle in np.linspace(0, self.dircount, self.func(t)):
                    #Find max dist from centre given previous points
                    prevdata.append(self.ReturnPoint(dir_angle, dat, t, count))
                    count += 1
                datahold.extend(prevdata)

        return pd.DataFrame(datahold, columns=self.cols)

    def Sailable(self, point1, point2, t_w, s_w):
        #Returns false if points can be reached in time
        #find sailing angle
        theta = np.arctan2(point2[1] - point1[1], point2[0] - point1[0])

        theta = theta * self.conv if theta > 0 else (2 * np.pi + theta) * self.conv
        dist = math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)

        #wind relative to boat
        boat_ang = (theta - t_w * self.conv) % self.dircount

        pol, speed = self.bestpolar(s_w, boat_ang)
        out = dist / speed < self.timestep if speed !=0 else False

        out = True if dist == 0 else out
        return out, pol, speed

    def MaxDist(self, point1, mindist, angle, t_w, s_w):
        #finds the maximum distance from origin to point2 given point1
        ang = ((np.pi / 2 - angle) % (2 * np.pi))   #converts angle from math to compass angles
        point2x, point2y = mindist * np.cos(ang) + self.ini_x, mindist * np.sin(ang) + self.ini_y

        dist = mindist
        distdelta = 2
        i = 0
        increase = True
        pol, speed = 0, 0
        if self.Sailable(point1, [point2x, point2y], t_w, s_w)[0]:
            while distdelta > 0.001 and i < 30:
                i += 1
                previnc = increase
                bool, pol, speed = self.Sailable(point1, [point2x, point2y], t_w, s_w)
                #print(angle, i, distdelta, dist, self.Sailable(point1, [point2x, point2y], t_w, s_w))
                if bool:
                    dist = dist + distdelta
                    increase = True
                else:
                    dist = dist - distdelta
                    increase = False
                point2x, point2y = dist * np.cos(ang) + self.ini_x, dist * np.sin(ang) + self.ini_y

                if i > 1 and (previnc != increase):
                    distdelta = distdelta / 2

        return dist, point2x, point2y, speed, pol

    def ReturnPoint(self, angle, list, time, newid):
        #given list, return point
        #find min distance given angle
        #list consists of all points in previous layer
        filtlist = list[(list[:, 6] >= angle - 1) & (list[:, 6] <= angle + 1)]
        mindist = np.min(filtlist[:, 5]) if filtlist.shape[0] > 0 else 0

        #run through set of points and find max distance
        dist = np.zeros((list.shape[0], 5))
        maxdist, id = 0, 0
        #run through every n-th row, and then find closest for reinspection
        n = 10
        for row in list[0::n]:
            point1 = [row[3], row[4]]
            t_w, s_w = self.winddata(point1)
            d = self.MaxDist(point1, mindist, angle / self.conv, t_w, s_w)[0]
            if d > maxdist:
                maxdist = d
                id = row[0]

        #filter array and re search points not run
        filtlist = list[(list[:, 0] >= id - n) & (list[:, 0] <= id + n)]
        i = 0
        for row in filtlist:
            point1 = [row[3], row[4]]
            t_w, s_w = self.winddata(point1)
            dist[i, :] = self.MaxDist(point1, mindist, angle / self.conv, t_w, s_w)
            i += 1

        id = np.argmax(dist[:, 0])
        #cols = [id_col, prev_col, time_col, x_col, y_col, rad_col, the_o_col, speed_col, sail_col]
        point = [newid, list[id, 0], time, dist[id, 1], dist[id, 2], dist[id, 0], angle, dist[id, 3], dist[id, 4]]

        return point

max_stars_repo_name: ushham/SailingTracking

max_issues_repo_name: ushham/SailingTracking

max_forks_repo_name: ushham/SailingTracking


text:
import itertools as itt
import pandas as pd
import numpy as np
from typing import Iterable, Tuple, List
from timeseriescv.core import BaseTimeSeriesCrossValidator, purge, embargo

class PurgedWalkForwardCV(BaseTimeSeriesCrossValidator):
    """
    Purged walk-forward cross-validation

    As described in Advances in financial machine learning, Marcos Lopez de Prado, 2018.

    The samples are decomposed into n_splits folds containing equal numbers of samples, without shuffling. In each cross
    validation round, n_test_splits contiguous folds are used as the test set, while the train set consists in between
    min_train_splits and max_train_splits immediately preceding folds.

    Each sample should be tagged with a prediction time pred_time and an evaluation time eval_time. The split is such
    that the intervals [pred_times, eval_times] associated to samples in the train and test set do not overlap. (The
    overlapping samples are dropped.)

    With split_by_times = True in the split method, it is also possible to split the samples in folds spanning equal
    time intervals (using the prediction time as a time tag), instead of folds containing equal numbers of samples.

    Parameters
    ----------
    n_splits : int, default=10
        Number of folds. Must be at least 2.

    n_test_splits : int, default = 1
        Number of folds used in the test set. Must be at least 1.

    min_train_splits: int, default = 2
        Minimal number of folds to be used in the train set.

    max_train_splits: int, default = None
        Maximal number of folds to be used in the train set. If None, there is no upper limit.

    """
    def __init__(self, n_splits=10, n_test_splits=1, min_train_splits=2, purge_count=3):
        super().__init__(n_splits)
        self.n_test_splits = n_test_splits
        self.min_train_splits = min_train_splits
        self.max_train_splits = self.n_splits - self.n_test_splits
        self.purge_count = purge_count
        self.fold_bounds = []

    def split(self, X: pd.DataFrame, split_by_time: bool = False) -> Iterable[Tuple[np.ndarray, np.ndarray]]:
        """
        Yield the indices of the train and test sets.

        Although the samples are passed in the form of a pandas dataframe, the indices returned are position indices,
        not labels.

        Parameters
        ----------
        X : pd.DataFrame, shape (n_samples, n_features), required
            Samples. Only used to extract n_samples.

        split_by_time: bool
            If False, the folds contain an (approximately) equal number of samples. If True, the folds span identical
            time intervals.

        Returns
        -------
        train_indices: np.ndarray
            A numpy array containing all the indices in the train set.

        test_indices : np.ndarray
            A numpy array containing all the indices in the test set.

        """
        super().split(X)

        # Fold boundaries
        self.fold_bounds = compute_fold_bounds(self, split_by_time)

        count_folds = 0
        for fold_bound in self.fold_bounds:
            if count_folds < self.min_train_splits:
                count_folds = count_folds + 1
                continue
            if self.n_splits - count_folds < self.n_test_splits:
                break
            # Computes the bounds of the test set, and the corresponding indices
            test_indices = self.compute_test_set(fold_bound, count_folds)
            test_length = len(test_indices)
            # Computes the train set indices
            train_indices = self.compute_train_set(fold_bound, count_folds)
            train_length = len(train_indices)
            train_indices = train_indices[train_length-test_length:]
            test_indices = test_indices[self.purge_count:]

            count_folds = count_folds + 1
            yield train_indices, test_indices

    def compute_train_set(self, fold_bound: int, count_folds: int) -> np.ndarray:
        """
        Compute the position indices of samples in the train set.

        Parameters
        ----------
        fold_bound : int
            Bound between the train set and the test set.

        count_folds : int
            The number (starting at 0) of the first fold in the test set.

        Returns
        -------
        train_indices: np.ndarray
            A numpy array containing all the indices in the train set.

        """
        if count_folds > self.max_train_splits:
            start_train = self.fold_bounds[count_folds - self.max_train_splits]
        else:
            start_train = 0
        train_indices = np.arange(start_train, fold_bound)
        # Purge
        train_indices = purge(self, train_indices, fold_bound, self.indices[-1])
        return train_indices

    def compute_test_set(self, fold_bound: int, count_folds: int) -> np.ndarray:
        """
        Compute the indices of the samples in the test set.

        Parameters
        ----------
        fold_bound : int
            Bound between the train set and the test set.

        count_folds : int
            The number (starting at 0) of the first fold in the test set.

        Returns
        -------
        test_indices: np.ndarray
            A numpy array containing the test indices.

        """
        if self.n_splits - count_folds > self.n_test_splits:
            end_test = self.fold_bounds[count_folds + self.n_test_splits]
        else:
            end_test = self.indices[-1] + 1
        return np.arange(fold_bound, end_test)

def compute_fold_bounds(cv: BaseTimeSeriesCrossValidator, split_by_time: bool) -> List[int]:
    """
    Compute a list containing the fold (left) boundaries.

    Parameters
    ----------
    cv: BaseTimeSeriesCrossValidator
        Cross-validation object for which the bounds need to be computed.
    split_by_time: bool
        If False, the folds contain an (approximately) equal number of samples. If True, the folds span identical
        time intervals.
    """
    if split_by_time:
        full_time_span = cv.pred_times.max() - cv.pred_times.min()
        fold_time_span = full_time_span / cv.n_splits
        fold_bounds_times = [cv.pred_times.iloc[0] + fold_time_span * n for n in range(cv.n_splits)]
        return cv.pred_times.searchsorted(fold_bounds_times)
    else:
        return [fold[0] for fold in np.array_split(cv.indices, cv.n_splits)]


text:
"""RefineNet-CRP-RCU-blocks in PyTorch
RefineNet-PyTorch for non-commercial purposes
Copyright (c) 2018, Vladimir Nekrasov (vladimir.nekrasov@adelaide.edu.au)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import torch.nn as nn
import torch.nn.functional as F

import numpy as np
import torch

IMG_SCALE  = 1./255
IMG_MEAN = np.array([0.485, 0.456, 0.406]).reshape((1, 1, 3))
IMG_STD = np.array([0.229, 0.224, 0.225]).reshape((1, 1, 3))

def maybe_download(model_name, model_url, model_dir=None, map_location=None):
    import os, sys
    from six.moves import urllib
    if model_dir is None:
        torch_home = os.path.expandwei(os.getenv('TORCH_HOME', '~/.torch'))
        model_dir = os.getenv('TORCH_MODEL_ZOO', os.path.join(torch_home, 'models'))
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    filename = '{}.pth.tar'.format(model_name)
    cached_file = os.path.join(model_dir, filename)
    if not os.path.exists(cached_file):
        url = model_url
        sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file))
        urllib.request.urlretrieve(url, cached_file)
    return torch.load(cached_file, map_location=map_location)

def prepare_img(img):
    return (img * IMG_SCALE - IMG_MEAN) / IMG_STD

def batchnorm(in_planes):
    "batch norm 2d"
    return nn.BatchNorm2d(in_planes, affine=True, eps=1e-5, momentum=0.1)

def conv3x3(in_planes, out_planes, stride=1, bias=False):
    "3x3 convolution with padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=bias)

def conv1x1(in_planes, out_planes, stride=1, bias=False):
    "1x1 convolution"
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
                     padding=0, bias=bias)

def convbnrelu(in_planes, out_planes, kernel_size, stride=1, groups=1, act=True):
    "conv-batchnorm-relu"
    if act:
        return nn.Sequential(
            nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=int(kernel_size / 2.), groups=groups,
                      bias=False),
            batchnorm(out_planes),
            nn.ReLU6(inplace=True))
    else:
        return nn.Sequential(
            nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=int(kernel_size / 2.), groups=groups,
                      bias=False),
            batchnorm(out_planes))

class CRPBlock(nn.Module):

    def __init__(self, in_planes, out_planes, n_stages):
        super(CRPBlock, self).__init__()
        for i in range(n_stages):
            setattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'),
                    conv3x3(in_planes if (i == 0) else out_planes,
                            out_planes, stride=1,
                            bias=False))
        self.stride = 1
        self.n_stages = n_stages
        self.maxpool = nn.MaxPool2d(kernel_size=5, stride=1, padding=2)

    def forward(self, x):
        top = x
        for i in range(self.n_stages):
            top = self.maxpool(top)
            top = getattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'))(top)
            x = top + x
        return x

stages_suffixes = {0: '_conv',
                   1: '_conv_relu_varout_dimred'}

class RCUBlock(nn.Module):

    def __init__(self, in_planes, out_planes, n_blocks, n_stages):
        super(RCUBlock, self).__init__()
        for i in range(n_blocks):
            for j in range(n_stages):
                setattr(self, '{}{}'.format(i + 1, stages_suffixes[j]),
                        conv3x3(in_planes if (i == 0) and (j == 0) else out_planes,
                                out_planes, stride=1,
                                bias=(j == 0)))
        self.stride = 1
        self.n_blocks = n_blocks
        self.n_stages = n_stages

    def forward(self, x):
        for i in range(self.n_blocks):
            residual = x
            for j in range(self.n_stages):
                x = F.relu(x)
                x = getattr(self, '{}{}'.format(i + 1, stages_suffixes[j]))(x)
            x += residual
        return x

max_stars_repo_name: manmaygit/recurrent-unet


text:
import pytest
from torch.nn import MSELoss as _MSELoss
from neuralpy.loss_functions import MSELoss

import numpy as np
import torch

@pytest.mark.parametrize(
	"reduction",
	[("invalid", "", 12, 6.3)]
)
def test_cce_should_throw_value_error(reduction):
    with pytest.raises(ValueError) as ex:
        x = MSELoss(reduction=reduction)

@pytest.mark.parametrize(
	"reduction",
	[("none"), ("mean"), ("sum")]
)
def test_mse_get_layer_method(reduction):
	x = MSELoss(reduction=reduction)

	details = x.get_loss_function()

	assert isinstance(details, dict) == True

	assert issubclass(details["loss_function"], _MSELoss) == True

	assert isinstance(details["keyword_arguments"], dict) == True

	assert details["keyword_arguments"]["reduction"] == reduction

def test_mse_get_layer_method_without_parameters():
	x = MSELoss()

	details = x.get_loss_function()

	assert isinstance(details, dict) == True

	assert issubclass(details["loss_function"], _MSELoss) == True

	assert isinstance(details["keyword_arguments"], dict) == True

	assert details["keyword_arguments"]["reduction"] == 'mean'


text:
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/07_evaluation.ipynb (unless otherwise specified).

__all__ = ['BaseEvaluator', 'NumeraiClassicEvaluator', 'NumeraiSignalsEvaluator']

# Cell
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm.auto import tqdm
from typing import Tuple, Union

from .numerframe import NumerFrame, create_numerframe
from .postprocessing import FeatureNeutralizer

# Cell
class BaseEvaluator:
    """
    Evaluation functionality that is relevant for both
    Numerai Classic and Numerai Signals.

    :param era_col: Column name pointing to eras. \n
    Most commonly "era" for Numerai Classic and "friday_date" for Numerai Signals. \n
    :param fast_mode: Will skip compute intensive metrics if set to True,
    namely max_exposure, feature neutral mean, TB200 and TB500.
    """
    def __init__(self, era_col: str = "era", fast_mode=False):
        self.era_col = era_col
        self.fast_mode = fast_mode

    def full_evaluation(
        self,
        dataf: NumerFrame,
        example_col: str,
        pred_cols: list = None,
        target_col: str = "target",
    ) -> pd.DataFrame:
        """
        Perform evaluation for each prediction column in the NumerFrame
        against give target and example prediction column.
        """
        val_stats = pd.DataFrame()
        dataf = dataf.fillna(0.5)
        pred_cols = dataf.prediction_cols if not pred_cols else pred_cols
        for col in tqdm(pred_cols, desc="Evaluation: "):
            col_stats = self.evaluation_one_col(
                dataf=dataf,
                pred_col=col,
                target_col=target_col,
                example_col=example_col,
            )
            val_stats = pd.concat([val_stats, col_stats], axis=0)
        return val_stats

    def evaluation_one_col(
        self,
        dataf: Union[pd.DataFrame, NumerFrame],
        pred_col: str,
        target_col: str,
        example_col: str,
    ):
        """
        Perform evaluation for one prediction column
        against given target and example prediction column.
        """
        col_stats = pd.DataFrame()
        # Compute stats
        val_corrs = self.per_era_corrs(
            dataf=dataf, pred_col=pred_col, target_col=target_col
        )
        mean, std, sharpe = self.mean_std_sharpe(era_corrs=val_corrs)
        max_drawdown = self.max_drawdown(era_corrs=val_corrs)
        apy = self.apy(era_corrs=val_corrs)
        example_corr = self.example_correlation(
            dataf=dataf, pred_col=pred_col, example_col=example_col
        )
        mmc_mean, mmc_std, mmc_sharpe = self.mmc(
            dataf=dataf,
            pred_col=pred_col,
            target_col=target_col,
            example_col=example_col,
        )

        col_stats.loc[pred_col, "target"] = target_col
        col_stats.loc[pred_col, "mean"] = mean
        col_stats.loc[pred_col, "std"] = std
        col_stats.loc[pred_col, "sharpe"] = sharpe
        col_stats.loc[pred_col, "max_drawdown"] = max_drawdown
        col_stats.loc[pred_col, "apy"] = apy
        col_stats.loc[pred_col, "mmc_mean"] = mmc_mean
        col_stats.loc[pred_col, "mmc_std"] = mmc_std
        col_stats.loc[pred_col, "mmc_sharpe"] = mmc_sharpe
        col_stats.loc[pred_col, "corr_with_example_preds"] = example_corr

        # Compute intensive stats
        if not self.fast_mode:
            max_feature_exposure = self.max_feature_exposure(
                dataf=dataf, pred_col=pred_col
            )
            fn_mean, fn_std, fn_sharpe = self.feature_neutral_mean_std_sharpe(
                dataf=dataf, pred_col=pred_col, target_col=target_col
            )
            tb200_mean, tb200_std, tb200_sharpe = self.tbx_mean_std_sharpe(
                dataf=dataf, pred_col=pred_col, target_col=target_col, tb=200
            )
            tb500_mean, tb500_std, tb500_sharpe = self.tbx_mean_std_sharpe(
                dataf=dataf, pred_col=pred_col, target_col=target_col, tb=500
            )
            col_stats.loc[pred_col, "max_feature_exposure"] = max_feature_exposure
            col_stats.loc[pred_col, "feature_neutral_mean"] = fn_mean
            col_stats.loc[pred_col, "feature_neutral_std"] = fn_std
            col_stats.loc[pred_col, "feature_neutral_sharpe"] = fn_sharpe
            col_stats.loc[pred_col, "tb200_mean"] = tb200_mean
            col_stats.loc[pred_col, "tb200_std"] = tb200_std
            col_stats.loc[pred_col, "tb200_sharpe"] = tb200_sharpe
            col_stats.loc[pred_col, "tb500_mean"] = tb500_mean
            col_stats.loc[pred_col, "tb500_std"] = tb500_std
            col_stats.loc[pred_col, "tb500_sharpe"] = tb500_sharpe
        return col_stats

    def per_era_corrs(
        self, dataf: pd.DataFrame, pred_col: str, target_col: str
    ) -> pd.Series:
        """Correlation between prediction and target for each era."""
        return dataf.groupby(dataf[self.era_col]).apply(
            lambda d: self._normalize_uniform(d[pred_col].fillna(0.5)).corr(
                d[target_col]
            )
        )

    def mean_std_sharpe(
        self, era_corrs: pd.Series
    ) -> Tuple[np.float64, np.float64, np.float64]:
        """
        Average, standard deviation and Sharpe ratio for
        correlations per era.
        """
        mean = pd.Series(era_corrs.mean()).item()
        std = pd.Series(era_corrs.std(ddof=0)).item()
        sharpe = mean / std
        return mean, std, sharpe

    @staticmethod
    def max_drawdown(era_corrs: pd.Series) -> np.float64:
        """Maximum drawdown per era."""
        # Arbitrarily large window
        rolling_max = (
            (era_corrs + 1).cumprod().rolling(window=9000, min_periods=1).max()
        )
        daily_value = (era_corrs + 1).cumprod()
        max_drawdown = -((rolling_max - daily_value) / rolling_max).max()
        return max_drawdown

    @staticmethod
    def apy(era_corrs: pd.Series, stake_compounding_lag: int = 4) -> np.float64:
        """
        Annual percentage yield.
        :param era_corrs: Correlation scores by era
        :param stake_compounding_lag: Compounding lag for Numerai rounds (4 for Numerai Classic)
        """
        payout_scores = era_corrs.clip(-0.25, 0.25)
        payout_daily_value = (payout_scores + 1).cumprod()
        apy = (
            ((payout_daily_value.dropna().iloc[-1]) ** (1 / len(payout_scores)))
            ** (
                52 - stake_compounding_lag
            )  # 52 weeks of compounding minus n for stake compounding lag
            - 1
        ) * 100
        return apy

    def example_correlation(
        self, dataf: Union[pd.DataFrame, NumerFrame], pred_col: str, example_col: str
    ):
        """Correlations with example predictions."""
        return self.per_era_corrs(
            dataf=dataf,
            pred_col=pred_col,
            target_col=example_col,
        ).mean()

    def max_feature_exposure(
        self, dataf: Union[pd.DataFrame, NumerFrame], pred_col: str
    ) -> np.float64:
        """Maximum exposure over all features."""
        max_per_era = dataf.groupby(self.era_col).apply(
            lambda d: d[dataf.feature_cols].corrwith(d[pred_col]).abs().max()
        )
        max_feature_exposure = max_per_era.mean(skipna=True)
        return max_feature_exposure

    def feature_neutral_mean_std_sharpe(
        self, dataf: Union[pd.DataFrame, NumerFrame], pred_col: str, target_col: str
    ) -> Tuple[np.float64, np.float64, np.float64]:
        """
        Feature neutralized mean performance.
        More info: https://docs.numer.ai/tournament/feature-neutral-correlation
        """
        fn = FeatureNeutralizer(pred_name=pred_col, proportion=1.0)
        neutralized_dataf = fn(dataf=dataf)
        neutral_corrs = self.per_era_corrs(
            dataf=neutralized_dataf,
            pred_col=f"{pred_col}_neutralized_1.0",
            target_col=target_col,
        )
        mean, std, sharpe = self.mean_std_sharpe(era_corrs=neutral_corrs)
        return mean, std, sharpe

    def tbx_mean_std_sharpe(
        self, dataf: pd.DataFrame, pred_col: str, target_col: str, tb: int = 200
    ) -> Tuple[np.float64, np.float64, np.float64]:
        """
        Calculate Mean, Standard deviation and Sharpe ratio
        when we focus on the x top and x bottom predictions.
        :param tb: How many of top and bottom predictions to focus on.
        TB200 and TB500 are the most common situations.
        """
        tb_val_corrs = self._score_by_date(
            dataf=dataf, columns=[pred_col], target=target_col, tb=tb
        )
        return self.mean_std_sharpe(era_corrs=tb_val_corrs)

    def mmc(
        self, dataf: pd.DataFrame, pred_col: str, target_col: str, example_col: str
    ) -> Tuple[np.float64, np.float64, np.float64]:
        """
        MMC Mean, standard deviation and Sharpe ratio.
        More info: https://docs.numer.ai/tournament/metamodel-contribution
        """
        mmc_scores = []
        corr_scores = []
        for _, x in dataf.groupby(self.era_col):
            series = self._neutralize_series(
                self._normalize_uniform(x[pred_col]), (x[example_col])
            )
            mmc_scores.append(np.cov(series, x[target_col])[0, 1] / (0.29 ** 2))
            corr_scores.append(self._normalize_uniform(x[pred_col]).corr(x[target_col]))

        val_mmc_mean = np.mean(mmc_scores)
        val_mmc_std = np.std(mmc_scores)
        corr_plus_mmcs = [c + m for c, m in zip(corr_scores, mmc_scores)]
        corr_plus_mmc_sharpe = np.mean(corr_plus_mmcs) / np.std(corr_plus_mmcs)
        return val_mmc_mean, val_mmc_std, corr_plus_mmc_sharpe

    @staticmethod
    def _neutralize_series(series, by, proportion=1.0):
        scores = series.values.reshape(-1, 1)
        exposures = by.values.reshape(-1, 1)

        # this line makes series neutral to a constant column so that it's centered and for sure gets corr 0 with exposures
        exposures = np.hstack(
            (exposures, np.array([np.mean(series)] * len(exposures)).reshape(-1, 1))
        )

        correction = proportion * (
            exposures.dot(np.linalg.lstsq(exposures, scores, rcond=None)[0])
        )
        corrected_scores = scores - correction
        neutralized = pd.Series(corrected_scores.ravel(), index=series.index)
        return neutralized

    def _score_by_date(
        self, dataf: pd.DataFrame, columns: list, target: str, tb: int = None
    ):
        """
        Get era correlation based on given TB (x top and bottom predictions).
        :param tb: How many of top and bottom predictions to focus on.
        TB200 is the most common situation.
        """
        unique_eras = dataf[self.era_col].unique()
        computed = []
        for u in unique_eras:
            df_era = dataf[dataf[self.era_col] == u]
            era_pred = np.float64(df_era[columns].values.T)
            era_target = np.float64(df_era[target].values.T)

            if tb is None:
                ccs = np.corrcoef(era_target, era_pred)[0, 1:]
            else:
                tbidx = np.argsort(era_pred, axis=1)
                tbidx = np.concatenate([tbidx[:, :tb], tbidx[:, -tb:]], axis=1)
                ccs = [
                    np.corrcoef(era_target[idx], pred[idx])[0, 1]
                    for idx, pred in zip(tbidx, era_pred)
                ]
                ccs = np.array(ccs)
            computed.append(ccs)
        return pd.DataFrame(
            np.array(computed), columns=columns, index=dataf[self.era_col].unique()
        )

    @staticmethod
    def _normalize_uniform(df: pd.DataFrame) -> pd.Series:
        """Normalize predictions uniformly using ranks."""
        x = (df.rank(method="first") - 0.5) / len(df)
        return pd.Series(x, index=df.index)

    def plot_correlations(
        self,
        dataf: NumerFrame,
        pred_cols: list = None,
        target_col: str = "target",
        roll_mean: int = 20,
    ):
        """
        Plot per era correlations over time.
        :param roll_mean: How many eras should be averaged to compute a rolling score.
        """
        validation_by_eras = pd.DataFrame()
        pred_cols = dataf.prediction_cols if not pred_cols else pred_cols
        for pred_col in pred_cols:
            per_era_corrs = self.per_era_corrs(
                dataf, pred_col=pred_col, target_col=target_col
            )
            validation_by_eras.loc[:, pred_col] = per_era_corrs

        validation_by_eras.rolling(roll_mean).mean().plot(
            kind="line",
            marker="o",
            ms=4,
            title=f"Rolling Per Era Correlation Mean (rolling window size: {roll_mean})",
            figsize=(15, 5),
        )
        plt.legend(
            loc="upper center",
            bbox_to_anchor=(0.5, -0.05),
            fancybox=True,
            shadow=True,
            ncol=1,
        )
        plt.axhline(y=0.0, color="r", linestyle="--")
        plt.show()

        validation_by_eras.cumsum().plot(
            title="Cumulative Sum of Era Correlations", figsize=(15, 5)
        )
        plt.legend(
            loc="upper center",
            bbox_to_anchor=(0.5, -0.05),
            fancybox=True,
            shadow=True,
            ncol=1,
        )
        plt.axhline(y=0.0, color="r", linestyle="--")
        plt.show()
        return None

# Cell
class NumeraiClassicEvaluator(BaseEvaluator):
    """Evaluator for all metrics that are relevant in Numerai Classic."""
    def __init__(self, era_col: str = "era", fast_mode=False):
        super().__init__(era_col=era_col, fast_mode=fast_mode)

# Cell
class NumeraiSignalsEvaluator(BaseEvaluator):
    """Evaluator for all metrics that are relevant in Numerai Signals."""
    def __init__(self, era_col: str = "friday_date", fast_mode=False):
        super().__init__(era_col=era_col, fast_mode=fast_mode)

max_stars_repo_name: mickeybeurskens/numerblox

max_issues_repo_name: mickeybeurskens/numerblox

max_forks_repo_name: mickeybeurskens/numerblox


text:
import tensorflow as tf
import numpy as np
import pandas as pd
import math
from tqdm import tqdm
from utility import generate_token_embedding, CNN_model
import logging
import HP
from multiprocessing import Pool
from Embedding import Embedding

_ = Embedding.get_embedding()

logging.basicConfig(filename=HP.log_file_name, level=logging.INFO)

result_csv = pd.read_csv(HP.result_csv)
patient_ids = np.asarray(result_csv["patient_id"])
n_patient = len(patient_ids)

# define placeholders
input_x = tf.placeholder(tf.float32,
                         [None, HP.max_document_length, HP.max_sentence_length, HP.embedding_size],
                         name="input_x")
sent_length = tf.placeholder(tf.int32, [None], name="sent_length")
input_ys = []
for i in range(HP.multi_size):
    input_ys.append(tf.placeholder(tf.int32, [None, HP.num_classes], name="input_y"+str(i)))
category_index = tf.placeholder(tf.int32, [None, HP.max_document_length], name='category_index')
dropout_keep_prob = tf.placeholder(tf.float32, [], name="dropout_keep_prob")

# return the patient vector
_, _, patient_vector = CNN_model(input_x, input_ys, sent_length, category_index, dropout_keep_prob)

saver = tf.train.Saver()
num_batch = int(math.ceil(n_patient / HP.n_batch))
with tf.Session() as sess:
    saver.restore(sess, HP.model_path)

    # start train
    for i in tqdm(range(num_batch)):
        tmp_train_patient_name = patient_ids[i*HP.n_batch:min((i+1)*HP.n_batch, n_patient)]
        pool = Pool(processes=HP.read_data_thread_num)
        generate_token_embedding_results = pool.map(generate_token_embedding, tmp_train_patient_name)
        pool.close()
        pool.join()

        tmp_x = np.zeros([len(generate_token_embedding_results),
                         HP.n_max_sentence_num,
                         HP.n_max_word_num,
                         HP.embedding_size], dtype=np.float32)
        l = []
        tmp_cate = []
        for (M, r) in enumerate(generate_token_embedding_results):
            tmp_x[M] = r[0]
            l.append(r[1])
            tmp_cate.append(r[2])

        cate_id = np.stack(tmp_cate)
        l = np.asarray(l)
        feed_dict = {input_x: tmp_x,
                     sent_length: l,
                     category_index: cate_id,
                     dropout_keep_prob: 1.0}
        # logging.info("start to train")
        tmp_patient_vector = sess.run(patient_vector, feed_dict=feed_dict)
        for j in range(len(tmp_train_patient_name)):
            np.save(HP.patient_vector_directory + tmp_train_patient_name[j] + ".npy", tmp_patient_vector[j])

max_stars_repo_name: ruiwangcl/deepmimiciii

max_issues_repo_name: ruiwangcl/deepmimiciii

max_forks_repo_name: ruiwangcl/deepmimiciii


text:
import numpy as np

import pydigree as pyd
from pydigree.simulation import QuantitativeTrait

ninds = 5000
nloc = 1000
maf = 0.5

traitname = 'synthetic'
# Create population
pop = pyd.Population()

# Create chromosomes
for i in range(100):
	c = pyd.ChromosomeTemplate()
	c.add_genotype(maf, 0)
	pop.add_chromosome(c)

# Create trait QuantitativeTrait
trait = QuantitativeTrait('synthetic', 'quantitative', chromosomes=pop.chromosomes)
for i in range(100):
	trait.add_effect((i,0), 1 * (-1 if i % 2 else 1))

print('Locus mean genotypic value: {}'.format(trait.effects[0].expected_genotypic_value))
print('Locus variance: {}'.format(trait.effects[0].locus_additive_variance))

print('Expected trait mean: {}'.format(trait.expected_genotypic_value))
print('Expected trait variance: {}'.format(trait.additive_genetic_variance))
for i in range(ninds):
	i = pop.founder_individual()
	i.get_genotypes(linkeq=True)
	i.phenotypes[traitname] = trait.predict_phenotype(i)

y = np.array([i.phenotypes[traitname] for i in pop.individuals])
print('Observed trait mean {}'.format(y.mean()))
print('Observed trait variance: {}'.format(y.var()))

max_stars_repo_name: jameshicks/pydigree

max_issues_repo_name: jameshicks/pydigree

max_forks_repo_name: jameshicks/pydigree


text:
"""Sum of array elements over a given axis."""
from __future__ import annotations
from typing import Any, Optional, Sequence, Union

import numpy
import numpy.typing

from ..baseclass import ndpoly, PolyLike
from ..dispatch import implements, simple_dispatch

@implements(numpy.sum)
def sum(
    a: PolyLike,
    axis: Union[None, int, Sequence[int]] = None,
    dtype: Optional[numpy.typing.DTypeLike] = None,
    out: Optional[ndpoly] = None,
    keepdims: bool = False,
    **kwargs: Any,
) -> ndpoly:
    """
    Sum of array elements over a given axis.

    Args:
        a:
            Elements to sum.
        axis:
            Axis or axes along which a sum is performed. The default,
            axis=None, will sum all of the elements of the input array. If axis
            is negative it counts from the last to the first axis. If axis is
            a tuple of ints, a sum is performed on all of the axes specified in
            the tuple instead of a single axis or all the axes as before.
        dtype:
            The type of the returned array and of the accumulator in which the
            elements are summed.  The dtype of `a` is used by default unless
            `a` has an integer dtype of less precision than the default
            platform integer.  In that case, if `a` is signed then the platform
            integer is used while if `a` is unsigned then an unsigned integer
            of the same precision as the platform integer is used.
        out:
            Alternative output array in which to place the result. It must have
            the same shape as the expected output, but the type of the output
            values will be cast if necessary.
        keepdims:
            If this is set to True, the axes which are reduced are left
            in the result as dimensions with size one. With this option,
            the result will broadcast correctly against the input array.
        initial:
            Starting value for the sum.
        where:
            Elements to include in the sum.

    Returns:
        An array with the same shape as `a`, with the specified axis removed.
        If `a` is a 0-d array, or if `axis` is None, a scalar is returned. If
        an output array is specified, a reference to `out` is returned.

    Examples:
        >>> q0, q1, q2 = numpoly.variable(3)
        >>> poly = numpoly.polynomial([[1, q0], [q1, q2]])
        >>> numpoly.sum(poly)
        polynomial(q2+q1+q0+1)
        >>> numpoly.sum(poly, axis=0)
        polynomial([q1+1, q2+q0])

    """
    return simple_dispatch(
        numpy_func=numpy.sum,
        inputs=(a,),
        out=None if out is None else (out,),
        axis=axis,
        dtype=dtype,
        keepdims=keepdims,
        **kwargs
    )

max_stars_repo_name: jonathf/npoly

max_issues_repo_name: jonathf/npoly

max_forks_repo_name: jonathf/npoly


text:
import numpy as np
import time
from multiprocessing import Pool
from scipy.optimize import minimize

###STAGE 2###
import sys
from config import CODE_DIRECTORY
sys.path.append(CODE_DIRECTORY)

# User settings for CUSP
import settings
from set_settings import *
from cusp_demo_utils import *

# Load the optimized VQE parameters from stage 1 of CUSP
stage1_param_list = []

for bond_length in bond_lengths:
    stage1_param = np.load('data/stage1_param_{}.npy'.format(bond_length))
    stage1_param_list.append(stage1_param)

search_parameters_stage2 = ['w1', 'w2', 'z', 'cz']
fixed_w1 = .25
fixed_w2 = .5
fixed_z = 1
fixed_cz = 1

user_parameters_stage2 = np.array([search_parameters_stage2, fixed_w1, fixed_w2,
                          fixed_z, fixed_cz], dtype=object)
np.save('data/user_parameters_stage2', user_parameters_stage2)

import cusp_stage2
import stage2_opt_data

print('Stage 2 using the following bond lengths for training: {}\n'.format(bond_lengths))

# QAE settings
threshold = 0.10
n_qae_trials = 20

print('#### STAGE 2 OF CUSP NOW RUNNING ####\n')
opt_qae_params = stage2_opt_data.run_qae_optimization(training_states=stage1_param_list,
                                                 n_repetitions=num_trials,
                                                 exact=True,
                                                 noisy=include_gate_noise)

# Repeat optimization of QAE circuit while error value is above threshold
iter_count = 0
while stage2_opt_data.compute_avg_fid_proxy(params=opt_qae_params,
                                       training_states=stage1_param_list,
                                       n_repetitions=num_trials,
                                       exact=True,
                                       noisy=include_gate_noise) > threshold:
    if iter_count >= n_qae_trials:
        print('Surpassed the QAE iteration limit. Exiting loop.')
        break

    print('Trial {}: Quantum autoencoder learning had low fidelity. '
          'Trying again.'.format(iter_count))

    opt_qae_params = stage2_opt_data.run_qae_optimization(training_states=stage1_param_list,
                                                     n_repetitions=num_trials,
                                                     exact=True,
                                                     noisy=include_gate_noise)
    iter_count += 1

# Compute error of optimized QAE circuit
err = stage2_opt_data.compute_avg_fid_proxy(opt_qae_params, training_states=stage1_param_list,
                                       n_repetitions=num_trials, exact=True,
                                       noisy=include_gate_noise)
print('Quantum autoencoder learning succeeded with error : {}'.format(err))

opt_qae_params = fix_list(opt_qae_params, stage2_opt_data.all_param,stage2_opt_data.var_param,
                          stage2_opt_data.fixed_vals)
# Save QAE results
np.save('data/stage2_param', opt_qae_params)
print('')

reason: import numpy,from scipy


text:
""" This script is used to create random dependency structures"""

import random

# visualisation
import matplotlib.pyplot as plt
import networkx as nx

# local
# import conll3

class DependencyTree(object):
	"""
	A class used to build dependency trees

	Attributes
	----------
	size : int
		number of nodes in the tree
	nodes : list
		a list of the tree's nodes
	edges : list
		a list of the tree's edges
	root : int
		the root of the dependency tree
	floating_nodes : list
		a list of all the nodes that have no edges linking them to other nodes
	potential_governors : list
		a list of nodes that are part of the tree and can act as governors for floating_nodes

	Methods
	-------
	add_edge()
		randomly add an edge to the dependency tree
	parse()
		creates a complete random parse for the dependency tree
	view()
		visualize the tree using networkx
	toTree()
		produces a dict version of the Dependency Tree that can be used by the conll3 library
	"""

	def __init__(self, size, root=0):
		"""
		Parameters
		----------
		size : int
			number of nodes in the tree
		root : int
			the root of the dependency tree (default is 0)
		"""
		self.size = size
		self.nodes = [x for x in range(size)]
		self.edges = []
		self.root = root
		self.floating_nodes = [n for n in self.nodes if n!=self.root]
		self.potential_governors = [self.root]
		self.parse()

	def __repr__(self):
		representation = "Tree\n  Edges: ["
		for a,b in self.edges:
			representation += "({}, {}), ".format(a,b)
		representation = representation[:-2]+"]"
		return representation

	def add_edge(self):
		"""
		Randomly add an edge to the dependency tree

		Updates the edges attribute by linking one of the floating nodes
		to one of the potential governors.

		Parameters
		----------
		self: DependencyTree
			the current dependency tree with its nodes, edges...

		Updates
		----------
		edges : list
			adds a new edge
		floating_nodes : list
			removes the last attached node
		potential_governors : list
			adds the last attached node to the list
		"""
		dep= self.floating_nodes[0]
		gov = random.choice(self.potential_governors)
		edge = (dep, gov)
		self.edges.append(edge)
		self.potential_governors.append(dep)
		self.floating_nodes = self.floating_nodes[1:]

	def parse(self):
		"""
		Creates a complete random parse for the dependency tree

		Parameters
		----------
		self: DependencyTree
			the current dependency tree with its nodes, edges...

		Updates
		----------
		edges : list
			adds a new edge
		floating_nodes : list
			removes the last attached node
		potential_governors : list
			adds the last attached node to the list
		"""
		self.tree = dict()
		while self.floating_nodes:
			self.add_edge()
		for (node, gov) in self.edges:
			if gov in self.tree:
				self.tree[gov] += [node]
			else:
				self.tree[gov] = [node]
			if node not in self.tree:
				self.tree[node] = []
			else:
				continue

	def view(self):
		"""
		Visualize the Dependency Tree with networkx

		Parameters
		----------
		self: DependencyTree
			the current dependency tree with its nodes, edges...

		"""
		G = nx.Graph()
		G.add_nodes_from(self.nodes)
		G.add_edges_from(self.edges)
		nx.draw(G, font_weight="bold", with_labels=True)
		plt.show()

	def toTree(self):
		"""
		Produces a dict version of the Dependency Tree that can be used by the conll3 library

		Parameters
		----------
		self: DependencyTree
			the current dependency tree with its nodes, edges...

		Returns
		----------
		c_tree : Tree
			dictionary version of the Dependency Tree, can be used by conll3.
			This tree only contains information about the dependency relations between the nodes.
			- each token is named __n__ where n is the integer symbolizing the node
			- the tree by default has no UPOS or XPOS
			- the relations are either root or unk (for unknown)
		"""
		tree = dict()
		tree[self.root+1] = {"id":self.root+1,"gov":{0:"root"}, "t":"__"+str(self.root+1)+"__"}
		for (dep, gov) in self.edges:
			tree[dep+1] = {"id":dep+1,"gov":{gov+1:"unk"}, "t":"__"+str(dep+1)+"__"}
		c_tree = conll3.Tree(tree)
		return c_tree

def build_random_forest(specs):
	"""
	Build random trees with specified sizes

	Parameters
	----------
	specs : dict
		Specify the number of trees of each size
		example : {3:10, 4:20} will give 10 trees of size 3 and 20 trees of size 4

	Returns
	-------
	forest : list
		a list of Tree objects, randomly parsed and that correspond to the specs.
	"""
	trees = list()
	for size, nb in specs.items():
		for i in range(nb):
			t = DependencyTree(size)
			tree = t.toTree()
			trees += [tree]
	return trees

if __name__ == "__main__":

	## small example
	# create a random tree with 5 nodes
	t = DependencyTree(5, root=1)
	print(t)
	# visualize it
	# t.view()
	# # transform it to conll3 format
	# tree = t.toTree()
	# # write it to a file
	# conll3.trees2conllFile([tree], "sample_random-trees.conllu")

	# ## random forest
	# specs = {3:10, 4:20}
	# forest = build_random_forest(specs)
	# conll3.trees2conllFile(forest, "sample_random-forest.conllu")

reason: import networkx


text:
from lib import *
import numpy as np

def garch_process(r, theta, p=1, q=1):
    w, alpha, gamma, beta = theta[0], theta[1:1 + p], theta[1 + p:1 + p + p], theta[1 + p + p:]
    if len(gamma) is not q:
        raise Exception('Parameter Length Incorrect!')
    r = np.array(r)
    T = len(r) - 1

    def garch_update(s, r, t, alpha, beta, gamma, p=p, q=q, T=T):
        "s = [st-1,...s0], r = [rT,...,r0], t is time" \
        "alpha, beta and gamma are from above" \
        "returns new_s = [st,...,s0]"
        r_temp = r[T - t + 1:T - t + 1 + q]  # [rt-1,...,rt-q]
        s_temp = s[0:p]  # [st-1,...st-p]

        var = np.array(s_temp) ** 2
        r_squared = np.array(r_temp) ** 2
        gjr = r_squared*(np.array(r_temp)<0)
        st = np.sqrt(np.abs(np.dot(np.array(beta), var) + np.dot(np.array(alpha), r_squared)
                      + np.dot(np.array(gamma), gjr) + w))

        new_s = [st] + s

        return new_s #[sT,...,s0]

    #"Initialize values of s and m as data variance and mean"
    s_int = np.std(r)
    L = max(p, q)
    s = [s_int for i in range(0, L)]

    for t in range(L, T + 1):
        s = garch_update(s, r, t, alpha, beta, gamma)

    return s

def garch_loss(r, theta, p, q):
    s = garch_process(r, theta, p, q)

    def garch_loss_helper(r=r, s=s):
        s = np.array(s)
        loss = 0.0
        for i in range(len(r)):
            loss += np.log(s[i] ** 2) + (r[i]/s[i])**2

    #print('training loss %f' % loss)
        return loss

    return garch_loss_helper()

def garch_loss_gen(p=1, q=1):
    def loss1(r):
        def loss(theta):
            return garch_loss(r, theta, p, q)
        return loss
    return loss1


text:
#svm is supervised machine learning algorithm that can be used for both regression and classification

import pandas as pd
import numpy as np
from sklearn import svm, datasets
import matplotlib.pyplot as plt

iris = datasets.load_iris()

# taking first two features

X = iris.data[:, :2]
y = iris.target

# plot the support vector machine boundaries with original data

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
X_plot = np.c_[xx.ravel(), yy.ravel()]

# value of regularization parameter

C = 1.0

# creating SVM classifier object

Svc_classifier = svm.SVC(kernel='linear',C=C, decision_function_shape = 'ovr')
Svc_classifier.fit(X, y)
Z = Svc_classifier.predict(X_plot)
Z = Z.reshape(xx.shape)
plt.figure(figsize = (15, 5))
plt.subplot(121)
plt.contourf(xx, yy, Z, cmap = plt.cm.tab10, alpha = 0.3)
plt.scatter(X[:, 0], X[:, 1], c = y, cmap = plt.cm.Set1)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.title('SVC with linear kernel')
plt.show()

max_stars_repo_path: machine learning/svm.py

max_stars_repo_name: pedrohma/python-ai-study

max_issues_repo_path: machine learning/svm.py

max_issues_repo_name: pedrohma/python-ai-study

max_forks_repo_path: machine learning/svm.py

max_forks_repo_name: pedrohma/python-ai-study


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

import cv2
import numpy as np

# 找图 返回最近似的点
def search_img(img, template, threshold=0.9, debug=False, gray=0):
    """
    在大图中找小图
    :param img: 大图
    :param template: 小图
    :param threshold: 相似度 1为完美 -1为最差
    :param debug: 是否显示图片匹配情况，True会框选出匹配项
    :param gray: 是否灰度匹配
    :return:
    """
    if gray == 0:
        color_mode = cv2.IMREAD_COLOR
    else:
        color_mode = cv2.COLOR_BGR2GRAY

    img = cv2.cvtColor(img, color_mode)
    template = cv2.cvtColor(template, color_mode)
    result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)

    # res大于70%
    loc = np.where(result >= threshold)
    # 使用灰度图像中的坐标对原始RGB图像进行标记
    point = ()
    for pt in zip(*loc[::-1]):
        template_size = template.shape[:2]
        cv2.rectangle(img, pt, (pt[0] + template_size[1], pt[1] + + template_size[0]), (7, 249, 151), 2)
        point = pt
    if point == ():
        return None
    if debug:
        cv2.circle(img, (int(point[0]), int(point[1])), 3, (0, 0, 255), 0)
        cv2.imshow("image", img)
        cv2.waitKey(0)
    return point[0], point[1]

if __name__ == '__main__':
    scale = 1

    test_img = cv2.imread('../images/liaoyan/test.png')  # 要找的大图
    # img = cv2.resize(img, (0, 0), fx=scale, fy=scale)

    test_template = cv2.imread('../images/liaoyan/num3.png')  # 图中的小图
    # template = cv2.resize(template, (0, 0), fx=scale, fy=scale)

    point_color = (0, 0, 255)
    x, y = search_img(test_img, test_template, threshold=0.9, debug=True)
    cv2.circle(test_img, (int(x), int(y)), 3, point_color, 0)
    # cv2.imshow("image", img)
    # cv2.waitKey(0)
    if test_img is None:
        print("没找到图片")
    else:
        print("找到图片 位置:" + str(x) + " " + str(y))


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import numpy as np

import time
import os
from six.moves import cPickle
import sys

sys.path.append("imagecaptioning/")

import captioning.utils.opts as opts
import captioning.models as models
from captioning.data.dataloader import *
from captioning.data.dataloaderraw import *
import captioning.utils.eval_utils as eval_utils
import argparse
import captioning.utils.misc as utils
import captioning.modules.losses as losses
import torch

class Namespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

def evaluation(model='',cnn_model='resnet101',image_folder="", infos_path='', only_lang_eval=0, force=0,device="cpu"):
    parser = argparse.ArgumentParser()

    function_map = {"model":model, "cnn_model":cnn_model, "infos_path":infos_path, "only_lang_eval":only_lang_eval, "force":force, "device":device, "image_folder":image_folder}
    function_map = opts.add_eval_options(function_map, parser)
    function_map= opts.add_diversity_opts(function_map, parser)
    opt = Namespace(**function_map)

    # Load infos
    with open(opt.infos_path, 'rb') as f:
        infos = utils.pickle_load(f)
    # override and collect parameters
    replace = ['input_fc_dir', 'input_att_dir', 'input_box_dir', 'input_label_h5', 'input_json', 'batch_size', 'id']
    ignore = ['start_from']

    for k in vars(infos['opt']).keys():
        if k in replace:
            setattr(opt, k, getattr(opt, k) or getattr(infos['opt'], k, ''))
        elif k not in ignore:
            if not k in vars(opt):
                vars(opt).update({k: vars(infos['opt'])[k]}) # copy over options from model

    vocab = infos['vocab'] # ix -> word mapping

    pred_fn = os.path.join('eval_results/', '.saved_pred_'+ opt.id + '_' + opt.split + '.pth')
    result_fn = os.path.join('eval_results/', opt.id + '_' + opt.split + '.json')

    if only_lang_eval == 1 or (not opt.force and os.path.isfile(pred_fn)):
        # if results existed, then skip, unless force is on
        if not opt.force:
            try:
                if os.path.isfile(result_fn):
                    json.load(open(result_fn, 'r'))
                    print('already evaluated')
                    os._exit(0)
            except:
                pass

        predictions, n_predictions = torch.load(pred_fn)
        lang_stats = eval_utils.language_eval(opt.input_json, predictions, n_predictions, vars(opt), opt.split)
        os._exit(0)

    # At this point only_lang_eval if 0
    if not opt.force:
        # Check out if
        try:
            # if no pred exists, then continue
            tmp = torch.load(pred_fn)
            # if language_eval == 1, and no pred exists, then continue
            if opt.language_eval == 1:
                json.load(open(result_fn, 'r'))
            print('Result is already there')
            os._exit(0)
        except:
            pass

    # Setup the model
    opt.vocab = vocab
    model = models.setup(opt)
    del opt.vocab
    model.load_state_dict(torch.load(opt.model, map_location='cpu'))
    model.to(opt.device)
    model.eval()
    crit = losses.LanguageModelCriterion()
    # Create the Data Loader instance
    if len(opt.image_folder) == 0:
        loader = DataLoader(opt)
    else:
        loader = DataLoaderRaw({'folder_path': opt.image_folder,
                            'coco_json': opt.coco_json,
                            'batch_size': opt.batch_size,
                            'cnn_model': opt.cnn_model,
                            "device": opt.device})
    # When eval using provided pretrained model, the vocab may be different from what you have in your cocotalk.json
    # So make sure to use the vocab in infos file.
    loader.dataset.ix_to_word = infos['vocab']

    # Set sample options
    opt.dataset = opt.input_json
    loss, split_predictions, lang_stats = eval_utils.eval_split(model, crit, loader,
        vars(opt))

    return split_predictions


text:
from io import StringIO
import os
import shutil
import json

import numpy as np

import dill as pickle  # used to pickle lambdas
from typing import BinaryIO, Callable, Any

from algorithms.configuration.maps.dense_map import DenseMap

from simulator.services.debug import DebugLevel
from simulator.services.service import Service
from simulator.services.services import Services
from simulator.services.resources.smart_unpickle import load as smart_load

class Directory(Service):
    _name: str
    _parent: str

    def __init__(self, services: Services, name: str, parent: str, create: bool = False,
                 overwrite: bool = True) -> None:  # Toggle overwrite: to True or False to enable overwriting of generated maps
        super().__init__(services)

        if name:
            name += "" if name[-1] == "/" else "/"

        self._name = name
        self._parent = parent

        if create:
            if overwrite and os.path.exists(self._full_path()):
                shutil.rmtree(self._full_path())

            if not os.path.exists(self._full_path()):
                os.makedirs(self._full_path())
            else:
                raise Exception("Directory already exists")

    def name_without_trailing_slash(self):
        if self._name:
            return self._name[:-1]
        return self._name

    @staticmethod
    def _default_save(dir: 'Directory', name: str, obj: Any):
        Directory._pickle(obj, name, dir._full_path())

    @staticmethod
    def _default_load(dir: 'Directory', name: str) -> Any:
        fi = Directory._unpickle(name, dir._full_path())
        # Assuming this is a .json
        if fi is None:
            return Directory._unjson(name, dir._full_path())
        return fi

    def save(self, name: str, obj: Any, save_function: Callable[['Directory', str, Any], None] = None) -> None:
        if not save_function:
            save_function = Directory._default_save
        save_function(self, name, obj)
        self._services.debug.write("Saved [{}]".format(self._full_path() + name), DebugLevel.LOW)

    def load(self, name: str, load_function: Callable[['Directory', str], Any] = None) -> Any:
        if not load_function:
            load_function = Directory._default_load
        obj: Any = load_function(self, name)
        if (isinstance(obj, np.ndarray) and np.prod(obj.shape) > 0) or obj: # disambiguates boolean check for numpy arrays
            self._services.debug.write("Loaded [{}]".format(self._full_path() + name), DebugLevel.LOW)
        else:
            msg = "File not found [{}]. Consider deleting and re-generating".format(self._full_path() + name)
            self._services.debug.write(msg, DebugLevel.LOW)
        return obj

    def exists(self, name: str, extension: str) -> bool:
        return os.path.exists(self._full_path() + name + extension)

    @staticmethod
    def _pickle(obj: Any, file_name: str, directory: str) -> None:
        file_name = Directory._add_extension(file_name, "pickle")
        handle: BinaryIO
        with open(directory + file_name, 'wb') as handle:
            pickle.dump(obj, handle)

    @staticmethod
    def _unpickle(file_name: str, directory: str) -> Any:
        file_name = Directory._add_extension(file_name, "pickle")
        if not os.path.isfile(directory + file_name):
            return None

        return smart_load(directory + file_name)

    @staticmethod
    def _unjson(file_name: str, directory: str) -> Any:
        file_name = Directory._add_extension(file_name, "json")
        if not os.path.isfile(directory + file_name):
            return None

        dic = json.load(open(directory + file_name))

        if dic['type'] == "DenseMap" and dic['version'] <= 1:
            return DenseMap(dic['grid'])

        return None

    @staticmethod
    def _add_extension(file_name: str, extension: str) -> str:
        if len(file_name.split(".")) == 1:
            file_name += '.' + extension
        return file_name

    def _full_path(self) -> str:
        return self._parent + self._name

    def save_log(self, log: StringIO, name: str = None) -> None:
        if not name:
            name = self.name_without_trailing_slash() + "_log.txt"
        with open(self._full_path() + name, "w") as f:
            f.write(log.getvalue())

max_forks_repo_name: judicaelclair/PathBenchURO


text:
import logging
import warnings
warnings.filterwarnings("ignore")
logger = logging.getLogger('tensorflow')
logger.disabled = True

import tensorflow.compat.v1 as tf
import tensorflow.contrib as contrib
import utils
from tqdm import tqdm
import numpy as np
import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument("-const", type = float, default = 0.5)
parser.add_argument("-seed", type = int, default = 0)
parser.add_argument("-dataset", type = int, required = True, choices = [0, 1, 2, 3, 4])
parser.add_argument("-inspect", default = False, action = "store_true")
parser.add_argument("-hidden", type = int, default = 128)
parser.add_argument("-c", default = 0.1, type = float)
parser.add_argument("-xi", default = 0.1, type = float)
parser.add_argument("-lr", default = 0.0001, type = float)
parser.add_argument("-nepochs", default = 20, type = int)
args = parser.parse_args()

config = {
    'overwrite_name': 'si-h%d-lr%g-c%g-xi%g-dataset%d' % (args.hidden, args.lr, args.c, args.xi, args.dataset),
}

utils.setup_logging(args.seed, config['overwrite_name'])
print("Seed: %d" % args.seed)
session_config = utils.set_seed(args.seed, args.dataset)
n_permute_tasks, it, layer_sizes = utils.setup_dataset(args.dataset, args.inspect)

config = {
    **config,
    'c': args.c,
    'xi': args.xi,
    'lr': args.lr,
}

if args.hidden != None:
    layer_sizes = layer_sizes[:1] + [args.hidden for ln in range(len(layer_sizes)-2)] + layer_sizes[-1:]
else:
    print('hidden unset')
config['layer_sizes'] = layer_sizes
print(config)

net = utils.SINetwork(
    layer_sizes = config['layer_sizes'],
    reshape_dims = it.reshape_dims,
    seed = args.seed,
    session_config = session_config,
    it = it,
    c = config['c'],
    xi = config['xi'],
    lr = config['lr'],
)

net.setup()

for n_task in range(n_permute_tasks):

    it.switch_task(n_task)
    it.i = 0
    n_labels = len(np.unique(it.test_y))
    division = 255.0 if args.dataset in [0, 1, 2] else 1.0
    net.preprocessed_(n_task, {
        'train_x': it.train_x.astype('float32') / division,
        'test_x': it.test_x.astype('float32') / division,
        'train_y': np.eye(n_labels)[it.train_y],
        'test_y': np.eye(n_labels)[it.test_y],
    })

for n_task in range(n_permute_tasks):

    print('Training for task %d' % n_task)
    it.switch_task(n_task)
    it.i = 0
    net.oopt.set_nb_data(it.n)

    for epoch in range(args.nepochs):
        net.train_epoch(n_task, epoch)

    net.oopt.update_task_metrics(
        net.all_data[n_task]['train_x'],
        net.all_data[n_task]['train_y'],
        it.batch_size
    )
    net.oopt.update_task_vars()

    print("Training finished for task %d" % n_task)

    net.final_stats(n_task, n_permute_tasks)


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

import csv
import os
from os import listdir
from os.path import isfile, join
import networkx as nx
import networkxgmml
import re
import regexes
import sys
reload(sys)
sys.setdefaultencoding('utf8')

def TargetInteractionFinder(
        source_xgmml='./source_xgmml/',
        node_ids=None,
        node_id_list_column_index=0,
        node_type='rna',
        output_dir='.',
        cache=True,
        debug=False):

    def print_debug(message):
        if debug:
            print message

    def get_file_paths(filelike_object, file_extension):
        if hasattr(filelike_object, 'name'):
            return filelike_object.name
        else:
            source_xgmml_file_paths = []
            if os.path.isdir(filelike_object):
                source_xgmml_file_paths += [filelike_object + f for f in
                                            listdir(filelike_object)
                                            if isfile(join(filelike_object, f))
                                            and has_file_extension(
                                                f, file_extension)]
            elif os.path.isfile(filelike_object):
                source_xgmml_file_paths += [filelike_object]
            else:
                print filelike_object
                raise ValueError('File or directory input error.')

            return source_xgmml_file_paths

    def has_file_extension(filename, expected_file_extension):
        expected_index = len(filename) - len(expected_file_extension)
        actual_index = filename.find(
            expected_file_extension, expected_index, len(filename))
        return actual_index == expected_index

    def has_matching_node(current_mapping_graph, node_id):
        verified_node_id = None
        if current_mapping_graph.has_node(node_id):
            verified_node_id = node_id
        else:
            for onenode in current_mapping_graph.nodes():
                current_node = current_mapping_graph.node[onenode]
                if ((('identifiers' in current_node)
                    and (node_id in current_node['identifiers']))
                    or (('@label' in current_node)
                        and (node_id == current_node['@label']))
                        or (('label' in current_node)
                            and (node_id == current_node['label']))
                        or (('mimat id' in current_node)
                            and (node_id == current_node['mimat id']))
                        or (('name' in current_node)
                            and (node_id == current_node['name']))):
                    if (('mimat id' in current_node)
                            and (node_id == current_node['mimat id'])):
                        verified_node_id = onenode
                        break
                    elif (('identifiers' in current_node)
                            and (node_id in current_node['identifiers'])):
                        verified_node_id = onenode
                    elif not verified_node_id:
                        verified_node_id = onenode
        return verified_node_id

    results_file_path = output_dir + 'interactions.csv'
    results_file = open(results_file_path, 'w')
    results_file.write('queryid,targetid,score,pvalue,pmid,datasource\n')
    results_file.close()

    logfile_path = output_dir + 'summary.log'
    open(logfile_path, 'w').close()

    log_result = dict()
    log_result['total_result_count'] = 0
    log_result['skipped_count'] = 0
    log_result['results_by_source'] = {}

    file_extension = '.xgmml'
    source_xgmml_file_paths = []
    if hasattr(source_xgmml, '__iter__'):
        for source_xgmml_filelike_object in source_xgmml:
            source_xgmml_file_paths += get_file_paths(
                source_xgmml_filelike_object, file_extension)
    else:
        source_xgmml_file_paths += get_file_paths(source_xgmml, file_extension)

    print_debug('source_xgmml_file_paths')
    print_debug(source_xgmml_file_paths)

    versions = []

    node_id_list = []
    if os.path.isfile(node_ids):
        with open(node_ids, 'rb') as csvfile:
            node_id_list_reader = csv.reader(
                csvfile, delimiter='\t', quotechar='|')
            for row in node_id_list_reader:
                node_id_list.append(row[node_id_list_column_index])
    else:
        if hasattr(node_ids, '__iter__'):
            node_id_list += node_ids
        else:
            node_id_list.append(node_ids)

    log_result['query_count'] = len(node_id_list)

    for source_xgmml_file_path in source_xgmml_file_paths:
        source_xgmml_dirname = os.path.dirname(source_xgmml_file_path)
        source_xgmml_basename = os.path.basename(source_xgmml_file_path)

        version = source_xgmml_basename.replace('.xgmml', '')
        versions.append(version)

        cached_graph_filename = '.' + source_xgmml_basename.replace(
            '.xgmml', '.p')
        cached_graph_filepath = os.path.join(
            source_xgmml_dirname, cached_graph_filename)
        if cache and os.path.isfile(cached_graph_filepath):
            print_debug('Using cached version of source_xgmml.')
            current_mapping_graph = nx.read_gpickle(cached_graph_filepath)
        else:
            current_xgmml_file = open(source_xgmml_file_path)
            current_mapping_graph = networkxgmml.XGMMLReader(
                current_xgmml_file)
            nx.write_gpickle(current_mapping_graph, cached_graph_filepath)

        log_result[version + '_result_count'] = 0

        datasource_subgraph_node_ids = []

        id_mappings = {}

        for node_id in node_id_list:
            verified_node_id = None
            name_or_identifier = None
            if not node_id:
                log_result['skipped_count'] += 1
                if debug:
                    print '''Warning: No node_id in line. Please provide a
                    name (e.g., hsa-miR-542-3p),
                    identifier (MIMAT0003389) or
                    IRI (http://identifiers.org/mirbase.mature/MIMAT0003389)'''
            else:
                if regexes.identifiers_org.match(node_id):
                    name_or_identifier = re.sub(
                        regexes.identifiers_org, '', node_id)

                verified_node_id = has_matching_node(
                    current_mapping_graph, node_id)
                # If provided node_id is an identifiers.org IRI,
                # but it doesn't match any nodes, we can
                # try to find nodes by matching the identifier
                # alone (stripping off the namespace).
                if name_or_identifier and (not verified_node_id):
                    verified_node_id = has_matching_node(
                        current_mapping_graph, name_or_identifier)

                    if not verified_node_id:
                        print 'Warning: No node found as a match for '
                        print '\t\tnode_id "' + node_id + '"'
                        print '\t\tin datasource "' + version + '"'

                # if node is verified to exist in this graph
                if verified_node_id:
                    if (verified_node_id != node_id):
                        print_debug('adding to mappings')
                        print_debug('verified_node_id')
                        print_debug(verified_node_id)
                        id_mappings[verified_node_id] = node_id

                    if node_type == 'rna':
                        neighbor_verified_node_ids = current_mapping_graph.successors(
                            verified_node_id)
                    else:
                        neighbor_verified_node_ids = current_mapping_graph.predecessors(
                            verified_node_id)

                    current_source_log_result = log_result['results_by_source']
                    if node_id not in current_source_log_result:
                        current_source_log_result[node_id] = {}
                    current_source_log_result[node_id][version] = len(
                        neighbor_verified_node_ids)

                    log_result['total_result_count'] += len(
                        neighbor_verified_node_ids)

                    log_result[version + '_result_count'] += len(
                        neighbor_verified_node_ids)

                    item_subgraph_node_ids = neighbor_verified_node_ids
                    item_subgraph_node_ids.append(verified_node_id)
                    datasource_subgraph_node_ids += item_subgraph_node_ids

        if (len(id_mappings) > 0):
            print_debug('id_mappings')
            print_debug(id_mappings)
            tmp_subgraph = current_mapping_graph.subgraph(
                datasource_subgraph_node_ids)
            datasource_subgraph = nx.relabel_nodes(
                tmp_subgraph, id_mappings, copy=False)
            print_debug('relabeled')
        else:
            datasource_subgraph = current_mapping_graph.subgraph(
                datasource_subgraph_node_ids)

        output_graph_name = version + '-subgraph.xgmml'
        output_xgmml_file_path = output_dir + output_graph_name
        output_xgmml = open(output_xgmml_file_path, 'w')
        networkxgmml.XGMMLWriter(
            output_xgmml, datasource_subgraph, output_graph_name)

        with open(results_file_path, 'a') as csvfile:
            csvwriter = csv.writer(csvfile, delimiter=',',
                                   quotechar='|', quoting=csv.QUOTE_MINIMAL)
            for oneedge in datasource_subgraph.edges(data=True):
                print_debug('oneedge')
                print_debug(oneedge)
                edge_attributes = oneedge[2]
                if oneedge[0] in node_id_list:
                    row_value = [oneedge[0], oneedge[1]]
                else:
                    row_value = [oneedge[1], oneedge[0]]

                if 'score' in edge_attributes:
                    row_value.append(edge_attributes['score'])
                else:
                    row_value.append('')

                if 'pValue' in edge_attributes:
                    row_value.append(edge_attributes['pValue'])
                else:
                    row_value.append('')

                if 'referenceID' in edge_attributes:
                    row_value.append(edge_attributes['referenceID'])
                else:
                    row_value.append('')

                if 'datasource' in edge_attributes:
                    row_value.append(edge_attributes['datasource'])
                else:
                    row_value.append('')

                print_debug('row_value')
                print_debug(row_value)
                csvwriter.writerow(row_value)

    print_debug('log_result')
    print_debug(log_result)
    with file(logfile_path, 'w') as f:
        print >>f, '# Queried {} line(s).'.format(
            log_result['query_count'])
        print >>f, '# Skipped {} line(s).'.format(
            log_result['skipped_count'])
        print >>f, '# Found {} results.'.format(
            log_result['total_result_count'])
        header = ['queryid'] + versions
        print >>f, '\t'.join(header)
        for one_node_id in log_result['results_by_source']:
            row = [one_node_id]
            for oneversion in versions:
                logs_by_version = log_result['results_by_source'][one_node_id]
                if oneversion in logs_by_version:
                    row.append(str(logs_by_version[oneversion]))
                else:
                    row.append('0')
            print >>f, '\t'.join(row)

    return log_result

max_stars_repo_name: AlexanderPico/target-interaction-finder

max_issues_repo_name: AlexanderPico/target-interaction-finder

max_forks_repo_name: ariutta/exrna-mapper


text:
import math
import tensorflow as tf
import os
import struct
import pdb
import numpy as np
from datasets import dataset_factory
from nets import nets_factory
import nets.resnet_v2 as resnet_v2
from preprocessing import preprocessing_factory
slim = tf.contrib.slim

def merge_predictions(predictions_fn):
    '''
    Merge predictions/logit scores for products that are the same.
    '''

    out_f = open(predictions_fn + '_merged', 'wb')
    f = open(predictions_fn, 'r')
    line = f.readline().strip().split()
    curr_id = line[0]
    curr_scores = np.power(np.array([float(x) for x in line[1:]]), 3)
    num_elems = 1
    line = f.readline().strip().split()

    while line != []:
        id = line[0]
        # raise elements to the third power, and then take the cubic root
        scores = np.power(np.array([float(x) for x in line[1:]]), 3)

        if id == curr_id:
            num_elems += 1
            curr_scores += scores
        else:
            curr_scores = np.cbrt(curr_scores / float(num_elems))
            for score in curr_scores:
                out_f.write(struct.pack('>f', score))

            curr_scores = scores
            num_elems = 1
            curr_id = id

        line = f.readline().strip().split()

    curr_scores = np.cbrt(curr_scores / float(num_elems))
    for score in curr_scores:
        out_f.write(struct.pack('>f', score))

    out_f.close()
    f.close()

if __name__ == '__main__':

    checkpoint_dir = '/home/shunan/Code/Data/cdiscount/training'
    dataset_dir = '/home/shunan/Code/Data/cdiscount/tf_records'
    num_classes = 5270
    image_size = 180
    batch_size = 100
    set_name = 'validation'
    data_sizes = {'train': 12195682, 'validation': 175611, 'test': 3095080}
    out_fn = os.path.join(dataset_dir, '{}_predictions.txt'.format(set_name))

    checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)

    # loading the dataset
    dataset = dataset_factory.get_dataset('cdiscount', set_name, dataset_dir)

    # dataset provider to load data from the dataset.
    provider = slim.dataset_data_provider.DatasetDataProvider(dataset, shuffle=False, common_queue_capacity=2*batch_size,
        common_queue_min=batch_size)
    [image, label, product_id] = provider.get(['image', 'label', 'product_id'])

    # Pre-processing step.
    image_preprocessing_fn = preprocessing_factory.get_preprocessing('simple', is_training=False)
    image = image_preprocessing_fn(image, image_size, image_size)

    images, labels, product_ids = tf.train.batch([image, label, product_id], batch_size=batch_size, num_threads=1,
                                                 capacity=5 * batch_size)

    # Get the model
    # network_fn = nets_factory.get_network_fn('resnet_v2_152', num_classes=num_classes, is_training=False)
    with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=0.)):
        logits, end_points = resnet_v2.resnet_v2_152(images, num_classes=num_classes, is_training=False)

    #Obtain the trainable variables and a saver
    variables_to_restore = slim.get_variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)
    output_f = open(out_fn, 'w')

    with tf.Session() as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        sess.run(tf.global_variables_initializer())
        saver.restore(sess, checkpoint_file)
        num_iters = int(math.ceil(data_sizes[set_name] / float(batch_size)))
        num_last_batch = batch_size - ((num_iters * batch_size) - data_sizes[set_name])

        for i in range(num_iters):
            output, ids = sess.run([logits, product_ids])

            if i == num_iters - 1:
                output = output[:num_last_batch, :]
                ids = ids[:num_last_batch]

            for j in range(output.shape[0]):
                vec_str = [str(x) for x in output[j, :]]
                output_f.write(str(ids[j]) + ' ' + ' '.join(vec_str) + '\n')

        output_f.close()