import shutil
from pathlib import Path
import logging
import os

FORMAT, DATE_FORMAT = "%(asctime)s - %(message)s", "%Y-%m-%d %H:%M:%S"
LOG_DIR, LOG_NAME = "logs", "log.log"

log = logging.getLogger('Logger')

def _create_log_dir():
    path_to_log_dir = os.path.abspath(LOG_DIR)
    Path(path_to_log_dir).mkdir(parents=True, exist_ok=True)
    shutil.rmtree(path_to_log_dir)
    os.mkdir(path_to_log_dir)
    log_file = open(os.path.abspath(os.path.join(LOG_DIR, LOG_NAME)), 'tw')
    log_file.close()

def _setup_file_handler():
    _create_log_dir()
    formatter = logging.Formatter(FORMAT, datefmt=DATE_FORMAT)
    file_handler = logging.FileHandler(os.path.join(LOG_DIR, LOG_NAME), encoding='utf-8')
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.DEBUG)
    log.addHandler(file_handler)

def _setup_console_handler():
    console_formatter = logging.Formatter("%(log_color)s %(message)s", datefmt=DATE_FORMAT)
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(console_formatter)
    console_handler.setLevel(logging.INFO)
    log.addHandler(console_handler)

def _setup_logstash_handler(host, port):
    from logstash_async.handler import AsynchronousLogstashHandler
    async_handler = AsynchronousLogstashHandler(host, port, database_path=None)
    log.addHandler(async_handler)

# example init logger
def setup_logger():
    _setup_console_handler()
    _setup_file_handler()
    _setup_logstash_handler(host='localhost', port=5000)


##
# Compute the area of a regular polygon
#

from math import pi, tan

# Read the length of a side s and the number of sides n
print("Please, enter: ")
s = float(input("the length of a side in cm: "))
n = int(input("the number of sides: "))

# Compute the area
area = (n * s ** 2) / (4 * tan(pi/n))

# Display the result
print("The area measures % .2f cm^2" % area)


import torch
import numpy as np
import os
from PIL import Image
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader, TensorDataset
from core.utils.dataset_utils import ImagePathDataset
from torchvision import transforms, utils
from neuro_data_analysis.neural_data_lib import load_img_resp_pairs, load_neural_data
import matplotlib.pyplot as plt
#%%
BFEStats_merge, BFEStats = load_neural_data()
#%%
def _get_ref_img(BFEStats, Expi,):
    S = BFEStats[Expi - 1]  # Expi follows matlab convention, starts from 1
    imglist = S['imageName']
    refimgfn_set = set([imgfnl[0] for imgfnl in imglist if imgfnl[0].endswith("_nat")])
    return refimgfn_set

def _get_comments(BFEStats, Expi):
    S = BFEStats[Expi - 1]  # Expi follows matlab convention, starts from 1
    return S["meta"]["comments"]

def get_expstr(BFEStats, Expi):
    S = BFEStats[Expi - 1]  # Expi follows matlab convention, starts from 1
    expstr = f"Exp {Expi:03d} {S['meta']['ephysFN']} Pref chan{int(S['evol']['pref_chan'][0])} U{int(S['evol']['unit_in_pref_chan'][0])}" \
             f"\nimage size {S['evol']['imgsize']} deg  pos {S['evol']['imgpos'][0]}" \
             f"\nEvol thr0: {S['evol']['space_names'][0][0]}" \
             f"   thr1: {S['evol']['space_names'][1][0]}"
    return expstr
#%%
from os.path import join
from CorrFeatTsr_lib import Corr_Feat_Machine, visualize_cctsr, loadimg_preprocess
# from core.utils.layer_hook_utils import featureFetcher_module
from core.utils.CNN_scorers import TorchScorer, load_featnet
figroot = r"E:\Network_Data_Sync\corrFeatTsr_BigGAN\fig_summary"
saveroot = r"E:\Network_Data_Sync\corrFeatTsr_BigGAN"
#%%
model, _ = load_featnet("resnet50_linf8")
recmodule_dict = {"layer1": model.layer1,
                   "layer2": model.layer2,
                   "layer3": model.layer3,
                   "layer4": model.layer4}
#%%
plot_err_dict = {}
Animal = "Both"
for Expi in [66]: #tqdm(range(70, 191)):
    for thread in range(2):
        try:
            imgfps, resp_vec, bsl_vec, gen_vec = load_img_resp_pairs(BFEStats, Expi,
                         "Evol", thread=thread, stimdrive="S:", output_fmt="vec")
        except Exception as e:
            print(f"Exp {Expi} thread {thread} failed to load, try Network version")
            try:
                imgfps, resp_vec, bsl_vec, gen_vec = load_img_resp_pairs(BFEStats, Expi,
                         "Evol", thread=thread, stimdrive="N:", output_fmt="vec")
            except Exception as e2:
                print(f"Exp {Expi} thread {thread} failed to load")
            plot_err_dict[(Expi, thread)] = e
            continue
        if len(imgfps) == 0:
            continue
        # create a dataloader for the image response pairs
        evol_ds = ImagePathDataset(imgfps, resp_vec, transform=None, img_dim=(224, 224))
        evol_dl = DataLoader(evol_ds, batch_size=60, shuffle=False, num_workers=0)

        fetcher = Corr_Feat_Machine()
        fetcher.register_module_hooks(recmodule_dict, verbose=False)
        fetcher.init_corr()
        for i, (imgtsr, resps) in tqdm(enumerate(evol_dl)):
            with torch.no_grad():
                model(imgtsr.cuda())
            fetcher.update_corr(resps.float())

        fetcher.calc_corr()
        fetcher.clear_hook()
        savedict = fetcher.make_savedict()

        np.savez(join(saveroot, f"{Animal}_Exp{Expi:02d}_Evol_thr{thread}_res-robust_corrTsr.npz"),
                 **savedict)

        titstr = get_expstr(BFEStats, Expi)
        figh = visualize_cctsr(fetcher, ["layer1", "layer2", "layer3", "layer4"], None, Expi,
                       "Both", f"BigGAN_Evol_thr{thread}_res-robust", titstr, figdir=figroot)
        plt.close(figh)
#%%

plot_err_dict_cmb = {}
Animal = "Both"
for Expi in tqdm(range(1, 191)):
    try:
        imgfps1, resp_vec1, bsl_vec1, gen_vec1 = load_img_resp_pairs(BFEStats, Expi,
                     "Evol", thread=0, stimdrive="S:", output_fmt="vec")
        imgfps2, resp_vec2, bsl_vec2, gen_vec2 = load_img_resp_pairs(BFEStats, Expi,
                     "Evol", thread=1, stimdrive="S:", output_fmt="vec")
        imgfps = imgfps1 + imgfps2
        resp_vec = np.concatenate((resp_vec1, resp_vec2), axis=0)
        bsl_vec = np.concatenate((bsl_vec1, bsl_vec2), axis=0)
        gen_vec = np.concatenate((gen_vec1, gen_vec2), axis=0)
    except Exception as e:
        print(f"Exp {Expi} failed to load")
        plot_err_dict_cmb[Expi] = e
        continue
    if len(imgfps) == 0:
        continue
    # create a dataloader for the image response pairs
    evol_ds = ImagePathDataset(imgfps, resp_vec, transform=None, img_dim=(224, 224))
    evol_dl = DataLoader(evol_ds, batch_size=60, shuffle=False, num_workers=0)

    fetcher = Corr_Feat_Machine()
    fetcher.register_module_hooks(recmodule_dict, verbose=False)
    fetcher.init_corr()
    for i, (imgtsr, resps) in tqdm(enumerate(evol_dl)):
        with torch.no_grad():
            model(imgtsr.cuda())
        fetcher.update_corr(resps.float())

    fetcher.calc_corr()
    fetcher.clear_hook()
    savedict = fetcher.make_savedict()

    np.savez(join(saveroot, f"{Animal}_Exp{Expi:02d}_Evol_thr{'_cmb'}_res-robust_corrTsr.npz"),
             **savedict)

    titstr = get_expstr(BFEStats, Expi)
    figh = visualize_cctsr(fetcher, ["layer1", "layer2", "layer3", "layer4"], None, Expi,
                   "Both", f"BigGAN_Evol_thr{'_cmb'}_res-robust", titstr, figdir=figroot)
    plt.close(figh)

#%%
Expi = 12
thread = 0
imgfps, resp_vec, bsl_vec, gen_vec = load_img_resp_pairs(BFEStats, Expi,
                 "Evol", thread=thread, stimdrive="S:", output_fmt="vec")
#%%
plt.plot(gen_vec, resp_vec, "o", alpha=0.5)
plt.plot(gen_vec, bsl_vec, "o", alpha=0.5)
plt.show()

#%% bug fixing
# fetcher.init_corr()
print(BFEStats[-9]["meta"]["stimuli"])
print(BFEStats[-8]["meta"]["stimuli"])

BFEStats[-9]["meta"]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2021-05-28-Beto-01\2021-05-28-10-12-47"
BFEStats[-8]["meta"]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2021-05-28-Beto-02\2021-05-28-10-28-01"
BFEStats[5 - 1]["meta"]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2020-07-23-Beto-01\2020-07-23-15-59-38"
BFEStats[92 - 1]["meta"]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2020-07-27-Alfa-01\2020-07-27-09-47-40"
BFEStats_merge["meta"][-9]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2021-05-28-Beto-01\2021-05-28-10-12-47"
BFEStats_merge["meta"][-8]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2021-05-28-Beto-02\2021-05-28-10-28-01"
BFEStats_merge["meta"][5 - 1]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2020-07-23-Beto-01\2020-07-23-15-59-38"
BFEStats_merge["meta"][92 - 1]["stimuli"] = r"N:\Stimuli\2020-BigGAN\2020-07-27-Alfa-01\2020-07-27-09-47-40"
#%%
import pickle
matroot = "E:\OneDrive - Washington University in St. Louis\Mat_Statistics"
# pickle.dump(BFEStats, open(join(matroot, "Both_BigGAN_FC6_Evol_Stats_expsep.pkl"), "wb"))
pickle.dump(BFEStats_merge, open(join(matroot, "Both_BigGAN_FC6_Evol_Stats.pkl"), "wb"))
#%% Check the images are loadable
error_dict = {}
for Expi in tqdm(range(1, 191)):
    for thread in range(2):
        try:
            imgfps, resp_vec, bsl_vec, gen_vec = load_img_resp_pairs(BFEStats, Expi,
                         "Evol", thread=thread, stimdrive="S:", output_fmt="vec")
        except Exception as e:
            print(f"Exp{Expi} thread{thread}", e)
            error_dict[(Expi, thread)] = e

#%%
# print(_get_comments(BFEStats, 110))
# _get_ref_img(BFEStats, 110)
print(get_expstr(BFEStats, 12))
#%%
import re
import glob
import shutil
from pathlib import Path
for Expi in [66, 78]: #range(107, 111):
    print(Expi, )
    refimgfns = _get_ref_img(BFEStats, Expi)
    imgfn_strips = [re.sub("_thread\d\d\d_nat", "", refimgfn) for refimgfn in refimgfns]
    imgfn_strips = set(imgfn_strips)
    for imgfn_strip in imgfn_strips:
        # imgfn_strip = re.sub("_thread\d\d\d_nat", "", refimgfn)
        fplist = glob.glob("S:\\Stimuli\\2019-Selectivity\\*\\"+glob.escape(imgfn_strip)+".*")
        if len(fplist) == 0:
            print(imgfn_strip)
        else:
            shutil.copy2(fplist[0], Path(BFEStats[Expi-1]["meta"]["stimuli"]).parent)
            shutil.copy2(fplist[0], Path(BFEStats[Expi-1]["meta"]["stimuli"].replace("N:", "S:")).parent)


def redondear(numero):
    numero2=numero*10
    resto=numero2//10
    numero3=numero-resto
    if numero3 >= 0.5:
        print(resto+1)
    elif numero3 <= 0.5:
        print(resto)

def es_primo(numero):
    if numero < 2:
        return False
    for i in range(2, int(numero ** 0.5) + 1):
        if numero % i == 0:
            return
    return print(numero)

import random
lista=[1,2,3,4,5,6]
print(int(random.uniform(2,11)))


import pandas as pd
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
from dotenv import load_dotenv

# draw the outputs using opencv tools
def draw_bbox_conf(image, boxes, scores, color=(255, 0, 0), thickness=-1):
    overlay = image.copy()

    font_size = 0.25+0.07*min(overlay.shape[:2])/100
    font_size = max(font_size, 0.5)
    font_size = min(font_size, 0.8)
    text_offset = 7

    for box, score in zip(boxes, scores):
        xmin = box[0]
        ymin = box[1]
        xmax = box[0]+box[2]
        ymax = box[1]+box[3]

        overlay = cv2.rectangle(overlay, (xmin, ymin),
                                (xmax, ymax), color, thickness)
        display_text = f"wheat_head: {score[0]:.2f}"
        (text_width, text_height), _ = cv2.getTextSize(
            display_text, cv2.FONT_HERSHEY_SIMPLEX, font_size, 2)

        cv2.rectangle(overlay, (xmin, ymin), (xmin+text_width+text_offset,
                      ymin-text_height-int(15*font_size)), color, thickness=-1)

        overlay = cv2.putText(overlay, display_text, (xmin+text_offset, ymin-int(10*font_size)),
                              cv2.FONT_HERSHEY_SIMPLEX, font_size, (255, 255, 255), 2, lineType=cv2.LINE_AA)

    return cv2.addWeighted(overlay, 0.75, image, 0.25, 0)

def detect():
    load_dotenv("./.env")

    results_path = os.getenv("RESULTS_CSV")
    res = pd.read_csv(results_path,
                      converters={'boxes': pd.eval, 'scores': pd.eval})

    test_path = os.getenv("TEST_PATH")

    # get image names
    image_names = os.listdir(test_path)

    # root for results
    result_dir = os.getenv("RESULTS_PATH")
    for id in image_names:
        if '_' not in id:
            img_test = os.path.join(test_path, id)
            boxes = np.array(res.loc[res['image_id'] == id]['boxes'])[0]
            scores = np.array(res.loc[res['image_id'] == id]['scores'])[0]

            cv_img = cv2.imread(img_test)
            image_boxed = draw_bbox_conf(
                cv_img, boxes, scores)

            cv2.imwrite(os.path.join(result_dir, id), image_boxed)

    # cv2.imshow('experiment', image_boxed)
    # cv2.waitKey(0)

def main():
    detect()

if __name__ == '__main__':
    main()


from functools import wraps
from datetime import datetime

from ideasphere.forms import AddMissionForm, AddProblemForm
from ideasphere.db.models import Mission, Problem
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.utils import redirect

def admin(fc):
    @wraps(fc)
    def wrapped_fc(request, **kwargs):
        user = request.logged_user()
        if user is None or not user.is_admin:
            return {}, 'notfound.phtml', 404

        return fc(request, user, **kwargs)

    return wrapped_fc

@admin
def add_mission(request, user):
    form = AddMissionForm()
    return {'form': form}, 'admin/addmission.phtml'

@admin
def save_mission(request, user):
    form = AddMissionForm(request.form)

    title = form.title.data
    description = form.description.data

    try:
        Mission.load(request.session, title=title)
        return {'form': form, 'error': 'Mission already exists'}, \
                'admin/addmission.phtml'
    except NoResultFound:
        mission = Mission(title=title, description=description, created=datetime.utcnow())
        request.session.add(mission)
        return redirect('/')

@admin
def add_problem(request, user, mission_id):
    try:
        mission = Mission.load(request.session, id=mission_id)
    except NoResultFound:
        return {}, 'notfound.phtml', 404

    form = AddProblemForm()
    form.mission_id.data = mission.id
    return {'form': form,
            'mission': mission}, 'admin/addproblem.phtml'

@admin
def save_problem(request, user):
    form = AddProblemForm(request.form)
    try:
        mission = Mission.load(request.session, id=form.mission_id.data)
    except NoResultFound:
        return {}, 'notfound.phtml', 404

    title = form.title.data
    description = form.description.data

    try:
        Problem.load(request.session, title=title, mission_id=mission.id)
        return {'form': form, 'mission': mission,
                'error': 'Problem already reported'}, \
               'admin/addproblem.phtml'
    except NoResultFound:
        Problem(title=title, mission=mission,
                          description=description, created=datetime.utcnow())
        return redirect('/mission/%d' % mission.id)


import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st

def alturas(n):
    """
    Recebe um inteiro n, retorna um array com n números aleatórios de uma
    distribuição normal representando alturas corporais de uma população de n
    pessoas adultas. Salva um histograma do array 'alturas.png'

    Parâmetros
    ----------
    n : int
      número de alturas no array

    Retorna
    -------
    arr : np.ndarray
      array com as alturas aleatórias de uma distribuição normal
    """
    arr = st.norm.rvs(loc=1.7, scale=0.08, size=n)

    # plotando a figura
    fig, ax = plt.subplots(figsize=(6, 6))
    ax.set_title('Alturas')
    plt.hist(arr, bins=20, color='c')
    plt.savefig('alturas.png')

    return arr

def pesos(alturas):
    """
    Recebe um array de alturas, retorna um array de pesos de mesmo tamanho
    seguindo um índice de massa corporal retirado aleatoriamente de uma
    distribuição normal. Salva um histograma do array 'pesos.png'

    Parâmetros
    ----------
    alturas : np.ndarray
      array de alturas

    Retorna
    -------
    pesos : np.ndarray
      array com os pesos
    """
    imcs = st.norm.rvs(loc=24.5, scale=4.3, size=len(alturas))
    pesos = imcs * (alturas ** 2)

    # plotando a figura
    fig, ax = plt.subplots(figsize=(6, 6))
    ax.set_title('Pesos')
    plt.hist(pesos, bins=20, color='m')
    plt.savefig('pesos.png')

    return pesos

def regressaoLinear(alturas, pesos):
    """
    Recebe dois arrays de mesmo tamanho, um representando alturas e outro
    representando pesos. Calcula uma regressão linear, retorna uma tupla
    com o coeficiente angular e o coeficiente linear da reta. Salva um gráfico
    de dispersão e a reta calculada pela regressão 'regressao.png'

    Parâmetros
    ----------
    alturas : np.ndarray
      array com alturas
    pesos: np.ndarray
      array com pesos

    Retorna
    -------
    tuple
      tupla com o coeficiente angular e o coeficiente linear da reta calculada
      pela regressão linear
    """
    a, b = st.linregress(alturas, pesos)[:2]

    fig, ax = plt.subplots(figsize=(6, 6))

    y = lambda x : a * x + b
    x = np.linspace(alturas.min(), alturas.max())

    # plotando a figura
    ax.set_title('Altura vs. peso')
    plt.scatter(alturas, pesos)
    plt.plot(x, y(x), color='red')
    plt.savefig('regressao.png')
    plt.xlabel('Altura')
    plt.ylabel('Peso')

    return a, b


from __future__ import division, print_function
from matplotlib import pyplot as plt
import numpy as np
import matplotlib
import os

# Set some parameters to apply to all plots. These can be overridden
# in each plot if desired
# Plot size to 14" x 7"
matplotlib.rc('figure', figsize=(14, 7))
# Font size to 14
matplotlib.rc('font', size=14)
# Do not display top and right frame lines
matplotlib.rc('axes.spines', top=False, right=False)
# Remove grid lines
matplotlib.rc('axes', grid=False)
# Set background color to white
matplotlib.rc('axes', facecolor='white')

characters_list = ['Orc', 'Wolf', 'Mech']

plot_cost = False
constant_threshold = False

for character in characters_list:
	if plot_cost:
		data_series = [
			{
				'title': character + ' (Reward + Imitation)',
				'color': 'r',
				'draw': False,
				'main_dir': [character, 'NC'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + ' (Reward)',
				'color': 'm',
				'draw': False,
				'main_dir': [character, 'Reward'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': 'Using FDI-MCTS cost function as reward',#character + ' (Cost)',
				'color': '#913844',
				'draw': True,
				'main_dir': [character, 'NegCost'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + ' (Cost + Imitation)',
				'color': 'c',
				'draw': False,
				'main_dir': [character, 'NegCost+Imitation'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + ' (Cost + Imitation, 75-50)',
				'color': 'y',
				'draw': False,
				'main_dir': [character, 'NegCost+Imitation-75-50'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': 'Using DeepMimic-style reward and termination curriculum (ours)',#character + ' (75-50)',
				'color': 'g',
				'draw': True,
				'main_dir': [character, '75-50'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
		]
	elif constant_threshold:
		data_series = [
			{
				'title': character + '-Extra-Tight Threshold (0.85)',
				'color': 'c',
				'draw': True,
				'main_dir': [character, '85'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + '-Tight Threshold (0.75)',
				'color': '#212cd1',
				'draw': True,
				'main_dir': [character, '75'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + '-Medium Threshold (0.5)',
				'color': 'm',
				'draw': True,
				'main_dir': [character, '50'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + '-Loose Threshold (0.25)',
				'color': '#913844',
				'draw': True,
				'main_dir': [character, '25'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + '-No Curriculum',
				'color': 'r',
				'draw': True,
				'main_dir': [character, 'NC'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
		]
	else:
		data_series = [
			{
				'title': 'Termination curriculum (ours)',#character + ' (75-50)',
				'color': 'g',
				'draw': True,
				'main_dir': [character, '75-50'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + ' (85-50)',
				'color': '#5d258e',
				'draw': False,
				'main_dir': [character, '85-50'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': character + '-Tight Threshold (0.85)',
				'color': 'c',
				'draw': False,
				'main_dir': [character, '85'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': 'Tight threshold',#character + '-Tight Threshold (0.75)',
				'color': '#212cd1',
				'draw': True,
				'main_dir': [character, '75'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': 'Medium threshold',#character + '-Medium Threshold (0.5)',
				'color': 'm',
				'draw': True,
				'main_dir': [character, '50'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': 'Loose threshold',#character + '-Loose Threshold (0.25)',
				'color': '#913844',
				'draw': True,
				'main_dir': [character, '25'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
			,
			{
				'title': 'No termination',#character + '-No Curriculum',
				'color': 'r',
				'draw': True,
				'main_dir': [character, 'NC'],
				'dir_list': ['1', '2', '3', '4', '5']
			}
		]

	base_dir = os.path.join(os.getcwd(), 'Results')

	fig = plt.figure()
	ax1 = fig.add_subplot(111)
	ax1.set_xlabel('Timesteps')
	ax1.set_ylabel('Average Cost' if plot_cost else 'Average Reward')
	if not plot_cost:
		ax1.set_ylim(0.0, 0.85)
	ax1.ticklabel_format(style='sci',scilimits=(-3,4),axis='both')
	# ax2 = fig.add_subplot(212)
	# ax2.set_xlabel('Timesteps')
	# ax2.set_ylabel('Average Episode Length')
	# ax2.ticklabel_format(style='sci',scilimits=(-3,4),axis='both')

	def find_indices(col_names):
		indices = {}
		for i in range(len(col_names)):
			indices[col_names[i]] = i
		return indices

	lines = []
	labels = []
	for data_pack in data_series:
		if data_pack['draw']:
			time_steps = None
			values = None
			values_col = 'AccumulatedCostMean' if plot_cost else 'AccumulatedRewMean'
			color = data_pack['color']
			main_dir = os.path.join(base_dir, *data_pack['main_dir'])

			for d in data_pack['dir_list']:
				path = os.path.join(os.path.join(main_dir, d), 'progress.csv')

				col_names = np.genfromtxt(path, max_rows=1, delimiter=',', dtype=str)
				# 'Iteration' 'MaxEpLen' 'MeanEpLen' 'MinEpLen' 'AccumulatedRewStd'
				# 'AccumulatedCostMean' 'AccumulatedRewMean' 'TimestepsSoFar' 'TimeElapsed' 'AccumulatedCostStd'
				col_indices = find_indices(col_names)
				new_data = np.genfromtxt(path, skip_header=1, delimiter=',', dtype=np.float32)

				new_values = new_data[:50, col_indices[values_col]] / new_data[:50, col_indices['MeanEpLen']]
				new_time_steps = new_data[:50, col_indices['TimestepsSoFar']]
				# print(len(new_time_steps))

				if time_steps is None:
					time_steps = new_time_steps
					values = new_values
				else:
					time_steps += new_time_steps
					values = np.row_stack((values, new_values))

			time_steps /= len(data_pack['dir_list'])
			if len(data_pack['dir_list']) > 1:
				mean = np.mean(values, axis=0)
				std = np.std(values, axis=0)
			else:
				mean = values
				std = np.zeros_like(values)
			# lines.append(ax1.plot(time_steps, mean, color=color, alpha=0.8, label=data_pack['title'])[0])
			lines.append(ax1.plot(time_steps, mean, color=color, alpha=0.8)[0])
			labels.append(data_pack['title'])
			ax1.fill_between(time_steps, mean-std/2, mean+std/2, color=color, alpha=0.2)

			'''
			if data_pack['curriculum'] and len(time_steps) > 4000:
				ax1.plot(time_steps[4499], mean[4499], 's', markersize=7.5, markerfacecolor=color,
						 markeredgewidth=2, markeredgecolor='k', alpha=0.8)
			ax2.plot(new_data[:, col_indices['TimestepsSoFar']], new_data[:, col_indices['MeanEpLen']]
					 , label=d, color=color, linewidth=0.25, alpha=1)
			ax2.legend(loc='best')
			'''

	# leg = ax1.legend(loc='best')
	# for line in leg.get_lines():
	# 	line.set_linewidth(10)
	# plt.show()
	name_prefix = ''
	if plot_cost:
		name_prefix = 'CostPlot'
	elif constant_threshold:
		name_prefix = 'ConstantThreshold'
	else:
		name_prefix = 'RewardPlot-TC'
	fig_leg = plt.figure()  # figsize=(1, 3))
	# fig_leg.legend(lines, labels=['one', 'two'], loc='center', ncol=2)
	leg = fig_leg.legend(lines, labels, loc='center', ncol=len(lines))
	for line in leg.get_lines():
		line.set_linewidth(10)
	fig_leg.savefig(os.path.join(base_dir, name_prefix + '-' + 'Legend.png'), bbox_inches='tight', pad_inches=0, dpi=200)
	fig.savefig(os.path.join(base_dir, name_prefix + '-' + character + '.png'), bbox_inches='tight', pad_inches=0, dpi=200)


import cv2
import torch
import numpy as np
import os
import datetime
import time
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
import pygame

model = torch.hub.load(r"C:\Users\Bob\Downloads\yolov5\content\yolov5",
                       'custom', path=r"C:\Users\Bob\Downloads\yolov5\content\yolov5\runs\train\results_3\weights\best.pt",
                       source='local')

model.conf = 0.5
model.classes = 0

fire_frames_path = 'fire_frames/'

if not os.path.exists(fire_frames_path):
    os.makedirs(fire_frames_path)

class VideoCamera(object):
    def __init__(self, video_source):
        self.frame_counter = 0
        self.last_save_time = 0
        self.video = cv2.VideoCapture(video_source)
        self.play_sound = False

    def detect_fire(self, image):
        results = model(image, size=240)
        a = np.squeeze(results.render())
        if len(results.xyxy[0]):
            labels = results.pandas().xyxy[0]['name']
            if 'fire' in labels.values:
                print('Fire detected!')
                current_time = time.time()
                if current_time - self.last_save_time > 1:
                    self.last_save_time = current_time
                    a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
                    self.frame_counter += 1
                    timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
                    date_folder = datetime.datetime.now().strftime('%Y_%m_%d')
                    date_folder_path = os.path.join(fire_frames_path, date_folder)
                    if not os.path.exists(date_folder_path):
                        os.makedirs(date_folder_path)
                    frame_filename = os.path.join(date_folder_path, f'fire_frame_{timestamp}.jpg')
                    cv2.imencode('.jpg', a)[1].tofile(frame_filename)

                    if not self.play_sound:
                        self.play_sound = True
                        pygame.mixer.init()
                        pygame.mixer.music.load('alarm.mp3')
                        pygame.mixer.music.play()

        else:
            if self.play_sound:
                pygame.mixer.music.stop()
                self.play_sound = False
            print('No fire detected')

    def get_frame(self):
        success, image = self.video.read()
        if not success:
            return None
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        self.detect_fire(image)
        return image

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, video_source):
        super(MainWindow, self).__init__()
        self.video_camera = VideoCamera(video_source)
        self.central_widget = QtWidgets.QWidget(self)
        self.setCentralWidget(self.central_widget)
        self.video_label = QtWidgets.QLabel(self.central_widget)
        self.layout = QtWidgets.QVBoxLayout(self.central_widget)
        self.layout.addWidget(self.video_label)
        self.update_video()
        self.resize(800,800)

    def update_video(self):
        frame = self.video_camera.get_frame()
        if frame is not None:
            frame = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)
            frame = QtGui.QPixmap.fromImage(frame)
            self.video_label.setPixmap(frame.scaled(self.video_label.size(), QtCore.Qt.KeepAspectRatio))
        QtCore.QTimer.singleShot(1, self.update_video)

if __name__ == '__main__':
    video_source = 'fire.mp4'
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow(video_source)
    main_window.show()
    sys.exit(app.exec_())


import appdaemon.plugins.hass.hassapi as hass
import datetime

class AirPurifierController(hass.Hass):
  def initialize(self):
    self.log("Started...")
    self.fan_id = self.args['fan_id']
    self.listen_state(self.on_adaptation_callback, entity = self.args['family_devices'])
    self.listen_state(self.on_adaptation_callback, entity = self.args['balcone_door'])
    self.listen_state(self.on_adaptation_callback, entity = self.args['calendar'])
    self.log("Watching entity: {}".format(self.args['alt_mode_entity']))
    self.listen_state(self.on_adaptation_callback, entity = self.args['alt_mode_entity'], new = "on")
    self.listen_state(self.on_adaptation_callback, entity = self.args['alt_mode_entity'], new = "off", old="on", immediate=True, duration=self.args['fallback'])
    self.adapt_air_purifier_mode()

  def alt_mode_entity_working(self):
    return self.get_state(self.args['alt_mode_entity']) == 'on'

  def balcone_door_opened(self):
    return self.get_state(self.args['balcone_door']) == 'on'

  def anyone_in_home(self):
    state = self.get_state(self.args['family_devices'])
    self.log("State is {} for {}".format(state, self.args['family_devices']))
    return state == 'home'

  def cleaning_time(self):
    if self.get_state(self.args['calendar']) == 'on':
      self.log("Calendar: {} is On".format(self.args['calendar']))
      return True
    else:
      self.log("Calendar: {} is Off".format(self.args['calendar']))
      return False

  def update_speed(self, speed):
    self.log("Setting speed to: {}".format(speed))
    self.call_service('fan/xiaomi_miio_set_favorite_level', entity_id=self.fan_id, level=speed)
    self.call_service('fan/set_speed', entity_id=self.fan_id, speed='Auto')

  def turn_off(self):
    self.log("Turning off air purifier")
    self.call_service('fan/turn_off', entity_id=self.fan_id)

  def switch_to_mode(self):
    if self.alt_mode_entity_working():
      self.log("Switching to {} mode because {} is working".format(self.args['alt_mode'], self.args['alt_mode_entity']))
      self.call_service('fan/set_speed', entity_id=self.fan_id, speed=self.args['alt_mode'])
    else:
      self.log("Switching to {} mode".format(self.args['mode']))
      self.call_service('fan/set_speed', entity_id=self.fan_id, speed=self.args['mode'])
  def turn_on(self):
    self.log("Turning on air purifier")
    self.call_service('fan/turn_on', entity_id=self.fan_id)

  def on_adaptation_callback(self, entity, attribute, old, new, kwargs):
    self.log("State callback triggered for {} from {} to {}. Adapting air quality".format(entity, old, new))
    self.adapt_air_purifier_mode()

  def adapt_air_purifier_mode(self):
    if self.anyone_in_home():
      self.log("People home")
      if self.balcone_door_opened():
        self.log("Balcone door is opened")
        self.turn_off()
      elif self.cleaning_time():
        self.turn_on()
        self.log("Adapting speed")
        self.switch_to_mode()
      else:
        self.log("Turning off...")
        self.turn_off()
    else:
      self.log("Nobody home")
      self.log("Turn off completle")
      self.turn_off()


import sqlite3
from sqlite3 import Error
import sys
import requests
from joblib import Parallel, delayed
from urllib.parse import urljoin
import pandas as pd

# baseUrl = 'http://localhost:4000/api/messages/'
# partitionUrl = 'http://localhost:4000/api/messages/partition/'

baseUrl = 'http://172.31.2.38/api/messages/'
partitionUrl = 'http://172.31.2.38:4000/api/messages/partition/'

def create_connection(db_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
    except Error as e:
        print(e)

    return conn

def save_to_csv(df, filename):
    indexer, datas = df['createActionId'], df.drop(['createActionId'], axis=1)
    indexer.to_csv('indexer.' + filename, index=False)
    datas.to_csv(filename, index=False)
    return

def check_result(df):
    # print('checking',)
    df2, _ = fetch_whole()
    df2 = df2.set_index('createActionId')
    # print('df', df.shape)
    # print('df2', df2.shape)
    # print('Compare result:', ((df == df2).all().sum() == 4))

def online_and_update(filename, latestActionId):
    dff = pd.read_csv(filename)
    indexer = pd.read_csv('indexer.' + filename)
    # print('indexer', indexer)
    dff = dff.set_index(indexer['createActionId'])
    # print('dff', dff)
    # latest = str(dff.index[-1])
    response = requests.get(urljoin(baseUrl, str(latestActionId))).json()
    # print('whe', response['message'])
    message = pd.DataFrame(response['message'])
    if 'createActionId' in message.columns:
        message = message.set_index('createActionId')
    dff = pd.concat([dff, message])
    batch = response['batch']
    for batch_type in batch:
        updated_value = pd.DataFrame(batch[batch_type])
        # print('batch',batch_type, len(updated_value))
        if 'messageCreateActionId' in updated_value.columns:
            updated_value = updated_value.set_index('messageCreateActionId')
            index = updated_value.index
            dff.loc[index, batch_type] = updated_value.loc[:, 'updatedValue']
    deletedCreateActionIds = response['deletedCreateActionIds']
    dff = dff.drop(deletedCreateActionIds, errors='ignore')

    check_result(dff)

    save_to_csv(dff.reset_index(), filename)
    return response['newLatestActionId']

def send_partition_req(partition):
    url = urljoin(partitionUrl,str(partition['skip']) + '/' +  str(partition['take']))
    return requests.get(url).json()

def fetch_whole():
    response = requests.get(baseUrl).json()
    if 'partitionSets' in response:
        partition_sets = response['partitionSets']
        # print('ps', partition_sets)
        allres = []
        for ps in partition_sets:
            # print('ps', ps)
            results = Parallel(n_jobs=len(ps))(delayed(send_partition_req)(ps[i]) for i in range(len(ps)))
            for item in results:
                allres += item
            # print('done')
        # arr = np.array(results)
        # arr = arr.reshape(arr.shape[0]*arr.shape[1])
        dff = pd.DataFrame(allres)
        # print(dff.head())
    else:
        dff = pd.DataFrame(response['message'])
    return dff, response['newLatestActionId']

def fetch_and_get_latest_id(filename):
    dff, newLatestActionId = fetch_whole()
    save_to_csv(dff, filename)
    return newLatestActionId

def fetch_appdata_by_filename(conn, filename):
    cur = conn.cursor()
    cur.execute(f"SELECT * FROM appdata WHERE filename='{filename}' LIMIT 1")

    datalist = cur.fetchall()
    if len(datalist) == 1:
        latestActionId = datalist[0][1]
        # print('old', latestActionId)
        newLatest = online_and_update(filename, latestActionId)
        # print('new', newLatest)
        cur.execute(f"UPDATE appdata SET latestAction={newLatest} WHERE filename='{filename}'")

    else:
        newLatest = fetch_and_get_latest_id(filename)
        # print('new', newLatest)
        cur.execute(f"INSERT INTO appdata (filename, latestAction) VALUES ('{filename}', '{newLatest}')")

def main():
    database = r"appdata.db"
    if len(sys.argv) < 2:
        # print('usage: python3 online.py filename.csv')
        quit()
    filename = sys.argv[1]

    # create a database connection
    conn = create_connection(database)
    with conn:
        fetch_appdata_by_filename(conn, filename)

if __name__ == '__main__':
    main()


#!/usr/bin/python3

from db_accessor.db_accessor import db2
import logging
#import random
import time
import sys
from espnapi import get_espn_score_by_qtr

logging.basicConfig(filename="score_checker.log", format="%(asctime)s %(levelname)-8s %(message)s", level=logging.DEBUG, datefmt="%Y-%m-%d %H:%M:%S")
starttime = time.mktime((2021, 12, 17, 18, 59, 0, 0, 0, 0))

print(sys.argv)
if len(sys.argv) < 1:
    print("Usage:  ./score_checker.py <boxid> <espnid>")

else:
    boxid = sys.argv[1]
    if len(sys.argv) < 2: # missing espnid, go look it up
        s = "SELECT espn_id FROM boxes WHERE boxid = %s;"
        espnid = db2(s, (boxid,))
        print(f"espnid from db: {espnid}")
    else:
        espnid = sys.argv[2]

    while True:

        scores = get_espn_score_by_qtr(espnid)

        print(scores)

        # {'LSU': {'schoolName': 'LSU', 'nickname': 'Tigers', 'logo':
        # 'https://a.espncdn.com/i/teamlogos/ncaa/500/99.png', 'current_score': '0',
        # 'qtr_scores': {1: 0, 2: 0, 3: 0, 4: 0}},
        # 'KSU': {'schoolName': 'Kansas State', 'nickname': 'Wildcats', 'logo':
        # 'https://a.espncdn.com/i/teamlogos/ncaa/500/2306.png', 'current_score': '0',
        # 'qtr_scores': {1: 0, 2: 0, 3: 0, 4: 0}}}

        qtrs = []
        print(f"scores {scores}")
        # for team in scores:
        #     q = len(scores[team]['qtr_scores'])
        #     print(f"q:  --  {q}")
        #     if q > 0:
        #         for qtr in scores[team]['qtr_scores']:
        #             print(f"qtr -- {qtr}")
        #             if qtr <= q or qtr == 4:
        #                 qtrs.append(scores[team]['qtr_scores'][qtr])

        # ALA then UGA
        q = len(scores['ALA']['qtr_scores'])
        print(f"q:  --  {q}")
        if q > 0:
            for qtr in scores['ALA']['qtr_scores']:
                print(f"qtr -- {qtr}")
                if qtr <= q or qtr == 4:
                    qtrs.append(scores['ALA']['qtr_scores'][qtr])
            for qtr in scores['UGA']['qtr_scores']:
                print(f"qtr -- {qtr}")
                if qtr <= q or qtr == 4:
                    qtrs.append(scores['UGA']['qtr_scores'][qtr])

        print(f"qtrs:  --- {qtrs}")
        quarter = len(qtrs) // 2
        cols = ''
        vals = ''
        total_x = 0
        total_y = 0
        if qtrs:
            yq = 1
            for y_qtr in qtrs[:quarter]:
                print(f"yqtr {y_qtr}")
                cols += 'y' + str(yq) + ', '
                vals += str(total_y + int(y_qtr)) + ', '
                total_y += int(y_qtr)
                yq += 1
            xq = 1
            for x_qtr in qtrs[quarter:]:
                print(f"xqtr {x_qtr}")
                cols += 'x' + str(xq) + ', '
                vals += str(total_x + int(x_qtr)) + ', '
                total_x += int(x_qtr)
                xq += 1
        if len(cols) > 1:
            cols = cols[:-2]
            vals = vals[:-2]

            q = f"INSERT INTO scores (boxid, {cols}) VALUES ({boxid}, {vals});"
            db2(q)
            print(q)
            #break

        time.sleep(10.0 - ((time.time() - starttime) % 10.0))


import torch
import torch.nn as nn
import numpy as np
from torch.optim import Adam, lr_scheduler
import pytorch_lightning as pl
from modules.wnet_generator import WNetGenerator
from modules.nlayer_discriminator import NLayerDiscriminator
from modules.losses import GANLoss
from utils import rgb2image, calculate_psnr_torch, init_weights
from color_map import get_color_map
from torch.distributions.geometric import Geometric
import os

class AskModel(pl.LightningModule):
    def __init__(self, args):
        super().__init__()
        self.args = args

        hint_channels = args.max_hints if args.feed_segments else 0
        self.generator = WNetGenerator(
            args.input_channels,
            args.max_hints,
            args.input_channels + args.output_channels + hint_channels + 1,
            args.output_channels,
            args.use_bilinear
        )
        self.discriminator = NLayerDiscriminator(args.input_channels + args.output_channels)

        if args.init_type is not None:
            init_weights(self.generator, args.init_type)
            init_weights(self.discriminator, args.init_type)

        # losses
        self.l1_loss = nn.L1Loss()
        self.mse_loss = nn.MSELoss()
        self.gan_loss = GANLoss('lsgan')

    def forward(self, base, real, tau=1.0, deterministic=False):
        logits, seg = self.segment(base, tau, deterministic)

        fcolor, fmask = self._get_full_hint(seg, real)
        act = self._get_random_act((base.shape[0], self.args.max_hints), self.args.stop_prob)

        color, mask = self._apply_act(fcolor, fmask, act)
        pred = self.color(base, color, mask, seg)

        return logits, fcolor, fmask, color, mask, pred

    def segment(self, base, tau=1.0, deterministic=False):
        logits, seg = self.generator.segment(base, tau, deterministic)
        return logits, seg

    def color(self, base, color, mask, seg):
        if self.args.feed_segments:
            pred = self.generator(torch.cat((base, color.sum(dim=1), mask.sum(dim=1) - self.args.ask_mask_cent, seg - self.args.ask_mask_cent), dim=1))
        else:
            pred = self.generator(torch.cat((base, color.sum(dim=1), mask.sum(dim=1) - self.args.ask_mask_cent), dim=1))
        return pred

    def _apply_act(self, fcolor, fmask, act):
        act = act.view(*act.shape, 1, 1, 1)
        color = fcolor * act
        mask = fmask * act
        return color, mask

    def _get_random_act(self, shape, stop_prob):
        act = torch.zeros(shape, device=self.device)
        live = torch.bernoulli(torch.full(shape, 1.0-stop_prob, device=self.device))
        for i in range(shape[1]):
            if i == 0:
                act[:, 0] = live[:, 0]
            else:
                act[:, i] = act[:, i-1] * live[:, i]
        return act

    def _get_full_hint(self, seg, real, eps=1e-20):
        """
        seg: batch_size x max_hints x height x width
        real: batch_size x color_channels x height x width
        act: batch_size x max_hints
        fcolor: batch_size x max_hints x color_channels x height x width
        fmask: batch_size x max_hints x 1 x height x width
        """
        fmask = seg.unsqueeze(2)
        hm = real.unsqueeze(1) * fmask
        avg_hm = hm.sum(dim=3, keepdim=True).sum(dim=4, keepdim=True) / (eps + fmask.sum(dim=3, keepdim=True).sum(dim=4, keepdim=True))

        fcolor = avg_hm * fmask
        return fcolor, fmask

    def _get_total_variance_loss(self, fcolor, real):
        fcolor = fcolor.sum(dim=1)
        loss = self.mse_loss(fcolor, real)
        return loss

    def _get_smoothness_loss(self, logits):
        if self.args.smoothness_method == 'four_l1':
            loss = 0.5 * (
                self.l1_loss(logits[:,:,:-1,:], logits[:,:,1:,:])
                + self.l1_loss(logits[:,:,:,:-1], logits[:,:,:,1:])
            )
        elif self.args.smoothness_method == 'eight_l1':
            loss = 0.25 * (
                self.l1_loss(logits[:,:,:-1,:], logits[:,:,1:,:])
                + self.l1_loss(logits[:,:,:,:-1], logits[:,:,:,1:])
                + self.l1_loss(logits[:,:,:-1,:-1], logits[:,:,1:,1:])
                + self.l1_loss(logits[:,:,1:,:-1], logits[:,:,:-1,1:])
            )
        elif self.args.smoothness_method == 'eight_l2':
            loss = 0.25 * (
                self.mse_loss(logits[:,:,:-1,:], logits[:,:,1:,:])
                + self.mse_loss(logits[:,:,:,:-1], logits[:,:,:,1:])
                + self.mse_loss(logits[:,:,:-1,:-1], logits[:,:,1:,1:])
                + self.mse_loss(logits[:,:,1:,:-1], logits[:,:,:-1,1:])
            )
        else:
            raise NotImplementedError()
        return loss

    # required
    def training_step(self, batch, batch_idx, optimizer_idx):
        base, real = get_base_real(self.args, batch)

        if self.args.tau_policy == 'decay':
            tau = self.args.min_tau ** (self.current_epoch / self.args.epochs)
        else:
            tau = self.args.min_tau

        if optimizer_idx == 0:
            # training generator
            logits, fcolor, _, _, _, pred = self(base, real, tau=tau)

            adv_g_loss = self.args.lambda_adv * self.gan_loss(self.discriminator(torch.cat((base, pred), dim=1)), True)
            rec_loss = self.args.lambda_rec * self.l1_loss(pred, real)
            tvr_loss = self.args.lambda_tvr * self._get_total_variance_loss(fcolor, real)
            smt_loss = self.args.lambda_smt * self._get_smoothness_loss(logits)
            loss = adv_g_loss + rec_loss + tvr_loss + smt_loss

            return {
                'loss': loss,
                'adv_g_loss': adv_g_loss,
                'rec_loss': rec_loss,
                'tvr_loss': tvr_loss,
                'smt_loss': smt_loss,
                'psnr': calculate_psnr(self.args, base, pred, real).mean()
            }
        else:
            # training discriminator
            _, _, _, _, _, pred = self(base, real, tau=tau)
            fake_loss = self.gan_loss(self.discriminator(torch.cat((base, pred), dim=1).detach()), False)
            real_loss = self.gan_loss(self.discriminator(torch.cat((base, real), dim=1)), True)
            loss = self.args.lambda_adv * 0.5 * (fake_loss + real_loss)
            return {
                'loss': loss,
                'adv_d_loss': loss
            }

    # required
    def training_epoch_end(self, outputs):
        adv_loss, rec_loss, tvr_loss, smt_loss, psnr = 0.0, 0.0, 0.0, 0.0, 0.0
        n = len(outputs[0])
        for output0, output1 in zip(outputs[0], outputs[1]):
            adv_loss += (output0['adv_g_loss'] + output1['adv_d_loss']) / n
            rec_loss += output0['rec_loss'] / n
            tvr_loss += output0['tvr_loss'] / n
            smt_loss += output0['smt_loss'] / n
            psnr += output0['psnr'] / n
        tot_loss = adv_loss + rec_loss + tvr_loss + smt_loss

        self.logger.experiment.add_scalars('train_losses', {
            'adv_loss': adv_loss,
            'rec_loss': rec_loss,
            'tvr_loss': tvr_loss,
            'smt_loss': smt_loss,
            'tot_loss': tot_loss
        }, self.current_epoch)
        self.logger.experiment.add_scalars('psnr', {'train': psnr}, self.current_epoch)

    # required
    def validation_step(self, batch, batch_idx):
        base, real = get_base_real(self.args, batch)
        _, _, _, color, mask, pred = self(base, real, tau=self.args.min_tau)

        if batch_idx == 0:
            lst = []
            for i in range(real.shape[0]):
                lst.append(get_preview_image(self.args, base[i:i+1], color[i:i+1], mask[i:i+1], pred[i:i+1], real[i:i+1]))

            for i, tag in enumerate(['base', 'color', 'mask', 'pred', 'real']):
                arr = np.stack([np.array(p[i]) for p in lst])/255.0
                self.logger.experiment.add_images(tag, arr, self.current_epoch, dataformats='NHWC')

        return {
            'loss': calculate_psnr(self.args, base, pred, real).mean()
        }

    # required
    def validation_epoch_end(self, outputs):
        psnr = torch.stack([output['loss'] for output in outputs]).mean()
        self.logger.experiment.add_scalars('psnr', {'val': psnr}, self.current_epoch)
        return {
            'loss': psnr,
            'checkpoint_on': psnr
        }

    # required
    def configure_optimizers(self):
        # optimizers
        optimizer_g = Adam(self.generator.parameters(), lr=self.args.lr)
        optimizer_d = Adam(self.discriminator.parameters(), lr=self.args.lr)
        optimizers = [
            optimizer_g,
            optimizer_d
        ]

        # schedulers
        def lr_lambda(epoch):
            return 1.0 - max(0, epoch - self.args.epochs * (1 - self.args.decay_ratio)) / (self.args.decay_ratio * self.args.epochs + 1)
        schedulers = [
            lr_scheduler.LambdaLR(optimizer_g, lr_lambda=lr_lambda),
            lr_scheduler.LambdaLR(optimizer_d, lr_lambda=lr_lambda)
        ]

        return optimizers, schedulers

def get_base_real(args, batch):
    """
    get base, real in [-1.0, 1.0]
    """
    base, real = batch[0] * 2.0 - 1.0, batch[1] * 2.0 - 1.0
    return base, real

def calculate_psnr(args, base, pred, real):
    img1, img2 = (pred + 1.0) * 0.5, (real + 1.0) * 0.5
    return calculate_psnr_torch(img1, img2)

def get_preview_image(args, base, color, mask, pred, real):
    base = (base + 1.0) * 0.5
    color = ((color + 1.0) * 0.5) * mask
    pred = (pred + 1.0) * 0.5
    real = (real + 1.0) * 0.5

    # making color, mask of easy-to-print form
    color2 = color.sum(dim=1)
    mask2 = mask.sum(dim=1)

    color_map = get_color_map(mask.shape[1], mask.device)
    mask3 = mask * color_map.view(1, mask.shape[1], 3, 1, 1)
    mask3 += 1 - mask.sum(dim=1, keepdim=True)
    mask3 = mask3.sum(dim=1)

    base = base.repeat(1, 3, 1, 1)
    return rgb2image(base),\
            rgb2image(base*(1 - mask2) + color2*mask2),\
            rgb2image(mask3),\
            rgb2image(pred),\
            rgb2image(real)


# -*- coding:utf-8 -*-
"""
Author: LJC
Email: jiacheng_li@std.uestc.edu.cn
name: detect_dual.py
Data: 2021.07.26
Note: Improved by Yolov5_detect
"""

import argparse
import copy
import time
from pathlib import Path

import cv2
import torch

import os

from numpy import random

from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
    scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path
from utils.plots import plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized
import dec_fusion_multispectral_final as Fu
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
import shutil

def del_file(path):
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)
        if os.path.isdir(c_path):
            del_file(c_path)
        else:
            os.remove(c_path)

def detect(save_img=False):
    save_dir_txt, source_vis, source_ir, weights_vis, weights_ir, view_img, save_txt, imgsz= opt.txt_dir,opt.source_vis,opt.source_ir, opt.weights_vis,opt.weights_ir, opt.view_img, opt.save_txt, opt.img_size
    save_img = not opt.nosave and not source_vis.endswith('.txt')  # save inference images

    # Directories
    save_dir_fusion_txt = os.path.join(save_dir_txt, 'detection_fusion_ours_txt')
    opt.project = os.path.join(opt.project, 'detect_fusion_ours')

    save_dir = Path(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))  # increment run
    (save_dir if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir

    # save_dir_ir_txt = os.path.join(os.path.abspath(os.path.join(save_dir_fusion_txt,"..")),'detection_ir_txt')
    # save_dir_vis_txt = os.path.join(os.path.abspath(os.path.join(save_dir_fusion_txt,"..")),'detection_vis_txt')
    save_dir_fusion_txt = Path(save_dir_fusion_txt)
    # save_dir_ir_txt = Path(save_dir_ir_txt)
    # save_dir_vis_txt = Path(save_dir_vis_txt)

    (save_dir_fusion_txt if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir
    del_file(save_dir_fusion_txt)
    # (save_dir_ir_txt if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir
    # del_file(save_dir_ir_txt)
    # (save_dir_vis_txt if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir
    # del_file(save_dir_vis_txt)

    # Initialize
    set_logging()
    device = select_device(opt.device)
    half = device.type != 'cpu'  # half precision only supported on CUDA

    # Load model
    model = attempt_load(weights_vis, map_location=device)  # load FP32 model        model_vis
    stride = int(model.stride.max())  # model stride
    imgsz = check_img_size(imgsz, s=stride)  # check img_size
    if half:
        model.half()  # to FP16

    model_ir= attempt_load(weights_ir, map_location=device)  # load FP32 model        model_ir
    stride_ir = int(model_ir.stride.max())  # model stride
    imgsz_ir = check_img_size(imgsz, s=stride_ir)  # check img_size
    if half:
        model_ir.half()  # to FP16

    # Set Dataloader
    vid_path, vid_writer = None, None

    dataset = LoadImages(source_vis, img_size=imgsz, stride=stride)
    dataset_ir = LoadImages(source_ir, img_size=imgsz_ir, stride=stride_ir)

    # Get names and colors
    names = model_ir.module.names if hasattr(model_ir, 'module') else model_ir.names
    names.append('sub_defect')

    # names.append('sub_defect')
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]

    # Run inference
    if device.type != 'cpu':
        model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters())))  # run once

    if device.type != 'cpu':
        model_ir(torch.zeros(1, 3, imgsz_ir, imgsz_ir).to(device).type_as(next(model_ir.parameters())))  # run once
    t0 = time.time()
    fusion_time = []

    for (path, img, im0s, vid_cap) , (path_ir, img_ir, im0s_ir, vid_cap_ir) in zip(dataset, dataset_ir):

        img_name = path.split('/')[-1].split('.')[0]
        img = torch.from_numpy(img).to(device)

        img = img.half() if half else img.float()  # uint8 to fp16/32
        img /= 255.0  # 0 - 255 to 0.0 - 1.0
        if img.ndimension() == 3:
            img = img.unsqueeze(0)

        img_ir = torch.from_numpy(img_ir).to(device)
        img_ir = img_ir.half() if half else img_ir.float()  # uint8 to fp16/32
        img_ir /= 255.0  # 0 - 255 to 0.0 - 1.0
        if img_ir.ndimension() == 3:
            img_ir = img_ir.unsqueeze(0)

        img_fuse = copy.deepcopy(img_ir)
        im0s_fuse = copy.deepcopy(im0s_ir)

        # Inference

        pred = model(img, augment=opt.augment)[0]

        # Apply NMS
        pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)

        for i in range(len(pred[0])):

            if pred[0][i][0] < 0:
                pred[0][i][0] = 0
            if pred[0][i][1] < 0:
                pred[0][i][1] = 0
            if pred[0][i][2] > img.shape[3]:
                pred[0][i][2] = img.shape[3]
            if pred[0][i][3] > img.shape[2]:
                pred[0][i][3] = img.shape[2]

        pred_ir = model_ir(img_ir, augment=opt.augment)[0]

        # Apply NMS
        pred_ir = non_max_suppression(pred_ir, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)

        for i in range(len(pred_ir[0])):

            if pred_ir[0][i][0] < 0:
                pred_ir[0][i][0] = 0
            if pred_ir[0][i][1] < 0:
                pred_ir[0][i][1] = 0
            if pred_ir[0][i][2] > img_ir.shape[3]:
                pred_ir[0][i][2] = img_ir.shape[3]
            if pred_ir[0][i][3] > img_ir.shape[2]:
                pred_ir[0][i][3] = img_ir.shape[2]
        t1 = time_synchronized()

        pred_fuse = Fu.Dec_Fusion_Multispectral_Withmatch(opt, img, img_ir, pred, pred_ir, opt.fuseiou_thres, opt.contain_thres, img_name)

        t2 = time_synchronized()

        image_name = img_name
        fusion_time_str = image_name + ' {:.4f}'.format(t2 - t1)
        fusion_time.append(fusion_time_str)

        # Process detections
        for (i, det) ,(i_ir, det_ir) ,(i_fuse, det_fuse) in zip(enumerate(pred), enumerate(pred_ir), enumerate(pred_fuse)):  # detections per image

            p, s, im0, frame = path, '', im0s, getattr(dataset, 'frame', 0)

            p = Path(p)  # to Path
            p_vis_temp = p.name.split('.')
            name1 = p_vis_temp[0]+'_vis.'+p_vis_temp[1]
            save_path = str(save_dir / name1)  # img.jpg
            s += '%gx%g ' % img.shape[2:]  # print string
            gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh

            p_ir, s_ir, im0_ir, frame_ir = path_ir, '', im0s_ir, getattr(dataset_ir, 'frame_ir', 0)

            p_ir = Path(p_ir)  # to Path
            p_ir_temp = p_ir.name.split('.')
            name2 = p_ir_temp[0]+'_ir.'+p_ir_temp[1]
            save_path_ir = str(save_dir / name2)  # img.jpg
            s_ir += '%gx%g ' % img_ir.shape[2:]  # print string
            gn_ir = torch.tensor(im0_ir.shape)[[1, 0, 1, 0]]  # normalization gain whwh

            p_fuse, s_fuse, im0_fuse, frame_fuse = path, '', im0s_fuse, getattr(dataset, 'frame_fuse', 0)

            p_fuse = Path(p_fuse)  # to Path

            temp3 = p_fuse.name.split('.')
            name3 = temp3[0]+'_fuse.'+temp3[1]

            save_path_fuse = str(save_dir / name3)  # img.jpg
            # txt_path_ir = str(save_dir_ir_txt / p_ir.stem) + (
            #     '' if dataset.mode == 'image' else f'_{frame_ir}')  # img.txt
            # txt_path_vis = str(save_dir_vis_txt / p.stem) + (
            #     '' if dataset.mode == 'image' else f'_{frame}')  # img.txt
            txt_path_fuse = str(save_dir_fusion_txt / p_fuse.stem) + ('' if dataset.mode == 'image' else f'_{frame_fuse}')  # img.txt
            s_fuse += '%gx%g ' % img_fuse.shape[2:]  # print string
            gn_fuse = torch.tensor(im0_fuse.shape)[[1, 0, 1, 0]]  # normalization gain whwh

            if len(det):
                # Rescale boxes from img_size to im0 size
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()

                # Print results
                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    s += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string

                # Write results
                for *xyxy, conf, cls in reversed(det):
                    # if save_txt:  # Write to file
                    #     # xywh_fuse = (xyxy2xywh(torch.tensor(xyxy_fuse).view(1, 4)) / gn_fuse).view(-1).tolist()  # normalized xywh
                    #     line = (names[0], conf, *xyxy) if opt.save_conf else (cls, *xyxy)  # label format
                    #     with open(txt_path_vis + '.txt', 'a') as f:
                    #         f.write(('%s ' + '%g ' * (len(line)-1)).rstrip() % line + '\n')
                    if save_img or view_img:  # Add bbox to image
                        im0 = im0.copy()
                        label = f'{names[int(cls)]} {conf:.2f}'
                        plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=1)
            # elif save_txt:
            #     f = open(txt_path_vis + '.txt', 'a')
            #     f.close()

            if len(det_ir):
                # Rescale boxes from img_size to im0 size
                det_ir[:, :4] = scale_coords(img_ir.shape[2:], det_ir[:, :4], im0_ir.shape).round()

                # Print results
                for c_ir in det_ir[:, -1].unique():
                    n_ir = (det_ir[:, -1] == c_ir).sum()  # detections per class
                    s_ir += f"{n_ir} {names[int(c_ir)]}{'s_ir' * (n_ir > 1)}, "  # add to string

                # Write results
                for *xyxy_ir, conf_ir, cls_ir in reversed(det_ir):
                    # if save_txt:  # Write to file
                    #     # xywh_fuse = (xyxy2xywh(torch.tensor(xyxy_fuse).view(1, 4)) / gn_fuse).view(-1).tolist()  # normalized xywh
                    #     line_ir = (names[1], conf_ir, *xyxy_ir) if opt.save_conf else (cls_ir, *xyxy_ir)  # label format
                    #     with open(txt_path_ir + '.txt', 'a') as f_ir:
                    #         f_ir.write(('%s ' + '%g ' * (len(line_ir)-1)).rstrip() % line_ir + '\n')
                    if save_img or view_img:  # Add bbox to image
                        im0_ir = im0_ir.copy()
                        label_ir = f'{names[int(cls_ir)]} {conf_ir:.2f}'
                        plot_one_box(xyxy_ir, im0_ir, label=label_ir, color=colors[int(cls_ir)], line_thickness=1)
            # elif save_txt:
            #     f_ir = open(txt_path_ir + '.txt', 'a')
            #     f_ir.close()

            if len(det_fuse):
                # Rescale boxes from img_size to im0 size
                det_fuse[:, :4] = scale_coords(img_fuse.shape[2:], det_fuse[:, :4], im0_fuse.shape).round()

                # Print results
                for c_fuse in det_fuse[:, -1].unique():
                    n_fuse = (det_fuse[:, -1] == c_fuse).sum()  # detections per class
                    s_fuse += f"{n_fuse} {names[int(c_fuse)]}{'s_fuse' * (n_fuse > 1)}, "  # add to string
                # Write results
                for *xyxy_fuse, conf_fuse, cls_fuse in reversed(det_fuse):
                    if save_txt:  # Write to file
                        # xywh_fuse = (xyxy2xywh(torch.tensor(xyxy_fuse).view(1, 4)) / gn_fuse).view(-1).tolist()  # normalized xywh
                        line_fuse = (names[int(cls_fuse)], conf_fuse, *xyxy_fuse) if opt.save_conf else (cls_fuse, *xyxy_fuse)  # label format
                        with open(txt_path_fuse + '.txt', 'a') as f_fuse:
                            f_fuse.write(('%s ' + '%g ' * (len(line_fuse)-1)).rstrip() % line_fuse + '\n')
                    if save_img or view_img:  # Add bbox to image
                        im0_fuse = im0_fuse.copy()
                        label_fuse = f'{names[int(cls_fuse)]} {conf_fuse:.2f}'
                        plot_one_box(xyxy_fuse, im0_fuse, label=label_fuse, color=colors[int(cls_fuse)], line_thickness=1)
            elif save_txt:
                f_fuse = open(txt_path_fuse + '.txt', 'a')
                f_fuse.close()

            # Print time (inference + NMS)
            print(f'{s}Done. ({t2 - t1:.3f}s)')

            # Stream results
            if view_img:
                cv2.imshow(str(p), im0)
                cv2.imshow(str(p_ir), im0_ir)
                cv2.imshow(str(p_fuse), im0_fuse)

            # Save results (image with detections)
            if save_img:
                if dataset.mode == 'image':
                    cv2.imwrite(save_path, im0)
                    cv2.imwrite(save_path_ir, im0_ir)
                    cv2.imwrite(save_path_fuse, im0_fuse)

    print(f'Done. ({time.time() - t0:.3f}s)')

def intersect(box_a, box_b):

    A = box_a.size(0)
    B = box_b.size(0)
    max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
                       box_b[:, 2:].unsqueeze(0).expand(A, B, 2))
    min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),
                       box_b[:, :2].unsqueeze(0).expand(A, B, 2))
    inter = torch.clamp((max_xy - min_xy), min=0)
    return inter[:, :, 0] * inter[:, :, 1]

def jaccard(box_a, box_b):

    inter = intersect(box_a, box_b)
    area_a = ((box_a[:, 2]-box_a[:, 0]) *
              (box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter)  # [A,B]
    area_b = ((box_b[:, 2]-box_b[:, 0]) *
              (box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter)  # [A,B]
    union = area_a + area_b - inter
    return inter / union  # [A,B]

def contain(box_a, box_b):

    inter = intersect(box_a, box_b)
    area_a = ((box_a[:, 2]-box_a[:, 0]) *
              (box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter)  # [A,B]

    return inter / area_a  # inter/A

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    # parser.add_argument('--weights-vis', nargs='+', type=str, default='vis-base.pt', help='model.pt path(s)')
    # parser.add_argument('--weights-ir', nargs='+', type=str, default='ir-base.pt', help='model.pt path(s)')

    parser.add_argument('--weights-vis', nargs='+', type=str, default='vis-07.pt', help='model.pt path(s)')
    parser.add_argument('--weights-ir', nargs='+', type=str, default='ir-07.pt', help='model.pt path(s)')

    parser.add_argument('--main-dir', type=str, default= '/home/ljc/vm/ljc_model/yolov5/YoloFusion_Demo/data/Demo_data_sample7',help='main_dir')  # file/folder, 0 for webcam   183

    parser.add_argument('--source-vis', type=str, default= 'VIS_sift_cut', help='source')  # file/folder, 0 for webcam   183
    parser.add_argument('--source-ir', type=str, default= 'IR_images_cut', help='source')  # file/folder, 0 for webcam

    parser.add_argument('--txt-dir', type=str, default= 'detection_txt',help='source')  # file/folder, 0 for webcam
    parser.add_argument('--project', default= 'detect', help='save results to project/name')

    parser.add_argument('--var', type=list, default=[], help='shift along the y-axis')
    parser.add_argument('--enlarge-box', type=bool, default=True, help='shift along the y-axis')

    parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
    parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold')   #0.4
    parser.add_argument('--iou-thres', type=float, default=0.6, help='IOU threshold for NMS')          #0.6
    parser.add_argument('--fuseiou-thres', type=float, default=0.6, help='IOU threshold for NMS')  #0.6
    parser.add_argument('--contain-thres', type=float, default=0.5, help='A contain intersect')     #0.3
    parser.add_argument('--device', default='0,1', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--view-img', action='store_true', help='display results')
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--update', action='store_true', help='update all models')

    parser.add_argument('--name', default='exp', help='save results to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')

    opt = parser.parse_args()

    opt.source_vis = os.path.join(opt.main_dir, opt.source_vis)
    opt.source_ir = os.path.join(opt.main_dir, opt.source_ir)

    opt.txt_dir = os.path.join(opt.main_dir, opt.txt_dir)
    opt.project = os.path.join(opt.main_dir, opt.project)

    print(opt)
    check_requirements(exclude=('pycocotools', 'thop'))

    with torch.no_grad():
        if opt.update:  # update all models (to fix SourceChangeWarning)
            for opt.weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']:
                detect()
                strip_optimizer(opt.weights)
        else:
            detect()


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

# In[1]:

import pandas as pd
import numpy as np

# In[2]:

def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict

# In[5]:

data1=unpickle('/home/gsmodi/Music/OneDrive_1_29-02-2020/Datasets/Question-1/cifar-10-batches-py/data_batch_1')

# In[6]:

data2=unpickle('/home/gsmodi/Music/OneDrive_1_29-02-2020/Datasets/Question-1/cifar-10-batches-py/data_batch_2')

# In[ ]:

data3=unpickle('/content/drive/My Drive/cifar-10-batches-py/data_batch_2')

# In[ ]:

data4=unpickle('/content/drive/My Drive/cifar-10-batches-py/data_batch_4')

# In[ ]:

data5=unpickle('/content/drive/My Drive/cifar-10-batches-py/data_batch_5')

# In[8]:

n1=list(data1.items())
n1=np.asarray(n1)

# In[9]:

labels=n1[1,1]
print(labels)
color_code=n1[2,1]
print(color_code)

# In[10]:

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(color_code,labels, test_size=0.2,random_state=42)

# In[11]:

from sklearn import svm
from sklearn.metrics import accuracy_score
def svm_linear(c):
  clf = svm.SVC(probability=False ,kernel='linear',C=c,gamma = 'auto')
  clf.fit(x_train,y_train)

  y_pred = clf.predict(x_test)
  print(accuracy_score(y_test, y_pred))

# In[12]:

c=[0.1]
for i in c:
  svm_linear(i)

# In[ ]:

#from matplotlib import pyplot as plt
#data=x_train[132].reshape(32,32,3)

#plt.imshow(data)

# In[ ]:

from sklearn.decomposition import PCA
pca = PCA(n_components=200)
x_train=pca.fit_transform(x_train)

# In[ ]:

from sklearn.decomposition import PCA
pca = PCA(n_components=200)
x_test=pca.fit_transform(x_test)

# In[ ]:

print(x_train.shape)
print(x_test.shape)

# In[ ]:

#c_svm_linear = [2,10,100]
c_svm_linear = [0.1]
for c in c_svm_linear:
  svm_linear(c)

# In[ ]:


class User:
    # pass  # Use pass to let Python know that we want to end the class/function with a blank body

    def __init__(self, user_id, username):  # Constructor
        self.user_id = user_id
        self.username = username
        self.followers = 0
        self.following = 0

    def follow(self, user):
        user.followers += 1
        self.following += 1

# user_1 = User()
# user_1.id = "001"
# user_1.username = "angela"

# user_2 = User()
# user_2.id = "002"
# user_2.username = "jack"

user_1 = User("001", "angela")
user_2 = User("002", "jack")

print(user_1.username)
print(user_2.username)

user_1.follow(user_2)
print(user_1.followers)
print(user_1.following)
print(user_2.followers)
print(user_2.following)


# -*- coding: utf-8 -*-
"""
Created on Fri Dec  9 21:22:45 2022

@author: kavin
"""

import connection as con
import pandas as pd

q = "select a.* , b.open from close_latest a,open_oldest b where a.symbol = b.symbol;"
con.myCursor.execute(q)
monthly = con.myCursor.fetchall()

monthly = pd.DataFrame(monthly)

monthly.columns=['SYMBOL','CLOSE','OPEN']

gain_arr = []

gain = (monthly['CLOSE'] - monthly['OPEN'])/monthly['OPEN']
print(type(monthly['CLOSE'] ))
gain = pd.DataFrame(gain)
final_op = pd.concat([monthly,gain],axis=1)
final_op.columns=['SYMBOL','CLOSE','OPEN','GAIN']

final_op = final_op.sort_values("GAIN", ascending=[False])

print(final_op[:26])
# final_op.columns= ['SYMBOL', 'SERIES', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'LAST', 'PREVCLOSE', 'TOTTRDQTY', 'TOTTRDVAL', 'TIMESTAMP', 'TOTALTRADES', 'ISIN', 'FIELD14',  'NAME_OF_COMPANY','DATE_OF_LISTING', 'PAID_UP_VALUE', 'MARKET_LOT', 'ISIN_NUMBER', 'FACE_VALUE','GAIN']
# final_op = final_op.sort_values("GAIN", ascending=[False])
# print(final_op[:26])


# author:爱在7元钱
# version:1.0
# date:2021年03月15日

import requests
import json

url = 'http://api.tianapi.com/txapi/worldtime/index'

def worldtime():
    currency_str_value = input("请输入要查询的城市(例如：芝加哥):")
    city1 = currency_str_value
    body = {
        "key": ""    , #需要自己去申请
        "city": city1}
    headers = {'content-type': "application/x-www-form-urlencoded"}
    response = requests.post(url, data=body, headers=headers)
    js = json.loads(response.text)
    if js["code"] == 200:
        city2 = js["newslist"][0]["city"]
        timezone = js["newslist"][0]['timeZone']
        time1 = js["newslist"][0]['strtime']
        print('%s 的时区为：%s' % (city2, timezone))
        print('%s 的当前时间为: %s' %(city2, time1))
    else:
        print('输入错误，请重新输入')

if __name__ == '__main__':
    worldtime()


import os
import platform
import subprocess
import sys

# We assume this script is located in the Mojo SDK in
# tools/bindings/pylib/mojom/parse.
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
SDK_ROOT = os.path.abspath(os.path.join(THIS_DIR, os.pardir, os.pardir,
    os.pardir, os.pardir, os.pardir))
PYTHON_SDK_DIR = os.path.abspath(os.path.join(SDK_ROOT, "python"))
sys.path.insert(0, PYTHON_SDK_DIR)
# In order to use mojom_files_mojom we need to make sure the dummy mojo_system
# can be found on the python path.
sys.path.insert(0, os.path.join(PYTHON_SDK_DIR, "dummy_mojo_system"))

from mojom.generate.generated import mojom_files_mojom
from mojom.generate.generated import mojom_types_mojom
from mojo_bindings import serialization

def RunParser(sdk_root, file_names, import_directories=None,
    meta_data_only=False):
  """Runs the mojom parser. Only 64-bit Linux and Mac is supported.

  Args:
    sdk_root: {str} Absolute path to the root of the Mojo SDK. The parser
        binary is expected to be found in
        <sdk_root>/tools/bindings/mojom_tool/bin/<platform>

    file_names {list of str} Paths to mojom files to be parsed. May be either
        absolute or relative to the current working directory.

    import_directories: {list of str} Optional specification of import
        directories where mojom imports should be searched. The elements of the
        list should be absolute paths or paths relative to the current working
        directory.

    meta_data_only: {bool} If True then the flag -meta-data-only
        will be passed to the parser.

  Returns:
    {str} The serialized mojom_files.MojomFileGraph returned by mojom parser,
    or None if the mojom parser returned a non-zero error code.
  """
  system_dirs = {
      ("Linux", "64bit"): "linux64",
      ("Darwin", "64bit"): "mac64",
      }
  system = (platform.system(), platform.architecture()[0])
  if system not in system_dirs:
    raise Exception("The mojom parser only supports Linux or Mac 64 bits.")

  mojom_tool = os.path.join(sdk_root, "tools", "bindings", "mojom_tool",
      "bin", system_dirs[system], "mojom")

  if not os.path.exists(mojom_tool):
    raise Exception(
        "The mojom parser could not be found at %s. "
        "You may need to run gclient sync."
        % mojom_tool)

  cmd = [mojom_tool, "parse"]
  if import_directories:
    cmd.extend(["-I", ",".join(import_directories)])
  if meta_data_only:
    cmd.extend(["-meta-data-only"])

  cmd.extend(file_names)

  try:
    return subprocess.check_output(cmd)
  except subprocess.CalledProcessError:
    return None

def DeserializeMojomFileGraph(serialized_bytes):
  """Deserializes a mojom_files.MojomFileGraph.

  Args:
    serialized_bytes: {str} The serialized mojom_files.MojomFileGraph returned
        by mojom parser
  Returns:
    {mojom_files.MojomFileGraph} The deserialized MojomFileGraph.
  """
  data = bytearray(serialized_bytes)
  context = serialization.RootDeserializationContext(data, [])
  return mojom_files_mojom.MojomFileGraph.Deserialize(context)

def ParseToMojomFileGraph(sdk_root, file_names, import_directories=None,
    meta_data_only=False):
  """Runs the mojom parser and deserializes the result. Only 64-bit Linux and
     Mac is supported.

  Args:
    sdk_root: {str} Absolute path to the root of the Mojo SDK. The parser
        binary is expected to be found in
        <sdk_root>/tools/bindings/mojom_tool/bin/<platform>

    file_names {list of str} Paths to mojom files to be parsed. May be either
        absolute or relative to the current working directory.

    import_directories: {list of str} Optional specification of import
        directories where mojom imports should be searched. The elements of the
        list should be absolute paths or paths relative to the current working
        directory.

    meta_data_only: {bool} If True then the flag -meta-data-only
        will be passed to the parser.

  Returns:
    {mojom_files.MojomFileGraph} The deserialized MojomFileGraph obtained by
    deserializing the bytes returned by mojom parser, or None if the mojom
    parser returned a non-zero error code.
  """
  serialized_bytes = RunParser(sdk_root, file_names, import_directories,
      meta_data_only)
  if serialized_bytes is None:
    return None
  return DeserializeMojomFileGraph(serialized_bytes)


from PyPDF2 import PdfReader, PdfWriter

pdf_file_path = input("Please type in the name of the PDF file: ")
file_base_name = pdf_file_path.replace('.pdf', '')

pdf = PdfReader(pdf_file_path)

n = list(map(int, input("Enter the page numbers (comma separated) to be extracted to a new file: ").split(",")))
pages = map(lambda p: p-1, n)

pdfWriter = PdfWriter()

for page_num in pages:
    pdfWriter.add_page(pdf.pages[page_num])

with open('{0}_subset.pdf'.format(file_base_name), 'wb') as f:
    pdfWriter.write(f)
    f.close()