22
33import warnings
44from itertools import chain
5- from typing import Collection , Optional
5+ from typing import Any , Collection , Optional
66from uuid import uuid4
77
88import pandas as pd
9- from graphdatascience import Graph , GraphDataScience
10- from graphdatascience .graph .v2 import GraphV2
9+ from graphdatascience import GraphDataScience
1110from graphdatascience .session import AuraGraphDataScience
1211
1312from neo4j_viz .colors import NEO4J_COLORS_DISCRETE , ColorSpace
1413
14+ from ._gds_compat import IS_GDS_2 , GdsGraph , _catalog , _check_graph_type , _degree_centrality
1515from .pandas import _from_dfs
1616from .visualization_graph import VisualizationGraph
1717
1818
1919def _fetch_node_dfs (
2020 gds : GraphDataScience | AuraGraphDataScience ,
21- G : GraphV2 ,
21+ G : Any ,
2222 node_properties_by_label : dict [str , list [str ]],
2323 node_labels : Collection [str ],
2424 additional_db_node_properties : list [str ],
2525) -> dict [str , pd .DataFrame ]:
2626 return {
27- lbl : gds . v2 . graph .node_properties .stream (
27+ lbl : _catalog ( gds ) .node_properties .stream (
2828 G ,
2929 node_properties = node_properties_by_label [lbl ],
3030 node_labels = [lbl ],
@@ -34,14 +34,14 @@ def _fetch_node_dfs(
3434 }
3535
3636
37- def _fetch_rel_dfs (gds : GraphDataScience | AuraGraphDataScience , G : GraphV2 ) -> list [pd .DataFrame ]:
37+ def _fetch_rel_dfs (gds : GraphDataScience | AuraGraphDataScience , G : Any ) -> list [pd .DataFrame ]:
3838 rel_props = G .relationship_properties ()
3939
4040 rel_dfs : list [pd .DataFrame ] = []
4141
4242 # Have to call per stream per relationship type as there was a bug in GDS < 2.21
4343 for rel_type , props in rel_props .items ():
44- rel_df = gds . v2 . graph .relationships .stream (
44+ rel_df = _catalog ( gds ) .relationships .stream (
4545 G , relationship_types = [rel_type ], relationship_properties = list (props )
4646 )
4747
@@ -62,7 +62,7 @@ def _fetch_rel_dfs(gds: GraphDataScience | AuraGraphDataScience, G: GraphV2) ->
6262
6363def from_gds (
6464 gds : GraphDataScience | AuraGraphDataScience ,
65- G : Graph | GraphV2 ,
65+ G : Any ,
6666 node_properties : Optional [list [str ]] = None ,
6767 db_node_properties : Optional [list [str ]] = None ,
6868 max_node_count : int = 10_000 ,
@@ -97,8 +97,9 @@ def from_gds(
9797 """
9898 if db_node_properties is None :
9999 db_node_properties = []
100- if isinstance (G , Graph ):
101- G_v2 = gds .v2 .graph .get (G .name ())
100+ _check_graph_type (G )
101+ if not IS_GDS_2 and not isinstance (G , GdsGraph ):
102+ G_v2 = _catalog (gds ).get (G .name ())
102103 else :
103104 G_v2 = G
104105
@@ -127,7 +128,7 @@ def from_gds(
127128 )
128129 sampling_ratio = float (max_node_count ) / node_count
129130 sample_name = f"neo4j-viz_sample_{ uuid4 ()} "
130- G_fetched , _ = gds . v2 . graph .sample .rwr (
131+ G_fetched , _ = _catalog ( gds ) .sample .rwr (
131132 G_v2 , sample_name , sampling_ratio = sampling_ratio , node_label_stratification = True
132133 )
133134 else :
@@ -139,7 +140,7 @@ def from_gds(
139140 # as a temporary property to ensure that we have at least one property for each label to fetch
140141 if sum ([len (props ) == 0 for props in node_properties_by_label .values ()]) > 0 :
141142 property_name = f"neo4j-viz_property_{ uuid4 ()} "
142- gds . v2 . degree_centrality .mutate (G_fetched , mutate_property = property_name )
143+ _degree_centrality ( gds ) .mutate (G_fetched , mutate_property = property_name )
143144 for props in node_properties_by_label .values ():
144145 props .append (property_name )
145146
@@ -155,7 +156,7 @@ def from_gds(
155156 if G_fetched .name () != G .name ():
156157 G_fetched .drop ()
157158 elif property_name is not None :
158- gds . v2 . graph .node_properties .drop (G_fetched , node_properties = [property_name ])
159+ _catalog ( gds ) .node_properties .drop (G_fetched , node_properties = [property_name ])
159160
160161 for df in node_dfs .values ():
161162 if property_name is not None and property_name in df .columns :
0 commit comments