import logging
from collections import OrderedDict
import numpy as np
import torch
import cv2
import math
import torch.nn as nn
from torch.nn.parallel import DataParallel, DistributedDataParallel
import models.networks as networks
import models.lr_scheduler as lr_scheduler
from .base_model import BaseModel
from models.modules.loss import ReconstructionLoss
from models.modules.ffmpeg_DE import DecodeEncode, DownSample, UpSample #codecImage, rgbdown
import sys
import torch.nn.functional as F
from models.modules.Quant import BypassRound, Quant
from models.modules.format_trans import rgb2yuv420_down, yuv4202rgb

logger = logging.getLogger('base')

class IRNModel(BaseModel):
    def __init__(self, opt):
        super(IRNModel, self).__init__(opt)

        if opt['dist']:
            self.rank = torch.distributed.get_rank()
        else:
            self.rank = -1  # non dist training
        train_opt = opt['train']
        test_opt = opt['test']
        self.train_opt = train_opt
        self.test_opt = test_opt
        self.scalea = opt['scalea']
        self.scaleb = opt['scaleb']

        self.netG = networks.define_G(opt).to(self.device)
        self.netA = networks.define_Analysis(opt).to(self.device)
        self.netS = networks.define_S(opt).to(self.device)

        if opt['dist']:
            self.netG = DistributedDataParallel(self.netG, device_ids=[torch.cuda.current_device()])
            self.netA = DistributedDataParallel(self.netA, device_ids=[torch.cuda.current_device()])
            self.netS = DistributedDataParallel(self.netS, device_ids=[torch.cuda.current_device()])

        else:
            self.netA = DataParallel(self.netA)
            self.netG = DataParallel(self.netG)
            self.netS = DataParallel(self.netS)

        # print network
        self.print_network()
        self.load()

        self.DecodeEncode = DecodeEncode()
        self.bypass_round = BypassRound

        if self.is_train:
            self.netG.train()
            self.netA.train()
            self.netS.train()

            # loss
            self.Reconstruction_forw = ReconstructionLoss(losstype=self.train_opt['pixel_criterion_forw'])
            self.Reconstruction_back = ReconstructionLoss(losstype=self.train_opt['pixel_criterion_back'])

            # optimizers
            wd_G = train_opt['weight_decay_G'] if train_opt['weight_decay_G'] else 0
            optim_params = []
            for k, v in self.netG.named_parameters():
                if v.requires_grad:
                    optim_params.append(v)
                else:
                    if self.rank <= 0:
                        logger.warning('Params [{:s}] will not optimize.'.format(k))
            self.optimizer_G = torch.optim.Adam(optim_params, lr=train_opt['lr_G'], weight_decay=wd_G, betas=(train_opt['beta1'], train_opt['beta2']))
            self.optimizers.append(self.optimizer_G)

            optim_params = []
            optim_rates = []
            for k, v in self.netA.named_parameters():
                if v.requires_grad:
                    if ('rate' not in k):
                        optim_params.append(v)
                    else:
                        optim_rates.append(v)
                else:
                    if self.rank <= 0:
                        logger.warning('Params [{:s}] will not optimize.'.format(k))
            self.optimizer_A = torch.optim.Adam(optim_params, lr=train_opt['lr_A'], weight_decay=wd_G, betas=(train_opt['beta1'], train_opt['beta2']))
            self.optimizers.append(self.optimizer_A)

            #self.optimizer_C = torch.optim.Adam(optim_rates, lr=train_opt['lr_A'], weight_decay=wd_G, betas=(train_opt['beta1'], train_opt['beta2']))
            #self.optimizers.append(self.optimizer_C)

            optim_params = []
            for k, v in self.netS.named_parameters():
                if v.requires_grad:
                    optim_params.append(v)
                else:
                    if self.rank <= 0:
                        logger.warning('Params [{:s}] will not optimize.'.format(k))
            self.optimizer_S = torch.optim.Adam(optim_params, lr=train_opt['lr_S'], weight_decay=wd_G, betas=(train_opt['beta1'], train_opt['beta2']))
            self.optimizers.append(self.optimizer_S)

            # schedulers
            if train_opt['lr_scheme'] == 'MultiStepLR':
                for optimizer in self.optimizers:
                    self.schedulers.append(
                        lr_scheduler.MultiStepLR_Restart(optimizer, train_opt['lr_steps'],
                                                         restarts=train_opt['restarts'],
                                                         weights=train_opt['restart_weights'],
                                                         gamma=train_opt['lr_gamma'],
                                                         clear_state=train_opt['clear_state']))
            elif train_opt['lr_scheme'] == 'CosineAnnealingLR_Restart':
                for optimizer in self.optimizers:
                    self.schedulers.append(
                        lr_scheduler.CosineAnnealingLR_Restart(
                            optimizer, train_opt['T_period'], eta_min=train_opt['eta_min'],
                            restarts=train_opt['restarts'], weights=train_opt['restart_weights']))
            else:
                raise NotImplementedError('MultiStepLR learning rate scheme is enough.')

            self.log_dict = OrderedDict()

    def gaussian_batch(self, dims):
        return torch.randn(tuple(dims)).to(self.device)

    def loss_forward(self, out, y):
        l_forw_fit = self.train_opt['lambda_fit_forw'] * self.Reconstruction_forw(out, y)

        return l_forw_fit

    def loss_backward(self, out, y):
        l_back_rec = self.train_opt['lambda_rec_back'] * self.Reconstruction_back(out, y)

        return l_back_rec

    def feed_data(self, data):
        #self.ref_L_rgb = data['LQ'].to(self.device)  # LQ
        self.real_H_rgb = data['GT'].to(self.device)  # GT

    def optimize_parameters(self, step):

        ### compress
        qp = "6"
        print('real_H_rgb size: ', self.real_H_rgb.shape)
        self.real_H, self.ref_L = rgb2yuv420_down(self.real_H_rgb, self.scalea, self.scaleb) # (B * 6 * H/2 * W/2 , B * 6 * H/4 * W/4)
        print('real_H size: ', self.real_H.shape)
        print('ref_L size: ', self.ref_L.shape)
        _, _, h, w = self.real_H.size()
        out_mv = None

        # update netA virtual codec
        lr_lanczos = self.ref_L # B * 6 * H/4 * W/4
        lr_lanczos_codec, out_mv = self.DecodeEncode(lr_lanczos, qp, False)
        self.optimizer_A.zero_grad()
        lr_lanczos_codec_est, _ = self.netA(x=lr_lanczos, mv=out_mv, I=True)
        l_codec_est = self.loss_forward(lr_lanczos_codec_est, lr_lanczos_codec) # loss between netA & x265 decoded lr
        loss_A = l_codec_est
        loss_A.backward()
        torch.nn.utils.clip_grad_norm_(self.netA.parameters(), 1.0)
        self.optimizer_A.step()

        # update encoder & decoder netG (& rate estimation network in netA), netS
        self.optimizer_G.zero_grad()
        # self.optimizer_C.zero_grad()
        y, y_syntax, content = self.netG(x=self.real_H, up=False)  # forward downscaling, B * 6 * H/4 * W/4
        print('content size: ', content.shape) # B * 6 * H/4 * W/4
        print('y size: ', y.shape) # B * 192 * H/32 * W/32
        print('y_syntax size: ', y_syntax.shape) # B * 16 * H/32 * W/32
        content_codec_est, bpp_netA = self.netA(x=content, mv=out_mv, I=True)
        # l_size_fit = torch.mean(bpp_netA)
        sr_netG = self.netG(x=content_codec_est, up=True) # B * M * H/2 * W/2

        # Net for syntax
        bpp_netS, sr_tilde = self.netS(y = y, x_tilde = sr_netG)  # B * 6 * H/2 * H/2

        l_tilde_real = self.loss_forward(sr_tilde, self.real_H)
        l_content_ref = self.loss_forward(content, self.ref_L)

        loss = l_tilde_real + 0.5 * l_content_ref  + bpp_netS #+ 8e-4 * l_size_fit
        loss = loss.mean()
        l_tilde_real = l_tilde_real.mean()
        l_content_ref = l_content_ref.mean()
        bpp_netS = bpp_netS.mean()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(self.netG.parameters(), 1.0)
        torch.nn.utils.clip_grad_norm_(self.netS.parameters(), 1.0)
        self.optimizer_G.step()
        self.optimizer_S.step()
        #self.optimizer_C.step()

        # set log
        self.log_dict['l_codec_est'] = l_codec_est.item()
        self.log_dict['l_content_ref'] = l_content_ref.item()
        self.log_dict['l_tilde_real'] = l_tilde_real.item()
        self.log_dict['bpp_netS'] = bpp_netS.item()
        self.log_dict['distortion'] = (l_tilde_real + 0.5 * l_content_ref).item()
        #self.log_dict['l_size_fit'] = l_size_fit.item()

    def test(self):
        qp = "22"
        self.netG.eval()
        self.netS.eval()
        with torch.no_grad():
            # YUV train
            real_H, ref_L = rgb2yuv420_down(self.real_H_rgb, self.scalea, self.scaleb) # (B * 6 * H/2 * W/2 , B * 6 * H/4 * W/4)
            self.ref_L = yuv4202rgb(ref_L) # B * 3 * H/2 * W/2
            # _, _, h, w = real_H.size()
            # ref_L, _ = self.DecodeEncode(ref_L, qp, True) # B * 6 * H/4 * W/4
            # fake_H_bic = self.yuvUpsample(ref_L, h, w) # b * 6 * height//2 * width//2
            # self.fake_H_bic = yuv4202rgb(fake_H_bic) # b * 3 * height * width
            # self.psnr_fix = 10 * torch.log10(1**2 / torch.mean((real_H - fake_H_bic)**2))

            y, y_syntax, content = self.netG(x=real_H, up=False) # forward downscaling, B * 6 * H/4 * W/4
            self.forw_L = yuv4202rgb(content) # B * 3 * H/2 * W/2
            content_codec, _ = self.DecodeEncode(content, qp, True) # B * 6 * H/4 * W/4
            sr_netG = self.netG(x=content_codec, up=True) # B * M * H/2 * W/2
            bpp_netS, sr_tilde = self.netS(y = y, x_tilde = sr_netG, mode='test')  # B * 6 * H/2 * H/2
            self.fake_H = yuv4202rgb(sr_tilde) # B * 3 * H * W
            self.psnr = 10 * torch.log10(1**2 / torch.mean((real_H - sr_tilde)**2))
            #self.psnr = 10 * torch.log10(255**2 / torch.mean((real_H - sr_tilde)**2))
            self.bpp_netS = bpp_netS
            self.psnr = self.psnr.mean()
            self.bpp_netS = self.bpp_netS.mean()

        self.netG.train()
        self.netS.train()

    def downscale(self, HR_img):
        self.netG.eval()
        with torch.no_grad():
            y, y_syntax, LR_img = self.netG(x=HR_img, up=False)
        return LR_img

    def upscale(self, LR_img, scale, gaussian_scale=1):
        with torch.no_grad():
            HR_img = self.netG(x=LR_img, up=True, flow=None, I=True)
        self.netG.eval()
        return HR_img

    def get_current_log(self):
        return self.log_dict

    def get_current_visuals(self):
        out_dict = OrderedDict()
        out_dict['LR_ref'] = self.ref_L.detach()[0].float().cpu()
        out_dict['SR'] = self.fake_H.detach()[0].float().cpu()
        out_dict['LR'] = self.forw_L.detach()[0].float().cpu()
        out_dict['GT'] = self.real_H.detach()[0].float().cpu()
        #out_dict['SR_bic'] = self.fake_H_bic.detach()[0].float().cpu()
        #out_dict['PSNR_fix'] = self.psnr_fix.detach().item()
        out_dict['PSNR'] = self.psnr.detach().item()
        out_dict['bpp_netS'] = self.bpp_netS.detach().item()
        # out_dict['SSIM_fix'] = self.ssim_fix.detach().item()
        # out_dict['SSIM'] = self.ssim.detach().item()
        return out_dict

    def print_network(self):
        s, n = self.get_network_description(self.netG)
        if isinstance(self.netG, nn.DataParallel) or isinstance(self.netG, DistributedDataParallel):
            net_struc_str = '{} - {}'.format(self.netG.__class__.__name__,
                                             self.netG.module.__class__.__name__)
        else:
            net_struc_str = '{}'.format(self.netG.__class__.__name__)
        if self.rank <= 0:
            logger.info('Network G structure: {}, with parameters: {:,d}'.format(net_struc_str, n))
            logger.info(s)

    def load(self):
        load_path_G = self.opt['path']['pretrain_model_G']
        if load_path_G is not None:
            logger.info('Loading model for G [{:s}] ...'.format(load_path_G))
            self.load_network(load_path_G, self.netG, self.opt['path']['strict_load'])

        load_path_A = self.opt['path']['pretrain_model_A']
        if load_path_A is not None:
            logger.info('Loading model for A [{:s}] ...'.format(load_path_A))
            self.load_network(load_path_A, self.netA, self.opt['path']['strict_load'])

        load_path_S = self.opt['path']['pretrain_model_S']
        if load_path_S is not None:
            logger.info('Loading model for S [{:s}] ...'.format(load_path_S))
            self.load_network(load_path_S, self.netS, self.opt['path']['strict_load'])

    def save(self, iter_label):
        self.save_network(self.netG, 'G', iter_label)
        self.save_network(self.netA, 'A', iter_label)
        self.save_network(self.netS, 'S', iter_label)


class BankAccount:
    bank_accounts = []

    def __init__(self, int_rate, balance=0):
        self.int_rate = int_rate
        self.balance = balance

    def deposit(self, amount):
        self.balance = self.balance + amount
        return self

    def withdraw(self, amount):
        if (self.balance < amount):
            print(
                f"Insufficient funds: Available balance: {self.balance}. Requested amount: {amount}")
        else:
            self.balance = self.balance - amount
        return self

    def display_account_info(self):
        print(f"Interest rate: {self.int_rate}")
        print(f"Available balance: {self.balance}")
        return self

    def yield_interest(self):
        if (self.balance > 0):
            self.balance = self.balance + (self.balance * self.int_rate)
        return self

    @classmethod
    def print_instances(cls):
        for i in range(0, len(cls.bank_accounts)):
            print(cls.bank_accounts[i].balance)
            print(cls.bank_accounts[i].int_rate)

Account1 = BankAccount(0.25, 50)
Account2 = BankAccount(0.25, 50)

Account1.deposit(25).deposit(55).deposit(45).withdraw(
    1).yield_interest().display_account_info()
Account2.deposit(150).deposit(200).withdraw(20).withdraw(25).withdraw(
    3).withdraw(40).yield_interest().display_account_info()

print(BankAccount.print_instances())


# Schedule Announcer Mate

# Imports

import datetime

from google.oauth2 import service_account
from googleapiclient.discovery import build

from threading import Thread

import pyttsx3

import time

# Get Structured Date Function
def get_current_structured_date():
    current_datetime = datetime.datetime.now()
    current_day_of_the_month = current_datetime.strftime("%d")
    current_month = current_datetime.strftime("%B")
    current_year = current_datetime.strftime("%Y")
    current_date = f"{current_day_of_the_month} {current_month} {current_year}"
    return current_date

# Get Structured Time Function
def get_current_structured_time():
    current_datetime = datetime.datetime.now()
    current_hour = current_datetime.strftime("%H")
    current_minute = current_datetime.strftime("%M")
    current_time = f"{current_hour}:{current_minute}"
    return [current_time, current_hour]

# pyttsx3 Variables

saying_engine = pyttsx3.init()
saying_engine.setProperty("rate", 200)

speaker_voices = saying_engine.getProperty("voices")
saying_engine.setProperty("voice", speaker_voices[1].id)

# For Sam To Be Able To Speak
def speak(thing_to_say):
    saying_engine.say(thing_to_say)
    saying_engine.runAndWait()

# Google Sheets Variables

credentials = service_account.Credentials.from_service_account_file("../credentials.json",  scopes=['https://www.googleapis.com/auth/spreadsheets'])
service = build("sheets", "v4", credentials=credentials)

spreadsheet_id = "1R1ooZQYaSHPByec1VqjOjUUd1SFjH_YsCsgpeZGXssI"

# Getting Schedule Accordingly From Google Sheets
def get_schedule(schedule_date):
    sheet_name = schedule_date
    schedule_range = f"{sheet_name}!B3:D290"

    # Response Structure
    # response = {values: arrays_of(time, task, task_details)}

    response = service.spreadsheets().values().get(spreadsheetId = spreadsheet_id, range = schedule_range).execute()
    return response

# Dictionary For Announcements With Their Times

# Structure = {"time": {"task": "task details"}}
CURRENT_SCHEDULE = {}
SAYING_QUEUE = []

# Function For Populating Global Schedule Dictionary With A Proper Format

def populate_current_schedule(schedule_to_populate_with):

    global CURRENT_SCHEDULE

    schedule_values = schedule_to_populate_with["values"]

    current_hour = get_current_structured_time()[1]

    for schedule_value in schedule_values:

        length_of_schedule_value_array = len(schedule_value)

        if length_of_schedule_value_array != 1:
            task_time = schedule_value[0]
            task = schedule_value[1]

            task_hour = task_time.split(":")[0]

            if task_hour >= current_hour:

                if length_of_schedule_value_array == 3:
                    task_detail = schedule_value[2]
                    CURRENT_SCHEDULE[task_time] = f"{task}, {task_detail}"
                else:
                    CURRENT_SCHEDULE[task_time] = f"{task}"

# Thread For Properly Announcing
def announcing_stuff():

    global SAYING_QUEUE

    while True:
        for thing_to_say in SAYING_QUEUE:
            speak(thing_to_say)

announcing_stuff_thread = Thread(target=announcing_stuff)

# Thread For Checking And Updating Date | Accordingly, Configuring The Dictionary

# To Check:
    # Date Changes
    # New Changes In Sheet

def keeping_schedule_up_to_date():

    previous_date = get_current_structured_date()
    previous_schedule = get_schedule(previous_date)

    populate_current_schedule(previous_schedule)

    while True:

        new_date = get_current_structured_date()
        new_schedule = get_schedule(new_date)

        if new_date != previous_date:

            populate_current_schedule(new_schedule)
            previous_date = new_date

        else:

            if new_schedule != previous_schedule:
                populate_current_schedule(new_schedule)
                previous_schedule = new_schedule

        time.sleep(2)

thread_for_keeping_schedule_up_to_date = Thread(target=keeping_schedule_up_to_date)

# Thread For Schedule Announcement For The Day | Going Through The Dictiornary, Checking For Each's Time

def pushing_schedule_to_announcement():

    global CURRENT_SCHEDULE, SAYING_QUEUE

    while True:

        current_time = get_current_structured_time()[0]

        try:

            for task_time, task_announcement in CURRENT_SCHEDULE.items():

                if current_time == task_time:

                    CURRENT_SCHEDULE.pop(task_time)

                    for iteration in range(3):
                        SAYING_QUEUE.append(task_announcement)

        except RuntimeError:

            error = "Current Schedule Was Changed When It Was Being Operated On"

pushing_schedule_to_announcement = Thread(target=pushing_schedule_to_announcement)

# Main With Everything Compiled

if __name__ == "__main__":
    announcing_stuff_thread.start()
    thread_for_keeping_schedule_up_to_date.start()
    pushing_schedule_to_announcement.start()


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

# Function for adding a new student to the records
def add_student(students):
    # Prompt user for input data
    student_number = input("Enter student number: ")
    surname = input("Enter surname: ")
    given_name = input("Enter given name: ")
    unit_mark = float(input("Enter unit mark: "))

    # Create dictionary of student data and append it to the array of students
    student = {
        'student_number': student_number,
        'surname': surname,
        'given_name': given_name,
        'unit_mark': unit_mark
    }
    students = np.append(students, student)

    # Print success message and return updated array of students
    print("Student details added successfully.")
    return students

# Function for searching the records for students based on a keyword
def search_students(students):
    # Prompt user for search keyword
    search_key = input("Enter student number or name to search: ").lower()
    matching_students = []

    # Check each student in the array for a match with the search key
    for student in students:
        if (search_key in student['student_number'].lower()) or (search_key in student['surname'].lower()) or (search_key in student['given_name'].lower()):
            matching_students.append(student)

    # Print list of matching students if any found, else print error message
    if len(matching_students) > 0:
        print("Matching students found:")
        for student in matching_students:
            print_student_details(student)
    else:
        print("No matching students found.")

# Helper function for printing details of a single student record
def print_student_details(student):
    print("Student Number:", student['student_number'])
    print("Surname:", student['surname'])
    print("Given Name:", student['given_name'])
    print("Unit Mark:", student['unit_mark'])
    print()

# Function for displaying all students in a particular grade range
def display_students_by_grade(students):
    # Prompt user for target grade
    grade = input("Enter unit grade (HD, D, C, P, or N): ")
    grade_students = []

    # Check each student in the array for a match with the target grade
    for student in students:
        student_grade = convert_mark_to_grade(student['unit_mark'])
        if student_grade == grade:
            grade_students.append(student)

    # Print list of matching students if any found, else print error message
    if len(grade_students) > 0:
        print("Students with grade", grade + ":")
        for student in grade_students:
            print_student_details(student)
    else:
        print("No students found with grade", grade + ".")

# Helper function for converting a numeric mark to a letter grade
def convert_mark_to_grade(mark):
    if mark >= 85:
        return "HD"
    elif mark >= 75:
        return "D"
    elif mark >= 65:
        return "C"
    elif mark >= 50:
        return "P"
    else:
        return "N"

# Function for deleting a student record from the array
def delete_student(students):
    # Prompt user for target student number
    student_number = input("Enter student number to delete: ")
    deleted = False

    # Check each student in the array for a match with the target student number
    for index, student in enumerate(students):
        if student['student_number'] == student_number:
            students = np.delete(students, index)
            deleted = True
            break

    # Print success message or error message if no matching student found
    if deleted:
        print("Student", student_number, "deleted successfully.")
    else:
        print("Student", student_number, "not found.")

    # Return updated array of students
    return students

# Function for loading student records from a CSV file and adding them to the existing array
def load_students_from_file(students):
    # Prompt user for file path and check if file exists
    file_path = input("Enter path to CSV file: ")
    if not os.path.exists(file_path):
        print("File not found:", file_path)
        return students

    # Open file and read each row as a new student, then add to the array
    with open(file_path, 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        next(reader)  # skip header row
        for row in reader:
            try:
                student_number = row[0]
                surname = row[1]
                given_name = row[2]
                unit_mark = float(row[3])
                student = {
                    'student_number': student_number,
                    'surname': surname,
                    'given_name': given_name,
                    'unit_mark': unit_mark
                }
                students = np.append(students, student)
            except ValueError:
                print("Error loading row:", row)

        # Print success message and return updated array of students
        print(len(students), "students loaded from file", file_path)
    return students

# Function for saving all current student records to a CSV file
def save_students_to_file(students):
    # Prompt user for file path and check if file exists, giving option to overwrite if it does
    file_path = input("Enter path to save CSV file: ")
    if os.path.exists(file_path):
        choice = input(f"{file_path} already exists. Do you want to overwrite it? (y/n)")
        if choice != 'y':
            print("Operation cancelled. No changes were made.")
            return

    # Open file and write header row, then write each student record as a new row
    with open(file_path, 'w', newline='') as csvfile:
        fieldnames = ['student_number', 'surname', 'given_name', 'unit_mark']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for student in students:
            writer.writerow(student)
        print(len(students), "students saved to file", file_path)

# Function for displaying a pie chart showing the distribution of grades among all students
def display_grade_distribution(students):
    grades, counts = np.unique([convert_mark_to_grade(student['unit_mark']) for student in students], return_counts=True)
    plt.pie(counts, labels=grades)
    plt.title('Grade Distribution')
    plt.show()

# Function for displaying a bar chart showing the distribution of numeric marks among all students
def display_marks_distribution(students):
    marks = [student['unit_mark'] for student in students]
    bins = [0, 30, 40, 50, 60, 70, 80, 90, 100]
    categories = ['0-29', '30-39', '40-49', '50-59', '60-69', '70-79', '80-89', '90-100']
    hist, _ = np.histogram(marks, bins=bins)
    percent = hist/len(marks) * 100
    plt.bar(categories, percent)
    plt.title('Marks Distribution')
    plt.xlabel('Mark Ranges')
    plt.ylabel('Percentage')
    plt.show()

# Function for printing the main menu of options and prompting user for choice
def print_menu():
    print("Menu:")
    print("1. Add student details")
    print("2. Search for students")
    print("3. Display students by grade")
    print("4. Delete student record")
    print("5. Load student records from a CSV file")
    print("6. Save the student records in the current system to a CSV file")
    print("7. Quit")
    print("8. Display grade distribution")
    print("9. Display marks distribution")

# Main function that controls program flow
def main():
    students = np.array([])

    # Load initial student records from file
    students = load_students_from_file(students)

    # Display main menu and prompt user for choice until they choose to quit
    while True:
        print_menu()
        choice = input("Enter your choice (1-9): ")
        if choice == '1':  # Call add_student() function if user chooses option 1
            students = add_student(students)
        elif choice == '2':  # Call search_students() function if user chooses option 2
            search_students(students)
        elif choice == '3':  # Call display_students_by_grade() function if user chooses option 3
            display_students_by_grade(students)
        elif choice == '4':  # Call delete_student() function if user chooses option 4
            students = delete_student(students)
        elif choice == '5':  # Call load_students_from_file() function if user chooses option 5
            students = load_students_from_file(students)
        elif choice == '6':  # Call save_students_to_file() function if user chooses option 6
            save_students_to_file(students)
        elif choice == '7':  # End program if user chooses option 7
            print("Quitting the program...")
            break
        elif choice == '8':  # Call display_grade_distribution() function if user chooses option 8
            display_grade_distribution(students)
        elif choice == '9':  # Call display_marks_distribution() function if user chooses option 9
            display_marks_distribution(students)
        else:
            # Print error message and re-prompt user for menu choice if they entered an invalid option
            print("Invalid choice. Please try again.")

if __name__ == '__main__':
    main()


import heapq
#本身是小根堆
class Solution:
    def getMax(self, arr: list, k):
        if k==0:return list()
        h=[-x for x in arr[:k]]#前k个元素取反，取反最小的就是最大的
        heapq.heapify(h)#转为heapq堆
        length=len(arr)
        for i in range(k,length):
            #minh=heapq.heappop(h)

            if -h[0]>arr[i]:#取反就是最大的
                heapq.heappop(h)
                heapq.heappush(h,-arr[i])

        res=[-x for x in h]
        return res
a=Solution()
res=a.getMax([3,2,1],2)
print(res)


# Problem Statement: https://leetcode.com/problems/insert-into-a-binary-search-tree/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        if not root: return TreeNode(val)

        cur, next = None, root
        while next:
            cur = next
            next = cur.left if val < cur.val else cur.right

        if val < cur.val:
            cur.left = TreeNode(val)
        else:
            cur.right = TreeNode(val)

        return root

Medium/701. Insert into a Binary Search Tree.py


import sys
sys.setrecursionlimit(10**7)

ip = int(input())

def fibo(n, arr):
    if len(arr) <= n:
        arr.append(fibo(n-1, arr) + fibo(n-2, arr))
    return arr[n] % 1234567

def solution(n):
    if n == 1 or n == 2: return 1
    elif n == 0: return 0
    else:
        arr = [0,1,1]
        return fibo(n, arr)

def solution2(num):
    a, b = 0, 1
    for i in range(num):
        a, b = b%1234567, (a+b)%1234567
    return a

# print(solution(ip)) # 2795 이상부터는 스택 오버플로우 발생
print(solution2(ip))


import random
import math
import matplotlib.pyplot as plt

MAPWIDTH = 100 # 地图的宽度
MAPLEN = 200 # 地图的长度

class Path:
    '''represent hamiltonian cycle.

    attributes:
    num(int):num of existing edges
    path(list):sequence of city ID listed in city_map
    city_map(CityMap):related map of cities
    '''
    def __init__(self, city_map, path_list=[]):
        self.num = city_map.num
        self.city_map = city_map
        if len(path_list) == 0:
            self.path = list(range(self.num))
            random.shuffle(self.path)
        else:
            self.path = path_list.copy()

    def get_distance(self):
        '''return total distance of the cycle.'''
        distance = 0
        for i in range(self.num):
            distance += self.city_map.map[self.path[i]][self.path[(i+1)%self.num]]
        return distance

    def crossover_path(self, partner):
        '''crossover with partner and return nothing.

        args:
        partner(Path):path to crossover with
        '''
        min = random.randint(0, self.num-1)
        max = random.randint(min, self.num-1)
        self.path[min: max+1], partner.path[min: max+1] = \
        partner.path[min: max+1], self.path[min: max+1]
        # 交换去重
        check_area = list(range(self.num))[0: min] +\
                     list(range(self.num))[max+1: self.num]
        ptn_s = 0
        for i in check_area:
            if self.path[i] in self.path[min:max+1]:
                for j in check_area[ptn_s:]:
                    if partner.path[j] in partner.path[min:max+1]:
                        self.path[i], partner.path[j] =\
                        partner.path[j], self.path[i]
                        ptn_s += 1
                        break
                    ptn_s += 1

class CityMap:
    '''representation of graph of cities.'''
    def __init__(self, num):
        self.num = num
        # 产生一个初始的城市地图，采用邻接矩阵来存储
        self.city = []
        for i in range(self.num):
            self.city.append((random.randrange(MAPWIDTH),
                              random.randrange(MAPLEN)))
        # 先假设每个城市之间都有路
        self.map = [[0]*self.num for i in range(self.num)]
        for i in range(self.num):
            for j in range(self.num):
                self.map[i][j] = distance(self.city[i],
                                          self.city[j])

class Group:
    '''representation of Group in revolution.'''

    def __init__(self, city_map, init_size):
        '''constructor of Group class.

        args:
        city_map(CityMap):city_map that the group is based on
        init_size(int):initial size of group
        '''
        self.city_map = city_map # 委托模式，仅仅是一个引用而不是新的对象
        self.init_size = init_size
        self.path = [Path(city_map) for i in range(init_size)]

    def score(self, save_rate=1.0):
        '''evaluate every path in the group and return a list of score.

        args:
        save_rate(float):highest surviving possibility
        '''
        result = []
        for i in range(self.init_size):
            result.append(float(1)/self.path[i].get_distance())
        max_possibility = max(result)
        result = [i / max_possibility * save_rate for i in result]
        return result

    def revolve(self, variation_rate):
        '''implement revolution.

        args:
        variation_rate(float):ratio of variate genes
        '''
        variation_rate = int(variation_rate * self.city_map.num)
        score = self.score()
        max_index = score.index(max(score))
        result = []
        for i in range(self.init_size):
            if random.random() > score[i]: # 把这一项变异掉
                list = self.path[max_index].path.copy()
                for i in range(variation_rate):
                    a = random.randrange(0, self.city_map.num)
                    b = random.randrange(0, self.city_map.num)
                    list[a], list[b] = list[b], list[a]
                self.path[i] = Path(self.city_map, list)
                result.append(0)
            else:
                result.append(1)
        good_index = [i[0] for i in enumerate(result) if i[1] == 1]
        random.shuffle(good_index)
        for i in range(int(len(good_index)/2)):
            self.path[good_index[2*i]].crossover_path(self.path[good_index[2*i+1]])

    def show(self, choice=0):
        '''display path as a matplotlib pic.

        args:
        choice(int):choose the path to display

        note:
        if choice missing then display the shortest path in the group.
        parameter choice should be within the range of 0~num or will arise Exception.
        '''
        if(choice == 0):
            score = self.score()
            choice = score.index(max(score))
        elif choice>0 and choice<len(self.path)+1:
            choice -= 1
        else:
            raise(Exception, "Wrong choice value.")

        plt.figure(figsize = (10, 10), dpi = 100)
        x = [i[0] for i in self.city_map.city]
        y = [i[1] for i in self.city_map.city]
        plt.scatter(x, y)
        for i in range(self.path[choice].num):
            plt.plot([self.city_map.city[self.path[choice].path[i]][0],\
                      self.city_map.city[self.path[choice].path[(i+1)%self.path[choice].num]][0]],\
                     [self.city_map.city[self.path[choice].path[i]][1],\
                      self.city_map.city[self.path[choice].path[(i+1)%self.path[choice].num]][1]])
        plt.show()

def distance(city1, city2):
    '''caculate and return distance between two cities.'''
    return math.sqrt(math.pow(city1[0]-city2[0], 2)+math.pow(city1[1]-city2[1], 2))

if __name__== '__main__':
    init_size = 1000
    epoch = 20

    city_map = CityMap(100)
    group = Group(city_map, init_size)
    result = []
    group.show()
    for i in range(epoch):
        group.revolve(10)
        score = group.score(0.8)
        min_length = group.path[score.index(max(score))].get_distance()
        print("min length:", min_length)
        result.append(min_length)
    group.show()
    plt.plot(result)
    plt.show()


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def solve(self, nums: List[int]) -> TreeNode:
        if len(nums) == 0:
            return None
        if len(nums) == 1:
            return TreeNode(nums[0])
        mid = len(nums) // 2
        root = TreeNode(nums[mid])
        l = self.solve(nums[:mid])
        r = self.solve(nums[mid+1:])
        root.left = l
        root.right = r
        return root

    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        return self.solve(nums)

0108. Convert Sorted Array to Binary Search Tree/solution.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec  8 14:21:40 2020

@author: gilleschardon
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
import acosolo.cmf as cmf
from acosolo.sourcemodels import freefield2D
from acosolo.beamforming import bmap_unc
from acosolo.utils import grid3D

# CMF-NNLS

# Ref.: G. Chardon, J. Picheral, F. Ollivier, Theoretical analysis of the DAMAS algorithm and efficient implementation of the Covariance Matrix Fitting method for large-scale problems, Journal of Sound and Vibration,

mat = loadmat("damasdata2D.mat")
k = mat['k'][0,0]
Sigma = mat['Data']
Array = mat['Pmic']

# Normalization, for stability
Sigma = Sigma / np.max(Sigma)

# Source grid
Z = 4.6

g2D = lambda x : freefield2D(Array, x, Z, k)

Xgrid, dimgrid = grid3D([-2, -1, Z], [1, 0, Z], 0.02)
A = g2D(Xgrid)
bmap = bmap_unc(Sigma, A)

cmf_map = cmf.cmf_nnls_lh(A, Sigma, dr = True)

cmfDB = 10*np.log10(cmf_map)

def plotmap(pmap, name, dynrange):
    mapDB = 10*np.log10(pmap)
    m = np.max(mapDB)
    plt.figure()
    plt.imshow(mapDB, cmap='hot', vmax=m, vmin=m-dynrange, origin="lower")
    current_cmap = plt.cm.get_cmap()
    current_cmap.set_bad(color='black')
    plt.axis('image')
    plt.title(name)
    ax = plt.gca()
    ax.set_facecolor('black')

#%%

plotmap(np.reshape(cmf_map, [dimgrid[1], dimgrid[0]]), 'CMF-NNLS', 20)
plotmap(np.reshape(bmap, [dimgrid[1], dimgrid[0]]), 'Beamforming', 20)


# -*- coding:utf-8 -*-
import os
import time
import pymongo
import requests
from dotenv import load_dotenv
load_dotenv()

def get_collection():
    mongo_url = os.getenv('MONGO_URL')
    client = pymongo.MongoClient(mongo_url)
    collection = client.image_collection
    return collection

def main():
    collection = get_collection()
    images = collection.images

    while True:
        try:
            _cpu_serial_id = None
            file_list = []
            for meta in images.find():
                _id = meta['_id']
                _cpu_serial_id = meta['cpu_serial']
                _image_path = meta['path']

                file_list.append(('image', open(_image_path, 'rb')))

                # remove information (mongo, database)
                images.delete_one({'_id': _id})
                os.remove(_image_path)

            if len(file_list) == 0:
                continue

            url = os.getenv('TARGET_URL') + 'datacollections/image/'
            res = requests.post(
                url,
                data={'cpu_serial_id': _cpu_serial_id},
                files=file_list
            )
        except KeyboardInterrupt:
            break

if __name__ == '__main__':
    main()


"""
RL-Scope configuration settings.

``DEBUG_*`` are useful for enabling certain debug logging statements.
:py:class:`_EnvVars` is for reading environment variables.
"""
from os.path import join as _j, abspath as _a, exists as _e, dirname as _d, basename as _b
from os import environ as ENV
import shutil
import re
import ctypes.util
import sys
import os
import textwrap

# NOTE: Don't import logger here since rlscope_logging needs py_config.
# from rlscope.profiler.rlscope_logging import logger
import rlscope

INSTALL_ROOT = _d(os.path.realpath(rlscope.__file__))

CPP_LIB = _j(INSTALL_ROOT, 'cpp', 'lib')
CPP_BIN = _j(INSTALL_ROOT, 'cpp', 'bin')
CPP_INCLUDE = _j(INSTALL_ROOT, 'cpp', 'include')

USE_NUMBA = False
nb = None
if USE_NUMBA:
  import numba as nb

ROOT = _d(_d(_a(__file__)))

RLSCOPE_DIR = ROOT

GIT_REPO_URL = "https://github.com/UofT-EcoSystem/rlscope"

RLSCOPE_TEST_DIR = _j(RLSCOPE_DIR, 'test_results')

DEBUG = False

VERSION_TXT = _j(ROOT, "version.txt")

CLONE = _j(ENV['HOME'], 'clone')
BASELINES_ROOT = _j(CLONE, 'baselines')
BENCH_DQN_PY = _j(BASELINES_ROOT, 'baselines/deepq/experiments/benchmark_dqn.py')

SQL_IMPL = 'psql'
assert SQL_IMPL in {'psql', 'sqlite'}

ANALYSIS_PY = _j(ROOT, "python/scripts", "analyze.py")
GENERATE_INDEX_PY = _j(ROOT, "python/scripts", "generate_rlscope_plot_index.py")

CPP_UNIT_TEST_CMD = 'rls-test'

# If true, when you provide --rlscope-debug, log messages when:
# - A session is created by calling tf.Session()
# - A session is used by calling sess.run()
# - RL-Scope trace-data belonging to a session is dumped
DEBUG_TRACE_SESSION = False

# Verbose debugging: Print calls to set_operation/end_operation
DEBUG_OPERATIONS = False

# Verbose debugging: Print calls to librlscope.so
DEBUG_RLSCOPE_LIB_CALLS = False

# If true, have rls-util-sampler log to stdout the GPU/CPU info it queries.
# This can be noisy.
DEBUG_UTIL_SAMPLER = False

# If true, dump stack when we initially start recording progress
# for a phase during rlscope.prof.report_progress(...)
DEBUG_REPORT_PROGRESS = False
# If true, dump stack for ALL calls to rlscope.prof.report_progress(...) that update progress
# (not just the first call for a phase)
DEBUG_REPORT_PROGRESS_ALL = False

# If True, print out how event are grouped into OpStack's when split_op_stacks.
DEBUG_SPLIT_STACK_OPS = False
# If True, skip inserting any profiling-overhead events.
DEBUG_SKIP_PROFILING_OVERHEAD = False

DEBUG_GPU_HW = False

# If True, then log all calls into librlscope.so
# DEBUG_SAMPLE_CUDA_API = True

# If True, then log all calls into a wrapped library (e.g. TensorFlow, simulator, etc.).
# DEBUG_C_LIB_CALLS = True

# In case docker causes different runtime behaviour, we can use this switch.
#
# Current issues:
# - nvidia-smi doesn't work properly from within a container.
#   Docker uses a DIFFERENT pid namespace within the container.
#   i.e. the pid of the proc in the container will be DIFFERENT
#   from the proc outside the the container, when you look at it with 'top'.
#   HOWEVER, nvidia-smi within the container still reports the pid of process
#   OUTSIDE the container.
#   - This bug affects GPU memory/compute utilization sampling;
#     if we're in a docker container just sample the entire memory usage of a GPU.
IS_DOCKER = False
if 'RLSCOPE_LOGGED_IN' in ENV:
  IS_DOCKER = True

# Print the names of all the TensorFlow/Simulator function calls that get wrapped.
DEBUG_WRAP_CLIB = False

# Print information about delays that happen during Profiler.finish().
DEBUG_CRITICAL_PATH = False

# LIB_SAMPLE_CUDA_API = None
# if _e(_j(ROOT, 'Debug', RLSCOPE_LIB)):
#   LIB_SAMPLE_CUDA_API = _j(ROOT, 'Debug', RLSCOPE_LIB)
# elif _e(_j(ROOT, 'Release', RLSCOPE_LIB)):
#   LIB_SAMPLE_CUDA_API = _j(ROOT, 'Release', RLSCOPE_LIB)
# elif _e(_j(ROOT, 'build', RLSCOPE_LIB)):
#   LIB_SAMPLE_CUDA_API =  _j(ROOT, 'build', RLSCOPE_LIB)

# Use a custom-built/modified version of TF for benchmarking things.
# Modifies C++ code to make tfprof add less overhead to the critical path.
# CUSTOM_TF = True
# CUSTOM_TF = False

class _EnvVars:
  """
  Typed wrapper around os.environ

  # E.g.
  $ export DEBUG=yes
  # OR
  $ export DEBUG=true
  ...
  >>> EnvVars.get_bool('DEBUG')
  True

  # E.g.
  $ export DEBUG=0
  # OR
  $ export DEBUG=false
  ...
  >>> EnvVars.get_bool('DEBUG')
  False
  """
  def __init__(self, env=None):
    if env is None:
      env = os.environ
    self.env = env

  def _as_bool(self, string):
    string = string.lower()
    if string in {'0', 'no', 'false', 'None'}:
      return False
    return True

  def _as_int(self, string):
    return int(string)

  def _as_float(self, string):
    return float(string)

  def _as_string(self, string):
    return string

  def _as_number(self, string):
    try:
      val = int(string)
      return val
    except ValueError:
      pass

    val = float(string)
    return val

  def _get_typed(self, var, converter, dflt):
    if var not in self.env:
      return dflt
    string = self.env[var]
    val = converter(string)
    return val

  def get_string(self, var, dflt=None):
    val = self._get_typed(var, self._as_string, dflt)
    return val

  def get_bool(self, var, dflt=False):
    val = self._get_typed(var, self._as_bool, dflt)
    return val

  def get_int(self, var, dflt=0):
    val = self._get_typed(var, self._as_int, dflt)
    return val

  def get_float(self, var, dflt=0.):
    val = self._get_typed(var, self._as_float, dflt)
    return val

  def get_number(self, var, dflt=0.):
    val = self._get_typed(var, self._as_number, dflt)
    return val
EnvVars = _EnvVars(env=ENV)

NUMBA_DISABLE_JIT = EnvVars.get_bool('NUMBA_DISABLE_JIT', dflt=False)

RLSCOPE_DISABLE_JIT = EnvVars.get_bool('RLSCOPE_DISABLE_JIT', dflt=False)

# Use numbafied implementation of event overlap computation.
RLSCOPE_USE_NUMBA = EnvVars.get_bool('RLSCOPE_USE_NUMBA', dflt=False)
# More verbose debugging information during unit-tests.
RLSCOPE_DEBUG_UNIT_TESTS = EnvVars.get_bool('RLSCOPE_DEBUG_UNIT_TESTS', dflt=False)
RLSCOPE_DEBUG_UNIQUE_SPLITS_BASE = EnvVars.get_string('RLSCOPE_DEBUG_UNIQUE_SPLITS_BASE', dflt=None)

HOST_INSTALL_PREFIX = _j(ROOT, "local.host")
DOCKER_INSTALL_PREFIX = _j(ROOT, "local.docker")

HOST_BUILD_PREFIX = _j(ROOT, "build.host")
DOCKER_BUILD_PREFIX = _j(ROOT, "build.docker")

# NOTE: Don't touch this, it gets set manually by unit-tests.
IS_UNIT_TEST = False

# NUMPY_TIME_USEC_TYPE = np.uint64
# NUMBA_TIME_USEC_TYPE = nb.uint64
#
# NUMPY_CATEGORY_KEY_TYPE = np.uint64
# NUMBA_CATEGORY_KEY_TYPE = nb.uint64

# NOTE: numba likes to assume np.int64 as the default platform type when dealing with integers
# (e.g. indices in a "for in range(n)" loop, "x = 0" statements).
# As a result, attempts to use np.uint64 result in lots of compilation errors.
# I haven't bothered too hard to try to fix them... hopefully an int64 is big enough for our purposes...
# should probably add a check for that!

if USE_NUMBA:
  import numpy as np
  NUMPY_TIME_USEC_TYPE = np.int64
  NUMBA_TIME_USEC_TYPE = nb.int64

  NUMPY_TIME_PSEC_TYPE = np.int64
  NUMBA_TIME_PSEC_TYPE = nb.int64

  NUMPY_CATEGORY_KEY_TYPE = np.int64
  NUMBA_CATEGORY_KEY_TYPE = nb.int64

def is_development_mode():
  return shutil.which('rlscope-is-development-mode') is not None

def is_production_mode():
  return not is_development_mode()

def yes_as_bool(yes_or_no):
  if yes_or_no.lower() in {'yes', 'y', 'on', '1'}:
    return True
  return False

def is_running_unit_tests():
  return yes_as_bool(os.environ.get('RLS_RUNNING_UNIT_TESTS', 'no'))

def read_rlscope_version():
  lines = []
  with open(VERSION_TXT) as f:
      for line in f:
        line = line.rstrip()
        if re.search(r'^\s*#.*', line):
          continue
        lines.append(line)
  if len(lines) == 0:
    return None
  return ''.join(lines)

if 'pytest' in sys.modules and 'RLS_RUNNING_UNIT_TESTS' not in os.environ:
  # Prevent users from running with "pytest" so we can detect in-code if we're running with pytest.
  # Useful for avoiding loading modules where optional imports may be used (e.g., "import tensorflow").
  # logger.error(textwrap.dedent("""
  sys.stderr.write(textwrap.dedent("""\
  Don't run \"pytest\" directly; instead:

    # Run Python unit tests using pytest:
    $ rls-unit-tests --tests py

    # Or, run Python and C++ unit tests:
    $ rls-unit-tests
  """))
  sys.exit(1)


from datetime import datetime
import time
import webbrowser

import httpx

from client_refresh_flow import RefreshFlow

def login(client):
    info = client.post("/authorize").json()
    print(f"""You have {info['expires_in']} seconds to enter the code

    {info['user_code']}

after authorizing at the URL:

    {info['authorization_uri']}
""")
    webbrowser.open(info["authorization_uri"])  # Try to open a web browser.
    deadline = datetime.now().timestamp() + info["expires_in"]

    # Poll the /token endpoing waiting for this pending session to be verified.
    print("Waiting...", end="", flush=True)
    while datetime.now().timestamp() < deadline:
        response = client.post("/token", data={"device_code": info["device_code"]})
        print(".", end="", flush=True)
        if response.status_code == 200:
            print("\nLogged in!")
            break
        time.sleep(info["interval"])
    tokens = response.json()
    client.auth = RefreshFlow(tokens, f"{client.base_url}/refresh")
    return client

if __name__ == "__main__":
    import time

    client = httpx.Client(base_url="http://localhost:8000")
    login(client)
    print(client.get("/data"))
    print(client.get("/data").json())


import io
from mnist.model import LogisticRegression
import boto3
import numpy as np
import os
import torch
from mnist.configuration import Configuration
from PIL import Image
import torchvision.transforms as transforms

def download_model_artifacts(config: Configuration):
    """
    Downloads model artifact from S3
    """
    model_name, model_path = config.model_artifact.split("/")[-1], config.model_artifact

    boto3_client = boto3.client(
        "s3",
        region_name=config.s3["region_name"],
        aws_access_key_id=config.s3["access_key"],
        aws_secret_access_key=config.s3["secret_key"],
    )
    boto3_client.download_file(config.s3["bucket"], model_name, model_path)

def load_model(config: Configuration):

    if not os.path.exists(config.model_artifact):
        download_model_artifacts(config)

    model = LogisticRegression()
    model.load_state_dict(torch.load(config.model_artifact))
    model.eval()
    return model

def transform_image(image_bytes):
    image = Image.open(io.BytesIO(image_bytes)).convert("L")
    image = np.asarray(transforms.Resize(28)(image))
    image = image.reshape(784) / 255
    return torch.Tensor(image)


#Get Details from Spotify API using Spotipy.
# Details we are trying to get: 1. Playlist of the user. 2. Songs in Playlist 3. Analysis of Songs in the playlist.#

import os
import spotipy
import webbrowser
import json,csv
import pandas as pd
from json.decoder import JSONDecodeError
import spotipy.util as util
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
#Importing Secure Keys using ENV (dotenv) Module
load_dotenv()

#User Credentials
client_id=os.getenv('SPOTIFY_CLIENT_ID')
client_secret=os.getenv('SPOTIFY_CLIENT_SECRET')
redirect_uri=os.getenv('REDIRECT_URI')
#Spotify Token Authentication
scope = os.getenv('SCOPES')

final_extract=".\\final_data\\extract_data\\"
final_stream='.\\final_data\\StreamingHistory\\'
spotify_data='.\\spotify_data\\vijayasai\\'

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id,client_secret=client_secret,
                                                redirect_uri=redirect_uri, scope=scope))

# # Prints the Current User's Details
# user=sp.current_user()
# print(json.dumps(user,indent=2))

#Gets Current Song played by the user

current_song=sp.current_user_playing_track()
print(current_song)
if(current_song==None):
    print("No Song is Playing Right Now! Please check your Spotify Again!!")
else:
    with open('current_song.json','w') as current_song_output:
        json.dump(current_song,current_song_output)
    print("Name of Current Song: "+current_song['item']['name']+
        "\nURI of the Song: "+current_song['item']['uri'])
    uri_current_song = current_song['item']['uri']
    audio_analysis= sp.audio_features(tracks=uri_current_song)
    # with open(os.path.join(final_extract,'current_song_analysis.json'),'w') as current_song_output:
    #     json.dump(audio_analysis,current_song_output)
    # print("Audio Analysis for current song extracted successfully")

current_song_cover=current_song["item"]["album"]["images"][0]["url"]
print(current_song_cover)

# Getting top tracks of the user
# top_tracks=sp.current_user_top_tracks(limit=20, offset=0, time_range='long_term')
# with open(os.path.join(final_extract,'top_tracks_long.json'),'w') as current_song_output:
#     json.dump(top_tracks, current_song_output)
# top_tracks_json=[]
# for i in range(len(top_tracks["items"])):
#     top_tracks_json.append(dict(trackName=top_tracks["items"][i]["name"],
#                                     trackURI=top_tracks["items"][i]["uri"]))
#     with open(os.path.join(final_extract,'top_tracks.json'),'w') as outfile:
#         json.dump(top_tracks_json, outfile)

# results = sp.current_user_saved_tracks()
# for idx, item in enumerate(results['items']):
#     track = item['track']
#     print(idx, track['artists'][0]['name'], " – ", track['name']," - ", track['id'])
# list_playlists=sp.current_user_playlists(limit=50, offset=0)
# print(json.dumps(list_playlists,indent=4))

# items_in_playlists=sp.playlist_items('2XIivRPHOyhKkm8FkphG9V', fields="items(track(name,id))", limit=100, offset=0, market=None, additional_types=('track', 'episode'))
# print(type(items_in_playlists))
# print(json.dumps(items_in_playlists,indent=4))
# print(items_in_playlists["items"][i]["track"]["id"])
# print(sp.audio_features(tracks=[items_in_playlists["items"][i]["track"]["id"]]),fields="danceability,energy,loudness,speechiness,acousticness")


#!/usr/bin/python2.7
# coding=utf-8
"""
Created on 2016-3-3

@author: Jay
"""

# 导航栏名字ID列表
LOAN_NAV_BAR_ID_LS = [
    # index
    LBL_ROOT,
    LBL_KNOWLEDGE,
    LBL_CASE,
    LBL_PROBLEM,
    LBL_LOGIN,

    # about
    LBL_ABOUT,
    LBL_CONTACT,
    LBL_COOPERATION,
    LBL_RECRUIT,

    # user
    LBL_U_BUSIMGR,          # 业务管理
    LBL_U_DATA,             # 个人资料
    LBL_U_CO_EMAIL,         # 公司邮箱
    LBL_U_NOTICE,           # 网站公告

    # user - 业务员管理
    LBL_UB_LIST,            # 业务列表
    LBL_UB_APPLY,           # 提交贷款
] = xrange(0, 15)

# INDEX导航栏详细信息列表
INDEX_NAV_BAR_DIC = {
    # index
    LBL_ROOT:       {"idx": 0, "des": "首页",     "url": "/",                "html": "/index/index.html"},
    LBL_KNOWLEDGE:  {"idx": 1, "des": "贷款攻略", "url": "/knowledge",        "html": "/util/list.html"},
    LBL_CASE:       {"idx": 2, "des": "贷款案例", "url": "/case",             "html": "/util/list.html"},
    LBL_PROBLEM:    {"idx": 3, "des": "常见问题", "url": "/problem",          "html": "/util/list.html"},
    LBL_ABOUT:      {"idx": 4, "des": "关于宏圆", "url": "/about",            "html": "/about/about.html"},
    LBL_LOGIN:      {"idx": 5, "des": "经理登录", "url": "/login",            "html": "/user/login.html"},
}

# ABOUT导航栏详细信息列表
ABOUT_NAV_BAR_DIC = {
    LBL_ABOUT:      {"idx": 0, "des": "关于我们", "url": "/about",            "html": "/about/about.html"},
    LBL_CONTACT:    {"idx": 1, "des": "联系我们", "url": "/about/contact",     "html": "/about/contact.html"},
    LBL_COOPERATION:{"idx": 2, "des": "商务合作", "url": "/about/cooperation", "html": "/about/cooperation.html"},
    LBL_RECRUIT:    {"idx": 3, "des": "招贤纳士", "url": "/about/recruit",     "html": "/about/recruit.html"},
}

# 用户管理信息列表
USER_NAV_BAR_DIC = {
    LBL_U_BUSIMGR:  {"idx": 0, "des": "业务管理", "url": "/u_busimgr"},
    LBL_U_DATA:     {"idx": 1, "des": "个人资料", "url": "/u_data",          "html": "/user/u_data.html"},
    LBL_U_CO_EMAIL: {"idx": 2, "des": "公司邮箱", "url": "/u_co_email",      "html": "/user/u_co_mail.html"},
    LBL_U_NOTICE:   {"idx": 3, "des": "网站公告", "url": "/u_notice",        "html": "/user/u_notice.html"},
}

# 用户管理-业务管理信息列表
USER_BUSIMGR_BAR_DIC = {
    LBL_UB_LIST:   {"idx": 0, "des": "业务列表", "url": "/u_busimgr/list",    "html": "/user/u_busimgr_list.html"},
    LBL_UB_APPLY:  {"idx": 1, "des": "提交贷款", "url": "/u_busimgr/apply",   "html": "/user/u_busimgr_apply.html"},
}

LOAN_NAV_BAR_DIC = {}
LOAN_NAV_BAR_DIC.update(INDEX_NAV_BAR_DIC)
LOAN_NAV_BAR_DIC.update(ABOUT_NAV_BAR_DIC)
LOAN_NAV_BAR_DIC.update(USER_NAV_BAR_DIC)
LOAN_NAV_BAR_DIC.update(USER_BUSIMGR_BAR_DIC)


"""
Pascal's triangle is a triangular array of integers constructed with the
following formula:

The first row consists of the number 1.
For each subsequent row, each element is the sum of the numbers directly
above it, on either side.
For example, here are the first few rows:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Given an input k, return the kth row of Pascal's triangle.
"""

def pascal(k):
    triangle = [[0,1,0]]
    bottomRow = [0,1,0]
    for i in range(k-1):
        newRow = [0]
        for n in range(len(bottomRow)-1):
            newRow.append(bottomRow[n]+bottomRow[n+1])
        newRow.append(0)
        bottomRow = newRow
    return bottomRow[1:-1]

print(pascal(5))


import torch
from lime.lime_text import LimeTextExplainer

from methods.default import DefaultMethod
import math

import numpy as np

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

class LimeExplainer(DefaultMethod):

    def __init__(self, model, dataset, threshold, model_type="transformer_foreginer", query="What is the sentiment of this review?", class_names=['negative', 'positive']):
        super(LimeExplainer, self).__init__(model, dataset, threshold, query)
        self._J=0
        self.expl = LimeTextExplainer(class_names=class_names,split_expression=" ", bow=False, random_state=42)
        self.model_type = model_type

    def scores(self, dataset):

        features, attentions = [], []
        labels = torch.ones(1, dtype=torch.int64).to(self.model.device)
        inputs = dataset['text']

        inputs_in , always_keep_masks, kept_tokens = self.model.create_batchs(inputs, 1, self.query)

        metadatas = []
        if self.query != None:
            query_tokens = ["[SEP]"] + self.query.split() + ["[SEP]"]
        else:
            query_tokens = ["[SEP]"]
        for i in range(len(inputs)):
            metadata = {}
            always_keep_mask = np.array(always_keep_masks[i])
            metadata['always_keep_mask'] = always_keep_mask
            metadata['convert_tokens_to_instance'] = self.model.convert_tokens_to_instance
            metadata['tokens'] = list(filter(lambda x: bool(len(x)), inputs[i].strip().split(' '))) + query_tokens
            metadatas.append(metadata)

        attentions = []
        predicted_labels = []
        probabilite = []
        for batch in inputs_in:
            outputs = self.model.forward(document=batch, metadata=None, label=labels)
            predicted_labels.extend(outputs["predicted_labels"].detach().cpu())
            probabilite.extend(outputs["probs"].detach().cpu())
            del outputs
            torch.cuda.empty_cache()

        def predic_prob(inp_texts):

            filtered_tokens = [
                    [int(t) for t in selected_tokens.split(" ") if t != "UNKWORDZ"] for selected_tokens in inp_texts
                ]

            #inputs_in, _, _ = create_batch(inp_texts, 1, inp_stream)
            probas = []
            for i in range(len(filtered_tokens)):

                document = self.model.lime_tokenizer(metadatas[self._J], always_keep_masks[self._J], filtered_tokens[i])
                outputs = self.model.forward(document=document, metadata=None, label=torch.ones(1, dtype=torch.int64).to(self.model.device))
                probas.append(outputs["probs"].detach().cpu())

                del outputs
                torch.cuda.empty_cache()

            probas = torch.cat(probas, dim=0)
            return probas

        indice=0
        self._J=0
        for row in inputs:
            logging.info("INPUT "+str(indice))
            predicted_label = predicted_labels[indice].cpu().tolist()

            always_keep_mask = always_keep_masks[indice]

            selection_tokens = [i for i, x in enumerate(always_keep_mask) if x != 1]

            num_features = math.ceil(self._threshold * len(selection_tokens))

            explanation = self.expl.explain_instance(
                " ".join([str(i) for i in selection_tokens]),
                predic_prob,
                num_features=min(len(selection_tokens), num_features*2),
                labels=(predicted_label,),
                num_samples=1000,
            )

            weights=explanation.as_list(predicted_label)

            saliency = [0.0 for _ in range(len(metadatas[self._J]['tokens']))]
            for f, w in weights:
                saliency[int(f)] = max(0.0, w)

            #Calculate the attention
            saliency = torch.Tensor([saliency]).to(self.model.device)
            attention = saliency
            k_p = torch.zeros(kept_tokens[indice].shape[1] - attention.shape[1]).to(self.model.device)
            attention = torch.cat((attention[0], k_p)).unsqueeze(0).to(self.model.device)

            attn1 = attention * (1 - kept_tokens[indice].to(self.model.device)).float()
            inter_attn = attn1 / attn1.sum(-1, keepdim=True)
            attentions.append(inter_attn.cpu().data.tolist())
            indice+=1
            self._J+=1

        score = [item for sublist in attentions for item in sublist]

        return {"features":features, "scores":score, "predicted_labels": predicted_labels, "probas": probabilite, "always_keep_masks": always_keep_masks, "metadatas":metadatas}

    def visualize(self, dataset, top_k=None):
        inputs = dataset["text"]
        labels = torch.tensor(dataset["label"]).to(self.model.device)

        inputs_in , always_keep_masks, kept_tokens = self.model.create_batchs(inputs, 1, self.query)

        metadatas = []
        if self.query != None:
            query_tokens = ["SEP"] + self.query.split() + ["SEP"]
        else:
            query_tokens = []
        for i in range(len(inputs)):
            metadata = {}
            always_keep_mask = np.array(always_keep_masks[i])
            metadata['always_keep_mask'] = always_keep_mask
            metadata['convert_tokens_to_instance'] = self.model.convert_tokens_to_instance
            metadata['tokens'] = list(filter(lambda x: bool(len(x)), inputs[i].strip().split(' '))) + query_tokens
            metadatas.append(metadata)

        predicted_labels = []
        i = 0
        for batch in inputs_in:
            outputs = self.model.forward(document=batch, metadata=None, label=labels[i])
            predicted_labels.extend(outputs["predicted_labels"].detach().cpu())
            del outputs
            torch.cuda.empty_cache()
            i+=1

        def predic_prob(inp_texts):
            filtered_tokens = [
                    [t for t in selected_tokens.split(" ") if t != "UNKWORDZ"] for selected_tokens in inp_texts
                ]

            #inputs_in, _, _ = create_batch(inp_texts, 1, inp_stream)
            probas = []
            for i in range(len(filtered_tokens)):

                document = self.model.lime_tokenizer_viz(metadatas[self._J], always_keep_masks[self._J], filtered_tokens[i])
                outputs = self.model.forward(document=document, metadata=None, label=torch.ones(1, dtype=torch.int64).to(self.model.device))
                probas.extend(outputs["probs"].detach().cpu().tolist())

                del outputs
                torch.cuda.empty_cache()

            return np.array(probas)

        self._J=0
        for inp in inputs:
            predicted_label = predicted_labels[self._J].cpu().tolist()

            always_keep_mask = always_keep_masks[self._J]

            selection_tokens = [i for i, x in enumerate(always_keep_mask) if x != 1]

            num_features = math.ceil(self._threshold * len(selection_tokens))

            explanation = self.expl.explain_instance(
                    inp,
                    predic_prob,
                    num_features=min(len(selection_tokens), num_features*2),
                    labels=(predicted_label,),
                    num_samples=1000,
                )

            explanation.show_in_notebook()
            explanation.save_to_file("output_"+str(self._J)+".html", labels=(predicted_label,))

            self._J+=1


from collections import defaultdict

memo = defaultdict(int)

red_tile = 2
green_tile = 3
blue_tile = 4

def calculate(place):
    if red_tile > place:
        return 1
    if memo[place]:
        return memo[place]

    place_red = calculate(place - red_tile)
    place_green, place_blue = 0, 0

    if green_tile <= place:
        place_green = calculate(place - green_tile)
    if blue_tile <= place:
        place_blue = calculate(place - blue_tile)

    skip_square = calculate(place - 1)

    r = place_red + place_green + place_blue + skip_square
    memo[place] = r
    return r

print(calculate(50))


import matplotlib.pyplot as plt

f = open("Utility_vs_Runtime.txt","r")
y = []
x = []
param = []
line = []
lines = f.readlines()
for i in lines:
	line = i.split(",")
	param.append(float(line[0]))
	x.append(round(float(line[1]),3))
	y.append(round(float(line[2][:-1]),1))
f.close()

fig, (ax1,ax2) = plt.subplots(nrows =1, ncols =2)
ax1.set_title("Sub-sampling frames: Utility runtime tradeoff")
ax1.set_ylabel("Run-time")
ax1.set_xlabel("Utility")
ax1.scatter(x, y, label='Data')
ax1.plot(x, y, linestyle = 'dashed')
for i in range(len(x)):
	ax1.annotate(" (" + str(x[i]) + "," + str(y[i]) + ")" ,(x[i], y[i]), horizontalalignment='right')
ax1.grid()

ax2.set_title("Sub-sampling frames: Runtime vs Sub-sampling parameter")
ax2.set_ylabel("Run-time")
ax2.set_xlabel("Sub-sampling parameter")
ax2.scatter(param,y, label='Data')
ax2.plot(param,y, linestyle = 'dashed')
for i in range(len(y)):
	ax2.annotate(" (" + str(param[i]) + "," + str(y[i]) + ")" ,(param[i], y[i]), horizontalalignment='right')
ax2.grid()
plt.tight_layout()
plt.show()


from django.contrib import admin

# Register your models here.
from .models import Transaction, TransactionDetails

class DetailInline(admin.TabularInline):
    model = TransactionDetails

class TransactionAdmin(admin.ModelAdmin):
    list_display = ('shopper', 'total_price', 'payment_id', 'is_valid')
    inlines = [DetailInline]

admin.site.register(Transaction, TransactionAdmin)


import numpy as np
import pandas as pd

from bld.project_paths import project_paths_join as ppj

VARIABLE_NAMES_PEQUIV = {
    # 'cid': 'ID_ORIGINAL_HH',  # Case id, id of original household
    "syear": "YEAR",  # survey year
    # 'hid': 'ID_HH',  # current household id
    "pid": "ID",  # permanent personal id
    "d11109": "YEARS_EDUCATION",
}

RETAINED_COLUMNS_PEQUIV = list(VARIABLE_NAMES_PEQUIV.keys())

def main():
    df = pd.read_stata(ppj("IN_DATA", "pequiv.dta"), columns=RETAINED_COLUMNS_PEQUIV)
    df = df.rename(columns=VARIABLE_NAMES_PEQUIV)

    df.loc[df.YEARS_EDUCATION < 0, "YEARS_EDUCATION"] = np.nan

    df.YEARS_EDUCATION = df.groupby("ID").YEARS_EDUCATION.transform(
        lambda x: x.fillna(method="ffill")
    )

    df.to_pickle(ppj("OUT_DATA", "edu_years.pkl"))

if __name__ == "__main__":
    main()


from builtins import set
from datasets import load_dataset
from collections import Counter
import sys

def static_vars(**kwargs):
    def decorate(func):
        for k in kwargs:
            setattr(func, k, kwargs[k])
        return func
    return decorate

@static_vars(dataset=load_dataset("ChristophSchuhmann/MS_COCO_2017_URL_TEXT"))
def find_image(sentence):
    urls = find_image.dataset["train"]["URL"]
    texts = find_image.dataset["train"]["TEXT"]

    results = []
    for idx, i in enumerate(texts):
        if sentence in i.lower():
            results.append(urls[idx])
    return results

def find_image_multi(sentences):
    urls = find_image.dataset["train"]["URL"]
    texts = find_image.dataset["train"]["TEXT"]
    sentence = sentences[0]
    results = Counter(find_image(sentence))
    for sentence_index in range(1,len(sentences)):
        results += Counter(find_image(sentences[sentence_index]))
    return(results.most_common(1)[0][0])

def build_caption_to_image_dict(dataset_folder):
    import json
    captions = json.load(open(f"{dataset_folder}/annotations/captions_train2017.json"))
    id2filename = dict()
    for image in captions["images"]:
        id2filename[image["id"]] = f'{dataset_folder}/train2017/{image["file_name"]}'
    ret = {i['caption'].lower(): id2filename[i['image_id']] for i in captions["annotations"]}
    captions = json.load(open(f"{dataset_folder}/annotations/captions_val2017.json"))
    id2filename = dict()
    for image in captions["images"]:
        id2filename[image["id"]] = f'{dataset_folder}/val2017/{image["file_name"]}'
    ret.update({i['caption'].lower(): id2filename[i['image_id']] for i in captions["annotations"]})
    return ret

if __name__ == "__main__":
    if len(sys.argv) > 1:
        sentences = [sys.argv[1]]
    else:
        sentences = ['a very clean white bathroom with blue', 'a bathroom vanity mirror above a bathroom', 'bathroom sink with mirror , lighting ,', 'a bathroom sink area is lit up', 'a bathroom with a white counter top']

    print(find_image_multi(sentences))


from . import movee
import asyncio
import sys
import traceback

def main(args=None):  # pragma: no cover
    try:
        asyncio.run(movee.movee(args))
        return 0
    except Exception as e:
        print('ERROR:', e, file=sys.stderr)
        if args.verbose:
            traceback.print_exc()
        return -1

if __name__ == '__main__':
    sys.exit(main())


from app import db

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    cparent = db.Column(db.Integer(11), db.ForeignKey('category.idn'))

    children = db.relationship(
        'Category',
        # cascade deletions
        cascade="all",

        # many to one + adjacency list - remote_side
        # is required to reference the 'remote'
        # column in the join condition.
        backref=backref("parent", remote_side='category.id'),

        # children will be represented as a dictionary
        # on the "name" attribute.
        collection_class=attribute_mapped_collection('name'),
        lazy="joined",
        join_depth=3,
    )
    product = db.relationship(
        'Product',
        collection_class=list,
        backref=backref("category", remote_side='category.id'),
        lazy="joined",
        join_depth=3, # так как продукты у меня связаны только с третьим уровнем.
    )

    def __init__(self, name, parent=None):
        self.name = name
        self.parent = parent

class Product(db.Model):
    id = db.Column(db.Integer(11), primary_key=True)
    id_cat = db.Column(db.Integer(11), db.ForeignKey('category.id'))
    cat = db.relationship("Category")
    data = db.Column(db.String(255))

# root_node = session.query(Category).filter(
#     Category.name == root_node_name).first()


import socket, threading, os, struct,selectors#,atexit
from pathlib import Path
from tkinter import *
import queue # thread-safe

Q = queue.Queue()
sel = selectors.DefaultSelector()

serv_fd = None
CLIENT_PORT = 9999
MAX_MSG = 4096
last_cmd = ''
last_print = ''

class CleanExit:
  pass

class TextEditor:
	#@staticmethod
	def quit_app(self, event=None):
		os.remove(self.fname)
		self.root.destroy()

	def save_file(self, event=None):
		fil_path = self.fname
		content = self.text_area.get('1.0', END + '-1c')
		if(content[-1:] != '\n'):
			content += '\n'
		self.fd.sendall(('updt '+fil_path+';'+content+'Āā').encode())
		self.quit_app(self)

	def __init__(self, root, fname,fd):
		self.fname = fname
		self.root = root
		self.fd = fd
		self.text_to_write = ""
		root.title("Text Editor")
		root.geometry("600x550")
		frame = Frame(root, width=600, height=550)
		scrollbar = Scrollbar(frame)
		self.text_area = Text(frame, width=600, height=550,
				        yscrollcommand=scrollbar.set,
				        padx=10, pady=10)
		scrollbar.config(command=self.text_area.yview)
		scrollbar.pack(side="right", fill="y")
		self.text_area.pack(side="left", fill="both", expand=True)
		frame.pack()
		the_menu = Menu(root)
		file_menu = Menu(the_menu, tearoff=0)
		file_menu.add_command(label="Save", command=self.save_file)
		file_menu.add_separator()
		file_menu.add_command(label="Quit (Unsaved)", command=self.quit_app)
		the_menu.add_cascade(label="Option", menu=file_menu)
		root.config(menu=the_menu)
		self.text_area.delete(1.0, END)
		with open(fname) as _file:
			self.text_area.insert(1.0, _file.read())
		root.update_idletasks()

def recCliMsg(fd):
	sel.register(fd, selectors.EVENT_READ)
	global last_cmd
	global last_print
	cmd = ''
	while True:
		print('<cmd>: ',end='',flush=True)
		events = sel.select()
		for key, mask in events:
			#print(key)
			#print(key.fd)
			#print(fd)
			if key.fd==fd.fileno():
				try:
					data = fd.recv(MAX_MSG).decode()
				except ConnectionResetError:
					data = ''
				if(len(data)>0):
					if(data[1:4]=='wr%'):#1 cause cmdParse sends \n
						tpath=data[3:data.find(';')]
						while(data[-2:]!='Āā'):
							data +=fd.recv(MAX_MSG).decode()
						try:
							with open(tpath, 'x') as f:
								f.write(data[data.find(';')+1:-2])
							print('\nFile Opened')
							if(os.fork()==0):
								root = Tk()
								text_editor = TextEditor(root, tpath, fd)
								root.mainloop()
								sel.close()
								Q.put('quit')
								return
								#os.execvp('gedit',['gedit', tpath])
						except Exception as e:
							print('\nUpdate File First')
							continue
					elif(data[1:4]=='con'):
						#print('Connecting To '+data[4:])
						sel.unregister(fd)
						last_cmd = cmd
						Q.put('discon')#data[4:])
						return
					else:
						print(data)
				else:
					print('\nConnection Closed.')
					sel.unregister(fd)
					last_cmd = ''
					Q.put('close')
					#ipq.put(CleanExit)
					return
			elif key.fd ==0:
				last_cmd = cmd
				cmd = input()
				if cmd == "close" or cmd == 'exit':
					sel.close()
					Q.put('quit')
					return
				elif cmd[:5] == 'updt ':
					fil_path ='%'+cmd[5:].replace('/','%')
					exp=False
					try:
						content = Path(fil_path).read_text()
						os.remove(fil_path)
					except Exception as e:
						exp = True
						print(e)
					if(exp==True):
						continue
					fd.sendall(('updt '+fil_path+';'+content+'Āā').encode())
				else:
					fd.sendall(cmd.encode())

def MakeSock():
	global serv_fd
	serv_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	serv_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	serv_fd.settimeout(4)
	serv_fd.bind(('', CLIENT_PORT))
	serv_fd.listen(0)

def main():
	print(r'''
	       .--.                   .---.
	   .---|__|           .-.     |~~~|
	.--|===|--|_          |_|     |~~~|--.
	|  |===|  |'\     .---!~|  .--|   |--|
	|%%| D |  |.'\    |===| |--|%%|   |  |
	|%%| F |  |\.'\   |   | |__|  |   |  |
	|  | S |  | \  \  |===| |==|  |   |  |
	|  |   |__|  \.'\ |   |_|__|  |~~~|__|
	|  |===|--|   \.'\|===|~|--|%%|~~~|--|
	^--^---'--^    `-'`---^-^--^--^---'--'
	Connected To
		     Peek-achu
			       File System
''')
	sel.register(0, selectors.EVENT_READ) #input
	global serv_fd
	#-----------------------------------------------------------
	multicast_group = ('224.4.255.255', 50000)
	mult_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	mult_sock.settimeout(0.2)
	ttl = struct.pack('b', 1)
	mult_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
	mult_sock.sendto(b'cli', multicast_group)
	#-----------------------------------------------------------
	MakeSock()
	while(True):
		while(True):
			if(last_cmd == ''):
				print('Connecting To System...')
			try:
				conn, addr = serv_fd.accept()
			except socket.timeout:
				mult_sock.sendto(b'cli', multicast_group)
			else:
				serv_fd.shutdown(socket.SHUT_RDWR)
				serv_fd.close()
				break
				#print('Out of accept')
		if(last_cmd != ''):
			conn.sendall(last_cmd.encode())
		t1 = threading.Thread(target=recCliMsg, kwargs={'fd':conn})
		t1.daemon = True
		t1.start()
		data = Q.get()
		#print(data)
		conn.close()
		t1.join()
		if(data == 'quit'):
			mult_sock.close()
			sys.exit()
		elif(data == 'close'):
			MakeSock()
			mult_sock.sendto(b'cli', multicast_group)
		elif(data == 'discon'):
			MakeSock()
			continue

if __name__ == "__main__":
	main()


"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.index),
    url(r'^index/', views.index),
    url(r'^register/', views.register),
    url(r'^login/', views.login),
    url(r'^logout/', views.logout),
    url(r'^publish/', views.publish),
    url(r'^publish_text/', views.publish_text),
    url(r'^gain/', views.gain),
    url(r'^login_confirm/', views.login_confirm),
    url(r'^recommend/', views.recommend),
    url(r'^favorite/', views.favorite),
    url(r'^comment/', views.comment),
    url(r'^make_comment/', views.make_comment),
    url(r'^page/', views.page),
]


VOWELS = ['A', 'E', 'I', 'O', 'U']

def count_by_consonants(filename):
    """
    Reads in the file whose name is filename and returns a dictionary
    that contains counts of words that share the same consonants in order.
    """

    with open(filename) as f:
        file_list = []
        for line in f:
            line = line.strip()
            file_list.append(line)
    file_dict = {}
    for i in file_list:
        word = i
        vowel_free_word = ''
        for a in range(len(word)):
            if word[a].upper() not in VOWELS:
                vowel_free_word += word[a]

        if vowel_free_word not in file_dict:
            file_dict[vowel_free_word] = 1
        else:
            file_dict[vowel_free_word] += 1

    return file_dict

def main():
    filename = input("Filename: ")
    print(count_by_consonants(filename))

if __name__ == "__main__":
    main()


# Distributed under the terms of the MIT License.

"""Module for CG OpenMM optimizer.

Inspired by https://bitbucket.org/4dnucleome/md_soft/src/master/

"""

import logging
import pathlib
import time
from collections import abc

import numpy as np
import pandas as pd
import stk
from openmm import app, openmm

from .assigned_system import ForcedSystem
from .ensembles import Timestep
from .utilities import get_atom_distance

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(message)s",
)

class OMMTrajectory:
    def __init__(
        self,
        base_molecule: stk.Molecule,
        data_path: pathlib.Path,
        traj_path: pathlib.Path,
        output_path: pathlib.Path,
        temperature: float,
        random_seed: int,
        num_steps: int,
        time_step: float,
        friction: float | None,
        reporting_freq: float,
        traj_freq: float,
    ) -> None:
        self._base_molecule = base_molecule
        self._data_path = data_path
        self._traj_path = traj_path
        self._output_path = output_path
        self._temperature = temperature
        self._num_steps = num_steps
        self._time_step = time_step
        self._reporting_freq = reporting_freq
        self._traj_freq = traj_freq
        self._num_confs = int(self._num_steps / self._traj_freq)

        self._random_seed = random_seed
        self._friction = friction

    def get_data(self) -> pd.DataFrame:
        return pd.read_csv(self._data_path)

    def get_base_molecule(self) -> stk.Molecule:
        return self._base_molecule

    def yield_conformers(self) -> abc.Iterator[Timestep]:
        num_atoms = self._base_molecule.get_num_atoms()
        start_trigger = "MODEL"
        triggered = False
        end_trigger = "ENDMDL"
        model_number = 0
        new_pos_mat: list = []
        atom_trigger = "HETATM"

        with open(self._traj_path) as f:
            for line in f.readlines():
                if end_trigger in line:
                    if len(new_pos_mat) != num_atoms:
                        msg = (
                            f"num atoms ({num_atoms}) does not match size of "
                            f"collected position matrix ({len(new_pos_mat)})."
                        )
                        raise ValueError(msg)

                    yield Timestep(
                        molecule=(
                            self._base_molecule.with_position_matrix(
                                np.array(new_pos_mat)
                            )
                        ),
                        timestep=model_number * self._traj_freq,
                    )
                    new_pos_mat = []
                    triggered = False

                if start_trigger in line:
                    model_number = int(line.strip().split()[-1])
                    triggered = True

                if triggered and atom_trigger in line:
                    x = float(line[30:38])
                    y = float(line[38:46])
                    z = float(line[46:54])
                    new_pos_mat.append([x, y, z])

    def __str__(self) -> str:
        return (
            f"{self.__class__.__name__}(steps={self._num_steps}, "
            f"conformers={self._num_confs})"
        )

    def __repr__(self) -> str:
        return str(self)

class CGOMMOptimizer:
    def __init__(
        self,
        fileprefix: str,
        output_dir: pathlib.Path,
        max_iterations: int | None = None,
        atom_constraints: abc.Iterable[tuple[int, int]] | None = None,
        platform: str | None = None,
    ) -> None:
        self._fileprefix = fileprefix
        self._output_dir = output_dir
        self._output_path = output_dir / f"{fileprefix}_omm.out"
        self._output_string = ""
        self._properties: dict | None

        if max_iterations is None:
            self._max_iterations = 0
        else:
            self._max_iterations = max_iterations

        self._tolerance = 1e-6 * openmm.unit.kilojoules_per_mole

        if platform is not None:
            self._platform = openmm.Platform.getPlatformByName(platform)
            if platform == "CUDA":
                self._properties = {"CudaPrecision": "mixed"}
            else:
                self._properties = None
        else:
            self._platform = None
            self._properties = None

        self._atom_constraints = atom_constraints

        # Default integrator.
        time_step = 0.1 * openmm.unit.femtoseconds
        self._integrator = openmm.VerletIntegrator(time_step)

    def _group_forces(self, system: openmm.System) -> dict:
        """Method to group forces.

        From https://github.com/XiaojuanHu/CTPOL_MD/pull/4/files

        Use these two methods as:
        `fgrps=forcegroupify(system)`
        `
        tt= getEnergyDecomposition(simulation.context, fgrps)
        for idd in tt.keys():
            print(idd,tt[idd])
        print('\n')
        `

        """
        forcegroups = {}
        for i in range(system.getNumForces()):
            force = system.getForce(i)
            force.setForceGroup(i)
            forcegroups[force] = i
        return forcegroups

    def _get_energy_decomposition(
        self,
        context: openmm.Context,
        forcegroups: dict,
    ) -> dict:
        """Method to decompose energies.

        From https://github.com/XiaojuanHu/CTPOL_MD/pull/4/files

        """
        energies = {}
        for f, i in forcegroups.items():
            energies[(i, f.__class__.__name__)] = context.getState(
                getEnergy=True,
                groups=2**i,
            ).getPotentialEnergy()
        return energies

    def _add_atom_constraints(
        self,
        system: openmm.System,
        molecule: stk.Molecule,
    ) -> openmm.System:
        if self._atom_constraints is None:
            return system

        self._output_string += "   constraints applied:\n"
        for constraint in self._atom_constraints:
            current_distance = get_atom_distance(
                molecule=molecule,
                atom1_id=constraint[0],
                atom2_id=constraint[1],
            )
            system.addConstraint(
                particle1=constraint[0],
                particle2=constraint[1],
                distance=current_distance / 10,
            )
            self._output_string += (
                f"{constraint[0]} {constraint[1]} "
                f"{current_distance / 10} nm\n"
            )

        self._output_string += "\n"
        return system

    def _setup_simulation(
        self,
        assigned_system: ForcedSystem,
    ) -> tuple[app.Simulation, openmm.System]:
        system = assigned_system.get_openmm_system()
        topology = assigned_system.get_openmm_topology()
        if self._atom_constraints is not None:
            system = self._add_atom_constraints(
                system, assigned_system.molecule
            )

        for i, f in enumerate(system.getForces()):
            f.setForceGroup(i)

        # Define simulation.
        simulation = app.Simulation(
            topology,
            system,
            self._integrator,
            platform=self._platform,
            platformProperties=self._properties,
        )

        # Set positions from structure.
        simulation.context.setPositions(
            assigned_system.molecule.get_position_matrix() / 10
        )

        return simulation, system

    def _run_energy_decomp(
        self,
        simulation: app.Simulation,
        system: openmm.System,
    ) -> dict:
        fgroups = self._group_forces(system)
        egroups = self._get_energy_decomposition(
            context=simulation.context,
            forcegroups=fgroups,
        )

        energy_decomp = {
            "tot_energy": openmm.unit.quantity.Quantity(
                value=0,
                unit=openmm.unit.kilojoules_per_mole,
            )
        }
        for idd in egroups:
            energy_decomp[idd] = egroups[idd]
            energy_decomp["tot_energy"] += egroups[idd]

        return energy_decomp

    def _output_energy_decomp(
        self,
        simulation: app.Simulation,
        system: openmm.System,
    ) -> None:
        energy_decomp = self._run_energy_decomp(simulation, system)
        self._output_string += "energy decomposition:\n"

        for idd in energy_decomp:
            if idd == "tot_energy":
                continue
            self._output_string += f"{idd}: {energy_decomp[idd]}\n"
        self._output_string += f"total energy: {energy_decomp['tot_energy']}\n"
        self._output_string += "\n"

    def _get_energy(self, simulation: app.Simulation) -> float:
        state = simulation.context.getState(
            getPositions=True,
            getEnergy=True,
        )

        return state.getPotentialEnergy().in_units_of(
            openmm.unit.kilojoules_per_mole
        )

    def _minimize_energy(
        self,
        simulation: app.Simulation,
        system: openmm.System,
    ) -> app.Simulation:
        self._output_energy_decomp(simulation, system)

        self._output_string += "minimizing energy\n\n"
        simulation.minimizeEnergy(
            tolerance=self._tolerance,
            maxIterations=self._max_iterations,
        )
        self._output_energy_decomp(simulation, system)

        return simulation

    def _update_stk_molecule(
        self,
        molecule: stk.Molecule,
        simulation: app.Simulation,
    ) -> stk.Molecule:
        state = simulation.context.getState(getPositions=True, getEnergy=True)
        positions = state.getPositions(asNumpy=True)
        return molecule.with_position_matrix(positions * 10)

    def calculate_energy(self, assigned_system: ForcedSystem) -> float:
        simulation, _ = self._setup_simulation(assigned_system)
        return self._get_energy(simulation)

    def read_final_energy_decomposition(self) -> dict:
        decomp_data = (
            self._output_string.split("energy decomposition:")[-1]
            .split("end time:")[0]
            .split("\n")
        )
        decomposition = {}
        for i in decomp_data:
            if i == "":
                continue
            force, value_unit = i.split(":")
            value, unit = value_unit.split()
            value = float(value)  # type: ignore[assignment]
            decomposition[force] = (value, unit)
        return decomposition

    def optimize(self, assigned_system: ForcedSystem) -> stk.Molecule:
        start_time = time.time()
        self._output_string += f"start time: {start_time}\n"
        self._output_string += (
            f"atoms: {assigned_system.molecule.get_num_atoms()}\n"
        )

        simulation, system = self._setup_simulation(assigned_system)
        simulation = self._minimize_energy(simulation, system)
        molecule = self._update_stk_molecule(
            assigned_system.molecule, simulation
        )

        end_time = time.time()
        self._output_string += f"end time: {end_time}\n"
        total_time = end_time - start_time
        self._output_string += f"total time: {total_time} [s]\n"
        with open(self._output_path, "w") as f:
            f.write(self._output_string)
        return molecule

class CGOMMDynamics(CGOMMOptimizer):
    def __init__(
        self,
        fileprefix: str,
        output_dir: pathlib.Path,
        temperature: openmm.unit.Quantity,
        num_steps: int,
        time_step: openmm.unit.Quantity,
        friction: openmm.unit.Quantity,
        reporting_freq: float,
        traj_freq: float,
        random_seed: int | None = None,
        max_iterations: int | None = None,
        atom_constraints: abc.Iterable[tuple[int, int]] | None = None,
        platform: str | None = None,
    ) -> None:
        self._fileprefix = fileprefix
        self._output_dir = output_dir
        self._output_path = output_dir / f"{fileprefix}_omm.out"
        self._trajectory_data = output_dir / f"{fileprefix}_traj.dat"
        self._trajectory_file = output_dir / f"{fileprefix}_traj.pdb"

        self._output_string = ""
        if max_iterations is None:
            self._max_iterations = 0
        else:
            self._max_iterations = max_iterations

        self._tolerance = 1e-6 * openmm.unit.kilojoules_per_mole

        self._atom_constraints = atom_constraints

        self._temperature = temperature

        if random_seed is None:
            logging.info("a random random seed is used")
            self._random_seed = np.random.randint(1000)
        else:
            self._random_seed = random_seed

        self._num_steps = num_steps
        self._time_step = time_step
        self._friction = friction
        self._reporting_freq = reporting_freq
        self._traj_freq = traj_freq

        if platform is not None:
            self._platform = openmm.Platform.getPlatformByName(platform)
            if platform == "CUDA":
                self._properties = {"CudaPrecision": "mixed"}
            else:
                self._properties = None
        else:
            self._platform = None
            self._properties = None

        # Default integrator.
        self._integrator = openmm.LangevinIntegrator(
            self._temperature,
            self._friction,
            self._time_step,
        )
        self._integrator.setRandomNumberSeed(self._random_seed)

    def _add_trajectory_reporter(
        self,
        simulation: app.Simulation,
    ) -> app.Simulation:
        simulation.reporters.append(
            app.PDBReporter(
                file=str(self._trajectory_file),
                reportInterval=self._traj_freq,
            )
        )
        return simulation

    def _add_reporter(self, simulation: app.Simulation) -> app.Simulation:
        simulation.reporters.append(
            app.StateDataReporter(
                file=str(self._trajectory_data),
                reportInterval=self._reporting_freq,
                step=True,
                potentialEnergy=True,
                kineticEnergy=True,
                totalEnergy=False,
                temperature=True,
                volume=False,
                density=False,
                progress=False,
                remainingTime=False,
                speed=False,
                totalSteps=self._num_steps,
                separator=",",
            )
        )
        return simulation

    def _run_molecular_dynamics(
        self,
        simulation: app.Simulation,
    ) -> app.Simulation:
        self._output_string += "simulation parameters:\n"
        self._output_string += (
            f"initalise velocities at T={self._temperature}\n"
        )
        simulation.context.setVelocitiesToTemperature(
            self._temperature,
            self._random_seed,
        )

        total_time = self._num_steps * self._time_step
        tt_in_ns = total_time.in_units_of(openmm.unit.nanoseconds)
        tf_in_ns = self._traj_freq * self._time_step
        self._output_string += (
            f"steps: {self._num_steps}\n"
            f"time step: {self._time_step}\n"
            f"total time: {tt_in_ns}\n"
            f"report frequency: {self._reporting_freq}\n"
            f"trajectory frequency per step: {self._traj_freq}\n"
            f"trajectory frequency per ns: {tf_in_ns}\n"
            f"seed: {self._random_seed}\n"
        )

        self._output_string += "running simulation\n"
        simulation = self._add_reporter(simulation)
        simulation = self._add_trajectory_reporter(simulation)

        start = time.time()
        simulation.step(self._num_steps)
        end = time.time()
        speed = self._num_steps / (end - start)
        self._output_string += (
            f"done in {end-start} s ({round(speed, 2)} steps/s)\n\n"
        )

        return simulation

    def _get_trajectory(self, molecule: stk.Molecule) -> OMMTrajectory:
        return OMMTrajectory(
            base_molecule=molecule,
            data_path=self._trajectory_data,
            traj_path=self._trajectory_file,
            output_path=self._output_path,
            temperature=self._temperature,
            random_seed=self._random_seed,
            num_steps=self._num_steps,
            time_step=self._time_step,
            friction=self._friction,
            reporting_freq=self._reporting_freq,
            traj_freq=self._traj_freq,
        )

    def run_dynamics(self, assigned_system: ForcedSystem) -> OMMTrajectory:
        start_time = time.time()
        self._output_string += f"start time: {start_time}\n"
        self._output_string += (
            f"atoms: {assigned_system.molecule.get_num_atoms()}\n"
        )

        simulation, system = self._setup_simulation(assigned_system)
        simulation = self._minimize_energy(simulation, system)
        simulation = self._run_molecular_dynamics(simulation)

        self._output_energy_decomp(simulation, system)

        end_time = time.time()
        self._output_string += f"end time: {end_time}\n"
        total_time = end_time - start_time
        self._output_string += f"total time: {total_time} [s]\n"
        with open(self._output_path, "w") as f:
            f.write(self._output_string)

        return self._get_trajectory(assigned_system.molecule)


import numpy as np

def knn_graph(X, k, threshold):
    '''
    KNN_GRAPH Construct W using KNN graph

        Input:
            X - data point features, n-by-p maxtirx.
            k - number of nn.
            threshold
        Output:
            W - adjacency matrix, n-by-n matrix.
    '''

    # YOUR CODE HERE
    # begin answer
    N, P = X.shape
    sigma = 0.1
    W = np.zeros((N, N))
    for i in range(N):
        L2 = np.linalg.norm(X - X[i, :], axis=1)
        Idx = np.argsort(L2)[1:k+1]
        for j in Idx:
            similarity = np.exp(-np.sum(np.square(X[i, :] - X[j, :]))/(2*sigma**2))
            W[i, j] = W[j, i] = similarity if similarity > threshold else 0
    return W
    # end answer


from django.urls import reverse
from django.test import Client, TestCase

class PostListViewTest(TestCase):
    """
    Basic test case for PostList view.
    """
    def setUp(self):
        self.client = Client()

    def test_post_list_view(self):
        response = self.client.get(reverse('home'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'index.html')
        self.assertIn('object_list', response.context)


import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
import os
import codecs

def get_endpoint(x):

    return max(x), min(x)

def postprocessing(info):

    info = sorted(info.items(), key = lambda x:x[0])
    x = [xx[0] for xx in info]
    y = [yy[1] for yy in info]

    return x, y

def draw_his(dic, des):

    for phn in dic:
        info = dic[phn]
        x, y = postprocessing(info)
        x_max, x_min = get_endpoint(x)
        y_max, y_min = get_endpoint(y)

        fig, ax = plt.subplots()
        left = np.array(x)
        right = np.array(x)
        bottom = np.zeros(len(left))
        top = bottom + y

        XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T
        barpath = path.Path.make_compound_path_from_polys(XY)
        patch = patches.PathPatch(barpath)
        ax.add_patch(patch)

        ax.set_xlim(x[0], x[-1])
        ax.set_ylim(bottom.min(), top.max())
        plt.xlabel("Duration distribution of phone: {}".format(phn))
        plt.ylabel("Count")
        #plt.show()

        fig.savefig("{}/{}.png".format(des, phn))
        plt.close("all")

def statistics_phone(data):

    dic = {}

    for line in data:
        line = line.strip().split()
        phn = line[-1]
        dur = int((float(line[-2]) - float(line[-3])) * 1000)
        if phn not in dic:
            dic[phn] = {}
        section = dur/5
        if section not in dic[phn]:
            dic[phn][section] = 0
        dic[phn][section] += 1

    return dic

def draw(data, des):

    dic = statistics_phone(data)

    draw_his(dic, des)

def load(f):

    fp = codecs.open(f, "r", encoding = "utf8")
    data = fp.readlines()
    fp.close()

    return data

def main(ori, des):

    for f in os.listdir(ori):
        fp = ori + os.sep + f
        data = load(fp)
        draw(data, des)

if __name__ == "__main__":

    if len(sys.argv) != 3:
        print ("This script is for data statistics")
        print ("Usage: {} data info".format(sys.argv[0]))
        exit(1)

    main(sys.argv[1], sys.argv[2])