# %%
from collections import defaultdict
import json
import os

import numpy as np
import pandas as pd

from matplotlib.pyplot import hist
from skmultilearn.model_selection import iterative_train_test_split

# enable lib loading even if not installed as a pip package or in PYTHONPATH
# also convenient for relative paths in example config files
from pathlib import Path
os.chdir(Path(__file__).resolve().parent.parent)

# %%
from adpkd_segmentation.data.data_utils import (  # noqa
    display_sample,
    get_labeled,
    get_y_Path,
    make_dcmdicts,
    path_2dcm_int16,
    path_2label,
)
from adpkd_segmentation.data.data_utils import (  # noqa
    PATIENT,
    SEQUENCE,
    KIDNEY_PIXELS,
    MR,
    VOXEL_VOLUME,
)
from adpkd_segmentation.data.link_data import makelinks  # noqa

STUDY_TKV = "study_tkv"

# %% needed once for new data
makelinks()

# %%
dcm_paths = sorted(get_labeled())
dcm2attribs, patient2dcm = make_dcmdicts(tuple(dcm_paths))
all_ids = list(patient2dcm.keys())

# TKV checks
# %%
def TKV_update(dcm2attribs):
    studies = defaultdict(int)
    for dcm, attribs in dcm2attribs.items():
        study_id = (attribs[PATIENT], attribs[MR])
        studies[study_id] += attribs[KIDNEY_PIXELS] * attribs[VOXEL_VOLUME]

    for dcm, attribs in dcm2attribs.items():
        tkv = studies[(attribs[PATIENT], attribs[MR])]
        attribs[STUDY_TKV] = tkv

    return studies, dcm2attribs

# %%
studies, dcm2attribs = TKV_update(dcm2attribs)
hist(studies.values(), bins=40)

# %%
hist(np.log(list(studies.values())), bins=40)

# %%
# Patient info
patient_info = set()
for dcm_path, attribs in dcm2attribs.items():
    patient = attribs[PATIENT]
    seq = attribs[SEQUENCE]
    tkv = attribs[STUDY_TKV]
    mr = attribs[MR]
    patient_info.add((patient, seq, mr, tkv))

print(patient_info)

# %%
df = pd.DataFrame.from_records(
    list(patient_info),
    columns=[PATIENT, SEQUENCE, MR, STUDY_TKV],
    index=PATIENT,
).sort_index()

# %%
df.to_csv("./notebooks/patients_2020_11_02.csv")

# %%
print(df.index.value_counts())

# %%
print(df.seq.value_counts())

# %%
print(df.study_tkv.describe())
print(np.log(df.study_tkv).describe())

# %%
def create_label_arrays(patient_info, all_ids):
    patient_to_label = {}
    for id_ in all_ids:
        patient_to_label[id_] = np.zeros(8, dtype=np.uint8)
    for patient, seq, mr, tkv in patient_info:
        # sequence labeling
        # the same patient can have more
        if "AX SSFSE HR KIDNEYS" == seq:
            patient_to_label[patient][0] = 1
        elif "AXL FIESTA" == seq:
            patient_to_label[patient][1] = 1
        elif "PEL" in seq:
            patient_to_label[patient][2] = 1
        else:
            patient_to_label[patient][3] = 1
        # LOG TKV category
        # 13.69 to 14.74 interquartile range
        # 14.19 median
        log_tkv = np.log(tkv)
        if log_tkv < 13.7:
            patient_to_label[patient][4] = 1
        elif 13.7 <= log_tkv < 14.2:
            patient_to_label[patient][5] = 1
        elif 14.2 <= log_tkv < 14.7:
            patient_to_label[patient][6] = 1
        elif 14.7 <= log_tkv:
            patient_to_label[patient][7] = 1

    return patient_to_label

# %%
patient2label = create_label_arrays(patient_info, all_ids)

# %%
labels = [patient2label[id_] for id_ in all_ids]
X = np.array(all_ids)[..., np.newaxis]
y = np.stack(labels)

print(y.shape)
print(len(all_ids))
print(X.shape)

# %%
# Split to train, val, test
TRAIN = 0.7
VAL = 0.15
TEST = 0.15
np.random.seed = 42

X_train_val, y_train_val, X_test, y_test = iterative_train_test_split(
    X, y, test_size=TEST
)

X_train, y_train, X_val, y_val = iterative_train_test_split(
    X_train_val, y_train_val, test_size=VAL / (TRAIN + VAL)
)

# %%
print("Sizes: ", len(X_train), len(X_val), len(X_test))

# %%
print(y_train)
print(y_val)
print(y_test)

# %%
print(df[df.index.isin(X_test.squeeze())])
print(df[df.index.isin(X_val.squeeze())])

# %%
split_dict = {
    "train": list(X_train.squeeze()),
    "val": list(X_val.squeeze()),
    "test": list(X_test.squeeze()),
}

# %%
# move patient with transplanted kidneys to train
# out of scope
# outlier = WC-ADPKD-____-
# split_dict["val"].remove(outlier)
# split_dict["train"].append(outlier)

# %%
with open("./stratification/strat_split_2020_11_03.json", "w") as f:
    json.dump(split_dict, f, indent=4)

# %%


import os, tkinter
import webbrowser
from tkinter import ttk, messagebox
from PIL import Image, ImageTk

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

import datasource

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

if not os.path.exists('cache/img/'):
    os.makedirs('cache/img/')

class MainWindow(object):
    '''
    窗口构造器:
    @param label: 窗口标题
    @param names: 名字列表(2维数组: 0维度为国家名称; 1维度为省级名称)
    '''
    def __init__(self, label, names):
        self.root = tkinter.Tk()
        self.root.title(label)
        self.names = names
        self.add_widgets()
        self.root.resizable(False, False)
        self.root.mainloop()

    '''
    窗口初始化函数:
        将窗口Frame以及Frame上的部件初始化并布局。
    '''
    def add_widgets(self):
        self.introduceFrame = ttk.Frame(self.root) # 程序标题显示面板
        self.countriesFrame = ttk.Frame(self.root) # 国家选项显示面板
        self.provincesFrame = ttk.Frame(self.root) # 省级选项显示面板
        self.operationFrame = ttk.Frame(self.root) # 程序操作显示面板
        self.introduceFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        self.countriesFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')
        self.provincesFrame.grid(padx=5, pady=5, row=2, column=0, sticky='nwe')
        self.operationFrame.grid(padx=5, pady=5, row=3, column=0, sticky='nwe')

        canvas_width = 730  # 画布宽度
        canvas_height = 280 # 画布高度
        self.canvas = tkinter.Canvas(self.introduceFrame, bg='lightblue', \
                width=canvas_width, height=canvas_height)
        self.canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        im = Image.open('res/head.png')
        self.ph = ImageTk.PhotoImage(im)
        self.canvas.create_image(0, 0, image=self.ph, anchor=tkinter.NW)

        worldButton = ttk.Button(self.countriesFrame, text='国家级', \
                command=self.queryWorld)
        worldButton.grid(padx=3, pady=3, row=0, column=0)
        self.buttons = []   # 程序按钮集合(2维数组: 0维度为国家级；1维度为省级)
        buttonList = []
        for i in range(len(self.names[0])):
            buttonList.append(ttk.Button(self.countriesFrame, text=self.names[0][i], \
                    command=self.maker(i, 0)))
            buttonList[i].grid(padx=3, pady=3, row=i//8+1, column=i%8)
        self.buttons.append(buttonList.copy())

        chinaButton = ttk.Button(self.provincesFrame, text='省级', \
                command=self.queryChina)
        chinaButton.grid(padx=3, pady=3, row=0, column=0)
        buttonList.clear()
        for i in range(len(self.names[1])):
            buttonList.append(ttk.Button(self.provincesFrame, text=self.names[1][i], \
                    command=self.maker(i, 1)))
            buttonList[i].grid(padx=3, pady=3, row=i//8+1, column=i%8)
        self.buttons.append(buttonList.copy())

        ttk.Button(self.operationFrame, text='刷新', command=self.refresh) \
                .grid(padx=3, pady=3, row=0, column=0)
        ttk.Button(self.operationFrame, text='在浏览器打开原始网页', \
                command=self.open_in_browser) \
                .grid(padx=3, pady=3, row=0, column=1)

    '''
    选择程序闭包:
        用于记录每个按钮绑定的运行时执行函数。
    @param argv: 国家/省标号(从0到len(names[0/1])-1)
    @param choice: 标记选择的标号是国家级标号还是省级标号
    @return: 按钮具体绑定的运行时执行函数
    '''
    def maker(self, argv, choice):
        if choice == 0:
            def func1():
                self.queryCountry(argv)
            return func1
        else:
            def func2():
                self.queryProvince(argv)
            return func2

    '''
    国家级详情信息查询:
        用于执行国家级详情信息查询功能，即新建一个子窗口对数据进行可视化展示。
    @param argv: 国家标号(从0到len(names[0])-1)
    '''
    def queryCountry(self, argv):
        root = tkinter.Toplevel()
        name = self.names[0][argv]
        root.title('%s' % name)
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        data = datasource.countries(argv)
        isUpdatedLabel = ttk.Label(labelFrame)
        isUpdatedLabel['text'] = '今日已更新' if data['isUpdated'] else '今日未更新'
        isUpdatedLabel['foreground'] = 'green' if data['isUpdated'] else 'red'
        isUpdatedLabel.grid(padx=5, pady=5, row=0, column=0, columnspan=4, \
                sticky='nwes')

        labels = create_labels(labelFrame, data)
        for i in range(len(labels)):
            labels[i].grid(padx=18, pady=5, row=1, column=i, sticky='w')

        if argv == 0:
            canvas = tkinter.Canvas(chartFrame, bg='lightblue', width=470, height=350)
            canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
            im = draw(data, name)
            ph = ImageTk.PhotoImage(im.resize((480, 360), Image.ANTIALIAS))
            canvas.create_image(0, 0, image=ph, anchor=tkinter.NW)
        else:
            pass

        root.mainloop()

    '''
    省级详情信息查询:
        用于执行省级详情信息查询功能，即新建一个子窗口对数据进行可视化展示。
    @param argv: 省标号(从0到len(names[1])-1)
    '''
    def queryProvince(self, argv):
        root = tkinter.Toplevel()
        name = self.names[1][argv]
        root.title('%s' % name)
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        data = datasource.provinces(argv)
        isUpdatedLabel = ttk.Label(labelFrame)
        isUpdatedLabel['text'] = '今日已更新' if data['isUpdated'] else '今日未更新'
        isUpdatedLabel['foreground'] = 'green' if data['isUpdated'] else 'red'
        isUpdatedLabel.grid(padx=5, pady=5, row=0, column=0, columnspan=4, \
                sticky='nwes')

        labels = create_labels(labelFrame, data)
        for i in range(len(labels)):
            labels[i].grid(padx=18, pady=5, row=1, column=i, sticky='w')

        table = create_table(chartFrame, data['children'])
        table.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')

        root.mainloop()

    def queryWorld(self):
        root = tkinter.Toplevel()
        root.title('世界疫情数据')
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        lastUpdateTime, data = datasource.world_data()
        ttk.Label(labelFrame, text="数据(中国)最近更新时间:\n%s" % \
                lastUpdateTime, width=52, foreground='green') \
                .grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        total_number = 0; china_number = data[0]['total']['confirm']
        for country in data:
            total_number += country['total']['confirm']
        text = "世界总确诊数: %d\n中国总确诊数: %d\n其余国家和地区总确诊数: %d" % \
                (total_number, china_number, total_number-china_number)
        ttk.Label(labelFrame, text=text, font='Helvetica -18', foreground='red') \
                .grid(padx=5, pady=5, row=1, column=0, sticky='nwes')

        canvas = tkinter.Canvas(chartFrame, bg='lightblue', width=900, height=350)
        canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        images = draw1(data)
        im0 = images[0]
        ph0 = ImageTk.PhotoImage(im0.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(0, 0, image=ph0, anchor=tkinter.NW)
        im1 = images[1]
        ph1 = ImageTk.PhotoImage(im1.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(440, 0, image=ph1, anchor=tkinter.NW)

        create_table(labelFrame, data).grid(padx=5, pady=10, row=0, column=1, \
                rowspan=2, sticky='e')

        root.mainloop()

    def queryChina(self):
        root = tkinter.Toplevel()
        root.title('中国疫情数据')
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        data = datasource.china_data()
        ttk.Label(labelFrame, text="数据(中国)最近更新时间: %s" % \
                data['lastUpdateTime'], width=52, foreground='green') \
                .grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        d = data['total']
        ttk.Label(labelFrame, text='累计确诊: %d例'%d['confirm'], \
                font='Helvetica -18', foreground='purple') \
                .grid(padx=5, pady=2, row=1, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='现有确诊: %d例'%d['nowConfirm'], \
                font='Helvetica -18', foreground='red') \
                .grid(padx=5, pady=2, row=2, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='累计治愈: %d例'%d['heal'], \
                font='Helvetica -18', foreground='green') \
                .grid(padx=5, pady=2, row=3, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='现有疑似: %d例'%d['suspect'], \
                font='Helvetica -18', foreground='orange') \
                .grid(padx=5, pady=2, row=4, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='累计死亡: %d例'%d['dead'], \
                font='Helvetica -18', foreground='grey') \
                .grid(padx=5, pady=2, row=5, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='现有重症: %d例'%d['nowSevere'], \
                font='Helvetica -18', foreground='magenta') \
                .grid(padx=5, pady=2, row=6, column=0, sticky='nwes')

        create_table(labelFrame, data['children']).grid(padx=5, pady=10, row=0, \
                column=1, rowspan=7, sticky='e')

        canvas = tkinter.Canvas(chartFrame, bg='lightblue', width=900, height=350)
        canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        images = draw2(data)
        im0 = images[0]
        ph0 = ImageTk.PhotoImage(im0.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(0, 0, image=ph0, anchor=tkinter.NW)
        im1 = images[1]
        ph1 = ImageTk.PhotoImage(im1.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(440, 0, image=ph1, anchor=tkinter.NW)

        root.mainloop()

    def refresh(self):
        datasource.refresh()
        messagebox.showinfo(title='REFRESH', message='数据刷新成功!')

    def open_in_browser(self):
        webbrowser.open('https://news.qq.com/zt2020/page/feiyan.htm?from=singlemessage')

'''
创造数据显示标签集合:
    用于展示国家级/省级数据，将四个标签作为一个列表返回。
'''
def create_labels(root, data):
    labels = []
    if 'suspect' in data['today']:
        labels.append(ttk.Label(root))
        todayNum = data['today']['confirm']
        labels[0]['text'] = '确诊\n%d\n较昨日%s' % (data['total']['confirm'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[0]['justify'] = 'center'
        labels[0]['foreground'] = 'red'
        labels.append(ttk.Label(root))
        todayNum = data['today']['suspect']
        labels[1]['text'] = '疑似\n%d\n较昨日%s' % (data['total']['suspect'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[1]['justify'] = 'center'
        labels[1]['foreground'] = 'orange'
        labels.append(ttk.Label(root))
        todayNum = data['today']['dead']
        labels[2]['text'] = '死亡\n%d\n较昨日%s' % (data['total']['dead'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[2]['justify'] = 'center'
        labels[2]['foreground'] = 'grey'
        labels.append(ttk.Label(root))
        todayNum = data['today']['heal']
        labels[3]['text'] = '治愈\n%d\n较昨日%s' % (data['total']['heal'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[3]['justify'] = 'center'
        labels[3]['foreground'] = 'green'
    else:
        labels.append(ttk.Label(root))
        labels[0]['text'] = '确诊\n%d' % data['total']['confirm']
        labels[0]['justify'] = 'center'
        labels[0]['foreground'] = 'red'
        labels.append(ttk.Label(root))
        labels[1]['text'] = '疑似\n%d' % data['total']['suspect']
        labels[1]['justify'] = 'center'
        labels[1]['foreground'] = 'orange'
        labels.append(ttk.Label(root))
        labels[2]['text'] = '死亡\n%d' % data['total']['dead']
        labels[2]['justify'] = 'center'
        labels[2]['foreground'] = 'grey'
        labels.append(ttk.Label(root))
        labels[3]['text'] = '治愈\n%d' % data['total']['heal']
        labels[3]['justify'] = 'center'
        labels[3]['foreground'] = 'green'
    return labels

def create_table(root, data, center=True):
    colums_name = ['地区', '确诊', '死亡', '治愈', '状态']
    colums_width = [100, 80, 80, 80, 80]
    indices = ['col%d' % i for i in range(len(colums_name))]
    table = ttk.Treeview(root, show='headings', columns=tuple(indices))
    for i in range(len(colums_name)):
        label = 'col' + str(i)
        val = colums_width[i]
        if (center):
            table.column(label, width=val, anchor='center')
        else:
            table.column(label, width=val, anchor='w')
        table.heading(label, text=colums_name[i])
    for i in range(len(data)):
        record = tuple([data[i]['name'], data[i]['total']['confirm'], \
                data[i]['total']['dead'], data[i]['total']['heal'], \
                '已更新' if data[i]['today']['isUpdated'] else '未更新'])
        table.insert('', i, values=record)
    return table # User should pack the table theirselves

def draw(data, name):
    colors = ['#d62728', '#ff7f0e', '#1f77b4', '#2ca02c']
    # X = ['确诊', '疑似', '死亡', '治愈']
    X = [2, 4, 6, 8]
    d = data['total']
    Y1 = [d['confirm'], d['suspect'], d['dead'], d['heal']]
    d = data['today']
    Y2 = [Y1[0]-d['confirm'], Y1[1]-d['suspect'], Y1[2]-d['dead'], Y1[3]-d['heal']]
    plt.xticks(X, ['确诊', '疑似', '死亡', '治愈'])
    X1 = [x-0.2 for x in X]
    plt.bar(X1, Y1, edgecolor='white', color=colors)
    #plt.bar(X, Y1, facecolor='#9999ff', edgecolor='white')
    X2 = [x+0.2 for x in X]
    plt.bar(X2, Y2, edgecolor='white', color=colors)
    #plt.bar(X, Y2, facecolor='#ff9999', edgecolor='white')

    for x, y in zip(X1, Y1):
        plt.text(x, y, '目前: %d' % y, ha='center', va='bottom')
    for x, y in zip(X2, Y2):
        plt.text(x, y, '以往: %d' % y, ha='center', va='top')

    img_name = 'cache/img/%s.png' % name
    plt.savefig(img_name)
    plt.close()
    image = Image.open(img_name)
    os.remove(img_name)
    return image

'''
绘制世界疫情数据图表:
    以饼状图来展示世界各个国家确诊数占比。
'''
def draw1(data):
    images = []

    labels = [data[0]['name'], '其余国家']
    others_total = 0
    for i in range(len(data)-1):
        others_total += data[1+i]['total']['confirm']
    nums = [data[0]['total']['confirm'], others_total]
    plt.pie(nums, labels=labels, autopct='%.2f%%', shadow=True)
    plt.title('世界疫情数据')
    img_name = 'cache/img/world_data.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    # image = Image.open('cache/img/world_data.png')
    os.remove(img_name)

    labels = [item['name'] for item in data][1:11]
    nums = [item['total']['confirm'] for item in data][1:11]
    X = range(1, 11)
    plt.bar(X, nums, facecolor='red', edgecolor='white')
    for x, y in zip(X, nums):
        plt.text(x, y, '%d' % y, ha='center', va='bottom')
    nums = [item['total']['heal'] for item in data][1:11]
    plt.bar(X, nums, facecolor='green', edgecolor='white')
    for x, y in zip(X, nums):
        plt.text(x, y, '%d' % y, ha='center', va='center')
    plt.xticks(X, labels)
    patches = [mpatches.Patch(color='red', label='确诊'), \
            mpatches.Patch(color='green', label='治愈')]
    plt.legend(handles=patches, ncol=1, loc='best')
    plt.title('除中国外前10位国家地区疫情数据')
    img_name = 'cache/img/world_data_1.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    os.remove(img_name)

    return images

def draw2(data):
    images = []

    X = [i for i in range(1, len(data['dayList'])+1)]
    dates = []; confirms = []; suspects = []; deads = []; heals = []
    d = data['dayList']
    for day in d:
        dates.append(day['date'])
        confirms.append(day['confirm'])
        suspects.append(day['suspect'])
        deads.append(day['dead'])
        heals.append(day['heal'])
    plt.plot(X, confirms, color='red')
    plt.plot(X, suspects, color='orange')
    plt.plot(X, deads, color='grey')
    plt.plot(X, heals, color='green')
    for x, y in zip(X[::2], confirms[::2]):
        plt.text(x, y, '%d' % y, ha='center', va='center')
    plt.xticks(X[::2], dates[::2], rotation=45)
    patches = [mpatches.Patch(color='red', label='确诊'), \
            mpatches.Patch(color='orange', label='疑似'), \
            mpatches.Patch(color='grey', label='死亡'), \
            mpatches.Patch(color='green', label='治愈')]
    plt.legend(handles=patches, ncol=1, loc='best')
    plt.title('中国疫情数据走势')
    img_name = 'cache/img/china_data.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    os.remove(img_name)

    X = [i for i in range(1, len(data['dayAddList'])+1)]
    dates = []; confirms = []; suspects = []; deads = []; heals = []
    d = data['dayAddList']
    for day in d:
        dates.append(day['date'])
        confirms.append(day['confirm'])
        suspects.append(day['suspect'])
        deads.append(day['dead'])
        heals.append(day['heal'])
    plt.plot(X, confirms, color='red')
    plt.plot(X, suspects, color='orange')
    plt.plot(X, deads, color='grey')
    plt.plot(X, heals, color='green')
    for x, y in zip(X[::2], confirms[::2]):
        plt.text(x, y, '%d' % y, ha='center', va='center')
    plt.xticks(X[::2], dates[::2], rotation=45)
    patches = [mpatches.Patch(color='red', label='确诊增长数'), \
            mpatches.Patch(color='orange', label='疑似增长数'), \
            mpatches.Patch(color='grey', label='死亡增长数'), \
            mpatches.Patch(color='green', label='治愈增长数')]
    plt.legend(handles=patches, ncol=1, loc='best')
    plt.title('中国疫情增长走势')
    img_name = 'cache/img/china_data.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    os.remove(img_name)

    return images


#!/usr/bin/env python3

# This is my first pygame
# Made by myself

import pygame
import os
import random
from os import path

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 225, 0)
WIDTH = 1204      # width for gaming screen
LENGTH = 806      # length for gaming screen
BALL_MOVE = 6
LINE_MOVE = 5
ASSET_FOLDER = "assets/Pong_graphics"

# set up assets folder

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((20, 20))
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, LENGTH / 2)
        self.x = BALL_MOVE
        self.y = BALL_MOVE
        if random.randint(0, 100) % 2 == 0:
            self.x *= -1
        if random.randint(0, 100) % 2 == 0:
            self.y *= -1

    def turn_right(self):
        self.x = BALL_MOVE

    def turn_left(self):
        self.x = -1 * BALL_MOVE

    def update(self):
        self.rect.x += self.x
        self.rect.y += self.y
        if self.rect.y >= LENGTH:
            self.y = -1 * BALL_MOVE

        if self.rect.x >= WIDTH:
            pygame.quit()

        if self.rect.x <= 0:
            pygame.quit()

        if self.rect.y <= 0:
            self.y = BALL_MOVE

class Line(pygame.sprite.Sprite):
    def __init__(self, file, is_left, x = 10, y = 10):
        self.file = file
        self.is_left = is_left
        self.x = x
        self.y = y
        pygame.sprite.Sprite.__init__(self)
        game_folder = path.join(path.dirname(__file__))
        self.image = pygame.image.load(
            os.path.join(game_folder, ASSET_FOLDER, self.file)
        ).convert()
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
        self.rect.center = (self.x, self.y)

    def update(self):
        key = pygame.key.get_pressed()

        if self.rect.top < 0:
            self.rect.y += 1
            return

        if self.rect.bottom > LENGTH:
            self.rect.y -= 1
            return

        if not self.is_left:
            if key[pygame.K_UP]:
                self.rect.y -= LINE_MOVE
            if key[pygame.K_DOWN]:
                self.rect.y += LINE_MOVE
        else:
            if key[pygame.K_w]:
                self.rect.y -= LINE_MOVE
            if key[pygame.K_s]:
                self.rect.y += LINE_MOVE

def init_pygame():
    FPS = 45          # frames per second (how fast the screen refreshes)
    # initialize pygame and create window
    pygame.init()
    pygame.mixer.init()  # for sound
    screen = pygame.display.set_mode((WIDTH, LENGTH))
    pygame.display.set_caption("Game Skeleton")
    clock = pygame.time.Clock()

    left_sprites = pygame.sprite.Group()
    right_sprites = pygame.sprite.Group()
    all_sprites = pygame.sprite.Group()

    ball = Ball()
    line1 = Line("line1.png", True, 100, LENGTH / 2)
    line2 = Line("line2.png", False, WIDTH - 100, LENGTH / 2)

    left_sprites.add(line1)
    right_sprites.add(line2)

    all_sprites.add(line1)
    all_sprites.add(line2)
    all_sprites.add(ball)

    # Game loop
    running = True
    while running:
        # keep loop running at the right speed
        clock.tick(FPS)
        # Process input (events)
        for event in pygame.event.get():
            # check for closing window
            if event.type == pygame.QUIT:
                running = False

        # Update
        all_sprites.update()

        # detect left collision
        if pygame.sprite.spritecollide(ball, left_sprites, False):
            ball.turn_right()

        # detect right collision
        if pygame.sprite.spritecollide(ball, right_sprites, False):
            ball.turn_left()

        # Draw / render
        screen.fill(WHITE)
        all_sprites.draw(screen)
        # *after* drawing everything, flip the display
        pygame.display.flip()

    pygame.quit()

if __name__ == '__main__':
    init_pygame()


def count_characters(text):
    return len(text)

def count_words(text):
    words = text.split()
    return len(words)

def count_sentences(text):
    sentences = text.split('. ')
    return len(sentences)

def analyze_file(file_path):
    with open(file_path, 'r') as file:
        text = file.read()

        character_count = count_characters(text)
        word_count = count_words(text)
        sentence_count = count_sentences(text)

        print("File Analysis:")
        print("Character count:", character_count)
        print("Word count:", word_count)
        print("Sentence count:", sentence_count)

print("Welcome to Words Counter!")

file_path = input("Enter the file path: ")
analyze_file(file_path)


import pygame
from pygame.locals import *
from random import choice

pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
size = [400, 300]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("GATO")
done = False
imagen = pygame.image.load('img/tableto.jpg')
# imagen X
x = pygame.image.load('img/X.png')
x = pygame.transform.scale(x, (55, 55))
# imagen O
o = pygame.image.load('img/O.png')
o = pygame.transform.scale(o, (55, 55))
ar = [x, o]
el = choice(ar)
screen.blit(imagen, (0, 0))
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_9:
                screen.blit(ar[1], (264, 50))
            elif event.key == pygame.K_8:
                screen.blit(ar[0], (175, 50))
            elif event.key == pygame.K_7:
                screen.blit(el, (85, 50))
            if event.key == pygame.K_6:
                screen.blit(el, (264, 125))
            elif event.key == pygame.K_5:
                screen.blit(el, (175, 125))
            elif event.key == pygame.K_4:
                screen.blit(el, (85, 125))
            elif event.key == pygame.K_3:
                screen.blit(el, (264, 200))
            if event.key == pygame.K_2:
                screen.blit(el, (175, 200))
            elif event.key == pygame.K_1:
                screen.blit(el, (85, 200))
            elif event.key == pygame.K_ESCAPE:
                pygame.quit()
    # Tablerp
    pygame.draw.line(screen, WHITE, (150, 250), (150, 50), 8)
    pygame.draw.line(screen, WHITE, (250, 250), (250, 50), 8)
    pygame.draw.line(screen, WHITE, (75, 115), (320, 115), 8)
    pygame.draw.line(screen, WHITE, (75, 190), (320, 190), 8)
    pygame.display.update()
pygame.quit()


import os
import sys
import logging

from Scopuli.Interfaces.GUI.ApplicationModule import Application
from Scopuli.Interfaces.GUI.WindowModule import Window
from Scopuli.Interfaces.GUI.WidgetModule import Widget

log = logging.getLogger(__name__)

class LogHandler(logging.Handler):
    def __init__(self, application):
        logging.Handler.__init__(self)

        self.applocation = application

    def emit(self, record):
        self.applocation.emit("log", "{} - {}".format(record.module, record.msg))

class ResourceChecker:
    def __init__(self, application):
        self.application = application
        self.configuration = self.application.config.configuration

        self._check_gui()

    def _check_gui(self):
        group_name = "gui"
        group_error = False

        for group in self.configuration.getElementsByTagName(group_name):
            for item in group.childNodes:
                if item.nodeType == 1:
                    for attr in ["glade", "image_splash", "image_logo"]:
                        if item.hasAttribute(attr):
                            attr_value = self.application.config.get(group_name, item.nodeName, attr, "")

                            if not os.path.isfile(attr_value):
                                group_error = True
                                log.critical("Ошибка конфигурации, файл {} не найден, проблема в группе {}, элемент {}, аттрибут {}".format(attr_value, group_name, item.nodeName, attr))

        if group_error:
            sys.exit(1)


# --------------------
# SECTION - III(c) - Extend a Class Functionality using Inheritance
# --------------------

class Employee: # Parent Class
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

    def work(self):
        print(f'{self.name} is working')

class SoftwareEngineer(Employee): # Syntax for a Child Class

    def __init__(self, name, age, salary, level):

        super().__init__(name, age, salary)

        self.level = level

    def debug(self):
        print(f"{self.name} is debugging")

class Designer(Employee): # Syntax for a Child CLass

    def draw(self):
        print(f"{self.name} is drawing")

# Implementation of Child Class using the instance attributes and methods of Parent Class

se = SoftwareEngineer("Max", 24, 6000, "Junior")
print(se.name, se.age)
se.work()
print(se.level)

d = Designer("Phillip", 27, 7000)
print(d.name, d.age)
d.work()

se.debug()
d.draw()

# So this is How we "Extend" the Functionality of Base Class,
# using "draw" and "debug" named functions (as done above)


#author: Riley Doyle
#date: 7/15/20
#file: CO2_loss_dynamic
#status: WORKING

#clear all
from IPython import get_ipython
get_ipython().magic('reset -sf')

from calc_Ks import *
from calc_alphas import *
from calc_density import *
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

global k1, k2, k3, k4

T = 20 + 273.15 #kelvins
S = 35 #g/kg
Tc = 20
P = 10 #(dbar)
t = Tc*1.00024
p = P/10
den = calc_density(S, t, p) #(kg/m3)
PCO2 = 0.000416 #atm
d = 0.15 #m

kLa = 0.5 #1/hr
y1 = 2.128 #1.714 (old algae eqn) #g CO2 per g algae
y2 = 0.3395 #0.1695 (old algae eqn) #g HCO3 as CO2 per g algae

Kh = calc_Kh(T,S)*(den/1000) #mol/L/atm
K1 = calc_K1(T, S)*(den/1000) #mol/L
pK1 = - np.log10(K1)
K2 = calc_K2(T, S)*(den/1000) #mol/L
pK2 = - np.log10(K2)

Csat = PCO2*Kh*44*1000 #g/m3

alk0 = 2.5
r_algae = 10
pH = 8

alpha0 = calc_alpha0(pH, pK1, pK2)
alpha1 = calc_alpha1(pH, pK1, pK2)
alpha2 = calc_alpha2(pH, pK1, pK2)

OH = 10**-(14-pH)*(10**3)
H = (10**(-pH))*(10**3)

k1 = alpha0/(alpha1 + 2*alpha2)*y2*r_algae
k2 = (kLa*d*24)
k3 = (kLa*d*24)*Csat
k4 = (y1 + y2)*r_algae - y2*r_algae*(alpha1 + 2*alpha2)

def rates(x,t):
    global k1, k2, k3, k4
    Caq = x[0]
    Cdel = x[1]
    Closs = x[2]
    dCaqdt = -k1
    dCdeldt = ((k2 *Caq) - k3) + k4
    dClossdt = (k2 *Caq) - k3
    return [dCaqdt, dCdeldt, dClossdt]

Caq0 = ((alk0 - OH + H)*alpha0/(alpha1 + 2*alpha2))*44
Cin0 = 0
Closs0 = 0

x0 = [Caq0, Cin0, Closs0]
t = np.linspace(0,4,100)

x = odeint(rates, x0, t)

Caq = x[:,0]*d
Cdel = x[:,1]
Closs = x[:,2]
plt.xlabel('time (days)')
plt.ylabel('CO$_2$ (g/m$^2$)')
plt.plot(t,Cdel)
plt.plot(t, Closs)
plt.legend(['CO$_2$ supply required', 'CO$_2$ loss to atmosphere'], frameon=False)
plt.axis([0, 3, 0, 70])
plt.show()

plt.xlabel('time (days)')
plt.ylabel('CO$_2$ (g/m$^2$)')
plt.plot(t,Caq)
plt.show()


from causal_discovery_utils.constraint_based import LearnStructBase
from causal_discovery_algs.pc import LearnStructPC
from graphical_models import PAG, arrow_head_types as Mark
from itertools import combinations

class LearnStructFCI(LearnStructBase):
    def __init__(self, nodes_set, ci_test,
                 is_selection_bias=True, is_tail_completeness=True):
        super().__init__(PAG, nodes_set=nodes_set, ci_test=ci_test)

        assert isinstance(is_selection_bias, bool)
        self.is_selection_bias = is_selection_bias  # if False, orientation rules R5, R6, R7 are not executed.
        assert isinstance(is_tail_completeness, bool)
        self.is_tail_completeness = is_tail_completeness  # if False, orientation rules R8, R9, R10 are not executed

        self.graph.create_complete_graph(Mark.Circle, nodes_set)  # Create a fully connected graph with edges: o--o
        self.pc_alg = LearnStructPC(nodes_set, ci_test)  # initialize a PC object for learning the skeleton
        self.found_D_Sep_link = False  # indicates if the learner removed an edges that the PC stage didn't remove

    def learn_structure(self):
        """
        Learn a partial ancestral graph (PAG) using the fast causal inference (FCI) algorithm
        :return:
        """
        # initial graph is a fully connected one with o--o edges between every pair of nodes
        # learn an initial skeleton using the same procedure as in the PC algorithm
        self._learn_pc_skeleton()

        # the resulting graph consists of only o--o edges
        # find and orient v-structures
        self.graph.orient_v_structures(self.sepset)

        # the resulting graph has only o--o, o-->, or <--> edges
        # find and remove edges between pairs of variables that are d-separated by some subset of Possible-D-SEP sets
        self.found_D_Sep_link = self._refine_pc_skeleton()

        # re-orient
        self.graph.reset_orientations(default_mark=Mark.Circle)
        self.graph.orient_v_structures(self.sepset)
        self.graph.maximally_orient_pattern(rules_set=[1, 2, 3, 4])
        if self.is_selection_bias:
            self.graph.maximally_orient_pattern(rules_set=[5, 6, 7])
        if self.is_tail_completeness:
            self.graph.maximally_orient_pattern(rules_set=[8, 9, 10])

    def _learn_pc_skeleton(self):
        """
        Learn an initial skeleton. This procedure is identical to the one of the PC algorithm
        :return:
        """

        self.pc_alg.learn_skeleton()
        self.sepset.copy_from(self.pc_alg.sepset, self.graph.nodes_set)
        self.graph.create_empty_graph()
        self.graph.copy_skeleton_from_pdag(self.pc_alg.graph)  # create edges with o-marks: X o--o Y
        self.graph.sepset = self.sepset

    def _refine_pc_skeleton(self):
        """
        Refine the skeleton (v-structures are oriented) recovered by the PC algorithm
        using subset of possible-d-sep set.

        :return: True if the graph was modified by this method
        """
        found_indep = False
        pds_list = dict()

        # Prepare the possible-d-sep set for each of the nodes
        for node_x in self.graph.nodes_set:
            pds_list[node_x] = possible_d_sep = self._create_pds_set(node_x)  # self.get_pds(node_x)

        # Test CI for the graph edges
        for node_x in self.graph.nodes_set:
            possible_d_sep = pds_list[node_x]
            adjacent_nodes = self.graph.find_adjacent_nodes(node_x)
            for node_y in adjacent_nodes:
                found_indep |= self._test_ci_increasing(node_x, node_y, possible_d_sep - {node_y})

        return found_indep

    def _test_ci_increasing(self, node_x, node_y, pds_super_set):
        """
        Search for a minimal separating set by gradually increasing conditioning set size.
        :param node_x: a node on one side of the tested edge
        :param node_y: a node on the other side of the tested edge
        :param pds_super_set: a super-set of nodes from which to construct conditioning sets
        :return: True if an edge was deleted, False if no independence was found
        """
        cond_indep = self.ci_test.cond_indep  # for better readability
        for ci_size in range(len(pds_super_set)+1):  # loop over condition set sizes; increasing set sizes
            for cond_set in combinations(pds_super_set, ci_size):  # loop over condition sets of a fixed size
                if cond_indep(node_x, node_y, cond_set):
                    self.graph.delete_edge(node_x, node_y)
                    self.sepset.set_sepset(node_x, node_y, cond_set)
                    return True

        return False

    def _create_pds_set(self, node_edge):
        """
        Construct a possible-d-sep set for node_edge

        :param node_edge: node on the edge being CI tested
        :return: a possible-d-sep
        """

        # Three lists are maintained: "first_nodes", "second_nodes", "neighbors".
        # Corresponding elements from the lists,
        #   "node_1" in "first_nodes",
        #   "node_2" in "second_nodes", and
        #   "node_3" in "neighbors",
        # form a path "node_1" --- "node_2" --- "node_3"
        # If this path is "legal" then "node_2" is in the possible-d-sep set and added to the PDS-tree

        # create an adjacency matrix (ignore edge-marks)
        adj_graph = self.graph.get_skeleton_graph()

        # initialize "first nodes" and "second nodes" lists
        neighbors = adj_graph.get_neighbors(node_edge)
        second_nodes = neighbors.copy()
        first_nodes = [node_edge for _ in range(len(second_nodes))]

        # initialize possible-d-sep list of nodes
        pds_nodes = neighbors.copy()  # initially: the neighbors of the node
        for node_nb in neighbors:
            adj_graph.remove_edge(node_edge, node_nb)  # make sure the search doesn't loop back to the root

        while len(second_nodes) > 0:
            node_1 = first_nodes.pop(0)
            node_2 = second_nodes.pop(0)

            neighbors = adj_graph.get_neighbors(node_2)

            for node_3 in neighbors:
                if self.graph.is_possible_collider(node_x=node_1, node_middle=node_2, node_y=node_3):  # test sub-path
                    adj_graph.remove_edge(node_2, node_3)
                    first_nodes.append(node_2)
                    second_nodes.append(node_3)
                    pds_nodes.append(node_3)

        possible_d_sep_set = set(pds_nodes)
        possible_d_sep_set.discard(node_edge)
        return possible_d_sep_set


# Extracts information from a project that has a distutils setup.py file.
import os
import sys
import logging

from copy import copy
from distutils import core

# From six (where else?):
if sys.version_info[0] == 3:
    def exec_(_code_, _globs_=None, _locs_=None):
        exec(_code_, _globs_, _locs_)
else:
    def exec_(_code_, _globs_=None, _locs_=None):
        """Execute code in a namespace."""
        if _globs_ is None:
            frame = sys._getframe(1)
            _globs_ = frame.f_globals
            if _locs_ is None:
                _locs_ = frame.f_locals
            del frame
        elif _locs_ is None:
            _locs_ = _globs_
        exec("""exec _code_ in _globs_, _locs_""")

class FakeContext(object):

    def __init__(self, path):
        self._path = path

    def __enter__(self):
        self._old_path = os.path.abspath(os.curdir)
        if self._old_path in sys.path:
            sys.path.remove(self._old_path)
        os.chdir(self._path)

        if self._path not in sys.path:
            sys.path.insert(0, self._path)
            self._path_appended = True
        else:
            self._path_appended = False

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self._path_appended:
            sys.path.remove(self._path)
        sys.path.append(self._old_path)

        os.chdir(self._old_path)

class SetupMonkey(object):

    used_setuptools = False

    def distutils_setup_replacement(self, **kw):
        self._distutils_setup(**kw)

    def setuptools_setup_replacement(self, **kw):
        self.used_setuptools = True
        self._setuptools_setup(**kw)

    def get_data(self):
        return self._kw

    def __enter__(self):
        import distutils.core
        self._distutils_setup = distutils.core.setup
        distutils.core.setup = self.distutils_setup_replacement

        try:
            import setuptools
            self._setuptools_setup = setuptools.setup
            setuptools.setup = self.setuptools_setup_replacement
        except ImportError:
            self._setuptools_setup = None

        self._kw = {}
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        import distutils.core
        distutils.core.setup = self._distutils_setup
        if self._setuptools_setup is not None:
            import setuptools
            setuptools.setup = self._setuptools_setup

# This is a version of distutils run_setup() that doesn't give
# up just because Setuptools throws errors if you try to exec it.
def run_setup(script_name, script_args=None, stop_after="run"):
    """Run a setup script in a somewhat controlled environment, and
    return the Distribution instance that drives things.  This is useful
    if you need to find out the distribution meta-data (passed as
    keyword args from 'script' to 'setup()', or the contents of the
    config files or command-line.

    'script_name' is a file that will be run with 'execfile()';
    'sys.argv[0]' will be replaced with 'script' for the duration of the
    call.  'script_args' is a list of strings; if supplied,
    'sys.argv[1:]' will be replaced by 'script_args' for the duration of
    the call.

    'stop_after' tells 'setup()' when to stop processing; possible
    values:
      init
        stop after the Distribution instance has been created and
        populated with the keyword arguments to 'setup()'
      config
        stop after config files have been parsed (and their data
        stored in the Distribution instance)
      commandline
        stop after the command-line ('sys.argv[1:]' or 'script_args')
        have been parsed (and the data stored in the Distribution)
      run [default]
        stop after all commands have been run (the same as if 'setup()'
        had been called in the usual way

    Returns the Distribution instance, which provides all information
    used to drive the Distutils.
    """
    if stop_after not in ('init', 'config', 'commandline', 'run'):
        raise ValueError("invalid value for 'stop_after': %r" % stop_after)

    core._setup_stop_after = stop_after

    save_argv = sys.argv
    glocals = copy(globals())
    glocals['__file__'] = script_name
    glocals['__name__'] = "__main__"
    try:
        try:
            sys.argv[0] = script_name
            if script_args is not None:
                sys.argv[1:] = script_args
            f = open(script_name)
            try:
                exec_(f.read(), glocals, glocals)
            finally:
                f.close()
        finally:
            sys.argv = save_argv
            core._setup_stop_after = None
    except Exception:
        logging.warn("Exception when running setup.", exc_info=True)

    if core._setup_distribution is None:
        raise RuntimeError(
            "'distutils.core.setup()' was never called -- "
            "perhaps '%s' is not a Distutils setup script?" %
            script_name)

    # I wonder if the setup script's namespace -- g and l -- would be of
    # any interest to callers?
    return core._setup_distribution

def get_data(path):
    """
    Returns data from a package directory.
    'path' should be an absolute path.
    """
    # Run the imported setup to get the metadata.
    with FakeContext(path):
        with SetupMonkey() as sm:
            try:
                distro = run_setup('setup.py', stop_after='config')

                metadata = {'_setuptools': sm.used_setuptools}

                for k, v in distro.metadata.__dict__.items():
                    if k[0] == '_' or not v:
                        continue
                    if all(not x for x in v):
                        continue
                    metadata[k] = v

                if sm.used_setuptools:
                    for extras in ['cmdclass', 'zip_safe', 'test_suite']:
                        v = getattr(distro, extras, None)
                        if v is not None and v not in ([], {}):
                            metadata[extras] = v

            except ImportError as e:
                # Either there is no setup py, or it's broken.
                logging.exception(e)
                metadata = {}

    return metadata


import rospy
import publisher_points as publ
from sensor_msgs.msg import Image
from sensor_msgs.msg import PointCloud2
import cv2
import numpy as np
import struct
import sensor_msgs.point_cloud2 as pc2

frm = None
previus_frame = None
msg_cpy = None

def callback(msg):
    ""
    global msg_cpy
    msg_cpy = msg
    #fields = msg_cpy.fields
    #print(fields)
    #rospy.loginfo(msg)
    #print("Image shape:", msg_cpy.height, msg_cpy.width)

def listener():
    global msg_cpy
    message_processed = True
    rospy.init_node('listener', anonymous=True)
    pub = rospy.Publisher('datapoints', PointCloud2, queue_size=100)
    rospy.Subscriber(publ.points, PointCloud2, callback)
    # Create publisher object to publish points to RViz
    rate = rospy.Rate(3) # 100Hz
    BG = None
    while not rospy.is_shutdown():
        key = cv2.waitKey(1)
        if key == 27:
            exit()
        if msg_cpy is not None and message_processed:
            point_cloud = pc2.read_points(msg_cpy, skip_nans=False, field_names= ("x", "y", "z"))
            points = np.array(list(point_cloud))
            x = points[:,0]
            y = points[:,1]
            z = points[:,2]
            #print(f"X: {x} with size of {len(x)}")
            #print(f"Y: {y} with size of {len(y)}")
            #print(f"Z: {z} with size of {len(z)}")
            publ.index = publ.index+1
            if (publ.index == 1):
                BG = msg_cpy

            elif (publ.index >= 41):
                msg_cpy = None
                publ.index = 0
                continue

            else:
                for j in range(len(points[:,0])):
                    xi = x[j]
                    yi = y[j]
                    zi = z[j]
                    #print(f"point {i}: X:{xi}\tY:{yi}\tZ:{zi}")
            print(publ.index)
            pub.publish(msg_cpy)
            FM = msg_cpy

            if FM == BG:
                print("yes")
        elif msg_cpy is None:
            message_processed = True

        rate.sleep()

if __name__ == '__main__':
    try:
        listener()

    except rospy.ROSInterruptException:
        pass

"""
"save" the first PointCloud as "Background"
for all other pointclouds
    iterate over all the points
    for each point (where p=x,y,z)
        find closest point q where q is a subset of "background"
        if |q-p| > d where d is the threshold
            add p to O (O is a new pointcloud)
"""


""" funcX : Fast function serving for clouds, clusters and supercomputers.

"""
import logging

from pythonjsonlogger import jsonlogger

from .version import VERSION

__author__ = "The funcX team"
__version__ = VERSION

def set_stream_logger(name="funcx_forwarder", level=logging.DEBUG, format_string=None):
    """Add a stream log handler.

    Args:
         - name (string) : Set the logger name.
         - level (logging.LEVEL) : Set to logging.DEBUG by default.
         - format_string (string) : Set to None by default.

    Returns:
         - Logger
    """
    if format_string is None:
        format_string = "%(asctime)s %(name)s %(levelname)s %(message)s"

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler()
    handler.setLevel(level)
    formatter = jsonlogger.JsonFormatter(format_string)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger

logging.getLogger("funcx_forwarder").addHandler(logging.NullHandler())


# This code is to be used in Thomas Bottegal's
# 'Harmony' software, which is used for chordal analysis
# of sheet music

root_third_interval = 3 # Number of half steps
third_fifth_interval = 4 # Number of half steps
root_note = 'D#'
roman_numeral = 'iii' # Case doesn't matter here
key_signature = ['F#', 'C#'] # D major or B minor as example

def determine_chord_quality(root_third_interval, third_fifth_interval):
  chord_quality = None
  if root_third_interval == 3:
    if third_fifth_interval == 3:
      chord_quality = 'Diminished'
    else:
      chord_quality = 'Minor'
  elif root_third_interval == 4:
    if third_fifth_interval == 4:
      chord_quality = 'Augmented'
    else:
      chord_quality = 'Major'
  return chord_quality

def determine_roman_numeral_case(roman_numeral, chord_quality):
  if chord_quality == 'Diminished' or chord_quality == 'Minor':
    roman_numeral = roman_numeral.lower()
  else:
    roman_numeral = roman_numeral.upper()
  return roman_numeral

def determine_accidental(roman_numeral, root_note):
  if '#' in root_note and root_note not in key_signature:
    roman_numeral = '#' + roman_numeral
  # I don't know what symbol you're using to denote flats
  elif 'flat' in root_note and root_note not in key_signature:
    roman_numeral = 'flat' + roman_numeral
  # Same here, not sure what you're using to denote naturals
  elif 'natural' in root_note and root_note not in key_signature:
    roman_numeral = 'natural' + roman_numeral
  return roman_numeral

chord_quality = determine_chord_quality(root_third_interval, third_fifth_interval)
roman_numeral = determine_roman_numeral_case(roman_numeral, chord_quality)
roman_numeral = determine_accidental(roman_numeral, root_note)

print(chord_quality)
print(roman_numeral)

# Routines for finding the inversion of a chord

def get_root_bass_interval(chord_root, bass_note):
  note_seq = list("CDEFGABCDEFGA")
  range_start = note_seq.index(chord_root[0])
  for j in range(range_start, range_start+7):
    if bass_note[0] == note_seq[j]:
      break
  root_bass_interval = j-range_start+1
  return root_bass_interval

# chord_root and bass_note expected as a one or two character string
def get_chord_inversion(chord_root, bass_note, seventh_chord = False):
  root_bass_interval = get_root_bass_interval(chord_root, bass_note)
  chord_interval = None
  if root_bass_interval == 3:
    if seventh_chord == False:
      chord_interval = '6'
    else:
      chord_interval = '6/5'
  elif root_bass_interval == 5:
    if seventh_chord == False:
      chord_interval = '6/4'
    else:
      chord_interval = '4/3'
  # Non 7th chords can't have a third inversion
  elif root_bass_interval == 7:
    chord_interval = '4/2'
  return chord_interval

print(get_chord_inversion('A', 'C', True))
print(get_chord_inversion('A', 'C'))
print(get_chord_inversion('C#', 'G#'))
print(get_chord_inversion('C#', 'G#', True))
print(get_chord_inversion('G', 'F'))


from dataclasses import dataclass
from typing import Dict, Optional, List

import discord

from .abstract import AbstractGuildConverter, PartialDiscordModel
from .role import PartialRoleModel, RoleConverter
from .channels.channel import PartialCategoryChannelModel, PartialGuildChannelModel
from .channels.getter import ChannelConverterGetter
from . import task

@dataclass
class PartialGuildModel(PartialDiscordModel):
    name: Optional[str]
    channels: List[PartialGuildChannelModel]
    roles: List[PartialRoleModel]

    def get_tasks(self, real_guild: discord.Guild) -> List[task.BaseTask]:
        tasks = []
        if real_guild.name != self.name:
            tasks.append(task.GuildEditTask(real_guild, self.name))  #  現在のチャンネル名と異なるのでチャンネル名編集のタスクを追加する

        for role_data in self.roles:
            role: discord.Role = real_guild.get_role(role_data.id_)

            if role is None:
                tasks.append(task.RoleAddTask(
                    real_guild,
                    name=role_data.name,
                    permissions=role_data.permissions,
                    colour=role_data.colour,
                    hoist=role_data.hoist,
                    mentionable=role_data.mentionable,
                ))  #  現在のギルドに同一IDのロールが存在しないので、追加する
                continue

            real_role_data = RoleConverter.parse(RoleConverter.convert(role))
            if real_role_data == role_data:
                continue  #  同一なので、編集する必要がない

            def get_data(data, real_data):
                return data if data is not None else real_data

            name: str = get_data(role_data.name, role.name)  #  ロール名は指定されていないければ現在の名前を使用する
            permissions: discord.Permissions = get_data(role_data.permissions, role.permissions)
            colour: discord.Colour = get_data(role_data.colour, role.colour)
            hoist: bool = get_data(role_data.hoist, role.hoist)
            mentionable: bool = get_data(role_data.mentionable, role.mentionable)

            tasks.append(task.RoleEditTask(
                role,
                name=name,
                permissions=permissions,
                colour=colour,
                hoist=hoist,
                mentionable=mentionable
            ))  #  現在のギルドに同一IDのロールが存在するので、編集する

        for role in real_guild.roles:
            role_data: Optional[PartialRoleModel] = discord.utils.get(self.roles, id_=role.id)
            if role_data is None:
                tasks.append(task.RoleRemoveTask(role))  #  設定ファイルに同一IDのロールが存在しないので、削除する

        for channel_data in self.channels:
            channel: Optional[discord.abc.GuildChannel] = real_guild.get_channel(channel_data.id_)
            if channel is None:
                pass  #  現在のギルドに同一IDのチャンネルが存在しないので、追加する

        for channel in real_guild.channels:
            channel_data: Optional[PartialGuildChannelModel] = discord.utils.get(self._flatted_channels, id_=channel.id)
            if channel_data is None:
                tasks.append(task.ChannelRemoveTask(channel))  #  設定ファイルに同一IDのチャンネルが存在しないので、削除する

        return tasks

    @property
    def _flatted_channels(self) -> List[PartialGuildChannelModel]:
        flatted = []
        for channel in self.channels:
            if channel.type_ == discord.ChannelType.category:
                category: PartialCategoryChannelModel = channel
                flatted.append(category)
                flatted += category.channels
            else:
                flatted.append(channel)
        return flatted

class GuildConverter(AbstractGuildConverter):
    key_id = 'ID'
    key_name = 'サーバー名'
    key_channels = 'チャンネル'
    key_roles = 'ロール'

    @classmethod
    def convert(cls, target: discord.Guild):
        channels = []
        for channel in target.channels:
            if channel.category_id is not None:
                continue  #  親が存在するので無視

            channels.append(ChannelConverterGetter.get_converter(channel).convert(channel))

        roles = []
        for role in target.roles:
            roles.append(RoleConverter.convert(role))

        return {
            cls.key_id: str(target.id),
            cls.key_name: target.name,
            cls.key_channels: channels,
            cls.key_roles: roles
        }

    @classmethod
    def parse(cls, target: Dict) -> PartialGuildModel:
        name = target[cls.key_name]
        roles_data: List[PartialRoleModel] = list(map(lambda role: RoleConverter.parse(role), target[cls.key_roles]))
        channels = list(map(lambda channel: ChannelConverterGetter.get_parser(channel).parse(channel), target[cls.key_channels]))

        return PartialGuildModel(name=name, roles=roles_data, channels=channels)


from django.urls import path
from . import views

urlpatterns = [
    path('register', views.registerationPage),
    path('login', views.loginPage, name='login'),
    path('logout', views.logoutUser, name='logout'),
    path('shopping_card', views.shoppingCard, name='shopping_card'),
    path('delete_order/<order_id>', views.deleteOrder, name='delete_order'),
    #path('profile/', views.profileStatus, name='profile'), for when we needed profile

]


from Rotor import ROTOR

class MACHINE():
    def __init__(self, r1, r2, r3, rf):
        self.r1 = ROTOR(r1)
        self.r2 = ROTOR(r2)
        self.r3 = ROTOR(r3)
        self.rf = ROTOR(rf)
        self.counter = 0
        self.key_map = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
                        'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    def machine_state_update(self):
        self.counter += 1
        self.r1.state_update()
        if (self.counter % 26 == 0) & (self.counter > 1):
            self.r2.state_update()
            if (self.counter % 26 ** 2) == 0 & (self.counter > 1):
                self.r3.state_update()

    def encode(self, code_letter):
        # self.machine_state_update()
        num = self.key_map.index(code_letter)
        x = self.r1.go_in(num)
        x = self.r2.go_in(x)
        x = self.r3.go_in(x)
        x = self.rf.go_in(x)
        x = self.r3.go_back(x)
        x = self.r2.go_back(x)
        x = self.r1.go_back(x)
        self.machine_state_update()
        return self.key_map[x]


from marshmallow import ValidationError
from werkzeug.exceptions import HTTPException, UnprocessableEntity
from flask import jsonify

class ApiExceptionHandler:
    def __init__(self, app):
        self.app = app
        self.register()

    def register(self):
        self.app.register_error_handler(
            HTTPException, self._handle_http_exception)
        self.app.register_error_handler(
            UnprocessableEntity, self._handle_unprocessable_exception)

    def _handle_http_exception(self, exception: HTTPException):
        return jsonify(description=exception.description, status_code=exception.code), exception.code

    def _handle_unprocessable_exception(self, exception: ValidationError):
        messages = exception.data.get('messages', ['Invalid request.'])
        return jsonify(
            description=exception.description,
            status_code=exception.code,
            fields=messages.get('json', messages)
        ), exception.code


import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    """Alien class, ome alien amongst the fleet."""

    def __init__(self, settings, screen):
        super(Alien, self).__init__()
        self.screen = screen
        self.settings = settings

        # The alien image.
        self.image = pygame.image.load('images/alien.png').convert_alpha()
        self.rect = self.image.get_rect()

        # Start each new alien in the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # Store the aliens exact position.
        self.x = float(self.rect.x)

        # Is this alien on the front line?
        self.row = 0
        self.front_line = False

    def check_edges(self):
        """Return true if alien is at edge of screen."""
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right:
            return True
        elif self.rect.left <= 0:
            return True

    def update(self):
        """Move the alien right or left."""
        self.x += (self.settings.alien_speed_factor *
                   self.settings.fleet_direction)
        self.rect.x = self.x


from django.conf import settings
from django.db import models
from edcs_consent.model_mixins import RequiresConsentFieldsModelMixin
from edcs_sites.models import SiteModelMixin
from edcs_visit_schedule.model_mixins import (
    VisitTrackingCrfModelMixin,
)

class CrfNoManagerModelMixin(
    VisitTrackingCrfModelMixin,
    RequiresConsentFieldsModelMixin,
    SiteModelMixin,
):
    """Base model for all scheduled models"""

    subject_visit = models.OneToOneField(
        settings.SUBJECT_VISIT_MODEL, on_delete=models.PROTECT
    )

    def natural_key(self) -> tuple:
        return self.subject_visit.natural_key()

    natural_key.dependencies = [  # type:ignore
        settings.SUBJECT_VISIT_MODEL,
        "sites.Site",
        "edc_appointment.appointment",
    ]

    class Meta:
        abstract = True
        indexes = [models.Index(fields=["subject_visit", "site", "id"])]
        default_permissions = ("add", "change", "delete", "view", "export", "import")


from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
#dj的翻译函数ugettext_lazy
import datetime  # 用来判断日期区间

from django import forms
from django.forms import ModelForm

from .models import BookInstance

class RenewBookForm(forms.Form):
    """一个表单,方便图书管理员续借"""
    renewal_date = forms.DateField(
            help_text="请输入一个4周以内的时间,默认时间3周..")

    def clean_renewal_date(self):
        #这里用默认表单验证工具,cleanend_data..
        #使用了默认的验证器,完成了对数据的清理,清楚可能不安全的输入
        #将数据转换成正确的标准类型即py的datetime.datetime对象
        data = self.cleaned_data['renewal_date']

        #更新日期不能在本日之前
        if data < datetime.date.today():
            raise ValidationError(_('无效日期- renewal in past'))
        #更行日期是否超过了4周的借个
        if data > datetime.date.today() + datetime.timedelta(weeks=4):
            raise ValidationError(
                _('Invalid date - renewal more than 4 weeks ahead'))

        #切记要返回更新后的日期..
        return data

#这里使用的是ModelForm
class RenewBookModelForm(ModelForm):
    def clean_due_back(self):
       data = self.cleaned_data['due_back']

       #Check date is not in past.
       if data < datetime.date.today():
           raise ValidationError(_('Invalid date - renewal in past'))

       #Check date is in range librarian allowed to change (+4 weeks)
       if data > datetime.date.today() + datetime.timedelta(weeks=4):
           raise ValidationError(_('Invalid date - renewal more than 4 weeks ahead'))

       # Remember to always return the cleaned data.
       return data

    class Meta:
        model = BookInstance
        fields = ['due_back',]
        labels = { 'due_back': _('Renewal date'), }
        help_texts = { 'due_back': _('Enter a date between now and 4 weeks (default 3).'), }


import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.neighbors import KNeighborsClassifier
########### Breast Cancer ################

b_cancer=pd.read_csv(r"C:\Training\Academy\Statistics (Python)\Cases\Wisconsin\BreastCancer.csv",
                     index_col=0)

dum_b_cancer = pd.get_dummies(b_cancer, drop_first=True)
X = dum_b_cancer.drop('Class_Malignant',axis=1)
y = dum_b_cancer['Class_Malignant']

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)
knn = KNeighborsClassifier(n_neighbors=3)
results = cross_val_score(knn, X, y ,scoring='roc_auc',
                          cv = kfold)
print(results)
print(results.mean())

roc = []
ks = np.arange(1,30,2)
for i in ks:
    knn = KNeighborsClassifier(n_neighbors=i)
    results = cross_val_score(knn, X, y ,scoring='roc_auc',
                              cv = kfold)
    roc.append(results.mean())

i_max = np.argmax(roc)
print("Best k =", ks[i_max], "with score =", np.max(roc))

############# HR

df=pd.read_csv(r"C:\Training\Academy\Statistics (Python)\Cases\human-resources-analytics\HR_comma_sep.csv")

dum_df = pd.get_dummies(df, drop_first=True)
X = dum_df.drop('left',axis=1)
y = dum_df['left']

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)

roc = []
ks = np.arange(1,30,2)
for i in ks:
    knn = KNeighborsClassifier(n_neighbors=i)
    results = cross_val_score(knn, X, y ,scoring='roc_auc',
                              cv = kfold)
    roc.append(results.mean())

i_max = np.argmax(roc)
print("Best k =", ks[i_max], "with score =", np.max(roc))

############### Grid Search CV
from sklearn.model_selection import GridSearchCV

b_cancer=pd.read_csv(r"C:\Training\Academy\Statistics (Python)\Cases\Wisconsin\BreastCancer.csv",
                     index_col=0)
dum_b_cancer = pd.get_dummies(b_cancer, drop_first=True)
X = dum_b_cancer.drop('Class_Malignant',axis=1)
y = dum_b_cancer['Class_Malignant']

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)
knn = KNeighborsClassifier()

params = {'n_neighbors':np.arange(1,30,2)}
gcv = GridSearchCV(knn, param_grid=params,scoring='roc_auc',
                          cv = kfold, verbose=3)
gcv.fit(X, y)
print(gcv.best_params_)
print(gcv.best_score_)

pd_gcv = pd.DataFrame(gcv.cv_results_)

############### Image Segementation
import os
os.chdir(r"C:\Training\Academy\Statistics (Python)\Cases\Image Segmentation")

from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

imag_seg = pd.read_csv('Image_Segmention.csv')

X = imag_seg.drop('Class', axis=1)
y = imag_seg['Class']

le = LabelEncoder()
le_y = le.fit_transform(y)
print(le.classes_)

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)

scaler = StandardScaler()
knn = KNeighborsClassifier()
pipe = Pipeline([('scl_std', scaler), ('knn_model', knn)])

params = {'knn_model__n_neighbors':np.arange(1,31)}
gcv = GridSearchCV(pipe, param_grid=params,scoring='neg_log_loss',
                          cv = kfold)
gcv.fit(X, y)
print(gcv.best_params_)
print(gcv.best_score_)

pd_gcv = pd.DataFrame(gcv.cv_results_)


#!/usr/bin/env python3

import sys
import random

NUM_VARS=int(sys.argv[1])
NUM_SAMPLES=int(sys.argv[2])
MAX_VAL=int(sys.argv[3])

for i in range(0, NUM_SAMPLES):
    sample = [str(i)]
    sample += [str(random.randint(0,MAX_VAL))]
    for i in range(NUM_VARS):
        sample += [str(random.randint(0,1))]
    print(",".join(sample))


import sys
import random

# 回答できる回数の設定
print("numer0nを始めます。20以下の答える回数を入力してください。(推奨:10)")
while True:
    try:
        reply = int(input())
        while True:
            if(reply > 20):
                print("20以下の数字を入力してください。(推奨:10)")
                reply = int(input())
            else:
                print("***************************************")
                break
        break
    except ValueError as e:
        print("数字を入力してください。")

# 答えの桁数の設定
print("続いて、解答の桁数(5桁以下)を入力してください。(推奨:4)")
while True:
    try:
        digit = int(input())
        while True:
            if(digit > 5):
                print("5以下の数字を入力してください。")
                digit = int(input())
            else:
                print("***************************************")
                break
        break
    except ValueError as e:
        print("数字を入力してください。")

# 正解の数字の生成
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(num)
ans = num[0:digit]

# 正誤判定
res = []

def isCollect(num):
    eat = 0
    bite = 0
    varDigit = "{:0"+str(digit)+"}"
    num = varDigit.format(num)
    numList = [int(a) for a in num]

    for i in range(len(ans)):
        if(numList[i] == ans[i]):
            eat += 1
        elif(numList[i] in ans):
            bite += 1
    if(int(eat) == int(digit)):
        print("正解は"+num + "でした。おめでとうございます！")
        print("回答した回数は"+str(int(len(res)+1))+"回！")
        sys.exit()
    print(num+"は、 eat: " + str(eat)+", bite: " + str(bite)+"です")
    res.append([str(num), "eat: " + str(eat), "bite: " + str(bite)])
    for i in res:
        print(i)
    print("***************************************")

print("それではゲームを始めます。")
# 回答のループ
i = 0
while i < reply:
    print(str(digit)+"桁の数字を入力してください")
    while True:
        try:
            challenge = str(input())
            while True:
                if(str(len(challenge)) != str(digit)):
                    print("入力に誤りがあります。"+str(digit)+"桁の数字を入力してください。")
                    challenge = str(input())
                else:
                    isCollect(int(challenge))
                    break
            break
        except ValueError as e:
            print("予期せぬエラー発生")
    i += 1

# 指定の回数以内に正解できなかった場合、答えを表示
map = map(str, ans)
print("残念！正解は"+str(''.join(map))+"でした！")


# find the largest and smallest number in an unsorted integer array
import sys

def largest_smallest(theList):
    largest = theList[0]
    smallest = theList[0]
    secondLargest = None
    secondSmallest = None
    for item in theList:
        if item > largest:
            secondLargest = largest
            largest = item
        elif secondLargest == None or secondLargest < item:
            secondLargest = item
        if item < smallest:
            secondSmallest = smallest
            smallest = item
        elif secondSmallest == None or secondSmallest > item:
            secondSmallest = item
    print("largest: ", largest)
    print("second largest: ", secondLargest)
    print("smallest: ", smallest)
    print("second smallest ", secondSmallest)

if __name__ == "__main__":
    arguments = sys.argv[1]
    input = list(map(int, arguments.split(",")))
    largest_smallest(input)


import json

from pyshark.packet.layer import JsonLayer
from pyshark.packet.packet import Packet

def duplicate_object_hook(ordered_pairs):
    """Make lists out of duplicate keys."""
    json_dict = {}
    for key, val in ordered_pairs:
        existing_val = json_dict.get(key)
        if not existing_val:
            json_dict[key] = val
        else:
            if isinstance(existing_val, list):
                existing_val.append(val)
            else:
                json_dict[key] = [existing_val, val]

    return json_dict

def packet_from_json_packet(json_pkt):
    # NOTE: We can use ujson here for ~25% speed-up, however since we can't use hooks in ujson
    # we lose the ability to view duplicates. This might still be a good option later on.
    pkt_dict = json.loads(json_pkt.decode('utf-8'), object_pairs_hook=duplicate_object_hook)
    # We use the frame dict here and not the object access because it's faster.
    frame_dict = pkt_dict['_source']['layers'].pop('frame')
    layers = []
    for layer in frame_dict['frame.protocols'].split(':'):
        layer_dict = pkt_dict['_source']['layers'].pop(layer, None)
        if layer_dict is not None:
            layers.append(JsonLayer(layer, layer_dict))
    # Add all leftovers
    for name, layer in pkt_dict['_source']['layers'].items():
        layers.append(JsonLayer(name, layer))

    return Packet(layers=layers, frame_info=JsonLayer('frame', frame_dict),
                  number=int(frame_dict.get('frame.number', 0)),
                  length=int(frame_dict['frame.len']),
                  sniff_time=frame_dict['frame.time'],
                  interface_captured=frame_dict.get('frame.interface_id'))


from main import vcon

def consultar(conexao, sql):

    c=conexao.cursor()
    c.execute(sql)
    resultado=c.fetchall()
    return resultado

#PESQUISA TUDO
#vsql="SELECT * FROM tb_contatos"
#PESQUISA ESPECIFICA
vsql="SELECT * FROM tb_contatos WHERE T_NOMECONTATO='Nivaldo'"

res=consultar(vcon,vsql)
for r in res:
    print(r)


#Time complexity: O(logn)
#Space complexity: O(1)

def binary_search1(alist, item)->bool:
    left = 0
    right = len(alist) - 1
    mid = (left + right) // 2

    #base case
    if left > right:
        return False

    elif alist[mid] == item:
        return True

    elif alist[mid] > item:
        return binary_search1(alist[:mid], item)

    else:
        return binary_search1(alist[mid + 1:], item)

def binary_search2(alist, item):
    left = 0
    right = len(alist) - 1
    found = False

    while left <= right and not found:
        mid = (left + right) // 2

        if alist[mid] == item:
            found = True

        elif alist[mid] < item:
            left = mid + 1

        else:
            right = mid - 1

    return found

if __name__ == "__main__":
    alist = [0, 1, 2, 8, 13, 17, 19, 32, 42, 66]
    item = int(input('Enter a value you want to find: '))
    print(binary_search1(alist, item))
    print(binary_search2(alist, item))

Fundamentals/Data StructuresAndAlgorithms/Sorting And Searching/Searching/binarySearch.py


PKG = 'runtime_monitor'
import roslib; roslib.load_manifest(PKG)

import wx
import unittest
import rospy, rostest
from time import sleep
import sys
from optparse import OptionParser

from runtime_monitor.monitor_panel import *

import threading

DURATION = 10

##\brief Main frame of runtime monitor for testing only
class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)

        self.panel = MonitorPanel(self)
        self.SetSize(wx.Size(750, 450))

    def on_exit(self, e):
        self.Close(True)

class MonitorApp(wx.App):
    def OnInit(self):
        self._frame = MainWindow(None, 'Runtime Monitor')
        self._frame.SetSize(wx.Size(500, 700))
        self._frame.Layout()
        self._frame.Show(True)
        return True

class RuntimeRunner(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.app = MonitorApp()

    def run(self):
        self.app.MainLoop()

class TestRuntimeMonitor(unittest.TestCase):
    def __init__(self, *args):
        super(TestRuntimeMonitor, self).__init__(*args)

        rospy.init_node('test_runtime_monitor', anonymous=True)
        parser = OptionParser(usage="usage ./%prog [options]", prog="test_monitor.py")

        # Option comes with rostest, will fail w/o this line
        parser.add_option('--gtest_output', action="store",
                          dest="gtest")

        options, args = parser.parse_args(rospy.myargv())

        self.runner = RuntimeRunner()
        self.runner.start()

    def test_runtime_monitor(self):
        start = rospy.get_time()
        while not rospy.is_shutdown():
            if rospy.get_time() - start > DURATION:
                break
            self.assert_(self.runner.isAlive(), "Thread running runtime monitor isn't running.")
            sleep(1.0)

        try:
            wx.CallAfter(self.runner.app._frame.on_exit(None))
            self.runner.join(5)
        except:
            import traceback
            self.assert_(False, "Caught exception closing runtime monitor: %s" % traceback.format_exc())

        self.assert_(not self.runner.isAlive(), "Thread running runtime monitor didn't join up.")
        self.assert_(not rospy.is_shutdown(), "Rospy shut down during test.")

if __name__ == '__main__':
    rostest.run(PKG, sys.argv[0], TestRuntimeMonitor, sys.argv)


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

# -- Project --

# # Final Project - Analyzing Sales Data
#
# **Date**: 30 December 2021
#
# **Author**: Kasidis Satangmongkol (Toy DataRockie)
#
# **Course**: `Pandas Foundation`

from pandas import DataFrame
# import data
import pandas as pd
df  = pd.read_csv("sample-store.csv")

# preview top 5 rows
df.head()

# shape of dataframe
df.shape

# see data frame information using .info()
df.info()

# We can use `pd.to_datetime()` function to convert columns 'Order Date' and 'Ship Date' to datetime.

# example of pd.to_datetime() function
pd.to_datetime(df['Order Date'].head(), format='%m/%d/%Y')

# TODO - convert order date and ship date to datetime in the original dataframe
import pandas as pd
df['Order Date'] = pd.to_datetime(df['Order Date'])
df['Ship Date'] = pd.to_datetime(df['Ship Date'])

df[['Order Date', 'Ship Date']].head()

# TODO - count nan in postal code column
df['Postal Code'].isna().sum()

# TODO - filter rows with missing values
df[df['Postal Code'].isna()]

# count country
df['Segment'].unique()

# TODO - Explore this dataset on your owns, ask your own questions

    # Which segment is higher sale?
import pandas as pd
my1_question = df.groupby(['Segment', 'Category', 'Sub-Category'])['Sales']\
    .sum().reset_index()\
    .sort_values('Sales', ascending = False)\
    .head(1)
print("Question 1")
print(my1_question)
print("-------------------------------------------------------------")
print(f"""The higher sales segment is Consumer.
Category is Furniture that it is Chairs can sales $ 172862.742""")
print("--------------------------------------------------------------")

    # Which product can make the most profit (top 3)
my2_question = df.groupby(['Segment', 'Category', 'Sub-Category'])['Profit']\
    .sum().reset_index()\
    .sort_values('Profit', ascending = False)\
    .head(3)
print("Question 2")
print(my2_question)
print(f"""The higher profitable is Copiers. It can make a profit $ 24083.71.
Following phones, $ 23837.11 and Accessories, $ 20735.92.
""")
print("--------------------------------------------------------------------")

    # What is the top 5 Sub-Category by Quantity
my3_question = df.groupby(['State', 'City', 'Sub-Category'])['Quantity']\
    .agg('sum','count').reset_index()\
    .sort_values('Quantity', ascending = False)\
    .head(5)
print("Question 3")
print(my3_question)
print(f"""
New York city is the first and second sub-category is Binders at 562 quantity,
and Paper at 464 quantity. Following by California, Paper at 454 quantity and
Binders at 360 quantity. The last one is Washington, sub-category is Binders
at 329 quantity.
""")

# ## Data Analysis Part
#
# Answer 10 below questions to get credit from this course. Write `pandas` code to find answers.

# TODO 01 - how many columns, rows in this dataset
df.shape

# TODO 02 - is there any missing values?, if there is, which colunm? how many nan values?
df.isna().sum().sort_values(ascending= False)

# TODO 03 - your friend ask for `California` data, filter it and export csv for him
df_cali = df.query('State == "California"').dropna()
df_cali.to_csv('df_cali.csv')

df_cali

# TODO 04 - your friend ask for all order data in `California` and `Texas` in 2017 (look at Order Date), send him csv file
df_order_cali_tex = df[((df['State'] == 'California') | (df['State'] == 'Texas')) & (df['Order Date'].dt.strftime('%Y') == '2017')]
df_order_cali_tex.to_csv("df_order_cali_tex.csv")

df_order_cali_tex

# TODO 05 - how much total sales, average sales, and standard deviation of sales your company make in 2017
df[df['Order Date'].dt.strftime('%Y') == '2017']['Sales']\
    .agg(['sum','average', 'std'])\
    .round()

# TODO 06 - which Segment has the highest profit in 2018
df_2018 = df[df['Order Date'].dt.year == 2018]

df_2018.groupby(['Segment', 'Category', 'Sub-Category', 'Order Date'])['Profit']\
    .sum().reset_index()\
    .sort_values('Profit', ascending = False)\
    .round()\
    .head(3)

# TODO 07 - which top 5 States have the least total sales between 15 April 2019 - 31 December 2019
df[(df['Order Date'] > '2019-04-15') & (df['Order Date'] < '2019-12-31')]\
    .groupby(['State'])['Sales']\
    .agg('sum')\
    .reset_index()\
    .sort_values('Sales', ascending = True)\
    .head(5)\
    .round()

# TODO 08 - what is the proportion of total sales (%) in West + Central('Region') in 2019 e.g. 25%
import pandas as pd
    # sum total sales
total_sales_2019  = df[df['Order Date'].dt.strftime('%Y') == '2019']['Sales'].sum().round()

    # sum total sales of West and Central
total_west_central_sales = df[((df['Region'] == 'West') | (df['Region'] == 'Central'))\
    & (df['Order Date'].dt.strftime('%Y') == '2019')]['Sales']\
    .sum()\
    .round()

    # the proportion of total sales(%)
per_sale_wc = (total_west_central_sales / total_sales_2019) * 100

print(f"The proportion of total sales (%) in West and Central in 2019 is {per_sale_wc.round()}")

# TODO 09 - find top 10 popular products in terms of number of orders vs. total sales during 2019-2020
import pandas as pd
df_19_20 = df[df['Order Date'].dt.year.isin([2019, 2020])]

top10_product_19_20 = df_19_20.groupby(['Product Name'])['Order ID']\
    .agg('count')\
    .reset_index()\
    .sort_values('Order ID', ascending = False)\
    .head(10)

print(f"The top 10 popular products during in 2019-2020 is\n {top10_product_19_20}")

# TODO 10 - plot at least 2 plots, any plot you think interesting :)

    # [1] Which Segment is the higher sales during Year 2019 - 2020

df_19_20.groupby(['Segment'])['Sales']\
    .agg('sum')\
    .round()\
    .plot(kind = 'bar',\
          x = 'Segment',\
          y = 'Total Sales',\
          color = ['salmon', 'orange', 'yellow']);

# TODO 10 - plot at least 2 plots, any plot you think interesting :)

    #

    # unique Year
#df['Order Date'].dt.year.unique()

    #filter year 2020
df_2020 = df[df['Order Date'].dt.year.isin([2020])]

    # Top 10 City Orders by Product names
df_2020.groupby(['Region'])['Sales']\
    .sum()\
    .round()\
    .reset_index()\
    .sort_values('Sales', ascending = False)\
    .plot(kind = 'bar',\
          x = 'Region',\
          y = 'Sales',\
          color = ['pink', 'orange', 'gold', 'yellow']);

# TODO Bonus - use np.where() to create new column in dataframe to help you answer your own questions
import pandas as pd
import numpy as np

# find mean of Quantity
avg_quantity = df_2020['Quantity'].mean().round()
print(f'The mean of Quantity is {avg_quantity}')

# if quantity <= 4 , Restock, Remain

import numpy as np

quan_restock = df_2020.groupby(['Category','Sub-Category', 'Quantity'])['Profit']\
    .sum()\
    .round()\
    .reset_index()\
    .sort_values('Profit', ascending = False)

quan_restock['Restock'] = np.where(quan_restock["Quantity"] <= 4, "Yes", "No")

# count yes no

count_yn = quan_restock['Restock'].value_counts().reset_index()

print(f'The question is Should we need to restock the product by using quantity.')
print(f'The mean of Quantity is {avg_quantity}, If quantity is <= 4, it should be restock, else is should be remain')
print("----------------------------------------------------------------------------------------------------")
print(quan_restock)
print("----------------------------------------------------------------------------------------------------")
print(f"""
In column 'Restock', the number of product that should be restock is 68
and product that should remain is 110.\n
{count_yn}
""")

Python/Home_work/pandas_final_project/Course Pandas Foundation - Final Project (Template).py


from dolfin import UnitIntervalMesh, FunctionSpace, DomainBoundary
from dolfin import interpolate, Expression
from hsmg.hsmg import Hs0NormMG

def test():
    mesh = UnitIntervalMesh(128)
    V = FunctionSpace(mesh, 'CG', 1)
    bdry = DomainBoundary()
    s = 0.5
    mg_params = {'macro_size': 1,
                 'nlevels': 4,
                 'eta': 0.4}

    MG_Half = Hs0NormMG(V, bdry, s, mg_params)

    x = interpolate(Expression('sin(k*pi*x[0])', k=3, degree=4), V).vector()
    # Action
    y = MG_Half*x
    # Nothing raises

# --------------------------------------------------------------------

if __name__ == '__main__':

    test()


from time import sleep

matriz = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
soma_pares = soma_coluna = maior_valor = 0
for linha in range(0, 3):
    for coluna in range(0, 3):
        matriz[linha][coluna] = int(
            input(f"Digite um valor para: [{linha}, {coluna}] ")
        )
print('Gerando a matriz...')
sleep(1)
for linha in range(0, 3):
    for coluna in range(0, 3):
        print(f"[{matriz[linha][coluna]:^5}]", end="")
        if matriz[linha][coluna] % 2 == 0:
            soma_pares += matriz[linha][coluna]
    print()
print(f"A soma dos valores pares é: {soma_pares} ")
for linha in range(0, 3):
    soma_coluna += matriz[linha][2]
print(f"A soma da terceira coluna é: {soma_coluna} ")
for coluna in range(0, 3):
    if coluna == 0:
        maior_valor = matriz[1][coluna]
    elif matriz[1][coluna] > maior_valor:
        maior_valor = matriz[1][coluna]
print(f"O maior valor da segunda linha é: {maior_valor} ")


"""
https://www.algoexpert.io/questions/Caesar%20Cipher%20Encryptor
"""

from string import ascii_lowercase as lower

# Sol 1
def caesar_rotation(string, key):
    alphabet_length = 26
    index_letter = {i: letter for i, letter in enumerate(lower, 1)}

    new_string = []
    for letter in string:
        for index, char in index_letter.items():
            if letter == char:
                new_value = index + key
                if new_value > alphabet_length:
                    new_value = new_value % alphabet_length
                new_string.append(index_letter[new_value])
    return "".join(new_string)

# Sol 2
def caesar(string, key):
    new_string = []

    for letter in string:
        upated_letter = rotate_letter(letter, key)
        new_string.append(upated_letter)
    return "".join(new_string)

def rotate_letter(letter, key):
    min_ord_value = 96
    max_ord_value = 122

    new_ord_value = ord(letter) + key

    if new_ord_value <= max_ord_value:
        return chr(new_ord_value)

    difference = new_ord_value - max_ord_value
    new_ord_value = min_ord_value + difference % max_ord_value

    return chr(new_ord_value)


from todo_app.data.view_model import ViewModel
from todo_app.data.item import Item
import pytest

def test_todo_items():
    # Setup -> add a second item not part of To do
    new_item = Item(1, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "To Do")
    list = [new_item]
    item_view_model = ViewModel(list)
    #Act
    todo_items = item_view_model.todo_items
    #Assert
    assert 1 == len(todo_items)
    assert todo_items[0].status == "To Do"

def test_doing_items():
    # Setup -> add a second item not part of To do
    list = []
    list.append(Item(1, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "To Do"))
    list.append(Item(2, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "Doing"))
    item_view_model = ViewModel(list)
    #Act
    doing_items = item_view_model.doing_items
    #Assert
    assert 1 == len(doing_items)
    assert doing_items[0].status == "Doing"

def test_done_items():
    # Setup
    list = []
    list.append(Item(1, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "To Do"))
    list.append(Item(2, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "Doing"))
    list.append(Item(3, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "Done"))
    list.append(Item(4, "Testing", "A card used for testing", "03/15/2022", "03/15/2022", "Done"))
    item_view_model = ViewModel(list)
    #Act
    done_items = item_view_model.done_items
    #Assert
    assert 2 == len(done_items)
    assert done_items[0].status == "Done"


# -*- coding: utf-8 -*-
#!/usr/bin/env python3
"""
Created on Thu Feb 16 20:55:19 2017
@Mail: minnglee@163.com
@Author: Ming Li
"""

###用于非标准VCF文件

import sys,os,logging,click

logging.basicConfig(filename='{0}.log'.format(os.path.basename(__file__).replace('.py','')),
                    format='%(asctime)s: %(name)s: %(levelname)s: %(message)s',level=logging.DEBUG,filemode='w')
logging.info(f"The command line is:\n\tpython3 {' '.join(sys.argv)}")

def LoadReadsCountCount(File):
    '''
    CHR     Pos     Ref     Alt
    1       87      1198    1091
    1       88      1216    1095
    '''
    Dict = {}
    ChrSet = set()
    next(File)
    for line in File:
        line = line.strip().split()
        if int(line[-1]) > int(line[-2]): line[-2],line[-1] = line[-1],line[-2]
        Dict.setdefault(line[0],[]).append([int(line[1]),int(line[-2]),int(line[-1])])
        ChrSet.add(line[0])
    return Dict,ChrSet
def SlideWindow(start,end,step,Index,InList):
    SubList = []
    for i in range(Index,len(InList)):
        if InList[i][0] <= start + step:
            SubList.append(InList[i])
            Index = i+1
        elif InList[i][0] <= end: SubList.append(InList[i])
        else: return SubList,Index,True
    return SubList,Index,False
def CalculateWindow(List):
    MaxAllele,MinAllele = 0,0
    SnpNum = len(List)
    if SnpNum == 0: return 0,0
    else:
        for i in List:
            MaxAllele += i[1]
            MinAllele += i[2]
        if MaxAllele == 0 : return 0,0
        else : Hp = 2*MaxAllele*MinAllele/(MaxAllele+MinAllele)**2
        return SnpNum,Hp
@click.command()
@click.option('-i','--input',help='Input file',type=click.File('r'),required=True)
@click.option('-w','--window',help='Slide Window Size',type=int,default=50000)
@click.option('-s','--step',help='Step Size',type=int,default=10000)
@click.option('-o','--output',help='Output file',type=click.File('w'),required=True)
def main(input,window,step,output):
    output.write('CHR\tSTART\tEND\tSnpNum\tHp\n')
    ReadsCountDict,ChrSet = LoadReadsCountCount(input)
    for CHR in ChrSet:
        start,end = 0,window
        Index = 0
        NotEnd = True
        while NotEnd :
            WinSnpList,Index,NotEnd = SlideWindow(start,end,step,Index,ReadsCountDict[CHR])
            SnpNum,Hp = CalculateWindow(WinSnpList)
            output.write(f'{CHR}\t{start}\t{end}\t{SnpNum}\t{Hp}\n')
            start += step
            end += step
if __name__ == '__main__':
    main()


import pandas as pd

names1880 = pd.read_csv('~/git-repos/baby-names/data/names/yob1880.txt')

years = range(1880, 2019)

pieces = []
columns = ['name', 'sex', 'births']

for year in years:
    path = '~/git-repos/baby-names/data/names/yob%d.txt' % year
    #print(path)
    frame = pd.read_csv(path, names=columns)

    frame['year'] = year
    pieces.append(frame)
    names = pd.concat(pieces, ignore_index=True)

total_births = names.pivot_table('births', index='year', columns='sex', aggfunc=sum)
total_births


import xlrd
import requests
import json
import gensim
import os
import logging
import urllib3
requests.packages.urllib3.disable_warnings()
from gensim.summarization import summarize
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

loc = ("C://Users/nandimar/Desktop/Go/R/groups_clouds.xlsx")

wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)

#payload = ""
payload = {
	"group":{
		"name": sheet.cell_value(0, 1),
		"description": sheet.cell_value(1, 1),
		"location": sheet.cell_value(2, 1)
		}
	}
mypayloadd = json.dumps(payload)
print(mypayloadd)
resp = requests.post('https://uis.cmp.unisys-waf.com/api/groups', headers={'Authorization': 'BEARER ' + '7b00caab-9bfc-48f8-8126-7df5bd350b9b'},data = mypayloadd,verify = False)
if resp.status_code == 200:
	print('Azure : ' + sheet.cell_value(0, 1) + ' ' + 'group' +' - is Successfully Created')
else:
	print('Azure : ' + sheet.cell_value(0, 1) + ' ' + 'group' + '  - is not Successfully Created due to json response issue')
print(resp.text)


import ufora.FORA.test.ValidatePerf as ValidatePerf
import ufora.core.SubprocessRunner as SubprocessRunner
import ufora.test.PerformanceTestReporter as PerformanceTestReporter
from ufora.FORA.python.ForaValue import FORATuple
from ufora.FORA.python.ForaValue import FORAValue

import time
import sys
import logging

_baseTiming = None

Symbol_callResult = FORAValue.makeSymbol("callResult")

def computeBaseTiming():
    times = []

    for ix in range(101):
        t0 = time.time()
        x = 0
        while x < 1000000:
            x=x+1
        times.append(time.time() - t0)

    import logging
    logging.info("base timing: " + str(min(times)))
    return min(times)

class PerfTestBase(object):
    @property
    def baseTiming(self):
        global _baseTiming
        if _baseTiming is None:
            _baseTiming = computeBaseTiming()
        return _baseTiming

    def validateTimingsForSubprocessCall(
                self,
                testName,
                subprocessArgs,
                meta,
                timeout = 600.0
                ):
        resultCode, out, err = SubprocessRunner.callAndReturnResultAndOutput(
            subprocessArgs,
            timeout = timeout
            )

        if resultCode != 0:
            meta.update({"failure": "subprocess call returned error"})

            if PerformanceTestReporter.isCurrentlyTesting():
                PerformanceTestReporter.recordTest(
                    testName,
                    None,
                    meta
                    )

        assert resultCode == 0, err

        logging.info("Actual time was %s for %s", out[0], subprocessArgs)

        measuredTiming = float(out[0]) / self.baseTiming

        if PerformanceTestReporter.isCurrentlyTesting():
            PerformanceTestReporter.recordTest(
                "fora_lang." + testName,
                float(out[0]),
                meta
                )

    def shouldCallResult(self, metadataForMember):
        if isinstance(metadataForMember, tuple) or \
           isinstance(metadataForMember, FORATuple):
            if Symbol_callResult in metadataForMember:
                return True

        return False

    def validatePerfForModuleMember(self, testName, moduleFileName, testCaseMemberName,
                                    metadataForMember):
        subprocessArgs = [sys.executable, ValidatePerf.__file__, moduleFileName, \
                              testCaseMemberName, "moduleMember"]

        if self.shouldCallResult(metadataForMember):
            subprocessArgs.append("callResult")

        self.validateTimingsForSubprocessCall(
            testName,
            subprocessArgs,
            {'file': "ufora/FORA/test/" + moduleFileName}
            )

    def validatePerfForExpression(self, testName, foraExpr, callResult = False):
        subprocessArgs = \
            [sys.executable, ValidatePerf.__file__, foraExpr];

        if callResult:
            subprocessArgs.append("callResult")

        self.validateTimingsForSubprocessCall(
            testName,
            subprocessArgs,
            PerformanceTestReporter.getCurrentStackframeFileAndLine(framesAbove=2)
            )