-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrefinement.py
More file actions
1348 lines (1188 loc) · 57.8 KB
/
Copy pathrefinement.py
File metadata and controls
1348 lines (1188 loc) · 57.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
import numpy as np
import torch
import random
import os
import json
from datetime import datetime
@dataclass
class RefinementConfig:
seed: int = 42
train_test_split: float = 0.8
batch_size: int = 64
learning_rate: float = 3e-4
max_epochs: int = 100
early_stopping_patience: int = 15
validation_frequency: int = 5
class RefinementFramework:
def __init__(self, config: RefinementConfig):
self.config = config
self._set_seed()
self.discussion_results = {}
self.final_plan = {}
def _set_seed(self):
random.seed(self.config.seed)
np.random.seed(self.config.seed)
torch.manual_seed(self.config.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(self.config.seed)
torch.cuda.manual_seed_all(self.config.seed)
def integrate_discussion_results(self, discussion_results: Dict[str, Any]):
self.discussion_results = discussion_results
self._generate_final_plan()
def _generate_final_plan(self):
self.final_plan = {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"model_architecture": self._refine_architecture(),
"data_processing": self._refine_data_processing(),
"training_strategy": self._refine_training_strategy(),
"evaluation_metrics": self._refine_evaluation_metrics(),
"implementation_details": self._refine_implementation_details(),
"biological_validation": self._refine_biological_validation(),
"computational_optimization": self._refine_computational_optimization(),
"model_assumptions": self._refine_model_assumptions(),
"extensions": self._refine_extensions(),
"mermaid_diagrams": self._generate_mermaid_json(),
"framework_overview": self._generate_framework_overview()
}
def _refine_architecture(self) -> Dict[str, Any]:
# Handle different possible structures of discussion results
if "model_architecture" in self.discussion_results:
arch = self.discussion_results["model_architecture"]
elif "architecture" in self.discussion_results:
arch = self.discussion_results["architecture"]
else:
# Generate default architecture if no discussion results
arch = self._generate_default_architecture()
refined_arch = {}
for component, details in arch.items():
if isinstance(details, dict):
refined_arch[component] = self._refine_component(details)
else:
refined_arch[component] = details
return refined_arch
def _generate_default_architecture(self) -> Dict[str, Any]:
"""Generate default architecture when no discussion results are available"""
return {
"encoder": {
"type": "transformer",
"layers": 4,
"heads": 8,
"dimension": 512,
"activation": "gelu"
},
"perturbation_encoder": {
"type": "mlp",
"layers": 3,
"dimensions": [256, 128, 64],
"activation": "swish"
},
"cross_attention": {
"heads": 8,
"dimension": 512,
"dropout": 0.1
},
"decoder": {
"type": "mlp",
"layers": 3,
"dimensions": [256, 512, 1024],
"activation": "swish"
}
}
def _refine_component(self, component: Dict[str, Any]) -> Dict[str, Any]:
refined = {}
for key, value in component.items():
if isinstance(value, dict):
refined[key] = self._refine_component(value)
elif isinstance(value, list):
refined[key] = [self._refine_component(item) if isinstance(item, dict) else item for item in value]
else:
refined[key] = value
return refined
def _refine_data_processing(self) -> Dict[str, Any]:
# Handle different possible structures
if "preprocessing" in self.discussion_results:
dp = self.discussion_results["preprocessing"]
elif "data_processing" in self.discussion_results:
dp = self.discussion_results["data_processing"]
else:
# Generate default data processing
dp = self._generate_default_data_processing()
refined_dp = {}
for step, details in dp.items():
if isinstance(details, dict):
refined_dp[step] = self._refine_component(details)
else:
refined_dp[step] = details
return refined_dp
def _generate_default_data_processing(self) -> Dict[str, Any]:
"""Generate default data processing pipeline"""
return {
"quality_control": {
"cell_filtering": {
"min_genes": 200,
"max_genes": 6000,
"min_counts": 1000,
"max_counts": 50000,
"max_mito_percent": 20
},
"gene_filtering": {
"min_cells": 3,
"min_counts": 10
}
},
"normalization": {
"method": "log1p",
"target_sum": 10000,
"regression_vars": ["total_counts", "pct_counts_mt"]
},
"batch_correction": {
"method": "harmony",
"parameters": {
"theta": 2,
"max_iterations": 20
}
}
}
def _refine_training_strategy(self) -> Dict[str, Any]:
# Handle different possible structures
if "training_strategy" in self.discussion_results:
ts = self.discussion_results["training_strategy"]
elif "training" in self.discussion_results:
ts = self.discussion_results["training"]
else:
# Generate default training strategy
ts = self._generate_default_training_strategy()
refined_ts = {}
for component, details in ts.items():
if isinstance(details, dict):
refined_ts[component] = self._refine_component(details)
else:
refined_ts[component] = details
return refined_ts
def _generate_default_training_strategy(self) -> Dict[str, Any]:
"""Generate default training strategy"""
return {
"optimizer": {
"type": "adamw",
"learning_rate": 3e-4,
"weight_decay": 0.01
},
"scheduler": {
"type": "cosine_annealing",
"T_0": 10,
"eta_min": 1e-6
},
"training": {
"batch_size": 64,
"gradient_clip": 1.0,
"early_stopping": {
"patience": 15,
"min_delta": 1e-4
}
}
}
def _refine_evaluation_metrics(self) -> Dict[str, Any]:
# Handle different possible structures
if "evaluation_metrics" in self.discussion_results:
metrics = self.discussion_results["evaluation_metrics"]
elif "optimization_strategy" in self.discussion_results:
opt = self.discussion_results["optimization_strategy"]
if "loss_functions" in opt:
metrics = {"loss_functions": opt["loss_functions"]}
else:
metrics = self._generate_default_evaluation_metrics()
else:
# Generate default evaluation metrics
metrics = self._generate_default_evaluation_metrics()
refined_metrics = {}
for level, details in metrics.items():
if isinstance(details, dict):
refined_metrics[level] = self._refine_component(details)
else:
refined_metrics[level] = details
return refined_metrics
def _generate_default_evaluation_metrics(self) -> Dict[str, Any]:
"""Generate default evaluation metrics"""
return {
"loss_functions": {
"reconstruction": "mse",
"perturbation": "bce",
"biological": "pathway_consistency"
},
"metrics": [
"MSE",
"Pearson correlation",
"R²",
"Biological pathway enrichment"
]
}
def _refine_implementation_details(self) -> Dict[str, Any]:
# Handle different possible structures
if "implementation_details" in self.discussion_results:
impl = self.discussion_results["implementation_details"]
else:
# Generate default implementation details
impl = self._generate_default_implementation_details()
refined_impl = {}
for aspect, details in impl.items():
if isinstance(details, dict):
refined_impl[aspect] = self._refine_component(details)
else:
refined_impl[aspect] = details
return refined_impl
def _generate_default_implementation_details(self) -> Dict[str, Any]:
"""Generate default implementation details"""
return {
"framework": "PyTorch",
"hardware_requirements": {
"gpu": "NVIDIA GPU with 8GB+ VRAM",
"ram": "32GB+ system RAM",
"storage": "100GB+ SSD"
},
"dependencies": [
"torch>=1.12.0",
"scanpy>=1.9.0",
"numpy>=1.21.0",
"scikit-learn>=1.0.0"
],
"code_structure": {
"data_loader": "Custom DataLoader for single-cell data",
"model": "Modular architecture with configurable components",
"training": "Training loop with early stopping and checkpointing",
"evaluation": "Comprehensive evaluation pipeline"
}
}
def _refine_biological_validation(self) -> Dict[str, Any]:
return {
"key_theoretical_foundations": {
"baseline_regulatory_info": {
"assumption": "Baseline contains sufficient regulatory info",
"validation": "Ablation study (masking key TFs)",
"biological_basis": "Central dogma of molecular biology"
},
"dose_response_continuity": {
"assumption": "Dose-response continuity",
"validation": "Dose interpolation experiments",
"biological_basis": "Ligand-receptor binding kinetics"
},
"pathway_modularity": {
"assumption": "Pathway modularity",
"validation": "Pathway enrichment analysis",
"biological_basis": "Known biological pathway organization"
},
"latent_space_smoothness": {
"assumption": "Latent space smoothness",
"validation": "UMAP visualization",
"biological_basis": "Waddington epigenetic landscape concept"
}
},
"adaptive_mechanisms": {
"dose_specific_attention": {
"description": "Higher attention weights to stress pathways at toxic doses",
"implementation": "Focus on growth signaling at therapeutic doses"
},
"cell_type_gating": {
"description": "Cell-type specific pathway modulation",
"implementation": "Suppress irrelevant pathways based on cell type"
},
"chemical_class_priors": {
"description": "Initialize embeddings based on chemical class",
"implementation": "Set initial weights based on known mechanisms"
}
}
}
def _refine_computational_optimization(self) -> Dict[str, Any]:
return {
"training_techniques": {
"progressive_dose_sampling": {
"description": "Gradually increase dose range during training",
"implementation": {
"epoch_0_10": "uniform(0, 1μM)",
"epoch_10_20": "log_normal(1μM-10μM)",
"epoch_20+": "full_range(0-100μM)"
}
}
},
"regularization_methods": {
"pathway_sparsity": {
"type": "l1",
"lambda": 0.01,
"target": "pathway_weights"
},
"dose_consistency": {
"type": "custom",
"formula": "L_consist = E[||f(x,d1) - f(f(x,d1),d2-d1)||^2]"
}
},
"parallelization": {
"gene_cluster_parallelism": {
"chr1": {
"gpu": "GPU0",
"genes": ["EGFR", "MYC"]
},
"chr17": {
"gpu": "GPU1",
"genes": ["TP53", "BRCA1"]
},
"others": {
"gpu": "GPU2",
"genes": "Housekeeping"
}
}
}
}
def _refine_model_assumptions(self) -> Dict[str, Any]:
return {
"data_assumptions": {
"sparse_imbalanced": {
"challenge": "Sparse and imbalanced datasets",
"solution": "Advanced sampling and weighting techniques"
},
"batch_effects": {
"challenge": "Batch effects between experiments",
"solution": "Harmony integration and batch correction"
},
"technical_noise": {
"challenge": "Missing values and technical noise",
"solution": "Robust preprocessing and imputation"
}
},
"computational_assumptions": {
"dimensionality": {
"challenge": "High-dimensional data",
"solution": "Efficient dimensionality reduction"
},
"transfer_learning": {
"challenge": "Limited training data",
"solution": "Pre-trained models and transfer learning"
},
"efficiency": {
"challenge": "Computational efficiency",
"solution": "Optimized implementations"
}
}
}
def _refine_extensions(self) -> Dict[str, Any]:
return {
"drug_discovery": {
"virtual_screening": {
"description": "Virtual screening of novel compounds",
"implementation": "Predict expression profiles for new compounds"
},
"drug_repurposing": {
"description": "Repurposing existing drugs",
"implementation": "Identify new indications based on expression patterns"
},
"side_effects": {
"description": "Predicting side effects",
"implementation": "Analyze off-target expression changes"
}
},
"precision_medicine": {
"patient_specific": {
"description": "Patient-specific drug response prediction",
"implementation": "Use baseline expression for personalized predictions"
},
"drug_combinations": {
"description": "Optimize drug combinations",
"implementation": "Predict synergistic effects"
},
"biomarkers": {
"description": "Biomarker discovery",
"implementation": "Identify sensitivity markers"
}
},
"model_extensions": {
"multi_modal": {
"description": "Multi-modal inputs",
"implementation": "Integrate proteomics and metabolomics"
},
"time_series": {
"description": "Time-series predictions",
"implementation": "Model temporal drug effects"
},
"mechanistic": {
"description": "Integration with mechanistic models",
"implementation": "Combine with pathway models"
}
}
}
def generate_mermaid_diagram(self) -> str:
"""Generate comprehensive Mermaid diagram based on actual method content"""
architecture = self.final_plan["model_architecture"]
data_processing = self.final_plan["data_processing"]
training = self.final_plan["training_strategy"]
evaluation = self.final_plan["evaluation_metrics"]
diagram = [
"# Research Framework Architecture",
"",
"## System Overview",
"This diagram shows the complete research framework based on expert discussions and method refinement.",
"",
"```mermaid",
"graph TD",
""
]
# Generate architecture based on actual content
diagram.extend(self._generate_architecture_from_content(architecture, data_processing, training, evaluation))
diagram.extend([
"```",
"",
"## Architecture Details",
""
])
# Add detailed component information
diagram.extend(self._generate_detailed_component_diagram(architecture, data_processing, training, evaluation))
return "\n".join(diagram)
def _generate_architecture_from_content(self, architecture, data_processing, training, evaluation):
"""Generate architecture diagram based on actual method content"""
diagram_lines = []
# Data Input Section
diagram_lines.extend([
" %% Data Input",
" subgraph DataInput [\"Data Input\"]",
" RawData[\"Raw Single-cell Data<br/>• Expression Matrix<br/>• Cell Metadata<br/>• Perturbation Info\"]",
" Metadata[\"Metadata<br/>• Cell Types<br/>• Batch Info<br/>• Conditions\"]",
])
# Data Processing Section - based on actual processing steps
diagram_lines.extend([
" end",
"",
" %% Data Processing Pipeline",
" subgraph DataProcessing [\"🔧 Data Processing\"]"
])
# Add actual data processing steps
for step_name, step_config in data_processing.items():
if isinstance(step_config, dict):
method = step_config.get("method", step_name)
params = step_config.get("parameters", {})
param_str = ", ".join([f"{k}={v}" for k, v in params.items()]) if params else ""
diagram_lines.append(f" {step_name}[\"{method}<br/>{param_str}\"]")
diagram_lines.append(" end")
# Model Architecture Section - based on actual architecture
diagram_lines.extend([
"",
" %% Model Architecture",
" subgraph ModelArch [\"🧠 Model Architecture\"]"
])
# Add actual model components
for comp_name, comp_config in architecture.items():
if isinstance(comp_config, dict):
comp_type = comp_config.get("type", comp_name)
# Add main component
diagram_lines.append(f" {comp_name}[\"{comp_type}\"]")
# Add sub-components if they exist
if "components" in comp_config:
for subcomp_name, subcomp_config in comp_config["components"].items():
if isinstance(subcomp_config, dict):
subcomp_type = subcomp_config.get("type", subcomp_name)
features = subcomp_config.get("features", [])
feature_str = "<br/>• " + "<br/>• ".join(features[:3]) if features else ""
diagram_lines.append(f" {subcomp_name}[\"{subcomp_type}{feature_str}\"]")
diagram_lines.append(f" {comp_name} --> {subcomp_name}")
diagram_lines.append(" end")
# Training Section - based on actual training strategy
diagram_lines.extend([
"",
" %% Training Strategy",
" subgraph Training [\"🎯 Training Strategy\"]"
])
for train_comp, train_config in training.items():
if isinstance(train_config, dict):
train_type = train_config.get("type", train_comp)
params = train_config.get("parameters", {})
param_str = ", ".join([f"{k}={v}" for k, v in params.items()]) if params else ""
diagram_lines.append(f" {train_comp}[\"{train_type}<br/>{param_str}\"]")
diagram_lines.append(" end")
# Evaluation Section - based on actual evaluation metrics
diagram_lines.extend([
"",
" %% Evaluation Metrics",
" subgraph Evaluation [\"📈 Evaluation\"]"
])
for eval_level, eval_config in evaluation.items():
if isinstance(eval_config, dict):
metrics = eval_config.get("metrics", [])
metric_str = "<br/>• " + "<br/>• ".join(metrics) if metrics else ""
diagram_lines.append(f" {eval_level}[\"{eval_level.title()}{metric_str}\"]")
diagram_lines.append(" end")
# Output Section
diagram_lines.extend([
"",
" %% Output",
" subgraph Output [\"📋 Output\"]",
" Predictions[\"Predictions<br/>• Expression Changes<br/>• State Transitions<br/>• Pathway Activities\"]",
" Insights[\"Biological Insights<br/>• Mechanism of Action<br/>• Drug Targets<br/>• Biomarkers\"]",
" end",
""
])
# Data Flow Connections - based on actual pipeline
diagram_lines.extend([
" %% Data Flow",
" RawData --> DataProcessing",
" Metadata --> DataProcessing"
])
# Connect data processing steps
prev_step = None
for step_name in data_processing.keys():
if prev_step:
diagram_lines.append(f" {prev_step} --> {step_name}")
prev_step = step_name
if prev_step:
diagram_lines.append(f" {prev_step} --> ModelArch")
# Connect model components
diagram_lines.append(" ModelArch --> Training")
diagram_lines.append(" Training --> Evaluation")
diagram_lines.append(" Evaluation --> Output")
# Styling
diagram_lines.extend([
"",
" %% Styling",
" classDef inputStyle fill:#e1f5fe,stroke:#01579b,stroke-width:2px",
" classDef processingStyle fill:#f3e5f5,stroke:#4a148c,stroke-width:2px",
" classDef modelStyle fill:#e8f5e8,stroke:#1b5e20,stroke-width:2px",
" classDef trainingStyle fill:#fff3e0,stroke:#e65100,stroke-width:2px",
" classDef evaluationStyle fill:#fce4ec,stroke:#880e4f,stroke-width:2px",
" classDef outputStyle fill:#e0f2f1,stroke:#004d40,stroke-width:2px",
" classDef subgraphStyle fill:#fafafa,stroke:#666,stroke-width:1px",
"",
" class RawData,Metadata inputStyle",
])
# Apply styles to actual components
for step_name in data_processing.keys():
diagram_lines.append(f" class {step_name} processingStyle")
for comp_name in architecture.keys():
diagram_lines.append(f" class {comp_name} modelStyle")
for train_comp in training.keys():
diagram_lines.append(f" class {train_comp} trainingStyle")
for eval_level in evaluation.keys():
diagram_lines.append(f" class {eval_level} evaluationStyle")
diagram_lines.extend([
" class Predictions,Insights outputStyle",
" class DataInput,DataProcessing,ModelArch,Training,Evaluation,Output subgraphStyle"
])
return diagram_lines
def _generate_detailed_component_diagram(self, architecture, data_processing, training, evaluation):
"""Generate detailed component diagram with specific implementations"""
detailed_diagram = [
"",
"## Detailed Component Architecture",
"",
"```mermaid",
"graph LR",
""
]
# Generate detailed components based on actual content
detailed_diagram.extend(self._generate_detailed_components(architecture, data_processing, training, evaluation))
detailed_diagram.extend([
"```",
"",
"## Implementation Specifications",
""
])
# Add specific implementation details
detailed_diagram.extend(self._add_implementation_details(architecture, data_processing, training, evaluation))
return detailed_diagram
def _generate_detailed_components(self, architecture, data_processing, training, evaluation):
"""Generate detailed component diagram based on actual content"""
diagram_lines = []
# Encoder Details
if "encoder" in architecture:
encoder_config = architecture["encoder"]
diagram_lines.extend([
" %% Encoder Details",
" subgraph EncoderDetails [\"🔍 Encoder Details\"]"
])
if "components" in encoder_config:
for comp_name, comp_config in encoder_config["components"].items():
if isinstance(comp_config, dict):
comp_type = comp_config.get("type", comp_name)
features = comp_config.get("features", [])
feature_str = "<br/>• " + "<br/>• ".join(features[:2]) if features else ""
diagram_lines.append(f" {comp_name}[\"{comp_type}{feature_str}\"]")
diagram_lines.append(" end")
# Decoder Details
if "decoder" in architecture:
decoder_config = architecture["decoder"]
diagram_lines.extend([
"",
" %% Decoder Details",
" subgraph DecoderDetails [\"🎯 Decoder Details\"]"
])
if "components" in decoder_config:
for comp_name, comp_config in decoder_config["components"].items():
if isinstance(comp_config, dict):
comp_type = comp_config.get("type", comp_name)
features = comp_config.get("features", [])
feature_str = "<br/>• " + "<br/>• ".join(features[:2]) if features else ""
diagram_lines.append(f" {comp_name}[\"{comp_type}{feature_str}\"]")
diagram_lines.append(" end")
# Training Details
diagram_lines.extend([
"",
" %% Training Details",
" subgraph TrainingDetails [\"⚙️ Training Details\"]"
])
for train_comp, train_config in training.items():
if isinstance(train_config, dict):
train_type = train_config.get("type", train_comp)
params = train_config.get("parameters", {})
param_str = "<br/>" + ", ".join([f"{k}={v}" for k, v in params.items()]) if params else ""
diagram_lines.append(f" {train_comp}[\"{train_type}{param_str}\"]")
diagram_lines.append(" end")
# Evaluation Details
diagram_lines.extend([
"",
" %% Evaluation Details",
" subgraph EvaluationDetails [\"📊 Evaluation Details\"]"
])
for eval_level, eval_config in evaluation.items():
if isinstance(eval_config, dict):
metrics = eval_config.get("metrics", [])
metric_str = "<br/>• " + "<br/>• ".join(metrics[:3]) if metrics else ""
diagram_lines.append(f" {eval_level}[\"{eval_level.title()}{metric_str}\"]")
diagram_lines.append(" end")
# Connections based on actual relationships
diagram_lines.extend([
"",
" %% Component Connections"
])
# Connect encoder components
if "encoder" in architecture and "components" in architecture["encoder"]:
encoder_comps = list(architecture["encoder"]["components"].keys())
for i in range(len(encoder_comps) - 1):
diagram_lines.append(f" {encoder_comps[i]} --> {encoder_comps[i+1]}")
# Connect decoder components
if "decoder" in architecture and "components" in architecture["decoder"]:
decoder_comps = list(architecture["decoder"]["components"].keys())
for i in range(len(decoder_comps) - 1):
diagram_lines.append(f" {decoder_comps[i]} --> {decoder_comps[i+1]}")
# Connect training components
train_comps = list(training.keys())
for i in range(len(train_comps) - 1):
diagram_lines.append(f" {train_comps[i]} --> {train_comps[i+1]}")
# Connect evaluation components
eval_comps = list(evaluation.keys())
for i in range(len(eval_comps) - 1):
diagram_lines.append(f" {eval_comps[i]} --> {eval_comps[i+1]}")
# Cross-connections
if "encoder" in architecture and "decoder" in architecture:
diagram_lines.append(" EncoderDetails --> DecoderDetails")
diagram_lines.append(" DecoderDetails --> TrainingDetails")
diagram_lines.append(" TrainingDetails --> EvaluationDetails")
# Styling
diagram_lines.extend([
"",
" %% Styling",
" classDef encoderStyle fill:#e8f5e8,stroke:#1b5e20,stroke-width:2px",
" classDef decoderStyle fill:#fff3e0,stroke:#e65100,stroke-width:2px",
" classDef trainingStyle fill:#f3e5f5,stroke:#4a148c,stroke-width:2px",
" classDef evaluationStyle fill:#fce4ec,stroke:#880e4f,stroke-width:2px",
""
])
# Apply styles to actual components
if "encoder" in architecture and "components" in architecture["encoder"]:
for comp_name in architecture["encoder"]["components"].keys():
diagram_lines.append(f" class {comp_name} encoderStyle")
if "decoder" in architecture and "components" in architecture["decoder"]:
for comp_name in architecture["decoder"]["components"].keys():
diagram_lines.append(f" class {comp_name} decoderStyle")
for train_comp in training.keys():
diagram_lines.append(f" class {train_comp} trainingStyle")
for eval_level in evaluation.keys():
diagram_lines.append(f" class {eval_level} evaluationStyle")
return diagram_lines
def generate_plan_markdown(self) -> str:
return f"""
Generated on: {self.final_plan['timestamp']}
{self.generate_mermaid_diagram()}
{self._format_dict(self.final_plan['model_architecture'])}
{self._format_dict(self.final_plan['data_processing'])}
{self._format_dict(self.final_plan['training_strategy'])}
{self._format_dict(self.final_plan['evaluation_metrics'])}
{self._format_dict(self.final_plan['implementation_details'])}
{self._format_dict(self.final_plan['biological_validation'])}
{self._format_dict(self.final_plan['computational_optimization'])}
{self._format_dict(self.final_plan['model_assumptions'])}
{self._format_dict(self.final_plan['extensions'])}
"""
def _format_dict(self, d: Dict[str, Any], indent: int = 0) -> str:
lines = []
for k, v in d.items():
if isinstance(v, dict):
lines.append(f"{' ' * indent}- {k}:")
lines.append(self._format_dict(v, indent + 1))
elif isinstance(v, list):
lines.append(f"{' ' * indent}- {k}: {', '.join(map(str, v))}")
else:
lines.append(f"{' ' * indent}- {k}: {v}")
return "\n".join(lines)
def save_plan(self, output_path: str, format: str = "markdown"):
if format == "markdown":
plan = self.generate_plan_markdown()
with open(output_path, 'w', encoding='utf-8') as f:
f.write(plan)
elif format == "json":
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(self.final_plan, f, indent=2, ensure_ascii=False)
else:
raise ValueError(f"Unsupported format: {format}")
def print_data_matrix_info(self, matrix: np.ndarray, name: str):
print(f"\n{name} Matrix Information:")
print(f"Shape: {matrix.shape}")
print(f"Data type: {matrix.dtype}")
print(f"Memory usage: {matrix.nbytes / 1024 / 1024:.2f} MB")
print(f"Value range: [{matrix.min():.2f}, {matrix.max():.2f}]")
print(f"Mean: {matrix.mean():.2f}")
print(f"Std: {matrix.std():.2f}")
print(f"NaN values: {np.isnan(matrix).sum()}")
print(f"Zero values: {(matrix == 0).sum()}")
def _generate_mermaid_json(self) -> Dict[str, Any]:
"""Generate Mermaid diagrams in JSON format with detailed explanations"""
return {
"system_overview": {
"title": "Research Framework Architecture",
"description": "Complete research framework for biological perturbation analysis using single-cell data",
"mermaid_code": self._get_system_overview_mermaid(),
"modules": {
"data_input_layer": {
"name": "Data Input Layer",
"description": "Handles raw single-cell data and metadata",
"components": {
"raw_data": {
"name": "Raw Single-cell Data",
"description": "Gene expression matrices, cell metadata, and perturbation information",
"format": ["h5ad", "loom", "mtx"],
"required_fields": ["counts", "var_names", "obs_names"]
},
"metadata": {
"name": "Metadata",
"description": "Cell types, batch information, and experimental conditions",
"fields": ["cell_type", "batch", "condition", "perturbation"]
}
}
},
"data_processing_layer": {
"name": "Data Processing Layer",
"description": "Quality control, normalization, and feature selection",
"components": {
"quality_control": {
"name": "Quality Control",
"description": "Cell filtering, gene filtering, and doublet detection",
"methods": ["scanpy.pp.filter_cells", "scanpy.pp.filter_genes", "scanpy.pp.scrublet"]
},
"normalization": {
"name": "Normalization",
"description": "Count normalization, log transformation, and batch correction",
"methods": ["scanpy.pp.normalize_total", "scanpy.pp.log1p", "scanpy.pp.regress_out"]
},
"feature_selection": {
"name": "Feature Selection",
"description": "Highly variable genes, dimensionality reduction, and feature engineering",
"methods": ["scanpy.pp.highly_variable_genes", "scanpy.pp.pca", "scanpy.pp.scale"]
}
}
},
"model_architecture_layer": {
"name": "Model Architecture Layer",
"description": "Neural network architecture for perturbation prediction",
"components": {
"encoder": {
"name": "Encoder",
"description": "Transformer/GNN for gene and cell type embedding",
"types": ["Transformer", "Graph Neural Network", "Autoencoder"],
"features": ["gene_embedding", "cell_embedding", "attention_mechanism"]
},
"decoder": {
"name": "Decoder",
"description": "Conditional generation for perturbation prediction and response modeling",
"types": ["Conditional Generation", "Trajectory Prediction", "Response Prediction"],
"outputs": ["expression_profiles", "cell_states", "pathway_activities"]
},
"constraints": {
"name": "Biological Constraints",
"description": "Integration of pathway information, network structure, and cell type specificity",
"types": ["gene_regulatory_networks", "pathway_interactions", "cell_type_specificity"]
}
}
},
"training_layer": {
"name": "Training Layer",
"description": "Model training and optimization",
"components": {
"optimizer": {
"name": "Optimizer",
"description": "Adam/AdamW with learning rate scheduling and gradient clipping",
"types": ["Adam", "AdamW", "SGD"],
"schedulers": ["CosineAnnealingLR", "ReduceLROnPlateau", "OneCycleLR"]
},
"loss_functions": {
"name": "Loss Functions",
"description": "Reconstruction loss, biological constraints, and regularization",
"types": ["MSE", "MAE", "Biological Loss", "Regularization"]
},
"validation": {
"name": "Validation",
"description": "Cross-validation, early stopping, and model checkpointing",
"methods": ["cross_validation", "early_stopping", "model_checkpointing"]
}
}
},
"evaluation_layer": {
"name": "Evaluation Layer",
"description": "Model evaluation and validation",
"components": {
"technical_metrics": {
"name": "Technical Metrics",
"description": "MSE, R², Pearson correlation, and AUROC",
"metrics": ["MSE", "R2", "Pearson", "AUROC", "Accuracy"]
},
"biological_metrics": {
"name": "Biological Metrics",
"description": "Pathway enrichment, network consistency, and cell type specificity",
"metrics": ["pathway_enrichment", "network_consistency", "cell_type_specificity"]
},
"biological_validation": {
"name": "Biological Validation",
"description": "Experimental validation, literature comparison, and expert assessment",
"methods": ["experimental_validation", "literature_comparison", "expert_assessment"]
}
}
},
"output_layer": {
"name": "Output Layer",
"description": "Predictions and biological insights",
"components": {
"predictions": {
"name": "Predictions",
"description": "Gene expression changes, cell state transitions, and pathway activities",
"types": ["expression_changes", "state_transitions", "pathway_activities"]
},
"insights": {
"name": "Biological Insights",
"description": "Mechanism of action, drug targets, and biomarkers",
"types": ["mechanism_of_action", "drug_targets", "biomarkers"]