from __future__ import division
from pylab import *
from scipy.optimize import fsolve
import matplotlib.pyplot as plt

def euler(f, t0, y0, h, N):
    t = t0 + arange(N+1)*h
    y = zeros((N+1, size(y0)))
    y[0] = y0
    for n in range(N):
        y[n+1] = y[n] + h*f(t[n], y[n])
    return y

def backwardEuler(f, t0, y0, h, N):
    t = t0 + arange(N+1)*h
    y = zeros((N+1, size(y0)))
    y[0] = y0
    for n in range(N):
        def F(ynplus1):
            return -ynplus1 + y[n] + h*f(t[n+1], ynplus1)
        y[n+1] = fsolve(F, y[n])
    return y

def trapezoid(f, t0, y0, h, N):
    t = t0 + arange(N+1)*h
    y = zeros((N+1, size(y0)))
    y[0] = y0
    for n in range(N):
        def F(ynplus1):
            return -ynplus1 + y[n] + 0.5*h*(f(t[n],y[n]) + f(t[n+1],ynplus1))
        y[n+1] = fsolve(F,y[n])
    return y

A = [[0,1],[-1,0]];

def f(t,y):
    return dot(A,y);

def main():
    y0 = [1,0];
    t0 = 0;
    tmax = 100;

    h1 = 0.1;
    N1 = 1000;
    y1 = euler(f,t0,y0,h1,N1);
    t1 = t0 + arange(N1+1)*h1;
    plt.plot(t1,y1[:,0]);
    plt.xlabel('t');
    plt.ylabel('x(t)');
    plt.title('Euler Method with h=0.1');
    plt.show();

    h2 = 0.01;
    N2 = 10000;
    y2 = euler(f,t0,y0,h2,N2);
    t2 = t0 + arange(N2+1)*h2;
    plt.plot(t2,y2[:,0]);
    plt.xlabel('t');
    plt.ylabel('x(t)');
    plt.title('Euler Method with h=0.01');
    plt.show();

    y3 = backwardEuler(f,t0,y0,h1,N1);
    plt.plot(t1,y3[:,0]);
    plt.xlabel('t');
    plt.ylabel('x(t)');
    plt.title('BackwardEuler Method with h=0.1');
    plt.show();

    y4 = backwardEuler(f,t0,y0,h2,N2);
    plt.plot(t2,y4[:,0]);
    plt.xlabel('t');
    plt.ylabel('x(t)');
    plt.title('BackwardEuler Method with h=0.01');
    plt.show();

    y5 = trapezoid(f,t0,y0,h1,N1);
    plt.plot(t1,y5[:,0]);
    plt.xlabel('t');
    plt.ylabel('x(t)');
    plt.title('Trapezoid Method with h=0.1');
    plt.show();

    y6 = trapezoid(f,t0,y0,h2,N2);
    plt.plot(t2,y6[:,0]);
    plt.xlabel('t');
    plt.ylabel('x(t)');
    plt.title('Trapezoid Method with h=0.01');
    plt.show();

if __name__ == "__main__": main()


# import os
from app import create_app

from flask.ext.script import Manager, Server

app = create_app('testing')
manager = Manager(app)
manager.add_command('runserver', Server(host='0.0.0.0'))

if __name__ == '__main__':
    manager.run()


from SENN.preprocessing import preprocess_text
from SENN.mapping import *
import os
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
import re

def load_allDatasets(data_folder):
    data_dailydialog, emotions_dailydialog = load_dailyDialogs(data_folder)
    data_crowdflower, emotions_crowdflower = load_crowdFlower(data_folder)
    data_tec, emotions_tec = load_TEC(data_folder)
    data_talesemotions, emotions_talesemotions = load_tales_emotions(data_folder)
    data_isear, emotions_isear = load_ISEAR(data_folder)
    data_emoint, emotions_emoint = load_emoInt(data_folder)
    data_electorialtweets, emotions_electorialtweets = load_electorialTweets(data_folder)
    data_emocause, emotions_emocause = load_electorialTweets(data_folder)

    test_size = 0.1

    X_train_dailydialog, X_test_dailydialog, y_train_dailydialog, y_test_dailydialog \
        = train_test_split(data_dailydialog, emotions_dailydialog, test_size=test_size)
    y_train_dailydialog = np.array(y_train_dailydialog)
    y_test_dailydialog = np.array(y_test_dailydialog)

    X_train_crowdflower, X_test_crowdflower, y_train_crowdflower, y_test_crowdflower \
        = train_test_split(data_crowdflower, emotions_crowdflower, test_size=test_size)
    y_train_crowdflower = np.array(y_train_crowdflower)
    y_test_crowdflower = np.array(y_test_crowdflower)

    X_train_tec, X_test_tec, y_train_tec, y_test_tec \
        = train_test_split(data_tec, emotions_tec, test_size=test_size)
    y_train_tec = np.array(y_train_tec)
    y_test_tec = np.array(y_test_tec)

    X_train_talesemotions, X_test_talesemotions, y_train_talesemotions, y_test_talesemotions \
        = train_test_split(data_talesemotions, emotions_talesemotions, test_size=test_size)
    y_train_talesemotions = np.array(y_train_talesemotions)
    y_test_talesemotions = np.array(y_test_talesemotions)

    X_train_isear, X_test_isear, y_train_isear, y_test_isear \
        = train_test_split(data_isear, emotions_isear, test_size=test_size)
    y_train_isear = np.array(y_train_isear)
    y_test_isear = np.array(y_test_isear)

    # print(len(data_emoint))
    X_train_emoint, X_test_emoint, y_train_emoint, y_test_emoint \
        = train_test_split(data_emoint, emotions_emoint, test_size=test_size)
    y_train_emoint = np.array(y_train_emoint)
    y_test_emoint = np.array(y_test_emoint)

    X_train_electorialtweets, X_test_electorialtweets, y_train_electorialtweets, y_test_electorialtweets \
        = train_test_split(data_electorialtweets, emotions_electorialtweets, test_size=test_size)
    y_train_electorialtweets = np.array(y_train_electorialtweets)
    y_test_electorialtweets = np.array(y_test_electorialtweets)

    X_train_emocause, X_test_emocause, y_train_emocause, y_test_emocause \
        = train_test_split(data_emocause, emotions_emocause, test_size=test_size)
    y_train_emocause = np.array(y_train_emocause)
    y_test_emocause = np.array(y_test_emocause)

    X_train = X_train_dailydialog+X_train_crowdflower+X_train_tec+X_train_talesemotions+X_train_isear+X_train_emoint+X_train_electorialtweets+X_train_emocause
    X_test = X_test_dailydialog+X_test_crowdflower+X_test_tec+X_test_talesemotions+X_test_isear+X_test_emoint+X_test_electorialtweets+X_test_emocause
    # print(y_train_dailydialog.shape, y_train_crowdflower.shape)
    y_train = np.concatenate((y_train_dailydialog, y_train_crowdflower, y_train_tec, y_train_talesemotions, y_train_isear, y_train_emoint,y_train_electorialtweets, y_train_emocause))
    y_test = np.concatenate((y_test_dailydialog, y_test_crowdflower, y_test_tec, y_test_talesemotions, y_test_isear, y_test_emoint, y_test_electorialtweets, y_test_emocause))

    return X_train, X_test, y_train, y_test

def load_dailyDialogs(data_folder):
    print("Loading daily dialogs")
    # data_rez = {}
    # emotion_rez = {}
    data_rez = []
    emotion_rez = []
    path = data_folder+'dailydialog/ijcnlp_dailydialog/'
    # print(os.listdir(path))
    data = (open(path + "dialogues_text.txt", "r")).read().split('\n')
    emotion = (open(path + "dialogues_emotion.txt", "r")).read().split('\n')
    # text_type =  (open(path+"dialogues_topic.txt", "r")).read().split('\n')
    for i in range(0, len(emotion)):
        # if text_type[i]=='':
        # continue
        tmp_data = data[i].split('__eou__')
        tmp_emo = emotion[i].split(' ')
        for j in range(0, len(tmp_emo)):
            if tmp_emo[j] == '':
                continue
            tmp_emo[j] = map_dialog_emotion(tmp_emo[j])
            tmp_data[j] = preprocess_text(tmp_data[j])
            # add_element(data_rez, map_dialog_topic(text_type[i]), tmp_data)
            # add_element(emotion_rez, map_dialog_topic(text_type[i]), tmp_emo)

            data_rez.append(tmp_data[j])
            emotion_rez.append(tmp_emo[j])
        # print(i)
    return data_rez, emotion_rez

def load_crowdFlower(data_folder):
    print("Loading crowd flower")
    file = data_folder+'crowdflower/text_emotion.csv'
    # print (file)
    df = pd.read_csv(file)
    # print(data.head())
    # print(df.columns)
    # print(set(df['sentiment'].tolist()))
    emotions = df['sentiment']
    data = df['content']
    # print(data.shape)
    return map_crowdflower(data, emotions)

def load_TEC(data_folder):
    print("Loading TEC")
    data = []
    emotions = []
    folder = data_folder+'TEC/'
    file = open(folder + 'Jan9-2012-tweets-clean.txt').read().split('\n')
    set_emo = set()
    for line in file:
        line = line.split(':')
        if line == ['']:
            continue
        emotion = map_TEC_emotion(line[-1])
        text = preprocess_text(line[1])
        # print(emotion, text)
        # set_emo.add(emotion)
        data.append(text)
        emotions.append(emotion)
    # print(set_emo)
    return data, emotions

def load_tales_emotions(data_folder):
    print("Loading Tales Emotions")
    data = []
    emotions = []
    author = "grimms"
    folder = data_folder+'tales-emotion/Grimms/emmood/'
    for file in os.listdir(folder):
        f_tmp = open(folder + file, 'r').read().split('\n')
        for f in f_tmp:
            f = f.split('\t')
            if f[0] == '':
                continue
            text = f[3]
            emotions_tmp = f[1].split(":")
            if emotions_tmp[0] != emotions_tmp[1]:
                # print(emotions_tmp)
                continue
            data.append(preprocess_text(text))
            emotions.append(map_tales_emotions(emotions_tmp[0]))
            # add_element(data, author, [preprocess_text(text)])
            # add_element(emotions, author, [map_tales_emotions(emotions_tmp[0])])
            # print(f)

    author = "HCAndersen"
    folder = data_folder+'tales-emotion/HCAndersen/emmood/'
    for file in os.listdir(folder):
        f_tmp = open(folder + file, 'r').read().split('\n')
        for f in f_tmp:
            f = f.split('\t')
            if f[0] == '':
                continue
            text = f[3]
            emotions_tmp = f[1].split(":")
            if emotions_tmp[0] != emotions_tmp[1]:
                # print(emotions_tmp)
                continue
            data.append(preprocess_text(text))
            emotions.append(map_tales_emotions(emotions_tmp[0]))
            # add_element(data, author, [preprocess_text(text)])
            # add_element(emotions, author, [map_tales_emotions(emotions_tmp[0])])
            # print(f)

    author = "Potter"
    folder = data_folder+'tales-emotion/Potter/emmood/'
    for file in os.listdir(folder):
        f_tmp = open(folder + file, 'r').read().split('\n')
        for f in f_tmp:
            f = f.split('\t')
            if f[0] == '':
                continue
            text = f[3]
            emotions_tmp = f[1].split(":")
            if emotions_tmp[0] != emotions_tmp[1]:
                # print(emotions_tmp)
                continue
            data.append(preprocess_text(text))
            emotions.append(map_tales_emotions(emotions_tmp[0]))
            # add_element(data, author, [preprocess_text(text)])
            # add_element(emotions, author, [map_tales_emotions(emotions_tmp[0])])
            # print(f)

    # print(data)
    # print(emotions)
    return data, emotions

def load_ISEAR(data_folder):
    print("Loading ISEAR")
    folder = data_folder+'isear/'
    file = open(folder + 'isear.csv').read().split('\n')
    # print(len(file))
    # set_emos = set()
    data = []
    emotions = []
    for line in file:
        line2 = line.split('|')
        if len(line2) != 43:
            emotion = map_isear_emotions(line2[36])
            # set_emos.add(emotion)
            text = preprocess_text(" ".join(line2[40:-2]))
            # print(emotion, text)
            # print(line)
            if emotion is not None:
                data.append(text)
                emotions.append(emotion)
        else:
            text = preprocess_text(line2[40])
            emotion = map_isear_emotions(line2[36])
            # set_emos.add(emotion)
            # print(line2)
            # print(emotion, text)
            if emotion is not None:
                data.append(text)
                emotions.append(emotion)
    # print(set_emos)
    return data, emotions

def load_emoInt(data_folder):
    print("Loading EmoInt")
    data = []
    emotions = []
    file = data_folder+"emoint/emoint_all"
    # for file in os.listdir(folder):
    #     print(file)
    # emo = map_emoInt(file.split('-')[0])
    # key = file.split('.')[-2]
    # print(key)
    f = open(file, 'r').read().split('\n')
    for line in f:
        if len(line) == 0:
            continue
        line = line.split('\t')
        emo = map_emoInt(line[-2])
        # print(line)
        if len(line) != 1 and emo!=None:
            # print("dodano")
            text = preprocess_text(line[1])
            data.append(preprocess_text(text))
            emotions.append(emo)
            # add_element(data, key, [text])
            # add_element(emotions, key, [emo])
    return data, emotions

def load_electorialTweets(data_folder):
    print("Loading electorial tweets")
    data = {}
    emotions = {}
    folder = data_folder+'electoraltweets/Annotated-US2012-Election-Tweets/'
    file1 = open(folder + 'Questionnaire1/AnnotatedTweets.txt').read().split('\n')
    file2 = open(folder + 'Questionnaire2/Batch1/AnnotatedTweets.txt').read().split('\n')
    file3 = open(folder + 'Questionnaire2/Batch2/AnnotatedTweets.txt').read().split('\n')
    tweets_ids = set()
    for line in file1:
        if line[0] < '0' or line[0] > '9':
            continue
        line = line.split('\t')
        if line[10] == 'This tweet has no emotional content.':
            data[line[0]] = preprocess_text(line[14])
            emotions[line[0]] = [0, 0, 0, 1, 0, 0, 0]
            tweets_ids.add(line[0])
    for line in file2:
        if line[0] < '0' or line[0] > '9':
            continue
        line = line.split('\t')
        # print(line)
        emotion = map_electiontweets(line[15])
        if emotion == None:
            continue
        text = preprocess_text(line[13])
        # print(emotion+"   "+text)
        if line[0] in tweets_ids and emotions[line[0]] != emotion:
            emotions[line[0]] = None
            continue
        data[line[0]] = text
        emotions[line[0]] = emotion
        tweets_ids.add(line[0])
    for line in file3:
        if line[0] < '0' or line[0] > '9':
            continue
        line = line.split('\t')
        # print(line)
        emotion = map_electiontweets(line[15])
        if emotion == None:
            continue
        text = preprocess_text(line[13])
        # print(emotion+"   "+text)
        if line[0] in tweets_ids and emotions[line[0]] != emotion:
            emotions[line[0]] = None
            continue
        data[line[0]] = text
        emotions[line[0]] = emotion
        tweets_ids.add(line[0])
    # print(str(len(data))+ "    "+str(len(emotions)))
    # print(len(tweets_ids))
    # print(tweets_ids)
    res_data = []
    res_emotions = []
    for id in data:
        if emotions[id] != None:
            res_data.append(data[id])
            # print(emotion)
            res_emotions.append(emotions[id])
    return res_data, res_emotions
    # print(len(file1))
    # print(len(file2))
    # print(len(file3))

def load_emotioncause(data_folder):
    data = []
    emotions = []
    # set_emos = set()
    file = open(data_folder+"emotion-cause/Dataset/Emotion cause.txt", 'r').read().split('\n')
    for line in file:
        emotion = map_emocause(line.split('>')[0][1:])
        if emotion=='':
            print(line)
            continue
        text = re.sub(r'<cause>', ' ', line)
        text = re.sub(r'<\\cause>', ' ', text)
        text = " ".join(text.split('<\\')[:-1])
        text = preprocess_text(" ".join(text.split('>')[1:]))
        # print(emotion)
        # print(text)
        # set_emos.add(emotion)
        data.append(text)
        emotions.append(emotion)
    file = open(data_folder+"emotion-cause/Dataset/No cause.txt", 'r').read().split('\n')
    for line in file:
        emotion = map_emocause(line.split('>')[0][1:])
        if emotion=='':
            print(line)
            continue
        text = re.sub(r'<cause>', ' ', line)
        text = re.sub(r'<\\cause>', ' ', text)
        text = " ".join(text.split('<\\')[:-1])
        text = preprocess_text(" ".join(text.split('>')[1:]))
        # print(emotion)
        # print(text)
        # set_emos.add(emotion)
        data.append(text)
        emotions.append(emotion)
    # print(set_emos)
    return data, emotions

def add_element(dict, key, el):
    if el[-1] == '':
        el = el[:-1]
    # print(el)
    if key in dict:
        dict[key] = dict[key] + el
        return dict
    dict[key] = []
    dict[key] = dict[key] + el
    return dict


from dataclasses import dataclass
from uuid import UUID, uuid4

from sanic import response
from sanic.exceptions import Unauthorized
from sanic.request import Request
from sanic.log import logger

from dataclasses_jsonschema import JsonSchemaMixin

from yrest.tree import Tree
from yrest.mongo import Mongo
from yrest.ysanic import MongoServer
from yrest.openapi import OpenApi
from yrest.utils import  Ok, OkResult, ErrorMessage

@dataclass
class SetupModeRequest(JsonSchemaMixin):
  code: UUID
  group: "Group"
  admin: "User"

class Setup(Mongo, Tree):
  """Allows to setup the app"""
  @classmethod
  async def setup(self, request: Request, consume: SetupModeRequest) -> OkResult:
    """Setups the app"""
    if str(consume.code) != str(request.app.setup_code):
      raise Unauthorized("Check your server's logs to get your authorization code")

    try:
      consume.group["path"] = ""
      group = request.app._models.Group(**consume.group)
      group._table = request.app._table
    except TypeError:
      raise TypeError("Invalid group")

    try:
      consume.admin["roles"] = ["admin"]
      admin = request.app._models.User(**consume.admin)
    except TypeError as e:
      raise TypeError(f"Invalid user: {e}")

    await group.create()
    await group.create_child(admin, request.app._models)

class SetupMongoServer(MongoServer, OpenApi):
  def __init__(self, root_model, models, *args, **kwargs):
    super().__init__(root_model, models, *args, **kwargs)

    self.add_route(self.dispatcher, "/", ["PUT"])

    self.setup_code = uuid4()
    logger.info(f"Setup code: {self.setup_code}")

  async def updater(self, request, path: str = None):
    try:
      data = SetupModeRequest(**request.json)
      await request.app._root_model.setup(request, data)
      return response.json(Ok())
    except TypeError as e:
      return response.json(ErrorMessage(message = e.args[0], code = 400), 400)
    except Unauthorized as e:
      return response.json(ErrorMessage(message = e.args[0], code = 401), 401)


"""
Auxiliary functions for calculation sizes of backped data blocks.
"""
import math
from pathlib import Path
from tarfile import BLOCKSIZE, LENGTH_NAME
from typing import List

def calc_aligned_files_size(files: List[Path], alignment: int = 1) -> int:
    """
    Calculate total size of files on disk with padding added after each file.
    """
    size = 0
    for file in files:
        filesize = file.stat().st_size
        remainder = filesize % alignment
        if remainder > 0:
            filesize += alignment - remainder
        size += filesize
    return size

def calc_tarball_size(file_names: List[str], aligned_files_size: int) -> int:
    """
    Calculate tarball (TAR archive) size.

    Args:
        file_names: Names of files archived in the tarball.
        aligned_files_size: Summed size of all files including padding on BLOCKSIZE boundary
            for each file.
    """
    result = aligned_files_size
    for name in file_names:
        if len(name) < LENGTH_NAME:
            result += BLOCKSIZE  # file header
        else:
            result += (
                math.ceil(len(name) / BLOCKSIZE) + 2
            ) * BLOCKSIZE  # long name header + name data + file header
    return result

def calc_encrypted_size(
    data_size: int, encrypt_chunk_size: int, encrypt_metadata_size: int
) -> int:
    """
    Calculate size of encrypted data.
    """
    return data_size + math.ceil(data_size / encrypt_chunk_size) * encrypt_metadata_size


from tensorflow import keras
import tensorflow as tf # Imports tensorflow
import tensorflow_addons as tfa

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from tensorflow.keras import regularizers, optimizers
from tensorflow.keras.utils import plot_model
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.metrics import Recall,Precision,AUC,TruePositives,TrueNegatives,FalseNegatives,FalsePositives
import seaborn as sns
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import os
import cv2
from glob import glob

asd_data = pd.read_csv('Autism_Data.arff',na_values='?')
asd_data.head(n=5)

asd_data.loc[(asd_data['age'].isnull()) |(asd_data['gender'].isnull()) |(asd_data['ethnicity'].isnull())
|(asd_data['jundice'].isnull())|(asd_data['austim'].isnull()) |(asd_data['contry_of_res'].isnull())
            |(asd_data['used_app_before'].isnull())|(asd_data['result'].isnull())|(asd_data['age_desc'].isnull())
            |(asd_data['relation'].isnull())]
asd_data.dropna(inplace=True)
asd_data.describe()
# Reminder of the features:
print(asd_data.dtypes)

# Total number of records in clean dataset
n_records = len(asd_data.index)

# TODO: Number of records where individual's with ASD in the clean dataset
n_asd_yes = len(asd_data[asd_data['Class/ASD'] == 'YES'])

# TODO: Number of records where individual's with no ASD in the clean dataset
n_asd_no = len(asd_data[asd_data['Class/ASD'] == 'NO'])

# Print the results
print("Total number of records: {}".format(n_records))
print("Individuals diagonised with ASD: {}".format(n_asd_yes))
print("Individuals not diagonised with ASD: {}".format(n_asd_no))
asd_raw = asd_data['Class/ASD']
features_raw = asd_data[['age', 'gender', 'ethnicity', 'jundice', 'austim', 'contry_of_res', 'result',
                      'relation','A1_Score','A2_Score','A3_Score','A4_Score','A5_Score','A6_Score','A7_Score','A8_Score',
                      'A9_Score','A10_Score']]

scaler = MinMaxScaler()
numerical = ['age', 'result']

features_minmax_transform = pd.DataFrame(data = features_raw)
features_minmax_transform[numerical] = scaler.fit_transform(features_raw[numerical])
features_minmax_transform
# Show an example of a record with scaling applied
print(features_minmax_transform.head(n = 5))
#One-hot encode the 'features_minmax_transform' data using pandas.get_dummies()
features_final = pd.get_dummies(features_minmax_transform)
X=features_final
print(features_final.head(5))

asd_classes = asd_raw.apply(lambda x: 1 if x == 'YES' else 0)
y=asd_classes

encoded = list(features_final.columns)
print("{} total features after one-hot encoding.".format(len(encoded)))
print(encoded)
print(X.shape)
print(y.shape)
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=42)

inputs = keras.Input(shape=94, name="ANN")
out0 = Dense(256, activation="relu")(inputs)
out0 = BatchNormalization()(out0)
out0 = Dropout(0.5)(out0)
x = Dense(512, activation="relu" )(out0)
x = Dense(512, activation="relu" )(x)
x = Dropout(0.25)(x)
y = Dense(512, activation="relu" )(out0)
y = Dense(512, activation="relu" )(y)
y = Dropout(0.25)(y)
out1 = keras.layers.add([x,y])
x = Dense(1024, activation="relu")(out1)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
x = Dense(256, activation="relu")(x)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
output= Dense(1, kernel_initializer='normal', activation='sigmoid')(x)
model = keras.Model(inputs, output, name="ANN")
model.summary()
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy',AUC(),TruePositives(),TrueNegatives(),FalseNegatives(),FalsePositives()])
history = model.fit(X_train, y_train,
          batch_size=609,
          epochs=300,
          validation_data=(X_test, y_test),
          verbose=2)
plot_model(model, to_file='test3.png',show_shapes= True , show_layer_names=True)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='lower left')
plt.savefig('test3_accuracy.png')
plt.show()

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.savefig('test3_loss.png')
plt.show()

model.save('test3.h5')

pd.DataFrame.from_dict(history.history).to_csv('test3.csv',index=False)


import requests

def get_repos():
    r = requests.get("https://glittr.org/api/list")
    collection_list = r.json()
    return(collection_list)

def write_toc(collection_list, outfile):
    for category in collection_list:
        category_name = category['name']
        outfile.write(f"- [{category_name}](#{category_name.lower().replace(' ', '-')})\n")
        for topic in category['topics']:
            topic_name = topic['name']
            outfile.write(f"  - [{topic_name}](#{topic_name.lower().replace(' ', '-')})\n")

def write_collection(collection_list, outfile):
    for category in collection_list:
        category_name = category['name']
        outfile.write("\n## " + category_name + '\n\n')
        for topic in category['topics']:
            topic_name = topic['name']
            outfile.write("\n### " + topic_name + '\n\n')
            repo_list = topic['repositories']
            for repo in repo_list:
                if repo['website'] == '':
                    outfile.write(f"- [**{repo['author']['display_name'].strip()}** {repo['name']}]({repo['url']})\n")
                else:
                    outfile.write(f"- [**{repo['author']['display_name'].strip()}** {repo['name']}]({repo['url']}) | [website]({repo['website']})\n")

if __name__ == '__main__':
    collection_list = get_repos()

    output_file = 'README.md'

    with open(output_file, 'w') as outfile:
        with open('scripts/collection_header.md') as infile:
            for line in infile:
                outfile.write(line)
            write_toc(collection_list, outfile)
            write_collection(collection_list, outfile)


import jieba.analyse as analyse
import numpy as np
import jieba
import time
import re

def jieba_set_config():
    '''
    对jieba库设定参数，添加用户词典，同时设置停用词（停用词引用了上次作业中stopword_list.txt)
    '''
    print("\nSetting Jieba module config.\n")

    jieba.load_userdict(
        "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/anger.txt")
    jieba.load_userdict(
        "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/disgust.txt")
    jieba.load_userdict(
        "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/fear.txt")
    jieba.load_userdict(
        "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/joy.txt")
    jieba.load_userdict(
        "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/sadness.txt")

    analyse.set_stop_words(
        "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/stopwords_list.txt")

def clear_data(file):
    '''
    进行数据清洗和整理

    去除URL，将正文内容，时间和地点分别提取出，并和微博内容组成一个维度为3的列表，最终组成一个矩阵
    '''
    with open(file, 'r', encoding='utf-8') as f:
        data = f.readlines()

    clean_data = []
    for i in data:
        vector = [0 for i in range(3)]

        pattern = re.compile(
            r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))')

        i = re.sub(pattern, '', i)  # 去除微薄的URL等无关信息

        i = i.replace("转发微博", '')

        pattern = re.compile(r'(\[[^]]*)\d(?=[^\]]*\])')

        position = re.findall(pattern, i)

        position = position[-1].replace('[', '')
        # 用正则表达式选出的pattern仍有一点缺陷，所以选出的position多了一个中括号，可以继续加以改进
        # 在这里就用最基础的方法多进行一次处理
        position = list(map(float, position.split(",")))

        vector[2] = position
        # print(i)
        i = re.sub(pattern, '', i)

        i = i.rstrip()
        i = i[:-1]
        # 去掉右侧的空格，然后再去掉最后的方括号

        timestring = i[-31:]
        # timestring = timestring.replace("+0800",'')
        timestring = timestring.split("\r")[0]
        timestring = timestring.split("\t")[0]

        vector[1] = time.strptime(timestring, "%a %b %d %H:%M:%S +0800 %Y")

        vector[0] = i.replace(timestring, '')
        clean_data.append(vector)

    pattern = re.compile(r"(回复)?(//)?\s*@\S*?\s*(:| |$)")

    timeseries = []
    for i in clean_data:
        i[0] = re.sub(pattern, '', i[0])
        i[0] = analyse.extract_tags(i[0])
        # print(i)

    return clean_data

def analysis1(clean_data):
    '''
    已经完成清洗和整理的数据进行分析

    分别与五个字典进行比较，返回一个矩阵

    矩阵的五列分别对应anger,disgust,fear,joy,sadness
    '''
    def convert(lis):
        '''
        对五个字典进行处理，返回一个中文词的列表
        '''
        lis = ''.join(lis).strip("\n").splitlines()
        return lis

    with open("C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/anger.txt", 'r', encoding='utf-8') as anger:
        anger_list = anger.readlines()
        anger_list = convert(anger_list)

    with open("C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/disgust.txt", 'r', encoding='utf-8') as anger:
        disgust_list = anger.readlines()
        disgust_list = convert(disgust_list)

    with open("C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/fear.txt", 'r', encoding='utf-8') as anger:
        fear_list = anger.readlines()
        fear_list = convert(fear_list)

    with open("C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/joy.txt", 'r', encoding='utf-8') as anger:
        joy_list = anger.readlines()
        joy_list = convert(joy_list)

    with open("C:/Users/DELL/Desktop/Code/BUAA_21/Week3/emotion_lexicon/sadness.txt", 'r', encoding='utf-8') as anger:
        sadness_list = anger.readlines()
        sadness_list = convert(sadness_list)

    def judge(matrix):
        nonlocal anger_list, disgust_list, fear_list, joy_list, sadness_list, clean_data

        for comment in clean_data:
            vector = [0 for i in range(5)]
            info = []
            for word in comment[0]:
                if word in anger_list:
                    vector[0] += 1
                elif word in disgust_list:
                    vector[1] += 1
                elif word in fear_list:
                    vector[2] += 1
                elif word in joy_list:
                    vector[3] += 1
                elif word in sadness_list:
                    vector[4] += 1
            sum_num = sum(vector)
            if sum_num != 0:
                # 加入一步判断，去掉无情绪的向量
                for i in range(5):
                    vector[i] /= float(sum_num)
                info.append(vector)
                # 筛选出符合实际时间范围的数据
                info.extend(comment[1:])
                matrix.append(info)
                # print(info)
            else:
                pass

        return matrix

    return judge

def emotion_trend(Matrix, mood, mode):
    '''
    对情绪变化的趋势进行时间分析

    而通过观察时间的分布范围发现，总体的时间范围是2013-10-11 至 2013-10-13
    所以情绪的周模式，日模式和小时模式是比较有分析价值的
    如果设定为分钟模式，则显得过于细碎，不能较好的体现出情绪变化的趋势
    所以在本函数中主要选用week, day, hour模式进行分析
    '''
    def take(vector):
        return vector[1]

    Matrix.sort(key=take)  # 按照时间对矩阵中的向量进行排序
    output_matrix = []  # 输出结果的字符串矩阵

    time_range = [Matrix[0][1], Matrix[-1][1]]
    emotion_list = ['anger', 'disgust', 'fear', 'joy', 'sadness']

    if mood in emotion_list:

        index = emotion_list.index(mood)

        mode_range_min = time_range[0]
        mode_range_max = time_range[1]

        if mode == 'week':
            emotion_sum = []
            for item in Matrix:
                emotion_sum.append(item[0][index])
            output = 'From 2013-10-11 to 2013-10-13, the ' + \
                emotion_list[index] + \
                ' proportion is %.6f' % (sum(emotion_sum)/len(emotion_sum))
            output_matrix.append(output)

        elif mode == 'day':

            for i in range(int(mode_range_min.tm_mday), int(mode_range_max.tm_mday)+1):
                emotion_sum = []
                for item in Matrix:
                    if item[1].tm_mday == i:
                        emotion_sum.append(item[0][index])
                    else:
                        pass
                try:
                    output = "The 2013-10-%d\'s " % i + emotion_list[index] + ' proportion is %.6f' % (sum(
                        emotion_sum)/len(emotion_sum))
                    output_matrix.append(output)
                except:
                    break

        elif mode == 'hour':
            for i in range(int(mode_range_min.tm_mday), int(mode_range_max.tm_mday)+1):
                for j in range(0, 24):
                    emotion_sum = []
                    for item in Matrix:
                        if item[1].tm_hour == j and item[1].tm_mday == i:
                            emotion_sum.append(item[0][index])
                        else:
                            pass
                    try:
                        output = "The 2013-10-%d %d:00--%d:00\'s " % (
                            i, j, j+1)+emotion_list[index]+' proportion is %.6f' % (sum(emotion_sum)/len(emotion_sum))
                        output_matrix.append(output)
                    except:
                        break
        else:
            print("The time mode you choose is invalid")

    else:
        print("The emotion you choose is invalid.\n")

    return output_matrix

def emotion_distance(Matrix, mood, radius):
    '''
    分析情绪的空间分布

    实现一个函数可以通过参数来控制返回情绪的空间分布
    围绕某个中心点，随着半径增加，该情绪所占比例的变化(以经纬度的0.01为步长)
    而radius在输入时应该是0~0.09
    （在本方法中，中心点默认是城市的中心位置）
    '''

    position_x = []
    position_y = []

    output_matrix = []
    for i in Matrix:
        position_x.append(i[2][0])
        position_y.append(i[2][1])

    center_x = sum(position_x)/len(position_x)
    center_y = sum(position_y)/len(position_y)

    coordinates = np.array([center_x, center_y])

    for i in Matrix:
        vector = np.array(i[2])
        dis = np.linalg.norm(vector-coordinates)
        i.append(dis)

    def taken(item):
        return item[3]

    Matrix.sort(key=taken)

    emotion_list = ['anger', 'disgust', 'fear', 'joy', 'sadness']

    if mood in emotion_list:
        index = emotion_list.index(mood)
        emotion_dic = {}
        # for j in range(0.01, round(radius, 2), 0.01):
        for j in np.arange(0.01, round(radius+0.01, 2), 0.01):
            if j <= radius:
                j = round(j, 2)
                emotion_num = []
                for i in Matrix:
                    dis = i[3]
                    if j-0.01 <= dis:
                        if dis <= j:
                            emotion_num.append(i[0][index])
                        else:
                            break
                    else:
                        pass
            else:
                break

            try:
                emotion_dic[str(round(j-0.01, 2))+'~'+str(round(j, 2))
                            ] = sum(emotion_num)/len(emotion_num)
            except:
                print("Fail")

    else:
        print("The emotion you choose is invalid.\n")

    for i in emotion_dic.keys():
        output = "The proportion of %s in the %s area is %.6f." % (
            mood, i, emotion_dic[i])
        output_matrix.append(output)

    return output_matrix

if __name__ == '__main__':

    Matrix = []

    jieba_set_config()

    file = "C:/Users/DELL/Desktop/Code/BUAA_21/Week3/weibo.txt"

    clean_data = clear_data(file)

    func = analysis1(clean_data)
    Matrix = func(Matrix)

    output = emotion_trend(Matrix, 'sadness', 'day')

    output = emotion_trend(Matrix, 'joy', 'day')

    with open("C:/Users/DELL/Desktop/Code/BUAA_21/Week3/output.txt", 'a+') as f:

        output = emotion_trend(Matrix, 'sadness', 'day')

        print(*output, sep='\n', file=f)

        print('\n\n-----------------------------------------\n\n', file=f)

        output = emotion_trend(Matrix, 'joy', 'day')

        print(*output, sep='\n', file=f)

        print('\n\n-----------------------------------------\n\n', file=f)

        output = emotion_trend(Matrix, "joy", "hour")

        print(*output, sep='\n', file=f)

        print('\n\n-----------------------------------------\n\n', file=f)

        output = emotion_trend(Matrix, "joy", "week")

        print(*output, sep='\n', file=f)

        print('\n\n-----------------------------------------\n\n', file=f)

        # emotion_distance(Matrix, "joy", 0.05)

        output2 = emotion_distance(Matrix, "sadness", 0.07)

        print(*output2, sep='\n', file=f)

        print('\n\n-----------------------------------------\n\n', file=f)

        output2 = emotion_distance(Matrix, "joy", 0.05)

        print(*output2, sep='\n', file=f)

        print('\n\n-----------------------------------------\n\n', file=f)


# Date and Time
import datetime as dt

# contoh
"""
hari_ini = dt.date.today()
print(hari_ini)
print(f"Hari ini adalah hari = {hari_ini:%A}")

tanggal = dt.date(2023,7,30)
print(tanggal)
print(f"Hari ini adalah hari = {tanggal:%A}")
"""

print("Silahkan masukkan tanggal, Bulan dan tahun\n")
tanggal = int(input('Tanggal\t\t:'))
bulan   = int(input('Bulan\t\t:'))
tahun   = int(input('Tahun\t\t:'))

tanggal_lahir = dt.date(tahun, bulan, tanggal)
today = dt.date.today()
umur_hari = today - tanggal_lahir
umur_tahun = umur_hari.days // 365
umur_bulan = (umur_hari.days % 365) // 30

print(f"Tanggal Lahir Anda: {tanggal_lahir}")
print(f"Tanggal Lahir Anda: {tanggal_lahir:%A}")
print(f"Umur Anda (Hari):{umur_hari}")
print(f"Umur Anda (Tahun):{umur_tahun}")
print(f"Umur Anda (Bulan):{umur_bulan}")


# -*- coding: utf-8 -*-
"""
Created on Wed Apr 15 22:08:42 2020

@author: BhaveshAchhada
"""

import os
import time
import cherrypy
import json
from simple_fsm import fsm
import random

class App(object):

    def __init__(self):

        self.is_lift_idle = True
        self.has_car_arrived = False
        self.pack_detected_by_car = False
        self.current_state = None
        self.availability = random.randint(5,10)

    @cherrypy.expose
    def print_hello(self,cargo=None):
        response = {}
        response['output'] = 'Hello' if cargo == None else str(cargo)
        return json.dumps(response)

    def error_state(self, cargo):
        print('Error: ' + str(cargo))
        self.current_state = 'wait'
        return ('wait' , None)

    @cherrypy.expose
    def pack_request(self):
        if self.current_state == 'wait':
            self.current_state = 'pack_request_arrived'
        else:
            return json.dumps({'output' : 'busy'})

    def check_availability(self , cargo=None):

        # return ('available' , None) if self.availability > 0 else ('error' , 'Packs not available.')
        if self.availability > 0:
            self.current_state = 'available'
            out = ('available' , None)
            return out

        self.current_state = 'error'
        return ('error', 'Packs not available')

    def check_lift_status(self, cargo=None):

        # return ('lift_idle' , None) if self.is_lift_idle == True else ('error', 'lift is busy')
        if self.is_lift_idle == True:
            self.current_state = 'lift_idle'
            return ('lift_idle' , None)
        self.current_state = 'error'
        return ('error' , 'lift is busy')

    def check_car_status(self, cargo=None):

        # return ('car_arrived' , None) if self.has_car_arrived == True else ('error' , 'Car not ready')
        if self.has_car_arrived == True:

            self.current_state = 'car_arrived'
            return ('car_arrived' , None)

        self.current_state = 'error'
        return ('error' , 'Car not ready')

    def pick_and_drop(self, cargo = None):
        time.sleep(5)
        self.current_state = 'dropped'
        self.has_car_arrived = False
        return ('dropped' , None)

    def wait(self , cargo = None):
        # print('wait')
        return (self.current_state , None)

    def success(self, cargo = None):
        msg = 'Pack sucessfully dropped'
        print(msg)
        response = {}
        response['status'] = msg

        time.sleep(5)
        self.is_lift_idle = True
        self.current_state = 'wait'
        return ('wait' , None)

    @cherrypy.expose
    def change_car_status(self,status = None):
        if status == 'True':
            self.has_car_arrived = True
        else:
            self.has_car_arrived = False

def Start(obj):
    try:
        # print('in start method.')
        cherrypy.config.update(
            {
                'server.socket_host': '0.0.0.0',
                'server.socket_port': 11000,
                'engine.autoreload.on': False,
                'tools.cors.on': False}
            )
        cherrypy.engine.start()
        cherrypy.tree.mount(obj, '/')
        # cherrypy.engine.block()
    except:
        print('error occurred.')

    obj.current_state = 'wait'
    # print('returning from start')
    return ('wait',None)

if __name__ == '__main__':

    #pass

    obj = App()

    machine = fsm()
    machine.add_state('init', Start)
    machine.add_state('wait' , obj.wait)
    machine.add_state('pack_request_arrived' , obj.check_availability)
    machine.add_state('available' , obj.check_lift_status)
    machine.add_state('error' , obj.error_state, True)
    machine.add_state('lift_idle' , obj.check_car_status)
    machine.add_state('car_arrived' , obj.pick_and_drop)
    machine.add_state('dropped' , obj.success , True)

    # while True:
    machine.run(obj)


# Used in DPLR; convert energy-force dataset to short range dataset by subtracting long range part;
# This code typically runs within a minute on a GPU
# Input a list of source datasets, output a single target dataset
# Source datasets should have the same type index; if not, run the code for each group of sets with the same type index
precision          = 'default'          # 'default'(fp32), 'low'(mixed 32-16), 'high'(fp64)
source_paths       = ['source_dataset_path'] # Path to the source data files
target_path        = 'target_dataset_path'   # Path to the target data file
wannier_model_path = 'model.pkl'        # Path to the model file
q_atoms            = [6, 1]             # charge of atomic cores, here Oxygen and Hydrogen
q_wc               = [-8]               # charge of wannier center/centroid
atomic_sel         = [0]                # type of wannier center/centroid association, here only Oxygen
beta               = 0.4                # inverse spread of the point charge distribution
resolution         = 0.2                # particle mesh grid length = resolution / beta

import numpy as np
import jax.numpy as jnp
from jax import jit, value_and_grad
import jax, datetime
import flax.linen as nn
from functools import partial
from deepmd_jax import data, utils
from deepmd_jax.dpmodel import DPModel
if precision == 'default':
    jax.config.update('jax_default_matmul_precision', 'float32')
if precision == 'high':
    jax.config.update('jax_enable_x64', True)
np.set_printoptions(precision=4, suppress=True)
print('# Program start at', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'on device:', jax.devices()[:1])
model, variables = utils.load_model(wannier_model_path)
dataset = data.DPDataset(source_paths, ['coord','box','force','energy'])
dataset.compute_lattice_candidate(model.params['rcut'])
dataset.pointer = 0
batch, type_count, lattice_args = dataset.get_batch(1)
sel_type_count = tuple(np.array(type_count)[model.params['nsel']])
static_args = nn.FrozenDict({'type_count':type_count, 'lattice':lattice_args})
pred_fn = jit(model.apply, static_argnums=(3,))
qatoms = np.repeat(q_atoms, type_count)
qwc = np.repeat(q_wc, sel_type_count)
def lr_energy(coord, box, M):
    wc = model.wc_predict(variables, coord, box, static_args)
    p3mlr_fn = utils.get_p3mlr_fn(jnp.diag(box), beta, M)
    return p3mlr_fn(jnp.concatenate([coord, wc]), jnp.concatenate([qatoms, qwc])), wc
@partial(jit, static_argnums=(2,))
def lr_energy_and_force(coord, box, M):
    (e, wc), negf = value_and_grad(lr_energy, has_aux=True)(coord, box, M)
    return e, -negf, wc
dataset.pointer = 0
energy_lr, force_lr, wc = [], [], []
for i in range(dataset.nframes):
    M = utils.get_p3mlr_grid_size(np.diag(dataset.data['box'][i]), beta, resolution=resolution)
    e_lr, f_lr, wc_pred = lr_energy_and_force(dataset.data['coord'][i], dataset.data['box'][i], M)
    energy_lr.append(e_lr), force_lr.append(f_lr), wc.append(wc_pred)
energy_lr, force_lr, wc = np.array(energy_lr), np.array(force_lr), np.array(wc)
energy_full, force_full = dataset.data['energy'], dataset.data['force']
energy_sr, force_sr = energy_full - energy_lr, force_full - force_lr
utils.save_dataset(target_path, {'coord':dataset.data['coord'],
                                 'box':dataset.data['box'],
                                 'energy':energy_sr,
                                 'force':force_sr,
                                 'type': np.repeat(range(len(type_count)),type_count)})
# Print mean/std for sanity check
print('Energy (mean,std): Full (%.2f, %.2f) LR (%.2f, %.2f) SR (%.2f, %.2f) Ratio of std %.3f'
    % (energy_full.mean(), energy_full.std(), energy_lr.mean(), energy_lr.std(),
       energy_sr.mean(), energy_sr.std(), energy_sr.std()/energy_full.std()))
print('Force (mean,std): Full (%.2f, %.2f) LR (%.2f, %.2f) SR (%.2f, %.2f) Ratio of std %.3f'
    % (force_full.mean(), force_full.std(), force_lr.mean(), force_lr.std(),
         force_sr.mean(), force_sr.std(), force_sr.std()/force_full.std()))


import torch
from PytorchNetwork import VisionEncoder, VisionDecoder, PositionEstimator, VisionEdgeDecoder
from TrainingFunction import train
from EvaluationFunction import evaluateModel
import matplotlib.pyplot as plt

saveIt = -1
encoderSavePath = "../SavedModel/VisionEncoderModel"
decoderSavePath = "../SavedModel/VisionDecoderModel"
posEstSavePath = "../SavedModel/PositionEstimatorModel"
edgeDecoderSavePath = "../SavedModel/VisionEdgeDecoderModel"

visionEncoderModel = VisionEncoder().cuda()
visionDecoderModel = VisionDecoder().cuda()
positionEstimator = PositionEstimator().cuda()
visionEdgeDecoderModel = VisionEdgeDecoder().cuda()

if (saveIt >= 0):
    visionEncoderModel.load_state_dict(torch.load(encoderSavePath + str(saveIt)))
    visionDecoderModel.load_state_dict(torch.load(decoderSavePath + str(saveIt)))
    positionEstimator.load_state_dict(torch.load(posEstSavePath + str(saveIt)))
    visionEdgeDecoderModel.load_state_dict(torch.load(edgeDecoderSavePath + str(saveIt)))

nbIteration = 1000
batchSize = 50
#lr = 1e-4
t1 = 10000

multitask = {'autoEncoder':True, 'posEstimator':True, 'edgeDecoder':True}

lossList, lossList2, lossList3 = train(visionEncoderModel, visionDecoderModel, positionEstimator, visionEdgeDecoderModel, nbIteration, batchSize, t1, multitask)

torch.save(visionEncoderModel.state_dict(), encoderSavePath + str(saveIt + 1))
torch.save(visionDecoderModel.state_dict(), decoderSavePath + str(saveIt + 1))
torch.save(positionEstimator.state_dict(), posEstSavePath + str(saveIt + 1))
torch.save(visionEdgeDecoderModel.state_dict(), edgeDecoderSavePath + str(saveIt + 1))

plt.plot(lossList)
plt.show()

plt.plot(lossList2)
plt.show()

plt.plot(lossList3)
plt.show()

evaluateModel(visionEncoderModel, visionDecoderModel, visionEdgeDecoderModel)


import torch
import matplotlib.pyplot as plt
from load_data import load_data
import torch.utils.data as Data
from sklearn import preprocessing

torch.manual_seed(1)    # reproducible

# parameters
BATCH_SIZE = 128
EPOCH_NUM = 10

# load data
train_dataset, test_dataset = load_data()

# mini-batch
loader = Data.DataLoader(
    dataset=train_dataset,      # torch TensorDataset format
    batch_size=BATCH_SIZE,      # mini batch size
    shuffle=True,               # random shuffle for training
    num_workers=2,              # subprocesses for loading data
)

# define the model
net = torch.nn.Sequential(
                    torch.nn.Linear(28, 200),
                    torch.nn.ReLU(),
                    torch.nn.Linear(200, 3)
                    # torch.nn.ReLU(),
                    # torch.nn.Linear(100, 3),
)

# define the optimizer
optimizer = torch.optim.Adam(net.parameters(), lr=0.02)

# define the loss
loss_func = torch.nn.CrossEntropyLoss()

if __name__ == '__main__':
    # train the model
    for epoch in range(EPOCH_NUM):
        for step, (batch_x, batch_y) in enumerate(loader):
            out = net(batch_x)
            loss = loss_func(out, batch_y)
            optimizer.zero_grad()  # clear gradients for next train
            loss.backward()  # backpropagation, compute gradients
            optimizer.step()  # apply gradients
        print('epoch: {}, loss: {:.4}'.format(epoch, loss.data.item()))

    test_loader = Data.DataLoader(
        dataset=test_dataset,  # torch TensorDataset format
        batch_size=BATCH_SIZE,  # mini batch size
        shuffle=True,  # random shuffle for training
        num_workers=2,  # subprocesses for loading data
    )

    ## save the model
    PATH = 'D:/科研/碧月课题/代码/BPNN_Baseline/saved_model/model_GY02.pkl'
    torch.save(net, PATH)

    ## evaluate the model
    net.eval()
    eval_loss = 0
    eval_acc = 0
    for batch in test_loader:
        data, label = batch
        out = net(data)
        loss = loss_func(out, label)
        eval_loss += loss.data.item() * label.size(0)
        _, pred = torch.max(out, 1)
        num_correct = (pred == label).sum()
        eval_acc += num_correct.item()
    print('Test Loss: {:.6f}, Acc: {:.6f}'.format(
        eval_loss / (len(test_dataset)),
        eval_acc / (len(test_dataset))
    ))


import sys
import argparse
import math
import numpy as np
import matplotlib.pyplot as plt

class Circle:
    def __init__(self, radius, xc, yc):
        self.radius = radius
        self.xc = xc
        self.yc = yc

    def calculate(self, t):
        angle = 2 * math.pi * t
        x = math.cos(angle) * self.radius + self.xc
        y = math.sin(angle) * self.radius + self.yc
        return x, y

class Ellipse:
    def __init__(self, radius_a, radius_b, xc, yc):
        self.radius_a = radius_a
        self.radius_b = radius_b
        self.xc = xc
        self.yc = yc

    def calculate(self, t):
        angle = 2 * math.pi * t
        x = math.cos(angle) * self.radius_a + self.xc
        y = math.sin(angle) * self.radius_b + self.yc
        return x, y

def circle(t, radius, center):
    angle = 2 * math.pi * t
    x = math.cos(angle)*radius + center[0]
    y = math.sin(angle)*radius + center[1]
    return x, y

def ellipse(t, a, b, center):
    angle = 2 * math.pi * t
    x = math.cos(angle)*a + center[0]
    y = math.sin(angle)*b + center[1]
    return x, y

def create_points(steps, function):
    points = []
    for i in range(steps):
        t = np.random.random()
        point = list((function.calculate(t)))
        points.append(point)

    return points

def create_points_ordered(begin, end, step, function):
    points = []
    t = begin
    while True:
        if t >= end:
            break
        point = list((function.calculate(t)))
        points.append(point)
        t += step

    return points

def shuffle_points(points):
    permutation = np.random.permutation(len(points))
    new_points = []
    for i in range(len(points)):
        new_points.append(points[permutation[i]])

    return new_points

def save_points(points, file_name):
    file = open(file_name, 'w')
    sample_number = len(points)
    var_number = len(points[0])

    file.write("{0} {1}\n".format(sample_number, var_number))
    for i in range(sample_number):
        line = " ".join([str(x) for x in points[i]])
        file.write(line + "\n")

    file.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Implicit function generator")
    parser.add_argument('--lower_bound', '-L', type=int,
                        help='lower bound integer for variable')
    parser.add_argument('--upper_bound', '-U', type=int,
                        help='upper bound integer for variable')
    parser.add_argument('--steps', '-S', type=int)
    parser.add_argument('--step_begin', '-SB', type=float)
    parser.add_argument('--step_end', '-SE', type=float)
    parser.add_argument('--step_step', '-SS', type=float)
    parser.add_argument('--method', '-M', default='circle', choices=['circle', 'ellipse'])
    parser.add_argument('--radius', '-R', type=float)
    parser.add_argument('--radius_a', '-RA', type=float)
    parser.add_argument('--radius_b', '-RB', type=float)
    parser.add_argument('--center_x', '-xc', type=float)
    parser.add_argument('--center_y', '-yc', type=float)
    parser.add_argument('--points_number', '-N', type=int)
    parser.add_argument('--shuffle')
    parser.add_argument('--save')

    args = parser.parse_args()
    function = None
    if args.method:
        if args.method.lower() == 'circle':
            if not args.radius or (not args.center_x and args.center_x != 0.0) or (not args.center_y and args.center_y != 0.0):
                print("You haven't provided all arguments!")
                exit(1)
            function = Circle(args.radius, args.center_x, args.center_y)
        elif args.method.lower() == 'ellipse':
            if not args.radius_a or not args.radius_b or not args.center_x or not args.center_y:
                print("You haven't provided all arguments!")
                exit(1)
            function = Ellipse(args.radius_a, args.radius_b, args.center_x, args.center_y)
        else:
            print('Method not supported!')
            exit(1)

    points = None
    if args.steps:
        points = create_points(args.steps, function)
    elif not args.step_begin or not args.step_end or not args.step_step:
        points = create_points_ordered(args.step_begin, args.step_end, args.step_step, function)
    else:
        print("Step not configured well!")
        exit(1)

    if args.shuffle:
        points = shuffle_points(points)

    if args.save:
        save_points(points, args.save)


import io
import sys
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, datediff, udf
from pyspark.sql import functions as F

# определяем является ли тип объявления CPM
func_is_cpm = udf(lambda x: 1 if x == 'CPM' else 0)

# определяем является ли тип объявления CPC
func_is_cpc = udf(lambda x: 1 if x == 'CPC' else 0)

# добавляем колонку day_count
def func_day_count_add(input_file):
    # создаем DF - группируем исходный по ad_id, для событий с просмотрами,
    # находим для каждого ad_id timestamp первого и последнего просмотров
    df_timestamps = input_file.where(col('event') == 'view').groupBy('ad_id') \
        .agg(F.min('date').alias('timestamp_first_view'), \
             F.max('date').alias('timestamp_last_view'))

    # добавляем к созданному DF колонку с количеством дней, когда показывалась реклама,
    # считаем разницу в днях между первым и последним показом
    df_day_count = df_timestamps.withColumn('day_count', datediff(col('timestamp_last_view'), \
                                                                  col('timestamp_first_view')))

    # делаем outer join исходного DF и колонки day_count из нового DF по ad_id, в результате для некоторых объявлений
    # в колонке day_count будут Null значения, что является маркером того, что для данного ad_id
    # в БД отражены события связанные с кликами
    df_joined = input_file.join(df_day_count, ['ad_id'], how='outer').orderBy('day_count').fillna({'day_count': 0})

    return df_joined

# добавляем колонку CTR
def func_CTR_add(input_file):
    # создаем DF - группируя исходный DF по ad_id и считаем количество кликов для каждого
    df_clicks = input_file.where(col('event') == 'click').groupBy('ad_id').agg(F.count('event').alias('clicks_number'))

    # создаем DF - группируя исходный DF по ad_id и считаем количество показов для каждого
    df_views = input_file.where(col('event') == 'view').groupBy('ad_id').agg(F.count('event').alias('views_number'))

    # объединяем полученные DF-мы с исходным DF (т.е. добавляем 2 новые колонки - клики и показы), null в соответствующих
    # колонках заполняем 0-ми
    df_joined = input_file.join(df_clicks, ['ad_id'], how='outer') \
        .join(df_views, ['ad_id'], how='outer') \
        .fillna({'clicks_number': 0, 'views_number': 0})

    # добавляем колонку CTR к итоговому DF и переопределяем его с тем же именем
    df_joined = df_joined.withColumn('CTR', col('clicks_number') / col('views_number'))

    return df_joined

# последовательно добавляем все новые колонки, получаем итоговый DF
def transform_pipeline(input_file):
    # добавляем колонку day_count
    new_input_file = func_day_count_add(input_file)

    # добавляем колонку CTR
    new_input_file = func_CTR_add(new_input_file)

    # добавляем колонки is_cpm и is_cpc
    new_input_file = new_input_file.withColumn('is_cpm', func_is_cpm(col('ad_cost_type'))) \
        .withColumn('is_cpc', func_is_cpc(col('ad_cost_type')))

    return new_input_file

# делаем разбиение, записываем результат по соответствующему пути
def get_result(DF, path):
    splits = DF.randomSplit([0.5, 0.25, 0.25], seed=0)

    splits[0].coalesce(1).write.option('header', 'true').parquet(str(path) + '/train')
    splits[1].coalesce(1).write.option('header', 'true').parquet(str(path) + '/test')
    splits[2].coalesce(1).write.option('header', 'true').parquet(str(path) + '/validate')

def process(spark, input_file, target_path):

    input_file = spark.read.parquet('input_file')

    DF = transform_pipeline(input_file)[
        ['ad_id',
         'target_audience_count',
         'has_video',
         'is_cpm',
         'is_cpc',
         'ad_cost',
         'day_count',
         'CTR']
    ]

    get_result(DF.distinct(), 'target_path')

def main(argv):
    input_path = argv[0]
    print("Input path to file: " + input_path)
    target_path = argv[1]
    print("Target path: " + target_path)
    spark = _spark_session()
    process(spark, input_path, target_path)

def _spark_session():
    return SparkSession.builder.appName('PySparkJob').getOrCreate()

if __name__ == "__main__":
    arg = sys.argv[1:]
    if len(arg) != 2:
        sys.exit("Input and Target path are require.")
    else:
        main(arg)


from pathlib import Path
from typing import Any, Dict

from tools.lambda_handler import LambdaEvent, LambdaResponse, LambdaResponseData, get_dummy_context
from tools.lambda_handler.lambda_event_base import LambdaEventBase
from tools.logger import Logger

from external_api.utils.external_email_template import ExternalEmailTemplate

class SummaryEmailsHandler(ExternalEmailTemplate):
    name = __name__
    sql_path: Path = Path(__file__).resolve().parent / "sql"
    event: LambdaEvent

    @property
    def job_name(self) -> str:
        return "risk_summary_emails"

    def compute_risk_metrics(self) -> Dict[str, Any]:
        """Compute risk metrics for project"""
        summary_query = """
        SELECT
            risk_type_id,
            COUNT(DISTINCT risk_id) AS risk_count,
            SUBSTRING_INDEX(
                GROUP_CONCAT(
                    DISTINCT risk_description
                    ORDER BY risk_incident_date DESC
                    SEPARATOR '|'
                ), '|', 1
            ) AS risk_desc,
            MAX(severity) AS severity
        FROM
            top_risks
        WHERE
            (dt_created > %(yesterday)s)
            AND (status IS NULL OR status = 'active')
        ORDER BY 1
        """
        daily_risks = self.run_read_query(
            query_string=summary_query,
            query_params={
                "yesterday": self.yesterday.date(),
            },
        )

        risk_metrics = self.run_read_query(
            file_path=self.sql_path / "risks_summary_base.sql",
            query_params={
                "yesterday": self.yesterday.date(),
            },
        )

        return {"risk_metrics": risk_metrics, "daily_risks": daily_risks}

    def get_response(self, event: LambdaEventBase) -> LambdaResponseData:
        if self.configs.status == "active":
            self.send_user_emails(self.compute_risk_metrics())

        return {}

def main_handler(event: Dict[str, Any], context: Any) -> LambdaResponse:
    Logger.main(formatter=Logger.JSONFormatter())
    return SummaryEmailsHandler(enable_sentry=True, raise_errors=False).handle(event, context)

def main() -> None:
    context = get_dummy_context()
    context.function_name = "risks-summary-emails"
    test_event = {
        "project_id": "thoughtlabs",
    }
    main_handler(event=test_event, context=context)

##########################
if __name__ == "__main__":
    main()


"""API for managing channel memberships"""
import logging
import operator
from functools import reduce

from django.contrib.auth import get_user_model

from channels.api import get_admin_api
from channels.models import Channel
from profiles.filters import UserFilter
from profiles.models import Profile

log = logging.getLogger()
User = get_user_model()

def update_memberships_for_managed_channels(*, channel_ids=None, user_ids=None):
    """
    Update channels that are managed and have channel memberhip configs

    Args:
        channel_ids (list of int):
            optional list of channel ids to generate memberships for
        user_ids (list of int):
            optional list of user ids to filter to
    """
    # channels with a membership config
    channels = Channel.objects.filter(
        membership_is_managed=True, channel_membership_configs__isnull=False
    )

    if channel_ids:
        channels = channels.filter(id__in=channel_ids)
    for channel in channels:
        update_memberships_for_managed_channel(channel, user_ids=user_ids)

def update_memberships_for_managed_channel(channel, *, user_ids=None):
    """
    Update the channel memberships for a given channel.
    If the channel is not managed, nothing happens.

    Args:
        channel (Channel):
            the channel to generate memberships for
        user_ids (list of int):
            optional list of user ids to filter to
    """
    if (
        not channel.membership_is_managed
        or not channel.channel_membership_configs.exists()
    ):
        log.debug(
            "update_managed_channel_membership() called for a channel"
            "that is not managed and/or has no channel membership configs: %s",
            channel.name,
        )
        return

    admin_api = get_admin_api()
    active_users = User.objects.filter(is_active=True)

    # create a list of user queries as generated by UserFilter
    filtered_user_queries = [
        UserFilter(config.query, queryset=active_users).qs
        for config in channel.channel_membership_configs.all()
    ]
    # bitwise OR the queries together so that users that match ANY of them are added to the channel
    users_in_channel = reduce(
        operator.or_,
        filtered_user_queries,
        # specify an initial empty query in case we hit a race condition and
        # `filtered_user_queries` ended up empty, otherwise `reduce()` would fail
        User.objects.none(),
    )

    # ensure that we're not about to select all users, because this is almost definitely not what we want
    # this can happen if the query configs didn't match ANY filter options
    # this is a bit of a hack to protect this, but it's sufficient for now
    if str(users_in_channel.query) == str(active_users.query):
        log.error(
            "Membership query configs for channel '%s' result in all active users being added, this is likely not desired",
            channel.name,
        )
        return

    # filter here, rather than earlier
    # this ensure the check above works in all cases
    if user_ids:
        users_in_channel = users_in_channel.filter(id__in=user_ids)

    for user in users_in_channel.only("username").distinct():
        try:
            admin_api.add_contributor(user.username, channel.name)
            admin_api.add_subscriber(user.username, channel.name)
        except Profile.DoesNotExist:
            log.exception(
                "Channel %s membership update failed due to missing user profile: %s",
                channel.name,
                user.username,
            )


import requests
import getthreedigit
from bs4 import BeautifulSoup
col=0
def vtu_results():
    url="http://result.vtu.ac.in/cbcs_results2016.aspx"
    col=0
    i=1
    w=1
    for Z in ['ME','IS','CS']:

        for e in range(80,200):
            student = requests.get(url+str("?usn=1rn15")+Z+str(getthreedigit.digit(e))+str("&sem=2")).text
            soup = BeautifulSoup(student, "html.parser")
            print("USN:1RN15" +Z+ str(getthreedigit.digit(e)))

            names = soup.findAll("input", {"name": "txtName"})
            for n in names:
                print(n['value'])

            for rows in soup.findAll('tr'):
                for columns in soup.findAll('td'):
                    for columns in soup.findAll('input', {"id": ("txtCP"+ str(i))}):
                        if (w==1):
                            print ("Mathematics:"+columns['value'])
                        elif (w==2):
                            print("Chemistry:" + columns['value'])
                        elif (w==3):
                            print("PCD:" + columns['value'])
                        elif(w==4):
                            print("CAED:" + columns['value'])
                        elif(w==5):
                            print("Electronics:" + columns['value'])
                        elif(w==6):
                            print("Comp Programming Lab:" + columns['value'])
                        elif(w==7):
                            print("Chemistry Lab:" + columns['value'])

                        col= col + int(columns['value'])
                        w+=1
                        i+=1
                        if (i>7):
                            break
            w=1
            i=1
            print("Total Credits Earned=" + str(col) )
            P = col / 24.00
            SGPA = ("%0.2f" % P )
            print("SGPA:" + str(SGPA)+ "\n\n")
            col = 0

vtu_results()


#!/usr/bin/env python3

import logging
from time import sleep
from collections import namedtuple

from dns.resolver import query
import dns
import configargparse

Status = namedtuple("Status", ["name", "ip"])

def set_up_log(filename="/var/log/watch_vpn_dns.log", level="WARN"):
    """
    Accept optional filename, level.
    Create a logger.
    """
    numeric_level = getattr(logging, level.upper(), None)
    if not isinstance(numeric_level, int):
        raise ValueError("Invalid log level: %s" % level)

    logging.basicConfig(
        filename=filename,
        format="%(asctime)s %(levelname)-8s %(message)s",
        level=numeric_level,
        datefmt="%Y%m%dT%H:%M:%S",
    )

def _get_status_lines(status_files):
    """
    Accept status files (list of file-like objects),
    Return generator of lines starting with "CLIENT_LIST,"
    """
    for status_file in status_files:
        with open(status_file) as fileh:
            for line in fileh:
                if line.startswith("CLIENT_LIST,"):
                    yield line.strip()

def _parse_status(status_line):
    """
    Accept status line (str).
    Return namedtuple Status of name and ip.
    """
    name_field = 1
    ip_field = 3
    fields = status_line.split(",")
    return Status(name=fields[name_field], ip=fields[ip_field])

def ips_from_vpn(status_files):
    """
    Accept status_files (list of file-like objects).
    Return dict of name: ip
    """
    status_lines = _get_status_lines(status_files)
    statuses = [_parse_status(line) for line in status_lines]
    return {
        status.name: status.ip
        for status in statuses
        if status.name != "UNDEF" and status.ip != ""
    }

def dig_record(name, domain):
    """
    Accept name (str),
    domain (str).
    Return ip of name's A record in domain,
    or None if there is none.
    """
    record = name + domain
    try:
        answer = query(record, "A")[0].to_text()
    except dns.resolver.NXDOMAIN:
        answer = None
    logging.debug(f"{name}{domain} returned {answer}")
    return answer

def ips_from_dns(names, domain):
    """
    Accept names (list of common_names),
    domain (str).
    Return dict of common_name: ip.
    """
    return {name: dig_record(name, domain) for name in names}

def diff(what_should_be, what_is):
    """
    Accept what_should_be (dict of name: ip)
    and what_is (dict of name: ip)
    Return dict of name: ip with ip from 'what_should_be' where 'what_is' does
    not match 'what_should_be'.
    """
    return {
        name: ip
        for name, ip in what_should_be.items()
        if what_should_be[name] != what_is.get(name, None)
    }

def send_to_pipe(fifo, to_update):
    """
    Accept fifo (file-like object) and to_update (dict of common_name, ip).
    Send to_update as key:value pair to fifo.
    Return True on success,
    otherwise raise Exception.
    """
    logging.info("Updating: " + str(to_update))
    with open(fifo, "w") as pipe:
        for name, ip in to_update.items():
            pipe.write(name + "," + ip + "\n")

def reconcile_dns(status_files, fifo, domain):
    """
    Accept status files (list of file-like objects),
    fifo (a file, presumable a pipe, to write to),
    domain (str).
    Send DNS records for update to pipe based  n VPN status.
    """
    from_vpn = ips_from_vpn(status_files)
    logging.debug(f"IPs should be: {from_vpn}")
    from_dns = ips_from_dns(from_vpn.keys(), domain)
    logging.debug(f"IPs from dns: {from_dns}")
    to_update = diff(from_vpn, from_dns)
    logging.debug(f"To update: {to_update}")
    if to_update:
        send_to_pipe(fifo, to_update)

def main():
    """
    Parse arguments, initialize logging, and enter main loop.
    """
    parser = configargparse.ArgumentParser(
        default_config_files=["/etc/watch-vpn-dns.conf"]
    )
    parser.add_argument(
        "-c",
        "--config",
        help="Use config file CONFIG",
        is_config_file=True,
        default="/etc/watch-vpn-dns.conf",
        action="store",
        dest="config",
    )
    parser.add_argument(
        "-L",
        "--log-file",
        help="Use log-file LOG_FILE",
        default="/var/log/watch_vpn_dns.log",
        action="store",
        dest="log_file",
    )
    parser.add_argument(
        "-l",
        "--log-level",
        help="Log at level LOG_LEVEL, one of NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL",
        default="INFO",
        action="store",
        dest="log_level",
    )
    parser.add_argument(
        "-f",
        "--file",
        help="write to FILE (most likely a fifo)",
        default="/var/run/vpn-dydns.fifo",
        action="store",
        dest="fifo",
    )
    parser.add_argument(
        "-s",
        "--status_file",
        help="read from status file(s) STATUS",
        action="append",
        dest="status",
    )
    parser.add_argument(
        "-d",
        "--domain",
        help="append domain DOMAIN to incoming hostnames, eg: example.com",
        default="",
        action="store",
        dest="domain",
    )
    parser.add_argument(
        "-w",
        "--wait-time",
        help="wait TIME seconds between posting records to route53",
        type=int,
        default=60,
        action="store",
        dest="wait_time",
    )

    args = parser.parse_args()
    set_up_log(filename=args.log_file, level=args.log_level)

    logging.info(args)
    while True:
        reconcile_dns(args.status, args.fifo, args.domain)
        sleep(args.wait_time)

if __name__ == "__main__":
    main()


import pygame
# Import the game settings
import settings

class Bullet(pygame.sprite.Sprite):
    """
    Create a Bullet object

    Attributes
    ----------
    image : pygame.Surface
        The surface of the object
    rect : pygame.Surface().get_rect
        The rectangle of the object
    sound : pygame.mixer.Sound
        The sound played when the bullet hits another object

    Methods
    -------
    update()
        Updates the state of the bullet
    """
    def __init__(self, pos_x: int, pos_y: int):
        super().__init__()
        """
        Parameters
        ----------
        pos_x : int
            The position of the bullet on the x-axis
        pos_y : int
            The position of the bullet on the y-axis
        """

        self.image = pygame.Surface((6, 31)).convert_alpha()
        pygame.transform.scale(pygame.image.load(r"imgs\BasicAttackBullet.png").convert_alpha(), (6, 31), self.image)
        self.rect = self.image.get_rect(center=(pos_x, pos_y))

        self.sound = pygame.mixer.Sound(settings.SOUNDS["hit"])
        self.sound.set_volume(0.4)

    def update(self) -> None:
        """
        Update the state of the bullet.
        """
        self.rect.y -= 20  # move the bullet up the screen
        # Check if the bullet collides with any other object on the screen
        for sprite in settings.enemy_sprite_group:
            if self.rect.colliderect(sprite.rect) and sprite.health > 0:
                # If it does collide, play the hit sound
                self.sound.play()
                # If the bullet destroys the object, add +1 to the player's score
                if sprite.health - 1 == 0:
                    settings.player_score += 1
                # Decrease the health points of the object the bullet collides with
                sprite.health -= 1
                # Destroy the bullet
                self.kill()
        # If the bullet leaves the screen, destroy it
        if self.rect.y < -90:
            self.kill()


import requests, wikipedia

TDMB = 'API KEY' #Get your own api key https://www.themoviedb.org/

def find_my_ip():
    ip_address = requests.get('https://api64.ipify.org?format=json').json()
    return ip_address["ip"]

def get_trending_movies():
    trending_movies = []
    res = requests.get(
        f"https://api.themoviedb.org/3/trending/movie/day?api_key=" + TDMB).json()
    results = res["results"]
    for r in results:
        trending_movies.append(r["original_title"])
    return trending_movies[:5]

def wikipediaa(query, sentencess):
    try:
        query = query.replace("wikipedia", "")
        results =  wikipedia.summary(query, sentences = sentencess)
        output = f"Wikipedia says, {results}"
        print(results)
        return output
    except wikipedia.exceptions.PageError as e:
        x = query.split(' wikipedia')
        print(f'No result found about {x}')


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

import sys
import os

import lxml

import gi
gi.require_version("Gio", "2.0")
from gi.repository import Gio, GLib

if __name__ == '__main__':
    try:
       bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
    except BaseException:
       # print("ERROR: No DBus bus", file=sys.stderr)
       exit()
    # print ("INFO: Got DBus bus", file=sys.stderr)
    proxy = Gio.DBusProxy.new_sync(bus, Gio.DBusProxyFlags.NONE, None,
                                  'org.freedesktop.DBus',
                                  '/org/freedesktop/DBus',
                                  'org.freedesktop.DBus', None)
    names_list = proxy.call_sync('ListNames', None, Gio.DBusCallFlags.NO_AUTO_START, 500, None)
    # names_list is a GVariant, must unpack
    names = names_list.unpack()[0]
    # Look for Inkscape; names is a tuple.
    for name in names:
       if ('org.inkscape.Inkscape' in name):
           # print ("INFO: Found: " + name, file=sys.stderr)
           break

    appGroupName = "/org/inkscape/Inkscape"
    winGroupName = appGroupName + "/window/1"
    docGroupName = appGroupName + "/document/1"

    applicationGroup = Gio.DBusActionGroup.get( bus, name, appGroupName)
    windowGroup = Gio.DBusActionGroup.get(bus, name, winGroupName)
    documentGroup = Gio.DBusActionGroup.get(bus, name, docGroupName)

    applicationGroup.activate_action('element-image-edit', None)


import os
import datetime
import glob

import matplotlib.pyplot as plt
import pandas as pd

from analysis.output import OUTPUT_DATA_SPEC as cols

def prepare_data(filepaths, labels):
    keys = ['buy', 'rent', 'wage', 'no_policy']
    database = dict()
    for key in keys:
        database[key] = pd.DataFrame()
        for each in filepaths:
            this_key = each.split('=')[1].split('\\')[0]
            if key == this_key:
                d = pd.read_csv(each, header=None, sep=';', names=labels)
                d['month'] = pd.to_datetime(d.month).dt.date
                database[key] = database[key].append(d[d.loc[:, 'month'] > datetime.date(2011, 1, 1)])
    return database

def plot(database, lbls, path, dpi=720, ft='png'):
    colors = {'buy': 'tab:red', 'no_policy': 'tab:orange', 'rent': 'tab:blue', 'wage': 'tab:green'}
    fmts = {'gdp_index': '{:.0f}',
            'gini_index': '{:.3f}',
            'average_qli': '{:.3f}',
            'house_price': '{:.0f}',
            'pct_zero_consumption': '{:.3f}',
            'rent_default': '{:.3f}'}

    for lb in lbls:
        if lb in ['gdp_index', 'gini_index', 'average_qli', 'house_price', 'rent_default',
                  'pct_zero_consumption']:
            if lb not in ['month']:
                fig, ax = plt.subplots(dpi=dpi)
                for i, data in enumerate(database):
                    ax.plot(database[data]['avg']['month'], database[data]['avg'][lb], linewidth=.2,
                            color=colors[data], label=data, alpha=.6)
                    ax.plot(database[data]['upper_table']['month'], database[data]['upper_table'][lb], linewidth=.5,
                            color=colors[data])
                    ax.plot(database[data]['upper_table']['month'], database[data]['lower_table'][lb], linewidth=.5,
                            color=colors[data])
                    ax.fill_between(pd.to_datetime(database[data]['avg']['month']),
                                    database[data]['upper_table'][lb],
                                    database[data]['lower_table'][lb],
                                    facecolor=colors[data], alpha=.5)
                ax.spines['top'].set_visible(False)
                ax.spines['bottom'].set_visible(True)
                ax.spines['right'].set_visible(False)
                ax.spines['left'].set_visible(True)
                ax.get_xaxis().tick_bottom()
                ax.get_yaxis().tick_left()
                # ax.get_legend().remove()
                fmt = fmts[lb] if lb in fmts else '{:.4f}'
                ax.yaxis.set_major_formatter(plt.FuncFormatter(fmt.format))
                ax.set(xlabel='Year')
                plt.grid(True, 'major', 'y', ls='-', lw=.5, c='k', alpha=.3)

                plt.tick_params(axis='both', which='both', bottom=False, top=False,
                                labelbottom=True, left=False, right=False, labelleft=True)
                n = os.path.join(path, f'{lb}.{ft}')
                plt.savefig(f"output/{lb}_R1.{ft}")
                # plt.savefig(n)
                plt.show()
                plt.close(fig)

def transform_into_avg_quartiles(database):
    data = dict()
    for key in database:
        data[key] = dict()
        data[key]['avg'] = database[key].groupby(by='month').agg('mean').reset_index()
        data[key]['upper_table'] = database[key].groupby(by='month').quantile(.75).reset_index()
        data[key]['lower_table'] = database[key].groupby(by='month').quantile(.25).reset_index()
    return data

def organizing_files_avg_policy(path):
    return [f for f in glob.glob(path + '/**/**/temp_stats.csv', recursive=True) if 'avg' not in f]

if __name__ == '__main__':
    p = r'\\storage1\carga\modelo dinamico de simulacao' \
        r'\Exits_python\PS2020\POLICIES__2021-02-19T22_39_56.035769'
    # r'\Exits_python\PS2020\POLICIES__2021-02-25T11_28_10.744348'

    pths = organizing_files_avg_policy(p)
    dat = prepare_data(pths,  cols['stats']['columns'])
    dat = transform_into_avg_quartiles(dat)
    plot(dat,  cols['stats']['columns'], p)


import scrapy
import logging

from scraper.spider_utils import (SIA_URL, get_airac_date_from_arg,
                                  make_root_url, get_pretty_airac_date, extract_field_value_from_row)
from ..items import AirfieldMapItem

logger = logging.getLogger('django')

INDEX_URL = "FR-AD-1.3-fr-FR.html"
ROW_SELECTOR = "tr[id*='ICAO']"
ROW_ICAO_CODE_SELECTOR = "span[id*=CODE_ICAO]"

NO_VAC_AIRFIELD_LIST = ['LFOA', 'LFOE', 'LFBC', 'LFBM', 'LFXQ', 'LFPC',
                        'LFKK', 'LFSX', 'LFSI', 'LFKS', 'LFSO', 'LFPR', 'LFMO', 'LFPV']
WITH_VACH_AIRFIELD_LIST = ['LFBD', 'LFBO', 'LFBP', 'LFBZ', 'LFLJ', 'LFLY',
                           'LFMD', 'LFML', 'LFMN', 'LFMT', 'LFPB', 'LFPG',
                           'LFPI', 'LFPN', 'LFPO', 'LFPZ', 'LFRS', 'LFSB']
MISSING_AIRFIELD_LIST = ['LFPI']

VAC_MAP_URL_TEMPLATE = "https://www.sia.aviation-civile.gouv.fr/dvd/eAIP_{}/Atlas-VAC/PDF_AIPparSSection/VAC/AD/AD-2.{}.pdf"
VACH_MAP_URL_TEMPLATE = "https://www.sia.aviation-civile.gouv.fr/dvd/eAIP_{}/Atlas-VAC/PDF_AIPparSSection/VACH/AD/AD-3.{}.pdf"

# scrapy crawl vfr_maps -a airac=2021-03-25

# There is no easily parsable index of VAC/VACH map files
# So we guess their urls from the airfield list in AD1.3

class VFRMapsSpider(scrapy.Spider):
    name = "vfr_maps"
    allowed_urls = [SIA_URL]

    def start_requests(self):
        airac_date = get_airac_date_from_arg(self)
        self.root_url = make_root_url(airac_date)
        yield scrapy.Request(url="{}{}".format(self.root_url, INDEX_URL),
                             callback=self.parse,
                             cb_kwargs={'airac': airac_date})

    def parse(self, response, airac):
        # extract airfields from AD1.3.1 and download VAC and VACH maps
        failed_airfields = []
        for row in response.css(ROW_SELECTOR):
            icao_code = extract_field_value_from_row(
                row, ROW_ICAO_CODE_SELECTOR).get()
            # !! do not parse helistations
            if icao_code and icao_code.startswith("LF") and icao_code not in NO_VAC_AIRFIELD_LIST:
                with_vach = icao_code in WITH_VACH_AIRFIELD_LIST
                pretty_airac_date = get_pretty_airac_date(airac)
                yield AirfieldMapItem(
                    airac=airac, airfield=icao_code, name="VAC", file_urls=[VAC_MAP_URL_TEMPLATE.format(pretty_airac_date, icao_code)])
                if with_vach:
                    yield AirfieldMapItem(
                        airac=airac, airfield=icao_code, name="VACH", file_urls=[VACH_MAP_URL_TEMPLATE.format(pretty_airac_date, icao_code)])
        logger.error("Parse errors for airfields : {}".format(
            ', '.join(failed_airfields)))
        for a in MISSING_AIRFIELD_LIST:
            yield from self.parse_vac_maps(airac, a)

    def parse_vac_maps(self, airac, icao_code):
        no_vac = not icao_code or icao_code in NO_VAC_AIRFIELD_LIST
        with_vach = icao_code in WITH_VACH_AIRFIELD_LIST
        pretty_airac_date = get_pretty_airac_date(airac)
        if not no_vac:
            yield AirfieldMapItem(airac=airac, airfield=icao_code, name="VAC", file_urls=[VAC_MAP_URL_TEMPLATE.format(pretty_airac_date, icao_code)])
            if with_vach:
                yield AirfieldMapItem(airac=airac, airfield=icao_code, name="VACH", file_urls=[VACH_MAP_URL_TEMPLATE.format(pretty_airac_date, icao_code)])


from django.urls import path, include

from .views import *

from mainapp.apps import MainappConfig

app_name = MainappConfig.name

urlpatterns = [
    path("", MainPageView.as_view(), name='main'),
    path("doc_site/", DocSitePageView.as_view(), name='docs'),
    path("contacts/", ContactsPageView.as_view(), name='contacts'),
    path("news/", NewsPageView.as_view(), name='news'),
    path("news/<int:pk>/", NewsPageDetailView.as_view(), name="news_detail"),
    path("courses_list/", CoursesPageView.as_view(), name='courses'),
    path("courses/<int:pk>/", CoursesDetailView.as_view(), name="courses_detail"),
]


import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import gsq.ci_tests as gsq
from itertools import chain, combinations
import logging
logging.basicConfig()

#The function for conditional indepenence test.
# The value 1 means conditional indepenence
def citest(data,x,y,z,threshold):
    p=gsq.ci_test_dis(data,x,y,z)
    if p>threshold:
        value=1
    else:
        value=0
    return value

#The function for finding the markov blanket of node X
#Return a list S
def markovblanket(Xnodeindex,data,threshold):
    S=[]
    for Ynodeindex in range(0,data.shape[1]):
       if Ynodeindex!=Xnodeindex:
          Value=citest(data,Xnodeindex,Ynodeindex,S,threshold)
          if Value == 0:
              S.append(Ynodeindex)

    for Index in S:
        if len(S)>1:
            Sy=S
            Sy.remove(Index)
        elif len(S) == 1:
            Sy=[]
        Value=citest(data,Xnodeindex,Index,Sy,threshold)
        if Value == 1:
            S.remove(Index)

    return S

#The function for finding a smaller between two sets X and Y
def Findsmallset(X,Y):
    if (X==None) or (Y==None):
        T=[]
    elif len(X)<len(Y):
        T=X
    else:
        T=Y
    return T

#The function for finding all the subsets for a set S
def Subset(S):
  xs = list(S)
  return chain.from_iterable(combinations(xs,n) for n in range(len(xs)+1))

#The function for finding all the neighbour
#return N as a dictionary N={node: its neighbour}
def Neighbor(data,threshold):
    MB={}
    N={}
    for index in range(0,data.shape[1]):
        MB[index]=markovblanket(index,data,threshold)

    for Xindex in range(0,data.shape[1]):
        MBX=MB[Xindex]
        for Yindex in MBX:
            MBY=MB[Yindex]
            if (Yindex in MBX) and (Xindex in MBY):
                T=Findsmallset(MBX.remove(Yindex), MBY.remove(Xindex))
            else:
                T=Findsmallset(MBX, MBY)
            Tsubset=list(Subset(set(T)))
            v=0
            for S in Tsubset:
                value=citest(data,Xindex,Yindex,S,threshold)
                if value == 1:
                    v=value
            if v==0:
                N[Xindex]=Yindex

    for Xindex in range(0,data.shape[1]):
        if Xindex not in N.keys():
            N[Xindex]=[]

    return N

def main():
    #load dataset
    testdata=np.loadtxt("testdata.csv",delimiter=',')
    threshold=0.05
    #Find all the markov blankets of all nodes
    MB={}
    for index in range(0,testdata.shape[1]):
        MB[index]=markovblanket(index,testdata,threshold)
    N=Neighbor(testdata,threshold)
    Nud=N
    #This part do the orient edge part.Save the direction result in ND
    ND={}
    for Xindex in N.keys():
        for Yindex in MB[Xindex]:
            if N[Yindex]==[] and N[Xindex]==[]:
                Z=set(N[Xindex])-set(N[Yindex])-set([Yindex])
            elif N[Yindex]==[]:
                Z=set([N[Xindex]])-set(N[Yindex])-set([Yindex])
            elif N[Xindex]==[]:
                Z=set(N[Xindex])-set([N[Yindex]])-set([Yindex])
            else:
                Z=set([N[Xindex]])-set([N[Yindex]])-set([Yindex])
            for Zindex in Z:
                TY=list(set(MB[Yindex])-set([Xindex,Zindex]))
                TZ=list(set(MB[Zindex])-set([Xindex,Yindex]))
                T=Findsmallset(TY,TZ)
                Tsubsets=Subset(T)
                for S in Tsubsets:
                    S=list(set(S)+set([Xindex]))
                    value=citest(testdata,Yindex,Zindex,S)
                    if value==0:
                        ND[Xindex]=Yindex

    #If ND is empty,we can draw undirection graph
    if ND == {}:
        G=nx.Graph()
        G.add_nodes_from(range(0,testdata.shape[1]))
        for Xnode,Ynode in Nud.items():
            if Xnode and Ynode !=[]:
                G.add_edge(Xnode,Ynode)

    #If ND is not empty, we can draw direction graph
    else:
        G=nx.DiGraph()
        G.add_nodes_from(range(0,testdata.shape[1]))
        for Xnode,Ynode in ND.items():
            if Xnode and Ynode !=[]:
                G.add_edge(Ynode,Xnode)

    nx.draw(G,with_labels=True)
    plt.show()

if __name__ == "__main__":
    main()


import requests
import json
from config import keys

class ConverterException(Exception):
    pass

class Converter:
    @staticmethod
    def calculate_rate(message):
        data = message.text.split()

        if len(data) != 3:
            raise ConverterException('Ошибка ввода данных.')
        else:
            base, quote, amount = data
            base = base.lower()
            quote = quote.lower()
        try:
            amount = float(amount)

        except ValueError:
            raise ConverterException('Ошибка ввода данных. Сумма должна быть числом.')
        else:
            if amount < 0:
                raise ConverterException('Ошибка ввода данных. Сумма должна быть положительной.')

        if base == quote:
            raise ConverterException(f'Указана одинаковая валюта для пересчета - {base}')
        if base not in keys.keys():
            raise ConverterException(f'Неправильно указано название валюты - {base}')
        if quote not in keys.keys():
            raise ConverterException(f'Неправильно указано название валюты - {quote}')

        path = f'https://min-api.cryptocompare.com/data/price?fsym={keys[base]}&tsyms={keys[quote]}'
        r = requests.get(path).content
        result = json.loads(r)
        final_sum = amount * result[keys[quote]]
        return f'{base} в сумме {amount:.2f} = {quote} в сумме {final_sum:.2f}'


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

import rospy
import sys
import moveit_commander
from moveit_ros_planning_interface import _moveit_move_group_interface
import tf
import copy
import threading
import numpy as np
import json
import cmath
import math
from geometry_msgs.msg import PoseStamped,Pose,PointStamped
from sensor_msgs.msg import JointState

class draw_face_demo:
    def __init__(self):

        rospy.init_node('draw_face_demo',anonymous = True)

        moveit_commander.roscpp_initialize(sys.argv)

        self.arm = moveit_commander.MoveGroupCommander('ur3_arm')

        self.end_effector_link = self.arm.get_end_effector_link()

        self.reference_frame = 'base_link'

        self.arm.allow_replanning(False)

        self.arm.set_max_velocity_scaling_factor(0.5)
        self.arm.set_max_acceleration_scaling_factor(0.5)

        self.arm.set_goal_position_tolerance(0.01)
        self.arm.set_goal_orientation_tolerance(0.05)

        self.arm.set_start_state_to_current_state()

        self.draw_plan = self.arm.plan()
        self.max_fraction_plan = self.arm.plan()

        self.waypoints = []

    def draw_face_test(self):

        self.arm.set_named_target("write")
        self.arm.go()
        rospy.sleep(1.0)

        start_point = self.arm.get_current_pose().pose

        start_pointX = start_point.position.x
        start_pointY = start_point.position.y
        start_pointZ = start_point.position.z

        filename = '/home/cjw/PycharmProjects/Face_Painting/image/draw.txt'

        fraction = 0.0
        max_fraction = 0.0

        write_point = self.arm.get_current_pose().pose

        self.waypoints.append(copy.deepcopy(write_point))
        write_point.position.x += 0.10
        self.waypoints.append(copy.deepcopy(write_point))

        with open(filename,'r') as file_to_read:

            list = []

            while True:

                lines = file_to_read.readline()
                if not lines:
                    break

                item = [j for j in lines.split(",")]
                point_x = json.loads(item[0])
                point_y = json.loads(item[1])

                if point_x != 0 or point_y != 0:

                    point_x = (150 - point_x) / (150*10.0) + write_point.position.y - 0.02
                    point_y = (150 - point_y) / (150*10.0) + write_point.position.z - 0.05

                    # point_x = point_x / (436*10.0) + write_point.position.y - 0.02
                    # point_y = point_y / (452*10.0) + write_point.position.z - 0.05

                list.append([point_x,point_y])
            print(len(list))
            print("!------------------!")
            for k in range(0,len(list)-1,1):

                if list[k][0] == 0 and list[k][1] == 0:
                    write_point.position.x -= 0.02
                    self.waypoints.append(copy.deepcopy(write_point))

                    k += 1

                    if( (list[k][0] != 0.0 and list[k][1] != 0.0) and (list[k+1][0] == 0.0 and list[k+1][1] == 0.0)):
                        print("Only one point , Pass!!!!")
                        k += 2
                            # continue

                    write_point.position.y = list[k][0]
                    write_point.position.z = list[k][1]

                    self.waypoints.append(copy.deepcopy(write_point))

                    write_point.position.x += 0.02
                    self.waypoints.append(copy.deepcopy(write_point))
                else:

                    write_point.position.y = list[k][0]
                    write_point.position.z = list[k][1]

                    self.waypoints.append(copy.deepcopy(write_point))

        write_point.position.x -= 0.02
        self.waypoints.append(copy.deepcopy(write_point))

        for l in range(0,100):

            (self.draw_plan, fraction) = self.arm.compute_cartesian_path(
                                         self.waypoints,   # self.waypoints to follow
                                         0.001,        # eef_step
                                         0.0)         # jump_threshold

            if fraction > max_fraction:
                max_fraction = fraction
                self.max_fraction_plan = self.draw_plan

            if fraction == 1.0:
                break

        print(max_fraction)
        if max_fraction == 1.0:
            self.arm.execute(self.max_fraction_plan,wait=True)
            del self.waypoints[:]
        else:
            del self.waypoints[:]

        rospy.sleep(3.0)

if __name__ == "__main__":

    test = draw_face_demo()
    test.draw_face_test()


import functools
import torch.nn as nn
from torch.optim import lr_scheduler

def get_scheduler(optimizer, opts, cur_ep=-1):
    if opts.lr_policy == 'lambda':
        def lambda_rule(ep):
            lr_l = 1.0 - max(0, ep - opts.n_ep_decay) / float(opts.n_ep - opts.n_ep_decay + 1)
            return lr_l

        scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule, last_epoch=cur_ep)
    elif opts.lr_policy == 'step':
        scheduler = lr_scheduler.StepLR(optimizer, step_size=opts.n_ep_decay, gamma=0.1, last_epoch=cur_ep)
    else:
        return NotImplementedError('no such learn rate policy')
    return scheduler

def meanpoolConv(inplanes, outplanes):
    sequence = []
    sequence += [nn.AvgPool2d(kernel_size=2, stride=2)]
    sequence += [nn.Conv2d(inplanes, outplanes, kernel_size=1, stride=1, padding=0, bias=True)]
    return nn.Sequential(*sequence)

def convMeanpool(inplanes, outplanes):
    sequence = []
    sequence += conv3x3(inplanes, outplanes)
    sequence += [nn.AvgPool2d(kernel_size=2, stride=2)]
    return nn.Sequential(*sequence)

def get_norm_layer(layer_type='instance'):
    if layer_type == 'batch':
        norm_layer = functools.partial(nn.BatchNorm2d, affine=True)
    elif layer_type == 'instance':
        norm_layer = functools.partial(nn.InstanceNorm2d, affine=False)
    elif layer_type == 'none':
        norm_layer = None
    else:
        raise NotImplementedError('normalization layer [%s] is not found' % layer_type)
    return norm_layer

def get_non_linearity(layer_type='relu'):
    if layer_type == 'relu':
        nl_layer = functools.partial(nn.ReLU, inplace=True)
    elif layer_type == 'lrelu':
        nl_layer = functools.partial(nn.LeakyReLU, negative_slope=0.2, inplace=False)
    elif layer_type == 'elu':
        nl_layer = functools.partial(nn.ELU, inplace=True)
    else:
        raise NotImplementedError('nonlinearity activitation [%s] is not found' % layer_type)
    return nl_layer

def conv3x3(in_planes, out_planes):
    return [nn.ReflectionPad2d(1), nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1, padding=0, bias=True)]

def gaussian_weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1 and classname.find('Conv') == 0:
        m.weight.data.normal_(0.0, 0.02)