Skip to content

Commit

Permalink
Remove id from all other files
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Nov 13, 2024
1 parent 5ee0397 commit 0ad4172
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion xgi/convert/bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def from_bipartite_graph(G, create_using=None, dual=False):
H.add_nodes_from(nodes)
for edge in edges:
nodes_in_edge = list(G.neighbors(edge))
H.add_edge(nodes_in_edge, id=edge)
H.add_edge(nodes_in_edge, idx=edge)
return H.dual() if dual else H


Expand Down
14 changes: 7 additions & 7 deletions xgi/convert/hypergraph_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,28 @@ def from_hypergraph_dict(data, nodetype=None, edgetype=None, max_order=None):
raise XGIError("Failed to get hypergraph data attributes.")

try:
for id, dd in data["node-data"].items():
for idx, dd in data["node-data"].items():
if nodetype is not None:
try:
id = nodetype(id)
idx = nodetype(idx)
except ValueError as e:
raise TypeError(
f"Failed to convert edge IDs to type {nodetype}."
) from e
H.add_node(id, **dd)
H.add_node(idx, **dd)
except KeyError:
raise XGIError("Failed to import node attributes.")

try:
for id, edge in data["edge-dict"].items():
for idx, edge in data["edge-dict"].items():
if max_order and len(edge) > max_order + 1:
continue
if edgetype is not None:
try:
id = edgetype(id)
idx = edgetype(idx)
except ValueError as e:
raise TypeError(
f"Failed to convert the edge with ID {id} to type {edgetype}."
f"Failed to convert the edge with ID {idx} to type {edgetype}."
) from e

if nodetype is not None:
Expand All @@ -141,7 +141,7 @@ def from_hypergraph_dict(data, nodetype=None, edgetype=None, max_order=None):
raise TypeError(
f"Failed to convert nodes to type {nodetype}."
) from e
H.add_edge(edge, id)
H.add_edge(edge, idx)

except KeyError as e:
raise XGIError("Failed to import edge dictionary.") from e
Expand Down
28 changes: 14 additions & 14 deletions xgi/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ def __iter__(self):
"""Returns an iterator over the IDs."""
return iter(self._ids)

def __getitem__(self, id):
def __getitem__(self, idx):
"""Get the attributes of the ID.
Parameters
----------
id : hashable
idx : hashable
node or edge ID
Returns
Expand All @@ -143,13 +143,13 @@ def __getitem__(self, id):
hypergraph, or if id is not hashable.
"""
if id not in self:
raise IDNotFound(f"The ID {id} is not in this view")
return self._id_attr[id]
if idx not in self:
raise IDNotFound(f"The ID {idx} is not in this view")
return self._id_attr[idx]

def __contains__(self, id):
def __contains__(self, idx):
"""Checks whether the ID is in the hypergraph"""
return id in self._ids
return idx in self._ids

def __str__(self):
"""Returns a string of the list of IDs."""
Expand Down Expand Up @@ -342,14 +342,14 @@ def filterby_attr(self, attr, val, mode="eq", missing=None):
)
return type(self).from_view(self, bunch)

def neighbors(self, id, s=1):
def neighbors(self, idx, s=1):
"""Find the neighbors of an ID.
The neighbors of an ID are those IDs that share at least one bipartite ID.
Parameters
----------
id : hashable
idx : hashable
ID to find neighbors of.
s : int, optional
The intersection size s for two edges or nodes to be considered neighbors.
Expand Down Expand Up @@ -377,15 +377,15 @@ def neighbors(self, id, s=1):
"""
if s == 1:
return {
i for n in self._id_dict[id] for i in self._bi_id_dict[n]
}.difference({id})
i for n in self._id_dict[idx] for i in self._bi_id_dict[n]
}.difference({idx})
else:
return {
i
for n in self._id_dict[id]
for n in self._id_dict[idx]
for i in self._bi_id_dict[n]
if len(self._id_dict[id].intersection(self._id_dict[i])) >= s
}.difference({id})
if len(self._id_dict[idx].intersection(self._id_dict[i])) >= s
}.difference({idx})

def duplicates(self):
"""Find IDs that have a duplicate.
Expand Down
10 changes: 5 additions & 5 deletions xgi/drawing/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ def draw_node_labels(
ax = ax_nodes

if node_labels is True:
node_labels = {id: id for id in H.nodes}
node_labels = {idx: idx for idx in H.nodes}

# Plot the labels in the last layer
if zorder is None:
Expand Down Expand Up @@ -1175,11 +1175,11 @@ def draw_hyperedge_labels(
ax = ax_edges

if hyperedge_labels is True:
hyperedge_labels = {id: id for id in H.edges}
hyperedge_labels = {idx: idx for idx in H.edges}

text_items = {}
for id, label in hyperedge_labels.items():
he = H.edges.members(id)
for idx, label in hyperedge_labels.items():
he = H.edges.members(idx)
coordinates = [[pos[n][0], pos[n][1]] for n in he]
x, y = np.mean(coordinates, axis=0)

Expand Down Expand Up @@ -1228,7 +1228,7 @@ def draw_hyperedge_labels(
bbox=bbox,
clip_on=clip_on_edges,
)
text_items[id] = t
text_items[idx] = t

return text_items

Expand Down
4 changes: 2 additions & 2 deletions xgi/drawing/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ def edge_positions_from_barycenters(H, node_pos):

H = to_hypergraph(H)
edge_pos = {}
for id, e in H.edges.members(dtype=dict).items():
edge_pos[id] = np.mean([node_pos[n] for n in e], axis=0)
for idx, e in H.edges.members(dtype=dict).items():
edge_pos[idx] = np.mean([node_pos[n] for n in e], axis=0)

return edge_pos

Expand Down
8 changes: 4 additions & 4 deletions xgi/generators/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,10 @@ def dcsbm_hypergraph(k1, k2, g1, g2, omega, seed=None):

kappa1 = defaultdict(lambda: 0)
kappa2 = defaultdict(lambda: 0)
for id, g in g1.items():
kappa1[g] += k1[id]
for id, g in g2.items():
kappa2[g] += k2[id]
for idx, g in g1.items():
kappa1[g] += k1[idx]
for idx, g in g2.items():
kappa2[g] += k2[idx]

for group1 in community1_nodes.keys():
for group2 in community2_nodes.keys():
Expand Down
8 changes: 4 additions & 4 deletions xgi/generators/uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ def uniform_hypergraph_configuration_model(k, m, seed=None):
"Increasing the degree of random nodes so that it is."
)
random_ids = random.sample(list(k.keys()), int(round(m - remainder)))
for id in random_ids:
k[id] = k[id] + 1
for idx in random_ids:
k[idx] = k[idx] + 1

stubs = []
# Creating the list to index through
for id in k:
stubs.extend([id] * int(k[id]))
for idx in k:
stubs.extend([idx] * int(k[idx]))

H = empty_hypergraph()
H.add_nodes_from(k.keys())
Expand Down
12 changes: 6 additions & 6 deletions xgi/readwrite/bigg_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def _bigg_to_dihypergraph(d_index, d_model):

DH = DiHypergraph()

id = d_model["id"]
idx = d_model["id"]

DH["name"] = id
DH["name"] = idx

info = next((item for item in d_index["results"] if item["bigg_id"] == id), None)
info = next((item for item in d_index["results"] if item["bigg_id"] == idx), None)
DH["organism"] = info["organism"]

for m in d_model["metabolites"]:
Expand Down Expand Up @@ -139,7 +139,7 @@ def _bigg_to_dihypergraph(d_index, d_model):
if not reactants and not products:
warn(f"{r['id']} is an empty reaction!")
continue
DH.add_edge((reactants, products), id=r["id"], name=r["name"])
DH.add_edge((reactants, products), idx=r["id"], name=r["name"])

# reversible
if l < 0 and u > 0:
Expand All @@ -153,10 +153,10 @@ def _bigg_to_dihypergraph(d_index, d_model):
warn(f"{r['id']} is an empty reaction!")
continue
# add forward reaction
DH.add_edge((reactants, products), id=r["id"], name=r["name"])
DH.add_edge((reactants, products), idx=r["id"], name=r["name"])
# add reverse reaction
DH.add_edge(
(products, reactants), id=str(r["id"]) + "_reverse", name=r["name"]
(products, reactants), idx=str(r["id"]) + "_reverse", name=r["name"]
)

return DH
6 changes: 3 additions & 3 deletions xgi/readwrite/bipartite.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def generate_bipartite_edgelist(H, delimiter=" "):
A iterator of strings
Each entry is a line to be written to the output file.
"""
for id in H.edges:
for node in H.edges.members(id):
yield delimiter.join(map(str, [node, id]))
for idx in H.edges:
for node in H.edges.members(idx):
yield delimiter.join(map(str, [node, idx]))


def write_bipartite_edgelist(H, path, delimiter=" ", encoding="utf-8"):
Expand Down
4 changes: 2 additions & 2 deletions xgi/readwrite/edgelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def generate_edgelist(H, delimiter=" "):
iterator of strings
Each entry is a line for the file to write.
"""
for id in H.edges:
e = H.edges.members(id)
for idx in H.edges:
e = H.edges.members(idx)
yield delimiter.join(map(str, e))


Expand Down
20 changes: 10 additions & 10 deletions xgi/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def powerset(
return chain.from_iterable(combinations(s, r) for r in range(start, max_size + 1))


def update_uid_counter(H, new_id):
def update_uid_counter(H, idx):
"""
Helper function to make sure the uid counter is set correctly after
adding an edge with a user-provided ID.
Expand All @@ -165,19 +165,19 @@ def update_uid_counter(H, new_id):
----------
H : xgi.Hypergraph
Hypergraph of which to update the uid counter
id : any hashable type
idx : any hashable type
User-provided ID.
"""
uid = next(H._edge_uid)
if (
not isinstance(new_id, str)
and not isinstance(new_id, tuple)
and float(new_id).is_integer()
and uid <= new_id
not isinstance(idx, str)
and not isinstance(idx, tuple)
and float(idx).is_integer()
and uid <= idx
):
# tuple comes from merging edges and doesn't have as as_integer() method.
start = int(new_id) + 1
start = int(idx) + 1
# we set the start at one plus the maximum edge ID that is an integer,
# because count() only yields integer IDs.
else:
Expand Down Expand Up @@ -409,8 +409,8 @@ def convert_labels_to_integers(net, label_attribute="label", in_place=False):
edge_attrs = net._edge_attr.copy()
edges = net._edge.copy()
net.clear(remove_net_attr=False)
net.add_nodes_from((id, deepcopy(node_attrs[n])) for n, id in node_dict.items())
net.set_node_attributes({id: {label_attribute: n} for n, id in node_dict.items()})
net.add_nodes_from((idx, deepcopy(node_attrs[n])) for n, idx in node_dict.items())
net.set_node_attributes({idx: {label_attribute: n} for n, idx in node_dict.items()})
if isinstance(net, SimplicialComplex):
net.add_simplices_from(
(
Expand Down Expand Up @@ -442,7 +442,7 @@ def convert_labels_to_integers(net, label_attribute="label", in_place=False):
for e, edge in edges.items()
)

net.set_edge_attributes({id: {label_attribute: e} for e, id in edge_dict.items()})
net.set_edge_attributes({idx: {label_attribute: e} for e, idx in edge_dict.items()})

if not in_place:
return net
Expand Down

0 comments on commit 0ad4172

Please sign in to comment.