from django.shortcuts import render
from django.shortcuts import redirect
from django.http import HttpResponse
import pandas as pd
import plotly.graph_objs as go
import plotly as plotly1
from .models import Image
from .models import ProbedataSatellite,FnirsDeoxySatellite,FnirsOxySatellite
# Create your views here.
def dataprocess(request):
    if not request.session.get('is_login', None):
        return redirect("/login/")
    if request.method == "POST":
        experiment_id=  request.POST.get('Experimentid')
        if experiment_id=='1':
            return redirect('/TEST/experiment1/')
        else:
            return redirect('/TEST/experiment2/')
    return render(request, 'TEST/dataprocessing.html')

def experiment2(request):
    if not request.session.get('is_login', None):
        return redirect("/login/")
    if request.method == "POST":
        j=[]
        for i in range(11,54):
            j.append(str(i))
        patient_id=  request.POST.get('patient_id')
        session_id=int(request.POST.get('sessionid'))
        if(patient_id not in j):
            return HttpResponse("This patient ID does not exist in Experiment 2")
        querySet=Image.objects.filter(patientid=patient_id,sessionid=session_id).values()
        df = pd.DataFrame(querySet)
        columns=df['datatype']
        return render(request, 'TEST/fNIRsdatatype.html',  {'columns': columns,'patient_id':patient_id,'sessionid':session_id})
    return render(request,'TEST/experiment2.html')

def experiment1(request):
    if not request.session.get('is_login', None):
        return redirect("/login/")
    if request.method == "POST":
        j=[]
        for i in range(1,11):
            j.append(str(i))
        patient_id= request.POST.get('patient_id')
        datatype=request.POST.get('datatype')
        probetype=request.POST.get('probetype')
        if(patient_id not in j):
            return HttpResponse("This patient ID does not exist in Experiment 1 ")
        else:
            if(probetype=='MES'):
                querySet = ProbedataSatellite.objects.filter(patient_id=patient_id,stimuli_type=datatype).values()
            else:
                if(probetype=='oxy'):
                    querySet = FnirsOxySatellite.objects.filter(patient_id=patient_id, fnirs_type=datatype).values()
                else:
                    querySet = FnirsDeoxySatellite.objects.filter(patient_id=patient_id, fnirs_type=datatype).values()
            df = pd.DataFrame(list(querySet))
            length=df.shape[0]
            columns=df.columns.tolist ()
            column123=[]
            for i in columns:
                if 'ch' in i:
                    column123.append(i)
            columns=column123
            return render(request, 'TEST/probedataex.html',  {'columns': columns,'patient_id':patient_id,'datatype':datatype,'probetype':probetype,'length':length})
    return render(request,'TEST/experiment1.html')

def probedata(request):
    if not request.session.get('is_login', None):
        return redirect("/login/")
    return render(request, 'TEST/experiment2.html')

def fNIRs(request):
    if not request.session.get('is_login', None):
        return redirect("/login/")
    if request.method == "POST":
        patient_id = request.POST.get('patient_id')
        datatype = request.POST.get('datatype')
        session_id = request.POST.get('session_id')
        querySet = Image.objects.filter(patientid=patient_id, sessionid=session_id,datatype=datatype).values()
        df = pd.DataFrame(list(querySet))
        image_data=df['data']
        return HttpResponse(image_data, content_type="image/png")
    return redirect("/index/")

def plot(request):
    if not request.session.get('is_login', None):
        return redirect("/login/")
    if request.method == "POST":
        patient_id= request.POST.get('patient_id')
        datatype=request.POST.get('datatype')
        quantity1=int(request.POST.get('quantity1'))
        quantity2=int(request.POST.get('quantity2'))
        probetype=request.POST.get('probetype')
        if (probetype == 'MES'):
            querySet = ProbedataSatellite.objects.filter(patient_id=patient_id, stimuli_type=datatype).values()
        else:
            if (probetype == 'oxy'):
                querySet = FnirsOxySatellite.objects.filter(patient_id=patient_id, fnirs_type=datatype).values()
            else:
                querySet = FnirsDeoxySatellite.objects.filter(patient_id=patient_id, fnirs_type=datatype).values()
        df = pd.DataFrame(list(querySet))
        columns = df.columns.tolist()
        columnlist=[]
        for i in columns:
            c=request.POST.get(i)
            if(c=='1.1'):
                columnlist.append(i)
        listfin=[]
        listout=[]
        df=df.loc[quantity1:quantity2,columnlist]
        out=quantity2+1-quantity1
        for i in range(quantity1,quantity2+1):
            listfin.append(i)
        for j in range(out):
            listout.append(j)
        text = []
        for column in columnlist:
            x1=listfin
            y1=[k for k in df[column]]
            trace = go.Scatter(
                x=x1,
                y=y1,
                textposition='top center',
                mode='markers+text+lines',
                name=column,  # Style name/legend entry with html tags
                connectgaps=True  # 是否连接缺失值
            )
            text.append(trace)
        layout = go.Layout(
            autosize=False,
            width=3000,
            height=2000,
            xaxis=go.layout.XAxis(linecolor='black',
                                  linewidth=1,
                                  mirror=True),

            yaxis=go.layout.YAxis(linecolor='black',
                                  linewidth=1,
                                  mirror=True),

            margin=go.layout.Margin(
                l=50,
                r=50,
                b=100,
                t=100,
                pad=4
            )
        )
        fig = go.Figure(data=text, layout=layout)
        graph_div = plotly1.offline.plot(fig, auto_open = True, output_type="div")
        return render(request, 'TEST/probedataplot.html',{'graph_div':graph_div,'patient_id':patient_id,'datatype':datatype,'probetype':probetype})
    return render(request,'TEST/probedataplot.html')


import os
import json
import tensorflow as tf
from keras.models import Sequential
import keras
from keras.layers import Dense, Dropout, LSTM, CuDNNLSTM, Activation, BatchNormalization, LeakyReLU, Flatten, TimeDistributed, CuDNNGRU, RNN, SimpleRNN, GRU
import numpy as np
import random
from sklearn.preprocessing import normalize
from tensorflow.keras.callbacks import TensorBoard
from keras import backend as K
import time
from normalizer import norm
import matplotlib.pyplot as plt
import pickle
import sys
from sklearn import preprocessing
import itertools
from sklearn.model_selection import train_test_split

#gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
#sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

class Visualize(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if epoch % 10 == 0 or 1==1:
            preds = []
            grounds = []
            x = list(range(len(test_samples)))
            for i in test_samples:
                preds.append(model.predict([[x_test[i]]])[0][0])
                grounds.append(y_test[i])

            plt.clf()
            plt.plot(x, grounds, label='ground-truth')#, marker='o')
            plt.plot(x, preds, label='prediction-{}'.format(epoch))#, marker='o')
            plt.title("test data (unseen)")
            plt.legend()
            plt.savefig("models/h5_models/{}/0-{}-epoch-{}.png".format(model_name, model_name, epoch))
            plt.pause(.1)
            model.save("models/h5_models/{}/{}-epoch-{}.h5".format(model_name, model_name, epoch))

#v_ego, v_lead, d_lead
data_dir = "LSTM"
os.chdir("C:/Git/dynamic-follow-tf")

norm_dir = "data/{}/normalized.npy"

model_name = "LSTM"

'''with open("data/{}/x_train".format(data_dir), "rb") as f:
    x_train = pickle.load(f)

with open("data/{}/y_train".format(data_dir), "rb") as f:
    y_train = pickle.load(f)'''

samples_to_use = 7000000
if samples_to_use != 'all':
    y_train = np.load("data/{}/y_train.npy".format(data_dir))[:samples_to_use]
else:
    y_train = np.load("data/{}/y_train.npy".format(data_dir))
    print(len(y_train))
is_array = False

if not os.path.exists(norm_dir.format(data_dir)):
    if samples_to_use != 'all':
        x_train = np.load("data/{}/x_train.npy".format(data_dir))[:samples_to_use]
    else:
        x_train = np.load("data/{}/x_train.npy".format(data_dir))
    print("Normalizing...", flush=True)
    normalized = norm(x_train)
    x_train = normalized['normalized']
    scales = normalized['scales']
    print("Dumping normalization...", flush=True)
    np.save(norm_dir.format(data_dir), x_train)
    with open('data/LSTM/scales', "wb") as f:
        pickle.dump(normalized['scales'], f)
    #with open(norm_dir.format(data_dir), "wb") as f:
        #pickle.dump(normalized, f)
else:
    is_array = True
    print("Loading normalized data...", flush=True)
    x_train = np.load(norm_dir.format(data_dir))
    with open('data/LSTM/scales', "rb") as f:
        scales = pickle.load(f)
    print('Loaded!', flush=True)
    #with open(norm_dir.format(data_dir), "rb") as f:
        #normalized = pickle.load(f)

print(len(x_train))
#scales = normalized['scales']
#x_train = normalized['normalized']
y_train = np.array([np.interp(i, [-1, 1], [0, 1]) for i in y_train])

flatten = True
if flatten:
    print('Flattening...', flush=True)
    #x_train = np.array([[inner for outer in sample for inner in outer] for sample in x_train])
    x_train = np.array([i.flatten() for i in x_train])  # whole lot faster lul
elif not is_array:
    x_train = np.array(x_train)
print(x_train.shape)

x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.1)

#random_choices = []
num_test = 600
num_test = min(len(x_test), num_test)

test_samples = random.sample(range(len(x_test)), num_test)
test_samples = sorted(list(zip([y_test[i] for i in test_samples], test_samples)))
test_samples = [y for x, y in test_samples] # sort from lowest to highest for visualization

opt = keras.optimizers.Adam(lr=0.0001)#, decay=1e-6)
opt = keras.optimizers.Adadelta()
#opt = keras.optimizers.RMSprop(0.001)
#opt = keras.optimizers.Adagrad(lr=0.00001)
opt = 'adam'
#opt = 'rmsprop'
#opt = keras.optimizers.SGD(lr=0.01, decay=1e-8, momentum=0.9, nesterov=True)

layers = 6
nodes = 186

model_type = 'dense' # lstm, dense, or gru

model = Sequential()
if model_type.lower() == 'lstm':
    to_sub = 2
    model.add(CuDNNLSTM(nodes, input_shape=(x_train.shape[1:]), return_sequences=True))
elif model_type.lower() == 'dense':
    to_sub = 1
    model.add(Dense(nodes, activation="relu", input_shape=(x_train.shape[1:])))
    #model.add(Dropout(0.1))
#model.add(CuDNNGRU(nodes, return_sequences=True, input_shape=(x_train.shape[1:])))
#model.add(SimpleRNN(128, activation='tanh', return_sequences=False, input_shape=(x_train.shape[1:])))
#model.add(Dropout(.05))
for i in range(layers - to_sub):
    #model.add(Dense(nodes, activation="relu"))
    #model.add(Dropout(.05))
    if model_type.lower() == 'lstm':
        model.add(CuDNNLSTM(nodes, return_sequences=True))
    #model.add(CuDNNGRU(nodes, return_sequences=True))
    elif model_type.lower() == 'dense':
        model.add(Dense(nodes, activation="relu"))
        #model.add(Dropout(0.1))
    #model.add(SimpleRNN(64, activation='tanh'))
#model.add(Dense(128, activation='relu'))
#model.add(Permute((2,1), input_shape=(10, 5)))
#model.add(CuDNNGRU(nodes, return_sequences=False))
if model_type.lower() == 'lstm':
    model.add(CuDNNLSTM(nodes, return_sequences=False))

model.add(Dense(1, activation='linear'))

model.compile(loss='mean_squared_error', optimizer=opt)
#tensorboard = TensorBoard(log_dir="logs/test-{}".format("30epoch"))
callback_list = [Visualize()]
model.fit(x_train, y_train, shuffle=True, batch_size=256, epochs=200, validation_data=(x_test, y_test), callbacks=callback_list)

def get_acc():
    accs = []
    for idx, i in enumerate(x_test[:20000]):
        pred = model.predict([[i]])[0][0]
        accs.append(abs(pred - y_test[idx]))
    print('Test accuracy: {}'.format(1 - sum(accs) / len(accs)))

    '''accs = []
    for idx, i in enumerate(x_train):
        pred = model.predict([[i]])[0][0]
        accs.append(abs(pred - y_train[idx]))
    print('Train accuracy: {}'.format(1 - sum(accs) / len(accs)))'''

get_acc()

for i in range(10):
    rand = random.randint(0, len(x_train))
    pred = model.predict([[x_train[rand]]])[0][0]
    print("Ground truth: {}".format(y_train[rand]))
    print("Prediction: {}\n".format(pred))

save_model = False
if save_model:
    model.save("models/h5_models/"+model_name+".h5")
    print("Saved model!")


from ticketguardian.affiliate import Affiliate

def test_parent_scope():
    affiliate = None
    for i in Affiliate.list():
        if i.parent is not None:
            # Get Affiliate that has a parent
            affiliate = i
            break

    parent = affiliate.parent
    parent_list = affiliate.parent_scope

    for index, test_parent in enumerate(parent_list):
        assert parent.id == test_parent.id
        parent = parent.parent


finished_dictionary = {}
with open("ABC123.txt", "r") as file:
    for line in file:
        key, value = line.split(' : ')
        key = key.strip('"')
        finished_dictionary[key] = int(value[0:3])
    print(finished_dictionary)
def numbering_function (dictionary):
    finished_dictionary1 = {}
    count = 1
    for key, value in dictionary.items():
        my_list = [key, value]
        finished_dictionary1[str(count) + "."] = my_list
        count += 1
    return finished_dictionary1

def java_house_menu_with_user_input(dictionary):
    total = 0
    for key, value in dictionary.items():
        print(key, value[0])
    item_numbers = []
    print("\n")
    answear = "No"
    while answear == "No" or answear == "no" or answear == "n":
        item = input("Item number? ")
        item_numbers.append(item)
        answear = input("Is that all? ")
    print("\n")
    for every_number in item_numbers:
        print(f"That will be a",dictionary[str(every_number) + "."][0])
        total += dictionary[str(every_number) + "."][1]
    print(f"The Total Will Be KES {total}")
java_house_menu_with_user_input(numbering_function(finished_dictionary))


import sqlite3

def setup_db():
    connection = sqlite3.connect('hacker.db')
    cursor = connection.cursor()
    query = f'''
    CREATE TABLE IF NOT EXISTS CipherBreaker (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        messege VARCHAR(50),
        encypted_message TEXT
    );
    '''
    cursor.executescript(query)
    connection.commit()
    connection.close()

def main():
    setup_db()

if __name__ == '__main__':
    main()


import requests
from django.http import JsonResponse

def cep(request) -> JsonResponse:
    current_cep = request.GET.get('cep').replace('-', '')
    url = f'https://viacep.com.br/ws/{current_cep}/json/'
    response = requests.get(url)
    if response.status_code == 200:
        resp = response.json()
        return JsonResponse({
            'status': 'success',
            'content': {
                'address': resp.get('logradouro'),
                'city': resp.get('localidade'),
                'state': resp.get('uf'),
            }
        })
    return JsonResponse({'status': 'error', 'message': 'CEP não encontrado'}, status=200)


""" Tests for regressors module.

Tests in ``py.test`` format.
"""

from os.path import dirname, join as pjoin

import numpy as np
import numpy.linalg as npl
from scipy.interpolate import interp1d

from skimage.filters import threshold_otsu
import nibabel as nib

from fmri_designs.regressors import (events2neural_hr, poly_drift,
                                     deltas_at_rows, spm_hrf_dt,
                                     conds2hrf_cols, f_tests, f_tests_3d,
                                     f_for_outliers, outlier_metrics)
from fmri_designs.tmpdirs import dtemporize
from fmri_designs.spm_funcs import spm_hrf

import pytest
from numpy.testing import assert_array_equal, assert_almost_equal

HERE = dirname(__file__)
CONDS = pjoin(HERE, 'conds')

# Example onset, duration, amplitude triplets
COND_TEST = """\
10    6.0    1
20    4.0    2
24    2.0    0.1"""

# Name of file to write for testing
COND_TEST_FNAME = 'cond_test1.txt'

# Work in temporary directory
@dtemporize
def test_events2neural_hr_simple():
    # test events2neural_hr function
    # Write condition test file
    with open(COND_TEST_FNAME, 'wt') as fobj:
        fobj.write(COND_TEST)
    # Read it back
    times_neural = events2neural_hr(COND_TEST_FNAME, 32, dt=2.)
    assert times_neural.shape == (2, 16)
    times, neural = times_neural
    assert_array_equal(times, np.arange(0, 32, 2))
    # Expected values for tr=2, n_trs=16
    expected = np.zeros(16)
    expected[5:8] = 1
    expected[10:12] = 2
    expected[12] = 0.1
    assert_array_equal(neural, expected)
    times, neural = events2neural_hr(COND_TEST_FNAME, 30, dt=1.)
    assert_array_equal(times, np.arange(30))
    # Expected values for tr=1, n_trs=30
    expected = np.zeros(30)
    expected[10:16] = 1
    expected[20:24] = 2
    expected[24:26] = 0.1
    assert_array_equal(neural, expected)
    # Extend duration, more zeros in neural
    times, neural = events2neural_hr(COND_TEST_FNAME, 40, dt=1.)
    assert_array_equal(times, np.arange(40))
    assert_array_equal(neural, np.concatenate((expected, np.zeros(10))))
    # Drop duration, truncates
    times, neural = events2neural_hr(COND_TEST_FNAME, 20, dt=1.)
    assert_array_equal(times, np.arange(20))
    assert_array_equal(neural, expected[:20])
    # dt of 0.1
    times, neural = events2neural_hr(COND_TEST_FNAME, 30, dt=0.1)
    assert_array_equal(times, np.arange(0, 30, 0.1))
    # Expected values for tr=0.1, n_trs=300
    expected = np.zeros(300)
    expected[100:160] = 1
    expected[200:240] = 2
    expected[240:260] = 0.1
    assert_array_equal(neural, expected)
    # 0.1 is the default
    times, neural = events2neural_hr(COND_TEST_FNAME, 30)
    assert_array_equal(times, np.arange(0, 30, 0.1))
    assert_array_equal(neural, expected)

def test_poly_drift():
    # Test polynomial drift
    times = np.arange(0, 20, 2.15)
    n = len(times)
    assert_array_equal(poly_drift(times, 0), np.ones((n, 1)))
    linear = np.linspace(-1, 1, n)
    linear /= npl.norm(linear)
    assert_almost_equal(poly_drift(times, 1), np.c_[linear, np.ones(n)])
    quadratic = (linear ** 2) - (linear ** 2).mean()
    quadratic /= npl.norm(quadratic)
    assert_almost_equal(poly_drift(times, 2),
                        np.c_[quadratic, linear, np.ones(n)])
    cubic = (linear ** 3) - (linear ** 3).mean()
    cubic /= npl.norm(cubic)
    assert_almost_equal(poly_drift(times, 3),
                        np.c_[cubic, quadratic, linear, np.ones(n)])
    d_10 = poly_drift(times, 10)
    exp_sums = np.concatenate((np.zeros(10), [n]))
    assert_almost_equal(np.sum(d_10, axis=0), exp_sums)
    exp_lengths = np.concatenate((np.ones(10), [np.sqrt(n)]))
    assert_almost_equal(np.sqrt(np.sum(d_10 ** 2, axis=0)), exp_lengths)

def test_deltas_at_rows():
    # Test design for deltas as given rows
    for rows, n in (([3, 7, 15], 20),
                    ([1, 2, 9], 12)):
        d = deltas_at_rows(rows, n)
        for col, row in enumerate(rows):
            assert d[row, col] == 1
            d[row, col] = 0
        assert np.all(d == 0)
    with pytest.raises(IndexError):
        deltas_at_rows([3, 12], 12)

def test_spm_hrf_dt():
    # Test SPM HRF at duration and dt
    assert_almost_equal(spm_hrf(np.arange(0, 30, 0.1)),
                        spm_hrf_dt())
    assert_almost_equal(spm_hrf(np.arange(0, 20, 0.1)),
                        spm_hrf_dt(20))
    assert_almost_equal(spm_hrf(np.arange(10)),
                        spm_hrf_dt(10, 1))

def test_conds2hrf_cols():
    # Test function to return design columns for condition files
    cond_fnames = [pjoin(HERE, 'ds114_sub009_t2r1_cond.txt'),
                   pjoin(HERE, 'new_cond.txt')]
    TR = 2.5
    tr_times = np.arange(0, 400, TR)
    hrf_cols = conds2hrf_cols(cond_fnames, tr_times)
    # Now go the slow way round
    hrf = spm_hrf(np.arange(0, 30, 0.1))
    hr_times, neural = events2neural_hr(cond_fnames[0], 410)
    conv = np.convolve(neural, hrf)[:len(neural)]
    interp0 = interp1d(hr_times, conv, bounds_error=False, fill_value=0)
    hr_times, neural = events2neural_hr(cond_fnames[1], 410)
    conv = np.convolve(neural, hrf)[:len(neural)]
    interp1 = interp1d(hr_times, conv, bounds_error=False, fill_value=0)
    hrf_cols_manual = np.c_[interp0(tr_times), interp1(tr_times)]
    assert_almost_equal(hrf_cols, hrf_cols_manual)

def test_f_tests():
    # Test F test routine against results from R
    # See f_tests.R
    x = np.loadtxt(pjoin(HERE, 'x.txt'))
    y1 = np.loadtxt(pjoin(HERE, 'y1.txt'))
    y2 = np.loadtxt(pjoin(HERE, 'y2.txt'))
    Y = np.c_[y1, y2]
    n = len(x)
    X_f = np.ones((n, 2))
    X_f[:, 1] = x
    F, nu_1, nu_2 = f_tests(Y, X_f, np.ones((n, 1)))
    # Test F test results come from the output of R
    exp_f = [0.01197, 0.5955]
    assert_almost_equal(F, exp_f, 4)
    assert (nu_1, nu_2) == (1, 98)
    # Test 3D version
    vol_shape = (2, 3, 4)
    Y_4d = np.zeros(vol_shape + (n,))
    mask = np.zeros(vol_shape, dtype=bool)
    Y_4d[1, 1, 1] = y1
    Y_4d[1, 1, 2] = y2
    mask[1, 1, 1] = True
    mask[1, 1, 2] = True
    exp_f_3d = np.zeros(vol_shape, dtype=float)
    exp_f_3d[mask] = exp_f
    F, nu_1, nu_2 = f_tests_3d(Y_4d, mask, X_f, np.ones((n, 1)))
    assert_almost_equal(F, exp_f_3d, 4)
    assert (nu_1, nu_2) == (1, 98)

def test_f_for_outliers():
    # More or less smoke test for big-picture routine
    img_fname = pjoin(HERE, 'group00_sub04_run1.nii')
    cond_fnames = [pjoin(CONDS, 'group00_sub04_run1_cond1.txt'),
                   pjoin(CONDS, 'group00_sub04_run1_cond2.txt'),
                   pjoin(CONDS, 'group00_sub04_run1_cond3.txt'),
                   pjoin(CONDS, 'group00_sub04_run1_cond4.txt')]
    tr = 2.0
    f_1, f_2, f_3, msk = f_for_outliers(img_fname, cond_fnames, tr, [2, 3])
    # Long manual process
    data = nib.load(img_fname).get_data()
    n_trs = data.shape[-1]
    tr_times = np.arange(n_trs) * tr
    hrf_cols = conds2hrf_cols(cond_fnames, tr_times)
    drift = poly_drift(tr_times, 3)
    outliers = deltas_at_rows([2, 3], n_trs)
    mean = data.mean(axis=-1)
    mask = mean > threshold_otsu(mean)
    assert_array_equal(mask, msk)
    f_hrf, h_n1, h_n2 = f_tests_3d(data, mask, np.c_[hrf_cols, drift], drift)
    assert_array_equal(f_1[0], f_hrf)
    reduced = np.c_[hrf_cols, drift]
    f_outliers, o_n1, o_n2 = f_tests_3d(data, mask,
                                        np.c_[outliers, reduced], reduced)
    reduced = np.c_[outliers, drift]
    f_both, b_n1, b_n2 = f_tests_3d(data, mask,
                                    np.c_[hrf_cols, reduced], reduced)
    assert_array_equal(f_2[0], f_outliers)
    assert_array_equal(f_3[0], f_both)
    # HRF cols 2 and 3 all zero because of truncated data (first 10 vols)
    assert f_1[1:] == (2, n_trs - 6)
    assert f_2[1:] == (2, n_trs - 8)
    assert f_3[1:] == (2, n_trs - 8)

def test_outlier_metrics():
    a = np.random.normal(10, 2, size=(10, 11, 12))
    b = np.random.normal(10, 2, size=(10, 11, 12))
    c = np.random.normal(10, 2, size=(10, 11, 12))
    msk = np.random.normal(10, 2, size=(10, 11, 12)) > 0.2
    n = np.sum(msk)
    first, second, third, d = outlier_metrics((a, 1, 1), (b, 1, 1),
                                              (c, 1, 1), msk)
    assert first == a[msk].sum() / n
    assert second == b[msk].sum() / n
    assert third == c[msk].sum() / n
    assert d == third - first


import cv2 as cv
import numpy as np
from windowcapture import WindowCapture

steve = cv.imread("Steve.PNG")
steve_w = steve.shape[1]
steve_h = steve.shape[0]
wincap = WindowCapture('minecraft - Google Search - Google Chrome')
while True:

    img = wincap.get_screenshot()
    #imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    result = cv.matchTemplate(steve, img, cv.TM_CCOEFF_NORMED)
    # get location of best match
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)

    cv.circle(img, (max_loc[1]+steve_w,max_loc[0]+steve_h), 50, (255, 0, 0), 10)

    cv.imshow("Computer Vision", img)
    if cv.waitKey(1) & 0xFF == ord('q'):
        cv.destroyAllWindows()
        break

print("Done.")


#!/D:/ProgramFiles/Python/Python35/python.exe
#coding:utf-8
import urllib.request
import string

# url = "http://www.dianping.com/search/category/1/10/g113r801"
# url = "http://cailianpress.com/"
url = "http://english.cri.cn/"
headers = ('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')
opener = urllib.request.build_opener()
opener.addheaders = [headers]

data = opener.open(url).read()

try:
	page_file = open ('page.html', 'w')
	print(data.decode('utf-8').encode('utf-8'), file = page_file)
	# page_file.write(data.decode('utf-8').encode('utf-8'))
except IOError:
	print ("someting wrong")
finally:
	if 'page_file' in locals(): #
		page_file.close()

# print (data)


from Type import effective as eff
from commands import random_number

def kumpul(role: eff, bahan: list[list[str]]) -> list[list[str]]:
    if role!="jin_pengumpul":
        print("Kamu tidak memiliki akses ke fitur ini!")
    else:
        lootSand = (random_number(0,5))
        lootRock = (random_number(0,5))
        lootWater = (random_number(0,5))

        print(f"Jin menemukan {lootSand} pasir, {lootRock} batu, {lootWater} air.")

        bahan[0][2] = str(int(bahan[0][2]) + lootSand)
        bahan[1][2] = str(int(bahan[1][2]) + lootRock)
        bahan[2][2] = str(int(bahan[2][2]) + lootWater)


import functools
import logging
import unicodedata

class Normaliser:
	"""
	Normalises strings and keeps track of those that (1) do not comply to
	Unicode's normal form; (2) have whitespace issues.
	"""

	def __init__(self, nfc_chars=[]):
		"""
		Constructor. The optional arg specifies the set of chars that should
		not be decomposed.
		"""
		self.log = logging.getLogger(__name__)

		self.norm_f = functools.partial(unicodedata.normalize, 'NFD')

		self.nfc_chars = set(nfc_chars)

		self.strip_errors = []
		self.norm_errors = []

	def normalise(self, string, line_num):
		"""
		Strips the whitespace and applies Unicode normalisation to the given
		string. The second arg is used as an ID of the string when reporting
		its lint errors (if such).
		"""
		stripped = string.strip()
		if stripped != string:
			self.strip_errors.append(line_num)

		nfc_pos = [index
					for index, char in enumerate(stripped)
					if char in self.nfc_chars]

		parts = []
		start_pos = 0

		for pos in nfc_pos:
			if pos > 0:
				parts.append(self.norm_f(stripped[start_pos:pos]))

			parts.append(stripped[pos])
			start_pos = pos + 1

		if start_pos < len(stripped):
			parts.append(self.norm_f(stripped[start_pos:]))

		norm = ''.join(parts)

		if norm != stripped:
			self.norm_errors.append(line_num)

		return norm

	def report(self, reporter, ignore_nfd=False, ignore_ws=False):
		"""
		Adds the problems that have been found so far to the given Reporter
		instance. The two keyword args can be used to restrict the error types
		to be reported.
		"""
		if self.strip_errors and not ignore_ws:
			reporter.add(self.strip_errors, 'leading or trailing whitespace')

		if self.norm_errors and not ignore_nfd:
			reporter.add(self.norm_errors, 'not in Unicode NFD')


''' return prime numbers between 1 and n '''
''' Reference : https://stackoverflow.com/questions/11619942/print-series-of-prime-numbers-in-python/39106237#39106237'''

def find_prime_num(num):
    prime_num_list = []
    sieve = [True] * (num + 1)
    for i in range(2, num + 1):
        if (sieve[i]):
            prime_num_list.append(i)
            for i in range(i, num + 1, i):
                sieve[i] = False
    return prime_num_list

def brute_force_find_prime_num(num):
    prime_num_list = [];count=0

    for num in range(2, num+1):
        for den in range(2, num+1):
            if num % den == 0:
                count += 1
        if count == 1:
            prime_num_list.append(num)
        count=0

    return prime_num_list

if __name__ == "__main__":
    # print('prime numbers b/w 1 and {} are {}'.format(10, brute_force_find_prime_num(10)))
    print('prime numbers b/w 1 and {} are {}'.format(10, find_prime_num(10)))


#!/usr/bin/env python
"""Provides scikit interface."""

import numpy as np
import scipy as sp
import random

from ego.optimization.part_importance_estimator import PartImportanceEstimator

from ego.optimization.neighborhood_graph_grammar import NeighborhoodAdaptiveGraphGrammar
from ego.optimization.neighborhood_graph_grammar import NeighborhoodPartImportanceGraphGrammar

from ego.optimization.neighborhood_edge_swap import NeighborhoodEdgeSwap
from ego.optimization.neighborhood_edge_label_swap import NeighborhoodEdgeLabelSwap
from ego.optimization.neighborhood_edge_label_mutation import NeighborhoodEdgeLabelMutation
from ego.optimization.neighborhood_edge_move import NeighborhoodEdgeMove
from ego.optimization.neighborhood_edge_remove import NeighborhoodEdgeRemove
from ego.optimization.neighborhood_edge_add import NeighborhoodEdgeAdd
from ego.optimization.neighborhood_edge_expand import NeighborhoodEdgeExpand
from ego.optimization.neighborhood_edge_contract import NeighborhoodEdgeContract

from ego.optimization.neighborhood_node_label_swap import NeighborhoodNodeLabelSwap
from ego.optimization.neighborhood_node_label_mutation import NeighborhoodNodeLabelMutation
from ego.optimization.neighborhood_node_remove import NeighborhoodNodeRemove
from ego.optimization.neighborhood_node_add import NeighborhoodNodeAdd
from ego.optimization.neighborhood_node_smooth import NeighborhoodNodeSmooth

from ego.optimization.neighborhood_edge_crossover import NeighborhoodEdgeCrossover
from ego.optimization.neighborhood_cycle_crossover import NeighborhoodCycleCrossover
from ego.optimization.neighborhood_double_edge_crossover import NeighborhoodDoubleEdgeCrossover
from ego.optimization.neighborhood_break_crossover import NeighborhoodBreakCrossover

from ego.optimization.score_estimator import GraphUpperConfidenceBoundEstimator
from ego.optimization.score_estimator import GraphRandomForestScoreEstimator
from ego.optimization.score_estimator import GraphLinearScoreEstimator
from ego.optimization.score_estimator import GraphExpectedImprovementEstimator
from ego.optimization.score_estimator import GraphNeuralNetworkScoreEstimator
from ego.optimization.score_estimator import GraphNearestNeighborScoreEstimator
from ego.optimization.score_estimator import EnsembleScoreEstimator

from ego.optimization.feasibility_estimator import FeasibilityEstimator
from ego.decomposition.paired_neighborhoods import decompose_neighborhood
from ego.vectorize import hash_graph
from ego.utils.parallel_utils import simple_parallel_map
# from ego.optimization.score_estimator import GraphLinearClassifier
import time
from toolz import curry
import logging

logger = logging.getLogger()

def remove_duplicates(graphs):
    """remove_duplicates."""
    df = decompose_neighborhood(radius=2)
    selected_graphs_dict = {hash_graph(
        g, decomposition_funcs=df): g for g in graphs}
    return list(selected_graphs_dict.values())

def remove_duplicates_in_set(graphs_to_filter, graph_archive):
    """remove_duplicates_in_set."""
    df = decompose_neighborhood(radius=2)
    val_set = set([hash_graph(g, decomposition_funcs=df)
                   for g in graph_archive])
    selected_graphs = [g for g in graphs_to_filter if hash_graph(
        g, decomposition_funcs=df) not in val_set]
    return selected_graphs

def biased_sample(graphs, scores, sample_size):
    """biased_sample."""
    p = np.array(scores)
    p = np.nan_to_num(p)
    p[p < 0] = 0
    # if there are non zero probabilities
    # replace negative or zero probabilities with smallest positive prob
    if len(p[p > 0]) > 1:
        min_p = np.min(p[p > 0])
        p[p == 0] = min_p
        p = p / p.sum()
        sample_ids = np.random.choice(
            len(graphs), size=sample_size, replace=False, p=p)
    # otherwise sample uniformly at random
    else:
        sample_ids = np.random.choice(
            len(graphs), size=sample_size, replace=False)
    sample_graphs = [graphs[sample_id] for sample_id in sample_ids]
    sample_scores = [scores[sample_id] for sample_id in sample_ids]
    return sample_graphs, sample_scores

def tournament_sample(graphs, scores, sample_size):
    n = len(graphs)
    ids = list(range(n))
    random.shuffle(ids)
    policy_selected_graphs = []
    policy_selected_scores = []
    for i in range(0, n - 1, 2):
        if scores[ids[i]] > scores[ids[i + 1]]:
            policy_selected_graphs.append(graphs[ids[i]])
            policy_selected_scores.append(scores[ids[i]])
        else:
            policy_selected_graphs.append(graphs[ids[i + 1]])
            policy_selected_scores.append(scores[ids[i + 1]])
    policy_selected_graphs, policy_selected_scores = policy_selected_graphs[:sample_size], policy_selected_scores[:sample_size]
    return policy_selected_graphs, policy_selected_scores

def sample(graphs, scores, sample_size, greedy_frac=0.5, policy='tournament'):
    """sample."""
    assert len(graphs) > 0, 'Something went wrong: no graphs left to sample'
    if sample_size > len(scores):
        sample_size = len(scores)

    # select with greedy strategy
    greedy_selected_graphs = []
    greedy_selected_scores = []
    n_greedy_selection = int(sample_size * greedy_frac)
    sorted_ids = np.argsort(-np.array(scores))
    greedy_selected_ids = sorted_ids[:n_greedy_selection]
    if len(greedy_selected_ids) > 0:
        greedy_selected_graphs = [graphs[greedy_selected_id]
                                  for greedy_selected_id in greedy_selected_ids]
        greedy_selected_scores = [scores[greedy_selected_id]
                                  for greedy_selected_id in greedy_selected_ids]
    # select the rest with a specific policy: either biased or random
    policy_selected_graphs = []
    policy_selected_scores = []
    unselected_ids = sorted_ids[n_greedy_selection:]
    if len(unselected_ids) > 0:
        unselected_graphs = [graphs[unselected_id]
                             for unselected_id in unselected_ids]
        unselected_scores = [scores[unselected_id]
                             for unselected_id in unselected_ids]
        size = sample_size - len(greedy_selected_graphs)
        if policy == 'tournament':
            policy_selected_graphs, policy_selected_scores = tournament_sample(
                unselected_graphs, unselected_scores, size)
        elif policy == 'biased':
            policy_selected_graphs, policy_selected_scores = biased_sample(
                unselected_graphs, unselected_scores, size)

    selected_graphs = greedy_selected_graphs + policy_selected_graphs
    selected_scores = greedy_selected_scores + policy_selected_scores
    return selected_graphs, selected_scores

@curry
def _perturb(g, neighborhood_estimator=None, part_importance_estimator=None, feasibility_estimator=None):
    neighbor_graphs = []
    neighborhood_estimator.part_importance_estimator = part_importance_estimator
    neighbors = neighborhood_estimator.neighbors(g)
    if feasibility_estimator is None:
        feasibility_scores = [True] * len(neighbors)
    else:
        feasibility_scores = feasibility_estimator.predict(neighbors)
    for neighbor, is_feasible in zip(neighbors, feasibility_scores):
        if is_feasible:
            neighbor.graph['parent'] = g.copy()
            # TODO if the score is the same, then evaluate if they are the same, if so then use 'identity' as type
            neighbor.graph['type'] = type(neighborhood_estimator).__name__
            neighbor_graphs.append(neighbor)
    return neighbor_graphs

def perturb(graphs, neighborhood_estimator, part_importance_estimator=None, feasibility_estimator=None, execute_concurrently=False):
    """perturb."""
    # generate a fixed num of neighbors for each graph in input
    _perturb_ = _perturb(neighborhood_estimator=neighborhood_estimator, part_importance_estimator=part_importance_estimator, feasibility_estimator=feasibility_estimator)
    if execute_concurrently:
        neighbors_list = simple_parallel_map(_perturb_, graphs)
    else:
        neighbors_list = [_perturb_(g) for g in graphs]
    neighbor_graphs = []
    for neighbors in neighbors_list:
        neighbor_graphs.extend(neighbors)
    return neighbor_graphs

def mutate(proposed_graphs, graphs, part_importance_estimator=None, feasibility_estimator=None):
    """mutate."""
    mutated_proposed_graphs = []
    mutated_proposed_graphs += perturb(
        proposed_graphs,
        NeighborhoodNodeLabelMutation(n_nodes=None).fit(graphs),
        part_importance_estimator,
        feasibility_estimator)
    mutated_proposed_graphs += perturb(
        proposed_graphs,
        NeighborhoodEdgeLabelMutation(n_edges=None).fit(graphs),
        part_importance_estimator,
        feasibility_estimator)
    mutated_proposed_graphs += perturb(
        proposed_graphs,
        NeighborhoodEdgeSwap(n_edges=None).fit(graphs),
        part_importance_estimator,
        feasibility_estimator)
    return mutated_proposed_graphs

def elitism(proposed_graphs, oracle_func, frac_instances_to_remove_per_iter):
    """elitism."""
    scores = [oracle_func(g) for g in proposed_graphs]
    ids = np.argsort(scores)
    n = int(frac_instances_to_remove_per_iter * len(scores))
    ids = ids[n:]
    surviving_graphs = [proposed_graphs[id] for id in ids]
    surviving_scores = [scores[id] for id in ids]
    for graph, score in zip(surviving_graphs, surviving_scores):
        graph.graph['oracle_score'] = score
    return surviving_graphs, surviving_scores

@curry
def materialize_iterated_neighborhood(
        id_estimator,
        neighborhood_estimators=None,
        next_proposed_graphs=None,
        graphs=None,
        part_importance_estimator=None,
        feasibility_estimator=None,
        score_estimator=None,
        step=None,
        n_steps_driven_by_estimator=None,
        sample_size_to_perturb=None,
        n_queries_to_oracle_per_iter=None,
        parallelization_strategy=None,
        greedy_frac=0.5):
    """materialize_iterated_neighborhood."""
    if parallelization_strategy == 'graph_wise':
        execute_concurrently = True
    else:
        execute_concurrently = False
    neighborhood_estimator = neighborhood_estimators[id_estimator]
    start_time = time.process_time()
    all_neighbor_graphs = perturb(
        next_proposed_graphs,
        neighborhood_estimator,
        part_importance_estimator,
        feasibility_estimator,
        execute_concurrently)
    next_proposed_graphs, num_estimator_queries = [], 0
    neighbor_graphs = remove_duplicates(all_neighbor_graphs)
    if len(neighbor_graphs) == 0:
        logger.debug('Warning: removing duplicates among results resulted in no graphs')
        return next_proposed_graphs, num_estimator_queries
    neighbor_graphs = remove_duplicates_in_set(neighbor_graphs, graphs)
    if len(neighbor_graphs) == 0:
        logger.debug('Warning: removing duplicates w.r.t. archived graphs resulted in no graphs')
        return next_proposed_graphs, num_estimator_queries
    predicted_scores = score_estimator.acquisition_score(neighbor_graphs)
    num_estimator_queries = len(predicted_scores)
    # sample neighborhood according to surrogate score
    if step < n_steps_driven_by_estimator - 1:
        sample_size = int(sample_size_to_perturb / len(neighborhood_estimators))
    else:
        sample_size = int(n_queries_to_oracle_per_iter / len(neighborhood_estimators))
    sample_size = max(2, sample_size)
    next_proposed_graphs, next_proposed_scores = sample(
        neighbor_graphs,
        predicted_scores,
        sample_size,
        greedy_frac=greedy_frac)
    if len(next_proposed_graphs) == 0:
        logger.debug('Warning: sampling graphs resulted in no graphs')
        return next_proposed_graphs, num_estimator_queries

    end_time = time.process_time()
    elapsed_time = (end_time - start_time) / 60.0
    if n_steps_driven_by_estimator == 1:
        step_str = ''
    else:
        step_str = '%d:' % (step + 1)
    logger.info('%s%2d/%d) %30s:%4d novel graphs out of %4d generated  %3d selected graphs   best predicted score:%.3f   time:%.1f min' % (
        step_str, id_estimator + 1, len(neighborhood_estimators),
        type(neighborhood_estimator).__name__.replace('Neighborhood', ''),
        len(neighbor_graphs), len(all_neighbor_graphs),
        len(next_proposed_graphs), max(next_proposed_scores),
        elapsed_time))
    return next_proposed_graphs, num_estimator_queries

def select_iterated_neighborhoods(
        proposed_graphs,
        neighborhood_fitting_graphs,
        neighborhood_fitting_scores,
        graphs,
        neighborhood_estimators,
        part_importance_estimator,
        feasibility_estimator,
        score_estimator,
        n_steps_driven_by_estimator,
        sample_size_to_perturb,
        n_queries_to_oracle_per_iter,
        parallelization_strategy,
        greedy_frac):
    """select_iterated_neighborhoods."""
    all_n_estimator_queries = 0
    if parallelization_strategy == 'neighborhood_wise':
        execute_concurrently = True
    else:
        execute_concurrently = False
    for n_estimator, neighborhood_estimator in enumerate(neighborhood_estimators):
        neighborhood_estimator.fit(neighborhood_fitting_graphs, neighborhood_fitting_scores)
    for step in range(n_steps_driven_by_estimator):
        all_proposed_graphs = []
        _materialize_iterated_neighborhood_ = materialize_iterated_neighborhood(
            neighborhood_estimators=neighborhood_estimators,
            next_proposed_graphs=proposed_graphs[:],
            graphs=graphs,
            part_importance_estimator=part_importance_estimator,
            feasibility_estimator=feasibility_estimator,
            score_estimator=score_estimator,
            step=step,
            n_steps_driven_by_estimator=n_steps_driven_by_estimator,
            sample_size_to_perturb=sample_size_to_perturb,
            n_queries_to_oracle_per_iter=n_queries_to_oracle_per_iter)
        if execute_concurrently:
            list_of_graphs = simple_parallel_map(_materialize_iterated_neighborhood_, range(len(neighborhood_estimators)))
        else:
            list_of_graphs = [_materialize_iterated_neighborhood_(i) for i in range(len(neighborhood_estimators))]
        for gs, n_estimator_queries in list_of_graphs:
            all_proposed_graphs += gs
            all_n_estimator_queries += n_estimator_queries
        # for n_estimator, neighborhood_estimator in enumerate(neighborhood_estimators):
        #    next_proposed_graphs = _materialize_iterated_neighborhood_(n_estimator)
        #    all_proposed_graphs += next_proposed_graphs
        proposed_graphs = remove_duplicates(all_proposed_graphs)
        proposed_graphs = remove_duplicates_in_set(proposed_graphs, graphs)
        proposed_predicted_scores = score_estimator.acquisition_score(proposed_graphs)
        if step < n_steps_driven_by_estimator - 1:
            sample_size = sample_size_to_perturb
        else:
            sample_size = n_queries_to_oracle_per_iter
        proposed_graphs, proposed_predicted_scores = sample(
            proposed_graphs,
            proposed_predicted_scores,
            sample_size,
            greedy_frac)

    if n_queries_to_oracle_per_iter < len(proposed_graphs):
        logger.info('sampling %d out of %d non redundant graphs out of %d graphs generated for oracle evaluation' % (
            n_queries_to_oracle_per_iter, len(proposed_graphs), len(all_proposed_graphs)))
        proposed_graphs, proposed_predicted_scores = sample(
            proposed_graphs,
            proposed_predicted_scores,
            n_queries_to_oracle_per_iter,
            greedy_frac)
    # at this point we have proposed_graphs, proposed_scores:
    # for each graph we have the 'parent' and the 'type' and the score
    # we can formulate a learning task where starting from the parent graph
    # we have to predict in multiclass the type that has max score in any of the offsprings
    # so we have to collect all predictions relative to the same (hashed) parent graph
    # and select the argmax as the class to predict
    # the prediction can be used to allocate resources: expand only the k types that are
    # predicted to be yielding the best future improvements with k depending on the budget
    # the type predictor will be passed to the 'perturb' function and will mute the generation
    # for types predicted to under perform
    # NOTE: code should allow empty neighbor_graphs
    # Note: allow for no prediction at all when all types need to be generated

    return proposed_graphs, proposed_predicted_scores, all_n_estimator_queries

def compute_prediction_correlation(predicted_scores, true_scores):
    val, p = sp.stats.spearmanr(predicted_scores, true_scores)
    if np.isnan(val):
        val = 0
    return val

def optimize(graphs,
             oracle_func=None,
             n_iter=100,
             n_queries_to_oracle_per_iter=100,
             frac_instances_to_remove_per_iter=.1,
             sample_size_to_perturb=8,
             neighborhood_estimators=None,
             score_estimator=None,
             part_importance_estimator=None,
             feasibility_estimator=None,
             threshold_score_to_terminate=.99,
             monitor=None,
             draw_graphs=None,
             parallelization_strategy='graph_wise',
             greedy_sample_vs_tournament_frac=0.5,
             patience=3,
             n_steps_driven_by_estimator_base=3,
             add_mutations_when_asking_oracle=True):
    """optimize."""
    assert oracle_func is not None, 'An oracle function must be made available'
    n_steps_driven_by_estimator = n_steps_driven_by_estimator_base
    original_n_queries_to_oracle_per_iter = n_queries_to_oracle_per_iter
    exploration_vs_exploitation = score_estimator.exploration_vs_exploitation
    oracle_start_time = time.process_time()
    true_scores = [oracle_func(graph)
                   if graph.graph.get('oracle_score', None) is None
                   else graph.graph['oracle_score']
                   for graph in graphs]
    for graph, score in zip(graphs, true_scores):
        graph.graph['oracle_score'] = score
    oracle_end_time = time.process_time()
    oracle_elapsed_time = (oracle_end_time - oracle_start_time) / 60.0
    logger.info('Oracle evaluated on %d graphs in %.1f min' % (len(graphs), oracle_elapsed_time))

    proposed_graphs = []
    proposed_true_scores = []
    proposed_predicted_scores = []
    corr_true_vs_pred_scores_history = []
    max_score_history = []
    corr_true_vs_pred_scores_delta = 0
    corr_true_vs_pred_scores = 0
    max_score_delta = 0
    curr_patience = patience
    for i in range(n_iter):
        logger.info('\n\n- iteration: %d/%d' % (i + 1, n_iter))

        # update with oracle
        num_proposed_graphs = len(proposed_graphs)
        if num_proposed_graphs:
            oracle_start_time = time.process_time()
            proposed_graphs, proposed_true_scores = elitism(
                proposed_graphs, oracle_func, frac_instances_to_remove_per_iter)
            graphs += proposed_graphs[:]
            true_scores += proposed_true_scores[:]
            weights, errors = score_estimator.estimate_weights(proposed_graphs, proposed_true_scores)
            estimator_names = [type(estimator).__name__.replace('Graph', '').replace('Score', '').replace('Estimator', '') for estimator in score_estimator.estimators]
            for es, er, we in zip(estimator_names, errors, weights):
                print('est: %30s:   err: %.1e   w:%.3f' % (es, er, we))
            selected_proposed_predicted_scores = score_estimator.predict(proposed_graphs)
            if len(proposed_true_scores) == len(selected_proposed_predicted_scores):
                corr_true_vs_pred_scores = compute_prediction_correlation(proposed_true_scores, selected_proposed_predicted_scores)
            else:
                # start case
                corr_true_vs_pred_scores = 0
            corr_true_vs_pred_scores_history.append(corr_true_vs_pred_scores)
            if len(corr_true_vs_pred_scores_history) > 1:
                corr_true_vs_pred_scores_delta = (corr_true_vs_pred_scores_history[-1] - corr_true_vs_pred_scores_history[-2])
            else:
                corr_true_vs_pred_scores_delta = 0
            oracle_end_time = time.process_time()
            oracle_elapsed_time = (oracle_end_time - oracle_start_time) / 60.0
            logger.info('Oracle evaluated on %d graphs in %.1f min' % (num_proposed_graphs, oracle_elapsed_time))
            logger.info('Correlation between %d predicted and true scores: %.3f (increased of %.3f from previous iteration)' %
                        (len(proposed_true_scores), corr_true_vs_pred_scores, corr_true_vs_pred_scores_delta))
        # policy for n_steps_driven_by_estimator
        if corr_true_vs_pred_scores_delta > 0:
            n_steps_driven_by_estimator_factor = 1
        else:
            n_steps_driven_by_estimator_factor = -1
        n_steps_driven_by_estimator += n_steps_driven_by_estimator_factor
        n_steps_driven_by_estimator = int(n_steps_driven_by_estimator)
        if corr_true_vs_pred_scores < 0.5:
            n_steps_driven_by_estimator = n_steps_driven_by_estimator_base
        n_steps_driven_by_estimator = max(n_steps_driven_by_estimator_base, n_steps_driven_by_estimator)
        n_steps_driven_by_estimator = min(10, n_steps_driven_by_estimator)
        logger.info('n_steps_driven_by_estimator: %d' % (n_steps_driven_by_estimator))
        # termination condition
        if proposed_true_scores:
            max_iteration_score = max(proposed_true_scores)
        else:
            max_iteration_score = 0
        max_score = max(true_scores)
        max_score_history.append(max_score)
        if max_score >= threshold_score_to_terminate:
            logger.info('Termination! score:%.3f is above user defined threshold:%.3f' % (max_score, threshold_score_to_terminate))
            break
        else:
            logger.info('Max score in last iteration: %.3f    Global max score: %.3f' % (max_iteration_score, max_score))
            if draw_graphs is not None:
                if proposed_graphs:
                    logger.info('Current iteration')
                    draw_graphs(proposed_graphs)
                logger.info('Current status')
                draw_graphs(graphs)
        # policy for n_queries_to_oracle_per_iter and exploration_vs_exploitation
        if len(max_score_history) > 1:
            max_score_delta = max_score_history[-1] - max_score_history[-2]
            if max_score_delta <= 0 and curr_patience > 0:
                curr_patience -= 1
                logger.info('Lack of improvement detected  [patience:%d]'%curr_patience)
            elif max_score_delta <= 0 and curr_patience <= 0:
                curr_patience = patience
                logger.info('Lack of improvement detected  [patience:%d]  INCREASING exploration_vs_exploitation_factor AND INCREASING n_queries_to_oracle_per_iter'%curr_patience)
                n_queries_to_oracle_per_iter_increment_factor = 1.2
                exploration_vs_exploitation_factor = 10
            else:
                curr_patience = patience
                n_queries_to_oracle_per_iter_increment_factor = 0.08
                exploration_vs_exploitation_factor = 0.1
        else:
            max_score_delta = 0
            n_queries_to_oracle_per_iter_increment_factor = 1
            exploration_vs_exploitation_factor = 1
        exploration_vs_exploitation *= exploration_vs_exploitation_factor
        exploration_vs_exploitation = min(1, exploration_vs_exploitation)
        exploration_vs_exploitation = max(1e-4, exploration_vs_exploitation)
        score_estimator.set_exploration_vs_exploitation(exploration_vs_exploitation)
        logger.info('exploration_vs_exploitation:%.2e' % exploration_vs_exploitation)
        n_queries_to_oracle_per_iter *= n_queries_to_oracle_per_iter_increment_factor
        n_queries_to_oracle_per_iter = int(n_queries_to_oracle_per_iter)
        n_queries_to_oracle_per_iter = max(original_n_queries_to_oracle_per_iter, n_queries_to_oracle_per_iter)
        n_queries_to_oracle_per_iter = min(original_n_queries_to_oracle_per_iter * 20, n_queries_to_oracle_per_iter)
        logger.info('n_queries_to_oracle_per_iter:%d' % n_queries_to_oracle_per_iter)

        # update score_estimator
        score_estimator_start_time = time.process_time()
        score_estimator.fit(graphs, true_scores)
        score_estimator_end_time = time.process_time()
        score_estimator_elapsed_time = (score_estimator_end_time - score_estimator_start_time) / 60.0
        logger.info('Score estimator fitted on %d graphs in %.1f min' % (len(graphs), score_estimator_elapsed_time))

        # update part_importance_estimator
        part_importance_estimator_start_time = time.process_time()
        part_importance_estimator.fit(graphs, true_scores)
        part_importance_estimator_end_time = time.process_time()
        part_importance_estimator_elapsed_time = (part_importance_estimator_end_time - part_importance_estimator_start_time) / 60.0
        logger.info('Part importance  estimator fitted on %d graphs in %.1f min' % (len(graphs), part_importance_estimator_elapsed_time))

        # select small number (sample_size_to_perturb) of promising graphs for neighborhood expansion
        proposed_graphs, proposed_true_scores = sample(
            graphs,
            true_scores,
            sample_size_to_perturb,
            greedy_sample_vs_tournament_frac)

        # materialize neighborhood and select best candidates using score estimator
        iter_start_time = time.process_time()
        logger.info('From a biased draw of %d samples from %d graphs...' % (sample_size_to_perturb, len(graphs)))
        proposed_graphs, proposed_predicted_scores, all_n_estimator_queries = select_iterated_neighborhoods(
            proposed_graphs,
            graphs,
            true_scores,
            graphs,
            neighborhood_estimators,
            part_importance_estimator,
            feasibility_estimator,
            score_estimator,
            n_steps_driven_by_estimator,
            sample_size_to_perturb,
            n_queries_to_oracle_per_iter,
            parallelization_strategy,
            greedy_sample_vs_tournament_frac)

        if add_mutations_when_asking_oracle is True:
            mutated_proposed_graphs = mutate(proposed_graphs, graphs, part_importance_estimator, feasibility_estimator)
            logger.info('Added %d mutations to original %d proposed graphs'%(len(mutated_proposed_graphs),len(proposed_graphs)))
            proposed_graphs += mutated_proposed_graphs

        if monitor:
            monitor(i, proposed_graphs, graphs, score_estimator, all_n_estimator_queries, part_importance_estimator=part_importance_estimator)
        if len(proposed_graphs) == 0:
            break
        iter_end_time = time.process_time()
        iter_elapsed_time_m = (iter_end_time - iter_start_time) / 60.0
        iter_elapsed_time_h = iter_elapsed_time_m / 60.0
        logger.info('overall iteration time: %.1f min (%.1f h)' % (iter_elapsed_time_m, iter_elapsed_time_h))

    # final update with oracle
    num_proposed_graphs = len(proposed_graphs)
    if num_proposed_graphs:
        oracle_start_time = time.process_time()
        proposed_graphs, proposed_true_scores = elitism(
            proposed_graphs, oracle_func, frac_instances_to_remove_per_iter)
        max_iteration_score = max(proposed_true_scores)
        graphs += proposed_graphs[:]
        true_scores += proposed_true_scores[:]
        max_score = max(true_scores)
        oracle_end_time = time.process_time()
        oracle_elapsed_time = (oracle_end_time - oracle_start_time) / 60.0
        logger.info('Oracle evaluated on %d graphs in %.1f min' % (num_proposed_graphs, oracle_elapsed_time))
        logger.info('Max score in last iteration: %.3f' % (max_iteration_score))
        logger.info('Max score globally: %.3f' % (max_score))
        if draw_graphs is not None:
            if proposed_graphs:
                logger.info('Current iteration')
                draw_graphs(proposed_graphs)
            logger.info('Current status')
            draw_graphs(graphs)
    return graphs

def optimizer_setup(use_RandomForest_estimator=False,
                    use_Linear_estimator=False,
                    use_EI_estimator=False,
                    use_UCB_estimator=False,
                    use_KNN_stimator=False,
                    use_ANN_estimator=False,
                    ANN_estimator_hidden_layer_sizes=[100, 50],
                    n_estimators_ANN=10,
                    n_neighbors_KNN=5,
                    n_estimators=100,
                    exploration_vs_exploitation=0,
                    decomposition_score_estimator=None,
                    execute_estimator_concurrently=False,

                    use_feasibility_estimator=False,
                    decomposition_feasibility_estimator=None,
                    domain_graphs_feasibility_estimator=None,

                    decomposition_part_importance_estimator=None,

                    use_part_importance_graph_grammar=False,
                    n_neighbors_part_importance_graph_grammar=None,
                    conservativeness_part_importance_graph_grammar=1,
                    max_num_substitutions_part_importance_graph_grammar=None,
                    context_size_part_importance_graph_grammar=1,
                    decomposition_part_importance_graph_grammar=None,
                    domain_graphs_part_importance_graph_grammar=None,
                    fit_at_each_iteration_part_importance_graph_grammar=False,

                    use_adaptive_graph_grammar=False,
                    n_neighbors_adaptive_graph_grammar=None,
                    conservativeness_adaptive_graph_grammar=1,
                    max_num_substitutions_adaptive_graph_grammar=None,
                    context_size_adaptive_graph_grammar=1,
                    part_size_adaptive_graph_grammar=4,
                    decomposition_adaptive_graph_grammar_base=None,
                    decomposition_adaptive_graph_grammar_approx=None,

                    use_edge_crossover=False,
                    n_neighbors_edge_crossover=10,
                    n_edge_edge_crossover=1,
                    tournament_size_factor_edge_crossover=10,
                    size_n_std_to_accept_edge_crossover=2,

                    use_break_crossover=False,
                    n_neighbors_break_crossover=1,
                    n_edge_break_crossover=1,
                    min_size_break_crossover=5,
                    max_size_break_crossover=.5,
                    tournament_size_factor_break_crossover=3,
                    size_n_std_to_accept_break_crossover=1,

                    use_cycle_crossover=False,
                    n_neighbors_cycle_crossover=10,
                    n_cycle_cycle_crossover=1,
                    n_edges_per_cycle_cycle_crossover=1,
                    tournament_size_factor_cycle_crossover=10,
                    size_n_std_to_accept_cycle_crossover=2,

                    use_double_edge_crossover=False,
                    n_neighbors_double_edge_crossover=10,
                    n_double_edges_double_edge_crossover=10,
                    tournament_size_factor_double_edge_crossover=10,
                    size_n_std_to_accept_double_edge_crossover=2,

                    use_edge_swapping=False,
                    n_neighbors_edge_swapping=None, n_edge_swapping=1,

                    use_edge_label_swapping=False,
                    n_neighbors_edge_label_swapping=None,
                    n_edge_label_swapping=1,

                    use_edge_label_mutation=False,
                    n_neighbors_edge_mutation=None, n_edge_mutation=1,

                    use_edge_move=False,
                    n_neighbors_edge_move=None, n_edge_move=1,

                    use_edge_removal=False,
                    n_neighbors_edge_removal=None, n_edge_removal=1,

                    use_edge_addition=False,
                    n_neighbors_edge_addition=None, n_edge_addition=1,

                    use_edge_expand=False,
                    n_neighbors_edge_expand=None, n_edge_expand=1,

                    use_edge_contract=False,
                    n_neighbors_edge_contract=None, n_edge_contract=1,

                    use_node_label_swapping=False,
                    n_neighbors_node_label_swapping=None,
                    n_node_label_swapping=1,

                    use_node_label_mutation=False,
                    n_neighbors_node_mutation=None, n_node_mutation=1,

                    use_node_removal=False,
                    n_neighbors_node_removal=None, n_node_removal=1,

                    use_node_addition=False,
                    n_neighbors_node_addition=None, n_node_addition=1,

                    use_node_smooth=False,
                    n_neighbors_node_smooth=None, n_node_smooth=1):
    """optimizer_setup."""
    part_importance_estimator = PartImportanceEstimator(
        decompose_func=decomposition_part_importance_estimator)

    neighborhood_estimators = []

    if use_edge_crossover:
        nec = NeighborhoodEdgeCrossover(
            n_edges=n_edge_edge_crossover,
            n_neighbors=n_neighbors_edge_crossover,
            tournament_size_factor=tournament_size_factor_edge_crossover,
            size_n_std_to_accept=size_n_std_to_accept_edge_crossover)
        neighborhood_estimators.append(nec)

    if use_break_crossover:
        nbc = NeighborhoodBreakCrossover(
            n_edges=n_edge_break_crossover,
            min_size = min_size_break_crossover,
            max_size = max_size_break_crossover,
            n_neighbors=n_neighbors_break_crossover,
            tournament_size_factor=tournament_size_factor_break_crossover,
            size_n_std_to_accept=size_n_std_to_accept_break_crossover)
        neighborhood_estimators.append(nbc)

    if use_cycle_crossover:
        ncc = NeighborhoodCycleCrossover(
            n_cycles=n_cycle_cycle_crossover,
            n_edges_per_cycle=n_edges_per_cycle_cycle_crossover,
            n_neighbors=n_neighbors_cycle_crossover,
            tournament_size_factor=tournament_size_factor_cycle_crossover,
            size_n_std_to_accept=size_n_std_to_accept_cycle_crossover)
        neighborhood_estimators.append(ncc)

    if use_double_edge_crossover:
        ndec = NeighborhoodDoubleEdgeCrossover(
            n_double_edges=n_double_edges_double_edge_crossover,
            n_neighbors=n_neighbors_double_edge_crossover,
            tournament_size_factor=tournament_size_factor_double_edge_crossover,
            size_n_std_to_accept=size_n_std_to_accept_double_edge_crossover)
        neighborhood_estimators.append(ndec)

    if use_edge_swapping:
        nes = NeighborhoodEdgeSwap(
            n_edges=n_edge_swapping, n_neighbors=n_neighbors_edge_swapping)
        neighborhood_estimators.append(nes)

    if use_edge_label_swapping:
        nels = NeighborhoodEdgeLabelSwap(
            n_edges=n_edge_label_swapping, n_neighbors=n_neighbors_edge_label_swapping)
        neighborhood_estimators.append(nels)

    if use_edge_label_mutation:
        nelm = NeighborhoodEdgeLabelMutation(
            n_edges=n_edge_mutation, n_neighbors=n_neighbors_edge_mutation)
        neighborhood_estimators.append(nelm)

    if use_edge_move:
        nem = NeighborhoodEdgeMove(
            n_edges=n_edge_move, n_neighbors=n_neighbors_edge_move)
        neighborhood_estimators.append(nem)

    if use_edge_removal:
        ner = NeighborhoodEdgeRemove(
            n_edges=n_edge_removal, n_neighbors=n_neighbors_edge_removal)
        neighborhood_estimators.append(ner)

    if use_edge_addition:
        nea = NeighborhoodEdgeAdd(
            n_edges=n_edge_addition, n_neighbors=n_neighbors_edge_addition)
        neighborhood_estimators.append(nea)

    if use_edge_expand:
        nee = NeighborhoodEdgeExpand(
            n_edges=n_edge_expand, n_neighbors=n_neighbors_edge_expand)
        neighborhood_estimators.append(nee)

    if use_edge_contract:
        nec = NeighborhoodEdgeContract(
            n_edges=n_edge_contract, n_neighbors=n_neighbors_edge_contract)
        neighborhood_estimators.append(nec)

    if use_node_label_swapping:
        nnls = NeighborhoodNodeLabelSwap(
            n_nodes=n_node_label_swapping,
            n_neighbors=n_neighbors_node_label_swapping)
        neighborhood_estimators.append(nnls)

    if use_node_label_mutation:
        nnlm = NeighborhoodNodeLabelMutation(
            n_nodes=n_node_mutation, n_neighbors=n_neighbors_node_mutation)
        neighborhood_estimators.append(nnlm)

    if use_node_removal:
        nnr = NeighborhoodNodeRemove(
            n_nodes=n_node_removal, n_neighbors=n_neighbors_node_removal)
        neighborhood_estimators.append(nnr)

    if use_node_addition:
        nna = NeighborhoodNodeAdd(
            n_nodes=n_node_addition, n_neighbors=n_neighbors_node_addition)
        neighborhood_estimators.append(nna)

    if use_node_smooth:
        nns = NeighborhoodNodeSmooth(
            n_nodes=n_node_smooth, n_neighbors=n_neighbors_node_smooth)
        neighborhood_estimators.append(nns)

    if use_part_importance_graph_grammar:
        npigge = NeighborhoodPartImportanceGraphGrammar(
            decomposition_function=decomposition_part_importance_graph_grammar,
            context=context_size_part_importance_graph_grammar,
            count=conservativeness_part_importance_graph_grammar,
            filter_max_num_substitutions=max_num_substitutions_part_importance_graph_grammar,
            frac_nodes_to_select=.5,
            n_neighbors=n_neighbors_part_importance_graph_grammar,
            fit_at_each_iteration=fit_at_each_iteration_part_importance_graph_grammar,
            domain_graphs=domain_graphs_part_importance_graph_grammar)
        # if fitting is not done iteratively then fit once on the domain_graphs here
        if fit_at_each_iteration_part_importance_graph_grammar is False:
            # fitting the grammar always adds the input graphs to the domain_graphs
            # so here we will add no novel graphs to the domain_graphs
            npigge.fit_grammar([])
        neighborhood_estimators.append(npigge)

    if use_adaptive_graph_grammar:
        ane = NeighborhoodAdaptiveGraphGrammar(
            base_decomposition_function=decomposition_adaptive_graph_grammar_base,
            approximate_decomposition_function=decomposition_adaptive_graph_grammar_approx,
            context=context_size_adaptive_graph_grammar,
            count=conservativeness_adaptive_graph_grammar,
            filter_max_num_substitutions=max_num_substitutions_adaptive_graph_grammar,
            n_neighbors=n_neighbors_adaptive_graph_grammar,
            ktop=part_size_adaptive_graph_grammar,
            enforce_connected=True)
        neighborhood_estimators.append(ane)

    score_estimators = []
    if use_UCB_estimator:
        score_estimator = GraphUpperConfidenceBoundEstimator(
            decomposition_funcs=decomposition_score_estimator,
            exploration_vs_exploitation=exploration_vs_exploitation)
        score_estimators.append(score_estimator)

    if use_RandomForest_estimator:
        score_estimator = GraphRandomForestScoreEstimator(
            decomposition_funcs=decomposition_score_estimator,
            n_estimators=n_estimators,
            exploration_vs_exploitation=exploration_vs_exploitation)
        score_estimators.append(score_estimator)

    if use_Linear_estimator:
        score_estimator = GraphLinearScoreEstimator(
            decomposition_funcs=decomposition_score_estimator,
            n_estimators=n_estimators,
            exploration_vs_exploitation=exploration_vs_exploitation)
        score_estimators.append(score_estimator)

    if use_EI_estimator:
        score_estimator = GraphExpectedImprovementEstimator(
            decomposition_funcs=decomposition_score_estimator,
            exploration_vs_exploitation=exploration_vs_exploitation)
        score_estimators.append(score_estimator)

    if use_ANN_estimator:
        score_estimator = GraphNeuralNetworkScoreEstimator(
            hidden_layer_sizes=ANN_estimator_hidden_layer_sizes,
            decomposition_funcs=decomposition_score_estimator,
            n_estimators=n_estimators_ANN,
            exploration_vs_exploitation=exploration_vs_exploitation)
        score_estimators.append(score_estimator)

    if use_KNN_stimator:
        score_estimator = GraphNearestNeighborScoreEstimator(
            n_neighbors=n_neighbors_KNN,
            decomposition_funcs=decomposition_score_estimator,
            n_estimators=n_estimators,
            exploration_vs_exploitation=exploration_vs_exploitation)
        score_estimators.append(score_estimator)

    score_estimator = EnsembleScoreEstimator(score_estimators, execute_concurrently=execute_estimator_concurrently)
    score_estimator.set_exploration_vs_exploitation(exploration_vs_exploitation)

    if use_feasibility_estimator:
        feasibility_estimator = FeasibilityEstimator(
            decomposition_funcs=decomposition_feasibility_estimator)
        logger.info('Fitting feasibility estimator on %d graphs' % len(domain_graphs_feasibility_estimator))
        feasibility_estimator.fit(domain_graphs_feasibility_estimator)
    else:
        feasibility_estimator = None
    return neighborhood_estimators, score_estimator, part_importance_estimator, feasibility_estimator


p=0.5

D = { (0,0) : 1 , (1,0) : 1 , (1,1) : p }

import numpy as np
import scipy.special
import scipy.integrate as integrate
import matplotlib.pyplot as plt
import numpy as np
from os import walk, system
from scipy import fft
import matplotlib
matplotlib.rcParams['text.usetex'] = True
from pickle import dumps, dump, load, loads
import pickle
import cv2
import sys, os
from numpy.lib import vectorize
sys.path.append(os.path.realpath(".."))
from lib.exp import Exp, FittedObj, gencolor, glabels, plotlabels, gColors, putlabel
import matplotlib
matplotlib.rcParams['text.usetex'] = True

def contrast( _path = "./mgyi/mgyi_1"):
    for dirpath, dirname, filenams in walk(_path):
        for filename in filter( lambda s : "tif" in s , filenams):
            print(filename)
            DIRname = dirpath.split("/")[-1]
            system( f"echo y | sh ./scripts/impr.sh {dirpath}/{filename} ./tif3/{DIRname}/{filename}")

def loadf(_path):
    print(_path)
    return cv2.imread(f'{_path}', cv2.IMREAD_GRAYSCALE).astype(np.float128)

def time (_path ):
    return int(_path.split('_')[1])

def norm(X):
    return np.linalg.norm(X, 'fro' )

def picklize():
    _path = "./tif/"
    _keyword = "YFP" #"Phasefast"
    _dict = {}

    for dirpath, dirname, filenams in walk(_path):
        DIRname = dirpath.split("/")[-1]
        _dict[DIRname] = [  ]
        for _filename in filter( lambda s : ("tif" in s ) and ( _keyword in s), filenams):
            __file = "{0}/{1}".format( dirpath, _filename )
            print(__file)
            _dict[DIRname].append( ( time(__file), norm(loadf( __file ))))
        print(_dict)

    with open(f"{_keyword}.pkl", 'wb') as handle:
        dump(_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)

if __name__ == "__main__":

    contrast( _path = "./mgyi_3/" )

    exit(0)

    _keyword = "YFP"

    _dict = None
    with open(f"{_keyword}.pkl", 'rb') as handle:
        _dict = pickle.load(handle)
    # print(_dict)

    propb = np.zeros((50,250))
    # probexcpt = np.zeros((50,250))

    for _key, dictvec in _dict.items():
        print("----" + str( _key) + "-----")
        if len(dictvec) > 0:
            vec = (np.array( sorted( dictvec, key=lambda x: x[0]) )).T
            vec[1] /= vec[1][0]

            # print(vec.T)

            for timetick, mulsize in vec.T:
                if timetick < 110:
                    propb[int(mulsize)][int(timetick)] += 1

    from scipy.signal import find_peaks

    for _key, dictvec in _dict.items():
        print("----" + str( _key) + "-----")
        if len(dictvec) > 0:
            vec = (np.array( sorted( dictvec, key=lambda x: x[0]) )).T
            vec[1] /= vec[1][0]
            # print(vec.T)

            for timetick, mulsize in vec.T:
                peaks, _ = find_peaks(  propb[int(mulsize)] )
                np.diff(peaks)
                if len(peaks) > 1 :
                    if abs(timetick - peaks[-1]) < 3:
                    # if timetick > 2 *  np.average(propb[int(mulsize)]):
                        print( f" anomaloy :  {_key} , {timetick}, {mulsize}")

    print(propb[7])


# FUNCTIONS
# Defining a function
def greet():
    print("hello")
    print("darling")

# defining with arguments or parameters
def add_sub(x, y):
    c = x+y
    d = x-y
    return c, d

# calling a function
greet()
# we can call this multiple times
# function does two things  execute a task or return values
# example for returning values
result = add_sub(5, 4)
# when 2 values are returned we need to have two variables to print these 2 values
result1, result2 = add_sub(5, 4)
print(result)
print(result1, result2)

# Function Arguments
def update(x):
    x = 8
    print(x)
update(10) #o/p is 8
a = 10
update(a) # passing 10 not a
print("a ",  a)
# call by value and call by reference
# cav - when you are calling a function with value
# car - when you change the value of x then it will change the value of a
# In python everything is an object and neither cav nor car apply
# in the above function the id of a is passed to x, so both the ids will be same before passing a value to x
# After passing the value the id/address is changed
# since int is immutable after passing a new value id is changed
# same goes for str, but when mutable objects like list are used and values are updated the id doesn't change
def up(lst):
    print(id(lst))
    lst[1] = 25
    print((id(lst)))
    print(lst)

a = [1, 5, 6]
print(id(a))
up(a)
print("a", a)

# below is the o/p
# 2200931841792
# 2200931841792
# 2200931841792
# [1, 25, 6]
# a [1, 25, 6]

# types of arguments
# Formal Arguments - defined arguments and Actual Arguments - arguments that we pass to a function
# Actual Arguments - Position, keyword, default and variable length
# position
def person(name, age):
    print(name)
    print(age)
# position based arguments
person('vegeta', 8)
# if you dont know the sequence
# we use keyword arguments
person(age=8, name='goku')
# default
# imagine if the function is defined  like this
# def person(name, age=8) -- here age default is 8 and if you don't pass any value to the argument then it takes 8
# in the above case if you send an argument that is not default it will take the argument that has been sent

# Variable length argument
# Consider a scenario where you need to write a function to add all the given number
# in this case we cant just pass 2 arguments, we use variable length arguments
def sum(*b):
    # above while passing arguments we can give a, *b as at least one value will definitely be passed
    # by *b, we define b as tuple
    c = 0
    for i in b:
        c = c + i
    print(c)

sum(5, 6, 8, 9, 2)

# keyworded variable length arguments
# sending multiple arguments with keywords
def person(name, **data):
    print(name)
    for i, j in data.items():
        print(i, j)
        # since data here is no more a tuple it is a key value pair we have 2 values
        # also we cannot just give data we need to use function called items

person('navin', age=28, city='mumbai', mob=9848456)

# Global and Local variables
# we can create variables inside and outside of functions
# Global - variable outside the function, Local - variable used inside the function
# preference will be given to the local variable inside the function
# cannot access local variable outside the function
# but, we can access the global variable inside any function
# Even though we can use the global variable inside a function we cannot change the value of it without explictly defining it
# we define global variable inside a function like global variable_name

a = 10
def som():
    # global a
    # after the above we cannot define a local variable called a inside the function
    # when you want to change the global variable inside a function and also define a local variable
    # x = globals() returns all the global variables
    x = globals()['a'] # global variable with name a
    # to verify check the id of x and outside variable a both will be same as it points to the global variable a
    a = 9
    print("initial value of gv: ", x)
    print("in fun local variable", a)
    # to change the global variable without affecting the local use below
    globals()['a'] = 15

som()
print("outside changed / actual global variable", a)

# passing list as an input for a function
def count(lst):
    even = 0
    odd = 0
    for i in lst:
        if i % 2 == 0:
            even += 1
        else:
            odd += 1
    return even, odd

lst = [14, 52, 36, 96, 85, 47, 89]

e, o = count(lst)

print(e)
print(o)
print("even : {} and odd: {}".format(e, o))
# in the above print as we are having string arguments to convert set braces into the values of o,e we use string function format

# take 10 names from user and then count and display users name with more than 5 characters
def count_name(lst):
    lnames = 0
    for x in lst:
        if len(x) > 5:
            print(x)
            lnames += 1
    return lnames

l = int(input("enter number of users: "))
lit = []
for i in range(l):
    lit.append(input("enter user names:"))
print(lit)

name = count_name(lit)
print(name)


from entites import *

if __name__ == '__main__':
    brady = Player("Tom Brady")
    lamar = Player("Lamar Jackson")
    juju  = Player("JuJu Smith-Schuster")
    dede  = Player("Dede Westbrook")
    kam   = Player("Alvin Kamara")
    obj   = Player("Odell Beckham Jr.")

    en_1 = User("Bill", [brady, lamar, juju])
    en_2 = User("JonRoss", [dede, kam])
    en_3 = User("Sam", [obj])

    en_1.add_preferences([kam, brady, obj, juju, lamar, dede])
    en_2.add_preferences([juju, obj, kam, lamar, brady, dede])
    en_3.add_preferences([juju, lamar, kam, brady, dede, obj])

    print(en_1)
    print(en_2)
    print(en_3)


import sys

def main():
    d = {}
    n = sys.stdin.readline()
    numbers = list(map(int, sys.stdin.readline().split()))
    for a in numbers:
        chk = d.get(a)
        if chk is None:
            d[a] = 1
        else:
            d[a] = chk + 1
    freq = list(d.values())
    mf = max(freq)
    res = []
    for (k, v) in d.items():
        if v == mf:
            res += [k]
    print(max(res))

if __name__ == "__main__":
    main()


from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

import uvicorn

from qa import qa

origins = [
    "*",
]

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class QAHuggingFace(BaseModel):
    question: str
    context: str

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/qa")
async def root(qa_hf: QAHuggingFace):
    question = qa_hf.question
    context = qa_hf.context
    result = qa(question, context)
    return {"result": result}

if __name__ == "__main__":
    uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True)


import pandas
import numpy as np
from sklearn.linear_model import LinearRegression
from scipy import stats

class regression():
	def __init__(self, FerData, Time):
		self.FerData = FerData
		self.Time = Time
		self.grate = 0
		self._P = 0
		self.sig_P = 0
		self.alpha = 0
		self._I = 0
		self.sig_I = 0

	def bootstrap(self):
		index = np.random.choice(len(self.FerData.iloc[:, 2]), len(self.FerData.iloc[:, 2]))
		self.FerData = self.FerData.iloc[index, :]

	def reg(self):
		y = np.log(self.FerData.iloc[:, 2]) - np.log(self.FerData.iloc[:, 1])
		y = y.values.reshape(-1, 1)
		X = np.array([self.Time for i in range(len(y))])
		X = X.reshape(-1, 1)
		clf = LinearRegression(fit_intercept=False)
		clf.fit(X, y)
		y_hat = clf.predict(X)
		self.grate = round(clf.coef_[0][0], 4)
		self.P = y - y_hat
		self.sig_P = round(np.std(self.P, ddof=1), 4)
		y2 = self.FerData.iloc[:, 3] / self.FerData.iloc[:, 2]
		y2 = y2.values.reshape(-1, 1)
		self.alpha = round(pow(np.prod(y2), 1.0 / len(y2)), 4)
		y_hat2 = self.FerData.iloc[:, 2] * self.alpha
		y_hat2 = y_hat2.values.reshape(-1, 1)
		exp_I = self.FerData.iloc[:, 3].values.reshape(-1, 1) / y_hat2
		self._I = np.log(exp_I)
		self.sig_I = round(np.std(self._I, ddof=1), 4)


from azure.mgmt.loadtestservice import LoadTestClient
from azure.mgmt.loadtestservice.models import LoadTestResource

# for managing authentication and authorization can be installed from pypi, follow:
# https://pypi.org/project/azure-identity/ using DefaultAzureCredentials, read more at:
# https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
from azure.identity import DefaultAzureCredential

# importing os and dotenv for managing and loading environment variables
import os
from dotenv import load_dotenv

# Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, SUBSCRIPTION_ID, RESOURCE_GROUP

# loading dotenv file
load_dotenv()

SUBSCRIPTION_ID = os.environ["SUBSCRIPTION_ID"]
RESOURCE_GROUP = os.environ["RESOURCE_GROUP"]

# setting up management client
mgmt_client = LoadTestClient(
    credential=DefaultAzureCredential(),
    subscription_id=SUBSCRIPTION_ID
)

# creating a new loadtest resource
result = mgmt_client.load_tests.create_or_update(
    resource_group_name=RESOURCE_GROUP,
    load_test_name="new-loadtest-python-sdk",
    load_test_resource=LoadTestResource(
        location="eastus",
        description="New LoadTest Resource created via Python SDK"
    )
)

# getting dataplane url from loadtest
print(result.data_plane_uri)


import base64
import cv2
import math
import socket

SERVER_IP = '127.0.0.1'

def getVideoFrame():
    grabbed, frame = camera.read()  # grab the current frame
    frame = cv2.resize(frame, (640, 480))  # resize the frame
    encoded, buffer = cv2.imencode('.jpg', frame)
    jpg_as_text = base64.b64encode(buffer)
    return jpg_as_text  # output byte array

def generatePacket(data, seq_number):
    packet_dimension = 4096 - 8  # Byte dimension of packet
    # number of packet to be generated to contain all data
    packet_number = math.ceil(len(data)/packet_dimension)

    packets = []
    for i in range(0, packet_number):
        # Genrate packet header
        # Structure
        # |Sequence Number(2)|Packet Number(2)|Current Packet(2)|Data(packet_dimension - 8)|
        header = seq_number.to_bytes(
            2, "little", signed=False) + packet_number.to_bytes(
            2, "little", signed=False) + i.to_bytes(2, "little", signed=False)

        # Split data into packets
        start = i * packet_dimension
        end = start + packet_dimension
        packet = header + data[start:end]
        packets.append(packet)

        print("Header| " + " Seq: " + str(seq_number) + " | Total: " +
              str(packet_number) + " | Curr: " + str(i) + "|", end ='\r')

    return packets

# MAIN
# Init
camera = cv2.VideoCapture(0)  # init the camera
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
seq_number = 0
print("CLIENT STARTED")

# Loop
while True:
    img = getVideoFrame()
    data = generatePacket(img, seq_number)

    for i in data:
        clientsocket.sendto(i, (SERVER_IP, 7777))

    seq_number += 1


#!/usr/bin/env python3
from typing import *

def solve(n: int, a: List[int], b: List[int], c: List[int]) -> int:
    dp = [[-1, -1, -1] for _ in range(n + 1)]
    dp[0] = [0, 0, 0]
    for i in range(n):
        dp[i + 1][0] = a[i] + max(dp[i][1], dp[i][2])
        dp[i + 1][1] = b[i] + max(dp[i][2], dp[i][0])
        dp[i + 1][2] = c[i] + max(dp[i][0], dp[i][1])
    return max(dp[n])

def main() -> None:
    n = int(input())
    a = list(range(n))
    b = list(range(n))
    c = list(range(n))
    for i in range(n):
        a[i], b[i], c[i] = map(int, input().split())
    ans = solve(n, a, b, c)
    print(ans)

if __name__ == '__main__':
    main()


import socket

HOST = "127.0.0.1"
PORT = 5052
ENCODING = "ascii"

if __name__ == '__main__':
    with socket.socket(socket.AF_INET , socket.SOCK_STREAM) as s:
        s.bind((HOST , PORT))
        s.listen()

        conn,addr = s.accept()
        with conn:
            print(f"[CONNECTED] {addr}")
            while True:
                data1 = conn.recv(1024).decode(ENCODING)
                if not data1:
                    break
                print(f"[RECEIVED] {data1}")
                data2 = conn.recv(1024).decode(ENCODING)
                if not data2:
                    break
                print(f"[RECEIVED] {data2}")
                try:
                    result = int(data1) + int(data2)
                    conn.send(str(result).encode(ENCODING))
                except:
                    conn.send("Invalid Arguments".encode(ENCODING))


import matplotlib.pylab as plt
import numpy as np

x = np.linspace(-2, 2, 200)
plt.plot(x, np.cosh(x))
plt.xlabel('Angle [rad]')
plt.ylabel('cosh(x)')
plt.axis('tight')
plt.show()


import pytest
import numpy as np

from dpipe.torch import to_np
from dpipe.train import ConsoleLogger
from dpipe.torch import inference_step
from dpipe.train.policy import Schedule
from dpipe.prototypes import GradientsAccumulator, LossAccumulator, train_multiple_strategies

@pytest.mark.parametrize("use_hf", [False, True])
def test_single_forward_loss(forward_strategy_all_ds_batch, task_data, use_hf):
    task_inputs, task_gt = task_data
    s, model = forward_strategy_all_ds_batch(use_hf, n_targets=1)
    s.start_epoch(0)
    pred = inference_step(task_inputs[..., None], architecture=model)[..., 0]
    # loss weight == 2
    true_loss = 2 * np.mean((pred - task_gt) ** 2)
    first_batch_loss = to_np(s.process_batch(0, 0))
    assert np.isclose(true_loss, first_batch_loss)
    second_batch_loss = to_np(s.process_batch(0, 1))
    assert np.isclose(true_loss, second_batch_loss)

@pytest.mark.parametrize("use_hf", [False, True])
@pytest.mark.parametrize("accum", [GradientsAccumulator, LossAccumulator])
def test_single_optimization(accum, use_hf, single_strategy, task_data):
    lr = 0.1
    task_inputs, task_gt = task_data
    s, model = single_strategy(accum, use_hf, lr=lr, n_targets=1)
    x_data, y_data = task_data
    # test several iterations
    s.start_epoch(0)
    for batch_index in range(4):
        init_bias = to_np(model.bias).copy()
        init_weight = to_np(model.weight).copy()
        pred = inference_step(x_data[..., None], architecture=model)[..., 0]
        s.process_batch(0, batch_index)
        # correct gradients
        w_grad = 4 / len(x_data) * np.dot(x_data, pred - y_data)
        b_grad = 4 * np.mean((pred - y_data))
        # check gradients
        atol = 1e-3
        assert np.isclose(to_np(model.bias.grad), b_grad, atol=atol)
        assert np.isclose(to_np(model.weight.grad), w_grad, atol=atol)
        # check updates
        assert np.isclose(to_np(model.weight), init_weight - lr * w_grad, atol=atol)
        assert np.isclose(to_np(model.bias), init_bias - lr * b_grad, atol=atol)

@pytest.mark.parametrize("use_hf", [True, False])
@pytest.mark.parametrize("accum", [GradientsAccumulator, LossAccumulator])
def test_single_strategy_lifecycle(accum, use_hf, single_strategy):
    weights = []

    class Logger(ConsoleLogger):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

        def policies(self, policies: dict, step: int):
            nonlocal weights
            weights.append(policies['weight'])
            return super().policies(policies, step)

    # check policy update
    lr = Schedule(initial=1, epoch2value_multiplier={1: 2, 2: 3, 3: 4, 4: 5})
    weight = Schedule(initial=1, epoch2value_multiplier={1: 2, 2: 3, 3: 4, 4: 5})
    s, _ = single_strategy(accum, use_hf, lr=lr, weight=weight, logger=Logger(), n_targets=1)
    counter = 0

    def validate():
        nonlocal counter
        counter += 1
        return {'counter': counter}

    s.strategies[0].validate_step = validate
    train_multiple_strategies(s, n_epochs=5)
    assert counter == 5
    assert weights == [1, 2, 6, 24, 120]

    for param_group in s.optimization_policy.optimizer.param_groups:
        assert param_group['lr'] == 120

@pytest.mark.parametrize("use_hf", [True, False])
@pytest.mark.parametrize("accum", [GradientsAccumulator, LossAccumulator])
def test_double_grad_propagation(accum, use_hf, single_strategy, task_data):
    lr = 1
    s, model = single_strategy(accum, use_hf, lr=lr, n_targets=0)
    x_data, y_data = task_data
    s.start_epoch(0)

    # test several iterations
    for batch_index in range(4):
        init_bias = to_np(model.bias).copy()
        init_weight = to_np(model.weight).copy()

        s.process_batch(0, batch_index)
        # correct gradients
        atol = 1e-4
        b_grad = 0.
        w_grad = 4 * init_weight * np.mean((x_data - y_data) ** 2)
        # check gradients
        assert np.isclose(to_np(model.bias.grad), b_grad, atol=atol)
        assert np.isclose(to_np(model.weight.grad), w_grad, atol=atol)
        # check updates
        assert np.isclose(to_np(model.weight), init_weight - lr * w_grad, atol=atol)
        assert np.isclose(to_np(model.bias), init_bias - lr * b_grad, atol=atol)


from PIL import Image

fileName = "monMons.txt"
length = 3
base = Image.open("baseMon.png")
pool = []

class mon(object):
    def __init__(self, color):
        self.color = color # As a integer tupple of 3 numbers

def importMons(fileName):
    monsArray = []
    with open(fileName, 'r') as f:
        fileContent = f.readlines()
        for i in range(len(fileContent)):
            line = (fileContent[i].strip("\n")).split(",") # splits the line into the core components
            if line[0][0] == "#": continue #ignores comments (denoted by "#" at the start)

            if(len(line) == length):
                monsArray.append(mon((int(line[0]), int(line[1]), int(line[2]) )))
            else:
                print("Line ", i+1, " is invalid")
    return monsArray

def writeMon(monData):
    strMonData = []
    for i in range(len(monData)):
        strMonData.append(str(monData[i]))

    with open(fileName, "a") as f:
        f.write(",".join(strMonData) + "\n")

#Let's create some monMons! ---------------------------------------------------
def getParents():
    p1,p2 = randint(0,len(pool)-1),randint(0,len(pool)-1)
    #Gets rid of any duplicates. We can't have any babies being born out of 1 person
    while p1 == p2:
        p2 = randint(0,len(pool)-1)

    return [pool[p1],pool[p2]]

def createChild():
    parents = getParents()

    childData = [
        parents[randint(0,1)].color[0],
        parents[randint(0,1)].color[1],
        parents[randint(0,1)].color[2],
    ]

    #Mutation of the child
    for i in range(3):
        chance = randint(1,chanceOfMutation)
        if chance == 1:
            chance = randint(-2,2)
            childData[i] += chance

    writeMon(childData)
    return mon(*childData)

def createChildren(numOfChildren):
    #Creates new children for the pool
    newMons = []
    for i in range(numOfChildren):
        newMons.append(createChild())
    return newMons
# Let's create some townsfolk! ------------------------------------------------
def paintMon(mon):
    img = Image.open("baseMon.png")
    for y in range(img.height):
        for x in range(img.width):
            if img.getpixel((x, y)) == (0,0,0):
                img.putpixel((x, y), (mon.color[0], mon.color[1], mon.color[2]))
    return img

#Main program start -----------------------------------------------------------
pool = importMons(fileName)

for i in range(len(pool)):
    paintMon(pool[i]).save("images/{}.png".format(i))


# -*- coding: utf-8 -*-
import scrapy
from isbn.items import IsbnLoader

class AmazonSpider(scrapy.Spider):
    name = "amazon"
    allowed_domains = ["amazon.cn"]
    custom_settings = {
        'COOKIES_ENABLED': True,
        'COOKIES_DEBUG': True,
        'DOWNLOAD_DELAY': 1,
        'DOWNLOADER_MIDDLEWARES': {
            'isbn.middlewares.RandomUserAgentMiddleware': 300,
        }
    }

    def __init__(self, isbn_list, *args, **kwargs):
        super(AmazonSpider, self).__init__(*args, **kwargs)
        self.isbn_list = isbn_list

    def start_requests(self):
        for isbn in self.isbn_list:
            url = f'https://www.amazon.cn/s/field-keywords={isbn}'
            yield scrapy.Request(url, meta={'isbn': isbn}, callback=self.parse)

    def parse(self, response):
        for div in response.xpath("//div[@class='s-item-container']"):
            url = div.xpath(".//a[contains(@class, 's-color-twister-title-link')]/@href").extract_first()
            print("url", url)
            if url:
                yield scrapy.Request(url=url, meta=response.meta, callback=self.parse_book_page)

    def parse_book_page(self, response):
        import isbnlib
        il = IsbnLoader(selector=response)
        il.add_xpath('title', ".//span[@id='productTitle' or @id='ebooksProductTitle']/text()")
        il.add_xpath('author', ".//span[@class='author notFaded']/a/text()")
        il.add_value('isbn', isbnlib.mask(response.meta['isbn']))
        il.add_xpath('publish', "//li[b/text()='出版社:']/text()")
        item = il.load_item()
        yield item

def from_file(filename):
    with open(filename) as f:
        url_list = f.readlines()
    return url_list

def main():
    from scrapy.crawler import CrawlerProcess
    from scrapy.utils.project import get_project_settings
    import re

    url_list = [
        # QQ 扫码得到的链接
        # "http://qm.qq.com/cgi-bin/result?r=9787302444060&p=i&v=7.0.1.407",
    ]
    # or from file
    url_list = set(url_list + from_file('list.txt'))
    isbn_list = [re.search('r=(\d*)', url).group(1) for url in url_list]
    print(isbn_list)
    settings = {**get_project_settings(), 'FEED_FORMAT': 'csv', 'FEED_URI': 'books.csv'}
    process = CrawlerProcess(settings)
    process.crawl(AmazonSpider, isbn_list)
    process.start()
    process.stop()

if __name__ == '__main__':
    main()