darabos commited on
Commit
6c9b792
·
1 Parent(s): ac9f9c5

Add an "Import GraphML" box.

Browse files
lynxkite-graph-analytics/src/lynxkite_plugins/graph_analytics/lynxkite_ops.py CHANGED
@@ -12,6 +12,7 @@ import pandas as pd
12
  import polars as pl
13
  import traceback
14
  import typing
 
15
 
16
  ENV = "LynxKite Graph Analytics"
17
  op = ops.op_registration(ENV)
@@ -52,6 +53,7 @@ class Bundle:
52
  d = dict(graph.nodes(data=True))
53
  nodes = pd.DataFrame(d.values(), index=d.keys())
54
  nodes["id"] = nodes.index
 
55
  return cls(
56
  dfs={"edges": edges, "nodes": nodes},
57
  relations=[
@@ -182,6 +184,24 @@ def import_csv(
182
  )
183
 
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  @op("Create scale-free graph")
186
  def create_scale_free_graph(*, nodes: int = 10):
187
  """Creates a scale-free graph with the given number of nodes."""
 
12
  import polars as pl
13
  import traceback
14
  import typing
15
+ import zipfile
16
 
17
  ENV = "LynxKite Graph Analytics"
18
  op = ops.op_registration(ENV)
 
53
  d = dict(graph.nodes(data=True))
54
  nodes = pd.DataFrame(d.values(), index=d.keys())
55
  nodes["id"] = nodes.index
56
+ nodes.drop(columns=["index"], inplace=True)
57
  return cls(
58
  dfs={"edges": edges, "nodes": nodes},
59
  relations=[
 
184
  )
185
 
186
 
187
+ @op("Import GraphML")
188
+ def import_graphml(*, filename: str):
189
+ """Imports a GraphML file."""
190
+ if filename.endswith(".zip"):
191
+ with zipfile.ZipFile(filename, "r") as z:
192
+ for fn in z.namelist():
193
+ if fn.endswith(".graphml"):
194
+ with z.open(fn) as f:
195
+ G = nx.read_graphml(f)
196
+ break
197
+ else:
198
+ raise ValueError("No GraphML file found in the ZIP archive.")
199
+ else:
200
+ with z.open(filename) as f:
201
+ G = nx.read_graphml(f)
202
+ return G
203
+
204
+
205
  @op("Create scale-free graph")
206
  def create_scale_free_graph(*, nodes: int = 10):
207
  """Creates a scale-free graph with the given number of nodes."""