import sys

lines = []
for line in sys.stdin:
    if 'Exit' == line.rstrip():
        break
    lines.append(line)

inputStr = lines[0].rstrip()

# minimun number of letters removed so that it becomes a peragram
# peragram is basically just any character needs to be there twice, except one
wordSet = set()
for char in inputStr:
    if char in wordSet:
        wordSet.remove(char)
    else:
        wordSet.add(char)

if len(wordSet) > 0:
    print(len(wordSet)-1)
else:
    print("0")


MIN_VV_VALUE = -25
MIN_VH_VALUE = -32.5
MIN_DEM_VALUE = -450
MAX_DEM_VALUE = 9000

cgls_simplified_mapping = {
    0: 0,  # unknown
    20: 1,  # shrubs
    30: 2,  # herbaceaous vegetation/grassland
    40: 3,  # cropland
    50: 4,  # built up
    60: 5,  # bare/sparse
    70: 6,  # ice/snow
    80: 7,  # water
    90: 8,  # wetlands
    100: 5,  # moss/lichen -> barren
    111: 9,
    112: 9,
    113: 9,
    114: 9,
    115: 9,
    116: 9,  # forest
    121: 9,
    122: 9,
    123: 9,
    124: 9,
    125: 9,
    126: 9,
    200: 7,  # water
}

BandNames = {
    "s2": [
        "B1",
        "B2",
        "B3",
        "B4",
        "B5",
        "B6",
        "B7",
        "B8",
        "B8A",
        "B9",
        "B11",
        "B12",
        "cloud_probability",
    ],
    "s1": ["VV_sigma0", "VH_sigma0", "VV_corrected", "VH_corrected", "incAngle"],
    "dem": ["dem"],
    "worldcover": ["worldcover"],
}


from app import app
from flask import render_template, request
import pickle
from sklearn.neighbors import NearestNeighbors

@app.route("/")
def index():
    with open(r'app/static/pickle/df1.pkl', "rb") as input_file:
        dataFrame_1=pickle.load(input_file)
    car_name=dataFrame_1['car_name'].values.tolist()
    return render_template('index.html',car_name=car_name)

@app.route("/content-based-search",methods = ['POST'])
def content_based_search():
    if request.method == 'POST':
      result = request.form
    #   print(result['keyword'])
      return recommend(result['keyword'])

@app.route("/item-based-search",methods = ['POST'])
def item_based_search():
    if request.method == 'POST':
      result = request.form
    #   print(result['keyword'])
      return recommend_knn(result['highway_mileage'],result['city_mileage'],result['price'],result['companyRatingvalue'],result['consumerRatingvalue'])

def recommend_knn(highway_mileage,city_mileage,price,companyRatingvalue,consumerRatingvalue):
    with open(r'app/static/pickle/csr_sample.pkl', "rb") as input_file:
        csr_sample=pickle.load(input_file)
    input_file.close()
    with open(r'app/static/pickle/df1.pkl', "rb") as input_file:
        cars_data=pickle.load(input_file)
    input_file.close()
    cars_data.rename(columns={'name':'cname'},inplace=True)
    with open(r'app/static/pickle/df2.pkl', "rb") as input_file:
        df=pickle.load(input_file)
    input_file.close()

    knn = NearestNeighbors(metric='cosine', algorithm='brute', n_neighbors=5, n_jobs=-1)
    knn.fit(csr_sample)
    dataset_sort_des = df.sort_values(['rating','city_milege','highway_milege','msrp','consumer_rating'], ascending=[True,True,True,True,True])
    # filter1 = dataset_sort_des.loc[((dataset_sort_des['rating'] == companyRatingvalue)  | (dataset_sort_des['consumer_rating'] == consumerRatingvalue) )  | ((dataset_sort_des['city_milege'] == city_mileage) | (dataset_sort_des['highway_milege'] == highway_mileage))  | (dataset_sort_des['msrp'] == price) ].row_num
    filter1 = dataset_sort_des.loc[ (dataset_sort_des['consumer_rating'] == float(consumerRatingvalue))  | (dataset_sort_des['rating'] == float(companyRatingvalue) ) | ( ( dataset_sort_des['msrp'] == float(price)) ) |( (dataset_sort_des['city_milege'] == float(city_mileage)) | (dataset_sort_des['highway_milege'] == float(highway_mileage))) ].row_num
    filter1 = filter1.tolist()

    #print(len(filter1) )

    distances1=[]
    indices1=[]

    for i in filter1:

        distances , indices = knn.kneighbors(csr_sample[i],n_neighbors=5)
        indices = indices.flatten()
        indices= indices[1:]
        indices1.extend(indices)

    df1=df[df['row_num'].isin(indices1)]
    recom_list=df1['ID'].values.tolist()[1:11]
    result={}
    if len(recom_list)>0:
        list_of_cars=[]
        for val in recom_list:
            df2=cars_data.loc[cars_data['ID']==val]
            # print(df2)
            temp={}
            temp['car name']=df2.cname.values[0]
            temp['brand name']=df2.brand_name.values[0]
            temp['engine']=df2.engine_cc.values[0]
            temp['submodel']=df2.submodel.values[0]
            temp['msrp']=df2.msrp.values[0]
            temp['photo']=df2.photo.values[0]
            temp['hm']=df2.highway_milege.values[0]
            temp['cm']=df2.city_milege.values[0]
            temp['conrat']=df2.consumer_rating.values[0]
            temp['comrat']=df2.rating.values[0]

            list_of_cars.append(temp)
        result['data']=list_of_cars
        result['Status']=200
        result['msg']="successfull"
    else:
        result['data']=[]
        result['Status']=404
        result['msg']="Cars not Found!!!"
    return result

def recommend(car):
    with open(r'app/static/pickle/df1.pkl', "rb") as input_file:
        cars_data=pickle.load(input_file)
    input_file.close()

    cars_data.rename(columns={'name':'cname'},inplace=True)
    # print(cars_data.columns)

    with open(r'app/static/pickle/similarity.pkl', "rb") as input_file:
        similarity=pickle.load(input_file)
    input_file.close()
    car_record=cars_data[cars_data['car_name'] == car]
    result={}
    if len(car_record)>0:
        car_index = car_record.index[0]
        distances = similarity[car_index]
        cars_list = sorted(list(enumerate(distances)),reverse=True,key = lambda x: x[1])[1:7]

        list_of_cars=[]
        for i in cars_list:
            # print(cars_data.iloc[i[0]])
            temp={}
            temp['car name']=cars_data.iloc[i[0]].cname
            temp['brand name']=cars_data.iloc[i[0]].brand_name
            temp['engine']=cars_data.iloc[i[0]].engine_cc
            temp['submodel']=cars_data.iloc[i[0]].submodel
            temp['msrp']=cars_data.iloc[i[0]].msrp
            temp['photo']=cars_data.iloc[i[0]].photo

            list_of_cars.append(temp)
        result['data']=list_of_cars
        result['Status']=200
        result['msg']="successfull"
    else:
        result['data']=[]
        result['Status']=404
        result['msg']="Cars not Found!!!"
    return result


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""=================================================
@IDE    ：PyCharm
@Author ：shengbo.tang
@Email  : t975426031@163.com
@Date   ：2021/8/31 23:51
=================================================="""
# 浏览器类，用来提取driver
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from common.config_utils import local_config
from common.log_utils import logger

current_path = os.path.dirname(__file__)
driver_path = os.path.join(current_path, '..', local_config.get_driver_path)

class Browser:

    def __init__(self, driver_path=driver_path):
        self.driver_path = driver_path
        self.driver_name = local_config.get_driver_name

    def get_driver(self):
        """

        :return:
        """
        if self.driver_name.lower() == 'chrome':
            return self.__get_chrome_driver()
        elif self.driver_name.lower() == 'firefox':
            return self.__get_firefox_driver()
        elif self.driver_name.lower() == 'edge':
            return self.__get_edge_driver()

    def __get_chrome_driver(self):
        """
        谷歌浏览器取消chrome受控制提示
        :return:
        """
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
        chrome_options.add_argument('lang=zh_CN.UTF-8')  # 设置默认编码为utf-8
        chrome_options.add_experimental_option('useAutomationExtension', False)  # 取消chrome受自动控制提示
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])  # 取消chrome受自动控制提示
        chrome_driver_path = os.path.join(self.driver_path, 'chromedriver.exe')
        driver = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options)
        logger.info('初始化chrome浏览器并启动')
        return driver

    def __get_firefox_driver(self):
        """
        封装firefox浏览器
        :return:
        """
        firefox_driver_path = os.path.join(self.driver_path, 'geckodriver.exe')
        driver = webdriver.Firefox(executable_path=firefox_driver_path)
        logger.info('初始化Firefox浏览器并启动')
        return driver

    def __get_edge_driver(self):
        """
        封装edge浏览器
        :return:
        """
        edge_driver_path = os.path.join(self.driver_path, 'msedgedriver.exe')
        driver = webdriver.Edge(executable_path=edge_driver_path)
        logger.info('初始化edge浏览器并启动')
        return driver


#gui
from tkinter import *

#Files
from importFile import *
from settingsPage import *
import settings
from settings import *
from testingFramework import *

#graphing
import matplotlib as graphTool
from matplotlib import pyplot as graphEditor
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

#Key Modules
import os

root = Tk()

root.title(TITLE)

data = []
timeData = []
fileSuccess = False
icons = []
currentFilter = None

class Init:
    def setup():
        img = PhotoImage(file = "bg.png")
        bg = Label(root, image = img)
        bg.image = img
        bg.place(x = 0, y = 0)
        Init.buttons()

    def buttons():
        global icons
        icons = Init.loadIcons()

        allDataPoints = Button(root, image = icons[1], command = lambda: Commands.filterData("all"))
        allDataPoints.image = icons[1]
        allDataPoints['state'] = DISABLED
        allDataPoints.place(x = 20, y = 255)

        ph = Button(root, image = icons[2], command = lambda: Commands.filterData("ph"))
        ph.image = icons[2]
        ph['state'] = DISABLED
        ph.place(x = 20, y = 355)

        temperature = Button(root, image = icons[3], command = lambda: Commands.filterData("temp"))
        temperature.image = icons[3]
        temperature['state'] = DISABLED
        temperature.place(x = 20, y = 450)

        ppm = Button(root, image=icons[4], command = lambda: Commands.filterData("ppm"))
        ppm.image = icons[4]
        ppm['state'] = DISABLED
        ppm.place(x = 20, y = 540)

        settingButton = Button(root, image=icons[6], command = lambda: settingsPage.startup(root))
        settingButton.image = icons[6]
        settingButton.place(x = 1004, y = 20)

        buttons = [allDataPoints, ph, temperature, ppm]

        loadFile = Button(root, image = icons[0], command = lambda: Commands.load(buttons))
        loadFile.image = icons[0]
        loadFile.place(x = 20, y = 100)

    def loadIcons():
        icons = []
        currentdir = os.path.dirname(os.path.abspath(__file__))
        currentdir += '\icons'
        dirLength = len(os.listdir(currentdir))

        for icon in range(0, dirLength):
            img = PhotoImage(file = f"{currentdir}/{icon}.png")
            icons.append(img)

        return icons

class Commands:
    ##Loads the file
    def load(buttonArray):
        global fileSuccess
        fileSuccess = False
        print("Loading file...")
        file = FileImport.importfile()
        if file != "failed":
            fileSuccess = True
        print(fileSuccess)
        if fileSuccess:
            file = open(file, "r")
            for button in buttonArray:
                button['state'] = ACTIVE
            Commands.decodeData(file)

    #STUB - filters the graph by data type
    def filterData(filter):
        global data, timeData, fileSuccess, currentFilter
        if fileSuccess:
            tempArray = []

            #data[0] is pH,
            #data[1] = temperature
            #data[2] = ppm
            #data[3] = time

            if filter == "all" or filter == None:
                graph = LineGraph.graph_draw(data, timeData)
                currentFilter = 'all'

            if filter == "ph":
                tempArray = []
                tempArray.append(data[0])
                graph = LineGraph.graph_draw(tempArray, timeData, LINE_COLOURS[0], "pH")
                currentFilter = 'ph'

            if filter == "temp":
                tempArray = []
                tempArray.append(data[1])
                graph = LineGraph.graph_draw(tempArray, timeData, LINE_COLOURS[1], "Temperature")
                currentFilter = 'temp'

            if filter == "ppm":
                tempArray = []
                tempArray.append(data[2])
                graph = LineGraph.graph_draw(tempArray, timeData, LINE_COLOURS[2], "Salinity")
                currentFilter = 'ppm'
            return graph
        else:
            messagebox.showwarning("No File Loaded", "No file was loaded. Please load one for data to display")

    #Decodes the text file and formats it properly
    def decodeData(file):
        global data, timeData
        oldData = file.readlines()
        repairedData = []
        #remove unnecessary data
        for dataItem in oldData:
            dataItem = dataItem.strip("\n")
            repairedData.append(dataItem)
        tempData = []

        #Check if data is complete
        if (len(repairedData) % DATA_AMOUNT) == 0:
            print("Data is adequate!")
        elif ((len(repairedData) - 1) % DATA_AMOUNT) == 0:
            print("Data is repaired! Removed 1 data piece")
            settings.removedDataLength += 1
        elif ((len(repairedData) - 2) % DATA_AMOUNT) == 0:
            print("Data is repaired! Removed 2 data pieces")
            settings.removedDataLength += 2
        elif ((len(repairedData) - 3) % DATA_AMOUNT) == 0:
            print("Data is repaired! Removed 3 data pieces")
            settings.removedDataLength += 3

        ##Remove the excess data that is unneeded
        for i in range(0,settings.removedDataLength):
            repairedData.remove(repairedData[(len(repairedData) - 1) - i])

        ##Split the data into groups of 4 to represent the different pieces of data
        pHArray = []
        temperatureArray = []
        ppmArray = []
        timeData = []

        for i in range(0,len(repairedData)):
            dataPiece = repairedData[i]
            if settings.dataCounter == 0: #ph
                pHArray.append(dataPiece)
            if settings.dataCounter == 1: #ph
                temperatureArray.append(dataPiece)
            if settings.dataCounter == 2: #ph
                ppmArray.append(dataPiece)
            if settings.dataCounter == 3:
                timeData.append(dataPiece)
                settings.dataCounter = 0
            else:
                settings.dataCounter += 1

        data.append(pHArray)
        data.append(temperatureArray)
        data.append(ppmArray)

        #Graph Data
        #Graph.graph_draw([[50,20,24,53,17]])
        LineGraph.graph_draw(data, timeData)

    def checkdatatype(mainArray, partOfArray, mainArrayLength):
        for i in range(0, mainArrayLength):
            if i == 0 and mainArray[i] == partOfArray:
                return "pH"
            elif i == 1 and mainArray[i] == partOfArray:
                return "Temperature"
            elif i == 2 and mainArray[i] == partOfArray:
                return "Salinity"

class LineGraph:
    def init():
        graphTool.use("TkAgg")

    def graph_draw(data, timerange, *args):
        global icons
        filteredData = False
        colours = COLOURS
        #Initialise the graph
        f = Figure(figsize = (GRAPH_WIDTH,GRAPH_HEIGHT), dpi=100)
        a = f.add_subplot(111)
        a.set_xlabel("Time (in minutes)")
        a.set_ylabel("Data")
        themedGraph = LineGraph.setTheme(f, a)
        f = themedGraph[0]
        a = themedGraph[1]
        #check for filter arguments
        if args:
            filteredData = True

        #Load and display the data
        insertedTimeRange = []
        for dataArray in data:
            insertedTimeRange = timerange
            dataLabel = Commands.checkdatatype(data, dataArray, len(data))
            dataArray = list(map(int, dataArray))
            insertedTimeRange = list(map(int, insertedTimeRange))

            if not filteredData:
                a.plot(insertedTimeRange, dataArray, label = dataLabel)
            else:
                a.plot(insertedTimeRange, dataArray, color = args[0], label = args[1])

        f.suptitle("Data Collected")

        canvas = FigureCanvasTkAgg(f, root)
        a.legend()
        canvas.get_tk_widget().place(x = 250, y = 100)

        saveImg = Button(root, image = icons[5], command = LineGraph.saveGraph)
        saveImg.image = icons[5]
        saveImg.place(x =940, y = 625)
        return f

    def saveGraph():
        global currentFilter
        #Get the current graph
        savableGraph = Commands.filterData(currentFilter)
        #save graph
        if currentFilter == "all":
            savableGraph.savefig("Graph.png")
        elif currentFilter == "ph":
            savableGraph.savefig("Acidity.png")
        elif currentFilter == "temp":
            savableGraph.savefig("Temperature.png")
        elif currentFilter == "ppm":
            savableGraph.savefig("Salinity.png")

    def setTheme(fig, graph):
        currentTheme = settings.CURRENT_THEME
        colours = COLOURS
        if currentTheme == 'Light':
            attri = LineGraph.changeTheme(graph, 0)
        elif currentTheme == 'Dark':
            attri = LineGraph.changeTheme(graph, 1)
        elif currentTheme == 'Desert':
            attri = LineGraph.changeTheme(graph, 2)
        elif currentTheme == 'Oceanic':
            attri = LineGraph.changeTheme(graph, 3)
        elif currentTheme == 'MOZ-Theme':
            attri = LineGraph.changeTheme(graph, 4)
        fig.set_facecolor(attri[0])
        graph.set_facecolor(attri[1])
        return fig, graph

    def changeTheme(self, theme):
        graphEditor.rcParams['text.color'] = themeAttributes[theme][0]
        self.spines['left'].set_color(themeAttributes[theme][1])
        self.spines['right'].set_color(themeAttributes[theme][2])
        self.spines['top'].set_color(themeAttributes[theme][3])
        self.spines['bottom'].set_color(themeAttributes[theme][4])
        self.xaxis.label.set_color(themeAttributes[theme][5])
        self.yaxis.label.set_color(themeAttributes[theme][6])
        self.tick_params(axis = 'x', colors = themeAttributes[theme][7])
        self.tick_params(axis = 'y', colors = themeAttributes[theme][8])
        outline = themeAttributes[theme][9]
        background = themeAttributes[theme][10]

        return outline, background

class TestingFramework:
    def testData():
        start = Button(root, text = "Generate Test Data", command = dataGen.allData)
        start.place(x = 50, y = 650)

##Run window and setup
Init.setup()
root.geometry(f"{WIDTH}x{HEIGHT}")
root.resizable(width = False, height = False)
root.mainloop()


''' 경비원
시간 제한 : 1초, 메모리 제한 : 128 MB

시간 : 68 ms, 메모리 : 28776 KB
'''

import sys
read = sys.stdin.readline

# 입력
w, h = map(int, read().split())
n = int(read())
store = [list(map(int, read().split())) for _ in range(n)]
direction, loc = list(map(int, read().split()))

# 풀이
answer = 0

if direction == 1:  # 북
    for d, l in store:
        if d == 1:
            answer += abs(loc - l)
        elif d == 2:
            left_way = loc + h + l
            right_way = (w - loc) + h + (w - l)
            answer += min(left_way, right_way)
        elif d == 3:
            answer += loc + l
        elif d == 4:
            answer += (w - loc) + l
elif direction == 2:  # 남
    for d, l in store:
        if d == 1:
            left_way = loc + h + l
            right_way = (w - loc) + h + (w - l)
            answer += min(left_way, right_way)
        elif d == 2:
            answer += abs(loc - l)
        elif d == 3:
            answer += loc + (h - l)
        elif d == 4:
            answer += (w - loc) + (h - l)
elif direction == 3:  # 서
    for d, l in store:
        if d == 1:
            answer += loc + l
        elif d == 2:
            answer += (h - loc) + l
        elif d == 3:
            answer += abs(loc - l)
        elif d == 4:
            left_way = loc + w + l
            right_way = (h - loc) + w + (h - l)
            answer += min(left_way, right_way)
elif direction == 4:  # 동
    for d, l in store:
        if d == 1:
            answer += loc + (w - l)
        elif d == 2:
            answer += (h - loc) + (w - l)
        elif d == 3:
            left_way = (h - loc) + w + (h - l)
            right_way = loc + w + l
            answer += min(left_way, right_way)
        elif d == 4:
            answer += abs(loc - l)

# 출력
print(answer)


import json
import logging
import sys
from multiprocessing.dummy import Pool as ThreadPool

import requests

# 日志配置
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
logger = logging.getLogger()
logger.setLevel(level=logging.INFO)

def main_handler(event, context):
    logger.info("start main handler")
    request_id = context.get('request_id')

    if "body" not in event.keys():
        return {"code": 410, "errorMsg": "event is not come from api gateway"}
    callback_url = ''
    try:
        req_body = event['body']
        req_param = json.loads(req_body)
        logger.info("输入参数: " + json.dumps(req_param))
        videos = req_param['Data']['Videos']
        audio = req_param['Data']['Audio']
        callback_url = req_param['Data']['CallbackURL']
        vod_region = req_param['Data']['Output']['Vod']['Region']
        sub_app_id = req_param['Data']['Output']['Vod']['SubAppId']
        class_id = req_param['Data']['Output']['Vod']['ClassId']

        param_list = []
        for video in videos:
            json_str = json.dumps(video)
            print(json_str)
            param_list.append(json_str)

        pool = ThreadPool(6)
        results = pool.map(composition, param_list)
        print(results)
        pool.close()
        pool.join()

        urls = []
        logger.info("开始拼接视频")
        for result in results:
            print(result)
            if 'Failure' in result:
                return result
            url = result['Data']['OutputUrl']
            urls.append(url)
        callback_body = splice(urls, audio, '', vod_region, sub_app_id, class_id)
        callback_body['RequestId'] = request_id
        callback(callback_url, callback_body)
    except Exception as err:
        callback_body = {
            "Result": "Failure",
            "ErrorCode": "InvalidParameter",
            "ErrorMessage": "Invalid parameter: " + str(err),
            "RequestId": request_id
        }
        callback(callback_url, callback_body)
        return json.dumps(callback_body)
    return callback_body

def splice(urls, audio, callback_url, vod_region, sub_app_id, class_id):
    splice_data = {
        "Action": "SpliceVideo",
        "Data": {
            "Input": {
                "URLs": urls,
                "Audio": audio,
                "Transitions": "None",
                "CallbackURL": callback_url
            },
            "Output": {
                "Vod": {
                    "Region": vod_region,
                    "SubAppId": sub_app_id,
                    "ClassId": class_id
                }
            }
        }
    }
    print(splice_data)

    response = requests.post(
        'https://service-bis1czil-1307427535.cd.apigw.tencentcs.com/release/shifang-ffmpeg-splice-sync',
        data=json.dumps(splice_data))
    print(response.content)
    return json.loads(response.text.encode('utf8'))

def composition(param_json):
    response = requests.post(
        'https://service-ngvp1ppd-1307427535.cd.apigw.tencentcs.com/release/shifang-ffmpeg-composition-sync',
        data=param_json)
    print(response.content)
    return json.loads(response.text.encode('utf8'))

# 回调逻辑。
def callback(url, data):
    if not url:
        logger.info("Callback url is empty, no need to callback.")
        return

    response = requests.post(url, json=data)
    logger.info("Callback response:", response.text.encode('utf8'))


import uuid
import time
import datetime
import os
import json
import dill
import numpy as np
import pandas as pd
import torch
from torch import nn
import torch.utils.data as data

from modules.train_utils import seq_corr_1d, seq_corr_3d

class Model_Trainer():
    def __init__(self, model, train_loader, test_loader):
        self.experiment_id = uuid.uuid4().hex
        self.model = model
        self.config = model.config
        self.model_filename = self.config.model_type
        self.train_loader = train_loader
        self.test_loader = test_loader

        folder_path = f"mlruns/{self.experiment_id}"
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        with open(f"mlruns/{self.experiment_id}/config.dill", 'wb') as handle:
            dill.dump(self.config, handle)
        self.log_file_name = f"mlruns/{self.experiment_id}/log.txt"
        with open(self.log_file_name, 'w') as f:
            f.write(f"{self.experiment_id}\n")

        self.model_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        print(self.model_device)
        if torch.cuda.is_available():
            self.data_parallel = True if torch.cuda.device_count() > 1 else False
            self.device_count = max(torch.cuda.device_count(), 1)
            if self.device_count > 1:
                print("Using " + str(torch.cuda.device_count()) + " GPUs")
                self.model = nn.DataParallel(model)
                self.model = self.model.cuda()
            else:
                self.model = model.cuda()
        else:
            self.data_parallel = False
            self.device_count = 1

        self.optimizer = self.config.optimizer(
            self.model.parameters(),
            lr=self.config.learning_rate,
            weight_decay=self.config.l2_lambda
        )
        self.max_epochs = self.config.max_epochs
        self.init_counters()

    def init_counters(self):
        self.finished_training = False
        self.steps = 0
        self.recent_steps = 0
        self.test_steps = 0
        self.consecutive_losses_increasing = 0
        self.finished_training = False
        self.detailed_losses = {i: [] for i in range(self.config.n_targets)}
        self.train_losses = []
        self.test_losses = []

    def train(self):
        for epoch in range(0, self.max_epochs):
            self.running_loss = 0.0
            self.recent_loss = 0.0
            self.epoch_steps = 0
            self.epoch = epoch

            for inputs, labels in self.train_loader:
                self.steps += 1
                self.recent_steps += 1
                self.epoch_steps += 1

                # display info on the very first step
                if self.epoch == 0 and self.steps == 1:
                    print("Batch input shape is: " + str(inputs[0].shape))
                    if self.data_parallel:
                        print("Parallel devices being used: " + str(self.model.device_ids))

                model_output = self.model.forward(inputs)
                if len(model_output.shape) == 3:
                    model_output = torch.squeeze(model_output[:, -1, :], 1)

                # display info on the very first step
                if self.epoch == 0 and self.steps == 1:
                    print("Batch output shape is: ", model_output.size())

                loss = self.config.loss_function(model_output.view(-1, self.config.n_targets), labels.view(-1, self.config.n_targets))
                with open(self.log_file_name, 'a') as f:
                    f.write(f'n_steps_train: {self.steps}\n')
                    f.write(f'train_batch_loss: {loss.item()}\n')

                loss_4_accum = loss / self.config.accumulation_steps
                loss_4_accum.backward()
                self.running_loss += loss.item()
                self.recent_loss += loss.item()

                if self.steps % self.config.accumulation_steps == 0:
                    if self.config.accumulation_steps > 1:
                        with open(self.log_file_name, 'a') as f:
                            f.write(f"step #{self.steps}, loss = {loss}\n")
                    elif self.steps % 10 == 0:
                        with open(self.log_file_name, 'a') as f:
                            f.write(f"step #{self.steps}, loss = {loss}\n")
                    self.optimizer.step()
                    self.optimizer.zero_grad()

                if self.steps % (self.config.evaluate_every_n_steps // self.device_count) == 0:
                    self.evaluate_model()
                    print(self.test_losses)

                if self.consecutive_losses_increasing == self.config.consecutive_losses_to_stop:
                    self.finished_training = True
                    with open(self.log_file_name, 'a') as f:
                        f.write(f'best_loss: {min(self.test_losses)}\n')
                        dt = datetime.datetime.fromtimestamp(time.time())
                        f.write(f'run_date: {dt.year*1000+dt.month*100+dt.day}\n')
                        f.write(f'run_end_datetime: {time.time()}\n')
                    break
            self.epoch_steps = 0
            if self.finished_training:
                break

    def evaluate_model(self):
        test_loss = 0
        start_time = time.time()
        self.model.eval()
        with torch.no_grad():
            self.test_steps_running = 0
            detailed_losses_temp = {
                i: 0.0 for i in range(self.config.n_targets)}
            combined_features = []
            combined_targets = []
            combined_outputs = []

            for inputs, labels in self.test_loader:
                self.test_steps += 1
                self.test_steps_running += 1
                model_output = self.model.forward(inputs)
                if len(model_output.shape) == 3:
                    model_output = torch.squeeze(model_output[:, -1, :], 1)
                if self.test_steps_running == 1 and len(self.test_losses) == 0:
                    print("(Eval) Batch output shape is: ", model_output.size())
                model_output = model_output.view(-1, self.config.n_targets)
                labels = labels.view(-1, self.config.n_targets)
                for i, (output, label) in enumerate(zip(torch.transpose(model_output, 0, 1), torch.transpose(labels, 0, 1))):
                    detailed_losses_temp[i] += self.config.loss_function(
                        output, label).item() / len(self.test_loader)
                batch_loss = self.config.loss_function(model_output, labels)
                test_loss += batch_loss.item()
                combined_features.append(inputs)
                combined_targets.append(labels)
                combined_outputs.append(model_output)

            self.test_losses.append(test_loss / len(self.test_loader))
            # record losses for each target
            for key in self.detailed_losses:
                self.detailed_losses[key].append(detailed_losses_temp[key])

            with open(self.log_file_name, 'a') as f:
                f.write(f'n_evals: {len(self.test_losses)}\n')
                f.write(f'test_loss_at_eval: {test_loss / len(self.test_loader)}\n')
                f.write(f'train_running_loss_at_eval: {self.running_loss / self.epoch_steps}\n')
                f.write(f'train_recent_loss_at_eval: {self.recent_loss / self.recent_steps}\n')

            if self.test_losses[-1] > min(self.test_losses):
                self.consecutive_losses_increasing += 1
            else:
                self.consecutive_losses_increasing = 0

            # DataParallel messes with the state dict - cleaner to save out of module
            if self.data_parallel:
                torch.save(self.model.module.state_dict(), f'mlruns/{self.experiment_id}/model.pth')
            else:
                torch.save(self.model.state_dict(), f'mlruns/{self.experiment_id}/model.pth')

            with open(self.log_file_name, 'a') as f:
                f.write(f'n_epoch: {self.epoch}\n')
                f.write(f'train_loss_epoch: {self.running_loss / self.epoch_steps}\n')
                f.write(f'best_eval_loss_epoch: {min(self.test_losses)}\n')
                f.write(f'consecutive_losses_epoch: {self.consecutive_losses_increasing}\n')
                f.write(f"Time to eval--- {time.time() - start_time} seconds ---\n")
                f.write(f"Epoch {self.epoch :.0f}.. \n")
                f.write(f"Step: {self.steps :.0f}.. \n")
                f.write(f"Train loss (running): {self.running_loss / self.epoch_steps :.6f}.. \n")
                f.write(f"Train loss (recent): {self.recent_loss / self.recent_steps :.6f}.. \n")
                f.write(f"Test loss: {test_loss / len(self.test_loader) :.6f}.. \n")
                f.write(f"Consecutive losses: {self.consecutive_losses_increasing}\n")
            self.train_losses.append(
                self.running_loss / len(self.train_loader))
            self.recent_steps = 0
            self.recent_loss = 0
            # Set model back to train
            self.model.train()

    def score_model(self, score_data_loader):
        self.model.eval()
        res = np.zeros((0, self.config.n_targets * 2))
        with torch.no_grad():
            for inputs, labels in score_data_loader:
                model_output = self.model.forward(inputs)
                if len(model_output.shape) == 3:
                    model_output = torch.squeeze(model_output[:, -1, :], 1)
                model_output = model_output.view(-1, self.config.n_targets)
                labels = labels.view(-1, self.config.n_targets)
                # concatenate targets and labels
                cat_res = torch.cat([model_output, labels], dim=1).cpu().detach().numpy()
                # concat results batch to np dataframe
                res = np.concatenate((res, cat_res), axis=0)
                # and append security IDs and datetimes for reference, then save the whole DF down
        res_col_names = ['target_' + str(n) + '_pred' for n in range(0, self.config.n_targets)] + \
            ['target_' + str(n) + '_actual' for n in range(0, self.config.n_targets)]
        res_df = pd.concat(
            [
                pd.DataFrame([pair for pair in score_data_loader.dataset.pairs], columns=["date", "rel_date_num", "ticker_1", "ticker_2"]),
                pd.DataFrame(res, columns=res_col_names)
            ],
            axis=1
        )
        res_df.to_csv(f'mlruns/{self.experiment_id}/scored_results.csv')

        mse_avg = 0.0
        mae_avg = 0.0
        for i in range(0, self.config.n_targets):
            mse = ((res[:, i] - res[:, i+self.config.n_targets])**2).mean()
            mse_avg += mse / self.config.n_targets
            mae = abs(res[:, i] - res[:, i+self.config.n_targets]).mean()
            mae_avg += mae / self.config.n_targets
            with open(self.log_file_name, 'a') as f:
                f.write(f'mse_target_{i}: {mse}\n')
                f.write(f'mae_target_{i}: {mae}\n')
        with open(self.log_file_name, 'a') as f:
            f.write(f'mse_avg: {mse_avg}\n')
            f.write(f'mae_avg: {mae_avg}\n')

        return res_df

    def cleanup_model(self):
        self.model.destroy()


# ！
# encoding: utf-8
'''
@author: String
@softwore: pycharm
@file:
@desc:
'''

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time

uer = []
# 定义全局变量url、headers、movie_links、movie_names = []
url = "https://movie.douban.com/top250"
headers = {'user-agent': 'my-app/0.0.1'}
movie_links = []
movie_names = []
all_movie = []

# 在获取每个电影信息的时候，查提取每个电影的连接的方法
def get_list(soup_list):
    list = []
    for ele in soup_list:
        list.append(ele.text)
    return list

# 访问主页面，并且完成页面跳转
def get_page(page_url):
    page = 0
    max_page = 225
    while page <= max_page:
        time.sleep(5)
        url = page_url+"?start="+page.__str__()+"&filter="
        response = requests.get(url=url, headers=headers).text
        soup = BeautifulSoup(response, 'html.parser')
        get_link(soup)
        page += 25

# 获取每页的所有电影链接
def get_link(soup):
    for ele in soup.findAll(class_="hd"):
        movie_links.append(ele.find('a', href=True).attrs['href'])
        movie_names.append(ele.find(class_="title").text)
        print(movie_links)
        # exit()

# 根据电影链接，获取个电影的信息
def get_movieinfo(movie_link):
    time.sleep(5)
    resbons = requests.get(url=movie_link, headers=headers).text
    soup = BeautifulSoup(resbons, 'html.parser')
    movie_info = {}
    movie_info['movie_title'] = soup.find(property="v:itemreviewed").string
    movie_info['movie_director'] = soup.find(rel="v:directedBy").text
    movie_info['movie_writer'] = soup.find_all(class_="attrs")[1].text if len(soup.find_all(class_="attrs"))>1 else ""
    # print(movie_info['movie_writer'])
    # exit()
    movie_info['movie_actor'] = get_list(soup.find_all(rel="v:starring"))
    movie_info['movie_genre'] = get_list(soup.findAll(property="v:genre"))
    movie_info['movie_country'] = soup.find(text="制片国家/地区:").next_element
    movie_info['movie_langage'] = soup.find(text="语言:").next_element
    movie_info['movie_ReleaseDate'] = soup.find(property="v:initialReleaseDate").text
    movie_info['movie_runtime'] = soup.find(property="v:runtime").text
    movie_info['movie_average'] = soup.find(property="v:average").text
    movie_info['movie_RatingPeople'] = soup.find(property="v:votes").text
    movie_info['movie_linke'] = movie_link
    for key in movie_info:
        print(key, ":", movie_info.get(key))

    # 把一个电影的所有信息装入一个容器里
    all_movie.append(movie_info)

# for循环找出每一条连接，并根据连接获取每个电影的详细信息
def get_onelink(movie_links):
    for n,l in zip(movie_names,movie_links):
        print( n+"："+l )
        get_movieinfo(l)
        print("-------------------------------------------------------")

# 运行作为一个入口文件
if __name__ =='__main__':
    # 测试代码
    # get_movieinfo('https://movie.douban.com/subject/26430107/')
    # exit()

    # 调用获取页面连接方法
    get_page(url)
    # 调用获取页面信息方法
    get_onelink(movie_links)

    # 将所有电影数据存入表格中
    data = pd.DataFrame(all_movie)
    data.to_excel("Top250豆瓣高分电影.xlsx")


import os
import logging
import subprocess
import sys

import yaml

DEFAULT_LOG_LEVEL = os.environ.get("LOG_LEVEL", logging.INFO)
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)

# set up logging to file
logFilePath = '/tmp/' + sys.argv[0].split('/')[-1] + '.log'
file_handler = logging.FileHandler(filename=logFilePath)
file_handler.setLevel(os.environ.get("FILE_LOG_LEVEL", DEFAULT_LOG_LEVEL))
logger.addHandler(file_handler)

# set up logging to console
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(os.environ.get("CONSOLE_LOG_LEVEL", DEFAULT_LOG_LEVEL))
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)

SKOPEO_IMAGE = "quay.io/skopeo/stable:latest"
DOCKER_REGISTRY = "docker://dtr.dev.cray.com"
VALIDATE_PACKER_IMAGES = False

EXPECTED_CONFIGMAPS = {
    "ims-config",
    "cray-configmap-ims-v2-image-create-kiwi-ng",
    "cray-configmap-ims-v2-image-create-packer",
    "cray-configmap-ims-v2-image-customize"
}

class ImsException(Exception):
    pass

def ims_configmaps():
    configmaps = set()
    try:
        logger.debug("Getting list of configmaps in the services namespace.")
        command_line = ['kubectl', 'get', 'cm', '-n', 'services', '-o', 'name']
        result = subprocess.check_output(command_line, stderr=subprocess.STDOUT).decode("utf8")
        logger.debug(result)
        for cm in result.splitlines():
            if "ims" in cm.lower():
                # result is prefaced with "configmap/". We want to strip that off.
                configmaps.add(cm[10:])
    except subprocess.CalledProcessError as err:
        logger.error(f"Could not list IMS configmaps. Got exit code {err.returncode}. Msg: {err.output}")
        raise ImsException

    if not configmaps == EXPECTED_CONFIGMAPS:
        logger.error(f"The IMS configmaps in the services namespace did not match what was expected.")
        logger.error(f"Found Configmaps: {configmaps}")
        logger.error(f"Expected Configmaps: {EXPECTED_CONFIGMAPS}")
        raise ImsException

    for configmap in configmaps:
        if configmap.lower().startswith("cray-configmap-ims"):
            if VALIDATE_PACKER_IMAGES or "packer" not in configmap.lower():
                yield configmap

def cm_dependent_images(cm):
    dependent_images = set()
    try:
        logger.info(f"Validating {cm} configmap")
        command_line = ['kubectl', 'get', 'cm', '-n', 'services', '-o', 'yaml', cm]
        response = subprocess.check_output(command_line, stderr=subprocess.STDOUT).decode("utf8")
        logger.debug(response)
        configmap = yaml.safe_load(response)
        for resource_name in configmap["data"]:
            resource = yaml.safe_load(configmap["data"][resource_name])
            if resource["kind"] == "Job":
                for container_group in ['initContainers', 'containers']:
                    for container in resource["spec"]["template"]["spec"][container_group]:
                        dependent_images.add(container["image"])
    except subprocess.CalledProcessError as err:
        logger.error(f"Could not retrieve IMS configmap {cm}. Got exit code {err.returncode}. Msg: {err.output}")
        raise ImsException

    for dependent_image in dependent_images:
        logger.info(f"  - Configmap references the image {dependent_image}")
        yield dependent_image

def validate_dependent_image_exists(dependent_image):
    try:
        logger.debug(f"Validating that the image {dependent_image} exists in the packages.local repo")
        command_line = ["podman", "run", "--rm", SKOPEO_IMAGE, "inspect", '/'.join([DOCKER_REGISTRY, dependent_image])]
        response = subprocess.check_output(command_line, stderr=subprocess.STDOUT).decode("utf8")
        logger.info(f"    * Verified that the image {dependent_image} exists in the local docker registry.")
        logger.debug(response)
        return True
    except subprocess.CalledProcessError as err:
        logger.error(f"Could not validate the image {dependent_image}. "
                     f"Got exit code {err.returncode}. Msg: {err.output}")
        return False

def main():
    try:
        return_value = True
        logger.info("Beginning verification that IMS dependent images are available in the local docker registry")
        for cm in ims_configmaps():
            for dependent_image in cm_dependent_images(cm):
                return_value = return_value and validate_dependent_image_exists(dependent_image)

        if not return_value:
            logger.error("Validation of IMS dependent images failed")
            return 1

        logger.info("Validation of IMS dependent images succeeded")
        return 0
    except ImsException:
        return 1
    except Exception as exc:
        logger.error(f"Unexpected error validating dependent IMS images.", exc_info=exc)
        return 1

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


import os
import imutils
import numpy as np
from imutils import paths
from matplotlib import pyplot as plt

from source.face_recognition import FaceRecognition

def extract_embeddings(classes_dir: str = "", model_path: str = "", default_classname: str=None,
                       one_face_limit=True, save_dataset=True):
    image_paths = list(paths.list_images(classes_dir))

    recognition = FaceRecognition()

    X = []
    y = []
    for k, image_path in enumerate(image_paths):
        print(f"[INFO] processing image {image_path} --- {k + 1}/{len(image_paths)}")

        label = image_path.split(os.path.sep)[-2] if not default_classname else default_classname
        image_array = recognition.load_image(image_path)
        if image_array is not None:
            height, width = image_array.shape[:2]
            if width > 600:
                image_array = imutils.resize(image_array, width=600)
            faces = recognition.detect_faces(image_array)

            if faces:
                if one_face_limit and len(faces) > 1:
                    print(f'ERROR - {image_path} - faces - {len(faces)}')
                    continue
                    # plt.imshow(face_array)
                    # plt.title(image_path.split(os.path.sep)[-1])
                    # plt.show()

                face_array = faces[0]['array']
                face_keypoints = faces[0]['keypoints']
                face_array = recognition.align_face(image_array, face_keypoints)

                face_array = recognition.preprocess_face(face_array, target_size=(160, 160))
                embedding = recognition.get_embeddings(face_array)
                X.append(embedding)
                y.append(label)
            else:
                print(f'[WARNING] Face is not found: {image_path}')

    if save_dataset:
        np.savez_compressed(f"{model_path}/embeddings-dataset.npz", np.asarray(X), np.asarray(y))

    return np.asarray(X), np.asarray(y)

if __name__ == '__main__':
    base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    dataset_path = os.path.join(base_path, "dataset")
    model_path = os.path.join(base_path, "models")

    extract_embeddings(dataset_path, model_path)


from metrics import StreamSegMetrics
from .trainer import Trainer

class SourceOnlyTrainer(Trainer):

    def __init__(self, args, writer, device, rank, world_size):
        super().__init__(args, writer, device, rank, world_size)

    @staticmethod
    def set_metrics(writer, num_classes):
        writer.write("Setting up metrics...")
        metrics = {
            'train': StreamSegMetrics(num_classes, 'train'),
            'test_source': StreamSegMetrics(num_classes, 'test_source'),
            'test_target': StreamSegMetrics(num_classes, 'test_target'),
        }
        writer.write("Done.")
        return metrics

    def handle_ckpt_step(self):
        return None, self.checkpoint_step, None, None

    def max_iter(self):
        return self.args.num_source_epochs * self.source_train_clients[0].len_loader

    def perform_centr_sourceonly_training(self, e_type, e, e_ref, train_client, train_metric):
        self.writer.write(f"{e_type}: {e + 1}/{e_ref}")
        self.model.train()
        _ = train_client.run_epoch(e, self.optimizer, train_metric, self.scheduler, e_name=e_type)
        # train_metric.synch(self.device)
        self.writer.plot_metric(e, train_metric, str(train_client), self.ret_score)
        train_metric.reset()
        self.save_model(e + 1, self.optimizer, self.scheduler)

    def train(self, train_metric=None, max_scores=None):

        if train_metric is None:
            train_metric = self.metrics['train']

        max_scores = [0] * len(self.target_test_clients)

        for e in range(self.ckpt_source_epoch, self.args.num_source_epochs):
            self.perform_centr_sourceonly_training('SOURCE EPOCH', e, self.args.num_source_epochs,
                                               self.source_train_clients[0], train_metric)
            if (e + 1) % self.args.test_interval == 0 or (e + 1) == self.args.num_source_epochs:
                max_scores, improvement = self.test(self.target_test_clients, self.metrics['test_target'], e,
                                                    'SOURCE EPOCH', max_scores, cl_type='target')
                self.test(self.source_test_clients, self.metrics['test_source'], e, 'SOURCE EPOCH',
                          self.get_fake_max_scores(improvement, len(self.source_test_clients)), cl_type='source')

        return max_scores


#!/usr/bin/env python

import urllib
import base64
import requests
try:
    import simplejson as json
except ImportError as e:
    import json

input_url = 'https://api.splunkstorm.com/1/inputs/http?'
project_id = None
access_token = None

def log(
    event_json,
    sourcetype='syslog',
    host=None,
    source=None
):

    """ posts to input_url

        Args:
            event_json{dict} - event payload
            sourcetype{str} - sourcetype,  defaults to syslog
            host{str} - log host origin host
            source{str} - log origin api

        Returns:
            {str} - post response

    """

    if not project_id:
        raise Exception('project_id is required')
    if not access_token:
        raise Exception('access_token is required')

    params = {
        'project': project_id,
        'sourcetype': sourcetype
    }
    if host:
        params['host'] = host
    if source:
        params['source'] = source

    url = input_url + urllib.urlencode(params)

    payload = json.dumps(event_json)

    authorization = 'Basic ' + base64.b64encode(':'+access_token)
    headers = {'Authorization': authorization}

    return requests.post(
        url,
        data=payload,
        headers=headers
    )


def oanda_stream_api(account, instrument,action):
    from oandapyV20 import API
    from oandapyV20.endpoints.pricing import PricingStream
    from oandapyV20.exceptions import V20Error

    params = {
        "instruments": instrument
    }
    api = API(access_token=account.token)
    ps = PricingStream(account.id, params)

    try:
        for rsp in api.request(ps):
            action(rsp)
    except V20Error as e:
        print("Error: {}".format(e))


import pandas as pd

def GrabOccurrenceData(past_occurrence, mag_value):
	qdf = pd.read_csv('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/' + str(past_occurrence) + '.csv')
	qdf = qdf[qdf['mag'] > int(mag_value)]
	return qdf

def GrabMagnitudes(past_occurrence):

	'''
	Parameters : `past_occurrence`
	Return : `list`
	'''

	qdf = pd.read_csv('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/' + str(past_occurrence) + '.csv')
	mags = qdf['mag'].to_list()
	max_mag = max(mags)
	mag_range = list(range(1, (int(max_mag) + 1)))
	return mag_range

def GrabSpecificArea(past_occurrence, mag_value):
	qdf = GrabOccurrenceData(past_occurrence, mag_value)
	places = qdf['place'].to_list()

	specific_areas = []
	for place in places:
		area = place.split(', ')
		if len(area) == 2:
			specific_areas.append(area[1])
		if len(area) < 2:
			specific_areas.append(area[0])

	area_counts = []
	for area in specific_areas:
		area_counts.append(area + ' - ' + str(specific_areas.count(area)))

	specific_areas = list(set(area_counts))
	return specific_areas


import subprocess
import os
import numpy as np

BASE_DIR = os.path.dirname(os.path.realpath(__file__))

if subprocess.call(['make', '-C', BASE_DIR]) != 0:  # return value
    raise RuntimeError('Cannot compile lanms: {}'.format(BASE_DIR))

def merge_quadrangle_n9(polys, thres=0.3, precision=10000):
    from .adaptor import merge_quadrangle_n9 as nms_impl
    if len(polys) == 0:
        return np.array([], dtype='float32')
    p = polys.copy()
    p[:,:8] *= precision
    ret = np.array(nms_impl(p, thres), dtype='float32')
    ret[:,:8] /= precision
    return ret


# -*- coding: utf-8 -*-
from __future__ import print_function
import numpy
import numpy.ma
import scipy
import scipy.misc
import scipy.ndimage
import scipy.signal
import scipy.cluster
#import matplotlib
#import matplotlib.mlab
#import pylab
#import pretty_logger
def get_Z_offset_guess(room):
	# Assume we are ~2.5 m away in Z
	DEFAULT_OFFSET = 2.5

	if room.units == 'm':
		offset = DEFAULT_OFFSET
	elif room.units == 'cm':
		offset = DEFAULT_OFFSET * 100
	elif room.units == 'in':
		offset = (DEFAULT_OFFSET * 100) / 2.54
	else:
		raise NotImplementedError('Unknown unit: {}'.format(room.units))

	if room.user_is == 'below':
		pass
	elif room.user_is == 'above':
		offset = -offset
	else:
		raise NotImplementedError('Unkonwn user position: {}'.format(room.user_is))

	return offset
def aoa(room, lights, Zf=2950, k_init_method= 'YS_brute', actual_location=None):
    centers = numpy.array(list(zip(*lights))[0])
    transmitters = numpy.array(list(zip(*lights))[1])
    centers = numpy.append(centers, Zf * numpy.ones((len(lights), 1)), axis=1)
    image_squared_distance = numpy.sum(numpy.square(centers), axis=1)
    pair_shape = (len(lights)-1, len(lights))
    transmitter_pair_squared_distance = numpy.zeros(pair_shape)
    pairwise_image_inner_products     = numpy.zeros(pair_shape)
    for i in range(len(lights)-1):
        for j in range(i+1, len(lights)):
            transmitter_pair_squared_distance[i][j] =\
            numpy.sum(numpy.square(transmitters[i] - transmitters[j]))
            pairwise_image_inner_products[i][j]=\
            numpy.dot(centers[i], centers[j])
    def least_squares_scaling_factors(k_vals):
        errs = []
        for i in range(len(lights)-1):
            for j in range(i+1, len(lights)):
                errs.append(
                        k_vals[i]**2 * image_squared_distance[i] +\
						      k_vals[j]**2 * image_squared_distance[j] -\
						      2*k_vals[i]*k_vals[j] * pairwise_image_inner_products[i][j] -\
						      transmitter_pair_squared_distance[i][j]
						      )
        return errs
    def scalar_scaling(k_vals):
        errs = numpy.array(least_squares_scaling_factors(k_vals))
        return numpy.sum(errs)
    def sol_guess_subset(index, var_cnt, sol_guess):
        sol_guess_sub = numpy.array([sol_guess[0,0]])
        for i in range(1,len(lights)):
            if sol_guess[i, 1] < 0:
                sol_guess_sub = numpy.append(sol_guess_sub, sol_guess[i, int((index%(2**var_cnt))/2**(var_cnt-1))])
                var_cnt -= 1
            else:
                sol_guess_sub = numpy.append(sol_guess_sub, sol_guess[i, 0])
        return sol_guess_sub
    def brute_force_k():
        number_of_iteration = 500
        k0_vals = numpy.linspace(-0.1, -0.01, number_of_iteration)
        err_history = []
        idx_history = []
        k_vals = numpy.array([])
        for j in range(number_of_iteration+1):
            if (j==number_of_iteration):
                min_error_history_idx = err_history.index(min(err_history))
                min_idx = idx_history[min_error_history_idx]
                k0_val = k0_vals[min_idx]
            else:
                k0_val = k0_vals[j]
            sol_guess = numpy.array([[k0_val, 0]])
            sol_found = 1
            multiple_sol = 0
            for i in range(1, len(lights)):
                sol = numpy.roots([image_squared_distance[i], -2*sol_guess[0,0]*pairwise_image_inner_products[0,i], (sol_guess[0,0]**2*image_squared_distance[0]-transmitter_pair_squared_distance[0, i])]);
                if numpy.isreal(sol)[0]:
                    if (sol[0] < 0) and (sol[1] < 0):
                        sol_guess = numpy.append(sol_guess, [sol], axis=0)
                        multiple_sol += 1
                    elif sol[0] < 0:
                        sol_guess = numpy.append(sol_guess, numpy.array([[sol[0], 0]]), axis=0)
                    elif sol[1] < 0:
                        sol_guess = numpy.append(sol_guess, numpy.array([[sol[1], 0]]), axis=0)
                    else:
                        sol_found = 0
                        break
                else:
                    sol_found = 0
                    break
            if sol_found:
                scaling_factors_error_combination = []
                for m in range(1, 2**multiple_sol+1):
                    sol_guess_combination = sol_guess_subset(m, multiple_sol, sol_guess)
                    scaling_factors_error_arr = least_squares_scaling_factors(sol_guess_combination)
                    scaling_factors_error = 0
                    for n in scaling_factors_error_arr:
                        scaling_factors_error += n**2
                    scaling_factors_error_combination.append(scaling_factors_error)
                k_vals = sol_guess_subset(numpy.argmin(scaling_factors_error_combination)+1, multiple_sol, sol_guess)
                err_history.append(min(scaling_factors_error_combination))
                idx_history.append(j)
        return k_vals

    if k_init_method == 'static':
        k_val_from_z = get_Z_offset_guess(room) / Zf
        k_vals_init= [k_val_from_z] * len(lights)
    elif k_init_method == 'YS_brute':
        k_vals = brute_force_k()
    elif k_init_method == 'scipy_brute':
        k_ranges = [slice(.01, .1, (.1-.01)/10) for i in range(len(lights))]
        k_vals_init = scipy.optimize.brute(scalar_scaling, k_ranges, disp=True)
    elif k_init_method == 'scipy_basin':
        k_vals_init = [-.05] * len(lights)
        res = scipy.optimize.basinhopping(scalar_scaling, k_vals_init,
				T=1e12,
				stepsize=0.01,
				)
        k_vals_init = res.x
    elif k_init_method == 'actual':
        assert actual_location is not None
        k_vals_init = k_vals_actual
    else:
        k_vals, ier = scipy.optimize.leastsq(least_squares_scaling_factors, k_vals_init)
    def least_squares_rx_location(rx_location):
        dists = []
        for i in range(len(lights)):
            dists.append(
					numpy.sum(numpy.square(rx_location - transmitters[i])) -\
					k_vals[i]**2 * image_squared_distance[i]
					)
        return dists
    def initial_position_guess(room, transmitters):
        guess = numpy.mean(transmitters, axis=0)[0]
        offset = get_Z_offset_guess(room)
        guess[2] = guess[2] - offset
        return guess
    if k_init_method == 'actual':
        rx_location_init = actual_location
    else:
        rx_location_init = initial_position_guess(room, transmitters)
        rx_location, ier = scipy.optimize.leastsq(least_squares_rx_location, rx_location_init)
    def least_squares_rotation(rotation):
        rotation = rotation.reshape((3,3))
        r = transmitters.T - rotation.dot(absolute_centers) - rx_location.reshape(3,1)
        r = numpy.square(r)
        r = r.flatten()
        return r
    absolute_centers = centers.T * numpy.vstack([k_vals, k_vals, k_vals])
    rx_rotation_init = numpy.array([[0, -1, 0], [1, 0, 0], [0, 0, 1]])
    rx_rotation, ier = scipy.optimize.leastsq(least_squares_rotation, rx_rotation_init)
    rx_rotation = numpy.array(rx_rotation).reshape((3,3))
    rx_location_error = numpy.sum(numpy.abs(
			numpy.sqrt(numpy.sum(numpy.square(rx_location - transmitters[i]))) -\
			numpy.sqrt(k_vals[i]**2 * image_squared_distance[i])
			))
    return (rx_location, rx_rotation, rx_location_error)


# 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 removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]:

        dummy = TreeNode(0, root)

        def delete(node):
            if not node:
                return True

            left = delete(node.left)
            right = delete(node.right)

            if left:
                node.left = None
            if right:
                node.right = None

            return node.val == target and left and right

        delete(dummy)

        return dummy.left


from app_store_scraper import AppStore
import nltk
from nltk.corpus import stopwords
from pprint import pprint
import re

'''
Setup
  - Install "pip", the package installer for python. (https://pip.pypa.io/en/stable/installation/)
  - Install "NLTK", a platform for working with human language data. (https://www.nltk.org/install.html). We use this to filter out common English words.
  - Install "app_store_scaper". Instructions here: (https://pypi.org/project/app-store-scraper/)

  - When calling the "generate_word_count_dict" function in the code below below: modify the app_name, country, and the number_of_reviews you want to analyze.

  - To run, in your terminal, navigate to the directory where this file lives and type:

    python3 appStore_wordFrequency_scraper.py
 '''

class WordCount:
	def __init__(self, word, count):
		self.word = word
		self.count = count

	def __str__(self):
		return "%s: %d" % (self.word, self.count)

	def __repr__(self):
		return str(self)

# generic func to fetch app store reviews and generate word count.
# note: uses the NLTK data to filter out common "stopwords" (ie: "a", "the", etc.)
def generate_word_count_dict(app_name, country_code, number_of_reviews):
	# fetches app metadata
	app = AppStore(country=country_code, app_name=app_name)
	app.review(how_many=number_of_reviews) #

	# create an object to store and associate words with their respective frequency.
	word_count_dict = {}

	# Get list of English stop words to filter out (ie: "a", "the", etc.) and convert them all to lowercase
	take_out = stopwords.words('english')
	take_out = [word.lower() for word in take_out]

	# iterate through each review
	for review_metadata in app.reviews:
		raw_review = review_metadata["review"]

		# break a single review into individual words
		review_words = raw_review.split()

		for word in review_words:

			# remove non-alphanumeric characters from each word and lowercase everything
			word_filtered = re.sub(r'\W+', '', word).lower()

			# skip words that are common
			if word_filtered not in take_out:
				if word_filtered in word_count_dict:
					word_count_dict[word_filtered] += 1
				else:
					word_count_dict[word_filtered] = 1

	return word_count_dict

# --------PROGRAM EXECUTION BEGINS HERE------------
nltk.download('stopwords')
word_count_dict = generate_word_count_dict("myfitnesspal", "us", 100)

# Uncomment to visualize the generated word count dictionary
# pprint(word_count_dict)

word_count_list = []

# transform {word : count} pair into WordCount object and add to list for sorting.
for key in word_count_dict:
	wordObject = WordCount(key, word_count_dict[key])
	word_count_list.append(wordObject)

# sort our words by frequency they were used
word_count_list.sort(key=lambda x: x.count)

# output the results
print(*word_count_list,sep='\n')


from pymodaq.control_modules.viewer_utility_classes import DAQ_Viewer_base, \
    comon_parameters, main
from pymodaq.utils.parameter import Parameter
from pymodaq.utils.data import DataFromPlugins, DataToExport
import numpy as np
from pathlib import Path
import serial

class PhotoinoController:
    """Controller class for interacting with photoino."""

    available_ports = \
        [str(path) for path in list(Path('/dev/').glob('ttyACM0'))]

    def open(self, port, baudrate):
        if port == '':
            port = self.available_ports[0]
        if baudrate == 0:
            baudrate = 115200

        if hasattr(self, 'ser') and self.ser is not None:
            self.ser.close()

        try:
            self.ser = serial.Serial(port=port, baudrate=baudrate, timeout=1)
        except:
            self.ser = None
            raise ValueError("couldn't open port '%s'" % port)

    def close(self):
        if self.ser is not None:
            self.stop()
            self.ser.close()
            self.ser = None

    def start(self):
        self.ser.write('start\n'.encode())

    def stop(self):
        self.ser.write('stop\n'.encode())

    def receive_number(self):
        while True: # skip leading line breaks
            c = self.ser.read(1)
            if c != b'\n' and c != b'\r':
                break
        result = 0
        while c != b'\n' and c != b'\r':
            result = result * 10 + int(c)
            c = self.ser.read(1)
        n = self.ser.in_waiting # skip trailing stuff
        if n > 0:
            self.ser.read(n)
        return result

    @property
    def count_rate(self):
        self.ser.write(str.encode('rate?\n'))
        return self.receive_number()

    @property
    def trigger_level(self):
        self.ser.write(str.encode('level?\n'))
        return self.receive_number()

    @trigger_level.setter
    def trigger_level(self, value):
        self.ser.write(str.encode('level %f\n' % value))

    @property
    def time_base(self):
        self.ser.write(str.encode('timebase?\n'))
        return self.receive_number()

    @time_base.setter
    def time_base(self, value):
        self.ser.write(str.encode('timebase %f\n' % value))

class DAQ_0DViewer_photoino(DAQ_Viewer_base):
    """PyMoDAQ plugin for controlling photoino single-photon
       counting module"""

    controller_type = PhotoinoController
    serial_ports = PhotoinoController.available_ports

    params = comon_parameters+[
        {'title': 'Serial port:', 'name': 'serial_port', 'type': 'str',
         'limits': serial_ports },
        {'title': 'Baud rate:', 'name': 'baud_rate', 'type': 'int', 'min': 0},
        {'title': 'Time base:', 'name': 'time_base', 'type': 'float',
         'min': 0.1, 'max': 1e5},
        {'title': 'Trigger level:', 'name': 'trigger_level', 'type': 'float',
         'min': -5.0, 'max': 5.0},
    ]

    def ini_attributes(self):
        self.controller: PhotoinoDriver = None

    def ini_detector(self, controller=None):
        """Detector communication initialization

        Parameters
        ----------
        controller: (object)
            custom object of a PyMoDAQ plugin (Slave case). None if only one
            actuator/detector by controller (Master case)

        Returns
        -------
        info: str
        initialized: bool
            False if initialization failed otherwise True
        """

        import pdb
        pdb.set_trace()
        self.ini_detector_init(old_controller=controller,
                               new_controller=self.controller_type())
        self.controller.open(self.settings['serial_port'],
                             self.settings['baud_rate'])

        self.init_params()

        info = "photoino initialised"
        return info, True

    def init_params(self):
        self.controller.time_base = self.settings['time_base']
        self.controller.trigger_level = self.settings['trigger_level']

    def close(self):
        self.controller.close()

    def commit_settings(self, param: Parameter):
        """Apply the consequences of a change of value in the detector settings

        Parameters
        ----------
        param: Parameter
            A given parameter (within detector_settings) whose value has been
            changed by the user
        """
        if param.name() == "time_base":
            self.controller.time_base = self.settings.child('time_base').value()
        elif param.name() == "trigger_level":
            self.controller.trigger_level = \
                self.settings.child('trigger_level').value()

    def grab_data(self, Naverage=1, **kwargs):
        """Start a grab from the detector

        Parameters
        ----------
        Naverage: int
            Number of hardware averaging (if hardware averaging is possible,
            self.hardware_averaging should be set to
            True in class preamble and you should code this implementation)
        kwargs: dict
            others optionals arguments
        """

        data = [np.array([self.controller.count_rate])]
        data_to_emit = DataFromPlugins(name='Photon counter', data=data,
                                       dim='Data0D', labels=['Counts'],)
        self.data_grabed_signal.emit([data_to_emit])

    def stop(self):
        self.controller.stop()
        return ''

if __name__ == '__main__':
    main(__file__)


#!/usr/bin/env python
from vtkmodules.vtkCommonDataModel import vtkSphere
from vtkmodules.vtkFiltersCore import vtkFlyingEdges3D
from vtkmodules.vtkFiltersModeling import vtkOutlineFilter
from vtkmodules.vtkImagingHybrid import vtkSampleFunction
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Create the RenderWindow, Renderer and both Actors
#
ren1 = vtkRenderer()
renWin = vtkRenderWindow()
renWin.SetMultiSamples(0)
renWin.AddRenderer(ren1)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Create a synthetic source
sphere = vtkSphere()
sphere.SetCenter( 0.0,0.0,0.0)
sphere.SetRadius(0.25)

# Iso-surface to create geometry. This uses a very small volume to stress
# boundary conditions in vtkFlyingEdges; i.e., we want the sphere isosurface
# to intersect the boundary.
sample = vtkSampleFunction()
sample.SetImplicitFunction(sphere)
sample.SetModelBounds(-0.5,0.5, -0.5,0.5, -0.5,0.5)
sample.SetSampleDimensions(3,3,3)

iso = vtkFlyingEdges3D()
iso.SetInputConnection(sample.GetOutputPort())
iso.SetValue(0,0.25)
iso.ComputeNormalsOn()
iso.ComputeGradientsOn()
iso.ComputeScalarsOn()
iso.InterpolateAttributesOff()

isoMapper = vtkPolyDataMapper()
isoMapper.SetInputConnection(iso.GetOutputPort())
isoMapper.ScalarVisibilityOff()
isoActor = vtkActor()
isoActor.SetMapper(isoMapper)
isoActor.GetProperty().SetColor(1,1,1)
isoActor.GetProperty().SetOpacity(1)

outline = vtkOutlineFilter()
outline.SetInputConnection(sample.GetOutputPort())
outlineMapper = vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtkActor()
outlineActor.SetMapper(outlineMapper)
outlineProp = outlineActor.GetProperty()

# Add the actors to the renderer, set the background and size
#
ren1.AddActor(outlineActor)
ren1.AddActor(isoActor)
ren1.SetBackground(0,0,0)
renWin.SetSize(400,400)
ren1.ResetCamera()
iren.Initialize()

renWin.Render()
iren.Start()
# --- end of script --


def product(vals):
    d = {"forward" : 0, "up": 0, "down": 0}
    for cmd in vals:
        mv, amt = cmd.split(" ")
        d[mv] += int(amt)

    return d["forward"] * (d["down"]-d["up"])

def aim(vals):
    depth = 0
    horiz = 0
    aim = 0
    for cmd in vals:
        mv, amt = cmd.split(" ")
        amt = int(amt)
        aim += amt if mv == "down" else 0
        aim -= amt if mv == "up" else 0
        depth += amt*aim if mv == "forward" else 0
        horiz += amt if mv == "forward" else 0

    return depth*horiz

def main():
    with open("input.txt", "r") as fp:
        data = [x.strip("\n") for x in fp.readlines()]

    position = product(data)
    print(f"Position product: {product(data)}")
    print(f"Position aim: {aim(data)}")

if __name__ == '__main__':
    main()


import logging
import platform
import sys
import warnings
from pathlib import Path
from typing import Any
from typing import Iterator

import click
from click_default_group import DefaultGroup

from xsdata import __version__
from xsdata.codegen.transformer import SchemaTransformer
from xsdata.logger import logger
from xsdata.models.config import GeneratorConfig
from xsdata.models.config import GeneratorOutput
from xsdata.utils.click import LogFormatter
from xsdata.utils.click import LogHandler
from xsdata.utils.click import model_options
from xsdata.utils.downloader import Downloader
from xsdata.utils.hooks import load_entry_points

# Load cli plugins
load_entry_points("xsdata.plugins.cli")

# Setup xsdata logger to print records to stdout/stderr
handler = LogHandler()
handler.formatter = LogFormatter()

logger.handlers = [handler]
logger.propagate = False

# Attach the cli handler to the python warnings logger
py_warnings = logging.getLogger("py.warnings")
py_warnings.handlers = [handler]
py_warnings.propagate = False

# Log warnings as well
logging.captureWarnings(True)

@click.group(cls=DefaultGroup, default="generate", default_if_no_args=False)
@click.pass_context
@click.version_option(__version__)
def cli(ctx: click.Context, **kwargs: Any):
    """Xsdata command line interface."""
    logger.setLevel(logging.INFO)
    formatwarning_orig = warnings.formatwarning
    logger.info(
        "========= xsdata v%s / Python %s / Platform %s =========\n",
        __version__,
        platform.python_version(),
        sys.platform,
    )

    def format_warning(message: Any, category: Any, *args: Any) -> str:
        return (
            f"{category.__name__}: {message}" if category else message
        )  # pragma: no cover

    def format_warning_restore():
        warnings.formatwarning = formatwarning_orig

    warnings.formatwarning = format_warning  # type: ignore

    ctx.call_on_close(format_warning_restore)

@cli.command("init-config")
@click.argument("output", type=click.Path(), default=".xsdata.xml")
@click.option("-pp", "--print", is_flag=True, default=False, help="Print output")
def init_config(**kwargs: Any):
    """Create or update a configuration file."""
    file_path = Path(kwargs["output"])
    if file_path.exists():
        config = GeneratorConfig.read(file_path)
        logger.info("Updating configuration file %s", kwargs["output"])
    else:
        logger.info("Initializing configuration file %s", kwargs["output"])
        config = GeneratorConfig.create()

    if kwargs["print"]:
        config.write(sys.stdout, config)
    else:
        with file_path.open("w") as fp:
            config.write(fp, config)

    handler.emit_warnings()

@cli.command("download")
@click.argument("source", required=True)
@click.option(
    "-o",
    "--output",
    type=click.Path(),
    default="./",
    help="Output directory, default cwd",
)
def download(source: str, output: str):
    """Download a schema or a definition locally with all its dependencies."""
    downloader = Downloader(output=Path(output).resolve())
    downloader.wget(source)

    handler.emit_warnings()

@cli.command("generate")
@click.argument("source", required=True)
@click.option(
    "-r",
    "--recursive",
    is_flag=True,
    default=False,
    help="Search files recursively in the source directory",
)
@click.option("-c", "--config", default=".xsdata.xml", help="Project configuration")
@click.option("-pp", "--print", is_flag=True, default=False, help="Print output")
@click.option("--cache", is_flag=True, default=False, help="Cache sources loading")
@click.option("--debug", is_flag=True, default=False, help="Show debug messages")
@model_options(GeneratorOutput)
def generate(**kwargs: Any):
    """
    Generate code from xml schemas, webservice definitions and any xml or json
    document.

    The input source can be either a filepath, uri or a directory
    containing xml, json, xsd and wsdl files.
    """

    debug = kwargs.pop("debug")
    if debug:
        logger.setLevel(logging.DEBUG)

    source = kwargs.pop("source")
    stdout = kwargs.pop("print")
    cache = kwargs.pop("cache")
    recursive = kwargs.pop("recursive")
    config_file = Path(kwargs.pop("config")).resolve()

    params = {k.replace("__", "."): v for k, v in kwargs.items() if v is not None}
    config = GeneratorConfig.read(config_file)
    config.output.update(**params)

    transformer = SchemaTransformer(config=config, print=stdout)
    uris = sorted(resolve_source(source, recursive=recursive))
    transformer.process(uris, cache=cache)

    handler.emit_warnings()

def resolve_source(source: str, recursive: bool) -> Iterator[str]:
    if source.find("://") > -1 and not source.startswith("file://"):
        yield source
    else:
        path = Path(source).resolve()
        match = "**/*" if recursive else "*"
        if path.is_dir():
            for ext in ["wsdl", "xsd", "dtd", "xml", "json"]:
                yield from (x.as_uri() for x in path.glob(f"{match}.{ext}"))
        else:  # is file
            yield path.as_uri()

if __name__ == "__main__":  # pragma: no cover
    cli()


from math import atan2
from collections import defaultdict

with open('input', 'r') as f:
    the_final_frontier = f.read().splitlines()

height = len(the_final_frontier)
width = len(the_final_frontier[0])

for i in range(len(the_final_frontier)):
    the_final_frontier[i] = [j for j in the_final_frontier[i]]

def get_angle(p1, p2):
    dx = p2[0] - p1[0]
    dy = p2[1] - p1[1]
    angle = atan2(dy, dx)
    return angle

asteroids = []

for y, r in enumerate(the_final_frontier):
    for x, value in enumerate(r):
        if value == '#':
            asteroids.append((x,y))

# logic behind the program
# from every single asteroid to all the other asteroids, calculate the angles
# the number of visible asteroids will simply be equal to the number of
# unique angles

angles = defaultdict(set)

for i in asteroids:
    for j in [k for k in asteroids if k!=i]:
        angles.setdefault(i,set()).add(get_angle(i,j))

asteroid = None
max_vis = 0

for ast, vis in angles.items():
    if len(vis)>max_vis:
        asteroid = ast
        max_vis = len(vis)

print(asteroid)
print(max_vis)


from typing import Mapping, Optional

import numpy as np
from scipy import special as scipy_special

from backend.return_period_calculator import base_fitter
from backend.return_period_calculator import exceptions
from backend.return_period_calculator import grubbs_beck_tester as gbt
from backend.return_period_calculator import theoretical_distribution_utilities as tdu

# This default value is defined on page 83.
_DEFAULT_CONVERGNCE_THRESHOLD = 1e-10
_MAX_ITERATIONS = 1000

# pylint: disable=g-complex-comprehension

# TODO(gsnearing): Add historical and gap data to EMA.
# TODO(gsnearing): Add regional skew to EMA.

def _parameters_from_central_moments(
    moments: dict[str, float],
) -> dict[str, float]:
  """Estimate distribution parameters from central moments."""
  # This is equation 7-13 (page 83).
  alpha, beta, tau = tdu.parameters_from_moments(
      mean=moments['M'],
      std=moments['S'],
      skew=moments['G'],
  )
  return {'alpha': alpha, 'beta': beta, 'tau': tau}

def _central_moments_from_data(
    data: np.ndarray,
) -> dict[str, float]:
  """Estimate distribution parameters from sample."""
  mean, std, skew = tdu.sample_moments(data=data)
  return {'M': mean, 'S': std, 'G': skew}

def _partial_gamma_integral(
    alpha: float,
    lower: float,
    upper: float,
) -> float:
  upper_gamma = scipy_special.gammainc(alpha, max(0, upper))
  lower_gamma = scipy_special.gammainc(alpha, max(0, lower))
  alpha_gamma = scipy_special.gamma(alpha)
  return alpha_gamma * (upper_gamma - lower_gamma)

def _expected_value_on_interval(
    lower_bound: float,
    upper_bound: float,
    alpha: float,
    beta: float,
    tau: float,
    moment: int,
) -> float:
  """Estimate central moment on interval data."""
  # Implements equation 7-18.

  if upper_bound == lower_bound:
    return upper_bound**moment

  scaled_lower = (lower_bound - tau) / beta
  scaled_upper = (upper_bound - tau) / beta
  if beta < 0:
    denominator = _partial_gamma_integral(
        alpha=alpha,
        lower=scaled_upper,
        upper=scaled_lower,
    )
  else:
    denominator = _partial_gamma_integral(
        alpha=alpha,
        lower=scaled_lower,
        upper=scaled_upper,
    )

  summation = 0
  for j in range(moment + 1):
    if beta < 0:
      numerator = _partial_gamma_integral(
          alpha=alpha+j,
          lower=scaled_upper,
          upper=scaled_lower,
      )
    else:
      numerator = _partial_gamma_integral(
          alpha=alpha+j,
          lower=scaled_lower,
          upper=scaled_upper,
      )
    summation += scipy_special.comb(moment, j) * beta**j * tau**(moment - j) * (
        numerator / denominator)

  return summation

def _expected_centralized_value_on_interval(
    lower_bound: float,
    upper_bound: float,
    alpha: float,
    beta: float,
    tau: float,
    mean: float,
    moment: int,
) -> float:
  """..."""
  summation = 0.
  for l in range(moment + 1):
    interval_expected_value = _expected_value_on_interval(
        lower_bound=lower_bound,
        upper_bound=upper_bound,
        alpha=alpha,
        beta=beta,
        tau=tau,
        moment=l,
    )
    summation += (
        scipy_special.comb(moment, l) *
        interval_expected_value *
        (-mean)**(moment - l)
    )
  return summation

def _central_moments_from_data_and_parameters(
    lower_bounds: np.ndarray,
    upper_bounds: np.ndarray,
    parameters: dict[str, float],
) -> dict[str, float]:
  """Estimate central moments from data and distribution parameters."""

  # Number of samples.
  num_samples = len(lower_bounds)

  # Estimate first moment of distribution (equation 7-15).
  interval_expected_values1 = [
      _expected_value_on_interval(
          lower_bound=lb,
          upper_bound=ub,
          alpha=parameters['alpha'],
          beta=parameters['beta'],
          tau=parameters['tau'],
          moment=1,
      ) for (lb, ub) in zip(lower_bounds, upper_bounds)
  ]
  mean = 1 / num_samples * np.sum(interval_expected_values1)

  # Estimate second moment of distribution (equation 7-15).
  interval_expected_values2 = [
      _expected_centralized_value_on_interval(
          lower_bound=lb,
          upper_bound=ub,
          alpha=parameters['alpha'],
          beta=parameters['beta'],
          tau=parameters['tau'],
          mean=mean,
          moment=2,
      ) for (lb, ub) in zip(lower_bounds, upper_bounds)
  ]
  stdev = np.sqrt(1 / (num_samples - 1) * np.sum(interval_expected_values2))

  # Estimate third moment of distribution (equation 7-15).
  interval_expected_values3 = [
      _expected_centralized_value_on_interval(
          lower_bound=lb,
          upper_bound=ub,
          alpha=parameters['alpha'],
          beta=parameters['beta'],
          tau=parameters['tau'],
          mean=mean,
          moment=3,
      ) for (lb, ub) in zip(lower_bounds, upper_bounds)
  ]
  skew = num_samples / stdev**3 / (num_samples - 1) / (
      num_samples - 2) * np.sum(interval_expected_values3)

  return {'M': mean, 'S': stdev, 'G': skew}

def _check_convergence_norm1(
    current_moments: dict[str, float],
    previous_moments: dict[str, float],
    convergence_threshold: Optional[float] = None,
) -> bool:
  """Checks whether the EMA algorithm has converged using first norm."""
  # Set default convergence threshold if a value is not supplied.
  if not convergence_threshold:
    convergence_threshold = _DEFAULT_CONVERGNCE_THRESHOLD

  # Calculate first norm.
  differences = [
      current_moments[key] - previous_moments[key] for key in current_moments
  ]
  norm_of_differences = np.sum(np.abs(differences))

  # Check norm against threshold.
  return norm_of_differences < convergence_threshold

def _ema(
    systematic_record: np.ndarray,
    pilf_threshold: float,
    convergence_threshold: Optional[float] = None,
) -> dict[str, float]:
  """Implements the Genearlized Expected Moments Algorithm (EMA).

  This is the full fitting procedure from Bulletin 17c, and is the main
  difference between that and the 1981 USGS protocol from Bulletin 17b.
  This algorithm is described on page 27 of Bulletin 17c, with full
  implementation details given in Appendix 7 (page 82).

  Args:
    systematic_record: Systematic data record of flood peaks.
      Must be in transformed units.
    pilf_threshold: As determined by the MGBT test. Units must match
      systematic record.
    convergence_threshold: Convergence threshold to be applied to the first
      norm of the moments of the EMA-estimated distribution. The default value
      is 1e-10.

  Returns:
    dict of fit parameters keyed by parameter name according to Equations 8-10.

  Raises:
    NumericalFittingError if there are nans or infs in iterative algorithm.
  """
  # Turn all data into lower and upper bounds.
  num_pilfs = len(systematic_record[systematic_record < pilf_threshold])
  lower_bounds = systematic_record[systematic_record >= pilf_threshold]
  upper_bounds = systematic_record[systematic_record >= pilf_threshold]
  if num_pilfs > 0:
    lower_bounds = np.concatenate([
        np.full(num_pilfs, -np.inf),
        lower_bounds,
    ])
    upper_bounds = np.concatenate([
        np.full(num_pilfs, pilf_threshold),
        upper_bounds,
    ])

  # Steps in this algorithm are listed on pages 83-84.
  # Step #1a: Initial estimates of central moments.
  # These are used for the first expected value calculations and also for the
  # first convergence check.
  previous_moments = _central_moments_from_data(data=systematic_record)

  # Step #2: Expectation-maximization loop.
  converged = False
  iteration = 0
  while not converged:
    iteration += 1

    # Update distribution parameters.
    parameters = _parameters_from_central_moments(moments=previous_moments)

    # Step #2a: Update expected moments.
    expected_moments = _central_moments_from_data_and_parameters(
        lower_bounds=lower_bounds,
        upper_bounds=upper_bounds,
        parameters=parameters,
    )

    # Error checking.
    if (np.isnan([val for val in expected_moments.values()]).any()
        or np.isinf([val for val in expected_moments.values()]).any()):
      raise exceptions.NumericalFittingError(
          routine='GEMA',
          condition=f'NaN or inf found on iteration {iteration}.',
      )

    # Step #2b: Weight with regional skew.
    # TODO(gsnearing) Implement regional skew.

    # Step #2c: Check for convergence.
    converged = _check_convergence_norm1(
        current_moments=expected_moments,
        previous_moments=previous_moments,
        convergence_threshold=convergence_threshold,
    )
    previous_moments = expected_moments

    if iteration > _MAX_ITERATIONS:
      raise exceptions.NumericalFittingError(
          routine='GEMA',
          condition='max iterations reached'
      )

  return parameters

class GEMAFitter(base_fitter.BaseFitter):
  """Estimates return periods using the Generalized Expected Moments Algorithm.

  This is the baseline algorithm from Bulletin 17c.
  """

  def __init__(
      self,
      data: np.ndarray,
      kn_table: Optional[Mapping[int, float]] = None,
      convergence_threshold: Optional[float] = None,
      log_transform: bool = True
  ):
    """Constructor for a GEMA distribution fitter.

    Fits parameters of a log-Pearson-III distribution with the iterative EMA
    procedure, using the generalized versions of the distribution moments
    and interval moments.

    Args:
      data: Flow peaks to fit in physical units.
      kn_table: Custom test statistics table to override the Kn Table from
        Bulletin 17b in the Standard Grubbs Beck Test (GBT).
      convergence_threshold: Convergence threshold to be applied to the first
        norm of the moments of the EMA-estimated distribution. The default value
        is 1e-10.
      log_transform: Whether to transform the data before fitting a
        distribution.
    """
    super().__init__(
        data=data,
        log_transform=log_transform
    )

    # Find the PILF threshold.
    # TODO(gsnearing): Use Multiple Grubbs-Beck Test instead of Grubbs-Beck
    # Test.
    self._pilf_tester = gbt.GrubbsBeckTester(
        data=self.transformed_sample,
        kn_table=kn_table,
    )

    # Run the EMA algorithm.
    self._distribution_parameters = _ema(
        systematic_record=self.transformed_sample,
        pilf_threshold=self._pilf_tester.pilf_threshold,
        convergence_threshold=convergence_threshold,
    )

  @property
  def type_name(self) -> str:
    return self.__class__.__name__

  def exceedance_probabilities_from_flow_values(
      self,
      flows: np.ndarray,
  ) -> np.ndarray:
    """Predicts exceedance probabilities from streamflow values.

    Args:
      flows: Streamflow values in physical units.

    Returns:
      Predicted exceedance probabilities.

    Raises:
      ValueError if return periods are requested for zero-flows.
    """
    if np.any(flows <= 0):
      raise ValueError('All flow values must be positive.')
    transformed_flows = self._transform_data(flows)
    return 1 - tdu.pearson3_cdf(
        alpha=self._distribution_parameters['alpha'],
        beta=self._distribution_parameters['beta'],
        tau=self._distribution_parameters['tau'],
        values=transformed_flows,
    )

  def flow_values_from_exceedance_probabilities(
      self,
      exceedance_probabilities: np.ndarray,
  ) -> np.ndarray:
    """Predicts from pre-fit log-linear regression.

    Args:
      exceedance_probabilities: Probability of exceeding a particular flow value
        in a given year.

    Returns:
      Flow values corresponding to requeseted exceedance_probabilities.

    Raises:
      ValueError if cumulative probailities are outside realistic ranges, or
        include 0 or 1.
    """
    transformed_flow_values = tdu.pearson3_invcdf(
        alpha=self._distribution_parameters['alpha'],
        beta=self._distribution_parameters['beta'],
        tau=self._distribution_parameters['tau'],
        quantiles=(1 - exceedance_probabilities),
    )
    return self._untransform_data(data=transformed_flow_values)


#最初的深度神经网络的版本
import random
import json

# Third-party libraries
import numpy as np

np.seterr(divide='ignore', invalid='ignore')
class Quadratic(object):

    @staticmethod
    def cost_result(a, y, z= None):
        """return the output_result of this cost function"""
        return 0.5 * (a - y)**2

    @staticmethod
    def output_delta(a, y, z= None):
        """return the derivative of this cost function"""
        return (a - y) * sigmoid_derivative(z)

class LogLikehood(object):

    @staticmethod
    def cost_result(a, y, z= None):
        """return the output_result of this cost function"""
        return (- y * np.log(a))

    @staticmethod
    def output_delta(a, y, z= None):
        """return the derivative of this cost function"""
        return (a - y)

class CrossEntropy(object):

    @staticmethod
    def cost_result(a, y, z= None):
        """return the output_result of this cost function"""
        return (-y * np.log(a) - (1 - y) * np.log(1-a))

    @staticmethod
    def output_delta(a, y, z= None):
        """return the derivative of this cost function"""
        pass

class FCLayer(object):

    def __init__(self, sizes, cost= LogLikehood):

        self.num_layers = len(sizes)
        self.sizes = sizes
        self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
        self.weights = [np.random.randn(y, x) / np.sqrt(x)
                        for x, y in zip(sizes[:-1], sizes[1:])]
        self.cost = cost

    def feedforward(self, a):
        """Return the output of the network if ``a`` is input."""
        for b, w in zip(self.biases[:-1], self.weights[:-1]):
            #a = sigmoid(np.dot(w, a)+b)
            z = np.dot(w, a) + b
            # print("mean:", np.mean(z))
            # print("std:", np.std(z))
            a = tanh(np.dot(w, a) + b)
        a = softmax(np.dot(self.weights[-1], a) + self.biases[-1])
        return a

    def SGD(self, training_data, epochs, mini_batch_size, eta, test_data = None):
        if test_data:
            n_test = len(test_data)
        n_train = len(training_data)
        test_error = []
        log_cost = []
        for j in range(epochs):
            #打乱顺序
            random.shuffle(training_data)
            mini_batches = [training_data[k: k + mini_batch_size] for k in range(0, n_train, mini_batch_size)]
            for mini_batch in mini_batches:
                # self.update_mini_batch(mini_batch, eta)
                self.update_mini_batch(mini_batch, eta)

            if test_data:
                print("Epoch {0}: {1} / {2}".format(j, self.evaluate(test_data), n_test))
            else:
                print("Epoch {0} complete".format(j))
            # test_cost.append(self.cost(test_data).tolist())
            # test_error.append(self.evaluate(test_data) / n_test)
        # with open("C:/Users/Forest-fall/Documents/GitHub/NN-homework/test_accuracy","w") as f:
        #     json.dump(test_error, f)

        #version2
        # for j in range(epochs):
        #     random.shuffle(training_data)

        #     batch_epoch = 0
        #     for k in range(0, n_train, mini_batch_size):
        #         mini_batch = training_data[k: k + mini_batch_size]
        #         batch_epoch += 1
        #         self.update_mini_batch(mini_batch, eta, test_data, j, batch_epoch, n_test, test_error, log_cost)

    def update_mini_batch(self, mini_batch, eta,         ):#test_data, j, batch_epoch, n_test, test_error, log_cost

        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        for x, y in mini_batch:
            delta_nabla_b, delta_nabla_w = self.backprop(x, y) #log_cost
            nabla_b = [nb + dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
            nabla_w = [nw + dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
        self.weights = [w - (eta  /len(mini_batch)) * nw for w, nw in zip(self.weights, nabla_w)]
        self.biases = [b - (eta / len(mini_batch)) * nb for b, nb in zip(self.biases, nabla_b)]
        #如果每一批都看一次正确率呢？？
        # if test_data:
        #     print("Epoch {0}, batch {1}: {2} / {3}".format(j, batch_epoch, self.evaluate(test_data), n_test))
        # else:
        #     print("Epoch {0} complete".format(j))
        # test_error.append(self.evaluate(test_data) / n_test)
        # with open("C:/Users/Forest-fall/Documents/GitHub/NN-homework/batch_accuracy", "w") as f:
        #     json.dump(test_error, f)

    def backprop(self, x, y): #,      log_cost
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        # feedforward
        activation = x
        activations = [x] # list to store all the activations, layer by layer
        zs = [] # list to store all the z vectors, layer by layer
        for b, w in zip(self.biases[:-1], self.weights[:-1]):
            z = np.dot(w, activation) + b
            zs.append(z)
            #activation = sigmoid(z)
            activation = tanh(z)
            activations.append(activation)
        z = np.dot(self.weights[-1], activation) + self.biases[-1]
        zs.append(z)
        activation = softmax(z)
        activations.append(activation)

        #计算Loss
        # y_index = np.argmax(y)
        # a = activations[-1][y_index]
        # loss = (self.cost).cost_result(a,1).tolist()[0]
        # log_cost.append(loss)
        # with open("C:/Users/Forest-fall/Documents/GitHub/NN-homework/log_cost", "w") as f:
        #     json.dump(log_cost, f)

        # backward pass
        #delta = self.cost_derivative(activations[-1], y) * sigmoid_derivative(zs[-1])

        delta = (self.cost).output_delta(activations[-1], y, zs[-1]) #activations[-1] - y
        nabla_b[-1] = delta
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())

        for l in range(2, self.num_layers):
            z = zs[-l]
            #activation_derivative = sigmoid_derivative(z)
            activation_derivative = tanh_derivative(z)
            #Hadamard乘积
            delta = np.dot(self.weights[-l + 1].transpose(), delta) * activation_derivative
            nabla_b[-l] = delta
            nabla_w[-l] = np.dot(delta, activations[-l - 1].transpose())
        return (nabla_b, nabla_w)

    def evaluate(self, test_data):
        "caluculate accuracy on test data"
        test_results =[]
        for x, y in test_data:
            y_predict = np.argmax(self.feedforward(x))
            test_results.append((y_predict, y))
        # test_results = [(np.argmax(self.feedforward(x)), y) for (x, y) in test_data]
        return sum(int(x == y) for (x, y) in test_results)

    # def cost(self, data):
    #     cost = 0
    #     for x, y in data:
    #         cost += -y * np.log(self.feedforward(x)) / len(data)
    #     return cost

    # def cost_derivative(self, output_activations, y):
    #     """Return the vector of partial derivatives  (partial C_x \partial a )for the output activations."""
    #     return (output_activations - y)

#### Miscellaneous functions
def sigmoid(z):
    """The sigmoid function."""
    return 1.0 / (1.0 + np.exp(-z))

def sigmoid_derivative(z):
    """Derivative of the sigmoid function."""
    return sigmoid(z) * (1.0 - sigmoid(z))

def softmax(z):
    """The softmax funtcion."""
    return np.exp(z) / sum(np.exp(z))

def softmax_derivative(z):
    """Derivative of softmax function."""
    return softmax(z) * (1.0 - softmax(z))

def tanh(z):
    """The tanh function."""
    return 1.0 - (2.0 / (np.exp(2.0 * z) + 1.0))

def tanh_derivative(z):
    """Derivative of the tanh function."""
    return 1.0 - (tanh(z)*tanh(z))

def vectorized_result(j):
    v = np.zeros((10, 1))
    v[j] = 1.0
    return v