dsfb-add 0.1.0

Deterministic Algebraic Deterministic Dynamics (ADD) parameter sweeps for AET, TCP, RLT, and IWLT
Documentation
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
use std::fs;
use std::path::{Path, PathBuf};

use chrono::Utc;
use csv::Writer;

use crate::{rlt::RltTrajectoryPoint, AddError, TcpPoint};

#[derive(Debug, Clone)]
pub struct PhaseBoundaryRow {
    pub steps_per_run: usize,
    pub mode: String,
    pub is_perturbed: bool,
    pub lambda_star: Option<f64>,
    pub lambda_0_1: Option<f64>,
    pub lambda_0_9: Option<f64>,
    pub transition_width: Option<f64>,
    pub max_derivative: Option<f64>,
}

#[derive(Debug, Clone)]
pub struct StructuralLawSummaryRow {
    pub steps_per_run: usize,
    pub is_perturbed: bool,
    pub pearson_r: f64,
    pub spearman_rho: f64,
    pub slope: f64,
    pub intercept: f64,
    pub r2: f64,
    pub residual_variance: f64,
    pub mse_resid: f64,
    pub slope_ci_low: f64,
    pub slope_ci_high: f64,
    pub sample_count: usize,
    pub ratio_mean: f64,
    pub ratio_std: f64,
}

#[derive(Debug, Clone)]
pub struct DiagnosticsSummaryRow {
    pub steps_per_run: usize,
    pub residual_mean: f64,
    pub residual_std: f64,
    pub residual_skew_approx: f64,
    pub residual_kurtosis_approx: f64,
    pub ratio_mean: f64,
    pub ratio_std: f64,
    pub ratio_min: f64,
    pub ratio_max: f64,
}

#[derive(Debug, Clone)]
pub struct CrossLayerThresholdRow {
    pub steps_per_run: usize,
    pub lambda_star: Option<f64>,
    pub echo_slope_star: Option<f64>,
    pub entropy_density_star: Option<f64>,
}

#[derive(Debug, Clone)]
pub struct TcpPhaseAlignmentRow {
    pub steps_per_run: usize,
    pub lambda_star: Option<f64>,
    pub lambda_tp_peak: Option<f64>,
    pub lambda_b1_peak: Option<f64>,
    pub delta_tp: Option<f64>,
    pub delta_b1: Option<f64>,
}

#[derive(Debug, Clone)]
pub struct RobustnessMetricRow {
    pub metric: String,
    pub steps_per_run: usize,
    pub baseline: f64,
    pub perturbed: f64,
    pub delta: f64,
}

pub fn repo_root_dir() -> PathBuf {
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    manifest_dir
        .parent()
        .and_then(|path| path.parent())
        .map(Path::to_path_buf)
        .unwrap_or(manifest_dir)
}

pub fn create_timestamped_output_dir() -> Result<PathBuf, AddError> {
    let output_root = repo_root_dir().join("output-dsfb-add");
    fs::create_dir_all(&output_root)?;

    let timestamp = Utc::now().format("%Y-%m-%dT%H-%M-%SZ").to_string();
    let mut output_dir = output_root.join(&timestamp);
    let mut counter = 1_u32;

    while output_dir.exists() {
        output_dir = output_root.join(format!("{timestamp}-{counter:02}"));
        counter += 1;
    }

    fs::create_dir_all(&output_dir)?;
    Ok(output_dir)
}

fn ensure_len(context: &'static str, expected: usize, actual: usize) -> Result<(), AddError> {
    if expected == actual {
        return Ok(());
    }

    Err(AddError::LengthMismatch {
        context,
        expected,
        got: actual,
    })
}

fn fmt_f64(value: f64) -> String {
    format!("{value:.10}")
}

fn fmt_option_f64(value: Option<f64>) -> String {
    value.map(fmt_f64).unwrap_or_default()
}

pub fn write_aet_csv(
    path: &Path,
    lambda_grid: &[f64],
    echo_slope: &[f64],
    avg_increment: &[f64],
    steps_per_run: usize,
    is_perturbed: bool,
) -> Result<(), AddError> {
    ensure_len("aet echo_slope", lambda_grid.len(), echo_slope.len())?;
    ensure_len("aet avg_increment", lambda_grid.len(), avg_increment.len())?;

    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "lambda",
        "echo_slope",
        "avg_increment",
        "steps_per_run",
        "is_perturbed",
    ])?;

    for idx in 0..lambda_grid.len() {
        writer.write_record([
            fmt_f64(lambda_grid[idx]),
            fmt_f64(echo_slope[idx]),
            fmt_f64(avg_increment[idx]),
            steps_per_run.to_string(),
            is_perturbed.to_string(),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_tcp_csv(
    path: &Path,
    lambda_grid: &[f64],
    betti0: &[usize],
    betti1: &[usize],
    l_tcp: &[f64],
    avg_radius: &[f64],
    max_radius: &[f64],
    variance_radius: &[f64],
    steps_per_run: usize,
    is_perturbed: bool,
) -> Result<(), AddError> {
    ensure_len("tcp betti0", lambda_grid.len(), betti0.len())?;
    ensure_len("tcp betti1", lambda_grid.len(), betti1.len())?;
    ensure_len("tcp l_tcp", lambda_grid.len(), l_tcp.len())?;
    ensure_len("tcp avg_radius", lambda_grid.len(), avg_radius.len())?;
    ensure_len("tcp max_radius", lambda_grid.len(), max_radius.len())?;
    ensure_len(
        "tcp variance_radius",
        lambda_grid.len(),
        variance_radius.len(),
    )?;

    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "lambda",
        "betti0",
        "betti1",
        "l_tcp",
        "avg_radius",
        "max_radius",
        "variance_radius",
        "steps_per_run",
        "is_perturbed",
    ])?;

    for idx in 0..lambda_grid.len() {
        writer.write_record([
            fmt_f64(lambda_grid[idx]),
            betti0[idx].to_string(),
            betti1[idx].to_string(),
            fmt_f64(l_tcp[idx]),
            fmt_f64(avg_radius[idx]),
            fmt_f64(max_radius[idx]),
            fmt_f64(variance_radius[idx]),
            steps_per_run.to_string(),
            is_perturbed.to_string(),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_rlt_csv(
    path: &Path,
    lambda_grid: &[f64],
    escape_rate: &[f64],
    expansion_ratio: &[f64],
    steps_per_run: usize,
    is_perturbed: bool,
) -> Result<(), AddError> {
    ensure_len("rlt escape_rate", lambda_grid.len(), escape_rate.len())?;
    ensure_len(
        "rlt expansion_ratio",
        lambda_grid.len(),
        expansion_ratio.len(),
    )?;

    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "lambda",
        "escape_rate",
        "expansion_ratio",
        "steps_per_run",
        "is_perturbed",
    ])?;

    for idx in 0..lambda_grid.len() {
        writer.write_record([
            fmt_f64(lambda_grid[idx]),
            fmt_f64(escape_rate[idx]),
            fmt_f64(expansion_ratio[idx]),
            steps_per_run.to_string(),
            is_perturbed.to_string(),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_iwlt_csv(
    path: &Path,
    lambda_grid: &[f64],
    entropy_density: &[f64],
    avg_increment: &[f64],
    steps_per_run: usize,
    is_perturbed: bool,
) -> Result<(), AddError> {
    ensure_len(
        "iwlt entropy_density",
        lambda_grid.len(),
        entropy_density.len(),
    )?;
    ensure_len("iwlt avg_increment", lambda_grid.len(), avg_increment.len())?;

    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "lambda",
        "entropy_density",
        "avg_increment",
        "steps_per_run",
        "is_perturbed",
    ])?;

    for idx in 0..lambda_grid.len() {
        writer.write_record([
            fmt_f64(lambda_grid[idx]),
            fmt_f64(entropy_density[idx]),
            fmt_f64(avg_increment[idx]),
            steps_per_run.to_string(),
            is_perturbed.to_string(),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_tcp_points_csv(path: &Path, points: &[TcpPoint]) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record(["t", "x", "y"])?;

    for point in points {
        writer.write_record([point.t.to_string(), fmt_f64(point.x), fmt_f64(point.y)])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_rlt_trajectory_csv(
    path: &Path,
    points: &[RltTrajectoryPoint],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "step",
        "lambda",
        "vertex_id",
        "x",
        "y",
        "distance_from_start",
    ])?;

    for point in points {
        writer.write_record([
            point.step.to_string(),
            fmt_f64(point.lambda),
            point.vertex_id.to_string(),
            point.x.to_string(),
            point.y.to_string(),
            point.distance_from_start.to_string(),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_rlt_phase_boundary_csv(
    path: &Path,
    rows: &[PhaseBoundaryRow],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "steps_per_run",
        "mode",
        "is_perturbed",
        "lambda_star",
        "lambda_0_1",
        "lambda_0_9",
        "transition_width",
        "max_derivative",
    ])?;

    for row in rows {
        writer.write_record([
            row.steps_per_run.to_string(),
            row.mode.clone(),
            row.is_perturbed.to_string(),
            fmt_option_f64(row.lambda_star),
            fmt_option_f64(row.lambda_0_1),
            fmt_option_f64(row.lambda_0_9),
            fmt_option_f64(row.transition_width),
            fmt_option_f64(row.max_derivative),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_structural_law_summary_csv(
    path: &Path,
    rows: &[StructuralLawSummaryRow],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "steps_per_run",
        "is_perturbed",
        "pearson_r",
        "spearman_rho",
        "slope",
        "intercept",
        "r2",
        "residual_variance",
        "mse_resid",
        "slope_ci_low",
        "slope_ci_high",
        "sample_count",
        "ratio_mean",
        "ratio_std",
    ])?;

    for row in rows {
        writer.write_record([
            row.steps_per_run.to_string(),
            row.is_perturbed.to_string(),
            fmt_f64(row.pearson_r),
            fmt_f64(row.spearman_rho),
            fmt_f64(row.slope),
            fmt_f64(row.intercept),
            fmt_f64(row.r2),
            fmt_f64(row.residual_variance),
            fmt_f64(row.mse_resid),
            fmt_f64(row.slope_ci_low),
            fmt_f64(row.slope_ci_high),
            row.sample_count.to_string(),
            fmt_f64(row.ratio_mean),
            fmt_f64(row.ratio_std),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_diagnostics_summary_csv(
    path: &Path,
    rows: &[DiagnosticsSummaryRow],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "steps_per_run",
        "residual_mean",
        "residual_std",
        "residual_skew_approx",
        "residual_kurtosis_approx",
        "ratio_mean",
        "ratio_std",
        "ratio_min",
        "ratio_max",
    ])?;

    for row in rows {
        writer.write_record([
            row.steps_per_run.to_string(),
            fmt_f64(row.residual_mean),
            fmt_f64(row.residual_std),
            fmt_f64(row.residual_skew_approx),
            fmt_f64(row.residual_kurtosis_approx),
            fmt_f64(row.ratio_mean),
            fmt_f64(row.ratio_std),
            fmt_f64(row.ratio_min),
            fmt_f64(row.ratio_max),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_cross_layer_thresholds_csv(
    path: &Path,
    rows: &[CrossLayerThresholdRow],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "steps_per_run",
        "lambda_star",
        "echo_slope_star",
        "entropy_density_star",
    ])?;

    for row in rows {
        writer.write_record([
            row.steps_per_run.to_string(),
            fmt_option_f64(row.lambda_star),
            fmt_option_f64(row.echo_slope_star),
            fmt_option_f64(row.entropy_density_star),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_tcp_phase_alignment_csv(
    path: &Path,
    rows: &[TcpPhaseAlignmentRow],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record([
        "steps_per_run",
        "lambda_star",
        "lambda_tp_peak",
        "lambda_b1_peak",
        "delta_tp",
        "delta_b1",
    ])?;

    for row in rows {
        writer.write_record([
            row.steps_per_run.to_string(),
            fmt_option_f64(row.lambda_star),
            fmt_option_f64(row.lambda_tp_peak),
            fmt_option_f64(row.lambda_b1_peak),
            fmt_option_f64(row.delta_tp),
            fmt_option_f64(row.delta_b1),
        ])?;
    }

    writer.flush()?;
    Ok(())
}

pub fn write_robustness_metrics_csv(
    path: &Path,
    rows: &[RobustnessMetricRow],
) -> Result<(), AddError> {
    let mut writer = Writer::from_path(path)?;
    writer.write_record(["metric", "steps_per_run", "baseline", "perturbed", "delta"])?;

    for row in rows {
        writer.write_record([
            row.metric.clone(),
            row.steps_per_run.to_string(),
            fmt_f64(row.baseline),
            fmt_f64(row.perturbed),
            fmt_f64(row.delta),
        ])?;
    }

    writer.flush()?;
    Ok(())
}