Skip to content

Commit

Permalink
add dual plot for weekday or weekends
Browse files Browse the repository at this point in the history
  • Loading branch information
zmek committed Nov 25, 2024
1 parent 719e3ab commit 7ac096c
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 67 deletions.
125 changes: 58 additions & 67 deletions notebooks/7_Modelling_to_show_true_demand.ipynb

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 188 additions & 0 deletions src/patientflow/viz/arrival_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,191 @@ def plot_cumulative_arrival_rates(
filename = f"{file_prefix}{clean_title_for_filename(title)}"
plt.savefig(media_file_path / filename, dpi=300)
plt.show()


def plot_dual_arrival_rates(
inpatient_arrivals_1,
inpatient_arrivals_2,
title,
labels=("Set 1", "Set 2"),
lagged_by=None,
curve_params=None,
time_interval=60,
start_plot_index=0,
x_margin=0.5,
file_prefix="",
media_file_path=None,
):
"""
Plot two sets of inpatient arrivals on the same chart for comparison.
When spread rates are present, they use the main colors while other rates are grey.
"""
# Calculate arrival rates for both sets
def process_dataset(arrivals):
rates_dict = time_varying_arrival_rates(arrivals, time_interval)
rates, hour_labels, hour_values = process_arrival_rates(rates_dict)

if lagged_by is not None:
lagged_dict = time_varying_arrival_rates_lagged(arrivals, lagged_by, time_interval)
rates_lagged, _, _ = process_arrival_rates(lagged_dict)
else:
rates_lagged = None

if curve_params is not None:
x1, y1, x2, y2 = curve_params
spread_dict = true_demand_by_hour(arrivals, x1, y1, x2, y2)
rates_spread, _, _ = process_arrival_rates(spread_dict)
else:
rates_spread = None

return rates, rates_lagged, rates_spread, hour_labels, hour_values

# Process both datasets
rates1, rates1_lagged, rates1_spread, hour_labels, hour_values = process_dataset(inpatient_arrivals_1)
rates2, rates2_lagged, rates2_spread, _, _ = process_dataset(inpatient_arrivals_2)

# Helper function for cyclic data
def get_cyclic_data(data):
return data[start_plot_index:] + data[0:start_plot_index]

# Create plot
plt.figure(figsize=(12, 7))
x_values = get_cyclic_data(hour_labels)

# Define main colors for each group
color1 = '#1f77b4' # Blue
color2 = '#9467bd' # Purple

# Define grey shades
grey1 = '#888888' # Darker grey
grey2 = '#BBBBBB' # Lighter grey

# Plot first dataset
y_values1 = get_cyclic_data(rates1)
if rates1_spread is not None and len(rates1_spread) > 0:
# Base arrival rates in grey
plt.plot(
x_values,
y_values1,
marker="x",
color=grey1,
markersize=4,
linestyle=":",
linewidth=1,
label=f"{labels[0]} - Arrivals",
)

# Lagged arrival rates in lighter grey
if rates1_lagged is not None:
plt.plot(
x_values,
get_cyclic_data(rates1_lagged),
marker="o",
markersize=4,
color=grey2,
linestyle="--",
linewidth=1,
label=f"{labels[0]} - Lagged arrivals",
)

# Spread arrival rates in main color
plt.plot(
x_values,
get_cyclic_data(rates1_spread),
marker="o",
color=color1,
label=f"{labels[0]} - Aspirational ({int(curve_params[1]*100)}% in {int(curve_params[0])}h)",
)
else:
# If no spread rates, use normal coloring
plt.plot(
x_values,
y_values1,
marker="o",
color=color1,
linestyle="-",
label=f"{labels[0]} - Arrivals",
)

if rates1_lagged is not None:
plt.plot(
x_values,
get_cyclic_data(rates1_lagged),
marker="s",
color=color1,
linestyle="--",
label=f"{labels[0]} - Lagged arrivals",
)

# Plot second dataset
y_values2 = get_cyclic_data(rates2)
if rates2_spread is not None and len(rates2_spread) > 0:
# Base arrival rates in grey
plt.plot(
x_values,
y_values2,
marker="x",
color=grey1,
markersize=4,
linestyle=":",
linewidth=1,
label=f"{labels[1]} - Arrivals",
)

# Lagged arrival rates in lighter grey
if rates2_lagged is not None:
plt.plot(
x_values,
get_cyclic_data(rates2_lagged),
marker="o",
markersize=4,
color=grey2,
linestyle="--",
linewidth=1,
label=f"{labels[1]} - Lagged arrivals",
)

# Spread arrival rates in main color
plt.plot(
x_values,
get_cyclic_data(rates2_spread),
marker="o",
color=color2,
label=f"{labels[1]} - Aspirational ({int(curve_params[1]*100)}% in {int(curve_params[0])}h)",
)
else:
# If no spread rates, use normal coloring
plt.plot(
x_values,
y_values2,
marker="o",
color=color2,
linestyle="-",
label=f"{labels[1]} - Arrivals",
)

if rates2_lagged is not None:
plt.plot(
x_values,
get_cyclic_data(rates2_lagged),
marker="s",
color=color2,
linestyle="--",
label=f"{labels[1]} - Lagged arrivals",
)

# Set plot parameters
plt.ylim(0, max(max(rates1), max(rates2)) + 0.25)
plt.xlim(hour_values[0] - x_margin, hour_values[-1] + x_margin)
plt.xlabel("Hour of day")
plt.ylabel("Arrival Rate (patients per hour)")
plt.title(title)
plt.grid(True, alpha=0.3)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()

if media_file_path:
filename = f"{file_prefix}{clean_title_for_filename(title)}"
plt.savefig(media_file_path / filename, dpi=300, bbox_inches='tight')

plt.show()

0 comments on commit 7ac096c

Please sign in to comment.