|
| 1 | +# anomaly_detector.py |
| 2 | +from typing import Dict, List, Optional |
| 3 | +import numpy as np |
| 4 | +from datetime import datetime |
| 5 | +import tensorflow as tf |
| 6 | +from google.cloud import storage |
| 7 | +import json |
| 8 | + |
| 9 | +class AnomalyDetector: |
| 10 | + def __init__(self, logger, bucket_name: str): |
| 11 | + self.logger = logger |
| 12 | + self.bucket_name = bucket_name |
| 13 | + self.client = storage.Client() |
| 14 | + self.bucket = self.client.bucket(bucket_name) |
| 15 | + |
| 16 | + # Define thresholds |
| 17 | + self.thresholds = { |
| 18 | + 'missing_labels_ratio': 0.05, |
| 19 | + 'corrupt_images_ratio': 0.02, |
| 20 | + 'min_image_size': 100 * 100, # minimum 100x100 pixels |
| 21 | + 'max_image_size': 1000 * 1000, # maximum 1000x1000 pixels |
| 22 | + 'class_imbalance_ratio': 0.1 # minimum 10% for any class |
| 23 | + } |
| 24 | + |
| 25 | + def check_data_completeness(self) -> Dict: |
| 26 | + """Check for missing data and labels""" |
| 27 | + try: |
| 28 | + self.logger.log_task_start("check_data_completeness") |
| 29 | + |
| 30 | + # Get all image files |
| 31 | + image_files = set( |
| 32 | + blob.name.split('/')[-1] |
| 33 | + for blob in self.bucket.list_blobs(prefix='raw/xray/') |
| 34 | + if blob.name.endswith(('.png', '.jpg', '.jpeg')) |
| 35 | + ) |
| 36 | + |
| 37 | + # Get all labeled files |
| 38 | + labels_blob = self.bucket.blob('raw/xray/labels.csv') |
| 39 | + labels_df = pd.read_csv(labels_blob.download_as_string()) |
| 40 | + labeled_files = set(labels_df['image_id'].values) |
| 41 | + |
| 42 | + # Calculate metrics |
| 43 | + metrics = { |
| 44 | + 'total_images': len(image_files), |
| 45 | + 'total_labeled': len(labeled_files), |
| 46 | + 'missing_labels': len(image_files - labeled_files), |
| 47 | + 'extra_labels': len(labeled_files - image_files), |
| 48 | + 'timestamp': datetime.now().isoformat() |
| 49 | + } |
| 50 | + |
| 51 | + # Check for anomalies |
| 52 | + missing_ratio = metrics['missing_labels'] / metrics['total_images'] |
| 53 | + if missing_ratio > self.thresholds['missing_labels_ratio']: |
| 54 | + self.logger.log_error( |
| 55 | + "data_completeness", |
| 56 | + f"High ratio of missing labels: {missing_ratio:.2%}", |
| 57 | + alert=True |
| 58 | + ) |
| 59 | + |
| 60 | + return metrics |
| 61 | + |
| 62 | + except Exception as e: |
| 63 | + self.logger.log_error("check_data_completeness", e) |
| 64 | + raise |
| 65 | + |
| 66 | + def check_image_quality(self, sample_size: int = 100) -> Dict: |
| 67 | + """Check image quality and format""" |
| 68 | + try: |
| 69 | + self.logger.log_task_start("check_image_quality") |
| 70 | + |
| 71 | + # Sample images |
| 72 | + blobs = list(self.bucket.list_blobs(prefix='raw/xray/'))[:sample_size] |
| 73 | + |
| 74 | + metrics = { |
| 75 | + 'corrupt_images': 0, |
| 76 | + 'invalid_dimensions': 0, |
| 77 | + 'invalid_format': 0, |
| 78 | + 'samples_checked': len(blobs) |
| 79 | + } |
| 80 | + |
| 81 | + for blob in blobs: |
| 82 | + try: |
| 83 | + # Try to decode image |
| 84 | + image_data = blob.download_as_bytes() |
| 85 | + image = tf.image.decode_image(image_data) |
| 86 | + |
| 87 | + # Check dimensions |
| 88 | + image_size = image.shape[0] * image.shape[1] |
| 89 | + if (image_size < self.thresholds['min_image_size'] or |
| 90 | + image_size > self.thresholds['max_image_size']): |
| 91 | + metrics['invalid_dimensions'] += 1 |
| 92 | + |
| 93 | + except Exception: |
| 94 | + metrics['corrupt_images'] += 1 |
| 95 | + |
| 96 | + # Calculate ratios |
| 97 | + metrics['corrupt_ratio'] = metrics['corrupt_images'] / metrics['samples_checked'] |
| 98 | + metrics['invalid_dim_ratio'] = metrics['invalid_dimensions'] / metrics['samples_checked'] |
| 99 | + |
| 100 | + # Check for anomalies |
| 101 | + if metrics['corrupt_ratio'] > self.thresholds['corrupt_images_ratio']: |
| 102 | + self.logger.log_error( |
| 103 | + "image_quality", |
| 104 | + f"High ratio of corrupt images: {metrics['corrupt_ratio']:.2%}", |
| 105 | + alert=True |
| 106 | + ) |
| 107 | + |
| 108 | + return metrics |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + self.logger.log_error("check_image_quality", e) |
| 112 | + raise |
| 113 | + |
| 114 | + def check_class_distribution(self) -> Dict: |
| 115 | + """Check for class imbalance""" |
| 116 | + try: |
| 117 | + self.logger.log_task_start("check_class_distribution") |
| 118 | + |
| 119 | + # Load labels |
| 120 | + labels_blob = self.bucket.blob('raw/xray/labels.csv') |
| 121 | + labels_df = pd.read_csv(labels_blob.download_as_string()) |
| 122 | + |
| 123 | + # Calculate class distribution |
| 124 | + class_dist = labels_df['label'].value_counts() |
| 125 | + total_samples = len(labels_df) |
| 126 | + |
| 127 | + metrics = { |
| 128 | + 'class_distribution': class_dist.to_dict(), |
| 129 | + 'class_ratios': (class_dist / total_samples).to_dict() |
| 130 | + } |
| 131 | + |
| 132 | + # Check for class imbalance |
| 133 | + for class_name, ratio in metrics['class_ratios'].items(): |
| 134 | + if ratio < self.thresholds['class_imbalance_ratio']: |
| 135 | + self.logger.log_error( |
| 136 | + "class_distribution", |
| 137 | + f"Class {class_name} is underrepresented: {ratio:.2%}", |
| 138 | + alert=True |
| 139 | + ) |
| 140 | + |
| 141 | + return metrics |
| 142 | + |
| 143 | + except Exception as e: |
| 144 | + self.logger.log_error("check_class_distribution", e) |
| 145 | + raise |
0 commit comments