Coverage for kwave/executor.py: 32%

37 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-24 11:52 -0700

1import stat 

2import sys 

3import unittest.mock 

4import logging 

5 

6from pathlib import Path 

7import os 

8import h5py 

9import numpy as np 

10 

11 

12class Executor: 

13 

14 def __init__(self, device): 

15 self._is_linux = sys.platform.startswith('linux') 

16 

17 if not self._is_linux: 

18 raise NotImplementedError('Running on Windows and Mac is not implemented yet.' 

19 ' Please open an issue on Github:' 

20 ' https://github.com/waltsims/k-wave-python/issues/new') 

21 

22 binary_folder = 'linux' if self._is_linux else 'windows' 

23 binary_name = 'kspaceFirstOrder' 

24 if device == 'gpu': 

25 binary_name += '-CUDA' 

26 elif device == 'cpu': 

27 binary_name += '-OMP' 

28 else: 

29 raise ValueError("Unrecognized value passed as target device. Options are 'gpu' or 'cpu'.") 

30 

31 path_of_this_file = Path(__file__).parent.resolve() 

32 self.binary_path = path_of_this_file / 'bin' / binary_folder / binary_name 

33 

34 self._make_binary_executable() 

35 

36 def _make_binary_executable(self): 

37 self.binary_path.chmod(self.binary_path.stat().st_mode | stat.S_IEXEC) 

38 

39 def run_simulation(self, input_filename: str, output_filename: str, options: str): 

40 env_variables = 'export LD_LIBRARY_PATH=;' \ 

41 'OMP_PLACES=cores;' \ 

42 'OMP_PROC_BIND=SPREAD;' 

43 

44 command = f'{env_variables} {self.binary_path} ' \ 

45 f'-i {input_filename} -o {output_filename} {options}' 

46 

47 return_code = os.system(command) 

48 

49 try: 

50 assert return_code == 0, f'Simulation call returned code: {return_code}' 

51 except AssertionError: 

52 if isinstance(return_code, unittest.mock.MagicMock): 

53 logging.info('Skipping AssertionError in testing.') 

54 

55 with h5py.File(output_filename, 'r') as hf: 

56 sensor_data = np.array(hf['p'])[0].T 

57 

58 return sensor_data