Skip to content

Commit ca0d3b6

Browse files
committed
reformat printing
1 parent 65c3445 commit ca0d3b6

36 files changed

Lines changed: 363 additions & 318 deletions

most_queue/io/plots.py

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from enum import Enum
66

77
import matplotlib.pyplot as plt
8+
from matplotlib.axes import Axes
89

910

1011
class DependsType(Enum):
@@ -18,60 +19,83 @@ class DependsType(Enum):
1819
COEFFICIENT_OF_VARIATION = 3
1920

2021

21-
def plot_sim_vs_calc_moments(
22-
xs: list[float],
23-
sim_results: list[float],
24-
calc_results: list[float],
25-
depends_on: DependsType = DependsType.CHANNELS_NUMBER,
26-
is_errors=False,
27-
is_waiting_time=True,
28-
save_path=None,
29-
): # pylint: disable=too-many-positional-arguments, too-many-arguments
22+
class Plotter:
3023
"""
31-
Plots the simulation and calculated moments for a given list of x values.
32-
:param xs: A list of x values (e.g., utilization factors or number of channels).
33-
:type xs: list[float]
34-
:param sim_results: A list of simulated results.
35-
:type sim_results: list[float]
36-
:param calc_results: A list of calculated results using some analytical or numerical method.
37-
:param depends_on: The type of dependency for plotting
38-
(utilization factor, number of channels, etc.).
39-
:type depends_on: DependsType
40-
:param is_errors: If True, plots the percentage error between
41-
simulation and calculation results.
42-
:type is_errors: bool
43-
:param is_waiting_time: If True, labels the y-axis as waiting time; otherwise, as sojourn time.
44-
:type is_waiting_time: bool
45-
:param save_path: The path where the plot should be saved. If None, the plot will not be saved.
46-
:type save_path: str or NoneType
47-
:return: None
24+
A class for plotting simulation results.
4825
"""
49-
_fig, ax = plt.subplots()
50-
if is_errors:
51-
errors = [100 * (w_sim - w_tt) / w_tt for w_sim, w_tt in zip(sim_results, calc_results)]
52-
53-
ax.plot(xs, errors, color="black")
54-
ax.set_ylabel(r"$\varepsilon$, %")
55-
else:
56-
ax.plot(xs, sim_results, label="Sim", color="black", linestyle="--")
57-
ax.plot(xs, calc_results, label="Calc", color="black")
58-
if is_waiting_time:
59-
ax.set_ylabel(r"$\omega_{1}$")
26+
27+
def __init__(
28+
self,
29+
xs: list[float],
30+
sim_results: list[float],
31+
calc_results: list[float],
32+
depends_on: DependsType = DependsType.CHANNELS_NUMBER,
33+
):
34+
self.xs = xs
35+
self.sim_results = sim_results
36+
self.calc_results = calc_results
37+
self.depends_on = depends_on
38+
39+
def _configure_plot(self, ax: Axes, ylabel: str):
40+
"""
41+
Helper method to configure common plot elements.
42+
"""
43+
ax.set_ylabel(ylabel)
44+
if self.depends_on == DependsType.UTILIZATION_FACTOR:
45+
ax.set_xlabel(r"$\rho$")
46+
elif self.depends_on == DependsType.COEFFICIENT_OF_VARIATION:
47+
ax.set_xlabel(r"$\nu$")
48+
else:
49+
ax.set_xlabel("n")
50+
plt.xticks(range(1, len(self.xs) + 1))
51+
52+
def plot_errors(self, save_path: str = None):
53+
"""
54+
Plots the simulation and calculated moments for a given list of x values.
55+
"""
56+
_fig, ax = plt.subplots()
57+
errors = [100 * (w_sim - w_tt) / w_tt for w_sim, w_tt in zip(self.sim_results, self.calc_results)]
58+
59+
ax.plot(self.xs, errors, color="black")
60+
self._configure_plot(ax, r"$\varepsilon$, %")
61+
62+
if save_path is not None:
63+
plt.savefig(save_path, dpi=300)
6064
else:
61-
ax.set_ylabel(r"$\upsilon_{1}$")
62-
plt.legend()
63-
64-
if depends_on == DependsType.UTILIZATION_FACTOR:
65-
ax.set_xlabel(r"$\rho$")
66-
elif depends_on == DependsType.COEFFICIENT_OF_VARIATION:
67-
ax.set_xlabel(r"$\nu$")
68-
else:
69-
ax.set_xlabel("n")
70-
plt.xticks(range(1, len(xs) + 1))
71-
72-
if save_path is not None:
73-
plt.savefig(save_path, dpi=300)
74-
else:
75-
plt.show()
76-
77-
plt.close(_fig)
65+
plt.show()
66+
67+
plt.close(_fig)
68+
69+
def plot_waiting(self, save_path: str = None):
70+
"""
71+
Plots the simulation and calculated queue waiting times.
72+
"""
73+
_fig, ax = plt.subplots()
74+
ax.plot(self.xs, self.sim_results, label="Sim", color="black", linestyle="--")
75+
ax.plot(self.xs, self.calc_results, label="Calc", color="black")
76+
ax.legend()
77+
self._configure_plot(ax, r"$\omega_{1}$")
78+
79+
if save_path is not None:
80+
plt.savefig(save_path, dpi=300)
81+
else:
82+
plt.show()
83+
84+
plt.close(_fig)
85+
86+
def plot_sojourn(self, save_path: str = None):
87+
"""
88+
Plots the simulation and calculated sojourn times.
89+
"""
90+
_fig, ax = plt.subplots()
91+
ax.plot(self.xs, self.sim_results, label="Sim", color="black", linestyle="--")
92+
ax.plot(self.xs, self.calc_results, label="Calc", color="black")
93+
ax.legend()
94+
self._configure_plot(ax, r"$\upsilon_{1}$")
95+
96+
if save_path is not None:
97+
plt.savefig(save_path, dpi=300)
98+
else:
99+
plt.show()
100+
101+
plt.close(_fig)

0 commit comments

Comments
 (0)