A tool to estimate calories burned from fitness files.
file_to_calories.py: Main script for batch processing.fitfiles in thefitfiles/directory, using user configuration fromconfig.json.cardio_calculator.py: Command-line calculator for solving the Keytel et al. formula for calories, heart rate, weight, or age.utils.py: Shared utility functions for calorie calculations and configuration loading.logger.py: Centralized logging configuration.config.json: Stores user-specific parameters (weight_kg,age_years,gender).fitfiles/: Directory for.fitfiles to be analyzed.test_file_to_calories.py: Unit tests forfile_to_calories.py(uses pytest and unittest.mock).test_cardio_calculator.py: Unit tests forcardio_calculator.py(uses pytest).
The codebase has been refactored to eliminate code duplication through a modular architecture:
- Utils Module: The
utils.pymodule serves as a central repository for shared functionality:- Defines constants for both male and female Keytel formula calculations
- Provides unified calorie calculation functions used by both main scripts
- Includes configuration loading functionality
- Contains formula solvers for different variables (calories, heart rate, weight, age)
The calorie calculation formulas have been consolidated in the utils.py module:
-
Formula Constants: Male and female formula constants are defined as dictionaries:
MALE_CONSTANTS = { 'base': -55.0969, 'hr_coef': 0.6309, 'weight_coef': 0.1988, 'age_coef': 0.2017, 'conversion': 4.184 } FEMALE_CONSTANTS = { 'base': -20.4022, 'hr_coef': 0.4472, 'weight_coef': -0.1263, 'age_coef': 0.074, 'conversion': 4.184 }
-
Unified Calculation Functions: A single implementation handles both male and female formulas:
def calculate_kcal_per_min(hr, weight, age, gender='male'): constants = FEMALE_CONSTANTS if gender.lower() == 'female' else MALE_CONSTANTS return (constants['base'] + (constants['hr_coef'] * hr) + (constants['weight_coef'] * weight) + (constants['age_coef'] * age)) / constants['conversion']
The refactored code now provides improved support for both male and female formulas:
- All calculation functions accept a
genderparameter (defaulting to 'male') - The appropriate constants are selected based on the gender parameter
- The negative weight coefficient for females is properly handled
-
Install dependencies:
pip install fitparse pytest
-
To process all
.fitfiles in thefitfiles/directory:python file_to_calories.py
Results will be printed for each file.
-
To use the interactive calculator:
python cardio_calculator.py
Enter values for heart rate, weight, age, or kcal/min (leave one blank to solve for it).
Edit config.json to set your parameters:
weight_kg: User's weight in kilogramsage_years: User's age in yearsgender: User's gender (maleorfemale)
-
Run all tests from the project root with:
pytest
-
Tests are located in
test_file_to_calories.pyandtest_cardio_calculator.py.
See LICENSE for details.
The project implements a robust error handling system with custom exception classes:
-
FIT File Processing Exceptions:
FitFileError: Base exception for FIT file processing errorsInvalidFitFileError: Raised when a FIT file is invalid or corruptedMissingDataError: Raised when required data is missing from a FIT fileConfigError: Raised for configuration-related errors
-
Calculator Exceptions:
CardioCalculatorError: Base exception for cardio calculator errorsInputValidationError: Raised when input validation failsCalculationError: Raised when a calculation fails
The system performs comprehensive input validation:
- Type Checking: Ensures inputs are of the correct data type
- Range Validation: Validates that values fall within physiologically reasonable ranges:
- Heart rate: positive and ≤ 250 bpm
- Weight: positive and ≤ 500 kg
- Age: positive and ≤ 130 years
- Calories: non-negative and ≤ 100 kcal/min
- Format Validation: Ensures strings like gender are in the expected format ('male' or 'female')
- File Validation: Checks file existence, permissions, and format before processing
The project follows these error handling principles:
- Specific Exceptions: Uses specific exception types for different error scenarios
- Detailed Messages: Provides clear error messages that explain what went wrong
- Exception Chaining: Preserves original exception context using the
fromkeyword - Comprehensive Logging: Logs errors at appropriate levels (warning, error, critical)
- Graceful Degradation: Falls back to default values when configuration is invalid
- User-Friendly Feedback: Displays informative error messages to the user
When using the tools, you might encounter these errors:
- Configuration Errors: Check that your
config.jsonfile exists and contains valid values forweight_kg,age_years, andgender - File Errors: Ensure your FIT files exist, are readable, and are valid FIT format
- Data Errors: Verify that your FIT files contain heart rate data
- Input Errors: When using the calculator, ensure inputs are within reasonable physiological ranges
The system will provide specific error messages to help diagnose and fix these issues. For more detailed information, check the log file generated by the logger.