feat: Implement dose calculation fix and enhance legend feature
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled

- Fixed dose calculation logic in `_calculate_daily_dose` to correctly parse timestamps with multiple colons.
- Added comprehensive test cases for various dose formats and edge cases in `test_dose_calculation.py`.
- Enhanced graph legend to display individual medicines with average dosages and track medicines without dose data.
- Updated legend styling and positioning for better readability and organization.
- Created new tests for enhanced legend functionality, including handling of medicines with and without data.
- Improved mocking for matplotlib components in tests to prevent TypeErrors.
This commit is contained in:
William Valentin
2025-07-30 14:22:07 -07:00
parent d14d19e7d9
commit b76191d66d
12 changed files with 1042 additions and 68 deletions

103
ENHANCED_LEGEND_FEATURE.md Normal file
View File

@@ -0,0 +1,103 @@
# Enhanced Graph Legend Feature
## Overview
Expanded the graph legend to display each medicine individually with enhanced formatting and additional information about tracked medicines.
## Changes Made
### 1. Enhanced Legend Display (`src/graph_manager.py`)
#### Legend Formatting Improvements:
- **Multi-column Layout**: Legend now displays in 2 columns for better space usage
- **Improved Positioning**: Positioned at upper left with proper bbox anchoring
- **Enhanced Styling**: Added frame, shadow, and transparency for better readability
- **Font Optimization**: Uses smaller font size to fit more information
#### Medicine-Specific Information:
- **Average Dosage Display**: Each medicine shows average dosage in the legend
- Format: `"Bupropion (avg: 125.5mg)"`
- Calculated from all days with non-zero doses
- **Color-Coded Entries**: Each medicine maintains its distinct color in the legend
- **Tracked Medicine Indicator**: Shows medicines that are toggled on but have no dose data
### 2. Legend Configuration Details
```python
self.ax.legend(
handles,
labels,
loc='upper left', # Position
bbox_to_anchor=(0, 1), # Anchor point
ncol=2, # 2 columns
fontsize='small', # Compact text
frameon=True, # Show frame
fancybox=True, # Rounded corners
shadow=True, # Drop shadow
framealpha=0.9 # Semi-transparent background
)
```
### 3. Data Tracking Enhancements
#### Medicine Categorization:
- **`medicines_with_data`**: Medicines with actual dose recordings
- **`medicines_without_data`**: Medicines toggled on but without dose data
#### Average Calculation:
```python
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)
```
## Features
### Enhanced Legend Display:
**Multi-column Layout**: Efficient use of graph space
**Medicine-Specific Info**: Average dosage displayed for each medicine
**Color Coding**: Consistent color scheme for easy identification
**Tracked Medicine Status**: Shows which medicines are being monitored
**Professional Styling**: Frame, shadow, and transparency effects
### Information Provided:
- **Symptom Data**: Depression, Anxiety, Sleep, Appetite with descriptive labels
- **Medicine Doses**: Each medicine with average dosage calculation
- **Tracking Status**: Indication of medicines being tracked but without current dose data
- **Visual Consistency**: Color-coded entries matching the graph elements
### Example Legend Entries:
```
Depression (0:good, 10:bad) Sleep (0:bad, 10:good)
Anxiety (0:good, 10:bad) Appetite (0:bad, 10:good)
Bupropion (avg: 225.0mg) Propranolol (avg: 12.5mg)
Tracked (no doses): hydroxyzine, gabapentin
```
## Benefits
### For Users:
- **Clear Identification**: Easy to see which medicines are displayed and their average doses
- **Data Context**: Understanding of dosage patterns at a glance
- **Tracking Awareness**: Knowledge of which medicines are being monitored
- **Professional Appearance**: Clean, organized legend that doesn't clutter the graph
### For Analysis:
- **Quick Reference**: Average doses visible without calculation
- **Pattern Recognition**: Color coding helps identify medicine effects
- **Data Completeness**: Clear indication of missing vs. present data
- **Visual Organization**: Structured layout for easy reading
## Technical Implementation
### Legend Components:
1. **Handles and Labels**: Retrieved from current plot elements
2. **Additional Info**: Dynamically added for medicines without data
3. **Dummy Handles**: Invisible rectangles for text-only legend entries
4. **Formatting**: Applied consistently across all legend elements
### Positioning Logic:
- **Upper Left**: Avoids interference with data plots
- **2-Column Layout**: Maximizes information density
- **Responsive**: Adjusts to available content
The enhanced legend provides comprehensive information about all displayed elements while maintaining a clean, professional appearance that enhances the overall user experience.