import pandas as pd
import numpy as np
import os
import argparse

def split_df(ori_df, random_seed=42, val_ratio=0.1, test_ratio=0.1):
    num = len(ori_df)
    ids = list(range(num))
    np.random.seed(random_seed)
    np.random.shuffle(ids)
    val_num = int(num * val_ratio)
    test_num = int(num * test_ratio)
    train_num = num - val_num - test_num
    train_ids = ids[:train_num]
    val_ids = ids[train_num:train_num + val_num]
    test_ids = ids[-test_num:]
    return ori_df.iloc[train_ids], ori_df.iloc[val_ids], ori_df.iloc[test_ids]

def get_cifs(cif_dir, cif_list):
    res = []
    for cif in cif_list:
        with open(os.path.join(cif_dir, cif),'r') as f:
            l = f.read()
            res.append(l)
    return res

def main(args):
    ori_csv = os.path.join(args.dir, args.csv)
    ori_df = pd.read_csv(ori_csv, header=0)
    train_df, val_df, test_df = split_df(ori_df, random_seed=args.random_seed, val_ratio=args.val, test_ratio=args.test)
    train_df['cif'] = get_cifs(os.path.join(args.dir,'cif'), train_df['name'])
    val_df['cif'] = get_cifs(os.path.join(args.dir,'cif'), val_df['name'])
    test_df['cif'] = get_cifs(os.path.join(args.dir,'cif'), test_df['name'])
    train_df['material_id'] = [_.split('.')[0] for _ in train_df['name']]
    val_df['material_id'] = [_.split('.')[0] for _ in val_df['name']]
    test_df['material_id'] = [_.split('.')[0] for _ in test_df['name']]
    train_df.to_csv(os.path.join(args.dir, 'train.csv'))
    val_df.to_csv(os.path.join(args.dir, 'val.csv'))
    test_df.to_csv(os.path.join(args.dir, 'test.csv'))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir', required=True)
    parser.add_argument('--csv', required=True)
    parser.add_argument('--random_seed', default=42, type=int)
    parser.add_argument('--val', default=0.1, type=float)
    parser.add_argument('--test', default=0.1, type=float)
    args = parser.parse_args()
    main(args)


from googleads import adwords

from harvest_app.api_calls import accounts, campaigns
from harvest_app.api_calls import campaignCriterion, locationCriterion

def createClient():
    """
    Loads in the credentials from the googleads.yaml file.
    """
    client = adwords.AdWordsClient.LoadFromStorage(path='data/config file API/googleads.yaml')
    return client

def createService(client, type, version='v201809'):
    if type == 'account':
        service = client.GetService('ManagedCustomerService', version=version)
        return service
    elif type == 'campaign':
        service = client.GetService('CampaignService', version=version)
        return service
    elif type == 'locationCriterion':
        service = client.GetService('LocationCriterionService', version=version)
        return service
    elif type == 'campaignCriterion':
        service = client.GetService('CampaignCriterionService', version=version)
        return service
    else:
        pass
# ------------------------------------------------------------------------------
def loadAccounts():
    client = createClient()
    service = createService(client, 'account')
    # Get all accounts.
    accountList = accounts.getAllAccounts(service)
    return accountList
# ------------------------------------------------------------------------------
def loadCampaigns():
    client = createClient()
    service = createService(client, 'campaign')
    # Get all campaigns.
    campaignList = campaigns.getAllCampaigns(service)
    return campaignList

def load1Campaign(id):
    client = createClient()
    service = createService(client, 'campaign')
    # Get 1 campaign by ID.
    campaign = campaigns.getCampaignById(service, id)
    return campaign
# ------------------------------------------------------------------------------
def loadCampaignCriteriaWithBid(campaignId):
    client = createClient()
    service = createService(client, 'campaignCriterion')
    # Get campaign location criteria in a dictionary.
    cDict = campaignCriterion.getCriteriaWithBid(service, campaignId)
    return cDict
# ------------------------------------------------------------------------------
def addCampaignCriteria(campaignId, postcode, targetList, bidModifier=0, returnOp=False):
    client = createClient()
    serviceL = createService(client, 'locationCriterion')
    serviceC = createService(client, 'campaignCriterion')
    # Look up location IDs.
    newLocs = locationCriterion.lookUpLocationsByName(serviceL, postcode)
    # Add location criteria to a campaign ID.
    temp = campaignCriterion.addCriteria(serviceC, campaignId, newLocs,
        targetList, bidModifier, returnOp=returnOp)
    return temp

def removeCampaignCriteria(campaignId, postcodes, targetList):
    client = createClient()
    serviceL = createService(client, 'locationCriterion')
    serviceC = createService(client, 'campaignCriterion')
    # Look up location IDs.
    locIds = locationCriterion.lookUpLocationsByName(serviceL, postcodes)
    # Remove location criteria on a campaign ID, using the location IDs.
    temp = campaignCriterion.removeCriteria(serviceC, campaignId, locIds, targetList)
    return temp

def updateCampaignCriteria(campaignId, postcodes, targetList, bidMod, returnOp=False):
    client = createClient()
    serviceL = createService(client, 'locationCriterion')
    serviceC = createService(client, 'campaignCriterion')
    # Look up location IDs.
    locIds = locationCriterion.lookUpLocationsByName(serviceL, postcodes)
    # Update bid modifier of a campaign criteria.
    temp = campaignCriterion.updateCriteria(serviceC, campaignId, locIds,
        targetList, bidMod, returnOp=returnOp)
    return temp

def callCampCritApi(operationsList):
    client = createClient()
    service = createService(client, 'campaignCriterion')
    temp = campaignCriterion.callCampaignCritApi(service, operationsList)
    return temp


import cv2
import os
import numpy as np
def to_bin(data):
    """Convert `data` to binary format as string"""
    if isinstance(data, str):
        return ''.join([ format(ord(i), "08b") for i in data ])
    elif isinstance(data, bytes) or isinstance(data, np.ndarray):
        return [ format(i, "08b") for i in data ]
    elif isinstance(data, int) or isinstance(data, np.uint8):
        return format(data, "08b")
    else:
        raise TypeError("Type not supported.")

def encode(image_name, secret_data):
    # read the image
    image = cv2.imread(image_name)
    # maximum bytes to encode
    n_bytes = image.shape[0] * image.shape[1] * 3 // 8
    print("[*] Maximum bytes to encode:", n_bytes)
    if len(secret_data) > n_bytes:
        raise ValueError("[!] Insufficient bytes, need bigger image or less data.")
    print("[*] Encoding data...")
    # add stopping criteria
    secret_data += "====="
    data_index = 0
    # convert data to binary
    binary_secret_data = to_bin(secret_data)
    # size of data to hide
    data_len = len(binary_secret_data)
    for row in image:
        for pixel in row:
            # convert RGB values to binary format
            r, g, b = to_bin(pixel)
            # modify the least significant bit only if there is still data to store
            if data_index < data_len:
                # least significant red pixel bit
                pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)
                data_index += 1
            if data_index < data_len:
                # least significant green pixel bit
                pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)
                data_index += 1
            if data_index < data_len:
                # least significant blue pixel bit
                pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)
                data_index += 1
            # if data is encoded, just break out of the loop
            if data_index >= data_len:
                break
    return image

def decode(image_name):
    print("[+] Decoding...")
    # read the image
    image = cv2.imread(image_name)
    binary_data = ""
    for row in image:
        for pixel in row:
            r, g, b = to_bin(pixel)
            binary_data += r[-1]
            binary_data += g[-1]
            binary_data += b[-1]
    # split by 8-bits
    all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
    # convert from bits to characters
    decoded_data = ""
    for byte in all_bytes:
        decoded_data += chr(int(byte, 2))
        if decoded_data[-5:] == "=====":
            break
    return decoded_data[:-5]
#_________________________________data input__________________
from tkinter import filedialog as ttt
def ss(L):

    r=""
    for i in range (len(L)):
        r=r+L[i]
    return r

enc = "utf-8"

openfilename1=ttt.askopenfilename(filetypes=[("all files", "*")])
try:
    a1=(openfilename1.encode(enc))
    a1=str(a1)
    a1=list(a1)
    del a1[0]
    del a1[0]
    del a1[len(a1)-1]
    a1=ss(a1)
    a33=ttt.askdirectory()
    a3=(a33.encode(enc))
    a3=str(a3)
    a3=list(a3)
    del a3[0]
    del a3[0]
    del a3[len(a3)-1]
    a3=ss(a3)
except:
    print("try again")

#_________________________________________________________

if __name__ == "__main__":
    input_image = a1
    print(input_image)
    no=input("name of output image with extension")
    output_image = a3+"/"+no
    print(output_image)
    secret_data = input("text")
    # encode the data into the image
    encoded_image = encode(image_name=input_image, secret_data=secret_data)
    # save the output image (encoded image)
    cv2.imwrite(output_image, encoded_image)
    # decode the secret data from the image
    decoded_data = decode(output_image)
    print("[+] Decoded data:", decoded_data)


#!/usr/local/bin/python
import helpers    # you need to use load_csv_data
import api    # use train/predict method below
import numpy as np
import implementations
import helpers

standardization = False

def run():
	print("START")

	# load full dataset
	print("loading train set...")
	y_full, x_full, _ = helpers.load_csv_data('data/train.csv', sub_sample = False, background_value = -1)

	if standardization == True:
		y_full, _, _ = helpers.standardize_by_mean(y_full)
		x_full, _, _ = helpers.standardize_by_mean(x_full)
	# train the model with chosen parameters

	#LAMBDAS = lambdas_star
	#DEGREES = degrees_star

	LAMBDAS = [1.59985871961e-05, 3.23745754282e-05, 0.152641796718, 3.23745754282e-05, 0.0184206996933, 7.90604321091e-06, 0.625055192527, 0.00910298177992]
	DEGREES = [2, 2, 13, 2, 2, 3, 3, 3]

	# check accuracy
	print("checking accuracy...")
	w_cv = api.train(y_full, x_full, poly=DEGREES, split_method='mass', replace=None, cv=True, cut=0., \
		model_func=implementations.ridge_regression, lambdas = LAMBDAS)
	print("--------------------------------------------")
	print("Kaggle summary")
	print("  method: mass, replace: None, method: RR")
	print("  lambdas: ", LAMBDAS)
	print("  degrees: ", DEGREES)
	print("  accuracy mean :" ,np.mean(np.array(w_cv)[:,3]))
	print("  details: ", np.array(w_cv)[:,3])
	print("--------------------------------------------")

	# train de model
	print("training model...")
	w = api.train(y_full, x_full, poly=DEGREES, split_method='mass', replace=None, cv=False, cut=0., \
		model_func=implementations.ridge_regression, lambdas = LAMBDAS)

	# load test set
	print("loading test set...")
	test_y, test_x, test_ids = helpers.load_csv_data('data/test.csv')

	if standardization == True:
		test_y, _, _ = helpers.standardize_by_mean(test_y)
		test_x, _, _ = helpers.standardize_by_mean(test_x)

	# do the final prediction
	print("predicting...")
	final_pred = api.predict(test_y, test_x, test_ids, 0., w, poly=DEGREES, split_method='mass', \
		replace=None, loss_method='ls', res_to_file=True)

	print("Prediction produced in file predict_split.csv")
	print("FINISH")

if __name__ == '__main__':
	run()


"""Создать текстовый файл test_file.txt, заполнить его тремя строками: «сетевое
программирование», «сокет», «декоратор». Проверить кодировку файла по умолчанию.
Принудительно открыть файл в формате Unicode и вывести его содержимое.
"""

import locale

def write_file(file, lines):
    with open(file, 'w') as f:
        for line in lines:
            f.write(line + '\n')

def print_file(file, encoding):
    with open(file, 'r', encoding=encoding) as f:
        print(f.read())

LINES = ['сетевое программирование', 'сокет', 'декоратор']
FILE = 'test_file.txt'
ENCODING = ['utf-8', 'cp1251']

print(f'Кодировка по умолчанию: {locale.getpreferredencoding()}', '\n')

write_file(FILE, LINES)
for i in ENCODING:
    print(f'Кодировка: {i}')
    try:
        print_file(FILE, i)
    except UnicodeDecodeError as e:
        print(e)
    finally:
        print()


# Python program to illustrate the concept
# of threading
import threading
import os
import time

# global variable
g_id = 100
lock = threading.Lock()
sleep_duration = 1

# Enable synchronous execution by setting enable_lock to 1
enable_sync = 1

def up():
    global g_id
    # auto_lock = ClsLock()
    g_id += 1

def down():
    global g_id
    # auto_lock = ClsLock()
    g_id -= 1

# ------------------------------
class ClsLock:

    # Initializing
    def __init__(self):
        if bool(enable_sync):
            print('Lock Acquired.')
            lock.acquire()

    # Deleting (Calling destructor)
    def __del__(self):
        if bool(enable_sync):
            print('Lock released.')
            lock.release()
# ------------------------------

def task1():
    print("Task 1 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 1: {}".format(threading.current_thread().ident))
    global g_id, sleep_duration
    synclock = ClsLock()
    print("Task 1 - BEFORE Updated ID: ", g_id)
    up()
    time.sleep(sleep_duration)
    down()
    print("Task 1 - AFTER Updated ID: ", g_id)
    del synclock  # Explicitly release lock

def task2():
    print("Task 2 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 2: {}".format(threading.current_thread().ident))
    global g_id, sleep_duration
    synclock = ClsLock()  # This will auto release lock when goes out of scope
    print("Task 2 - BEFORE Updated ID: ", g_id)
    up()
    time.sleep(sleep_duration)
    down()
    print("Task 2 - AFTER Updated ID: ", g_id)

def task3():
    print("Task 3 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 2: {}".format(threading.current_thread().ident))
    global g_id, sleep_duration
    synclock = ClsLock()  # This will auto release lock when goes out of scope
    print("Task 3 - BEFORE Updated ID: ", g_id)
    up()
    time.sleep(sleep_duration)
    down()
    print("Task 3 - AFTER Updated ID: ", g_id)

def task4():
    print("Task 4 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 4: {}".format(threading.current_thread().ident))
    global g_id, sleep_duration
    synclock = ClsLock()  # This will auto release lock when goes out of scope
    print("Task 4 - BEFORE Updated ID: ", g_id)
    up()
    time.sleep(sleep_duration)
    down()
    print("Task 4 - AFTER Updated ID: ", g_id)

def task5():
    print("Task 5 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 5: {}".format(threading.current_thread().ident))
    global g_id, sleep_duration
    synclock = ClsLock()  # This will auto release lock when goes out of scope
    print("Task 5 - BEFORE Updated ID: ", g_id)
    up()
    time.sleep(sleep_duration)
    down()
    print("Task 5 - AFTER Updated ID: ", g_id)

if __name__ == "__main__":
    start = time.time()
    # print ID of current process
    print("ID of process running main program: {}".format(os.getpid()))

    # print name of main thread
    print("Main thread name: {}".format(threading.main_thread().name))

    t1 = threading.Thread(target=task1, name='t5')
    t2 = threading.Thread(target=task2, name='t2')
    t3 = threading.Thread(target=task3, name='t3')
    t4 = threading.Thread(target=task4, name='t4')
    t5 = threading.Thread(target=task5, name='t5')

    # starting threads
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t5.start()

    # wait until all threads finish
    t1.join()
    t2.join()
    t5.join()
    t4.join()
    t5.join()
    end = time.time()
    print('Elapsed time: [{0}] Sec\n'.format(end-start))


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def showClusteredData(df,K):
    data_array = np.array(df)
    dimension = len(df.columns)
    data_mean = np.mean(data_array,axis=0,dtype=float) # make sure if it is okay
    data_covar = np.cov(data_array)
    eigenvalue, eigenvector = np.linalg.eig(data_covar)
    index_max_1 = np.argmax(eigenvalue)
    eigenvalue = np.delete(eigenvalue,index_max_1)
    index_max_2 = np.argmax(eigenvalue)
    principal_components = np.empty([K,dimension])

def check():
    s = np.array([[2, 1, 2],
                  [1, 2, 3],
                  [1, 2, 3],
                  [3, 1, 5]],dtype=float)
    s[1] = np.multiply(s[1],0.2)
    print(s)

def matplot():

    # Fixing random state for reproducibility
    np.random.seed(19680801)

    N = 50
    x = np.random.rand(N)
    y = np.random.rand(N)
    colors = np.random.rand(N)
    # area = (30 * np.random.rand(N)) ** 2  # 0 to 15 point radii

    plt.scatter(x, y, c=colors, alpha=0.5)
    plt.show()

def matplot2():
    X = [1, 2, 3, 4]
    x2 = [2,3,4,5]
    Y1 = [4, 8, 12, 16]
    Y2 = [1, 4, 9, 16]

    plt.scatter(X, Y1, color='red')
    plt.scatter(x2, Y2, color='blue')
    plt.show()

def main():
    # df = pd.read_csv('data_sh.txt', delimiter='\t', header=None)
    # v = np.empty([2,3])
    # s = np.array([[2,1,2],
    #               [1,2,3],
    #               [1,2,3],
    #               [3,1,5]])
    # row = [1,2]
    # p = s[row]
    # print(p)
    # check()
    matplot2()

if __name__ == '__main__':
    main()


class Solution:
    # @return an integer
    def uniquePaths(self, m, n):
    	grid = [([0] * n) for i in range(m)]
    	grid[0]=1
    	for x in range(0,m):
    		grid[x][0] =1
    	for x in range(1,m):
    		for i in range(1,n):
    			grid[x][i] = grid[x-1][i] + grid[x][i-1]
    	return(grid[m-1][n-1])

ss= Solution()
result = ss.uniquePaths(4,4)


from typing import Any

class Node:
    def __init__(self, data: Any):
        self.data = data
        self.previous = None
        self.next = None

    def __str__(self) -> str:
        return f'{self.data}'

class DoubleLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def __iter__(self):
        """
        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_head('b')
        >>> linked_list.insert_head('a')
        >>> linked_list.insert_tail('c')
        >>> tuple(linked_list)
        ('a', 'b', 'c')
        """
        node = self.head
        while node:
            yield node.data
            node = node.next

    def __str__(self) -> str:
        """
        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_tail('a')
        >>> linked_list.insert_tail('b')
        >>> linked_list.insert_tail('c')
        >>> str(linked_list)
        'a->b->c'
        """
        return '->'.join([str(item) for item in self])

    def __len__(self) -> int:
        """
        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> for i in range(0, 5):
        ...    linked_list.insert_tail(i)
        >>> len(linked_list) == 5
        True
        """
        return len(tuple(iter(self)))

    def insert_head(self, data: Any) -> None:
        """
        Add data into the begining of the list.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_head('a')
        >>> linked_list.insert_head('b')
        >>> linked_list.insert_head('c')
        >>> str(linked_list)
        'c->b->a'
        """
        return self.insert_nth(0, data)

    def insert_tail(self, data: Any) -> None:
        """
        Add data into the end of the list.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_tail('a')
        >>> linked_list.insert_tail('b')
        >>> linked_list.insert_tail('c')
        >>> str(linked_list)
        'a->b->c'
        """
        return self.insert_nth(len(self), data)

    def insert_nth(self, index: int, data: Any) -> None:
        """
        Insert data into the nth node of the list

        :param index: index of node to insert
        :param data: data to insert

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> for i in range(0, 5):
        ...    linked_list.insert_tail(i)
        >>> str(linked_list)
        '0->1->2->3->4'
        >>> linked_list.insert_nth(2, 'second')
        >>> str(linked_list)
        '0->1->second->2->3->4'
        """
        if not 0 <= index <= len(self):
            raise IndexError('List index out of range.')

        new_node = Node(data)
        if self.head is None:  # Notice: remember to assign both head and tail.
            self.head = new_node
            self.tail = new_node
        elif index == 0:
            self.head.previous = new_node
            new_node.next = self.head
            self.head = new_node
        elif index == len(self):
            self.tail.next = new_node
            new_node.previous = self.tail
            self.tail = new_node
        else:
            temp = self.head
            for _ in range(index):
                temp = temp.next
            temp.previous.next = new_node
            new_node.previous = temp.previous
            temp.previous = new_node
            new_node.next = temp

    def delete_head(self) -> Any:
        """
        Delete the head node and return the data.
        :return: data of the head node.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_tail('a')
        >>> linked_list.insert_tail('b')
        >>> linked_list.insert_tail('c')
        >>> linked_list.delete_head()
        'a'
        """
        return self.delete_nth(0)

    def delete_tail(self) -> Any:
        """
        Delete the tail node and return the data.
        :return: data of the tail node.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_tail('a')
        >>> linked_list.insert_tail('b')
        >>> linked_list.insert_tail('c')
        >>> linked_list.delete_tail()
        'c'
        """
        return self.delete_nth(len(self) - 1)

    def delete_nth(self, index: int) -> Any:
        """
        Delete the node at the given index.

        :param index: index of node to delete.
        :return: data of the deleted node.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_tail('a')
        >>> linked_list.insert_tail('b')
        >>> linked_list.insert_tail('c')
        >>> linked_list.delete_nth(1)
        'b'
        >>> str(linked_list)
        'a->c'
        """
        if not 0 <= index <= len(self) - 1:
            raise IndexError('List index out of range')

        delete_node = self.head
        if len(self) == 1:
            self.tail = self.head = None
        elif index == 0:
            self.head = delete_node.next
            self.head.previous = None
        elif index == len(self) - 1:
            delete_node = self.tail
            self.tail = delete_node.previous
            self.tail.next = None
        else:
            for _ in range(index):
                delete_node = delete_node.next
            delete_node.previous.next = delete_node.next
            delete_node.next.previous = delete_node.previous
        return delete_node.data

    def delete(self, data: Any) -> str:
        """
        Delete the node having the given data.

        :param data: data of the node to delete.
        :return: string of the node's data or error if not found.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.insert_tail('a')
        >>> linked_list.insert_tail('b')
        >>> linked_list.insert_tail('c')
        >>> linked_list.delete('b')
        'b'
        >>> str(linked_list)
        'a->c'
        """
        current = self.head

        while current.data != data:
            if current.next is not None:
                current = current.next
            else:
                return "No data matching the given value"

        if current == self.head:
            self.delete_head()
        elif current == self.tail:
            self.delete_tail()
        else:
            current.previous.next = current.next
            current.next.precious = current.previous
        return str(current.data)

    def is_empty(self) -> bool:
        """
        Determine if the list is empty.
        :return: true when empty.

        Examples:

        >>> linked_list = DoubleLinkedList()
        >>> linked_list.is_empty()
        True
        >>> linked_list.insert_tail('a')
        >>> linked_list.is_empty()
        False
        """
        return len(self) == 0


import hashlib
import time
from transaction import Transaction
from key import verify_signature

class Block:

# =============================================================================
#
# =============================================================================
    def __init__(self, index, previousHash, signature=0, nonce=0, timestamp=0, transactions=[], hashVal=None, minerName=None):
        self.index = index
        self.previousHash = previousHash
        self.hashVal = hashVal
        self.timestamp = timestamp
        self.nonce = nonce
        self.transactions = []
        for elem in transactions:
            self.transactions.append(Transaction(elem["amount"], elem['sender'], elem['receiver'], elem['timestamp'],elem['sign'], elem["number"]))
        self.minerName = minerName
        self.signature = signature

# =============================================================================
#
# =============================================================================
    def hashFunction(self, nonce):
        sha = hashlib.sha256()
        data=""
        data = str(self.index) + str(self.timestamp) + str(nonce) + str(self.previousHash)
        for elt in self.transactions:
            data += str(elt.amount) +str(elt.sender) + str(elt.receiver)
        sha.update(data.encode())
        return sha.hexdigest()

# =============================================================================
#
# =============================================================================
    def addTransaction(self, newTransaction, wallet):
        t = newTransaction
        t.signTransaction(wallet)
        t.number = len(self.transactions)
        self.transactions.append(t)

# =============================================================================
#
# =============================================================================
    def __repr__(self):
        string = "Block number : " + str(self.index) + "\n" + \
            "Nonce: " + str(self.nonce) + "\n" + \
            "Timestamp: " + str(self.timestamp) + "\n" + \
            "Miner: " + str(self.minerName) + "\n" + \
            "Transactions: " + str(self.transactions) + "\n" + \
            "Previous Hash: " + str(self.previousHash) + "\n" + \
            "Hash: " + str(self.hashVal) + "\n" + \
            "Signature: " + str(self.signature) + "\n"
        return string
# =============================================================================
#
# =============================================================================
    def miningFunction(self, difficulty, wallet):
        self.timestamp = time.time()
        nonce = 0
        prefix = "0" * difficulty
        hashRes = self.hashFunction(nonce)

        while(hashRes.startswith(prefix)==False):
            nonce+=1
            hashRes = self.hashFunction(nonce)

        self.hashVal = hashRes
        self.nonce = nonce
        self.minerName = wallet.to_address()

        # Signature
        msg = str(self.index) + str(self.nonce) + \
            str(self.timestamp) + str(self.minerName) + \
                str(self.transactions) + str(self.previousHash) + \
                    str(self.hashVal)
        signature = wallet.sign(msg)
        self.signature = signature.hex()

        return nonce

# =============================================================================
#
# =============================================================================
    def hashVerification(self, nonce):
        computedHash = self.hashFunction(nonce)
        if computedHash != self.hashVal:
            return False
        return True

# =============================================================================
#
# =============================================================================
    def to_dict(self):
        block_dict = {}
        block_dict["index"] = self.index
        block_dict["nonce"] = self.nonce
        block_dict["timestamp"] = self.timestamp
        block_dict["minerName"] = self.minerName
        block_dict["transactions"] = []
        for elt in self.transactions:
            block_dict["transactions"].append(elt.to_dict())
        block_dict["previousHash"] = self.previousHash
        block_dict["hashVal"] = self.hashVal
        block_dict["signature"] = self.signature
        return block_dict

# =============================================================================
#
# =============================================================================
    def verifyBlockSignature(self):

        sign = self.signature
        msg = str(self.index) + str(self.nonce) + \
    str(self.timestamp) + str(self.minerName) + \
        str(self.transactions) + str(self.previousHash) + \
            str(self.hashVal)
        address = self.minerName
        if verify_signature(sign, msg, address)!=True:
            return False

        # Check transactions
        for t in self.transactions:
            if t.verify()!=True:
                return False
        return True


#程序文件Pex10_9.py
import numpy as np
import networkx as nx
import pylab as plt
p=[25,26,28,31]; a=[10,14,18,26]; r=[20,16,13,11];
b=np.zeros((5,5)); #邻接矩阵（非数学上的邻接矩阵）初始化
for i in range(5):
    for j in range(i+1,5):
        b[i,j]=p[i]+np.sum(a[0:j-i])-r[j-i-1];
print(b)
G=nx.DiGraph(b)
p=nx.dijkstra_path(G, source=0, target=4, weight='weight')  #求最短路径；
print("最短路径为:",np.array(p)+1)  #python下标从0开始
d=nx.dijkstra_path_length(G, 0, 4, weight='weight') #求最短距离
print("所求的费用最小值为：",d)
s=dict(zip(range(5),range(1,6))) #构造用于顶点标注的标号字典
plt.rc('font',size=16)
pos=nx.shell_layout(G)  #设置布局
w=nx.get_edge_attributes(G,'weight')
nx.draw(G,pos,font_weight='bold',labels=s,node_color='r')
nx.draw_networkx_edge_labels(G,pos,edge_labels=w)
path_edges=list(zip(p,p[1:]))
nx.draw_networkx_edges(G,pos,edgelist=path_edges,
            edge_color='r',width=3)
plt.savefig("figure10_9.png",pdi=500); plt.show()


"""
Author:sick
DateTime:2022/2/17 18:45
# type:openpyxl.workbook.workbook.Worksheet
# Email:goqsqulatiimdeai
"""
import json

# def Json2Csv(filename):
#     try:
#         with open(file=filename, mode="r", encoding="utf-8") as f:
#             data = json.loads(f.read())
#
if __name__ == '__main__':
    try:
        with open(file='data/input.json',mode='r') as f:
            data = json.loads(f.read())
        # output=Name,age,birthyear
        output = ','.join([*data[0]])
        # dict1 = {"obj": "world", "name": "python"}
        # print("hello {names[obj]}, this is {names[name]}.".format(names=dict1))
        # {'Name': 'Akash', 'age': 26, 'birthyear': '1994'}
        for obj in data:
            # output += '{obj["Name"]},{obj["age"]},{obj["birthyear"]}'.format(obj=[*data])
             output += '\n'+'{names[Name]},{names[age]},{names[birthyear]}'.format(names=obj)
            # output += f'\n{obj["Name"]},{obj["age"]},{obj["birthyear"]}'
            # print('{obj[0]},{obj[1]},{obj[2]}'.format(data))
            # print(output)
            # print(output)
        # print(output)
        with open(file="data/output.csv",mode="w") as f:
            f.write(output)
    except Exception as ex:
        print(f'Error:{str(ex)}')


import numpy as np
import torch
import pytorch_lightning as pl
import argparse
import os
import sys
import yaml
import warnings

from scripts import datamodule, model, callback

warnings.filterwarnings('ignore')

AVAIL_GPUS = min(1, torch.cuda.device_count())

pl.seed_everything(42)

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument('--action', help='Defines the action (train/test) a model.')

    args = parser.parse_args()

    try:
        with open('./configs/config.yaml', 'r') as f:
            cfg = yaml.safe_load(f)

    except yaml.YAMLError as exc:
        print(exc)

    if args.action == 'train':
        # Load the DataModule
        dm = datamodule.DataModule(path=cfg['path'], batch_size=cfg['batch_size'], train_percent=0.98)

        # Create the model
        classifier = model.LitClassifier(cfg)

        # Declare the Logger
        wandb_logger = pl.loggers.WandbLogger(project='is_that_santa')

        # Declare the trainer
        trainer = pl.Trainer(
            gpus=AVAIL_GPUS,
            max_epochs=cfg['epochs'],
            callbacks = [
                pl.callbacks.early_stopping.EarlyStopping(monitor='val_loss', patience=5, verbose=True),
                pl.callbacks.model_checkpoint.ModelCheckpoint(monitor='val_loss', mode='min', dirpath='models/', filename='best_model'),

                ],

            logger=wandb_logger)

        # Fit the model
        trainer.fit(classifier, dm)

    elif args.action == 'test':

        # Load the Data Module
        dm = datamodule.DataModule(path=cfg['path'], batch_size=1)
        dm.setup(stage='test')

        # Load the model from the checkpoint
        classifier = model.LitClassifier.load_from_checkpoint('models/epoch=16-step=84.ckpt', cfg=cfg)
        classifier.freeze()

        # Define the trainer
        trainer = pl.Trainer(accelerator='cpu')

        # Test the model on the test set
        trainer.test(classifier, dm)

    else:
        print(f'Sorry an action is required to proceed with the program.')


import sys
import os
import pandas as pd
import numpy as np
from argparse import ArgumentParser
sys.path.append(os.path.dirname(__file__) + "/..")
from sequence_annotation.utils.utils import get_gff_with_attribute, read_gff, write_bed

def gff_info2bed_info(cDNAs):
    # input one based data, return one based data
    id_ = set(cDNAs['name'])
    if len(id_) == 1:
        id_ = list(id_)[0]
    else:
        raise Exception("Multiple ids in cDNA cluster, get {}".format(id_))
    starts = list(cDNAs['start'])
    ends = list(cDNAs['end'])
    min_start = min(starts)
    max_end = max(ends)
    indice = np.argsort(starts)
    starts = [starts[index] for index in indice]
    ends = [ends[index] for index in indice]
    sizes = [str(end - start + 1) for start, end in zip(starts, ends)]
    for size in sizes:
        if int(size) <= 0:
            raise Exception(
                "The cDNA size in {} should be positive".format(id_))
    rel_starts = [str(start - min_start) for start in starts]
    info = dict(cDNAs.iloc[0])
    info['id'] = id_
    info['start'] = min_start
    info['end'] = max_end
    info['rgb'] = '.'
    info['score'] = '.'
    info['count'] = len(sizes)
    info['block_size'] = ",".join(sizes)
    info['block_related_start'] = ",".join(rel_starts)
    info['thick_start'] = min_start
    info['thick_end'] = min_start - 1
    return info

def cluster_gmap_cdna_to_bed(gff):
    gff = get_gff_with_attribute(gff)
    cDNA_group = gff[gff['feature'] == 'cDNA_match'].groupby('name')
    bed_info_list = []
    for _, cdnas in cDNA_group:
        bed_info = gff_info2bed_info(cdnas)
        bed_info_list.append(bed_info)
    bed = pd.DataFrame.from_dict(bed_info_list)
    return bed

def main(gff_path, bed_path):
    gff = read_gff(gff_path)
    bed = cluster_gmap_cdna_to_bed(gff)
    write_bed(bed, bed_path)

if __name__ == '__main__':
    parser = ArgumentParser(description="This program will group "
                            "cDNAs with same id together, and "
                            "write to bed format")
    parser.add_argument("-i","--gff_path",required=True,
                        help="Path of input gff file")
    parser.add_argument("-o","--bed_path",required=True,
                        help="Path of output bed file")
    args = parser.parse_args()
    kwargs = vars(args)
    main(**kwargs)


import sys

def turn(dir):
    a, b, c, d, e, f = dice[0], dice[1], dice[2], dice[3], dice[4], dice[5]
    # 동
    if dir == 1:
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = d, b, a, f, e, c
    # 서
    elif dir == 2:
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = c, b, f, a, e, d
    # 북
    elif dir == 3:
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = e, a, c, d, f, b
    # 남
    else:
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = b, f, c, d, a, e

n, m, x, y, k = map(int, sys.stdin.readline().split())
graph = []
# 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4
dx = [0, 0, 0, -1, 1]
dy = [0, 1, -1, 0, 0]
dice = [0, 0, 0, 0, 0, 0]

for _ in range(n):
    graph.append(list(map(int, sys.stdin.readline().split())))
k_list = list(map(int, sys.stdin.readline().split()))

nx, ny = x, y
for i in k_list:
    nx += dx[i]
    ny += dy[i]

    if 0 <= nx < n and 0 <= ny < m:
        turn(i)
        if graph[nx][ny] == 0:
            graph[nx][ny] = dice[-1]
        else:
            dice[-1] = graph[nx][ny]
            graph[nx][ny] = 0

        print(dice[0])
    else:
        nx -= dx[i]
        ny -= dy[i]


# from numpy.core.fromnumeric import size
import torch
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import MobileUnet
import cv2
from post_process_tool import *
from math import ceil, pi
# model = MobileUnet.MobileUnet(1)
# torch.load('best.pt', map_location='cpu')
model = torch.load('cpu_diaphragm.pth')
model.eval()  # !如果未使用评估模式,BN层和dropout会有影响
depth = [860, 704, 848, 770, 770, 815,  735, 770]
# w_start = [0, 0, 0, 233, 0, 0,  285, 420]
# w_end = [1680, 1680, 1680, 1630, 1340, 1630,  1630, 1237]

# 不限制宽度区域的
w_start = [0, 0, 0, 0, 0, 0,  0, 0]
w_end = [1680, 1680, 1680, 1680, 1680, 1680,  1680, 1680]

half_h = 555
half_roi_height = 96

line_data = np.zeros((54, 1, 192))
line_label = np.zeros((54, 2, 192))
number = 0
for picture_num in range(1, 8):
    print(f'now it\'s picture {picture_num}')
    ScreenShot = Image.open(
        f"./dataset/raw_image/diaphragm ({picture_num}).jpg")
    # width, height = ScreenShot.size
    # (left, top, right, bottom) = (0, height*0.5, 1640, 1090)
    # croped_image = ScreenShot.crop((left, top, right, bottom))

    # roi_depth = find_roi_depth(croped_image)
    roi_depth = depth[picture_num]

    # width, height = croped_image.size
    (left, top, right, bottom) = (w_start[picture_num], roi_depth -
                                  half_roi_height, w_end[picture_num], roi_depth+half_roi_height)
    roi_area = ScreenShot.crop((left, top, right, bottom))
    width, height = roi_area.size
    resize_num = 4
    x = int(width/resize_num)
    zoom = 3
    size = (zoom*x, zoom*height)
    resized_roi = roi_area.resize(size)

    # resized_roi.save(f"dataset/roi_area/diaphragm({picture_num}).jpg")
    width, height = resized_roi.size
    # plt.imshow(im)
    # plt.show()
    # im = Image.open(f"./dataset/roi_area/roi_area_{picture_num}.jpg")
    step = 192
    original_peak_x = []
    original_peak_top_y = []
    original_peak_bottom_y = []

    original_valley_x = []
    original_valley_top_y = []
    original_valley_bottom_y = []

    for patch_num in range(0, ceil((width-height)/step)+1):
        print(f'now it\'s patch {patch_num}')

        #! 完整roi切片
        patch_left = step*patch_num
        patch_top = 0
        patch_right = height+step*patch_num
        patch_bottom = height
        input_patch = resized_roi.crop(
            (patch_left, patch_top, patch_right, patch_bottom))
        #! 语义分割
        img = input_patch.convert('L')
        img = img.resize((192, 192))
        numpy_input = ((np.array(img))[
                       :, :, np.newaxis] / 255.).astype(np.float32)
        input = torch.tensor((numpy_input.transpose((2, 0, 1)))[np.newaxis, :])

        preds = model(input)
        preds = preds.data.cpu().numpy()
        pred_mask = preds[0, 0, :, :]
        a = np.sum(pred_mask, axis=0)
        b = a.sum()
        # if patch_num == 1:
        #     print(f'!!!b is {b}')
        pred_mask = np.where(pred_mask > 0, 1, 0)
        # plt.imshow(pred_mask)
        # plt.savefig(f'dataset\patch\output\\roi_area_{i}_patch_{j+1}.jpg')
        # plt.close()
        #! 分割结果轮廓提取,保留最大面积的轮廓
        pred_mask_0 = (np.array(pred_mask, dtype='uint8'))
        pred_mask = cv2.medianBlur(pred_mask_0, 15)
        contours, _ = cv2.findContours(pred_mask, cv2.RETR_TREE,
                                       cv2.CHAIN_APPROX_NONE)
        if len(contours) == 0:
            print(
                f'find no segged part in ./dataset/roi_area/roi_area_{picture_num}_patch_{patch_num+1}.jpg')
            break

        areas = []
        for contour in contours:
            areas.append(cv2.moments(contour)['m00'])
        areas = np.array(areas)
        index = np.argmax(areas)
        main_contour = contours[index]
        color = 2
        cv2.polylines(pred_mask, [main_contour], True, color, 1)
        # plt.subplot(1, 3, 1)
        # plt.imshow((np.array(img)))
        # plt.subplot(1, 3, 2)
        # plt.imshow(pred_mask_0)
        # plt.subplot(1, 3, 3)
        # plt.imshow(pred_mask)
        # plt.show()

        #! 原始上下沿提取
        length = 192
        x = np.arange(length)

        top_line = np.zeros(length)
        bottom_line = np.zeros(length)
        for i in range(0, length):
            for j in range(0, 192):
                if pred_mask[j, i] == color:
                    top_line[i] = j
                    break
            for j in range(191, 0, -1):
                if pred_mask[j, i] == color:
                    bottom_line[i] = j
                    break
        #! 获取上下沿有效区域起止点
        top_line_start = 0
        top_line_end = 0
        bottom_line_start = 0
        bottom_line_end = 0
        for i in range(length):
            if top_line[i] != 0:
                top_line_start = i
                break
        for i in range(191, 0, -1):
            if top_line[i] != 0:
                top_line_end = i
                break
        for i in range(length):
            if bottom_line[i] != 0:
                bottom_line_start = i
                break
        for i in range(191, 0, -1):
            if bottom_line[i] != 0:
                bottom_line_end = i
                break

        top_search_start = int((top_line_start+top_line_end)/2)
        bottom_search_start = int((bottom_line_start+bottom_line_end)/2)
        # 绘制上下沿的起点,终点,中点

        #! 分割区域边缘会凹凸不平
        #! 根据上下沿两端的差分,判断哪里是凹凸不平的地方,从而截断
        # TODO 差分的阈值是超参数
        top_cut_point_start = find_start_cut_point(
            top_line, top_line_start, top_search_start)
        top_cut_point_end = find_end_cut_point(
            top_line, top_line_end, top_search_start)
        bottom_cut_point_start = find_start_cut_point(
            bottom_line, bottom_line_start, bottom_search_start)
        bottom_cut_point_end = find_end_cut_point(
            bottom_line, bottom_line_end, bottom_search_start)
        # 画截断点位置和截断线
        cut_point_start = max(top_cut_point_start, bottom_cut_point_start)
        cut_point_end = min(top_cut_point_end, bottom_cut_point_end)

        #! 开始检测波峰波谷
        # 把bottom line的有效区域作为参考线,对其进行强烈平滑
        refer_line = np.zeros(length)
        for i in range(cut_point_start, cut_point_end):
            refer_line[i] = 1*(bottom_line[i]+top_line[i])
            # refer_line[i] = (bottom_line[i])

        # line_data[number, 0, :] = refer_line.copy()
        valid_line = refer_line.copy()
        # 0通道放peak(原图中向下突出的地方),1通道放valley(原图中向上凹陷的地方)
        label = np.zeros((2, length))
        N = 8
        p3 = np.zeros(length)
        box = refer_line[cut_point_start:cut_point_start+2*N].sum()
        for j in range(cut_point_start+N+1, cut_point_end-N-1):
            box = box-refer_line[j-N]+refer_line[j+N]
            # p3[j] = int(box) >> 4
            p3[j] = box/16

        # refer_line = p_bottom-p_top
        refer_line = p3

        add_line = np.zeros(length)
        for i in range(cut_point_start, cut_point_end):
            add_line[i] = (top_line[i]+bottom_line[i])
        # plt.plot(x, top_line, c='r')
        # plt.plot(x, bottom_line, c='g')
        # plt.plot(x, refer_line)
        # plt.plot(x, valid_line)

        exhale_thickness = []
        inhale_thickness = []
        exhale_peak = []
        inhale_peak = []
        # TODO 寻峰的平滑窗长与最值窗长也是超参数
        half_window = 10
        # ! 如果写if (picture_num == 2 & patch_num == 1)会进不去
        if ((picture_num == 2) & (patch_num == 4)):
            add_line = []
        for i in range(cut_point_start+half_window, cut_point_end-half_window):
            if ((refer_line[i] > refer_line[i-1]) & (refer_line[i] >= refer_line[i+1]) & (peak_judge(bottom_line, refer_line, i, cut_point_start, cut_point_end, half_window))):
                # 最值锚定,平台居中
                (index, max_start, max_end) = correct_peak(
                    i, bottom_line, half_window, cut_point_end-half_window)
                exhale_thickness.append(top_line[index]-bottom_line[index])
                exhale_peak.append(index)
                # plt.scatter(index, valid_line[index], marker='x', c='r')
                line_label[number, 0, max_start:max_end] = 1

            if ((refer_line[i] < refer_line[i-1]) & (refer_line[i] <= refer_line[i+1]) & (valley_judge(bottom_line, refer_line, i, cut_point_start, cut_point_end, half_window))):
                (index, min_start, min_end) = correct_valley(
                    i, bottom_line, half_window, cut_point_end-half_window)
                inhale_thickness.append(top_line[index]-bottom_line[index])
                inhale_peak.append(index)
                # plt.scatter(index, valid_line[index], marker='x', c='b')
                line_label[number, 1, min_start:min_end] = 1

        # plt.plot((cut_point_start+half_window, cut_point_start+half_window),
        #          (bottom_line[cut_point_start+half_window], top_line[cut_point_start+half_window]), c='b', marker='*', linestyle='--')
        # plt.plot((cut_point_end-half_window, cut_point_end-half_window),
        #          (bottom_line[cut_point_end-half_window], top_line[cut_point_end-half_window]), c='b', marker='*', linestyle='--')
        # plt.show()

        #! 切片坐标和原图坐标的映射变换
        if (len(exhale_peak) != 0):
            for i in range(len(exhale_peak)):
                original_peak_x.append(
                    w_start[picture_num]+((step*patch_num+exhale_peak[i]*(576/192))*resize_num/zoom))
                original_peak_top_y.append(
                    (top_line[exhale_peak[i]]*576./192.)/zoom+roi_depth-half_roi_height)
                original_peak_bottom_y.append(
                    (bottom_line[exhale_peak[i]]*576./192.)/zoom+roi_depth-half_roi_height)

        if (len(inhale_peak) != 0):
            for i in range(len(inhale_peak)):
                original_valley_x.append(
                    w_start[picture_num]+((step*patch_num+inhale_peak[i]*(576/192))*resize_num/zoom))
                original_valley_top_y.append(
                    (top_line[inhale_peak[i]]*576./192.)/zoom+roi_depth-half_roi_height)
                original_valley_bottom_y.append(
                    (bottom_line[inhale_peak[i]]*576./192.)/zoom+roi_depth-half_roi_height)
        number += 1

    #! 波峰聚类
    (refined_peak_x, refined_peak_top_y, refined_peak_bottom_y) = peaks_clustering(
        original_peak_x.copy(), original_peak_top_y.copy(), original_peak_bottom_y.copy())
    #! 波谷聚类
    (refined_valley_x, refined_valley_top_y, refined_valley_bottom_y) = peaks_clustering(
        original_valley_x.copy(), original_valley_top_y.copy(), original_valley_bottom_y.copy())

    raw_image = Image.open(
        f".\\dataset\\raw_image\\diaphragm ({picture_num}).jpg")
    # dataset\raw_image\useful (1).jpg
    w, h = raw_image.size
    plt.imshow(raw_image)

    inhale_thickness = []
    for i in range(len(refined_peak_x)):
        plt.scatter(refined_peak_x[i],
                    refined_peak_top_y[i]-1, marker='+', c='r')
        plt.scatter(refined_peak_x[i],
                    refined_peak_bottom_y[i]-1, marker='+', c='r')
        x_place = refined_peak_x[i]-60
        y_place = refined_peak_bottom_y[i]+20
        inhale_thickness.append(
            float(refined_peak_bottom_y[i]-refined_peak_top_y[i]))
        plt.text(x_place, y_place,
                 f'inhale: {inhale_thickness[i]:.2f}', c='r', fontsize=12)

    exhale_thickness = []
    for i in range(len(refined_valley_x)):
        plt.scatter(refined_valley_x[i],
                    refined_valley_top_y[i]-1, marker='+', c='y')
        plt.scatter(refined_valley_x[i],
                    refined_valley_bottom_y[i]-1, marker='+', c='y')
        x_place = refined_valley_x[i]-60
        y_place = refined_valley_bottom_y[i]+20
        exhale_thickness.append(
            float(refined_valley_bottom_y[i]-refined_valley_top_y[i]))
        plt.text(x_place, y_place,
                 f'exhale: {exhale_thickness[i]:.2f}', c='y', fontsize=12)
    average_inhale_thickness = sum(inhale_thickness)/len(inhale_thickness)
    average_exhale_thickness = sum(exhale_thickness)/len(exhale_thickness)
    plt.text(int(w/2)-160, h-180,
             f'average inhale: {average_inhale_thickness:.2f}', c='g', fontsize=14)
    plt.text(int(w/2)-160, h-140,
             f'average exhale: {average_exhale_thickness:.2f}', c='g', fontsize=14)
    plt.show()

np.save('./dataset/lines/line_data.npy', line_data)
np.save('./dataset/lines/line_label.npy', line_label)

print(f'number = {number}')


GRAPHBOARD_SCALE = 0.4
PICTOGRAPH_SCALE = 0.2
ARROWBOX_SCALE = GRAPHBOARD_SCALE * 0.75

BETA_OFFSET = 25

GRAPHBOARD_HEIGHT = 900
GRAPHBOARD_WIDTH = 750
GRID_WIDTH = 650
GRID_PADDING = (GRAPHBOARD_HEIGHT - GRID_WIDTH) / 2

DISTANCE = 40

RATIO = 75 / 90

LETTER_ITEM_HEIGHT = 100

BUTTON_SIZE = 30


from django.urls import path

from . import views

urlpatterns = [
    path('predict_cnn', views.predict_cnn, name='predict'),
    path('predict_svm', views.predict_svm, name='predict'),
    path('cnn', views.index_cnn, name='index_cnn'),
    path('svm', views.index_svm, name='index_svm')
]


from art import logo, vs
from game_data import data
import random
import os

def gen_game_data():
    games_data = random.choice(data)
    return games_data

def print_game_data(game_data, choice):
    print(f"Compare {choice}: {game_data['name']},{game_data['description']}, from {game_data['country']} ")

keep_on_playing = True
first_play = True
while keep_on_playing:

    first = "A"
    second = "B"
    correct_choice = "A"
    score = 0
    game_data1 = gen_game_data()
    while first_play:
        print(logo)
        print_game_data(game_data1, first)
        print(vs)
        game_data2 = gen_game_data()
        print_game_data(game_data2, second)
        option = input("Who has more follower type (A/B):").upper()
        if option not in ["A", "B"]:
            print("Invalid option")
        else:
            if game_data1['follower_count'] < game_data2['follower_count']:
                correct_choice = "B"
            if option == correct_choice:
                score += 1
                game_data1 = game_data2
                keep_on_playing = False
                os.system('cls' if os.name == 'nt' else 'clear')
            else:
                print(f"Sorry that's wrong , your final score is {score}")
                keep_on_playing = False
                first_play = False


# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from solum.objects import registry
from solum.objects.sqlalchemy import operation
from solum.tests import base
from solum.tests import utils

class TestOperation(base.BaseTestCase):
    def setUp(self):
        super(TestOperation, self).setUp()
        self.db = self.useFixture(utils.Database())
        self.ctx = utils.dummy_context()

        self.data = [{'project_id': self.ctx.project_id,
                      'user_id': 'fred',
                      'uuid': 'ce43e347f0b0422825245b3e5f140a81cef6e65b',
                      'name': 'o1',
                      'description': 'Scale up the resource',
                      'documentation': 'http://documentation.link',
                      'target_resource': 'http://target.resource.link'}]
        utils.create_models_from_data(operation.Operation, self.data, self.ctx)

    def test_objects_registered(self):
        self.assertIsNotNone(registry.Operation)
        self.assertIsNotNone(registry.OperationList)

    def test_get_all(self):
        lst = operation.OperationList()
        self.assertEqual(1, len(lst.get_all(self.ctx)))

    def test_check_data(self):
        pl = operation.Operation().get_by_id(self.ctx, self.data[0]['id'])
        for key, value in self.data[0].items():
            self.assertEqual(value, getattr(pl, key))


import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import os
import datetime

OUTPUT_PATH = r"reports"

def plot_keywords_distribution(df_train:pd.DataFrame) -> bool:
    fig = plt.figure(figsize=(8,72), dpi=100)
    sns.countplot(y = df_train.sort_values(by='target_mean', ascending=False)['keyword'],\
                  hue = df_train.sort_values(by='target_mean', ascending=False)['target'],\
                  )
    plt.tick_params(axis = 'x', labelsize = 15)
    plt.tick_params(axis='y', labelsize=12)
    plt.legend()
    plt.title("Target distribution in Keywords")
    if not os.path.exists(OUTPUT_PATH):
        os.makedirs(OUTPUT_PATH)
    file_name = "target-dist-{}".format(datetime.datetime.utcnow().strftime("%m%d%Y%H%M%S"))
    plt.savefig(os.path.join(OUTPUT_PATH, file_name))
    plt.clf()
    plt.cla()
    return True

def plot_meta_features(df_train:pd.DataFrame, df_test: pd.DataFrame) -> bool:
    META_FEATURES = ['word_count', 'unique_word_count', 'stop_word_count', 'url_count'\
        , 'mean_word_length', 'char_count', 'hashtag_counts', 'mention_counts']

    DISASTER_TWEETS = df_train['target'] == 1

    fig, axes = plt.subplots(ncols=2, nrows=len(META_FEATURES), figsize=(20, 50), dpi=100)

    for i, feature in enumerate(META_FEATURES):
        sns.distplot(df_train.loc[~DISASTER_TWEETS][feature], label='Not Disaster', ax=axes[i][0], color='green')
        sns.distplot(df_train.loc[DISASTER_TWEETS][feature], label='Disaster', ax=axes[i][0], color='red')

        sns.distplot(df_train[feature], label='Training', ax=axes[i][1])
        sns.distplot(df_test[feature], label='Test', ax=axes[i][1])

        for j in range(2):
            axes[i][j].set_xlabel('')
            axes[i][j].tick_params(axis='x', labelsize=12)
            axes[i][j].tick_params(axis='y', labelsize=12)
            axes[i][j].legend()

        axes[i][0].set_title(f'{feature} Target Distribution in Training Set', fontsize=13)
        axes[i][1].set_title(f'{feature} Training & Test Set Distribution', fontsize=13)

    if not os.path.exists(OUTPUT_PATH):
        os.makedirs(OUTPUT_PATH)
    file_name = "meta-{}".format(datetime.datetime.utcnow().strftime("%m%d%Y%H%M%S"))
    plt.savefig(os.path.join(OUTPUT_PATH, file_name))
    plt.clf()
    plt.cla()
    return True


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from conans import ConanFile, CMake, tools
from conans.model.version import Version
from conans.errors import ConanInvalidConfiguration
import os

class OpenCVConan(ConanFile):
    name = "opencv"
    version = "4.0.1"
    license = "BSD-3-Clause"
    homepage = "https://github.com/opencv/opencv"
    url = "https://github.com/conan-community/conan-opencv"
    author = "Conan Community"
    topics = ("conan", "opencv", "computer-vision", "image-processing", "deep-learning")
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False],
               "fPIC": [True, False],
               "contrib": [True, False],
               "jpeg": [True, False],
               "jpegturbo": [True, False],
               "tiff": [True, False],
               "webp": [True, False],
               "png": [True, False],
               "jasper": [True, False],
               "openexr": [True, False],
               "gtk": [None, 2, 3],
               "nonfree": [True, False]}
    default_options = {"shared": False,
                       "fPIC": True,
                       "contrib": False,
                       "jpeg": True,
                       "jpegturbo": False,
                       "tiff": True,
                       "webp": True,
                       "png": True,
                       "jasper": True,
                       "openexr": True,
                       "gtk": 3,
                       "nonfree": False}
    exports_sources = ["CMakeLists.txt"]
    exports = "LICENSE"
    generators = "cmake"
    description = "OpenCV is an open source computer vision and machine learning software library."
    short_paths = True
    _source_subfolder = "source_subfolder"
    _build_subfolder = "build_subfolder"

    def configure(self):
        compiler_version = Version(self.settings.compiler.version.value)
        if self.settings.compiler == "Visual Studio" and compiler_version < "14":
            raise ConanInvalidConfiguration("OpenCV 4.x requires Visual Studio 2015 and higher")

    def source(self):
        sha256 = "7b86a0ee804244e0c407321f895b15e4a7162e9c5c0d2efc85f1cadec4011af4"
        tools.get("{}/archive/{}.tar.gz".format(self.homepage, self.version), sha256=sha256)
        os.rename('opencv-%s' % self.version, self._source_subfolder)

        sha256 = "0d8acbad4b7074cfaafd906a7419c23629179d5e98894714402090b192ef8237"
        tools.get("https://github.com/opencv/opencv_contrib/archive/{}.tar.gz".format(self.version), sha256=sha256)
        os.rename('opencv_contrib-%s' % self.version, 'contrib')

        if self.settings.os != 'Android':
            tools.rmdir(os.path.join(self._source_subfolder, '3rdparty'))

    def config_options(self):
        if self.settings.os == 'Windows':
            del self.options.fPIC
        if self.settings.os != 'Linux':
            del self.options.gtk

    def system_requirements(self):
        if self.settings.os == 'Linux' and tools.os_info.is_linux:
            if tools.os_info.with_apt:
                installer = tools.SystemPackageTool()
                arch_suffix = ''
                if self.settings.arch == 'x86':
                    arch_suffix = ':i386'
                elif self.settings.arch == 'x86_64':
                    arch_suffix = ':amd64'
                packages = []
                if self.options.gtk == 2:
                    packages.append('libgtk2.0-dev%s' % arch_suffix)
                elif self.options.gtk == 3:
                    packages.append('libgtk-3-dev%s' % arch_suffix)
                for package in packages:
                    installer.install(package)
            elif tools.os_info.with_yum:
                installer = tools.SystemPackageTool()
                arch_suffix = ''
                if self.settings.arch == 'x86':
                    arch_suffix = '.i686'
                elif self.settings.arch == 'x86_64':
                    arch_suffix = '.x86_64'
                packages = []
                if self.options.gtk == 2:
                    packages.append('gtk2-devel%s' % arch_suffix)
                elif self.options.gtk == 3:
                    packages.append('gtk3-devel%s' % arch_suffix)
                for package in packages:
                    installer.install(package)

    def requirements(self):
        self.requires.add('zlib/1.2.11@conan/stable')
        if self.options.jpeg:
            # NOTE : use the same libjpeg implementation as jasper uses
            # otherwise, jpeg_create_decompress will fail on version check
            if self.options.jpegturbo:
                self.requires.add('libjpeg-turbo/1.5.2@bincrafters/stable')
            else:
                self.requires.add('libjpeg/9c@bincrafters/stable')
        if self.options.tiff:
            self.requires.add('libtiff/4.0.9@bincrafters/stable')
        if self.options.webp:
            self.requires.add('libwebp/1.0.0@bincrafters/stable')
        if self.options.png:
            self.requires.add('libpng/1.6.34@bincrafters/stable')
        if self.options.jasper:
            self.requires.add('jasper/2.0.14@conan/stable')
        if self.options.openexr:
            self.requires.add('openexr/2.3.0@conan/stable')

    def _configure_cmake(self):
        cmake = CMake(self)
        cmake.definitions['BUILD_EXAMPLES'] = False
        cmake.definitions['BUILD_DOCS'] = False
        cmake.definitions['BUILD_TESTS'] = False
        cmake.definitions['BUILD_PERF_TEST'] = False
        cmake.definitions['WITH_IPP'] = False
        cmake.definitions['BUILD_opencv_apps'] = False
        cmake.definitions['BUILD_opencv_java'] = False

        if self.settings.compiler == 'Visual Studio':
            cmake.definitions['BUILD_WITH_STATIC_CRT'] = 'MT' in str(self.settings.compiler.runtime)
        if self.settings.os != 'Windows':
            cmake.definitions['ENABLE_PIC'] = self.options.fPIC

        # 3rd-party

        # disable builds for all 3rd-party components, use libraries from conan only
        cmake.definitions['BUILD_ZLIB'] = False
        cmake.definitions['BUILD_TIFF'] = False
        cmake.definitions['BUILD_JASPER'] = False
        cmake.definitions['BUILD_JPEG'] = False
        cmake.definitions['BUILD_PNG'] = False
        cmake.definitions['BUILD_OPENEXR'] = False
        cmake.definitions['BUILD_WEBP'] = False
        cmake.definitions['BUILD_TBB'] = False
        cmake.definitions['BUILD_IPP_IW'] = False
        cmake.definitions['BUILD_ITT'] = False
        cmake.definitions['BUILD_JPEG_TURBO_DISABLE'] = True

        cmake.definitions['WITH_JPEG'] = self.options.jpeg
        cmake.definitions['WITH_TIFF'] = self.options.tiff
        cmake.definitions['WITH_WEBP'] = self.options.webp
        cmake.definitions['WITH_PNG'] = self.options.png
        cmake.definitions['WITH_JASPER'] = self.options.jasper
        cmake.definitions['WITH_OPENEXR'] = self.options.openexr
        cmake.definitions['WITH_PROTOBUF'] = False
        cmake.definitions['WITH_FFMPEG'] = False
        cmake.definitions['WITH_QUIRC'] = False
        cmake.definitions["WITH_CAROTENE"] = False

        if self.options.openexr:
            cmake.definitions['OPENEXR_ROOT'] = self.deps_cpp_info['openexr'].rootpath

        # system libraries
        if self.settings.os == 'Linux':
            cmake.definitions['WITH_GTK'] = self.options.gtk is not None
            cmake.definitions['WITH_GTK_2_X'] = self.options.gtk == 2

        if self.options.contrib:
            cmake.definitions['OPENCV_EXTRA_MODULES_PATH'] = os.path.join(self.build_folder, 'contrib', 'modules')

        if self.options.nonfree:
            cmake.definitions['OPENCV_ENABLE_NONFREE'] = True

        if self.settings.os == 'Android':
            cmake.definitions['ANDROID_STL'] = self.settings.compiler.libcxx
            cmake.definitions['ANDROID_NATIVE_API_LEVEL'] = self.settings.os.api_level

            cmake.definitions['BUILD_PERF_TESTS'] = False
            cmake.definitions['BUILD_ANDROID_EXAMPLES'] = False

            arch = str(self.settings.arch)
            if arch.startswith(('armv7', 'armv8')):
                cmake.definitions['ANDROID_ABI'] = 'NEON'
            else:
                cmake.definitions['ANDROID_ABI'] = {'armv5': 'armeabi',
                                                    'armv6': 'armeabi-v6',
                                                    'armv7': 'armeabi-v7a',
                                                    'armv7hf': 'armeabi-v7a',
                                                    'armv8': 'arm64-v8a'}.get(arch, arch)

            if 'ANDROID_NDK_HOME' in os.environ:
                cmake.definitions['ANDROID_NDK'] = os.environ.get('ANDROID_NDK_HOME')

        cmake.configure(build_folder=self._build_subfolder)
        return cmake

    def build(self):
        # https://github.com/opencv/opencv/issues/8010
        if str(self.settings.compiler) == 'clang' and str(self.settings.compiler.version) == '3.9':
            tools.replace_in_file(os.path.join(self._source_subfolder, 'modules', 'imgproc', 'CMakeLists.txt'),
                                  'ocv_define_module(imgproc opencv_core WRAP java python js)',
                                  'ocv_define_module(imgproc opencv_core WRAP java python js)\n'
                                  'set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/src/'
                                  'imgwarp.cpp PROPERTIES COMPILE_FLAGS "-O0")')

        # allow to find conan-supplied OpenEXR
        if self.options.openexr:
            find_openexr = os.path.join(self._source_subfolder, 'cmake', 'OpenCVFindOpenEXR.cmake')
            tools.replace_in_file(find_openexr,
                                  r'SET(OPENEXR_ROOT "C:/Deploy" CACHE STRING "Path to the OpenEXR \"Deploy\" folder")',
                                  '')
            tools.replace_in_file(find_openexr, r'set(OPENEXR_ROOT "")', '')
            tools.replace_in_file(find_openexr, 'SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)', '')
            tools.replace_in_file(find_openexr, 'SET(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)',
                                  '')

            def openexr_library_names(name):
                # OpenEXR library may have different names, depends on namespace versioning, static, debug, etc.
                reference = str(self.requires["openexr"])
                version_name = reference.split("@")[0]
                version = version_name.split("/")[1]
                version_tokens = version.split('.')
                major, minor = version_tokens[0], version_tokens[1]
                suffix = '%s_%s' % (major, minor)
                names = ['%s-%s' % (name, suffix),
                         '%s-%s_s' % (name, suffix),
                         '%s-%s_d' % (name, suffix),
                         '%s-%s_s_d' % (name, suffix),
                         '%s' % name,
                         '%s_s' % name,
                         '%s_d' % name,
                         '%s_s_d' % name]
                return ' '.join(names)

            for lib in ['Half', 'Iex', 'Imath', 'IlmImf', 'IlmThread']:
                tools.replace_in_file(find_openexr, 'NAMES %s' % lib, 'NAMES %s' % openexr_library_names(lib))

        cmake = self._configure_cmake()
        cmake.build()

    def package(self):
        self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
        cmake = self._configure_cmake()
        cmake.install()
        cmake.patch_config_paths()

    def add_libraries_from_pc(self, library):
        pkg_config = tools.PkgConfig(library)
        libs = [lib[2:] for lib in pkg_config.libs_only_l]  # cut -l prefix
        lib_paths = [lib[2:] for lib in pkg_config.libs_only_L]  # cut -L prefix
        self.cpp_info.libs.extend(libs)
        self.cpp_info.libdirs.extend(lib_paths)
        self.cpp_info.sharedlinkflags.extend(pkg_config.libs_only_other)
        self.cpp_info.exelinkflags.extend(pkg_config.libs_only_other)

    def package_info(self):
        opencv_libs = ["stitching",
                    "photo",
                    "video",
                    "ml",
                    "calib3d",
                    "features2d",
                    "highgui",
                    "videoio",
                    "flann",
                    "imgcodecs",
                    "objdetect",
                    "imgproc",
                    "core"]

        if self.settings.os != 'Android':
            # gapi depends on ade but ade disabled for Android
            # https://github.com/opencv/opencv/blob/4.0.1/modules/gapi/cmake/DownloadADE.cmake#L2
            opencv_libs.append("gapi")

        if self.options.contrib:
            opencv_libs = [
                "aruco",
                "bgsegm",
                "bioinspired",
                "ccalib",
                "datasets",
                "dpm",
                "face",
                "freetype",
                "fuzzy",
                "hfs",
                "img_hash",
                "line_descriptor",
                "optflow",
                "phase_unwrapping",
                "plot",
                "reg",
                "rgbd",
                "saliency",
                "shape",
                "stereo",
                "structured_light",
                "superres",
                "surface_matching",
                "tracking",
                "videostab",
                "xfeatures2d",
                "ximgproc",
                "xobjdetect",
                "xphoto"] + opencv_libs

        suffix = 'd' if self.settings.build_type == 'Debug' and self.settings.compiler == 'Visual Studio' else ''
        version = self.version.replace(".", "") if self.settings.os == "Windows" else ""
        for lib in opencv_libs:
            self.cpp_info.libs.append("opencv_%s%s%s" % (lib, version, suffix))

        if self.settings.compiler == 'Visual Studio':
            arch = {'x86': 'x86',
                    'x86_64': 'x64'}.get(str(self.settings.arch))
            vc = 'vc%s' % str(self.settings.compiler.version)
            bindir = os.path.join(self.package_folder, arch, vc, 'bin')
            libdir = os.path.join(self.package_folder, arch, vc, 'lib' if self.options.shared else 'staticlib')
            self.cpp_info.bindirs.append(bindir)
            self.cpp_info.libdirs.append(libdir)

        if self.settings.os == "Linux":
            self.cpp_info.libs.extend([
                "pthread",
                "m",
                "dl"])
            if self.options.gtk == 2:
                self.add_libraries_from_pc('gtk+-2.0')
            elif self.options.gtk == 3:
                self.add_libraries_from_pc('gtk+-3.0')
        elif self.settings.os == 'Macos':
            for framework in ['OpenCL',
                              'Accelerate',
                              'CoreMedia',
                              'CoreVideo',
                              'CoreGraphics',
                              'AVFoundation',
                              'QuartzCore',
                              'Cocoa']:
                self.cpp_info.exelinkflags.append('-framework %s' % framework)
            self.cpp_info.sharedlinkflags = self.cpp_info.exelinkflags
        elif self.settings.os == 'Windows':
            self.cpp_info.libs.append('Vfw32')
        if self.settings.os == 'Android' and not self.options.shared:
            self.cpp_info.includedirs.append(os.path.join('sdk', 'native', 'jni', 'include'))
            self.cpp_info.libdirs.append(os.path.join('sdk', 'native', 'staticlibs'))
        else:
            self.cpp_info.includedirs.append(os.path.join('include', 'opencv4'))
            self.cpp_info.libdirs.append(os.path.join('lib', 'opencv4', '3rdparty'))
            if not self.options.shared:
                self.cpp_info.libs.append('ade')