Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import numpy as np
import math
from scipy.stats import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
# ==================================================== ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ ==============================================================
discrete_distributions = [
"Биномиальное",
"Отрицательное биномиальное",
"Пуассона",
"Геометрическое"
]
continuous_distributions = [
"Нормальное",
"Экспоненциальное",
"Равномерное",
"Стьюдента",
"Хи-квадрат"
]
distribution_parameters = {
"Биномиальное": ["n", "p"],
"Отрицательное биномиальное": ["r", "p"],
"Пуассона": ["lambda"],
"Геометрическое": ["p"],
"Нормальное": ["mu", "sigma"],
"Экспоненциальное": ["lambda"],
"Равномерное": ["a", "b"],
"Стьюдента": ["df"],
"Хи-квадрат": ["df"]
}
fig1 = None
ax1 = None
canvas1 = None
fig2 = None
ax2 = None
canvas2 = None
fig_sum = None
ax_sum = None
canvas_sum = None
param_entries1 = {}
param_entries2 = {}
# ==================================================== ФУНКЦИИ =========================================================
# ----------------------------------- Обновление для выбора распределений -------------------------------------------
def update_distribution_comboboxes(selected_type):
if selected_type == "discrete":
available_distributions = discrete_distributions
elif selected_type == "continuous":
available_distributions = continuous_distributions
else:
available_distributions = []
dist1_combobox['values'] = available_distributions
dist2_combobox['values'] = available_distributions
dist1_combobox.set("Выберите...")
dist2_combobox.set("Выберите...")
update_parameter_fields()
clear_plots()
# ----------------------------------- Обновление для параметров распределений -------------------------------------
def update_parameter_fields(event=None):
global param_entries1, param_entries2
dist1_name = dist1_combobox.get()
dist2_name = dist2_combobox.get()
param_entries1 = create_parameter_widgets(params1_frame, dist1_name)
param_entries2 = create_parameter_widgets(params2_frame, dist2_name)
clear_plots()
# ----------------------------------- Создание виджетов для параметров -------------------------------------------
def create_parameter_widgets(frame, dist_name):
for widget in frame.winfo_children():
widget.destroy()
parameters = distribution_parameters.get(dist_name)
if parameters:
ttk.Label(frame, text=f"Параметры для {dist_name}:").grid(row=0, column=0, columnspan=2, sticky="w", pady=5)
entry_widgets = {}
for i, param_name in enumerate(parameters):
ttk.Label(frame, text=f"{param_name}:").grid(row=i+1, column=0, sticky="w", padx=5, pady=2)
entry = ttk.Entry(frame)
entry.grid(row=i+1, column=1, sticky="ew", padx=5, pady=2)
entry_widgets[param_name] = entry
frame.columnconfigure(1, weight=1)
return entry_widgets
else:
ttk.Label(frame, text="Выберите распределение").grid(row=0, column=0, sticky="w")
return None
# ----------------------------------- Очистка графиков -------------------------------------------
def clear_plots():
global ax1, ax2, ax_sum
global canvas1, canvas2, canvas_sum
if ax1:
ax1.clear()
ax1.set_title("График Распределения 1")
ax1.set_xlabel("Значение")
ax1.set_ylabel("Вероятность/Плотность")
if canvas1:
canvas1.draw_idle()
if ax2:
ax2.clear()
ax2.set_title("График Распределения 2")
ax2.set_xlabel("Значение")
ax2.set_ylabel("Вероятность/Плотность")
if canvas2:
canvas2.draw_idle()
if ax_sum:
ax_sum.clear()
ax_sum.set_title("Распределение сумм")
ax_sum.set_xlabel("Значение суммы")
ax_sum.set_ylabel("Частота")
if canvas_sum:
canvas_sum.draw_idle()
dist1_description_label.config(text="Распределение 1: Не выбрано")
dist2_description_label.config(text="Распределение 2: Не выбрано")
# ----------------------------------- Постройка графиков -------------------------------------------
def plot_distributions():
clear_plots()
dist1_name = dist1_combobox.get()
dist2_name = dist2_combobox.get()
# print("got names")
if dist1_name == "Выберите..." or dist2_name == "Выберите...":
print("Пожалуйста, выберите оба распределения.")
# TODO: Добавить вывод ошибки в GUI
return
params1_values = {name: entry.get() for name, entry in param_entries1.items()}
params2_values = {name: entry.get() for name, entry in param_entries2.items()}
dist1_description_label.config(text=f"Распределение 1: {dist1_name} (Параметры: {params1_values})")
dist2_description_label.config(text=f"Распределение 2: {dist2_name} (Параметры: {params2_values})")
# print("got params")
global fig1, fig2
global ax1, ax2
global canvas1, canvas2
if fig1 is None:
fig1, ax1 = plt.subplots(figsize=(6, 4))
canvas1 = FigureCanvasTkAgg(fig1, master=plot1_frame)
canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
if fig2 is None:
fig2, ax2 = plt.subplots(figsize=(6, 4))
canvas2 = FigureCanvasTkAgg(fig2, master=plot2_frame)
canvas2.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# --- Создание и отображение графика для Распределения 1 ---
values1, probabilities1 = get_graph(dist1_name, params1_values)
if values1 is None:
return
# print("start viz1")
if dist1_name in discrete_distributions:
if len(values1) < 50:
ax1.bar(values1, probabilities1, width=0.7, color='blue', edgecolor='black')
else:
ax1.bar(values1, probabilities1, width=1, color='blue')
elif dist1_name in continuous_distributions:
ax1.plot(values1, probabilities1, color='blue')
ax1.set_title(f"{dist1_name}")
ax1.set_xlabel("Значение")
ax1.set_ylabel(f"{'Вероятность' if dist2_name in discrete_distributions else 'Плотность'}")
# print("end viz1")
canvas1.draw_idle()
# --- Создание и отображение графика для Распределения 2 ---
values2, probabilities2 = get_graph(dist2_name, params2_values)
if values2 is None:
return
# print("start viz2")
if dist2_name in discrete_distributions:
if len(values2) < 50:
ax2.bar(values2, probabilities2, width=0.7, color='red', edgecolor='black')
else:
ax2.bar(values2, probabilities2, width=1, color='red')
elif dist2_name in continuous_distributions:
ax2.plot(values2, probabilities2, color='red')
ax2.set_title(f"{dist2_name}")
ax2.set_xlabel("Значение")
ax2.set_ylabel(f"{'Вероятность' if dist2_name in discrete_distributions else 'Плотность'}")
# print("end viz2")
canvas2.draw_idle()
plt.close(fig1)
plt.close(fig2)
main_notebook.select(results_frame)
# ----------------------------------- Создание объекта распределения ----------------------------------------------
def get_dist(dist_name, params_values):
if dist_name in discrete_distributions:
dist = None
x = None
limit = None
if dist_name == "Биномиальное":
try:
n = int(params_values['n'])
if n <= 0:
messagebox.showerror("Ошибка ввода", f"Параметр 'n' должен быть натуральным числом. Вы ввели: {params_values['n']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'n' должен быть натуральным числом. Вы ввели: {params_values['n']}")
return None, None, None
try:
p = float(params_values['p'])
if not 0 < p < 1:
messagebox.showerror("Ошибка ввода", f"Параметр 'p' должен быть числом из диапазона (0; 1). Вы ввели: {params_values['p']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'p' должен быть числом из диапазона (0; 1). Вы ввели: {params_values['p']}")
return None, None, None
dist = binom(n=n, p=p)
x = 0
limit = n
elif dist_name == "Отрицательное биномиальное":
try:
r = int(params_values['r'])
if r <= 0:
messagebox.showerror("Ошибка ввода", f"Параметр 'r' должен быть натуральным числом. Вы ввели: {params_values['r']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'r' должен быть натуральным числом. Вы ввели: {params_values['r']}")
return None, None, None
try:
p = float(params_values['p'])
if not 0 < p < 1:
messagebox.showerror("Ошибка ввода", f"Параметр 'p' должен быть числом из диапазона (0; 1). Вы ввели: {params_values['p']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'p' должен быть числом из диапазона (0; 1). Вы ввели: {params_values['p']}")
return None, None, None
dist = nbinom(n=r, p=p)
x = r
elif dist_name == "Пуассона":
try:
lmd = float(params_values['lambda'])
if lmd <= 0:
messagebox.showerror("Ошибка ввода", f"Параметр 'lambda' должен быть положительным числом. Вы ввели: {params_values['lambda']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'lambda' должен быть положительным числом. Вы ввели: {params_values['lambda']}")
return None, None, None
dist = poisson(mu=lmd)
x = 0
elif dist_name == "Геометрическое":
try:
p = float(params_values['p'])
if not 0 < p < 1:
messagebox.showerror("Ошибка ввода", f"Параметр 'p' должен быть числом из диапазона (0; 1). Вы ввели: {params_values['p']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'p' должен быть числом из диапазона (0; 1). Вы ввели: {params_values['p']}")
return None, None, None
dist = geom(p=p)
x = 1
else:
print("Пожалуйста, выберите распределение")
# TODO: сделать ошибку в GUI
return None, None, None
return dist, x, limit
elif dist_name in continuous_distributions:
dist = None
x = None
limit = None
if dist_name == "Нормальное":
try:
mu = float(params_values['mu'])
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'mu' должен быть числом. Вы ввели: {params_values['mu']}")
return None, None, None
try:
sigma = float(params_values['sigma'])
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'sigma' должен быть числом. Вы ввели: {params_values['sigma']}")
return None, None, None
dist = norm(loc=mu, scale=sigma)
elif dist_name == "Экспоненциальное":
try:
lmd = float(params_values['lambda'])
if lmd <= 0:
messagebox.showerror("Ошибка ввода", f"Параметр 'lambda' должен быть положительным числом. Вы ввели: {params_values['lambda']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'lambda' должен быть положительным числом. Вы ввели: {params_values['lambda']}")
return None, None, None
dist = expon(loc=0, scale=1/lmd)
x = 0
elif dist_name == "Равномерное":
try:
a = float(params_values['a'])
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'a' должен быть числом. Вы ввели: {params_values['a']}")
return None, None, None
try:
b = float(params_values['b'])
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'b' должен быть числом. Вы ввели: {params_values['b']}")
return None, None, None
dist = uniform(loc=a, scale=b-a)
x = a
limit = b
elif dist_name == "Стьюдента":
try:
df = int(params_values['df'])
if df < 0:
messagebox.showerror("Ошибка ввода", f"Параметр 'df' должен быть натуральным числом. Вы ввели: {params_values['df']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'df' должен быть натуральным числом. Вы ввели: {params_values['df']}")
return None, None, None
dist = t(df, loc=0, scale=1)
elif dist_name == "Хи-квадрат":
try:
df = int(params_values['df'])
if df < 0:
messagebox.showerror("Ошибка ввода", f"Параметр 'df' должен быть натуральным числом. Вы ввели: {params_values['df']}")
return None, None, None
except ValueError:
messagebox.showerror("Ошибка ввода", f"Параметр 'df' должен быть натуральным числом. Вы ввели: {params_values['df']}")
return None, None, None
dist = chi2(df, loc=0, scale=1)
else:
print("Пожалуйста, выберите распределение")
# TODO: сделать ошибку в GUI
return None, None, None
return dist, x, limit
else:
print("Пожалуйста, выберите распределение")
# TODO: сделать ошибку в GUI
return None, None, None
# ----------------------------------- Получение данных для графика ----------------------------------------------
def get_graph(dist_name, params_values):
if dist_name in discrete_distributions:
epsilon = 5e-4
def build(dist_, x_, limit_=None):
mean = dist_.mean()
values_ = []
probabilities_ = []
while True and (limit_ is None or x_ <= limit_):
px_ = dist_.pmf(x_)
if px_ < epsilon:
if x_ < mean:
x_ += 1
continue
break
values_.append(x_)
probabilities_.append(px_)
x_ += 1
values_ = np.array(values_)
probabilities_ = np.array(probabilities_)
return values_, probabilities_
dist, x, limit = get_dist(dist_name, params_values)
if dist is None:
return None, None
values, probabilities = build(dist, x, limit)
elif dist_name in continuous_distributions:
coverage = 0.99
if dist_name == "Равномерное":
coverage = 0.9999
alpha_ = 1 - coverage
num_points = 1000
def build(dist_):
low_b = dist_.ppf(alpha_ / 2)
up_b = dist_.ppf(1 - alpha_ / 2)
values_ = np.linspace(low_b, up_b, num_points)
probabilities_ = np.array([dist_.pdf(k) for k in values_])
return values_, probabilities_
dist, _, _ = get_dist(dist_name, params_values)
if dist is None:
return None, None
values, probabilities = build(dist)
else:
print("Пожалуйста, выберите распределение")
# TODO: сделать ошибку в GUI
return None, None
return values, probabilities
# ----------------------------------- Получение числа с графика ----------------------------------------------
def get_dist_rnd(dist_name, params_values, sample_size):
if dist_name in discrete_distributions:
dist, _, _ = get_dist(dist_name, params_values)
if dist is None:
return None
elif dist_name in continuous_distributions:
dist, _, _ = get_dist(dist_name, params_values)
if dist is None:
return None
else:
print("Пожалуйста, выберите распределение")
# TODO: сделать ошибку в GUI
return None
return dist.rvs(size=sample_size)
# ----------------------------------- Построение гистограммы сум ----------------------------------------------
def create_sum_result_hist():
dist1_name = dist1_combobox.get()
dist2_name = dist2_combobox.get()
if dist1_name == "Выберите..." or dist2_name == "Выберите...":
print("Пожалуйста, выберите оба распределения.")
# TODO: Добавить вывод ошибки в GUI
return
params1_values = {name: entry.get() for name, entry in param_entries1.items()}
params2_values = {name: entry.get() for name, entry in param_entries2.items()}
try:
sample_size = int(sample_size_entry.get())
if sample_size <= 0:
messagebox.showerror("Ошибка ввода", f"Введите корректный размер выборки - натуральное число. Вы ввели {sample_size_entry.get()}")
return
except ValueError:
messagebox.showerror("Ошибка ввода", f"Введите корректный размер выборки - натуральное число. Вы ввели {sample_size_entry.get()}")
return
sample1 = get_dist_rnd(dist1_name, params1_values, sample_size)
sample2 = get_dist_rnd(dist2_name, params2_values, sample_size)
if sample1 is None or sample2 is None:
return
samples_sum = sample1 + sample2
max_el = max(np.quantile(sample1, 0.99), np.quantile(sample2, 0.99), np.quantile(samples_sum, 0.99))
min_el = min(np.quantile(sample1, 0.01), np.quantile(sample2, 0.01), np.quantile(samples_sum, 0.01))
bins_num = math.floor(max_el - min_el + 1)
bins = None
if dist1_name in continuous_distributions:
bins_num = max(bins_num, 20)
bins_num = math.ceil(2 * (sample_size * bins_num ** 0.5) ** (1/3))
bins = np.linspace(min_el, max_el, bins_num)
else:
bins = np.arange(min_el, max_el + 2).astype(float)
bins = bins - 0.5
global fig_sum
global ax_sum
global canvas_sum
if fig_sum is None:
fig_sum, ax_sum = plt.subplots(figsize=(7, 5))
canvas_sum = FigureCanvasTkAgg(fig_sum, master=plot_sum_frame)
canvas_sum.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
if ax_sum:
ax_sum.clear()
ax_sum.set_title("Распределение сумм")
ax_sum.set_xlabel("Значение суммы")
ax_sum.set_ylabel("Частота")
if canvas_sum:
canvas_sum.draw_idle()
ax_sum.hist(samples_sum, bins=bins, density=True, rwidth=0.7, color="black", histtype='stepfilled')
ax_sum.hist(sample1, bins=bins, density=True, rwidth=0.7, color="blue", histtype='step', linewidth=3, alpha=0.5)
ax_sum.hist(sample2, bins=bins, density=True, rwidth=0.7, color="red", histtype='step', linewidth=3, alpha=0.5)
# ax.grid(True)
canvas_sum.draw_idle()
plt.close(fig_sum)
# ==================================================== СОЗДАНИЕ ОКНА ==================================================
# ------------------------------------------ Создание вкладок -------------------------------------
root = tk.Tk()
root.title("Project-KD")
# root.geometry("800x700")
main_notebook = ttk.Notebook(root)
main_notebook.pack(pady=10, padx=10, fill="both", expand=True)
setup_frame = ttk.Frame(main_notebook, padding="10")
results_frame = ttk.Frame(main_notebook, padding="10")
main_notebook.add(setup_frame, text='Настройка')
main_notebook.add(results_frame, text='Результаты')
# ------------------------------------------ Фрейм для выбора типа -------------------------------------
type_selection_frame = ttk.LabelFrame(setup_frame, text="Выберите тип распределений", padding="10")
type_selection_frame.pack(pady=10, padx=10, fill="x")
selected_distribution_type = tk.StringVar()
selected_distribution_type.set("discrete")
discrete_radio = ttk.Radiobutton(
type_selection_frame,
text="Дискретные",
variable=selected_distribution_type,
value="discrete",
command=lambda: update_distribution_comboboxes(selected_distribution_type.get())
)
discrete_radio.pack(side="left", padx=10)
continuous_radio = ttk.Radiobutton(
type_selection_frame,
text="Непрерывные",
variable=selected_distribution_type,
value="continuous",
command=lambda: update_distribution_comboboxes(selected_distribution_type.get())
)
continuous_radio.pack(side="left", padx=10)
# ------------------------------------------ Фрейм для выбора распределений -------------------------------------
distribution_selection_frame = ttk.LabelFrame(setup_frame, text="Выберите два распределения", padding="10")
distribution_selection_frame.pack(pady=5, padx=10, fill="x")
ttk.Label(distribution_selection_frame, text="Распределение 1:").grid(row=0, column=0, sticky="w", padx=5, pady=5)
dist1_combobox = ttk.Combobox(distribution_selection_frame, state="readonly")
dist1_combobox.grid(row=0, column=1, sticky="ew", padx=5, pady=5)
ttk.Label(distribution_selection_frame, text="Распределение 2:").grid(row=1, column=0, sticky="w", padx=5, pady=5)
dist2_combobox = ttk.Combobox(distribution_selection_frame, state="readonly")
dist2_combobox.grid(row=1, column=1, sticky="ew", padx=5, pady=5)
distribution_selection_frame.columnconfigure(1, weight=1)
dist1_combobox.bind("<<ComboboxSelected>>", update_parameter_fields)
dist2_combobox.bind("<<ComboboxSelected>>", update_parameter_fields)
# ------------------------------------------ Фрейм для параметров -------------------------------------
params1_frame = ttk.LabelFrame(setup_frame, text="Параметры Распределения 1", padding="10")
params1_frame.pack(pady=5, padx=10, fill="x")
params2_frame = ttk.LabelFrame(setup_frame, text="Параметры Распределения 2", padding="10")
params2_frame.pack(pady=5, padx=10, fill="x")
# ------------------------------------------ Кнопка постройки графиков -------------------------------------
plots_button = ttk.Button(setup_frame, text="Построить графики", command=plot_distributions)
plots_button.pack(pady=10)
# ------------------------------------------ Фрейм для графиков -------------------------------------
plots_frame = ttk.LabelFrame(results_frame, text="Распределения", padding="10")
plots_frame.pack(pady=5, padx=10, fill="x")
plot1_frame = ttk.LabelFrame(plots_frame, text="График Распределения 1", padding="10")
plot1_frame.grid(row=1, column=0, sticky="nsew", padx=10, pady=5)
plot2_frame = ttk.LabelFrame(plots_frame, text="График Распределения 2", padding="10")
plot2_frame.grid(row=1, column=1, sticky="nsew", padx=10, pady=5)
plots_frame.columnconfigure(0, weight=1)
plots_frame.columnconfigure(1, weight=1)
plots_frame.rowconfigure(1, weight=1)
dist1_description_label = ttk.Label(plots_frame, text="Распределение 1: Не выбрано")
dist1_description_label.grid(row=0, column=0, sticky="nw", padx=10, pady=5)
dist2_description_label = ttk.Label(plots_frame, text="Распределение 2: Не выбрано")
dist2_description_label.grid(row=0, column=1, sticky="nw", padx=10, pady=5)
# --------------------------------- Фрейм для размера выборки и кнопки запуска симуляции -------------------------------
simulation_frame = ttk.LabelFrame(results_frame, text="Настройка симуляции", padding="10")
simulation_frame.pack(pady=5, padx=10, fill="x")
ttk.Label(simulation_frame, text=f"Размер выборки: ").grid(row=0, column=0, sticky="w", pady=5)
sample_size_entry = ttk.Entry(simulation_frame)
sample_size_entry.insert(0, "100000")
sample_size_entry.grid(row=0, column=1, sticky="e", padx=5, pady=2)
simulation_button = ttk.Button(simulation_frame, text="Начать симуляцию", command=create_sum_result_hist)
simulation_button.grid(row=0, column=2, sticky="e", padx=5, pady=2)
simulation_frame.columnconfigure(2, weight=1)
simulation_frame.rowconfigure(0, weight=1)
# --------------------------------- Фрейм для гистограммы сумм -------------------------------
plot_sum_frame = ttk.LabelFrame(results_frame, text="Гистограмма сумм", padding="10")
plot_sum_frame.pack(pady=5, padx=10, fill="x")
# ==================================================== ЗАПУСК =========================================================
update_distribution_comboboxes(selected_distribution_type.get())
update_parameter_fields()
root.mainloop()
are there any bugs?
Your code is a well-structured Tkinter application for visualizing and simulating probability distributions. However, there are a few potential issues, bugs, and areas for improvement that you might want to address. Below is a detailed analysis of the code, including potential bugs, logical issues, and suggestions for improvement.
plot_distributions function, you call plt.close(fig1) and plt.close(fig2) after creating and drawing the plots. This can cause the plots to disappear from the Tkinter canvas because closing the figure destroys the underlying Matplotlib figure object, which is still being used by the FigureCanvasTkAgg widget.plt.close(fig1) and plt.close(fig2) calls in plot_distributions. Similarly, remove plt.close(fig_sum) in create_sum_result_hist. These figures should remain open as long as the Tkinter window is active, and they will be automatically cleaned up when the application closes.# Remove these lines from plot_distributions
plt.close(fig1)
plt.close(fig2)
# Remove this line from create_sum_result_hist
plt.close(fig_sum)
plot_distributions and create_sum_result_hist functions, you retrieve parameter values from the entry widgets using {name: entry.get() for name, entry in param_entries1.items()}. However, if a user leaves an entry field empty, entry.get() will return an empty string (""), which will cause a ValueError when trying to convert it to an integer or float in get_dist.def validate_params(params_values, dist_name):
for param_name, value in params_values.items():
if not value.strip(): # Check if the field is empty
messagebox.showerror("Ошибка ввода", f"Параметр '{param_name}' для {dist_name} не может быть пустым.")
return False
return True
# In plot_distributions, before calling get_graph
if not validate_params(params1_values, dist1_name) or not validate_params(params2_values, dist2_name):
return
# In create_sum_result_hist, before calling get_dist_rnd
if not validate_params(params1_values, dist1_name) or not validate_params(params2_values, dist2_name):
return
print("Пожалуйста, выберите оба распределения.")) but does not display a user-friendly error message in the GUI.print statements with messagebox.showerror to provide a GUI-based error message.# In plot_distributions
if dist1_name == "Выберите..." or dist2_name == "Выберите...":
messagebox.showerror("Ошибка", "Пожалуйста, выберите оба распределения.")
return
# In create_sum_result_hist
if dist1_name == "Выберите..." or dist2_name == "Выберите...":
messagebox.showerror("Ошибка", "Пожалуйста, выберите оба распределения.")
return
get_dist function, if the distribution name is not found in either discrete_distributions or continuous_distributions, the code prints a message to the console (print("Пожалуйста, выберите распределение")) but does not display a GUI error message.print statements with messagebox.showerror to provide a GUI-based error message.# In get_dist, replace print statements with:
messagebox.showerror("Ошибка", "Пожалуйста, выберите распределение.")
return None, None, None
canvas1, canvas2, and canvas_sum objects are initialized only once (when fig1, fig2, or fig_sum is None). If the user changes the distribution type or parameters, the existing canvas objects are reused, but the axes (ax1, ax2, ax_sum) are cleared and redrawn. This is generally fine, but if the canvas widget is accidentally destroyed or removed (e.g., due to a layout change), the application may crash.canvas.get_tk_widget().winfo_exists().# Example for canvas1 in plot_distributions
if fig1 is None or not canvas1.get_tk_widget().winfo_exists():
fig1, ax1 = plt.subplots(figsize=(6, 4))
canvas1 = FigureCanvasTkAgg(fig1, master=plot1_frame)
canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# Similarly for canvas2 and canvas_sum
create_sum_result_hist function, the number of bins (bins_num) for continuous distributions is calculated using a formula that depends on sample_size and the range of the data. However, this formula may produce an excessively large number of bins for large sample sizes, leading to performance issues or overly detailed histograms.# In create_sum_result_hist, modify the bins calculation for continuous distributions
if dist1_name in continuous_distributions:
bins_num = max(bins_num, 20)
bins_num = min(math.ceil(2 * (sample_size * bins_num ** 0.5) ** (1/3)), 100) # Cap at 100 bins
bins = np.linspace(min_el, max_el, bins_num)
ax.grid(True) in create_sum_result_hist. If uncommented, it would add a grid to the histogram, but the same is not done for the distribution plots in plot_distributions. This inconsistency may confuse users.# In plot_distributions, after setting up ax1
ax1.grid(True)
# In plot_distributions, after setting up ax2
ax2.grid(True)
# In create_sum_result_hist, after setting up ax_sum
ax_sum.grid(True)
sample_size_entry is set to 100,000. For some distributions (e.g., continuous distributions with complex PDFs), generating such a large sample and plotting a histogram can be computationally expensive, leading to performance issues or unresponsiveness.# In create_sum_result_hist, add a maximum limit
MAX_SAMPLE_SIZE = 1000000
try:
sample_size = int(sample_size_entry.get())
if sample_size <= 0:
messagebox.showerror("Ошибка ввода", f"Введите корректный размер выборки - натуральное число. Вы ввели {sample_size_entry.get()}")
return
if sample_size > MAX_SAMPLE_SIZE:
messagebox.showerror("Ошибка ввода", f"Размер выборки не может превышать {MAX_SAMPLE_SIZE}. Вы ввели {sample_size_entry.get()}")
return
except ValueError:
messagebox.showerror("Ошибка ввода", f"Введите корректный размер выборки - натуральное число. Вы ввели {sample_size_entry.get()}")
return
# Set a lower default sample size
sample_size_entry.delete(0, tk.END)
sample_size_entry.insert(0, "10000")
get_graph function, discrete distributions use an epsilon threshold (5e-4) to determine the range of values to plot. However, this approach may miss important parts of the distribution if the probabilities drop below epsilon prematurely, especially for distributions with long tails (e.g., Poisson with a large lambda).ppf (percent point function) to find the lower and upper bounds of the distribution.# In get_graph, for discrete distributions
def build(dist_, x_, limit_=None):
if limit_ is not None:
values_ = np.arange(x_, limit_ + 1)
else:
# Use ppf to cover 99.9% of the distribution
low_bound = dist_.ppf(0.0005) # 0.05% quantile
up_bound = dist_.ppf(0.9995) # 99.95% quantile
values_ = np.arange(max(0, math.floor(low_bound)), math.ceil(up_bound) + 1)
probabilities_ = dist_.pmf(values_)
return values_, probabilities_
plot_distributions function where the ylabel for ax1 uses dist2_name instead of dist1_name to determine whether to use "Вероятность" or "Плотность".ax1 to use dist1_name.# In plot_distributions, for ax1
ax1.set_ylabel(f"{'Вероятность' if dist1_name in discrete_distributions else 'Плотность'}")
# For ax2 (already correct)
ax2.set_ylabel(f"{'Вероятность' if dist2_name in discrete_distributions else 'Плотность'}")
create_sum_result_hist) does not handle this case explicitly, and the visualization may be misleading.# In create_sum_result_hist, add a type check
if (dist1_name in discrete_distributions and dist2_name in continuous_distributions) or \
(dist1_name in continuous_distributions and dist2_name in discrete_distributions):
messagebox.showerror("Ошибка", "Оба распределения должны быть одного типа (дискретные или непрерывные).")
return
figsize=(6, 4) (or figsize=(7, 5) for the histogram). This may not scale well on different screen sizes or resolutions, especially if the Tkinter window is resized.fill and expand options correctly in the Tkinter layout.# Example of dynamic figure size based on frame size
def get_frame_size(frame):
frame.update_idletasks() # Ensure the frame size is updated
width = frame.winfo_width() / 100 # Convert pixels to inches (approximate)
height = frame.winfo_height() / 100
return max(width, 6), max(height, 4)
# In plot_distributions, for fig1
figsize = get_frame_size(plot1_frame)
if fig1 is None or not canvas1.get_tk_widget().winfo_exists():
fig1, ax1 = plt.subplots(figsize=figsize)
canvas1 = FigureCanvasTkAgg(fig1, master=plot1_frame)
canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# Similarly for fig2 and fig_sum
create_sum_result_hist function performs a simulation that can take a noticeable amount of time for large sample sizes. During this time, the GUI may appear unresponsive, and users may think the application has crashed.tkinter.ttk.Progressbar or a simple label update, and run the simulation in a separate thread to keep the GUI responsive.import threading
# In create_sum_result_hist, add a progress indicator
def run_simulation():
progress_label = ttk.Label(simulation_frame, text="Симуляция выполняется, пожалуйста, подождите...")
progress_label.grid(row=1, column=0, columnspan=3, pady=5)
def simulation_task():
# Run the simulation code here
sample1 = get_dist_rnd(dist1_name, params1_values, sample_size)
sample2 = get_dist_rnd(dist2_name, params2_values, sample_size)
if sample1 is None or sample2 is None:
root.after(0, progress_label.destroy) # Remove progress label on error
return
samples_sum = sample1 + sample2
# ... (rest of the simulation code)
# Remove progress label when done
root.after(0, progress_label.destroy)
# Run the simulation in a separate thread
simulation_thread = threading.Thread(target=simulation_task)
simulation_thread.start()
# Replace the simulation code in create_sum_result_hist with:
run_simulation()
sigma in the normal distribution is positive, or that b > a in the uniform distribution. These checks are currently missing or incomplete in get_dist.get_dist and get_graph.np.histogram instead of ax.hist to separate the computation from the plotting.get_dist, get_graph, get_dist_rnd). This will help catch bugs early, especially when modifying the code.Here is a summary of the key fixes to address the identified bugs:
plt.close calls in plot_distributions and create_sum_result_hist.validate_params function.print statements with GUI error messages using messagebox.showerror.plot_distributions for ax1.create_sum_result_hist.get_graph.Your code is already functional and well-organized, but addressing the issues above will make it more robust, user-friendly, and maintainable. If you need help implementing any of the fixes or improvements, feel free to ask!