Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ def get_networkx_graph(self, uri: str) -> nx.Graph:
nx.Graph: The NetworkX graph.

"""
return ACCEPTED_EXTENSIONS[uri.split(".")[-1]](uri)
extension = next(
(candidate for candidate in sorted(ACCEPTED_EXTENSIONS, key=len, reverse=True) if uri.endswith(candidate)),
None,
)
if extension is None:
raise ValueError(f"Unsupported graph file: {uri}")
return ACCEPTED_EXTENSIONS[extension](uri)
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,15 @@ def _read_csv_edgelist(self, filepath: str) -> nx.Graph:
if len(df.columns) < 2:
raise ValueError("CSV file must have at least 2 columns for source and target nodes")

# Create graph from edgelist
G = nx.Graph()

# Get column names
source_col = df.columns[0]
target_col = df.columns[1]

# Add edges
for _, row in df.iterrows():
source = row[source_col]
target = row[target_col]

# Add edge with any additional attributes
edge_attrs = {}
for col in df.columns[2:]:
edge_attrs[col] = row[col]

G.add_edge(source, target, **edge_attrs)

return G
return nx.from_pandas_edgelist(
df,
source=source_col,
target=target_col,
edge_attr=list(df.columns[2:]) or None,
create_using=nx.Graph,
)

except Exception as e:
# If CSV reading fails, try as plain text edgelist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ def test_can_count_motifs():
r = FilesystemGraphHostProvider()
# Query the provider for the motif count.
assert r.get_motif_count(uri, "A->B") == 12


def test_can_read_compressed_graphml():
g = nx.path_graph(4)
with tempfile.NamedTemporaryFile(suffix=".graphml.gz") as f:
nx.write_graphml(g, f.name)
provider = FilesystemGraphHostProvider()

assert nx.is_isomorphic(g, provider.get_networkx_graph("file://" + f.name))
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ def test_display_name_survives_provider_restart(tmp_path):
restarted = TemporaryGraphHostProvider(str(tmp_path))

assert restarted.get_file_info(temp_id)["display_name"] == "My graph"


def test_csv_attributes_are_preserved(tmp_path):
provider = TemporaryGraphHostProvider(str(tmp_path))
path = tmp_path / "attributes.csv"
path.write_text("source,target,weight,label\na,b,2,edge\n")

graph = provider._read_csv_edgelist(str(path))

assert graph.edges["a", "b"]["weight"] == 2
assert graph.edges["a", "b"]["label"] == "edge"
Loading