dag-ml-data 0.2.16__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/PKG-INFO +1 -1
  2. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/Cargo.toml +3 -3
  3. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/adapter.rs +1 -2
  4. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/buffer.rs +13 -0
  5. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/buffer_file_store.rs +1 -2
  6. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/error.rs +8 -0
  7. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/lib.rs +2 -0
  8. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/model.rs +129 -3
  9. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/nd_tensor.rs +98 -27
  10. dag_ml_data-0.3.0/dag-ml-data-core/src/source_payload.rs +431 -0
  11. dag_ml_data-0.3.0/dag-ml-data-core/tests/adapter_contribution.rs +64 -0
  12. dag_ml_data-0.3.0/dag-ml-data-core/tests/fixtures/image_first_pixel.adapter.json +15 -0
  13. dag_ml_data-0.3.0/dag-ml-data-core/tests/fixtures/source_payload.v1.json +39 -0
  14. dag_ml_data-0.3.0/dag-ml-data-core/tests/source_payload.rs +87 -0
  15. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-py/Cargo.lock +4 -4
  16. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-py/Cargo.toml +3 -3
  17. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/pyproject.toml +1 -1
  18. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/LICENSE +0 -0
  19. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/aggregation.rs +0 -0
  20. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/alignment.rs +0 -0
  21. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/builtin_models.rs +0 -0
  22. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/collation.rs +0 -0
  23. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/content_hash.rs +0 -0
  24. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/coordinator.rs +0 -0
  25. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/fingerprint.rs +0 -0
  26. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/fitted_adapter.rs +0 -0
  27. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/fusion.rs +0 -0
  28. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/handle.rs +0 -0
  29. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/ids.rs +0 -0
  30. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/plan.rs +0 -0
  31. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/planner.rs +0 -0
  32. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/predict_cohort.rs +0 -0
  33. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/relation.rs +0 -0
  34. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-core/src/representation_registry.rs +0 -0
  35. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-py/.gitignore +0 -0
  36. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-py/LICENSE +0 -0
  37. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-py/README.md +0 -0
  38. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/dag-ml-data-py/src/lib.rs +0 -0
  39. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/python/dag_ml_data/__init__.py +0 -0
  40. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/python/dag_ml_data/__init__.pyi +0 -0
  41. {dag_ml_data-0.2.16 → dag_ml_data-0.3.0}/python/dag_ml_data/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dag-ml-data
3
- Version: 0.2.16
3
+ Version: 0.3.0
4
4
  Classifier: Development Status :: 4 - Beta
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: 3.11
@@ -1,9 +1,9 @@
1
1
  [package]
2
2
  name = "dag-ml-data-core"
3
3
  description = "Core data contracts, schemas and plans for dag-ml-data."
4
- version = "0.2.16"
4
+ version = "0.3.0"
5
5
  edition = "2021"
6
- rust-version = "1.85.0"
6
+ rust-version = "1.88.0"
7
7
  license = "CECILL-2.1 OR AGPL-3.0-or-later"
8
8
  authors = ["G. Beurier <beurier@cirad.fr>"]
9
9
  repository = "https://github.com/GBeurier/dag-ml-data"
@@ -12,5 +12,5 @@ homepage = "https://github.com/GBeurier/dag-ml-data"
12
12
  [dependencies]
13
13
  serde = { version = "^1.0", features = ["derive"] }
14
14
  serde_json = { version = "^1.0", features = ["float_roundtrip"] }
15
- sha2 = "=0.11.0"
15
+ sha2 = "=0.11.1"
16
16
  thiserror = "^2.0"
@@ -363,8 +363,7 @@ impl AdapterRegistry {
363
363
  "no_path"
364
364
  },
365
365
  format!(
366
- "no adapter path from `{}/{}` to `{}/{}`",
367
- source_type, source_representation, target_type, target_representation
366
+ "no adapter path from `{source_type}/{source_representation}` to `{target_type}/{target_representation}`"
368
367
  ),
369
368
  Vec::new(),
370
369
  );
@@ -8,8 +8,10 @@ use crate::error::{DataError, Result};
8
8
  use crate::handle::{CoordinatorFeatureBlock, CoordinatorFeatureBlockF64, CoordinatorFeatureTable};
9
9
  use crate::ids::{ObservationId, RepresentationId, SourceId};
10
10
 
11
+ /// Layout version of [`NumericFeatureBufferManifest`].
11
12
  pub const NUMERIC_FEATURE_BUFFER_MANIFEST_SCHEMA_VERSION: u32 = 1;
12
13
 
14
+ /// Identity-indexed numeric columns with per-cell missing values.
13
15
  #[derive(Clone, Debug, PartialEq)]
14
16
  pub struct NumericFeatureBuffer {
15
17
  pub feature_set_id: String,
@@ -20,6 +22,7 @@ pub struct NumericFeatureBuffer {
20
22
  row_index_by_observation: BTreeMap<ObservationId, usize>,
21
23
  }
22
24
 
25
+ /// Provider-wide descriptor and fingerprint for one numeric feature buffer.
23
26
  #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
24
27
  pub struct NumericFeatureBufferManifest {
25
28
  pub schema_version: u32,
@@ -34,6 +37,7 @@ pub struct NumericFeatureBufferManifest {
34
37
  pub buffer_fingerprint: String,
35
38
  }
36
39
 
40
+ /// Record of one numeric feature buffer bound to a materialized data handle.
37
41
  #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
38
42
  pub struct NumericFeatureBufferBinding {
39
43
  pub feature_set_id: String,
@@ -44,6 +48,7 @@ pub struct NumericFeatureBufferBinding {
44
48
  pub buffer_fingerprint: String,
45
49
  }
46
50
 
51
+ /// Row-major f64 input with one observation ID per row and optional cell validity.
47
52
  #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
48
53
  pub struct NumericFeatureMatrixF64 {
49
54
  pub feature_set_id: String,
@@ -55,6 +60,7 @@ pub struct NumericFeatureMatrixF64 {
55
60
  pub validity_mask: Option<Vec<bool>>,
56
61
  }
57
62
 
63
+ /// Column-major f64 input with shared row identities and optional per-column masks.
58
64
  #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
59
65
  pub struct NumericFeatureMatrixF64Columnar {
60
66
  pub feature_set_id: String,
@@ -66,11 +72,13 @@ pub struct NumericFeatureMatrixF64Columnar {
66
72
  pub validity_masks: Option<Vec<Vec<bool>>>,
67
73
  }
68
74
 
75
+ /// Validated numeric buffers keyed by their feature-set identifiers.
69
76
  #[derive(Clone, Debug, Default, PartialEq)]
70
77
  pub struct NumericFeatureBufferStore {
71
78
  buffers: BTreeMap<String, NumericFeatureBuffer>,
72
79
  }
73
80
 
81
+ /// Immutable numeric buffers with bindings scoped to materialized data handles.
74
82
  #[derive(Clone, Debug, Default, PartialEq)]
75
83
  pub struct NumericFeatureBufferArena {
76
84
  store: NumericFeatureBufferStore,
@@ -794,6 +802,7 @@ impl NumericFeatureBufferStore {
794
802
  }
795
803
 
796
804
  impl NumericFeatureBufferArena {
805
+ /// Creates an arena with the supplied store and no live handle bindings.
797
806
  pub fn new(store: NumericFeatureBufferStore) -> Self {
798
807
  Self {
799
808
  store,
@@ -801,10 +810,12 @@ impl NumericFeatureBufferArena {
801
810
  }
802
811
  }
803
812
 
813
+ /// Lists the provider-wide buffer manifests independently of handle lifetime.
804
814
  pub fn manifests(&self) -> Result<Vec<NumericFeatureBufferManifest>> {
805
815
  self.store.manifests()
806
816
  }
807
817
 
818
+ /// Stores bindings for matching representations and covered source observations.
808
819
  pub fn bind_data_handle(
809
820
  &mut self,
810
821
  data_handle: u64,
@@ -825,10 +836,12 @@ impl NumericFeatureBufferArena {
825
836
  Ok(bindings)
826
837
  }
827
838
 
839
+ /// Removes the data handle's bindings. Returns whether a binding record existed.
828
840
  pub fn release_data_handle(&mut self, data_handle: u64) -> bool {
829
841
  self.data_bindings.remove(&data_handle).is_some()
830
842
  }
831
843
 
844
+ /// Lists the buffer bindings of a data handle. A missing binding record is an error.
832
845
  pub fn bindings_for_data_handle(
833
846
  &self,
834
847
  data_handle: u64,
@@ -67,8 +67,7 @@ pub fn deserialize_columnar_store(bytes: &[u8]) -> Result<NumericFeatureBufferSt
67
67
  let version = read_u32(bytes, 4)?;
68
68
  if version != NUMERIC_FEATURE_BUFFER_FILE_FORMAT_VERSION {
69
69
  return Err(DataError::Validation(format!(
70
- "feature buffer file uses unsupported format version {version}, expected {}",
71
- NUMERIC_FEATURE_BUFFER_FILE_FORMAT_VERSION
70
+ "feature buffer file uses unsupported format version {version}, expected {NUMERIC_FEATURE_BUFFER_FILE_FORMAT_VERSION}"
72
71
  )));
73
72
  }
74
73
  let buffer_count = read_u32(bytes, 8)? as usize;
@@ -21,11 +21,17 @@ pub struct DataErrorDescriptor {
21
21
  pub context: BTreeMap<String, Value>,
22
22
  }
23
23
 
24
+ /// Validation, compatibility and lifecycle errors returned by data contracts.
25
+ ///
26
+ /// Bindings can use [`DataError::descriptor`] to export the stable diagnostic
27
+ /// category, code and context with a human-readable message.
24
28
  #[derive(Debug, Error)]
25
29
  pub enum DataError {
30
+ /// An identifier failed the grammar for its typed identifier wrapper.
26
31
  #[error("invalid identifier `{value}`: {reason}")]
27
32
  InvalidIdentifier { value: String, reason: &'static str },
28
33
 
34
+ /// Input, projection or a provider definition failed semantic validation.
29
35
  #[error("data contract validation failed: {0}")]
30
36
  Validation(String),
31
37
 
@@ -66,6 +72,7 @@ pub enum DataError {
66
72
  actual: &'static str,
67
73
  },
68
74
 
75
+ /// The JSON payload could not be decoded or encoded for the contract.
69
76
  #[error("serialization error: {0}")]
70
77
  Serialization(#[from] serde_json::Error),
71
78
  }
@@ -223,6 +230,7 @@ impl DataError {
223
230
  }
224
231
  }
225
232
 
233
+ /// The result type shared by data contracts, providers and bindings.
226
234
  pub type Result<T> = std::result::Result<T, DataError>;
227
235
 
228
236
  #[cfg(test)]
@@ -29,6 +29,7 @@ pub mod planner;
29
29
  pub mod predict_cohort;
30
30
  pub mod relation;
31
31
  pub mod representation_registry;
32
+ pub mod source_payload;
32
33
 
33
34
  pub use adapter::*;
34
35
  pub use aggregation::*;
@@ -51,3 +52,4 @@ pub use planner::*;
51
52
  pub use predict_cohort::*;
52
53
  pub use relation::*;
53
54
  pub use representation_registry::*;
55
+ pub use source_payload::*;
@@ -76,7 +76,10 @@ pub enum CoordinateDType {
76
76
  }
77
77
 
78
78
  /// Axis coordinate values: either an explicit per-index list or a regular
79
- /// numeric grid (`value(i) = start + i * step`, count taken from the axis size).
79
+ /// numeric grid with one value per axis index. The index is converted to
80
+ /// binary64 and `start + i * step` uses separate binary64 multiplication and
81
+ /// addition. The represented coordinates must be finite and strictly ordered
82
+ /// in the direction of `step`.
80
83
  #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
81
84
  #[serde(tag = "kind", rename_all = "snake_case")]
82
85
  pub enum CoordinateValues {
@@ -128,11 +131,11 @@ impl CoordinateSpec {
128
131
  "axis `{axis_name}` regular-grid coordinates require numeric dtype"
129
132
  )));
130
133
  }
131
- if size.is_none() {
134
+ let Some(size) = size else {
132
135
  return Err(DataError::Validation(format!(
133
136
  "axis `{axis_name}` regular-grid coordinates require a known axis size"
134
137
  )));
135
- }
138
+ };
136
139
  if !start.is_finite() || !step.is_finite() {
137
140
  return Err(DataError::Validation(format!(
138
141
  "axis `{axis_name}` regular-grid start/step must be finite"
@@ -148,6 +151,7 @@ impl CoordinateSpec {
148
151
  "axis `{axis_name}` regular-grid coordinates are inherently ordered; set ordered=true"
149
152
  )));
150
153
  }
154
+ validate_regular_grid(axis_name, size, *start, *step)?;
151
155
  }
152
156
  }
153
157
  Ok(())
@@ -218,6 +222,82 @@ impl CoordinateSpec {
218
222
  }
219
223
  }
220
224
 
225
+ fn validate_regular_grid(axis_name: &str, size: usize, start: f64, step: f64) -> Result<()> {
226
+ if size < 2 {
227
+ return Ok(());
228
+ }
229
+
230
+ let last_index = size - 1;
231
+ let last_offset = last_index as f64 * step;
232
+ let last = start + last_offset;
233
+ if !last.is_finite() {
234
+ return Err(DataError::Validation(format!(
235
+ "axis `{axis_name}` regular-grid coordinate at index {last_index} is not finite"
236
+ )));
237
+ }
238
+
239
+ let coincident = |left, right| {
240
+ DataError::Validation(format!(
241
+ "axis `{axis_name}` regular-grid coordinates at indices {left} and {right} coincide in binary64"
242
+ ))
243
+ };
244
+ // The first adjacent integer indices that round to the same binary64.
245
+ const FIRST_REPEATED_INDEX: u64 = (1 << f64::MANTISSA_DIGITS) + 1;
246
+ if last_index as u64 >= FIRST_REPEATED_INDEX {
247
+ return Err(coincident(FIRST_REPEATED_INDEX - 1, FIRST_REPEATED_INDEX));
248
+ }
249
+
250
+ // Multiplication by a fixed step and addition of a finite start are
251
+ // monotone under rounding. Thus finite endpoints rule out interior
252
+ // overflow, and only equal adjacent values can violate strict ordering.
253
+ let offset_bound = last_offset.abs();
254
+ let coordinate_bound = start.abs().max(last.abs());
255
+ let offset_spacing = offset_bound.next_up() - offset_bound;
256
+ let coordinate_spacing = coordinate_bound.next_up() - coordinate_bound;
257
+ // Adding zero introduces no new rounding.
258
+ if start == 0.0 && step.abs() > offset_spacing {
259
+ return Ok(());
260
+ }
261
+
262
+ // At these spacings, multiples are exactly representable throughout the
263
+ // respective bounded ranges. This also covers grids with one-ULP steps.
264
+ let offsets_exact = step % offset_spacing == 0.0;
265
+ if offsets_exact && start % coordinate_spacing == 0.0 && step % coordinate_spacing == 0.0 {
266
+ return Ok(());
267
+ }
268
+
269
+ // Each rounded operation can reduce an adjacent difference by at most
270
+ // one spacing (half a spacing at each end).
271
+ let offset_error = if offsets_exact { 0.0 } else { offset_spacing };
272
+ if step.abs() - offset_error > coordinate_spacing {
273
+ return Ok(());
274
+ }
275
+
276
+ let value_at = |index: usize| start + index as f64 * step;
277
+ if value_at(last_index - 1) == last {
278
+ return Err(coincident(last_index as u64 - 1, last_index as u64));
279
+ }
280
+ // A compact schema can declare far more coordinates than can be visited
281
+ // during admission. Bound exhaustive validation when the proof above is
282
+ // inconclusive, and never accept a grid from a partial check.
283
+ const MAX_ADJACENT_CHECKS: usize = 1 << 16;
284
+ let checks = (last_index - 1).min(MAX_ADJACENT_CHECKS);
285
+ let mut previous = start;
286
+ for index in 1..=checks {
287
+ let current = value_at(index);
288
+ if current == previous {
289
+ return Err(coincident(index as u64 - 1, index as u64));
290
+ }
291
+ previous = current;
292
+ }
293
+ if checks < last_index - 1 {
294
+ return Err(DataError::Validation(format!(
295
+ "axis `{axis_name}` regular-grid ordering could not be verified within {MAX_ADJACENT_CHECKS} adjacent checks. Use grid parameters with more separation in binary64 or provide the explicit coordinates"
296
+ )));
297
+ }
298
+ Ok(())
299
+ }
300
+
221
301
  /// Strictly monotonic in EITHER direction (ascending or descending); rejects
222
302
  /// equal/unorderable neighbours.
223
303
  fn require_strictly_monotonic<T>(
@@ -1421,6 +1501,52 @@ mod tests {
1421
1501
  assert!(unordered.validate("wl", Some(3), false).is_err());
1422
1502
  }
1423
1503
 
1504
+ #[test]
1505
+ fn regular_grid_coordinates_must_be_representable_in_binary64() {
1506
+ let validate = |size, start, step| {
1507
+ coord(
1508
+ CoordinateDType::Numeric,
1509
+ true,
1510
+ CoordinateValues::RegularGrid { start, step },
1511
+ )
1512
+ .validate("wavelength", Some(size), false)
1513
+ };
1514
+
1515
+ assert!(validate(1, 1e308, 1e308).is_ok());
1516
+ assert!(validate(4, 1.0, f64::EPSILON).is_ok());
1517
+ assert!(validate(4, 1.0, -f64::EPSILON / 2.0).is_ok());
1518
+ assert!(validate(4, 0.0, f64::from_bits(1)).is_ok());
1519
+
1520
+ for (size, start, step, message) in [
1521
+ (4, 1e308, 1e308, "is not finite"),
1522
+ // A fused multiply-add would report a finite endpoint.
1523
+ (3, -1e308, 1e308, "is not finite"),
1524
+ (4, 1e16, 1.0, "coincide in binary64"),
1525
+ (4, -1e16, -1.0, "coincide in binary64"),
1526
+ // Both the first and last adjacent pairs differ.
1527
+ (5, 9_007_199_254_740_992.0, 1.5, "indices 2 and 3 coincide"),
1528
+ ] {
1529
+ let error = validate(size, start, step).unwrap_err();
1530
+ assert_eq!(error.code(), "data_contract_validation");
1531
+ let diagnostic = error.to_string();
1532
+ assert!(diagnostic.contains("`wavelength`"), "{diagnostic}");
1533
+ assert!(diagnostic.contains(message), "{diagnostic}");
1534
+ }
1535
+
1536
+ let inconclusive = validate(1 << 18, 1.0, -f64::EPSILON / 2.0).unwrap_err();
1537
+ assert!(inconclusive
1538
+ .to_string()
1539
+ .contains("ordering could not be verified"));
1540
+
1541
+ #[cfg(target_pointer_width = "64")]
1542
+ {
1543
+ let last_distinct_index = 1 << f64::MANTISSA_DIGITS;
1544
+ assert!(validate(last_distinct_index / 2, 0.0, 1.0).is_ok());
1545
+ assert!(validate(last_distinct_index / 2 + 1, 0.0, 0.1).is_ok());
1546
+ assert!(validate(last_distinct_index + 2, 0.0, 1.0).is_err());
1547
+ }
1548
+ }
1549
+
1424
1550
  #[test]
1425
1551
  fn explicit_coordinates_must_match_known_size_and_be_non_empty() {
1426
1552
  let wrong_len = coord(CoordinateDType::Numeric, false, explicit(nums(&[1.0, 2.0])));