Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phenograph/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,12 @@ def cluster(
print("Setting directed=False because prune=True")
directed = False

kernelargs = {}
if n_jobs == 1:
kernel = jaccard_kernel
else:
kernelargs["n_jobs"] = n_jobs
kernel = parallel_jaccard_kernel
kernelargs = {}

# Start timer
tic = time.time()
Expand Down
26 changes: 20 additions & 6 deletions phenograph/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,30 @@ def calc_jaccard(i, idx):
return idx[i], coefficients


def parallel_jaccard_kernel(idx):
def parallel_jaccard_kernel(idx, n_jobs=-1):
"""Compute Jaccard coefficient between nearest-neighbor sets in parallel
:param idx: n-by-k integer matrix of k-nearest neighbors

:return (i, j, s): row indices, column indices, and nonzero values for a sparse adjacency matrix
Parameters
----------
idx
n-by-k integer matrix of k-nearest neighbors
n_jobs
Number of concurrently running workers. If 1 is given, no parallelism is
used. If set to -1, all CPUs are used. For n_jobs below -1, `n_cpus + 1 + n_jobs`
are used.

Returns
-------
i, j, s
row indices, column indices, and nonzero values for a sparse adjacency matrix
"""
if n_jobs == -1:
n_jobs = len(os.sched_getaffinity(0))
if n_jobs < -1:
n_jobs = len(os.sched_getaffinity(0)) + 1 + n_jobs

n = len(idx)
with closing(Pool()) as pool:
with closing(Pool(n_jobs)) as pool:
jaccard_values = pool.starmap(calc_jaccard, zip(range(n), repeat(idx)))

graph = sp.lil_matrix((n, n), dtype=float)
Expand Down Expand Up @@ -269,7 +285,6 @@ def get_modularity(msg):
run = 0
updated = 0
while run - updated < 20 and run < max_runs and (time.time() - tic) < time_limit:

# run community
fout = open(filename + ".tree", "w")
args = [
Expand All @@ -291,7 +306,6 @@ def get_modularity(msg):

# continue only if we've reached a higher modularity than before
if q[-1] - Q > tol:

Q = q[-1]
updated = run

Expand Down