Skip to content

Commit d9862a9

Browse files
Defer kwave import to when it needs to be used (#377)
This is needed to prevent kwave from getting auto imported just by importing openlifu. Otherwise we can never jump ahead of kwave's auto-download of its binaries to offer alternative ways of getting the binaries within openlifu.
1 parent 9689068 commit d9862a9

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/openlifu/sim/kwave_if.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,15 @@
33
import logging
44
from typing import List
55

6-
import kwave
7-
import kwave.data
86
import numpy as np
97
import xarray as xa
10-
from kwave.kgrid import kWaveGrid
11-
from kwave.kmedium import kWaveMedium
12-
from kwave.ksensor import kSensor
13-
from kwave.ksource import kSource
14-
from kwave.kspaceFirstOrder3D import kspaceFirstOrder3D
15-
from kwave.options.simulation_execution_options import SimulationExecutionOptions
16-
from kwave.options.simulation_options import SimulationOptions
17-
from kwave.utils.kwave_array import kWaveArray
188

199
from openlifu import xdc
2010
from openlifu.util.units import getunitconversion
2111

2212

2313
def get_kgrid(coords: xa.Coordinates, t_end = 0, dt = 0, sound_speed_ref=1500, cfl=0.5):
14+
from kwave.kgrid import kWaveGrid
2415
units = [coords[dim].attrs['units'] for dim in coords.dims]
2516
if not all(unit == units[0] for unit in units):
2617
raise ValueError("All coordinates must have the same units")
@@ -40,6 +31,9 @@ def get_karray(arr: xdc.Transducer,
4031
upsampling_rate: int = 5,
4132
translation: List[float] = [0.,0.,0.],
4233
rotation: List[float] = [0.,0.,0.]):
34+
import kwave
35+
import kwave.data
36+
from kwave.utils.kwave_array import kWaveArray
4337
karray = kWaveArray(bli_tolerance=bli_tolerance, upsampling_rate=upsampling_rate,
4438
single_precision=True)
4539
for el in arr.elements:
@@ -53,6 +47,7 @@ def get_karray(arr: xdc.Transducer,
5347
return karray
5448

5549
def get_medium(params: xa.Dataset, ref_values_only: bool = False):
50+
from kwave.kmedium import kWaveMedium
5651
if ref_values_only:
5752
medium = kWaveMedium(sound_speed=params['sound_speed'].attrs['ref_value'],
5853
density=params['density'].attrs['ref_value'],
@@ -68,11 +63,13 @@ def get_medium(params: xa.Dataset, ref_values_only: bool = False):
6863
return medium
6964

7065
def get_sensor(kgrid, record=['p_max','p_min']):
66+
from kwave.ksensor import kSensor
7167
sensor_mask = np.ones([kgrid.Nx, kgrid.Ny, kgrid.Nz])
7268
sensor = kSensor(sensor_mask, record=record)
7369
return sensor
7470

7571
def get_source(kgrid, karray, source_sig):
72+
from kwave.ksource import kSource
7673
source = kSource()
7774
logging.info("Getting binary mask")
7875
source.p_mask = karray.get_array_binary_mask(kgrid)
@@ -95,6 +92,9 @@ def run_simulation(arr: xdc.Transducer,
9592
gpu: bool = True,
9693
ref_values_only: bool = False
9794
):
95+
from kwave.kspaceFirstOrder3D import kspaceFirstOrder3D
96+
from kwave.options.simulation_execution_options import SimulationExecutionOptions
97+
from kwave.options.simulation_options import SimulationOptions
9898
delays = delays if delays is not None else np.zeros(arr.numelements())
9999
apod = apod if apod is not None else np.ones(arr.numelements())
100100
kgrid = get_kgrid(params.coords, dt=dt, t_end=t_end, cfl=cfl)

tests/test_assets.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import shutil
4+
import sys
45
from pathlib import Path
56
from unittest.mock import MagicMock
67

@@ -159,3 +160,10 @@ def test_install_kwave_asset_from_file_fails_on_unrecognized_name(tmp_path, mock
159160

160161
# Ensure we didn't accidentally try to install anything
161162
mock_install_asset.assert_not_called()
163+
164+
def test_kwave_not_imported():
165+
"""Ensure that kwave does not get imported just by importing openlifu.
166+
(Otherwise the tools in openlifu.util.assets cannot be used ahead of the
167+
kwave auto-download of assets.)"""
168+
import openlifu # noqa: F401
169+
assert "kwave" not in sys.modules

0 commit comments

Comments
 (0)