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
use crate::{
api::core::storage::timeindex::AsTime,
db::{
api::view::{DynamicGraph, EdgeSelect, IntoDynBoxed, IntoDynamic, StaticGraphViewOps},
graph::{
edge::EdgeView,
edges::{Edges, NestedEdges},
},
},
errors::GraphError,
prelude::*,
python::{
filter::filter_expr::PyFilterExpr,
graph::{
history::{HistoryIterable, NestedHistoryIterable},
properties::{MetadataListList, MetadataView, PropertiesView, PyNestedPropsIterable},
},
types::{
repr::{iterator_repr, Repr},
wrappers::iterables::{
ArcStringIterable, ArcStringVecIterable, BoolIterable, EventTimeIterable,
GIDGIDIterable, NestedArcStringIterable, NestedArcStringVecIterable,
NestedBoolIterable, NestedEventTimeIterable, NestedGIDGIDIterable,
NestedOptionEventTimeIterable, OptionEventTimeIterable,
},
},
utils::export::{create_row, extract_properties, get_column_names_from_props},
},
};
use pyo3::{prelude::*, types::PyDict};
use raphtory_api::core::storage::arc_str::ArcStr;
use raphtory_storage::core_ops::CoreGraphOps;
use rayon::{iter::IntoParallelIterator, prelude::*};
use std::collections::HashMap;
/// A list of edges that can be iterated over.
#[pyclass(name = "Edges", module = "raphtory", frozen)]
pub struct PyEdges {
edges: Edges<'static, DynamicGraph>,
}
impl_edgeviewops!(PyEdges, edges, Edges<'static, DynamicGraph>, "Edges");
impl_iterable_mixin!(
PyEdges,
edges,
Vec<EdgeView<DynamicGraph>>,
"list[Edge]",
"edge",
|edges: &Edges<'static, DynamicGraph>| edges.clone().into_iter()
);
impl<'graph, G: GraphViewOps<'graph>> Repr for Edges<'graph, G> {
fn repr(&self) -> String {
format!("Edges({})", iterator_repr(self.iter()))
}
}
impl<'py, G: StaticGraphViewOps + IntoDynamic> IntoPyObject<'py> for Edges<'static, G> {
type Target = PyEdges;
type Output = Bound<'py, PyEdges>;
type Error = <Self::Target as IntoPyObject<'py>>::Error;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let base_graph = self.base_graph.into_dynamic();
let edges = self.edges;
PyEdges {
edges: Edges { base_graph, edges },
}
.into_pyobject(py)
}
}
impl<G: StaticGraphViewOps + IntoDynamic> From<Edges<'static, G>> for PyEdges {
fn from(value: Edges<'static, G>) -> Self {
let base_graph = value.base_graph.into_dynamic();
Self {
edges: Edges::new(base_graph, value.edges),
}
}
}
#[pymethods]
impl PyEdges {
fn __getitem__(&self, filter: PyFilterExpr) -> PyResult<PyEdges> {
let r = self.edges.select(filter)?;
Ok(PyEdges::from(r))
}
/// Returns the number of edges.
///
/// Returns:
/// int:
fn count(&self) -> usize {
self.edges.len()
}
/// Returns the earliest time of the edges.
///
/// Returns:
/// OptionEventTimeIterable: Iterable of `EventTime`s.
#[getter]
fn earliest_time(&self) -> OptionEventTimeIterable {
let edges = self.edges.clone();
(move || edges.earliest_time()).into()
}
/// Returns the latest times of the edges.
///
/// Returns:
/// OptionEventTimeIterable: Iterable of `EventTime`s.
#[getter]
fn latest_time(&self) -> OptionEventTimeIterable {
let edges = self.edges.clone();
(move || edges.latest_time()).into()
}
/// Returns the times of exploded edges
///
/// Returns:
/// EventTimeIterable: Iterable of `EventTime`s.
#[getter]
fn time(&self) -> Result<EventTimeIterable, GraphError> {
match self.edges.time().next() {
Some(Err(err)) => Err(err),
_ => {
let edges = self.edges.clone();
Ok((move || edges.time().map(|t| t.unwrap())).into())
}
}
}
/// Returns all properties of the edges
///
/// Returns:
/// PropertiesView:
#[getter]
fn properties(&self) -> PropertiesView {
let edges = self.edges.clone();
(move || edges.properties()).into()
}
/// Returns all the metadata of the edges
///
/// Returns:
/// MetadataView:
#[getter]
fn metadata(&self) -> MetadataView {
let edges = self.edges.clone();
(move || edges.metadata()).into()
}
/// Returns all ids of the edges.
///
/// Returns:
/// GIDGIDIterable:
#[getter]
fn id(&self) -> GIDGIDIterable {
let edges = self.edges.clone();
(move || edges.id()).into()
}
/// Returns a history object for each edge containing time entries for when the edge is added or change to the edge is made.
///
/// Returns:
/// HistoryIterable: An iterable of history objects, one for each edge.
#[getter]
fn history(&self) -> HistoryIterable {
let edges = self.edges.clone();
(move || edges.history().map(|history| history.into_arc_dyn())).into()
}
/// Returns a history object for each edge containing their deletion times.
///
/// Returns:
/// HistoryIterable: An iterable of history objects, one for each edge.
#[getter]
fn deletions(&self) -> HistoryIterable {
let edges = self.edges.clone();
(move || edges.deletions().map(|history| history.into_arc_dyn())).into()
}
/// Check if the edges are valid (i.e. not deleted).
///
/// Returns:
/// BoolIterable:
fn is_valid(&self) -> BoolIterable {
let edges = self.edges.clone();
(move || edges.is_valid()).into()
}
/// Check if the edges are active (there is at least one update during this time).
///
/// Returns:
/// BoolIterable:
fn is_active(&self) -> BoolIterable {
let edges = self.edges.clone();
(move || edges.is_active()).into()
}
/// Check if the edges are on the same node.
///
/// Returns:
/// BoolIterable:
fn is_self_loop(&self) -> BoolIterable {
let edges = self.edges.clone();
(move || edges.is_self_loop()).into()
}
/// Check if the edges are deleted.
///
/// Returns:
/// BoolIterable:
fn is_deleted(&self) -> BoolIterable {
let edges = self.edges.clone();
(move || edges.is_deleted()).into()
}
/// Get the layer name that all edges belong to - assuming they only belong to one layer
///
/// Returns:
/// ArcStringIterable:
#[getter]
fn layer_name(&self) -> Result<ArcStringIterable, GraphError> {
match self.edges.layer_name().next() {
Some(Err(err)) => Err(err),
_ => {
let edges = self.edges.clone();
Ok((move || edges.layer_name().map(|layer| layer.unwrap())).into())
}
}
}
/// Get the layer names that all edges belong to - assuming they only belong to one layer.
///
/// Returns:
/// ArcStringVecIterable:
#[getter]
fn layer_names(&self) -> ArcStringVecIterable {
let edges = self.edges.clone();
(move || edges.layer_names()).into()
}
/// Converts the graph's edges into a Pandas DataFrame.
///
/// This method will create a DataFrame with the following columns:
/// - "src": The source node of the edge.
/// - "dst": The destination node of the edge.
/// - "layer": The layer of the edge.
/// - "properties": The properties of the edge.
/// - "update_history": The update history of the edge. This column will be included if `include_update_history` is set to `true`.
///
/// Args:
/// include_property_history (bool): A boolean, if set to `True`, the history of each property is included, if `False`, only the latest value is shown. Ignored if exploded. Defaults to True.
/// convert_datetime (bool): A boolean, if set to `True` will convert the timestamp to python datetimes. Defaults to False.
/// explode (bool): A boolean, if set to `True`, will explode each edge update into its own row. Defaults to False.
///
/// Returns:
/// DataFrame: If successful, this PyObject will be a Pandas DataFrame.
#[pyo3(signature = (include_property_history = true, convert_datetime = false, explode = false))]
pub fn to_df(
&self,
include_property_history: bool,
convert_datetime: bool,
mut explode: bool,
) -> PyResult<PyObject> {
let mut column_names = vec![
String::from("src"),
String::from("dst"),
String::from("layer"),
];
let edge_meta = self.edges.base_graph.edge_meta();
let is_prop_both_temp_and_const = get_column_names_from_props(&mut column_names, edge_meta);
let mut edges = self.edges.explode_layers();
if explode {
edges = self.edges.explode_layers().explode();
}
explode = explode
|| edges
.iter()
.next()
.filter(|e| e.edge.time().is_some())
.is_some();
let edge_tuples: Vec<_> = edges
.collect()
.into_par_iter()
.flat_map(|item| {
let mut properties_map: HashMap<String, Prop> = HashMap::new();
let mut prop_time_dict: HashMap<i64, HashMap<String, Prop>> = HashMap::new();
extract_properties(
include_property_history,
convert_datetime,
explode,
&column_names,
&is_prop_both_temp_and_const,
&item.properties(),
&item.metadata(),
&mut properties_map,
&mut prop_time_dict,
item.start().map(|t| t.t()).unwrap_or(0),
);
let row_header: Vec<Prop> = vec![
Prop::from(item.src().name()),
Prop::from(item.dst().name()),
Prop::from(item.layer_name().unwrap_or(ArcStr::from(""))),
];
let start_point = 3;
let history = item.history().t().collect();
create_row(
convert_datetime,
explode,
&column_names,
properties_map,
prop_time_dict,
row_header,
start_point,
history,
)
})
.collect();
Python::with_gil(|py| {
let pandas = PyModule::import(py, "pandas")?;
let kwargs = PyDict::new(py);
kwargs.set_item("columns", column_names)?;
Ok(pandas
.call_method("DataFrame", (edge_tuples,), Some(&kwargs))?
.unbind())
})
}
}
impl Repr for PyEdges {
fn repr(&self) -> String {
format!("Edges({})", iterator_repr(self.edges.iter()))
}
}
#[pyclass(name = "NestedEdges", module = "raphtory")]
pub struct PyNestedEdges {
edges: NestedEdges<'static, DynamicGraph>,
}
impl_edgeviewops!(
PyNestedEdges,
edges,
NestedEdges<'static, DynamicGraph>,
"NestedEdges"
);
impl_iterable_mixin!(
PyNestedEdges,
edges,
Vec<Vec<EdgeView<DynamicGraph>>>,
"list[list[Edges]]",
"edge"
);
impl<'py, G: StaticGraphViewOps + IntoDynamic> IntoPyObject<'py> for NestedEdges<'static, G> {
type Target = PyNestedEdges;
type Output = Bound<'py, Self::Target>;
type Error = <Self::Target as IntoPyObject<'py>>::Error;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let edges = NestedEdges {
nodes: self.nodes,
graph: self.graph.into_dynamic(),
edges: self.edges,
};
PyNestedEdges { edges }.into_pyobject(py)
}
}
impl<'graph, G: GraphViewOps<'graph>> Repr for NestedEdges<'graph, G> {
fn repr(&self) -> String {
format!("NestedEdges({})", iterator_repr(self.iter()))
}
}
impl<G: StaticGraphViewOps + IntoDynamic> From<NestedEdges<'static, G>> for PyNestedEdges {
fn from(value: NestedEdges<'static, G>) -> Self {
let base_graph = value.graph.into_dynamic();
Self {
edges: NestedEdges::new(base_graph, value.nodes, value.edges),
}
}
}
#[pymethods]
impl PyNestedEdges {
fn __getitem__(&self, filter: PyFilterExpr) -> PyResult<PyNestedEdges> {
let r = self.edges.select(filter)?;
Ok(PyNestedEdges::from(r))
}
/// Returns the earliest time of the edges.
///
/// Returns:
/// NestedOptionEventTimeIterable: A nested iterable of `EventTime`s.
#[getter]
fn earliest_time(&self) -> NestedOptionEventTimeIterable {
let edges = self.edges.clone();
(move || edges.earliest_time()).into()
}
/// Returns the latest time of the edges.
///
/// Returns:
/// NestedOptionEventTimeIterable: A nested iterable of `EventTime`s.
#[getter]
fn latest_time(&self) -> NestedOptionEventTimeIterable {
let edges = self.edges.clone();
(move || edges.latest_time()).into()
}
/// Returns the times of exploded edges.
///
/// Returns:
/// NestedEventTimeIterable: A nested iterable of `EventTime`s.
///
/// Raises:
/// GraphError: If a graph error occurs (e.g. the edges are not exploded).
#[getter]
fn time(&self) -> Result<NestedEventTimeIterable, GraphError> {
match self.edges.time().flatten().next() {
Some(Err(err)) => Err(err),
_ => {
let edges = self.edges.clone();
Ok((move || {
edges
.time()
.map(|t_iter| t_iter.map(|t| t.unwrap()).into_dyn_boxed())
.into_dyn_boxed()
})
.into())
}
}
}
/// Returns the name of the layer the edges belong to - assuming they only belong to one layer.
///
/// Returns:
/// NestedArcStringIterable:
#[getter]
fn layer_name(&self) -> Result<NestedArcStringIterable, GraphError> {
match self.edges.layer_name().flatten().next() {
Some(Err(err)) => Err(err),
_ => {
let edges = self.edges.clone();
Ok((move || {
edges
.layer_name()
.map(|layer_name_iter| {
layer_name_iter
.map(|layer_name| layer_name.unwrap())
.into_dyn_boxed()
})
.into_dyn_boxed()
})
.into())
}
}
}
/// Returns the names of the layers the edges belong to.
///
/// Returns:
/// NestedArcStringVecIterable:
#[getter]
fn layer_names(&self) -> NestedArcStringVecIterable {
let edges = self.edges.clone();
(move || edges.layer_names()).into()
}
// FIXME: needs a view that allows indexing into the properties
/// Returns all properties of the edges
///
/// Returns:
/// PyNestedPropsIterable:
#[getter]
fn properties(&self) -> PyNestedPropsIterable {
let edges = self.edges.clone();
(move || edges.properties()).into()
}
/// Get a view of the metadata only.
///
/// Returns:
/// MetadataListList:
#[getter]
pub fn metadata(&self) -> MetadataListList {
let edges = self.edges.clone();
(move || edges.metadata()).into()
}
/// Returns all ids of the edges.
///
/// Returns:
/// NestedGIDGIDIterable:
#[getter]
fn id(&self) -> NestedGIDGIDIterable {
let edges = self.edges.clone();
(move || edges.id()).into()
}
/// Returns a history object for each edge containing time entries for when the edge is added or change to the edge is made.
///
/// Returns:
/// NestedHistoryIterable: A nested iterable of history objects, one for each edge.
#[getter]
fn history(&self) -> NestedHistoryIterable {
let edges = self.edges.clone();
(move || {
edges
.history()
.map(|history_iter| history_iter.map(|history| history.into_arc_dyn()))
})
.into()
}
/// Returns a history object for each edge containing their deletion times.
///
/// Returns:
/// NestedHistoryIterable: A nested iterable of history objects, one for each edge.
#[getter]
fn deletions(&self) -> NestedHistoryIterable {
let edges = self.edges.clone();
(move || {
edges
.deletions()
.map(|history_iter| history_iter.map(|history| history.into_arc_dyn()))
})
.into()
}
/// Check if edges are valid (i.e., not deleted).
///
/// Returns:
/// NestedBoolIterable:
fn is_valid(&self) -> NestedBoolIterable {
let edges = self.edges.clone();
(move || edges.is_valid()).into()
}
/// Check if the edges are active (there is at least one update during this time).
///
/// Returns:
/// NestedBoolIterable:
fn is_active(&self) -> NestedBoolIterable {
let edges = self.edges.clone();
(move || edges.is_active()).into()
}
/// Check if the edges are on the same node.
///
/// Returns:
/// NestedBoolIterable:
fn is_self_loop(&self) -> NestedBoolIterable {
let edges = self.edges.clone();
(move || edges.is_self_loop()).into()
}
/// Check if edges are deleted.
///
/// Returns:
/// NestedBoolIterable:
fn is_deleted(&self) -> NestedBoolIterable {
let edges = self.edges.clone();
(move || edges.is_deleted()).into()
}
}