|
1 | 1 | import os |
2 | | - |
3 | 2 | import numpy as np |
4 | 3 | from caiman.utils.utils import load_dict_from_hdf5 |
5 | 4 | from caiman.source_extraction.cnmf import cnmf |
|
12 | 11 | CaimanSeriesExtensions, |
13 | 12 | set_parent_raw_data_path, |
14 | 13 | ) |
15 | | -from mesmerize_core.batch_utils import DATAFRAME_COLUMNS, COMPUTE_BACKEND_SUBPROCESS, get_full_raw_data_path |
| 14 | +from mesmerize_core.batch_utils import ( |
| 15 | + DATAFRAME_COLUMNS, |
| 16 | + COMPUTE_BACKEND_SUBPROCESS, |
| 17 | + COMPUTE_BACKEND_LOCAL, |
| 18 | + COMPUTE_BACKEND_ASYNC, |
| 19 | + get_full_raw_data_path) |
16 | 20 | from mesmerize_core.utils import IS_WINDOWS |
| 21 | +from mesmerize_core.algorithms._utils import ensure_server |
17 | 22 | from uuid import uuid4 |
18 | 23 | from typing import * |
19 | 24 | import pytest |
|
30 | 35 | import tifffile |
31 | 36 | from copy import deepcopy |
32 | 37 |
|
| 38 | +pytest_plugins = ('pytest_asyncio',) |
| 39 | + |
33 | 40 | tmp_dir = Path(os.path.dirname(os.path.abspath(__file__)), "tmp") |
34 | 41 | vid_dir = Path(os.path.dirname(os.path.abspath(__file__)), "videos") |
35 | 42 | ground_truths_dir = Path(os.path.dirname(os.path.abspath(__file__)), "ground_truths") |
@@ -1254,3 +1261,48 @@ def test_cache(): |
1254 | 1261 | output2 = df.iloc[1].cnmf.get_output(return_copy=False) |
1255 | 1262 | assert(hex(id(output)) == hex(id(output2))) |
1256 | 1263 | assert(hex(id(cnmf.cnmf_cache.get_cache().iloc[-1]["return_val"])) == hex(id(output))) |
| 1264 | + |
| 1265 | + |
| 1266 | +def test_backends(): |
| 1267 | + """test subprocess, local, and async_local backend""" |
| 1268 | + set_parent_raw_data_path(vid_dir) |
| 1269 | + algo = "mcorr" |
| 1270 | + df, batch_path = _create_tmp_batch() |
| 1271 | + input_movie_path = get_datafile(algo) |
| 1272 | + |
| 1273 | + # make small version of movie for quick testing |
| 1274 | + movie = tifffile.imread(input_movie_path) |
| 1275 | + small_movie_path = input_movie_path.parent.joinpath("small_movie.tif") |
| 1276 | + tifffile.imwrite(small_movie_path, movie[:1001]) |
| 1277 | + print(input_movie_path) |
| 1278 | + |
| 1279 | + # put backends that can run in the background first to save time |
| 1280 | + backends = [COMPUTE_BACKEND_SUBPROCESS, COMPUTE_BACKEND_ASYNC, COMPUTE_BACKEND_LOCAL] |
| 1281 | + for backend in backends: |
| 1282 | + df.caiman.add_item( |
| 1283 | + algo="mcorr", |
| 1284 | + item_name=f"test-{backend}", |
| 1285 | + input_movie_path=small_movie_path, |
| 1286 | + params=test_params["mcorr"], |
| 1287 | + ) |
| 1288 | + |
| 1289 | + # run using each backend |
| 1290 | + procs = [] |
| 1291 | + with ensure_server(None) as (dview, _): |
| 1292 | + for backend, (_, item) in zip(backends, df.iterrows()): |
| 1293 | + procs.append(item.caiman.run(backend=backend, dview=dview, wait=False)) |
| 1294 | + |
| 1295 | + # wait for all to finish |
| 1296 | + for proc in procs: |
| 1297 | + proc.wait() |
| 1298 | + |
| 1299 | + # compare results |
| 1300 | + df = load_batch(batch_path) |
| 1301 | + for i, item in df.iterrows(): |
| 1302 | + output = item.mcorr.get_output() |
| 1303 | + |
| 1304 | + if i == 0: |
| 1305 | + # save to compare to other results |
| 1306 | + first_output = output |
| 1307 | + else: |
| 1308 | + numpy.testing.assert_array_equal(output, first_output) |
0 commit comments