dagex 2026.21.0

A pure Rust DAG executor supporting implicit node connections, branching, and config sweeps
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
//! GraphData container for passing typed data between nodes
//!
//! This module provides a generic container that can hold various data types
//! (numbers, arrays, complex arrays, strings, etc.) and be passed through graph nodes.
//!
//! Large data types (Vec, Array) are wrapped in Arc for efficient cloning across nodes.

use std::collections::HashMap;
use std::sync::Arc;

#[cfg(feature = "radar_examples")]
use ndarray::Array1;
#[cfg(feature = "radar_examples")]
use num_complex::Complex;

#[cfg(feature = "python")]
use pyo3::PyObject;

/// GraphData enum supporting multiple data types
/// 
/// Large data types (Vec, Array) are wrapped in Arc for efficient sharing between nodes.
/// Small types (Int, Float, String) remain unwrapped as they're cheap to clone.
#[derive(Clone, Debug)]
pub enum GraphData {
    /// 64-bit integer (small, no Arc needed)
    Int(i64),
    /// 64-bit floating point (small, no Arc needed)
    Float(f64),
    /// UTF-8 string (already uses internal Arc-like optimization)
    String(String),
    /// Vector of floats (Arc-wrapped for efficient cloning)
    FloatVec(Arc<Vec<f64>>),
    /// Vector of integers (Arc-wrapped for efficient cloning)
    IntVec(Arc<Vec<i64>>),
    /// Complex number (small, no Arc needed)
    #[cfg(feature = "radar_examples")]
    Complex(Complex<f64>),
    /// 1D array of floats (Arc-wrapped for efficient cloning)
    #[cfg(feature = "radar_examples")]
    FloatArray(Arc<Array1<f64>>),
    /// 1D array of complex numbers (Arc-wrapped for efficient cloning)
    #[cfg(feature = "radar_examples")]
    ComplexArray(Arc<Array1<Complex<f64>>>),
    /// Nested map of GraphData (for structured data)
    Map(HashMap<String, GraphData>),
    /// Python object (opaque, no conversion)
    #[cfg(feature = "python")]
    PyObject(PyObject),
    /// Empty/null value
    None,
}

impl GraphData {
    /// Create an Int variant
    pub fn int(value: i64) -> Self {
        GraphData::Int(value)
    }

    /// Create a Float variant
    pub fn float(value: f64) -> Self {
        GraphData::Float(value)
    }

    /// Create a String variant
    pub fn string(value: impl Into<String>) -> Self {
        GraphData::String(value.into())
    }

    /// Create a FloatVec variant (wraps in Arc)
    pub fn float_vec(value: Vec<f64>) -> Self {
        GraphData::FloatVec(Arc::new(value))
    }

    /// Create an IntVec variant (wraps in Arc)
    pub fn int_vec(value: Vec<i64>) -> Self {
        GraphData::IntVec(Arc::new(value))
    }

    /// Create a Map variant
    pub fn map(value: HashMap<String, GraphData>) -> Self {
        GraphData::Map(value)
    }

    /// Create a None variant
    pub fn none() -> Self {
        GraphData::None
    }

    #[cfg(feature = "radar_examples")]
    /// Create a Complex variant
    pub fn complex(value: Complex<f64>) -> Self {
        GraphData::Complex(value)
    }

    #[cfg(feature = "radar_examples")]
    /// Create a FloatArray variant (wraps in Arc)
    pub fn float_array(value: Array1<f64>) -> Self {
        GraphData::FloatArray(Arc::new(value))
    }

    #[cfg(feature = "radar_examples")]
    /// Create a ComplexArray variant (wraps in Arc)
    pub fn complex_array(value: Array1<Complex<f64>>) -> Self {
        GraphData::ComplexArray(Arc::new(value))
    }

    #[cfg(feature = "python")]
    /// Create a PyObject variant (stores Python object without conversion)
    pub fn py_object(value: PyObject) -> Self {
        GraphData::PyObject(value)
    }

    /// Try to extract as i64
    pub fn as_int(&self) -> Option<i64> {
        match self {
            GraphData::Int(v) => Some(*v),
            _ => None,
        }
    }

    /// Try to extract as f64
    pub fn as_float(&self) -> Option<f64> {
        match self {
            GraphData::Float(v) => Some(*v),
            GraphData::Int(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Try to extract a scalar f64, including Python number objects when the
    /// `python` feature is enabled.  Used by `Dag::predict()` to collect
    /// Monte Carlo samples from the outputs of Python node functions.
    pub fn as_f64_lossy(&self) -> Option<f64> {
        match self {
            GraphData::Float(v) => Some(*v),
            GraphData::Int(v) => Some(*v as f64),
            #[cfg(feature = "python")]
            GraphData::PyObject(obj) => {
                pyo3::Python::with_gil(|py| {
                    obj.extract::<f64>(py)
                        .ok()
                        .or_else(|| obj.extract::<i64>(py).ok().map(|i| i as f64))
                })
            }
            _ => None,
        }
    }

    /// Try to extract as String reference
    pub fn as_string(&self) -> Option<&str> {
        match self {
            GraphData::String(s) => Some(s.as_str()),
            _ => None,
        }
    }

    /// Try to extract as `Vec<f64>` reference (dereferences Arc)
    pub fn as_float_vec(&self) -> Option<&Vec<f64>> {
        match self {
            GraphData::FloatVec(v) => Some(v.as_ref()),
            _ => None,
        }
    }

    /// Try to extract as `Vec<i64>` reference (dereferences Arc)
    pub fn as_int_vec(&self) -> Option<&Vec<i64>> {
        match self {
            GraphData::IntVec(v) => Some(v.as_ref()),
            _ => None,
        }
    }

    /// Try to extract as HashMap reference
    pub fn as_map(&self) -> Option<&HashMap<String, GraphData>> {
        match self {
            GraphData::Map(m) => Some(m),
            _ => None,
        }
    }

    #[cfg(feature = "radar_examples")]
    /// Try to extract as Complex<f64>
    pub fn as_complex(&self) -> Option<Complex<f64>> {
        match self {
            GraphData::Complex(c) => Some(*c),
            _ => None,
        }
    }

    #[cfg(feature = "radar_examples")]
    /// Try to extract as Array1<f64> reference (dereferences Arc)
    pub fn as_float_array(&self) -> Option<&Array1<f64>> {
        match self {
            GraphData::FloatArray(a) => Some(a.as_ref()),
            _ => None,
        }
    }

    #[cfg(feature = "radar_examples")]
    /// Try to extract as Array1<Complex<f64>> reference (dereferences Arc)
    pub fn as_complex_array(&self) -> Option<&Array1<Complex<f64>>> {
        match self {
            GraphData::ComplexArray(a) => Some(a.as_ref()),
            _ => None,
        }
    }

    #[cfg(feature = "python")]
    /// Try to extract as PyObject reference
    pub fn as_py_object(&self) -> Option<&PyObject> {
        match self {
            GraphData::PyObject(obj) => Some(obj),
            _ => None,
        }
    }

    /// Check if this is None
    pub fn is_none(&self) -> bool {
        matches!(self, GraphData::None)
    }

    /// Convert GraphData to a string representation (for compatibility)
    pub fn to_string_repr(&self) -> String {
        match self {
            GraphData::Int(v) => v.to_string(),
            GraphData::Float(v) => v.to_string(),
            GraphData::String(s) => s.clone(),
            GraphData::FloatVec(v) => format!("{:?}", v),
            GraphData::IntVec(v) => format!("{:?}", v),
            #[cfg(feature = "radar_examples")]
            GraphData::Complex(c) => format!("{:?}", c),
            #[cfg(feature = "radar_examples")]
            GraphData::FloatArray(a) => format!("{:?}", a),
            #[cfg(feature = "radar_examples")]
            GraphData::ComplexArray(a) => format!("{:?}", a),
            GraphData::Map(m) => format!("{:?}", m),
            #[cfg(feature = "python")]
            GraphData::PyObject(_) => "<PyObject>".to_string(),
            GraphData::None => "None".to_string(),
        }
    }

    /// Try to parse GraphData from a string
    pub fn from_string(s: &str) -> Self {
        // Try to parse as i64
        if let Ok(v) = s.parse::<i64>() {
            return GraphData::Int(v);
        }
        // Try to parse as f64
        if let Ok(v) = s.parse::<f64>() {
            return GraphData::Float(v);
        }
        // Otherwise, store as string
        GraphData::String(s.to_string())
    }
}

impl Default for GraphData {
    fn default() -> Self {
        GraphData::None
    }
}

impl From<i64> for GraphData {
    fn from(v: i64) -> Self {
        GraphData::Int(v)
    }
}

impl From<f64> for GraphData {
    fn from(v: f64) -> Self {
        GraphData::Float(v)
    }
}

impl From<String> for GraphData {
    fn from(v: String) -> Self {
        GraphData::String(v)
    }
}

impl From<&str> for GraphData {
    fn from(v: &str) -> Self {
        GraphData::String(v.to_string())
    }
}

impl From<Vec<f64>> for GraphData {
    fn from(v: Vec<f64>) -> Self {
        GraphData::FloatVec(Arc::new(v))
    }
}

impl From<Vec<i64>> for GraphData {
    fn from(v: Vec<i64>) -> Self {
        GraphData::IntVec(Arc::new(v))
    }
}

#[cfg(feature = "radar_examples")]
impl From<Complex<f64>> for GraphData {
    fn from(v: Complex<f64>) -> Self {
        GraphData::Complex(v)
    }
}

#[cfg(feature = "radar_examples")]
impl From<Array1<f64>> for GraphData {
    fn from(v: Array1<f64>) -> Self {
        GraphData::FloatArray(Arc::new(v))
    }
}

#[cfg(feature = "radar_examples")]
impl From<Array1<Complex<f64>>> for GraphData {
    fn from(v: Array1<Complex<f64>>) -> Self {
        GraphData::ComplexArray(Arc::new(v))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_int_construction() {
        let data = GraphData::int(42);
        assert_eq!(data.as_int(), Some(42));
        assert_eq!(data.as_float(), Some(42.0));
    }

    #[test]
    fn test_float_construction() {
        let data = GraphData::float(3.14);
        assert_eq!(data.as_float(), Some(3.14));
        assert!(data.as_int().is_none());
    }

    #[test]
    fn test_string_construction() {
        let data = GraphData::string("hello");
        assert_eq!(data.as_string(), Some("hello"));
    }

    #[test]
    fn test_float_vec_construction() {
        let data = GraphData::float_vec(vec![1.0, 2.0, 3.0]);
        assert_eq!(data.as_float_vec(), Some(&vec![1.0, 2.0, 3.0]));
    }

    #[test]
    fn test_int_vec_construction() {
        let data = GraphData::int_vec(vec![1, 2, 3]);
        assert_eq!(data.as_int_vec(), Some(&vec![1, 2, 3]));
    }

    #[test]
    fn test_map_construction() {
        let mut map = HashMap::new();
        map.insert("x".to_string(), GraphData::int(10));
        map.insert("y".to_string(), GraphData::float(20.5));
        let data = GraphData::map(map);

        let extracted = data.as_map().unwrap();
        assert_eq!(extracted.get("x").and_then(|d| d.as_int()), Some(10));
        assert_eq!(extracted.get("y").and_then(|d| d.as_float()), Some(20.5));
    }

    #[test]
    fn test_none_construction() {
        let data = GraphData::none();
        assert!(data.is_none());
    }

    #[test]
    fn test_from_conversions() {
        let d1: GraphData = 42i64.into();
        assert_eq!(d1.as_int(), Some(42));

        let d2: GraphData = 3.14f64.into();
        assert_eq!(d2.as_float(), Some(3.14));

        let d3: GraphData = "test".into();
        assert_eq!(d3.as_string(), Some("test"));

        let d4: GraphData = vec![1.0, 2.0].into();
        assert_eq!(d4.as_float_vec(), Some(&vec![1.0, 2.0]));
    }

    #[test]
    fn test_to_string_repr() {
        assert_eq!(GraphData::int(42).to_string_repr(), "42");
        assert_eq!(GraphData::float(3.14).to_string_repr(), "3.14");
        assert_eq!(GraphData::string("test").to_string_repr(), "test");
        assert!(GraphData::none().to_string_repr().contains("None"));
    }

    #[test]
    fn test_from_string() {
        let d1 = GraphData::from_string("42");
        assert_eq!(d1.as_int(), Some(42));

        let d2 = GraphData::from_string("3.14");
        assert_eq!(d2.as_float(), Some(3.14));

        let d3 = GraphData::from_string("not a number");
        assert_eq!(d3.as_string(), Some("not a number"));
    }
}