import json

def load_populations():
    dat = {}
    with open("country_populations.txt", 'r', encoding='utf-8') as f:
        f.readline()
        for line in f:
            if '"' in line:
                nline = line.strip().split('"')
                name = nline[1]
                line_dat = nline[2].strip(',').split(',')
                tag = line_dat[0]
                year = int(line_dat[1])
                population = int(line_dat[2])
            else:
                line_dat = line.strip().split(',')
                name = line_dat[0]
                tag = line_dat[1]
                year = int(line_dat[2])
                population = int(line_dat[3])
            if tag in dat:
                dat[tag]['populations'][year] = population
            else:
                dat[tag] = {'name': name, 'populations': {year: population}}
    return dat

def load_groupings():
    with open('ideologies.txt', 'r', encoding='utf-8') as f:
        return json.load(f)

def get_population(name: str, populations):
    while '_' in name:
        i = name.find('_')
        name = name[:i] + ' ' + name[i + 1:]
    for attr in populations.values():
        if name in attr['name']:
            return attr['populations'][max(attr['populations'].keys())]
    else:
        names = {'North Korea': 'PRK',
                 'South Korea': 'KOR',
                 'Czechia': 'CZE',
                 'Slovakia': 'SVK',
                 'DR Congo': 'COD',
                 'Timor Leste': 'TLS',
                 'Laos': 'LAO',
                 'Cote d Ivoire': 'CIV',
                 'Kyrgyzstan': 'KGZ',
                 'Guinea Bissau': 'GNB',
                 'Saint Vincent and the Grenadines': 'VCT'}
        try:
            tag = names[name]
            temp = populations[tag]['populations']
            return temp[max(temp.keys())]
        except KeyError:
            if name == 'Taiwan':
                return 23590000
            else:
                print("Not found:", name)
                return 0

if __name__ == '__main__':
    populations = load_populations()
    groupings = load_groupings()
    ideological_pops = {}
    for col, attr in groupings['groups'].items():
        ideological_pops[attr['label']] = sum([get_population(name, populations) for name in attr['paths']])
    order = sorted(list(ideological_pops.keys()), key=lambda n: ideological_pops[n], reverse=True)
    print()
    print('Ideologies'.rjust(24), 'Population'.rjust(16))
    for ideology in order:
        print(ideology.rjust(24), str(ideological_pops[ideology]).rjust(16))


import os
import sys

#Function that takes a file path, converts our output data to a bed file with the result.
def var_to_bed(file_path):
    rgb_dict = {'G' : '209,113,5', 'T' : '255,0,0', 'A' : '50,241,50', 'C' : '0,0,255'}
    write_path = file_path[:-4] + ".bed"
    read_fh = open(file_path, 'r')
    write_fh = open(write_path, 'w')
    for line in read_fh:
        if "Position" in line:
            continue
        split_line = line.split()[:3]
        chrom = split_line[0]
        start = int(split_line[1]) - 1
        stop = start + 1
        name = split_line[2]
        line = [chrom, str(start), str(stop), f"Observed Variant: {name}", "0", ".", str(start), str(stop), rgb_dict[name]]
        line = "\t".join(line)+"\n"
        write_fh.write(line)
    read_fh.close()
    write_fh.close()

def main(directory):
    files = os.listdir(directory)
    for file in files:
        if '.tsv' not in file or 'pooled' in file:
            continue
        var_to_bed(f"{directory}/{file}")

if __name__ == "__main__":
    main(sys.argv[1])


A=[1,3,5,6,2,2,1,4]

def left_elem(A, i):
  sum_left,j=0,0
  while(j<i):
    sum_left+=A[j]
    j+=1
  return sum_left

def right_elem(A, i):
  sum_right,j=0,(i+1)
  while(j<len(A)):
    sum_right+=A[j]
    j+=1
  return sum_right

def equ_pt(A):
  for k in range(len(A)):
    l=left_elem(A, k)
    r=right_elem(A, k)
    if (l == r):
      return A[k]

e=equ_pt(A)
print(e)


def CampCleanup(part1InputFile):
    f = open(part1InputFile, 'r')
    input = [line.rstrip('\n').split(",") for line in f]

    numOverlappingRanges = 0

    for pair1, pair2 in input:

        x1, y1 = map(int, pair1.split('-'))
        x2, y2 = map(int, pair2.split('-'))

        if x1 >= x2 and y1 <= y2:
            numOverlappingRanges += 1

        elif x2 >= x1 and y2 <= y1:
            numOverlappingRanges += 1

    print(numOverlappingRanges)

CampCleanup("day4\input.txt")


#!/usr/bin/python3
# -*- coding: utf-8 -*-

### Console Version

##----- line_with_different_scale.py

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt
import sqlite3

conn = sqlite3.connect('AK98_log_pressures_and_valves_new_version.db')
c = conn.cursor()

#c.execute('''SELECT time_since_start*1, c2122*1, c2141*1, PD_PRESSURE*1 FROM ak98_events WHERE archive_name = "Archive_4469_202208010940" AND (c2122 IS NOT NULL OR c2141 IS NOT NULL OR PD_PRESSURE IS NOT NULL);''')

#rows = c.fetchall()

time_since_start = []
pressure_1 = []
pressure_2 = []
pressure_3 = []

'''
for row in rows:
    time_since_start.append(row[0])
    pressure_1.append(row[1])
    pressure_2.append(row[2])
    pressure_3.append(row[3])
'''

#figu = plt.figure()
ax = plt.axes(projection ='3d')

c.execute('''SELECT date(moment_specific) as date_day FROM ak98_events GROUP BY date(moment_specific);''')
date_days = c.fetchall()

for date_day in date_days:
	#print(row)
    q = "SELECT time_since_start*1, c2122*1, c2141*1, PD_PRESSURE*1 FROM ak98_events WHERE date(moment_specific) = '" + date_day[0] + "' ORDER BY time_since_start*1 LIMIT 10;"
    c.execute(q)
    rows_pressure = c.fetchall()

    time_since_start.clear()
    pressure_1.clear()
    pressure_2.clear()
    pressure_3.clear()

    for row in rows_pressure:
        print("time=", int(row[0]), " press1=", row[3])
        time_since_start.append(int(row[0]))
        pressure_1.append(row[1])
        pressure_2.append(row[2])
        pressure_3.append(row[3])

    #plt.scatter(time_since_start, pressure_1)
    #plt.scatter(time_since_start, pressure_2)
    plt.plot(time_since_start, pressure_3)

    #print(len(time_since_start))

    #i = 0
    #for time_moment in time_since_start:
    #    i = i + 1
    #    #ax.plot3D(time_since_start, pressure_3, i)
    #    print(i)

    #ax.scatter3D(time_since_start, pressure_3, i)

#commit the changes to db
conn.commit()
#close the connection
conn.close()

"""
# X and Y data

numberofemp = [13, 200, 250, 300, 350, 400]

rev = [0.4, 0.6, 0.8, 0.7, 0.8, 0.9]

year = [2011, 2012, 2013, 2014, 2015, 2016]

# plot numberofemp on xaxis_1
fig, xaxis_1 = plt.subplots()

#xaxis_1.plot( year, numberofemp, marker="D", mfc="green", mec="yellow", ms="7" )
xaxis_1.plot( time_since_start, pressure_1, marker="D", mfc="green", mec="yellow", ms="7" )

xaxis_1.plot( time_since_start, pressure_2, marker="D", mfc="red", mec="yellow", ms="7" )

xaxis_1.plot( time_since_start, pressure_3, marker="D", mfc="blue", mec="yellow", ms="7" )

xaxis_1.set_xlabel("Year")

xaxis_1.set_ylabel("Number of Employees")

xaxis_1.set_title("Number of Employee and Revenue")
" ""
# create xaxis_2 with shared x-axis

xaxis_2 = xaxis_1.twinx()

# plot rev on xaxis_2

xaxis_2.plot(year, rev, marker="o", mfc="red", mec="green",ms="7")

xaxis_2.set_ylabel("Rev [$M]")

# setting the legend
" ""
fig.legend(["Number of Employee", "Rev"], loc="upper left")
"""
plt.show()


class Solution(object):
    def findDiagonalOrder(self, nums):
        """
        :type nums: List[List[int]]
        :rtype: List[int]
        """

        diagonals = [deque() for i in range(len(nums) + max(len(row) for row in nums) - 1)]
        for row_id, row in enumerate(nums):
            for col_id in range(len(row)):
                diagonals[row_id + col_id].appendleft(row[col_id])
        return list(chain(*diagonals))


import datetime
import pendulum
import os
import pandas as pd
import numpy as np
import pickle
from prophet import Prophet
from pmdarima import auto_arima
from datetime import date, timedelta
from airflow.sensors.external_task import ExternalTaskSensor
from airflow.models.baseoperator import chain
import time
from airflow.decorators import dag, task

@dag(
    dag_id="forecast-traffy-flood",
    schedule_interval='0 1 * * *',
    start_date=pendulum.datetime(2023, 5, 16, tz='Asia/Bangkok'),
    catchup=False,
    dagrun_timeout=datetime.timedelta(minutes=360),
)
def ForecastTraffyFlood():

    @task
    def get_data(**kwargs):
        ti = kwargs["ti"]
        df = pd.read_csv('/opt/airflow/dags/data/report.csv')
        ti.xcom_push("base_data_df", df)

    @task
    def get_rain(**kwargs):
        ti = kwargs["ti"]
        df = pd.read_csv('/opt/airflow/dags/data/clean_rain_amount.csv')
        ti.xcom_push("clean_rain_df", df)

    @task
    def get_sea(**kwargs):
        ti = kwargs["ti"]
        df = pd.read_csv('/opt/airflow/dags/data/clean_sea_level.csv')
        ti.xcom_push("clean_sea_df", df)

    @task
    def get_f_rain(**kwargs):
        ti = kwargs["ti"]
        df = pd.read_csv('/opt/airflow/dags/data/output/rain_amount_output_all.csv')
        ti.xcom_push("f_rain_df", df)

    @task
    def get_f_sea(**kwargs):
        ti = kwargs["ti"]
        df = pd.read_csv('/opt/airflow/dags/data/output/sea_level_output.csv')
        ti.xcom_push("f_sea_df", df)

    @task
    def clean_data(**kwargs):
        ti = kwargs["ti"]
        df = ti.xcom_pull(task_ids="get_data", key="base_data_df")
        ti.xcom_push("clean_data_df", df)
        # data_df['date'] = pd.to_datetime(data_df['date']).dt.date
        # data_df['report'] = 1
        # group_df = data_df.groupby('district')
        # district = list()
        # for key,value in group_df:
        #     ser = value['date'].value_counts()
        #     district.append({'date':ser.index,key:ser.values})
        # st_date = date(2019, 9, 19)
        # ed_date = date(2023, 4, 21)
        # all_day = {'date':[]}
        # for n in range(int((ed_date - st_date).days)):
        #     all_day['date'].append((st_date + timedelta(n)).strftime("%Y-%m-%d"))
        #     all_day = pd.DataFrame(all_day)
        #     all_day['date'] = pd.to_datetime(all_day['date']).dt.date
        # for d in district:
        #     tmp = pd.DataFrame(d)
        #     all_day = all_day.merge(tmp,how='left',on='date')
        # all_day.fillna(0,inplace=True)
        # all_day.columns = all_day.columns.str.replace('_.*','',regex=True)
        #return data_df

    @task
    def forecast(**kwargs):
        ti = kwargs["ti"]
        clean_report_df = ti.xcom_pull(task_ids="clean_data", key="clean_data_df")
        clean_rain_df = ti.xcom_pull(task_ids="get_rain", key="clean_rain_df")
        clean_sea_df = ti.xcom_pull(task_ids="get_sea", key="clean_sea_df")
        f_rain_df = ti.xcom_pull(task_ids="get_f_rain", key="f_rain_df")
        f_sea_df = ti.xcom_pull(task_ids="get_f_sea", key="f_sea_df")
        array1 = np.array(f_sea_df['yhat'])
        array2 = { d:np.array(f_rain_df[d]) for d in clean_rain_df.columns[1::]}
        districts = clean_rain_df.columns[1::]
        clean_sea_df = clean_sea_df.rename(columns={'y':'sea_level'})
        for district in districts:
            tmp = clean_rain_df[['date',district]]
            train = tmp.rename(columns={'date':'ds',district:'y'})
            temp_report = clean_report_df[['date', f'{district}']].copy()
            temp_rain_amount= clean_rain_df[['date', f'{district}']].copy()
            # temp_report['date'] = pd.to_datetime(temp_report['date'])
            # temp_rain_amount['date'] = pd.to_datetime(temp_rain_amount['date'])
            temp_report.rename(columns={'date':'ds',f'{district}':'y'},inplace=True)
            temp_rain_amount.rename(columns={'date':'ds',f'{district}':'rain_amount'},inplace=True)
            merged_df = pd.concat([temp_report.set_index('ds'), temp_rain_amount.set_index('ds') \
                                   , clean_sea_df.set_index('ds')], axis=1, join='inner').reset_index()
            model_report = Prophet()
            model_report.add_regressor('sea_level')
            model_report.add_regressor('rain_amount')
            model_report.fit(merged_df)

            future_dates_report = model_report.make_future_dataframe(periods=365)
            x = future_dates_report.shape[0]
            future_dates_report['sea_level'] = array1[-x:]
            future_dates_report['rain_amount'] = array2[district][-x:]
            forecast3 = model_report.predict(future_dates_report)

            record_report_amount = forecast3[['ds', 'yhat']].copy()
            record_report_amount.to_csv(f'/opt/airflow/dags/data/output/report_output_{district}.csv'\
                            ,index=False,header=True,encoding = 'utf-8-sig')
            with open(f'/opt/airflow/dags/data/model/report_{district}.pkl','wb') as f:
                    pickle.dump(model_report, f)

    wait_for_sea_level_forecast = ExternalTaskSensor(
        task_id="wait_sea_level_forecast",
        external_dag_id="process-sea-level",
        external_task_id="trigger_sea_forecast",
        allowed_states=["success"],
        failed_states=["failed", "skipped"],
        execution_delta= timedelta(hours=1),
        poke_interval = 10
    )

    wait_for_rain_amount_forecast = ExternalTaskSensor(
        task_id="wait_rain_amount_forecast",
        external_dag_id="process-rain-amount",
        external_task_id="trigger_rain_forecast",
        allowed_states=["success"],
        failed_states=["failed", "skipped"],
        execution_delta= timedelta(hours=1),
        poke_interval = 10
    )

    chain([ wait_for_rain_amount_forecast, wait_for_sea_level_forecast],get_data(),clean_data(), \
                                [get_rain() , get_sea(), get_f_rain(), get_f_sea()],forecast())

dag = ForecastTraffyFlood()


def array_plus_array(arr1, arr2):
    sum = 0
    for i in arr1:
        sum += int(i)
    for j in arr2:
        sum += int(j)
    return sum

def solution (arr1, arr2):
    return sum(arr1 + arr2)

print(array_plus_array([1,2,3],[4,5,6]))
print(array_plus_array([-1, -2, -3], [-4, -5, -6]))

print(solution([1,2,3],[4,5,6]))
print(solution([-1, -2, -3], [-4, -5, -6]))


import socket
import json
import threading
import signal

from cluster_predict import predictCnt, predictTfidf, init_clustering

HOST = "localhost"
PORT = 65432

def parseJson(jsonString):
    root = json.loads(jsonString)
    messages = root['jobInfo']
    messages = [m['value'] for m in messages]
    count = len(messages)
    v = root['vectorizer']
    return count, messages, v

def createResponse(data_dict):
    return json.dumps(data_dict)

#Get JSON from client, return another JSON that contains clustering information
def process_data(data):
    count, messages, vectorizer = parseJson(data)
    responses = []
    if count > 0:
        if vectorizer == 'count':
            responses = predictCnt(messages)
        else:
            responses = predictTfidf(messages)
        responses = [{"value" : int(response)} for response in responses]
    jsonResponse = {"count" : count, "clusters" : responses}
    return createResponse(jsonResponse)

def print_thread(data):
    print(data)

def handle_request(connection, address):
    with connection:
        print("Connection by", address)
        while True:
            data = connection.recv(16384)
            if not data:
                break
            data = data.decode('utf-8')
#            threading.Thread(target=print_thread, args=(data,)).start()
            response = process_data(data)
#            threading.Thread(target=print_thread, args=(response,)).start()
            connection.sendall(response.encode('utf-8'))

def handle_connection():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen()
        while True:
            connection, address = s.accept()
            handle_request(connection, address)
        s.close()

def start_connection():
    while True:
        try:
            handle_connection()
        except ConnectionResetError:
            print("Connection has been reset by remote client. Restarting...")
            pass
    print("No longer listening for connection")

if __name__ == '__main__':
    #Init pickle modules
    init_clustering()
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    #Start listening for connections
    start_connection()


from typing import Optional, List

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root:
            return []

        returnable = []
        index, nodes_in_layer = 0, 1
        while index <= len(root) - 1:
            target_list = root[index: index + nodes_in_layer]
            if None not in target_list:
                returnable.append(target_list)
            else:
                returnable.append([x for x in target_list if x is not None])

            index += nodes_in_layer
            nodes_in_layer *= 2

        return returnable

def main():
    # input = [3, 9, 20, None, None, 15, 7]
    input = [3, 7, 12, -2, 1, None, None, None, None, 6, 5, None, None, None, None]
    sol = Solution()
    print(sol.levelOrder(input))

if __name__ == '__main__':
    main()


from django import template

from home.models import HomePage

register = template.Library()

items = HomePage.objects.first().get_children().live().public().in_menu()

# Advert snippets
@register.inclusion_tag('site_settings/tags/menu_items.html', takes_context=True)
def menu_items(context):
    return {
        'menu_items': items,
        'request': context['request'],
    }


import os
import sys

import pandas as pd
import numpy as np
from scipy import stats

import matplotlib as mpl
import matplotlib.pyplot as plt
dpi = 1600
side = 7
rc_fonts = {
    "font.family": "serif",
    "font.size": 12,
    'figure.figsize': (0.8*side, 0.6*side),
    "text.usetex": True
    }
mpl.rcParams.update(rc_fonts)

R = 8
ratio = 48
A = -40

try:
    surf_con = float(sys.argv[1])
except IndexError:
    surf_con = 1.0

if R == 2:
    separation = 1.7
elif R == 4:
    separation = 2.5
elif R == 6:
    separation = 4
elif R == 8:
    separation = 5.5
    separation = 8.5
elif R == 10:
    separation = 7

case = f'R{R}_ratio{ratio}_A{abs(A)}'
# path = f'/home/luishcc/md-projects/analysis/cluster/R{R}_ratio{ratio}_A{abs(A)}/'
path = f'/home/luishcc/md-projects/analysis/cluster/break-avg/R{R}_ratio{ratio}_A{abs(A)}'
# path = f'/home/luishcc/md-projects/analysis/cluster/break-avg/R{R}_ratio{ratio}_A{abs(A)}'

case = f'R{R}-{surf_con}'
path = f'/home/luishcc/md-projects/analysis/cluster/R{R}-{surf_con}'

num_cluster = {}
num_main = {}
num_satellite = {}
num_drops = {}

for file in os.scandir(path):

    try:
        df = pd.read_csv(file.path)
    except:
        continue
    name = int(file.name.split('.')[0])

    print(df.shape)

    df.drop(df[df['size'] <= 8].index, inplace=True)
    df.drop(df[df['anisotropy'] > 0.25].index, inplace=True)
    df['radius'] = df['radius'].multiply(np.sqrt(5/3))

    satellite = df[df['radius'] < separation]
    main = df[df['radius'] > separation]

    print(df.shape)

    num_main[name] = main.shape[0] / (30 * 2*np.pi*R*0.8)

snap = max(num_main, key=num_main.get)

file = path + f'/{snap}.csv'

df = pd.read_csv(file)

print(df.shape)

df.drop(df[df['size'] <= 1].index, inplace=True)
df.drop(df[df['anisotropy'] > 0.2].index, inplace=True)

# Radius of Gyration to sphere:
df['radius'] = df['radius'].multiply(np.sqrt(5/3))

print(df.shape)

plt.figure(1)

# df['radius'].plot.kde(bw_method=0.1)
df['radius'].plot.hist(bins=50, alpha=0.5, density=True)
data = df['radius'].plot.kde(bw_method=0.2).get_lines()[0].get_xydata()

main = df.drop(df[df['radius'] < separation].index)
satellite = df.drop(df[df['radius'] > separation].index)

data = data.swapaxes(0,1)

for i,d in enumerate(data[0]):
    if d > separation:
        sep_id = i
        break

print(data[0][np.argmax(data[1][:sep_id])])
print(data[0][np.argmax(data[1][sep_id:])+sep_id])

plt.title(f'Droplet Size Distribution, A={A}, snapshot={snap}')
# plt.xlim(0, 15)
# plt.ylim(0, 0.7)
plt.xlabel('Radius')
plt.ylabel('Density')
plt.grid(True)
plt.show()
plt.close(1)

#


import os

path = '/media/ActiveTraining/Python-Codes/3D-Text2LIVE/code/nerf-pytorch/data/custom/val/'
print(path)
for file in os.listdir(path):
    img_type = file.split('.')
    if not img_type[1] == 'png-composite':
        os.remove(path + file)
    if img_type[1] == 'png-composite':
        os.rename(path+file, path+img_type[0]+'.png')


from transformers import (WEIGHTS_NAME, AdamW, get_linear_schedule_with_warmup,
                          RobertaConfig, RobertaModel, RobertaTokenizer)
from model import Model
import torch
import scann
import numpy as np
import torch.nn.functional as f
import sys
sys.path.append('.')

if __name__=='__main__':
    model_name='saved_search_context/checkpoint-best-mrr/model.bin'
    config = RobertaConfig.from_pretrained('codebert',
                                          cache_dir= None)
    tokenizer = RobertaTokenizer.from_pretrained('codebert',
                                                cache_dir=None)
    code_model = RobertaModel.from_pretrained('codebert',
                                            config=config,
                                           cache_dir=None)
    nl_model = RobertaModel.from_pretrained('codebert',
                                            config=config,
                                           cache_dir=None)
    model=Model(nl_model,code_model,config,tokenizer,args=None)
    model.load_state_dict(torch.load(model_name))
    #search: save a pytorch model pth
    pytorch_total_params = sum(p.numel() for p in nl_model.parameters() if p.requires_grad)
    print('Total number of parameters:', pytorch_total_params)

    torch.save(model.state_dict(), 'saved_search_context/model.pth')


import unittest
import cv2
import numpy as np
import sys, os
import random
import shutil
import itertools

from bauta.utils.BasicBackgroundRemover import BasicBackgroundRemover
from bauta.utils.ImageUtils import ImageUtils

class TestBasicBackgroundRemoval(unittest.TestCase):

    def testBasicBackgroundRemoval(self):
        image_utils = ImageUtils()
        image_base = cv2.imread('test/data/images/generated/augmented_image_with_overlappings/1_mask_square.png')
        basic_background_remover = BasicBackgroundRemover()
        image_base_with_alpha_channel = image_utils.addAlphaChannelToImage(image_base)
        image_without_background = basic_background_remover.removeFlatBackgroundFromRGB(image_base)

        difference_of_images = cv2.subtract(image_without_background, image_base_with_alpha_channel)
        difference_of_images_inverse = cv2.subtract(image_base_with_alpha_channel, image_without_background)

        self.assertTrue(np.count_nonzero(difference_of_images - difference_of_images_inverse > 10) < 10)

    def testBasicBackgroundRemovalForImageWithHole(self):
        image_utils = ImageUtils()
        image_base = cv2.imread('./test/data/images/square/square_5_with_hole_and_background.png')
        image_result = cv2.imread('test/data/images/generated/background_removed/square_5_background_removed.png', cv2.IMREAD_UNCHANGED)
        basic_background_remover = BasicBackgroundRemover()
        image_without_background = basic_background_remover.removeFlatBackgroundFromRGB(image_base)

        difference_of_images = cv2.subtract(image_without_background, image_result)
        difference_of_images_inverse = cv2.subtract(image_result, image_without_background)
        self.assertTrue(np.count_nonzero( (difference_of_images + difference_of_images_inverse) > 10) < 10)

if __name__ == '__main__':
    unittest.main()


from dromosense import getCsvDatas, rd
from dromosense.constantes import *
import numpy as np
from scipy.integrate import odeint
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
import math

"""
col 0 : Température air (°C)
col 1 : Température point de rosée (°C)
col 2 : Nature des précipitations
col 3 : Vitesse du vent (m/s)
col 4 : Rayonnement global (W/m2)
col 5 : Rayonnement atmosphérique (W/m2)
"""
step, datas = getCsvDatas("meteo.csv",preview=True)

"""
L : Largeur de la chaussée en m
"""
L=4

"""
Hv Coefficient convectif entre la surface et l'air (fonction affine de la vitesse du vent)
en W/(m2K)
"""
Hv = 5.8 + 4.1*datas[:,3]
B1 = (1-albedo)*datas[:,4] + datas[:,5] + Hv*datas[:,0]

"""
épaisseurs en m
"""
hs=0.06
hd= 0.08
hb=10

# température d'injection du fluide dans le dromotherme en °C
Tinjection=10

"""
on part du principe que l'on travaille à débit fixe de 50 l/h par mètre linéaire de chaussée dans le profil en long
"""
qf=0.05/3600 # en m^3/s

"""
surface d'échange entre le fluide et une paroi solide (couche drainante)
On suppose qu'elle est 100 fois plus grande de la surface du dromotherme
"""
S=L*100
"""
coefficient d'échange convectif entre le fluide et la couche drainante en W/(m2K)
perso, je trouve que c'est beaucoup, ce n'est quant même pas de l'eau bouillante ?
"""
h=2000

"""
les valeurs initialement utilisées en dur
"""
#rds=27 # coefficient d'échange surfacique entre la couche drainante et la surface
#rdb=3.28 # coefficient d'échange surfacique entre la couche drainante et la couche de base

rds=rd(ks,kd,hs,hd)
rdb=rd(kd,kb,hd,hb)
print("coefficient d'échange surfacique surface/drainant {} W/(m2K)".format(rds))
print("coefficient d'échange surfacique drainant/base {} W/(m2K)".format(rdb))
input("press any key")

def Tf_out(Td):
    """
    calcule la température sortante du fluide pour une température de la couche drainante donnée
    varie selon la température d'injection....
    Td : scalaire ou vecteur
    """
    return Td+(Tinjection-Td)*math.exp(-h*S/(qf*Cf))

def F(t, X):
    """
    Définition du système d'équations différentielles
    i is the time
    """
    Ts=X[0]
    Td=X[1]
    Tb=X[2]
    i=int(t/step)

    Tsortie=Tf_out(Td)

    y1 = ( B1[i] - Hv[i]*Ts - epsilon*sigma*(Ts+273.15)**4 - rds*(Ts-Td) ) / ( Cs*hs )
    y2 = ( rds*(Ts-Td) - rdb*(Td-Tb) - qf*Cf/L*(Tsortie-Tinjection) ) / ( Cd*hd )
    y3 = rdb*(Td-Tb) / ( Cb * hb )

    return [y1,y2,y3]

def G(X,t):
    """
    pour odeint fonction de l'ancienne API
    """
    return F(t,X)

"""
on recrée le temps discrétisé
"""
t=step*np.arange(datas.shape[0])

"""
cf https://docs.scipy.org/doc/scipy/reference/integrate.html
"""
solution = solve_ivp(F,[0,(datas.shape[0]-1)*step],[10,10,10],t_eval=t)
oldsol = odeint(G,[10,10,10],t)

Tsortie=Tf_out(solution.y[1])

figure1 = plt.figure(figsize = (10, 5))
plt.subplot(211)
plt.xlabel('Temps (en secondes)')
plt.ylabel('Températures (en °C)')
plt.title("Profils de températures 0D")
plt.plot(solution.t,solution.y[0],label="Température couche de surface")
plt.plot(solution.t,solution.y[1],label="Température couche drainante (solve_ivp new API)")
plt.plot(solution.t,solution.y[2],label="Température couche de base")
plt.plot(solution.t,Tsortie)
plt.legend(loc="upper right")
plt.subplot(212)
plt.plot(solution.t,solution.y[0],label="Température couche de surface")
plt.plot(solution.t,oldsol[:,1],label="Température couche drainante (odeint old API)")
plt.legend(loc="upper right")
plt.show()


shoppingList = [
    ('product1', 200),
    ('product2', 300),
    ('product3', 400),
    ('product4', 500),
    ('product5', 600),
    ('product6', 700),
    ('product7', 800),
]

usingMapFunction = list(map(lambda item: item[1], shoppingList))
print(usingMapFunction)
# Achieving the same result using list comprephension
usingListComprehension = [item[1] for item in shoppingList]
print(usingListComprehension)

usingFilterFunction = list(filter(lambda item: item[1] >= 400, shoppingList))
print(usingFilterFunction)
# achieving the same result using list comprehension
filterUsingListComprehension = [
    item for item in shoppingList if item[1] >= 300]


"""
 python  main_run.py arg
 example: python main_run.py --iterations 1000 --replicas 1 --nrsteps 100000 --folderName 'Data' --algorithm 0 --diffMapMetric 'rmsd'

pythonw main_run.py --iterations 100 --replicas 5 --nrsteps 10000 --folderName 'Data' --algorithm 4 --diffMapMetric 'euclidean'

  python main_run.py --iterations 1000 --replicas 1 --nrsteps 100000 --folderName 'Data' --algorithm 0
"""

import numpy as np

import sys
import os
sys.path.append( os.getcwd()+'/srcDiffmap')

import mdtraj as md
import sampler
import integrator

from simtk import openmm, unit
from openmmtools.constants import kB

import model

#create model defined in class model

modelName = 'Alanine'
mdl=model.Model(modelName)

# IC = md.load('alanine_start_state_IC.h5')
# mdl.positions = IC.xyz * mdl.x_unit

print(mdl.x_unit)
print(mdl.modelname)
print('System has %d particle(s)' % mdl.system.getNumParticles())
#systemName='Ala2Pept'

# Parse command-line arguments
import argparse

parser = argparse.ArgumentParser(description='Diffusion map accelerated sampling.')
parser.add_argument('--algorithm', dest='algoFlag', type=str, default='0',
                    help='algorithm name or number to use (default: 0)')
parser.add_argument('--iterations', dest='niterations', type=int, default=2,
                    help='number of iterations to run (default: 2)')
parser.add_argument('--replicas', dest='nreplicas', type=int, default=2,
                    help='number of replicas to use per iteration (default: 2)')
parser.add_argument('--nrsteps', dest='nrSteps', type=int, default=1000,
                    help='number of replicas to use per iteration (default: 1000)')
#parser.add_argument('--temporaryFolder', dest='temporaryFile', type=int, default=0,
#                    help='0: save data into Data folder; 1: Save data to temporary folder; 2: another temporary folder ')
parser.add_argument('--folderName', dest='saveToFileName', type=str, default='Data',
                    help='Create folder and save data there')
parser.add_argument('--diffMapMetric', dest='diffMapMetric', type=str, default='euclidean',
                    help='Metric for diffusion maps. Choose between rmsd and euclidean.')
parser.add_argument('--diffusionMap', dest='diffusionMap', type=str, default='Diffmap',
                    help='Diffusion map: choose Diffmap for vanilla or TMDiffmap for target measure diffusion map.')

args = parser.parse_args()

saveToFileName = args.saveToFileName
diffusionMap = args.diffusionMap

#read imput parameters to choose the algorithm
algoFlag=args.algoFlag
diffMapMetric=args.diffMapMetric

if(algoFlag=='std' or algoFlag=='0'):
    iAlgo=0
    algoName = 'std'
elif(algoFlag=='eftad_fixed_cv' or algoFlag=='1'):
    iAlgo=1
    algoName = 'eftad_fixed_cv'
elif(algoFlag=='initial_condition' or algoFlag=='2'):
    iAlgo=2
    algoName ='initial_condition'
elif(algoFlag=='frontier_points_corner_change_temperature' or algoFlag=='3'):
    iAlgo=3
    algoName = 'frontier_points_corner_change_temperature'
elif(algoFlag=='frontier_points_corner_change_temperature_off' or algoFlag=='4'):
    iAlgo=4
    algoName = 'frontier_points_corner_change_temperature_off'
elif(algoFlag=='local_frontier_points_corner_change_temperature_off' or algoFlag=='5'):
    iAlgo=5
    algoName = 'local_frontier_points_corner_change_temperature_off'
elif(algoFlag=='local_frontier_points_corner_change_temperature' or algoFlag=='6'):
    iAlgo=6
    algoName = 'local_frontier_points_corner_change_temperature'
elif(algoFlag=='DiffmapABF' or algoFlag=='7'):
    iAlgo=7
    algoName = 'DiffmapABF'
elif(algoFlag=='local_frontier_points' or algoFlag=='8'):
    iAlgo=8
    algoName = 'local_frontier_points'
    print('Error: ABF not working yet. ')
else:
    print('Error: wrong algorithm flag. ')

# parameters
T=300.0
temperature =  T * unit.kelvin
kT = kB * temperature

gamma = 1.0 / unit.picosecond
dt = 1.0 * unit.femtosecond

TemperatureTAMDFactor=30.0
massScale=50.0

gammaScale=100.0
kappaScale=1000.0

if iAlgo >0:
    print("TemperatureTAMDFactor = " + repr(TemperatureTAMDFactor))
temperatureAlpha= (T*TemperatureTAMDFactor)* unit.kelvin

print('Gamma is '+repr(gamma))
print('Temperature is '+repr(temperature))
if iAlgo >0:
    print('Temperature TAMD '+repr(TemperatureTAMDFactor)+'xTemperature')
    print('Mass alpha is '+repr(massScale)+'x Mass')

#create folders to save the data

dataFileName=saveToFileName+'/'+str(modelName)+'/'+str(algoName)+'/Traj/'
dataFileNameFrontierPoints=saveToFileName+'/'+str(modelName)+'/'+str(algoName)+'/Traj/FrontierPoints/'
dataFileNameEigenVectors=saveToFileName+'/'+str(modelName)+'/'+str(algoName)+'/Traj/Eigenvectors/'
dataFileEnergy=saveToFileName+'/'+str(modelName)+'/'+str(algoName)+'/Traj/Energies/'

newpath = os.path.join(os.getcwd(),dataFileName)
if not os.path.exists(newpath):
        os.makedirs(newpath)

newpath = os.path.join(os.getcwd(),dataFileNameFrontierPoints)
if not os.path.exists(newpath):
        os.makedirs(newpath)

newpath = os.path.join(os.getcwd(),dataFileNameEigenVectors)
if not os.path.exists(newpath):
        os.makedirs(newpath)

newpath = os.path.join(os.getcwd(),dataFileEnergy)
if not os.path.exists(newpath):
        os.makedirs(newpath)

# simulation class sampler takes integrator class with chosen parameters as input
integrator=integrator.Integrator( model=mdl, gamma=gamma, temperature=temperature, temperatureAlpha=temperatureAlpha, dt=dt, massScale=massScale, gammaScale=gammaScale, kappaScale=kappaScale)

## load initial condition from file
# IC = md.load('alanine_start_state_IC.h5')
# # remove first dimension - the intial condition has shape (1,nrParticles, spaceDimension) when taken from trajectory
# InitialPosition = np.squeeze(IC.xyz)
# integrator.x0 = InitialPosition * mdl.x_unit

general_sampler=sampler.Sampler(model=mdl, integrator=integrator, algorithm=iAlgo, diffusionMapMetric=diffMapMetric, dataFileName=dataFileName, dataFrontierPointsName = dataFileNameFrontierPoints, dataEigenVectorsName =dataFileNameEigenVectors, dataEnergyName = dataFileEnergy, diffusionMap=diffusionMap)

# nrSteps is number of steps for each nrRep , and iterate the algo nrIterations times - total simulation time is nrSteps x nrIterations
nrSteps=args.nrSteps
nrEquilSteps = 0 #10000
nrIterations=args.niterations
nrRep=args.nreplicas

print('Simulation time: '+repr(nrSteps*nrIterations*dt.value_in_unit(unit.femtosecond))+' '+str(unit.femtosecond)+'\n ***** \n' )
#
if (nrEquilSteps>0):
    print('Equilibration: '+repr(nrEquilSteps*dt.value_in_unit(unit.picosecond))+' '+str(unit.picosecond)+'\n ***** \n' )
    general_sampler.run_std(nrEquilSteps, 1, 1)
    general_sampler.resetInitialConditions()
# run the simulation
print('\n ****\n Starting simulation\n')
general_sampler.run(nrSteps, nrIterations, nrRep)


# 1.
def AMCount():
    ct=ct_m=0
    with open("PROGRAMS/poem1.txt") as f:
        for i in f.read():
            if(i=="A" or i=="a"):
                ct+=1
            elif(i=="M" or i=="m"):
                ct_m+=1
    return ct,ct_m
print("Count of A ora and M or m is: ",AMCount(),respectively)


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

# In[1]:

# Hanna Born
# ANLY 590 Fall 2021
# HW6.0

# references:
# code on course github at https://github.com/jh2343/590-CODES/
# https://blog.keras.io/building-autoencoders-in-keras.html

# In[2]:

# import packages
import sys

import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from sklearn.preprocessing import MinMaxScaler

import keras
from keras import models
from keras import layers
from keras import Model

from keras.datasets import mnist, fashion_mnist #, cifar10

# In[3]:

# REPEAT 6.1 but instead using a convolutional auto-encoder
# write output to log file
old_stdout = sys.stdout
log_file = open("HW6.1_log_file.log","w")
sys.stdout = log_file

# ----------------------------
# USER PARAMETERS
# for MNIST data
# ----------------------------
N_channels=1
PIX=28
EPOCHS          =   10          # 35
NKEEP           =   2500        # DOWNSIZE DATASET
# NKEEP           =   60000       # FULL SIZE DATASET
BATCH_SIZE      =   128

# ----------------------------
# GET MNIST DATASET
# ----------------------------
(X, Y), (test_images, test_labels) = mnist.load_data()
# (x_train, _), (x_test, _) = mnist.load_data()

#NORMALIZE AND RESHAPE
X = X.astype('float32') / 255.
test_images = test_images.astype('float32') / 255.

# ----------------------------
# GET MNIST-FASHION DATA
# ----------------------------
from keras.datasets import fashion_mnist

(X_fashion, Y_fashion), (test_images_fashion, test_labels_fashion) = fashion_mnist.load_data()

# NORMALIZE AND RESHAPE
X_fashion=X_fashion.astype('float32') / 255.
test_images_fashion=test_images_fashion.astype('float32') / 255.

# ----------------------------
# MODEL
# BUILD CNN-AE MODEL
# ----------------------------
input_img = keras.Input(shape=(PIX, PIX, N_channels))

# ENCODER --------------------
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
# AT THIS POINT THE REPRESENTATION IS (4, 4, 8) I.E. 128-DIMENSIONAL

# DECODER --------------------
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, (3, 3), activation='relu')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(N_channels, (3, 3), activation='sigmoid', padding='same')(x)

# ----------------------------
# COMPILE
# ----------------------------
model = keras.Model(input_img, decoded)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']);
model.summary()

# ----------------------------
# TRAIN
# and track training history
# ----------------------------
history = model.fit(X, X,
                epochs=EPOCHS,
                batch_size=BATCH_SIZE,
                shuffle=True,
                validation_data=(test_images, test_images))

# # # save the trained model for future use
# model.save('HW6.1_trained_model_2500.h5')

# # load and evaluate the model on the test set
# model = tf.keras.models.load_model('HW6.1_trained_model_2500.h5')
# model.evaluate(test_images, test_images)

# ----------------------------
# HISTORY PLOT
# ----------------------------
epochs = range(1, len(history.history['loss']) + 1)
plt.figure()
plt.plot(epochs, history.history['loss'], 'bo', label='Training loss')
plt.plot(epochs, history.history['val_loss'], 'b', label='Validation loss')
plt.legend()

# -------------------------------
# MAKE PREDICTIONS FOR TEST DATA
# -------------------------------
decoded_imgs = model.predict(test_images)
decoded_imgs_fashion = model.predict(test_images_fashion)

# #COMPILE AND FIT
# model.compile(optimizer='rmsprop',
#               loss='mean_squared_error',
#               metrics=['accuracy'])
# model.summary()
# model.fit(X, X, epochs=10, batch_size=1000,validation_split=0.2)

# --------------------------------
# ORIGINAL AND RECONSTRUCTED
# --------------------------------
X1=model.predict(X)
X1_fashion=model.predict(X_fashion)

# ----------------------------
# DEFINE ERROR THRESHOLD
# ----------------------------
# define error threshold for anomaly detection
# if(err > threshold) --> anomaly
threshold = 4*model.evaluate(X,X, batch_size=X.shape[0])
# print('threshold', threshold)

#-------------------------------------
# EVALUATE ON TEST DATA (MNIST)
# and
# EVALUATE ON TEST DATA (MNIST-FASHION)
#-------------------------------------
train_loss, train_acc = model.evaluate(X, X, batch_size=X.shape[0])
test_loss, test_acc = model.evaluate(test_images, test_images, batch_size=test_images.shape[0])
test_loss_fashion, test_acc_fashion = model.evaluate(test_images_fashion,
                                                     test_images_fashion,
                                                     batch_size=test_images_fashion.shape[0])

print('\nthreshold: \t\t\t', threshold)

print('\ntrain_loss: \t\t\t', train_loss)
print('test_loss (MNIST): \t\t', test_loss)
print('test_loss (MNIST-FASHION): \t', test_loss_fashion)
print('\ntrain_acc: \t\t\t', train_acc)
print('test_acc (MNIST): \t\t', test_acc)
print('test_acc (MNIST-FASHION): \t', test_acc_fashion)

# -------------------------------------------
# COMPARE ORIGINAL AND RECONSTRUCTED IMAGES
# -------------------------------------------
fig, axs = plt.subplots(1,6)
axs[0].imshow(test_images[11].reshape(PIX, PIX))
axs[1].imshow(decoded_imgs[11].reshape(PIX, PIX))
axs[2].imshow(decoded_imgs_fashion[11].reshape(PIX, PIX))
axs[3].imshow(test_images[46].reshape(PIX, PIX))
axs[4].imshow(decoded_imgs[46].reshape(PIX, PIX))
axs[5].imshow(decoded_imgs_fashion[46].reshape(PIX, PIX))

# ---------------------------
# ANOMALY DETECTION
# ---------------------------
# reference: https://www.analyticsvidhya.com/blog/2021/05/anomaly-detection-using-autoencoders-a-walk-through-in-python/

def anomaly_predictions(model, test_images, threshold):
    predictions = model.predict(test_images)
    errors = tf.keras.losses.mean_squared_logarithmic_error(predictions,
                                  test_images)
    errors = tf.math.reduce_sum(errors, axis=1, keepdims=False, name=None)
    errors = tf.math.reduce_sum(errors, axis=1, keepdims=False, name=None)/28
    print(errors)
    anomaly_mask = pd.Series(errors > np.mean(threshold))
    preds = anomaly_mask.map(lambda x: 1.0 if x == True else 0.0) # 0 normal, 1 anomaly
    percent_anomalies = sum(preds)/len(preds) * 100
    # return preds # to get predictions
    return print(f'-- {sum(preds):.0f} anomalies / {len(preds)} total \n-- % anomalies:\t {percent_anomalies:.2f}%')

# threshold
print(f"threshold: \t {np.mean(threshold):2f}")

# get anomaly predictions for MNIST (model trained on MNIST)
print("\nanomaly predictions for (MNIST trained on MNIST):")
anomaly_predictions(model, decoded_imgs, threshold)

# get anomaly predictions for MNIST-FASHION (model trained on MNIST)
print("\nanomaly predictions for (MNIST-FASHION trained on MNIST):")
anomaly_predictions(model, decoded_imgs_fashion, threshold)

# finish wirting output to log file and close
sys.stdout = old_stdout
log_file.close()

# In[ ]:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
try:
    import simplejson as json
except ImportError:
    import json
from conf import pn_problem_collect_conf
from src.lib import db_mysql
from src.lib import zabbix_api
from conf import cannon_conf
#mysql_conn = db_mysql.MyPymysqlPool()
zabbix = zabbix_api.ZabbixApi()
hostip_list = pn_problem_collect_conf.hostip_list
pn_node_list = pn_problem_collect_conf.pn_node_list
trigger_delay = pn_problem_collect_conf.trigger_delay
trigger_block = pn_problem_collect_conf.trigger_block
trigger_telnet = pn_problem_collect_conf.trigger_telnet
template_name = pn_problem_collect_conf.template_name

pn_event_table='pn_event'
pn_event_source_data_table='pn_event_source_data'
pn_event_history_table='pn_event_history'

task_log_file = cannon_conf.task_manage_log
#start = time.strptime('2020-05-11 16:56:15', "%Y-%m-%d %H:%M:%S")
#stop = time.strptime('2020-05-17 21:12:15', "%Y-%m-%d %H:%M:%S")
#start = int(time.mktime(start))
#stop = int(time.mktime(stop))
eventid_list = []

def get_itemid(triggerid):
    status = False
    result = zabbix.get_item(triggerid)
    # print('result',triggerid,result)

    if not result:
        return status
    try:
        item_info = result['result']
        # print('item_info',item_list)
    except:
        return status
    if not item_info:
        return status
    itemid = item_info[0]['itemid']
    return itemid

def get_templateid(template_name):
    status = False
    result = zabbix.get_template(template_name)
    template_info = []
    if not result:
        return template_info, status
    try:
        template_info = result['result']
    except:
        return template_info, status
    #print(template_info[0]['templateid'])
    return template_info[0]['templateid']

def get_eventids(triggerid_info):
    eventid_info = []
    for key in triggerid_info:
        event_info = zabbix.get_event(triggerid_info[key], start, stop)
        # print('event_info',event_info)
        if not event_info:
            return event_info, 'event is null'
        try:
            event_info = event_info['result']
        except:
            return event_info
        if event_info:
            # print('event_info',event_info)
            for event in event_info:
                # print(event['name'],event['eventid'])
                eventid_info.append(event['eventid'])
    print('eventid_info', eventid_info)
    return eventid_info

def get_porblem_info(eventids):
    problem_info = []
    for eventid_d in eventids:
        problem_data = zabbix.get_problem(eventid_d)
        if not problem_data:
            return problem_data,  'problem is null'
        try:
            problem_data = problem_data['result']
            # print('problem_data',problem_data)
        except:
            return problem_data
        if problem_data:
            problem_info.append(problem_data)
    return problem_info

def insert_event_source_data(problem_info,triggerid_info,event_type,hostname):
    mysql_conn = db_mysql.MyPymysqlPool('mysql')
    for pi in problem_info:
        select_eventid_sql = "select * from %s where pn_eventid = '%s';" % (pn_event_source_data_table, pi[0]['eventid'])
        result = mysql_conn.select(select_eventid_sql)  # 根据传入的eventid值来匹配数据
        #print('pi',pi,result)
        if not result:
            #print('mei you chong fu shu ju le')
            node_name = pi[0]['name'].split(' ', )[0]
            if node_name == hostname or pi[0]['r_clock'] == '0':
                #print('test shi fou xiang deng')
                break

            for key in triggerid_info:
                #print('triggerid_info',triggerid_info,pi[0]['name'])
                if key == pi[0]['name']:
                    triggerid = triggerid_info[key]
                    itemid01 = get_itemid(triggerid)
                    #print('itemid01',itemid01)
                    sql_insert_problem_info = "insert into %s(pn_eventid,clock,r_clock,pn_node,type,itemid,source) " \
                                      "values('%s','%s','%s','%s','%s','%s','%s')" % (
                                          pn_event_source_data_table, pi[0]['eventid'], pi[0]['clock'], pi[0]['r_clock'],
                                          node_name, event_type, itemid01, hostname)
                    mysql_conn.insert(sql_insert_problem_info)
    mysql_conn.dispose()

def get_pn_event_source_data(task_id=None):
    global start
    global stop
    stop = time.time()  # 当前时间的时间戳
    print('当前时间',time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
    start = stop - 168800  # 当前时间3小时前的时间戳
    status = False
    if pn_node_list.__len__() != 12:
        return False

    for hostip in hostip_list:
        hostname = zabbix.get_hostname_with_hostip(hostip)
        print('hostname',hostname)
        triggers_get = zabbix.get_pn_trigger(hostip)
        if not triggers_get:
            return triggers_get, status, 'trigger is null'
        try:
            triggers_get = triggers_get['result']
            print('triggers_get',triggers_get)
        except:
            print("get trigger is  error")
            return False,
        #print('triggers_info',triggers_info)
        #triggerid_delay = {}
        triggerid_block = {}
        triggerid_telnet = {}
        for i in triggers_get:
            #for d in trigger_delay:
                #if d == i['description']:
                    #triggerid_delay[d] = i['triggerid']
            for b in trigger_block:
                if b == i['description']:
                    triggerid_block[b] = i['triggerid']
            for t in trigger_telnet:
                if t == i['description']:
                    triggerid_telnet[t] = i['triggerid']

        #eventid_delay = get_eventids(triggerid_delay)
        eventid_block = get_eventids(triggerid_block)
        eventid_telnet = get_eventids(triggerid_telnet)
        #print('triggerid_block',triggerid_block)
        #print('triggerid_telnet',triggerid_telnet)

        #problem_delay_info = get_porblem_info(eventid_delay)
        problem_block_info = get_porblem_info(eventid_block)
        problem_telnet_info = get_porblem_info(eventid_telnet)
        #print('problem_delay_info',problem_delay_info)
        #print('problem_block_info',problem_block_info)
        #print('problem_telnet_info',problem_telnet_info)

        #insert_event_source_data(problem_delay_info,triggerid_delay,1,hostname)
        insert_event_source_data(problem_block_info,triggerid_block,0,hostname)
        insert_event_source_data(problem_telnet_info,triggerid_telnet,2,hostname)

    return True

if __name__ == "__main__":
    get_pn_event_source_data()


import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import cv2
import numpy as np
import glob
import re

# 'percentage_binary_thresholding.ipynb'
def percentages_to_thresholds(img_gray, lower_percentage, upper_percentage):
    norm_hist = (np.cumsum(np.histogram(img_gray, 255, (0.0, 255.0))[0]) / \
                 img_gray.size).astype(np.float32)
    values = np.arange(0, 255)[((norm_hist >= lower_percentage) & \
                                (norm_hist <= upper_percentage))]

    return np.min(values), np.max(values)

# 'gradient_experiment.ipynb'
def get_binary_gradient(img, low_thresh=0.95, high_thresh=1.0):
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2HLS_FULL)[:,:,1]

    gradient_x = np.absolute(cv2.Sobel(img_gray, cv2.CV_64F, 1, 0))
    gradient_x = 255 * gradient_x / np.max(gradient_x)

    binary = np.zeros_like(img_gray).astype(np.bool8)
    low_thresh, high_thresh = percentages_to_thresholds(gradient_x, \
        low_thresh, high_thresh)

    binary[(gradient_x >= low_thresh) & (gradient_x <= high_thresh)] = True
    return binary

# 'color_scheme_experiment.ipynb'
def Sally(img):
    S = cv2.cvtColor(img, cv2.COLOR_RGB2HLS_FULL)[:,:,2]
    A = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)[:,:,1]

    return np.sqrt( \
                S.astype(np.uint32) * \
                A.astype(np.uint32) \
                  ).astype(np.uint8)

# 'color_scheme_experiment.ipynb'
def get_binary_color(img, low_thresh = 0.9, high_thresh = 1.0):
    img_gray = Sally(img)

    low_thresh, high_thresh = percentages_to_thresholds(img_gray, \
        low_thresh, high_thresh)

    img_binary = np.zeros_like(img_gray).astype(np.bool8)
    img_binary[(img_gray > low_thresh) & (img_gray < high_thresh)] = True

    return img_binary

from moviepy.editor import VideoFileClip

temp = None
def process_image(img):
    binary_R = 255 * np.zeros(img.shape[:2]).astype(np.uint8)
    binary_G = 255 * get_binary_color(img).astype(np.uint8)
    binary_B = 255 * get_binary_gradient(img).astype(np.uint8)
    binary = np.dstack((binary_R, binary_G, binary_B))

    global temp
    if temp is None:
        temp = np.copy(binary)

    return binary

# The video processing and storing code
input_video = VideoFileClip("white.mp4")
output_video = input_video.fl_image(process_image)

output_filename = 'project_output_1.mp4'
output_video.write_videofile(output_filename, audio=False)


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

from tqdm import tqdm
import pickle

UNK, PAD = '<UNK>', '<PAD>'  # unknown word, padding character

def _build_vocab(file_path, tokenizer, max_vocab_size, min_word_freq, line_sep):
    vocab_dic = {}
    bad_line = 0
    with open(file_path, 'r', encoding='UTF-8') as f:
        for line in tqdm(f):
            lin = line.strip()
            if not lin:
                bad_line += 1
                continue

            seps = lin.split(line_sep)
            if 1 <= len(seps) <= 2:
                content = seps[0]  # default first part separated is content, second is label
            else:
                bad_line += 1
                continue

            for word in tokenizer(content):
                vocab_dic[word] = vocab_dic.get(word, 0) + 1
        vocab_list = sorted([_ for _ in vocab_dic.items() if _[1] >= min_word_freq],
                            key=lambda x: x[1], reverse=True)[:max_vocab_size]
        vocab_dic = {word_count[0]: idx for idx, word_count in enumerate(vocab_list)}
        vocab_dic.update({UNK: len(vocab_dic), PAD: len(vocab_dic) + 1})
    print(f"bad line number is {bad_line}")
    return vocab_dic

def _line_to_number(content, tokenizer, vocab_dic, pad_size):
    token = tokenizer(content)
    seq_len = len(token)
    if seq_len < pad_size:
        token.extend([vocab_dic.get(PAD)]*(pad_size - seq_len))
    else:
        token = token[: pad_size]
        seq_len = pad_size

    words_line = []
    for word in token:
        words_line.append(vocab_dic.get(word, vocab_dic.get(UNK)))
    return words_line, seq_len

def format_content(file_path, train_granularity, build_vocab, vocab_save_path,
                   max_vocab_size, min_word_freq, pad_size, line_sep, if_with_label=True, word_sep=" "):
    if train_granularity == "word":
        tokenizer = lambda x: x.split(word_sep)
    elif train_granularity == "char":
        tokenizer = lambda x: [y for y in x]
    else:
        raise ValueError("param train_granularity must be in ('word', 'char')!")

    if build_vocab:
        vocab_dic = _build_vocab(file_path, tokenizer, max_vocab_size, min_word_freq, line_sep)
        pickle.dump(vocab_dic, open(vocab_save_path, 'wb'))
        print(f"create vocabulary from {file_path}, vocabulary size {len(vocab_dic)}")
    else:
        vocab_dic = pickle.load(open(vocab_save_path, 'rb'))
        print(f"load vocabulary from {vocab_save_path}, vocabulary size {len(vocab_dic)}")

    contents = []
    bad_line = 0
    with open(file_path, 'r', encoding='UTF-8') as f:
        for line in tqdm(f):
            lin = line.strip()
            if not lin:
                continue
            seps = lin.split(line_sep)
            if if_with_label:
                if len(seps) == 2:
                    content, label = seps
                    words_line, seq_len = _line_to_number(content, tokenizer, vocab_dic, pad_size)
                    contents.append((words_line, seq_len, int(label)))
                else:
                    bad_line += 1
                    continue
            else:
                if len(seps) == 1:
                    content = seps
                    words_line, seq_len = _line_to_number(content, tokenizer, vocab_dic, pad_size)
                    contents.append((words_line, seq_len, None))
                else:
                    bad_line += 1
                    continue
    print(f"bad line number is {bad_line}")
    return contents, len(vocab_dic)


# encoding: utf-8
#

import sklearn.datasets
import numpy as np
from action.linear.perceptron import Perceptron
from action.linear.adalineGD import AdalineGD
import pandas as pd

iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target
df1 = pd.DataFrame(X)
df2 = pd.DataFrame(y)
df = pd.concat([df1, df2], axis=1)
size = X.shape[0]
X_train, y_train = df.iloc[:100, [0,2]].values, df.iloc[:100, 4].values
y_train = np.where(y_train == 0, -1, 1)

def test_perceptron(X_train, y_train):
    p = Perceptron()
    p.fit(X_train, y_train)
    # print("train ", X_train.shape, "test ", X_test.shape)
    print(p.error_)

test_perceptron(X_train, y_train)

def test_adaline(X_train, y_train):
    ad = AdalineGD(eta=0.01)
    X_train = (X_train - X_train.mean())/X_train.std()
    ad.fit(X_train, y_train)
    print(ad.cost_)

# test_adaline(X_train, y_train)


#!/usr/bin/env python3
#해쉬뱅을 작성함으로써 ROS에서 사용할 Python의 버전을 설정한다.

import rospy
from turtlesim.msg import Pose
from geometry_msgs.msg import Twist
from random import *

#사용할 모듈을 입력한다.
class Loop_test():
    def __init__(self):
        rospy.init_node("wego_node") # 1. 노드 작성
        self.pub = rospy.Publisher("/turtle1/cmd_vel", Twist, queue_size=1) #2. 노드의 역할을 설정
        rospy.Subscriber("/turtle1/pose", Pose, self.callback) #2. 노드의 역할을 설정
        self.pub_data = Twist()
        self.sub_data = Pose()

    def callback(self, sub_data):
        if 1< sub_data.x <9 and 1<sub_data.y<9:
            self.pub_data.linear.x = random()*2
            self.pub_data.angular.z = random() *4 - 2
        else:
            self.pub_data.linear.x = 0.25
            self.pub_data.angular.z = 0.5
        print(f"sub_data:\n{sub_data}")
        print("-----------------------")
        self.pub.publish(self.pub_data) # 3. 토픽을 보내는 송신 시점 설정
        print(f"pub_data:\n{self.pub_data}")

def main():
    loop_test = Loop_test()
    rospy.spin()

if __name__ == "__main__":
    main()


import pyautogui
import pytesseract
from PIL import Image
import time

time.sleep(3)

# It's not advised to use while loop
# There is a built-in mechanism in pyautogui to terminate program if we
# move our cursor to top left. This will cause program to break

while True:
    # region may vary for different screen
    im = pyautogui.screenshot(region=(232, 279, 835, 106))
    im.save('data.png')

    # To click text area and start typing
    pyautogui.click(x=464, y=425)

    # To get text from given image
    value = pytesseract.image_to_string(Image.open('data.png'))

    # To replace new line from it and make one clean list
    value = value.replace('\n\n',' ')
    b = value.split(' ')

    print(b)

    # Backspace is used to avoid any error
    for i in b:
        pyautogui.press('backspace')
        pyautogui.typewrite(i)
        pyautogui.press('space')
    if len(b) <= 4:
        break


import sys
import os

from PGM import Network

def main(argv):
    filename = argv[1]
    name, ext = os.path.splitext(filename)
    evid_file = filename + '.evid'
    map_file = filename + '.map'

    net = Network(kind='BN', query='MAP')
    net.read(filename)
    net.write_evidence(evid_file, 'single_line')
    net.write_map_query(map_file, 'single_line')

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


from tkinter import *

root = Tk()

my_button = Button(root, text="Click Me", padx=10, pady=10, state=DISABLED)
# padx is used to change the size of the button in x-axis and pady in y-axis.
# DISABLED state is used to make the button unusable wheres ACTIVE state makes it usable.

my_button.pack()

root.mainloop()


# Nameof project= Merak
# made by Sahar Nikkhah
#This is to find the time lag of Merak light to reach the telescopes relative to each other/and this code use the formula in MerakTimeLag note.
from subprocess import call
from astropy.coordinates import EarthLocation
from astropy.time import Time
from astropy.coordinates import AltAz
from astropy.coordinates import SkyCoord
from datetime import datetime, timedelta
import math
#===============finding time lag formula is from slides==========
def Time_lag(T1X,T1Y,T1H,T2X,T2Y,T2H,az,alt):
    theta12=math.atan((T1X-T2X)/(T1Y-T2Y))
    beta12=math.acos(math.sqrt((math.pow((T1X-T2X),2))+(math.pow((T1Y-T2Y),2)))/math.sqrt((math.pow((T1X-T2X),2))+(math.pow((T1Y-T2Y),2))+(math.pow((T1H-T2H),2))))
    alpha12=math.acos(math.cos(az-theta12)*math.cos(alt))
    delta12=math.sqrt((math.pow((T1X-T2X),2))+(math.pow((T1Y-T2Y),2))+(math.pow((T1H-T2H),2)))
    t= delta12*math.cos(alpha12+beta12)/3e8
    return t

#==================some infos====================
observing_time = Time('2021-03-31 3:00:00')# observing starts
observing_time2= Time('2021-03-31 3:00:07')#observing ends
Merak= SkyCoord.from_name('Merak')#finding Merak in sky
Veritas= EarthLocation(lat='31.675', lon='-110.952')#loaction of orgin in slides

#============telescopes locations(slides)========
T1X=135.48
T1Y=-8.61
T1H= 12.23
T2X=44.1
T2Y=-47.7
T2H=4.4
T3X=29.4
T3Y=60.1
T3H=9.8
T4X=-35.9
T4Y=11.3
T4H= 7.0

 #======start of loop over time============
while observing_time < observing_time2:
    #taking star data for the time
    aa = AltAz(location=Veritas, obstime=observing_time)
    altdeg=Merak.transform_to(aa).alt.deg
    azdeg=Merak.transform_to(aa).az.deg
    alt= math.radians(altdeg)
    az=math.radians(azdeg)

    #time lag

    print('=================',observing_time,'===================')
    t12=Time_lag(T1X,T1Y,T1H,T2X,T2Y,T2H,az,alt)
    t13=Time_lag(T1X,T1Y,T1H,T3X,T3Y,T3H,az,alt)
    t14=Time_lag(T1X,T1Y,T1H,T4X,T4Y,T4H,az,alt)
    t23=Time_lag(T2X,T2Y,T2H,T3X,T3Y,T3H,az,alt)
    t24=Time_lag(T2X,T2Y,T2H,T4X,T4Y,T4H,az,alt)
    t34=Time_lag(T3X,T3Y,T3H,T4X,T4Y,T4H,az,alt)

    print('t12=',t12,'\n','t13=',t13, '\n','t14=',t14, '\n','t24=',t24, '\n','t23=',t23, '\n','t34=',t34 )

    observing_time += timedelta(0,1)


import download_images
import requests

def fetch_spacex_photo_latest_launch(
    spacex_url='https://api.spacexdata.com/v3/launches/latest'
):
    response = requests.get(spacex_url)
    response.raise_for_status()
    response_json = response.json()
    image_urls = [
        {
            'url':image_url,
            'name': '{name}_{index}'.format(name=response_json['mission_name'],
                                            index=index)
        }
        for index, image_url in enumerate(
            response_json['links']['flickr_images'], 1)
    ]
    return image_urls

def main():
    try:
        image_urls = fetch_spacex_photo_latest_launch()
        download_images.download_images_by_urls_and_names(image_urls)
    except requests.exceptions.HTTPError as error:
        exit("Can't get data from server:\n{0}".format(error))

if __name__ == '__main__':
    main()


# # Implementing CLSM

# ## Purpose
# The purpose of this notebook is to implement Microsoft's [Convolutional Latent Semantic Model](http://www.iro.umontreal.ca/~lisa/pointeurs/ir0895-he-2.pdf) on our dataset.
#
# ## Inputs
# - This notebook requires *wiki-pages* from the FEVER dataset as an input.

# ## Preprocessing Data

import pickle
from multiprocessing import cpu_count
import os

import joblib
import nltk
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from joblib import Parallel, delayed
from logger import Logger
from hyperdash import Experiment, monitor
from scipy import sparse
from sys import argv
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.metrics import classification_report, accuracy_score
from torch.autograd import Variable
from torch.utils.data import DataLoader
from tqdm import tqdm, tqdm_notebook

import cdssm
import pytorch_data_loader
import argparse
import utils

torch.backends.cudnn.benchmark=True
nltk.data.path.append('/usr/users/mnadeem/nltk_data/')

def parse_args():
    parser = argparse.ArgumentParser(description='Learning the optimal convolution for network.')
    parser.add_argument("--batch-size", type=int, help="Number of queries per batch.", default=1)
    parser.add_argument("--data-sampling", type=int, help="Number of examples per query.", default=8)
    parser.add_argument("--learning-rate", type=float, help="Learning rate for model.", default=1e-3)
    parser.add_argument("--epochs", type=int, help="Number of epochs to learn for.", default=3)
    parser.add_argument("--data", help="Training dataset to load file from.", default="shared_task_dev.pkl")
    parser.add_argument("--model", help="Model to evaluate.")
    parser.add_argument("--sparse-evidences", default=False, action="store_true")
    return parser.parse_args()

@monitor("CLSM Test")
def run():
    BATCH_SIZE = args.batch_size
    LEARNING_RATE = args.learning_rate
    DATA_SAMPLING = args.data_sampling
    NUM_EPOCHS = args.epochs
    MODEL = args.model

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    logger = Logger('./logs/{}'.format(time.localtime()))

    print("Created model...")
    model = cdssm.CDSSM()
    model = model.cuda()
    model = model.to(device)
    if torch.cuda.device_count() > 0:
      print("Let's use", torch.cuda.device_count(), "GPU(s)!")
      model = nn.DataParallel(model)
    model.load_state_dict(torch.load(MODEL))

    print("Created dataset...")
    dataset = pytorch_data_loader.WikiDataset(test, claims_dict, data_sampling=DATA_SAMPLING, testFile="shared_task_dev.jsonl")
    dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, num_workers=3, shuffle=True, collate_fn=pytorch_data_loader.PadCollate())

    OUTPUT_FREQ = int((len(dataset)/BATCH_SIZE)*0.02)
    criterion = torch.nn.BCEWithLogitsLoss()

    parameters = {"batch size": BATCH_SIZE, "loss": criterion.__class__.__name__, "data batch size": DATA_SAMPLING, "data": args.data}
    exp_params = {}
    exp = Experiment("CLSM V2")
    for key, value in parameters.items():
       exp_params[key] = exp.param(key, value)

    true = []
    pred = []
    print("Evaluating...")
    model.eval()
    test_running_accuracy = 0.0
    test_running_loss = 0.0
    num_batches = 0
    for batch_num, inputs in enumerate(dataloader):
        num_batches += 1
        claims_tensors, claims_text, evidences_tensors, evidences_text, labels = inputs

        y_pred = model(claims, evidences)

        y = (labels).float()
        y_pred = y_pred.squeeze()
        y = y.squeeze()
        y = y.view(-1)
        y_pred = y_pred.view(-1)
        bin_acc = torch.sigmoid(y_pred).round()

        loss = criterion(y_pred, y)

        true.extend(y.tolist())
        pred.extend(bin_acc.tolist())

        accuracy = (y==bin_acc).float().mean()
        test_running_accuracy += accuracy.item()
        test_running_loss += loss.item()

        if batch_num % OUTPUT_FREQ==0 and batch_num>0:
            print("[{}]: {}".format(batch_num, test_running_accuracy / OUTPUT_FREQ))

            # 1. Log scalar values (scalar summary)
            info = { 'test_loss': test_running_loss/OUTPUT_FREQ, 'test_accuracy': test_running_accuracy/OUTPUT_FREQ }

            for tag, value in info.items():
                exp.metric(tag, value, log=False)
                # logger.scalar_summary(tag, value, batch_num+1)

            # 2. Log values and gradients of the parameters (histogram summary)
            for tag, value in model.named_parameters():
                tag = tag.replace('.', '/')
                # logger.histo_summary(tag, value.data.cpu().numpy(), batch_num+1)

            test_running_loss = 0.0
            test_running_accuracy = 0.0

    print(true[0], pred[0])
    true = np.array(true).astype("int")
    pred = np.array(pred).astype("int")
    final_accuracy = accuracy_score(true, pred)
    print("Final accuracy: {}".format(final_accuracy))
    print(classification_report(true, pred))
    filename = "predicted_labels"
    for key, value in parameters.items():
        filename += "_{}-{}".format(key.replace(" ", "_"), value)

    joblib.dump({"true": true, "pred": pred}, filename)

if __name__=="__main__":
    args = parse_args()

    print("Loading {}".format(args.data))
    test = joblib.load(args.data)

    try:
        claims_dict
    except:
        print("Loading validation claims data...")
        claims_dict = joblib.load("claims_dict.pkl")

    torch.multiprocessing.set_start_method("spawn", force=True)
    run()


import random
import psychopy
from math import log2
import numpy as np
import os
import csv
from psychopy.hardware import keyboard
from psychopy import visual, event, core, gui, data
from pyglet.window import key
import datetime
import sys
import copy
import math
from math import ceil
import stat

class my_button():
    """生成按钮"""

    def __init__(self, length, height, text, pos=[0, 0], textColor=[0, 0, 0],
                 textSize=20, fillColor=None):
        self.text = generate_textMark(
            pos[0], pos[1], text, textSize, textColor)
        self.button = generate_button(
            length, height, pos, fillColor)

    def draw(self):
        self.button.draw()
        self.text.draw()

def generate_textMark(x, y, text, size=40,  color=[0, 0, 0]):
    """生成标识数字"""
    textMark = psychopy.visual.TextStim(
        win=win,
        text=text,
        color=color,
        colorSpace='rgb255',
        pos=(x, y),
        height=size,
        units="pix"
    )
    return textMark

def generate_button(length, height, pos=[0, 0], fillColor=[255, 255, 255]):
    acceptBoxleft = -length/2
    acceptBoxright = length/2
    acceptBoxtop = height/2
    acceptBoxbot = -height/2

    delta = 0.025 * 400
    delta2 = delta/7
    acceptBoxVertices = [
        [acceptBoxleft, acceptBoxtop - delta],
        [acceptBoxleft + delta2, acceptBoxtop - 3 * delta2],
        [acceptBoxleft + 3 * delta2, acceptBoxtop - delta2],
        [acceptBoxleft + delta, acceptBoxtop],
        [acceptBoxright - delta, acceptBoxtop],
        [acceptBoxright - 3 * delta2, acceptBoxtop - delta2],
        [acceptBoxright - delta2, acceptBoxtop - 3 * delta2],
        [acceptBoxright, acceptBoxtop - delta],
        [acceptBoxright, acceptBoxbot + delta],
        [acceptBoxright - delta2, acceptBoxbot + 3 * delta2],
        [acceptBoxright - 3 * delta2, acceptBoxbot + delta2],
        [acceptBoxright - delta, acceptBoxbot],
        [acceptBoxleft + delta, acceptBoxbot],
        [acceptBoxleft + 3 * delta2, acceptBoxbot + delta2],
        [acceptBoxleft + delta2, acceptBoxbot + 3 * delta2],
        [acceptBoxleft, acceptBoxbot + delta]]

    button = psychopy.visual.ShapeStim(
        win=win, vertices=acceptBoxVertices,
        fillColor=fillColor, lineColor=fillColor,
        fillColorSpace="rgb255", lineColorSpace="rgb255",
        pos=pos,
        autoLog=False)

    return button

def trials(difficult):
    information = ['', '', '', '', '']
    mouse = psychopy.event.Mouse()
    # 问题列表
    questionlist1 = ['学校今天留了\n多少道题目？', '佩奇上午做了\n多少道数学题？',
                     '佩奇晚上做了\n多少道数学题？', '佩奇有几道数\n学题没做完?']
    questionlist2 = ['学校今天留了\n多少道题目？', '佩奇在学校做\n了多少道数学题？',
                     '佩奇在家里做了\n多少道数学题？', '佩奇有几道数学\n题没做完?']
    # 随机选择提什么问题
    Qnumbool = random.choice([True, False])
    Qnum = random.randint(0, 3)
    if difficult > 1 and difficult < 5:
        Qnum = random.randint(0, 2)
    if Qnumbool == 1:
        question = questionlist1[Qnum]
    else:
        question = questionlist2[Qnum]
    information[0] = question
    # 根据难度设置信息呈现参数
    if difficult == 1:
        m = random.randint(10, 100)  # m为数字之和的大小取值
    if difficult == 2:
        m = random.randint(10, 20)
    if difficult == 3:
        m = random.randint(21, 50)
    if difficult == 4:
        m = random.randint(51, 100)
    if difficult == 5:
        m = random.randint(10, 20)
    if difficult == 6:
        m = random.randint(21, 50)
    if difficult == 7:
        m = random.randint(51, 100)

    # 第一屏信息
    bool1 = 1
    if Qnum == 0:
        bool1 = 0
    if difficult == 1:
        bool1 = 1
    mstr = str(m)  # 学校留的作业数转为字符串
    background.image = 'pictures/Antelope.png'
    background.draw()
    text1_1 = '学校今天留了\n'+mstr+'道数学题'
    text1_2 = '学校今天留了一\n些数学题目'
    if bool1 == 1:
        text.text = text1_1
        information[1] = text1_1
    else:
        text.text = text1_2
        information[1] = text1_2
    text.pos = [-250, 100]
    text.draw()
    win.flip()
    clock.reset()
    while clock.getTime() < stim_dynamict_s:  # 刺激呈现时间内持续呈现
        Yaoyaxin = 1
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()
    # 第二屏呈现
    background.image = 'pictures/Peppa.png'
    m1 = random.randint(2, m-6)
    m1str = str(m1)
    bool2 = 1
    if Qnum == 1:
        bool2 = 0
    if difficult == 1:
        bool2 = 1
    text2_1 = '我今天上午在学校\n做了'+m1str+'道数学题'
    text2_2 = '我今天在学校做\n了一些数学题'
    if bool2 == 1:
        text.text = text2_1
        information[2] = text2_1
    else:
        text.text = text2_2
        information[2] = text2_2
    text.pos = [-40, 120]
    background.draw()
    text.draw()
    win.flip()
    clock.reset()
    while clock.getTime() < stim_dynamict_s:
        Yaoyaxin = 1
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()

    # 第三屏
    background.image = 'pictures/Antelope.png'
    if difficult > 1 and difficult < 5:
        m2 = m-m1
    else:
        m2 = random.randint(2, m-m1-2)
    m2str = str(m2)
    bool3 = 1
    if Qnum == 2:
        bool3 = 0
    if difficult == 1:
        bool3 = 1
    text3_1 = '佩奇晚上在家里\n做了'+m2str+'道数学题'
    text3_2 = '佩奇晚上在家里做了\n一些数学题'
    if bool3 == 1:
        text.text = text3_1
        information[3] = text3_1
    else:
        information[3] = text3_2
        text.text = text3_2
    text.pos = [-270, 100]
    background.draw()
    text.draw()
    win.flip()

    # 第四屏
    clock.reset()
    while clock.getTime() < stim_dynamict_s:
        Yaoyaxin = 1
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()
    background.image = 'pictures/Peppa.png'
    m3 = m - m1 - m2
    m3str = str(m3)
    bool4 = 1
    if Qnum == 3:
        bool4 = 0
    if difficult == 1:
        bool4 = 1
    text4_1 = '我还有'+m3str+'道题\n没做完'
    text4_2 = '我今天的数学作\n业没有做完'
    text4_3 = '我今天做完了数\n学作业'
    if bool4 == 1:
        text.text = text4_1
        information[4] = text4_1
    else:
        text.text = text4_2
        information[4] = text4_2
    if difficult > 1 and difficult < 5:
        text.text = text4_3
        information[4] = text4_3
    text.pos = [-40, 100]
    background.draw()
    text.draw()
    win.flip()
    clock.reset()
    while clock.getTime() < stim_dynamict_s:
        Yaoyaxin = 1
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()
    # 第五屏
    background.image = 'pictures/Antelope.png'
    background.draw()
    text.text = question
    text.pos = [-270, 100]
    text.draw()
    win.flip()
    clock.reset()
    while clock.getTime() < stim_dynamict_s:
        Yaoyaxin = 1
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()
    # 答案选择页面
    AnswerArray = [mstr, m1str, m2str, m3str]  # 字符串格式备选答案
    AnswerArrayint = [m, m1, m2, m3]
    background.image = 'pictures/blank'
    background.draw()
    Answerbool2 = random.randint(0, 3)  # 错误答案大小
    Answerbool3 = random.randint(0, 3)
    while Answerbool2 == Answerbool3:
        Answerbool2 = random.randint(0, 3)  # 错误答案大小
        Answerbool3 = random.randint(0, 3)
    Answerlevel = [-2, -1, 1, 2]
    erranswer1 = AnswerArrayint[Qnum]+Answerlevel[Answerbool2]
    erranswer2 = AnswerArrayint[Qnum]+Answerlevel[Answerbool3]

    answer_button = [AnswerArrayint[Qnum], erranswer1, erranswer2]
    random.shuffle(answer_button)  # 将答案随机打乱
    button_answer1_text = str(answer_button[0])
    button_answer2_text = str(answer_button[1])
    button_answer3_text = str(answer_button[2])
    button_answer1 = my_button(160, 80, button_answer1_text, [-350, 0], textColor=[0, 0, 0],
                               textSize=80)
    button_answer1cover = my_button(160, 80, button_answer1_text, [-350, 0], textColor=[0, 0, 0],
                                    textSize=80, fillColor=[250, 240, 230])
    button_answer2 = my_button(160, 80, button_answer2_text, [0, 0], textColor=[0, 0, 0],
                               textSize=80)
    button_answer2cover = my_button(160, 80, button_answer2_text, [0, 0], textColor=[0, 0, 0],
                                    textSize=80, fillColor=[250, 240, 230])
    button_answer3 = my_button(160, 80, button_answer3_text, [350, 0], textColor=[0, 0, 0],
                               textSize=80)
    button_answer3cover = my_button(160, 80, button_answer3_text, [350, 0], textColor=[0, 0, 0],
                                    textSize=80, fillColor=[250, 240, 230])
    button_next = my_button(160, 80, '下一题', [0, -300], textColor=[0, 0, 0],
                            textSize=50, fillColor=[245, 245, 245])
    text.text = '请选择你的答案'
    text.heigh = 60
    text.pos = [0, 300]
    text.draw()

    button_answer1.draw()
    button_answer2.draw()
    button_answer3.draw()
    button_next.draw()
    win.flip()
    clock.reset()
    clickornot = 0
    while 1 == 1:
        if clickornot != 1:
            if mouse.isPressedIn(button_answer1.button):
                background.draw()
                text.draw()
                button_next.draw()
                button_answer1cover.draw()
                button_answer2.draw()
                button_answer3.draw()
                win.flip()
                if answer_button[0] == AnswerArrayint[Qnum]:
                    feedback = 1
                else:
                    feedback = 0
                time = clock.getTime()
                clickornot = 1
        if clickornot != 2:
            if mouse.isPressedIn(button_answer2.button):
                background.draw()
                text.draw()
                button_next.draw()
                button_answer2cover.draw()
                button_answer1.draw()
                button_answer3.draw()
                win.flip()
                if answer_button[1] == AnswerArrayint[Qnum]:
                    feedback = 1
                else:
                    feedback = 0
                time = clock.getTime()
                clickornot = 2
        if clickornot != 3:
            if mouse.isPressedIn(button_answer3.button):
                background.draw()
                text.draw()
                button_next.draw()
                button_answer3cover.draw()
                button_answer1.draw()
                button_answer2.draw()
                win.flip()
                if answer_button[2] == AnswerArrayint[Qnum]:
                    feedback = 1
                else:
                    feedback = 0
                time = clock.getTime()
                clickornot = 3
        if mouse.isPressedIn(button_next.button):
            if clickornot != 0:
                feedback = show_feedback(feedback)
                return feedback, time, information
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()

def show_feedback(feedback):
    """显示反馈
    feedback=1为正确，显示笑脸
    feedback=0为错误，显示哭脸
    feedback=2为未选择，显示提示语
    函数返回正确为1错误或者未选择为0（用于正确率计算）"""
    if feedback == 1:
        text.text = ""
        feedback_picture = smilling_face
    elif feedback == 0:
        text.text = ""
        feedback_picture = crying_face
        feedback_picture.opacity = 1
    else:
        text.text = "你没有按键！"
        feedback_picture = crying_face
        feedback_picture.opacity = 0
        feedback = 0
    text.pos = [0, 0]
    clock.reset()
    while clock.getTime() < feedback_duration_s:
        background.draw()
        feedback_picture.draw()
        text.draw()
        win.flip()
        if defaultKeyboard.getKeys(keyList=["escape"]):
            save_and_quit()
    return feedback

def save_and_quit():
    """保存后退出"""
    os.chmod(filename + '.csv', stat.S_IWRITE)  # 权限改为读写
    thisExp.saveAsWideText(filename + '.csv', appendFile=True)
    os.chmod(filename + '.csv', stat.S_IREAD)  # 权限改为只读
    # make sure everything is closed down
    thisExp.abort()  # or data files will save again on exit
    core.quit()

# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)

# Store info about the experiment session
psychopyVersion = '2021.2.3'
# from the Builder filename that created this script
expName = 'task8'
expInfo = {'被试实验编号': '', '姓名拼音': '', '第几次训练': ''}
dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
if dlg.OK == False:
    core.quit()  # user pressed cancel
expInfo['participant'] = expInfo.pop("被试实验编号")
expInfo['name'] = expInfo.pop("姓名拼音")
expInfo['session'] = expInfo.pop("第几次训练")
expInfo['date'] = data.getDateStr()  # add a simple timestamp
expInfo['expName'] = expName
expInfo['psychopyVersion'] = psychopyVersion
# Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = os.path.dirname(os.path.dirname(_thisDir)) + os.sep + \
    u'data/%s_%s' % (expInfo['participant'], expName)
# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
                                 extraInfo=expInfo, runtimeInfo=None,
                                 originPath=os.path.abspath(__file__),
                                 savePickle=True, saveWideText=True,
                                 dataFileName=filename)

# 窗口
win = psychopy.visual.Window(
    size=[1350, 891],
    units="pix",
    fullscr=False,         # fullscr means full-screen mode
)
# # store frame rate of monitor if we can measure it
# expInfo['frameRate'] = win.getActualFrameRate()
# if expInfo['frameRate'] != None:
#     frameDur = 1.0 / round(expInfo['frameRate'])
# else:
#     frameDur = 1.0 / 60.0  # could not measure, so guess
background = psychopy.visual.ImageStim(
    win=win,
    image='pictures/background-图片透明度降低20%.png',
    units='pix',
    size=win.size,
    opacity=0.8
)

smilling_face = psychopy.visual.ImageStim(
    win=win,
    image="pictures/positive_.png",
    units="pix",
    size=[250, 250]
)

crying_face = psychopy.visual.ImageStim(
    win=win,
    image="pictures/negative_.png",
    units="pix",
    size=[250, 250]
)

defaultKeyboard = keyboard.Keyboard()

pre_duration_s = 0.5  # 500ms的黑屏
stim_static_s = 1.0  # 1000ms的刺激呈现
stim_dynamict_s = 2.5  # 500ms刺激移动
interal_duration_s = 1.0  # 1000ms的幕间暂停
res_duration_s = 6  # 6000ms的反应时间
feedback_duration_s = 2.0  # 2000ms的反馈时间

trials_in_block = 45

clock = psychopy.core.Clock()

text = psychopy.visual.TextStim(
    win=win,
    units="pix",
    height=40,
    bold=True,
    font="STXINWEI",
    color='black',
    pos=(0, 0),
    # wrapWidth=None, ori=0,
    # color='white', colorSpace='rgb', opacity=1, languageStyle='LTR',
    # depth=0.0, font='Arial'
)

background.draw()
win.flip()
psychopy.event.waitKeys()

difficulty = 1
accuracy = []
for i_trials in range(trials_in_block):

    if len(accuracy) > 4:
        if sum(accuracy)/len(accuracy) >= 0.8:
            del accuracy[:]
            if difficulty < 8:
                difficulty = difficulty + 1

    feedback, time, information = trials(difficulty)
    accuracy.append(feedback)
    # 保存结果
    thisExp.addData('trial', i_trials+1)
    thisExp.addData('feedback', feedback)
    thisExp.addData('reaction time', time)
    thisExp.addData('difficulty', difficulty)
    thisExp.addData('question', information[0])
    thisExp.addData('screen1', information[1])
    thisExp.addData('screen2', information[2])
    thisExp.addData('screen3', information[3])
    thisExp.addData('screen4', information[4])
    thisExp.nextEntry()

    #thisExp.addData('log_difference', log_difference)
    #thisExp.addData('accuracy', corrects / 20)
    # thisExp.nextEntry()
save_and_quit()
background.image = 'pictures/close.png'
background.draw()
win.flip()
psychopy.event.waitKeys()
# make sure everything is closed down
thisExp.abort()  # or data files will save again on exit
core.wait(2)
win.close()


import webbrowser

input_file = open('in.txt', 'r', encoding='utf-8')

output = ""
for line in input_file:
    if len(line) > 1 and line.strip()[-1] == '-':
        output += line.strip()[:-1]
    else:
        output += line.strip() + ' '

url = 'https://translate.google.com/#view=home&op=translate&sl=auto&tl=zh-CN&text='
url += output.replace(' ', '%20')
print(output)
webbrowser.open(url)

input_file.close()


"""
Создайте программу для игры с конфетами человек против человека.
Условие задачи: На столе лежит 2021 конфета.
Играют два игрока делая ход друг после друга.
Первый ход определяется жеребьёвкой.
За один ход можно забрать не более чем 28 конфет.
Все конфеты оппонента достаются сделавшему последний ход.
Сколько конфет нужно взять первому игроку,
чтобы забрать все конфеты у своего конкурента?
"""
from random import randint

candies_on_table = int(input('Сколько конфет лежит на столе? '))
current_player = randint(0, 1)  # Определяем, кто ходит первым: 0 - PC, 1 - Пользователь

while candies_on_table > 0:

    if current_player == 1:  # Пользователь
        print(f'Ход делает Пользователь.')
        can_take = 28 if candies_on_table > 28 else candies_on_table  # Максимальное число конфет, которое можно взять
        recommend_number = candies_on_table % 29 if candies_on_table % 29 != 0 else randint(1, 28)
        print(f'На столе {candies_on_table} конфет. (Рекомендуем взять {recommend_number} конфет.)')
        take_candies = int(input(f'Сколько конфет нужно взять? (введите число от 1 до {can_take}) '))
        while take_candies < 1 or take_candies > 28 or take_candies > candies_on_table:
            take_candies = int(input('Вы пытаетесь взять неверное количество конфет! Попробуйте снова: '))
        candies_on_table -= take_candies
        print(f'Пользователь взял {take_candies} конфет, осталось {candies_on_table} конфет.')

        if candies_on_table == 0:
            print('Победил Пользователь!')

        current_player = 0  # Переход хода
        print()

    elif current_player == 0:  # Компьютер
        print(f'Ход делает PC.')
        print(f'На столе {candies_on_table} конфет.')
        take_candies = candies_on_table % 29 if candies_on_table % 29 != 0 else randint(1, 28)
        candies_on_table -= take_candies
        print(f'PC взял {take_candies} конфет, осталось {candies_on_table} конфет.')

        if candies_on_table == 0:
            print(f'Победил PC!')

        current_player = 1
        print()

print('Игра закончена!')


import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import statsmodels.api as sm

os.chdir(r"D:\Data Mining\Sample files")
df = pd.read_csv('Life Expectancy Data.csv')
dataset=df.copy()
dataset.head(10)

# REMOVING UNNECESSARY FEATURES
dataset.drop(['GDP','Population','Income composition of resources', 'Schooling','Total expenditure'],axis=1,inplace=True)

# missing values handling
dataset.isnull().sum()
dataset = dataset[(dataset.Country != 'Monaco')&(dataset.Country != 'San Marino')& (dataset.Country != 'South Sudan') & (dataset.Country != 'Sudan') ]#,'San Marino','Sudan')] # South sudan has alot of missing data.
dataset['Alcohol'].fillna(method='ffill',inplace=True)
dataset['Life expectancy '].fillna(method='ffill',inplace=True)
dataset['Adult Mortality'].fillna(method='ffill',inplace=True)

#Canada and France are mislabeled as Developing
dataset.loc[(dataset.Country == 'Canada'), 'Status'] = "Developed"
dataset.loc[(dataset.Country == 'France'), 'Status'] = "Developed"

dataset.fillna({'Hepatitis_B': df.groupby('Country')['Hepatitis_B'].transform(lambda grp: grp.fillna(np.mean(grp))),
                'Polio': df.groupby('Country')['Polio'].transform(lambda grp: grp.fillna(np.mean(grp))),
                'Diphtheria ': df.groupby('Country')['Diphtheria '].transform(lambda grp: grp.fillna(np.mean(grp)))}
               ,inplace=True)
dataset.fillna({'Hepatitis_B':dataset['Hepatitis_B'].mode().iloc[0]},inplace = True) # some countries dont have this value at all
dataset.isnull().sum()

# ENCODING

dataset.Status = dataset.Status.replace({'Developing':0,
                             'Developed':1})

#QQ plot
stats.probplot(df_out['Life expectancy '], dist="norm", plot=plt)
plt.title('Life Expectancy QQ Plot')
plt.show()
print(stats.shapiro(dataset['Life expectancy ']))

# Outliers identification
before = dataset.describe()
Q1 = dataset.quantile(0.25)
Q3 = dataset.quantile(0.75)
IQR = Q3 - Q1
outliers = ((dataset < (Q1 - 1.5 * IQR)) |(dataset > (Q3 + 1.5 * IQR)))
df_out = dataset[~((dataset < (Q1 - 1.5 * IQR)) |(dataset > (Q3 + 1.5 * IQR))).any(axis=1)]
after = df_out.describe()

X= dataset.drop(['Country' ,'Year','Status'],axis=1)

X = sm.add_constant(X)

y = dataset['Life expectancy ']

X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)

ols = sm.OLS(y_train,X_train) # target, list of independent variable
lr = ols.fit()
print(lr.summary())

while (lr.pvalues.max()>0.05):
    X_train.drop(lr.pvalues.idxmax(),axis=1,inplace=True)
    X_test.drop(lr.pvalues.idxmax(),axis=1, inplace = True)
    ols= sm.OLS(y_train,X_train)
    lr = ols.fit()

X_train.columns # remove the constant column

X_train = X2.drop('const', axis=1, inplace=True)

from sklearn.linear_model import LinearRegression

model = LinearRegression()

#train the model on training set
model.fit(X_train,y_train)

#cross validation
y_pred = model.predict(X_test)

from sklearn.metrics import mean_absolute_error, mean_squared_error
mean_abs_error = mean_absolute_error(y_test,y_pred)
mean_sq_error = mean_squared_error(y_test, y_pred)

import math
rmse = math.sqrt(mean_sq_error)
print (rmse,mean_abs_error)

incorrect results after feature elimination.py


def main():

    data = readFiles()

    grid = makeOverlapGrid()
    mapOverlapGrid(data, grid)
    result = countOverlap(grid)

    print(result)

def makeOverlapGrid():
    return [[0 for x in range(1000)] for y in range(1000)]

def mapOverlapGrid(data, grid):

    for datum in data:
        for x in range(datum["startX"], datum["endX"]):
            for y in range(datum["startY"], datum["endY"]):

                if grid[x][y] == 0:
                    grid[x][y] = 1
                elif grid[x][y] == 1:
                    grid[x][y] = 2

def countOverlap(grid):
    count = 0
    for x in range(1000):
        for y in range(1000):
            if grid[x][y] == 2:
                count += 1
    return count

def processFileLine(line):
    datum = {}
    lineSplit = line.split()
    startCoor = lineSplit[2].split(',')
    startCoor[1] = startCoor[1][:-1]
    dim = line.split()[3].split('x')
    datum["startX"] = int(startCoor[0])
    datum["startY"] = int(startCoor[1])
    datum["endX"] = datum["startX"] + int(dim[0])
    datum["endY"] = datum["startY"] + int(dim[1])
    return datum

def readFiles():
    data = []
    file = open("input.txt", "r")
    for line in file:
        data.append(processFileLine(line))
    file.close()
    return data

if __name__ == "__main__":
    main()