"""General-purpose training script for image-to-image translation.

This script works for various models (with option '--model': e.g., pix2pix, cyclegan, colorization) and
different datasets (with option '--dataset_mode': e.g., aligned, unaligned, single, colorization).
You need to specify the dataset ('--dataroot'), experiment name ('--name'), and model ('--model').

It first creates model, dataset, and visualizer given the option.
It then does standard network training. During the training, it also visualize/save the images, print/save the loss plot, and save models.
The script supports continue/resume training. Use '--continue_train' to resume your previous training.

Example:
    Train a CycleGAN model:
        python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
    Train a pix2pix model:
        python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA

See options/base_options.py and options/train_options.py for more training options.
See training and test tips at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md
See frequently asked questions at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md
"""
import time
from options.train_options import TrainOptions
from data import create_dataset
from models import create_model
from util.visualizer import Visualizer
import matplotlib.pyplot as plt
import os
import pandas as pd
import torch
import numpy as np

if __name__ == '__main__':

    torch.cuda.empty_cache()
    opt = TrainOptions().parse()  # get training options
    dataset = create_dataset(opt)  # create a dataset given opt.dataset_mode and other options
    dataset_size = len(dataset)  # get the number of images in the dataset.
    print('The number of training images = %d' % dataset_size)
    print('shuffle :{}'.format(not opt.serial_batches))
    model = create_model(opt)  # create a model given opt.model and other options
    # model.print_networks(verbose=True)
    model.setup(opt)  # regular setup: load and print networks; create schedulers
    visualizer = Visualizer(opt)  # create a visualizer that display/save images and plots
    total_iters = 0  # the total number of training iterations
    total_time_start = time.time()
    loss_summary = []
    loss_summary_test = []

    # ##Create test dataset for evaluation of test loss
    optTest = opt
    optTest.phase = 'test'
    optTest.num_threads = 0  # test code only supports num_threads = 1
    optTest.batch_size = 1  # test code only supports batch_size = 1
    optTest.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
    optTest.no_flip = True  # no flip; comment this line if results on flipped images are needed.
    optTest.display_id = -1  # no visdom display; the test code saves the results to a HTML file.
    optTest.isTrain = False
    optTest.mode = 'eval'
    dataset_test = create_dataset(optTest)
    dataset_size_test = len(dataset_test)  # get the number of images in the dataset.
    print('The number of validation images = %d' % dataset_size_test)

    for epoch in range(opt.epoch_count,
                       opt.n_epochs + opt.n_epochs_decay + 1):  # outer loop for different epochs; we save the model by <epoch_count>, <epoch_count>+<save_latest_freq>
        epoch_start_time = time.time()  # timer for entire epoch
        iter_data_time = time.time()  # timer for data loading per iteration
        epoch_iter = 0  # the number of training iterations in current epoch, reset to 0 every epoch
        visualizer.reset()  # reset the visualizer: make sure it saves the results to HTML at least once every epoch

        model.set_epoch(epoch)

        total_loss_each_epoch = 0
        for i, data in enumerate(dataset):  # inner loop within one epoch
            iter_start_time = time.time()  # timer for computation per iteration
            if total_iters % opt.print_freq == 0:
                t_data = iter_start_time - iter_data_time

            total_iters += opt.batch_size
            epoch_iter += opt.batch_size
            model.set_input(data)  # unpack data from dataset and apply preprocessing
            # if i == 0:
            #     model.save_weights(opt)
            model.optimize_parameters()  # calculate loss functions, get gradients, update network weights

            if total_iters % opt.display_freq == 0:  # display images on visdom and save images to a HTML file
                save_result = total_iters % opt.update_html_freq == 0
                model.compute_visuals()
                visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)

            if total_iters % opt.print_freq == 0:  # print training losses and save logging information to the disk
                losses = model.get_current_losses()
                total_loss_each_epoch = total_loss_each_epoch + sum(losses.values())
                t_comp = (time.time() - iter_start_time) / opt.batch_size
                visualizer.print_current_losses(epoch, epoch_iter, losses, t_comp, t_data)
                # if opt.display_id > 0:
                #     visualizer.plot_current_losses(epoch, float(epoch_iter) / dataset_size, losses)

            if total_iters % opt.save_latest_freq == 0:  # cache our latest model every <save_latest_freq> iterations
                print('saving the latest model (epoch %d, total_iters %d)' % (epoch, total_iters))
                save_suffix = 'iter_%d' % total_iters if opt.save_by_iter else 'latest'
                model.save_networks(save_suffix)

            iter_data_time = time.time()
        if epoch % opt.save_epoch_freq == 0:  # cache our model every <save_epoch_freq> epochs
            print('saving the model at the end of epoch %d, iters %d' % (epoch, total_iters))
            model.save_networks('latest')
            model.save_networks(epoch)

        print('End of epoch %d / %d \t Time Taken: %d sec' % (epoch, opt.n_epochs + opt.n_epochs_decay, time.time() - epoch_start_time))
        model.update_learning_rate()  # update learning rates at the end of every epoch.
        avg_loss_each_epoch_train = total_loss_each_epoch/len(dataset)
        print('Avg Loss at the end of epoch {}:{}'.format(epoch, avg_loss_each_epoch_train))
        loss_summary.append(avg_loss_each_epoch_train)

        #Evaluating Validation Loss
        total_loss_each_epoch_test = 0
        model.eval()
        for i, data_test in enumerate(dataset_test):
            # if i >= opt.num_test:  # only apply our model to opt.num_test images.
            #   break
            with torch.no_grad():
                model.set_input(data_test)  # unpack data from data loader
                model.test()
                model.calculate_validation_loss()
                val_losses = model.get_current_losses()
                # print('Validation loss for iter {} is {}'.format(i+1, val_losses))
            total_loss_each_epoch_test = total_loss_each_epoch_test + sum(val_losses.values())
        avg_loss_each_epoch_test = total_loss_each_epoch_test / len(dataset_test)
        print('Avg Test Loss at the end of epoch {}:{}'.format(epoch, avg_loss_each_epoch_test))
        loss_summary_test.append(avg_loss_each_epoch_test)
        ##Reset to training mode
        model.train()

    # plt.plot(losssummary, np.arange(opt.epoch_count, opt.n_epochs + opt.n_epochs_decay, 1), 'r--')
    total_time_end = time.time()
    total_time = total_time_end - total_time_start
    print('Total Time for training is {}'.format(total_time))

    ##print total loss after each epoch
    loss_dir = os.path.join(opt.checkpoints_dir, opt.name)
    loss_filename = 'loss_log.txt'
    loss_filename = os.path.join(loss_dir, loss_filename)
    with open(loss_filename, 'a') as f:
        print('Total Time for training is {}'.format(total_time), file=f)

    loss_filename = 'total_loss_train.txt'
    loss_filename = os.path.join(loss_dir, loss_filename)
    # print(loss_summary)
    print(loss_filename)
    with open(loss_filename, 'w') as f:
        for i, line in enumerate(loss_summary):
            print("{} {}".format(i + 1, line), file=f)
    df = pd.read_csv(loss_filename, delim_whitespace=True, header=None)
    df.columns = ["Epoch", "Train_Loss"]
    df.to_excel(r"{}.xlsx".format(loss_filename))

    loss_filename = 'total_loss_test.txt'
    loss_filename = os.path.join(loss_dir, loss_filename)
    # print(loss_summary)
    print(loss_filename)
    with open(loss_filename, 'w') as f:
        for i, line in enumerate(loss_summary_test):
            print("{} {}".format(i + 1, line), file=f)
    df = pd.read_csv(loss_filename, delim_whitespace=True, header=None)
    df.columns = ["Epoch", "Test_Loss"]
    df.to_excel(r"{}.xlsx".format(loss_filename))


import schedule
import time
import subprocess
import datetime

def job():
    proc = subprocess.Popen(["python3","my_google_meet_manager.py"],stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
    stdout, stderr = proc.communicate()
    print(stdout.decode())
    print(stderr.decode())

# Run job from Mon to Fri every hour from 8:30-4:30

schedule.every().day.hour.at(":30").do(job)

while True :
    curr_time = datetime.datetime.now()
    curr_time = curr_time.strftime('%H:%M')
    print(curr_time)
    if curr_time == "12:30":
        print("Sleeping for 50 minutes..")
        time.sleep(3000)
    schedule.run_pending()
    time.sleep(5)


from collections import defaultdict
import json
from ..common.log_utils import log

from tsalib.ts import get_decls
from tsalib.backend import get_backend_by_name, get_str_type

tensor_classes = {
    'torch': ['torch.Tensor', 'torch.nn.parameter.Parameter', 'torch.nn.modules.sparse.Embedding'],
    # 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'
    'tensorflow': ['ops.resource_variable_ops.ResourceVariable', 'ops.Tensor', 'ops.EagerTensor']
}

def is_name_in_class (name, class_names):
    return any([k in name for k in class_names])

def is_tensor(x, debug):
    t = get_str_type(x)

    if 'numpy.' in t: ret = ('numpy.ndarray' in t)
    elif 'torch.' in t:
        ret = any([x in t for x in tensor_classes['torch']])
    elif 'tensorflow.' in t:
        ret = is_name_in_class(t, tensor_classes['tensorflow'])
    else: ret = False

    if debug and not ret:
        log (f'shape_cache: Not is_tensor: {t}', style='red')

    return ret

class ShapeCache:
    def __init__ (self, debug, backend):
        self.var2shape = defaultdict(dict)
        self.func2ann = defaultdict(dict) # forward -> {lineno: shape}
        self.sz2name = None
        self.debug = debug

        self.BE = get_backend_by_name(backend)

    def make_decl_map (self):
        if self.sz2name is None:
            decl_dvs = get_decls().values()
            self.sz2name = {dv.size: dv.shortname for dv in decl_dvs}

    def get_shape(self, val):
        self.make_decl_map()
        shape = self.BE.shape(val)
        actual_shape_ann = [self.sz2name[s] if s in self.sz2name else str(s) for s in shape]
        return shape, ','.join(actual_shape_ann)

    def update_var_shape(self, v, val, func_name, filename, lineno, show=False):
        if not is_tensor(val, self.debug): return None

        #print ('update_var_shape', type(val))
        if not isinstance(val, (list, tuple) ):
            shape, actual_shape_ann = self.get_shape(val)
        else:
            lenv = len(val)
            shape, actual_shape_ann = self.get_shape(val[0])
            shape = (lenv, shape)
            actual_shape_ann = (lenv, actual_shape_ann)

        self.var2shape[func_name][v] = shape
        self.func2ann[func_name+','+filename][lineno] = (actual_shape_ann, v)
        if show:
            log(f'\nUpdate at line {lineno}: actual shape of {v} = {actual_shape_ann}')
        return shape

    def shape_check (self, v, shape_ann, func_name, lineno, verbose=True):
        cache = self.var2shape[func_name]
        assert v in cache
        store_shape = tuple(cache[v])
        #shape = resolve_to_int_tuple(shape)
        #print (type(shape[0]))
        if store_shape == shape_ann:
            log (f'>> shape check succeeded at line {lineno}', style='green')
        else:
            log (f'>> FAILED shape check at line {lineno}', style='red')
            log (f'expected: {shape_ann}, actual: {store_shape}', style='red')
            #assert False

    def save(self, fname):
        print (f'saving shapes to {fname} ..')
        json_obj = (dict(self.func2ann))
        with open(fname, "w", encoding='utf-8') as of:
            json.dump(json_obj, of, indent=2)


import streamlit as st
import pandas as pd
import folium
import io
from streamlit_folium import folium_static
import requests
import json
import ast
import crawling

AK= "iNcU8nqYs08Le0v8k9zehlCIOEXWEw7o"
output = "json"

#=======================以下是各类函数===========================
def WGS_to_bd(coord):
    x = coord[0]
    y = coord[1]
    service = "http://api.map.baidu.com/geoconv/v1/?"
    f = 1; t = 5
    parameters = f"coords={x},{y}&from={f}&to={t}&ak={AK}"
    url = service + parameters
    response = requests.get(url)
    s=response.text
    dic=json.loads(s)
    return [dic["result"][0]["x"],dic["result"][0]["y"]]

#坐标纠差
def rectify(lng,lat):
    #对地理编码得到的坐标进行转换，并计算偏差
    d_lng = lng - WGS_to_bd([lng,lat])[0]
    d_lat = lat - WGS_to_bd([lng,lat])[1]
    #根据偏差对坐标进行纠偏
    WGS_lng = lng + d_lng
    WGS_lat = lat + d_lat
    return [WGS_lng,WGS_lat]

def get_location(address):
    service ="http://api.map.baidu.com/geocoding/v3/?"
    city="上海市"
    parameters = f"address={address}&output={output}&ak={AK}&city={city}"
    url = service + parameters
    response = requests.get(url)
    text=response.text
    dic=json.loads(text)
    status = dic["status"]
    if status==0:
        lng = dic["result"]["location"]["lng"]
        lat = dic["result"]["location"]["lat"]
        [WGS_lng,WGS_lat]=rectify(lng,lat)
        return [WGS_lng,WGS_lat]
    else:
        print(f"{address}:地理编码不成功")
        return["unknown","unknown"]

def getPOIs(address):
    #以下为获取poi内容
    lat = get_location(address)[1]
    lng = get_location(address)[0]
    classes=["教育培训","交通设施","医疗","美食"]
    service ="http://api.map.baidu.com/place/v2/search?"
    for clas in classes:
        df=pd.DataFrame()
        query = clas
        #取10公里内的poi点
        radius = 10000
        #指定输出坐标系为WGS84
        coord_type=3
        parameters = f"query={query}&location={lat},{lng}&radius={radius}\
        &output={output}&coord_type={coord_type}&ak={AK}"
        url = service + parameters
        response = requests.get(url)
        text=response.text
        dic=json.loads(text)
        if dic["status"]==0:
            for result in dic["results"]:
                #只将每个poi的名字和经纬度录入
                allowed_keys=["name","navi_location"]
                filter_result={key:result[key] for key in filter(lambda x:x in allowed_keys,result)}
                df=df.append(filter_result,ignore_index=True)
        else:
            print("POI检索不成功")
        df.to_csv(f"{clas}poi.csv")

def cal_distance_duration(address):
    o_lat = get_location(address)[1]
    o_lng = get_location(address)[0]
    service="http://api.map.baidu.com/directionlite/v1/driving?"
    classes=["教育培训","交通设施","医疗","美食"]
    #destination=poi
    for clas in classes:
        frame = pd.read_csv(rf"C:\Users\pcy\Desktop\软件工程与GIS开发\大作业\{clas}poi.csv")
        distances = []
        durations = []
        for address in frame["location"]:
            address=ast.literal_eval(address)
            d_lat=address["lat"]
            d_lng=address["lng"]
            parameters = f"origin={o_lat},{o_lng}&destination={d_lat},{d_lng}&ak={AK}"
            url = service + parameters
            response = requests.get(url)
            text=response.text
            dic=json.loads(text)
            distance = dic["result"]["routes"][0]["distance"]
            duration = dic["result"]["routes"][0]["duration"]
            distances.append(distance)
            durations.append(duration)
        frame["distance"]=distances
        frame["duration"]=durations
        frame.to_csv(f"{clas}poi.csv")

def convenience_index(index):
# 计算各类便利程度指标
    df = pd.read_csv(index+'poi.csv')
    distances=df["distance"]
    commute_times=df["duration"]
    weights = {'distance': 0.4, 'commute_time': 0.6}  # 距离和通勤时间的权重
    max_distance = 5000
    #最远通勤距离为5km
    max_commute_time = 3600
    #最大通勤时间为1h
    scores = []
    for i, neighborhood in df.iterrows():
        distance_score = 1 - distances[i] / max_distance  # 距离得分（范围：0-1）
        commute_time_score = 1 - commute_times[i] / max_commute_time  # 通勤时间得分（范围：0-1）
        score = weights['distance'] * distance_score + weights['commute_time'] * commute_time_score  # 综合得分（范围：0-1）
        scores.append(score)
    return sum(scores)/len(scores)

#计算便利程度综合指标
def total_grade():
    grade=0
    classes=["教育培训","交通设施","医疗","美食"]
    for i in classes:
        scores=convenience_index(i)
        grade+=scores
    return grade

#====================以下是页面设计部分===============================
# 设置页面标题和导航栏
st.set_page_config(page_title="闵行区二手房可视化及评价", page_icon=":house:", layout="wide")
menu = ["二手房数据爬取","闵行区二手房可视化", "房源评价"]
choice = st.sidebar.selectbox("选择功能", menu)

#显示界面1
if choice == "二手房数据爬取":
    st.title("链家二手房数据获取")
    st.write("通过输入目标地区的链家网址（url）,生成houseInfo2.xlsx二手房数据， houseInfo.xlsx为闵行区二手房数据，已爬取生成")
    page_url= st.text_input("请输入链家二手房地区网页的URL：","https://sh.lianjia.com/ershoufang/minhang/")
    if page_url:
        if st.button("开始爬取数据"):
            # 调用爬虫程序
            data1,data2 = crawling.run(page_url)
            st.success('数据已保存到houseInfo2.xlsx中')
            # 显示数据摘要
            st.write('数据摘要：')
            st.write(data1.describe())
# 显示页面2
if choice == "闵行区二手房可视化":
    st.title("闵行区二手房可视化")
    st.write("展示闵行区所有二手房的相关信息，通过输入对房价、面积的范围，筛选出符合条件的房源，并在地图上可视化，直观体现位置分布")
    st.write("***运行可能较慢，请耐心等待")
    # 读取Excel文件
    #Excel文件是通过crawling.py文件爬取，也可以爬取生成其他地区的二手房数据
    excel_file = st.file_uploader("上传链家数据文件（已通过crawling.py爬取有houseInfo.xlsx，可以个性化生成二手房数据）", type="xlsx")
    if excel_file is not None:
        excel_data = io.BytesIO(excel_file.read())
        df1 = pd.read_excel(excel_data)
        st.write(df1)
        st.subheader("筛选查询")
        min_price = st.text_input("最低总价（万元）","100")
        max_price = st.text_input("最高总价（万元）","2000")
        min_area = st.text_input("最小面积（平方米）","50")
        max_area = st.text_input("最大面积（平方米）","500")
        exp1 = df1["总价"].apply(lambda x:x[0:-1]).astype('float') > float(min_price)
        exp2 = df1["总价"].apply(lambda x:x[0:-1]).astype('float') < float(max_price)
        exp3 = df1["面积"].apply(lambda x:x[0:-2]).astype('float') > float(min_area)
        exp4 = df1["面积"].apply(lambda x:x[0:-2]).astype('float') < float(max_area)
        exp = exp1 & exp2 & exp3 & exp4
        subset = df1[exp]
        #st.button("确定")

        # 显示符合条件的子集的记录数
        st.title(f"共有{subset.shape[0]} 套房符合要求")
        st.dataframe(subset)
        #读取房源的经纬度信息
        df2=pd.read_excel(excel_data,sheet_name="Sheet2")
        #解决多套房源同属一个小区的问题，合并同位置房源信息
        def concat_func(x):
           return pd.Series({
            '总价':','.join(x['总价'].unique()),
            '户型':','.join(x['户型'].unique()),
            '面积':','.join(x["面积"].unique()),
            'lng,lat':','.join(x["lng,lat"].unique())
        }
        )
        merge_df=pd.merge(subset,df2,on="位置")
        result=merge_df.groupby(merge_df['位置']).apply(concat_func).reset_index()
       # 获取符合条件的房源坐标,对编码失败的位置进行剔除
        result = result[~result['lng,lat'].isin(["['unknown', 'unknown']"])]
       # 对经纬度列进行处理，拆分为经度和纬度两列，并转换为float类型
        result[['lng', 'lat']] = result['lng,lat'].apply(lambda x: pd.Series(eval(x)))
        result = result.drop('lng,lat', axis=1)  # 删除原始经纬度列

       # 创建地图
        m = folium.Map(location=[31.031557,121.453679], zoom_start=14)

       # 添加marker和popup
        for i, row in result.iterrows():
            location = [row['lat'], row['lng']]
            popup = folium.Popup(f"位置：{row['位置']}
总价：{row['总价']}
户型：{row['户型']}
面积：{row['面积']}", max_width=300)
            folium.Marker(location=location, popup=popup).add_to(m)

        # 获取用户输入的经纬度
        address = st.text_input("请输入您想查询的位置","闵行区吴泾镇东川路500号")
        lat = get_location(address)[1]
        lng = get_location(address)[0]
        st.button("确定")
        # 在地图上显示符合条件的房源
        st.write("您查询的位置（红）周边合适的房源地图如下：")
        # 根据用户输入的经纬度生成不同颜色的标记
        marker_color = 'red'

        # 创建标记对象
        t = folium.Marker(
            location=[lat, lng],
            icon=folium.Icon(color='red')
        )

        # 将标记添加到地图对象
        t.add_to(m)
        # 显示地图
        folium_static(m)
    else:
        st.write("请上传Excel文件")

# 显示页面3
elif choice == "房源评价":
    st.title("房源评价")
    st.write("在此输入文本内容，选择想深入了解的房源，进行poi数据可视化；\n并通过对教育、交通、医疗、餐饮要素便利程度指标分析，展示目标房源综合得分，供买房者参考")
    #选择想深入了解的房源
    address = st.text_input("请输入您想查询的房源","虹梅新苑 吴泾")
    #生成房源经纬度信息
    o_lng,o_lat=get_location(address)[0],get_location(address)[1]
    #生成poi数据
    getPOIs(address)
    #计算房源到poi的驾驶花费时间和驾驶距离
    cal_distance_duration(address)
    # 计算便利程度综合指标
    grade=total_grade()
    st.write(f"该房源的得分（满分为4）为：{grade:.4f}")
    # 创建地图
    m = folium.Map(location=[31.031557,121.453679], zoom_start=14)
    # 根据用户输入的经纬度生成不同颜色的标记
    marker_color = 'red'

    # 创建标记对象
    t = folium.Marker(
        location=[o_lat, o_lng],
        icon=folium.Icon(color='red')
    )
    # 将标记添加到地图对象
    t.add_to(m)

    #各类poi得分查询
    options = ["教育培训","交通设施","医疗","美食"]
    # 获取用户选择的POI类别
    selected_option = st.selectbox("请选择一类poi", options, index=0)
    if st.button("确认"):
        # 读取CSV文件中的数据，并将其作为DataFrame对象传递给st.dataframe()函数
        poi_data = pd.read_csv(selected_option + "poi.csv")
        poi_data_filtered = poi_data.iloc[:, 2:]
        st.dataframe(poi_data_filtered)
        # 创建标记，将POI添加到地图上
        for index, row in poi_data.iterrows():
            # 从location列中获取经纬度
            lat, lng = eval(row['location']).values()
            #纠差
            [lat,lng]=[rectify(lng,lat)[1],rectify(lng, lat)[0]]
            # 创建标记
            popup = folium.Popup(f"名称：{row['name']}
距离：{row['distance']}米
时间：{row['duration']/60:.2f}分钟",max_width=600)
            marker = folium.Marker(
                location=[lat, lng],
                popup=popup
            )
            # 将标记添加到地图上
            marker.add_to(m)
    # 显示地图
    folium_static(m)


class Macaco:
    def __init__(self, nome):
        self.nome = nome
        self.bucho = []

    def comer(self, comida):
        self.bucho.append(comida)

    def verBucho(self):
        for item in self.bucho:
            print(str(item))

    def digerir(self):
        self.bucho = []
        print("Estou com fome!!!! \n")

macaco1 = Macaco("Alfredo")
macaco2 = Macaco("Rodolfo")

print("Dados Macaco 1")
macaco1.comer("batata")
macaco1.verBucho()
macaco1.comer("rucula")
macaco1.verBucho()
macaco1.comer("alface")
macaco1.verBucho()
macaco1.digerir()
macaco1.verBucho()

print("Dados Macaco 2")
macaco2.comer("chocolate")
macaco2.verBucho()
macaco2.comer("batata frita")
macaco2.verBucho()
macaco2.comer("empadão")
macaco2.verBucho()
macaco2.digerir()
macaco2.verBucho()

print("Macaco virando canibal")
macaco1.comer(macaco2)
macaco1.verBucho()


import os
import sys
curr_path = os.getcwd()
package_path = (os.path.abspath(os.path.join(curr_path, os.pardir))).replace('\\', '/')+'/'
sys.path.insert(1, package_path)
from config.config import *
from processing_scripts.model_preprocessing import *

df_full= pd.read_csv(Train_path_final)

df_full.drop(['Unnamed: 0'] , axis=1 , inplace=True)
df_train= get_model_data(df_full , train_years)
df_valid = get_model_data(df_full , valid_years)

X_full , y_full = df_full.drop(["status","launch_year","deadline"], axis=1) , df_full['status']
X_train , y_train = df_train.drop(["status","launch_year","deadline"] , axis=1) , df_train['status']
X_valid , y_valid = df_valid.drop(["status","launch_year","deadline"] , axis=1) , df_valid['status']

print("Training.....")
scores  = XG_score(X_train ,y_train ,X_valid , y_valid)
print("Accuracy of the model on validation set-2019")
print(scores)

print("Final Training Model.....")
model = XG_train(X_full , y_full)

try:
    os.remove(model_path)
    print("removed the model file")

except:
    pass

print("saving the model")
joblib.dump(model, model_path)


import torch
from torch import nn

if __name__=="__main__":
    # 問1
    _in = torch.ones(32, 1024)
    print(_in.shape)
    # 問2
    fc1 = nn.Linear(in_features=1024, out_features=256, bias=True)
    print(fc1(_in).shape)
    # 問3
    fc2 = nn.Linear(in_features=1024, out_features=2048, bias=True)
    print(fc2(_in).shape)
    # 問4
    out = fc1(_in).view(32, 16, 16)
    print(out.shape)


from helper import *
from tank_class import *
from constans import *
from map_maker.tiles import *
from map_maker.map_input import *
from AI import *
import socket
import pickle

def convert_map(map):
    ar = []
    for i in range(len(map.tiles_array)):
        ar.append([])
        for j in range(len(map.tiles_array[0])):
            if map.tiles_array[i][j].type == 'stone':
                ttype = 'S'
            elif map.tiles_array[i][j].type == 'finish':
                ttype = 'F'
            else:
                ttype = map.tiles_array[i][j].type[0]
            ar[i].append(ttype)
    return ar

def prepared_tank(tank):
    return [tank.center, tank.corner, tank.body_ang, tank.turret_ang, tank.cooldawn,
            tank.time_cooldawn, tank.hp]

def prepared_bullet(bul):
    return [bul.center, bul.corner, bul.ang]

def update_tank_keys(tank, data):
    tank.fw = data[0]
    tank.fa = data[1]
    tank.fs = data[2]
    tank.fd = data[3]
    tank.mouse = data[4]
    tank.flpk = data[5]
    tank.center_visible = data[6]
    tank.corner_visible = data[7]

class all:
    def __init__(self, tanks, bullets, tank_player, map):
        self.tanks = {}
        for tank in tanks:
            self.tanks[tank.ID] = prepared_tank(tank)
        self.list_update = map.list_update
        self.tank_player = prepared_tank(tank_player)
        self.bullets = []
        for bul in bullets:
            self.bullets.append(prepared_bullet(bul))

class all_start:
    def __init__(self, tanks, map, tank_player):
        self.map = convert_map(map)
        self.tank_player = [tank_player.center.x, tank_player.center.y, tank_player.body_ang,
                            tank_player.type, tank_player.ID]
        self.tanks_init = {}
        for tank in tanks:
            self.tanks_init[tank.ID] = [tank.center.x, tank.center.y, tank.body_ang,
                                        tank.type, tank.ID]

def server_main(ip, port, game_input, num_of_pl):
    try:
        to_connect_with = (ip, int(port))

        pygame.init()

        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.bind(to_connect_with)

        number_of_players = int(num_of_pl)
        server.listen(number_of_players)
        players = {}
        for i in range(number_of_players):
            player, adress = server.accept()
            players['pl'+str(i)] = player
        print('connected!')

        players_start_tank_types = {}
        for ID in players:
            players_start_tank_types[ID] = pickle.loads(players[ID].recv(1024))

        clock = pygame.time.Clock()
        finished = False

        screen = pygame.display.set_mode((w, h))

        map, tanks_bots_list, tanks_players = file_reader_level(game_input)
        map = Map(map, screen)
        dict_tanks_players = {}
        for i in range(number_of_players):
            tank_player = create_tank_player(tanks_players[i][0], tanks_players[i][1], tanks_players[i][2],
                                             players_start_tank_types['pl'+str(i)],
                                             tanks_players[i][3], screen)
            dict_tanks_players['pl'+str(i)] = tank_player

        #dict_tanks_bots = {}
        for i in range(len(tanks_bots_list)):
            tank_bot = create_tank_bot(tanks_bots_list[i][0], tanks_bots_list[i][1], tanks_bots_list[i][2],
                                       tanks_bots_list[i][3], tanks_bots_list[i][4], screen,
                                       tanks_bots_list[i][5], tanks_bots_list[i][6])
            #dict_tanks_bots[str(i)] = tank_bot

        observating_point = dict_tanks_players['pl0'].center

        for ID in players:
            start_data = all_start(tanks, map, dict_tanks_players[ID])
            players[ID].send(pickle.dumps(start_data))
            players[ID].recv(100)

        for tank in tanks:
            tank.before_draw(observating_point)

        while not finished:
            screen.fill((255, 255, 255))
            map.draw(observating_point)

            for tank in tanks:
                #tank.before_draw(observating_point)
                tank.draw(observating_point)

            for tank in tanks_bots:
                for ID in players:
                    meet_with_tank(tank, dict_tanks_players[ID])

            for tank in tanks:
                tank.draw_turret()

            for bul in bullets:
                bul.draw(observating_point)

            pygame.display.update()
            clock.tick(FPS)

            for tank in tanks_bots:
                move_AI(tank)

            for tank in tanks:
                tank.move(map)
                tank.fire_gun()
                tank.update_cooldawn()

            for bul in bullets:
                bul.move()
                for tile in bul.tiles_near(map):
                    tile.meet_with_bullet(bul, map)

                for tank in tanks:
                    tank.meet_with_bullet(bul)

            # отправка игроку данных
            for ID in players:
                send_all = all(tanks, bullets, dict_tanks_players[ID], map)
                players[ID].send(pickle.dumps(send_all))

            map.list_update = []

            for ID in players:
                data = pickle.loads(players[ID].recv(1024))
                update_tank_keys(dict_tanks_players[ID], data)

    except ValueError:
        print('ошибка в формате данных')

    except socket.gaierror:
        print('невозможно создать сервер по заданным данным')


# -*- test-case-name: axiom.test.test_scheduler -*-

import gc
from datetime import timedelta

from twisted.trial import unittest
from twisted.trial.unittest import TestCase
from twisted.application.service import IService
from twisted.internet.task import Clock
from twisted.python import filepath

from epsilon.extime import Time

from axiom.scheduler import TimedEvent, _SubSchedulerParentHook, TimedEventFailureLog
from axiom.scheduler import Scheduler, SubScheduler
from axiom.scheduler import SITE_SCHEDULER
from axiom.store import Store
from axiom.item import Item
from axiom.substore import SubStore

from axiom.attributes import integer, text, inmemory, boolean, timestamp
from axiom.iaxiom import IScheduler

class TestEvent(Item):

    typeName = 'test_event'
    schemaVersion = 1

    testCase = inmemory()       # this won't fall out of memory due to
                                # caching, thanks.
    name = text()

    runCount = integer()
    runAgain = integer()        # milliseconds to add, then run again
    winner = integer(default=0) # is this the event that is supposed to
                                # complete the test successfully?

    def __init__(self, **kw):
        super(TestEvent, self).__init__(**kw)
        self.runCount = 0

    def run(self):
        # When this is run from testSubScheduler, we want to make an
        # additional assertion.  There is exactly one SubStore in this
        # configuration, so there should be no more than one
        # TimedEvent with a _SubSchedulerParentHook as its runnable.
        if self.store.parent is not None:
            count = 0
            s = self.store.parent
            for evt in s.query(TimedEvent):
                if isinstance(evt.runnable, _SubSchedulerParentHook):
                    count += 1
            if count > 1:
                return self.fail("Too many TimedEvents for the SubStore: %d" % (count,))

        self.runCount += 1
        if self.runAgain is not None:
            result = self.testCase.now() + timedelta(seconds=self.runAgain)
            self.runAgain = None
        else:
            result = None
        return result

    def fail(self, msg):
        self.testCase.fail(msg)

class NotActuallyRunnable(Item):
    huhWhat = integer()

class SpecialError(Exception):
    pass

class SpecialErrorHandler(Item):
    huhWhat = integer()
    broken = integer(default=0)
    procd = integer(default=0)

    def run(self):
        self.broken = 1
        raise SpecialError()

    def timedEventErrorHandler(self, timedEvent, failureObj):
        failureObj.trap(SpecialError)
        self.procd = 1

class HookRunner(Item):
    """
    Runnable that simply calls a supplied hook.
    """
    ignored = integer()
    hook = inmemory()

    def run(self):
        self.hook(self)

class SchedTest:
    def tearDown(self):
        return IService(self.siteStore).stopService()

    def setUp(self):
        self.clock = Clock()

        scheduler = IScheduler(self.siteStore)
        self.stubTime(scheduler)
        IService(self.siteStore).startService()

    def now(self):
        return Time.fromPOSIXTimestamp(self.clock.seconds())

    def stubTime(self, scheduler):
        scheduler.callLater = self.clock.callLater
        scheduler.now = self.now

    def test_implementsSchedulerInterface(self):
        """
        Verify that IScheduler is declared as implemented.
        """
        self.assertTrue(IScheduler.providedBy(IScheduler(self.store)))

    def test_scheduler(self):
        """
        Test that the ordering and timing of scheduled calls is correct.
        """
        # create 3 timed events.  the first one fires.  the second one fires,
        # then reschedules itself.  the third one should never fire because the
        # reactor is shut down first.  assert that the first and second fire
        # only once, and that the third never fires.
        s = self.store

        t1 = TestEvent(testCase=self,
                       name='t1',
                       store=s, runAgain=None)
        t2 = TestEvent(testCase=self,
                       name='t2',
                       store=s, runAgain=2)
        t3 = TestEvent(testCase=self,
                       name='t3',
                       store=s, runAgain=None)

        now = self.now()
        self.ts = [t1, t2, t3]

        S = IScheduler(s)

        # Schedule them out of order to make sure behavior doesn't
        # depend on tasks arriving in soonest-to-latest order.
        S.schedule(t2, now + timedelta(seconds=3))
        S.schedule(t1, now + timedelta(seconds=1))
        S.schedule(t3, now + timedelta(seconds=100))

        self.clock.pump([2, 2, 2])
        self.assertEqual(t1.runCount, 1)
        self.assertEqual(t2.runCount, 2)
        self.assertEqual(t3.runCount, 0)

    def test_unscheduling(self):
        """
        Test the unscheduleFirst method of the scheduler.
        """
        sch = IScheduler(self.store)
        t1 = TestEvent(testCase=self, name='t1', store=self.store)
        t2 = TestEvent(testCase=self, name='t2', store=self.store, runAgain=None)

        sch.schedule(t1, self.now() + timedelta(seconds=1))
        sch.schedule(t2, self.now() + timedelta(seconds=2))
        sch.unscheduleFirst(t1)
        self.clock.advance(3)
        self.assertEqual(t1.runCount, 0)
        self.assertEqual(t2.runCount, 1)

    def test_inspection(self):
        """
        Test that the L{scheduledTimes} method returns an iterable of all the
        times at which a particular item is scheduled to run.
        """
        now = self.now() + timedelta(seconds=1)
        off = timedelta(seconds=3)
        sch = IScheduler(self.store)
        runnable = TestEvent(store=self.store, name='Only event')
        sch.schedule(runnable, now)
        sch.schedule(runnable, now + off)
        sch.schedule(runnable, now + off + off)

        self.assertEqual(
            list(sch.scheduledTimes(runnable)),
            [now, now + off, now + off + off])

    def test_scheduledTimesDuringRun(self):
        """
        L{Scheduler.scheduledTimes} should not include scheduled times that have
        already triggered.
        """
        futureTimes = []
        scheduler = IScheduler(self.store)
        runner = HookRunner(
            store=self.store,
            hook=lambda self: futureTimes.append(
                list(scheduler.scheduledTimes(self))))

        then = self.now() + timedelta(seconds=1)
        scheduler.schedule(runner, self.now())
        scheduler.schedule(runner, then)
        self.clock.advance(1)
        self.assertEqual(futureTimes, [[then], []])

    def test_deletedRunnable(self):
        """
        Verify that if a scheduled item is deleted,
        L{TimedEvent.invokeRunnable} just deletes the L{TimedEvent} without
        raising an exception.
        """
        now = self.now()
        scheduler = IScheduler(self.store)
        runnable = TestEvent(store=self.store, name='Only event')
        scheduler.schedule(runnable, now)

        runnable.deleteFromStore()

        # Invoke it manually to avoid timing complexity.
        timedEvent = self.store.findUnique(
            TimedEvent, TimedEvent.runnable == runnable)
        timedEvent.invokeRunnable()

        self.assertEqual(
            self.store.findUnique(
                TimedEvent,
                TimedEvent.runnable == runnable,
                default=None),
            None)

class TopStoreSchedTest(SchedTest, TestCase):
    def setUp(self):
        self.store = self.siteStore = Store()
        super(TopStoreSchedTest, self).setUp()

    def test_namedService(self):
        """
        The site store IScheduler implementation can be retrieved as a named
        service from the store's IServiceCollection powerup.
        """
        self.assertIdentical(
            IService(self.store).getServiceNamed(SITE_SCHEDULER),
            IScheduler(self.store))

    def testBasicScheduledError(self):
        S = IScheduler(self.store)
        S.schedule(NotActuallyRunnable(store=self.store), self.now())

        te = TestEvent(store=self.store, testCase=self,
                       name='t1', runAgain=None)
        S.schedule(te, self.now() + timedelta(seconds=1))

        self.assertEqual(
            self.store.query(TimedEventFailureLog).count(), 0)

        self.clock.advance(3)

        self.assertEqual(te.runCount, 1)

        errs = self.flushLoggedErrors(AttributeError)
        self.assertEqual(len(errs), 1)
        self.assertEqual(self.store.query(TimedEventFailureLog).count(), 1)

    def testScheduledErrorWithHandler(self):
        S = IScheduler(self.store)
        spec = SpecialErrorHandler(store=self.store)
        S.schedule(spec, self.now())

        te = TestEvent(store=self.store, testCase=self,
                       name='t1', runAgain=None)
        S.schedule(te, self.now() + timedelta(seconds=1))
        self.assertEqual(
            self.store.query(TimedEventFailureLog).count(), 0)

        self.clock.advance(3)

        self.assertEqual(te.runCount, 1)

        errs = self.flushLoggedErrors(SpecialError)
        self.assertEqual(len(errs), 1)
        self.assertEqual(self.store.query(TimedEventFailureLog).count(), 0)
        self.assertTrue(spec.procd)
        self.assertFalse(spec.broken)

class SubSchedulerTests(SchedTest, TestCase):
    """
    Tests for the substore implementation of IScheduler.
    """
    def setUp(self):
        """
        Create a site store for the substore which will contain the IScheduler
        being tested.  Start its IService so any scheduled events will run.
        """
        self.storePath = filepath.FilePath(self.mktemp())
        self.siteStore = Store(self.storePath)
        super(SubSchedulerTests, self).setUp()

        substoreItem = SubStore.createNew(self.siteStore, ['scheduler_test'])
        self.substore = substoreItem.open()
        self.scheduler = scheduler = IScheduler(self.substore)
        self.stubTime(scheduler)

        self.store = self.substore

    def test_now(self):
        """
        The user store's L{IScheduler} powerup's C{now} method returns whatever
        the site store's L{IScheduler} powerup's C{now} method returns.
        """
        # I don't want the stubbed now method.
        del self.scheduler.now

        self.clock.advance(17)
        self.assertEqual(
            self.scheduler.now(),
            Time.fromPOSIXTimestamp(self.clock.seconds()))

    def test_scheduleAfterParentHookError(self):
        """
        A transient error during a L{_SubSchedulerParentHook} run (such as
        failing to open the substore for whatever reason) should not disable
        subsequent scheduling.
        """
        runnable = TestEvent(store=self.store)

        # Schedule runnable, but fail the _SubSchedulerParentHook run.

        self.scheduler.schedule(runnable, self.now() + timedelta(seconds=1))
        hook = self.siteStore.findUnique(_SubSchedulerParentHook)
        def stumble():
            raise IOError('Denied')
        object.__setattr__(hook, 'run', stumble)
        self.clock.advance(1)
        object.__delattr__(hook, 'run')

        self.assertEqual(
            self.siteStore.findUnique(TimedEventFailureLog).runnable,
            hook)
        [err] = self.flushLoggedErrors(IOError)
        self.assertEqual(str(err.value), 'Denied')
        self.assertEqual(runnable.runCount, 0)

        # Schedule runnable again.  The restored hook in the site store should
        # trigger both scheduled runs in the substore now.

        self.scheduler.schedule(runnable, self.now() + timedelta(seconds=1))
        self.clock.advance(1)
        self.assertEqual(runnable.runCount, 2)

class SchedulerStartupTests(TestCase):
    """
    Tests for behavior relating to L{Scheduler} service startup.
    """
    def setUp(self):
        self.clock = Clock()
        self.store = Store()

    def tearDown(self):
        return self.stopStoreService()

    def now(self):
        return Time.fromPOSIXTimestamp(self.clock.seconds())

    def time(self, offset):
        return self.now() + timedelta(seconds=offset)

    def makeScheduler(self):
        """
        Create, install, and return a Scheduler with a fake callLater.
        """
        scheduler = IScheduler(self.store)
        scheduler.callLater = self.clock.callLater
        scheduler.now = self.now
        return scheduler

    def startStoreService(self):
        """
        Start the Store Service.
        """
        service = IService(self.store)
        service.startService()

    def stopStoreService(self):
        service = IService(self.store)
        if service.running:
            return service.stopService()

    def test_schedulerStartsWhenServiceStarts(self):
        """
        Test that IScheduler(store).startService() gets called whenever
        IService(store).startService() is called.
        """
        service = IService(self.store)
        service.startService()
        scheduler = service.getServiceNamed(SITE_SCHEDULER)
        self.assertTrue(scheduler.running)

    def test_scheduleWhileStopped(self):
        """
        Test that a schedule call on a L{Scheduler} which has not been started
        does not result in the creation of a transient timed event.
        """
        scheduler = self.makeScheduler()
        scheduler.schedule(TestEvent(store=self.store), self.time(1))
        self.assertEqual(self.clock.calls, [])

    def test_scheduleWithRunningService(self):
        """
        Test that if a scheduler is created and installed on a store which has
        a started service, a transient timed event is created when the scheduler
        is used.
        """
        self.startStoreService()
        scheduler = self.makeScheduler()
        scheduler.schedule(TestEvent(store=self.store), self.time(1))
        self.assertEqual(len(self.clock.calls), 1)

    def test_schedulerStartedWithPastEvent(self):
        """
        Test that an existing Scheduler with a TimedEvent in the past is
        started immediately (but does not run the TimedEvent synchronously)
        when the Store Service is started.
        """
        scheduler = self.makeScheduler()
        scheduler.schedule(TestEvent(store=self.store), self.time(-1))
        self.assertEqual(self.clock.calls, [])
        self.startStoreService()
        self.assertEqual(len(self.clock.calls), 1)

    def test_schedulerStartedWithFutureEvent(self):
        """
        Test that an existing Scheduler with a TimedEvent in the future is
        started immediately when the Store Service is started.
        """
        scheduler = self.makeScheduler()
        scheduler.schedule(TestEvent(store=self.store), self.time(1))
        self.assertEqual(self.clock.calls, [])
        self.startStoreService()
        self.assertEqual(len(self.clock.calls), 1)

    def test_schedulerStopped(self):
        """
        Test that when the Store Service is stopped, the Scheduler's transient
        timed event is cleaned up.
        """
        self.test_scheduleWithRunningService()
        d = self.stopStoreService()
        def cbStopped(ignored):
            self.assertEqual(self.clock.calls, [])
        d.addCallback(cbStopped)
        return d

class MissingService(unittest.TestCase):
    """
    A set of tests to verify that things *aren't* scheduled with the reactor
    when the scheduling service doesn't exist, merely persisted to the
    database.
    """

    def setUp(self):
        """
        Create a store with a scheduler installed on it and hook the C{now} and
        C{callLater} methods of that scheduler so their behavior can be
        controlled by these tests.
        """
        self.calls = []
        self.store = Store(filepath.FilePath(self.mktemp()))
        self.siteScheduler = IScheduler(self.store)
        self.siteScheduler.callLater = self._callLater

    def _callLater(self, s, f, *a, **k):
        self.calls.append((s, f, a, k))

    def test_schedule(self):
        """
        Test that if an event is scheduled against a scheduler which is not
        running, not transient scheduling (eg, reactor.callLater) is performed.
        """
        return self._testSchedule(self.siteScheduler)

    def test_subSchedule(self):
        """
        The same as test_schedule, except using a subscheduler.
        """
        subst = SubStore.createNew(self.store, ['scheduler_test'])
        substore = subst.open()
        subscheduler = IScheduler(substore)
        return self._testSchedule(subscheduler)

    def _testSchedule(self, scheduler):
        t1 = TestEvent(store=scheduler.store)
        scheduler.schedule(t1, Time.fromPOSIXTimestamp(0))
        self.failIf(self.calls,
                    "Should not have had any calls: {!r}".format(self.calls))
        self.assertIdentical(
            scheduler._getNextEvent(Time.fromPOSIXTimestamp(1)).runnable, t1)

class ScheduleCallingItem(Item):
    """
    Item which invokes C{schedule} on its store's L{IScheduler} from its own
    C{run} method.
    """

    ran = boolean(default=False)
    rescheduleFor = timestamp()

    def run(self):
        scheduler = IScheduler(self.store)
        scheduler.schedule(self, self.rescheduleFor)
        self.ran = True

class NullRunnable(Item):
    """
    Runnable item which does nothing.
    """
    ran = boolean(default=False)

    def run(self):
        pass

class SubStoreSchedulerReentrancy(TestCase):
    """
    Test re-entrant scheduling calls on an item run by a SubScheduler.
    """
    def setUp(self):
        self.clock = Clock()

        self.dbdir = filepath.FilePath(self.mktemp())
        self.store = Store(self.dbdir)
        self.substoreItem = SubStore.createNew(self.store, ['sub'])
        self.substore = self.substoreItem.open()

        self.scheduler = IScheduler(self.store)
        self.subscheduler = IScheduler(self.substore)

        self.scheduler.callLater = self.clock.callLater
        self.scheduler.now = lambda: Time.fromPOSIXTimestamp(self.clock.seconds())
        self.subscheduler.now = lambda: Time.fromPOSIXTimestamp(self.clock.seconds())

        IService(self.store).startService()

    def tearDown(self):
        return IService(self.store).stopService()

    def _scheduleRunner(self, now, offset):
        scheduledAt = Time.fromPOSIXTimestamp(now + offset)
        rescheduleFor = Time.fromPOSIXTimestamp(now + offset + 10)
        runnable = ScheduleCallingItem(store=self.substore, rescheduleFor=rescheduleFor)
        self.subscheduler.schedule(runnable, scheduledAt)
        return runnable

    def testSchedule(self):
        """
        Test the schedule method, as invoked from the run method of an item
        being run by the subscheduler.
        """
        now = self.clock.seconds()
        runnable = self._scheduleRunner(now, 10)

        self.clock.advance(11)

        self.assertEqual(
            list(self.subscheduler.scheduledTimes(runnable)),
            [Time.fromPOSIXTimestamp(now + 20)])

        hook = self.store.findUnique(
            _SubSchedulerParentHook,
            _SubSchedulerParentHook.subStore == self.substoreItem)

        self.assertEqual(
            list(self.scheduler.scheduledTimes(hook)),
            [Time.fromPOSIXTimestamp(now + 20)])

    def testScheduleWithLaterTimedEvents(self):
        """
        Like L{testSchedule}, but use a SubScheduler which has pre-existing
        TimedEvents which are beyond the new runnable's scheduled time (to
        trigger the reschedule-using code-path in
        _SubSchedulerParentHook._schedule).
        """
        now = self.clock.seconds()
        when = Time.fromPOSIXTimestamp(now + 30)
        null = NullRunnable(store=self.substore)
        self.subscheduler.schedule(null, when)
        runnable = self._scheduleRunner(now, 10)

        self.clock.advance(11)

        self.assertEqual(
            list(self.subscheduler.scheduledTimes(runnable)),
            [Time.fromPOSIXTimestamp(now + 20)])

        self.assertEqual(
            list(self.subscheduler.scheduledTimes(null)),
            [Time.fromPOSIXTimestamp(now + 30)])

        hook = self.store.findUnique(
            _SubSchedulerParentHook,
            _SubSchedulerParentHook.subStore == self.substoreItem)

        self.assertEqual(
            list(self.scheduler.scheduledTimes(hook)),
            [Time.fromPOSIXTimestamp(20)])

    def testScheduleWithEarlierTimedEvents(self):
        """
        Like L{testSchedule}, but use a SubScheduler which has pre-existing
        TimedEvents which are before the new runnable's scheduled time.
        """
        now = self.clock.seconds()
        when = Time.fromPOSIXTimestamp(now + 15)
        null = NullRunnable(store=self.substore)
        self.subscheduler.schedule(null, when)
        runnable = self._scheduleRunner(now, 10)

        self.clock.advance(11)

        self.assertEqual(
            list(self.subscheduler.scheduledTimes(runnable)),
            [Time.fromPOSIXTimestamp(now + 20)])

        self.assertEqual(
            list(self.subscheduler.scheduledTimes(null)),
            [Time.fromPOSIXTimestamp(now + 15)])

        hook = self.store.findUnique(
            _SubSchedulerParentHook,
            _SubSchedulerParentHook.subStore == self.substoreItem)

        self.assertEqual(
            list(self.scheduler.scheduledTimes(hook)),
            [Time.fromPOSIXTimestamp(now + 15)])

    def testMultipleEventsPerTick(self):
        """
        Test running several runnables in a single tick of the subscheduler.
        """
        now = self.clock.seconds()
        runnables = [
            self._scheduleRunner(now, 10),
            self._scheduleRunner(now, 11),
            self._scheduleRunner(now, 12)]

        self.clock.advance(13)

        for n, runnable in enumerate(runnables):
            self.assertEqual(
                list(self.subscheduler.scheduledTimes(runnable)),
                [Time.fromPOSIXTimestamp(now + n + 20)])

        hook = self.store.findUnique(
            _SubSchedulerParentHook,
            _SubSchedulerParentHook.subStore == self.substoreItem)

        self.assertEqual(
            list(self.scheduler.scheduledTimes(hook)),
            [Time.fromPOSIXTimestamp(now + 20)])

class BackwardsCompatibilitySchedTests(object):
    """
    L{Scheduler} and L{SubScheduler} are going to be removed.  In the mean
    time, if someone gets their hands on one and tries to do something
    supported to it, it should be as if they were doing it to the suitable
    replacement, L{_SiteScheduler} or L{_UserScheduler}.  This mixin provides
    tests for that behavior, as well as for the deprecations of L{Scheduler}
    and L{SubScheduler}.

    @ivar schedulerType: L{Scheduler} or L{SubScheduler}, whichever is to be
        tested.
    """
    def setUp(self):
        """
        Create a store with an instance of C{self.schedulerType} in it.
        """
        self.store = Store()
        self.oldScheduler = self.schedulerType(store=self.store)
        warnings = self.flushWarnings([self.setUp])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], PendingDeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            self.schedulerType.__name__ + " is deprecated since Axiom 0.5.32.  "
            "Just adapt stores to IScheduler.")
        self.scheduler = IScheduler(self.store)

    def _checkSynonym(self, name):
        # Whatever the value of the attribute is on the _SiteScheduler or
        # _UserScheduler instance, that's what it should be on the Scheduler
        # or SubScheduler Item.
        foo = object()
        setattr(self.scheduler, name, foo)
        self.assertIdentical(getattr(self.oldScheduler, name), foo)

        # And rebinding the attribute on the (Sub)Scheduler Item should rebind it on
        # the _SiteScheduler instance.
        bar = object()
        setattr(self.oldScheduler, name, bar)
        self.assertIdentical(getattr(self.scheduler, name), bar)

    def test_now(self):
        """
        L{Scheduler.now} is a synonym for L{_SiteScheduler.now}.
        """
        self._checkSynonym("now")

    def test_tick(self):
        """
        L{Scheduler.tick} is a synonym for L{_SiteScheduler.tick}.
        """
        self._checkSynonym("tick")

    def test_schedule(self):
        """
        L{Scheduler.schedule} is a synonym for L{_SiteScheduler.schedule}.
        """
        self._checkSynonym("schedule")

    def test_scheduledTimes(self):
        """
        L{Scheduler.scheduledTimes} is a synonym for
        L{_SiteScheduler.scheduledTimes}.
        """
        self._checkSynonym("scheduledTimes")

    def test_unscheduleAll(self):
        """
        L{Scheduler.unscheduleAll} is a synonym for
        L{_SiteScheduler.unscheduleAll}.
        """
        self._checkSynonym("unscheduleAll")

    def test_reschedule(self):
        """
        L{Scheduler.reschedule} is a synonym for
        L{_SiteScheduler.reschedule}.
        """
        self._checkSynonym("reschedule")

    def test_deprecated(self):
        """
        Loading an existing L{Scheduler} from a L{Store} emits a deprecation
        warning.
        """
        storeID = self.oldScheduler.storeID
        del self.oldScheduler
        gc.collect()
        # Just load the scheduler, we don't need to use it for anything.
        self.store.getItemByID(storeID)
        warnings = self.flushWarnings([self.test_deprecated])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], PendingDeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            self.schedulerType.__name__ + " is deprecated since Axiom 0.5.32.  "
            "Just adapt stores to IScheduler.")

class BackwardsCompatibilitySchedulerTests(BackwardsCompatibilitySchedTests, TestCase):
    schedulerType = Scheduler

    def test_interface(self):
        """
        L{Scheduler} provides L{IScheduler} (which it proxies) and
        L{IService} (which is a no-op).
        """
        self.assertTrue(IScheduler.providedBy(self.oldScheduler))
        self.assertTrue(IService.providedBy(self.oldScheduler))

class BackwardsCompatibilitySubSchedulerTests(BackwardsCompatibilitySchedTests, TestCase):
    schedulerType = SubScheduler

    def test_interface(self):
        """
        L{SubScheduler} provides L{IScheduler} (which it proxies).
        """
        self.assertTrue(IScheduler.providedBy(self.oldScheduler))


import typing as t

import gym
import tqdm
import numpy as np

class Agent:
    def get_action(self, obs) -> t.Any:
        raise NotImplementedError

class RandomAgent(Agent):
    def __init__(self, action_space):
        self.action_space = action_space

    def get_action(self, obs) -> t.Any:
        return self.action_space.sample()

class Trajectory:
    def __init__(self):
        self.clear()

    def append(self, obs, act, obs_next, reward, done):
        self.obs.append(obs)
        self.act.append(act)
        self.obs_next.append(obs_next)
        self.reward.append(reward)
        self.done.append(done)

    def concat(self, traj):
        self.obs += traj.obs
        self.obs_next += traj.obs_next
        self.act += traj.act
        self.done += traj.done
        self.reward += traj.reward

    def clear(self):
        self.obs = []
        self.obs_next = []
        self.act = []
        self.done = []
        self.reward = []

def play_one_episode(env: t.Union[gym.Env, str], agent: Agent):
    if isinstance(env, str):
        env = gym.make(env)

    assert isinstance(env, gym.Env)

    obs = env.reset()
    info = {}
    done = False
    total_reward = 0
    total_steps = 0
    traj = Trajectory()

    while not done:
        act = agent.get_action(obs)

        obs_next, reward, done, info = env.step(act)
        traj.append(obs, act, obs_next, reward, done)

        obs = obs_next
        total_reward += reward
        total_steps  += 1

    return total_reward, total_steps, traj

def evaluate_n_episodes(env: t.Union[gym.Env, str], agent: Agent, n_episodes: int):
    epi_rewards = []
    agg_traj = Trajectory()
    for _ in tqdm.tqdm(range(n_episodes)):
        epi_reward, _, traj = play_one_episode(env, agent)
        epi_rewards.append(epi_reward)
        agg_traj.concat(traj)

    return np.mean(epi_rewards), epi_rewards, agg_traj


# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy
import re
import datetime

class FictionDetails(scrapy.Item):
    fiction_id = scrapy.Field()
    fiction_url = scrapy.Field()
    fiction_name = scrapy.Field()
    author = scrapy.Field()
    fiction_tag = scrapy.Field()
    fiction_state = scrapy.Field()
    words_num = scrapy.Field()
    fiction_origin = scrapy.Field()
    fiction_abstract = scrapy.Field()
    front_image_url = scrapy.Field()
    front_image_path = scrapy.Field()
    crawl_time = scrapy.Field()

    def get_insert_sql(self):
        insert_sql = """
                    insert into fiction_details(fiction_id, fiction_url, fiction_name, author, fiction_tag,
                    fiction_state, words_num, fiction_origin, fiction_abstract, front_image_url, crawl_time)
                    VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                    ON DUPLICATE KEY UPDATE fiction_state=VALUES(fiction_state),words_num=VALUES(words_num),
                                            crawl_time=VALUES(crawl_time)
                """
        fiction_id = self["fiction_id"][0]
        fiction_url = "".join(self["fiction_url"])
        fiction_name = "".join(self["fiction_name"])
        author = re.match('.*：(.*)', "".join(self["author"])).group(1)
        fiction_tag = "".join(self["fiction_tag"])
        fiction_state = "".join(self["fiction_state"])
        words_num = re.match('字数：(.*万)', "".join(self["words_num"])).group(1).replace("\r\n", "").strip()
        fiction_origin = re.match('.*：(.*)', "".join(self["fiction_origin"])).group(1)
        fiction_abstract = self["fiction_abstract"][0].replace("\r\n", "").strip()
        front_image_url = "".join(self["front_image_url"])
        crawl_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        items = (fiction_id, fiction_url, fiction_name, author, fiction_tag, fiction_state, words_num, fiction_origin,
                 fiction_abstract, front_image_url, crawl_time)
        return insert_sql, items

class FictionContent(scrapy.Item):
    fiction_id = scrapy.Field()
    fiction_name = scrapy.Field()
    chapter_id = scrapy.Field()
    chapter_update_time = scrapy.Field()
    chapter_num = scrapy.Field()
    chapter_name = scrapy.Field()
    chapter_content = scrapy.Field()
    crawl_time = scrapy.Field()

    def get_insert_sql(self):
        insert_sql = """
                    insert into fiction_content(fiction_id, fiction_name, chapter_id, chapter_update_time, chapter_num,
                    chapter_name, chapter_content, crawl_time)
                    VALUES(%s, %s, %s, %s, %s, %s, %s, %s)
                    ON DUPLICATE KEY UPDATE crawl_time=VALUES(crawl_time),chapter_content=VALUES(chapter_content)
                """
        fiction_id = self["fiction_id"][0]
        fiction_name = self["fiction_name"][0]
        chapter_id = self["chapter_id"][0]
        chapter_update_time = re.match('.*更新时间：(.*)', self["chapter_update_time"][0], re.S).group(1)
        chapter_num = int(re.match('.*字数：(\\d+).*', "".join(self["chapter_num"]), re.S).group(1))
        chapter_name = self["chapter_name"][0]
        chapter_content = "".join(self["chapter_content"]).replace("\r\n", "").strip()
        crawl_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        items = (fiction_id, fiction_name, chapter_id, chapter_update_time, chapter_num, chapter_name, chapter_content,
                 crawl_time)

        return insert_sql, items


import logging
import time
# 3rd party
import eo_filling
import numpy

def main():
    nan = numpy.nan
    img_target = numpy.array([[10, 10, 20, 20],
                            [10, nan, nan, 20],
                            [30, nan, nan, 20],
                            [30, 30, 30, 30]])
    img_reference = numpy.array([[5, 5, 10, 10],
                           [5, 15, 10, 10],
                           [15, 15, 3, 10],
                           [15, 15, 15, 15]])
    img_segmentation = numpy.array([[1, 1, 2, 2],
                           [1, 3, 2, 2],
                           [3, 3, 2, 2],
                           [3, 3, 3, 3]])

    # Search for no data
    gaps_target = eo_filling.get_gaps(img_target)
    if numpy.any(gaps_target):
        logging.info('Filling')
        img_filled = eo_filling.fill_maxwell_2004(img_target, img_segmentation, gaps_target)
        logging.info(img_filled)
        img_filled = eo_filling.fill_marujo(img_target, img_reference, img_segmentation, gaps_target)
        logging.info(img_filled)
    else:
        logging.info('Nothing to fill')
        return

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    logging.info('Starting Test')
    start = time.time()
    main()
    end = time.time()
    logging.info('Duration time: {}'.format(end - start))


import asyncio
import base64
import binascii
import hashlib
import json
import logging
import os
import random
import requests
import sys
import time
from urllib.parse import urlparse

from qrcode import QRCode

from aiohttp import ClientError

from uuid import uuid4
from datetime import date

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))  # noqa

from runners.support.agent import DemoAgent, default_genesis_txns
from runners.support.utils import (
    log_json,
    log_msg,
    log_status,
    log_timer,
    prompt,
    prompt_loop,
    require_indy,
)

CRED_PREVIEW_TYPE = (
    "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview"
)
SELF_ATTESTED = os.getenv("SELF_ATTESTED")

LOGGER = logging.getLogger(__name__)

TAILS_FILE_COUNT = int(os.getenv("TAILS_FILE_COUNT", 100))

class VSWAgent(DemoAgent):
    def __init__(
        self,
        http_port: int,
        admin_port: int,
        tails_server_base_url: str = None,
        **kwargs,
    ):
        super().__init__(
            "VSW.Agent",
            http_port,
            admin_port,
            prefix="VSW",
            tails_server_base_url=tails_server_base_url,
            extra_args=["--auto-accept-invites", "--auto-accept-requests"],
            **kwargs,
        )
        self.connection_id = None
        self._connection_ready = asyncio.Future()
        self.cred_state = {}
        self.cred_done = asyncio.Future()
        # TODO define a dict to hold credential attributes
        # based on credential_definition_id
        self.cred_attrs = {}
        self.proof_done = asyncio.Future()

    async def detect_connection(self):
        await self._connection_ready

    async def credential_complete(self):
        await self.cred_done

    async def proof_complete(self):
        await self.proof_done

    @property
    def connection_ready(self):
        return self._connection_ready.done() and self._connection_ready.result()

    async def handle_connections(self, message):
        if message["connection_id"] == self.connection_id:
            if message["state"] == "active" and not self._connection_ready.done():
                self.log("Connected")
                self._connection_ready.set_result(True)

    async def handle_present_proof(self, message):
        state = message["state"]

        presentation_exchange_id = message["presentation_exchange_id"]
        self.log(
            "Presentation: state =",
            state,
            ", presentation_exchange_id =",
            presentation_exchange_id,
        )

        if state == "presentation_received":
            log_status("#27 Process the proof provided by X")
            log_status("#28 Check if proof is valid")
            proof = await self.admin_POST(
                f"/present-proof/records/{presentation_exchange_id}/verify-presentation"
            )
            self.log("Proof =", proof["verified"])

            # if presentation is a vsw schema (app publication),
            # check the values received
            pres = message["presentation"]
            self.log("pres:", pres)
            name = pres['requested_proof']['revealed_attrs']['0_name_uuid']['raw']
            url = pres['requested_proof']['revealed_attrs']['0_url_uuid']['raw']
            digest = pres['requested_proof']['revealed_attrs']['0_digest_uuid']['raw']
            response = requests.get(url, allow_redirects=True)
            if response.status_code != 200:
                print("Failed to download file from URL")
                sys.exit(1)
            computed = hashlib.sha256(response.content).hexdigest()
            if computed != digest:
                print("SHA does not match")
                print(computed)
                sys.exit(1)
            else:
                open(f'vsw/{name}.wasm', 'wb').write(response.content)

            self.log("SUCCESS")
            self.proof_done.set_result(True)

    async def handle_basicmessages(self, message):
        self.log("Received message:", message["content"])

async def main(
    start_port: int,
    name: str,
    show_timing: bool = False,
):

    with open('/home/indy/vsw/.config.json') as f:
        config = json.load(f)

    genesis = await default_genesis_txns()
    if not genesis:
        print("Error retrieving ledger genesis transactions")
        sys.exit(1)

    agent = None

    try:
        log_status("#1 Provision an agent and wallet, get back configuration details")
        agent = VSWAgent(
            start_port,
            start_port + 1,
            genesis_data=genesis,
            timing=show_timing,
        )
        await agent.listen_webhooks(start_port + 2)
        # FIXME: This user should not have to publish their DID, but if I remove the next line it fails
        await agent.register_did()

        with log_timer("Startup duration:"):
            await agent.start_process()
        log_msg("Admin URL is at:", agent.admin_url)
        log_msg("Endpoint URL is at:", agent.endpoint)

        # Connect to repo
        log_status("#9 Connect to repo")
        connection = await agent.admin_POST("/connections/receive-invitation", config['invitation'])
        agent.connection_id = connection["connection_id"]
        log_json(connection, label="Invitation response:")

        await agent.detect_connection()

        log_status("#20 Request app credential from repo")
        req_attrs = [
            {
                "name": "name",
                "value": name,
                "restrictions": [{"schema_name": "vsw schema"}]
            },
            {
                "name": "url",
                "restrictions": [{"schema_name": "vsw schema"}]
            },
            {
                "name": "digest",
                "restrictions": [{"schema_name": "vsw schema"}]
            }
        ]
        req_preds = []
        indy_proof_request = {
            "name": "Retrieve by Name",
            "version": "1.0",
            "nonce": str(uuid4().int),
            "requested_attributes": {
                f"0_{req_attr['name']}_uuid": req_attr
                for req_attr in req_attrs
            },
            "requested_predicates": {}
        }
        proof_request_web_request = {
            "connection_id": agent.connection_id,
            "proof_request": indy_proof_request
        }

        # this sends the request to our agent, which forwards it to the repo
        # (based on the connection_id)
        await agent.admin_POST(
            "/present-proof/send-request",
            proof_request_web_request
        )

        await agent.proof_complete()

    finally:
        terminated = True
        try:
            if agent:
                await agent.terminate()
        except Exception:
            LOGGER.exception("Error terminating agent:")
            terminated = False

    await asyncio.sleep(0.1)

    if not terminated:
        os._exit(1)

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Runs a VSW agent.")
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=8050,
        metavar=("<port>"),
        help="Choose the starting port number to listen on",
    )

    parser.add_argument(
        "--timing", action="store_true", help="Enable timing information"
    )

    parser.add_argument("name", type=str, help="name of app to install")

    args = parser.parse_args()

    ENABLE_PYDEVD_PYCHARM = os.getenv("ENABLE_PYDEVD_PYCHARM", "").lower()
    ENABLE_PYDEVD_PYCHARM = ENABLE_PYDEVD_PYCHARM and ENABLE_PYDEVD_PYCHARM not in (
        "false",
        "0",
    )
    PYDEVD_PYCHARM_HOST = os.getenv("PYDEVD_PYCHARM_HOST", "localhost")
    PYDEVD_PYCHARM_CONTROLLER_PORT = int(
        os.getenv("PYDEVD_PYCHARM_CONTROLLER_PORT", 5001)
    )

    if ENABLE_PYDEVD_PYCHARM:
        try:
            import pydevd_pycharm

            print(
                "VSW remote debugging to "
                f"{PYDEVD_PYCHARM_HOST}:{PYDEVD_PYCHARM_CONTROLLER_PORT}"
            )
            pydevd_pycharm.settrace(
                host=PYDEVD_PYCHARM_HOST,
                port=PYDEVD_PYCHARM_CONTROLLER_PORT,
                stdoutToServer=True,
                stderrToServer=True,
                suspend=False,
            )
        except ImportError:
            print("pydevd_pycharm library was not found")

    require_indy()

    try:
        asyncio.get_event_loop().run_until_complete(
            main(
                args.port,
                args.name,
                args.timing,
            )
        )
    except KeyboardInterrupt:
        os._exit(1)


from pysmt.shortcuts import Times, Symbol, INT, Real, Plus, simplify, ToReal, EqualsOrIff,get_env, Minus
from cegispro2.solving.Solver import *
from fractions import Fraction

class LinearExpression:
    """
    Stores a dict from PySmt Variables x_1,...,x_n to their real coefficients plus an absolute real value.
    """

    def __init__(self, var_to_coeff, abs):
        self.var_to_coeff = var_to_coeff
        self.abs = abs

        self.pysmt_expression = Plus([Times(var_to_coeff[key], ToReal(key)) for key in var_to_coeff] + [abs])

    def add(self, lin_exp):
        """
        :param lin_exp: Add lin_exp to this linear expression
        :return: The linear expression obtained from adding this linear expression to lin_exp
        """

        new_var_to_coeff = dict()

        for key in lin_exp.var_to_coeff:
            new_var_to_coeff[key] = simplify(Plus(self.var_to_coeff[key], lin_exp.var_to_coeff[key]))

        new_abs = simplify(Plus(self.abs, lin_exp.abs))
        return LinearExpression(new_var_to_coeff, new_abs)

    # def subtract(self, lin_exp):
    #     """
    #     :param lin_exp: Add lin_exp to this linear expression
    #     :return: The linear expression obtained from adding this linear expression to lin_exp
    #     """
    #
    #     new_var_to_coeff = dict()
    #
    #     for key in lin_exp.var_to_coeff:
    #         new_var_to_coeff[key] = simplify(Minus(self.var_to_coeff[key], lin_exp.var_to_coeff[key]))

    #    new_abs = simplify(Minus(self.abs, lin_exp.abs))
    #    return LinearExpression(new_var_to_coeff, new_abs)

    def set_abs(self, val):
        self.abs = val
        self.pysmt_expression = Plus([Times(self.var_to_coeff[key], ToReal(key)) for key in self.var_to_coeff] + [self.abs])

    def multiply(self, real_constant):
        """
        :param real_constant:
        :return: The linear expression obtained from multiplying this linear expression by real_constant
        """

        new_var_to_coeff = self.var_to_coeff.copy()
        for key in new_var_to_coeff:
            new_var_to_coeff[key] = simplify(Times(real_constant, new_var_to_coeff[key]))

        new_abs = simplify(Times(real_constant, self.abs))

        return LinearExpression(new_var_to_coeff, new_abs)

    def substitute(self, var_to_linexp):
        """

        :param var_to_linexp: A dict from PySmt variables to LinearExpressions. If for some variable var no linear expression is given, the identity substition for that
        variable is applied.
        :return: The linear expression obtained from substituting every variable in keys of var_to_linexp by the corresponding linear expression
        """

        new_var_to_coeff = {key: Real(0) for key in self.var_to_coeff}
        new_abs = self.abs

        for key in self.var_to_coeff:
            cur_coefficient = self.var_to_coeff[key]
            # Take identitiy substitution of key by default
            key_is_substituted_by = var_to_linexp.get(key, LinearExpression({i:Real(1) if i==key else Real(0) for i in self.var_to_coeff}, Real(0)))

            # now go through the linear_expression key is substituted by and add it accordingly to the new linear expression
            for var in key_is_substituted_by.var_to_coeff:
                new_var_to_coeff[var] = simplify(Plus(new_var_to_coeff[var], Times(cur_coefficient, key_is_substituted_by.var_to_coeff[var])))

            new_abs = simplify(Plus(new_abs, Times(cur_coefficient, key_is_substituted_by.abs)))

        return LinearExpression(new_var_to_coeff, new_abs)

    def instantiate(self, template_variable_valuation):
        """

        :param template_variable_valuation:
        :return: Returns the linear expression obtained form substituting every template variable in some coefficient by the given value.
        """
        return LinearExpression({var : simplify(LinearExpression.fast_substitute(self.var_to_coeff[var], template_variable_valuation)) for var in self.var_to_coeff},
                                simplify(LinearExpression.fast_substitute(self.abs,template_variable_valuation)))

    def get_constantification(self, var_to_helpervariables):
        """
        Helper function for conditional difference boundedness. Substitutes every variable by it's helper variable, thereby
        turning this linear expression into a constant one.

        Careful: After de-constantification by back-substituting aux vars, var_to_coeff is no longer appropriate.
        :param var_to_helpervariables: A dict from the linear expression's variables to helper variables.
        :return:
        """

        new_abs = Plus([LinearExpression.fast_substitute(self.abs, var_to_helpervariables)] + [Times(self.var_to_coeff[var], ToReal(var_to_helpervariables[var])) for var in self.var_to_coeff])
        new_var_to_coeff = {var : Real(0) for var in self.var_to_coeff}
        return LinearExpression(new_var_to_coeff, new_abs)

    def equals(self, other):
        return EqualsOrIff(self.pysmt_expression, other.pysmt_expression)

    def equals_syntactically(self, linexp):
        return self.abs == linexp.abs and self.var_to_coeff == linexp.var_to_coeff

    @staticmethod
    def get_constant_expression(variables, constant):
        return LinearExpression({var:Real(0) for var in variables}, constant)

    def __str__(self):
        out = ""
        for key in self.var_to_coeff:
            if self.var_to_coeff[key] != Real(0):
                out += (str(simplify(self.var_to_coeff[key])) + "*" if not self.var_to_coeff[key] == Real(1) else "") + str(key) + " +"

        if self.abs != Real(0):
            out += str(simplify(self.abs))
        else:
            out = out[:-2]

        if out == "":
            out = "0"
        return out

    @staticmethod
    def fast_substitute(formula, sub):
        return get_env().substituter.walk(formula, substitutions=sub)

    def as_probably_string(self):
        """

        :return: A string representation of this linear expression which is parsable by probably
        """

        res = ""
        begin = True
        for var in self.var_to_coeff:
            coeff = Fraction(str(self.var_to_coeff[var]))
            if coeff != 0:
                if begin:
                    res = str(coeff) + "*" + str(var) if coeff >=0 else (" (0 - %s" % str(abs(coeff))) + "*" + str(var) + ")"
                    begin = False
                else:
                    res += " + " + (str(coeff) + "*" + str(var) if coeff >=0 else (" (0 - %s" % str(abs(coeff))) + "*" + str(var) + ")")

        coeff = Fraction(str(self.abs))
        if begin or coeff !=0:
            res += (" + " if not begin else "") + (str(coeff) if coeff >= 0 else (" (0 - %s)" % str(abs(coeff))))

        return res

    def all_coefficients_int(self):
        """

        :return: True iff all coefficients and the absolute values are integers
        """
        return is_pysmt_real_int(self.abs) and all(is_pysmt_real_int(self.var_to_coeff[var])for var in self.var_to_coeff)

    def is_constant_expression(self):
        """

        :return: True iff all coefficients except for the absolute part are equal to Real(0).
        """

        return all([self.var_to_coeff[var] == Real(0) for var in self.var_to_coeff])

def is_pysmt_real_int(pysmt_real):
    """

    :param pysmt_real: True iff pysmt_real represents an integer
    :return:
    """

    return Fraction(str(pysmt_real)).denominator == 1


import numpy as np
import sys, os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from kinematic.paint_kinematics import PaintKinematicsKDL

def main():
    urdf_path = "conf/vander_bot_v2.urdf"
    pk = PaintKinematicsKDL()
    pk.load_URDF(urdf_file_path = urdf_path)
    start_pose = np.array([0, 0])
    cp_pose = np.array([0.6, -0.])

    new_jpos = pk.ik_pose(new_pos = cp_pose, old_pos = start_pose)
    print(new_jpos)
    fk_pose = pk.fk_pose(q = np.array(new_jpos))
    # new_jpos = np.array([np.pi/4, np.pi/2])
    print(fk_pose)

if __name__=="__main__":
    main()


from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse

def region_to_latlon(region):
    if region == "cambridge":
        return [52.2053,0.1218]

def index(request):
    if request.method == "POST":
        template = loader.get_template('model_query/index.html')
        data = request.POST
        formData = {
            "region": data["region"],
            "latlon": region_to_latlon(data["region"]),
            "photoperiod": data["photoperiod"],
            "texture": data["texture"],
            "fertility": data["fertility"],
            "pH": data["pH"],
            "change": data["change"],
            "optimal": data["optimal"],
            "timescale": data["timescale"],
            "largeScale": False,
            "category": data["category"],
            "nutrients": data["nutrient"],
        }

        if "largeScale" in data:
            formData["largeScale"] = True

        print(formData)
        context = {"out": request.POST.keys()}
    else:
        template = loader.get_template('model_query/index.html')
        context = {"out": "not test"}
    return HttpResponse(template.render(context, request))

"""    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})"""


from scipy.stats import poisson
import math
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider,Button

class Poisson:
    def __init__(self,Lambda=10) :
        self.Lambda=Lambda
        self.x=np.arange(0,200,1)
        self.pmf=self.pmf_value()
        self.cdf=self.cdf_value()

    def pmf_value(self):
        return poisson.pmf(self.x,mu=self.Lambda)

    def cdf_value(self):
        return poisson.cdf(self.x,mu=self.Lambda)

    def plot_graph(self):
        fig,ax=plt.subplots(1,2,figsize=(15, 6))
        pmf_curve,=ax[0].plot(self.x,self.pmf,lw=2)
        fig.suptitle("Poisson probability distribution")
        ax[0].set_title("PMF")
        ax[0].set_xlabel("random variables")
        ax[0].set_ylabel("probability")
        ax[1].set_ylabel("cumulative probability")
        ax[1].set_xlabel("random variables")
        ax[1].set_title("CDF")

        cdf_curve,=ax[1].plot(self.x,self.cdf)
        fig.subplots_adjust(bottom=0.25,)
        fig.subplots_adjust(bottom=0.25,)
        axmu=fig.add_axes([0.25,0.1,0.65,0.03])

        Lambda_slider=Slider(ax=axmu,label='Lambda',valmin=0.1,valmax=100,valinit=self.Lambda)

        def update(val):
            self.Lambda=Lambda_slider.val
            print("value of lambda - %s mean of the distribution - %s variance of the distribution - %s"%(self.Lambda,self.Lambda,self.Lambda))
            self.cdf=self.cdf_value()
            self.pmf=self.pmf_value()
            cdf_curve.set_ydata(self.cdf)
            cdf_curve.set_xdata(self.x)

            # print('mean - %s and variance - %s'%(mean,vari))
            pmf_curve.set_ydata(self.pmf)
            pmf_curve.set_xdata(self.x)
            fig.canvas.draw_idle()

        resetax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
        button = Button(resetax, 'Reset', hovercolor='0.975')

        def reset(event):
            Lambda_slider.reset()

        button.on_clicked(reset)

        Lambda_slider.on_changed(update)

        plt.show()

if __name__ == "__main__":
    s=Poisson(Lambda=20)
    s.plot_graph()


#!/usr/bin/env python
from datetime import datetime  # Zeit modul um festzulegen wie oft messungen gemacht werden
import time

def is_time(): # Alle 30 Minuten die Daten auslesen.
    return True
    now = datetime.now()
    current_minute = now.strftime("%M")
    print(current_minute)
    if current_minute == "30" or current_minute == "00":
        print("fa")
        return True
    else:
        return False

sensor1 = "/sys/bus/w1/devices/28-3c01f0960400/w1_slave"  # Sensor 1
sensor2 = "/sys/bus/w1/devices/28-3c01f096db57/w1_slave"  # Sensor 2
f = open(sensor1, "r")  # Auf beide Sensoren zugreifen
f2 = open(sensor2, "r")

sensor1_text = open("Sensor1.txt", "a")
sensor2_text = open("Sensor2.txt", "a")

data = f.read()  # Die Daten auslesen
data2 = f2.read()
while True:
    if is_time():
        f = open(sensor1, "r")  # Auf beide Sensoren zugreifen
        f2 = open(sensor2, "r")
        data = f.read()
        data2 = f2.read()

        print("Its time")
        print("Data ready")
        (discard, sep, reading) = data.partition(' t=')
        t = float(reading) / 1000.0
        print("Sensor 1 {:.1f}".format(t))
        with open("Sensor1.txt", "a") as f:
            f.write("\n")
            now = datetime.now()
            current_minute = now.strftime("%H:%M")
            f.write(current_minute + " ," + str(t))

        print("Data 2 ready")
        (discard, sep, reading) = data2.partition(' t=')
        t = float(reading) / 1000.0
        print("Sensor 2 {:.1f}".format(t))
        with open("Sensor2.txt", 'a') as f:
            f.write("\n")
            now = datetime.now()
            current_minute = now.strftime("%H:%M")
            f.write(current_minute + " ," + str(t))

    else:
        time.sleep(20)


"""
Bismallah
"""
import random

def get_word_list(choice):
    words = []
    with open(choice) as word_file:
        for line in word_file:
            if (len(line) >= 4):
                words.append(line.strip())
    return words

def game_word(choice):
    word_list = get_word_list(choice)
    rand = random.randrange(len(word_list))
    return word_list[rand].upper()


import cv2
import numpy as np

def start():
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == 27:
            break

    cap.release()
    cv2.destroyAllWindows()

def close(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.destroyAllWindows()
        start()

def main_page():
    img = cv2.imread('eye.jpg', 1)
    cv2.imshow('start', img)
    cv2.setMouseCallback('start', close)
    k = cv2.waitKey(0) & 0xFF

if __name__ == '__main__':
    main_page()


import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.orm import Session
import sqlalchemy.ext.declarative as dec
from sqlalchemy import orm

SqlAlchemyBase = dec.declarative_base()

#
# Работа с бызой данных ведётся с помощью SQLAlchemy.
# Тут определяются функции global_init(db_file), подключающая базу данных к проекту и
# create_session(), которая создаёт сессию для работиы с базой данных.
#

__factory = None

def global_init(db_file):
    global __factory

    if __factory:
        return

    if not db_file or not db_file.strip():
        raise Exception("Необходимо указать файл базы данных.")

    conn_str = f'sqlite:///{db_file.strip()}?check_same_thread=False'
    print(f"Подключение к базе данных по адресу {conn_str}")

    engine = sa.create_engine(conn_str, echo=False)
    __factory = orm.sessionmaker(bind=engine)

    from . import __all_models

    SqlAlchemyBase.metadata.create_all(engine)

def create_session() -> Session:
    global __factory
    return __factory()


# 自己写的版本 类似于3sum。
# run time 的要求没有3sum 那么严格 可以不太考虑去重
class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums:
            return 0
        nums.sort()
        min_diff = sys.maxsize
        result = 0
        for i in range(len(nums)-1):
            fix_n = nums[i]
            diff = target-fix_n
            ps = i+1
            pt = len(nums)-1
            while ps < pt:
                val = nums[ps]+nums[pt]
                if abs(diff-val)<min_diff: # update 结果 注意这里面每一次都要update一下
                    min_diff = abs(diff-val)
                    result = fix_n+val
                if fix_n+val < target: #
                    ps+=1
                elif fix_n+val >target:
                    pt-=1
                else:
                    return target
        return result


from django.db import models
from Perfil.models import Perfil
from POT.models import POT

# Create your models here.
class Reuniao(models.Model):
    nome = models.CharField(blank=False, null=False, max_length=255)
    descricao = models.CharField(blank=False, null=False, max_length=400)
    data_hora = models.DateTimeField()
    created_at = models.DateTimeField(auto_now_add=True)

    participantes = models.ManyToManyField(Perfil,
        related_name="reunioes_do_perfil")
    POT = models.ForeignKey(POT, models.CASCADE,
        related_name="reunioes_do_pot")

    def __str__(self) -> str:
        return f"{self.nome} {self.data_hora}"


def get_input(filename):
    vent_lines = []
    max_x = 0
    max_y = 0
    with open(filename, 'r') as i:
        for line in i.readlines():
            line = line.strip()
            start, end = line.split(" -> ")
            x1, y1 = start.split(",")
            x2, y2 = end.split(",")
            x1 = int(x1)
            x2 = int(x2)
            y1 = int(y1)
            y2 = int(y2)
            max_x = max(max_x, x1, x2)
            max_y = max(max_y, y1, y2)
            vent_lines.append(
                {
                    "start": {
                        "x": x1,
                        "y": y1
                    },
                    "end": {
                        "x": x2,
                        "y": y2
                    }
                }
            )
    return vent_lines, max_x, max_y

def map_vents(vent_lines, max_x, max_y, diagonal=False):
    vent_overlap_count = 0
    vent_map = []
    for y in range(max_y + 1):
        vent_map.append([0 for x in range(max_x + 1)])

    for vent_line in vent_lines:
        x1 = vent_line["start"]["x"]
        y1 = vent_line["start"]["y"]
        x2 = vent_line["end"]["x"]
        y2 = vent_line["end"]["y"]

        # vertical lines
        if x1 == x2:
            for y in range(min(y1, y2), max(y1, y2) + 1):
                vent_map[y][x1] += 1
                if vent_map[y][x1] == 2:
                    vent_overlap_count += 1
        # horizontal lines
        elif y1 == y2:
            for x in range(min(x1, x2), max(x1, x2) + 1):
                vent_map[y1][x] += 1
                if vent_map[y1][x] == 2:
                    vent_overlap_count += 1
        # diagonals
        elif diagonal:
            # preserving ordering of x1,y1 to x2,y2 is needed
            # for diagonals, have to figure out which way to
            # iterate the ranges to generate proper coords
            xdir = -1 if x1 > x2 else 1
            ydir = -1 if y1 > y2 else 1
            xs = [x for x in range(x1, x2 + xdir, xdir)]
            ys = [y for y in range(y1, y2 + ydir, ydir)]
            for index, x in enumerate(xs):
                vent_map[ys[index]][x] += 1
                if vent_map[ys[index]][x] == 2:
                    vent_overlap_count += 1

    return vent_overlap_count

def main():
    vent_lines, max_x, max_y = get_input("input")
    vent_overlap_count = map_vents(vent_lines, max_x, max_y)
    print("Part 1:")
    print(vent_overlap_count)
    print()

    vent_overlap_count = map_vents(vent_lines, max_x, max_y, diagonal=True)
    print("Part 2:")
    print(vent_overlap_count)

if __name__ == "__main__":
    main()


# def funA():
#     return 'This is funA.'

# funB = funA
# print('run funA:', funA())
# print('run funB:', funB())
# print('name of funA:', funA.__name__)
# print('name of funB:', funB.__name__)
# class ClsA:
#     pass

# ClsB = ClsA

# print('new ClsA:', ClsA())
# print('new ClsB:', ClsB())
# print('name of ClsA:', ClsA.__name__)
# print('name of ClsB:', ClsB.__name__)

class ClsA:
    _cnt = 0
    def __init__(self):
        self.__class__._cnt += 1
        self.idx = self._cnt
ClsB = ClsA
insA = ClsA()
print('new ClsA:', insA)
print('idx of insA:', insA.idx)
insB = ClsB()
print('new ClsB:', insB)
print('idx of insB:', insB.idx)


import unittest
import responses
from src.exceptions import NoAccessGroupError, NoUserProfileError
from src.search1_conversion import convert_result
from src.utils.config import config
from tests.unit.mocks.mocked import \
    get_data, \
    workspace_call, user_profile_call

# TODO test post processing
# TODO test the following fields: object_name, obj_id, version, type, creator

class Search1ConversionTest(unittest.TestCase):
    @responses.activate
    def test_search_objects_valid(self):
        responses.add_callback(responses.POST, config['workspace_url'],
                               callback=workspace_call)

        responses.add_callback(responses.POST, config['user_profile_url'],
                               callback=user_profile_call)

        # Using case-01 params, ES result and api result.
        _found, test_params = get_data(
            'SearchAPI/legacy/search_objects/case-01/params.json')
        _found, test_es_search_results = get_data(
            'elasticsearch/legacy/search_objects/case-01/result.json')
        _found, test_expected = get_data(
            'SearchAPI/legacy/search_objects/case-01/result.json')

        final = convert_result.search_objects(test_params, test_es_search_results,
                                              {'auth': None})

        # Remove unwanted comparisons.
        del final['search_time']
        del test_expected['search_time']

        self.maxDiff = None
        self.assertEqual(final, test_expected)

    @responses.activate
    def test_get_objects_valid(self):
        responses.add_callback(responses.POST, config['workspace_url'],
                               callback=workspace_call)

        responses.add_callback(responses.POST, config['user_profile_url'],
                               callback=user_profile_call)

        _found, test_params = get_data(
            'SearchAPI/legacy/get_objects/case-02/params.json')
        _found, test_es_search_results = get_data(
            'elasticsearch/legacy/get_objects/case-02/result.json')
        _found, test_expected = get_data(
            'SearchAPI/legacy/get_objects/case-02/result.json')

        final = convert_result.get_objects(test_params, test_es_search_results, {'auth': None})
        self.assertEqual(final, test_expected)

    def test_search_types_valid(self):
        _found, test_es_search_results = get_data(
            'elasticsearch/legacy/search_types/case-01/result.json')
        _found, test_expected = get_data(
            'SearchAPI/legacy/search_types/case-01/result.json')

        # Not that converting search_types does not require any
        # params or context.
        final = convert_result.search_types(test_es_search_results)

        self.assertEqual(final['type_to_count'], test_expected['type_to_count'])

    def test_fetch_narrative_info_no_hits(self):
        results = {
            'hits': []
        }
        ctx = {}
        result = convert_result._fetch_narrative_info(results, ctx)
        assert len(result) == 2
        assert result[0] == {}
        assert result[1] == {}

    # TODO: This condition should not occur in any object index!
    def test_fetch_narrative_info_no_access_group(self):
        results = {
            'hits': [{
                'doc': {}
            }]
        }
        with self.assertRaises(NoAccessGroupError):
            convert_result._fetch_narrative_info(results, {'auth': None})

    @responses.activate
    def test_fetch_narrative_info_owner_has_profile(self):
        responses.add_callback(responses.POST, config['workspace_url'],
                               callback=workspace_call)

        responses.add_callback(responses.POST, config['user_profile_url'],
                               callback=user_profile_call)

        _found, test_es_search_results = get_data(
            'elasticsearch/legacy/search_objects/case-01/result.json')
        _found, test_expected = get_data(
            'SearchAPI/legacy/search_objects/case-01/result.json')

        ctx = {
            'auth': None
        }
        result = convert_result._fetch_narrative_info(test_es_search_results, ctx)
        self.assertEqual(len(result), 2)

        expected_result = test_expected['access_group_narrative_info']
        self.assertEqual(result[1], expected_result)

    @responses.activate
    def test_fetch_narrative_info_owner_has_no_profile(self):
        responses.add_callback(responses.POST, config['workspace_url'],
                               callback=workspace_call)

        responses.add_callback(responses.POST, config['user_profile_url'],
                               callback=user_profile_call)
        results = {
            'hits': [{
                'doc': {
                    'access_group': 10056638
                }
            }]
        }
        meta = {
            'auth': None
        }
        with self.assertRaises(NoUserProfileError) as e:
            convert_result._fetch_narrative_info(results, meta)
        self.assertEqual(e.exception.message,
                         'A user profile could not be found for "kbaseuitestx"')

    def test_get_object_data_from_search_results(self):
        responses.add_callback(responses.POST, config['workspace_url'],
                               callback=workspace_call)
        responses.add_callback(responses.POST, config['user_profile_url'],
                               callback=user_profile_call)

        _found, test_params = get_data(
            'SearchAPI/legacy/search_objects/case-01/params.json')
        _found, test_es_search_results = get_data(
            'elasticsearch/legacy/search_objects/case-01/result.json')
        _found, test_expected = get_data(
            'SearchAPI/legacy/search_objects/case-01/result.json')

        post_processing = test_params['post_processing']
        converted = convert_result._get_object_data_from_search_results(
            test_es_search_results,
            post_processing)
        self.assertEqual(converted, test_expected['objects'])