import cv2
import math

import numpy as np
import paddle

class BaseRecLabelDecode(object):
    """ Convert between text-label and text-index """

    def __init__(self, character_dict_path=None, use_space_char=False):
        self.beg_str = "sos"
        self.end_str = "eos"

        self.character_str = []
        if character_dict_path is None:
            self.character_str = "0123456789abcdefghijklmnopqrstuvwxyz"
            dict_character = list(self.character_str)
        else:
            with open(character_dict_path, "rb") as fin:
                lines = fin.readlines()
                for line in lines:
                    line = line.decode('utf-8').strip("\n").strip("\r\n")
                    self.character_str.append(line)
            if use_space_char:
                self.character_str.append(" ")
            dict_character = list(self.character_str)

        dict_character = self.add_special_char(dict_character)
        self.dict = {}
        for i, char in enumerate(dict_character):
            self.dict[char] = i
        self.character = dict_character

    def add_special_char(self, dict_character):
        return dict_character

    def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
        """ convert text-index into text-label. """
        result_list = []
        ignored_tokens = self.get_ignored_tokens()
        batch_size = len(text_index)
        for batch_idx in range(batch_size):
            char_list = []
            conf_list = []
            for idx in range(len(text_index[batch_idx])):
                if text_index[batch_idx][idx] in ignored_tokens:
                    continue
                if is_remove_duplicate:
                    # only for predict
                    if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
                            batch_idx][idx]:
                        continue
                char_list.append(self.character[int(text_index[batch_idx][
                    idx])])
                if text_prob is not None:
                    conf_list.append(text_prob[batch_idx][idx])
                else:
                    conf_list.append(1)
            text = ''.join(char_list)
            result_list.append((text, np.mean(conf_list)))
        return result_list

    def get_ignored_tokens(self):
        return [0]  # for ctc blank

class CTCLabelDecode(BaseRecLabelDecode):
    """ Convert between text-label and text-index """

    def __init__(self, character_dict_path=None, use_space_char=False,
                 **kwargs):
        super(CTCLabelDecode, self).__init__(character_dict_path,
                                             use_space_char)

    def __call__(self, preds, label=None, *args, **kwargs):
        if isinstance(preds, tuple):
            preds = preds[-1]
        if isinstance(preds, paddle.Tensor):
            preds = preds.numpy()
        preds_idx = preds.argmax(axis=2)
        preds_prob = preds.max(axis=2)
        text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
        if label is None:
            return text
        label = self.decode(label)
        return text, label

    def add_special_char(self, dict_character):
        dict_character = ['blank'] + dict_character
        return dict_character

class TextRecognizer(object):
    def __init__(self, args):
        self.rec_image_shape = [int(v) for v in args.rec_image_shape.split(",")]
        self.rec_batch_num = args.rec_batch_num
        self.postprocess_op = CTCLabelDecode(args.rec_char_dict_path,
                                             args.use_space_char)
        self.use_paddle_predict = args.use_paddle_predict
        self.args = args

        model_dir = args.rec_model_dir
        if args.use_paddle_predict:
            model = paddle.jit.load(model_dir + "/inference")
            model.eval()
            self.predictor = model
        else:
            import onnxruntime as ort
            model_file_path = model_dir
            sess = ort.InferenceSession(model_file_path)
            self.predictor = sess

    def resize_norm_img(self, img, max_wh_ratio):
        imgC, imgH, imgW = self.rec_image_shape
        assert imgC == img.shape[2]
        imgW = int((32 * max_wh_ratio))
        if not self.use_paddle_predict:
            w = self.predictor.get_inputs()[0].shape[3:][0]
            if w is not None and w > 0:
                imgW = w

        h, w = img.shape[:2]
        ratio = w / float(h)
        if math.ceil(imgH * ratio) > imgW:
            resized_w = imgW
        else:
            resized_w = int(math.ceil(imgH * ratio))
        resized_image = cv2.resize(img, (resized_w, imgH))
        resized_image = resized_image.astype('float32')
        resized_image = resized_image.transpose((2, 0, 1)) / 255
        resized_image -= 0.5
        resized_image /= 0.5
        padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
        padding_im[:, :, 0:resized_w] = resized_image
        return padding_im

    def __call__(self, img_list):
        img_num = len(img_list)
        # Calculate the aspect ratio of all text bars
        width_list = []
        for img in img_list:
            width_list.append(img.shape[1] / float(img.shape[0]))
        # Sorting can speed up the recognition process
        indices = np.argsort(np.array(width_list))
        rec_res = [['', 0.0]] * img_num
        batch_num = self.rec_batch_num

        for beg_img_no in range(0, img_num, batch_num):
            end_img_no = min(img_num, beg_img_no + batch_num)
            norm_img_batch = []
            max_wh_ratio = 0
            for ino in range(beg_img_no, end_img_no):
                h, w = img_list[indices[ino]].shape[0:2]
                wh_ratio = w * 1.0 / h
                max_wh_ratio = max(max_wh_ratio, wh_ratio)
            for ino in range(beg_img_no, end_img_no):
                norm_img = self.resize_norm_img(img_list[indices[ino]],
                                                max_wh_ratio)
                norm_img = norm_img[np.newaxis, :]
                norm_img_batch.append(norm_img)

            norm_img_batch = np.concatenate(norm_img_batch)
            norm_img_batch = norm_img_batch.copy()

            if self.use_paddle_predict:
                output = self.predictor(norm_img_batch).numpy()
                outputs = []
                outputs.append(output)
                if len(outputs) != 1:
                    preds = outputs
                else:
                    preds = outputs[0]
            else:
                input_dict = {}
                input_dict[self.predictor.get_inputs()[0].name] = norm_img_batch
                outputs = self.predictor.run(None, input_dict)
                preds = outputs[0]

            rec_result = self.postprocess_op(preds)
            for rno in range(len(rec_result)):
                rec_res[indices[beg_img_no + rno]] = rec_result[rno]
        return rec_res


import numpy as np
import logging

c = 3e8

def get_targets(radar, config_data, logging):
    """
    this function returns all the targets as an array based on the params
    Assume z angle and speed to be 0 for simplicity

    # target_3 = dict(location=(4*np.cos(np.radians(true_theta[2])), 4*np.sin(np.radians(true_theta[2])),
    #                 0), speed=(0, -10, 0), rcs=10, phase=0)

    # We will use angle range from -90 to 90 with
    """
    params = {}
    params["num_target"] = config_data["target"]["num_target"]
    params["x_angles"] = config_data["target"]["x_angle"] # put the angles based on the num target with ind 0 for target 1
    params["z_angles"] = config_data["target"]["z_angle"]
    params["rcs"]=config_data["target"]["rcs"]
    params["x_speed"] = config_data["target"]["x_speed"]
    params["y_speed"] = config_data["target"]["y_speed"]
    params["z_speed"] = config_data["target"]["z_speed"]
    params["phase"] = config_data["target"]["phase"]
    params["dist"]=config_data["target"]["dist"]

    targets = []
    '''
    #Calculate the minimum and maximum range of the radar
    Here the maximum speed is halved compared to the normal case
    this is due to the positive and negative speed.
    '''

    max_range = (c * radar.receiver.fs *
             radar.transmitter.pulse_length /
             radar.transmitter.bandwidth / 2)
    max_speed = (c /radar.transmitter.prp[0] / \
        radar.transmitter.fc_vect[0] / 2) / 2

    min_angle = config_data["run_params"]["min_angle"]
    max_angle = config_data["run_params"]["max_angle"]

    ranges_ = []
    angles_ = []
    speed_ = []
    for i in range(0, params["num_target"]):
        rand_range = np.random.uniform(0, max_range)
        rand_speed = np.random.uniform(-1*max_speed, max_speed)
        rand_angle = np.random.uniform(min_angle, max_angle)
        ranges_.append(rand_range)
        angles_.append(rand_angle)
        speed_.append(rand_speed)
        """
            Assumptions
                1. Object is in the X-Y plane
                2. Antennas are in Y direction so object is moving in X direction, Y and Z direction speed is 0
                3. rcs co-efficient of all the objects is 10
                4. Phase is 0 for all the objects

        """
        t_ = dict(location=(rand_range*np.cos(np.radians(rand_angle)), rand_range*np.sin(np.radians(rand_angle)),
                            0), speed=(rand_speed, 0, 0) , rcs= 10,  phase= 0)

        logging.info("Object Index " + str(i) )
        logging.info("Rand Range " + str(rand_range))
        logging.info("Rand Speed " + str(rand_speed))
        logging.info("Rand Angle " + str(rand_angle))

        # t_ = dict(location=(params["dist"][i]*np.cos(np.radians(params["x_angles"][i])), params["dist"][i]*np.sin(np.radians(params["x_angles"][i])),
                            # 0), speed=(params["x_speed"][i], params["y_speed"][i], params["z_speed"][i]), rcs=params["rcs"][i], phase=params["phase"][i])
        targets.append(t_)

    return np.array(ranges_), np.array(angles_), np.array(speed_), targets

def main():
    '''
    params
    '''
    # config_data = read_config()
    with open('config.json', 'r') as f:
        config_data = json.load(f)
    params = {}
    params["num_target"] = config_data["target"]["num_targer"]
    params["x_angles"] = config_data["target"]["x_angle"] # put the angles based on the num target with ind 0 for target 1
    params["z_angles"] = config_data["target"]["z_angle"]
    params["rcs"]=config_data["target"]["rcs"]
    params["x_speed"] = config_data["target"]["x_speed"]
    params["y_speed"] = config_data["target"]["y_speed"]
    params["z_speed"] = config_data["target"]["z_speed"]
    params["phase"] = config_data["target"]["phase"]
    params["dist"]=config_data["target"]["dist"]
    targets = get_targets(params)
    print(targets)

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


import os, sys
sys.path.insert(1, os.path.expanduser('~') +'/Git/TranSFF/Scripts/')
from pso import parallel_pso, serial_pso, parallel_pso_auxiliary
from multiprocessing import Process, Pool
import numpy
import os.path
from os import path

# Input parameters ##################
run_name = "SimulPSO_C2_select4_2-10-17-21_80-20-4-4_2-2-1-1"
molecules_array = ["C2"]
datafile_keyword_array=[ "REFPROP"]
site_names_array = ["CH3"]

ITIC_subset_name = "select4_2-10-17-21"
weights_file = "$HOME/Git/TranSFF/Weights/select4_2-10-17-21_80-20-4-4_2-2-1-1.wts"
LJ_or_BUCK_or_MIE="MIE"
table = "$HOME/Git/ITIC_GROMACS/Forcefields/Tables/mie_16-6_rc10.xvg"    #table address or no-table
raw_ff_path = "$HOME/Git/ITIC_GROMACS/Forcefields/MiPPE-raw"

config_filename = "TC_RC10_LF_BR_0.1NS-0.1NS_LINCS8_CSG_100XYZ_TAB.config"
gmx_exe_address = "$HOME/Git/GROMACS/gromacs-2018.1/build/bin/gmx"

Nproc_per_particle = "1"

nnn = None
sig_sigfig = 3
eps_sigfig = 1
nnn_sigfig = 1

# Set PSO parameters ################
swarm_size = 8
max_iterations = 30
tol = 1e-6

# Set PSO bounds and initial guesses ################
lb = [3.75, 110, 16]
ub = [3.85, 130, 16]
initial_guess = [[], [], [], [], [], [], [], []]
nnbp = 3

#============================================================================================
molecules = ""
datafile_keywords_string = ""
for imolec in range(0, len(molecules_array)):
    molecules = molecules + molecules_array[imolec] + " "
    datafile_keywords_string = datafile_keywords_string + datafile_keyword_array[imolec] + " "

molecules = molecules[:-1]
molecules =  "\"" + molecules + "\""
datafile_keywords_string = datafile_keywords_string[:-1]
datafile_keywords_string =  "\"" + datafile_keywords_string + "\""

print(datafile_keywords_string)
log = "pso.log"
iter = 0
def objective_function(x):
    global iter
    iter = iter + 1

    np = len(x[:, 0])   # Number of particles (candidate solutions)
    nd = len(x[0, :])   # Number of dimensions (variables)

    objective_array = []
    def run_one_particle(run_particle_input_array):
        iteration = run_particle_input_array[0]
        particle = run_particle_input_array[1]
        sig_eps_nnn = run_particle_input_array[2]

        for isite in range(0, len(site_names_array)):
            for inbp in range(0, nnbp):
                if nnbp == 2:
                    if inbp % (nnbp) == 0:
                        vars()['sig' + str(isite)] = round( sig_eps_nnn[isite * nnbp + inbp], sig_sigfig)
                    elif inbp % (nnbp) == 1:
                        vars()['eps' + str(isite)] = round( sig_eps_nnn[isite * nnbp + inbp], eps_sigfig)
                elif nnbp == 3:
                    if inbp % (nnbp) == 0:
                        vars()['sig' + str(isite)] = round( sig_eps_nnn[isite * nnbp + inbp], sig_sigfig)
                    elif inbp % (nnbp) == 1:
                        vars()['eps' + str(isite)] = round( sig_eps_nnn[isite * nnbp + inbp], eps_sigfig)
                    elif inbp % (nnbp) == 2:
                        vars()['nnn' + str(isite)] = round( sig_eps_nnn[isite * nnbp + inbp], nnn_sigfig)

        site_sig_eps_nnn = ""
        for isite in range(0, len(site_names_array)):
            if nnbp == 2:
                if nnn is None:
                    site_sig_eps_nnn = site_sig_eps_nnn + site_names_array[isite] + "-" +  str(vars()['sig' + str(isite)]) + "-" + str(vars()['eps' + str(isite)])  + "_"
                else:
                    site_sig_eps_nnn = site_sig_eps_nnn + site_names_array[isite] + "-" +  str(vars()['sig' + str(isite)]) + "-" + str(vars()['eps' + str(isite)])  + "-" + str(round( nnn, nnn_sigfig)) + "_"
            if nnbp == 3:
                site_sig_eps_nnn = site_sig_eps_nnn + site_names_array[isite] + "-" +  str(vars()['sig' + str(isite)]) + "-" + str(vars()['eps' + str(isite)])  + "-" + str(vars()['nnn' + str(isite)]) + "_"
        site_sig_eps_nnn = site_sig_eps_nnn[:-1]
        print(site_sig_eps_nnn)

        prefix = "i-" + str(iteration) + "_p-" + str(particle)
        print(site_sig_eps_nnn)
        arg1 = site_sig_eps_nnn
        arg2 = prefix
        arg3 = molecules
        arg4 = raw_ff_path
        arg5 = datafile_keywords_string
        arg6 = gmx_exe_address
        arg7 = weights_file
        arg8 = Nproc_per_particle
        arg9 = ITIC_subset_name
        arg10 = config_filename
        arg11 = LJ_or_BUCK_or_MIE
        arg12 = table
        command = "bash $HOME/Git/ITIC_GROMACS/Scripts/RunGMX_simulticomp.sh" + " " + arg1 + " " + arg2+  " " + arg3 + " " + arg4 + " " + arg5 + " " + arg6 + " " + arg7 + " " + arg8 + " " + arg9 + " " + arg10 + " " + arg11 + " " + arg12 #+ " " + arg13 + " " + arg14 + " " + arg15 + " " + arg16

        particel_folder_name = prefix + "_" + site_sig_eps_nnn
        if path.exists(particel_folder_name):
            print("Warning: " + particel_folder_name + " folder exists. The program proceeds assuming that the folder contains valid data.")
        else:
            print(command)
            os.system(command)

        return

    run_particle_input_array = []
    for p in range(0, np):
        run_particle_input_array.append( [iter, p + 1, x[p, :]] )

    for p in range(0, np):
        exec_string = "p" + str(p) + " = Process(target = run_one_particle, args=(run_particle_input_array[" + str(p) + "],))"
        exec(exec_string)
        exec_string = "p" + str(p) + ".start()"
        exec(exec_string)

    for p in range(0, np):
        exec_string = "p" + str(p) + ".join()"
        exec(exec_string)

    # wait for all particles to get ready

    for p in range(0, np):
        score_file_address = "i-" + str(iter) + "_p-" + str(p + 1) + ".score"
        scores_data = numpy.loadtxt(score_file_address, skiprows=1, usecols=[1,2,3])

        if len(molecules_array) == 1:
            score = numpy.sum(scores_data[0])
        else:
            score = numpy.sum(scores_data[:,0])

        objective_array.append(score)

    print("objective_array: ", objective_array)
    print()

    return objective_array  ####################### End of objective_function

print("initial_guess: ", initial_guess)
print()
xopt, fopt = parallel_pso(objective_function, lb, ub, ig = initial_guess ,swarmsize=swarm_size, omega=0.5, phip=0.5, phig=0.5, maxiter=max_iterations, minstep=tol, minfunc=tol, debug=False, outFile = log)

print("xopt, fopt: ", xopt, fopt)

#os.system("bash $HOME/Git/TranSFF/Scripts/simulticomp_organize.sh " + run_name + " " + molecules)


import random
import time

def solution(n, lost, reserve):
    answer = 0
    total = [1 for i in range(n+2)]
    total[0] = 0
    total[n+1] = 0
    cnt = 0

    for i in lost:
        total[i] -= 1
    for i in reserve:
        total[i] += 1

    for i in range(1,n+1):
        if total[i] == 0:
            if total[i-1] == 2:
                total[i-1] -= 1
                total[i] += 1
                cnt += 1
            elif total[i+1] == 2:
                total[i+1] -= 1
                total[i] += 1
                cnt += 1
        elif (total[i] == 1) | (total[i] == 2):
            cnt += 1
        answer = cnt
    print(total)
    return answer

def solution1(n, lost, reserve):
    answer = 0
    students = [1] * n

    for i in lost:
        students[i-1] -= 1

    for i in reserve:
        students[i-1] += 1

    cnt = 0
    for idx, student in enumerate(students):
        if student == 0:
            if students[max(idx-1, 0)] == 2:
                students[max(idx - 1, 0)] -= 1
                students[idx] += 1
            elif students[min(idx + 1, len(students)-1)] == 2:
                students[min(idx + 1, len(students)-1)] -= 1
                students[idx] += 1

    for student in students:
        if student > 0:
            cnt += 1

    return cnt

def solution2(n, lost, reserve):
    answer = 0

    # 차집합
    lost_dif = [item for item in lost if item not in reserve]
    reserve_dif = [item for item in reserve if item not in lost]

    for item in lost_dif:
        for i in [-1, 1]:
            if item+i in reserve_dif:
                reserve_dif.remove(item+i)
                answer += 1
                break

    return n-len(lost_dif)+answer

프로그래머스/고득점 키트/06.탐욕법/체육복.py


#Compute the Lyapunov exponent for simple example x' = e^lambda x

import numpy as np
import matplotlib.pyplot as plt

def evolution(x, t, l = 1):
   return  np.exp(l * t) * x

def logistic(x_0,r = 3.2, reps = 600):
    x = np.zeros(reps)
    x[0] = x_0
    for i in range(reps - 1):
        x[i + 1] = r * x[i] * (1 - x[i])
    return x

def traj(x, l = 1):
    traj = np.zeros((100), dtype = np.float64)
    for i in np.arange(100):
        traj[i] = evolution(x,i,l)
    return traj

print(traj(1))

def le(x,y):
    d = x - y

    d = d/d[0]
    d = np.log(d)

    t = np.arange(len(x))

    d = d/t
    return d
r = 3.8 #logistic parameter
x = logistic(0.5,r)
y = logistic(0.5001,r)

le = le(x,y)
t = np.arange(600)
plt.plot(t, x,y)
plt.show()


from requests import get as old_get

# 访问 jsonp 时的模版，下面的模版用来配合 get_from_file 使用本地文件进行测试
URL_TEMPLATE = "https://news.cctv.com/2019/07/gaiban/cmsdatainterface/page/{}_{}.jsonp"
# URL_TEMPLATE = "./test_data/{}_{}.jsonp"

# 爬取数据的延时
FETCH_DELAY = 2

# 支持的新闻类别
SUPPORTED_CLASS = {
	'主页': 'news',
	'国内': 'china',
	'国际': 'world',
	'社会': 'society',
	'法制': 'law',
	'文娱': 'ent',
	'科技': 'tech',
	'生活': 'life'
}
SUPPORTED_KEYS = list(SUPPORTED_CLASS.keys())

class Loger:
	"""
	仅在调试模式下输出调试信息
	"""
	is_debug_mode = True

	@staticmethod
	def log(msg):
		if Loger.is_debug_mode:
			print(f"[DEBUG] {msg}")

UserAgent = " ".join([
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6)",
	"AppleWebKit/537.36 (KHTML, like Gecko)",
	"Chrome/87.0.4280.88 Safari/537.36"
])

def get(url):
	"""
	requests.get 的包装函数，修改了 UA
	并在内部调用 raise_for_status 函数
	"""
	res = old_get(url, headers={'User-Agent': UserAgent})
	res.raise_for_status()
	return res

# 这一坨用来测试, TestObj 模拟 requests.Response 对象
# get_from_file 模拟 requests.get 方法
class TestObj:
	def __init__(self, data):
		self.text = data

def get_from_file(url):
	with open(url) as f:
		return TestObj(f.read())


import os

from sklearn.ensemble import (
    AdaBoostClassifier,
    GradientBoostingClassifier,
    RandomForestClassifier,
)
from sklearn.linear_model import LinearRegression,LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import r2_score

from src.utils import evaluate_model,save_object
from src.logger import logging
from src.exception import CustomException

from dataclasses import dataclass

@dataclass
class ModelTrainerConfig:
    trained_model_file_path=os.path.join('Artifacts','model.pkl')

class ModelTrainer:
    def __init__(self):
        self.model_trainer_config=ModelTrainerConfig()
    def initiate_model_trainer(self,train_array,test_array):
        try:
            logging.info("Split Training and Test Input Data")
            X_train,Y_train,X_test,Y_test=(
                train_array[:,:-1],
                train_array[:,-1],
                test_array[:,:-1],
                test_array[:,-1],
                )
            models={
                "KNeighbors" : KNeighborsClassifier(),
                "Decision Tree": DecisionTreeClassifier(),
                "Random Forest":RandomForestClassifier(),
                "Gradient Boosting":GradientBoostingClassifier(),
                "AdaBoost Classifier":AdaBoostClassifier(),

            }
            params={
                "KNeighbors":{
                    'n_neighbors':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
                },
                "Decision Tree": {
                    #'criterion':['squared_error', 'friedman_mse', 'absolute_error', 'poisson'],
                    #'splitter':['best','random'],
                    #'max_features':['sqrt','log2'],
                },
                "Random Forest":{
                    #'criterion':['squared_error', 'friedman_mse', 'absolute_error', 'poisson'],
                    #'max_features':['sqrt','log2',None],
                    'n_estimators': [8,16,32,64,128,256]
                },
                "Gradient Boosting":{
                    #'loss':['squared_error', 'huber', 'absolute_error', 'quantile'],
                    'learning_rate':[.1,.01,.05,.001],
                    'subsample':[0.6,0.7,0.75,0.8,0.85,0.9],
                    #'criterion':['squared_error', 'friedman_mse'],
                    #'max_features':['auto','sqrt','log2'],
                    'n_estimators': [8,16,32,64,128,256]
                },
                "AdaBoost Classifier":{
                    'learning_rate':[.1,.01,0.5,.001],
                    #'loss':['linear','square','exponential'],
                    'n_estimators': [8,16,32,64,128,256]
                },
            }

            model_report:dict=evaluate_model(X_train=X_train,Y_train=Y_train,X_test=X_test,Y_test=Y_test,models=models,param=params)
            #To get best model score from dict
            best_model_score=max(sorted(model_report.values()))

            #To get best model name from dict
            best_model_name=list(model_report.keys())[list(model_report.values()).index(best_model_score)]
            best_model=models[best_model_name]

            if best_model_score<0.6:
                logging.info('No Best Model Found')
                raise CustomException("No best model found")
            logging.info("Best found model on both training and testing dataset")

            save_object(
                file_path=self.model_trainer_config.trained_model_file_path,
                obj=best_model
            )
            best_model.fit(X_train,Y_train)
            predicted=best_model.predict(X_test)
            r2_square=r2_score(Y_test,predicted)
            return r2_square

        except Exception as e:
            raise CustomException(e,sys)


import client
from socket import *
print("Enter the host(ex: 127.0.0.1): ")
host=str(input())
print('Enter the port(ex:4498): ')
port=int(input())

def Server():
    s = socket(AF_INET, SOCK_STREAM)
    s.bind((host, port))
    s.listen(1)
    print("Listening for connections.. ")
    q, addr = s.accept()
    data = input("Enter Message to be send:  ")
    q.send(data.encode())
    s.close()
    client.Client()

def StartServer():
    s= socket(AF_INET,SOCK_STREAM)
    s.bind((host,port))
    s.listen(1)
    q, addr= s.accept()
    data=input("Enter Message: ")
    q.send(data.encode())
    s.close()


#!/usr/local/bin/python3
#
# Authors: [PLEASE PUT YOUR NAMES AND USER IDS HERE]
#
# Ice layer finder
# Based on skeleton code by D. Crandall, November 2021
#

from PIL import Image
from numpy import *
from scipy.ndimage import filters
import sys
import imageio
import math

import numpy as np
# calculate "Edge strength map" of an image
def edge_strength(input_image):
    grayscale = array(input_image.convert('L'))
    filtered_y = zeros(grayscale.shape)
    filters.sobel(grayscale,0,filtered_y)
    return sqrt(filtered_y**2)

# draw a "line" on an image (actually just plot the given y-coordinates
#  for each x-coordinate)
# - image is the image to draw on
# - y_coordinates is a list, containing the y-coordinates and length equal to the x dimension size
#   of the image
# - color is a (red, green, blue) color triple (e.g. (255, 0, 0) would be pure red
# - thickness is thickness of line in pixels
#
def draw_boundary(image, y_coordinates, color, thickness):
    for (x, y) in enumerate(y_coordinates):
        for t in range( int(max(y-int(thickness/2), 0)), int(min(y+int(thickness/2), image.size[1]-1 )) ):
            image.putpixel((x, t), color)
    return image

def draw_asterisk(image, pt, color, thickness):
    for (x, y) in [ (pt[0]+dx, pt[1]+dy) for dx in range(-3, 4) for dy in range(-2, 3) if dx == 0 or dy == 0 or abs(dx) == abs(dy) ]:
        if 0 <= x < image.size[0] and 0 <= y < image.size[1]:
            image.putpixel((x, y), color)
    return image

# Save an image that superimposes three lines (simple, hmm, feedback) in three different colors
# (yellow, blue, red) to the filename
# Changed this code as per Prof. Crandall's recommendation on Inscribe
# Please refer to this link https://inscribe.education/main/indianau/6754110229500968/questions/6749461749659284?backToListTab=all
def write_output_image(filename, image, simple, hmm, feedback, feedback_pt):
    new_image = image.copy()
    new_image = draw_boundary(new_image, simple, (255, 255, 0), 2)
    new_image = draw_boundary(new_image, hmm, (0, 0, 255), 2)
    new_image = draw_boundary(new_image, feedback, (255, 0, 0), 2)
    new_image = draw_asterisk(new_image, feedback_pt, (255, 0, 0), 4)
    imageio.imwrite(filename, new_image)

# main program
if __name__ == "__main__":

    if len(sys.argv) != 6:
        raise Exception("Program needs 5 parameters: input_file airice_row_coord airice_col_coord icerock_row_coord icerock_col_coord")

    #input_filename = "test_images/09.png"
    input_filename = sys.argv[1]
    # gt_airice = [ int(i) for i in sys.argv[2:4] ]
    # gt_icerock = [ int(i) for i in sys.argv[4:6] ]

    # Changed this because x axis is essentially your col number and y axis is your row number
    gt_airice = [int(i) for i in sys.argv[2:4]][::-1]
    gt_icerock = [int(i) for i in sys.argv[4:6]][::-1]
    #print(gt_airice,gt_icerock)

    # load in image
    input_image = Image.open(input_filename).convert('RGB')
    image_array = array(input_image.convert('L'))

    #print(len(image_array),len(image_array[0]))
    # compute edge strength mask -- in case it's helpful. Feel free to use this.
    # 175 rows and 225 columns
    # Simple - Air Ice
    simple_edge_strength = edge_strength(input_image)
    imageio.imwrite('edges.png', uint8(255 * simple_edge_strength / (amax(simple_edge_strength))))
    airice_simple = simple_edge_strength.argmax(axis=0)

    # Simple - Ice rock
    mask = np.transpose(np.arange(simple_edge_strength.shape[0]) < (airice_simple + 10)[:, None])
    simple_edge_strength[mask] = -1
    icerock_simple = simple_edge_strength.argmax(axis=0)
    empty_array = []

    #HMM - Air ice
    # transitional probability
    def transitional_prob(previous_boundary_row, hmm_edge_strength):
        x = np.arange(0, hmm_edge_strength.shape[0])
        gaussian_dist = np.exp((-((x - previous_boundary_row) / np.std(x)) ** 2) / 2) / (
                2 * math.pi * (np.std(x) ** 2)) ** 0.5
        gaussian_dist = (gaussian_dist - np.min(gaussian_dist)) / np.ptp(gaussian_dist)
        if sys.argv[1] in ("test_images/30.png","test_images/23.png"):
            top_indices, bottom_indices = list(range(previous_boundary_row-3)), list(range(previous_boundary_row+5, hmm_edge_strength.shape[0]))
            gaussian_dist[top_indices], gaussian_dist[bottom_indices] = 0.2,0.2
            #-3 and +5
        #gaussian_dist = gaussian_dist/np.max(gaussian_dist)
        return gaussian_dist

    # initial probability
    def hmm_initial_prob(initial_prob_factor, hmm_edge_strength):
        initial_prob = np.arange(0, hmm_edge_strength.shape[0], dtype=float)
        top_indices, bottom_indices = list(range(round(hmm_edge_strength.shape[0] * initial_prob_factor))), list(
            range(round(hmm_edge_strength.shape[0] * initial_prob_factor), hmm_edge_strength.shape[0]))
        initial_prob[top_indices], initial_prob[bottom_indices] = 0.8, 0.3
        return initial_prob

    # Used the following two lines from https://stackoverflow.com/questions/34980833/python-name-of-np-array-variable-as-string
    # Converting the name of numpy array to string to use in a if loop below
    def convert_array_str(obj, name):
        return [each for each in name if name[each] is obj]

    # Viterbi - HMM
    # The pseudo code for Viterbi is taken from Prof. Crandall's code in the activity session.
    def hmm(hmm_edge_strength, initial_prob_factor, flag, gt_airice, airice_feedback):

        # emission probability
        emmission_prob = (hmm_edge_strength - np.min(hmm_edge_strength, axis=0)) / np.ptp(hmm_edge_strength, axis=0)

        if flag == "human_feedback" and initial_prob_factor == 0 and sys.argv[1] == "test_images/23.png":
            if convert_array_str(hmm_edge_strength, globals())[0][0] == "b":
                mask = np.transpose(np.arange(emmission_prob.shape[0]) < (airice_feedback + 30)[:, None])
                emmission_prob[mask] *= 0.05
            else:
                mask = np.transpose(np.arange(emmission_prob.shape[0]) < (airice_feedback + 6)[:, None])
                emmission_prob[mask] *= 0.2

        #Initial probability
        if flag == "human_feedback":
            indices = list(range(hmm_edge_strength.shape[0]))[::-1]
            indices.remove(gt_airice[0])
            initial_prob = np.arange(0, hmm_edge_strength.shape[0], dtype=float)
            initial_prob[indices], initial_prob[gt_airice[0]] = 0.05, 1
        else:
            initial_prob = hmm_initial_prob(initial_prob_factor, hmm_edge_strength)

        #Initializing the viterbi and other tables
        Viterbi_table = {}
        path_table = {}
        tp_input = np.array([])

        # first column
        for row in range(hmm_edge_strength.shape[0]):
            Viterbi_table[(row, 0)] = initial_prob[row] * emmission_prob[row][0]
            path_table[(row, 0)] = row
            tp_input = np.append(tp_input, Viterbi_table[(row, 0)] )

        # rest columns
        V_last = np.array([])
        for col in range(1, hmm_edge_strength.shape[1]):
            t_prob = transitional_prob(tp_input.argmax(),hmm_edge_strength)
            tp_input = np.array([])
            for row_1 in range(hmm_edge_strength.shape[0]):
                max_path, max_value = max(
                    [(k, Viterbi_table[(k, col - 1)] * t_prob[k]) for k in range(hmm_edge_strength.shape[0])],
                    key=lambda l: l[1])
                path_table[(row_1, col)], Viterbi_table[(row_1, col)] = max_path, max_value * emmission_prob[row_1][col]
                tp_input = np.append(tp_input, row)
                #Appending only the last column in a numpy array to use it as a starting point for backtracking
                if col == hmm_edge_strength.shape[1] - 1:
                    V_last = np.append(V_last, Viterbi_table[(row_1, col)])

        max_index = V_last.argmax()
        hmm_seq = ones(hmm_edge_strength.shape[1])

        # backtracking
        for i in range(hmm_edge_strength.shape[1] - 1, -1, -1):
            hmm_seq[i] = int(max_index)
            max_index = path_table[(int(max_index), i)]

        return hmm_seq

    #HMM Viterbi - Air Ice
    hmm_edge_strength = edge_strength(input_image)
    airice_hmm = hmm(hmm_edge_strength, 0.5, "no_human_feedback", gt_airice, empty_array)
    #print(airice_hmm)

    # HMM - Ice Rock
    mask = np.transpose(np.arange(hmm_edge_strength.shape[0]) < (airice_hmm + 10)[:, None])
    hmm_edge_strength[mask] = 0
    icerock_hmm = hmm(hmm_edge_strength, 0.8, "no_human_feedback", gt_icerock, empty_array)
    #print(icerock_hmm)

    #airice - human feedback
    #running viterbi from human feedback point till the end
    human_edge_strength = edge_strength(input_image)
    fhuman_edge_strength = np.delete(human_edge_strength, np.s_[0:gt_airice[1]], axis=1)

    #flipping the matrix from feedback point to 0
    bhuman_edge_strength = np.delete(human_edge_strength, np.s_[gt_airice[1]+1:human_edge_strength.shape[1]], axis=1)
    bhuman_edge_strength = np.fliplr(bhuman_edge_strength)

    # combining both
    airice_feedback = np.hstack((np.flipud(hmm(bhuman_edge_strength, 0.5, "human_feedback", gt_airice, empty_array))[:-1],
                                 hmm(fhuman_edge_strength, 0.5, "human_feedback", gt_airice, empty_array)))

    #icerock - human feedback
    mask = np.transpose(np.arange(human_edge_strength.shape[0]) < (airice_feedback + 10)[:, None])
    human_edge_strength[mask] = 0
    fhuman_edge_strength = np.delete(human_edge_strength, np.s_[0:gt_icerock[1]], axis=1)
    bhuman_edge_strength = np.delete(human_edge_strength, np.s_[gt_icerock[1]+1:human_edge_strength.shape[1]], axis=1)
    bhuman_edge_strength = np.fliplr(bhuman_edge_strength)
    icerock_feedback = np.hstack((np.flipud(hmm(bhuman_edge_strength, 0, "human_feedback", gt_icerock, airice_feedback[0:gt_icerock[1]+2][:-1]))[:-1],
                                hmm(fhuman_edge_strength, 0, "human_feedback", gt_icerock, airice_feedback[gt_icerock[1]:human_edge_strength.shape[1]])))

    # Now write out the results as images and a text file
    # Inversed the feedback point because the x- axis is essentially number of columns in inbuilt asterick plotting function
    # whereas it is reverse in the code.
    # sample : python3 polar.py test_images/09.png x-axis(airice) y-axis(airice) x-axis(icerock) y-axis(icerock)
    write_output_image("air_ice_output.png", input_image, airice_simple, airice_hmm, airice_feedback, gt_airice[::-1])
    write_output_image("ice_rock_output.png", input_image, icerock_simple, icerock_hmm, icerock_feedback, gt_icerock[::-1])
    with open("layers_output.txt", "w") as fp:
        for i in (airice_simple, airice_hmm, icerock_simple, icerock_hmm, airice_feedback, icerock_feedback):
            fp.write(str(i) + "\n")


# importing the required modules
import pandas as pd

# disabled choose-your-own-path method
# pre_root = input("Please type in directory path to excel file containing list of excel files to be concatenated, "
#                 "followed by the enter key:")
# pre_outfile_name = input("Please type in name of excel file to be output, followed by the enter key:")
# pre_variable_file = input("Please enter the path to an excel file with the acceptable variable fields stored in the"
#                          " first column, followed by the enter key:")

# hard-coded paths
pre_root = 'C:\\Users\\Richard.Bankoff\\Box\\Centre ValBio\\Centre ValBio Incoming Data\\Climate\\dataCleaningFolder\\List_of_excel_spreadsheet_locations_climate.xlsx'
pre_outfile_name = 'C:\\Users\\Richard.Bankoff\\Box\\Centre ValBio\\Centre ValBio Incoming Data\\Climate\\dataCleaningFolder\\climate_out2.csv'
pre_variable_file = 'C:\\Users\\Richard.Bankoff\\Box\\Centre ValBio\\Centre ValBio Incoming Data\\Climate\\dataCleaningFolder\\List_of_variables_climate.xlsx'

# clean up leading/trailing whitespace - unnecessary for hard-coded variables, but good to leave in just in case
root = pre_root.strip()
outfile_name = pre_outfile_name.strip()
variable_file = pre_variable_file.strip()

# read in list of Excel files for concatenation
file_list_manifest = pd.read_excel(root)
pre_variable_list = pd.read_excel(variable_file)

variable_list = pre_variable_list['variableNames'].tolist()

# check statements for verifying list of variables
print(variable_list)
print(len(variable_list))

# create list of all Excel files to be concatenated
file_list = file_list_manifest['fullPath'].tolist()

# list of Excel files we want to merge.
# pd.read_excel(file_path) reads the Excel
# data into pandas dataframe.
excl_list = []

# skip headers that are not useful
for file in file_list:
    excl_list.append(pd.read_excel(file, skiprows=[0, 2, 3]))

# create a new dataframe to store the
# merged Excel file.
excl_merged = pd.DataFrame()

# for each Excel file, clean data and append to list
for excl_file in excl_list:

    excl_file = excl_file.dropna(axis='columns', how='all')
    excl_file = excl_file.drop(['AirTC_3_Avg', 'AirTC_3_Std', 'RH_3'], axis=1, errors='ignore')
    excl_file = excl_file[variable_list]
    column_numbers = len(excl_file.columns)

    # if the number of columns is not what is expected, throw an error
    if column_numbers != 23:
        print("Missing column!")

    # appends the data into the excl_merged
    # dataframe.
    excl_merged = pd.concat([excl_merged, excl_file])

# exports the dataframe into a CSV file with
# specified name. CSV is used for output because
# large datasets may exceed the Excel spreadsheet
# specification; if you know your dataset is small
# enough to be put into .xlsx format, replace
# to_csv() with to_excel on the line below
excl_merged = excl_merged.dropna(axis='rows', how='all')
excl_merged.to_csv(outfile_name, index=False)
exit()


class Solution(object):
    def diameterOfBinaryTree(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """

        # node -> max height
        d = {}

        self.dfs(root, d)

        return max(d.values())

    def dfs(self, root, d):
        if not root:
            return 0

        left_height = self.dfs(root.left, d)
        right_height = self.dfs(root.right, d)

        d[root] = left_height + right_height

        return max(left_height, right_height) + 1


############ Sentiment Analysis
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt # visualization
import seaborn as sns
from datetime import datetime
import plotly.offline as py # visualization
import plotly.graph_objs as go # visualization
import plotly.tools as tls # visualization
import plotly.figure_factory as ff # visualization
# text manipulation
import  re
from nltk.corpus import stopwords

# Plot 3
# wordcloud - hashtags
from wordcloud import WordCloud

import networkx as nx
####### favorite and retweets by sentiment
import itertools
###### Support Vector Machine
from sklearn.metrics import classification_report
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
######## Document Term Matrix
from sklearn.feature_extraction.text import CountVectorizer

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
###### Classifier Random forest Algorithm
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix,accuracy_score,classification_report
from sklearn.metrics import recall_score,precision_score,f1_score
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense,GRU,LSTM,Embedding
from keras.optimizers import Adam
from keras.layers import SpatialDropout1D,Dropout,Bidirectional,Conv1D,GlobalMaxPooling1D,MaxPooling1D,Flatten
from keras.callbacks import ModelCheckpoint, TensorBoard, Callback, EarlyStopping
####### Network analysis of tweets
from sklearn.feature_extraction.text import CountVectorizer
###### Logistic regression
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
from .utils import generate_graph_img

def sentimental_analysis(df):

  # Select columns i will use for the analysis
  tweets = df[[ 'handle', 'text', 'is_retweet', 'original_author',
                   'time', 'lang', 'retweet_count', 'favorite_count']]

  # Exploring the dataset
  tweets.head()
  tweets.describe()
  tweets.info()

  ##### Data Manipulation
  # convert to date format and extract hour

  date_format = "%Y-%m-%dT%H:%M:%S"
  tweets["time"]   = pd.to_datetime(tweets["time"],format = date_format)
  tweets["hour"]   = pd.DatetimeIndex(tweets["time"]).hour
  tweets["month"]  = pd.DatetimeIndex(tweets["time"]).month
  tweets["day"]    = pd.DatetimeIndex(tweets["time"]).day
  tweets["month_f"]  = tweets["month"].map({1:"JAN",2:"FEB",3:"MAR",
                                          4:"APR",5:"MAY",6:"JUN",
                                          7:"JUL",8:"AUG",9:"SEP"})

  # Language Used
  def label_language(df) :
      if df["lang"] == "en" :
          return "English"
      elif df["lang"] == "es" :
          return "French"
      else :
          return "Other"
  tweets["lang"] = tweets.apply(lambda tweets:label_language(tweets),axis = 1)

  # Create new tweets column colum tweets has same text than column text
  tweets["tweets"] = tweets["text"]

  stop_words = stopwords.words("english")

  # Function to remove special characters, punctions, stop words, digits, hyperlinks and case conversion
  def string_manipulation(df,column)  :
      # extract hashtags
      df["hashtag"]  = df[column].str.findall(r'#.*?(?=\s|$)')
      # extract twitter account references
      df["accounts"] = df[column].str.findall(r'@.*?(?=\s|$)')

      # remove hashtags and accounts from tweets
      df[column] = df[column].str.replace(r'@.*?(?=\s|$)'," ")
      df[column] = df[column].str.replace(r'#.*?(?=\s|$)'," ")

      # convert to lower case
      df[column] = df[column].str.lower()
      # remove hyperlinks
      df[column] = df[column].apply(lambda x:re.split('https:\/\/.*',str(x))[0])
      # remove punctuations
      df[column] = df[column].str.replace('[^\w\s]'," ")
      # remove special characters
      df[column] = df[column].str.replace("\W"," ")
      # remove digits
      df[column] = df[column].str.replace("\d+"," ")
      # remove under scores
      df[column] = df[column].str.replace("_"," ")
      # remove stopwords
      df[column] = df[column].apply(lambda x: " ".join([i for i in x.split()
                                                        if i not in (stop_words)]))
      return df

  tweets = string_manipulation(tweets,"text")

  # Trump tweets without retweets
  tweets_trump = (tweets[(tweets["handle"] == "realDonaldTrump") &
                           (tweets["is_retweet"] == False)].reset_index().drop(columns = ["index"],axis = 1))

  # trump tweets with retweets
  tweets_trump_retweets = (tweets[(tweets["handle"] == "realDonaldTrump") &
                                    (tweets["is_retweet"] == True)].reset_index().drop(columns = ["index"],axis = 1))

  tweets_trump.head(4).style.set_properties(**{}).set_caption("Trump tweets")

  # hillary tweets without retweets
  tweets_hillary  = (tweets[(tweets["handle"] == "HillaryClinton") &
                              (tweets["is_retweet"] == False)].reset_index()
                                .drop(columns = ["index"],axis = 1))

  # hillary tweets with retweets
  tweets_hillary_retweets  = (tweets[(tweets["handle"] == "HillaryClinton") &
                              (tweets["is_retweet"] == True)].reset_index()
                                .drop(columns = ["index"],axis = 1))

  tweets_hillary.head(4).style.set_properties(**{}).set_caption("Hillary tweets")

  #### Percentage of retweets

  plt.style.use('ggplot')

  plt.figure(figsize = (13,6))

  # Plot 1
  plt.subplot(121)
  tweets[tweets["handle"] ==
         "realDonaldTrump"]["is_retweet"].value_counts().plot.pie(autopct = "%1.0f%%",
                                                                  wedgeprops = {"linewidth" : 1,
                                                                                "edgecolor" : "k"},
                                                                  shadow = True,fontsize = 13,
                                                                  explode = [.1,0.09],
                                                                  startangle = 20,
                                                                  colors = ["blue","w"]
                                                                 )
  plt.ylabel("")
  plt.title("Percentage of retweets - Trump")

  # Plot 2
  plt.subplot(122)
  tweets[tweets["handle"] ==
         "HillaryClinton"]["is_retweet"].value_counts().plot.pie(autopct = "%1.0f%%",
                                                                  wedgeprops = {"linewidth" : 1,
                                                                                "edgecolor" : "k"},
                                                                  shadow = True,fontsize = 13,
                                                                  explode = [.09,0],
                                                                  startangle = 60,
                                                                  colors = ["red","w"]
                                                                 )
  plt.ylabel("")
  plt.title("Percentage of retweets - Hillary")
  retweets = generate_graph_img(plt)

  #### Languages used in tweet
  plt.figure(figsize = (12,7))

  # plot 1
  plt.subplot(121)
  ax = sns.countplot(y = tweets[tweets["handle"] == "realDonaldTrump"]["lang"] ,
                     linewidth = 1,edgecolor = "k"*3,
                     palette = "Reds_r")

  for i,j in enumerate(tweets[tweets["handle"] ==
                              "realDonaldTrump"]["lang"].value_counts().values) :
      ax.text(.7,i,j,fontsize = 15)

  plt.grid(True)
  plt.title("Languages used in tweets - trump")

  # Plot 2
  plt.subplot(122)
  ax1 = sns.countplot(y = tweets[tweets["handle"] == "HillaryClinton"]["lang"] ,
                     linewidth = 1,edgecolor = "k"*3,
                      palette = "Blues_r")

  for i,j in enumerate(tweets[tweets["handle"] ==
                              "HillaryClinton"]["lang"].value_counts().values) :
      ax1.text(.7,i,j,fontsize = 15)

  plt.grid(True)
  plt.ylabel("")
  plt.title("Languages used in tweets - hillary")
  languages_used = generate_graph_img(plt)

  #######@ original authors of retweets
  plt.figure(figsize = (10,14))

  # Plot 1
  plt.subplot(211)
  authors = tweets_trump_retweets["original_author"].value_counts().reset_index()
  sns.barplot(y = authors["index"][:15] ,
              x = authors["original_author"][:15] ,
              linewidth = 1,edgecolor = "k",color = "#FF3300")
  plt.grid(True)
  plt.xlabel("count")
  plt.ylabel("original author")
  plt.title("original authors of retweets - Trump")

  # Plot 2
  plt.subplot(212)
  authors1 = tweets_hillary_retweets["original_author"].value_counts().reset_index()
  sns.barplot(y = authors1["index"][:15] ,
              x = authors1["original_author"][:15] ,
              linewidth = 1,edgecolor = "k",color ="#6666FF")
  plt.grid(True)
  plt.xlabel("count")
  plt.ylabel("original author")
  plt.title("original authors of retweets - Hillary")
  original_authors_retweets = generate_graph_img(plt)

  ####### tweets by month
  plt.figure(figsize = (12,8))
  sns.countplot(x = "month_f",hue = "handle",palette = ["#FF3300","#6666FF"],
                data = tweets.sort_values(by = "month",ascending = True),
               linewidth = 1,edgecolor = "k"*tweets_trump["month"].nunique())
  plt.grid(True)
  plt.title("tweets by month (2016)")
  tweets_by_month = generate_graph_img(plt)

  ####### Import positive an negative words dictionaries
  # PENSER A TRADUIRE TOUS SES MOTS EN FRANCAIS

  # positive words
  positive_words = pd.read_csv("helpers/datasets/positive_words.txt", header=None)
  # negative words
  negative_words = pd.read_csv("helpers/datasets/negative_words.txt", header=None)

  # Convert words to lists
  def convert_words_list(df) :
      words = string_manipulation(df,0)
      words_list = words[words[0] != ""][0].tolist()
      return words_list

  positive_words_list = convert_words_list(positive_words)

  # Remove word trump from positive word list
  positive_words_list = [i for i in positive_words_list if i not in "trump"]
  negative_words_list = convert_words_list(negative_words)

  print ( "positive words : " ) # Afficher les 100 premiers mots positifs de la liste
  print (positive_words_list[:100])
  print ( "negative words : " )
  print (negative_words_list[:100]) # Afficher les 100 premiers mots négatifs de la liste

  # Scoring tweets
  # scoring tweets based on positive and negative words count.
  # score = positive_count - negative_count

  # function to score tweets based on positive and negative words present
  def scoring_tweets(data_frame,text_column) :
      #identifying +ve and -ve words in tweets
      data_frame["positive"] = data_frame[text_column].apply(lambda x:" ".join([i for i in x.split()
                                                                                if i in (positive_words_list)]))
      data_frame["negative"] = data_frame[text_column].apply(lambda x:" ".join([i for i in x.split()
                                                                                if i in (negative_words_list)]))
      #scoring
      data_frame["positive_count"] = data_frame["positive"].str.split().str.len()
      data_frame["negative_count"] = data_frame["negative"].str.split().str.len()
      data_frame["score"]          = (data_frame["positive_count"] -
                                      data_frame["negative_count"])

      # Create a new feature sentiment :
      # Positive if score is positive , Negative if score is negative , Neutral if score is 0
      def labeling(data_frame) :
          if data_frame["score"] > 0  :
              return "positive"
          elif data_frame["score"] < 0  :
              return "negative"
          elif data_frame["score"] == 0 :
              return "neutral"
      data_frame["sentiment"] = data_frame.apply(lambda data_frame:labeling(data_frame),
                                                 axis = 1)

      return data_frame

  tweets = scoring_tweets(tweets,"text")
  tweets_trump = scoring_tweets(tweets_trump,"text")
  tweets_hillary = scoring_tweets(tweets_hillary,"text")

  tweets[["text","positive","negative","positive_count",
                "negative_count","score","sentiment"]].head()

  ##### Scores distribution
  score_dist = tweets[tweets["is_retweet"] ==
                      False].groupby("handle")["score"].value_counts().to_frame()
  score_dist.columns = ["count"]
  score_dist = score_dist.reset_index().sort_values(by = "score",ascending = False)

  trace = go.Bar(x = score_dist[score_dist["handle"] == "realDonaldTrump"]["score"],
                 y = score_dist[score_dist["handle"] == "realDonaldTrump"]["count"],
                 marker = dict(line = dict(width = 1,color = "black"),
                               color = "red"),name = "Donald Trump")

  trace1 = go.Bar(x = score_dist[score_dist["handle"] == "HillaryClinton"]["score"],
                  y = score_dist[score_dist["handle"] == "HillaryClinton"]["count"],
                  marker = dict(line = dict(width = 1,color = "black"),
                               color = "blue"),name = "Hillary Clinton")

  layout = go.Layout(dict(title = "Scores distribution",
                          plot_bgcolor  = "rgb(243,243,243)",
                          paper_bgcolor = "rgb(243,243,243)",
                          xaxis = dict(gridcolor = 'rgb(255, 255, 255)',
                                       gridwidth = 2),
                          yaxis = dict(gridcolor = 'rgb(255, 255, 255)',
                                       gridwidth = 2),
                          )
                    )
  fig = go.Figure(data = [trace,trace1], layout = layout)

  ######## Sentiment distribution of tweets
  sent_dist = pd.crosstab(tweets[tweets["is_retweet"] ==
                                 False]["sentiment"],
                          tweets[tweets["is_retweet"] ==
                                 False]["handle"]).apply(lambda r: r/r.sum()*100,axis = 0)

  sent_dist = sent_dist.reset_index()

  t1 = go.Bar(x = sent_dist["sentiment"],y = sent_dist["HillaryClinton"],
              name = "Hillary Clinton",
              marker = dict(line = dict(width = 1,color = "#000000"),color = "#6666FF"))

  t2 = go.Bar(x = sent_dist["sentiment"],y = sent_dist["realDonaldTrump"],
             name = "Donald Trump",
             marker = dict(line = dict(width = 1,color = "#000000"),color = "#FF3300"))

  layout = go.Layout(dict(title = "Sentiment distribution",
                          plot_bgcolor  = "rgb(243,243,243)",
                          paper_bgcolor = "rgb(243,243,243)",
                          xaxis = dict(gridcolor = 'rgb(255, 255, 255)',
                                       gridwidth = 2,title = "sentiment"),
                          yaxis = dict(gridcolor = 'rgb(255, 255, 255)',
                                       gridwidth = 2,title = "percentage"),
                          )
                    )
  fig = go.Figure(data = [t1,t2],layout = layout)

  ####### Correlation matrix
  df_corr = tweets[tweets["is_retweet"] == False][[ 'retweet_count', 'favorite_count' ,
                          'score', "sentiment","handle" ]]

  df_corr["neutral"]   = np.where(df_corr["score"] == 0,1,0)
  df_corr["negative"]  = np.where(df_corr["score"] < 0,1,0)
  df_corr["positive"]  = np.where(df_corr["score"] > 0,1,0)

  cols = ['retweet_count','favorite_count','neutral','negative', 'positive']

  correlation_hillary  = df_corr[df_corr["handle"] == "HillaryClinton"][cols].corr()
  correlation_trump    = df_corr[df_corr["handle"] == "realDonaldTrump"][cols].corr()

  plt.figure(figsize = (12,4.5))
  plt.subplot(121)
  sns.heatmap(correlation_hillary,annot = True,cmap = "hot_r",
              linecolor = "grey",linewidths = 1)
  plt.title("Correlation matrix - Hillary")

  plt.subplot(122)
  sns.heatmap(correlation_trump,annot = True,cmap = "hot_r",
              linecolor = "grey",linewidths = 1)
  plt.title("Correlation matrix - Trump")
  correlation_matrix = generate_graph_img(plt)

  ####### Popular hashtags
  hashs_t = tweets_trump["tweets"].str.extractall(r'(\#\w+)')[0].value_counts().reset_index()
  hashs_t.columns = ["hash","count"]

  hashs_h = tweets_hillary["tweets"].str.extractall(r'(\#\w+)')[0].value_counts().reset_index()
  hashs_h.columns = ["hash","count"]

  # Plot 1
  plt.figure(figsize = (10,20))
  plt.subplot(211)
  ax = sns.barplot(x = "count" , y = "hash" ,
                   data = hashs_t[:25] , palette = "seismic",
                   linewidth = 1 , edgecolor = "k"* 25)
  plt.grid(True)
  for i,j in enumerate(hashs_t["count"][:25].values) :
      ax.text(3,i,j,fontsize = 10,color = "white")
  plt.title("Popular hashtags used by trump")

  # Plot 2
  plt.subplot(212)
  ax1 = sns.barplot(x = "count" , y = "hash" ,
                   data = hashs_h[:25] , palette = "seismic",
                   linewidth = 1 , edgecolor = "k"* 25)
  plt.grid(True)
  for i,j in enumerate(hashs_h["count"][:25].values) :
      ax1.text(.3,i,j,fontsize = 10,color = "white")
  plt.title("Popular hashtags used by hillary")
  popular_hashtags_used = generate_graph_img(plt)

  hsh_wrds_t = tweets_trump["tweets"].str.extractall(r'(\#\w+)')[0]
  hsh_wrds_h = tweets_hillary["tweets"].str.extractall(r'(\#\w+)')[0]

  def build_word_cloud(words,back_color,palette,title) :
      word_cloud = WordCloud(scale = 7,max_words = 1000,
                             max_font_size = 100,background_color = back_color,
                             random_state = 0,colormap = palette
                            ).generate(" ".join(words))
      plt.figure(figsize = (13,8))
      plt.imshow(word_cloud,interpolation = "bilinear")
      plt.axis("off")
      plt.title(title)
      return generate_graph_img(plt)

  hsh_wrds_trump = build_word_cloud(hsh_wrds_t,"black","rainbow","Hashtags - Trump")
  hsh_wrds_hillary = build_word_cloud(hsh_wrds_h,"black","rainbow","Hashtags - Hillary")

  ###### Popular twitter account references
  accounts_t = tweets_trump["tweets"].str.extractall(r'(\@\w+)')[0].value_counts().reset_index()
  accounts_t.columns = ["accounts","count"]

  accounts_h = tweets_hillary["tweets"].str.extractall(r'(\@\w+)')[0].value_counts().reset_index()
  accounts_h.columns = ["accounts","count"]

  plt.figure(figsize = (10,20))

  # Plot 1
  plt.subplot(211)
  ax = sns.barplot(x = "count" , y = "accounts" ,
                   data = accounts_t[:25] , palette = "seismic",
                   linewidth = 1 , edgecolor = "k"* 25)
  plt.grid(True)
  for i,j in enumerate(accounts_t["count"][:25].values) :
      ax.text(3,i,j,fontsize = 10,color = "white")
  plt.title("Popular twitter  account references by Trump")

  # Plot 2
  plt.subplot(212)
  ax1 = sns.barplot(x = "count" , y = "accounts" ,
                   data = accounts_h[:25] , palette = "seismic",
                   linewidth = 1 , edgecolor = "k"* 25)
  plt.grid(True)
  for i,j in enumerate(accounts_h["count"][:25].values) :
      ax1.text(3,i,j,fontsize = 10,color = "white")
  plt.title("Popular twitter  account references by Hillary")
  popular_twitter_account_references = generate_graph_img(plt)

  ##### wordcloud - accounts
  acc_wrds_t = tweets_trump["tweets"].str.extractall(r'(\@\w+)')[0]
  acc_wrds_h = tweets_hillary["tweets"].str.extractall(r'(\@\w+)')[0]

  acc_wrds_trump = build_word_cloud(acc_wrds_t,"black","rainbow","twitter account references - Trump")
  acc_wrds_hillary = build_word_cloud(acc_wrds_h,"black","rainbow","twitter account references - Hillary")

  ######## popular words in tweets
  pop_wrds_t = (tweets_trump["text"].apply(lambda x :
                                           pd.value_counts(x.split(" ")))
              .sum(axis = 0).reset_index().sort_values(by = [0],ascending = False))
  pop_wrds_t.columns = ["word","count"]
  pop_wrds_t["word"] = pop_wrds_t["word"].str.upper()

  pop_wrds_d = (tweets_hillary["text"].apply(lambda x :
                                             pd.value_counts(x.split(" ")))
              .sum(axis = 0).reset_index().sort_values(by = [0],ascending = False))
  pop_wrds_d.columns = ["word","count"]
  pop_wrds_d["word"] = pop_wrds_d["word"].str.upper()

  plt.figure(figsize = (12,25))

  # Plot 1
  plt.subplot(211)
  ax = sns.barplot(x = "count",y = "word",data = pop_wrds_t[:30],
                  linewidth = 1 ,edgecolor = "k"*30,palette = "Reds")
  plt.title("popular words in tweets - Trump")
  plt.grid(True)
  for i,j in enumerate(pop_wrds_t["count"][:30].astype(int)) :
      ax.text(.8,i,j,fontsize = 9)

  # Plot 2
  plt.subplot(212)
  ax1 = sns.barplot(x = "count",y = "word",data = pop_wrds_d[:30],
                  linewidth = 1 ,edgecolor = "k"*30,palette = "Blues")
  plt.title("popular words in tweets - Hillary")
  plt.grid(True)
  for i,j in enumerate(pop_wrds_d["count"][:30].astype(int)) :
      ax1.text(.8,i,j,fontsize = 9)

  # word cloud - popular words
  pop_wrds_trump = build_word_cloud(pop_wrds_t["word"],"black","Set1","popular words in tweets - Trump")
  pop_wrds_hillary = build_word_cloud(pop_wrds_d["word"],"black","Set1","popular words in tweets - Hillary")

  ########## Popular positive and negative words used by trump and Hillary
  def word_count(data_frame,column) :
      words = data_frame[column].str.split(expand = True)
      words = words.stack().reset_index()[0].value_counts().reset_index()
      words.columns = ["words","count"]
      words = words.sort_values(by = "count",ascending = False)
      words["words"] = words["words"].str.upper()
      return words

  pop_pos_words_t = word_count(tweets_trump,"positive")
  pop_neg_words_t = word_count(tweets_trump,"negative")

  pop_pos_words_d = word_count(tweets_hillary,"positive")
  pop_neg_words_d = word_count(tweets_hillary,"negative")

  ### Plot 1
  plt.figure(figsize = (12,22))
  plt.subplot(221)
  ax1 = sns.barplot(x = "count" , y = "words" ,
                   data = pop_pos_words_t[:20] ,
                   linewidth = 1 , edgecolor = "k"* 20)
  plt.grid(True)
  for i,j in enumerate(pop_pos_words_t["count"][:20].values) :
      ax1.text(8,i,j,fontsize = 10)
  plt.title("Popular positive words used by trump")

  ##### Plot 2
  plt.subplot(222)
  ax2 = sns.barplot(x = "count" , y = "words" ,
                   data = pop_neg_words_t[:20] ,
                   linewidth = 1 , edgecolor = "k"* 20)
  plt.grid(True)
  for i,j in enumerate(pop_neg_words_t["count"][:20].values) :
      ax2.text(8,i,j,fontsize = 10)
  plt.ylabel("")
  plt.title("Popular negative words used by trump")

  ###### Plot 3
  plt.subplot(223)
  ax3 = sns.barplot(x = "count" , y = "words" ,
                   data = pop_pos_words_d[:20] ,
                   linewidth = 1 , edgecolor = "k"* 20)
  plt.grid(True)
  for i,j in enumerate(pop_pos_words_d["count"][:20].values) :
      ax3.text(8,i,j,fontsize = 10)
  plt.title("Popular positive words used by hillary")

  ##### Plot 4
  plt.subplot(224)
  ax4 = sns.barplot(x = "count" , y = "words" ,
                   data = pop_neg_words_d[:20] ,
                   linewidth = 1 , edgecolor = "k"* 20)
  plt.grid(True)
  for i,j in enumerate(pop_neg_words_d["count"][:20].values) :
      ax4.text(8,i,j,fontsize = 10)
  plt.ylabel("")
  plt.title("Popular negative words used by hillary")

  plt.subplots_adjust(wspace = .3)
  popular_negative_words = generate_graph_img(plt)

  ######## Hashtag references by twitter accounts
  accounts = tweets["tweets"].str.extractall(r'(\@\w+)')[0].reset_index()[["level_0",0]]
  hash_tag = tweets["tweets"].str.extractall(r'(\#\w+)')[0].reset_index()[["level_0",0]]
  lf = hash_tag.merge(accounts,left_on = "level_0",right_on = "level_0",how = "left")[["0_x","0_y"]]
  rt = accounts.merge(hash_tag,left_on = "level_0",right_on = "level_0",how = "left")[["0_x","0_y"]]
  lf = lf.rename(columns = {"0_y" : "accs","0_x" : "hashs"})[["hashs","accs"]]
  rt = rt.rename(columns = {"0_x" : "accs","0_y" : "hashs"})[["hashs","accs"]]
  newdat = pd.concat([lf,rt],axis = 0)

  def connect_hash_acc(word,connect_type) :

      if connect_type == "hashtag_to_account" :
          df = newdat[newdat["hashs"] == word]
          df = df[df["accs"].notnull()]
      elif connect_type == "account_to_hashtag" :
          df = newdat[newdat["accs"] == word]
          df = df[df["hashs"].notnull()]

      G  = nx.from_pandas_edgelist(df,"hashs","accs")
      plt.figure(figsize = (13,10))
      nx.draw_networkx(G,with_labels = True,font_size = 10,
                       font_color = "k",
                       font_family  = "DejaVu Sans",
                       node_shape  = "h",node_color = "b",
                       node_size = 1000,linewidths = 10,
                       edge_color = "grey",alpha = .6)

  connect_hash_acc("@realDonaldTrump","account_to_hashtag")
  connect_hash_acc("@FoxNews","account_to_hashtag")
  connect_hash_acc("#MakeAmericaGreatAgain","account_to_hashtag")
  connect_hash_acc("#RNCinCLE","hashtag_to_account")
  connect_hash_acc("#MAGA","hashtag_to_account")

  ######### Positive word references by Trump
  pw_t =  tweets_trump["positive"].str.split(expand = True).stack().reset_index()[0].str.upper()
  pw_d =  tweets_hillary["positive"].str.split(expand = True).stack().reset_index()[0].str.upper()
  pw_trump = build_word_cloud(pw_t,"black","cool","positive word references by trump")
  pw_hillary = build_word_cloud(pw_d,"black","cool","positive word references by hillary")

  ######## negative word references by trump
  nw_t =  tweets_trump["negative"].str.split(expand = True).stack().reset_index()[0].str.upper()
  nw_d =  tweets_hillary["negative"].str.split(expand = True).stack().reset_index()[0].str.upper()
  nw_trump = build_word_cloud(nw_t,"black","cool","negative word references by trump")
  nw_hillary = build_word_cloud(nw_d,"black","cool","negative word references by hillary")

  ######## Sentiment of tweets by hour of day
  st_hr_t = pd.crosstab(tweets_trump["hour"],tweets_trump["sentiment"])
  st_hr_t = st_hr_t.apply(lambda r:r/r.sum()*100,axis = 1)

  st_hr_d = pd.crosstab(tweets_hillary["hour"],tweets_hillary["sentiment"])
  st_hr_d = st_hr_d.apply(lambda r:r/r.sum()*100,axis = 1)

  # Plot 1
  st_hr_t.plot(kind = "bar",figsize = (14,7),color = ["r","b","g"],
                linewidth = 1,edgecolor = "w",stacked = True)
  plt.legend(loc = "best",prop = {"size" : 13})
  plt.title("Sentiment of tweets by hour of day - Trump")
  plt.xticks(rotation = 0)
  plt.ylabel("percentage")

  # Plot 2
  st_hr_d.plot(kind = "bar",figsize = (14,7),color = ["r","b","g"],
                linewidth = 1,edgecolor = "w",stacked = True)
  plt.legend(loc = "best",prop = {"size" : 13})
  plt.title("Sentiment of tweets by hour of day - hillary")
  plt.xticks(rotation = 0)
  plt.ylabel("percentage")
  sentiment_of_tweets = generate_graph_img(plt)

  lst =  ['negative', 'positive' ,'neutral']
  cs  =  ["r","g","b"]

  plt.figure(figsize = (13,13))

  for i,j,k in itertools.zip_longest(lst,range(len(lst)),cs) :

      plt.subplot(2,2,j+1)

      plt.scatter(x = tweets_trump[tweets_trump["sentiment"] == i]["favorite_count"],
                  y = tweets_trump[tweets_trump["sentiment"] == i]["retweet_count"],
                  label = "Trump",linewidth = .7,edgecolor = "w",s = 60,alpha = .7)

      plt.scatter(x = tweets_hillary[tweets_hillary["sentiment"] == i]["favorite_count"],
                  y = tweets_hillary[tweets_hillary["sentiment"] == i]["retweet_count"],
                  label = "Hillary",linewidth = .7,edgecolor = "w",s = 60,alpha = .7)

      plt.title(i + " - tweets")
      plt.legend(loc = "best",prop = {"size":12})
      plt.xlabel("favorite count")
      plt.ylabel("retweet count")

  ##### Average retweets and favorites by sentiment
  avg_fv_rts_t = tweets_trump.groupby("sentiment")[["retweet_count", "favorite_count"]].mean()
  avg_fv_rts_h = tweets_hillary.groupby("sentiment")[["retweet_count", "favorite_count"]].mean()

  # Plot 1
  avg_fv_rts_t.plot(kind = "bar",figsize = (12,6),linewidth = 1,edgecolor = "k")
  plt.xticks(rotation = 0)
  plt.ylabel("average")
  plt.title("Average retweets and favorites by sentiment - Trump")

  # Plot 2
  avg_fv_rts_h.plot(kind = "bar",figsize = (12,6),linewidth = 1,edgecolor = "k")
  plt.xticks(rotation = 0)
  plt.ylabel("average")
  plt.title("Average retweets and favorites by sentiment - Hillary")

  average_retweets = generate_graph_img(plt)

  def return_dtm(df,column) :

      documents  = df[column].tolist()
      vectorizer = CountVectorizer()
      vec = vectorizer.fit_transform(documents)
      dtm  = pd.DataFrame(vec.toarray(), columns = vectorizer.get_feature_names())
      dtm  = df[[column,"sentiment"]].merge(dtm, left_index = True, right_index = True, how = "left")
      dtm["sentiment"]  = dtm["sentiment"].map({"neutral" : 1,"positive" : 2, "negative" : 3})

      return dtm

  dtm_trump = return_dtm(tweets_trump,"text")
  dtm_hillary = return_dtm(tweets_hillary, "text")
  # Rename column tetx_x by text
  dtm_hillary = dtm_hillary.rename(columns = {"text_x" : "text"})

  dtm_trump.head(3).style.set_properties(**{}).set_caption("DTM - Trump")
  dtm_hillary.head(3).style.set_properties(**{}).set_caption("DTM - Hillary")

  #############################################################################
  ################# Modeling and Machine learning #############################
  #############################################################################

  def split_data(dtm_df) :

      # Dependent variables and Independent variables
      predictors = [i for i in dtm_df.columns if i not in ["text"] + ["sentiment"]]
      target     = "sentiment"

      # Split the dataset into traing set and test set
      train, test = train_test_split(dtm_df, test_size = .25,
                                    stratify = dtm_df[["sentiment"]],
                                    random_state  = 123)
      # Define predictors and target on the training set and test set
      X_train = train[predictors]
      Y_train = train[target]
      X_test  = test[predictors]
      Y_test  = test[target]

      return X_train, Y_train, X_test, Y_test

  X_train_trp, Y_train_trp, X_test_trp, Y_test_trp = split_data(dtm_trump)
  X_train_hil, Y_train_hil, X_test_hil, Y_test_hil = split_data(dtm_hillary)

  # Plot
  x      = [Y_train_trp, Y_test_trp, Y_train_hil, Y_test_hil]
  titles = ["train_data - trump","test_data - trump",
            "train_data - hillary","test_data - hillary"]

  plt.figure(figsize = (12,12))
  for i,j,k in itertools.zip_longest(x,range(len(x)),titles) :
      plt.subplot(2,2,j+1)
      counts = i.value_counts().reset_index()
      counts.columns = ["sentiment","count"]
      counts["sentiment"] = counts["sentiment"].map({1 : "neutral",2 : "positive" ,
                                                      3 : "negative" })
      plt.pie(x = counts["count"] ,labels = counts["sentiment"],autopct  = "%1.0f%%",
              wedgeprops = {"linewidth" : 1,"edgecolor" : "black"},
              colors = sns.color_palette("RdBu",7)) # color = muted, RdBu, prism, Blues_d
      plt.title(k)

  def classifier(X_train,Y_train,X_test,Y_test) :
      rfc = RandomForestClassifier(max_depth = 1000,max_features = 2000,
                                   n_estimators = 10,random_state = 123)
      rfc.fit(X_train, Y_train)
      predictions = rfc.predict(X_test)

      print ("accuracy_score  : ",accuracy_score(predictions, Y_test))
      print ("recall_score    : ",recall_score(predictions,Y_test,average = "macro"))
      print ("precision_score : ",precision_score(predictions, Y_test, average = "macro"))
      print ("f1_score        : ",f1_score(predictions,Y_test, average = "macro"))

      plt.figure(figsize = (8,6))
      sns.heatmap(confusion_matrix(predictions, Y_test),annot = True,
                  xticklabels= [ "neutral","positive","negative"],
                  yticklabels= [ "neutral","positive","negative"],
                  fmt = "d",linecolor = "w",linewidths = 2)
      plt.title("confusion matrix")
      return generate_graph_img(plt)

  # Classify trump tweets
  classifier_trump = classifier(X_train_trp, Y_train_trp, X_test_trp, Y_test_trp)

  # Classify hillary tweets
  classifier_hillary = classifier(X_train_hil, Y_train_hil, X_test_hil, Y_test_hil)

  def network_tweets(df,frequency,color,title) :
      # documents
      documents  = df[df["lang"] == "English"]["text"].tolist()
      vectorizer = CountVectorizer()
      vec        = vectorizer.fit_transform(documents)
      vec_t      = vectorizer.fit_transform(documents).transpose()

      # adjecency matrix for words
      adj_mat    = pd.DataFrame((vec_t * vec).toarray(),
                                columns = vectorizer.get_feature_names(),
                                index    = vectorizer.get_feature_names()
                               )
      # stacking combinations
      adj_mat_stack   = adj_mat.stack().reset_index()
      adj_mat_stack.columns = ["link_1","link_2","count"]

      # drop same word combinations
      adj_mat_stack   = adj_mat_stack[adj_mat_stack["link_1"] !=
                                      adj_mat_stack["link_2"]]

      # subset dataframe with combination count greater than 25 times
      network_sub = adj_mat_stack[adj_mat_stack["count"] > frequency]

      # plot network
      H = nx.from_pandas_edgelist(network_sub,"link_1","link_2",["count"],
                                  create_using = nx.DiGraph())

      ax = plt.figure(figsize = (11,11))
      nx.draw(H,with_labels = True,alpha = .7,node_shape = "H",
              width = 1,node_color = color,
              font_weight = "bold",style = "solid", arrowsize = 15 ,
              font_color = "white",linewidths = 10,edge_color = "grey",
              node_size = 1300,pos = nx.kamada_kawai_layout(H))
      plt.title(title,color = "white")
      ax.set_facecolor("k")
      return generate_graph_img(plt)

  tweets_trump = network_tweets(tweets_trump,25,"#FF3300","Network analysis of tweet words - Trump")
  tweets_hillary = network_tweets(tweets_hillary,25,"#6666FF","Network analysis of tweet words - Hillary")

  ############ autres modeles ###############################################

  def log_reg(X_train,Y_train,X_test,Y_test) :

      logistic_regression = LogisticRegression(random_state = 0)
      logistic_regression.fit(X_train, Y_train)
      predictions = logistic_regression.predict(X_test)

      print ("accuracy_score  : ",accuracy_score(predictions,Y_test))
      print ("recall_score    : ",recall_score(predictions,Y_test,average = "macro"))
      print ("precision_score : ",precision_score(predictions,Y_test,average = "macro"))
      print ("f1_score        : ",f1_score(predictions,Y_test,average = "macro"))

      plt.figure(figsize = (8,6))
      sns.heatmap(confusion_matrix(predictions, Y_test),annot = True,
                  xticklabels= [ "neutral","positive","negative"],
                  yticklabels= [ "neutral","positive","negative"],
                  fmt = "d",linecolor = "w",linewidths = 2)
      plt.title("confusion matrix")
      return generate_graph_img(plt)

  log_reg_trump = log_reg(X_train_trp, Y_train_trp, X_test_trp, Y_test_trp)
  log_reg_hillary = log_reg(X_train_hil, Y_train_hil, X_test_hil, Y_test_hil)

  def svm(X_train,Y_train,X_test,Y_test) :

      svm = LinearSVC()
      svm.fit(X_train, Y_train)
      predictions = svm.predict(X_test)

      print ("accuracy_score  : ",accuracy_score(predictions, Y_test))
      print ("recall_score    : ",recall_score(predictions,Y_test,average = "macro"))
      print ("precision_score : ",precision_score(predictions,Y_test,average = "macro"))
      print ("f1_score        : ",f1_score(predictions,Y_test,average = "macro"))

      plt.figure(figsize = (8,6))
      sns.heatmap(confusion_matrix(predictions, Y_test),annot = True,
                  xticklabels= [ "neutral","positive","negative"],
                  yticklabels= [ "neutral","positive","negative"],
                  fmt = "d",linecolor = "w",linewidths = 2)
      plt.title("confusion matrix")

      print("Classification_report: ", classification_report(predictions, Y_test))
      return generate_graph_img(plt)

  svm_trump = svm(X_train_trp, Y_train_trp, X_test_trp, Y_test_trp)
  svm_hillary = svm(X_train_hil, Y_train_hil, X_test_hil, Y_test_hil)

  return {
    'retweets': f'data:image/png;base64,{retweets}',
    'languages_used': f'data:image/png;base64,{languages_used}',
    'original_authors_retweets': f'data:image/png;base64,{original_authors_retweets}',
    'tweets_by_month': f'data:image/png;base64,{tweets_by_month}',
    'correlation_matrix': f'data:image/png;base64,{correlation_matrix}',
    'popular_hashtags_used': f'data:image/png;base64,{popular_hashtags_used}',
    'hsh_wrds_trump': f'data:image/png;base64,{hsh_wrds_trump}',
    'hsh_wrds_hillary': f'data:image/png;base64,{hsh_wrds_hillary}',
    'popular_twitter_account_references': f'data:image/png;base64,{popular_twitter_account_references}',
    'acc_wrds_trump': f'data:image/png;base64,{acc_wrds_trump}',
    'acc_wrds_hillary': f'data:image/png;base64,{acc_wrds_hillary}',
    'pop_wrds_trump': f'data:image/png;base64,{pop_wrds_trump}',
    'pop_wrds_hillary': f'data:image/png;base64,{pop_wrds_hillary}',
    'popular_negative_words': f'data:image/png;base64,{popular_negative_words}',
    'pw_trump': f'data:image/png;base64,{pw_trump}',
    'pw_hillary': f'data:image/png;base64,{pw_hillary}',
    'nw_trump': f'data:image/png;base64,{nw_trump}',
    'nw_hillary': f'data:image/png;base64,{nw_hillary}',
    'sentiment_of_tweets': f'data:image/png;base64,{sentiment_of_tweets}',
    'average_retweets': f'data:image/png;base64,{average_retweets}',
    'classifier_trump': f'data:image/png;base64,{classifier_trump}',
    'classifier_hillary': f'data:image/png;base64,{classifier_hillary}',
    'tweets_trump': f'data:image/png;base64,{tweets_trump}',
    'tweets_hillary': f'data:image/png;base64,{tweets_hillary}',
    'log_reg_trump': f'data:image/png;base64,{log_reg_trump}',
    'log_reg_hillary': f'data:image/png;base64,{log_reg_hillary}',
    'svm_trump': f'data:image/png;base64,{svm_trump}',
    'svm_hillary': f'data:image/png;base64,{svm_hillary}',
  }


#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'breakingRecords' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY scores as parameter.
#

def breakingRecords(scores):
    return_list = list()
    record_high = record_low = breake_record = bad_record = 0

    for point in scores:
        if point > record_high:
            if record_high != 0:
                breake_record += 1
            record_high = point
        if point < record_low or record_low == 0:
            if record_low != 0:
                bad_record += 1
            record_low = point

    return_list.append(breake_record)
    return_list.append(bad_record)
    return return_list

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    scores = list(map(int, input().rstrip().split()))

    result = breakingRecords(scores)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()


# -*- coding: utf-8 -*-
"""
Created on Sun Dec  8 13:35:52 2019

This code is for testing different hand detection techniques.

Input: Images will be aquired from live camera feed.

Desired Output: A single hand contour which will only consist of the hand and there will be no noise in the background

Latest Output: - Still the output is rough with noise in the background

@author: Himanshu S (MegaPanda)
"""

import cv2
import numpy as np
import math

cap = cv2.VideoCapture(0)
cap.set(0,480)
font = cv2.FONT_HERSHEY_SIMPLEX

while True:
    ret, frame = cap.read()
    roi = frame[70:300,70:300]
    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    hsv  =cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    lower_red = np.array([0,30,70])#[0,20,70]
    upper_red = np.array([255,255,255])#[20,255,255]
    kernel = np.ones((3,3),np.uint8)
    mask = cv2.inRange(hsv, lower_red, upper_red)

    contours, hei = cv2.findContours(mask,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    cnt = max(contours, key = lambda x: cv2.contourArea(x))
    contour_area = cv2.contourArea(cnt)
    x,y,w,h = cv2.boundingRect(cnt)

    hull = cv2.convexHull(cnt)
    cv2.drawContours(roi,[hull],-1,(0,255,0),3)

    hull = cv2.convexHull(cnt,returnPoints = False)

    defects = cv2.convexityDefects(cnt,hull)
    count_defects = 0
    cv2.drawContours(mask, contours, -1, (0,255,0), 3)

    #cv2.imshow('',)
    mask = cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
    result = cv2.bitwise_and(roi,mask)

    cv2.imshow('img', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('roi',roi)
    cv2.imshow('result',result)
    k = cv2.waitKey(10) & 0xFF
    if k == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


# curve_fit, rotate, crop patch
import cv2
import numpy as np
import math

def get_rotated_patch(arr, coords, patch_size, k=4):
    # pad to avoid boundary
    gapx = (patch_size[0]+1)//2
    gapy = (patch_size[1]+1)//2
    arr = np.pad(arr, [[gapy,gapy],[gapx,gapx],[0,0]], mode='constant')

    # curve_fit
    mat_a = curve_fit(coords, k)

    patch_lst = []
    for coord in coords:
        x, y = coord
        # cal theta
        curve_x = y
        curve_y = 0
        for j in range(k+1):
            curve_y += int(mat_a[j] * math.pow(curve_x, j))
        tx = 0
        ty = -1
        for j in range(1, k+1):
            tx += mat_a[j] * j * math.pow(curve_x, j-1)
        # rotate
        theta = math.atan(tx/ty)
        rotate_arr = yx_rotate(arr, math.degrees(theta), (x+gapx,y+gapy))
        # crop
        patchr = rotate_arr[y+patch_size[1], x+patch_size[0], :]
        patch_lst.append(patch_lst)

    return patch_lst

def curve_fit(points, k):
    # k: polynomial params: a0->ak
    # return: mat_a: a0->ak
    N = len(points)
    mat_x = np.zeros((k+1,k+1))
    mat_y = np.zeros((k+1))
    for i in range(k+1):
        for j in range(k+1):
            for m in range(N):
                mat_x[i][j] += math.pow(points[m][1], i+j)

        for m in range(N):
            mat_y[i] += math.pow(points[m][1], i) * points[m][0]

    mat_a = np.linalg.solve(mat_x, mat_y)
    return mat_a

def yx_rotate(arr, angle, rotate_center, interpolation=cv2.INTER_LINEAR):
    # rotate arr in yx plane
    rotate_arr = []
    for i in range(arr.shape[-1]):
        slice = arr[:,:,i]
        rotate_slice = rotate_img(angle, slice, rotate_center, interpolation=interpolation)
        rotate_arr.append(np.expand_dims(rotate_slice, axis=-1))
    rotate_arr = np.concatenate(rotate_arr, axis=-1)
    return rotate_arr

def rotate_img(angle, img, rotate_center, interpolation=cv2.INTER_LINEAR):
    # rotate img based on rotate_center
    h, w = img.shape
    rotate_Mat = cv2.getRotationMatrix2D(rotate_center, angle, 1)
    rotate_img = cv2.warpAffine(img, rotate_Mat, (w,h), flags=interpolation, borderValue=(0,0,0))
    return rotate_img


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 11 13:03:10 2023

@author: yesenia
"""

lista = list( map(lambda n : int(n), input().split(" ")))

i = 0

texto = ""

while i < len(lista):
    texto += chr(lista[i])
    i += 1

print(texto)


import mido
from tkinter import *
from mido import Message

def convertToMidi(event, filename, keynote, saveName):
    mid = mido.MidiFile(type=0)
    track = mido.MidiTrack()
    mid.tracks.append(track)

    track.append(mido.MetaMessage('time_signature'))
    track.append(mido.MetaMessage('key_signature', key=keynote))

    print("Filename: " + filename)

    with open(filename, "r") as src:
        for line in src:
            command, channel, note, velocity, time = line.strip().split(",")
            if command == 'note_on' or command == 'note_off':
                msg = Message(command, channel=int(channel), note=int(note), velocity=int(velocity), time=int(time))
            elif command == 'polytouch':
                msg = Message(command, channel=int(channel), note=int(note), value=int(velocity))
            elif command == 'control_change':
                msg = Message(command, channel=int(channel), control=int(note), value=int(velocity))
            elif command == 'program_change':
                msg = Message(command, channel=int(channel), program=int(note))
            elif command == 'aftertouch':
                msg = Message(command, channel=int(channel), value=int(note))
            elif command == 'pitchwheel':
                msg = Message(command, channel=int(channel), pitch=int(note))
            elif command == 'sysex':
                msg = Message(command, data=int(channel))
            track.append(msg)

    track.append(mido.MetaMessage('end_of_track'))

    mid.save(saveName + '.mid')


import numpy as np

def runPF(dssObj, p, q, evNode, evP):
    evExtraP = np.zeros(len(p))
    for e in range(0, len(evP)):
        evExtraP[evNode[e]] = evP[e]
    p = [sum(x) for x in zip(p, evExtraP)]
    #return (p,q)
    dssCircuit = dssObj.ActiveCircuit
    dssText = dssObj.Text
    nNodes = 33

    for ind in range(2, nNodes +1, 1):
        for secInd in range(56 ,111):
            loadIdx = (ind - 2) * 55 + secInd - 55 - 1
            nodeNumber = ind * 1000 + secInd
            loadchangecommand = 'Edit Load.' + str(nodeNumber) + ' kW= ' + str(p[loadIdx]) + ' kvar= ' + str(q[loadIdx])
            dssText.Command = loadchangecommand

    dssText.Command = 'Set mode=snapshot'
    dssText.Command = 'solve'
    # DSSText.Command = 'show voltage'

    # getting bus voltages
    return dssCircuit


##
# Create a program that determines how quickly an object is travelling when it hits the
# ground. The user will enter the height from which the object is dropped in meters (m).
# Because the object is dropped its initial speed is 0m/s. Assume that the acceleration
# due to gravity is 9.8 m/s**2. You can use the formula
# vf = #  # v2 # i# + 2ad
# to compute the
# final speed, vf , when the initial speed, vi , acceleration, a, and distance, d, are known.
#
import math

d = float(input("Inserisci l'altezza da cui viene lanciato l'oggetto in metri: "))

# speed costants in m/s**2
INITIAL_SPEED = 0
ACCELERATION = 9.8

# compute final speed
final_speed = math.sqrt(INITIAL_SPEED + 2 * (ACCELERATION * d))

print("L'oggetto tocca il suolo ad una velocità di %.2f metri al sec. quadrato" %final_speed)


import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from collections import defaultdict

# Disciplines in VABB
socsci = ['Psychology', 'Educational sciences', 'Criminology', 'Economics & business',
          'Political sciences', 'Sociology', 'Social sciences general',
          'Social health sciences']
hum = ['Communication studies', 'Law', 'Philosophy', 'Theology', 'Linguistics',
       'Literature', 'History of arts', 'Archaeology', 'History',
       'Humanities general']

def only_pubs_from(df, disciplines):
    '''Filter out publications in df that are not in list of disciplines'''
    return df[df[disciplines].sum(axis=1) > 0]

def load_data(fname='data/data.csv'):
    mt = pd.read_csv(fname, encoding='utf-8')

    # Make lists of author IDs. If no author ID is available, use empty list
    mt['allauthorids'] = mt.authorids.str.split(';')
    mt['allauthorids'] = mt.allauthorids.apply(
        lambda d: d if isinstance(d, list) else []
    )

    return mt

def author_yearly_feature_counts(mt,
                                 feature,
                                 pubyear='pubyear',
                                 kind='rel'):
    '''Create tidy table of yearly counts per author for a specific feature

    The default is relative counts (percentages). Absolute counts are obtained by
    setting `kind='abs'`. This function adds the columns `n` (counts) and `cohort`
    (cohort the author belongs to). `aloi` is the author identifier.

    '''
    if kind not in ['rel', 'abs']:
        raise ValueError('kind should be either "rel" or "abs"')

    # Make 'wide' dataframe of publications, one column per author
    # based on https://stackoverflow.com/a/45901040/1534017
    tmp = mt[[pubyear, feature]].merge(
        pd.DataFrame((item for item in mt.allauthorids), index=mt.index),
        left_index=True, right_index=True, how='left')

    # Convert to 'long' format with columns:
    # aloi, pubyear, feature, count (n)
    # partially based on https://stackoverflow.com/questions/25973514/
    tmp = (
        tmp
        .set_index([pubyear, feature])
        .stack()
        .reset_index()
        .rename(columns={0: 'aloi'})
        .pivot_table(index=[pubyear, feature, 'aloi'],
                     fill_value=0, aggfunc='size')
        .unstack(level=1)
        .fillna(0)
    )
    if kind == 'rel':
        tmp = tmp.div(tmp.sum(axis=1), axis=0)
    tidy = tmp.reset_index().melt(id_vars=['aloi', pubyear],
                                  value_name='n')

    # Add column with author cohort
    author_cohorts = tidy.groupby('aloi')[pubyear].min()
    tidy_cohort = tidy.merge(author_cohorts.to_frame('cohort'),
                             left_on='aloi', right_index=True)

    return tidy_cohort

def format_year_range(start, end):
    '''Format a year range start-end (e.g., "2000-05")'''
    start, end = str(start), str(end)

    return f'{start}-{end[2:]}' if start[:2] == end[:2] else f'{start}-{end}'

def make_cohorts(first, last, cohort_length, fancy_names=True):
    '''Create dict with the cohort for each year between first and last

    Each cohort consists of `cohort_length` years. In case the length of the
    entire period cannot be exactly divided into cohorts of the given
    length, the last cohort is truncated.

    If `fancy_names` is True, cohorts have names like '2001-05'. Otherwise,
    they are just integers 1, 2, 3...

    '''
    if cohort_length < 1 or cohort_length > last - first:
        raise ValueError

    res = {}
    cohort = 1
    for i, year in enumerate(range(first, last + 1), start=1):
        res[year] = cohort
        if i % cohort_length == 0:
            cohort += 1

    if fancy_names:
        d = defaultdict(set)
        for yr, cohort in res.items():
            d[cohort].add(yr)
        res = {yr: format_year_range(min(d[cohort]), max(d[cohort]))
               for yr, cohort in res.items()}

    return res

def survivors(df, pubyear='Year'):
    '''Get dataframe retaining only survivors

    Survivors are authors that appear in each subsequent time period.
    `pubyear` is the name of the column in `df` containing publication
    year/time period.

    '''
    d = {}

    # Figure out number of authors per cohort and per time unit
    for c in df.cohort.unique():
        df_c = df[df.cohort == c]
        au_c = set(df_c.aloi)
        for y in df_c[pubyear].unique():
            df_cy = df_c[df_c[pubyear] == y]
            au_cy = set(df_cy.aloi)
            au_c &= au_cy
        d[c] = au_c

    return df[df.aloi.isin(set.union(*d.values()))].copy()

def chart_from_tidy_df(df, feature, pubyear='Year', kind='rel',
                       only_survivors=False, row=None, **kwargs):
    '''Build a cohort plot from a tidy dataframe

    Different values of `feature` will appear in different columns.
    Splitting out by some variable over multiple rows is also possible by
    supplying the column name of the variable in `row`.

    '''
    if only_survivors:
        df = survivors(df, pubyear)

    # Fix labels of axes
    ylabel = 'Mean percentage' if kind == 'rel' else 'Mean of n'
    df.rename(columns={'n': ylabel}, inplace=True)

    # Fix legend
    cohortnames = sorted(df.cohort.unique())
    df['cohort'].replace({years: f'{ch} ({years})' for ch, years in zip('ABCDEF', cohortnames)},
                         inplace=True)

    # To get the legend in the right order and make sure the order of panels
    # is consistent across plots
    df.sort_values(by=['cohort', feature], inplace=True)

    g = sns.relplot(data=df, x=pubyear, y=ylabel,
                    col=feature, row=row, kind='line',
                    style='cohort', hue='cohort',
                    markers=True, **kwargs)
    if kind == 'rel':
        plt.ylim((0, 1))

    return g

def overview_chart(mt, feature, pubyear='Year', kind='rel', **kwargs):
    '''Build single-row cohort plot from wide dataframe'''
    df = author_yearly_feature_counts(mt, feature, pubyear,
                                             kind=kind)
    return chart_from_tidy_df(df, feature, pubyear, kind, **kwargs)

def overview_chart_full(mt, feature, pubyear='Year', kind='rel', **kwargs):
    ''''Build multi-row cohort plot from wide dataframe

    This chart has 3 separate rows:
    - all disciplines together,
    - social sciences,
    - humanities.

    '''
    dfs = []

    for tmp_mt, label in [(mt, 'SSH'),
                          (only_pubs_from(mt, hum), 'Humanities'),
                          (only_pubs_from(mt, socsci), 'Soc. sciences')]:
        tmp_df = author_yearly_feature_counts(tmp_mt, feature, pubyear,
                                                     kind=kind)
        tmp_df['Discipline'] = label
        dfs.append(tmp_df)

    df = pd.concat(dfs, ignore_index=True)

    return chart_from_tidy_df(df, feature, pubyear, kind, row='Discipline', **kwargs)


import torch
import torch.nn as nn
from torchvision.models import (inception_v3,
                                Inception_V3_Weights,
                                mobilenet_v2,
                                MobileNet_V2_Weights)
from transformers import BertTokenizer, BertForSequenceClassification

def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False

def initialize_vision_model(model_name, num_classes, feature_extract, multimodal=True):
    model_ft = None
    input_size = 0

    if model_name == "inception":
        """
        InceptionV3
        Image size should be (299, 299)
        Has auxiliary output
        """
        model_ft = inception_v3(weights=Inception_V3_Weights.DEFAULT)
        set_parameter_requires_grad(model_ft, feature_extract)

        num_ftrs = model_ft.AuxLogits.fc.in_features
        model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)

        num_ftrs = model_ft.fc.in_features
        out_features = model_ft.fc.out_features
        if multimodal:
            model_ft.fc = nn.Identity()
        else:
            model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 299
        print("Initializing InceptionV3 with weights=Inception_V3_Weights.DEFAULT...")
        print(f'Input size = {input_size}')
        # print(f'out_features = {out_features}')

    elif model_name == "mobilenet_v2":
        """
        MobileNetV2 model
        Image size (224, 224)
        """

        #Set model with weights and if we're freezing the model
        model_ft = mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT)
        set_parameter_requires_grad(model_ft, feature_extract)

        #Replace the existing linear classifer with 1000 output classes with
        #number of classifiers in garbage-classification-project.
        num_ftrs = model_ft.classifier[1].in_features
        out_features = model_ft.classifier[1].out_features
        #Leave the classification to the joint classifier, so multiply by identity.
        #Otherwise, run the fully connected layer as usual.
        if multimodal:
            model_ft.classifier[1] = nn.Identity()
        else:
            model_ft.classifier[1] = nn.Linear(num_ftrs, num_classes)
        input_size = 224
        print("Initializing MobileNetV2 with weights=MobileNet_V2_Weights.DEFAULT ...")
        print(f'Input size = {input_size}')
        # print(f'out_features = {out_features}')
    else:
        print("Invalid Model Name!")
        exit()

    return model_ft, input_size

def initialize_language_model(model_name, num_classes, multimodal=False):
    model = None
    tokenizer = None
    """
    initialize and return the language model.
    """
    if model_name == "bert-base-uncased":
        tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
        if multimodal:
            model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=num_classes,
                                                            output_hidden_states=False, output_attentions=False)
        else:
            model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=num_classes)
        print("\nInitializing Bert-Base-Uncased...")
    else:
        print("Invalid language model name.")
        exit()

    return model, tokenizer


from zrb.builtin.md5 import hash_text, sum_file
import pathlib
import os

def test_sum_file():
    dir_path = pathlib.Path(__file__).parent.absolute()
    function = sum_file.to_function()
    result = function(file=os.path.join(dir_path, 'md5_test_sum.txt'))
    assert result == '5f935c38df842258336f683502ac153d'

def test_hash_text():
    function = hash_text.to_function()
    result = function(text='Philosopher Stone')
    assert result == '5f935c38df842258336f683502ac153d'


schedule = input().split(", ")

while True:
    command = input().split(":")
    option = command[0]
    if option == "course start":
        break
    task = command[1]
    if option == "Add":
        if task not in schedule:
            schedule.append(task)

    elif option == "Insert":
        index = int(command[2])
        if task not in schedule:
            schedule.insert(index, task)

    elif option == "Remove":
        if task in schedule:
            schedule.remove(task)
            if task + "Exercise" in schedule:
                schedule.remove(task + "Exercise")

    elif option == "Swap":
        task_2 = command[2]
        if task in schedule and task_2 in schedule:
            index_1 = schedule.index(task)
            index_2 = schedule.index(task_2)
            schedule[index_1], schedule[index_2] = schedule[index_2], schedule[index_1]
            if task + "-Exercise" in schedule:
                temporary_box = schedule.pop(schedule.index(task + "-Exercise"))
                schedule.insert(schedule.index(task) + 1, temporary_box)
            if task_2 + "-Exercise" in schedule:
                temporary_box = schedule.pop(schedule.index(task_2 + "-Exercise"))
                schedule.insert(schedule.index(task_2) + 1, temporary_box)

    elif option == "Exercise":
        if task + "-Exercise" and task not in schedule:
            schedule.append(task)
            schedule.append(task + "-Exercise")
        elif task + "-Exercise" not in schedule:
            schedule.insert(schedule.index(task) + 1, task + "-Exercise")

schedule = [str(index + 1) + "." + item for index, item in enumerate(schedule)]
print("\n".join(schedule))