Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
TeddyRoncin committed Nov 17, 2021
1 parent e74b05e commit 35365b4
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 135 deletions.
41 changes: 41 additions & 0 deletions src/DataInput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from tkinter import *


class DataInput(Frame):

def __init__(self, master=None, cnf={}, **kw):
Frame.__init__(self, master, cnf, **kw)
self.entries = []
self.generate_entry()

def generate_entry(self):
entry_value = StringVar()
last_value = ""
def add_data_callback(var, indx, mode):
nonlocal last_value
if entry_value.get() == "":
entry_value.set(0)
return
try:
float(entry_value.get().replace(",", "."))
except ValueError:
entry_value.set(last_value)
return
if "," in entry_value.get():
entry_value.set(entry_value.get().replace(",", "."))
last_value = entry_value.get()
return
last_value = entry_value.get()
if self.entries[-1].get() != "":
self.generate_entry()
# For some reason, if we remove that, it doesn't seem to work anymore. Strange...
entry_value.trace_add("write", add_data_callback)
entry = Entry(self, width=50, font=("Arrial", 18), bg='#FFFFFF', fg='#4682B4',
textvariable=entry_value)
entry.pack(expand=YES)
self.entries.append(entry)

def get_data(self):
print([entry for entry in self.entries])
[print(entry.get()) for entry in self.entries]
return [float(entry.get()) for entry in self.entries if entry.get() != ""]
24 changes: 17 additions & 7 deletions src/Frame.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from tkinter import *
from tkinter import Frame, Label, Button, YES
from time import sleep
from src.DataInput import DataInput
from src.utils import present_xhi2_data


class ClassFrame(Frame):
class Frame(Frame):

def __init__(self, containeur, color):
super().__init__(containeur, bg=color)
Expand Down Expand Up @@ -50,6 +51,9 @@ def add_Button(self, content, police, size, background, forground, cmd):

def add_Entry(self, entry):
self.add_element(entry, expand=YES)

def on_load(self, callback):
self.bind("<Map>", callback)
"""
def add_Bind(self, cmmd):
self.Bind = bind(self, command=cmd)
Expand All @@ -60,21 +64,27 @@ def add_Bind(self, cmmd):
"""
@classmethod
def build_home(cls, window, callback):
frame = ClassFrame(window, "#4682B4")
frame = Frame(window, "#4682B4")
frame.add_text("Bienvenue sur le test du \u03C7\u00B2", "Arial", 35, '#4682B4', '#FFFFFF')
frame.add_Button("Continuer", "Arrial", 20, '#FFFFFF', '#4682B4', callback)
frame.add_Button("Continuer", "Arial", 20, '#FFFFFF', '#4682B4', callback)
frame.pack(expand=YES)
return frame

@classmethod
def build_data_selection(cls, window, callback):

frame = ClassFrame(window, "#4682B4")
frame = Frame(window, "#4682B4")
frame.add_text(
"Sélectionnez votre échantillons de mesures",
"Arial", 18, '#4682B4', '#FFFFFF')
#frame.add_Entry(())
frame.add_Button("Calculer", "Arrial", 20, '#FFFFFF', '#4682B4', callback)
frame.add_Button("Calculer", "Arial", 20, '#FFFFFF', '#4682B4', callback)
data_input = DataInput(frame)
frame.add_element(data_input)
return frame, data_input

@classmethod
def build_table(cls, window, data_provider):
frame = Frame(window, "#4682B4")
frame.add_text("Tableau de \u03C7\u00B2", "Arial", 20, '#4682B4', '#FFFFFF')
frame.on_load(lambda e: present_xhi2_data(data_provider()))
return frame
159 changes: 31 additions & 128 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import statistics
import matplotlib.pyplot as plt
import numpy as np
from DataArray import DataArray
from ClassData import ClassData
from src.ClassFrame import ClassFrame
import src.ClassTable
from src.DataArray import DataArray
from src.ClassData import ClassData
from src.Frame import Frame
from src.utils import present_xhi2_data

from tkinter import Tk, mainloop, Entry, YES

data = []


from tkinter import *
def ecart_centre_red(a, mean, ecart_type):
b = (a - mean) / ecart_type
return b
Expand All @@ -19,18 +23,6 @@ def population(a, b, data):
pop += 1
return pop

def theorical_frequency(a, b, integration_table):
# int(a < 0) = -1 si a > 0, 1 si a < 0
# Les calculs sont bizarres, mais ils doivent marcher ^^
print(a)
print(b)
a = abs(int(100 * a))
b = abs(int(100 * b))
print((str(a // 100) + "." + str(a // 10 - a // 100 * 10), str(a - a // 10 * 10)))
print((str(b // 100) + "." + str(b // 10 - b // 100 * 10), str(b - b // 10 * 10)))
return \
int(a < 0) * float(integration_table[str(a // 100) + "." + str(a // 10 - a // 100 * 10), str(a - a // 10 * 10)]) + \
int(b > 0) * float(integration_table[str(b // 100) + "." + str(b // 10 - b // 100 * 10), str(b - b // 10 * 10)])

def home_buttom_continue():
frame_home.destroy()
Expand All @@ -42,63 +34,29 @@ def return_entry(en):
return content


def get_data():
return data


def data_buttom_calculate():
content_entry = entry_frameselct.get()
content_entry = str(content_entry).split(",")
sample = sorted([int(i) for i in content_entry])
sample_mean = statistics.mean(sample)
def Calculate_e_type_sb():
e_type_sb = statistics.stdev(sample, sample_mean)
return e_type_sb

values = sorted([ecart_centre_red(i, sample_mean, Calculate_e_type_sb()) for i in sample])

width = values[-1] - values[0]
class_count = int(math.sqrt(len(sample))) + 1
class_width = width / class_count
# Je pense que ça sert à rien, si on peut avoir plus précis, autant avoir plus précis
# class_width = round(class_width, 4) + 0.0001

# La même ici je pense
min_value = values[0] # round(values[0], 4) - 0.0001

# Format d'une class : [(borne_sup, borne_inf), population, fréquence_expérimentale, fréquence_théorique, x²]
class_data = []
for i in range(class_count):
data = ClassData()
data.infimum = min_value + class_width * i
data.supremum = min_value + (1 + i) * class_width
class_data.append(data)
# On ajoute 1 pour faire comme si l'intervalle était fermé. On aurait pu ajouter 2, 3, 4, 0.0000001,
# mais autant ajouter 1, c'est pratique ^^
# class_data[-1].supremum = values[-1] + 1
# J'ai pas compris ici, mais ça fesait un dernier intervalle incoherent

for i, data in enumerate(class_data):
data.population = population(min_value + class_width * i, min_value + (1 + i) * class_width, values)
for data in class_data:
data.experimental_frequency = data.population / len(values)

integration_table = DataArray.load_array("Integrate_gauss")
for data in class_data:
data.theorical_frequency = theorical_frequency(data.infimum, data.supremum, integration_table)

xhi2 = class_count * sum(
[(data.experimental_frequency * data.theorical_frequency) ** 2 / data.theorical_frequency for data in
class_data])

result = ("Valeurs centrées réduites : {}\nEtendue des valeurs : {}\nLongueur des classes : {}\nDonnées des classes : {}\nxhi² : {}".format(str(values), str(width), str(class_width), str(class_data), str(xhi2)))

#xhi2 = present_xhi2_data(data_input.get_data())
#print(xhi2)

#result = ("Valeurs centrées réduites : {}\nEtendue des valeurs : {}\nLongueur des classes : {}\nDonnées des classes : {}\nxhi² : {}".format(str(values), str(width), str(class_width), str(class_data), str(xhi2)))
#print(result)

bins_class = [] # On definis les diffférents intervalles
for data in class_data:
bins_class.append(data.infimum)
bins_class.append(values[-1])
#bins_class = [] # On definis les diffférents intervalles
#for data in class_data:
# bins_class.append(data.infimum)
#bins_class.append(values[-1])
global data
data = data_input.get_data()
frame_dataselection.destroy()
frame_table_presentaion.pack()
list_calculate = (sample, sample_mean, Calculate_e_type_sb(), values, width, class_width, class_data, xhi2, bins_class)
print(list_calculate)
return list_calculate
frame_table_presentation.pack()
#list_calculate = (sample, sample_mean, Calculate_e_type_sb(), values, width, class_width, class_data, xhi2, bins_class)
#print(list_calculate)
#return list_calculate



Expand All @@ -110,64 +68,9 @@ def Calculate_e_type_sb():
window.config(background='#4682B4')


frame_home = ClassFrame(window, "#4682B4")
frame_home.add_Subitle("Bienvenue sur le test du \u03C7\u00B2", "Arial", 35, '#4682B4', '#FFFFFF')
frame_home.add_Buttom("Continuer", "Arrial", 20, '#FFFFFF', '#4682B4', home_buttom_continue)

frame_home.pack(expand=YES)

frame_dataselection = ClassFrame(window, "#4682B4")
frame_dataselection.add_Title("Sélectionnez votre échantillons de mesures \n(chaque mesure doit être séparée par une virgule et entière)", "Arial", 18, '#4682B4', '#FFFFFF')
entry_frameselct = Entry(frame_dataselection, width=50, font=("Arrial", 18), bg='#FFFFFF', fg='#4682B4')
entry_frameselct.pack(expand=YES)

entry_frameselct.bind('<Return>', return_entry(entry_frameselct))
frame_dataselection.add_Buttom("Calculer", "Arrial", 20, '#FFFFFF', '#4682B4', data_buttom_calculate)

frame_table_presentaion = ClassFrame(window, "#4682B4")
frame_table_presentaion.add_Title("Tableau de \u03C7\u00B2", "Arrial", 20, '#4682B4', '#FFFFFF')

def buttom_TableandGraph():
fig, ax = plt.subplots() # On cree la fenetre pyplot
exp_freq_array = np.array(data_buttom_calculate()[3]) # On change une liste en une matrice


# 10,10,11,11,11,13,12,9,12,14
# Echantillon test1
x = []
y = []
# On cree une liste d'antécendant une une matrice d'immage pour pouvoir
# creer la courbe théorique
while len(x) <= 100:
x.append((sample[0] - sample_mean) + len(x) * (sample[-1] - sample[0]) / 100)
for i in range(len(x)):
y.append((1 / (np.sqrt(2 * np.pi) * e_type_sb)) * np.exp(-0.5 * ((x[i]) / e_type_sb) ** 2))

num_bins = class_count
# On construit l'histogramme reel
n, bins, patches = ax.hist(exp_freq_array, num_bins, density=True)
# 14,15,16,17,18,14,15,17,16,17,17,18,17,17,13,15,15,15,14,13,16,17,18,14,15,16,17,18,19,17,14,17,16,15,15,16
# echantillon test 2
# 0.385,0.387,0.388,0.383,0.391,0.380,0.382,0.384,0,.386,0.389,0.390,0.385,0.389,0.388,0.382,0.387,0.388,0.392,0.383,0.388,0.385
# 385,387,388,383,391,380,382,384,386,389,390,385,389,388,382,387,388,392,383,388,385
# On transforme les liste en matrice
x_array = np.array(x)
y_array = np.array(y)
# On met le teste de la bulle
textstr = "$\mu=%.2f${}\n$\sigma=%.2f${}\n\u03C7\u00B2={}".format(round(sample_mean, 3), round(e_type_sb, 3),
round(xhi2, 4))
# On creer la bulle
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
# On assemble le texte et la bulle pour former la légende
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props)

# On creer la courbe théorique
plt.plot(x_array, y_array, '--')
# On met le titre
plt.title("Test du \u03C7\u00B2")
# On met une legende sur l'axe y
plt.ylabel('Frequence')
# plt.show()
frame_home = Frame.build_home(window, home_buttom_continue)
frame_dataselection, data_input = Frame.build_data_selection(window, data_buttom_calculate)
frame_table_presentation = Frame.build_table(window, lambda: data)


#total_rows = len(lst)
Expand Down
1 change: 1 addition & 0 deletions src/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from src.utils.xhi2 import *
Loading

0 comments on commit 35365b4

Please sign in to comment.