|
| 1 | +# biosensors/utils.py |
| 2 | + |
| 3 | +import logging |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +# Configure logging |
| 7 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 8 | + |
| 9 | +def format_sensor_data(sensor_id, data): |
| 10 | + """ |
| 11 | + Format the sensor data into a readable string. |
| 12 | + |
| 13 | + :param sensor_id: The ID of the sensor. |
| 14 | + :param data: Dictionary containing sensor data. |
| 15 | + :return: Formatted string representation of the sensor data. |
| 16 | + """ |
| 17 | + try: |
| 18 | + formatted_data = f"Sensor ID: {sensor_id}\n" |
| 19 | + formatted_data += f"Temperature: {data['temperature']} °C\n" |
| 20 | + formatted_data += f"Heart Rate: {data['heart_rate']} BPM\n" |
| 21 | + formatted_data += f"Timestamp: {format_timestamp(data['timestamp'])}\n" |
| 22 | + return formatted_data |
| 23 | + except KeyError as e: |
| 24 | + logging.error(f"Missing key in data for sensor {sensor_id}: {e}") |
| 25 | + return None |
| 26 | + |
| 27 | +def format_timestamp(timestamp): |
| 28 | + """ |
| 29 | + Convert a timestamp to a human-readable format. |
| 30 | + |
| 31 | + :param timestamp: The timestamp to format. |
| 32 | + :return: Formatted timestamp string. |
| 33 | + """ |
| 34 | + return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') |
| 35 | + |
| 36 | +def log_error(message): |
| 37 | + """ |
| 38 | + Log an error message. |
| 39 | + |
| 40 | + :param message: The error message to log. |
| 41 | + """ |
| 42 | + logging.error(message) |
| 43 | + |
| 44 | +def log_info(message): |
| 45 | + """ |
| 46 | + Log an informational message. |
| 47 | + |
| 48 | + :param message: The informational message to log. |
| 49 | + """ |
| 50 | + logging.info(message) |
| 51 | + |
| 52 | +def calculate_average(data_list): |
| 53 | + """ |
| 54 | + Calculate the average of a list of numerical values. |
| 55 | + |
| 56 | + :param data_list: List of numerical values. |
| 57 | + :return: Average value. |
| 58 | + """ |
| 59 | + if not data_list: |
| 60 | + logging.warning("Empty data list provided for average calculation.") |
| 61 | + return None |
| 62 | + return sum(data_list) / len(data_list) |
| 63 | + |
| 64 | +def calculate_standard_deviation(data_list): |
| 65 | + """ |
| 66 | + Calculate the standard deviation of a list of numerical values. |
| 67 | + |
| 68 | + :param data_list: List of numerical values. |
| 69 | + :return: Standard deviation value. |
| 70 | + """ |
| 71 | + if not data_list: |
| 72 | + logging.warning("Empty data list provided for standard deviation calculation.") |
| 73 | + return None |
| 74 | + mean = calculate_average(data_list) |
| 75 | + variance = sum((x - mean) ** 2 for x in data_list) / len(data_list) |
| 76 | + return variance ** 0.5 |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + # Example usage |
| 80 | + sensor_id = "sensor_1" |
| 81 | + sensor_data = { |
| 82 | + "temperature": 36.5, |
| 83 | + "heart_rate": 75, |
| 84 | + "timestamp": 1633072800 # Example timestamp |
| 85 | + } |
| 86 | + |
| 87 | + formatted_data = format_sensor_data(sensor_id, sensor_data) |
| 88 | + if formatted_data: |
| 89 | + print(formatted_data) |
| 90 | + |
| 91 | + average_temp = calculate_average([36.5, 37.0, 38.2]) |
| 92 | + print(f"Average Temperature: {average_temp} °C") |
| 93 | + |
| 94 | + std_dev_hr = calculate_standard_deviation([75, 80, 90]) |
| 95 | + print(f"Standard Deviation of Heart Rate: {std_dev_hr} BPM") |
0 commit comments