Skip to content

Commit

Permalink
groupby and k hop changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdpedigo committed Apr 8, 2024
1 parent 57b80fe commit c1c7a1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 7 additions & 2 deletions networkframe/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ def __iter__(self):
for target_group, target_objects in self._target_groupby:
yield target_group, self._frame.loc[:, target_objects.index]

def apply_nodes(self, func):
pass
# def apply_nodes(self, func):
# nodes = self._frame.nodes

# if isinstance(func, str):
# if func == 'n_possible':



def apply_edges(self, func, columns=None):
by = self.by
Expand Down
31 changes: 30 additions & 1 deletion networkframe/networkframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def __len__(self) -> int:
The number of nodes in the network.
"""
return len(self.nodes)

def deepcopy(self):
return copy.deepcopy(self)

Expand Down Expand Up @@ -1220,6 +1220,35 @@ def k_hop_neighborhood(
select_indices
return self.query_nodes("index in @select_indices", local_dict=locals())

def k_hop_decomposition(self, k: int, directed: bool = False) -> pd.Series:
"""
Return the k-hop decomposition of the network.
This function returns a series of NetworkFrames, each representing the k-hop
neighborhood of a node.
Parameters
----------
k
The number of hops to consider.
"""
if k < 0:
raise ValueError("k must be non-negative.")

sparse_adjacency = self.to_sparse_adjacency()

from scipy.sparse.csgraph import dijkstra

dists = dijkstra(sparse_adjacency, directed=directed, limit=k, unweighted=True)
mask = ~np.isinf(dists)

out = {}
for iloc in range(len(self.nodes)):
select_indices = self.nodes.index[mask[iloc]]
sub_nf = self.query_nodes("index in @select_indices", local_dict=locals())
out[self.nodes.index[iloc]] = sub_nf
return pd.Series(out)

def condense(
self,
by: Union[Any, list],
Expand Down

0 comments on commit c1c7a1d

Please sign in to comment.