import numpy as np
import cv2

class VideoUnifier:
    def __init__(self):
        pass

    def _obtain_readers_and_data(self, video_paths):
        vid_readers = list()
        # to measure minimum length of movie
        min_vid_length = 10000000
        # to measure maximum width of movie
        max_width = 0
        # to measure final height
        max_height = 0

        for vid_path in video_paths:
            reader = cv2.VideoCapture("{0}".format(vid_path))
            final_frame_count, frame_width, frame_height, fps = int(
                reader.get(cv2.CAP_PROP_FRAME_COUNT)), int(reader.get(cv2.CAP_PROP_FRAME_WIDTH)), int(
                reader.get(cv2.CAP_PROP_FRAME_HEIGHT)), reader.get(cv2.CAP_PROP_FPS)
            vid_data_dict = {
                "reader": reader,
                "final_frame_count": final_frame_count,
                "frame_width": frame_width,
                "frame_height": frame_height
            }
            vid_readers.append(vid_data_dict)
            # obtaining the length and width of the result
            if min_vid_length > final_frame_count:
                min_vid_length = final_frame_count
            if max_width < frame_width:
                max_width = frame_width
            if max_height < frame_height:
                max_height = frame_height

        return vid_readers, min_vid_length, max_width, max_height

    def unify_videos(self, videos_to_unify_paths: list, output_file, alignment: str = 'vertical'):

        videos_readers_dicts, min_video_length, max_video_width, max_height = self._obtain_readers_and_data(videos_to_unify_paths)
        if alignment == 'vertical':
            vid_writer = cv2.VideoWriter("{0}".format(output_file), cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (max_video_width, max_height*len(videos_readers_dicts)))
        else:
            vid_writer = cv2.VideoWriter("{0}".format(output_file), cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
                                         (max_video_width * len(videos_readers_dicts), max_height))
        frame_ctr = 0

        while frame_ctr < min_video_length:
            current_frames = list()
            for reader_dict in videos_readers_dicts:
                ret, frame = reader_dict['reader'].read()
                if not ret:
                    raise Exception("something went wrong!")
                # resize frame to same width as final movie, and same height as current
                frame = cv2.resize(frame, (max_video_width, max_height))
                current_frames.append(frame)
            if alignment == 'vertical':
                new_composite_frame = np.ndarray(shape=(max_height*len(current_frames), max_video_width, 3))
            else:
                new_composite_frame = np.ndarray(shape=(max_height, max_video_width * len(current_frames), 3))
            # unifying the frames
            if alignment == 'vertical':
                curr_height = 0
                for frame in current_frames:
                    frame_height = frame.shape[0]
                    new_composite_frame[curr_height: curr_height + frame_height, :] = frame
                    curr_height += frame_height
            else:
                curr_width = 0
                for frame in current_frames:
                    frame_width = frame.shape[1]
                    new_composite_frame[:, curr_width: curr_width + frame_width] = frame
                    curr_width += frame_width
            # writing the new frame
            im = np.uint8(new_composite_frame)
            vid_writer.write(im)

            frame_ctr += 1

        # wraping up
        vid_writer.release()
        [reader_dict["reader"].release() for reader_dict in videos_readers_dicts]

        # orig = cv2.LoadImage("rot.png")
        # cv2.Flip(orig, flipMode=-1)

NEW_VID_PATH = "/Users/yishaiazabary/PycharmProjects/platelets/ForAnalyze/temp/FibrinogenUnifiedNoGradient.avi"
# VIDEO1 = "/Users/yishaiazabary/Downloads/Videos/coll1RBC.avi"
# VIDEO2 = "/Users/yishaiazabary/Downloads/Videos/coll2RBC.avi"
VIDEO1 = "/Users/yishaiazabary/PycharmProjects/platelets/ForAnalyze/temp/PRP_FIBRINOGEN 2.avi"
VIDEO2 = "/Users/yishaiazabary/PycharmProjects/platelets/ForAnalyze/temp/PRP_FBG_exp.63_control_SP3.avi"
VIDEO3 = "/Users/yishaiazabary/PycharmProjects/platelets/ForAnalyze/temp/PRP_FBG_exp.63_control_SP4.avi"
vu = VideoUnifier()
vu.unify_videos([VIDEO1, VIDEO2, VIDEO3], NEW_VID_PATH, alignment='horizontal')


import pygit2
import pytest

from . import utils

@pytest.fixture
def repo(tmp_path):
    with utils.TemporaryRepository('binaryfilerepo.zip', tmp_path) as path:
        yield pygit2.Repository(path)

PATCH_BINARY = """diff --git a/binary_file b/binary_file
index 86e5c10..b835d73 100644
Binary files a/binary_file and b/binary_file differ
"""

PATCH_BINARY_SHOW = """diff --git a/binary_file b/binary_file
index 86e5c1008b5ce635d3e3fffa4434c5eccd8f00b6..b835d73543244b6694f36a8c5dfdffb71b153db7 100644
GIT binary patch
literal 8
Pc${NM%FIhFs^kIy3n&7R

literal 8
Pc${NM&PdElPvrst3ey5{

"""

def test_binary_diff(repo):
    diff = repo.diff('HEAD', 'HEAD^')
    assert PATCH_BINARY == diff.patch
    diff = repo.diff('HEAD', 'HEAD^', flags=pygit2.GIT_DIFF_SHOW_BINARY)
    assert PATCH_BINARY_SHOW == diff.patch
    diff = repo.diff(b'HEAD', b'HEAD^')
    assert PATCH_BINARY == diff.patch
    diff = repo.diff(b'HEAD', b'HEAD^', flags=pygit2.GIT_DIFF_SHOW_BINARY)
    assert PATCH_BINARY_SHOW == diff.patch


import numpy as np
from scipy.stats import wishart

def _add_noise(cov_type, y, alpha, rng, n_sensors, n_times):
    noise_type = "random"
    if cov_type == "diag":
        if noise_type == "random":
            # initialization of the noise covariance matrix with a random diagonal matrix
            rv = wishart(df=n_sensors, scale=1e-3 * np.eye(n_sensors))
            cov = rv.rvs()
            cov = np.diag(np.diag(cov))
        else:
            # initialization of the noise covariance with an identity matrix
            cov = 1e-2 * np.diag(np.ones(n_sensors))
    else:
        # initialization of the noise covariance matrix with a full PSD random matrix
        rv = wishart(df=n_sensors, scale=1e-3 * np.eye(n_sensors))
        cov = rv.rvs()

    signal_norm = np.linalg.norm(y, "fro")
    noise = rng.multivariate_normal(np.zeros(n_sensors), cov, size=n_times).T
    noise_norm = np.linalg.norm(noise, "fro")
    noise_normalised = noise / noise_norm

    noise_scaled = ((1 - alpha) / alpha) * signal_norm * noise_normalised
    cov_scaled = cov * (((1 - alpha) / alpha) * (signal_norm / noise_norm)) ** 2
    y += noise_scaled

    return y, cov_scaled, noise_scaled

def get_data(
    cov_type,
    path_to_leadfield,
    n_sensors=50,
    n_times=10,
    n_sources=200,
    nnz=3,
    orientation_type="fixed",
    alpha=0.99,  # 40dB snr
    seed=None,
):
    n_orient = 3 if orientation_type == "free" else 1
    rng = np.random.RandomState(seed)
    if path_to_leadfield is not None:
        lead_field = np.load(path_to_leadfield, allow_pickle=True)
        L = lead_field["lead_field"]
        if orientation_type == "fixed":
            n_sensors, n_sources = L.shape
        elif orientation_type == "free":
            n_sensors, n_sources, _ = L.shape
    else:
        L = (
            rng.randn(n_sensors, n_sources)
            if orientation_type == "fixed"
            else rng.randn(n_sensors, n_sources, n_orient)
        )

    # generate source locations
    idx = rng.choice(n_sources, size=nnz, replace=False)
    if orientation_type == "fixed":
        x = np.zeros((n_sources, n_times))
        x[idx] = rng.randn(nnz, n_times)
        y = L @ x
    elif orientation_type == "free":
        x = np.zeros((n_sources, n_orient, n_times))
        x[idx] = rng.randn(nnz, n_orient, n_times)
        y = np.einsum("nmr, mrd->nd", L, x)

    # add noise
    y, cov_scaled, noise_scaled = _add_noise(
        cov_type, y, alpha, rng, n_sensors, n_times
    )

    if orientation_type == "free":
        # reshaping L to (n_sensors, n_sources*n_orient)
        L = L.reshape(L.shape[0], -1)

    if n_times == 1 and orientation_type == "fixed":
        y = y[:, 0]
        x = x[:, 0]

    return y, L, x, cov_scaled, noise_scaled


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

# 와인 데이터셋 로드
wine = pd.read_csv("https://bit.ly/wine-date")

# 와인 데이터셋에서 타겟셋 분리
data = wine[["alcohol", "sugar", "pH"]].to_numpy()
target = wine[["class"]].to_numpy()

# 훈련셋 / 테스트셋 분리
from sklearn.model_selection import train_test_split

train_input, test_input, train_target, test_target = train_test_split(
    data, target, test_size=0.2, random_state=42
)

# RandomForest 분류
from sklearn.model_selection import cross_validate
from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(n_jobs=-1, random_state=42)
scores = cross_validate(
    rf, train_input, train_target, return_train_score=True, n_jobs=-1
)
print(np.mean(scores["train_score"]), np.mean(scores["test_score"]))

rf.fit(train_input, train_target)
print(rf.feature_importances_)

# 모델 평가
rf = RandomForestClassifier(oob_score=True, n_jobs=-1, random_state=42)
rf.fit(train_input, train_target)
print(rf.oob_score_)

# 엑스트라 트리
from sklearn.ensemble import ExtraTreesClassifier

et = ExtraTreesClassifier(n_jobs=-1, random_state=42)
scores = cross_validate(
    et, train_input, train_target, return_train_score=True, n_jobs=-1
)
print(np.mean(scores["train_score"]), np.mean(scores["test_score"]))

et.fit(train_input, train_target)
print(et.feature_importances_)

# 그레디언트 부스팅
from sklearn.ensemble import GradientBoostingClassifier

gb = GradientBoostingClassifier(random_state=42)
scores = cross_validate(
    gb, train_input, train_target, return_train_score=True, n_jobs=-1
)
print(np.mean(scores["train_score"]), np.mean(scores["test_score"]))

# 학습률과 트리 개수를 증가시킨 결과
gb = GradientBoostingClassifier(n_estimators=500, learning_rate=0.2, random_state=42)
scores = cross_validate(
    gb, train_input, train_target, return_train_score=True, n_jobs=-1
)
print(np.mean(scores["train_score"]), np.mean(scores["test_score"]))

gb.fit(train_input, train_target)
print(gb.feature_importances_)

# 히스토그램 기반 그레디언트 부스팅
from sklearn.experimental import enable_hist_gradient_boosting
from sklearn.ensemble import HistGradientBoostingClassifier

hgb = HistGradientBoostingClassifier(random_state=42)
scores = cross_validate(
    hgb, train_input, train_target, return_train_score=True, n_jobs=-1
)
print(np.mean(scores["train_score"]), np.mean(scores["test_score"]))

hgb.fit(train_input, train_target)
print(hgb.feature_importances_)
print(hgb.score(test_input, test_target))


# This python program captures real-time image of the vegetable using webcam and
# detects the vegetable using OpenCV with a trained dataset
# the program is tranined for 5 different vegetables: Carrot, Bell_pepper, Tomato, Potato, and Broccoli

#import required libraries
import cv2
import numpy as np
import mysql.connector
import calendar
import time
import os

# 'tkinter' is used to create the GUI
from tkinter import * #import all libraries in tkinter
class MyWindow:       #main class
    def __init__(self, win):        # initialize method
        # promt to take user inputs
        self.lbl1=Label(win, text='User Id:')
        self.lbl2=Label(win, text='Weight in lb:')
        self.lbl3=Label(win, text='Message:')
        self.lbl4=Label(win, text='****Press "Space" to capture photo or "Escape" to cancel****')
        # when "Space" is pressed image would be captured, when "Escape" is pressed the widow is cancelled
        self.t1=Entry(bd=3) #Take user input
        self.t2=Entry()
        # create buttons
        self.btn0 = Button(win, text='Sell') #if item being sold
        self.btn1 = Button(win, text='Buy')  #if item being bought
        self.btn2=Button(win, text='Reset')  #if the inputs need to be reset

        #positioning elements
        self.lbl1.place(x=100, y=50)
        self.t1.place(x=200, y=50)
        self.lbl2.place(x=100, y=100)
        self.t2.place(x=200, y=100)
        # if button being pressed, call associated function
        self.b0=Button(win, text='Sell', command=self.sell) #call sell function
        self.b1=Button(win, text='Buy', command=self.buy) #call buy function
        self.b2=Button(win, text='Reset') #call reset function
        self.b2.bind('<Button-1>', self.reset)

        #positioning elements
        self.b0.place(x=100, y=150)
        self.b1.place(x=160, y=150)
        self.b2.place(x=220, y=150)
        self.lbl3.place(x=20, y=220)
        self.lbl4.place(x=30, y=190)

        #creating the message box
        self.t4 = Text(win, x=20, y=250, height=5, width=20,bg='yellow')
        self.t4.grid(row=3,column=2)
        self.t4.place(x=20, y=250,height=220, width=350)
        self.t4.bind("<1>", lambda event: self.t4.focus_set())

    # image capture, detection and database update
    def imgDetection(self):

        #connect with the MySQL database named "py_project"
        mydb = mysql.connector.connect(
          host="localhost",
          user="root",
          password="",
          database="py_project",
        )
        mycursor = mydb.cursor()

        # capture image using webcam
        cam = cv2.VideoCapture(0)
        cv2.namedWindow("Capture image")

        img_counter = 0
        itemName = ''

        while True:
            ret, frame = cam.read()
            if not ret:
                print("failed to grab frame")
                break
            cv2.imshow("Capture image", frame)

            k = cv2.waitKey(1)
            if k%256 == 27:
                # when ESC pressed
                print("Escape hit, closing...")
                break
            elif k%256 == 32:
                # when SPACE pressed, image being captured
                #creating time-stamp to name every images
                gmt = time.gmtime()
                ts = calendar.timegm(gmt)
                img_name = str(ts)+".png"
                # saving the image file in the directory
                path = 'C:/xampp/htdocs/py_project/uploads'
                cv2.imwrite(os.path.join(path, img_name), frame)

                # Image Detection
                # Load Yolo trained dataset
                net = cv2.dnn.readNet("yolov4-obj_best.weights", "yolov4-obj.cfg")
                classes = []
                with open("obj.names", "r") as f:
                    classes = [line.strip() for line in f.readlines()]
                output_layers = net.getUnconnectedOutLayersNames()
                colors = np.random.uniform(0, 255, size=(len(classes), 3))

                # Loading image
                img = cv2.imread(os.path.join(path, img_name))
                img = cv2.resize(img, None, fx=0.4, fy=0.4)
                height, width, channels = img.shape

                # Detecting objects
                blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
                net.setInput(blob)
                outs = net.forward(output_layers)

                # creating rectangle
                class_ids = []
                confidences = []
                boxes = []
                for out in outs:
                    for detection in out:
                        scores = detection[5:]
                        class_id = np.argmax(scores)
                        confidence = scores[class_id]
                        if confidence > 0.5:
                            # Object detected
                            center_x = int(detection[0] * width)
                            center_y = int(detection[1] * height)
                            w = int(detection[2] * width)
                            h = int(detection[3] * height)

                            # Rectangle coordinates
                            x = int(center_x - w / 2)
                            y = int(center_y - h / 2)
                            boxes.append([x, y, w, h])
                            confidences.append(float(confidence))
                            class_ids.append(class_id)

                indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
                font = cv2.FONT_HERSHEY_PLAIN
                for i in range(len(boxes)):
                    if i in indexes:
                        x, y, w, h = boxes[i]
                        label = str(classes[class_ids[i]])
                        color = colors[i]
                        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
                        cv2.putText(img, label, (x, y + 30), font, 3, color, 3)
                        #detected object name
                        itemName = classes[class_ids[i]]

                        item_name_ck = ""
                        for x in itemName:
                            item_name_ck+= x

                # showing the detected object in the image
                cv2.imshow("Image", img)
                gmt = time.gmtime()
                ts = calendar.timegm(gmt)
                img_name = "detected"+str(ts)+".png"
                #cv2.imwrite(img_name, img)

                path = 'C:/xampp/htdocs/py_project/uploads'
                cv2.imwrite(os.path.join(path, img_name), img)

                cv2.waitKey(0)
                cv2.destroyAllWindows()
                # release the camera
                cam.release()
                cv2.destroyAllWindows()

                # read the item related information from the database
                mycursor.execute("""SELECT * FROM `tbl_item_price` WHERE item_name = '%s'""" % (itemName))
                itemPriceInfo = mycursor.fetchone()

                # use the user inputs
                userId = int(self.t1.get())
                userQty = float(self.t2.get())
                # read customer information from the database
                mycursor.execute("""SELECT * FROM `tbl_customer_accounts` WHERE customer_id = '%s'""" % (userId))
                userAccountInfo = mycursor.fetchone()

                # preparing the message to display
                result = "User Details:" + "\nUser Name: " + str(userAccountInfo[2]) + "\nAddress: " + str(userAccountInfo[4])+"\nBank Name: " + str(userAccountInfo[5])+"\nAccount No: " + str(userAccountInfo[3])+"\nPrevious Account Balance($): " + str(round((userAccountInfo[6]),2))

                # Calculte payable amount
                payAmount = float(itemPriceInfo[2]) * float(userQty)
                #if item being sold
                if self.arg==1:
                    updatedAmount = float(userAccountInfo[6]) + float(payAmount) #variable to update account balance
                    newstock=float(itemPriceInfo[4])+ float(userQty) #variable to update current stock
                    result = result+"\nItem sold!"
                #if item being bought
                if self.arg==0:
                    updatedAmount = float(userAccountInfo[6]) - float(payAmount)
                    newstock=float(itemPriceInfo[4])- float(userQty)
                    result = result+"\nItem bought!"
                # adding more info in the message to display
                result = result+"\n\n"
                result = result +"Your customer Id: " + str(userId) + "\nDetected item: " + str(itemName) + "\nItem Price($): " + str(itemPriceInfo[2]) +" (per lb)"+ "\nWeight(lb): " + str(userQty) + "\nTotal price($): " + str(round((payAmount),4)) + " (item price*weight)"

                # Insert Image information into the database
                sql = "INSERT INTO tbl_image_info (`img_name`, `category`, `up_by`) VALUES (%s, %s, %s)"
                val = (img_name, itemName, userId)
                mycursor.execute(sql, val)
                mydb.commit()

                # update current stock of the item detected
                sql_update = "UPDATE tbl_item_price SET current_stock_in_lb=%s WHERE item_name=%s"
                val =  (newstock, itemName)
                mycursor.execute(sql_update, val)
                mydb.commit()

                # update balance in user account
                sql_update = "UPDATE tbl_customer_accounts SET balance=%s WHERE customer_id=%s"
                val =  (updatedAmount, userId)
                mycursor.execute(sql_update, val)
                mydb.commit()
                mycursor.close ()
                mydb.close()
                result = result + "\nYour Updated Account Balance($): "+ str(round((updatedAmount),2))
                #display message
                self.t4.insert(END, str(result))
                break

    # function to take user input and call the image detection function
    # if Sell is pressed
    def sell(self):
        self.t4.delete(1.0,END) #erases previous info
        num1=int(self.t1.get()) #get the user inputs
        num2=float(self.t2.get())
        self.arg=1 #flag variable for Sell
        self.imgDetection() # calling the imgDetection function

    # if Buy is pressed
    def buy(self):
        self.t4.delete(1.0,END) #erases previous info
        num1=int(self.t1.get()) #get the user inputs
        num2=float(self.t2.get())
        self.arg=0 #flag variable for Buy
        self.imgDetection() # calling the imgDetection function

    # function to erase the user iputs if Reset is pressed
    def reset(self, event):
        self.t4.delete(1.0,END)
        self.t1.delete(0, 'end')
        self.t2.delete(0, 'end')

# creating the window
window=Tk()
mywin=MyWindow(window)
window.title('Automated Vegetable Store')
window.geometry("400x500+10+10")
window.mainloop()


#ptt movie板
# 抓取網頁資料
import urllib.request as request
#下兩行為mac需求
import ssl
context=ssl._create_unverified_context()
#url="https://www.ptt.cc/bbs/Gossiping/index.html"
for i in range(7020,7019,-1):
    url="https://www.ptt.cc/bbs/movie/index"+str(i)+".html"
    print(url)
#sys.exit() #可以只執行到這邊

#偽裝成真實的連線 #大小寫要注意一下
    req=request.Request(url, headers={

        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

     })

    with request.urlopen(req,context=context) as response:
        data=response.read().decode("utf-8")

    print("=============")

    # 使用BeautifulSoup 擷取目標文字
    import bs4
    root= bs4.BeautifulSoup(data, "html.parser")

    titles=root.find_all("div","title") #找出
...

    for titles in titles:
        try:
            print(titles)
        except AttributeError:
            continue


import warnings
warnings.filterwarnings("ignore")
import logging
import os

from models.vit import VisionTransformer, interpolatePosEmbed
from models.med import BertConfig, BertLMHeadModel
from transformers import BertTokenizer

import torch
from torch import nn
try:
    from torch.hub import get_dir
except ImportError:
    from torch.hub import _get_torch_home as get_dir
from torch.hub import HASH_REGEX, download_url_to_file, urlparse

class BLIP_Decoder(nn.Module):
    def __init__(self,
                 med_config = 'models/medConfig.json',
                 imageSize = 384,
                 visionWidth = 1024,
                 depth = 24,
                 numHeads = 16,
                 dropPathRate = 0.1
                 ):

        super().__init__()

        self.visual_encoder, vision_width = createVit(imageSize, visionWidth, depth, numHeads, dropPathRate)
        self.tokenizer = initTokenizer()
        med_config = BertConfig.from_json_file(med_config)
        med_config.encoder_width = vision_width
        self.text_decoder = BertLMHeadModel(config=med_config)

        self.prompt = "a picture of "
        self.prompt_length = len(self.tokenizer(self.prompt).input_ids)-1

    def forward(self, image, caption):

        imageEmbeds = self.visual_encoder(image)
        imageAtts = torch.ones(imageEmbeds.size()[:-1],dtype=torch.long).to(image.device)

        text = self.tokenizer(caption, padding='longest', truncation=True, max_length=40, return_tensors="pt").to(image.device)

        text.input_ids[:,0] = self.tokenizer.bos_token_id

        decoderTargets = text.input_ids.masked_fill(text.input_ids == self.tokenizer.pad_token_id, -100)
        decoderTargets[:,:self.prompt_length] = -100

        decoderOutput = self.text_decoder(text.input_ids,
                                           attention_mask = text.attention_mask,
                                           encoder_hidden_states = imageEmbeds,
                                           encoder_attention_mask = imageAtts,
                                           labels = decoderTargets,
                                           return_dict = True,
                                          )
        loss = decoderOutput.loss

        return loss

    def generate(self, image, strategy, num_beams=3, max_length=20, min_length=5, top_p=0.9, repetition_penalty=1.0):
        imageEmbeds = self.visual_encoder(image)

        if strategy == "Beam Search":
            imageEmbeds = imageEmbeds.repeat_interleave(num_beams,dim=0)

        imageAtts = torch.ones(imageEmbeds.size()[:-1],dtype=torch.long).to(image.device)
        model_kwargs = {"encoder_hidden_states": imageEmbeds, "encoder_attention_mask":imageAtts}

        prompt = [self.prompt] * image.size(0)
        input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.to(image.device)
        input_ids[:,0] = self.tokenizer.bos_token_id
        input_ids = input_ids[:, :-1]

        if strategy == "Nucleus Sampling":
            outputs = self.text_decoder.generate(input_ids=input_ids,
                                                  max_length=max_length,
                                                  min_length=min_length,
                                                  do_sample=True,
                                                  top_p=top_p,
                                                  num_return_sequences=1,
                                                  eos_token_id=self.tokenizer.sep_token_id,
                                                  pad_token_id=self.tokenizer.pad_token_id,
                                                  repetition_penalty=1.1,
                                                  **model_kwargs)
        elif strategy == "Beam Search":
            outputs = self.text_decoder.generate(input_ids=input_ids,
                                                  max_length=max_length,
                                                  min_length=min_length,
                                                  num_beams=num_beams,
                                                  eos_token_id=self.tokenizer.sep_token_id,
                                                  pad_token_id=self.tokenizer.pad_token_id,
                                                  repetition_penalty=repetition_penalty,
                                                  **model_kwargs)

        captions = []
        for output in outputs:
            caption = self.tokenizer.decode(output, skip_special_tokens=True)
            captions.append(caption[len(self.prompt):])
        return captions

def createVit(imageSize, visionWidth, depth, numHeads, dropPathRate):
    visualEncoder = VisionTransformer(img_size = imageSize, embed_dim=visionWidth, depth=depth,
                                      num_heads=numHeads, drop_path_rate = dropPathRate)

    return visualEncoder, visionWidth

def initTokenizer():
    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    tokenizer.add_special_tokens({'bos_token':'[DEC]'})
    tokenizer.add_special_tokens({'additional_special_tokens':['[ENC]']})
    tokenizer.enc_token_id = tokenizer.additional_special_tokens_ids[0]
    return tokenizer

def BLIP(url: str, **kwargs):
    model = BLIP_Decoder(**kwargs)
    model,msg = loadCheckPoint(model,url)
    assert(len(msg.missing_keys)==0)

    return model

def loadCheckPoint(model,url):
    cachedFile = downloadCachedFile(url, checkHash=False, progress=True)
    checkpoint = torch.load(cachedFile)

    stateDict = checkpoint['model']

    stateDict['visual_encoder.pos_embed'] = interpolatePosEmbed(stateDict['visual_encoder.pos_embed'],model.visual_encoder)
    if 'visual_encoder_m.pos_embed' in model.state_dict().keys():
        stateDict['visual_encoder_m.pos_embed'] = interpolatePosEmbed(stateDict['visual_encoder_m.pos_embed'],
                                                                         model.visual_encoder_m)
    for key in model.state_dict().keys():
        if key in stateDict.keys():
            if stateDict[key].shape!=model.state_dict()[key].shape:
                del stateDict[key]

    msg = model.load_state_dict(stateDict,strict=False)
    print(url, "loaded")
    return model,msg

_logger = logging.getLogger(__name__)

def downloadCachedFile(url, checkHash, progress):
    parts = urlparse(url.replace('*', ''))
    filename = os.path.basename(parts.path)
    cachedFile = os.path.join(getCacheDir(''), filename)
    if not os.path.exists(cachedFile):
        _logger.info('Downloading: "{}" to {}\n'.format(url, cachedFile))
        hashPrefix = None
        if checkHash:
            r = HASH_REGEX.search(filename)
            hashPrefix = r.group(1) if r else None
        download_url_to_file(url, cachedFile, hashPrefix, progress=progress)
    return cachedFile

def getCacheDir(childDir):
    if os.getenv('TORCH_MODEL_ZOO'):
        _logger.warning('TORCH_MODEL_ZOO is deprecated, please use env TORCH_HOME instead')

    hubDir = get_dir().replace("/", '\\')
    childDir = () if not childDir else (childDir,)
    modelDir = os.path.join(hubDir, 'checkpoints', *childDir)
    os.makedirs(modelDir, exist_ok=True)
    return modelDir


"""
Conduct an experiment to prove that the product of two numbers does not depend
on the size of the two numbers being multiplied. Write a program that plots the
results of multiplying numbers of various sizes together. HINT: To get a good
reading you may want to do more than one of these multiplications and time
them as a group since a multiplication happens pretty quickly in a computer.
Verify that it truly is a O(1) operation. Do you see any anomalies? It might be
explained by Python’s support of large integers. What is the cutoff point for
handling multiplications in constant time? Why? Write a program that produces
an XML file with your results in the format given in this chapter. Then visualize
your results with the PlotData.py program provided in this chapter.
"""

import datetime
import random
import time

def main():

    file = open("MultiplyTimeTest.xml","w")

    file.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')

    file.write('<Plot title="Average Multiply Time">\n')

    xList = []
    yList = []
    mList = []
    for e10 in range(1,20):
        xmin = 10**e10
        xmax = 10**(e10+1)

        xList.append(e10+1)
        factor1list = []
        factor2list = []
        for x in range(100000):
            factor1 = random.randint(xmin, xmax)
            factor1list.append(factor1)

            factor2 = random.randint(xmin, xmax)
            factor2list.append(factor2)
        starttime = datetime.datetime.now()

        for index in range(100000):
            mList.append(factor1list[index] * factor2list[index])

        endtime = datetime.datetime.now()

        deltaT = endtime - starttime

        averageTime = deltaT.total_seconds() /100000 * 1000000

        yList.append(averageTime)

    file.write('    <Axes>\n')
    file.write('        <XAxis min="'+str(10)+'" max="'+str(xmax)+'">Factor Size</XAxis>\n')
    file.write('        <YAxis min="'+str(min(yList))+'" max="'+str(100)+'">Microseconds</YAxis>\n')
    file.write('    </Axes>\n')

    file.write('    <Sequence title="Average Access Time vs Factor Size" color="red">\n')

    for i in range(len(xList)):
        file.write('    <DataPoint x="'+str(xList[i])+'" y="'+str(yList[i])+'"/>\n')
    file.write('    </Sequence>\n')
    file.write('</Plot>\n')
    file.close()

if __name__ == "__main__":
    main()


import argparse
from argparse import Namespace
import numpy as np
from tensorflow.keras import layers
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.regularizers import l2
from utils import *

class VGG(Model):
    def __init__(self, args: Namespace):
        super(VGG, self).__init__()
        # initialize model
        self.model: Sequential = Sequential()
        # features
        VGG._make_layers(self.model, args)
        # flatten layer
        self.model.add(layers.Flatten())
        # classifier head
        VGG._make_fc(self.model, args)

    def call(self, x):
        # print(x.shape)
        x = self.model(x)
        # print(x.shape)
        return x

    @staticmethod
    def _make_fc(classifier, args):
        conv_width: int = args.conv_width
        dropout: float = args.dropout
        num_block: int = args.num_block
        num_dense: int = args.num_dense
        weight_decay: float = args.weight_decay

        classifier.add(layers.Dropout(rate=dropout))
        if num_dense == 1:
            # only other option currently is to offer 2 dense layers
            classifier.add(layers.Dense(10))
        elif conv_width == 64:
            num_flat_features = 4096
            if num_block == 4:
                num_flat_features = 256
            classifier.add(layers.Dense(num_flat_features, kernel_regularizer=l2(weight_decay)))
            classifier.add(layers.Activation('relu'))
            classifier.add(layers.Dropout(rate=dropout))
            classifier.add(layers.Dense(10))
        elif conv_width == 128:
            num_flat_features = 8192
            if num_block == 4:
                num_flat_features = 512
            classifier.add(layers.Dense(num_flat_features, kernel_regularizer=l2(weight_decay)))
            classifier.add(layers.Activation('relu'))
            classifier.add(layers.Dropout(rate=dropout))
            classifier.add(layers.Dense(10))
        elif conv_width == 256:
            num_flat_features = 16384
            if num_block == 4:
                num_flat_features = 1024
            classifier.add(layers.Dense(num_flat_features, kernel_regularizer=l2(weight_decay)))
            classifier.add(layers.Activation('relu'))
            classifier.add(layers.Dropout(rate=dropout))
            classifier.add(layers.Dense(10))
        elif conv_width == 512:
            num_flat_features = 32768
            if num_block == 4:
                num_flat_features = 2048
            classifier.add(layers.Dense(num_flat_features, kernel_regularizer=l2(weight_decay)))
            classifier.add(layers.Activation('relu'))
            classifier.add(layers.Dropout(rate=dropout))
            classifier.add(layers.Dense(10))

    @staticmethod
    def _make_layers(features, args):
        conv_width: int = args.conv_width
        num_block: int = args.num_block
        weight_decay: float = args.weight_decay

        # First Block
        features.add(layers.Conv2D(conv_width, (3, 3), padding='same',
                                   input_shape=[32, 32, 3],
                                   kernel_regularizer=l2(weight_decay)))
        features.add(layers.Activation('relu'))
        # features.add(layers.Dropout(dropout)) we shouldn't have dropout in conv block
        features.add(layers.Conv2D(conv_width, (3, 3), padding='same', kernel_regularizer=l2(weight_decay)))
        features.add(layers.Activation('relu'))
        features.add(layers.MaxPooling2D(pool_size=(2, 2)))

        for _ in range(num_block-1):
            features.add(layers.Conv2D(conv_width, (3, 3), padding='same',
                                       kernel_regularizer=l2(weight_decay)))
            features.add(layers.Activation('relu'))
            # features.add(layers.Dropout(dropout))
            features.add(layers.Conv2D(conv_width, (3, 3), padding='same',
                                       kernel_regularizer=l2(weight_decay)))
            features.add(layers.Activation('relu'))
            features.add(layers.MaxPooling2D(pool_size=(2, 2)))
        return features

def _test():
    parser = argparse.ArgumentParser()
    parser.add_argument('--conv_width', type=int, default=128)
    parser.add_argument('--dropout', type=float, default=0.0)
    parser.add_argument('--num_block', type=int, default=4)
    parser.add_argument('--num_dense', type=int, default=2)
    parser.add_argument('--weight_decay', type=float, default=0.0)
    hparams = parser.parse_args()
    net = VGG(hparams)

    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
    x_train, x_test = normalize(x_train, x_test)
    train_loader = tf.data.Dataset.from_tensor_slices((x_train, y_train)) \
        .map(prepare_data).shuffle(50000).batch(50000) #.batch(hparams.batch_size)
    # net = VGG16((32, 32, 3))
    import time
    for (data, _) in train_loader:
        print(data.shape)
        start = time.time()
        net(data)
        end = time.time()
        print(end - start)

    # x = np.random.randn(500, 32, 32, 3)

if __name__ == '__main__':
    _test()


from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth.views import logout
from .views import *

urlpatterns = [
    url(r'^$', MainView.as_view(), name='main'),
    url(r'^calendar/', include([
        url(r'^$', calendar.CalendarView.as_view(), name='index'),
        url(r'^(?P<year>\d+)/(?P<month>\d+)/$',
            calendar.CalendarView.as_view(), name='month'),
        url(r'^(?P<year>\d+)/week/(?P<week>\d+)/$',
            calendar.WeekView.as_view(), name='week'),
        url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$',
            calendar.DayView.as_view(), name='day'),
    ], namespace='calendar')),
    url(r'^event/', include([
        url(r'^(?P<pk>\d+)/$', events.EventDetailView.as_view(), name='show'),
        url(r'^add/$', events.CreateEventView.as_view(), name='add'),
        url(r'^add/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<hour>\d+)/' +
            r'(?P<minute>\d+)/$', events.CreateEventView.as_view(),
            name='add'),
        url(r'^add/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$',
            events.CreateEventView.as_view(), name='add'),
        url(r'^add/(?P<year>\d+)/(?P<month>\d+)/$',
            events.CreateEventView.as_view(), name='add'),
        url(r'^add/(?P<year>\d+)/week/(?P<week>\d+)/$',
            events.CreateEventView.as_view(), name='add-on-week'),
        url(r'^participate/(?P<pk>\d+)/$',
            events.EventDetailView.add_participation_view, name='participate'),
        url(r'^leave/(?P<pk>\d+)/$',
            events.EventDetailView.remove_participation_view, name='leave'),
        url(r'^edit/(?P<pk>\d+)/$', events.EventUpdateView.as_view(),
            name='edit'),
        url(r'^delete/(?P<pk>\d+)/$', events.EventDeleteView.as_view(),
            name='delete'),
    ], namespace='events')),
    url(r'^admin/', admin.site.urls),
    url(r'^account/', include([
        url(r'^login/$', account.LoginOptionsView.as_view(), name='login'),
        url(r'^logout/$', logout, name='logout'),
        url(r'^signup/$', account.SignupView.as_view(), name='signup'),
        url(r'^signup/(?P<token>\w+)/$', account.SignupFormView.as_view(),
            name='signup-form'),
    ], namespace='account')),
    url(r'^auth/', include('social_django.urls', namespace='social')),
    url(r'^invitations/', include('invitations.urls',
        namespace='invitations')),
]


from django.shortcuts import render
from django.views.generic import ListView, TemplateView, CreateView, DeleteView, UpdateView, DetailView
from .models import Book
from django.urls import reverse_lazy

# Create your views here.
class HomePageView(ListView):
    template_name = 'home.html'
    model = Book
    context_object_name = 'books_list'

class AboutPageView(TemplateView):
    template_name = 'about.html'

class ContactsPageView(TemplateView):
    template_name = 'contacts.html'

class BookDetailView(DetailView):
    model = Book
    template_name = 'book_detail.html'
    context_object_name = 'book'

class BookCreateView(CreateView):
    model = Book
    template_name = 'book_new.html'
    fields = '__all__'

class BookUpdateView(UpdateView):
    model = Book
    template_name = 'book_edit.html'
    fields = ['year', 'isbn']

class BookDeleteView(DeleteView):
    model = Book
    template_name = 'book_delete.html'
    success_url = reverse_lazy('home')


import numpy as np
import itertools
from multiprocessing import Pool
import os
import pickle
import random
import time

def get_time_stamp(seconds):
    hours, seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(seconds, 60)
    seconds = int(seconds)
    return '{} : {} : {}'.format(int(hours), str(int(minutes)).rjust(2, '0'), str(int(seconds)).rjust(2, '0'))

def STDP_1(pre_t, post_t, params):
    x = post_t - pre_t
    A_pos = params['A_pos']
    A_neg = params['A_neg']
    t_pos = params['t_pos']
    t_neg = params['t_neg']

    if x == 0:
        return 0
    elif x > 0:
        return A_pos * np.exp(-x / t_pos)
    else:
        return -A_neg * np.exp(x / t_neg)

def STDP_2(pre_t, post_t, params):
    x = post_t - pre_t

    if x == 0:
        return 0
    elif x > 0:
        return -x + 1
    else:
        return -x - 1

def sum_spikes(spikes_1, spikes_2, func, params):
    return sum([func(pre_t, post_t, params) for pre_t, post_t in itertools.product(spikes_1, spikes_2)])

def get_spikes(phase, rate):
    if rate == 0:
        return np.empty(0)
    else:
        return np.mod(np.arange(0, 1, 1/rate) + phase, 1)

def jitter_spikes(spike_train, noise_level):
    spike_train = spike_train + (np.random.rand(spike_train.size)-1/2)*2*noise_level
    # spike_train = np.array([i for i in spike_train if 0 <= i < 1])
    return spike_train

def get_dW(nxn):
    params = {
        'A_pos': 1,
        'A_neg': 1,
        't_pos': 1/4,
        't_neg': 1/4
    }

    p0 = nxn[0][0]
    r0 = nxn[0][1]
    p1 = nxn[1][0]
    r1 = nxn[1][1]

    index = (p0[0], r0[0], p1[0], r1[0])
    spikes_1 = get_spikes(p0[1], r0[1])
    spikes_2 = get_spikes(p1[1], r1[1])
    dW = sum_spikes(spikes_1, spikes_2, STDP_2, params)
    # dW = np.mean([sum_spikes(jitter_spikes(spikes_1, 0.01), jitter_spikes(spikes_2, 0.01), STDP_1, params) for i in range(5)])

    return (index, dW)

def get_dW_RIP(nxn):
    p0 = nxn[0][0]
    r0 = nxn[0][1]
    p1 = nxn[1][0]
    r1 = nxn[1][1]

    stdp_weight = 1
    hebb_weight = 1/16

    index = (p0[0], r0[0], p1[0], r1[0])
    a = 4
    hebb_term = r0[1] * r1[1]
    phase_diff = p1[1] - p0[1]
    stdp_term = 2 * np.floor(phase_diff / 2) + 1 - phase_diff

    # print(hebb_term, stdp_term)
    factor = 8 * a * hebb_term / ((a + 2 * hebb_term) ** 2)
    # factor = 1
    dW = factor * stdp_weight * stdp_term + hebb_weight * hebb_term

    return (index, dW)

def get_chunks(lst, n):
    chunks = list()
    for i in range(0, len(lst), n):
        chunks.append(lst[i:i+n])
    return chunks

def gen_range(*args):
    if len(args) == 1 and type(args[0]) == tuple:
        arg = args[0]
        values = np.linspace(*arg)
    else:
        values = np.array(args)
    values = list(zip(range(values.size), values))
    return values

def gen_STDP_diagram(pre_phases, pre_rates, post_phases, post_rates, func, folder, filename, time_stamp):
    print('Initializing phase and rate arrays...')
    pre_phase_x_rate = list(itertools.product(pre_phases, pre_rates))
    post_phase_x_rate = list(itertools.product(post_phases, post_rates))
    neuron_x_neuron = list(itertools.product(pre_phase_x_rate, post_phase_x_rate))
    print('Shuffling data...')
    random.shuffle(neuron_x_neuron)
    pr_x_pr = np.zeros((len(pre_phases), len(pre_rates), len(post_phases), len(post_rates)))
    print('beginning...')

    N = len(neuron_x_neuron)
    start_time = time.time()
    check_time = time.time()
    with Pool(35) as p:
        res = p.imap(func, neuron_x_neuron)
        for j in range(N):
            x = res.next()
            pr_x_pr[x[0]] = x[1]
            t = time.time()
            if t - check_time > 10:
                run_time = t - start_time
                T = N / (j + 1) * run_time
                ETA = T - run_time
                print('ETA: {}'.format(get_time_stamp(ETA)))
                check_time = t

    print('Writing file...')
    filepath = os.path.join(folder, '{}_{}.pkl'.format(time_stamp, filename))
    with open(filepath, 'wb') as f:
        pickle.dump(pr_x_pr, f)

if __name__ == '__main__':
    phases_pre = gen_range(.5)
    rates_pre = gen_range(1)
    phases_post = gen_range((0, 1, 501))
    rates_post = gen_range((0, 10, 501))

    folder = '/Users/alexbaranski/Desktop/fig_folder/figure/'
    time_stamp = str(int(time.time()))

    print(time_stamp)

    gen_STDP_diagram(phases_pre, rates_pre, phases_post, rates_post, get_dW, folder, 'STDP', time_stamp)
    gen_STDP_diagram(phases_pre, rates_pre, phases_post, rates_post, get_dW_RIP, folder, 'RIP', time_stamp)

    # print('Initializing phase and rate arrays...')
    # pre_phase_x_rate = list(itertools.product(pre_phases, pre_rates))
    # post_phase_x_rate = list(itertools.product(post_phases, post_rates))
    # neuron_x_neuron = list(itertools.product(pre_phase_x_rate, post_phase_x_rate))
    # print('Shuffling data...')
    # random.shuffle(neuron_x_neuron)
    # print('Dividing data into chunks...')
    # # data_chunks = get_chunks(neuron_x_neuron, 10000)
    # pr_x_pr = np.zeros((len(pre_phases), len(pre_rates), len(post_phases), len(post_rates)))
    # print('beginning...')
    #
    # N = len(neuron_x_neuron)
    # start_time = time.time()
    # check_time = time.time()
    # with Pool(35) as p:
    #     res = p.imap(get_dW, neuron_x_neuron)
    #     for j in range(N):
    #         x = res.next()
    #         pr_x_pr[x[0]] = x[1]
    #         t = time.time()
    #         if t - check_time > 10:
    #             run_time = t - start_time
    #             T = N / (j + 1) * run_time
    #             ETA = T - run_time
    #             # print(ETA)
    #             print('ETA: {}'.format(get_time_stamp(ETA)))
    #             check_time = t
    #
    # print('Writing file...')
    # folder = '/Users/alexbaranski/Desktop/stdp_data'
    # time_stamp = str(int(time.time()))
    # filepath = os.path.join(folder, '{}_stdp.pkl'.format(time_stamp))
    # with open(filepath, 'wb') as f:
    #     pickle.dump(pr_x_pr, f)


#!/usr/bin/env python

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

# Select TensorFlow as backend for Keras
from os import environ
environ["KERAS_BACKEND"] = "tensorflow"

import numpy as np
np.random.seed(1234)

from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import SGD

from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler

# Load iris dataset
dataset = load_iris()
inputs = dataset["data"]
target_names = dataset["target_names"]
targets = dataset["target"]
targets_onehot = np_utils.to_categorical(targets, len(target_names))

print("Loaded iris dataset with {} entries.".format(inputs.shape[0]))
print("Example entry: {}".format(inputs[0]))

# Define model
model = Sequential()

model.add(Dense(
    8, # Number of nodes
    kernel_initializer="glorot_normal", # Initialization
    activation="relu", # Activation
    input_dim=inputs.shape[1])) # Shape of inputs (only needed for the first layer)

model.add(Dense(
    len(target_names),
    kernel_initializer="glorot_uniform",
    activation="softmax"))

model.summary()

# Set loss, optimizer and evaluation metrics
model.compile(
        loss="categorical_crossentropy",
        optimizer=SGD(lr=0.10),
        metrics=["accuracy",])

# Set up preprocessing
preprocessing = StandardScaler()
preprocessing.fit(inputs)
inputs = preprocessing.transform(inputs)

# Train
history = model.fit(
        inputs,
        targets_onehot,
        batch_size=20,
        epochs=10)

# Calculate accuracy manually
predictions = model.predict(inputs)
predictions_argmax = np.argmax(predictions, axis=1)
accuracy = np.sum(predictions_argmax==targets)/float(inputs.shape[0])
print("Accuracy on full dataset: {}".format(accuracy))

# Save model
model.save("iris_model.h5")

# Plot loss and accuracy over epochs
loss = history.history['loss']
acc = history.history['acc']
epochs = range(1, 1+len(loss))

fig, ax1 = plt.subplots()

ax1.set_xlabel("Epochs")
ax1.set_ylabel("Loss")
ax1.plot(epochs, loss, 'rs', label="Loss")

ax2 = ax1.twinx()
ax2.set_ylabel("Accuracy")
ax2.plot(epochs, acc, 'bo', label="Accuracy")

plt.savefig("iris_epochs.png")


import urllib2
import urllib
from google.appengine.api import modules
from google.appengine.api import users

import webapp2
import sys
import traceback
import healthcheck
import json
import basemodule
import socket

from google.appengine.api import urlfetch

import os
import jinja2

from coms.requests import BaseRequestObject

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
JINJA_ENVIRONMENT = jinja2.Environment(
	loader=jinja2.FileSystemLoader(template_dir),
	extensions=['jinja2.ext.autoescape'],
	autoescape=True)

class APIForwardRequest(BaseRequestObject):
	url = ""
	arguments = {}
	method = "GET"

class FrontEndApiTestHandler(webapp2.RequestHandler):
	def get(self):
		allModules = modules.get_modules()
		allResponses = []
		for moduleName in allModules:
			module_hostname = modules.get_hostname(module=moduleName)
			url = "http://{}/api/get_api_methods".format(module_hostname)
			try:
				response = urlfetch.fetch(
					url,
					payload=None,
					method=urlfetch.GET,
					validate_certificate=False,
					headers=self.request.headers,
					deadline=15)
				allResponses.append(json.loads(response.content))
			except (urllib2.URLError, socket.timeout, urlfetch.DeadlineExceededError, urlfetch.SSLCertificateError, ValueError):
				e_type, e_value, e_tb = sys.exc_info()
				formatted_exc = "".join(traceback.format_exception(e_type, e_value, e_tb))
				allResponses.append({'module': moduleName, 'result': 'Got Error: {}\r\n'.format(formatted_exc)})
		template_values = {
			'user': users.get_current_user(),
			'api_targets': allResponses
		}

		template = JINJA_ENVIRONMENT.get_template('index.html')
		self.response.write(template.render(template_values))

class FrontEndAPIHandler(basemodule.BaseApiHandler):
	test_method = healthcheck.test_registry()
	api_method = basemodule.api_registry()

	@test_method(expected_value=True)
	def test_jinja(self):
		"""verifies that the front end is serving and can run jinja templates"""
		template = JINJA_ENVIRONMENT.get_template('test.html')
		template.render({
			"test_value": 123
		})
		return True

	@api_method(response_type=basemodule.METHOD_OVERRIDE_RESPONSE)
	def _index(self):
		template = JINJA_ENVIRONMENT.get_template('test.html')
		template.render({
			"test_value": 123
		})
		return

	@api_method(req_type=basemodule.GET_METHOD, base_request=APIForwardRequest(required_args=["url"]))
	def forward_api(self):
		try:
			form_data = urllib.urlencode(self.parsed_arguments.arguments)
			if self.parsed_arguments.method == "GET":
				method = urlfetch.GET
			else:
				method = urlfetch.POST
			response = urlfetch.fetch(
				"{}?{}".format(self.parsed_arguments.url, form_data),
				payload=self.parsed_arguments.arguments,
				method=method,
				validate_certificate=False,
				headers=self.request.headers,
				deadline=180)
			self.response.set_status(response.status_code)
			try:
				response_json = json.loads(response.content)
				return response_json
			except ValueError:
				return response.content
		except (urllib2.URLError, socket.timeout, urlfetch.DeadlineExceededError, urlfetch.SSLCertificateError):
			e_type, e_value, e_tb = sys.exc_info()
			formatted_exc = "".join(traceback.format_exception(e_type, e_value, e_tb))
			return json.dumps({'url': self.parsed_arguments.url, 'result': 'Got Error: {}\r\n'.format(formatted_exc)})

class FrontEndHeathCheckHandler(healthcheck.HeathCheckMixIn, FrontEndAPIHandler):
	pass

class TestAllModulesHandler(basemodule.BaseApiHandler):
	def get(self):
		self.startup()
		if not self._validate_request():
			self.response.set_status(401, "unauthorized")
			return
		formatJson = self.request.get("json")

		allModules = modules.get_modules()
		allResponses = []

		for moduleName in allModules:
			module_hostname = modules.get_hostname(module=moduleName)
			url = "http://{}/healthcheck".format(module_hostname)
			if self.request.query_string:
				url += "?{}".format(self.request.query_string)
			if not formatJson:
				allResponses.append('{} Module: BackendURL {url}\r\n'.format(moduleName, url=url))
			try:
				response = urlfetch.fetch(
					url,
					payload=None,
					method=urlfetch.GET,
					validate_certificate=False,
					headers=self.request.headers,
					deadline=15)
				self.info("testall response: {}", response)
				allResponses.append(response.content)
			except (urllib2.URLError, socket.timeout, urlfetch.DeadlineExceededError, urlfetch.SSLCertificateError):
				e_type, e_value, e_tb = sys.exc_info()
				formatted_exc = "".join(traceback.format_exception(e_type, e_value, e_tb))
				if formatJson:
					allResponses.append(
						json.dumps({'module': moduleName, 'result': 'Got Error: {}\r\n'.format(formatted_exc)}))
				else:
					allResponses.append('Got Error: {}\r\n'.format(formatted_exc))

		if formatJson:
			self.format_json(allResponses)
		else:
			self.format_html(allResponses)

	def format_html(self, allResponses):
		import datetime
		self.response.write('
Test All  ({})
'.format(datetime.datetime.utcnow()))
		self.response.write('
')
		self.response.write("
".join(allResponses))
		self.response.write('
')

	def format_json(self, allResponses):

		allJson = {'results': []}

		for jsonString in allResponses:
			parsedJson = json.loads(jsonString)
			allJson['results'].append(parsedJson)
		self.response.write(json.dumps(allJson, sort_keys=True, indent=4))

app = webapp2.WSGIApplication([
	('/cron/.*', FrontEndAPIHandler),
	('/api/.*', FrontEndAPIHandler),
	('/task/.*', FrontEndAPIHandler),
	('/qa/poll_metrics', FrontEndAPIHandler),
	('/testall', TestAllModulesHandler),
	('/healthcheck', FrontEndHeathCheckHandler),
	('/', FrontEndAPIHandler),
	('/api', FrontEndApiTestHandler)
], debug=True)


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 21 17:01:54 2022

@author: luisp
"""
import pandas as pd
import numpy as np
import os

def calculate_metrics(true_data, pred_data, metric=['explained_variance_score',
                                         'max_error',
                                         'mean_absolute_error',
                                         'mean_squared_error',
                                         'mean_squared_log_error',
                                         'mean_absolute_percentage_error',
                                         'median_absolute_error',
                                         'r2_score',
                                         'pearson_correlation_coefficient',
                                         'spearman_correlation_coefficient'],
                      power=0, write_mode=False, output_name=''):
    from sklearn.metrics import explained_variance_score, max_error,mean_absolute_error, mean_squared_error, mean_squared_log_error,mean_absolute_percentage_error, median_absolute_error, r2_score
    from scipy import stats

    dic={'explained_variance_score': explained_variance_score,
         'max_error':max_error,
         'mean_absolute_error': mean_absolute_error,
         'mean_squared_error': mean_squared_error,
         'mean_squared_log_error':mean_squared_log_error,
         'mean_absolute_percentage_error':mean_absolute_percentage_error,
         'median_absolute_error':median_absolute_error,
         'r2_score':r2_score,
         'pearson_correlation_coefficient':stats.pearsonr ,
         'spearman_correlation_coefficient':stats.spearmanr}

    dic_2={}
    for i in metric:
        if i!='pearson_correlation_coefficient' and 'spearman_correlation_coefficient':
            if i=='mean_squared_log_error':
                try:
                    dic_2[i]=dic[i](true_data, pred_data)
                except:
                    dic_2[i]=None
            else:
                dic_2[i]=dic[i](true_data, pred_data)
        if i=='pearson_correlation_coefficient':
            r,p=stats.pearsonr(np.squeeze(true_data),np.squeeze(pred_data))
            dic_2['pearson_correlation_coefficient']=r
        if i=='spearman_correlation_coefficient':
            rho,pval=stats.spearmanr(true_data, pred_data)
            dic_2['spearman_correlation_coefficient']=rho
    if write_mode==True:
        output_file=open(output_name,'w')
        output_file.write('Metric; Value \n')
        for result in list(dic_2.keys()):
            if dic_2[result]==None:
                output_file.write(result + ';' + str(dic_2[result]) + '\n')
            else:
                output_file.write(result + ';' + str(np.round(dic_2[result],3)) + '\n')

    return dic_2


import streamlit as st
import pandas as pd
import pandas as pd
import datetime

## Create week options
pd_calendar = pd.read_csv("data/calendar.csv")
pd_calendar = pd_calendar[(pd_calendar["WEEKKEY_FOLHETO"]>=2021031) & (pd_calendar["WEEKKEY_FOLHETO"]<=2022053)]
options = []
for a,s in  pd_calendar.iterrows():
    week = s['WEEKKEY_FOLHETO']
    time_i = datetime.datetime.strptime(str(s['TIME_I']), '%Y%m%d').strftime('%m/%d/%y')
    time_f = datetime.datetime.strptime(str(s['TIME_I']), '%Y%m%d').strftime('%m/%d/%y')
    option = str(f"{week} ({time_i} - {time_f})")
    options.append(option)

def write():

    c29, c30, c31 = st.columns([1, 6, 1])

    with c30:

        st.subheader("📅 Update Calendar")
        calendar_file = st.file_uploader(
            "",
            key="1",
            help="To activate 'wide mode', go to the hamburger menu > Settings > turn on 'wide mode'",
        )

        if calendar_file is not None:
            file_container = st.expander("Check your uploaded .csv")
            shows = pd.read_csv(calendar_file)
            calendar_file.seek(0)
            file_container.write(shows)
        st.info(
            f"""
                ⬇  Get the template [calenda.csv](calendar)
                """
        )
        st.markdown('<hr style="height:3px;border-width:0;color:gray;background-color:gray">', unsafe_allow_html=True)

        st.subheader("📍 Update Parishs Info")
        parish_file = st.file_uploader(
            "",
            key="2",
            help="To activate 'wide mode', go to the hamburger menu > Settings > turn on 'wide mode'",
        )

        if parish_file is not None:
            file_container = st.expander("Check your uploaded .csv")
            shows = pd.read_csv(parish_file)
            parish_file.seek(0)
            file_container.write(shows)
        st.info(
            f"""
                ⬇  Get the template [parish.csv](calendar)
                """
        )


# -*- coding: utf-8 -*-
"""
Created on Sat Feb 01 10:45:09 2014
Training models remotely in cloud
@author: pacif_000
"""
from monk.roles.configuration import get_config
from monk.roles.administrator import AdminBroker
from monk.roles.monitor import MonitorBroker
import monk.core.api as monkapi
import logging
import sys
from monk.network.broker import KafkaBroker
from monk.network.server import MonkServer, taskT, Task
import monk.utils.utils as ut

logger = logging.getLogger("monk.roles.worker")

class WorkerBroker(KafkaBroker):
    def add_clone(self, userName, turtleName, follower, **kwargs):
        self.produce('AddClone', userName, turtleName=turtleName, follower=follower, **kwargs)

    def remove_clone(self, userName, turtleName, **kwargs):
        self.produce('RemoveClone', userName, turtleName=turtleName, **kwargs)

    def follow(self, userName, turtleName, leader=None, follower=None, **kwargs):
        self.produce('Follow', userName, turtleName=turtleName, leader=leader, follower=follower, **kwargs)

    def unfollow(self, userName, turtleName, leader=None, follower=None, **kwargs):
        self.produce('UnFollow', userName, turtleName=turtleName, leader=leader, follower=follower, **kwargs)

    def add_data(self, userName, turtleName, ent, **kwargs):
        self.produce('AddData', userName, turtleName=turtleName, entity=ent, **kwargs)

    def save_turtle(self, userName, turtleName, **kwargs):
        self.produce('SaveTurtle', userName, turtleName=turtleName, **kwargs)

    def merge(self, userName, turtleName, follower, **kwargs):
        self.produce('Merge', userName, turtleName=turtleName, follower=follower, **kwargs)

    def train(self, userName, turtleName, **kwargs):
        self.produce('Train', userName, turtleName=turtleName, **kwargs)

    def predict(self, userName, turtleName, ent, **kwargs):
        self.produce('Predict', userName, turtleNmae=turtleName, entity=ent, **kwargs)

    def reset(self, userName, turtleName, **kwargs):
        self.produce('Reset', userName, turtleName=turtleName, **kwargs)

    def reset_all_data(self, userName, turtleName, **kwargs):
        self.produce('ResetAllData', userName, turtleName=turtleName, **kwargs)

    def offset_commit(self, userName, turtleName, **kwargs):
        self.produce('OffsetCommit', userName, turtleName=turtleName, **kwargs)

    def set_mantis_parameter(self, userName, turtleName, para, value, **kwargs):
        self.produce('SetMantisParameter', userName, turtleName=turtleName, para=para, value=value, **kwargs)

    def monk_reload(self, **kwargs):
        self.produce('MonkReload', None, **kwargs)

class MonkWorker(MonkServer):
    def maintain(self):
        self.adminBroker.update_worker(self.serverName)

    def onexit(self):
        self.adminBroker.unregister_worker(self.serverName)

    def init_brokers(self, config):
        monkapi.initialize(config)
        self.adminBroker = AdminBroker(config.kafkaConnectionString,
                                       config.administratorGroup,
                                       config.administratorTopic,
                                       KafkaBroker.SIMPLE_CONSUMER,
                                       config.administratorClientPartitions,
                                       KafkaBroker.FIXED_PRODUCER,
                                       config.administratorServerPartitions)
        self.workerBroker = WorkerBroker(config.kafkaConnectionString,
                                         config.workerGroup,
                                         config.workerTopic,
                                         consumerType=KafkaBroker.SIMPLE_CONSUMER,
                                         producerType=KafkaBroker.USER_PRODUCER)
        self.monitorBroker = MonitorBroker(config.kafkaConnectionString,
                                           config.monitorGroup,
                                           config.monitorTopic,
                                           producerType=KafkaBroker.FIXED_PRODUCER,
                                           producerPartitions=[0])
        ut.set_monitor(self.monitorBroker)

        self.MAINTAIN_INTERVAL = config.workerMaintainInterval
        self.POLL_INTERVAL = config.workerPollInterval
        self.EXECUTE_INTERVAL = config.workerExecuteInterval
        self.MAX_QUEUE_SIZE = config.workerMaxQueueSize

        self.adminBroker.register_worker(self.serverName, offsetSkip=config.workerConsumerOffsetSkip)
        return [self.adminBroker, self.workerBroker]

worker = MonkWorker()

class Train(Task):
    def act(self):
        monkapi.train(self.turtleName, self.userName)
        #leader = monkapi.get_leader(self.turtleName, self.userName)
        #worker.workerBroker.merge(leader, self.turtleName, self.userName)
taskT(Train)

class Merge(Task):
    def act(self):
        follower = self.get('follower')
        monkapi.merge(self.turtleName, self.userName, follower)
        #if monkapi.merge(self.turtleName, self.userName, follower):
            #for follower in monkapi.get_followers(self.turtleName, self.userName):
               #worker.workerBroker.train(follower, self.turtleName)
taskT(Merge)

class Reset(Task):
    def act(self):
        logger.debug('reset turtle {} for user {}'.format(self.turtleName, self.userName))
        monkapi.reset(self.turtleName, self.userName)
taskT(Reset)

class SaveTurtle(Task):
    def act(self):
        monkapi.save_turtle(self.turtleName, self.userName)
taskT(SaveTurtle)

class ResetAllData(Task):
    def act(self):
        logger.debug('reset_all_data turtle {0} of user {1} '.format(self.turtleName, self.userName))
        monkapi.reset_all_data(self.turtleName, self.userName)
taskT(ResetAllData)

class OffsetCommit(Task):
    def act(self):
        worker.workerBroker.commit()
taskT(OffsetCommit)

class SetMantisParameter(Task):
    def act(self):
        para = self.get('para', '')
        value = self.get('value', 0)
        logger.debug('set_mantis_parameter {} to {}'.format(para, value))
        monkapi.set_mantis_parameter(self.turtleName, self.userName, para, value)
taskT(SetMantisParameter)

class MonkReload(Task):
    def act(self):
        monkapi.reloads()
taskT(MonkReload)

class Follow(Task):
    def act(self):
        leader = self.get('leader')
        follower = self.get('follower')
        monkapi.follow_turtle(self.turtleName, self.userName, leader=leader, follower=follower)
taskT(Follow)

class UnFollow(Task):
    def act(self):
        leader = self.get('leader')
        follower = self.get('follower')
        monkapi.unfollow_turtle(self.turtleName, self.userName, leader=leader, follower=follower)
taskT(UnFollow)

class AddClone(Task):
    def act(self):
        follower = self.get('follower')
        logger.debug('follower = {}'.format(follower))
        if follower:
            logger.debug('cloning turtle {}@{} to {}'.format(self.turtleName, self.userName, follower))
            monkapi.clone_turtle(self.turtleName, self.userName, follower)
            logger.debug('{} is following {}'.format(follower, self.userName))
            monkapi.follow_turtle(self.turtleName, self.userName, follower=follower)
taskT(AddClone)

class RemoveClone(Task):
    def act(self):
        leader = monkapi.get_leader(self.turtleName, self.userName)
        followers = monkapi.get_followers(self.turtleName, self.userName)
        for follower in followers:
            worker.workerBroker.unfollow(follower, self.turtleName, leader=self.userName)
            worker.workerBroker.follow(follower, self.turtleName, leader=leader)
        monkapi.remove_turtle(self.turtleName, self.userName)
        worker.workerBroker.unfollow(leader, self.turtleName, follower=self.userName)
taskT(RemoveClone)

class AddData(Task):
    def act(self):
        entity = self.get('entity')
        if entity:
            monkapi.add_data(self.turtleName, self.userName, entity)
taskT(AddData)

class Predict(Task):
    def act(self):
        logger.debug('test on data from {}'.format(self.userName))
        entity = self.get('entity')
        #isPersonalized = decodedMessage.get('isPersonalized',1)
        if entity:
            monkapi.predict(self.turtleName, self.userName, entity)
taskT(Predict)

class AcknowledgeRegistration(Task):
    def act(self):
        workerName = self.get('name')
        partition = self.get('partition')
        offsetSkip = self.get('offsetSkip')
        logger.info('Received registration for {} at partition {}'.format(workerName, partition))
        if workerName == worker.serverName:
            worker.workerBroker.set_consumer_partition([partition])
            logger.info('{} registered and is ready'.format(workerName))
            try:
                worker.workerBroker.seek(int(offsetSkip))
            except Exception as e:
                logger.warning('Can not seek to offset {}'.format(offsetSkip))
                logger.warning(e.message)
taskT(AcknowledgeRegistration)

def main():
    global worker
    myname = '_'.join([sys.argv[1], str(ut.get_mac())])
    config = get_config(sys.argv[2:], myname, 'monkworker.py name')
    worker = MonkWorker(myname, config)
    worker.run()

if __name__=='__main__':
    main()


import os
from pprint import pprint
from upwork.routers import auth
from upwork.routers.hr.freelancers import applications
from upwork.routers.jobs import profile
from datetime import datetime, timedelta
from modules.database import create_tables, insert_applications_and_questions
import json
from rich.console import Console

console = Console()

def get_job_applications(client):
    # Uses this API:
    # https://developers.upwork.com/?lang=python#contracts-and-offers_list-job-applications-as-freelancer
    console.print("Initializing database...", style="bold cyan")

    # Instantiate the applications API with your client
    console.print("Setting up applications API...", style="bold cyan")
    app_api = applications.Api(client)

    # Read the days back from the .env file for job applications, defaulting to 7 if not set
    job_applications_days_back = int(os.environ.get("JOB_APPLICATIONS_DAYS_BACK", 7))

    # Get the timestamp from the specified days back
    timestamp_from = int((datetime.now() - timedelta(days=job_applications_days_back)).timestamp())

    # Define any optional parameters that you need
    optional_params = {
        "status": "submitted",
        "cursor_limit": 20,
    }

    statuses_to_check = ["submitted", "archived"]
    saved_count = 0  # Initialize saved_count variable

    for status in statuses_to_check:
        optional_params["status"] = status
        # Flag to check if there are applications to process
        has_applications = True
        console.print(f"Processing status: {status}...", style="bold cyan")

        while has_applications:
            # Make the API call and store the result
            result = app_api.get_list(optional_params)

            # Filter applications by timestamp
            recent_applications = list(filter(
                lambda x: int(datetime.fromisoformat(x["auditDetails"]["createdTs"].rstrip('Z')).timestamp()) >= timestamp_from,
                result["data"]["applications"]
            ))

            # Save the recent applications to a SQLite database
            insert_applications_and_questions(recent_applications, client, get_job_details)

            # Update the saved_count variable with the number of saved applications
            saved_count += len(recent_applications)

            if not recent_applications:  # No recent applications in the current batch
                has_applications = False

            else:
                # Check if there's a next cursor
                if "paging" in result["data"]:
                    total_applications = int(result["data"]["paging"]["total"])
                    current_offset = int(result["data"]["paging"]["count"])  # current page size

                    if current_offset < total_applications:
                        # Update the cursor to the next offset value
                        encoded_offset = result["data"]["paging"]["offset"]
                        optional_params["cursor"] = encoded_offset
                        console.print(f"Moving to the next cursor for status: {status}...", style="bold cyan")
                    else:
                        # No more applications available
                        has_applications = False
                else:
                    # No more applications available
                    has_applications = False

    # Print the result
    console.print(f"{saved_count} Job Applications saved to the database.", style="bold green")

def get_job_details(client, job_key):
    # Uses this API:
    # https://developers.upwork.com/?lang=python#jobs_get-job-profile

    # Instantiate the jobs API with your client
    profile_api = profile.Api(client)

    # Get the job details by key (sent as "opening_ciphertext")
    job_details = profile_api.get_specific(job_key)

    return job_details

def get_user_info(client):
    console.print("My info", style="bold")
    pprint(auth.Api(client).get_user_info())
    return


import numpy as np
import matplotlib.pyplot as plt

begintime = 0
endtime = 16
timelength = endtime - begintime
N = 512 # number of sample
sampling_freq = timelength / N # sampling points들의 간격.
timepoints = np.arange(begintime, endtime, sampling_freq)

alpha = 2.0
f_0 = 0.8
def f_T(x) :
    if x <= 0 :
        u = 0
    else :
        u = 1
    return x * np.exp(-alpha * x) * np.cos(2*np.pi*f_0*x) * u
signal = [f_T(x) for x in timepoints]

# Discrete Fourier transform with Fast Fourier Transform
fft = np.fft.fft(signal) / N
fft = np.fft.fftshift(fft)
fft_magnitude = abs(fft)

# freq domain
k = np.arange(N)
T = N / sampling_freq
freq = k / T

# Draw the signal graph
plt.title("the amplitude")
plt.ylabel("amplitude")
plt.plot(freq, fft_magnitude)
plt.grid()

plt.savefig("02fft_magnitude.png")


import os
import datetime
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import copy

from resnet_ode.models import (
    IResNetAuto2,
    ResNetMemoryCustom,
)

device = "cuda"

n_workers = 10

n_layers = [64, 64, 64, 64]
net = IResNetAuto2
net = net(num_classes=1000, n_layers=n_layers)
"""
LOAD HERE the checkpoint e.g:
checkpoint =
torch.load("models/comparison_auto/results_imagenet_auto_compare/
comparison_auto/iresnet_0_nl_8/checkpoint.pth.tar")
"""
net = torch.nn.DataParallel(net).cuda()
net.load_state_dict(checkpoint["state_dict"])

net_2 = ResNetMemoryCustom(num_classes=1000, layers=n_layers, use_backprop=False)
net_2 = torch.nn.DataParallel(net_2).cuda()

layer = net.module.layer1[0]
net_2.module.layer1[0].conv1 = copy.deepcopy(layer.conv1)
net_2.module.layer1[0].bn1 = copy.deepcopy(layer.bn1)
net_2.module.layer1[0].conv2 = copy.deepcopy(layer.conv2)
net_2.module.layer1[0].bn2 = copy.deepcopy(layer.bn2)
net_2.module.layer1[0].conv3 = copy.deepcopy(layer.conv3)
net_2.module.layer1[0].bn3 = copy.deepcopy(layer.bn3)
net_2.module.layer1[0].downsample[0] = copy.deepcopy(layer.downsample[0])
net_2.module.layer1[0].downsample[1] = copy.deepcopy(layer.downsample[1])

layer = net.module.layer2[0]
net_2.module.layer2[0].conv1 = copy.deepcopy(layer.conv1)
net_2.module.layer2[0].bn1 = copy.deepcopy(layer.bn1)
net_2.module.layer2[0].conv2 = copy.deepcopy(layer.conv2)
net_2.module.layer2[0].bn2 = copy.deepcopy(layer.bn2)
net_2.module.layer2[0].conv3 = copy.deepcopy(layer.conv3)
net_2.module.layer2[0].bn3 = copy.deepcopy(layer.bn3)
net_2.module.layer2[0].downsample[0] = copy.deepcopy(layer.downsample[0])
net_2.module.layer2[0].downsample[1] = copy.deepcopy(layer.downsample[1])

layer = net.module.layer3[0]
net_2.module.layer3[0].conv1 = copy.deepcopy(layer.conv1)
net_2.module.layer3[0].bn1 = copy.deepcopy(layer.bn1)
net_2.module.layer3[0].conv2 = copy.deepcopy(layer.conv2)
net_2.module.layer3[0].bn2 = copy.deepcopy(layer.bn2)
net_2.module.layer3[0].conv3 = copy.deepcopy(layer.conv3)
net_2.module.layer3[0].bn3 = copy.deepcopy(layer.bn3)
net_2.module.layer3[0].downsample[0] = copy.deepcopy(layer.downsample[0])
net_2.module.layer3[0].downsample[1] = copy.deepcopy(layer.downsample[1])

layer = net.module.layer4[0]
net_2.module.layer4[0].conv1 = copy.deepcopy(layer.conv1)
net_2.module.layer4[0].bn1 = copy.deepcopy(layer.bn1)
net_2.module.layer4[0].conv2 = copy.deepcopy(layer.conv2)
net_2.module.layer4[0].bn2 = copy.deepcopy(layer.bn2)
net_2.module.layer4[0].conv3 = copy.deepcopy(layer.conv3)
net_2.module.layer4[0].bn3 = copy.deepcopy(layer.bn3)
net_2.module.layer4[0].downsample[0] = copy.deepcopy(layer.downsample[0])
net_2.module.layer4[0].downsample[1] = copy.deepcopy(layer.downsample[1])

layer = net.module.layer1[1].function
for i, _ in enumerate(net_2.module.layer1[1].network.functions):
    net_2.module.layer1[1].network.functions[i].conv1 = copy.deepcopy(layer.conv1)
    net_2.module.layer1[1].network.functions[i].bn1 = copy.deepcopy(layer.bn1)
    net_2.module.layer1[1].network.functions[i].conv2 = copy.deepcopy(layer.conv2)
    net_2.module.layer1[1].network.functions[i].bn2 = copy.deepcopy(layer.bn2)
    net_2.module.layer1[1].network.functions[i].conv3 = copy.deepcopy(layer.conv3)
    net_2.module.layer1[1].network.functions[i].bn3 = copy.deepcopy(layer.bn3)

layer = net.module.layer2[1].function
for i, _ in enumerate(net_2.module.layer2[1].network.functions):
    net_2.module.layer2[1].network.functions[i].conv1 = copy.deepcopy(layer.conv1)
    net_2.module.layer2[1].network.functions[i].bn1 = copy.deepcopy(layer.bn1)
    net_2.module.layer2[1].network.functions[i].conv2 = copy.deepcopy(layer.conv2)
    net_2.module.layer2[1].network.functions[i].bn2 = copy.deepcopy(layer.bn2)
    net_2.module.layer2[1].network.functions[i].conv3 = copy.deepcopy(layer.conv3)
    net_2.module.layer2[1].network.functions[i].bn3 = copy.deepcopy(layer.bn3)

layer = net.module.layer3[1].function
for i, _ in enumerate(net_2.module.layer3[1].network.functions):
    net_2.module.layer3[1].network.functions[i].conv1 = copy.deepcopy(layer.conv1)
    net_2.module.layer3[1].network.functions[i].bn1 = copy.deepcopy(layer.bn1)
    net_2.module.layer3[1].network.functions[i].conv2 = copy.deepcopy(layer.conv2)
    net_2.module.layer3[1].network.functions[i].bn2 = copy.deepcopy(layer.bn2)
    net_2.module.layer3[1].network.functions[i].conv3 = copy.deepcopy(layer.conv3)
    net_2.module.layer3[1].network.functions[i].bn3 = copy.deepcopy(layer.bn3)

layer = net.module.layer4[1].function
for i, _ in enumerate(net_2.module.layer4[1].network.functions):
    net_2.module.layer4[1].network.functions[i].conv1 = copy.deepcopy(layer.conv1)
    net_2.module.layer4[1].network.functions[i].bn1 = copy.deepcopy(layer.bn1)
    net_2.module.layer4[1].network.functions[i].conv2 = copy.deepcopy(layer.conv2)
    net_2.module.layer4[1].network.functions[i].bn2 = copy.deepcopy(layer.bn2)
    net_2.module.layer4[1].network.functions[i].conv3 = copy.deepcopy(layer.conv3)
    net_2.module.layer4[1].network.functions[i].bn3 = copy.deepcopy(layer.bn3)

net_2.module.conv1 = copy.deepcopy(net.module.conv1)

net_2.module.bn1 = copy.deepcopy(net.module.bn1)

net_2.module.maxpool = copy.deepcopy(net.module.maxpool)

net_2.module.avgpool = copy.deepcopy(net.module.avgpool)

net_2.module.fc = copy.deepcopy(net.module.fc)

net = net_2

## IMAGENET

# net = ResNet101
# net = net(num_classes=1000)
#
# checkpoint = torch.load('models/checkpoint_fine_tuning_imagenet/resnet101_v2_0.pth.tar')
#
# net = torch.nn.DataParallel(net).cuda()
# net.load_state_dict(checkpoint['state_dict'])

datapath = "/gpfsdswork/dataset/imagenet/RawImages/"
expe_name = "finetuning"
arch = net
batch_size = 256
gpu = None
weight_decay = 1e-4
print_freq = 100
momentum = 0.9
lr = 0.0005

criterion = nn.CrossEntropyLoss().cuda(gpu)
optimizer = torch.optim.SGD(
    net.parameters(), lr, momentum=momentum, weight_decay=weight_decay
)

traindir = os.path.join(datapath, "train")
valdir = os.path.join(datapath, "val")

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

train_dataset = datasets.ImageFolder(
    traindir,
    transforms.Compose(
        [
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]
    ),
)

train_sampler = None

train_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size=batch_size,
    shuffle=(train_sampler is None),
    num_workers=n_workers,
    pin_memory=True,
    sampler=train_sampler,
)

val_loader = torch.utils.data.DataLoader(
    datasets.ImageFolder(
        valdir,
        transforms.Compose(
            [
                transforms.Resize(256),
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                normalize,
            ]
        ),
    ),
    batch_size=batch_size,
    shuffle=False,
    num_workers=n_workers,
    pin_memory=True,
)

def train(train_loader, model, criterion, optimizer, epoch):
    batch_time = AverageMeter("Time", ":6.3f")
    data_time = AverageMeter("Data", ":6.3f")
    losses = AverageMeter("Loss", ":.4e")
    top1 = AverageMeter("Acc@1", ":6.2f")
    top5 = AverageMeter("Acc@5", ":6.2f")
    progress = ProgressMeter(
        len(train_loader),
        [batch_time, data_time, losses, top1, top5],
        prefix="Epoch: [{}]".format(epoch),
    )

    # switch to train mode
    model.train()

    end = time.time()
    for i, (images, target) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        if gpu is not None:
            images = images.cuda(gpu, non_blocking=True)
        if torch.cuda.is_available():
            target = target.cuda(gpu, non_blocking=True)

        # compute output
        output = model(images)
        loss = criterion(output, target)

        # measure accuracy and record loss
        acc1, acc5 = accuracy(output, target, topk=(1, 5))
        losses.update(loss.item(), images.size(0))
        top1.update(acc1[0], images.size(0))
        top5.update(acc5[0], images.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % print_freq == 0:
            progress.display(i)
    return losses.avg, top1.avg.item(), top5.avg.item()

def validate(val_loader, model, criterion):
    batch_time = AverageMeter("Time", ":6.3f")
    losses = AverageMeter("Loss", ":.4e")
    top1 = AverageMeter("Acc@1", ":6.2f")
    top5 = AverageMeter("Acc@5", ":6.2f")
    progress = ProgressMeter(
        len(val_loader),
        [batch_time, losses, top1, top5],
        prefix="Test: ",
    )

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (images, target) in enumerate(val_loader):
            if gpu is not None:
                images = images.cuda(gpu, non_blocking=True)
            if torch.cuda.is_available():
                target = target.cuda(gpu, non_blocking=True)

            # compute output
            output = model(images)
            loss = criterion(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), images.size(0))
            top1.update(acc1[0], images.size(0))
            top5.update(acc5[0], images.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            if i % print_freq == 0:
                progress.display(i)

        print(
            " * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}".format(top1=top1, top5=top5)
        )

    return losses.avg, top1.avg.item(), top5.avg.item()

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self, name, fmt=":f"):
        self.name = name
        self.fmt = fmt
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

    def __str__(self):
        fmtstr = "{name} {val" + self.fmt + "} ({avg" + self.fmt + "})"
        return fmtstr.format(**self.__dict__)

class ProgressMeter(object):
    def __init__(self, num_batches, meters, prefix=""):
        self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
        self.meters = meters
        self.prefix = prefix

    def display(self, batch):
        entries = [self.prefix + self.batch_fmtstr.format(batch)]
        entries += [str(meter) for meter in self.meters]
        print("\t".join(entries))

    def _get_batch_fmtstr(self, num_batches):
        num_digits = len(str(num_batches // 1))
        fmt = "{:" + str(num_digits) + "d}"
        return "[" + fmt + "/" + fmt.format(num_batches) + "]"

def adjust_learning_rate(optimizer, epoch, lr):
    """Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
    lr = lr * (0.33 ** (epoch // 5))
    for param_group in optimizer.param_groups:
        param_group["lr"] = lr
    print("learning rate:", lr)

def accuracy(output, target, topk=(1,)):
    """Computes the accuracy over the k top predictions for the specified values of k"""
    with torch.no_grad():
        maxk = max(topk)
        batch_size = target.size(0)

        _, pred = output.topk(maxk, 1, True, True)
        pred = pred.t()
        correct = pred.eq(target.view(1, -1).expand_as(pred))

        res = []
        for k in topk:
            correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
            res.append(correct_k.mul_(100.0 / batch_size))
        return res

train_losss, train_top1s, train_top5s = [], [], []
test_losss, test_top1s, test_top5s = [], [], []
start_epoch = 0
n_epochs = 16

saveadr = "results_fine_tuning_imagenet_adjoint/resnet_64_lr_0005"
model_save_adr = (
    "models/checkpoint_fine_tuning_imagenet_adjoint/resnet_64_lr_0005.pth.tar"
)

def save_checkpoint(state, filename="checkpoint.pth.tar"):
    torch.save(state, filename)

def main():
    for epoch in range(start_epoch, start_epoch + n_epochs):
        t0 = time.time()
        adjust_learning_rate(optimizer, epoch, lr)

        # train for one epoch
        test_loss, test_top1, test_top5 = validate(val_loader, net, criterion)
        train_loss, train_top1, train_top5 = train(
            train_loader, net, criterion, optimizer, epoch
        )

        # evaluate on validation set

        train_losss.append(train_loss)
        train_top1s.append(train_top1)
        train_top5s.append(train_top5)
        test_losss.append(test_loss)
        test_top1s.append(test_top1)
        test_top5s.append(test_top5)
        print(test_loss, test_top1, test_top5)
        np.save(
            "%s.npy" % saveadr,
            np.array(
                [
                    train_losss,
                    train_top1s,
                    train_top5s,
                    test_losss,
                    test_top1s,
                    test_top5s,
                ]
            ),
        )
        print(
            "Epoch %d took %s"
            % (
                epoch,
                str(datetime.timedelta(seconds=int(time.time() - t0))),
            )
        )

        save_checkpoint(
            {
                "epoch": epoch + 1,
                "arch": arch,
                "state_dict": net.state_dict(),
                "optimizer": optimizer.state_dict(),
                "train_loss": train_losss,
                "train_top1": train_top1s,
                "train_top5": train_top5s,
                "test_loss": test_losss,
                "test_top1": test_top1s,
                "test_top5": test_top5s,
            },
            filename=model_save_adr,
        )
    return train_top1s, train_losss, test_top1s, test_losss

main()


# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
"""
Vikas Dangi
B20238
DS3-Lab 4
"""
# Importing modules
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
# import matplotlib.pyplot as plt
# from sklearn.metrics import plot_confusion_matrix

# reading the file
df=pd.read_csv("SteelPlateFaults-2class.csv")
k=[1,3,5]
# assigning data in X and target in y
X=df.iloc[:,:27]
y=df['Class']

# Performing the split by taking approx 70% of data for training from both the data and the target
#NOTE: After this we will have 70% of target from each class and then they will be clumbed together automatically to form our train data using train_test_split method
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42,shuffle=True)

X_train.to_csv('SteelPlateFaults-train.csv',index=False)
X_test.to_csv('SteelPlateFaults-test.csv',index=False)

# %%

#Question 1

#A

# Function to Calculate and print the Confusion matrix
def ConfusionMat(n):
    knn_clf=KNeighborsClassifier(n_neighbors=n)
    knn_clf.fit(X_train,y_train)
    knn_predictions=knn_clf.predict(X_test)
    print("The Confusion matrix for k=",n,"is: ")
    print(confusion_matrix(y_test,knn_predictions))

#calling function
for n in k:
    ConfusionMat(n)

#B

# Function to Calculate and print the Classification accuracy
def ClassificationAccuracy(n):
    knn_clf=KNeighborsClassifier(n_neighbors=n)
    knn_clf.fit(X_train,y_train)
    knn_predictions=knn_clf.predict(X_test)
    return accuracy_score(y_test,knn_predictions)
#calling function
mx=0
nmax=0

for n in k:
    acc=ClassificationAccuracy(n)
    print("Accuracy of the knn algorithm with k=",n,"is : ",acc)
    if(acc>mx):
        mx=acc
        nmax=n
print("The maximum accuracy is for the value k=",nmax," that is ",mx)

# %%
# Question 2

#normalisation
X_test=(X_test-X_train.min())/(X_train.max()-X_train.min())
X_test.to_csv("SteelPlateFaults-test-Normalised.csv")

#normalisation
X_train=(X_train-X_train.min())/(X_train.max()-X_train.min())
X_train.to_csv("SteelPlateFaults-train-Normalised.csv")

#A
for n in k:
    ConfusionMat(n)

#B
mx1=0
nmax=0
for n in k:
    acc=ClassificationAccuracy(n)
    print("Accuracy of the knn algorithm with k=",n,"is : ",acc)
    if(acc>mx1):
        mx1=acc
        nmax=n
print("The maximum accuracy is for the value k=",nmax," that is ",mx1)

# %%
# Question 3
#Dropping the correlated columns
df=df.drop(["X_Minimum","Y_Minimum","TypeOfSteel_A300","TypeOfSteel_A400"],axis=1)
X0=df.loc[df["Class"]==0]
X1=df.loc[df["Class"]==1]
X0=X0.drop("Class",axis=1)
X1=X1.drop("Class",axis=1)
y0=df["Class"].loc[df["Class"]==0]
y1=df["Class"].loc[df["Class"]==1]
X_train0, X_test0, y_train0, y_test0 = train_test_split(X0, y0, test_size=0.3, random_state=42,shuffle=True)
X_train1, X_test1, y_train1, y_test1 = train_test_split(X1, y1, test_size=0.3, random_state=42,shuffle=True)

# function to calculate the likelihoood value
def likelihood(x,D):
    cov=D.cov()
    mean=D.mean()
    det=abs(np.linalg.det(cov))
    meanminus=x-mean
    d=len(D.columns)
    invcov=pd.DataFrame(np.linalg.pinv(cov.values), cov.columns, cov.index)
    # using the formula for multivariate or multimodel distribution
    p=(1/(((2*np.pi)**(d/2))*det**0.5)) * (np.e**(-0.5*(meanminus@invcov@meanminus)))
    return p

P_C0=len(y_train0)/(len(y_train0)+len(y_train1))
P_C1=len(y_train1)/(len(y_train0)+len(y_train1))
Xtrain=X_train0.append(X_train1)
Xtest=X_test0.append(X_test1)
ytest=y_test0.append(y_test1)
predictions=[] #to store the final predictions

# to predict all the test cases
for i in range(len(Xtest)):
    lh0=likelihood(Xtest.iloc[i],X_train0)
    lh1=likelihood(Xtest.iloc[i],X_train1)
    postPosb0=P_C0*lh0/(P_C0*lh0+P_C1*lh1)
    postPosb1=P_C1*lh1/(P_C0*lh0+P_C1*lh1)
    if(postPosb0>=postPosb1):
        predictions.append(0)
    else:
        predictions.append(1)
mx2=accuracy_score(ytest,predictions)
print("The accuracy of the Bayes CLassifier is ",mx2)

print("The Confusion matrix for k=",n,"is: ")
print(confusion_matrix(ytest,predictions))

print("The mean of Class 0 training data: \n",X_train0.mean())
print("The mean of Class 1 training data: \n",X_test0.mean())

# %%
#Question 4
# printing all the accuracies and comparing them
print("The different accuracies in the above processes are ",mx," ",mx1," ",mx2)
print("The highest we get in the ",max(max(mx,mx1),mx2))

# %%

D. Data Classification using K-Nearest Neighbour Classifier and Bayes
Classifier with Unimodal Gaussian Density/Code_Lab4-B20238.py


# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions

# This is a simple example for a custom action which utters "Hello World!"

from typing import Any, Text, Dict, List, Tuple
from rasa_sdk import Action, Tracker, FormValidationAction
from rasa_sdk.events import EventType, SlotSet
from rasa_sdk.executor import CollectingDispatcher
#
#
from database_utils.constants import EMAIL, REQUESTED_SLOT,OTP
from database_utils.database_utils import is_valid_user, is_valid_otp

class AuthenticatedAction(Action):
    def name(self) -> Text:
        return "action_authenticated"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
            email = tracker.get_slot("email")
            otp = tracker.get_slot("otp")
            if email is not None and otp is not None:
                dispatcher.utter_message(template="utter_authenticated_successfully")
            else:
                dispatcher.utter_message(text="utter_authentication_failure")
            return []

class ValidateAuthFormAction(FormValidationAction):
    def name(self) -> Text:
        return "validate_auth_form"

    def validate_email(
        self,
        value: Text,
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: "DomainDict",
    ) -> List[EventType]:
        returned_slots = {}
        if value is not None and is_valid_user(value):
            returned_slots = {EMAIL:value}
        else:
            returned_slots = {REQUESTED_SLOT: EMAIL}
            if value is None:
                dispatcher.utter_message(template="utter_email_not_valid")
            elif not is_valid_user(value):
                dispatcher.utter_message(template="utter_email_not_registered")
        return returned_slots

    def validate_otp(
        self,
        value: float,
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: "DomainDict",
    ) -> List[EventType]:
        email = tracker.get_slot(EMAIL)
        returned_slots = {}
        if value is not None and is_valid_otp(value,email):
            returned_slots = {OTP:value}
        else:
            returned_slots ={REQUESTED_SLOT:OTP}
            dispatcher.utter_message(template="utter_otp_not_valid")
        return returned_slots