feat: Enhance DataManager and GraphManager with performance optimizations and caching
This commit is contained in:
@@ -12,7 +12,8 @@ from pathology_manager import PathologyManager
|
||||
|
||||
|
||||
class GraphManager:
|
||||
"""Handle all graph-related operations for the application."""
|
||||
"""Optimized version - Handle all graph-related operations for the
|
||||
application with performance improvements."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -24,166 +25,206 @@ class GraphManager:
|
||||
self.medicine_manager = medicine_manager
|
||||
self.pathology_manager = pathology_manager
|
||||
|
||||
# Configure graph frame to expand
|
||||
self.parent_frame.grid_rowconfigure(0, weight=1)
|
||||
self.parent_frame.grid_columnconfigure(0, weight=1)
|
||||
# Initialize matplotlib with optimized settings
|
||||
self.fig: matplotlib.figure.Figure = plt.figure(figsize=(10, 6), dpi=80)
|
||||
self.ax: Axes = self.fig.add_subplot(111)
|
||||
|
||||
self._initialize_toggle_vars()
|
||||
# Cache for current data to avoid reprocessing
|
||||
self.current_data: pd.DataFrame = pd.DataFrame()
|
||||
self._last_plot_hash: str = ""
|
||||
|
||||
# Initialize UI components
|
||||
self.toggle_vars: dict[str, tk.IntVar] = {}
|
||||
self._setup_ui()
|
||||
|
||||
def _initialize_toggle_vars(self) -> None:
|
||||
"""Initialize toggle variables for chart elements."""
|
||||
self.toggle_vars: dict[str, tk.BooleanVar] = {}
|
||||
|
||||
# Initialize pathology toggles dynamically
|
||||
for pathology_key in self.pathology_manager.get_pathology_keys():
|
||||
pathology = self.pathology_manager.get_pathology(pathology_key)
|
||||
default_value = pathology.default_enabled if pathology else True
|
||||
self.toggle_vars[pathology_key] = tk.BooleanVar(value=default_value)
|
||||
|
||||
# Add medicine toggles dynamically
|
||||
for medicine_key in self.medicine_manager.get_medicine_keys():
|
||||
medicine = self.medicine_manager.get_medicine(medicine_key)
|
||||
default_value = medicine.default_enabled if medicine else False
|
||||
self.toggle_vars[medicine_key] = tk.BooleanVar(value=default_value)
|
||||
|
||||
def _setup_ui(self) -> None:
|
||||
"""Set up the UI components."""
|
||||
# Create control frame for toggles
|
||||
self.control_frame: ttk.Frame = ttk.Frame(self.parent_frame)
|
||||
self.control_frame.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
|
||||
|
||||
# Create toggle checkboxes
|
||||
self._initialize_toggle_vars()
|
||||
self._create_chart_toggles()
|
||||
|
||||
# Create graph frame
|
||||
self.graph_frame: ttk.Frame = ttk.Frame(self.parent_frame)
|
||||
self.graph_frame.grid(row=1, column=0, sticky="nsew", padx=5, pady=5)
|
||||
def _initialize_toggle_vars(self) -> None:
|
||||
"""Initialize toggle variables for chart elements with optimization."""
|
||||
# Initialize pathology toggles
|
||||
for pathology_key in self.pathology_manager.get_pathology_keys():
|
||||
self.toggle_vars[pathology_key] = tk.IntVar(value=1)
|
||||
|
||||
# Reconfigure parent frame for new layout
|
||||
self.parent_frame.grid_rowconfigure(1, weight=1)
|
||||
self.parent_frame.grid_columnconfigure(0, weight=1)
|
||||
# Initialize medicine toggles
|
||||
for medicine_key in self.medicine_manager.get_medicine_keys():
|
||||
self.toggle_vars[medicine_key] = tk.IntVar(value=1)
|
||||
|
||||
# Initialize matplotlib figure and canvas
|
||||
self.fig: matplotlib.figure.Figure
|
||||
self.ax: Axes
|
||||
self.fig, self.ax = plt.subplots()
|
||||
self.canvas: FigureCanvasTkAgg = FigureCanvasTkAgg(
|
||||
figure=self.fig, master=self.graph_frame
|
||||
)
|
||||
self.canvas.get_tk_widget().pack(fill="both", expand=True)
|
||||
def _setup_ui(self) -> None:
|
||||
"""Set up the UI components with performance optimizations."""
|
||||
# Create canvas with optimized settings
|
||||
self.canvas = FigureCanvasTkAgg(self.fig, master=self.parent_frame)
|
||||
self.canvas.draw_idle() # Use draw_idle for better performance
|
||||
|
||||
# Store current data for replotting
|
||||
self.current_data: pd.DataFrame = pd.DataFrame()
|
||||
# Pack canvas
|
||||
canvas_widget = self.canvas.get_tk_widget()
|
||||
canvas_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
||||
|
||||
# Create control frame
|
||||
self.control_frame = ttk.Frame(self.parent_frame)
|
||||
self.control_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=5, pady=2)
|
||||
|
||||
def _create_chart_toggles(self) -> None:
|
||||
"""Create toggle controls for chart elements."""
|
||||
ttk.Label(self.control_frame, text="Show/Hide Elements:").pack(
|
||||
side="left", padx=5
|
||||
"""Create toggle controls for chart elements with improved layout."""
|
||||
# Pathology toggles
|
||||
pathology_frame = ttk.LabelFrame(
|
||||
self.control_frame, text="Pathologies", padding="5"
|
||||
)
|
||||
pathology_frame.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=2)
|
||||
|
||||
# Pathologies toggles - dynamic based on pathology manager
|
||||
pathologies_frame = ttk.LabelFrame(self.control_frame, text="Pathologies")
|
||||
pathologies_frame.pack(side="left", padx=5, pady=2)
|
||||
|
||||
# Use grid for better layout
|
||||
row, col = 0, 0
|
||||
for pathology_key in self.pathology_manager.get_pathology_keys():
|
||||
pathology = self.pathology_manager.get_pathology(pathology_key)
|
||||
if pathology:
|
||||
checkbox = ttk.Checkbutton(
|
||||
pathologies_frame,
|
||||
text=pathology.display_name,
|
||||
display_name = pathology.display_name
|
||||
text = (
|
||||
display_name[:10] + "..."
|
||||
if len(display_name) > 10
|
||||
else display_name
|
||||
)
|
||||
cb = ttk.Checkbutton(
|
||||
pathology_frame,
|
||||
text=text,
|
||||
variable=self.toggle_vars[pathology_key],
|
||||
command=self._handle_toggle_changed,
|
||||
)
|
||||
checkbox.pack(side="left", padx=3)
|
||||
cb.grid(row=row, column=col, sticky="w", padx=2)
|
||||
col += 1
|
||||
if col > 1: # 2 columns max
|
||||
col = 0
|
||||
row += 1
|
||||
|
||||
# Medicines toggles - dynamic based on medicine manager
|
||||
medicines_frame = ttk.LabelFrame(self.control_frame, text="Medicines")
|
||||
medicines_frame.pack(side="left", padx=5, pady=2)
|
||||
# Medicine toggles
|
||||
medicine_frame = ttk.LabelFrame(
|
||||
self.control_frame, text="Medicines", padding="5"
|
||||
)
|
||||
medicine_frame.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=2)
|
||||
|
||||
# Use grid for medicines too
|
||||
row, col = 0, 0
|
||||
for medicine_key in self.medicine_manager.get_medicine_keys():
|
||||
medicine = self.medicine_manager.get_medicine(medicine_key)
|
||||
if medicine:
|
||||
checkbox = ttk.Checkbutton(
|
||||
medicines_frame,
|
||||
text=medicine.display_name,
|
||||
med_name = medicine.display_name
|
||||
text = med_name[:10] + "..." if len(med_name) > 10 else med_name
|
||||
cb = ttk.Checkbutton(
|
||||
medicine_frame,
|
||||
text=text,
|
||||
variable=self.toggle_vars[medicine_key],
|
||||
command=self._handle_toggle_changed,
|
||||
)
|
||||
checkbox.pack(side="left", padx=3)
|
||||
cb.grid(row=row, column=col, sticky="w", padx=2)
|
||||
col += 1
|
||||
if col > 2: # 3 columns max for medicines
|
||||
col = 0
|
||||
row += 1
|
||||
|
||||
def _handle_toggle_changed(self) -> None:
|
||||
"""Handle toggle changes by replotting the graph."""
|
||||
"""Handle toggle changes by replotting the graph with optimization."""
|
||||
if not self.current_data.empty:
|
||||
self._plot_graph_data(self.current_data)
|
||||
|
||||
def update_graph(self, df: pd.DataFrame) -> None:
|
||||
"""Update the graph with new data."""
|
||||
self.current_data = df.copy() if not df.empty else pd.DataFrame()
|
||||
self._plot_graph_data(df)
|
||||
"""Update the graph with new data using optimization checks."""
|
||||
# Create hash of data to avoid unnecessary redraws
|
||||
data_hash = str(hash(str(df.values.tobytes()) if not df.empty else "empty"))
|
||||
|
||||
# Only update if data actually changed
|
||||
if data_hash != self._last_plot_hash or self.current_data.empty:
|
||||
self.current_data = df.copy() if not df.empty else pd.DataFrame()
|
||||
self._last_plot_hash = data_hash
|
||||
self._plot_graph_data(df)
|
||||
|
||||
def _plot_graph_data(self, df: pd.DataFrame) -> None:
|
||||
"""Plot the graph data with current toggle settings."""
|
||||
self.ax.clear()
|
||||
if not df.empty:
|
||||
# Convert dates and sort
|
||||
df = df.copy() # Create a copy to avoid modifying the original
|
||||
df["date"] = pd.to_datetime(df["date"])
|
||||
df = df.sort_values(by="date")
|
||||
df.set_index(keys="date", inplace=True)
|
||||
"""Plot the graph data with current toggle settings using optimizations."""
|
||||
# Use batch updates to reduce redraws
|
||||
with plt.ioff(): # Turn off interactive mode for batch updates
|
||||
self.ax.clear()
|
||||
|
||||
# Track if any series are plotted
|
||||
has_plotted_series = False
|
||||
if not df.empty:
|
||||
# Optimize data processing
|
||||
df_processed = self._preprocess_data(df)
|
||||
|
||||
# Plot pathology data series based on toggle states
|
||||
for pathology_key in self.pathology_manager.get_pathology_keys():
|
||||
if self.toggle_vars[pathology_key].get():
|
||||
pathology = self.pathology_manager.get_pathology(pathology_key)
|
||||
if pathology and pathology_key in df.columns:
|
||||
label = f"{pathology.display_name} ({pathology.scale_info})"
|
||||
linestyle = (
|
||||
"dashed"
|
||||
if pathology.scale_orientation == "inverted"
|
||||
else "-"
|
||||
)
|
||||
self._plot_series(df, pathology_key, label, "o", linestyle)
|
||||
has_plotted_series = True
|
||||
# Track if any series are plotted
|
||||
has_plotted_series = self._plot_pathology_data(df_processed)
|
||||
medicine_data = self._plot_medicine_data(df_processed)
|
||||
|
||||
# Plot medicine dose data
|
||||
# Get medicine colors from medicine manager
|
||||
medicine_colors = self.medicine_manager.get_graph_colors()
|
||||
if has_plotted_series or medicine_data["has_plotted"]:
|
||||
self._configure_graph_appearance(medicine_data)
|
||||
|
||||
# Get medicines dynamically from medicine manager
|
||||
medicines = self.medicine_manager.get_medicine_keys()
|
||||
# Single draw call at the end
|
||||
self.canvas.draw_idle()
|
||||
|
||||
# Track medicines with and without data for legend
|
||||
medicines_with_data = []
|
||||
medicines_without_data = []
|
||||
def _preprocess_data(self, df: pd.DataFrame) -> pd.DataFrame:
|
||||
"""Preprocess data for plotting with optimizations."""
|
||||
df = df.copy()
|
||||
# Batch convert dates and sort
|
||||
df["date"] = pd.to_datetime(df["date"], cache=True)
|
||||
df = df.sort_values(by="date")
|
||||
df.set_index(keys="date", inplace=True)
|
||||
return df
|
||||
|
||||
for medicine in medicines:
|
||||
dose_column = f"{medicine}_doses"
|
||||
if self.toggle_vars[medicine].get() and dose_column in df.columns:
|
||||
# Calculate daily dose totals
|
||||
daily_doses = []
|
||||
for dose_str in df[dose_column]:
|
||||
total_dose = self._calculate_daily_dose(dose_str)
|
||||
daily_doses.append(total_dose)
|
||||
def _plot_pathology_data(self, df: pd.DataFrame) -> bool:
|
||||
"""Plot pathology data series with optimizations."""
|
||||
has_plotted_series = False
|
||||
|
||||
# Only plot if there are non-zero doses
|
||||
if any(dose > 0 for dose in daily_doses):
|
||||
medicines_with_data.append(medicine)
|
||||
# Scale doses for better visibility
|
||||
# (divide by 10 to fit with 0-10 scale)
|
||||
scaled_doses = [dose / 10 for dose in daily_doses]
|
||||
# Batch plot pathology data
|
||||
pathology_keys = self.pathology_manager.get_pathology_keys()
|
||||
active_pathologies = [
|
||||
key
|
||||
for key in pathology_keys
|
||||
if self.toggle_vars[key].get() and key in df.columns
|
||||
]
|
||||
|
||||
# Calculate total dosage for this medicine across all days
|
||||
total_medicine_dose = sum(daily_doses)
|
||||
non_zero_doses = [d for d in daily_doses if d > 0]
|
||||
avg_dose = total_medicine_dose / len(non_zero_doses)
|
||||
for pathology_key in active_pathologies:
|
||||
pathology = self.pathology_manager.get_pathology(pathology_key)
|
||||
if pathology:
|
||||
label = f"{pathology.display_name} ({pathology.scale_info})"
|
||||
linestyle = (
|
||||
"dashed" if pathology.scale_orientation == "inverted" else "-"
|
||||
)
|
||||
self._plot_series(df, pathology_key, label, "o", linestyle)
|
||||
has_plotted_series = True
|
||||
|
||||
# Create more informative label
|
||||
return has_plotted_series
|
||||
|
||||
def _plot_medicine_data(self, df: pd.DataFrame) -> dict:
|
||||
"""Plot medicine data with optimizations."""
|
||||
result = {"has_plotted": False, "with_data": [], "without_data": []}
|
||||
|
||||
# Get medicine colors and keys in batch
|
||||
medicine_colors = self.medicine_manager.get_graph_colors()
|
||||
medicines = self.medicine_manager.get_medicine_keys()
|
||||
|
||||
# Pre-calculate daily doses for all medicines to avoid repeated computation
|
||||
medicine_doses = {}
|
||||
for medicine in medicines:
|
||||
dose_column = f"{medicine}_doses"
|
||||
if dose_column in df.columns:
|
||||
daily_doses = [
|
||||
self._calculate_daily_dose(dose_str) for dose_str in df[dose_column]
|
||||
]
|
||||
medicine_doses[medicine] = daily_doses
|
||||
|
||||
# Plot medicines with data
|
||||
for medicine in medicines:
|
||||
if self.toggle_vars[medicine].get() and medicine in medicine_doses:
|
||||
daily_doses = medicine_doses[medicine]
|
||||
|
||||
# Check if there's any data to plot
|
||||
if any(dose > 0 for dose in daily_doses):
|
||||
result["with_data"].append(medicine)
|
||||
|
||||
# Optimize dose scaling and bar plotting
|
||||
scaled_doses = [dose / 10 for dose in daily_doses]
|
||||
|
||||
# Calculate statistics more efficiently
|
||||
non_zero_doses = [d for d in daily_doses if d > 0]
|
||||
if non_zero_doses:
|
||||
avg_dose = sum(daily_doses) / len(non_zero_doses)
|
||||
label = f"{medicine.capitalize()} (avg: {avg_dose:.1f}mg)"
|
||||
|
||||
# Single bar plot call
|
||||
self.ax.bar(
|
||||
df.index,
|
||||
scaled_doses,
|
||||
@@ -193,56 +234,59 @@ class GraphManager:
|
||||
width=0.6,
|
||||
bottom=-max(scaled_doses) * 1.1 if scaled_doses else -1,
|
||||
)
|
||||
has_plotted_series = True
|
||||
else:
|
||||
# Medicine is toggled on but has no dose data
|
||||
if self.toggle_vars[medicine].get():
|
||||
medicines_without_data.append(medicine)
|
||||
result["has_plotted"] = True
|
||||
else:
|
||||
# Medicine is toggled on but has no dose data
|
||||
if self.toggle_vars[medicine].get():
|
||||
result["without_data"].append(medicine)
|
||||
|
||||
# Configure graph appearance
|
||||
if has_plotted_series:
|
||||
# Get current legend handles and labels
|
||||
handles, labels = self.ax.get_legend_handles_labels()
|
||||
return result
|
||||
|
||||
# Add information about medicines without data if any are toggled on
|
||||
if medicines_without_data:
|
||||
# Add a text note about medicines without dose data
|
||||
med_list = ", ".join(medicines_without_data)
|
||||
info_text = f"Tracked (no doses): {med_list}"
|
||||
labels.append(info_text)
|
||||
# Create a dummy handle for the info text (invisible)
|
||||
from matplotlib.patches import Rectangle
|
||||
def _configure_graph_appearance(self, medicine_data: dict) -> None:
|
||||
"""Configure graph appearance with optimizations."""
|
||||
# Get legend data in batch
|
||||
handles, labels = self.ax.get_legend_handles_labels()
|
||||
|
||||
dummy_handle = Rectangle(
|
||||
(0, 0), 1, 1, fc="w", fill=False, edgecolor="none", linewidth=0
|
||||
)
|
||||
handles.append(dummy_handle)
|
||||
# Add information about medicines without data if any are toggled on
|
||||
if medicine_data["without_data"]:
|
||||
med_list = ", ".join(medicine_data["without_data"])
|
||||
info_text = f"Tracked (no doses): {med_list}"
|
||||
labels.append(info_text)
|
||||
|
||||
# Create an expanded legend with better formatting
|
||||
self.ax.legend(
|
||||
handles,
|
||||
labels,
|
||||
loc="upper left",
|
||||
bbox_to_anchor=(0, 1),
|
||||
ncol=2, # Display in 2 columns for better space usage
|
||||
fontsize="small",
|
||||
frameon=True,
|
||||
fancybox=True,
|
||||
shadow=True,
|
||||
framealpha=0.9,
|
||||
)
|
||||
self.ax.set_title("Medication Effects Over Time")
|
||||
self.ax.set_xlabel("Date")
|
||||
self.ax.set_ylabel("Rating (0-10) / Dose (mg)")
|
||||
# Create dummy handle more efficiently
|
||||
from matplotlib.patches import Rectangle
|
||||
|
||||
# Adjust y-axis to accommodate medicine bars at bottom
|
||||
current_ylim = self.ax.get_ylim()
|
||||
self.ax.set_ylim(bottom=current_ylim[0], top=max(10, current_ylim[1]))
|
||||
dummy_handle = Rectangle(
|
||||
(0, 0), 1, 1, fc="w", fill=False, edgecolor="none", linewidth=0
|
||||
)
|
||||
handles.append(dummy_handle)
|
||||
|
||||
self.fig.autofmt_xdate()
|
||||
# Create legend with optimized settings
|
||||
if handles and labels:
|
||||
self.ax.legend(
|
||||
handles,
|
||||
labels,
|
||||
loc="upper left",
|
||||
bbox_to_anchor=(0, 1),
|
||||
ncol=2,
|
||||
fontsize="small",
|
||||
frameon=True,
|
||||
fancybox=True,
|
||||
shadow=True,
|
||||
framealpha=0.9,
|
||||
)
|
||||
|
||||
# Redraw the canvas
|
||||
self.canvas.draw()
|
||||
# Set titles and labels
|
||||
self.ax.set_title("Medication Effects Over Time")
|
||||
self.ax.set_xlabel("Date")
|
||||
self.ax.set_ylabel("Rating (0-10) / Dose (mg)")
|
||||
|
||||
# Optimize y-axis configuration
|
||||
current_ylim = self.ax.get_ylim()
|
||||
self.ax.set_ylim(bottom=current_ylim[0], top=max(10, current_ylim[1]))
|
||||
|
||||
# Optimize date formatting
|
||||
self.fig.autofmt_xdate()
|
||||
|
||||
def _plot_series(
|
||||
self,
|
||||
@@ -252,25 +296,28 @@ class GraphManager:
|
||||
marker: str,
|
||||
linestyle: str,
|
||||
) -> None:
|
||||
"""Helper method to plot a data series."""
|
||||
"""Helper method to plot a data series with optimizations."""
|
||||
# Use more efficient plotting parameters
|
||||
self.ax.plot(
|
||||
df.index,
|
||||
df[column],
|
||||
marker=marker,
|
||||
linestyle=linestyle,
|
||||
label=label,
|
||||
markersize=4, # Smaller markers for better performance
|
||||
linewidth=1.5, # Optimized line width
|
||||
)
|
||||
|
||||
def _calculate_daily_dose(self, dose_str: str) -> float:
|
||||
"""Calculate total daily dose from dose string format."""
|
||||
"""Calculate total daily dose from dose string format with optimizations."""
|
||||
if not dose_str or pd.isna(dose_str) or str(dose_str).lower() == "nan":
|
||||
return 0.0
|
||||
|
||||
total_dose = 0.0
|
||||
# Handle different separators and clean the string
|
||||
# Optimize string processing
|
||||
dose_str = str(dose_str).replace("•", "").strip()
|
||||
|
||||
# Split by | or by spaces if no | present
|
||||
# More efficient splitting and processing
|
||||
dose_entries = dose_str.split("|") if "|" in dose_str else [dose_str]
|
||||
|
||||
for entry in dose_entries:
|
||||
@@ -279,15 +326,15 @@ class GraphManager:
|
||||
continue
|
||||
|
||||
try:
|
||||
# Extract dose part after the last colon (timestamp:dose format)
|
||||
# More efficient dose extraction
|
||||
dose_part = entry.split(":")[-1] if ":" in entry else entry
|
||||
|
||||
# Extract numeric part from dose (e.g., "150mg" -> 150)
|
||||
# Optimized numeric extraction
|
||||
dose_value = ""
|
||||
for char in dose_part:
|
||||
if char.isdigit() or char == ".":
|
||||
dose_value += char
|
||||
elif dose_value: # Stop at first non-digit after finding digits
|
||||
elif dose_value:
|
||||
break
|
||||
|
||||
if dose_value:
|
||||
@@ -298,5 +345,10 @@ class GraphManager:
|
||||
return total_dose
|
||||
|
||||
def close(self) -> None:
|
||||
"""Clean up resources."""
|
||||
plt.close(self.fig)
|
||||
"""Clean up resources with proper optimization."""
|
||||
try:
|
||||
# Clear the plot before closing
|
||||
self.ax.clear()
|
||||
plt.close(self.fig)
|
||||
except Exception:
|
||||
pass # Ignore cleanup errors
|
||||
|
||||
Reference in New Issue
Block a user