dag-ml-data-core 0.2.8

Core data contracts, schemas and plans for dag-ml-data.
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
//! Frozen, published catalogue of the built-in representation IDs (`B-014` /
//! `DMD-001`).
//!
//! `B-014` is a freeze/publish problem, not an invention problem: the stable
//! representation-ID vocabulary already lives in [`crate::builtin_models`] as
//! `pub const` strings, a [`BuiltinDataModel`] enum and its constructors. This
//! module does not add a new vocabulary; it *publishes* the existing one as a
//! single, deterministic, machine-readable registry so cross-repo consumers
//! (notably `ControllerManifest.data_requirements` ports on `L16` and the
//! `nirs4all-io` emit on `L7`) can reference the frozen strings, and so a drift
//! test can freeze them against [`crate::builtin_models`].
//!
//! The companion artifact `docs/contracts/representation_registry.v1.json` is
//! generated from [`representation_registry`] (via the
//! `dag-ml-data-cli representation-registry` command) and pinned by the
//! `published_registry_matches_builtin_models` drift test, which fails the
//! moment a built-in representation changes without the published manifest
//! being regenerated.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use crate::adapter::ModelInputSpec;
use crate::builtin_models::{
    BuiltinDataModel, BUILTIN_DATA_MODELS, REPRESENTATION_FEATURE_BLOCK_SET,
    REPRESENTATION_GRAY_IMAGE, REPRESENTATION_MC_IMAGE, REPRESENTATION_MULTISPECTRAL_IMAGE,
    REPRESENTATION_RGB_IMAGE, REPRESENTATION_SAMPLE_METADATA, REPRESENTATION_SIGNAL_1D,
    REPRESENTATION_SIGNAL_WITH_PROCESSINGS, REPRESENTATION_TARGET_CATEGORICAL,
    REPRESENTATION_TARGET_CATEGORICAL_MATRIX, REPRESENTATION_TARGET_NUMERIC,
    REPRESENTATION_TARGET_NUMERIC_MATRIX,
};
use crate::error::{DataError, Result};
use crate::model::RepresentationSpec;

/// Wire-shape version of the published representation registry manifest.
pub const REPRESENTATION_REGISTRY_SCHEMA_VERSION: u32 = 1;

/// Stable identifier of the representation registry contract.
pub const REPRESENTATION_REGISTRY_ID: &str = "dag-ml-data.representation_registry.v1";

/// Repo-relative provenance of the frozen representation IDs.
pub const REPRESENTATION_REGISTRY_SOURCE: &str = "crates/dag-ml-data-core/src/builtin_models.rs";

/// Name of the spectra+image MVP profile defined by `B-014` / `IO_spec.md` §5.
pub const MVP_PROFILE_SPECTRA_IMAGE: &str = "spectra_image";

/// The 8 spectra/target/metadata representation IDs that the `nirs4all-io`
/// bridge already emits for the spectra+image MVP (`IO_spec.md` §5).
const MVP_SPECTRA_IMAGE_EMITTED: &[&str] = &[
    REPRESENTATION_SIGNAL_1D,
    REPRESENTATION_SIGNAL_WITH_PROCESSINGS,
    REPRESENTATION_FEATURE_BLOCK_SET,
    REPRESENTATION_TARGET_NUMERIC,
    REPRESENTATION_TARGET_CATEGORICAL,
    REPRESENTATION_TARGET_NUMERIC_MATRIX,
    REPRESENTATION_TARGET_CATEGORICAL_MATRIX,
    REPRESENTATION_SAMPLE_METADATA,
];

/// The 4 image representation IDs that are landed in this crate but not yet
/// emitted by `nirs4all-io` (`IO-010`).
const MVP_SPECTRA_IMAGE_PENDING: &[&str] = &[
    REPRESENTATION_GRAY_IMAGE,
    REPRESENTATION_RGB_IMAGE,
    REPRESENTATION_MC_IMAGE,
    REPRESENTATION_MULTISPECTRAL_IMAGE,
];

/// Cross-repo publication status of a representation ID inside an MVP profile.
///
/// This annotates *downstream emission readiness only*; every representation in
/// the registry is fully landed in this crate (constructor, axes and dtype all
/// present). The status is sourced from `IO_spec.md` §5, not verified at runtime
/// by `dag-ml-data`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MvpEmissionStatus {
    /// Landed here and already emitted by the `nirs4all-io` → `dag-ml-data`
    /// bridge (the 8 spectra/target/metadata IDs of `IO_spec.md` §5).
    Emitted,
    /// Landed here (representation, axes, dtype and constructor present) but not
    /// yet emitted by `nirs4all-io`; emission is tracked by `IO-010`.
    LandedPendingEmit,
}

/// MVP profile membership for a published representation ID.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct MvpStatus {
    /// Named MVP profile this ID belongs to (currently only
    /// [`MVP_PROFILE_SPECTRA_IMAGE`]).
    pub profile: String,
    /// Cross-repo emission status of the ID within that profile.
    pub emission: MvpEmissionStatus,
}

/// One frozen, published entry of the built-in representation registry.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RegisteredRepresentation {
    /// Stable representation-ID string (e.g. `signal_1d`) — the value that
    /// `ControllerManifest.data_requirements` ports reference by string.
    pub representation_id: String,
    /// [`BuiltinDataModel`] catalogue key (e.g. `nirs.signal_1d`).
    pub builtin_key: String,
    /// Generic modality label (e.g. `nirs`, `image`, `target`).
    pub modality: String,
    /// Optional spectra+image MVP annotation; `None` for post-MVP IDs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mvp: Option<MvpStatus>,
    /// The complete frozen representation spec (axes, rank, dtype, container).
    pub representation: RepresentationSpec,
}

/// The frozen, published catalogue of built-in representation IDs.
///
/// Build it with [`representation_registry`]. The serialized form is the
/// `docs/contracts/representation_registry.v1.json` contract artifact.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RepresentationRegistry {
    /// Wire-shape version of this manifest
    /// ([`REPRESENTATION_REGISTRY_SCHEMA_VERSION`]).
    pub schema_version: u32,
    /// Stable identifier of this registry contract
    /// ([`REPRESENTATION_REGISTRY_ID`]).
    pub registry_id: String,
    /// Repo-relative provenance of the frozen IDs
    /// ([`REPRESENTATION_REGISTRY_SOURCE`]).
    pub source: String,
    /// Every built-in representation, in [`BUILTIN_DATA_MODELS`] order.
    pub representations: Vec<RegisteredRepresentation>,
}

/// Build the frozen, published catalogue of built-in representation IDs from
/// [`BUILTIN_DATA_MODELS`]. The result is deterministic and source-order stable.
pub fn representation_registry() -> RepresentationRegistry {
    RepresentationRegistry {
        schema_version: REPRESENTATION_REGISTRY_SCHEMA_VERSION,
        registry_id: REPRESENTATION_REGISTRY_ID.to_string(),
        source: REPRESENTATION_REGISTRY_SOURCE.to_string(),
        representations: BUILTIN_DATA_MODELS
            .iter()
            .copied()
            .map(registered_representation)
            .collect(),
    }
}

impl RepresentationRegistry {
    /// Validate the published registry manifest shape and the embedded
    /// representation specs before a consumer uses it for compatibility checks.
    pub fn validate(&self) -> Result<()> {
        if self.schema_version != REPRESENTATION_REGISTRY_SCHEMA_VERSION {
            return Err(DataError::Validation(format!(
                "representation registry uses unsupported schema_version {}, expected {}",
                self.schema_version, REPRESENTATION_REGISTRY_SCHEMA_VERSION
            )));
        }
        if self.registry_id != REPRESENTATION_REGISTRY_ID {
            return Err(DataError::Validation(format!(
                "representation registry id `{}` does not match `{}`",
                self.registry_id, REPRESENTATION_REGISTRY_ID
            )));
        }
        if self.representations.is_empty() {
            return Err(DataError::Validation(
                "representation registry contains no representations".to_string(),
            ));
        }
        let mut ids = BTreeSet::new();
        for entry in &self.representations {
            if entry.representation_id.trim().is_empty() {
                return Err(DataError::Validation(
                    "representation registry contains an empty representation_id".to_string(),
                ));
            }
            if !ids.insert(entry.representation_id.as_str()) {
                return Err(DataError::Validation(format!(
                    "representation registry contains duplicate representation `{}`",
                    entry.representation_id
                )));
            }
            if entry.representation_id != entry.representation.id.as_str() {
                return Err(DataError::Validation(format!(
                    "representation registry entry `{}` does not match embedded representation id `{}`",
                    entry.representation_id, entry.representation.id
                )));
            }
            entry.representation.validate()?;
        }
        Ok(())
    }
}

/// Validate that a model-input/data-requirements contract consumes only
/// published representation IDs, and that each accepted representation's frozen
/// `type_id` and rank are allowed by the port.
pub fn validate_model_input_spec_against_registry(
    model_input: &ModelInputSpec,
    registry: &RepresentationRegistry,
) -> Result<()> {
    registry.validate()?;
    model_input.validate()?;
    let by_id = registry
        .representations
        .iter()
        .map(|entry| (entry.representation_id.as_str(), entry))
        .collect::<BTreeMap<_, _>>();
    for port in &model_input.ports {
        for representation_id in &port.accepted_representations {
            let representation_key = representation_id.as_str();
            let Some(entry) = by_id.get(representation_key) else {
                return Err(DataError::Validation(format!(
                    "model input port `{}` accepts unknown representation `{representation_key}`",
                    port.name
                )));
            };
            if !port
                .accepted_types
                .iter()
                .any(|type_id| type_id == &entry.representation.type_id)
            {
                return Err(DataError::Validation(format!(
                    "model input port `{}` representation `{}` has type `{}` not listed in accepted_types",
                    port.name, representation_key, entry.representation.type_id
                )));
            }
            if let Some(rank) = port.rank {
                if entry.representation.rank != Some(rank) {
                    return Err(DataError::Validation(format!(
                        "model input port `{}` requires rank {rank} but representation `{}` has {:?}",
                        port.name, representation_key, entry.representation.rank
                    )));
                }
            }
        }
    }
    Ok(())
}

fn registered_representation(model: BuiltinDataModel) -> RegisteredRepresentation {
    let representation = model.representation();
    let representation_id = representation.id.as_str().to_string();
    let mvp = mvp_status(&representation_id);
    RegisteredRepresentation {
        representation_id,
        builtin_key: model.key().to_string(),
        modality: model.modality().to_string(),
        mvp,
        representation,
    }
}

fn mvp_status(representation_id: &str) -> Option<MvpStatus> {
    if MVP_SPECTRA_IMAGE_EMITTED.contains(&representation_id) {
        Some(MvpStatus {
            profile: MVP_PROFILE_SPECTRA_IMAGE.to_string(),
            emission: MvpEmissionStatus::Emitted,
        })
    } else if MVP_SPECTRA_IMAGE_PENDING.contains(&representation_id) {
        Some(MvpStatus {
            profile: MVP_PROFILE_SPECTRA_IMAGE.to_string(),
            emission: MvpEmissionStatus::LandedPendingEmit,
        })
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use crate::adapter::InputPortSpec;
    use crate::ids::{RepresentationId, TypeId};

    use super::*;

    const PUBLISHED: &str = include_str!("../../../docs/contracts/representation_registry.v1.json");

    #[test]
    fn published_registry_matches_builtin_models() {
        let generated = serde_json::to_value(representation_registry()).unwrap();
        let published: serde_json::Value = serde_json::from_str(PUBLISHED).unwrap();
        assert_eq!(
            published, generated,
            "docs/contracts/representation_registry.v1.json drifted from builtin_models.rs; \
             regenerate with `cargo run -p dag-ml-data-cli -- representation-registry`"
        );
    }

    #[test]
    fn registry_header_is_stable() {
        let registry = representation_registry();
        assert_eq!(
            registry.schema_version,
            REPRESENTATION_REGISTRY_SCHEMA_VERSION
        );
        assert_eq!(registry.registry_id, REPRESENTATION_REGISTRY_ID);
        assert_eq!(registry.source, REPRESENTATION_REGISTRY_SOURCE);
    }

    #[test]
    fn registry_covers_every_builtin_with_unique_ids() {
        let registry = representation_registry();
        assert_eq!(registry.representations.len(), BUILTIN_DATA_MODELS.len());

        let mut ids = BTreeSet::new();
        let mut keys = BTreeSet::new();
        for entry in &registry.representations {
            assert!(
                ids.insert(entry.representation_id.clone()),
                "duplicate representation id {}",
                entry.representation_id
            );
            assert!(
                keys.insert(entry.builtin_key.clone()),
                "duplicate builtin key {}",
                entry.builtin_key
            );
            // The denormalized id must equal the embedded representation spec id.
            assert_eq!(entry.representation_id, entry.representation.id.as_str());
            // The embedded spec must validate (it is the frozen contract object).
            entry.representation.validate().unwrap();
        }
    }

    #[test]
    fn spectra_image_mvp_profile_is_consistent() {
        let registry = representation_registry();
        let frozen: BTreeSet<&str> = registry
            .representations
            .iter()
            .map(|entry| entry.representation_id.as_str())
            .collect();

        let emitted: BTreeSet<&str> = registry
            .representations
            .iter()
            .filter(|entry| {
                matches!(&entry.mvp, Some(status) if status.emission == MvpEmissionStatus::Emitted)
            })
            .map(|entry| entry.representation_id.as_str())
            .collect();
        let pending: BTreeSet<&str> = registry
            .representations
            .iter()
            .filter(|entry| {
                matches!(
                    &entry.mvp,
                    Some(status) if status.emission == MvpEmissionStatus::LandedPendingEmit
                )
            })
            .map(|entry| entry.representation_id.as_str())
            .collect();

        // B-014 spectra+image MVP = 12 IDs (8 emitted + 4 image landed-pending-emit).
        assert_eq!(emitted.len(), 8, "spectra MVP must publish 8 emitted IDs");
        assert_eq!(pending.len(), 4, "image MVP must publish 4 pending IDs");
        assert!(emitted.is_disjoint(&pending), "MVP groups must be disjoint");
        for id in emitted.iter().chain(pending.iter()) {
            assert!(
                frozen.contains(id),
                "MVP id {id} is not a frozen representation"
            );
        }

        // The four pending IDs are exactly the image set.
        assert_eq!(
            pending,
            BTreeSet::from([
                REPRESENTATION_GRAY_IMAGE,
                REPRESENTATION_RGB_IMAGE,
                REPRESENTATION_MC_IMAGE,
                REPRESENTATION_MULTISPECTRAL_IMAGE,
            ])
        );

        // Every MVP annotation uses the published profile name.
        for entry in &registry.representations {
            if let Some(status) = &entry.mvp {
                assert_eq!(status.profile, MVP_PROFILE_SPECTRA_IMAGE);
            }
        }
    }

    #[test]
    fn registry_round_trips_through_json() {
        let registry = representation_registry();
        let json = serde_json::to_string(&registry).unwrap();
        let decoded: RepresentationRegistry = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded, registry);
    }

    #[test]
    fn model_input_spec_registry_validation_accepts_published_tabular_requirement() {
        let spec = crate::tabular_numeric_model_input_spec();
        validate_model_input_spec_against_registry(&spec, &representation_registry()).unwrap();
    }

    #[test]
    fn model_input_spec_registry_validation_rejects_unknown_representation() {
        let spec = ModelInputSpec {
            ports: vec![InputPortSpec {
                name: "x".to_string(),
                accepted_representations: vec![RepresentationId::new("columnar_f64").unwrap()],
                accepted_types: vec![TypeId::new("table").unwrap()],
                rank: Some(2),
                multi_source: false,
                optional: false,
            }],
            default_fusion: None,
        };
        let error = validate_model_input_spec_against_registry(&spec, &representation_registry())
            .unwrap_err();
        assert!(
            error.to_string().contains("unknown representation"),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn model_input_spec_registry_validation_rejects_type_drift() {
        let mut spec = crate::tabular_numeric_model_input_spec();
        spec.ports[0].accepted_types = vec![TypeId::new("f64").unwrap()];
        let error = validate_model_input_spec_against_registry(&spec, &representation_registry())
            .unwrap_err();
        assert!(
            error.to_string().contains("not listed in accepted_types"),
            "unexpected error: {error}"
        );
    }
}