space_trav_lr_rust 1.3.0

Spatial gene regulatory network inference and in-silico perturbation (Rust port of SpaceTravLR)
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
use crate::config::{CnnTrainingMode, SpaceshipConfig};
use crate::estimator::ClusterTrainingSummary;
use std::collections::{HashMap, VecDeque};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;

#[derive(Debug, Clone)]
pub struct RunConfigSummary {
    pub config_source: String,
    pub compute_backend: String,
    pub compute_device_detail: String,
    pub compute_notice: String,
    pub layer: String,
    pub cluster_annot: String,
    pub spatial_radius: f64,
    pub spatial_dim: usize,
    pub contact_distance: f64,
    pub weighted_ligand_scale_factor: f64,
    pub tf_ligand_cutoff: f64,
    pub max_ligands: String,
    pub l1_reg: f64,
    pub group_reg: f64,
    pub n_iter: usize,
    pub tol: f64,
    pub learning_rate: f64,
    pub score_threshold: f64,
    pub epochs_per_gene: usize,
    pub gene_selection: String,
    pub cnn_training_mode: String,
    pub condition_split: String,
}

impl RunConfigSummary {
    pub fn build(
        config_path: Option<&Path>,
        compute_backend: &str,
        compute_device_detail: &str,
        compute_notice: &str,
        cfg: &SpaceshipConfig,
        max_genes: Option<usize>,
        gene_filter: Option<&[String]>,
        condition_split: Option<&str>,
    ) -> Self {
        let config_source = config_path
            .map(|p| p.display().to_string())
            .unwrap_or_else(|| "spaceship_config.toml (search path)".to_string());

        let max_ligands = cfg
            .grn
            .max_ligands
            .map(|n| n.to_string())
            .unwrap_or_else(|| "".to_string());

        let cnn_training_mode = match cfg.resolved_cnn_mode() {
            CnnTrainingMode::Seed => "seed",
            CnnTrainingMode::Full => "full",
            CnnTrainingMode::Hybrid => "hybrid",
        }
        .to_string();

        let gene_selection = match (gene_filter, max_genes) {
            (Some(genes), _) if !genes.is_empty() => {
                let take = 4usize.min(genes.len());
                let head: Vec<_> = genes.iter().take(take).cloned().collect();
                let mut s = head.join(", ");
                if genes.len() > take {
                    s.push_str(&format!(" (+{} more)", genes.len() - take));
                }
                s
            }
            (None, Some(n)) => format!("first {} genes (var order)", n),
            _ => "all genes (var order)".to_string(),
        };

        Self {
            config_source,
            compute_backend: compute_backend.to_string(),
            compute_device_detail: compute_device_detail.to_string(),
            compute_notice: compute_notice.to_string(),
            layer: cfg.data.layer.clone(),
            cluster_annot: cfg.data.cluster_annot.clone(),
            spatial_radius: cfg.spatial.radius,
            spatial_dim: cfg.spatial.spatial_dim,
            contact_distance: cfg.spatial.contact_distance,
            weighted_ligand_scale_factor: cfg.spatial.weighted_ligand_scale_factor,
            tf_ligand_cutoff: cfg.grn.tf_ligand_cutoff,
            max_ligands,
            l1_reg: cfg.lasso.l1_reg,
            group_reg: cfg.lasso.group_reg,
            n_iter: cfg.lasso.n_iter,
            tol: cfg.lasso.tol,
            learning_rate: cfg.training.learning_rate,
            score_threshold: cfg.training.score_threshold,
            epochs_per_gene: cfg.training.epochs,
            gene_selection,
            cnn_training_mode,
            condition_split: condition_split.unwrap_or("").to_string(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct TrainingHudState {
    pub dataset_path: String,
    pub output_dir: String,
    pub run_config: RunConfigSummary,
    pub full_cnn: bool,
    pub genes_exported_seed_only: usize,
    pub genes_exported_cnn: usize,
    pub epochs_per_gene: usize,
    pub n_parallel: usize,
    pub total_genes: usize,
    pub genes_done: usize,
    pub genes_skipped: usize,
    pub genes_failed: usize,
    pub genes_orphan: usize,
    pub genes_rounds: usize,
    pub active_genes: HashMap<String, String>,
    /// Per-gene LASSO progress: clusters (cell types) completed / total, for TUI only.
    pub gene_lasso_cluster_progress: HashMap<String, (usize, usize)>,
    pub n_cells: usize,
    pub n_clusters: usize,
    pub cell_type_counts: Vec<(String, usize)>,
    pub started: Instant,
    pub finished: Option<Result<(), String>>,
    pub cancel_requested: Arc<AtomicBool>,
    /// Mean LASSO R² per completed gene (for TUI best / worst list only).
    /// Third field: count of `beta_*` columns in written betadata (non-zero across rows/cells);
    /// falls back to design-matrix width when not supplied (e.g. training demo).
    pub gene_r2_mean: Vec<(String, f64, usize)>,
    pub perf_stats_generation: u64,
    pub gene_train_times: VecDeque<(String, f64)>,
    /// Human-readable obs value for the subset currently training (`--condition` mode).
    pub current_condition_value: Option<String>,
    /// `(1-based index, total splits)` for the active subset.
    pub condition_split_progress: Option<(usize, usize)>,
    /// `spacetravlr --demo`: synthetic run; TUI uses `genes_rounds` for progress (no output dir scan).
    pub is_demo: bool,
}

impl TrainingHudState {
    pub fn new(
        dataset_path: String,
        output_dir: String,
        run_config: RunConfigSummary,
        full_cnn: bool,
        epochs_per_gene: usize,
        n_parallel: usize,
        cancel_requested: Arc<AtomicBool>,
    ) -> Self {
        Self {
            dataset_path,
            output_dir,
            run_config,
            full_cnn,
            genes_exported_seed_only: 0,
            genes_exported_cnn: 0,
            epochs_per_gene,
            n_parallel,
            total_genes: 0,
            genes_done: 0,
            genes_skipped: 0,
            genes_failed: 0,
            genes_orphan: 0,
            genes_rounds: 0,
            active_genes: HashMap::new(),
            gene_lasso_cluster_progress: HashMap::new(),
            n_cells: 0,
            n_clusters: 0,
            cell_type_counts: Vec::new(),
            started: Instant::now(),
            finished: None,
            cancel_requested,
            gene_r2_mean: Vec::new(),
            perf_stats_generation: 0,
            gene_train_times: VecDeque::new(),
            current_condition_value: None,
            condition_split_progress: None,
            is_demo: false,
        }
    }

    pub fn reset_for_new_split(
        &mut self,
        dataset_path: String,
        output_dir: String,
        condition_split: Option<(String, usize, usize)>,
    ) {
        self.dataset_path = dataset_path;
        self.output_dir = output_dir;
        match condition_split {
            Some((label, idx, total)) => {
                self.current_condition_value = Some(label);
                self.condition_split_progress = Some((idx, total));
            }
            None => {
                self.current_condition_value = None;
                self.condition_split_progress = None;
            }
        }
        self.genes_exported_seed_only = 0;
        self.genes_exported_cnn = 0;
        self.total_genes = 0;
        self.genes_done = 0;
        self.genes_skipped = 0;
        self.genes_failed = 0;
        self.genes_orphan = 0;
        self.genes_rounds = 0;
        self.active_genes.clear();
        self.gene_lasso_cluster_progress.clear();
        self.n_cells = 0;
        self.n_clusters = 0;
        self.cell_type_counts.clear();
        self.started = Instant::now();
        self.finished = None;
        self.gene_r2_mean.clear();
        self.perf_stats_generation = self.perf_stats_generation.wrapping_add(1);
        self.gene_train_times.clear();
        self.is_demo = false;
    }

    pub fn record_gene_time(&mut self, gene: &str, secs: f64) {
        const MAX: usize = 64;
        while self.gene_train_times.len() >= MAX {
            self.gene_train_times.pop_front();
        }
        self.gene_train_times.push_back((gene.to_string(), secs));
    }

    pub fn record_training_metrics(
        &mut self,
        gene: &str,
        summaries: &[ClusterTrainingSummary],
        n_betadata_beta_columns: Option<usize>,
    ) {
        if summaries.is_empty() {
            return;
        }
        let mean: f64 = summaries.iter().map(|s| s.lasso_r2).sum::<f64>() / summaries.len() as f64;
        let n_modulators = n_betadata_beta_columns
            .unwrap_or_else(|| summaries.iter().map(|s| s.n_modulators).max().unwrap_or(0));
        self.gene_r2_mean
            .push((gene.to_string(), mean, n_modulators));
        self.perf_stats_generation = self.perf_stats_generation.wrapping_add(1);
    }

    pub fn record_gene_export_mode(&mut self, per_cell_cnn: bool) {
        if per_cell_cnn {
            self.genes_exported_cnn = self.genes_exported_cnn.saturating_add(1);
        } else {
            self.genes_exported_seed_only = self.genes_exported_seed_only.saturating_add(1);
        }
    }

    pub fn set_gene_status(&mut self, gene: &str, status: impl std::fmt::Display) {
        self.active_genes
            .insert(gene.to_string(), status.to_string());
    }

    pub fn set_gene_lasso_cluster_progress(&mut self, gene: &str, done: usize, total: usize) {
        if total == 0 {
            self.gene_lasso_cluster_progress.remove(gene);
            return;
        }
        match self.gene_lasso_cluster_progress.get_mut(gene) {
            Some(v) if v.0 == done && v.1 == total => {}
            Some(v) => *v = (done, total),
            None => {
                self.gene_lasso_cluster_progress
                    .insert(gene.to_string(), (done, total));
            }
        }
    }

    pub fn clear_gene_lasso_cluster_progress(&mut self, gene: &str) {
        self.gene_lasso_cluster_progress.remove(gene);
    }

    pub fn remove_gene(&mut self, gene: &str) {
        self.active_genes.remove(gene);
        self.gene_lasso_cluster_progress.remove(gene);
    }

    pub fn should_cancel(&self) -> bool {
        self.cancel_requested.load(Ordering::Relaxed)
    }

    pub fn elapsed_secs(&self) -> f64 {
        self.started.elapsed().as_secs_f64()
    }

    pub fn mean_completed_gene_secs(&self) -> Option<f64> {
        let n = self.gene_train_times.len();
        if n == 0 {
            return None;
        }
        let sum: f64 = self.gene_train_times.iter().map(|(_, t)| *t).sum();
        let m = sum / n as f64;
        if m.is_finite() && m > 0.0 {
            Some(m)
        } else {
            None
        }
    }

    pub fn parallel_rate_genes_per_sec(&self) -> Option<f64> {
        let elapsed = self.elapsed_secs().max(0.001);
        if self.genes_rounds > 0 {
            let observed = self.genes_rounds as f64 / elapsed;
            if observed.is_finite() && observed > f64::EPSILON {
                return Some(observed);
            }
        }
        if let Some(single_gene_secs) = self.mean_completed_gene_secs() {
            let workers = self.n_parallel.max(1) as f64;
            let estimated = workers / single_gene_secs;
            if estimated.is_finite() && estimated > f64::EPSILON {
                return Some(estimated);
            }
        }
        None
    }

    pub fn parallel_wall_secs_per_gene(&self) -> Option<f64> {
        self.parallel_rate_genes_per_sec()
            .map(|rate| 1.0 / rate)
            .filter(|secs| secs.is_finite() && *secs > 0.0)
    }

    pub fn eta_secs(&self) -> Option<f64> {
        if self.total_genes == 0 {
            return None;
        }
        let remaining = self.total_genes.saturating_sub(self.genes_rounds);
        if remaining == 0 {
            return Some(0.0);
        }
        if let Some(rate) = self.parallel_rate_genes_per_sec() {
            let eta = remaining as f64 / rate;
            if eta.is_finite() && eta >= 0.0 {
                return Some(eta);
            }
        }
        None
    }
}

pub type TrainingHud = Arc<Mutex<TrainingHudState>>;

/// After a run with the dashboard, explain when nothing wrote betadata (TUI hides per-gene `println!`).
pub fn print_training_outcome_banner(hud: &Option<TrainingHud>) {
    let Some(h) = hud else {
        return;
    };
    let Ok(g) = h.lock() else {
        return;
    };
    if g.total_genes == 0 {
        return;
    }
    let exported = g.genes_exported_seed_only + g.genes_exported_cnn;
    if exported > 0 {
        return;
    }
    if g.genes_rounds < g.total_genes {
        return;
    }
    if g.genes_failed == 0 && g.genes_orphan == 0 && g.genes_skipped >= g.total_genes {
        eprintln!(
            "\nNote: no new *_betadata.feather files were written — every gene was skipped (outputs already exist or another process holds a .lock)."
        );
        return;
    }
    eprintln!("\n=== No betadata Feather files were written this run ===");
    eprintln!("Genes queued: {}", g.total_genes);
    eprintln!("  skipped (existing CSV / lock): {}", g.genes_skipped);
    eprintln!(
        "  failed (init or fit — check {}/log/ for details): {}",
        g.output_dir, g.genes_failed
    );
    eprintln!(
        "  orphan (no modulators in GRN for that target): {}",
        g.genes_orphan
    );
    eprintln!(
        "Typical fixes: set [data].layer and [data].cluster_annot to match the .h5ad; ensure obsm has spatial / X_spatial / spatial_loc (≥2 cols); verify species/GRN covers your gene symbols; relax --genes filter."
    );
}

pub fn log_line(hud: &Option<TrainingHud>, msg: String) {
    if hud.is_none() {
        println!("{}", msg);
    }
}

pub fn pipeline_step_begin(hud: &Option<TrainingHud>, label: &str) -> Instant {
    if hud.is_none() {
        println!("[pipeline] … {}", label);
    }
    Instant::now()
}

pub fn pipeline_step_end(hud: &Option<TrainingHud>, label: &str, started: Instant) {
    if hud.is_none() {
        println!(
            "[pipeline] done {} ({:.1}s)",
            label,
            started.elapsed().as_secs_f64()
        );
    }
}