|
1 | | -import caiman as cm |
2 | 1 | from contextlib import contextmanager |
3 | | -from ipyparallel import DirectView |
4 | | -from multiprocessing.pool import Pool |
5 | 2 | import os |
6 | 3 | import psutil |
7 | | -from typing import Union, Optional, Generator |
| 4 | +from typing import Optional, Union, Generator |
| 5 | + |
| 6 | +import caiman as cm |
| 7 | +from caiman.cluster import setup_cluster |
| 8 | +from ipyparallel import DirectView |
| 9 | +from multiprocessing.pool import Pool |
| 10 | + |
8 | 11 |
|
9 | 12 | Cluster = Union[Pool, DirectView] |
10 | 13 |
|
11 | 14 | def get_n_processes(dview: Optional[Cluster]) -> int: |
12 | 15 | """Infer number of processes in a multiprocessing or ipyparallel cluster""" |
13 | 16 | if isinstance(dview, Pool) and hasattr(dview, '_processes'): |
14 | | - return dview._processes |
| 17 | + return dview._processes # type: ignore |
15 | 18 | elif isinstance(dview, DirectView): |
16 | 19 | return len(dview) |
17 | 20 | else: |
@@ -39,9 +42,10 @@ def ensure_server(dview: Optional[Cluster]) -> Generator[tuple[Cluster, int], No |
39 | 42 | n_processes = psutil.cpu_count() - 1 |
40 | 43 |
|
41 | 44 | # Start cluster for parallel processing |
42 | | - _, dview, n_processes = cm.cluster.setup_cluster( |
| 45 | + _, dview, n_processes = setup_cluster( |
43 | 46 | backend="multiprocessing", n_processes=n_processes, single_thread=False |
44 | 47 | ) |
| 48 | + assert isinstance(dview, Pool) and isinstance(n_processes, int), 'setup_cluster with multiprocessing did not return a Pool' |
45 | 49 | try: |
46 | 50 | yield dview, n_processes |
47 | 51 | finally: |
|
0 commit comments