changed font size on plots
This commit is contained in:
parent
f56935c748
commit
bd7889eed2
69
latency.py
69
latency.py
@ -2,6 +2,14 @@ import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from scipy import stats
|
||||
import argparse
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-f', '--file', type=str, help='Path to the file')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def calculate_confidence_interval(data, confidence=0.95):
|
||||
"""
|
||||
@ -23,8 +31,9 @@ def calculate_confidence_interval(data, confidence=0.95):
|
||||
|
||||
return mean, ci[0], ci[1], std_error
|
||||
|
||||
|
||||
def plot_delay_histogram_with_ci(csv_file_path, save_to_file=True, show_plot=False,
|
||||
confidence_level=0.99, delimiter=','):
|
||||
confidence_level=0.99, title="default", delimiter=','):
|
||||
"""
|
||||
Универсальный скрипт для построения гистограммы с доверительным интервалом
|
||||
"""
|
||||
@ -32,8 +41,9 @@ def plot_delay_histogram_with_ci(csv_file_path, save_to_file=True, show_plot=Fal
|
||||
try:
|
||||
# Чтение данных
|
||||
df = pd.read_csv(csv_file_path, delimiter=delimiter, header=None,
|
||||
names=['index', 'delay_ms'])
|
||||
delays = df['delay_ms'].values
|
||||
names=['index', 'delay_ms'])
|
||||
delays = np.array(df['delay_ms'].values)
|
||||
plt.rcParams.update({'font.size': 18})
|
||||
|
||||
# Вычисление базовой статистики
|
||||
mean_delay = np.mean(delays)
|
||||
@ -72,7 +82,8 @@ def plot_delay_histogram_with_ci(csv_file_path, save_to_file=True, show_plot=Fal
|
||||
print(f"Коэффициент вариации: {cv:.1f}%")
|
||||
print(f"Q1 (25-й перцентиль): {np.percentile(delays, 25):.2f} мс")
|
||||
print(f"Q3 (75-й перцентиль): {np.percentile(delays, 75):.2f} мс")
|
||||
print(f"IQR (межквартильный размах): {np.percentile(delays, 75) - np.percentile(delays, 25):.2f} мс")
|
||||
print(
|
||||
f"IQR (межквартильный размах): {np.percentile(delays, 75) - np.percentile(delays, 25):.2f} мс")
|
||||
|
||||
# Создание гистограммы
|
||||
plt.figure(figsize=(14, 8))
|
||||
@ -80,34 +91,38 @@ def plot_delay_histogram_with_ci(csv_file_path, save_to_file=True, show_plot=Fal
|
||||
|
||||
# Гистограмма
|
||||
n, bins, patches = plt.hist(delays, bins=n_bins, alpha=0.7, color='steelblue',
|
||||
edgecolor='black', linewidth=0.5, density=False)
|
||||
edgecolor='black', linewidth=0.5, density=False)
|
||||
|
||||
plt.xlabel('Задержка (мс)', fontsize=12)
|
||||
plt.ylabel('Частота', fontsize=12)
|
||||
plt.title(f'Распределение задержек на обработку нажатия мыши (n={len(delays)}, CI={confidence_level*100}%)',
|
||||
fontsize=14, fontweight='bold')
|
||||
plt.xlabel('Задержка (мс)')
|
||||
plt.ylabel('Частота')
|
||||
plt.title(f'Распределение задержек на обработку нажатия мыши {title} (n={len(delays)}, CI={confidence_level*100}%)',
|
||||
fontsize=14, fontweight='bold')
|
||||
plt.grid(True, alpha=0.3)
|
||||
|
||||
mean_delay = float(mean_delay)
|
||||
median_delay = float(median_delay)
|
||||
# Добавление статистических линий
|
||||
plt.axvline(mean_delay, color='red', linestyle='-', linewidth=2,
|
||||
label=f'Среднее ({mean_delay:.1f} мс)')
|
||||
label=f'Среднее ({mean_delay:.1f} мс)')
|
||||
plt.axvline(median_delay, color='green', linestyle='-', linewidth=2,
|
||||
label=f'Медиана ({median_delay:.1f} мс)')
|
||||
label=f'Медиана ({median_delay:.1f} мс)')
|
||||
|
||||
# Добавление доверительного интервала
|
||||
plt.axvspan(ci_lower, ci_upper, alpha=0.2, color='yellow',
|
||||
label=f'ДИ {confidence_level*100}%: [{ci_lower:.1f}, {ci_upper:.1f}] мс')
|
||||
label=f'ДИ {confidence_level*100}%: [{ci_lower:.1f}, {ci_upper:.1f}] мс')
|
||||
|
||||
# Вертикальные линии для границ доверительного интервала
|
||||
plt.axvline(ci_lower, color='orange', linestyle='--', linewidth=1, alpha=0.7)
|
||||
plt.axvline(ci_upper, color='orange', linestyle='--', linewidth=1, alpha=0.7)
|
||||
plt.axvline(ci_lower, color='orange',
|
||||
linestyle='--', linewidth=1, alpha=0.7)
|
||||
plt.axvline(ci_upper, color='orange',
|
||||
linestyle='--', linewidth=1, alpha=0.7)
|
||||
|
||||
plt.legend(loc='upper right', fontsize=10)
|
||||
plt.legend(loc='upper right')
|
||||
plt.tight_layout()
|
||||
|
||||
# Сохранение в файл
|
||||
if save_to_file:
|
||||
output_file = f'delay_distribution_ci_{int(confidence_level*100)}.png'
|
||||
output_file = f'delay_distribution_{title}_ci_{int(confidence_level*100)}.png'
|
||||
plt.savefig(output_file, dpi=300, bbox_inches='tight')
|
||||
print(f"\n✓ Гистограмма сохранена как: {output_file}")
|
||||
|
||||
@ -136,14 +151,15 @@ def plot_delay_histogram_with_ci(csv_file_path, save_to_file=True, show_plot=Fal
|
||||
print(f"✗ Ошибка: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def compare_confidence_intervals(csv_file_path, confidence_levels=[0.90, 0.95, 0.99], delimiter=','):
|
||||
"""
|
||||
Сравнение доверительных интервалов для разных уровней доверия
|
||||
"""
|
||||
try:
|
||||
df = pd.read_csv(csv_file_path, delimiter=delimiter, header=None,
|
||||
names=['index', 'delay_ms'])
|
||||
delays = df['delay_ms'].values
|
||||
names=['index', 'delay_ms'])
|
||||
delays = np.array(df['delay_ms'].values)
|
||||
|
||||
print("=" * 70)
|
||||
print("СРАВНЕНИЕ ДОВЕРИТЕЛЬНЫХ ИНТЕРВАЛОВ ДЛЯ РАЗНЫХ УРОВНЕЙ ДОВЕРИЯ")
|
||||
@ -154,7 +170,8 @@ def compare_confidence_intervals(csv_file_path, confidence_levels=[0.90, 0.95, 0
|
||||
|
||||
results = []
|
||||
for conf in confidence_levels:
|
||||
mean, ci_lower, ci_upper, std_error = calculate_confidence_interval(delays, conf)
|
||||
mean, ci_lower, ci_upper, std_error = calculate_confidence_interval(
|
||||
delays, conf)
|
||||
width = ci_upper - ci_lower
|
||||
margin = width / 2
|
||||
|
||||
@ -178,11 +195,12 @@ def compare_confidence_intervals(csv_file_path, confidence_levels=[0.90, 0.95, 0
|
||||
print(f"Ошибка при сравнении интервалов: {e}")
|
||||
return None
|
||||
|
||||
|
||||
# Пример использования
|
||||
if __name__ == "__main__":
|
||||
# Укажите путь к вашему файлу
|
||||
csv_file = "delays.csv"
|
||||
|
||||
csv_file = parse_args().file
|
||||
title = csv_file.split('/')[1].split('.')[0]
|
||||
print("АНАЛИЗ ДОВЕРИТЕЛЬНЫХ ИНТЕРВАЛОВ ДЛЯ ЗАДЕРЖЕК")
|
||||
print("=" * 50)
|
||||
|
||||
@ -191,18 +209,21 @@ if __name__ == "__main__":
|
||||
csv_file_path=csv_file,
|
||||
save_to_file=True,
|
||||
show_plot=False,
|
||||
confidence_level=0.99
|
||||
confidence_level=0.99,
|
||||
title=title
|
||||
)
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
|
||||
# Сравнение разных уровней доверия
|
||||
compare_results = compare_confidence_intervals(csv_file, [0.90, 0.95, 0.99])
|
||||
compare_results = compare_confidence_intervals(
|
||||
csv_file, [0.90, 0.95, 0.99])
|
||||
|
||||
# Интерпретация результатов
|
||||
if results:
|
||||
print("\n--- ИНТЕРПРЕТАЦИЯ РЕЗУЛЬТАТОВ ---")
|
||||
print(f"С вероятностью 95% истинное среднее значение задержки ")
|
||||
print(f"находится в интервале от {results['ci_lower']:.2f} до {results['ci_upper']:.2f} мс.")
|
||||
print(
|
||||
f"находится в интервале от {results['ci_lower']:.2f} до {results['ci_upper']:.2f} мс.")
|
||||
print(f"Это означает, что если бы мы повторили эксперимент много раз,")
|
||||
print(f"95% вычисленных таким образом интервалов содержали бы истинное среднее значение.")
|
||||
Loading…
x
Reference in New Issue
Block a user