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
use crate::{
core::entities::nodes::node_ref::{AsNodeRef, NodeRef},
db::{
api::{
state::{
ops::{ArrowMap, DynNodeFilter, HistoryOp, IntoArrowNodeOp, Map},
AvgIntervalStruct, IntervalStruct, IntervalsStruct, LazyNodeState, NodeOp,
NodeState,
},
view::{DynamicGraph, GraphViewOps},
},
graph::{node::NodeView, nodes::Nodes},
},
impl_lazy_node_state, impl_lazy_node_state_ord, impl_node_state, impl_node_state_ops,
impl_node_state_ord_ops,
python::{
graph::history::PyIntervals,
types::{repr::Repr, wrappers::iterators::PyBorrowingIterator},
utils::PyNodeRef,
},
};
use pyo3::{
exceptions::{PyKeyError, PyTypeError},
prelude::*,
types::{PyDict, PyNotImplemented},
IntoPyObjectExt,
};
use std::collections::HashMap;
use crate::db::{api::state::OutputTypedNodeState, graph::nodes::IntoDynNodes};
pub(crate) use crate::{
db::api::state::{ops::IntoDynNodeOp, NodeStateOps, OrderedNodeStateOps},
prelude::*,
python::graph::node_state::node_state::ops::NodeFilterOp,
};
type HistoryIntervals<G> = ArrowMap<Map<HistoryOp<'static, G>, PyIntervals>, IntervalsStruct>;
impl_lazy_node_state!(
IntervalsView<HistoryIntervals<DynamicGraph>>,
"NodeStateIntervals",
"Intervals"
);
impl_node_state!(
NodeStateIntervals<PyIntervals>,
"NodeStateIntervals",
"Intervals"
);
// Custom functions for LazyNodeState<Intervals>
#[pymethods]
impl IntervalsView {
/// Calculate the mean interval in milliseconds for each node.
///
/// Returns:
/// IntervalsFloatView: A lazy view over the mean interval between consecutive timestamps for each node. The mean is None if there is fewer than 1 interval.
pub fn mean(
&self,
) -> LazyNodeState<
'static,
ArrowMap<Map<HistoryIntervals<DynamicGraph>, Option<f64>>, AvgIntervalStruct>,
DynamicGraph,
DynamicGraph,
DynNodeFilter,
> {
let op = self.inner.op.clone().map(|v| v.mean());
LazyNodeState::new(op.into_arrow_node_op(), self.inner.nodes())
}
/// Calculate the median interval in milliseconds for each node.
///
/// Returns:
/// IntervalsIntegerView: A lazy view over the median interval between consecutive timestamps for each node. The median is None if there is fewer than 1 interval.
pub fn median(
&self,
) -> LazyNodeState<
'static,
ArrowMap<Map<HistoryIntervals<DynamicGraph>, Option<i64>>, IntervalStruct>,
DynamicGraph,
DynamicGraph,
DynNodeFilter,
> {
let op = self.inner.op.clone().map(|v| v.median());
LazyNodeState::new(op.into_arrow_node_op(), self.inner.nodes())
}
/// Calculate the maximum interval in milliseconds for each node.
///
/// Returns:
/// IntervalsIntegerView: A lazy view over the maximum interval between consecutive timestamps for each node. The maximum is None if there is fewer than 1 interval.
pub fn max(
&self,
) -> LazyNodeState<
'static,
ArrowMap<Map<HistoryIntervals<DynamicGraph>, Option<i64>>, IntervalStruct>,
DynamicGraph,
DynamicGraph,
DynNodeFilter,
> {
let op = self.inner.op.clone().map(|v| v.max());
LazyNodeState::new(op.into_arrow_node_op(), self.inner.nodes())
}
/// Calculate the minimum interval in milliseconds for each node.
///
/// Returns:
/// IntervalsIntegerView: A lazy view over the minimum interval between consecutive timestamps for each node. The minimum is None if there is fewer than 1 interval.
pub fn min(
&self,
) -> LazyNodeState<
'static,
ArrowMap<Map<HistoryIntervals<DynamicGraph>, Option<i64>>, IntervalStruct>,
DynamicGraph,
DynamicGraph,
DynNodeFilter,
> {
let op = self.inner.op.clone().map(|v| v.min());
LazyNodeState::new(op.into_arrow_node_op(), self.inner.nodes())
}
}
// types needed for LazyNodeStates generated above.
type IntervalsFloat<G> = ArrowMap<Map<HistoryIntervals<G>, Option<f64>>, AvgIntervalStruct>;
impl_lazy_node_state_ord!(
IntervalsFloatView<IntervalsFloat<DynamicGraph>>,
"NodeStateOptionF64",
"Optional[float]"
);
type IntervalsI64<G> = ArrowMap<Map<HistoryIntervals<G>, Option<i64>>, IntervalStruct>;
impl_lazy_node_state_ord!(
IntervalsIntegerView<IntervalsI64<DynamicGraph>>,
"NodeStateOptionI64",
"Optional[int]"
);
// Custom functions for computed NodeState<Intervals>
#[pymethods]
impl NodeStateIntervals {
/// Calculate the mean interval in milliseconds for each node.
///
/// Returns:
/// NodeStateOptionF64: A NodeState with the computed mean interval between consecutive timestamps for each node. The mean is None if there is fewer than 1 interval.
pub fn mean(&self) -> NodeState<'static, Option<f64>, DynamicGraph> {
let values = self
.inner
.iter_values()
.map(|v| v.mean())
.collect::<Vec<Option<f64>>>()
.into();
NodeState::new(self.inner.graph().clone(), values, self.inner.ids().clone())
}
/// Calculate the median interval in milliseconds for each node.
///
/// Returns:
/// NodeStateOptionI64: A NodeState with the computed median interval between consecutive timestamps for each node. The median is None if there is fewer than 1 interval.
pub fn median(&self) -> NodeState<'static, Option<i64>, DynamicGraph> {
let values = self
.inner
.iter_values()
.map(|v| v.median())
.collect::<Vec<Option<i64>>>()
.into();
NodeState::new(self.inner.graph().clone(), values, self.inner.ids().clone())
}
/// Calculate the maximum interval in milliseconds for each node.
///
/// Returns:
/// NodeStateOptionI64: A NodeState with the computed maximum interval between consecutive timestamps for each node. The maximum is None if there is fewer than 1 interval.
pub fn max(&self) -> NodeState<'static, Option<i64>, DynamicGraph> {
let values = self
.inner
.iter_values()
.map(|v| v.max())
.collect::<Vec<Option<i64>>>()
.into();
NodeState::new(self.inner.graph().clone(), values, self.inner.ids().clone())
}
/// Calculate the minimum interval in milliseconds for each node.
///
/// Returns:
/// NodeStateOptionI64: A NodeState with the computed minimum interval between consecutive timestamps for each node. The minimum is None if there is fewer than 1 interval.
pub fn min(&self) -> NodeState<'static, Option<i64>, DynamicGraph> {
let values = self
.inner
.iter_values()
.map(|v| v.min())
.collect::<Vec<Option<i64>>>()
.into();
NodeState::new(self.inner.graph().clone(), values, self.inner.ids().clone())
}
/// Collect all intervals in milliseconds into a list for each node.
///
/// Returns:
/// list[list[int]]: List of intervals in milliseconds for each node.
pub fn to_list(&self) -> Vec<Vec<i64>> {
self.inner
.iter_values()
.map(|v| v.to_list())
.collect::<Vec<Vec<i64>>>()
}
}