Skip to content

Commit 2e9626e

Browse files
fix the to_df in AlgorithmResult and Edges (#1820)
* fix the `to_df` in AlgorithmResult such that it returns nodes instead of useless internal ids * remove PyDirection and make Direction directly constructable from string in python * fix the dataframe test * return node id instead of view * detect exploded edges in to_df * add test to check that calling `edges.to_df(explode=True)` is the same as `edges.explode().to_df()` * fix docstring
1 parent c8ac55e commit 2e9626e

File tree

15 files changed

+70
-120
lines changed

15 files changed

+70
-120
lines changed

python/python/raphtory/__init__.pyi

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ class AlgorithmResult:
100100
A sorted vector of tuples containing keys of type `H` and values of type `Y`.
101101
"""
102102

103-
def to_df(self):
103+
def to_df(self) -> DataFrame:
104104
"""
105105
Creates a dataframe from the result
106106
107107
Returns:
108-
A `pandas.DataFrame` containing the result
108+
DataFrame: A `pandas.DataFrame` containing the result
109109
"""
110110

111111
def to_string(self):
@@ -5656,14 +5656,6 @@ class PropertyFilter:
56565656
def __init__(self):
56575657
"""Initialize self. See help(type(self)) for accurate signature."""
56585658

5659-
class PyDirection:
5660-
"""A direction used by an edge, being incoming or outgoing"""
5661-
5662-
def __init__(self, direction):
5663-
"""Initialize self. See help(type(self)) for accurate signature."""
5664-
5665-
def as_str(self): ...
5666-
56675659
class PyGraphEncoder:
56685660
def __init__(self):
56695661
"""Initialize self. See help(type(self)) for accurate signature."""

python/python/raphtory/algorithms/__init__.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def average_degree(g: GraphView):
4646
def balance(
4747
g: GraphView,
4848
name: str = "weight",
49-
direction: Direction = "BOTH",
49+
direction: Direction = "both",
5050
threads: Optional[int] = None,
5151
) -> AlgorithmResult:
5252
"""
@@ -57,7 +57,7 @@ def balance(
5757
Arguments:
5858
g (GraphView): The graph view on which the operation is to be performed.
5959
name (str): The name of the edge property used as the weight. Defaults to "weight".
60-
direction (Direction): Specifies the direction of the edges to be considered for summation. Defaults to "BOTH".
60+
direction (Direction): Specifies the direction of the edges to be considered for summation. Defaults to "both".
6161
* "OUT": Only consider outgoing edges.
6262
* "IN": Only consider incoming edges.
6363
* "BOTH": Consider both outgoing and incoming edges. This is the default.
@@ -108,7 +108,7 @@ def dijkstra_single_source_shortest_paths(
108108
g: GraphView,
109109
source: InputNode,
110110
targets: list[InputNode],
111-
direction: Direction = "BOTH",
111+
direction: Direction = "both",
112112
weight: str = "weight",
113113
) -> dict:
114114
"""
@@ -118,7 +118,7 @@ def dijkstra_single_source_shortest_paths(
118118
g (GraphView): The graph to search in.
119119
source (InputNode): The source node.
120120
targets (list[InputNode]): A list of target nodes.
121-
direction (Direction): The direction of the edges to be considered for the shortest path. Defaults to "BOTH".
121+
direction (Direction): The direction of the edges to be considered for the shortest path. Defaults to "both".
122122
weight (str): The name of the weight property for the edges. Defaults to "weight".
123123
124124
Returns:

python/python/raphtory/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
dict[str, "Prop"],
1414
]
1515

16-
Direction = Literal["IN", "OUT", "BOTH"]
16+
Direction = Literal["in", "out", "both"]
1717

1818
InputNode = Union[int, str, "Node"]
1919

python/tests/test_algorithms.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import pandas as pd
33
import pandas.core.frame
4-
from raphtory import Graph, PersistentGraph, PyDirection
4+
from raphtory import Graph, PersistentGraph
55
from raphtory import algorithms
66
from raphtory import graph_loader
77

@@ -223,8 +223,8 @@ def test_algo_result():
223223
assert len(actual.group_by()[1]) == 8
224224
assert type(actual.to_df()) == pandas.core.frame.DataFrame
225225
df = actual.to_df()
226-
expected_result = pd.DataFrame({"Key": [1], "Value": [1]})
227-
row_with_one = df[df["Key"] == 1]
226+
expected_result = pd.DataFrame({"Node": 1, "Value": [1]})
227+
row_with_one = df[df["Node"] == 1]
228228
row_with_one.reset_index(inplace=True, drop=True)
229229
assert row_with_one.equals(expected_result)
230230
# Algo Str u64
@@ -254,7 +254,7 @@ def test_algo_result():
254254
"8": 0.11786468661230831,
255255
}
256256
assert actual.get_all_with_names() == expected_result
257-
assert actual.get("Not a node") == None
257+
assert actual.get("Not a node") is None
258258
assert len(actual.to_df()) == 8
259259
# algo str vector
260260
actual = algorithms.temporally_reachable_nodes(g, 20, 11, [1, 2], [4, 5])
@@ -463,19 +463,13 @@ def test_balance_algorithm():
463463
]
464464
for src, dst, val, time in edges_str:
465465
g.add_edge(time, src, dst, {"value_dec": val})
466-
result = algorithms.balance(
467-
g, "value_dec", PyDirection("BOTH"), None
468-
).get_all_with_names()
466+
result = algorithms.balance(g, "value_dec", "both", None).get_all_with_names()
469467
assert result == {"1": -26.0, "2": 7.0, "3": 12.0, "4": 5.0, "5": 2.0}
470468

471-
result = algorithms.balance(
472-
g, "value_dec", PyDirection("IN"), None
473-
).get_all_with_names()
469+
result = algorithms.balance(g, "value_dec", "in", None).get_all_with_names()
474470
assert result == {"1": 6.0, "2": 12.0, "3": 15.0, "4": 20.0, "5": 2.0}
475471

476-
result = algorithms.balance(
477-
g, "value_dec", PyDirection("OUT"), None
478-
).get_all_with_names()
472+
result = algorithms.balance(g, "value_dec", "out", None).get_all_with_names()
479473
assert result == {"1": -32.0, "2": -5.0, "3": -3.0, "4": -15.0, "5": 0.0}
480474

481475

python/tests/test_graph_conversions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,11 @@ def test_to_df():
11241124
pd.read_json(base_dir / "expected/dataframe_output/edge_df_explode.json"),
11251125
)
11261126

1127+
compare_df(
1128+
g.edges.explode().to_df(),
1129+
pd.read_json(base_dir / "expected/dataframe_output/edge_df_explode.json"),
1130+
)
1131+
11271132
# compare_df(
11281133
# g.edges.to_df(explode=True, convert_datetime=True),
11291134
# pd.read_json(base_dir / "expected/dataframe_output/edge_df_explode_datetime.json")

python/tests/test_graphdb/test_graphdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
import pandas.core.frame
99
import pytest
10-
from raphtory import Graph, PersistentGraph, PyDirection
10+
from raphtory import Graph, PersistentGraph
1111
from raphtory import algorithms
1212
from raphtory import graph_loader
1313
import tempfile

python/tests/test_graphdb/test_iterables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pandas as pd
55
import pandas.core.frame
66
import pytest
7-
from raphtory import Graph, PersistentGraph, PyDirection
7+
from raphtory import Graph, PersistentGraph
88
from raphtory import algorithms
99
from raphtory import graph_loader
1010
import tempfile

python/tests/test_loaders/test_load_from_pandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pandas as pd
77
import pandas.core.frame
88
import pytest
9-
from raphtory import Graph, PersistentGraph, PyDirection
9+
from raphtory import Graph, PersistentGraph
1010
from raphtory import algorithms
1111
from raphtory import graph_loader
1212
import tempfile
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::core::Direction;
2+
use pyo3::{exceptions::PyTypeError, FromPyObject, PyAny, PyResult};
3+
4+
impl<'source> FromPyObject<'source> for Direction {
5+
fn extract(ob: &'source PyAny) -> PyResult<Self> {
6+
let value: &str = ob.extract()?;
7+
match value {
8+
"out" => Ok(Direction::OUT),
9+
"in" => Ok(Direction::IN),
10+
"both" => Ok(Direction::BOTH),
11+
_ => Err(PyTypeError::new_err(PyTypeError::new_err(
12+
"Direction must be one of { 'out', 'in', 'both' }",
13+
))),
14+
}
15+
}
16+
}

raphtory-api/src/python/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod arcstr;
2+
mod direction;
23
mod gid;

0 commit comments

Comments
 (0)