Skip to content

Commit

Permalink
update hif
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Aug 22, 2024
1 parent 00cfd40 commit e7b8371
Showing 1 changed file with 34 additions and 48 deletions.
82 changes: 34 additions & 48 deletions xgi/readwrite/hif.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,17 @@ def to_hif(G, path):
data["network-type"] = "directed"

# get node data
try:
isolates = set(G.nodes.isolates())
nodes_with_attrs = set(n for n in G.nodes if G.nodes[n])
for n in isolates.union(nodes_with_attrs):
attr = {"attr": G.nodes[n]} if G.nodes[n] else {}
data["nodes"].append(IDDict({"node": n}) + attr)

except KeyError:
raise XGIError("Node attributes not saved!")

try:
empty = set(G.edges.empty())
edges_with_attrs = set(e for e in G.edges if G.edges[e])
for e in empty.union(edges_with_attrs):
attr = {"attr": G.edges[e]} if G.edges[e] else {}
data["edges"].append(IDDict({"edge": e}) + attr)

except KeyError:
raise XGIError("Edge attributes not saved!")
isolates = set(G.nodes.isolates())
nodes_with_attrs = set(n for n in G.nodes if G.nodes[n])
for n in isolates.union(nodes_with_attrs):
attr = {"attr": G.nodes[n]} if G.nodes[n] else {}
data["nodes"].append(IDDict({"node": n}) + attr)

empty = set(G.edges.empty())
edges_with_attrs = set(e for e in G.edges if G.edges[e])
for e in empty.union(edges_with_attrs):
attr = {"attr": G.edges[e]} if G.edges[e] else {}
data["edges"].append(IDDict({"edge": e}) + attr)

# hyperedge dict
if data["network-type"] == "directed":
Expand Down Expand Up @@ -155,12 +147,8 @@ def _empty_edge(network_type):
else:
network_type = "undirected"

if network_type == "asc":
# Convert to simplicial complex after
G = Hypergraph()
elif network_type == "undirected":
if network_type in {"asc", "undirected"}:
G = Hypergraph()
network_type = "undirected"
elif network_type == "directed":
G = DiHypergraph()
else:
Expand All @@ -171,28 +159,24 @@ def _empty_edge(network_type):
G._net_attr.update(data["metadata"])

# Import network structure through incidence records
try:
if network_type == "directed":
edgedict = defaultdict(lambda: {"in": set(), "out": set()})
else:
edgedict = defaultdict(set)

for record in data["incidences"]:
n = record["node"]
e = record["edge"]
if "attr" in record:
attr = record["attr"]
else:
attr = {}
if network_type == "directed":
edgedict = defaultdict(lambda: {"in": set(), "out": set()})
else:
edgedict = defaultdict(set)

if network_type == "directed":
d = record["direction"]
G.add_node_to_edge(n, e, d, **attr)
else:
G.add_node_to_edge(n, e, **attr)
for record in data["incidences"]:
n = record["node"]
e = record["edge"]
if "attr" in record:
attr = record["attr"]
else:
attr = {}

except KeyError as e:
raise XGIError("Failed to import incidences.") from e
if network_type == "directed":
d = record["direction"]
G.add_node_to_edge(n, e, d)
else:
G.add_node_to_edge(n, e)

# import node attributes if they exist
if "nodes" in data:
Expand All @@ -202,10 +186,11 @@ def _empty_edge(network_type):
attr = record["attr"]
else:
attr = {}
if n not in G._node:
G.add_node(n, **attr)
else:
G.set_node_attributes({n: attr})

if n not in G._node:
G.add_node(n, **attr)
else:
G.set_node_attributes({n: attr})

# import edge attributes if they exist
if "edges" in data:
Expand All @@ -215,6 +200,7 @@ def _empty_edge(network_type):
attr = record["attr"]
else:
attr = {}

if e not in G._edge:
G.add_edge(_empty_edge(network_type), e, **attr)
else:
Expand Down

0 comments on commit e7b8371

Please sign in to comment.