Created
November 27, 2011 09:09
-
-
Save idkravitz/1397278 to your computer and use it in GitHub Desktop.
lab3 in progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import numpy | |
| import numpy.linalg | |
| from math import sqrt, sin, cos, pi, floor, ceil | |
| import matplotlib.pyplot | |
| from tkinter import * | |
| from tkinter.messagebox import showerror | |
| class App: | |
| def __init__(self, root, M, Means, B): | |
| self.root = root | |
| self.M = M | |
| self.Means = Means | |
| self.B = B | |
| self.A = numpy.linalg.cholesky(B) | |
| frame = Frame(self.root) | |
| frame.pack() | |
| self.lb_1 = Listbox(frame, selectmode=SINGLE, exportselection=0) | |
| self.lb_2 = Listbox(frame, selectmode=SINGLE, exportselection=0) | |
| for i in range(1, self.M + 1): | |
| self.lb_1.insert(END, i) | |
| self.lb_2.insert(END, i) | |
| self.lb_1.pack(side=LEFT) | |
| self.lb_2.pack(side=LEFT) | |
| self.button = Button(frame, text="Draw plot", command=self.draw_isolines) | |
| self.button.pack(side=LEFT) | |
| self.probe_size = IntVar() | |
| frame2 = Frame(self.root) | |
| frame2.pack() | |
| self.probe_size_entry = Entry(frame2, textvariable=self.probe_size) | |
| self.probe_size_entry.pack() | |
| def draw_isolines(self): | |
| if len(self.lb_1.curselection()) == 0 or len(self.lb_2.curselection()) == 0: | |
| showerror("Error", "Both components number must be chosen") | |
| return | |
| i, j = [int(x) - 1 for x in self.lb_1.curselection() + self.lb_2.curselection()] | |
| if i == j: | |
| showerror("Error", "Choose only distinct components") | |
| return | |
| x, y = self.get_graph_dots(i, j, 5) | |
| matplotlib.pyplot.plot(x, y) | |
| matplotlib.pyplot.axes().set_aspect('equal') | |
| matplotlib.pyplot.show() | |
| def get_graph_dots(self, i, j, constant, num_dots=100): | |
| sigma_i = sqrt(self.B[i][i]) | |
| sigma_j = sqrt(self.B[j][j]) | |
| kovar_ij = self.B[i][j] | |
| korrel_ij = kovar_ij / (sigma_j * sigma_i) | |
| angles = numpy.linspace(0, 2 * pi, num=num_dots) | |
| radiuses = [constant / sqrt(1 - korrel_ij * sin(phi * 2)) for phi in angles] | |
| x = [self.Means[i] + sigma_i * r * cos(phi) for (r,phi) in zip(radiuses, angles)] | |
| y = [self.Means[j] + sigma_j * r * sin(phi) for (r,phi) in zip(radiuses, angles)] | |
| return (x, y) | |
| def __main__(argc, argv): | |
| if argc != 2: | |
| return "Usage: {0} filename".format(sys.argv[0]) | |
| content = open(argv[1], "r").read().split() | |
| M = int(content.pop(0)) | |
| content = [float(x) for x in content] | |
| Means = numpy.array(content[:M]) | |
| content = content[M:] | |
| B = numpy.array(content).reshape(M,M) | |
| #print(numpy.dot(A, A.T.conj())) | |
| root = Tk() | |
| app = App(root, M, Means, B) | |
| root.mainloop() | |
| sys.exit(__main__(len(sys.argv), sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment