Skip to content

Commit

Permalink
Remove l2gv2.utils.progress module
Browse files Browse the repository at this point in the history
Implement tqdm_close() to finish progressbars. The other functions
progress.reset() and progress.update() are not needed as we can
call tqdm directly.
  • Loading branch information
abhidg committed Feb 17, 2025
1 parent ea520b4 commit 9f4ef5f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
8 changes: 0 additions & 8 deletions docs/source/reference/l2gv2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ l2gv2.models module
:undoc-members:
:show-inheritance:

l2gv2.progress module
---------------------

.. automodule:: l2gv2.progress
:members:
:undoc-members:
:show-inheritance:

l2gv2.sparsify module
---------------------

Expand Down
15 changes: 8 additions & 7 deletions l2gv2/graphs/npgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import numpy as np
import torch
import numba
from tqdm import tqdm
from numba.experimental import jitclass

from ..utils import progress
from .utils import tqdm_close
from .graph import Graph


Expand Down Expand Up @@ -334,14 +335,14 @@ def _memmap_degree(edge_index, num_nodes):
degree = np.zeros(num_nodes, dtype=np.int64)
with numba.objmode:
print("computing degrees")
pbar = progress.reset(edge_index.shape[1])
pbar = tqdm(total=edge_index.shape[1])
for it, source in enumerate(edge_index[0]):
degree[source] += 1
if it % 1000000 == 0 and it > 0:
with numba.objmode:
progress.update(pbar, 1000000)
pbar.update(1000000)
with numba.objmode:
progress.close(pbar)
tqdm_close(pbar)
return degree


Expand Down Expand Up @@ -458,7 +459,7 @@ def partition_graph_edges(self, partition, self_loops):
num_edges = self.num_edges
with numba.objmode:
print("finding partition edges")
pbar = progress.reset(num_edges)
pbar = tqdm(total=num_edges)
num_clusters = partition.max() + 1
edge_counts = np.zeros((num_clusters, num_clusters), dtype=np.int64)
for i, (source, target) in enumerate(self.edge_index.T):
Expand All @@ -468,9 +469,9 @@ def partition_graph_edges(self, partition, self_loops):
edge_counts[source, target] += 1
if i % 1000000 == 0 and i > 0:
with numba.objmode:
progress.update(pbar, 1000000)
pbar.update(1000000)
with numba.objmode:
progress.close(pbar)
tqdm_close(pbar)
index = np.nonzero(edge_counts)
partition_edges = np.vstack(index)
weights = np.empty((len(index[0]),), dtype=np.int64)
Expand Down
9 changes: 4 additions & 5 deletions l2gv2/patch/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import numba

from local2global_embedding.network import TGraph, NPGraph
from local2global_embedding import progress


def distributed_clustering(graph: TGraph, beta, rounds=None, patience=3, min_samples=2) -> torch.Tensor:
Expand Down Expand Up @@ -136,7 +135,7 @@ def _fennel_clustering(edge_index, adj_index, num_nodes, num_clusters, load_limi
deltas = - alpha * gamma * (partition_sizes ** (gamma - 1))

with numba.objmode:
progress.reset(num_nodes)
pbar = tqdm(total=num_nodes)

for it in range(num_iters):
not_converged = 0
Expand Down Expand Up @@ -169,17 +168,17 @@ def _fennel_clustering(edge_index, adj_index, num_nodes, num_clusters, load_limi
if i % 10000 == 0 and i > 0:
progress_it = i
with numba.objmode:
progress.update(10000)
pbar.update(10000)
with numba.objmode:
progress.update(num_nodes - progress_it)
pbar.update(num_nodes - progress_it)

print('iteration: ' + str(it) + ', not converged: ' + str(not_converged))

if not_converged == 0:
print(f'converged after {it} iterations.')
break
with numba.objmode:
progress.close()
tqdm_close(pbar)

return clusters

Expand Down
7 changes: 6 additions & 1 deletion l2gv2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
# SOFTWARE.
"""TODO: module docstring for utils.py"""

from tempfile import TemporaryFile
from time import perf_counter

from tqdm import tqdm
import torch
import torch.nn


def tqdm_close(t: tqdm):
t.update(t.total - t.n)
t.close()

def speye(n: int, dtype: torch.dtype = torch.float) -> torch.Tensor:
"""identity matrix of dimension n as sparse_coo_tensor."""
return torch.sparse_coo_tensor(
Expand Down

0 comments on commit 9f4ef5f

Please sign in to comment.