|
| 1 | +# biosensors/data_processing.py |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import logging |
| 5 | + |
| 6 | +# Configure logging |
| 7 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 8 | + |
| 9 | +class DataProcessor: |
| 10 | + def __init__(self): |
| 11 | + """ |
| 12 | + Initialize the DataProcessor. |
| 13 | + """ |
| 14 | + self.processed_data = {} |
| 15 | + |
| 16 | + def validate_data(self, raw_data): |
| 17 | + """ |
| 18 | + Validate the raw data from biosensors. |
| 19 | + |
| 20 | + :param raw_data: Dictionary containing raw data from sensors. |
| 21 | + :return: Boolean indicating whether the data is valid. |
| 22 | + """ |
| 23 | + for sensor_id, data in raw_data.items(): |
| 24 | + if not isinstance(data, dict): |
| 25 | + logging.warning(f"Invalid data format for sensor {sensor_id}. Expected a dictionary.") |
| 26 | + return False |
| 27 | + |
| 28 | + if 'temperature' not in data or 'heart_rate' not in data: |
| 29 | + logging.warning(f"Missing required fields in data from sensor {sensor_id}.") |
| 30 | + return False |
| 31 | + |
| 32 | + if not (30 <= data['temperature'] <= 45): # Example temperature range in Celsius |
| 33 | + logging.warning(f"Temperature out of range for sensor {sensor_id}: {data['temperature']}") |
| 34 | + return False |
| 35 | + |
| 36 | + if not (40 <= data['heart_rate'] <= 180): # Example heart rate range in BPM |
| 37 | + logging.warning(f"Heart rate out of range for sensor {sensor_id}: {data['heart_rate']}") |
| 38 | + return False |
| 39 | + |
| 40 | + return True |
| 41 | + |
| 42 | + def normalize_data(self, raw_data): |
| 43 | + """ |
| 44 | + Normalize the raw data for further analysis. |
| 45 | + |
| 46 | + :param raw_data: Dictionary containing raw data from sensors. |
| 47 | + :return: Dictionary containing normalized data. |
| 48 | + """ |
| 49 | + normalized_data = {} |
| 50 | + for sensor_id, data in raw_data.items(): |
| 51 | + normalized_data[sensor_id] = { |
| 52 | + "temperature": self._normalize(data['temperature'], 30, 45), # Normalize temperature |
| 53 | + "heart_rate": self._normalize(data['heart_rate'], 40, 180) # Normalize heart rate |
| 54 | + } |
| 55 | + return normalized_data |
| 56 | + |
| 57 | + def _normalize(self, value, min_value, max_value): |
| 58 | + """ |
| 59 | + Normalize a value to a range of 0 to 1. |
| 60 | + |
| 61 | + :param value: The value to normalize. |
| 62 | + :param min_value: The minimum value of the range. |
| 63 | + :param max_value: The maximum value of the range. |
| 64 | + :return: Normalized value. |
| 65 | + """ |
| 66 | + return (value - min_value) / (max_value - min_value) |
| 67 | + |
| 68 | + def analyze_data(self, normalized_data): |
| 69 | + """ |
| 70 | + Perform basic statistical analysis on the normalized data. |
| 71 | + |
| 72 | + :param normalized_data: Dictionary containing normalized data from sensors. |
| 73 | + :return: Dictionary containing statistical analysis results. |
| 74 | + """ |
| 75 | + analysis_results = {} |
| 76 | + for sensor_id, data in normalized_data.items(): |
| 77 | + analysis_results[sensor_id] = { |
| 78 | + "temperature_mean": np.mean(data['temperature']), |
| 79 | + "heart_rate_mean": np.mean(data['heart_rate']), |
| 80 | + "temperature_std": np.std(data['temperature']), |
| 81 | + "heart_rate_std": np.std(data['heart_rate']) |
| 82 | + } |
| 83 | + return analysis_results |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + # Example usage |
| 87 | + raw_data = { |
| 88 | + "sensor_1": {"temperature": 36.5, "heart_rate": 75}, |
| 89 | + "sensor_2": {"temperature": 37.0, "heart_rate": 80}, |
| 90 | + "sensor_3": {"temperature": 38.2, "heart_rate": 90} |
| 91 | + } |
| 92 | + |
| 93 | + processor = DataProcessor() |
| 94 | + |
| 95 | + if processor.validate_data(raw_data): |
| 96 | + normalized_data = processor.normalize_data(raw_data) |
| 97 | + analysis_results = processor.analyze_data(normalized_data) |
| 98 | + print("Normalized Data:", normalized_data) |
| 99 | + print("Analysis Results:", analysis_results) |
| 100 | + else: |
| 101 | + logging.error("Raw data validation failed.") |
0 commit comments