import math
from tkinter import Button, Canvas, Entry, Frame, Label, Menu, Text, Tk,filedialog
from tkinter.constants import END
from turtle import distance
from CloudModel import plot_2d_cloud_model, plot_cloud_model
from IDW import IDWdraw3dpoints,IDWdraw3dsurface,GetDEMAllData,Drawgrid2dDEM,DrawgridSlope,Drawgrid2dDEMAspectOfSlope
from Drawkoch import DrawKoch
from Kmenas import ShowKmeans
from graph import MSTdata,MSTkruskal,MSTprim
# from TIN import ShowTIN

win_width = 1000
win_height = 700
bgcolor = 'white'

class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        ################################################
        self.unbindkey = 1# 判断是否解绑左键
        self.pointNum = 0# 所画点图形的个数
        self.shapeNum = 0# 所画多边形的个数
        self.lineNum = 0# 所画直线的个数
        self.ovalNum = 0# 所画椭圆的个数
        ################################################
        self.Points_point = []# 点图形点坐标
        self.Points_x_point = []# 点图形点坐标x值
        self.Points_y_point = []# 点图形点坐标y值
        self.Points_shape = []# 多边形点坐标
        self.Points_x_shape = []# 多边形点坐标x值
        self.Points_y_shape = []# 多边形点坐标y值
        self.Points_line = []# 直线点坐标
        self.Points_x_line = []# 直线点坐标x值
        self.Points_y_line = []# 直线点坐标y值
        self.Points_oval = []# 椭圆点坐标
        self.Points_x_oval = []# 椭圆点坐标x值
        self.Points_y_oval = []# 椭圆点坐标y值
        ################################################
        self.p_point = []# 点图形临时坐标值列表
        self.p_x_point = []# 点图形临时坐标x值列表
        self.p_y_point = []# 点图形临时坐标y值列表
        self.p_shape = []# 多边形图形临时坐标值列表
        self.p_x_shape = []# 多边形图形临时坐标x值列表
        self.p_y_shape = []# 多边形图形临时坐标y值列表
        self.p_line = []# 直线图形临时坐标值列表
        self.p_x_line = []# 直线多边形图形临时坐标x值列表
        self.p_y_line = []# 直线多边形图形临时坐标y值列表
        self.p_oval = []# 椭圆图形临时坐标值列表
        self.p_x_oval = []# 椭圆图形临时坐标x值列表
        self.p_y_oval = []# 椭圆图形临时坐标y值列表
        ################################################
        self.kochLength = 0#koch曲线长度
        self.kochNum = 0#koch曲线迭代次数
        ################################################
        self.Points_x_kmeans = []#K均值聚类的点数据x值
        self.Points_y_kmeans = []#K均值聚类的点数据y值
        ################################################
        # 图点数据
        self.TreeData = [[331,312],[422,422],[562,448],[630,360],[588,250],[436,205]]
        ################################################
        self.fgcolor = 'black'
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建画图区域
        self.drawpad = Canvas(self, width=win_width, height=win_height, bg=bgcolor)
        self.drawpad.pack()
        # 创建顶部菜单
        self.menubar = Menu(root)
        self.filemenu_openfile = Menu(self.menubar,tearoff=False)
        self.filemenu_openfile.add_command(label="点数据",command=self.OpenPoints)
        self.filemenu_openfile.add_command(label="线数据",command=self.OpenLines)
        self.filemenu_openfile.add_command(label="面数据",command=self.OpenShapes)
        self.filemenu_openfile.add_separator()
        self.filemenu_draw = Menu(self.menubar,tearoff=False)
        self.filemenu_draw.add_command(label="点",command=self.drawPoint)
        self.filemenu_draw.add_command(label="任意多边形",command=self.drawShape)
        self.filemenu_draw.add_command(label="直线",command=self.drawLine)
        self.filemenu_draw.add_command(label="椭圆",command=self.drawOval)
        self.filemenu_draw.add_command(label="test",command=self.Test)
        self.filemenu_draw.add_separator()
        self.filemenu_cloud = Menu(self.menubar,tearoff=False)
        self.filemenu_cloud.add_command(label="二维云模型",command=self.plot_cloud)
        self.filemenu_cloud.add_command(label="三维云模型",command=self.plot_2d_cloud)
        self.filemenu_cloud.add_separator()
        self.filemenu_dem = Menu(self.menubar,tearoff=False)
        self.filemenu_dem.add_command(label="获取数据",command=GetDEMAllData)
        self.filemenu_dem.add_command(label="DEM已知点散点图",command=IDWdraw3dpoints)
        self.filemenu_dem.add_command(label="DEM插值点表面图",command=IDWdraw3dsurface)
        self.filemenu_dem.add_separator()
        self.filemenu_demclass = Menu(self.menubar,tearoff=False)
        self.filemenu_demclass.add_command(label="二维DEM",command=Drawgrid2dDEM)
        self.filemenu_demclass.add_command(label="坡度图",command=DrawgridSlope)
        self.filemenu_demclass.add_command(label="坡向图",command=Drawgrid2dDEMAspectOfSlope)
        self.filemenu_demclass.add_separator()
        self.filemenu_koch = Menu(self.menubar,tearoff=False)
        self.filemenu_koch.add_command(label="获取koch曲线参数",command=self.DrawkochData)
        self.filemenu_koch.add_command(label="绘制koch曲线",command=self.StartDrawKoch)
        self.filemenu_koch.add_separator()
        self.filemenu_spatialmeasure = Menu(self.menubar,tearoff=False)
        self.filemenu_spatialmeasure.add_command(label="点与点距离",command=self.DistancePointandPoint)
        self.filemenu_spatialmeasure.add_command(label="点与线距离",command=self.DistancePointandLine)
        self.filemenu_spatialmeasure.add_command(label="点与面距离",command=self.DistancePointandShape)
        self.filemenu_spatialmeasure.add_command(label="线与面距离",command=self.DistanceLineandShape)
        self.filemenu_spatialmeasure.add_command(label="面与面距离",command=self.DistanceShapeandShape)
        self.filemenu_spatialmeasure.add_separator()
        self.filemenu_kmeans = Menu(self.menubar,tearoff=False)
        self.filemenu_kmeans.add_command(label="K均值聚类",command=self.StartKmeans)
        self.filemenu_kmeans.add_separator()
        self.filemenu_buffer = Menu(self.menubar,tearoff=False)
        self.filemenu_buffer.add_command(label="点缓冲区",command=self.BufferPoints)
        self.filemenu_buffer.add_command(label="线缓冲区",command=self.BufferLines)
        self.filemenu_buffer.add_command(label="面缓冲区",command=self.BufferShapes)
        self.filemenu_buffer.add_separator()
        self.filemenu_mst = Menu(self.menubar,tearoff=False)
        self.filemenu_mst.add_command(label="加载有权无向图",command=self.GetMSTdata)
        self.filemenu_mst.add_command(label="kruskal算法",command=self.ShowMSTkruskal)
        self.filemenu_mst.add_command(label="prim算法",command=self.ShowMSTprim)
        self.filemenu_mst.add_separator()
        self.filemenu_tin = Menu(self.menubar,tearoff=False)
        # self.filemenu_tin.add_command(label="生成三角网",command=ShowTIN)
        self.filemenu_tin.add_separator()
        self.menubar.add_cascade(label="打开",menu=self.filemenu_openfile)
        self.menubar.add_cascade(label="图形",menu=self.filemenu_draw)
        self.menubar.add_cascade(label="清屏",command=self.Clear)
        self.menubar.add_cascade(label="云模型",menu=self.filemenu_cloud)
        self.menubar.add_cascade(label="DEM",menu=self.filemenu_dem)
        self.menubar.add_cascade(label="DEMClass",menu=self.filemenu_demclass)
        self.menubar.add_cascade(label="koch曲线",menu=self.filemenu_koch)
        self.menubar.add_cascade(label="空间实体量测",menu=self.filemenu_spatialmeasure)
        self.menubar.add_cascade(label="K均值聚类",menu=self.filemenu_kmeans)
        self.menubar.add_cascade(label="缓冲区",menu=self.filemenu_buffer)
        self.menubar.add_cascade(label="最小代价树",menu=self.filemenu_mst)
        self.menubar.add_cascade(label="TIN三角网",menu=self.filemenu_tin)
        root.config(menu=self.menubar)
        # 创建功能按钮
        self.btn_area = Button(self,text='面积',command=self.ShowShapeArea)
        self.btn_area.pack(side='left',padx=10)
        self.btn_length = Button(self,text='周长',command=self.ShowShapeLength)
        self.btn_length.pack(side='left',padx=10)
        self.btn_cg = Button(self,text='重心',command=self.ShowShapeCG)
        self.btn_cg.pack(side='left',padx=10)
        # 添加显示标签
        self.text_result = Text(self)
        self.text_result.pack(side='left',padx=10)
        # 添加输入标签
        self.entry_input1 = Entry(self)
        self.entry_input1.pack(padx=10, pady=5)
        self.entry_input1.insert(END,'koch曲线长度')
        self.entry_input2 = Entry(self)
        self.entry_input2.pack(padx=10, pady=5)
        self.entry_input2.insert(END,'koch迭代次数')
        self.entry_input3 = Entry(self)
        self.entry_input3.pack(padx=10, pady=5)
        self.entry_input3.insert(END,'缓冲区大小')

    # 清空面板和数据
    def Clear(self):
        self.drawpad.delete('all')
        ##############################
        self.Points_point.clear()
        self.Points_x_point.clear()
        self.Points_y_point.clear()
        self.Points_shape.clear()
        self.Points_x_shape.clear()
        self.Points_y_shape.clear()
        self.Points_line.clear()
        self.Points_x_line.clear()
        self.Points_y_line.clear()
        self.Points_oval.clear()
        self.Points_x_oval.clear()
        self.Points_y_oval.clear()
        self.Points_x_kmeans.clear()
        self.Points_y_kmeans.clear()
        ##############################
        self.unbindkey = 1
        self.pointNum = 0
        self.shapeNum = 0
        self.lineNum = 0
        self.ovalNum = 0
        ##############################
        self.p_point.clear()
        self.p_x_point.clear()
        self.p_y_point.clear()
        self.p_shape.clear()
        self.p_x_shape.clear()
        self.p_y_shape.clear()
        self.p_line.clear()
        self.p_x_line.clear()
        self.p_y_line.clear()
        self.p_oval.clear()
        self.p_x_oval.clear()
        self.p_y_oval.clear()

    # 打开点数据文件
    def OpenPoints(self):
        filename = filedialog.askopenfilename()
        with open(filename,'r',encoding='utf-8') as f:
            for each in f:
                self.pointNum += 1
                P = each.split(',')
                p = []
                for i in P:
                    p.append(int(i))#消除回车等字符影响
                self.Points_x_point.append(p[0])
                self.Points_y_point.append(p[1])
        for i in range(len(self.Points_x_point)):
            x1,y1 = int(self.Points_x_point[i])-3,int(self.Points_y_point[i])-3
            x2,y2 = int(self.Points_x_point[i])+3,int(self.Points_y_point[i])+3
            self.drawpad.create_oval(x1,y1,x2,y2,fill="black",outline="black")
        self.text_result.insert('insert',"点数据导入成功！\n")

    # 打开线数据文件
    def OpenLines(self):
        filename = filedialog.askopenfilename()
        with open(filename,'r',encoding='utf-8') as f:
            for each in f:
                self.lineNum += 1
                P = each.split(',')
                p = []
                for i in P:
                    p.append(int(i))#消除回车等字符影响
                self.Points_line.append(p)
                p_x= []
                p_y= []
                p_x.append(p[0])
                p_x.append(p[2])
                p_y.append(p[1])
                p_y.append(p[3])
                self.Points_x_line.append(p_x)
                self.Points_y_line.append(p_y)
                p_x= []
                p_y= []
        for line in self.Points_line:
            self.drawpad.create_line(line,fill="black")
            print(line)
        print(self.Points_x_line)
        self.text_result.insert('insert',"线数据导入成功！\n")

    # 打开面数据文件
    # TODO 无法计算重心bug
    def OpenShapes(self):
        filename = filedialog.askopenfilename()
        with open(filename,'r',encoding='utf-8') as f:
            for each in f:
                self.shapeNum += 1
                P = each.split(',')
                p = []
                for i in P:
                    p.append(int(i))#消除回车等字符影响
                self.Points_shape.append(p)
                p_x= []
                p_y= []
                for j in range(len(p)):
                    if(j%2==0):
                        p_x.append(p[j])
                    else:
                        p_y.append(p[j])
                self.Points_x_shape.append(p_x)
                self.Points_y_shape.append(p_y)
                p_x= []
                p_y= []
        for shape in self.Points_shape:
            self.drawpad.create_polygon(shape,fill="",outline="blue")
        print(self.shapeNum)
        self.text_result.insert('insert',"面数据导入成功！\n")

    # 绘制koch曲线的数据
    def DrawkochData(self):
        self.kochLength,self.kochNum = int(self.entry_input1.get()),int(self.entry_input2.get())
        print(self.kochLength)
        print(self.kochNum)
    # 开始绘制koch曲线
    def StartDrawKoch(self):
        DrawKoch(self.kochLength,self.kochNum)

    # 解除点图形左键绑定
    def PointUnbindLeft(self,event):
        if(self.unbindkey == 1):
            root.unbind("<Button-1>")
            self.unbindkey = 0
        else:
            root.bind("<Button-1>",self.PointGetPoints)# 恢复左键绑定
            self.unbindkey = 1
    # 解除多边形左键绑定
    def ShapeUnbindLeft(self,event):
        if(self.unbindkey == 1):
            root.unbind("<Button-1>")
            self.unbindkey = 0
        else:
            root.bind("<Button-1>",self.ShapeGetPoints)# 恢复左键绑定
            self.unbindkey = 1
    # 解除直线左键绑定
    def LineUnbindLeft(self,event):
        if(self.unbindkey == 1):
            root.unbind("<Button-1>")
            self.unbindkey = 0
        else:
            root.bind("<Button-1>",self.LineGetPoints)# 恢复左键绑定
            self.unbindkey = 1
    # 解除椭圆左键绑定
    def OvalUnbindLeft(self,event):
        if(self.unbindkey == 1):
            root.unbind("<Button-1>")
            self.unbindkey = 0
        else:
            root.bind("<Button-1>",self.OvalGetPoints)# 恢复左键绑定
            self.unbindkey = 1

    # 计算面积
    def GetShapeArea(self,points_x=[],points_y=[]):
        area = 0.0
        for i in range(len(points_x)):
            if i + 1 <= len(points_x) - 1:
                area += points_x[i] * points_y[i + 1] - points_x[i + 1] * points_y[i]
            elif i + 1 > len(points_x) - 1:
                area += points_x[i] * points_y[0] - points_x[0] * points_y[i]
        area = area/2.0
        return abs(area)
    # 显示面积
    def ShowShapeArea(self):
        for i in range(self.shapeNum):
            area = self.GetShapeArea(self.Points_x_shape[i],self.Points_y_shape[i])
            print("图形%d面积: %f" % (i+1,abs(area)))
            result = "图形" + str(i+1) + "面积：" + str(abs(area)) + '\n'
            self.text_result.insert('insert',result)

    # 计算周长
    def GetShapeLength(self,points_x=[],points_y=[]):
        length = 0
        for i in range(len(points_x)):
            if i<len(points_x):
                L = math.sqrt((points_x[i]-points_x[i-1])*(points_x[i]-points_x[i-1])+(points_y[i]-points_y[i-1])*(points_y[i]-points_y[i-1]))
                length += L
            elif i==len(points_x):
                L = math.sqrt((points_x[0]-points_x[i-1])*(points_x[0]-points_x[i-1])+(points_y[0]-points_y[i-1])*(points_y[0]-points_y[i-1]))
                length += L
        return abs(length)
    # 显示周长
    def ShowShapeLength(self):
        for i in range(self.shapeNum):
            length = self.GetShapeLength(self.Points_x_shape[i],self.Points_y_shape[i])
            print("图形%d周长: %f" % (i+1,abs(length)))
            result = "图形" + str(i+1) + "周长：" + str(abs(length)) + '\n'
            self.text_result.insert('insert',result)

    # 计算重心
    def GetShapeCG(self,points_x=[],points_y=[]):
        area = self.GetShapeArea(points_x,points_y)
        x, y = 0.0, 0.0 #重心点坐标
        for i in range(len(points_x)):
            lat = points_x[i]  # weidu
            lng = points_y[i]  # jingdu
            if i == 0:
                lat1 = points_x[-1]
                lng1 = points_y[-1]
            else:
                lat1 = points_x[i - 1]
                lng1 = points_y[i - 1]
            fg = (lat * lng1 - lng * lat1) / 2.0
            area += fg
            x += fg * (lat + lat1) / 3.0
            y += fg * (lng + lng1) / 3.0
        x = x / area   # x坐标
        y = y / area   # y坐标
        return x,y
    # 显示重心
    def ShowShapeCG(self):
        for i in range(self.shapeNum):
            x,y = self.GetShapeCG(self.Points_x_shape[i],self.Points_y_shape[i])
            print("图形%d重心：(%f,%f)"%(i+1,x,y))
            result = "图形" + str(i+1) + "重心：(" + str(x) + ',' + str(y) + ')\n'
            self.text_result.insert('insert',result)

    # 得到点图形鼠标左键坐标
    def PointGetPoints(self,event):
        self.p_point.append(event.x)
        self.p_point.append(event.y)
        self.p_x_point.append(event.x)
        self.p_y_point.append(event.y)
        self.pointNum += 1
        print(event.x,event.y)
    # 开始绘制点
    def StartDrawPoint(self,event):
        self.Points_point += self.p_point# 将临时坐标值列表存入总列表
        self.Points_x_point += self.p_x_point# 将临时坐标x值列表存入总列表
        self.Points_y_point += self.p_y_point# 将临时坐标y值列表存入总列表
        # x1,y1 = int(self.Points_x_point[self.pointNum-1][0])-1,int(self.Points_y_point[self.pointNum-1][0])-1
        # x2,y2 = int(self.Points_x_point[self.pointNum-1][0])+1,int(self.Points_y_point[self.pointNum-1][0])+1
        # self.drawpad.create_oval(x1,y1,x2,y2,outline="red")
        for i in range(self.pointNum):
            x1,y1 = int(self.Points_x_point[i])-3,int(self.Points_y_point[i])-3
            x2,y2 = int(self.Points_x_point[i])+3,int(self.Points_y_point[i])+3
            self.drawpad.create_oval(x1,y1,x2,y2,fill="red",outline="red")
        print(self.Points_x_point)
        print(self.Points_y_point)
        self.p_point = [] # 清空临时列表
        self.p_x_point = []# 清空临时列表
        self.p_y_point = []# 清空临时列表
    # 准备绘制点
    def drawPoint(self):
        root.bind("<Button-1>",self.PointGetPoints)
        root.bind("<Button-2>",self.PointUnbindLeft)
        root.bind("<Button-3>",self.StartDrawPoint)

    # 得到多边形鼠标左键坐标
    def ShapeGetPoints(self,event):
        self.p_shape.append(event.x)
        self.p_shape.append(event.y)
        self.p_x_shape.append(event.x)
        self.p_y_shape.append(event.y)
        print(event.x,event.y)
    # 开始绘制多边形
    def StartDrawShape(self,event):
        self.shapeNum += 1
        self.Points_shape.append(self.p_shape)# 将临时坐标值列表存入总列表
        self.Points_x_shape.append(self.p_x_shape)# 将临时坐标x值列表存入总列表
        self.Points_y_shape.append(self.p_y_shape)# 将临时坐标y值列表存入总列表
        # self.p.clear()# 清空临时列表(IndexError: tuple index out of range,不能放在前面，原因未知。现在清空，上一句函数的Points也会被清空。)
        self.drawpad.create_polygon(self.Points_shape[self.shapeNum-1],fill="",outline="black")
        print(self.shapeNum)
        # 不要使用clear()方法清空，会导致将总列表的数值也清空，原因可能是函数进程未结束时append(self.p)会与self.p一直关联。
        print(self.Points_shape)
        self.p_shape = [] # 清空临时列表
        self.p_x_shape = []# 清空临时列表
        self.p_y_shape = []# 清空临时列表
    # 准备绘制多边形
    def drawShape(self):
        root.bind("<Button-1>",self.ShapeGetPoints)
        root.bind("<Button-2>",self.ShapeUnbindLeft)
        root.bind("<Button-3>",self.StartDrawShape)

    # 得到直线鼠标左键坐标
    def LineGetPoints(self,event):
        self.p_line.append(event.x)
        self.p_line.append(event.y)
        self.p_x_line.append(event.x)
        self.p_y_line.append(event.y)
        print(event.x,event.y)
    # 开始绘制直线
    def StartDrawLine(self,event):
        self.lineNum += 1
        self.Points_line.append(self.p_line)# 将临时坐标值列表存入总列表
        self.Points_x_line.append(self.p_x_line)# 将临时坐标x值列表存入总列表
        self.Points_y_line.append(self.p_y_line)# 将临时坐标y值列表存入总列表
        self.drawpad.create_line(self.Points_line[self.lineNum-1],fill="red")
        self.p_line = [] # 清空临时列表
        self.p_x_line = []# 清空临时列表
        self.p_y_line = []# 清空临时列表
        print(self.Points_line)
    # 准备绘制直线
    def drawLine(self):
        root.bind("<Button-1>",self.LineGetPoints)
        root.bind("<Button-2>",self.LineUnbindLeft)
        root.bind("<Button-3>",self.StartDrawLine)

    # 得到椭圆鼠标坐标坐标
    def OvalGetPoints(self,event):
        self.p_oval.append(event.x)
        self.p_oval.append(event.y)
        self.p_x_oval.append(event.x)
        self.p_y_oval.append(event.y)
        print(event.x,event.y)
    # 开始绘制椭圆
    def StartDrawOval(self,event):
        self.ovalNum += 1
        self.Points_oval.append(self.p_oval)# 将临时坐标值列表存入总列表
        self.Points_x_oval.append(self.p_x_oval)# 将临时坐标x值列表存入总列表
        self.Points_y_oval.append(self.p_y_oval)# 将临时坐标y值列表存入总列表
        self.drawpad.create_oval(self.Points_oval[self.ovalNum-1],fill="",outline="blue")
        self.p_oval = [] # 清空临时列表
        self.p_x_oval = []# 清空临时列表
        self.p_y_oval = []# 清空临时列表
    # 准备绘制椭圆
    def drawOval(self):
        root.bind("<Button-1>",self.OvalGetPoints)
        root.bind("<Button-2>",self.OvalUnbindLeft)
        root.bind("<Button-3>",self.StartDrawOval)

    # 二维云模型
    def plot_cloud(self):
        plot_cloud_model(0, 1, 0.1, 500, moni=True)
    # 三维云模型
    def plot_2d_cloud(self):
        plot_2d_cloud_model([0, 1], [0.3, 0.3], [0.01, 0.05], 2000)

    # 点与点之间的距离
    def DistancePointandPoint(self):
        distance = math.sqrt((self.Points_x_point[0]-self.Points_x_point[1])**2 + (self.Points_y_point[0]-self.Points_y_point[1])**2)
        s = "点与点之间的距离：" + str(distance) + '\n'
        print(s)
        self.text_result.insert('insert',s)
    # 点与线之间的距离
    def DistancePointandLine(self):
        k,b = 0.0,0.0
        for each in self.Points_line:
            k = math.sqrt((each[0]-each[2])**2 + (each[1]-each[3])**2)#斜率
            b = each[1] - k*each[0]#常数b
        A,B,C = k,-1,b
        distance = (abs(A*self.Points_x_point[0]+B*self.Points_y_point[0]+C))/math.sqrt(A*A + B*B)
        s = "点与线之间的距离：" + str(distance) + '\n'
        print(s)
        self.text_result.insert('insert',s)
    # 点与面之间的距离
    def DistancePointandShape(self):
        x,y = self.GetShapeCG(self.Points_x_shape[0],self.Points_y_shape[0])#重心x,y坐标值
        distance = math.sqrt((self.Points_x_point[0]-x)**2 + (self.Points_y_point[0]-y)**2)
        s = "点与面之间的距离：" + str(distance) + '\n'
        print(s)
        self.text_result.insert('insert',s)
    # 线与面之间的距离
    def DistanceLineandShape(self):
        k,b = 0.0,0.0
        for each in self.Points_line:
            k = math.sqrt((each[0]-each[2])**2 + (each[1]-each[3])**2)#斜率
            b = each[1] - k*each[0]#常数b
        A,B,C = k,-1,b
        x,y = self.GetShapeCG(self.Points_x_shape[0],self.Points_y_shape[0])#重心x,y坐标值
        distance = (abs(A*x+B*y+C))/math.sqrt(A*A + B*B)
        s = "线与面之间的距离：" + str(distance) + '\n'
        print(s)
        self.text_result.insert('insert',s)
    # 面与面之间的距离
    def DistanceShapeandShape(self):
        x1,y1 = self.GetShapeCG(self.Points_x_shape[0],self.Points_y_shape[0])#重心x1,y1坐标值
        x2,y2 = self.GetShapeCG(self.Points_x_shape[1],self.Points_y_shape[1])#重心x2,y2坐标值
        distance = math.sqrt((x1-x2)**2 + (y1-y2)**2)
        s = "面与面之间的距离：" + str(distance) + '\n'
        print(s)
        self.text_result.insert('insert',s)

    # 开始k值聚类
    def StartKmeans(self):
        ShowKmeans()
        self.text_result.insert('insert','K值聚类计算完成！\n')

    # 得到直线斜率
    def GetLineSlope(self,line_x=[],line_y=[]):
        k = 0.0#斜率
        k = (int(line_y[1])-int(line_y[0]))/(int(line_x[1])-int(line_x[0]))
        return k
    # 点缓冲区
    def BufferPoints(self):
        size = int(self.entry_input3.get())#缓冲区大小
        for i in range(len(self.Points_x_point)):
            x1,y1 = int(self.Points_x_point[i])-size,int(self.Points_y_point[i])-size
            x2,y2 = int(self.Points_x_point[i])+size,int(self.Points_y_point[i])+size
            self.drawpad.create_oval(x1,y1,x2,y2,fill="",outline="blue")
        self.text_result.insert('insert',"点缓冲区建立成功！\n")
    # 线缓冲区
    def BufferLines(self):
        size = int(self.entry_input3.get())#缓冲区大小
        line_k = []#存放直线斜率
        buffer_line_up = []#缓冲区上边
        buffer_line_down = []#缓冲区下边
        for i in range(self.lineNum):
            line_k.append(self.GetLineSlope(self.Points_x_line[i],self.Points_y_line[i]))
        print(line_k)
        i = 0
        for k in line_k:
            if(k>0):
                x1 = self.Points_x_line[i][0]+size*math.sin(math.atan(k))
                y1 = self.Points_y_line[i][0]-size*math.cos(math.atan(k))
                x2 = self.Points_x_line[i][1]+size*math.sin(math.atan(k))
                y2 = self.Points_y_line[i][1]-size*math.cos(math.atan(k))
                buffer = [x1,y1,x2,y2]
                buffer_line_up.append(buffer)
            else:
                k = -k
                x1 = self.Points_x_line[i][0]-size*math.sin(math.atan(k))
                y1 = self.Points_y_line[i][0]-size*math.cos(math.atan(k))
                x2 = self.Points_x_line[i][1]-size*math.sin(math.atan(k))
                y2 = self.Points_y_line[i][1]-size*math.cos(math.atan(k))
                buffer = [x1,y1,x2,y2]
                buffer_line_up.append(buffer)
            i += 1
        i = 0
        for k in line_k:
            if(k>0):
                x3 = self.Points_x_line[i][0]-size*math.sin(math.atan(k))
                y3 = self.Points_y_line[i][0]+size*math.cos(math.atan(k))
                x4 = self.Points_x_line[i][1]-size*math.sin(math.atan(k))
                y4 = self.Points_y_line[i][1]+size*math.cos(math.atan(k))
                buffer = [x4,y4,x3,y3]
                buffer_line_down.append(buffer)
            else:
                k = -k
                x3 = self.Points_x_line[i][0]+size*math.sin(math.atan(k))
                y3 = self.Points_y_line[i][0]+size*math.cos(math.atan(k))
                x4 = self.Points_x_line[i][1]+size*math.sin(math.atan(k))
                y4 = self.Points_y_line[i][1]+size*math.cos(math.atan(k))
                buffer = [x4,y4,x3,y3]
                buffer_line_down.append(buffer)
            i += 1
        print(buffer_line_up)
        print(buffer_line_down)
        for i in range(len(buffer_line_up)):
            points = buffer_line_up[i] + buffer_line_down[i]
            self.drawpad.create_polygon(points,fill="",outline="blue")
        self.text_result.insert('insert','线缓冲区建立成功！\n')
    # 面缓冲区
    def BufferShapes(self):
        size = int(self.entry_input3.get())#缓冲区大小
        x1,x4 = 200-size,200-size
        x2,x3 = 500+size,500+size
        y1,y2 = 200-size,200-size
        y3,y4 = 500+size,500+size
        p = [x1,y1,x2,y2,x3,y3,x4,y4]
        self.drawpad.create_polygon(p,fill="",outline="red")
        self.text_result.insert('insert','面缓冲区建立成功！\n')
        # for i in range(len(self.Points_x_shape)):

    # 得到有权无向图初始数据
    def GetMSTdata(self):
        self.Clear()
        s = ""
        data = []
        s,data = MSTdata()
        result = []#存放相邻点
        size = len(data)#树节点个数
        print(data)
        for i in range(size):
            for j in range(len(data[i])):
                k = data[i][j]
                if(k!=0 and k!=9999):
                    t = [i,j]
                    result.append(t)
        print(result)
        for i in range(len(result)):
            line = []
            line += self.TreeData[result[i][0]]
            line += self.TreeData[result[i][1]]
            print(line)
            self.drawpad.create_line(line,fill="blue")
        self.text_result.insert('insert',"有权无向图绘制完成！\n")
        self.text_result.insert('insert',s)
    # MSTkruskal算法
    def ShowMSTkruskal(self):
        self.Clear()
        kruskal = []
        kruskal = MSTkruskal()
        result = []#存放相邻点
        print(kruskal)
        for each in kruskal:
            result.append(each[0:2])
        print(result)
        for i in range(len(result)):
            line = []
            line += self.TreeData[result[i][0]]
            line += self.TreeData[result[i][1]]
            print(line)
            self.drawpad.create_line(line,fill="blue")
        self.text_result.insert('insert',"kruskal算法MST绘制完成！\n")
    # MSTprim算法
    def ShowMSTprim(self):
        self.Clear()
        prim = []
        prim = MSTprim()
        result = []#存放相邻点
        print(prim)
        for each in prim:
            result.append(each[0:2])
        print(result)
        for i in range(len(result)):
            line = []
            line += self.TreeData[result[i][0]]
            line += self.TreeData[result[i][1]]
            print(line)
            self.drawpad.create_line(line,fill="blue")
        self.text_result.insert('insert',"prims算法MST绘制完成！\n")

    # 测试
    def Test(self):
        print(self.text_input.get("0.0","end"))

if __name__ == '__main__':
    root = Tk()
    root.title('地理空间分析')
    root.geometry('1000x800+200+200')# 底部按钮栏
    app = Application(master=root)
    root.mainloop()


import sys
from PyQt5 import QtCore, QtGui, uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
win1 = uic.loadUiType("miles_calc.ui")[0]

class WindowClass(QtWidgets.QMainWindow, win1): ## inhertiene
    def __init__(self,parent=None):
        QtWidgets.QMainWindow.__init__(self, parent) ## constructor for the super model
        self.setupUi(self);self.txtInput.setVisible(True);
        self.RdoMiles.setChecked(True);self.RdoKm.setChecked(False)
        self.BtnCalculate.clicked.connect(self.calculation)
    # def validate(value):

    def calculation(self):
        strvalue=self.txtInput.text(); output=""
        fltvalue=float(strvalue)
        if self.RdoMiles.isChecked()==True:
            result=fltvalue/1.61;output="The value in miles ";
        elif self.RdoKm.isChecked()==True:
            result=fltvalue*1.61;output="the value in km is "
        result=round(result,2)
        self.lblOutput.setText(output+str(result))
        self.txtInput.setText(" ")
app = QtWidgets.QApplication(sys.argv)
myWindow = WindowClass(None) ## set the balue of our First window
myWindow.show() ## makes it show
app.exec_()


# _*_ coding: utf-8 _*_
# Copy 2018 the HERA Project
# Licensed under the 2-clause BSD license

"""Feature module:  SpectrumPeak
This module defines the format of the SpectrumPeak feature_sets.
"""

from __future__ import print_function, absolute_import, division
import os
import copy
import six
import numpy as np
import matplotlib.pyplot as plt
from .. import rids
# from . import peaks  # Scipy option
from . import peak_det  # Another option that seems to do better.
from . import bw_finder

class Spectral:
    """
    Generic spectral class, with the 'basics' initialized.  The other fields may get added
    as appropriate.
    For now, the order matters for 'spectral_fields' since used to order peak-finding priority
    """
    spectral_fields = ['maxhold', 'minhold', 'val', 'comment', 'polarization', 'freq', 'bw']

    def __init__(self, polarization='', comment=''):
        self.comment = comment
        self.polarization = polarization
        self.freq = []

class FileSet:
    """
    Class used to generate the feature_set function group by id
    and act on it (chunk it, etc)
    """

    def __init__(self, id):
        self.id = id
        self.included_feature_components = set()
        # Each of these below is per polarization
        self.timestamps = {}
        # ...for clarity, manually set feature_components from subset of spectral_fields above
        self.maxhold = {}
        self.minhold = {}
        self.val = {}

    def chunk_it(self, chunk_set):
        self.chunked = {}
        self.biggest_pol = {'pol': '-', 'len': -1, 'size': -1}
        for pol in self.timestamps:
            self.chunked[pol] = []
            _chnk = []
            _cize = 0
            for i, ts in enumerate(sorted(self.timestamps[pol])):
                if i and not i % chunk_set:
                    self.chunked[pol].append(_chnk)
                    _chnk = []
                _chnk.append(ts)
                _cize += 1
            self.chunked[pol].append(_chnk)
            if _cize > self.biggest_pol['size']:
                self.biggest_pol['pol'] = pol
                self.biggest_pol['len'] = len(self.chunked[pol])
                self.biggest_pol['size'] = _cize
        self.chunked_time_limits = [[] for x in range(self.biggest_pol['len'])]
        for p, ts_chunked in six.iteritems(self.chunked):
            for i, ts_list in enumerate(ts_chunked):
                self.chunked_time_limits[i].extend((ts_list[0], ts_list[-1]))
        for i in range(len(self.chunked_time_limits)):
            x = sorted(self.chunked_time_limits[i])
            self.chunked_time_limits[i] = [x[0], x[-1]]

def is_spectrum(tag, prefixes=('data', 'cal', 'baseline')):
    """
    This defines prefix "tags" that denote "spectra" - i.e. meant to be complete spectra
    (so plot with lines) and not a subset of features (e.g. peaks in this case, plot with points).
    """
    t = tag.lower()
    return t.startswith(prefixes)

class SpectrumPeak(rids.Rids):
    """
    Feature module:
    This feature module searches a directory for spectrum files and saves feature sets based
    peak finding (or saves rawdata).

    The feature_module_name is "SpectrumPeak"

    Direct attributes are (on top of base rids attributes):
        feature_module_name:  SpectrumPeak
        comment:  generic comments
        peaked_on:  event_component on which peaks were found
        delta:  delta value used in the peak-finding routine
        bw_range:  +/- bw_range (list) that gets searched over (and is then max)
        val_unit:  unit for data
        freq_unit:  (even though a _unit, handled differently than unit attribute)
        ... these are derived from data
        freq: can be used for is_spectrum feature_sets if desired
        fmin: minimum frequency
        fmax: maximum frequency
    Unit attributes are:
        threshold:  value used to threshold peaks
        threshold_unit:
        vbw:  if spectrum analyzer, video bandwidth
        vbw_unit:
        rbw:  if spectrum analyzer, resolution bandwidth (probably channel_width)
        rbw_unit:
    """
    sp__direct_attributes = ['peaked_on', 'delta', 'bw_range', 'feature_module_name',
                             'freq', 'freq_unit', 'fmin', 'fmax', 'val_unit']
    sp__unit_attributes = ['threshold', 'rbw', 'vbw']
    sp__polarizations = ['E', 'N', 'I', 'Q', 'U', 'V', 'H', 'X', 'Y', 'unk', 'none']

    def __init__(self, comment='', share_freq=False, view_ongoing=False):
        # Initialize base attributes
        super(SpectrumPeak, self).__init__(comment)

        # Initialize feature_module attributes
        for d in self.sp__direct_attributes:
            setattr(self, d, None)
        for d in self.sp__unit_attributes:
            setattr(self, d, None)
            setattr(self, d + '_unit', None)

        # Re-initialize attributes
        self.feature_module_name = 'SpectrumPeak'
        self.delta = 1.0  # Fairly random default value
        self.bw_range = [[-10.0, 10.0]]  # "
        self.feature_sets = {}
        # Other attributes
        feature_set_info = Spectral()
        self.feature_components = feature_set_info.spectral_fields
        self.hipk = None
        self.hipk_bw = None
        self.view_ongoing_features = view_ongoing
        self.share_freq = share_freq
        self.cal_files_present = False
        self.all_lowercase_polarization_names = [x.lower() for x in self.sp__polarizations]

    # Redefine the reader/writer/info base modules
    def reader(self, filename, reset=True):
        print("Reading {}".format(filename))
        if reset:
            self.reset()
        self._reader(filename, feature_direct=self.sp__direct_attributes,
                     feature_unit=self.sp__unit_attributes)
        # now need to fill in the share_freq feature values, if any
        for ftr in self.feature_sets:
            if self.feature_sets[ftr].freq == '@':
                self.feature_sets[ftr].freq = self.freq

    def writer(self, filename, fix_list=True):
        self._writer(filename, feature_direct=self.sp__direct_attributes,
                     feature_unit=self.sp__unit_attributes, fix_list=fix_list)

    def info(self):
        self._info(feature_direct=self.sp__direct_attributes,
                   feature_unit=self.sp__unit_attributes)

    def reset(self):
        self.__init__(share_freq=self.share_freq, view_ongoing=self.view_ongoing_features)

    def read_feature_set_dict(self, fs):
        """
        This is used in RIDS parent class for base reader.
        """
        feature_set = Spectral()
        for v, Y in six.iteritems(fs):
            if v not in self.feature_components:
                print("Unexpected field {}".format(v))
                continue
            setattr(feature_set, v, Y)
        return feature_set

    def get_feature_set_from_files(self, fset_tag, polarization, peak_on=None, **fnargs):
        """
        Get the feature sets for current iteration from individual spectrum files
        with naming convention as realized in module '_peel_filename' below
        **fnargs are filenames for self.feature_components {"feature_component": <filename>}
        """
        fset_name = fset_tag + polarization
        self.feature_sets[fset_name] = Spectral(polarization=polarization)
        self.feature_sets[fset_name].freq = []
        if len(fnargs) > 1:
            check_timestamps_for_match = _check_timestamps_for_match(**fnargs)
            if len(check_timestamps_for_match):
                self.feature_sets[fset_name].comment += check_timestamps_for_match
        if self.show_progress:
            print("Processing {}".format(fset_name))
        spectra = {}
        for fc, sfn in six.iteritems(fnargs):
            if fc not in self.feature_components or sfn is None:
                continue
            spectra[fc] = Spectral()
            if self.show_progress:
                print("\t{}".format(sfn))
            _spectrum_reader(sfn, spectra[fc], polarization)
            ftr_fmin = min(spectra[fc].freq)
            ftr_fmax = max(spectra[fc].freq)
            if ftr_fmin < self.fmin:
                self.fmin = ftr_fmin
            if ftr_fmax > self.fmax:
                self.fmax = ftr_fmax
            if is_spectrum(fset_tag):
                if self.share_freq:
                    self.feature_sets[fset_name].freq = '@'
                    if self.freq is None:
                        self.freq = copy.copy(spectra[fc].freq)
                elif len(spectra[fc].freq) > len(self.feature_sets[fset_name].freq):
                    self.feature_sets[fset_name].freq = spectra[fc].freq
                setattr(self.feature_sets[fset_name], fc, spectra[fc].val)
            if len(spectra[fc].comment):
                self.feature_sets[fset_name].comment += spectra[fc].comment
        self.nsets += 1
        if is_spectrum(fset_tag):
            return

        # Get the feature_component to use and find peaks/bw
        if peak_on is not None:
            fc = peak_on
        else:
            for fc in self.feature_components:
                if fc in fnargs.keys() and fnargs[fc] is not None:
                    break
            else:
                return
        if self.peaked_on is None:
            self.peaked_on = fc
        elif fc != self.peaked_on:
            spectra[fc].comment += 'Peaked on different component: {} rather than {}'.format(fc, self.peaked_on)
        # self.peak_finder(spectra[fc])  # old version not used anymore
        self.peak_det(spectra[fc], delta=self.delta)
        self.find_bw()

        # put values in feature set dictionary
        self.feature_sets[fset_name].freq = list(np.array(self.hipk_freq)[self.hipk])
        if len(spectra[fc].comment):
            self.feature_sets[fset_name].comment += spectra[fc].comment
        for fc, sfn in six.iteritems(fnargs):
            if sfn is None:
                continue
            try:
                setattr(self.feature_sets[fset_name], fc, list(np.array(spectra[fc].val)[self.hipk]))
            except IndexError:
                pass
        self.feature_sets[fset_name].bw = self.hipk_bw

    def peak_det(self, spec, delta=0.1):
        self.hipk_freq = spec.freq
        self.hipk_val = spec.val
        self.hipk = peak_det.peakdet(spec.val, delta=delta, threshold=self.threshold)
        self.hipk = list(self.hipk)

    def find_bw(self):
        self.hipk_bw = bw_finder.bw_finder(self.hipk_freq, self.hipk_val, self.hipk, self.bw_range)
        if self.view_ongoing_features:
            self.peak_viewer()

    def peak_finder(self, spec, cwt_range=[1, 3], rc_range=[4, 4]):
        """
        Deprecated peak_finder
        """
        self.hipk_freq = spec.freq
        self.hipk_val = spec.val
        self.hipk = peaks.fp(spec.val, self.threshold, cwt_range, rc_range)

    def peak_viewer(self):
        if self.hipk is None:
            return
        plt.plot(self.hipk_freq, self.hipk_val)
        plt.plot(np.array(self.hipk_freq)[self.hipk], np.array(self.hipk_val)[self.hipk], 'kv')
        if self.hipk_bw is not None:
            vv = np.array(self.hipk_val)[self.hipk]
            if 'dB' in self.val_unit:
                vv2 = vv - 6.0
            else:
                vv2 = vv / 4.0
            fl = np.array(self.hipk_freq)[self.hipk] + np.array(self.hipk_bw)[:, 0]
            fr = np.array(self.hipk_freq)[self.hipk] + np.array(self.hipk_bw)[:, 1]
            plt.plot([fl, fl], [vv, vv2], 'm')
            plt.plot([fr, fr], [vv, vv2], 'c')
        plt.show()

    def viewer(self, threshold=None, show_components='all', show_data=True):
        """
        Parameters:
        ------------
        show_components:  event_components to show (list) or 'all'
        show_data:  include data spectra (Boolean)
        """

        import collections
        if threshold is not None and threshold < self.threshold:
            print("Below threshold - using {}".format(self.threshold))
            threshold = None
        if threshold is not None:
            print("Threshold view not implemented yet.")
            threshold = None
        fc_list = collections.OrderedDict([('maxhold', ('r', 'v')), ('minhold', ('b', '^')), ('val', ('k', '_'))])
        if isinstance(show_components, (str, unicode)):
            show_components = fc_list.keys()
        color_list = ['r', 'b', 'k', 'g', 'm', 'c', 'y', '0.25', '0.5', '0.75']
        line_list = ['-', '--', ':']
        c = 0
        bl = 0
        for f, v in six.iteritems(self.feature_sets):
            issp = is_spectrum(f)
            if issp and not show_data:
                continue
            use_freq = v.freq
            if issp:
                clr = [fc_list[x][0] for x in show_components]
                ls = [line_list[bl % len(line_list)]] * len(show_components)
                fmt = zip(clr, ls)
                bl += 1
                if use_freq == '@':
                    use_freq = self.freq
            else:
                clr = [color_list[c % len(color_list)]] * len(show_components)
                mkr = [fc_list[x][1] for x in fc_list]
                fmt = zip(clr, mkr)
                c += 1
            for fc in show_components:
                try:
                    i = show_components.index(fc)
                    _spectrum_plotter(use_freq, getattr(v, fc), fmt=fmt[i], is_spectrum=issp, figure_name=self.rid_file)
                except AttributeError:
                    pass
            if issp:
                continue
            # Now plot bandwidth
            try:
                vv = np.array(getattr(v, self.peaked_on))
                fv = np.array(use_freq)
                bw = np.array(v.bw)
                if 'dB' in self.val_unit:
                    vv2 = vv - 6.0
                else:
                    vv2 = vv / 4.0
                fl = fv + bw[:, 0]
                if self.fmin is not None:
                    ifl = np.where(fl < self.fmin)
                    fl[ifl] = self.fmin
                fr = fv + bw[:, 1]
                if self.fmax is not None:
                    ifr = np.where(fr > self.fmax)
                    fr[ifr] = self.fmax
                for j in range(len(fl)):
                    plt.plot([fl[j], fl[j]], [vv[j], vv2[j]], clr[0])
                    plt.plot([fr[j], fr[j]], [vv[j], vv2[j]], clr[0])
            except AttributeError:
                continue
        plt.show()

    def read_cal(self, filename, polarization):
        tag = 'cal.' + filename + '.'
        self.cal_files_present = True
        self.get_feature_set_from_files(tag, polarization, val=filename)

    def apply_cal(self):
        print("NOT IMPLEMENTED YET: Apply the calibration, if available.")

    def process_files(self, directory='.', ident='all', data=[0, -1], peak_on=None,
                      data_only=False, sets_per_pol=10000, keep_data=False, show_progress=False):
        """
        This is the standard method to process spectrum files in a directory to
        produce ridz files.  The module has an "outer loop" that is meant to handle
        ongoing file-writing along with file reading.  It will quit when it has
        run out of appropriate files

        Format of the spectrum filename (_peel_filename below):
        identifier.{time-stamp}.feature_component.polarization

        Parameters:
        ------------
        directory:  directory where files reside and ridz files get written
        ident:  idents to do.  If 'all' processes all
        data:  indices of events to be saved as data spectra
        peak_on:  feature_component on which to find peaks, default order if None
        data_only:  flag if only saving data and not peaks
        sets_per_pol:  number of feature_sets per pol per ridz file (i.e. number of timestamps/pol/file)
        keep_data:  if False (default) it will delete the processed files, otherwise it will keep
        """
        self.show_progress = show_progress
        # Get overall meta-data for filename
        th_for_fn = ''
        if self.threshold is not None:
            th_for_fn = self.threshold
            if abs(self.threshold) < 1.0:
                th_for_fn *= 100.0
            th_for_fn = 'T{:.0f}'.format(th_for_fn)
        if self.peaked_on is not None:
            pk_for_fn = self.peaked_on[:3]
        else:
            pk_for_fn = 'None'
            th_for_fn = ''

        # Get file_idp FileSet of files to do
        file_idp = {}  # keyed set of classes for file-sets
        for af in sorted(os.listdir(directory)):
            if 'rid' in af.split('.')[-1] or af[0] == '.':
                continue
            fnd = _peel_filename(af, self.feature_components)
            if not len(fnd) or\
                    fnd['polarization'].lower() not in self.all_lowercase_polarization_names or\
                    fnd['feature_component'] not in self.feature_components:
                continue
            use_it = False
            if isinstance(ident, six.string_types):
                if ident == 'all' or fnd['ident'] == ident:
                    use_it = True
            elif isinstance(ident, list) and fnd['ident'] in ident:
                use_it = True
            if use_it:
                idkey = fnd['ident']
                pol = fnd['polarization']
                feco = fnd['feature_component']
                if idkey not in file_idp:
                    file_idp[idkey] = FileSet(fnd['ident'])
                file_idp[idkey].included_feature_components.add(feco)
                if pol not in file_idp[idkey].timestamps:
                    file_idp[idkey].timestamps[pol] = set()
                file_idp[idkey].timestamps[pol].add(fnd['timestamp'])
                if pol not in getattr(file_idp[idkey], feco):
                    getattr(file_idp[idkey], feco)[pol] = {}
                getattr(file_idp[idkey], feco)[pol][fnd['timestamp']] = af
        # Now process that dictionary
        for idkey in file_idp:
            #  This is the start of one id
            file_idp[idkey].chunk_it(sets_per_pol)
            bp = file_idp[idkey].biggest_pol

            for i in range(bp['len']):
                # This is start of one file
                self.fmin = 1E9
                self.fmax = -1E9
                self.timestamp_first, self.timestamp_last = file_idp[idkey].chunked_time_limits[i][0], file_idp[idkey].chunked_time_limits[i][-1]
                if self.cal_files_present:
                    print("NOT IMPLEMENTED.  Need to rewrite the cal files.")
                self.feature_sets = {}  # Reset the features sets for new file.
                self.nsets = 0
                files_this_pass = set()
                for pol in file_idp[idkey].chunked:
                    try:
                        chunk_ts_list = file_idp[idkey].chunked[pol][i]
                    except IndexError:
                        chunk_ts_list = []
                    # This is the start of one feature_set
                    # ... get data spectrum files
                    for j in data:
                        try:
                            ts = chunk_ts_list[j]
                        except IndexError:
                            break
                        fnargs = {}
                        for feco in file_idp[idkey].included_feature_components:
                            try:
                                fnargs[feco] = os.path.join(directory, getattr(file_idp[idkey], feco)[pol][ts])
                                files_this_pass.add(fnargs[feco])
                            except KeyError:
                                continue
                        if len(fnargs):
                            feature_tag = 'data:{}:'.format(ts)
                            self.get_feature_set_from_files(feature_tag, pol, **fnargs)
                    if not data_only:
                        # ... get feature_sets
                        for ts in chunk_ts_list:
                            fnargs = {}
                            for feco in file_idp[idkey].included_feature_components:
                                try:
                                    fnargs[feco] = os.path.join(directory, getattr(file_idp[idkey], feco)[pol][ts])
                                    files_this_pass.add(fnargs[feco])
                                except KeyError:
                                    continue
                            if len(fnargs):
                                feature_tag = '{}:'.format(ts)
                                self.get_feature_set_from_files(feature_tag, pol, peak_on=peak_on, **fnargs)
                if not keep_data:
                    # ... delete processed files
                    for x in files_this_pass:
                        os.remove(x)
                # Write the ridz file
                fn = "{}_{}.{}.n{}.{}{}.ridz".format(idkey, self.feature_module_name, self.timestamp_first,
                                                     self.nsets, pk_for_fn, th_for_fn)
                filename = os.path.join(directory, fn)
                self.writer(filename)

def _spectrum_reader(filename, spec, polarization=None):
    """
    This reads in an ascii spectrum file.
    """
    if filename is None:
        return
    if polarization is not None:
        spec.polarization = polarization
    spec.val = []
    with open(filename, 'r') as f:
        for line in f:
            if line.strip()[0] == ':':
                spec.comment += (line.strip()[1:] + '\n')
                continue
            data = [float(x) for x in line.split()]
            spec.freq.append(data[0])
            spec.val.append(data[1])

def _spectrum_plotter(x, y, fmt=None, is_spectrum=False, figure_name=None):
    if not len(x) or not len(y):
        return None, None
    if len(x) > len(y):
        _X = x[:len(y)]
        _Y = y[:]
    else:
        _X = x[:]
        _Y = y[:len(x)]
    if fmt is None:
        return _X, _Y

    if figure_name is not None:
        plt.figure(figure_name)
    if is_spectrum:
        plt.plot(_X, _Y, color=fmt[0], linestyle=fmt[1])
    else:
        plt.plot(_X, _Y, color=fmt[0], marker=fmt[1], linestyle="None")

def _peel_filename(v, fclist=None):
    if v == 'filename_format_help':
        s = "The filename format convention is:\n"
        s += "\tidentifier.{time-stamp}.feature_component.polarization\n"
        s += "\ni.e. x=filename.split(.) has:\n"
        s += "\tx[0]:  arbitrary identifier\n"
        s += "\tx[...]: time-stamp (may have .'s in it)\n"
        s += "\tx[-2]:  feature_component ('maxhold', 'minhold', 'val')\n"
        s += "\tx[-1]:  polarization\n"
        return s
    if v is None:
        return {}
    s = v.split('/')[-1].split('.')
    if len(s) < 4:
        return {}
    fnd = {'filename': v, 'ident': s[0]}
    fnd['timestamp'] = '.'.join(s[1:-2])
    fnd['feature_component'] = s[-2].lower()
    fnd['polarization'] = s[-1].upper()
    if fclist is None:
        return fnd
    fclist = [x.lower() for x in fclist]
    for fc in fclist:
        if fnd['feature_component'] in fc:
            fnd['feature_component'] = fc
            return fnd
    return {}

def _get_timestr_from_ftr_key(fkey):
    if ':' in fkey:
        return fkey.split(':')[-2]
    if '.' in fkey:
        return fkey.split('.')[-2]
    return None

def _check_timestamps_for_match(**fnargs):
    list_of = []
    set_of = set()
    for fc, sfn in six.iteritems(fnargs):
        fnd = _peel_filename(sfn)
        if len(fnd):
            ts = fnd['timestamp']
            list_of.append(ts)
            set_of.add(ts)
    if len(list_of) == len(set_of):
        return "Timestamps don't match: " + ','.join(fnargs.values()) + '\n'
    return ''


"""Interface to quantum-chemical programs."""

from __future__ import annotations

from collections.abc import Iterable, Sequence
import typing
from typing import Any

import numpy as np

from morfeus.data import ANGSTROM_TO_BOHR, BOHR_TO_ANGSTROM
from morfeus.typing import (
    Array1DFloat,
    Array1DStr,
    Array2DFloat,
    Array3DFloat,
    ArrayLike2D,
)
from morfeus.utils import convert_elements, Import, requires_dependency

if typing.TYPE_CHECKING:
    import qcelemental as qcel
    import qcengine as qcng

@requires_dependency([Import(module="qcengine", alias="qcng")], globals())
def optimize_qc_engine(
    elements: Iterable[int] | Iterable[str],
    coordinates: ArrayLike2D,
    charge: int | None = None,
    multiplicity: int | None = None,
    connectivity_matrix: ArrayLike2D | None = None,
    program: str = "xtb",
    model: dict[str, Any] | None = None,
    keywords: dict[str, Any] | None = None,
    local_options: dict[str, Any] | None = None,
    procedure: str = "berny",
    return_trajectory: bool = False,
) -> tuple[Array2DFloat | Array3DFloat, Array1DFloat]:
    """Optimize molecule with QCEngine.

    Args:
        elements: Elements as atomic symbols or numbers
        coordinates: Coordinates (Å)
        charge: Molecular charge
        multiplicity: Multiplicity
        connectivity_matrix: Connectivity matrix
        program: QCEngine program
        model: QCEngine model
        keywords: QCEngine keywords
        local_options: QCEngine local options
        procedure: QCEngine procedure
        return_trajectory: Return coordinates for all steps

    Returns:
        opt_coordinates (ndarray): Conformer coordinates (Å)
        energies (ndarray): Energies for all steps (a.u.)

    Raises:
        Exception: When QCEngine calculation fails
    """
    if (
        program.lower() == "rdkit"
        and charge is not None
        and connectivity_matrix is not None
    ):
        _check_qcng_rdkit(charge, connectivity_matrix)

    # Set defaults
    if model is None:
        model = {"method": "GFN2-xTB"}
    if keywords is None:
        keywords = {}
    if local_options is None:
        local_options = {}

    # Create molecule object
    molecule = _generate_qcel_molecule(
        elements, coordinates, charge, multiplicity, connectivity_matrix
    )

    # Create optimization input
    opt_input = {
        "keywords": {"program": program},
        "input_specification": {
            "driver": "gradient",
            "model": model,
            "keywords": keywords,
        },
        "initial_molecule": molecule,
    }

    # Perform optimization
    opt = qcng.compute_procedure(
        opt_input, procedure=procedure, local_options=local_options
    )
    if not opt.success:
        raise Exception(opt.error.error_message)

    # Take out results
    energies: Array1DFloat = np.array(opt.energies)
    if return_trajectory:
        opt_coordinates: Array2DFloat = np.array(
            [result.molecule.geometry for result in opt.trajectory]
        )
    else:
        opt_coordinates = opt.final_molecule.geometry
    opt_coordinates *= BOHR_TO_ANGSTROM

    return opt_coordinates, energies

@requires_dependency([Import(module="qcengine", alias="qcng")], globals())
def sp_qc_engine(
    elements: Iterable[int] | Iterable[str],
    coordinates: Sequence[Sequence[float]],
    charge: int | None = None,
    multiplicity: int | None = None,
    connectivity_matrix: Sequence[Sequence[int]] | None = None,
    program: str = "xtb",
    model: dict[str, Any] | None = None,
    keywords: dict[str, Any] | None = None,
    local_options: dict[str, Any] | None = None,
) -> float:
    """Single-point calculation with QCEngine.

    Args:
        elements: Elements as atomic symbols or numbers
        coordinates: Coordinates (Å)
        charge: Molecular charge
        multiplicity: Molecular multiplicity
        connectivity_matrix: Connectivity matrix
        program: QCEngine program
        model: QCEngine model
        keywords: QCEngine keywords
        local_options: QCEngine local options

    Returns:
        energy: Energy (a.u.)

    Raises:
        Exception: When QCEngine calculation fails
    """
    if (
        program.lower() == "rdkit"
        and charge is not None
        and connectivity_matrix is not None
    ):
        _check_qcng_rdkit(charge, connectivity_matrix)

    # Set defaults
    if model is None:
        model = {"method": "GFN2-xTB"}
    if keywords is None:
        keywords = {}
    if local_options is None:
        local_options = {}

    # Crate molecule object
    molecule = _generate_qcel_molecule(
        elements, coordinates, charge, multiplicity, connectivity_matrix
    )

    # Create sp input
    sp_input = qcel.models.AtomicInput(
        molecule=molecule,
        driver="energy",
        model=model,
        keywords=keywords,
    )

    # Perform sp calculation
    sp = qcng.compute(sp_input, program=program, local_options=local_options)
    if not sp.success:
        raise Exception(sp.error.error_message)

    # Take out results
    energy: float = sp.return_result

    return energy

def _check_qcng_rdkit(charge: int, connectivity_matrix: ArrayLike2D) -> None:
    """Check qncg calculation for RDKit incompatibilities."""
    if charge != 0:
        raise Exception("QCEngine using RDKit does not work with charged molecules.")
    if np.any(~np.isin(connectivity_matrix, [0, 1, 2, 3])):
        raise Exception(
            "QCEngine using RDKit cannot handle bond orders different from "
            "1, 2 or 3."
        )

@requires_dependency([Import(module="qcelemental", alias="qcel")], globals())
def _generate_qcel_molecule(
    elements: Iterable[int] | Iterable[str],
    coordinates: ArrayLike2D,
    charge: int | None = None,
    multiplicity: int | None = None,
    connectivity_matrix: ArrayLike2D | None = None,
) -> qcel.models.Molecule:
    """Generate QCElemental molecule object.

    Args:
        elements: Elements as atomic symbols or numbers
        coordinates: Coordinates (Å)
        charge: Molecular charge
        multiplicity: Molecular multiplicity
        connectivity_matrix: Connectivity matrix

    Returns:
        molecule: QCElemental molecule object.
    """
    # Generate bond order list from connectivity matrix
    bos: list[tuple[int, int, int]] | None
    if connectivity_matrix is not None:
        connectivity_matrix = np.array(connectivity_matrix)
        bos = []
        i, j = np.tril_indices_from(connectivity_matrix)
        for k, l in zip(i, j):
            if k != l:
                bo = int(connectivity_matrix[k, l])
                if bo != 0:
                    bos.append((k, l, bo))
    else:
        bos = None

    # Create molecule object
    elements: Array1DStr = np.array(convert_elements(elements, output="symbols"))
    coordinates = np.array(coordinates) * ANGSTROM_TO_BOHR
    molecule = qcel.models.Molecule(
        symbols=elements,
        geometry=coordinates,
        molecular_charge=charge,
        connectivity=bos,
        molecular_multiplicity=multiplicity,
    )

    return molecule


class Solution:
    def breakPalindrome(self, palindrome: str) -> str:
        if len(palindrome) == 1:
            return ""

        arr = list(palindrome)
        # print(arr)

        # halfIndex= len(arr)//2
        for i in range(len(arr)):
            if i == len(arr)//2:
                continue
            if arr[i] != 'a':
                arr[i] = 'a'
                return ''.join(arr)
        arr[-1] = 'b'
        return ''.join(arr)


from unittest import mock

from oslotest import base

from sushy_tools.emulator.resources.storage import StaticDriver

class StaticDriverTestCase(base.BaseTestCase):
    UUID = "da69abcc-dae0-4913-9a7b-d344043097c0"
    STORAGE_COL = [
        {
            "Id": "1",
            "Name": "Local Storage Controller",
            "StorageControllers": [
                {
                    "MemberId": "0",
                    "Name": "Contoso Integrated RAID",
                    "SpeedGbps": 12
                }
            ]
        }
    ]

    CONFIG = {
        'SUSHY_EMULATOR_STORAGE': {
            UUID: STORAGE_COL
        }
    }

    def test_get_storage_col(self):
        test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
        stg_col = test_driver.get_storage_col(self.UUID)
        self.assertEqual(self.STORAGE_COL, stg_col)

    def test_get_all_storage(self):
        test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
        stg = test_driver.get_all_storage()
        self.assertEqual([(self.UUID, '1')], stg)


# Visualize the data
# Here are the first 9 images in the training dataset. As you can see, label 1 is "dog" and label 0 is "cat".

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(int(labels[i]))
        plt.axis("off")

tensorflow keras/Computer Vision/0 CNN model code example/visualize the images data.py


from django.urls import path
from . import views

# List of url patterns
urlpatterns = [
    path("", views.index, name="index"),
    path("projects/", views.projects,
         name="projects-home"),
    # dynamic path with parameter. Allows for type casting
    path("projects/<slug:slug>", views.project_detail,
         name="project-detail")

]


# Simple test of a UDP server and client transferring data

import socket

NUM_NEW_SOCKETS = 4
NUM_TRANSFERS = 4
PORT = 8000

# Server
def instance0():
    multitest.globals(IP=multitest.get_network_ip())
    multitest.next()
    for i in range(NUM_NEW_SOCKETS):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
        multitest.broadcast("server ready")
        for j in range(NUM_TRANSFERS):
            data, addr = s.recvfrom(1000)
            print(data)
            s.sendto(b"server to client %d %d" % (i, j), addr)
        s.close()

# Client
def instance1():
    multitest.next()
    ai = socket.getaddrinfo(IP, PORT)[0][-1]
    for i in range(NUM_NEW_SOCKETS):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        multitest.wait("server ready")
        for j in range(NUM_TRANSFERS):
            s.sendto(b"client to server %d %d" % (i, j), ai)
            data, addr = s.recvfrom(1000)
            print(data)
        s.close()


import sys
import os
sys.path.append(os.getcwd())
import h5py
import glob
import os.path
import numpy as np
import pandas as pd
import argparse
import seaborn as sns
import matplotlib.pyplot as plt
import dask.array
import glob
from numpy_sugar.linalg import economic_qs, economic_svd
from glimix_core.lmm import LMM
import qtl_utilities as utils
import qtl_loader_utils

def qtl_plots(row,phenotype_ds,phenotype_corrected_ds,plinkGenotype,
              bim,fam,bed,annotation_df,sample2individual_df):
    # extracting SNP index
    print("Processing SNP: "+row[0])
    snp_idx = bim[bim.index == row[0]]["i"]

    # extracting snp-genotypes association
    if (plinkGenotype):
        snp_df = pd.DataFrame(data=bed[snp_idx.values, :].compute().transpose(), index=fam.index, columns=[row[0]])
    else:
        snp_df_dosage = pd.DataFrame(np.nan, index=fam.index, columns=row.index)
        snp_df = pd.DataFrame(np.nan, index=fam.index, columns=snp_names)
        geno = bgen["genotype"][snpId].compute()
        if (geno["ploidy"].min() > 1 & geno["ploidy"].max() < 3):
            if (geno["phased"]):
                snp_df_dosage_t = geno["probs"][:, [0, 2]].sum(1).astype(float)
                snp_df_t = (np.abs(np.argmax(geno["probs"][:, :2], axis=1) - 1) + np.abs(
                    np.argmax(geno["probs"][:, 2:4], axis=1) - 1)).astype(float)
                naId = (np.amax(geno["probs"][:, :2], 1) + np.amax(geno["probs"][:, 2:4], 1)) < (
                            1 + minimumProbabilityStep)
                snp_df_dosage_t[naId] = float('NaN')
                snp_df_t[naId] = float('NaN')
            else:
                snp_df_dosage_t = ((geno["probs"][:, 0] * 2) + geno["probs"][:, 1]).astype(float)
                snp_df_t = (np.abs(np.argmax(geno["probs"][:, :3], axis=1) - 2)).astype(float)
                naId = np.amax(geno["probs"][:, :3], 1) < ((1 / 3) + minimumProbabilityStep)
                snp_df_dosage_t[naId] = float('NaN')
                snp_df_t[naId] = float('NaN')
            snp_df_dosage.loc[:, snp_names[rowNumber]] = snp_df_dosage_t
            snp_df.loc[:, snp_names[rowNumber]] = snp_df_t
        snp_df_dosage = snp_df_dosage.loc[individual_ids, :]

    # filter genotypes
    snp_df = snp_df[snp_df.index.isin(sample2individual_df["iid"].values)]
    snp_pheno_df = pd.merge(snp_df.reset_index(), sample2individual_df)
    snp_pheno_df = snp_pheno_df.merge(phenotype_corrected_ds,left_on="sample",right_index=True)

    # convert to variant
    variant_dic = dict([(0,str(bim[bim.index == row[0]]["a0"][0])+str(bim[bim.index == row[0]]["a0"][0])),
                    (1,str(bim[bim.index == row[0]]["a0"][0])+str(bim[bim.index == row[0]]["a1"][0])),
                    (2,str(bim[bim.index == row[0]]["a1"][0])+str(bim[bim.index == row[0]]["a1"][0]))])

    snp_pheno_df = snp_pheno_df.replace({snp_pheno_df.columns[1]:variant_dic})
    snp_pheno_df = snp_pheno_df.sort_values(by=snp_pheno_df.columns[1])
    if not os.path.exists(output_directory + "/plots/"):
        os.makedirs(output_directory + "/plots/")
    # plot phenotype corrected
    plt.figure()
    sns.set(style="whitegrid")
    ax = sns.violinplot(x=snp_pheno_df.columns[1], y="exp",
                   data=snp_pheno_df, alpha =.1, inner=None)
    plt.setp(ax.collections,alpha=.3)
    ax = sns.swarmplot(x= snp_pheno_df.columns[1], y="exp",
                       data = snp_pheno_df)
    ax.set(ylabel = "Covariates adjusted expression")
    figure = ax.get_figure()
    figure.savefig(output_directory + "/plots/"+snp_pheno_df.columns[1]+"_violinplot_corrected.pdf", dpi=400)

    # filter genotypes
    snp_df = snp_df[snp_df.index.isin(sample2individual_df["iid"].values)]
    snp_pheno_df = pd.merge(snp_df.reset_index(), sample2individual_df)
    snp_pheno_df = snp_pheno_df.merge(phenotype_ds,left_on="sample",right_index=True)

    # convert to variant
    variant_dic = dict([(0,str(bim[bim.index == row[0]]["a0"][0])+str(bim[bim.index == row[0]]["a0"][0])),
                    (1,str(bim[bim.index == row[0]]["a0"][0])+str(bim[bim.index == row[0]]["a1"][0])),
                    (2,str(bim[bim.index == row[0]]["a1"][0])+str(bim[bim.index == row[0]]["a1"][0]))])

    snp_pheno_df = snp_pheno_df.replace({snp_pheno_df.columns[1]:variant_dic})
    snp_pheno_df = snp_pheno_df.sort_values(by=snp_pheno_df.columns[1])
    # plot phenotype standard
    plt.figure()
    sns.set(style="whitegrid")
    ax = sns.violinplot(x=snp_pheno_df.columns[1], y="exp",
                   data=snp_pheno_df, alpha =.1, inner=None)
    plt.setp(ax.collections,alpha=.3)
    ax = sns.swarmplot(x= snp_pheno_df.columns[1], y="exp",
                       data = snp_pheno_df)
    ax.set(ylabel = "Covariates adjusted expression")
    figure = ax.get_figure()
    figure.savefig(output_directory + "/plots/"+snp_pheno_df.columns[1]+"_violinplot.pdf", dpi=400)

def run_plots(plinkGenotype, geno_prefix, annotation_filename, phenotype_filename,
              covariate_filename, top_qtl_results_filename, output_directory,
              sample_mapping_filename,randomeff_filename):

    # # Loading Files
    # phenotype_filename = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Expression/Geuvadis_CEU_YRI_Expr.txt"
    # annotation_filename = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Expression/Geuvadis_CEU_Annot.txt"
    # covariate_filename = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Expression/Geuvadis_CEU_YRI_covariates.txt"
    # sample_mapping_filename = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Geuvadis_CEU_gte.txt"
    # geno_prefix = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Genotypes/Geuvadis"
    # output_directory = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Output"
    # top_qtl_results_filename = "/Users/chaaya/dhonveli_dkfz/limix_qtl/limix_qtl-master/Limix_QTL/test_data/Output/top_qtl_results_all_FDR0.05.txt"
    # plinkGenotype = True

    [phenotype_df, kinship_df, readdepth_df, covariate_df, sample2individual_df,complete_annotation_df, annotation_df, snp_filter_df,
     snp_feature_filter_df, geneticaly_unique_individuals, minimum_test_samples, feature_list, bim, fam, bed, bgen,
     chromosome, selectionStart, selectionEnd, feature_variant_covariate_df]=\
    utils.run_QTL_analysis_load_intersect_phenotype_covariates_kinship_sample_mapping(pheno_filename=phenotype_filename,
                    anno_filename=annotation_filename, geno_prefix=geno_prefix, plinkGenotype=plinkGenotype, cis_mode=True,
                    skipAutosomeFiltering = False, minimum_test_samples= 10,
                    relatedness_score=None, snps_filename=None, feature_filename=None,
                    snp_feature_filename=None, selection='all',covariates_filename=covariate_filename,
                    randomeff_filename=randomeff_filename, sample_mapping_filename=sample_mapping_filename,
                    extended_anno_filename=None, feature_variant_covariate_filename=None)

    # results
    top_qtl_results_df = qtl_loader_utils.get_top_qtl_results(top_qtl_results_filename)

    for row in top_qtl_results_df.iterrows():

        # feature specific parameters for QS mixing
        rho1 = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
        Sigma = {}
        Sigma_qs = {}
        best = {}
        snpQcInfo = None
        currentFeatureNumber+= 1

        phenotype_ds = phenotype_df.loc[row[1]["feature_id"]]
        individual_ids = sample2individual_df.loc[phenotype_ds.index, 'iid'].values
        sample2individual_feature = sample2individual_df.loc[phenotype_ds.index]

        if kinship_df is not None and readdepth_df is None:
            kinship_mat = kinship_df.loc[individual_ids,individual_ids].values
            kinship_mat = kinship_mat.astype(float)
            ##GOWER normalization of Kinship matrix.
            kinship_mat *= (kinship_mat.shape[0] - 1) / (kinship_mat.trace() - kinship_mat.mean(0).sum())
            ## This needs to go with the subselection stuff.
            if(QS is None and not contains_missing_samples):
                QS = economic_qs(kinship_mat)
            elif (contains_missing_samples):
                QS_tmp = QS
                QS = economic_qs(kinship_mat)

        randomeff_mix = False
        # combining the two matrices
        if kinship_df is not None and readdepth_df is not None:
            randomeff_mix = True
            kinship_mat = kinship_df.loc[individual_ids,individual_ids].values
            kinship_mat = kinship_mat.astype(float)
            ##GOWER normalization of Kinship matrix.
            kinship_mat *= (kinship_mat.shape[0] - 1) / (kinship_mat.trace() - kinship_mat.mean(0).sum())
            for rho in rho1:
                Sigma[rho] = rho * kinship_df + (1 - rho) * readdepth_df
                Sigma_qs[rho] = economic_qs(Sigma[rho])

        # creating a fake QS if none random effect is present or use the read depth one
        if kinship_df is None:
            if readdepth_df is None:
                K = np.eye(len(phenotype_ds.index))
                if(QS is None and not contains_missing_samples):
                    QS = economic_qs(K)
                elif (contains_missing_samples):
                    QS_tmp = QS
                    QS = economic_qs(K)
            else:
                if(QS is None and not contains_missing_samples):
                    QS = economic_qs(readdepth_df)
                elif (contains_missing_samples):
                    QS_tmp = QS
                    QS = economic_qs(readdepth_df)

        # covariance matrix
        cov_matrix = covariate_df.loc[sample2individual_feature['sample'], :].values if covariate_df is not None else None
        if covariate_df is None:
            cov_matrix = np.ones((len(individual_ids), 1))
        cov_matrix = cov_matrix.astype(float)

        phenotype = utils.force_normal_distribution(phenotype_ds.values,method="gaussnorm")

        # Prepare LMM
        phenotype = phenotype.astype(float)

        if randomeff_mix:
            # initialize best to minus infinite
            best["lml"] = - math.inf
            best["lmm"] = - math.inf
            best["rho1"] = - math.inf
            for rho, QS in Sigma_qs.items():
                lmm = LMM(phenotype, cov_matrix, QS)
                if not mixed:
                    lmm.delta = 1
                    lmm.fix('delta')
                lmm.fit(verbose=False)
                lml = lmm.lml()
                if lml > best["lml"]:
                    best["lml"] = lml
                    best["lmm"] = lmm
                    best["rho1"] = rho
            lmm = best["lmm"]
            print(best["rho1"])
            if best["rho1"] != 0:
                rho_log[(feature_id)] = best["rho1"]
                print("Read depth has actually an effect!")

        else:
            lmm = LMM(phenotype, cov_matrix, QS)
            if not mixed:
                lmm.delta = 1
                lmm.fix('delta')
            lmm.fit(verbose=False)

        # create phenotype_corrected_df for plotting
        phenotype_corrected = phenotype - cov_matrix[:, 1:].dot(lmm.beta[1:])
        phenotype_corrected_ds = pd.Series(data = phenotype_corrected, index = phenotype_ds.index, name="exp")

        qtl_plots(row, phenotype_ds, phenotype_corrected_ds, plinkGenotype, bim, fam, bed, annotation_df, sample2individual_df)

def parse_args():
    parser = argparse.ArgumentParser(description='Run standard QTL plots after minimal postprocess was run')
    parser.add_argument('--bgen','-bg',required=False)
    parser.add_argument('--plink','-pg',required=False)
    parser.add_argument('--annotation_file','-af', required=True)
    parser.add_argument('--phenotype_file','-pf', required=True)
    parser.add_argument('--randomeff_file', '-rf', required=False)
    parser.add_argument("--top_qtl_results","-tqr",required=True)
    parser.add_argument("--output_directory","-od",required=True)
    parser.add_argument('--covariates_file','-cf', required=False)
    parser.add_argument("--gaussianize_method","-gm",required=False)
    parser.add_argument("--sample_mapping_file","-smf",required=False)
    args = parser.parse_args()
    return args

if __name__=='__main__':
    args = parse_args()
    bgen  = args.bgen
    plink = args.plink
    annotation_filename = args.annotation_file
    phenotype_filename = args.phenotype_file
    top_qtl_results_filename = args.top_qtl_results
    output_directory  = args.output_directory
    covariates_filename = args.covariates_file
    sample_mapping_filename = args.sample_mapping_file
    randomeff_filename = args.randomeff_file

    if ((plink is None) and (bgen is None)):
        raise ValueError("No genotypes provided. Either specify a path to a binary plink genotype file or a bgen file.")
    if ((plink is not None) and (bgen is not None)):
        raise ValueError("Only one genotype file can be provided at once, not both plink and bgen")

    if (bgen is not None) :
        plinkGenotype=False
        geno_prefix = bgen
    else:
        plinkGenotype=True
        geno_prefix = plink

    run_plots(plinkGenotype, geno_prefix, annotation_filename,
              phenotype_filename, covariates_filename, top_qtl_results_filename,
              output_directory, sample_mapping_filename, randomeff_filename)


"""
Aufgabe K10 | A5: Implementieren Sie den Bubblesortalgorithmus (in-place)
"""

def bubble_sort(liste: list):  # in-place
    is_swapped = True
    while is_swapped:
        is_swapped = False
        for i in range(0, len(liste) - 1):
            if liste[i] > liste[i + 1]:
                liste[i], liste[i + 1] = liste[i + 1], liste[i]
                is_swapped = True
    return liste

meine_liste = [6, 2, 8, 3, 2]
print(bubble_sort(meine_liste))


import tensorflow as tf
import numpy as np
import pickle
from imblearn.under_sampling import RandomUnderSampler
from collections import Counter
from operator import itemgetter
from sklearn.metrics import precision_recall_fscore_support, roc_auc_score, accuracy_score

learn_size = 60000
test_size = 20000
d_bert = []; d_user = []; d_liwc = []; d_cont = []; d_time = []

def load_feature_vector(idx, element):
    if idx == 0:
        return d_bert[element]
    elif idx == 1:
        return d_user[element]['user']
    elif idx == 2:
        return d_liwc[element]['liwc']
    elif idx == 3:
        return d_cont[element]['cont'][0:3]
    elif idx == 4:
        return d_time[element]['ict']

def runRNNModel(hidden_size, learning_rate, batch_size, epochs, keep_rate, fs, seq_length=1):
    feature_types = ["bert", "user", "liwc", "cont", "time"]
    feature_uses = list(map(lambda x: x in fs, feature_types))
    print ('features:   ', feature_types)
    print ('uses:       ', feature_uses)

    length_list = [768, 3, 93, 3, 1]
    test_parent = False # for 1st -> 2nd test
    print_body = False

    print ('test_parent: %r'%(test_parent))

    # Prepare the dataset
    global d_bert, d_user, d_liwc, d_cont, d_time
    d_bert = pickle.load(open('/home/jhlim/data/bertfeatures' + str(seq_length) + '.p', 'rb'))
    d_user = pickle.load(open('/home/jhlim/data/userfeatures.activity.p', 'rb'))
    d_liwc = pickle.load(open('/home/jhlim/data/contentfeatures.others.p', 'rb'))
    d_cont = d_liwc
    d_time = pickle.load(open('/home/jhlim/data/temporalfeatures.p', 'rb'))
    list_d_types = [d_bert, d_user, d_liwc, d_cont, d_time]

    input_dim = 0
    for i in range(len(feature_uses)):
        if feature_uses[i]:
            input_dim += length_list[i]
    print ('seq_length: %d, input_dim: %d'%(seq_length, input_dim))
    rnn_hidden_size = hidden_size

    f = open('/home/jhlim/data/seq.learn.%d.csv'%(seq_length), 'r')
    learn_instances = list(map(lambda x:x.replace('\n', '').split(','), f.readlines()))
    f.close()
    f = open('/home/jhlim/data/seq.test.%d.csv'%(seq_length), 'r')
    test_instances = list(map(lambda x:x.replace('\n', '').split(','), f.readlines()))
    f.close()

    learn_X = []; learn_Y = []
    for seq in learn_instances[:learn_size]:
        sub_x = []

        try:
            for i, element in enumerate(seq[:-1]): # seq[-1] : Y. element: 't3_7dfvv'
                if False in list(map(lambda x:element in x, list_d_types)):
                    continue
                sub_x.append(element)
            if (len(sub_x) == seq_length):
                learn_X.append(sub_x)
                learn_Y.append(int(seq[-1]))
        except Exception as e:
            continue

    print ('size of learn_Y: %d' % len(learn_Y))
    print (Counter(learn_Y)) # shows the number of '0' and '1'

    learn_X_reshape = np.reshape(np.array(learn_X), [-1, seq_length]) # row num = file's row num
    sample_model = RandomUnderSampler(random_state=42) # random_state = seed. undersampling: diminish majority class
    learn_X, learn_Y = sample_model.fit_sample(learn_X_reshape, learn_Y)

    test_X = []; test_Y = []
    element_list = []

    for seq in test_instances[:test_size]:
        sub_x = []
        try:
            for i, element in enumerate(seq[:-1]):
                if False in list(map(lambda x:element in x, list_d_types)):
                    continue
                features = []

                for j in range(len(list_d_types)):
                    if feature_uses[j]:
                        features += load_feature_vector(j, element)

                if features != []:
                    sub_x.append(features)

            if (len(sub_x) == seq_length):
                test_X.append(np.array(sub_x))
                test_Y.append(float(seq[-1]))
                element_list.append(element)

        except Exception as e:
            continue

    learn_X = np.reshape(learn_X, [-1, seq_length, 1])
    matrix = []
    for v1, v2 in zip(learn_X, learn_Y):
        matrix.append([v1, v2])
    np.random.shuffle(matrix)
    learn_X = list(map(itemgetter(0), matrix))
    learn_Y = list(map(lambda x:[x], list(map(itemgetter(1), matrix))))

    test_Y = list(map(lambda x:[x], test_Y))
    print ('learn_Y: ', Counter(list(map(lambda x:x[0], learn_Y))))
    print ('test_Y: ', Counter(list(map(lambda x:x[0], test_Y))))
    print ('Data loading Complete learn:%d, test:%d'%(len(learn_Y), len(test_Y)))

    # Start to run the model

    if test_parent:
        if seq_length != 3:
            learn_X = np.array(learn_X)[:, (seq_length-1):, :].tolist()
            test_X = np.array(test_X)[:, (seq_length-1):, :].tolist()
            seq_length = 1
        else:
            learn_X = np.array(learn_X)[:, (seq_length-2):, :].tolist()
            test_X = np.array(test_X)[:, (seq_length-2):, :].tolist()
            seq_length = seq_length-1

    tf.reset_default_graph()
    tf.set_random_seed(50)
    X = tf.placeholder(tf.float32, [None, seq_length, input_dim])
    Y = tf.placeholder(tf.float32, [None, 1])

    is_training = tf.placeholder(tf.bool)
    keep_prob = tf.placeholder(tf.float32)

    weights = {}
    biases = {}

    cells = []
    for _ in range(1):
        cell = tf.contrib.rnn.BasicLSTMCell(num_units=rnn_hidden_size,
                                            state_is_tuple=True,
                                            activation=tf.nn.relu)
        cells.append(cell)

    cells = tf.nn.rnn_cell.MultiRNNCell(cells)

    outputs, states = tf.nn.dynamic_rnn(cells, X,
            dtype=tf.float32)

    outputs = outputs[:, -1]

    bn_output = tf.contrib.layers.batch_norm(outputs, center=True, scale=True, is_training=is_training)

    key = 'fc_l1'
    weights[key] = tf.Variable(tf.random_normal([rnn_hidden_size, hidden_size]))
    biases[key] = tf.Variable(tf.random_normal([hidden_size]))

    key = 'fc_l2'
    weights[key] = tf.Variable(tf.random_normal([hidden_size, hidden_size]))
    biases[key] = tf.Variable(tf.random_normal([hidden_size]))

    key = 'fc_l3'
    weights[key] = tf.Variable(tf.random_normal([hidden_size, 1]))
    biases[key] = tf.Variable(tf.random_normal([1]))

    optimizers = {}
    pred = []

    l1_output = tf.nn.relu(tf.matmul(bn_output, weights['fc_l1']) + biases['fc_l1'])
    #l1_output = tf.nn.relu(tf.matmul(outputs, weights['fc_l1']) + biases['fc_l1']) # might move relu layer to the behind of bn
    l1_bn_output = tf.contrib.layers.batch_norm(l1_output, center=True, scale=True, is_training=is_training)
    l1_dropout = tf.layers.dropout(l1_bn_output, rate=1-keep_prob, training=is_training)

    #l2_output = tf.matmul(l1_bn_output, weights['fc_l2']) + biases['fc_l2']
    l2_output = tf.nn.relu(tf.matmul(l1_bn_output, weights['fc_l2']) + biases['fc_l2'])
    l2_bn_output = tf.contrib.layers.batch_norm(l2_output, center=True, scale=True, is_training=is_training)
    l2_dropout = tf.layers.dropout(l2_bn_output, rate=1-keep_prob, training=is_training)

    logits = tf.matmul(l2_dropout, weights['fc_l3']) + biases['fc_l3']
    labels = Y

    loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)
    cost = tf.reduce_mean(loss)

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    optimizers = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
    optimizers = tf.group([optimizers, update_ops])

    hypothesis = tf.sigmoid(logits)
    pred.append(tf.cast(hypothesis > 0.5, dtype=tf.float32))

    correct_pred = tf.equal(tf.round(hypothesis), Y)
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for e in range(epochs):
            # train batch by batch
            batch_index_start = 0
            batch_index_end = batch_size

            for i in range(int(len(learn_X)/batch_size)):
                X_train_batch = learn_X[batch_index_start:batch_index_end]
                Y_train_batch = learn_Y[batch_index_start:batch_index_end]

                sequences = []
                for sequence in X_train_batch:
                    sub_x = []
                    for element in sequence:
                        element = element[0]
                        features = []

                        for j in range(len(list_d_types)):
                            if feature_uses[j]:
                                features += load_feature_vector(j, element)

                        sub_x.append(features)

                    sequences.append(sub_x)

                X_train_batch = np.array(sequences)
                Y_train_batch = np.array(Y_train_batch)

                opt, c, o, h, l, acc = sess.run([optimizers, cost, outputs, hypothesis, logits, accuracy],
                        feed_dict={X: X_train_batch, Y: Y_train_batch, keep_prob:keep_rate, is_training:True})

                batch_index_start += batch_size
                batch_index_end += batch_size

            if (e % 2 == 0):
                print ('[epochs : %d, cost: %.8f]'%(e, c))

                # TEST
                rst, c, h, l = sess.run([pred, cost, hypothesis, logits], feed_dict={X: test_X, Y: test_Y, keep_prob:1.0, is_training:False})

                out = np.vstack(rst).T
                out = out[0]

                predicts = []
                test_Y = list(map(lambda x:x[0], test_Y))

                for v1, v2 in zip(out, test_Y):
                    decision = False

                    if v1 == int(v2):
                        decision = True
                    predicts.append(decision)

                print ('seq_length: %d, # predicts: %d, # corrects: %d, acc: %f, auc: %f' %(seq_length, len(predicts), len(list(filter(lambda x:x, predicts))), accuracy_score(test_Y, out), roc_auc_score(test_Y, out)))
                test_Y = list(map(lambda x:[x], test_Y))

            if print_body and e == 10:
                print ('print correct elements.')
                test_Y = list(map(lambda x:x[0], test_Y))
                f = open('result/out.txt', 'w')

                for i, item in enumerate(zip(out, test_Y)):
                    v1, v2 = item
                    if v1 == int(v2):
                        f.write(element_list[i] + '\t' + str(v1) + '\t' + str(v2) + '\n')

                f.close()

                test_Y = list(map(lambda x:[x], test_Y))

        print ('\n\n')