from keras.preprocessing import image
from keras.preprocessing.image import load_img, img_to_array
from keras import models

import numpy as np
import matplotlib.pyplot as plt
import os

img_path = 'cats_and_dogs_small/test/cats/cat.1502.jpg'
model_path = '02_cats_and_dogs_small_2.h5'

now_path = os.getcwd()
img_dir = os.path.join(now_path, img_path)
model_dir = os.path.join(now_path, model_path)

model = models.load_model(model_dir)
model.summary()

img = load_img(img_dir, target_size=(150, 150))
img_tensor = img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.

layer_inputs = model.input
layer_outputs = [layer.output for layer in model.layers[:8]]

activation_model = models.Model(inputs=layer_inputs, outputs=layer_outputs)

activations = activation_model.predict(img_tensor)

layer_names = []
for layer in model.layers[:8]:
    layer_names.append(layer.name)

images_per_row = 16

for layer_name, layer_activation in zip(layer_names, activations):
    n_features = layer_activation.shape[-1]
    size = layer_activation.shape[1]

    n_cols = n_features // images_per_row
    display_grid = np.zeros((size * n_cols, images_per_row * size))

    for col in range(n_cols):
        for row in range(images_per_row):
            channel_image = layer_activation[0, :, :, col * images_per_row + row]
            channel_image -= channel_image.mean()
            channel_image /= channel_image.std()
            channel_image *= 64
            channel_image += 128
            channel_image = np.clip(channel_image, 0, 255).astype('uint8')
            display_grid[col * size : (col + 1) * size, row * size : (row + 1) * size] = channel_image

    scale = 1. / size
    plt.figure(figsize=(scale * display_grid.shape[1], scale * display_grid.shape[0]))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')

plt.show()


# -*- coding: utf-8 -*-
# https://mathmod.deviantart.com/art/Pseudo-Hopf-Tori-565531249
import os
from math import pi, atan2, asin, sqrt, cos, sin
import numpy as np
import pyvista as pv
import quaternion

def quaternion2hab(q):
    "Quaternion to heading, attitude, bank"
    c = 180 / pi
    t = q.x*q.y + q.z*q.w
    if t > 0.499: # north pole
        heading = 2 * atan2(q.x, q.w) * c
        attitude = 90
        bank = 0
    elif t < - 0.499: # south pole
        heading = - 2 * atan2(q.x, q.w) * c
        attitude = - 90
        bank = 0
    else:
        heading = atan2(2*(q.y*q.w - q.x*q.z) , 1 - 2*(q.y*q.y + q.z*q.z)) * c
        attitude = asin(2*t) * c
        bank = atan2(2*(q.x*q.w - q.y*q.z), 1 - 2*(q.x*q.x + q.z*q.z)) * c
    return (heading, attitude, bank)
# then the rotation is Ry(h) @ Rz(a) @ Rx(b)

def get_quaternion(u ,v): # u and v must be normalized
    "Get a unit quaternion whose corresponding rotation sends u to v"
    d = np.vdot(u, v)
    c = sqrt(1+d)
    r = 1 / sqrt(2) / c
    W = np.cross(u, v)
    arr = np.concatenate((np.array([c/sqrt(2)]), r*W))
    return quaternion.from_float_array(arr)

def satellite(t, R, alpha, k):
    return R * np.array([
        cos(alpha) * cos(t) * cos(k*t) - sin(t) * sin(k*t),
        cos(alpha) * sin(t) * cos(k*t) + cos(t) * sin(k*t),
        sin(alpha) * cos(k*t)
    ])

def satellite_motion(nframes, R, alpha=3*pi/4, k=4):
    quats = [None]*nframes
    t_ = np.linspace(0, 2*pi, nframes+1)[:nframes]
    satellite0 = satellite(0, R, alpha, k)
    A = satellite0.copy()
    q0 = quaternion.one
    quats[0] = q0
    for i in range(nframes-1):
        B = satellite(t_[i+1], R, alpha, k)
        q1 = get_quaternion(A/R, B/R) * q0
        quats[i+1] = q1
        A = B
        q0 = q1
    return (satellite0, quats)

cu = 0.0000000000001
cv = 0.0000000000001
N = 3

def Fx(u, v):
    return -np.cos(u+v) / (sqrt(2)+np.cos(v-u))

def DFx(u, v):
    DFxu = (Fx(u,v)-Fx(u+cu,v))/cu
    DFxv = (Fx(u,v)-Fx(u,v+cv))/cv
    return (DFxu, DFxv)

def Fy(u, v):
    return np.sin(v-u) / (sqrt(2)+np.cos(v-u))

def DFy(u, v):
    DFyu = (Fy(u,v)-Fy(u+cu,v))/cu
    DFyv = (Fy(u,v)-Fy(u,v+cv))/cv
    return (DFyu, DFyv)

def Fz(u, v):
    return np.sin(u+v) / (sqrt(2)+np.cos(v-u))

def DFz(u, v):
    DFzu = (Fz(u,v)-Fz(u+cu,v))/cu
    DFzv = (Fz(u,v)-Fz(u,v+cv))/cv
    return (DFzu, DFzv)

def n1(u, v):
    dfyu, dfyv = DFy(u, v)
    dfzu, dfzv = DFz(u, v)
    return dfyu*dfzv - dfzu*dfyv

def n2(u, v):
    dfxu, dfxv = DFx(u, v)
    dfzu, dfzv = DFz(u, v)
    return dfzu*dfxv - dfxu*dfzv

def n3(u, v):
    dfxu, dfxv = DFx(u, v)
    dfyu, dfyv = DFy(u, v)
    return dfxu*dfyv - dfyu*dfxv

def f(u, v):
    r = np.sqrt(n1(u,v)**2+n2(u,v)**2+n3(u,v)**2)
    t = (np.abs(np.sin(15*u)*np.cos(15*v)))**7 + 0.4*np.sin(2*N*u)
    tr = t / r
    return np.array([
        Fx(u, v) + tr*n1(u, v),
        Fy(u, v) + tr*n2(u, v),
        Fz(u, v) + tr*n3(u, v)
    ])

x = np.linspace(0, 2*pi, 500)
U, V = np.meshgrid(x, x)
X, Y, Z = f(U, V)
grid = pv.StructuredGrid(X, Y, Z)
mesh = grid.extract_geometry()#.clean(tolerance=1e-6)
mesh["dist"] = np.linalg.norm(mesh.points, axis=1)

pos0, quats = satellite_motion(120, 12)

for i, q in enumerate(quats):
    pngname = "zzpic%03d.png" % i
    pltr = pv.Plotter(window_size = [512, 512], off_screen=True)
    pltr.background_color = "#363940"
    pltr.set_focus(mesh.center)
    pltr.set_position(pos0)
    h, a, b = quaternion2hab(q)
    pltr.camera.roll = b
    pltr.camera.azimuth = a
    pltr.camera.elevation = h
    pltr.camera.zoom(1)
    pltr.add_mesh(
        mesh, smooth_shading=True, specular=15, cmap="plasma",
        log_scale=False, show_scalar_bar=False, flip_scalars=False
    )
    pltr.show(screenshot=pngname)

os.system(
    "gifski --frames=zzpic*.png --fps=9 -o pseudoHopfTorus.gif"
)


from sklearn import datasets, model_selection, metrics
from sklearn.externals import joblib
from sklearn.linear_model import LogisticRegression

iris = datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3, shuffle=True, random_state=0)

clf = LogisticRegression(random_state=0, solver="lbfgs", multi_class="multinomial")
clf.fit(X_train, y_train)

joblib.dump(clf, "iris_regression_clf.joblib")

y_pred = clf.predict(X_test)
print(metrics.accuracy_score(y_test, y_pred))


import numpy as np
import matplotlib.pyplot as plt

def plot_sun(sunposition, d):
    '''

    '''
    fig = plt.figure(figsize=d['figsize'])

    tks = [np.deg2rad(a) for a in np.linspace(0,360,8,endpoint=False)]
    xlbls = np.array(['N','45','E','135','S','225','W','315'])

    ax = fig.add_subplot(111, projection='polar')
    ax.set_theta_zero_location('N')
    ax.set_xticks((tks))
    ax.set_xticklabels(xlbls, rotation="vertical", size=12)
    ax.tick_params(axis='x', pad=0.5)
    ax.set_theta_direction(-1)
    ax.set_rmin(0)
    ax.set_rmax(90)
    ax.set_rlabel_position(90)
    ax.set_title('Sun Position')

    xs = np.deg2rad(sunposition[0,:])
    ys = 90 - sunposition[1,:]

    ax.scatter(xs, ys, s=10, c='orange', alpha=0.5)

    plt.subplots_adjust(top=d['top'], bottom=d['bottom'],
                        left=d['left'], right=d['right'])
    plt.close()
    return fig


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import preprocessing

# odczyt danych
film_data = pd.read_csv('MoviesOnStreamingPlatforms_updated.csv')

# Czyszczenie wierszy z pustymi warościami.
film_data.dropna(inplace=True)

# Usunięcie zbędnych kolumn
film_data.drop(film_data.columns[[0, 1]], axis = 1)

# Normalizacja: Lowercase dla danych tekstowych, standaryzacja (0..1) dla wartości float, sortowanie danych w komórce.

for col_name in ['Title', 'Directors', 'Genres', 'Country', 'Language']:
    film_data[col_name] = film_data[col_name].str.lower()

for col_name in ['Directors', 'Genres', 'Country', 'Language']:
    film_data[col_name] = film_data[col_name].str.split(',').map(lambda x: ','.join(sorted(x)))

scaler = preprocessing.MinMaxScaler()
film_data[['IMDb', 'Runtime']] = scaler.fit_transform(film_data[['IMDb', 'Runtime']])

# Podział zbioru na train, dev, test w proporcji 8:1:1
train_ratio = 0.8
validation_ratio = 0.1
test_ratio = 0.1

film_train, film_test = train_test_split(film_data, test_size=1 - train_ratio)

film_valid, film_test = train_test_split(film_test, test_size=test_ratio/(test_ratio + validation_ratio))

film_train.to_csv('MoviesOnStreamingPlatforms_updated.train')
film_test.to_csv('MoviesOnStreamingPlatforms_updated.test')
film_valid.to_csv('MoviesOnStreamingPlatforms_updated.dev')


import os
import re
from typing import TypeVar, Optional
from argparse import ArgumentParser, Namespace
from urllib.request import urlopen
from pathlib import Path
from hashlib import sha256

ARTIFACTS = [
    "opa_darwin_amd64",
    "opa_darwin_arm64_static",
    "opa_linux_amd64",
    "opa_linux_amd64_static",
    "opa_linux_arm64_static",
    "opa_windows_amd64",
]

T = TypeVar('T')

def required(value: Optional[T], name: str) -> T:
    if value is None:
        raise ValueError(f"Expected value for {name}")

    return value

def get_sha256(artifact_name: str, version: str) -> str:
    ext = ".exe" if "windows" in artifact_name else ""

    with urlopen(f"https://github.com/open-policy-agent/opa/releases/download/v{version}/{artifact_name}{ext}.sha256") as res:
        return res.read().decode().split(' ')[0]

def get_sha256_file(file: str, version: str) -> str:
    file_url = f"https://raw.githubusercontent.com/open-policy-agent/opa/v{version}/{file}"

    with urlopen(file_url) as res:
        return sha256(res.read()).hexdigest()

def main(args: Namespace):
    d = dict([
        (artifact_name, get_sha256(artifact_name, args.version))
        for artifact_name in ARTIFACTS
    ])

    bzl_path = WORKSPACE_ROOT.joinpath(
        'opa', 'private', 'opa_rules_dependencies.bzl')

    with open(bzl_path) as bzl_file:
        bzl_content = bzl_file.read()

    version_match = re.search(
        r"DEFAULT_VERSION\s*=\s*[\"'](.*?)[\"']", bzl_content)

    if version_match is None:
        raise ValueError(f"Could not find DEFAULT_VERSION in file {bzl_path}")

    start_version = version_match.start(1)
    end_version = version_match.end(1)

    bzl_content = bzl_content[:start_version] + \
        args.version + bzl_content[end_version:]

    dict_match = re.search(
        r"^_OPA_SHA256\s*=\s*{\s*$", bzl_content, re.MULTILINE)

    if dict_match is None:
        raise ValueError(f"Could not find _OPA_SHA256 in file {bzl_path}")

    bzl_content = bzl_content[:dict_match.end(
    )] + f"\n    \"{args.version}\": {{\n" + '\n'.join([
        f"        \"{artifact_name}\": \"{sha256}\","
        for artifact_name, sha256 in d.items()
    ] + [
        f"        \"opa_capabilities_json\": \"{get_sha256_file('capabilities.json', args.version)}\",",
        f"        \"opa_builtin_metadata_json\": \"{get_sha256_file('builtin_metadata.json', args.version)}\",",
    ]) + "\n    }," + bzl_content[dict_match.end():]

    with open(bzl_path, "w", encoding="utf-8") as bzl_file:
        bzl_file.write(bzl_content)

def get_workspace_root(wd: Path) -> Path:
    while not wd.joinpath("WORKSPACE").exists():
        wd = wd.parent

    return wd

BUILD_WORKING_DIRECTORY = Path(
    required(os.getenv("BUILD_WORKING_DIRECTORY"), "BUILD_WORKING_DIRECTORY"))
WORKSPACE_ROOT = get_workspace_root(BUILD_WORKING_DIRECTORY)

if __name__ == '__main__':

    parser = ArgumentParser()
    parser.add_argument('--version', '-v', required=True)

    main(parser.parse_args())


# load packages
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import MaxNLocator
from scipy.optimize import curve_fit

from tqdm import tqdm

from scipy.signal import find_peaks

plt.close('all')

#%% Define functions used in this code
def findpeaks(arr, h, w=1, d=1):
    # Adjust baseline to emulate 'DoubleSided'
    adjusted = arr  - arr.mean()
   # adjusted = arr
    indexes, _ = find_peaks(adjusted, height=h)
    # Retrieve extrema heights from original signal rather
    # than from the properties dict with adjusted heights
    heights = arr[indexes]

    return heights, indexes

#Converts nested list into single list
def flatten(x):
    result = []
    for el in x:
        if hasattr(el, "__iter__") and not isinstance(el, str):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

#Rounds Number to Specified number of significant digits
def round_sig(x, sig=2):
        if x!=0:
            rnd=round(x, sig-int(np.floor(np.log10(abs(x))))-1)
        else:
            rnd=0
        return rnd

#%%Input data:
#The program expects multiple (up to 10) files in .txt or .dat format. In each file, the Raman shift
#should be included in the first column, followed by the Intensity data in
#consecutive columns (as many spectra available, and not neccesarily the same number of spectra per file).

#The location and names of the files should be indicated in path and file_name
#variables, respectively. The total number of files should be included in
#total.
path='../Example Data/Example_Data_1/'

#path='../Example Data/Example_Data_2_2DMaps/' #Uncomment if you are running the Example_Data_2_2DMaps

file_name=['6,5-SWCNT',
           'CuMINT' ]

#file_name=['mapExample']  #Uncomment if you are running the Example_Data_2_2DMaps

total=len(file_name) #Total Number of files

delim='\t'  #Define delimeter of data in data file
type_='.txt'
name=['6,5 SWNT','CuMINT']#Indicate here the names of the data for legend and titles

#name= ['patterned graphene']  #Uncomment if you are running the Example_Data_2_2DMaps

imgtype='.svg' #What type of images do you want to save

#Histogram Normalization?
#Set to True if you want your histograms to be normalized
#Else, set to False
den=True

#%%Normalization:
#Choose normalization spectral rng. The spectra will be normalised to
#the maximun within the specified spectral rng:
normLow=1500; #Lower limit in cm-1
normHigh=1700; #Upper limit in cm-1

#%%Peak identification: Intensity and Shifts
#Intensity ratio Id/Ig will be calculated by taking: max Int value-min Int
#value within the spectral rng selected. Select appropriate rng where
#the full peaks are resolved.
#Raman shifts for D and G modes are caculated as the position of the
#maximun intensity
#RBM modes

#G band
band1Low=1450; #Lower limit in cm-1
band1High=1700; #Upper limit in cm-1
#D band
band2Low=1200; #Lower limit in cm-1
band2High=1400; #Upper limit in cm-1
#2D band
band3Low=2450; #Lower limit in cm-1
band3High=2800; #Upper limit in cm-1

#RBM modes
rbm=1# Set to 1 if RBM analysis desired
RBMregion_Low=200; #Lower limit in cm-1
RBMregion_High=360; #Upper limit in cm-1
Prom=[0.01]; #This value sets the max limit at which peaks will be considered.
    #This can be a single number, or a list of values that different for each file
    #Local maxima with a maximum height lower than Prom will be discarded.
if len(Prom)==1 and total!=1:
    Prom=Prom*np.ones(total)
if rbm==1:
    meanRBM=dict.fromkeys(name)
    stdRBM=dict.fromkeys(name)

#%% Model: Choose the method to analyse spectral features (simple peak
# identification or lorentzianpeak fitting)

lorentz=1; # lorentz fits for G, D and 2D (=0 for simple identification; =1 for lorentzian peak fitting)
nt=1; # Splitting G band in G+ and G-?

# If lorentzian fits are selected, the user is required to indicate
# initial guess for the peak position, FWHM and intensity

Gmin_init=[[1530, 20, 0.4], [1530, 25, 0.31]] #[center FWHM intensity_max] for G-, only if nt=1
Gplus_init=[[1580, 30, 1], [1580, 25, 1]] #[center FWHM intensity_max] fof G+
D_init=[[1300, 40, 0.08], [1325, 10, 0.04]] #[center FWHM intensity_max] for D band
init_2D=[[2600, 50, 0.4], [2640, 50, 0.3]] #[center FWHM intensity_max] for 2D band

#%%Ploting options:
#Choose the desired output plots (1 is yes and 0 is no).
raw=1; #Make figure af all raw spectra.
norm=1; #Plot all normalised spectra.
rng=1; #Plot spectral regions chosen for G, D and 2D bands in intensity calculation
peaks=1; #plot found peaks in the RBM region, Note that if the
#number of spectra is very high, the computing is going to slow down
#significantly.

correlations=1 #Plot correlations between peaks shifts and Id/Ig

#%%Fontsize for Plots
fs=16 # This is the fontsize you want to use for all the plots
width=3 #width of histograms

#%%Is it a 2D map? In case it is and you want the 3D plots in form of maps
maps=0; #set to 0 if not a maps and to 1 if you want a 2D Map
map_var='I'  # What do you want to map?
                                         #'I' for Intensity Ratio,
                                         #'G' for Gband
                                         #'D' for Dband
                                         #'2D' for 2DBand
rows=32 ; #Number of rows in 2D Map
col=32 ; #Number of columns in 2D Map
use_leng=1; #If 1, then map will use x and y dimensions defined below. Else if 0, will use pixel number
xlen=42; #x len in um
ylen=38;  #y len in um

#%%Code Starts here:
os.chdir(path) #Changes folder to the selected path

for z in range(0,total):

#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%%
#%%#%%#%%#%%#%%#Import data #%%#%%#%%#%%#%%#%%%
#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%%

    data = pd.read_csv(file_name[z]+type_,delimiter=delim,decimal='.',header=None);
    data=data.to_numpy()
    Shift = np.array(data[:,0]); #Raman shift is the first column of the file
    Intensity=data[:,1:]; #Raman intensities are all data from column 2 til end

    rng2=Shift>0
    Intensity=Intensity[rng2,:]
    Shift=Shift[rng2]
    #%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#
    #%%#%%#%%#Set color scales for plots #%%#%%#%%#%%
    #%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%%
    gray=cm.get_cmap('gist_yarg',len(Intensity[0,:])+20)
    winter=cm.get_cmap('winter',len(Intensity[0,:])+5)
    summer=cm.get_cmap('summer',len(Intensity[0,:])+5)
    autumn=cm.get_cmap('autumn',len(Intensity[0,:])+5)
    bone= cm.get_cmap('bone',len(Intensity[0,:])+5)
    cool=cm.get_cmap('cool',len(Intensity[0,:])+5)
    spring=cm.get_cmap('spring',len(Intensity[0,:])+5)
    copper=cm.get_cmap('copper',len(Intensity[0,:])+5)
    pink=cm.get_cmap('pink',len(Intensity[0,:])+5)
    hot=cm.get_cmap('hot',len(Intensity[0,:])+5)

    cmap=[gray,winter,summer,autumn,
          cool,spring,copper,pink,hot,bone]
    #Sets color maps for different files

    color=cmap[z];
    clr=color(np.linspace(0,1,len(Intensity.transpose())))

    if total==1:
        div=[1, 1];
    elif total==2:
        div=[1, 2];
    elif total==3:
        div=[1, 3];
    elif total==4:
        div=[2, 2];
    elif total==5:
        div=[2, 3];
    elif total==6:
        div=[2, 3];
    elif total==10:
        div=[2, 5];
    else:
        div=[3, 3];

#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%%
#%%#%%#%%#%%#%%Normalization #%%#%%#%%#%%#%%#%%#%%
#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%%

    indLow=np.where(abs(Shift-normLow)==min(abs(Shift-normLow)))[0][0]
    indHigh=np.where(abs(Shift-normHigh)==min(abs(Shift-normHigh)))[0][0];

    Intensity_norm=np.empty(np.shape(Intensity));
    for n in range(0,len(Intensity[1,:])):
        Intensity_norm[:,n]=(Intensity[:,n]-min(Intensity[:,n])); #substract min
        Intensity_norm[:,n]=Intensity_norm[:,n]/max(Intensity_norm[indLow:indHigh,n]); #divide by norm

    Intensity_av=np.mean(Intensity_norm,axis=1); #Calculate average spectra

    #%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%
    #%%#%%#%%#%%G and D modes: Intensity and Shift #%%#%%#%%#%%#%%
    #%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%

    ind1Low=np.where(abs(Shift-band1Low)==min(abs(Shift-band1Low)))[0][0]
    ind1High=np.where(abs(Shift-band1High)==min(abs(Shift-band1High)))[0][0];

    ind2Low=np.where(abs(Shift-band2Low)==min(abs(Shift-band2Low)))[0][0]
    ind2High=np.where(abs(Shift-band2High)==min(abs(Shift-band2High)))[0][0];

    ind3Low=np.where(abs(Shift-band3Low)==min(abs(Shift-band3Low)))[0][0]
    ind3High=np.where(abs(Shift-band3High)==min(abs(Shift-band3High)))[0][0];

    Intensity_1rng=Intensity_norm[ind1Low:ind1High,:];
    Intensity_2rng=Intensity_norm[ind2Low:ind2High,:];
    Intensity_3rng=Intensity_norm[ind3Low:ind3High,:];

    Shift_rng1=Shift[ind1Low:ind1High];
    Shift_rng2=Shift[ind2Low:ind2High];
    Shift_rng3=Shift[ind3Low:ind3High];

    Int_G=np.array([]);
    Int_D=np.array([]);
    Int_2D=np.array([]);

    center_G=np.array([]);
    center_D=np.array([]);
    center_2D=np.array([]);

    if nt==1:
        I_G_fit=[]
        center_Gmin=[]
        FWHM_Gmin=[]
        Int_Gmin=[]
        center_Gplus=[]
        FWHM_Gplus=[]
        Int_Gplus=[]
    if lorentz==1:
        I_D_fit=[]
        FWHM_D=[]
        I_2D_fit=[]
        FWHM_2D=[]
        if nt==0:
            I_G_fit=[]
            FWHM_G=[]

    for n in tqdm(range(0,len(Intensity_norm[0,:]))):

       if np.logical_and(lorentz==0,nt==0)==1:
           #peak 1/G:
           Int_G=np.append(Int_G,max(Intensity_1rng[:,n])-min(Intensity_1rng[:,n]));
           indMax1=np.where(Intensity_1rng[:,n]==max(Intensity_1rng[:,n]))[0][0];
           center_G=np.append(center_G,Shift_rng1[indMax1]);

       if lorentz==0:
           #peak 2/D:
           Int_D=np.append(Int_D,max(Intensity_2rng[:,n])-min(Intensity_2rng[:,n]));
           indMax2=np.where(Intensity_2rng[:,n]==max(Intensity_2rng[:,n]))[0][0];
           center_D=np.append(center_D,Shift_rng2[indMax2]);

           #peak 3/2D:
           Int_2D=np.append(Int_2D,max(Intensity_3rng[:,n])-min(Intensity_3rng[:,n]));
           indMax3=np.where(Intensity_3rng[:,n]==max(Intensity_3rng[:,n]))[0][0];
           center_2D=np.append(center_2D,Shift_rng3[indMax3]);

       #%%G+ and G- fitting
       if nt==1:

           #Define initial guesses
           InitGuess_G=[Gmin_init[z][2]*((Gmin_init[z][1]/2)**2),
                        Gmin_init[z][0],
                        (Gmin_init[z][1]/2)**2,
                        Gplus_init[z][2]*((Gplus_init[z][1]/2)**2),
                        Gplus_init[z][0],
                        (Gplus_init[z][1]/2)**2,
                        0.1]
           def fit_func(x, g1,g2,g3,g4,g5,g6,g7):
               return (g1/((x-g2)**2+g3)+g4/((x-g5)**2+g6)+g7)# lorentz

           gamma_G ,pcov2= curve_fit(fit_func, Shift_rng1, Intensity_1rng[...,n], InitGuess_G,
                                     bounds=(0,np.inf),maxfev=10000)

           I_G_fit.append(fit_func(Shift_rng1, *gamma_G))

           if np.logical_and(np.logical_and(gamma_G[1]>band1Low,gamma_G[4]>band1Low),np.logical_and(gamma_G[1]<band1High,gamma_G[4]<band1High))==1:
               if gamma_G[1]<gamma_G[4]:
                      center_Gmin.append(gamma_G[1])
                      FWHM_Gmin.append(2*np.sqrt(gamma_G[2]))
                      Int_Gmin.append(gamma_G[0]/gamma_G[2])
                      center_Gplus.append(gamma_G[4])
                      FWHM_Gplus.append(2*np.sqrt(gamma_G[5]))
                      Int_Gplus.append(gamma_G[3]/gamma_G[5])
               else:
                     center_Gplus.append(gamma_G[1])
                     FWHM_Gplus.append(2*np.sqrt(gamma_G[2]))
                     Int_Gplus.append(gamma_G[0]/gamma_G[2])
                     center_Gmin.append(gamma_G[4])
                     FWHM_Gmin.append(2*np.sqrt(gamma_G[5]))
                     Int_Gmin.append(gamma_G[3]/gamma_G[5])
           else:
                     center_Gplus.append(np.nan)
                     FWHM_Gplus.append(np.nan)
                     Int_Gplus.append(np.nan)
                     center_Gmin.append(np.nan)
                     FWHM_Gmin.append(np.nan)
                     Int_Gmin.append(np.nan)

        #%%Lorentizan Fits
       if lorentz==1:

           #D bands
           #Define Initial guesses
           InitGuess_D=[D_init[z][2]*((D_init[z][1]/2)**2),
                        D_init[z][0],
                        (D_init[z][1]/2)**2,
                        0];

           def fit_func2(x, g1,g2,g3,g4):
               return (g1/((x-g2)**2+g3)+g4)# lorentz

           gamma_D ,pcov2= curve_fit(fit_func2, Shift_rng2, Intensity_2rng[...,n],
                                      InitGuess_D,bounds=(0,np.inf),maxfev=10000)

           I_D_fit.append(fit_func2(Shift_rng2, *gamma_D))

           if np.logical_and(gamma_D[1]>band2Low,gamma_D[1]<band2High)==1:
               center_D=np.append(center_D,gamma_D[1]);
               FWHM_D.append(2*np.sqrt(gamma_D[2]));
               Int_D=np.append(Int_D,gamma_D[0]/gamma_D[2]);
           else:
               center_D=np.append(center_D,np.nan);
               FWHM_D.append(np.nan);
               Int_D=np.append(Int_D,np.nan);

           #2D bands
           #Define Initial guesses
           InitGuess_2D=[init_2D[z][2]*((init_2D[z][1]/2)**2),
                        init_2D[z][0],
                        (init_2D[z][1]/2)**2,
                        0];

           gamma_2D ,pcov2= curve_fit(fit_func2, Shift_rng3, Intensity_3rng[...,n],
                                      InitGuess_2D, bounds=(0,np.inf),maxfev=10000)

           I_2D_fit.append(fit_func2(Shift_rng3, *gamma_2D))

           if np.logical_and(gamma_2D[1]>band3Low,gamma_2D[1]<band3High)==1:

               center_2D=np.append(center_2D,gamma_2D[1]);
               FWHM_2D.append(2*np.sqrt(gamma_2D[2]));
               Int_2D=np.append(Int_2D,gamma_2D[0]/gamma_2D[2]);
           else:
               center_2D=np.append(center_2D,np.nan);
               FWHM_2D.append(np.nan);
               Int_2D=np.append(Int_2D,np.nan);

           if nt==0:
               #Lorentzian G fit if no G+/G- fit
               InitGuess_G=[Gplus_init[z][2]*((Gplus_init[z][1]/2)**2),
                            Gplus_init[z][0],
                            (Gplus_init[z][1]/2)**2,
                            0];

               gamma_G ,pcov2= curve_fit(fit_func2, Shift_rng1, Intensity_1rng[...,n],
                                InitGuess_G, bounds=(0,np.inf),maxfev=10000)

               I_G_fit.append(fit_func2(Shift_rng1, *gamma_G))

               if np.logical_and(gamma_G[1]>band1Low,gamma_G[1]<band1High)==1:
                   center_G=np.append(center_G,gamma_G[1]);
                   FWHM_G.append(2*np.sqrt(gamma_G[2]));
                   Int_G=np.append(Int_G,gamma_G[0]/gamma_G[2]);
               else:
                   center_G=np.append(center_G,np.nan);
                   FWHM_G.append(np.nan);
                   Int_G=np.append(Int_G,np.nan);

    if nt==1:
        I21=Int_D/Int_Gplus;
        G_av=np.nanmean(center_Gplus)
        G_std=np.nanstd(center_Gplus)
        Gmin_av=np.nanmean(center_Gmin)
        Gmin_std=np.nanstd(center_Gmin)
    else:
        I21=Int_D/Int_G;
        G_av=np.nanmean(center_G)
        G_std=np.nanstd(center_G)

    I21_av=np.nanmean(I21);
    I21_error=np.nanstd(I21);
    D_av=np.nanmean(center_D)
    D_std=np.nanstd(center_D)
    D2_av=np.nanmean(center_2D)
    D2_std=np.nanstd(center_2D)

#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%
#%%#%%#%%#%%#RBM modes Shifts  #%%#%%#%%#%%#%%#%%
#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%

    if rbm == 1:
        indRBMLow=np.where(Shift-RBMregion_Low==min(abs(Shift-RBMregion_Low)))[0][0];
        indRBMHigh=np.where(Shift-RBMregion_High==min(abs(Shift-RBMregion_High)))[0][0];
        PeaksInt=[];
        PeaksLoc1=[];

        if peaks == 1:
            if z==0:
                fig_peaks,ax_peaks= plt.subplots(div[0],div[1],figsize=(div[1]*5,div[0]*5), constrained_layout=True)  #Create the figure only once, on the first loop
                fig_peaks.canvas.manager.set_window_title('Region Peaks')
                if total==1:
                    ax_peaks=[ax_peaks]
                else:
                    ax_peaks=ax_peaks.flatten()

        for n in range(0,len(Intensity_norm[0,:])):
            [pksRBM,locsRBM]=findpeaks(Intensity_norm[indRBMLow:indRBMHigh,n],Prom[z]);
            PeaksInt.append(pksRBM);
            PeaksLoc1.append(Shift[locsRBM+indRBMLow]);

            if peaks == 1:
                ax_peaks[z].plot(Shift[indRBMLow:indRBMHigh],Intensity_norm[indRBMLow:indRBMHigh,n],color='C'+str(n))
                ax_peaks[z].plot(Shift[locsRBM+indRBMLow],pksRBM,ls='',marker='v',markersize=10,color='C'+str(n))

                ax_peaks[z].set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
                ax_peaks[z].set_ylabel('Intensity / a.u',fontsize=fs)
                ax_peaks[z].tick_params(axis="both", labelsize=fs)
                ax_peaks[z].set_title(name[z]+' :Region Peaks');
                ax_peaks[z].set_xlim((Shift[indRBMLow],Shift[indRBMHigh]))

#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%
#%%#%%#%%Figures #%%#%%#%%#%%%
#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%#%%

    #Raw spectra
    if raw==1:
       if z==0:
           fig_raw,ax_raw= plt.subplots(div[0],div[1],figsize=(div[1]*5,div[0]*5), constrained_layout=True)  #Create the figure only once, on the first loop
           fig_raw.canvas.manager.set_window_title('Raw Data')
           if total==1:
               ax_raw=[ax_raw]
           else:
               ax_raw=ax_raw.flatten()

       for n,clr2 in enumerate(clr):
           ax_raw[z].plot(Shift,Intensity[:,n],color=clr2,label=str(n));

     #  ax_raw[z].legend()
       ax_raw[z].set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
       ax_raw[z].set_ylabel('Intensity / a.u',fontsize=fs)
       ax_raw[z].tick_params(axis="both", labelsize=fs)
       ax_raw[z].set_title(name[z]+': Raw data');
       ax_raw[z].set_xlim((0,np.max(Shift)))
#%%Normalised spectra
    if norm==1:
        if z==0:
           fig_norm,ax_norm= plt.subplots(div[0],div[1], figsize=(div[1]*5,div[0]*5), constrained_layout=True)
           fig_norm.canvas.manager.set_window_title('Normalized Spectra')
           if total==1:
               ax_norm=[ax_norm]
           else:
               ax_norm=ax_norm.flatten()
        for n,clr2 in enumerate(clr):
           ax_norm[z].plot(Shift,Intensity_norm[:,n],color=clr2,label=str(n));
        #ax_norm[z].legend()
        ax_norm[z].set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
        ax_norm[z].set_ylabel('Intensity / a.u',fontsize=fs)
        ax_norm[z].tick_params(axis="both", labelsize=fs)
        ax_norm[z].set_title(name[z]+': Normalized spectra');
        ax_norm[z].set_ylim((0,np.max(Intensity_norm)))
        ax_norm[z].set_xlim((0,np.max(Shift)))

#%%Average spectra
    if z==0:
       fig_avg,ax_avg= plt.subplots(1,1, figsize=(6,6), constrained_layout=True)
       fig_avg.canvas.manager.set_window_title('Average Spectra')

    ax_avg.plot(Shift,Intensity_av,color=clr[round(len(clr)/2)],label=name[z]);
    ax_avg.legend(fontsize=fs)
    ax_avg.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
    ax_avg.set_ylabel('Intensity / a.u',fontsize=fs)
    ax_avg.tick_params(axis="both", labelsize=fs)
    ax_avg.set_title('Average spectra');

#%% #Spectral rng for Intensity ratio
    if rng ==1:
        if z==0:
           fig_rng,ax_rng= plt.subplots(div[0],div[1], figsize=(div[1]*5,div[0]*5), constrained_layout=True)
           fig_rng.canvas.manager.set_window_title('Range Used for Intensity Ratio Calculation')
           if total==1:
               ax_rng=[ax_rng]
           else:
               ax_rng=ax_rng.flatten()
        for n,clr2 in enumerate(clr):
            ax_rng[z].plot(Shift[ind1Low:ind1High],Intensity_1rng[:,n],color=clr2);
            ax_rng[z].plot(Shift[ind2Low:ind2High],Intensity_2rng[:,n],color=clr2);
            ax_rng[z].plot(Shift[ind3Low:ind3High],Intensity_3rng[:,n],color=clr2);

        ax_rng[z].set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
        ax_rng[z].set_ylabel('Intensity / a.u',fontsize=fs)
        ax_rng[z].tick_params(axis="both", labelsize=fs)
        ax_rng[z].set_title(name[z]+': Range for $I_{d}/I_{g}$ Calculation');
        ax_rng[z].set_ylim((0,1))

#%% Histogram intensity ratio

    if z==0:
        fig_IR,ax_IR= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
        fig_IR.canvas.manager.set_window_title('Intensity Ratio')

    #Define Bin Size
    binsInt=np.arange(min(I21), max(I21) + width/250, width/250)
    if len(binsInt)==1:
        binsInt=np.arange(min(I21)- width/250, max(I21) + width/250, width/250)

    ax_IR.hist(I21,binsInt,color=clr[round(len(clr)/2)],
               label=name[z]+': $I_{d}/I_{g}$='+str(round(I21_av,4))+'$\pm$'
               +str(round(I21_error,4)),alpha=0.5,ec='k',align='left',density=den)

    ax_IR.legend(fontsize=fs-2)
    ax_IR.set_xlabel('$I_{d}/I_{g}$',fontsize=fs)
    ax_IR.set_ylabel('counts',fontsize=fs)
    ax_IR.tick_params(axis="both", labelsize=fs)
    ax_IR.set_title('Intensity ratio: $I_{d}/I_{g}$',fontsize=fs+2);

#%% Raman shift G mode

    if z==0:
        fig_G,ax_G= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
        fig_G.canvas.manager.set_window_title('Raman shift G band')
        if nt==1:
            fig_G_I,ax_G_I= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
            fig_G_I.canvas.manager.set_window_title('Intensity ratio G modes')

    if nt==0:

        binsShift=np.arange(min(center_G)-width, max(center_G) + width, width)

        # if len(binsShift)==1:
        #     ax_G.hist(center_G,width, color=clr[round(len(clr)/2)],
        #               label=name[z]+': $G$ band='+str(round(G_av,2))+'$\pm$'
        #               +str(round(G_std,2))+' $cm^{-1}$',
        #               alpha=0.5,ec='k',align='left')

        #else:
        ax_G.hist(center_G,binsShift,color=clr[round(len(clr)/2)],
                      label=name[z]+': $G$ band='+str(round(G_av,2))+'$\pm$'
                      +str(round(G_std,2))+' $cm^{-1}$',
                      alpha=0.5,ec='k',align='left',density=den)

        ax_G.set_title('Raman shift $G$ band',fontsize=fs+2);
    else:
      #  binsShift=round(max(center_Gplus)-min(center_Gmin));
        binsShift=np.arange(min(center_Gmin), max(center_Gplus) + width, width)
        ax_G.hist(center_Gplus,binsShift, color=clr[round(len(clr)/3)],
                  label=name[z]+': $G^{+}$ band='+str(round(G_av,2))+'$\pm$'
                  +str(round(G_std,2))+' $cm^{-1}$',
                  alpha=0.5,ec='k',align='left',density=den)
        ax_G.hist(center_Gmin,binsShift,color=clr[round(len(clr)/2)],
                  label=name[z]+': $G^{-}$ band='+str(round(Gmin_av,2))+'$\pm$'
                  +str(round(Gmin_std,2))+' $cm^{-1}$',
                  alpha=0.5,ec='k',align='left',density=den)
        ax_G.set_title('Raman shift $G$ modes',fontsize=fs+2);

        Iplus_minus=np.array(Int_Gplus)/np.array(Int_Gmin);
        binsInt=np.arange(min(Iplus_minus), max(Iplus_minus) + width/10, width/10)
        ax_G_I.hist(Iplus_minus,binsInt,color=clr[round(len(clr)/2)],
                  label=name[z]+': $I_{G^+}/I_{G^-}$ ='+str(round(np.mean(Iplus_minus),2))+'$\pm$'
                  +str(round(np.std(Iplus_minus),2)),
                  alpha=0.5,ec='k',align='left',density=den)
        ax_G_I.legend(fontsize=fs-2)
        ax_G_I.set_xlabel('$I_{G^+}/I_{G^-}$',fontsize=fs)
        ax_G_I.set_ylabel('counts',fontsize=fs)
        ax_G_I.tick_params(axis="both", labelsize=fs)
        ax_G_I.set_title('Intensity ratio G modes',fontsize=fs+2);

    ax_G.legend(fontsize=fs-2)
    ax_G.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
    ax_G.set_ylabel('counts',fontsize=fs)
    ax_G.tick_params(axis="both", labelsize=fs)
#%% #Raman shift D mode

    if z==0:
        fig_D,ax_D= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
        fig_D.canvas.manager.set_window_title('Raman shift D band')

    binsShift=np.arange(min(center_D)-width, max(center_D) + width, width)

    ax_D.hist(center_D,binsShift,color=clr[round(len(clr)/2)],
              label=name[z]+': $D$ band='+str(round(D_av,2))+'$\pm$'
              +str(round(D_std,2))+' $cm^{-1}$',
              alpha=0.5,ec='k',align='left',density=den)

    ax_D.legend(fontsize=fs-2)
    ax_D.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
    ax_D.set_ylabel('counts',fontsize=fs)
    ax_D.tick_params(axis="both", labelsize=fs)
    ax_D.set_title('Raman shift $D$ band',fontsize=fs+2);
    ax_D.xaxis.set_major_locator(MaxNLocator(integer=True))
#%% #Raman shift 2D mode
    if z==0:
        fig_2D,ax_2D= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
        fig_2D.canvas.manager.set_window_title('Raman shift D 2band')

    binsShift=np.arange(min(center_2D)-width, max(center_2D) + width, width)

    ax_2D.hist(center_2D,binsShift,color=clr[round(len(clr)/2)],
               label=name[z]+': $2D$ band='+str(round(D2_av,2))+'$\pm$'
               +str(round(D2_std,2))+' $cm^{-1}$',
               alpha=0.5,ec='k',align='left',density=den)

    ax_2D.legend(fontsize=fs-2)
    ax_2D.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
    ax_2D.set_ylabel('counts',fontsize=fs)
    ax_2D.tick_params(axis="both", labelsize=fs)
    ax_2D.set_title('Raman shift $2D$ band',fontsize=fs+2);
    ax_2D.xaxis.set_major_locator(MaxNLocator(integer=True))

#%%%

# Lorentz fit results

    if nt==1 or lorentz==1:
        if nt==1 and lorentz==0:

                fig_lor_fit,ax_lor_fit= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
                fig_lor_fit.canvas.manager.set_window_title('Lorenztian Fitting results')
                ax_lor_fit=[ax_lor_fit]
                for n,clr2 in enumerate(clr):
                    ax_lor_fit[0].plot(Shift_rng1,Intensity_1rng[:,n],color=clr2)
                    ax_lor_fit[0].plot(Shift_rng1,np.transpose(I_G_fit)[:,n],'--r');

        elif lorentz==1:

                fig_lor_fit,ax_lor_fit= plt.subplots(1,3, figsize=(18,6), constrained_layout=True)
                fig_lor_fit.canvas.manager.set_window_title('Lorenztian Fitting results')

                for n,clr2 in enumerate(clr):
                    if n==int(len(clr)/2):
                        ax_lor_fit[0].plot(Shift_rng1,Intensity_1rng[:,n],color=clr2, label='Data')
                        ax_lor_fit[0].plot(Shift_rng1,np.transpose(I_G_fit)[:,n],'--r', label='Lorentzian Fit');

                        ax_lor_fit[1].plot(Shift_rng2,Intensity_2rng[:,n],color=clr2, label='Data')
                        ax_lor_fit[1].plot(Shift_rng2,np.transpose(I_D_fit)[:,n],'--r', label='Lorentzian Fit');

                        ax_lor_fit[2].plot(Shift_rng3,Intensity_3rng[:,n],color=clr2, label='Data')
                        ax_lor_fit[2].plot(Shift_rng3,np.transpose(I_2D_fit)[:,n],'--r', label='Lorentzian Fit');
                    else:
                        ax_lor_fit[0].plot(Shift_rng1,Intensity_1rng[:,n],color=clr2)
                        ax_lor_fit[0].plot(Shift_rng1,np.transpose(I_G_fit)[:,n],'--r');

                        ax_lor_fit[1].plot(Shift_rng2,Intensity_2rng[:,n],color=clr2)
                        ax_lor_fit[1].plot(Shift_rng2,np.transpose(I_D_fit)[:,n],'--r');

                        ax_lor_fit[2].plot(Shift_rng3,Intensity_3rng[:,n],color=clr2)
                        ax_lor_fit[2].plot(Shift_rng3,np.transpose(I_2D_fit)[:,n],'--r');

                ax_lor_fit[1].set_title('Fitting results D',fontsize=fs)
                ax_lor_fit[2].set_title('Fitting results 2D',fontsize=fs)

        for axx in ax_lor_fit:
            axx.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
            axx.set_ylabel('Intensity / a.u',fontsize=fs)
            axx.legend(fontsize=fs)
        ax_lor_fit[0].set_title('Fitting results G',fontsize=fs)
        fig_lor_fit.suptitle('Lorentzian fits of '+name[z],fontsize=fs+3)

        fig_lor_fit.savefig('Lorentz_fit_'+name[z]+imgtype)
#%% #Raman shift RBM
    if rbm==1:

        if z==0:
            fig_RBM,ax_RBM= plt.subplots(1,1, figsize=(8,6), constrained_layout=True)
            fig_RBM.canvas.manager.set_window_title('Raman shift RBM modes')

        npeaks=max([len(n) for n in PeaksLoc1])
        PeaksLoc  = np.array(flatten(PeaksLoc1))

        binsShift=np.arange(min(PeaksLoc)-width, max(PeaksLoc) + width, width)

        [histRBM, edgesRBM]=np.histogram(PeaksLoc,binsShift)
        peaksRBM, _ = find_peaks(np.concatenate(([min(histRBM)],histRBM,[min(histRBM)])), distance=10)
        peaksRBM=peaksRBM-1
        f_peaksRBM=edgesRBM[peaksRBM]

        groups=dict.fromkeys(f_peaksRBM)
        meanRBM[name[z]]=dict.fromkeys(f_peaksRBM)
        stdRBM[name[z]]=dict.fromkeys(f_peaksRBM)

        for n in groups:
            groups[n]=[]
        for p in PeaksLoc:
            try:
                diff_p=abs(p-f_peaksRBM)
                ni=np.where(diff_p==min(diff_p))[0][0]
                groups[f_peaksRBM[ni]].append(p)
            except ValueError:
                continue

        for n in groups:
            meanRBM[name[z]][n]=np.mean(groups[n])
            stdRBM[name[z]][n]=np.std(groups[n])

        lb=[str(round(meanRBM[name[z]][n],2))+ ' $\pm$ ' +str(round(stdRBM[name[z]][n],2)) for n in stdRBM[name[z]]]

        ax_RBM.hist(PeaksLoc,binsShift,color=clr[round(len(clr)/2)],label=name[z]+': '+', '.join(map(str, lb))+' $cm^{-1}$',
                      alpha=0.5,ec='k',align='left',density=den)

        ax_RBM.legend(fontsize=fs-2)
        ax_RBM.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
        ax_RBM.set_ylabel('counts',fontsize=fs)
        ax_RBM.tick_params(axis="both", labelsize=fs)
        ax_RBM.set_title('Raman shift $RBM$ modes',fontsize=fs+2);
        ax_RBM.xaxis.set_major_locator(MaxNLocator(integer=True))
        fig_peaks.savefig('Peak Data.svg')
        fig_RBM.savefig('RBM.svg')

#%% #Correlations

    if z==0:
        fig_Corr,ax_Corr= plt.subplots(2,2, figsize=(15,15), constrained_layout=True)
        fig_Corr.canvas.manager.set_window_title('Correlations')
        ax_Corr=ax_Corr.flatten()

    if nt==1:# In the case where there are separate G+/G- peaks, use G+
        #Linear fit Intensity Ratio vs G+

        idx = np.isfinite(I21) & np.isfinite(center_Gplus)
        fit_I21_G=np.polyfit(np.array(I21)[idx],np.array(center_Gplus)[idx],1)
        #Plot Intensity Ratio vs G+
        ax_Corr[0].plot(I21,center_Gplus,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_I21_G[0],3)))

        #Linear fit 2D vs Gplus
        idx = np.isfinite(center_Gplus) & np.isfinite(center_2D)
        fit_2D_G=np.polyfit(np.array(center_Gplus)[idx],np.array(center_2D)[idx],1)
        #Plot 2D vs G
        ax_Corr[2].plot(center_Gplus,center_2D,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_2D_G[0],3)))
        xx2=np.linspace(min(center_Gplus),max(center_Gplus),100)

        #Linear fit D vs Gplus
        idx = np.isfinite(center_Gplus) & np.isfinite(center_D)
        fit_D_G=np.polyfit(np.array(center_Gplus)[idx],np.array(center_D)[idx],1)
        #Plot 2D vs G
        ax_Corr[3].plot(center_Gplus,center_D,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_D_G[0],3)))
        xx2=np.linspace(min(center_Gplus),max(center_Gplus),100)

    else:
        idx = np.isfinite(I21) & np.isfinite(center_G)
        fit_I21_G=np.polyfit(np.array(I21)[idx],np.array(center_G)[idx],1)
        ax_Corr[0].plot(I21,center_G,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_I21_G[0],3)))

        #Linear fit 2D vs G
        idx = np.isfinite(center_G) & np.isfinite(center_2D)
        fit_2D_G=np.polyfit(np.array(center_G)[idx],np.array(center_2D)[idx],1)
        #Plot 2D vs G
        ax_Corr[2].plot(center_G,center_2D,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_2D_G[0],3)))
        xx2=np.linspace(min(center_G),max(center_G),100)

        #Linear fit D vs G
        idx = np.isfinite(center_G) & np.isfinite(center_D)
        fit_D_G=np.polyfit(np.array(center_G)[idx],np.array(center_D)[idx],1)
        #Plot 2D vs G
        ax_Corr[3].plot(center_G,center_D,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_D_G[0],3)))
        xx2=np.linspace(min(center_G),max(center_G),100)

    xx=np.linspace(min(I21),max(I21),100)
    ax_Corr[0].plot(xx,fit_I21_G[0]*xx+fit_I21_G[1],lw=2,ls='-',color=clr[round(len(clr)/3)])
    idx = np.isfinite(I21) & np.isfinite(center_D)
    fit_I21_D=np.polyfit(np.array(I21)[idx],np.array(center_D)[idx],1)
    ax_Corr[1].plot(I21,center_D,marker='o',ls='',color=clr[round(len(clr)/2)],label=name[z]+', slope='+str(round(fit_I21_D[0],3)))
    ax_Corr[1].plot(xx,fit_I21_D[0]*xx+fit_I21_D[1],lw=2,ls='-',color=clr[round(len(clr)/3)])

    ax_Corr[3].plot(xx2,fit_D_G[0]*xx2+fit_D_G[1],lw=2,ls='-',color=clr[round(len(clr)/3)])

    ax_Corr[2].plot(xx2,fit_2D_G[0]*xx2+fit_2D_G[1],lw=2,ls='-',color=clr[round(len(clr)/3)])

    xlab=['$I_{d}/I_{g}$','$I_{d}/I_{g}$','Raman Shift $G$ band /$ cm^{-1}$','Raman Shift $G$ band /$ cm^{-1}$']
    ylab=['Raman Shift $G$ band /$ cm^{-1}$','Raman Shift $D$ band /$ cm^{-1}$','Raman Shift $2D$ band /$ cm^{-1}$','Raman Shift $D$ band /$ cm^{-1}$']
    titles=['$G$ band Shift vs. $I_{d}/I_{g}$','$D$ band Shift vs. $I_{d}/I_{g}$','$2D$ band Shift vs. $G$ band Shift','$D$ band Shift vs. $G$ band Shift']
    for i,ax in enumerate(ax_Corr):
        ax.legend(fontsize=fs-3)
        ax.tick_params(axis="both", labelsize=fs)
        ax.set_xlabel(xlab[i],fontsize=fs)
        ax.set_ylabel(ylab[i],fontsize=fs)
        ax.set_title(titles[i],fontsize=fs)
        ax.yaxis.set_major_locator(MaxNLocator(integer=True))

#%% #2D Map

    if maps==1:
        if use_leng!=1:
            ratio=col/rows
        else:
            ratio=xlen/ylen
        try:
            fig_Map.clf()
        except NameError:
            pass
        fig_Map=plt.figure('2D Map', figsize=(7*ratio,6), constrained_layout=True)
        ax_Map= fig_Map.add_subplot(111)
        if map_var=='G':
            if nt==1:
                var2D=np.array(center_Gplus)
            else:
                var2D=center_G

        elif map_var=='I':
            var2D=I21
        elif map_var=='D':
            var2D=center_D
        elif map_var=='2D':
            var2D=center_2D
        else:
            print('map_var must be I, G, D, or 2D')
            break

        spec_label={'I':'$I_{d}/I_{g}$',
                    'G':'$G$ band',
                    'D':'$D$ band',
                    '2D':'$2D$ band'}
        if map_var=='I':
            unit=''
        else:
            unit=' Raman Shift /$ cm^{-1}$'

        G2D=var2D.reshape([rows,col])

        if use_leng!=1:
            y1=np.linspace(0,rows-1,rows)
            x1=np.linspace(0,col-1,col)
            ylab='y pixel'
            xlab='x pixel'
        else:
            y1=np.linspace(0,ylen,rows)
            x1=np.linspace(0,xlen,col)
            ylab='y length, ($\mu m$)'
            xlab='x length, ($\mu m$)'

        h=ax_Map.pcolormesh(x1,y1,G2D)
        clbr=plt.colorbar(h,ax=ax_Map)
        clbr.set_label(spec_label[map_var] +unit, rotation=270,labelpad=20,fontsize=fs)
        clbr.ax.tick_params(labelsize=fs)
        ax_Map.set_xlabel(xlab,fontsize=fs)
        ax_Map.set_ylabel(ylab,fontsize=fs)
        ax_Map.set_title('2D Map of '+spec_label[map_var]+' for '+name[z],fontsize=fs)

        ax_Map.tick_params(axis="both", labelsize=fs)
        sz=rows*col
        pixelwidthx=xlen/(2*int(col))
        pixelwidthy=ylen/(2*int(rows))
        x1a=np.linspace(0-pixelwidthx,xlen+pixelwidthx,int(col)+1)
        y1a=np.linspace(0-pixelwidthy,ylen+pixelwidthy,int(rows)+1)
        xx,y1=np.meshgrid(x1,y1)
        xx=xx.flatten()
        y1=y1.flatten()

        fig_Map.savefig(name[z]+'_2DMap_'+map_var+imgtype)

        pkn=5
        line, = ax_Map.plot(xx, y1, 'b',picker=pkn)
        line.set_visible(False)

#%% Save shifts in .csv

    if lorentz==0 and nt==1:
        data_all={'Intensity_G-':Int_Gmin,'Shift_G-':center_Gmin,'FWHM_G-':FWHM_Gmin,'Intensity_G+':Int_Gplus,
                  'Shift_G+':center_Gplus,'FWHM_G+':FWHM_Gplus,'Intensity_D':Int_D,'Shift_D':center_D,'Intensity_2D':Int_2D,'Shift_2D':center_2D}
    elif lorentz==0 and nt == 0:
        data_all={'Intensity_G':Int_G,'Shift_G':center_G,'Intensity_D':Int_D,'Shift_D':center_D,'Intensity_2D':Int_2D,'Shift_2D':center_2D}
    elif lorentz==1 and nt==1:
        data_all={'Intensity_G-':Int_Gmin,'Shift_G-':center_Gmin,'FWHM_G-':FWHM_Gmin,'Intensity_G+':Int_Gplus,
                  'Shift_G+':center_Gplus,'FWHM_G+':FWHM_Gplus,'Intensity_D':Int_D,'Shift_D':center_D,
                  'FWHM_D':FWHM_D,'Intensity_2D':Int_2D,'Shift_2D':center_2D,'FWHM_2D':FWHM_2D}
    elif lorentz==1 and nt==0:
        data_all={'Intensity_G':Int_G,'Shift_G':center_G,'FWHM_G':FWHM_G,'Intensity_D':Int_D,
                  'Shift_D':center_D,'FWHM_D':FWHM_D,'Intensity_2D':Int_2D,'Shift_2D':center_2D,'FWHM_2D':FWHM_2D}

    data_all['Intensity Ratio']=I21

    if rbm==1:
        data_all['Intensity_RBM']=PeaksInt
        data_all['Shift_RBM']=PeaksLoc1

    data_all_df=pd.DataFrame(data_all)
    data_all_df.index.name='Spectra #'
    data_all_df.to_csv(name[z]+'_results.csv')

#%%% Save Figures in Scaleable Vector Format
if raw==1:
    fig_raw.savefig('Raw Data'+imgtype)
if norm==1:
    fig_norm.savefig('Normalized Data'+imgtype)

fig_avg.savefig('Avg Spectra'+imgtype)

if rng==1:
    fig_rng.savefig('SpecRangeIR'+imgtype)

fig_IR.savefig('IntensityRatio'+imgtype)
fig_2D.savefig('2DBand'+imgtype)
fig_G.savefig('Gband'+imgtype)
fig_D.savefig('Dband'+imgtype)

if correlations==1:
    fig_Corr.savefig('Correlations'+imgtype)

if nt==1:
    fig_G_I.savefig('IntensityRatioG+G-'+imgtype)

#%% Pick Event for 2D Map

def onpick(event):
    global pts
    global ptsM
    global ind
    global figFC
    global axFC
    global data_spec

    if 'ind' in globals():
        del ind

    try:
        pts
    except NameError:
        pts = None

    if pts!=None:
        if plt.fignum_exists('Spectra'):
            pass
        else:
            for p in pts:
                for pp in p:
                    pp.remove()

    if plt.fignum_exists('Spectra'):
        pass
    else:
        figFC = plt.figure(num='Spectra',figsize=(10*1.5,5*1.5))
        axFC = figFC.add_subplot(111)
        data_spec=pd.DataFrame()
        data_spec['Raman Shift(cm-1)']=Shift
        pts=[]

    if event.artist!=line:  #check that you clicked on the object you wanted
        return True
    if not len(event.ind):  #check the index is valid
        return True

    ind = event.ind[0]

    if map_var=='I':
        unit=''
    else:
        unit=' $cm^{-1}$'

    pts.append(ax_Map.plot(xx[ind],y1[ind],'ro'))

    if use_leng!=1:
        labp='x:'+str(int(xx[ind]))+', y:'+str(int(y1[ind]))
    else:
        labp='x:'+str(round_sig(xx[ind],3))+'$\mu m$, y:'+str(round_sig(y1[ind],3))+'$\mu m$'

    data_spec[labp]=Intensity_norm[:,ind]

    #Plot spectra for selected pixel
    if use_leng!=1:
        axFC.plot(Shift,Intensity_norm[:,ind],
                  label=labp+', '+spec_label[map_var]+': '+str(round_sig(var2D[ind],5))+unit,
                  markersize=1)
        print('\nPixel Location x:',int(xx[ind]),' y:',int(y1[ind]),'\n')

    else:
        axFC.plot(Shift,Intensity_norm[:,ind], label=labp+', '+spec_label[map_var]+': '+str(round_sig(var2D[ind],5))+unit, markersize=1)
        print('\nPixel Location :'+str(round_sig(xx[ind],3))+' $\mu m$, y:'+str(round_sig(y1[ind],3))+' $\mu m$ \n')

    axFC.set_xlabel('Raman shift / $cm^{-1}$',fontsize=fs)
    axFC.set_ylabel('Intensity',fontsize=fs)
    axFC.legend(fontsize=fs-1)
    axFC.set_title('Selected Normalized Spectra on '+name[z],fontsize=fs+1)

    axFC.tick_params(axis="both", labelsize=fs)
    print('Intensity Ratio is '+str(round_sig(I21[ind],5))+'\n')
    if nt==1:
        print('G+ band Shift is '+str(round_sig(center_Gplus[ind],5))+' cm-1 \n')
    else:
        print('G band Shift is '+str(center_G[ind])+' cm-1 \n')
    print('D band Shift is '+str(center_D[ind])+' cm-1 \n')
    print('2D band Shift is '+str(center_2D[ind])+' cm-1  \n')

    data_spec.to_csv('Selected_Spectra.csv',index=False)
    fig_Map.canvas.draw()
    figFC.canvas.draw()
    figFC.savefig('SelectedSpectra'+imgtype)
    return True

if maps==1:
    fig_Map.canvas.mpl_connect('pick_event', onpick)


from __future__ import absolute_import
from __future__ import division

import os
import sys
import argparse

__author__ = "Jonathan Madsen"
__copyright__ = "Copyright 2020, The Regents of the University of California"
__credits__ = ["Jonathan Madsen"]
__license__ = "MIT"
__version__ = "@PROJECT_VERSION@"
__maintainer__ = "Jonathan Madsen"
__email__ = "jrmadsen@lbl.gov"
__status__ = "Development"

from .analyze import (
    load,
    match,
    search,
    expression,
    sort,
    group,
    add,
    subtract,
    unify,
    dump,
)

def embedded_analyze(
    args=None,
    data=[],
    call_exit=False,
    verbose=os.environ.get("TIMEMORY_VERBOSE", 0),
    ranks=[],
):
    """This is intended to be called from the embedded python interpreter"""
    if len(ranks) > 0:
        try:
            import mpi4py  # noqa: F401
            from mpi4py import MPI  # noqa: F401

            rank = MPI.COMM_WORLD.Get_rank()
            if rank not in ranks:
                return
        except (ImportError, RuntimeError):
            pass

    call_exit = False
    cmd_line = False
    if args is None:
        args = sys.argv[:]
        call_exit = True
        cmd_line = True

    try:
        parser = argparse.ArgumentParser()
        parser.add_argument(
            "files",
            metavar="file",
            type=str,
            nargs="*" if not cmd_line else "+",
            help="Files to analyze",
            default=[],
        )
        parser.add_argument(
            "-f",
            "--format",
            type=str,
            help="Data output format.",
            choices=(
                "dot",
                "flamegraph",
                "tree",
                "table",
                "markdown",
                "html",
                "markdown_grid",
            ),
            nargs="*",
            default=None,
        )
        parser.add_argument(
            "-o",
            "--output",
            type=str,
            help="Output to file(s)",
            nargs="*",
            default=None,
        )
        parser.add_argument(
            "-M",
            "--mode",
            help="Analysis mode",
            nargs="*",
            choices=("add", "subtract", "unify", "group"),
            default=[],
        )
        parser.add_argument(
            "-m",
            "--metric",
            "--column",
            type=str,
            help="Metric(s) to extract",
            default="sum.*(.inc)$",
        )
        parser.add_argument(
            "-s",
            "--sort",
            type=str,
            help="Sort the metric",
            choices=("ascending", "descending"),
            default=None,
        )
        parser.add_argument(
            "-g",
            "--group",
            type=str,
            help="Group by a column name in the dataframe",
            default=None,
        )
        parser.add_argument(
            "--field",
            type=str,
            help="Dataframe column to search/match against",
            default="name",
        )
        parser.add_argument(
            "--search",
            type=str,
            default=None,
            help="Regular expression for re.search(...), i.e. a substring match",
        )
        parser.add_argument(
            "--match",
            type=str,
            default=None,
            help="Regular expression for re.match(...), i.e. a full string match",
        )
        parser.add_argument(
            "--expression",
            type=str,
            default=None,
            help=(
                "A space-delimited comparison operation expression using 'x' "
                + "for the variable, numerical values, and: < <= > >= && ||. "
                + "E.g. 'x > 1.0e3 && x < 100000'. 'x' will be -m/--metric"
            ),
        )
        parser.add_argument(
            "-e",
            "--echo-dart",
            help="echo Dart measurement for CDash",
            action="store_true",
        )
        parser.add_argument(
            "--per-thread",
            help=(
                "Encode the thread ID in node hash to ensure squashing doesn't "
                + "combine thread-data"
            ),
            action="store_true",
        )
        parser.add_argument(
            "--per-rank",
            help=(
                "Encode the rank ID in node hash to ensure squashing doesn't "
                + "combine rank-data"
            ),
            action="store_true",
        )
        parser.add_argument(
            "--select",
            help=(
                "Select the component type if the JSON input contains output "
                + "from multiple components"
            ),
            type=str,
            default=None,
        )
        parser.add_argument(
            "--exit-on-failure",
            help="Abort with non-zero exit code if errors arise",
            action="store_true",
        )

        _args = parser.parse_args(args)

        if _args.exit_on_failure:
            call_exit = True

        if _args.group is None and _args.format is None:
            _args.format = ["tree"]
        if _args.group is not None and _args.format not in ("table", None):
            raise RuntimeError("Invalid data format for group")

        if isinstance(_args.select, str):
            _args.select = _args.select.split()

        gfs = []
        for itr in data:
            if not isinstance(itr, dict):
                print("data: {}\ndata-type: {}".format(itr, type(itr).__name__))
            gfs.append(
                load(
                    itr,
                    select=_args.select,
                    per_thread=_args.per_thread,
                    per_rank=_args.per_rank,
                )
            )
        for itr in _args.files:
            if not os.path.exists(itr):
                print("file: {}\nfile-type: {}".format(itr, type(itr).__name__))
            gfs.append(
                load(
                    itr,
                    select=_args.select,
                    per_thread=_args.per_thread,
                    per_rank=_args.per_rank,
                )
            )

        def apply(_input, _mode, *_args, **_kwargs):
            if _mode == "search":
                return search(_input, *_args, **_kwargs)
            elif _mode == "match":
                return match(_input, *_args, **_kwargs)
            elif _mode == "expression":
                return expression(_input, *_args, **_kwargs)
            elif _mode == "sort":
                return sort(_input, *_args, **_kwargs)
            elif _mode == "add":
                return add(_input)
            elif _mode == "subtract":
                return subtract(_input)
            elif _mode == "unify":
                return unify(_input)
            elif _mode == "group":
                return group(_input, *_args, **_kwargs)

        # apply search before match since this is less restrictive
        if _args.search is not None:
            gfs = apply(gfs, "search", pattern=_args.search, field=_args.field)

        # apply match after search since this is more restrictive
        if _args.match is not None:
            gfs = apply(gfs, "match", pattern=_args.match, field=_args.field)

        # apply numerical expression last
        if _args.expression is not None:
            gfs = apply(
                gfs,
                "expression",
                math_expr=_args.expression,
                metric=_args.metric,
            )

        # apply the mutating operations
        if "add" in _args.mode:
            gfs = apply(gfs, "add")
        elif "subtract" in _args.mode:
            gfs = apply(gfs, "subtract")
        elif "unify" in _args.mode:
            gfs = apply(gfs, "unify")

        if _args.sort is not None:
            gfs = apply(
                gfs,
                "sort",
                metric=_args.metric,
                ascending=(_args.sort == "ascending"),
            )

        files = _args.output
        if files is not None and len(files) == 1:
            files = files[0]

        if _args.group is not None:
            gfs = apply(
                gfs,
                "group",
                metric=_args.metric,
                field=_args.group,
                ascending=(_args.sort == "ascending"),
            )
            _args.format = [None]

        for fmt in _args.format:
            dump(gfs, _args.metric, fmt, files, _args.echo_dart)

    except Exception as e:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        import traceback

        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=10)
        print("Exception - {}".format(e))
        if call_exit or _args.exit_on_failure:
            sys.exit(1)
        elif not cmd_line:
            raise


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

# In[2]:

import networkx as nx
import pandas as pd

import matplotlib.pyplot as plt
df = pd.read_excel('Dataset.xlsx', index = None)
df.head()
G=nx.from_pandas_edgelist(df, 'Departure Station', 'Arrival Station','Time of Travel')
nx.draw(G, with_labels=False)
df['Route'] = df['Departure Station'].map(str) + ' - ' + df['Arrival Station'].map(str)
df.head()
df2 = df[['Route','Tube Line', 'Time of Travel']]
df3=df2.drop_duplicates()
df3['Route Reversed'] = df['Arrival Station'].map(str) + ' - ' + df['Departure Station'].map(str)
df6 = df3[['Route Reversed', 'Tube Line', 'Time of Travel']]
df6.rename(columns = {'Route Reversed' : 'Route'}, inplace = True)
df3 = df3.append(df6, ignore_index = True)
del df3['Route Reversed']

def route_calculator(source,target):

    d, p = nx.single_source_dijkstra(G,source=source, target=target, weight='Time of Travel')
    route=[]
    for i in range(len(p)-1):
        route.append(str(p[i]+" - "+p[i+1]))
    df4 = pd.DataFrame(route, columns=["Route"])
    df5 = pd.merge(df4,df3,"left", left_on="Route", right_on="Route")
    df5.to_excel('Result.xlsx')

    print(df5)
    print("Total time for Travel", d+ len(p) -2 , "Mins (Includes 1 min wait time at each station :))")


import requests

_BASE_WORD_URL = 'http://www.dictionary.com/browse/'
_DEFAULT_HEADERS = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
    'host': 'www.dictionary.com'
}
_DEFAULT_AUDIO_HEADERS = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
}

def get_word_page(word, timeout=None):
    return requests.get(
        _BASE_WORD_URL + word,
        headers=_DEFAULT_HEADERS,
        timeout=timeout)

def download_word_pronunciation(url):
    res = requests.get(url, headers=_DEFAULT_AUDIO_HEADERS, stream=True)
    audio = res.content
    content_type = res.headers.get('content-type', 'audio/mpeg3')
    return audio, content_type


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

# In[93]:

import numpy as np
import networkx as nx
import pandas as pd
import time

# # Load ca-GrQc

# In[94]:

def load_ca_grqc():
    data = pd.read_csv('CA-GrQc.txt', sep="\t", skiprows=3)
    records = data.to_records(index=False)
    edges = list(records)
    G = nx.DiGraph()
    G.add_edges_from(edges)

    return G

# # Load github social

# In[95]:

def load_github_social():
    data = pd.read_csv('git_web_ml/musae_git_edges.csv')
    records = data.to_records(index=False)
    edges = list(records)
    G = nx.Graph()
    G.add_edges_from(edges)
    return G

# # Load web-berkstan

# In[96]:

def load_web_berkstan():
    data = pd.read_csv('web-BerkStan.txt', sep="\t", skiprows=3)

    records = data.to_records(index=False)
    edges = list(records)
    G = nx.DiGraph()
    G.add_edges_from(edges)

    return G

# ## Chiba-Nishizeki

# In[97]:

def Chiba_Nishizeki(G):
    nodes_degree_sorted = sorted(G.degree, key=lambda x: x[1], reverse=True)
    nodes_sorted = [x[0] for x in nodes_degree_sorted]
    num_nodes = len(G.nodes)
    mark_dict = dict(zip(nodes_sorted,np.zeros(num_nodes)))
    triangle_list = []

    for i in range(num_nodes-2):
        v = nodes_sorted[i]
        for u in G.neighbors(v):
            mark_dict[u] = 1
        for u in G.neighbors(v):
            for w in G.neighbors(u):
                if(w!=v and mark_dict[w]==1):
                    triangle_list.append((v,u,w))
            mark_dict[u] = 0
        G.remove_node(v)

    return triangle_list

# ## Trie Iterator

# In[ ]:

class iterator:
    """
    Parameters:
        G: graph to iterator
        depth_nodes: nodes at each depth. If depth==0, iterate over all nodes. If depth==1, iterate over neighbors
            of specific nodes.
        depth: current depth the iterator is at
        depth_idx: index of node that the iterator is stopped at for each depth.
        current_node: current node the iterator is at
    """

    def __init__(self,G):
        self.atEnd = False;
        self.G = G
        self.depth_nodes = [sorted(list(self.G.nodes)),[]]
        self.depth = -1
        self.depth_idx = [0,0]
        self.current_node = None

    def key(self):
        return self.current_node

    def next(self):
        self.depth_idx[self.depth]+=1
        if(self.itr_atEnd() != True):
            self.current_node = (self.depth_nodes[self.depth])[self.depth_idx[self.depth]]
        else:
            self.current_node = None

    def seek(self,seekKey=0):
        if(self.current_node!=None and seekKey!=None):
            while(self.current_node<seekKey):
                if(self.itr_atEnd()!=True):
                    self.next()
                else:
                    break

                if(self.itr_atEnd()==True):
                    break

    def itr_atEnd(self):
        if (self.depth_idx[self.depth]==len(self.depth_nodes[self.depth])):
            return True
        else:
            return False

    def open(self):
        if(self.depth==-1):
            self.depth+=1
            self.current_node = (self.depth_nodes[self.depth])[self.depth_idx[self.depth]]
        elif(self.depth==0):
            neighbors = sorted(list(self.G.neighbors(self.current_node)))
            neighbors = [i for i in neighbors if i>self.current_node]
            self.depth+=1
            if(len(neighbors)==0):
                self.current_node=None
                self.depth_nodes[self.depth] = neighbors
            else:
                self.depth_nodes[self.depth] = neighbors
                self.current_node = (self.depth_nodes[self.depth])[0]
        #print(self.depth)

    def up(self):
        self.depth_idx[self.depth] = 0
        self.depth -= 1
        #print(self.depth)
        if(self.depth==-1):
            self.current_node = None
        else:
            self.current_node = (self.depth_nodes[self.depth])[self.depth_idx[self.depth]]

    def depth(self):
        return self.depth

    def get_end(self):
        return self.atEnd

# ## Leapfrog join

# In[99]:

class leapfrog_join:
    """
    Parameters:
        iterators: list of iterators that this join is using
        k: number of iterators
        p: index of current using iterator
        atEnd: whether this join is finished
        depth: indicates the variable that this leapfrog join is care about.
                Suppose do triejoin between R(a,b), S(b,c), T(a,c), we have depth 0 for a, 1 for b, 2 for c
    """
    def __init__(self,iterators,depth):
        self.iterators = iterators
        self.k = len(iterators)
        self.p = 0
        self.atEnd = False
        self.depth = depth

    #After init, the first result is received
    def leapfrog_init(self):
        for it in self.iterators:
            if(it.itr_atEnd()==True):
                self.atEnd = True
                return None
        count = -1
        for it in self.iterators:
            count += 1
            if(it.key()==None):
                print("false",count)

        self.atEnd = False
        self.iterators = sorted(self.iterators,key=lambda itr: itr.key())
        self.p = 0
        return self.leapfrog_search()

    def leapfrog_search(self):
        max_key = self.iterators[(self.p-1)%self.k].key()
        done = False
        while(done != True):
            least_key = self.iterators[self.p].key()
            if(least_key==max_key):
                done = True
                return max_key
            else:
                self.iterators[self.p].seek(max_key)
                if(self.iterators[self.p].itr_atEnd()):
                    return None
                else:
                    max_key = self.iterators[self.p].key()
                    self.p = (self.p+1)%self.k
    #After the first result, repeated call leapfrog_next() to get following results.
    def leapfrog_next(self):
        self.iterators[self.p].next()
        if(self.iterators[self.p].itr_atEnd()):
            self.atEnd = True
            return None
        else:
            self.p = (self.p+1)%self.k
            return self.leapfrog_search()
    #Locate the iterators at specific key
    def leapfrog_seek(self,seekKey):
        self.iterators[self.p].seek(seekKey)
        if(self.iterators[self.p].itr_atEnd()):
            self.atEnd = True
            return None
        else:
            self.p = (self.p+1)%self.k
            return self.leapfrog_search()

    def iterators_open(self):
        for itr in self.iterators:
            itr.open()
    def iterators_up(self):
        for itr in self.iterators:
            itr.up()

    def get_depth(self):
        return self.depth

# ## Leapfrog Triejoin

# In[100]:

#join of R(a,b), S(b,c), T(a,c)
#in our problem, it is the join of E,E,E
class leapfrog_triejoin:
    def __init__(self,G):
        #Create 3 iterators, each corresponds to R, S, T
        self.iterators = [iterator(G),iterator(G),iterator(G)]
        self.depth = -1
        #Create 3 leapfrog join instances, each corresponds to R&T,R&S,S&T(& represents join)
        self.leapfrog_join1 = leapfrog_join([self.iterators[0],self.iterators[2]],depth=0)
        self.leapfrog_join2 = leapfrog_join([self.iterators[0],self.iterators[1]],depth=1)
        self.leapfrog_join3 = leapfrog_join([self.iterators[1],self.iterators[2]],depth=2)

        self.leapfrog_joins = [self.leapfrog_join1,self.leapfrog_join2,self.leapfrog_join3]

    def triejoin_up(self):
        for lpfj in self.leapfrog_joins:
            if (lpfj.get_depth()==self.depth):
                lpfj.iterators_up()
        self.depth -= 1

    def triejoin_open(self):
        self.depth += 1
        for lpfj in self.leapfrog_joins:
            if (lpfj.get_depth()==self.depth):
                lpfj.iterators_open()
                return lpfj.leapfrog_init()

    def run(self):
        triangles = []
        #itr0->a,itr2->a
        a = self.triejoin_open()
        while(a!=None):
            #itr0->b,itr1->b
            b = self.triejoin_open()
            while(b!=None):
                #itr1->c,itr2->c
                c = self.triejoin_open()
                while(c!=None):
                    triangles.append((a,b,c))
                    c = self.leapfrog_joins[2].leapfrog_next()
                self.triejoin_up()
                b = self.leapfrog_joins[1].leapfrog_next()
            self.triejoin_up()
            a = self.leapfrog_joins[0].leapfrog_next()
        return triangles

# In[121]:

#We run each algorithm for each dataset 100 times, and record the time cost
def main():
    #for ca-GrQc:
    cn_ca_t = []
    lpfj_ca_t = []
    G_ca = load_ca_grqc()
    for _ in range(100):
        #record c-n runtime
        start_time = time.time()
        Chiba_Nishizeki(G_ca)
        cn_ca_t.append(time.time() - start_time)
        #record wcoj runtime
        start_time = time.time()
        leapfrog_triejoin(G_ca)
        lpfj_ca_t.append(time.time() - start_time)

    ca_t = [cn_ca_t,lpfj_ca_t]

    #for github social:
    cn_gs_t = []
    lpfj_gs_t = []
    G_gs = load_github_social()
    for _ in range(100):
        #record c-n runtime
        start_time = time.time()
        Chiba_Nishizeki(G_gs)
        cn_gs_t.append(time.time() - start_time)
        #record wcoj runtime
        start_time = time.time()
        leapfrog_triejoin(G_gs)
        lpfj_gs_t.append(time.time() - start_time)
    gs_t = [cn_gs_t,lpfj_gs_t]

    #for web-berkstan:
    cn_wb_t = []
    lpfj_wb_t = []
    G_wb = load_web_berkstan()
    for _ in range(100):
        #record c-n runtime
        start_time = time.time()
        Chiba_Nishizeki(G_wb)
        cn_wb_t.append(time.time() - start_time)
        #record wcoj runtime
        start_time = time.time()
        leapfrog_triejoin(G_wb)
        lpfj_wb_t.append(time.time() - start_time)

    wb_t = [cn_wb_t,lpfj_wb_t]

    t = [ca_t,gs_t,wb_t]

    return t

# In[ ]:

if __name__ == "__main__":
    t = main()
    cn_mean = []
    lpfj_mean = []
    for t_i in t:
        cn_i = t_i[0]
        lpfj_i = t_i[1]
        print("CN mean:",np.mean(cn_i))
        print("lpfj mean:",np.mean(lpfj_i))
        cn_mean.append(np.mean(cn_i))
        lpfj_mean.append(np.mean(lpfj_i))

        names = ['ca-GrQc', 'github social', 'web-berkstan']

        plt.figure(figsize=(12, 8))

        plt.bar(names, cn_mean,label='Chiba-Nishizeki')
        plt.bar(names,lpfj_mean,label='WCOJ')

        plt.suptitle('Average run time')
        plt.legend()
        plt.show()


# coding: utf-8

# In[ ]:

# In[29]:

#起始標準起手式~
import requests as rq
from bs4 import BeautifulSoup as bs
from collections import OrderedDict as od
import json
import csv
import traceback as tb
import re

# In[30]:

HOST = 'http://www.dodocook.com/recipe/'

# In[31]:

#def dodocook_crawler(開始文章ID,結束文章ID)
def dodocook_crawler(no_start_page,no_stop_page):
    HOST = 'http://www.dodocook.com/recipe/'
    #在pass這裡接上function() 網址產生器
    #pass
    url_builder(HOST,no_start_page,no_stop_page)

# In[32]:

#網址產生器
#url_builder(網站URL, 開始文章ID, 結束文章ID)
def url_builder(HOST,no_start_page,no_stop_page):
    for i in range(no_start_page, no_stop_page):
        url = HOST + "{}/".format(i)
        print("[INFO] {}".format(url))
        #在pass這裡接上function()  res_and_soup(url)
        #pass
        res_and_soup(url)

# In[33]:

#就字面意思
def res_and_soup(url):
    try:
        res = rq.get(url)
        soup = bs(res.text, 'lxml')
        print("[INFO] success")
        #在pass這裡接上爬取網頁內容的function()
        #pass
        dodocook_contents(soup)
    except:
        print("[Error] Error while getting contents")
        tb.print_exc()

# In[34]:

def dodocook_contents(soup):
    #title
    mydict={}
    for tag1 in soup.select(".band > h1"):
        title=tag1.text
    mydict["title"]=title

    #foodname
    foodName=[]
    for tag2 in soup.select(".body > .list > .name"):
        tag2_dict={}
        n=soup.select(".body > .list > .name").index(tag2)
        tag2_dict[tag2.text]=soup.select(".ingredient > .body > .list > .count")[n].text
        foodName.append(tag2_dict)
    mydict["foodName"]=foodName

    #step
    step1=[]
    step_list1=soup.select('.steps > .CSrtB > .Sno')
    step_go1=soup.select('.steps > .CSrtB > .SBpma')

#     step2=[]
#     step_list2=soup.select('.steps > .CSrtA > .SrtAk > .Sno')
#     for tag3 in step_list2:
#         print(tag3.text)
#     step_go2=soup.select('.steps > .CSrtA > .SrtAk > .Spma > p')
#     for tag4 in step_go2:
#         print(tag4.text)

    for tag3 in step_list1:
        tag3_dict={}
        n1=step_list1.index(tag3)
        tag3_dict[tag3.text]=step_go1[n1].text
        step1.append(tag3_dict)
    mydict["step"]=step1
    print(mydict)

# In[35]:

dodocook_crawler(40000,40010)

# In[ ]:


import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
import csv

FILE_PATH = "C:\\data\\fh_mal_train"
GRAPH_PATH = "C:\\code\\count_mal.pickle"

def get_size(start_path = '.'):
    total_size = 0
    total_count =0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
            total_count +=1
    return total_size,total_count

def haha(start_path):
    cal = list()
    for x in  os.listdir(start_path):
        start_path2 = os.path.join(start_path, x)
        if os.path.isdir(start_path2):
            year = x
            for x2 in os.listdir(start_path2):
                start_path3 = os.path.join(start_path2, x2)
                if os.path.isdir(start_path3):
                    month = x2
                    for x3 in os.listdir(start_path3):
                        start_path4 = os.path.join(start_path3, x3)
                        if os.path.isdir(start_path4):
                            day = x3
                            start_path6 = os.path.join(start_path4, 'total')
                            if os.path.isdir(start_path6):
                                haha= list(year,month,day,get_size(start_path6))
                                cal.append(haha)
    return cal

def draw_graph(gpath):
    f = open('count_mal.pickle','rb')
    dirdata = pickle.load(f)
    x=list()
    y=list()
    for t in dirdata:
        x.append(t[2])
        y.append(t[3][0])
    print(dirdata)
    plt.xlabel("day")
    plt.ylabel("data size")
    plt.plot(y)
    plt.show()

def writecsv(gpath):
    f = open('haha.csv','w',encoding='utf-8',newline='')
    wr = csv.writer(f)
    wr.writerow(['year','month','date','size','count'])
    f2 = open('count_mal.pickle', 'rb')
    dirdata = pickle.load(f2)
    dirdata = sorted(dirdata)
    for t in dirdata:
        t[2]=t[2][4:]
        wr.writerow([t[0],t[1],t[2],t[3][0],t[3][1]])

#print(haha(GRAPH_PATH))
#draw_graph(GRAPH_PATH)
writecsv(GRAPH_PATH)


# Задание №1

from sys import argv
hours_worked_out, rate_for_the_time, prize = argv
hours_worked_out = int(hours_worked_out)
rate_for_the_time = int(rate_for_the_time)
prize = int(prize)
result = int(hours_worked_out * rate_for_the_time + prize)
print(f"Зраработная плата сотрудника составит - {result}")

# Задание №2

my_list = [300, 2, 12, 44, 1, 1, 4, 10, 7, 1, 78, 123, 55]
new_list = []
num = 0
for num, el in enumerate(my_list):
    if my_list[num - 1] < my_list[num]:
        new_list.append(el)
        num += 1
print(new_list)

# Задание №3

print(f"Результат -  {[el for el in range(20, 240) if el % 20 == 0 or el % 21 == 0]}")

# Задание №4

my_list = [2, 2, 2, 7, 23, 1, 44, 44, 3, 2, 10, 7, 4, 11]

new_list = []

for el in my_list:
    if my_list.count(el) < 2:
        new_list.append(el)

print(new_list)

# Задание №5

from functools import reduce

def my_func(prev_el, el):
    return prev_el * el

my_list = range(100, 1001)
new_list = []
for el in my_list:
    if el % 2 == 0:
        new_list.append(el)
print(new_list)
print(f"Результат умножения - {reduce(my_func, new_list)}")

# Задание №6

from itertools import count, cycle
el_count = int(input("Введите целое число с которого начнется итерация: "))
el_count_end = int(input("Введите целое число, до которого будет идти итерация: "))
for el in count(el_count):
    if el > el_count_end:
        break
    else:
        print(el)
el_cycle = input("Введите данные для вывода повторений на экран: ")
print(el_cycle)
el_cycle_num = 1
for el in cycle(el_cycle):
    if el_cycle_num > 9:
        break
    else:
        print(el_cycle)
    el_cycle_num += 1

# Задание №7

from itertools import count
from math import factorial as fact

def my_func():
    for el in count(0, 1):
        yield fact(el)

generate = my_func()
x = 0
n = int(input("До какого значения будем генерировать? :"))
for i in generate:
    if x < n:
        print(i)
        x += 1
    else:
        break


from database.database import Databases
import uuid
import time

class scoreDB(Databases):
    def __init__(self):
        super().__init__()
        self.avg_score_table = "avg_score"
        self.realtime_score_table = "realtime_score"
        self.subject_name_table = "subject"
        self.user_subject_rel = "user_subject"

    def return_all(self):
        query = "SELECT * FROM public.user_class_rel"
        row = self.execute(query)
        print(row)

    def search_AVG(self,userid):
        """
        유저의 평균 반환
        """
        query = f"SELECT * FROM public.userrank WHERE userid = '{userid}'"
        row = self.execute(query)
        print(row)
        return row

    def user_Rank(self):
        """
        유저별 평균을 묶어서 반환, groupby로 유저 묶고, 해당 그룹의 평균 값을 리턴
        return: 평균 묶어서 반환한 값
        """
        query = "SELECT public.userinfo.userid,username, AVG(score) FROM public.user_class_rel, public.userinfo WHERE  public.userinfo.userid = public.user_class_rel.userid  GROUP BY public.userinfo.userid ORDER BY AVG(score) DESC"
        row = self.execute(query)
        return row

    def class_Rank(self):
        """
        과목별 평균을 묶어서 반환, groupby로 유저 묶고, 해당 그룹의 평균 값을 리턴
        return: 평균 묶어서 반환한 값
        """
        query = "SELECT classid, AVG(score) FROM public.user_class_rel GROUP BY classid ORDER BY AVG(score) DESC"
        row = self.execute(query)
        print(row)

    def search_subject_classname(self, classname):
        """
        과목명
        해당 과목이 존재하는지 여부를 반환
        return: True, False
        """
        query = f"SELECT * FROM public.class where classname = '{classname}'"
        row = self.execute(query)
        print(row)

    def write_user_score(self, userid, classid, score):
        """
        유저의 점수를 기록
        """
        createdAt = time.localtime(time.time())
        id = str(uuid.uuid4().hex)
        query = "INSERT INTO user_class_rel(id, userid, classid, score, createdAt) VALUES (%s, %s, %s, %s, NOW()) RETURNING id"
        stocked = (id, userid, classid, score)
        row = self.execute(query, stocked)
        print(row)
        self.commit()

    def delete_class(self, classid, userid):
        class_generator = self.execute(f"SELECT generatorid FROM class WHERE classid='{classid}'")[0][0]
        if userid != class_generator:
            return False
        else:
            query = f"DELETE FROM class WHERE classid = '{classid}' RETURNING classid"
            row = self.execute(query)
            self.commit()
            return True

    def std_summary(self, classid):
        query = f"SELECT u.userid,u.username,u.useremail, AVG(score) as avgs FROM public.user_class_rel as r JOIN public.userinfo as u ON r.userid = u.userid WHERE r.classid='{classid}' GROUP BY u.userid ORDER BY avgs DESC;"
        row = self.execute(query)

        return row

    def search_user_subject(self, userid, classid):
        """
        유저의 과목별 점수를 검색
        return : 점수 (INT)
        """
        query = f"SELECT score FROM public.user_class_rel WHERE userid = '{userid}' AND classid = '{classid}';"
        row = self.execute(query)
        print(row)

    def subject_AVG(self, classid):
        """
        수업을 들은 유저들을 모아서 유저들의 평균을 내서 보여준다. (새로 짜기)
        """
        query = f"SELECT classid, avg(score) FROM public.user_class_rel WHERE classid = '{classid}' GROUP BY classid ;"
        row = self.execute(query)

        return row

    def class_search(self, que):
        query = f"SELECT c.classid, c.classname, u.username, u.userid FROM public.class as c JOIN public.userinfo as u ON c.generatorid = u.userid where username ilike '%%{que}%%' OR classname ilike '%%{que}%%'"
        print(query)
        row = self.execute(query)
        return row

    def my_classes(self, userid):
        query = f"SELECT c.classid, c.classname, u.username, u.userid FROM public.class as c JOIN public.userinfo as u ON c.generatorid = u.userid WHERE u.userid = '{userid}'"
        print(query)
        row = self.execute(query)
        return row

    def class_detail(self, classid):
        query = f"SELECT c.classid, c.classname, u.username, u.userid, u.useremail FROM public.class as c JOIN public.userinfo as u ON c.generatorid = u.userid where classid ='{classid}'"
        row = self.execute(query)
        return row

    def generate_class(self, generatorid, classname):
        """
        수업 생성자 id, 수업 이름을 받아서, 임의의 classid를 부여하고, 그것으로 수업 생성
        return : None
        """
        classid = str(uuid.uuid4().hex)
        query = "INSERT INTO public.class(classid, generatorid, classname, isopen) VALUES (%s, %s, %s, %s) RETURNING classid"
        stocked = (classid, generatorid, classname, '1')
        row = self.execute(query, stocked)
        self.commit()
        return row

if __name__ == "__main__":
    udb = scoreDB()
    udb.return_all()


from collections import deque

# recursive solution
""" runtime O(n), space O(n) """
def getHeight(root):
    if not root:
        return 0
    return 1 + max(getHeight(root.left), getHeight(root.right))

# max height of the tree should be max height of stack
""" runtime O(n), space O(n) """
def getHeight_iter(root):
    if not root:
        return 0
    max_h = 0
    q = deque([])
    q.append(root)
    # perform a level order traversal
    while q:
        size = len(q)
        for _ in range(size):
            node = q.popleft()
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
        max_h += 1
    return max_h


from cgi import print_environ
import torch
from collections import OrderedDict
from maml import MAML

def adaptation(model, optimizer, train_x,train_y, val_x,val_y, loss_fn, lr, train_step, train, device):
    predictions = []
    labels = []
    epoch_loss = 0

    # x_train, y_train = trainbatch    #x_train テンソル化した画像、y_train ラベル(0 : crack , 1 : good)
    #print("x_train.size(0) : ",x_train.size(0))  #バッチサイズ 2
    for idx in range(train_x.size(0)):
        weights = OrderedDict(model.named_parameters())
        #print("weights : " , weights)

        # k-shotでadaptation
        for iter in range(train_step):
            logits = model.adaptation(train_x, weights)
            #print("logits1 : " , logits)
            # print("input_y1 : " , input_y1)
            #print("input_y1 : " , train_y)
            #logits = model.adaptation(input_x, weights)
            loss = loss_fn(logits, train_y)
            #print("train_loss1" , loss)
            # loss = loss_fn(logits, input_y)
            gradients = torch.autograd.grad(loss, weights.values(), create_graph=train)

            weights = OrderedDict((name, param - lr * grad) for ((name, param), grad) in zip(weights.items(), gradients))

        # queryで評価
        logits = model.adaptation(val_x, weights)
        #print("logits2 : " , logits)
        #print("val_y1 : " , val_y)
        loss = loss_fn(logits, val_y)
        #print("train_loss2" , loss)
        if train:
            model.train()
            optimizer.zero_grad()
            loss.backward(retain_graph=True)
            #print("train_loss3" , loss)
            optimizer.step()
            #epoch_loss += loss.item() *  val_x.size(0)

        y_pred = logits.softmax(dim=1)
        predictions.append(y_pred)
        labels.append(train_y)

    #print("labels : " , labels)
    #print("predictions : " , predictions)
    y_pred = torch.cat(predictions)
    y_label = torch.cat(labels)
    batch_acc = torch.eq(y_pred.argmax(dim=-1), y_label).sum().item() / y_pred.shape[0]
    return loss, batch_acc

def test(model, test_x, test_y, val_x, val_y, loss_fn, lr, train_step, device):
    # x_train, y_train = batch    #x_train テンソル化した画像、y_train ラベル(0 : crack , 1 : good)
    # x_val, y_val = batch
    predictions = []
    labels = []

    for idx in range(test_x.size(0)):
        weights = OrderedDict(model.named_parameters())

        # k-shotでadaptation
        for iter in range(train_step):
            logits = model.adaptation(test_x, weights)
            loss = loss_fn(logits, test_y)
            gradients = torch.autograd.grad(loss, weights.values())

            weights = OrderedDict((name, param - lr * grad) for ((name, param), grad) in zip(weights.items(), gradients))

        # queryで評価
        with torch.no_grad():
            # input_x = x_val[idx].to(device)
            # input_y = y_val[idx].to(device)
            # logits = model.adaptation(input_x, weights)
            # loss = loss_fn(logits, input_y)
            logits = model.adaptation(val_x, weights)
            loss = loss_fn(logits, val_y)

            y_pred = logits.softmax(dim=1)
            #print("y_pred : " , y_pred)
            s, predicted = torch.max(y_pred, 1)
            #score = s[0]
            # print("score : ", score)
            # score = score.to("cpu").detach().numpy().copy()

            predictions.append(y_pred)
            #print("predicted : " , predicted)
            labels.append(test_y)

    y_pred = torch.cat(predictions)
    #print("y_pred" , y_pred)
    y_label = torch.cat(labels)
    batch_acc = torch.eq(y_pred.argmax(dim=-1), y_label).sum().item() / y_pred.shape[0]
    #return loss, batch_acc ,predicted

    return loss, batch_acc ,predicted


#  This is the model that agent acts on.
import torch
import torch.nn as nn
import torch.nn.functional as F

from torch.autograd import Variable
from torch.nn.parameter import Parameter
import math

class DownsampleB(nn.Module):
    def __init__(self, nIn, nOut, stride=2):
        super(DownsampleB, self).__init__()
        self.avg = nn.AvgPool2d(stride)  # makes H and W smaller

    def forward(self, x):
        residual = self.avg(x)
        return torch.cat((residual, residual * 0), 1)  # pads with zeros for projection shortcut

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

# No projection: identity shortcut
class BasicBlock(nn.Module):  # standard
    expansion = 1

    def __init__(self, in_planes, planes, stride=1):
        super(BasicBlock, self).__init__()

        self.conv1 = conv3x3(in_planes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)

        self.conv2 = nn.Sequential(nn.ReLU(True), conv3x3(planes, planes))
        self.bn2 = nn.BatchNorm2d(planes)

    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = F.relu(out)

        out = self.conv2(out)
        y = self.bn2(out)

        return y

class ResNet(nn.Module):
    def __init__(self, block, layers, num_class=10):  # layers means blocks per layer.
        super(ResNet, self).__init__()

        factor = 1
        self.in_planes = int(32 * factor)
        self.conv1 = conv3x3(3, int(32 * factor))  # first layer 3 channels.
        self.bn1 = nn.BatchNorm2d(int(32 * factor))
        self.relu = nn.ReLU(inplace=True)

        strides = [2, 2, 2]  # strides for the downsampling.
        filt_sizes = [64, 128, 256]  # 64 depth, then 128 then 256
        self.blocks, self.ds = [], []

        for idx, (filt_size, num_blocks, stride) in enumerate(zip(filt_sizes, layers, strides)):  # create main blocks.
            blocks, ds = self._make_layer(block, filt_size, num_blocks, stride=stride)
            self.blocks.append(nn.ModuleList(blocks))
            self.ds.append(ds)
        self.blocks = nn.ModuleList(self.blocks)
        self.ds = nn.ModuleList(self.ds)

        self.bn2 = nn.Sequential(nn.BatchNorm2d(int(256 * factor)), nn.ReLU(True))
        self.avgpool = nn.AdaptiveAvgPool2d(1)  # we are just essentially resizing it i think smart.
        self.linear = nn.Linear(int(256 * factor), num_class)

        self.layer_config = layers
        for m in self.modules():  # initialize values for everything.
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def seed(self, x):
        x = self.bn1(self.conv1(x))
        return x

    def _make_layer(self, block, planes, blocks, stride=1):

        downsample = nn.Sequential()
        if stride != 1 or self.in_planes != planes * block.expansion:  # more out layers than in?
            downsample = DownsampleB(self.in_planes, planes * block.expansion, 2)  # downsample it.

        layers = [block(self.in_planes, planes, stride)]
        self.in_planes = planes * block.expansion  # reset the input planes.
        for i in range(1, blocks):
            layers.append(block(self.in_planes, planes))  # add the rest of the blocks.

        return layers, downsample

    def forward(self, x, policy=None):  # go forward with the policy given by the agent.
        t = 0  # Changed to work to delete blocks instead
        x = self.seed(x)

        if policy is not None:
            for segment, num_blocks in enumerate(self.layer_config):  # segment just i=0,i++, num_blocks is # per layer.
                for b in range(num_blocks):  # for each block
                    action = policy[:, t].contiguous()  # makes the elements contiguous (exist as if it were made)
                    # as it is not have its meta information changed. Not having this might cause errors.
                    # from this we get 1's and zeros, as to whether to use fine tune or not.
                    # here we are just getting the t'th element, so what to do for this given layer.
                    action_mask = action.float().view(-1, 1, 1, 1)  # 1 = use frozen, 0 = finetune.
                    # .float converts to float... it already is one. action_mask: tensor([[[[0.]]]], device='cuda:0')

                    residual = self.ds[segment](x) if b == 0 else x  # downsample on first of layer.
                    output = self.blocks[segment][b](x)  # run the block on x.

                    f1 = F.relu(residual + output)  # Using the block
                    x = f1 * (action_mask) + residual*(1-action_mask)  # decide which to use.
                    t += 1  # go for each layer
        else:
            for segment, num_blocks in enumerate(self.layer_config):  # no policy
                for b in range(num_blocks):
                    residual = self.ds[segment](x) if b == 0 else x  # fine tune layers
                    output = self.blocks[segment][b](x)
                    x = F.relu(residual + output)
                    t += 1
        x = self.bn2(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.linear(x)
        return x

def resnet26(num_class=10, blocks=BasicBlock):
    return ResNet(blocks, [4, 4, 4], num_class)