import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
V, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
V.data += x

#print(V)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(V, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = (sa.count_nonzero()==0)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats
x, mu, stddev = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = stats.lognorm(s=stddev, scale=np.exp(mu)).cdf(x)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
x, shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.ndimage.zoom(x, zoom=(shape[0]/x.shape[0], shape[1]/x.shape[1]), order=1)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import stats
import random
import numpy as np
def poisson_simul(rate, T):
    time = random.expovariate(rate)
    times = [0]
    while (times[-1] < T):
        times.append(time+times[-1])
        time = random.expovariate(rate)
    return times[1:]
rate, T, times = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = stats.kstest(times, stats.uniform(loc=0, scale=T).cdf)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
sa, sb = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sa.multiply(sb)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.optimize
import numpy as np
a, x_true, y, x0, x_lower_bounds = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def residual_ans(x, a, y):
    s = ((y - a.dot(x**2))**2).sum()
    return s
bounds = [[x, None] for x in x_lower_bounds]
out = scipy.optimize.minimize(residual_ans, x0=x0, args=(a, y), method= 'L-BFGS-B', bounds=bounds).x
#print(out)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(out, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.integrate
c, low, high = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(c=5, low=0, high=1):
    result = scipy.integrate.quadrature(lambda x: 2*c*x, low, high)[0]

    return result

result = f(c, low, high)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import io
import numpy as np
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
indices = [('1415777_at Pnliprp1', 'data'), ('1415777_at Pnliprp1', 'zscore'), ('1415805_at Clps', 'data'), ('1415805_at Clps', 'zscore'), ('1415884_at Cela3b', 'data'), ('1415884_at Cela3b', 'zscore')]
indices = pd.MultiIndex.from_tuples(indices)
df2 = pd.DataFrame(data=stats.zscore(df, axis = 0), index=df.index, columns=df.columns)
df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
result = pd.DataFrame(data=np.round(df3, 3), index=indices, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
kurtosis_result = (sum((a - np.mean(a)) ** 4)/len(a)) / np.std(a)**4

#print(kurtosis_result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(kurtosis_result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy
x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.polyfit(np.log(x), y, 1)[::-1]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy as sp
from scipy import integrate,stats
def bekkers(x, a, m, d):
    p = a*np.exp((-1*(x**(1/3) - m)**2)/(2*d**2))*x**(-2/3)
    return(p)
range_start, range_end, estimated_a, estimated_m, estimated_d, sample_data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def bekkers_cdf(x,a,m,d,range_start,range_end):
    values = []
    for value in x:
        integral = integrate.quad(lambda k: bekkers(k,a,m,d),range_start,value)[0]
        normalized = integral/integrate.quad(lambda k: bekkers(k,a,m,d),range_start,range_end)[0]
        values.append(normalized)
    return np.array(values)
result = stats.kstest(sample_data, lambda x: bekkers_cdf(x,estimated_a, estimated_m, estimated_d,range_start,range_end))
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import io
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = pd.DataFrame(data=stats.zscore(df, axis = 1), index=df.index, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial.distance
example_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(example_array):
    import itertools
    n = example_array.max()+1
    indexes = []
    for k in range(1, n):
        tmp = np.nonzero(example_array == k)
        tmp = np.asarray(tmp).T
        indexes.append(tmp)
    result = np.zeros((n-1, n-1))
    for i, j in itertools.combinations(range(n-1), 2):
        d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='sqeuclidean')
        result[i, j] = result[j, i] = d2.min()**0.5

    return result

result = f(example_array)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.optimize as optimize
from math import *

initial_guess = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def g(params):
    import numpy as np
    a, b, c = params
    return ((a+b-c)-2)**2 + ((3*a-b-c))**2 + np.sin(b) + np.cos(b) + 4

res = optimize.minimize(g, initial_guess)
result = res.x
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.optimize as sciopt
import numpy as np
import pandas as pd
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
weights = (a.values / a.values.sum()).squeeze()

#print(weights)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(weights, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.optimize import fsolve
def eqn(x, a, b):
    return x + 2*a - b**2

xdata, bdata = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.array([fsolve(lambda a,x,b: eqn(x, a, b), x0=0.5, args=(x,b))[0] for x, b in zip(xdata, bdata)])
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import stats
import random
import numpy as np
def poisson_simul(rate, T):
    time = random.expovariate(rate)
    times = [0]
    while (times[-1] < T):
        times.append(time+times[-1])
        time = random.expovariate(rate)
    return times[1:]

rate, T, times = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
res= stats.kstest(times, stats.uniform(loc=0, scale=T).cdf)

if res[1] < 0.05:
    result = False
else:
    result = True

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

arr = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
M = csr_matrix(arr)
result = M.A.diagonal(0)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import stats
import random
import numpy as np
def poisson_simul(rate, T):
    time = random.expovariate(rate)
    times = [0]
    while (times[-1] < T):
        times.append(time+times[-1])
        time = random.expovariate(rate)
    return times[1:]
rate, T, times = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(times, rate, T):
    result = stats.kstest(times, stats.uniform(loc=0, scale=T).cdf)

    return result

result = f(times, rate, T)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy
import scipy.optimize
import numpy as np
def test_func(x):
    return (x[0])**2+(x[1])**2

def test_grad(x):
    return [2*x[0],2*x[1]]
starting_point, direction = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

result = scipy.optimize.line_search(test_func, test_grad, np.array(starting_point), np.array(direction))[0]
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np

import scipy.stats
z_scores, mu, sigma = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

temp = np.array(z_scores)
p_values = scipy.stats.norm.cdf(temp)

#print(p_values)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(p_values, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import lil_matrix
from scipy import sparse

M = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
rows, cols = M.nonzero()
M[cols, rows] = M[rows, cols]

#print(M)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(M, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import sparse
c1, c2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
Feature = sparse.vstack((c1, c2))

#print(Feature)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Feature, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.spatial import distance
shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
xs, ys = np.indices(shape)
xs = xs.reshape(shape[0] * shape[1], 1)
ys = ys.reshape(shape[0] * shape[1], 1)
X = np.hstack((xs, ys))
mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial.distance
example_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
import itertools
n = example_array.max()+1
indexes = []
for k in range(1, n):
    tmp = np.nonzero(example_array == k)
    tmp = np.asarray(tmp).T
    indexes.append(tmp)
result = np.zeros((n-1, n-1), dtype=float)
for i, j in itertools.combinations(range(n-1), 2):
    d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='minkowski', p=1)
    result[i, j] = result[j, i] = d2.min()

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import numpy as np
import scipy.stats as stats
df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
import itertools as IT
for col1, col2 in IT.combinations(df.columns, 2):
    def tau(idx):
        B = df[[col1, col2]].iloc[idx]
        return stats.kendalltau(B[col1], B[col2])[0]
    df[col1+col2] = pd.Series(np.arange(len(df)), index=df.index).rolling(3).apply(tau)

#print(df)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
sA, sB = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(sA, sB):
    result = sA.multiply(sB)

    return result

result = f(sA, sB)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
square = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def filter_isolated_cells(array, struct):
    filtered_array = np.copy(array)
    id_regions, num_ids = scipy.ndimage.label(filtered_array, structure=struct)
    id_sizes = np.array(scipy.ndimage.sum(array, id_regions, range(num_ids + 1)))
    area_mask = (id_sizes == 1)
    filtered_array[area_mask[id_regions]] = 0
    return filtered_array
square = filter_isolated_cells(square, struct=np.ones((3,3)))
#print(square)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(square, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import sparse
c1, c2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
Feature = sparse.hstack((c1, c2)).tocsr()

#print(Feature)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Feature, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy
x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.polyfit(np.log(x), y, 1)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

col = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
Max, Min = col.max(), col.min()
#print(Max)
#print(Min)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([Max, Min], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = (sa.count_nonzero()==0)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy.optimize import curve_fit
import numpy as np
z, Ua, tau, degree = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def fourier(x, *a):
    ret = a[0] * np.cos(np.pi / tau * x)
    for deg in range(1, len(a)):
        ret += a[deg] * np.cos((deg+1) * np.pi / tau * x)
    return ret

popt, pcov = curve_fit(fourier, z, Ua, [1.0] * degree)
#print(popt, pcov)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([popt, pcov], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.optimize
x, y, p0 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.optimize.curve_fit(lambda t,a,b, c: a*np.exp(b*t) + c,  x,  y,  p0=p0)[0]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy
import numpy as np
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
a = 1-np.sign(a)

#print(a)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.spatial
import numpy as np
centroids, data, k = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

def find_k_closest(centroids, data, k=1, distance_norm=2):
    kdtree = scipy.spatial.cKDTree(data)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k > 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values
result, _ = find_k_closest(centroids, data, k)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
b = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, 1))

#print(b)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(b, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa, sb = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.hstack((sa, sb)).tocsr()

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.stats
N, p = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
n = np.arange(N + 1, dtype=np.int64)
dist = scipy.stats.binom(p=p, n=n)
result = dist.pmf(k=np.arange(N + 1, dtype=np.int64)[:, None]).T

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.stats as ss
x1, x2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
s, c_v, s_l = ss.anderson_ksamp([x1,x2])
result = c_v[2] >= s

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial
import scipy.optimize
np.random.seed(100)

points1, N, points2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
C = scipy.spatial.distance.cdist(points1, points2)
_, result = scipy.optimize.linear_sum_assignment(C)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.optimize as sciopt
fp   = lambda p, x: p[0]*x[0]+p[1]*x[1]
e    = lambda p, x, y: ((fp(p,x)-y)**2).sum()
pmin, pmax, x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
p_guess = (pmin + pmax)/2
bounds = np.c_[pmin, pmax]
fp   = lambda p, x: p[0]*x[0]+p[1]*x[1]
e    = lambda p, x, y: ((fp(p,x)-y)**2).sum()
sol = sciopt.minimize(e, p_guess, bounds=bounds, args=(x,y))
result = sol.x

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import signal
arr, n = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
res = signal.argrelextrema(arr, np.less_equal, order=n, axis = 1)
result = np.zeros((res[0].shape[0], 2)).astype(int)
result[:, 0] = res[0]
result[:, 1] = res[1]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.spatial
centroids, data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def find_k_closest(centroids, data, k=1, distance_norm=2):
    kdtree = scipy.spatial.cKDTree(data)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k > 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values
_, result = find_k_closest(centroids, data)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.optimize
import numpy as np

a, y, x0 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def residual_ans(x, a, y):
    s = ((y - a.dot(x**2))**2).sum()
    return s
out = scipy.optimize.minimize(residual_ans, x0=x0, args=(a, y), method= 'L-BFGS-B').x
#print(out)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(out, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.spatial
points, extraPoints = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vor = scipy.spatial.Voronoi(points)
kdtree = scipy.spatial.cKDTree(points)
_, index = kdtree.query(extraPoints)
result = vor.point_region[index]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.spatial
import numpy as np

centroids, data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

def find_k_closest(centroids, data, k=1, distance_norm=2):
    kdtree = scipy.spatial.cKDTree(data)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k > 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values
result, _ = find_k_closest(centroids, data)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.interpolate
s, t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
x, y = np.ogrid[-1:1:10j,-2:0:10j]
z = (x + y)*np.exp(-6.0 * (x * x + y * y))
spl = scipy.interpolate.RectBivariateSpline(x, y, z)
result = spl(s, t, grid=False)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.spatial
points, extraPoints = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vor = scipy.spatial.Voronoi(points)
kdtree = scipy.spatial.cKDTree(points)
_, result = kdtree.query(extraPoints)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import numpy as np
N0, time_span = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def dN1_dt (t, N1):
    return -100 * N1 + np.sin(t)
sol = scipy.integrate.solve_ivp(fun=dN1_dt, t_span=time_span, y0=[N0,])
result = sol.y
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.interpolate
x, array, x_new = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
new_array = scipy.interpolate.interp1d(x, array, axis=0)(x_new)

#print(new_array)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_array, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy
import numpy as np
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
a = np.sign(a)

#print(a)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.interpolate

points, V, request = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.interpolate.griddata(points, V, request).tolist()
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np

import scipy.stats
z_scores = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
temp = np.array(z_scores)
p_values = scipy.stats.norm.cdf(temp)

#print(p_values)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(p_values, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import numpy as np

N0, time_span = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def dN1_dt (t, N1):
    return -100 * N1 + np.sin(t)
sol = scipy.integrate.solve_ivp(fun=dN1_dt, t_span=time_span, y0=[N0,])

result = sol.y
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.sparse as sparse

vectors, max_vector_size = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.lil_matrix((len(vectors), max_vector_size))
for i, v in enumerate(vectors):
    result[i, :v.size] = v

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.spatial import distance
shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(shape = (6, 6)):
    xs, ys = np.indices(shape)
    xs = xs.reshape(shape[0] * shape[1], 1)
    ys = ys.reshape(shape[0] * shape[1], 1)
    X = np.hstack((xs, ys))
    mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
    result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)

    return result

result = f(shape)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.spdiags(matrix, (1, 0, -1), 5, 5).T.A

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy.optimize import minimize

def function(x):
    return -1*(18*x[0]+16*x[1]+12*x[2]+11*x[3])

I = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
x0=I

cons=[]
steadystate={'type':'eq', 'fun': lambda x: x.sum()-I.sum() }
cons.append(steadystate)
def f(a):
    def g(x):
        return x[a]
    return g
for t in range (4):
    cons.append({'type':'ineq', 'fun': f(t)})

out=minimize(function, x0, method="SLSQP", constraints=cons)
x=out["x"]

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(x, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import io
from scipy import integrate
df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
df.Time = pd.to_datetime(df.Time, format='%Y-%m-%d-%H:%M:%S')
df = df.set_index('Time')
integral_df = df.rolling('25S').apply(integrate.trapz)

#print(integral_df)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(integral_df, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats

pre_course_scores, during_course_scores = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
p_value = stats.ranksums(pre_course_scores, during_course_scores).pvalue

#print(p_value)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(p_value, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
V, x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
V = V.copy()
V.data += x
V.eliminate_zeros()
V.data += y
V.eliminate_zeros()

#print(V)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(V, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import lil_matrix
def f(sA):
    rows, cols = sA.nonzero()
    sA[cols, rows] = sA[rows, cols]

    return sA
sA = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

result = f(sA)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats

def f(pre_course_scores, during_course_scores):
    p_value = stats.ranksums(pre_course_scores, during_course_scores).pvalue

    return p_value

pre_course_scores, during_course_scores = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = f(pre_course_scores, during_course_scores)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa, sb = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.vstack((sa, sb)).tocsr()

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
square = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def filter_isolated_cells(array, struct):
    filtered_array = np.copy(array)
    id_regions, num_ids = scipy.ndimage.label(filtered_array, structure=struct)
    id_sizes = np.array(scipy.ndimage.sum(array, id_regions, range(num_ids + 1)))
    area_mask = (id_sizes == 1)
    filtered_array[area_mask[id_regions]] = 0
    return filtered_array
arr = np.sign(square)
filtered_array = filter_isolated_cells(arr, struct=np.ones((3,3)))
square = np.where(filtered_array==1, square, 0)
#print(square)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(square, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.stats as ss
x1, x2, x3, x4 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
statistic, critical_values, significance_level = ss.anderson_ksamp([x1,x2,x3,x4])

#print(statistic, critical_values, significance_level)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([statistic, critical_values, significance_level], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import io
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = pd.DataFrame(data=stats.zscore(df, axis = 0), index=df.index, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import math
import numpy as np
x, u, o2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
norm = (x-u)/o2
prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]
#print(prob)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(prob, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.interpolate
s, t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(s, t):
    x, y = np.ogrid[-1:1:10j,-2:0:10j]
    z = (x + y)*np.exp(-6.0 * (x * x + y * y))
    spl = scipy.interpolate.RectBivariateSpline(x, y, z)
    result = spl(s, t, grid=False)

    return result

result = f(s, t)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.stats
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
kurtosis_result = scipy.stats.kurtosis(a)

#print(kurtosis_result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(kurtosis_result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy as sp
from scipy import integrate,stats
def bekkers(x, a, m, d):
    p = a*np.exp((-1*(x**(1/3) - m)**2)/(2*d**2))*x**(-2/3)
    return(p)
range_start, range_end, estimated_a, estimated_m, estimated_d, sample_data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def bekkers_cdf(x,a,m,d,range_start,range_end):
    values = []
    for value in x:
        integral = integrate.quad(lambda k: bekkers(k,a,m,d),range_start,value)[0]
        normalized = integral/integrate.quad(lambda k: bekkers(k,a,m,d),range_start,range_end)[0]
        values.append(normalized)
    return np.array(values)

s, p_value = stats.kstest(sample_data, lambda x: bekkers_cdf(x, estimated_a, estimated_m, estimated_d, range_start,range_end))

if p_value >= 0.05:
    result = False
else:
    result = True
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import ndimage

def f(img):
    threshold = 0.75
    blobs = img > threshold
    labels, result = ndimage.label(blobs)

    return result
img = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = f(img)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import signal
arr, n = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = signal.argrelextrema(arr, np.less_equal, order=n)[0]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

col = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
n = col.shape[0]
val = col.data
for i in range(n-len(val)):
    val = np.append(val,0)
Median, Mode = np.median(val), np.argmax(np.bincount(val))

#print(Median)
#print(Mode)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([Median, Mode], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.spatial import distance
shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
xs, ys = np.indices(shape)
xs = xs.reshape(shape[0] * shape[1], 1)
ys = ys.reshape(shape[0] * shape[1], 1)
X = np.hstack((xs, ys))
mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
result = distance.cdist(X, np.atleast_2d([mid_x, mid_y]), 'minkowski', p=1).reshape(shape)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import sparse
V, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
V._update(zip(V.keys(), np.array(list(V.values())) + x))
#print(V)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(V, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial
import scipy.optimize

points1, N, points2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
C = scipy.spatial.distance.cdist(points1, points2, metric='minkowski', p=1)
_, result = scipy.optimize.linear_sum_assignment(C)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.optimize import fsolve
def eqn(x, a, b):
    return x + 2*a - b**2

xdata, adata = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
A = np.array([fsolve(lambda b,x,a: eqn(x, a, b), x0=0, args=(x,a))[0] for x, a in zip(xdata, adata)])
temp = -A
result = np.zeros((len(A), 2))
result[:, 0] = A
result[:, 1] = temp
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import sparse
import numpy as np
import math
sa = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
sa = sparse.csr_matrix(sa.toarray() / np.sqrt(np.sum(sa.toarray()**2, axis=0)))

#print(sa)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(sa, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats
mu, stddev = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
expected_value = np.exp(mu + stddev ** 2 / 2)
median = np.exp(mu)

#print(expected_value, median)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([expected_value, median], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import ndimage

img = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
threshold = 0.75
blobs = img < threshold
labels, result = ndimage.label(blobs)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import ndimage

img = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
threshold = 0.75
blobs = img > threshold
labels, nlabels = ndimage.label(blobs)
r, c = np.vstack(ndimage.center_of_mass(img, labels, np.arange(nlabels) + 1)).T
# find their distances from the top-left corner
d = np.sqrt(r * r + c * c)
result = sorted(d)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.interpolate

points, V, request = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.interpolate.griddata(points, V, request)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import stats
import numpy as np

x, y, alpha = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
s, p = stats.ks_2samp(x, y)
result = (p <= alpha)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
b = sparse.csr_matrix(a)
b.setdiag(0)
b.eliminate_zeros()

#print(b)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(b, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import misc
from scipy.ndimage import rotate
import numpy as np
data_orig, x0, y0, angle = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def rot_ans(image, xy, angle):
    im_rot = rotate(image,angle)
    org_center = (np.array(image.shape[:2][::-1])-1)/2.
    rot_center = (np.array(im_rot.shape[:2][::-1])-1)/2.
    org = xy-org_center
    a = np.deg2rad(angle)
    new = np.array([org[0]*np.cos(a) + org[1]*np.sin(a),
            -org[0]*np.sin(a) + org[1]*np.cos(a) ])
    return im_rot, new+rot_center
data_rot, (xrot, yrot) =rot_ans(data_orig, np.array([x0, y0]), angle)

#print(data_rot, (xrot, yrot))

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([data_rot, (xrot, yrot)], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import stats
import numpy as np
np.random.seed(42)
x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
statistic, p_value = stats.ks_2samp(x, y)

#print(statistic, p_value)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([statistic, p_value], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import io
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
indices = [('1415777_at Pnliprp1', 'data'), ('1415777_at Pnliprp1', 'zscore'), ('1415805_at Clps', 'data'), ('1415805_at Clps', 'zscore'), ('1415884_at Cela3b', 'data'), ('1415884_at Cela3b', 'zscore')]
indices = pd.MultiIndex.from_tuples(indices)
df2 = pd.DataFrame(data=stats.zscore(df, axis = 1), index=df.index, columns=df.columns)
df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
result = pd.DataFrame(data=df3, index=indices, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.interpolate
x, y, eval = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.interpolate.griddata(x, y, eval)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import stats
import pandas as pd
import numpy as np
LETTERS = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
df = df[(np.abs(stats.zscore(df.select_dtypes(exclude='object'))) < 3).all(axis=1)]

#print(df)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import math
import numpy as np
x, u, o2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
def f(x, u, o2):
    norm = (x-u)/o2
    prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]
    return prob

result = f(x, u, o2)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial.distance
example_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
import itertools
n = example_array.max()+1
indexes = []
for k in range(1, n):
    tmp = np.nonzero(example_array == k)
    tmp = np.asarray(tmp).T
    indexes.append(tmp)
result = np.zeros((n-1, n-1))
for i, j in itertools.combinations(range(n-1), 2):
    d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='sqeuclidean')
    result[i, j] = result[j, i] = d2.min()**0.5

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import interpolate
import numpy as np
x, y, x_val = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.zeros((5, 100))
for i in range(5):
    extrapolator = interpolate.UnivariateSpline(x[:, i], y[:, i], k = 2, s = 4)
    y_int = extrapolator(x_val)
    result[i, :] = y_int

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.integrate
c, low, high = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.integrate.quadrature(lambda x: 2*c*x, low, high)[0]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

col = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
mean = col.mean()
N = col.shape[0]
sqr = col.copy()  # take a copy of the col
sqr.data **= 2  # square the data, i.e. just the non-zero data
standard_deviation = np.sqrt(sqr.sum() / N - col.mean() ** 2)

#print(mean)
#print(standard_deviation)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([mean, standard_deviation], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import numpy as np
N0, time_span = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def dN1_dt(t, N1):
    input = 1-np.cos(t) if 0<t<2*np.pi else 0
    return -100*N1 + input
sol = scipy.integrate.solve_ivp(fun=dN1_dt, t_span=time_span, y0=[N0,])
result = sol.y
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np

import scipy.stats
p_values = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
z_scores = scipy.stats.norm.ppf(p_values)

#print(z_scores)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(z_scores, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.fft as sf
N = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sf.dct(np.eye(N), axis=0, norm= 'ortho')

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

M, row, column = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.array(M[row,column]).squeeze()
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import ndimage

img = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
threshold = 0.75
blobs = img > threshold
labels, result = ndimage.label(blobs)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

M, row, column = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = M[row,column]
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import sparse
import numpy as np
import math
sa = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
sa = sparse.csc_matrix(sa.toarray() / np.sqrt(np.sum(sa.toarray()**2, axis=0)))

#print(sa)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(sa, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy.linalg import block_diag
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = block_diag(*a)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
V, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
V.data += x

#print(V)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(V, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = (sa.count_nonzero()==0)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats
x, mu, stddev = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = stats.lognorm(s=stddev, scale=np.exp(mu)).cdf(x)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
x, shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.ndimage.zoom(x, zoom=(shape[0]/x.shape[0], shape[1]/x.shape[1]), order=1)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import stats
import random
import numpy as np
def poisson_simul(rate, T):
    time = random.expovariate(rate)
    times = [0]
    while (times[-1] < T):
        times.append(time+times[-1])
        time = random.expovariate(rate)
    return times[1:]
rate, T, times = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = stats.kstest(times, stats.uniform(loc=0, scale=T).cdf)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
sa, sb = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sa.multiply(sb)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.optimize
import numpy as np
a, x_true, y, x0, x_lower_bounds = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def residual_ans(x, a, y):
    s = ((y - a.dot(x**2))**2).sum()
    return s
bounds = [[x, None] for x in x_lower_bounds]
out = scipy.optimize.minimize(residual_ans, x0=x0, args=(a, y), method= 'L-BFGS-B', bounds=bounds).x
#print(out)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(out, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.integrate
c, low, high = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(c=5, low=0, high=1):
    result = scipy.integrate.quadrature(lambda x: 2*c*x, low, high)[0]

    return result

result = f(c, low, high)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import io
import numpy as np
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
indices = [('1415777_at Pnliprp1', 'data'), ('1415777_at Pnliprp1', 'zscore'), ('1415805_at Clps', 'data'), ('1415805_at Clps', 'zscore'), ('1415884_at Cela3b', 'data'), ('1415884_at Cela3b', 'zscore')]
indices = pd.MultiIndex.from_tuples(indices)
df2 = pd.DataFrame(data=stats.zscore(df, axis = 0), index=df.index, columns=df.columns)
df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
result = pd.DataFrame(data=np.round(df3, 3), index=indices, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
kurtosis_result = (sum((a - np.mean(a)) ** 4)/len(a)) / np.std(a)**4

#print(kurtosis_result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(kurtosis_result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy
x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.polyfit(np.log(x), y, 1)[::-1]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy as sp
from scipy import integrate,stats
def bekkers(x, a, m, d):
    p = a*np.exp((-1*(x**(1/3) - m)**2)/(2*d**2))*x**(-2/3)
    return(p)
range_start, range_end, estimated_a, estimated_m, estimated_d, sample_data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def bekkers_cdf(x,a,m,d,range_start,range_end):
    values = []
    for value in x:
        integral = integrate.quad(lambda k: bekkers(k,a,m,d),range_start,value)[0]
        normalized = integral/integrate.quad(lambda k: bekkers(k,a,m,d),range_start,range_end)[0]
        values.append(normalized)
    return np.array(values)
result = stats.kstest(sample_data, lambda x: bekkers_cdf(x,estimated_a, estimated_m, estimated_d,range_start,range_end))
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import io
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = pd.DataFrame(data=stats.zscore(df, axis = 1), index=df.index, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial.distance
example_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(example_array):
    import itertools
    n = example_array.max()+1
    indexes = []
    for k in range(1, n):
        tmp = np.nonzero(example_array == k)
        tmp = np.asarray(tmp).T
        indexes.append(tmp)
    result = np.zeros((n-1, n-1))
    for i, j in itertools.combinations(range(n-1), 2):
        d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='sqeuclidean')
        result[i, j] = result[j, i] = d2.min()**0.5

    return result

result = f(example_array)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.optimize as optimize
from math import *

initial_guess = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def g(params):
    import numpy as np
    a, b, c = params
    return ((a+b-c)-2)**2 + ((3*a-b-c))**2 + np.sin(b) + np.cos(b) + 4

res = optimize.minimize(g, initial_guess)
result = res.x
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.optimize as sciopt
import numpy as np
import pandas as pd
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
weights = (a.values / a.values.sum()).squeeze()

#print(weights)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(weights, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.optimize import fsolve
def eqn(x, a, b):
    return x + 2*a - b**2

xdata, bdata = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.array([fsolve(lambda a,x,b: eqn(x, a, b), x0=0.5, args=(x,b))[0] for x, b in zip(xdata, bdata)])
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import stats
import random
import numpy as np
def poisson_simul(rate, T):
    time = random.expovariate(rate)
    times = [0]
    while (times[-1] < T):
        times.append(time+times[-1])
        time = random.expovariate(rate)
    return times[1:]

rate, T, times = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
res= stats.kstest(times, stats.uniform(loc=0, scale=T).cdf)

if res[1] < 0.05:
    result = False
else:
    result = True

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

arr = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
M = csr_matrix(arr)
result = M.A.diagonal(0)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import stats
import random
import numpy as np
def poisson_simul(rate, T):
    time = random.expovariate(rate)
    times = [0]
    while (times[-1] < T):
        times.append(time+times[-1])
        time = random.expovariate(rate)
    return times[1:]
rate, T, times = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(times, rate, T):
    result = stats.kstest(times, stats.uniform(loc=0, scale=T).cdf)

    return result

result = f(times, rate, T)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy
import scipy.optimize
import numpy as np
def test_func(x):
    return (x[0])**2+(x[1])**2

def test_grad(x):
    return [2*x[0],2*x[1]]
starting_point, direction = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

result = scipy.optimize.line_search(test_func, test_grad, np.array(starting_point), np.array(direction))[0]
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np

import scipy.stats
z_scores, mu, sigma = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

temp = np.array(z_scores)
p_values = scipy.stats.norm.cdf(temp)

#print(p_values)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(p_values, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import lil_matrix
from scipy import sparse

M = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
rows, cols = M.nonzero()
M[cols, rows] = M[rows, cols]

#print(M)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(M, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import sparse
c1, c2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
Feature = sparse.vstack((c1, c2))

#print(Feature)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Feature, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.spatial import distance
shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
xs, ys = np.indices(shape)
xs = xs.reshape(shape[0] * shape[1], 1)
ys = ys.reshape(shape[0] * shape[1], 1)
X = np.hstack((xs, ys))
mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial.distance
example_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
import itertools
n = example_array.max()+1
indexes = []
for k in range(1, n):
    tmp = np.nonzero(example_array == k)
    tmp = np.asarray(tmp).T
    indexes.append(tmp)
result = np.zeros((n-1, n-1), dtype=float)
for i, j in itertools.combinations(range(n-1), 2):
    d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='minkowski', p=1)
    result[i, j] = result[j, i] = d2.min()

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import pandas as pd
import numpy as np
import scipy.stats as stats
df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
import itertools as IT
for col1, col2 in IT.combinations(df.columns, 2):
    def tau(idx):
        B = df[[col1, col2]].iloc[idx]
        return stats.kendalltau(B[col1], B[col2])[0]
    df[col1+col2] = pd.Series(np.arange(len(df)), index=df.index).rolling(3).apply(tau)

#print(df)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
sA, sB = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(sA, sB):
    result = sA.multiply(sB)

    return result

result = f(sA, sB)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
square = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def filter_isolated_cells(array, struct):
    filtered_array = np.copy(array)
    id_regions, num_ids = scipy.ndimage.label(filtered_array, structure=struct)
    id_sizes = np.array(scipy.ndimage.sum(array, id_regions, range(num_ids + 1)))
    area_mask = (id_sizes == 1)
    filtered_array[area_mask[id_regions]] = 0
    return filtered_array
square = filter_isolated_cells(square, struct=np.ones((3,3)))
#print(square)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(square, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
from scipy import sparse
c1, c2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
Feature = sparse.hstack((c1, c2)).tocsr()

#print(Feature)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Feature, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy
x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = np.polyfit(np.log(x), y, 1)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

col = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
Max, Min = col.max(), col.min()
#print(Max)
#print(Min)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([Max, Min], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = (sa.count_nonzero()==0)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy.optimize import curve_fit
import numpy as np
z, Ua, tau, degree = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def fourier(x, *a):
    ret = a[0] * np.cos(np.pi / tau * x)
    for deg in range(1, len(a)):
        ret += a[deg] * np.cos((deg+1) * np.pi / tau * x)
    return ret

popt, pcov = curve_fit(fourier, z, Ua, [1.0] * degree)
#print(popt, pcov)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([popt, pcov], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.optimize
x, y, p0 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.optimize.curve_fit(lambda t,a,b, c: a*np.exp(b*t) + c,  x,  y,  p0=p0)[0]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy
import numpy as np
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
a = 1-np.sign(a)

#print(a)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.spatial
import numpy as np
centroids, data, k = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

def find_k_closest(centroids, data, k=1, distance_norm=2):
    kdtree = scipy.spatial.cKDTree(data)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k > 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values
result, _ = find_k_closest(centroids, data, k)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
b = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, 1))

#print(b)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(b, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa, sb = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.hstack((sa, sb)).tocsr()

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.stats
N, p = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
n = np.arange(N + 1, dtype=np.int64)
dist = scipy.stats.binom(p=p, n=n)
result = dist.pmf(k=np.arange(N + 1, dtype=np.int64)[:, None]).T

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.stats as ss
x1, x2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
s, c_v, s_l = ss.anderson_ksamp([x1,x2])
result = c_v[2] >= s

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial
import scipy.optimize
np.random.seed(100)

points1, N, points2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
C = scipy.spatial.distance.cdist(points1, points2)
_, result = scipy.optimize.linear_sum_assignment(C)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.optimize as sciopt
fp   = lambda p, x: p[0]*x[0]+p[1]*x[1]
e    = lambda p, x, y: ((fp(p,x)-y)**2).sum()
pmin, pmax, x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
p_guess = (pmin + pmax)/2
bounds = np.c_[pmin, pmax]
fp   = lambda p, x: p[0]*x[0]+p[1]*x[1]
e    = lambda p, x, y: ((fp(p,x)-y)**2).sum()
sol = sciopt.minimize(e, p_guess, bounds=bounds, args=(x,y))
result = sol.x

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import signal
arr, n = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
res = signal.argrelextrema(arr, np.less_equal, order=n, axis = 1)
result = np.zeros((res[0].shape[0], 2)).astype(int)
result[:, 0] = res[0]
result[:, 1] = res[1]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.spatial
centroids, data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def find_k_closest(centroids, data, k=1, distance_norm=2):
    kdtree = scipy.spatial.cKDTree(data)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k > 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values
_, result = find_k_closest(centroids, data)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.optimize
import numpy as np

a, y, x0 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def residual_ans(x, a, y):
    s = ((y - a.dot(x**2))**2).sum()
    return s
out = scipy.optimize.minimize(residual_ans, x0=x0, args=(a, y), method= 'L-BFGS-B').x
#print(out)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(out, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.spatial
points, extraPoints = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vor = scipy.spatial.Voronoi(points)
kdtree = scipy.spatial.cKDTree(points)
_, index = kdtree.query(extraPoints)
result = vor.point_region[index]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy.spatial
import numpy as np

centroids, data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

def find_k_closest(centroids, data, k=1, distance_norm=2):
    kdtree = scipy.spatial.cKDTree(data)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k > 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values
result, _ = find_k_closest(centroids, data)
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.interpolate
s, t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
x, y = np.ogrid[-1:1:10j,-2:0:10j]
z = (x + y)*np.exp(-6.0 * (x * x + y * y))
spl = scipy.interpolate.RectBivariateSpline(x, y, z)
result = spl(s, t, grid=False)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.spatial
points, extraPoints = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vor = scipy.spatial.Voronoi(points)
kdtree = scipy.spatial.cKDTree(points)
_, result = kdtree.query(extraPoints)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import numpy as np
N0, time_span = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def dN1_dt (t, N1):
    return -100 * N1 + np.sin(t)
sol = scipy.integrate.solve_ivp(fun=dN1_dt, t_span=time_span, y0=[N0,])
result = sol.y
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.interpolate
x, array, x_new = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
new_array = scipy.interpolate.interp1d(x, array, axis=0)(x_new)

#print(new_array)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_array, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import scipy
import numpy as np
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
a = np.sign(a)

#print(a)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.interpolate

points, V, request = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = scipy.interpolate.griddata(points, V, request).tolist()
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np

import scipy.stats
z_scores = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
temp = np.array(z_scores)
p_values = scipy.stats.norm.cdf(temp)

#print(p_values)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(p_values, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import numpy as np

N0, time_span = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def dN1_dt (t, N1):
    return -100 * N1 + np.sin(t)
sol = scipy.integrate.solve_ivp(fun=dN1_dt, t_span=time_span, y0=[N0,])

result = sol.y
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.sparse as sparse

vectors, max_vector_size = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.lil_matrix((len(vectors), max_vector_size))
for i, v in enumerate(vectors):
    result[i, :v.size] = v

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.spatial import distance
shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(shape = (6, 6)):
    xs, ys = np.indices(shape)
    xs = xs.reshape(shape[0] * shape[1], 1)
    ys = ys.reshape(shape[0] * shape[1], 1)
    X = np.hstack((xs, ys))
    mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
    result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)

    return result

result = f(shape)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
import numpy as np
matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.spdiags(matrix, (1, 0, -1), 5, 5).T.A

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy.optimize import minimize

def function(x):
    return -1*(18*x[0]+16*x[1]+12*x[2]+11*x[3])

I = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
x0=I

cons=[]
steadystate={'type':'eq', 'fun': lambda x: x.sum()-I.sum() }
cons.append(steadystate)
def f(a):
    def g(x):
        return x[a]
    return g
for t in range (4):
    cons.append({'type':'ineq', 'fun': f(t)})

out=minimize(function, x0, method="SLSQP", constraints=cons)
x=out["x"]

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(x, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import io
from scipy import integrate
df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
df.Time = pd.to_datetime(df.Time, format='%Y-%m-%d-%H:%M:%S')
df = df.set_index('Time')
integral_df = df.rolling('25S').apply(integrate.trapz)

#print(integral_df)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(integral_df, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats

pre_course_scores, during_course_scores = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
p_value = stats.ranksums(pre_course_scores, during_course_scores).pvalue

#print(p_value)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(p_value, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
V, x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
V = V.copy()
V.data += x
V.eliminate_zeros()
V.data += y
V.eliminate_zeros()

#print(V)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(V, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import lil_matrix
def f(sA):
    rows, cols = sA.nonzero()
    sA[cols, rows] = sA[rows, cols]

    return sA

sA = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

result = f(sA)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import stats

def f(pre_course_scores, during_course_scores):
    p_value = stats.ranksums(pre_course_scores, during_course_scores).pvalue

    return p_value

pre_course_scores, during_course_scores = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = f(pre_course_scores, during_course_scores)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

from scipy import sparse
sa, sb = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = sparse.vstack((sa, sb)).tocsr()

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.ndimage
square = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def filter_isolated_cells(array, struct):
    filtered_array = np.copy(array)
    id_regions, num_ids = scipy.ndimage.label(filtered_array, structure=struct)
    id_sizes = np.array(scipy.ndimage.sum(array, id_regions, range(num_ids + 1)))
    area_mask = (id_sizes == 1)
    filtered_array[area_mask[id_regions]] = 0
    return filtered_array
arr = np.sign(square)
filtered_array = filter_isolated_cells(arr, struct=np.ones((3,3)))
square = np.where(filtered_array==1, square, 0)
#print(square)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(square, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.stats as ss
x1, x2, x3, x4 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
statistic, critical_values, significance_level = ss.anderson_ksamp([x1,x2,x3,x4])

#print(statistic, critical_values, significance_level)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([statistic, critical_values, significance_level], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import io
from scipy import stats

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = pd.DataFrame(data=stats.zscore(df, axis = 0), index=df.index, columns=df.columns)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import scipy.integrate
import math
import numpy as np
x, u, o2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
norm = (x-u)/o2
prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]
#print(prob)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(prob, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.interpolate
s, t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def f(s, t):
    x, y = np.ogrid[-1:1:10j,-2:0:10j]
    z = (x + y)*np.exp(-6.0 * (x * x + y * y))
    spl = scipy.interpolate.RectBivariateSpline(x, y, z)
    result = spl(s, t, grid=False)

    return result

result = f(s, t)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy.stats
a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
kurtosis_result = scipy.stats.kurtosis(a)

#print(kurtosis_result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(kurtosis_result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
import scipy as sp
from scipy import integrate,stats
def bekkers(x, a, m, d):
    p = a*np.exp((-1*(x**(1/3) - m)**2)/(2*d**2))*x**(-2/3)
    return(p)
range_start, range_end, estimated_a, estimated_m, estimated_d, sample_data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def bekkers_cdf(x,a,m,d,range_start,range_end):
    values = []
    for value in x:
        integral = integrate.quad(lambda k: bekkers(k,a,m,d),range_start,value)[0]
        normalized = integral/integrate.quad(lambda k: bekkers(k,a,m,d),range_start,range_end)[0]
        values.append(normalized)
    return np.array(values)

s, p_value = stats.kstest(sample_data, lambda x: bekkers_cdf(x, estimated_a, estimated_m, estimated_d, range_start,range_end))

if p_value >= 0.05:
    result = False
else:
    result = True
#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import ndimage

def f(img):
    threshold = 0.75
    blobs = img > threshold
    labels, result = ndimage.label(blobs)

    return result

img = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = f(img)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as file:
    pickle.dump(result, file)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
from scipy import signal
arr, n = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = signal.argrelextrema(arr, np.less_equal, order=n)[0]

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.sparse import csr_matrix

col = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
n = col.shape[0]
val = col.data
for i in range(n-len(val)):
    val = np.append(val,0)
Median, Mode = np.median(val), np.argmax(np.bincount(val))

#print(Median)
#print(Mode)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump([Median, Mode], f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy.spatial import distance
shape = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
xs, ys = np.indices(shape)
xs = xs.reshape(shape[0] * shape[1], 1)
ys = ys.reshape(shape[0] * shape[1], 1)
X = np.hstack((xs, ys))
mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
result = distance.cdist(X, np.atleast_2d([mid_x, mid_y]), 'minkowski', p=1).reshape(shape)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()

import numpy as np
from scipy import sparse
V, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
V._update(zip(V.keys(), np.array(list(V.values())) + x))
#print(V)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(V, f)


import pickle
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import numpy as np
import scipy.spatial
import scipy.optimize

points1, N, points2 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
C = scipy.spatial.distance.cdist(points1, points2, metric='minkowski', p=1)
_, result = scipy.optimize.linear_sum_assignment(C)

#print(result)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)