Skip to content

Commit

Permalink
CUSTOMERS-64: Made it possible to run custom callback on the arrus.ut…
Browse files Browse the repository at this point in the history
…ils.imaging callback. (#251)
  • Loading branch information
pjarosik authored Feb 3, 2022
1 parent b66519e commit 35e4609
Show file tree
Hide file tree
Showing 22 changed files with 254 additions and 103 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ set(PROJECT_VERSION 0.7.0)
set(ARRUS_PROJECT_VERSION "${PROJECT_VERSION}")

option(ARRUS_DEVELOP_VERSION "Build develop version." ON)

# TODO remove ARRUS_DEVELOP_VERSION, use ARRUS_APPEND_VERSION_SUFFIX_DATE
if(ARRUS_DEVELOP_VERSION)
set(PROJECT_FULL_VERSION "${PROJECT_VERSION}-dev")
Expand Down Expand Up @@ -88,7 +87,7 @@ set(ARRUS_DOCS_INSTALL_DIR docs)
################################################################################
# Common dependencies
################################################################################
find_package(Us4 0.6.10 EXACT REQUIRED US4OEM HV256 DBARLite)
find_package(Us4 0.7.0 EXACT REQUIRED US4OEM HV256 DBARLite)
find_package(Boost REQUIRED)
set(Boost_USE_STATIC_LIBS ON)
find_package(Protobuf REQUIRED)
Expand Down
15 changes: 14 additions & 1 deletion api/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ set(SOURCE_FILES
)

set(TEST_FILES
arrus/kernels/tests/imaging_test.py
# arrus/kernels/tests/imaging_test.py
)

if(ARRUS_CUDA)
Expand All @@ -115,9 +115,14 @@ endif()

set(SETUP_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in")
set(SETUP_PY_OUT "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
set(INIT_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/arrus/__init__.py")
# The below output will be moved to the arrus subdirectory (by custom_command),
# right after the .py source files are available in build_dir.
set(INIT_PY_OUT "${CMAKE_CURRENT_BINARY_DIR}/tmp/__init__.py")
set(TIMESTAMP "${CMAKE_CURRENT_BINARY_DIR}/timestamp")

configure_file(${SETUP_PY_IN} ${SETUP_PY_OUT})
configure_file(${INIT_PY_IN} ${INIT_PY_OUT})


if ("${ARRUS_BUILD_PLATFORM}" STREQUAL "windows")
Expand All @@ -127,6 +132,10 @@ if ("${ARRUS_BUILD_PLATFORM}" STREQUAL "windows")
COMMAND
${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/arrus ${CMAKE_CURRENT_BINARY_DIR}/arrus
# Override __init__.py file with the correct Python package version.
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/tmp/__init__.py ${CMAKE_CURRENT_BINARY_DIR}/arrus
COMMAND
${CMAKE_COMMAND} -E copy_directory
${Us4_ROOT_DIR}/lib64 ${CMAKE_CURRENT_BINARY_DIR}/arrus
Expand All @@ -147,6 +156,10 @@ elseif ("${ARRUS_BUILD_PLATFORM}" STREQUAL "linux")
COMMAND
${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/arrus ${CMAKE_CURRENT_BINARY_DIR}/arrus
# Override __init__.py file with the correct Python package version.
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/tmp/__init__.py ${CMAKE_CURRENT_BINARY_DIR}/arrus
COMMAND
${CMAKE_COMMAND} -E copy_directory
${Us4_ROOT_DIR}/lib64 ${CMAKE_CURRENT_BINARY_DIR}/arrus
Expand Down
2 changes: 2 additions & 0 deletions api/python/arrus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# use arrus-core.dll from the python package
os.environ["PATH"] += os.pathsep + os.path.dirname(os.path.join(os.path.abspath(__file__)))

__version__ = "${ARRUS_PROJECT_VERSION}"

# Exceptions
from arrus.exceptions import *

Expand Down
9 changes: 9 additions & 0 deletions api/python/arrus/devices/us4r.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class FrameChannelMapping:
:param channels: a mapping: (logical frame, logical channel) -> physical channel
:param us4oems: a mapping: (logical frame, logical channel) -> us4OEM number
:param frame_offsets: frame starting number for each us4OEM available in the system
:param n_frames: number of frames each us4OEM produces
:param batch_size: number of sequences in a single batch
"""
frames: np.ndarray
channels: np.ndarray
us4oems: np.ndarray
frame_offsets: np.ndarray
n_frames: np.ndarray
batch_size: int = 1


Expand Down Expand Up @@ -121,6 +123,13 @@ def set_test_pattern(self, pattern):
test_pattern_core = arrus.utils.core.convert_to_test_pattern(pattern)
self._handle.setTestPattern(test_pattern_core)

@property
def channels_mask(self):
"""
Returns a list of system channels that are masked in the configuration.
"""
return self._handle.getChannelsMask()

def _get_dto(self):
import arrus.utils.core
probe_model = arrus.utils.core.convert_to_py_probe_model(
Expand Down
66 changes: 20 additions & 46 deletions api/python/arrus/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ def upload(self, scheme: arrus.ops.us4r.Scheme):
###
# -- Constant metadata
# --- FCM
fcm_us4oems, fcm_frame, fcm_channel, frame_offsets = \
fcm_us4oems, fcm_frame, fcm_channel, frame_offsets, n_frames = \
arrus.utils.core.convert_fcm_to_np_arrays(fcm, us_device.n_us4oems)
fcm = arrus.devices.us4r.FrameChannelMapping(
us4oems=fcm_us4oems,
frames=fcm_frame,
channels=fcm_channel,
frame_offsets=frame_offsets,
n_frames=n_frames,
batch_size=batch_size)

# --- Frame acquisition context
Expand All @@ -138,52 +139,25 @@ def upload(self, scheme: arrus.ops.us4r.Scheme):
if processing is not None:
# setup processing
import arrus.utils.imaging as _imaging
if not isinstance(processing, _imaging.Pipeline):
raise ValueError("Currently only arrus.utils.imaging.Pipeline "
"processing is supported only.")
import cupy as cp
out_metadata = processing.prepare(const_metadata)
self.gpu_buffer = arrus.utils.imaging.Buffer(n_elements=2,
shape=const_metadata.input_shape,
dtype=const_metadata.dtype,
math_pkg=cp,
type="locked")
self.out_buffer = [arrus.utils.imaging.Buffer(n_elements=2,
shape=m.input_shape,
dtype=m.dtype, math_pkg=np,
type="locked")
for m in out_metadata]
# Wait for all the initialization done in by the Pipeline.
cp.cuda.Stream.null.synchronize()
user_out_buffer = queue.Queue(maxsize=1)

def buffer_callback(elements):
try:
user_elements = [None]*len(elements)
for i, element in enumerate(elements):
user_elements[i] = element.data.copy()
element.release()
try:
user_out_buffer.put_nowait(user_elements)
except queue.Full:
pass
except Exception as e:
print(f"Exception: {type(e)}")
except:
print("Unknown exception")
pipeline_wrapper = arrus.utils.imaging.PipelineRunner(
buffer, self.gpu_buffer, self.out_buffer, processing,
buffer_callback)
self._current_processing = pipeline_wrapper
buffer.append_on_new_data_callback(pipeline_wrapper.process)

buffer = user_out_buffer
if len(out_metadata) == 1:
const_metadata = out_metadata[0]
else:
const_metadata = out_metadata

return buffer, const_metadata
if isinstance(processing, _imaging.Pipeline):
# Wrap Pipeline into the Processing object.
processing = _imaging.Processing(
pipeline=processing,
callback=None,
extract_metadata=False
)
if isinstance(processing, _imaging.Processing):
self.processing = arrus.utils.imaging.ProcessingRunner(
buffer, const_metadata, processing)
outputs = self.processing.outputs
else:
raise ValueError("Unsupported type of processing: "
f"{type(processing)}")
else:
# Device buffer and const_metadata
outputs = buffer, const_metadata
return outputs

def __enter__(self):
return self
Expand Down
5 changes: 4 additions & 1 deletion api/python/arrus/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def convert_fcm_to_np_arrays(fcm, n_us4oems):
fcm_channel[frame, channel] = src_channel
frame_offsets = [fcm.getFirstFrame(i) for i in range(n_us4oems)]
frame_offsets = np.array(frame_offsets, dtype=np.uint32)
return fcm_us4oem, fcm_frame, fcm_channel, frame_offsets
n_frames = [fcm.getNumberOfFrames(i) for i in range(n_us4oems)]
n_frames = np.array(n_frames, dtype=np.uint32)
return fcm_us4oem, fcm_frame, fcm_channel, frame_offsets, n_frames



def convert_to_py_probe_model(core_model):
Expand Down
Loading

0 comments on commit 35e4609

Please sign in to comment.