1use super::*;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5pub struct MedicalImaging {
7 image_acquisition: ImageAcquisition,
8 image_processing: ImageProcessing,
9 image_analysis: ImageAnalysis,
10 image_storage: ImageStorage,
11}
12
13pub struct ImageAcquisition {
15 acquisition_protocols: HashMap<String, AcquisitionProtocol>,
16 quality_control: QualityControl,
17}
18
19#[derive(Debug, Clone)]
21pub struct AcquisitionProtocol {
22 pub protocol_id: String,
23 pub protocol_name: String,
24 pub imaging_modality: ImagingModality,
25 pub parameters: AcquisitionParameters,
26}
27
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
30pub enum ImagingModality {
31 XRay,
32 CT,
33 MRI,
34 Ultrasound,
35 PET,
36 SPECT,
37 Mammography,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct AcquisitionParameters {
43 pub resolution: String,
44 pub slice_thickness: f64,
45 pub field_of_view: String,
46 pub acquisition_time: u32,
47}
48
49pub struct QualityControl {
51 quality_metrics: HashMap<String, QualityMetric>,
52 quality_standards: HashMap<String, QualityStandard>,
53}
54
55#[derive(Debug, Clone)]
57pub struct QualityMetric {
58 pub metric_id: String,
59 pub metric_name: String,
60 pub metric_type: QualityMetricType,
61 pub acceptable_range: (f64, f64),
62}
63
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
66pub enum QualityMetricType {
67 SignalToNoise,
68 Contrast,
69 Resolution,
70 ArtifactLevel,
71}
72
73#[derive(Debug, Clone)]
75pub struct QualityStandard {
76 pub standard_id: String,
77 pub standard_name: String,
78 pub standard_type: QualityStandardType,
79 pub requirements: Vec<QualityRequirement>,
80}
81
82#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84pub enum QualityStandardType {
85 ACR,
86 FDA,
87 CE,
88 ISO,
89}
90
91#[derive(Debug, Clone)]
93pub struct QualityRequirement {
94 pub requirement_id: String,
95 pub requirement_name: String,
96 pub requirement_value: f64,
97 pub tolerance: f64,
98}
99
100pub struct ImageProcessing {
102 preprocessing_algorithms: HashMap<String, PreprocessingAlgorithm>,
103 enhancement_techniques: HashMap<String, EnhancementTechnique>,
104}
105
106#[derive(Debug, Clone)]
108pub struct PreprocessingAlgorithm {
109 pub algorithm_id: String,
110 pub algorithm_name: String,
111 pub algorithm_type: PreprocessingAlgorithmType,
112}
113
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub enum PreprocessingAlgorithmType {
117 NoiseReduction,
118 Normalization,
119 Registration,
120 Segmentation,
121}
122
123#[derive(Debug, Clone)]
125pub struct EnhancementTechnique {
126 pub technique_id: String,
127 pub technique_name: String,
128 pub technique_type: EnhancementTechniqueType,
129}
130
131#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
133pub enum EnhancementTechniqueType {
134 ContrastEnhancement,
135 EdgeEnhancement,
136 Sharpening,
137 Filtering,
138}
139
140pub struct ImageAnalysis {
142 analysis_algorithms: HashMap<String, AnalysisAlgorithm>,
143 detection_methods: HashMap<String, DetectionMethod>,
144}
145
146#[derive(Debug, Clone)]
148pub struct AnalysisAlgorithm {
149 pub algorithm_id: String,
150 pub algorithm_name: String,
151 pub algorithm_type: AnalysisAlgorithmType,
152}
153
154#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
156pub enum AnalysisAlgorithmType {
157 PatternRecognition,
158 FeatureExtraction,
159 Classification,
160 Segmentation,
161}
162
163#[derive(Debug, Clone)]
165pub struct DetectionMethod {
166 pub method_id: String,
167 pub method_name: String,
168 pub method_type: DetectionMethodType,
169}
170
171#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
173pub enum DetectionMethodType {
174 AnomalyDetection,
175 LesionDetection,
176 TumorDetection,
177 FractureDetection,
178}
179
180pub struct ImageStorage {
182 storage_systems: HashMap<String, StorageSystem>,
183 compression_methods: HashMap<String, CompressionMethod>,
184}
185
186#[derive(Debug, Clone)]
188pub struct StorageSystem {
189 pub system_id: String,
190 pub system_name: String,
191 pub system_type: StorageSystemType,
192 pub capacity: u64,
193}
194
195#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
197pub enum StorageSystemType {
198 Local,
199 Network,
200 Cloud,
201 Archive,
202}
203
204#[derive(Debug, Clone)]
206pub struct CompressionMethod {
207 pub method_id: String,
208 pub method_name: String,
209 pub method_type: CompressionMethodType,
210}
211
212#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
214pub enum CompressionMethodType {
215 Lossless,
216 Lossy,
217 Hybrid,
218}
219
220impl MedicalImaging {
221 pub fn new() -> Self {
223 Self {
224 image_acquisition: ImageAcquisition::new(),
225 image_processing: ImageProcessing::new(),
226 image_analysis: ImageAnalysis::new(),
227 image_storage: ImageStorage::new(),
228 }
229 }
230
231 pub fn initialize(&mut self) -> Result<(), MedicalError> {
233 self.image_acquisition.initialize()?;
234 self.image_processing.initialize()?;
235 self.image_analysis.initialize()?;
236 self.image_storage.initialize()?;
237 Ok(())
238 }
239
240 pub fn validate_image(&self, image: &MedicalImage) -> Result<(), MedicalError> {
242 if image.image_id.is_empty() {
243 return Err(MedicalError::ValidationError(
244 "Image ID cannot be empty".to_string(),
245 ));
246 }
247 Ok(())
248 }
249
250 pub fn analyze_grid(
254 &self,
255 data: &[f64],
256 width: usize,
257 height: usize,
258 bins: usize,
259 threshold: super::SegmentationThreshold,
260 window: Option<(f64, f64)>,
261 ) -> Result<super::ImageAnalysisResult, MedicalError> {
262 super::analyze_intensity_grid(data, width, height, bins, threshold, window)
263 }
264
265 pub fn process_image(
272 &mut self,
273 image: &MedicalImage,
274 processing_type: ImageProcessingType,
275 ) -> Result<ProcessedImage, MedicalError> {
276 let n = image.image_data.len();
277 if n == 0 {
278 return Err(MedicalError::ValidationError(
279 "process_image: image_data is empty".to_string(),
280 ));
281 }
282 let side = (n as f64).sqrt() as usize;
283 if side * side != n {
284 return Err(MedicalError::InsufficientData(format!(
285 "process_image: image_data length {n} is not a perfect square and MedicalImage \
286 carries no width/height metadata; use analyze_grid(data,width,height,..) with \
287 explicit dimensions"
288 )));
289 }
290 let data: Vec<f64> = image.image_data.iter().map(|&b| b as f64).collect();
291 let result = super::analyze_intensity_grid(
292 &data,
293 side,
294 side,
295 64,
296 super::SegmentationThreshold::Otsu,
297 None,
298 )?;
299
300 let processed_data: Vec<u8> = result
302 .windowed
303 .iter()
304 .map(|&w| (w * 255.0).round().clamp(0.0, 255.0) as u8)
305 .collect();
306
307 let mut processing_metadata = HashMap::new();
308 processing_metadata.insert(
309 "epistemic_status".to_string(),
310 result.epistemic_status.to_string(),
311 );
312 processing_metadata.insert("width".to_string(), side.to_string());
313 processing_metadata.insert("height".to_string(), side.to_string());
314 processing_metadata.insert("min".to_string(), result.min.to_string());
315 processing_metadata.insert("max".to_string(), result.max.to_string());
316 processing_metadata.insert("mean".to_string(), result.mean.to_string());
317 processing_metadata.insert("std_dev".to_string(), result.std_dev.to_string());
318 processing_metadata.insert("otsu_threshold".to_string(), result.threshold.to_string());
319 processing_metadata.insert(
320 "segmented_area".to_string(),
321 result.segmented_area.to_string(),
322 );
323 processing_metadata.insert(
324 "segmented_mean_intensity".to_string(),
325 result.segmented_mean_intensity.to_string(),
326 );
327
328 Ok(ProcessedImage {
329 processed_image_id: format!("processed_{}", image.image_id),
330 original_image_id: image.image_id.clone(),
331 processing_type,
332 processed_data,
333 processing_metadata,
334 })
335 }
336}
337
338impl ImageAcquisition {
339 pub fn new() -> Self {
341 Self {
342 acquisition_protocols: HashMap::new(),
343 quality_control: QualityControl::new(),
344 }
345 }
346
347 pub fn initialize(&mut self) -> Result<(), MedicalError> {
349 Ok(())
350 }
351
352 pub fn add_acquisition_protocol(&mut self, protocol: AcquisitionProtocol) {
354 self.acquisition_protocols
355 .insert(protocol.protocol_id.clone(), protocol);
356 }
357
358 pub fn get_acquisition_protocol(&self, protocol_id: &str) -> Option<&AcquisitionProtocol> {
360 self.acquisition_protocols.get(protocol_id)
361 }
362
363 pub fn quality_control(&self) -> &QualityControl {
365 &self.quality_control
366 }
367}
368
369impl QualityControl {
370 pub fn new() -> Self {
372 Self {
373 quality_metrics: HashMap::new(),
374 quality_standards: HashMap::new(),
375 }
376 }
377
378 pub fn add_quality_metric(&mut self, metric: QualityMetric) {
380 self.quality_metrics
381 .insert(metric.metric_id.clone(), metric);
382 }
383
384 pub fn get_quality_metric(&self, metric_id: &str) -> Option<&QualityMetric> {
386 self.quality_metrics.get(metric_id)
387 }
388
389 pub fn add_quality_standard(&mut self, standard: QualityStandard) {
391 self.quality_standards
392 .insert(standard.standard_id.clone(), standard);
393 }
394
395 pub fn get_quality_standard(&self, standard_id: &str) -> Option<&QualityStandard> {
397 self.quality_standards.get(standard_id)
398 }
399}
400
401impl ImageProcessing {
402 pub fn new() -> Self {
404 Self {
405 preprocessing_algorithms: HashMap::new(),
406 enhancement_techniques: HashMap::new(),
407 }
408 }
409
410 pub fn initialize(&mut self) -> Result<(), MedicalError> {
412 Ok(())
413 }
414
415 pub fn add_preprocessing_algorithm(&mut self, algorithm: PreprocessingAlgorithm) {
417 self.preprocessing_algorithms
418 .insert(algorithm.algorithm_id.clone(), algorithm);
419 }
420
421 pub fn get_preprocessing_algorithm(
423 &self,
424 algorithm_id: &str,
425 ) -> Option<&PreprocessingAlgorithm> {
426 self.preprocessing_algorithms.get(algorithm_id)
427 }
428
429 pub fn add_enhancement_technique(&mut self, technique: EnhancementTechnique) {
431 self.enhancement_techniques
432 .insert(technique.technique_id.clone(), technique);
433 }
434
435 pub fn get_enhancement_technique(&self, technique_id: &str) -> Option<&EnhancementTechnique> {
437 self.enhancement_techniques.get(technique_id)
438 }
439}
440
441impl ImageAnalysis {
442 pub fn new() -> Self {
444 Self {
445 analysis_algorithms: HashMap::new(),
446 detection_methods: HashMap::new(),
447 }
448 }
449
450 pub fn initialize(&mut self) -> Result<(), MedicalError> {
452 Ok(())
453 }
454
455 pub fn add_analysis_algorithm(&mut self, algorithm: AnalysisAlgorithm) {
457 self.analysis_algorithms
458 .insert(algorithm.algorithm_id.clone(), algorithm);
459 }
460
461 pub fn get_analysis_algorithm(&self, algorithm_id: &str) -> Option<&AnalysisAlgorithm> {
463 self.analysis_algorithms.get(algorithm_id)
464 }
465
466 pub fn add_detection_method(&mut self, method: DetectionMethod) {
468 self.detection_methods
469 .insert(method.method_id.clone(), method);
470 }
471
472 pub fn get_detection_method(&self, method_id: &str) -> Option<&DetectionMethod> {
474 self.detection_methods.get(method_id)
475 }
476}
477
478impl ImageStorage {
479 pub fn new() -> Self {
481 Self {
482 storage_systems: HashMap::new(),
483 compression_methods: HashMap::new(),
484 }
485 }
486
487 pub fn initialize(&mut self) -> Result<(), MedicalError> {
489 Ok(())
490 }
491
492 pub fn add_storage_system(&mut self, system: StorageSystem) {
494 self.storage_systems
495 .insert(system.system_id.clone(), system);
496 }
497
498 pub fn get_storage_system(&self, system_id: &str) -> Option<&StorageSystem> {
500 self.storage_systems.get(system_id)
501 }
502
503 pub fn add_compression_method(&mut self, method: CompressionMethod) {
505 self.compression_methods
506 .insert(method.method_id.clone(), method);
507 }
508
509 pub fn get_compression_method(&self, method_id: &str) -> Option<&CompressionMethod> {
511 self.compression_methods.get(method_id)
512 }
513}