ecological-model-core 0.7.1

Shared ecological initial states, interactions, trajectory observation, and terminal products
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
//! Ecological interaction matrices, reproducible recipes, and verified artifacts.

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use physics_in_parallel::prelude::basic::{
    DenseMatrix, MatrixError, RandType, RngConfig, RngConfigError, TensorRandError,
    TensorRandFiller,
};
use scientific_workflow::prelude::basics::{
    ArtifactDescriptor, ArtifactDisposition, ArtifactError, ArtifactLoadError, ExecutionScope,
    RngRecord, RngRecordError, load_verified_artifact, persist_artifact,
};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use thiserror::Error;

pub const INTERACTION_ARTIFACT_REFERENCE_FORMAT: &str =
    "ecological.interaction-artifact-reference.v1";

pub const INTERACTION_MATRIX_FORMAT: &str = "ecological.interaction-matrix.v2";
pub const INTERACTION_MATRIX_METADATA_KEY: &str = "interaction_matrix";
pub const INTERACTION_GENERATOR_RNG_NAMESPACE: &str = "ecological_model_core.interaction_matrix";
pub const INTERACTION_GENERATOR_IDENTITY: &str = "ecological_model_core.interaction_matrix";
pub const INTERACTION_GENERATOR_VERSION: &str = "4";

const DOMAIN_INDEPENDENT: u64 = 0x73d8_ba6e_209f_54c1;
const DOMAIN_CONNECTANCE: u64 = 0x5c1a_9f20_f678_314d;
const DOMAIN_FIRST_NORMAL: u64 = 0x9841_d60a_334b_c8e7;
const DOMAIN_SECOND_NORMAL: u64 = 0xa72e_1b49_963c_05fd;

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MatrixNormalization {
    None,
    SqrtSpecies,
}

impl MatrixNormalization {
    fn divisor(self, species: usize) -> f64 {
        match self {
            Self::None => 1.0,
            Self::SqrtSpecies => (species as f64).sqrt(),
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
pub enum DiagonalPolicy {
    Zero,
    Constant(f64),
    /// Sample diagonal entries from the Gaussian distribution of the recipe.
    Sampled,
}

impl DiagonalPolicy {
    fn fixed_value(self) -> Option<f64> {
        match self {
            Self::Zero => Some(0.0),
            Self::Constant(value) => Some(value),
            Self::Sampled => None,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SignStructure {
    Competition,
    Mutualism,
    ConsumerResource,
}

/// Reproducible scientific instructions for an ecological matrix realization.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "family", rename_all = "snake_case", deny_unknown_fields)]
pub enum InteractionMatrixRecipe {
    /// Independently sample every matrix entry from a uniform distribution.
    RandomUniform {
        minimum: f64,
        maximum: f64,
        #[serde(default)]
        rng: RngConfig,
    },
    /// Independently sample every matrix entry from a Gaussian distribution.
    RandomGaussian {
        mean: f64,
        standard_deviation: f64,
        #[serde(default)]
        rng: RngConfig,
    },
    /// Gaussian reciprocal pairs with Pearson correlation in `[-1,1]`.
    CorrelatedGaussian {
        mean: f64,
        standard_deviation: f64,
        reciprocal_correlation: f64,
        #[serde(default = "one")]
        connectance: f64,
        #[serde(default = "zero_diagonal")]
        diagonal: DiagonalPolicy,
        #[serde(default = "sqrt_species")]
        normalization: MatrixNormalization,
        #[serde(default)]
        rng: RngConfig,
    },
    /// Pairwise signs constrained to a common ecological relationship class.
    SignStructuredGaussian {
        structure: SignStructure,
        scale: f64,
        #[serde(default = "one")]
        connectance: f64,
        #[serde(default = "zero_diagonal")]
        diagonal: DiagonalPolicy,
        #[serde(default = "sqrt_species")]
        normalization: MatrixNormalization,
        #[serde(default)]
        rng: RngConfig,
    },
}

const fn sqrt_species() -> MatrixNormalization {
    MatrixNormalization::SqrtSpecies
}
const fn zero_diagonal() -> DiagonalPolicy {
    DiagonalPolicy::Zero
}
const fn one() -> f64 {
    1.0
}

impl InteractionMatrixRecipe {
    pub fn validate(&self, species: usize) -> Result<(), InteractionRecipeError> {
        if species == 0 {
            return Err(InteractionRecipeError::EmptySpecies);
        }
        match self {
            Self::RandomUniform {
                minimum, maximum, ..
            } => {
                require_finite("minimum", *minimum)?;
                require_finite("maximum", *maximum)?;
                if minimum >= maximum {
                    return Err(InteractionRecipeError::InvalidRange {
                        minimum: *minimum,
                        maximum: *maximum,
                    });
                }
            }
            Self::RandomGaussian {
                mean,
                standard_deviation,
                ..
            } => {
                require_finite("mean", *mean)?;
                require_nonnegative_finite("standard_deviation", *standard_deviation)?;
            }
            Self::CorrelatedGaussian {
                mean,
                standard_deviation,
                connectance,
                diagonal,
                ..
            } => {
                require_finite("mean", *mean)?;
                require_nonnegative_finite("standard_deviation", *standard_deviation)?;
                require_probability(*connectance)?;
                if let Some(value) = diagonal.fixed_value() {
                    require_finite("diagonal", value)?;
                }
            }
            Self::SignStructuredGaussian {
                scale,
                connectance,
                diagonal,
                ..
            } => {
                require_nonnegative_finite("scale", *scale)?;
                require_probability(*connectance)?;
                let value = diagonal.fixed_value().ok_or(
                    InteractionRecipeError::SampledDiagonalUnsupported {
                        family: "sign_structured_gaussian",
                    },
                )?;
                require_finite("diagonal", value)?;
            }
        }
        if let Self::CorrelatedGaussian {
            reciprocal_correlation,
            ..
        } = self
            && (!reciprocal_correlation.is_finite()
                || !(-1.0..=1.0).contains(reciprocal_correlation))
        {
            return Err(InteractionRecipeError::InvalidParameter {
                name: "reciprocal_correlation",
                value: *reciprocal_correlation,
            });
        }
        Ok(())
    }

    pub fn generate(&self, species: usize) -> Result<InteractionMatrix, InteractionRecipeError> {
        self.validate(species)?;
        let kind = match self {
            Self::RandomUniform {
                minimum, maximum, ..
            } => RandType::Uniform {
                low: *minimum,
                high: *maximum,
            },
            Self::RandomGaussian {
                mean,
                standard_deviation,
                ..
            } => RandType::Normal {
                mean: *mean,
                std: *standard_deviation,
            },
            Self::CorrelatedGaussian { .. } | Self::SignStructuredGaussian { .. } => {
                RandType::Normal {
                    mean: 0.0,
                    std: 1.0,
                }
            }
        };
        let mut filler = TensorRandFiller::try_new_indexed(kind, self.rng())?;
        let resolved_recipe = self.with_rng(filler.rng_config());
        let matrix_len = species
            .checked_mul(species)
            .ok_or(MatrixError::ShapeProductOverflow {
                rows: species,
                cols: species,
            })?;
        let mut values = vec![0.0; matrix_len];
        match &resolved_recipe {
            Self::RandomUniform { .. } | Self::RandomGaussian { .. } => {
                filler.try_fill_slice_at_layout(
                    &mut values,
                    species,
                    0,
                    DOMAIN_INDEPENDENT ^ species as u64,
                )?;
            }
            Self::CorrelatedGaussian {
                mean,
                standard_deviation,
                reciprocal_correlation,
                connectance,
                diagonal,
                normalization,
                ..
            } => {
                let samples = sample_structured_inputs(&mut filler, species, matrix_len)?;
                let first_normal = samples.first_normal;
                let second_normal = samples.second_normal;
                let connection_uniform = samples.connection_uniform;
                let divisor = normalization.divisor(species);
                for index in 0..species {
                    values[index * species + index] = diagonal.fixed_value().unwrap_or_else(|| {
                        (mean + standard_deviation * first_normal[index * species + index])
                            / divisor
                    });
                }
                let independent_weight = (1.0 - reciprocal_correlation.powi(2)).sqrt();
                for row in 0..species {
                    for column in (row + 1)..species {
                        let index = row * species + column;
                        if *connectance < 1.0 && connection_uniform[index] >= *connectance {
                            continue;
                        }
                        let first = first_normal[index];
                        let second = second_normal[index];
                        values[row * species + column] =
                            (mean + standard_deviation * first) / divisor;
                        values[column * species + row] = (mean
                            + standard_deviation
                                * (reciprocal_correlation * first + independent_weight * second))
                            / divisor;
                    }
                }
            }
            Self::SignStructuredGaussian {
                structure,
                scale,
                connectance,
                diagonal,
                normalization,
                ..
            } => {
                let samples = sample_structured_inputs(&mut filler, species, matrix_len)?;
                let first_normal = samples.first_normal;
                let second_normal = samples.second_normal;
                let connection_uniform = samples.connection_uniform;
                fill_diagonal(
                    &mut values,
                    species,
                    diagonal
                        .fixed_value()
                        .expect("sampled diagonal rejected during validation"),
                );
                let magnitude = scale / normalization.divisor(species);
                for row in 0..species {
                    for column in (row + 1)..species {
                        let index = row * species + column;
                        if *connectance < 1.0 && connection_uniform[index] >= *connectance {
                            continue;
                        }
                        let first = first_normal[index].abs() * magnitude;
                        let second = second_normal[index].abs() * magnitude;
                        let (forward, reverse) = match structure {
                            SignStructure::Competition => (-first, -second),
                            SignStructure::Mutualism => (first, second),
                            SignStructure::ConsumerResource => (first, -second),
                        };
                        values[row * species + column] = forward;
                        values[column * species + row] = reverse;
                    }
                }
            }
        }
        let generator = GeneratorProvenance::new(
            INTERACTION_GENERATOR_IDENTITY,
            INTERACTION_GENERATOR_VERSION,
            serde_json::to_value(&resolved_recipe)?,
            Some(filler.rng_config()),
        )?;
        Ok(InteractionMatrix::from_generated(
            DenseMatrix::try_from_vec(species, species, values)?,
            generator,
        )?)
    }

    pub const fn rng(&self) -> RngConfig {
        match self {
            Self::RandomUniform { rng, .. }
            | Self::RandomGaussian { rng, .. }
            | Self::CorrelatedGaussian { rng, .. }
            | Self::SignStructuredGaussian { rng, .. } => *rng,
        }
    }

    fn with_rng(&self, resolved: RngConfig) -> Self {
        let mut recipe = self.clone();
        match &mut recipe {
            Self::RandomUniform { rng, .. }
            | Self::RandomGaussian { rng, .. }
            | Self::CorrelatedGaussian { rng, .. }
            | Self::SignStructuredGaussian { rng, .. } => *rng = resolved,
        }
        recipe
    }
}

struct StructuredSamples {
    first_normal: Vec<f64>,
    second_normal: Vec<f64>,
    connection_uniform: Vec<f64>,
}

fn sample_structured_inputs(
    filler: &mut TensorRandFiller,
    species: usize,
    matrix_len: usize,
) -> Result<StructuredSamples, TensorRandError> {
    let mut first_normal = vec![0.0; matrix_len];
    let mut second_normal = vec![0.0; matrix_len];
    let mut connection_uniform = vec![0.0; matrix_len];
    filler.try_fill_slice_at_layout(
        &mut first_normal,
        species,
        0,
        DOMAIN_FIRST_NORMAL ^ species as u64,
    )?;
    filler.try_fill_slice_at_layout(
        &mut second_normal,
        species,
        0,
        DOMAIN_SECOND_NORMAL ^ species as u64,
    )?;
    filler.set_kind(RandType::Uniform {
        low: 0.0,
        high: 1.0,
    });
    filler.try_fill_slice_at_layout(
        &mut connection_uniform,
        species,
        0,
        DOMAIN_CONNECTANCE ^ species as u64,
    )?;
    Ok(StructuredSamples {
        first_normal,
        second_normal,
        connection_uniform,
    })
}

fn fill_diagonal(values: &mut [f64], species: usize, diagonal: f64) {
    for index in 0..species {
        values[index * species + index] = diagonal;
    }
}

fn require_finite(name: &'static str, value: f64) -> Result<(), InteractionRecipeError> {
    if value.is_finite() {
        Ok(())
    } else {
        Err(InteractionRecipeError::InvalidParameter { name, value })
    }
}

fn require_nonnegative_finite(
    name: &'static str,
    value: f64,
) -> Result<(), InteractionRecipeError> {
    if value.is_finite() && value >= 0.0 {
        Ok(())
    } else {
        Err(InteractionRecipeError::InvalidParameter { name, value })
    }
}

fn require_probability(value: f64) -> Result<(), InteractionRecipeError> {
    if value.is_finite() && (0.0..=1.0).contains(&value) {
        Ok(())
    } else {
        Err(InteractionRecipeError::InvalidParameter {
            name: "connectance",
            value,
        })
    }
}

#[derive(Clone, Debug)]
pub struct InteractionMatrix {
    values: Arc<DenseMatrix<f64>>,
    provenance: InteractionProvenance,
}

impl InteractionMatrix {
    pub fn from_matrix(values: DenseMatrix<f64>) -> Result<Self, InteractionMatrixError> {
        Self::resolve(
            Arc::new(values),
            InteractionProvenance::InMemory { label: None },
        )
    }

    pub fn from_shared(values: Arc<DenseMatrix<f64>>) -> Result<Self, InteractionMatrixError> {
        Self::resolve(values, InteractionProvenance::InMemory { label: None })
    }

    pub fn from_labeled_matrix(
        values: DenseMatrix<f64>,
        label: impl Into<String>,
    ) -> Result<Self, InteractionMatrixError> {
        let label = label.into();
        if label.trim().is_empty() {
            return Err(InteractionMatrixError::EmptyLabel);
        }
        Self::resolve(
            Arc::new(values),
            InteractionProvenance::InMemory { label: Some(label) },
        )
    }

    pub fn from_rows(rows: Vec<Vec<f64>>) -> Result<Self, InteractionMatrixError> {
        let row_count = rows.len();
        if row_count == 0 {
            return Err(InteractionMatrixError::EmptySpecies);
        }
        let column_count = rows.first().map_or(0, Vec::len);
        for (row, values) in rows.iter().enumerate() {
            if values.len() != column_count {
                return Err(InteractionMatrixError::RaggedRows {
                    row,
                    expected: column_count,
                    actual: values.len(),
                });
            }
        }
        let values = DenseMatrix::try_from_vec(
            row_count,
            column_count,
            rows.into_iter().flatten().collect(),
        )?;
        Self::resolve(Arc::new(values), InteractionProvenance::Inline)
    }

    pub fn load_json(path: impl Into<PathBuf>) -> Result<Self, InteractionMatrixError> {
        let path = path.into();
        let bytes = fs::read(&path).map_err(|source| InteractionMatrixError::Io {
            path: path.clone(),
            source,
        })?;
        Self::from_json_bytes(
            bytes,
            path.clone(),
            InteractionProvenance::JsonFile { path },
        )
    }

    pub fn from_generated(
        values: DenseMatrix<f64>,
        generator: GeneratorProvenance,
    ) -> Result<Self, InteractionMatrixError> {
        Self::resolve(
            Arc::new(values),
            InteractionProvenance::Generated { generator },
        )
    }

    pub fn species(&self) -> usize {
        self.values.rows()
    }
    pub fn values(&self) -> &DenseMatrix<f64> {
        &self.values
    }
    pub fn shared_values(&self) -> Arc<DenseMatrix<f64>> {
        Arc::clone(&self.values)
    }
    #[inline]
    pub fn coefficient(&self, row: usize, column: usize) -> f64 {
        self.values.get(row as isize, column as isize)
    }
    #[inline]
    pub fn mul_vector_into(&self, input: &[f64], output: &mut [f64]) -> Result<(), MatrixError> {
        self.values.mul_vector_into(input, output)
    }
    /// Apply this interaction matrix to a contiguous batch of species vectors.
    #[inline]
    pub fn mul_vectors_into(&self, input: &[f64], output: &mut [f64]) -> Result<(), MatrixError> {
        self.values.mul_vectors_into(input, output)
    }
    pub const fn provenance(&self) -> &InteractionProvenance {
        &self.provenance
    }
    /// Return `A - A^T` from this exact matrix realization.
    pub fn antisymmetrize(&self) -> Result<Self, InteractionMatrixError> {
        let transposed = self.values.transpose();
        let values = self.values.sub(&transposed);
        self.derive(values, InteractionTransformation::Antisymmetrize)
    }

    /// Return this matrix multiplied by a finite scalar.
    pub fn scale(&self, scalar: f64) -> Result<Self, InteractionMatrixError> {
        require_finite_transform("scalar", scalar)?;
        self.derive(
            self.values.scalar_mul(scalar),
            InteractionTransformation::Scale { scalar },
        )
    }

    /// Return the elementwise absolute value of this matrix.
    pub fn abs(&self) -> Result<Self, InteractionMatrixError> {
        self.derive(self.values.abs(), InteractionTransformation::Abs)
    }

    /// Raise every entry below `minimum` to that finite lower bound.
    pub fn clamp_min(&self, minimum: f64) -> Result<Self, InteractionMatrixError> {
        require_finite_transform("minimum", minimum)?;
        let rows = self.values.rows();
        let columns = self.values.cols();
        self.derive(
            DenseMatrix::from_fn(rows, columns, |row, column| {
                self.values.get(row as isize, column as isize).max(minimum)
            }),
            InteractionTransformation::ClampMin { minimum },
        )
    }

    /// Lower every entry above `maximum` to that finite upper bound.
    pub fn clamp_max(&self, maximum: f64) -> Result<Self, InteractionMatrixError> {
        require_finite_transform("maximum", maximum)?;
        let rows = self.values.rows();
        let columns = self.values.cols();
        self.derive(
            DenseMatrix::from_fn(rows, columns, |row, column| {
                self.values.get(row as isize, column as isize).min(maximum)
            }),
            InteractionTransformation::ClampMax { maximum },
        )
    }

    /// Require every absolute entry to be at most `threshold`.
    ///
    /// The maximum is reduced by PiP's backend-native parallel implementation.
    pub fn ensure_max_abs_at_most(&self, threshold: f64) -> Result<(), InteractionMatrixError> {
        if !threshold.is_finite() || threshold < 0.0 {
            return Err(InteractionMatrixError::InvalidTransformationParameter {
                name: "threshold",
                value: threshold,
            });
        }
        let maximum = self.values.max_abs_real();
        if maximum > threshold {
            return Err(InteractionMatrixError::MaximumAbsoluteEntryExceeded {
                threshold,
                maximum,
            });
        }
        Ok(())
    }

    /// Scale down uniformly until every absolute entry is at most `threshold`.
    ///
    /// A matrix already within the threshold is not enlarged.
    pub fn normalize(&self, threshold: f64) -> Result<Self, InteractionMatrixError> {
        if !threshold.is_finite() || threshold < 0.0 {
            return Err(InteractionMatrixError::InvalidTransformationParameter {
                name: "threshold",
                value: threshold,
            });
        }
        let maximum = self.values.max_abs_real();
        let scalar = if maximum > threshold {
            threshold / maximum
        } else {
            1.0
        };
        let transformation = InteractionTransformation::Normalize {
            threshold,
            maximum,
            scalar,
        };
        if scalar == 1.0 {
            return Ok(Self {
                values: Arc::clone(&self.values),
                provenance: self.derived_provenance(transformation),
            });
        }
        self.derive(self.values.scalar_mul(scalar), transformation)
    }
    pub fn generator_rng_record(&self) -> Result<Option<RngRecord>, RngRecordError> {
        self.provenance.generator_rng_record()
    }

    fn from_json_bytes(
        bytes: Vec<u8>,
        path: PathBuf,
        provenance: InteractionProvenance,
    ) -> Result<Self, InteractionMatrixError> {
        let values =
            serde_json::from_slice(&bytes).map_err(|source| InteractionMatrixError::Json {
                path: path.clone(),
                source,
            })?;
        Self::resolve(Arc::new(values), provenance)
    }

    fn derive(
        &self,
        values: DenseMatrix<f64>,
        transformation: InteractionTransformation,
    ) -> Result<Self, InteractionMatrixError> {
        Self::resolve(Arc::new(values), self.derived_provenance(transformation))
    }

    fn derived_provenance(
        &self,
        transformation: InteractionTransformation,
    ) -> InteractionProvenance {
        InteractionProvenance::Derived {
            source: Box::new(self.provenance.clone()),
            transformation,
        }
    }

    fn resolve(
        values: Arc<DenseMatrix<f64>>,
        provenance: InteractionProvenance,
    ) -> Result<Self, InteractionMatrixError> {
        let rows = values.rows();
        let columns = values.cols();
        if rows != columns {
            return Err(InteractionMatrixError::NonSquare { rows, columns });
        }
        for flat in 0..values.size() {
            let value = values.get_flat(flat as isize);
            if !value.is_finite() {
                return Err(InteractionMatrixError::NonFiniteEntry {
                    row: flat / columns,
                    column: flat % columns,
                    value,
                });
            }
        }
        Ok(Self { values, provenance })
    }
}

fn require_finite_transform(name: &'static str, value: f64) -> Result<(), InteractionMatrixError> {
    if value.is_finite() {
        Ok(())
    } else {
        Err(InteractionMatrixError::InvalidTransformationParameter { name, value })
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum InteractionSourceKind {
    InMemory,
    Inline,
    JsonFile,
    Generated,
    Derived,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "operation", rename_all = "snake_case", deny_unknown_fields)]
pub enum InteractionTransformation {
    Antisymmetrize,
    Abs,
    Scale {
        scalar: f64,
    },
    ClampMin {
        minimum: f64,
    },
    ClampMax {
        maximum: f64,
    },
    Normalize {
        threshold: f64,
        maximum: f64,
        scalar: f64,
    },
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum InteractionProvenance {
    InMemory {
        label: Option<String>,
    },
    Inline,
    JsonFile {
        path: PathBuf,
    },
    Generated {
        generator: GeneratorProvenance,
    },
    Derived {
        source: Box<InteractionProvenance>,
        transformation: InteractionTransformation,
    },
}

impl InteractionProvenance {
    pub const fn kind(&self) -> InteractionSourceKind {
        match self {
            Self::InMemory { .. } => InteractionSourceKind::InMemory,
            Self::Inline => InteractionSourceKind::Inline,
            Self::JsonFile { .. } => InteractionSourceKind::JsonFile,
            Self::Generated { .. } => InteractionSourceKind::Generated,
            Self::Derived { .. } => InteractionSourceKind::Derived,
        }
    }
    pub const fn generator(&self) -> Option<&GeneratorProvenance> {
        match self {
            Self::Generated { generator } => Some(generator),
            Self::Derived { source, .. } => source.generator(),
            _ => None,
        }
    }
    pub fn generator_rng_record(&self) -> Result<Option<RngRecord>, RngRecordError> {
        self.generator()
            .map_or(Ok(None), GeneratorProvenance::rng_record)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct GeneratorProvenance {
    identity: String,
    version: String,
    recipe: Value,
    rng: Option<RngConfig>,
}

impl GeneratorProvenance {
    pub fn new(
        identity: impl Into<String>,
        version: impl Into<String>,
        recipe: Value,
        rng: Option<RngConfig>,
    ) -> Result<Self, InteractionMatrixError> {
        let identity = identity.into();
        let version = version.into();
        if identity.trim().is_empty() {
            return Err(InteractionMatrixError::InvalidGeneratorLabel { field: "identity" });
        }
        if version.trim().is_empty() {
            return Err(InteractionMatrixError::InvalidGeneratorLabel { field: "version" });
        }
        if rng.is_some_and(|value| value.seed().is_none() || value.method().is_none()) {
            return Err(InteractionMatrixError::UnresolvedGeneratorRng { identity });
        }
        Ok(Self {
            identity,
            version,
            recipe,
            rng,
        })
    }
    pub fn identity(&self) -> &str {
        &self.identity
    }
    pub fn version(&self) -> &str {
        &self.version
    }
    pub const fn recipe(&self) -> &Value {
        &self.recipe
    }
    pub const fn rng(&self) -> Option<RngConfig> {
        self.rng
    }
    pub fn rng_record(&self) -> Result<Option<RngRecord>, RngRecordError> {
        let Some(rng) = self.rng else {
            return Ok(None);
        };
        let method = rng.method().expect("generator RNG is resolved");
        let mut parameters = Map::new();
        parameters.insert("recipe".to_owned(), self.recipe.clone());
        Ok(Some(RngRecord::new(
            INTERACTION_GENERATOR_RNG_NAMESPACE,
            format!("{}+{}", self.identity, method.name()),
            format!("{}+{}", self.version, method.version()),
            method.seed_encoding(),
            rng.encode_seed().expect("generator RNG seed is resolved"),
            Some(parameters),
        )?))
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct InteractionArtifactDescriptor {
    format: String,
    species: usize,
    #[serde(flatten)]
    artifact: ArtifactDescriptor,
    provenance: InteractionProvenance,
}

/// Portable pointer to a verified interaction artifact produced by an earlier execution.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct InteractionArtifactReference {
    format: String,
    execution_directory: PathBuf,
    descriptor: InteractionArtifactDescriptor,
}

impl InteractionArtifactReference {
    pub fn new(
        execution_directory: impl Into<PathBuf>,
        descriptor: InteractionArtifactDescriptor,
    ) -> Self {
        Self {
            format: INTERACTION_ARTIFACT_REFERENCE_FORMAT.to_owned(),
            execution_directory: execution_directory.into(),
            descriptor,
        }
    }

    pub fn load_json(path: impl Into<PathBuf>) -> Result<Self, InteractionArtifactLoadError> {
        let path = path.into();
        let bytes = fs::read(&path).map_err(|source| InteractionMatrixError::Io {
            path: path.clone(),
            source,
        })?;
        let reference: Self =
            serde_json::from_slice(&bytes).map_err(|source| InteractionMatrixError::Json {
                path: path.clone(),
                source,
            })?;
        if reference.format != INTERACTION_ARTIFACT_REFERENCE_FORMAT
            || reference.execution_directory.as_os_str().is_empty()
        {
            return Err(InteractionArtifactLoadError::InvalidDescriptor);
        }
        Ok(reference)
    }

    pub fn to_json_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
        serde_json::to_vec_pretty(self)
    }

    pub fn resolve(&self) -> Result<InteractionMatrix, InteractionArtifactLoadError> {
        load_verified_interaction_matrix(&self.execution_directory, &self.descriptor)
    }

    pub fn execution_directory(&self) -> &Path {
        &self.execution_directory
    }

    pub const fn descriptor(&self) -> &InteractionArtifactDescriptor {
        &self.descriptor
    }
}

impl InteractionArtifactDescriptor {
    pub fn format(&self) -> &str {
        &self.format
    }
    pub const fn species(&self) -> usize {
        self.species
    }
    pub const fn shape(&self) -> [usize; 2] {
        [self.species, self.species]
    }
    pub fn sha256(&self) -> &str {
        self.artifact.sha256()
    }
    pub fn path(&self) -> &str {
        self.artifact.path()
    }
    pub const fn source_kind(&self) -> InteractionSourceKind {
        self.provenance.kind()
    }
    pub const fn generator(&self) -> Option<&GeneratorProvenance> {
        self.provenance.generator()
    }
    pub const fn provenance(&self) -> &InteractionProvenance {
        &self.provenance
    }
    pub fn insert_into_metadata(&self, metadata: &mut Map<String, Value>) -> Option<Value> {
        metadata.insert(
            INTERACTION_MATRIX_METADATA_KEY.to_owned(),
            serde_json::to_value(self).expect("interaction descriptor is JSON-compatible"),
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PersistedInteraction {
    descriptor: InteractionArtifactDescriptor,
    disposition: ArtifactDisposition,
}

impl PersistedInteraction {
    pub const fn descriptor(&self) -> &InteractionArtifactDescriptor {
        &self.descriptor
    }
    pub const fn disposition(&self) -> ArtifactDisposition {
        self.disposition
    }
    pub fn into_descriptor(self) -> InteractionArtifactDescriptor {
        self.descriptor
    }
}

pub fn persist_interaction_matrix(
    scope: &ExecutionScope,
    matrix: &InteractionMatrix,
) -> Result<PersistedInteraction, InteractionArtifactError> {
    let bytes = serde_json::to_vec(matrix.values())?;
    let persisted = persist_artifact(scope, "interaction", "json", &bytes)?;
    Ok(PersistedInteraction {
        descriptor: InteractionArtifactDescriptor {
            format: INTERACTION_MATRIX_FORMAT.to_owned(),
            species: matrix.species(),
            artifact: persisted.descriptor().clone(),
            provenance: matrix.provenance().clone(),
        },
        disposition: persisted.disposition(),
    })
}

pub fn load_verified_interaction_matrix(
    execution_directory: impl AsRef<Path>,
    descriptor: &InteractionArtifactDescriptor,
) -> Result<InteractionMatrix, InteractionArtifactLoadError> {
    if descriptor.format != INTERACTION_MATRIX_FORMAT || descriptor.species == 0 {
        return Err(InteractionArtifactLoadError::InvalidDescriptor);
    }
    let verified = load_verified_artifact(execution_directory, &descriptor.artifact)?;
    let path = verified.path().to_path_buf();
    let matrix = InteractionMatrix::from_json_bytes(
        verified.into_bytes(),
        path,
        descriptor.provenance.clone(),
    )?;
    if matrix.species() != descriptor.species {
        return Err(InteractionMatrixError::SpeciesMismatch {
            expected: descriptor.species,
            actual: matrix.species(),
        }
        .into());
    }
    Ok(matrix)
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum InteractionRecipeError {
    #[error("interaction recipe requires at least one species")]
    EmptySpecies,
    #[error("interaction recipe parameter {name} is invalid: {value}")]
    InvalidParameter { name: &'static str, value: f64 },
    #[error("uniform interaction range requires minimum < maximum, got [{minimum}, {maximum})")]
    InvalidRange { minimum: f64, maximum: f64 },
    #[error("interaction family {family} does not support sampled diagonal entries")]
    SampledDiagonalUnsupported { family: &'static str },
    #[error(transparent)]
    Rng(#[from] RngConfigError),
    #[error(transparent)]
    TensorRand(#[from] TensorRandError),
    #[error(transparent)]
    Matrix(#[from] MatrixError),
    #[error(transparent)]
    Interaction(#[from] InteractionMatrixError),
    #[error(transparent)]
    Json(#[from] serde_json::Error),
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum InteractionMatrixError {
    #[error(transparent)]
    Matrix(#[from] MatrixError),
    #[error("interaction matrix species dimension must be positive")]
    EmptySpecies,
    #[error("interaction matrix must be square, found {rows}x{columns}")]
    NonSquare { rows: usize, columns: usize },
    #[error("interaction matrix has {actual} species, expected {expected}")]
    SpeciesMismatch { expected: usize, actual: usize },
    #[error("interaction matrix row {row} has {actual} columns, expected {expected}")]
    RaggedRows {
        row: usize,
        expected: usize,
        actual: usize,
    },
    #[error("interaction matrix entry ({row}, {column}) is not finite: {value}")]
    NonFiniteEntry {
        row: usize,
        column: usize,
        value: f64,
    },
    #[error("interaction matrix transformation parameter {name} is invalid: {value}")]
    InvalidTransformationParameter { name: &'static str, value: f64 },
    #[error("interaction matrix maximum absolute entry {maximum} exceeds threshold {threshold}")]
    MaximumAbsoluteEntryExceeded { threshold: f64, maximum: f64 },
    #[error("interaction matrix label must not be empty")]
    EmptyLabel,
    #[error("failed to read interaction matrix at `{path}`")]
    Io {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("invalid interaction matrix JSON at `{path}`")]
    Json {
        path: PathBuf,
        #[source]
        source: serde_json::Error,
    },
    #[error("interaction generator {field} must not be empty")]
    InvalidGeneratorLabel { field: &'static str },
    #[error("interaction generator `{identity}` has unresolved RNG")]
    UnresolvedGeneratorRng { identity: String },
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum InteractionArtifactError {
    #[error(transparent)]
    Serialize(#[from] serde_json::Error),
    #[error(transparent)]
    Workflow(#[from] ArtifactError),
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum InteractionArtifactLoadError {
    #[error("invalid interaction artifact descriptor")]
    InvalidDescriptor,
    #[error(transparent)]
    Workflow(#[from] ArtifactLoadError),
    #[error(transparent)]
    Matrix(#[from] InteractionMatrixError),
}