Skip to content

Commit

Permalink
remove uff dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
waltsims committed Dec 21, 2023
1 parent 1c6ea64 commit af0cca2
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 327 deletions.
128 changes: 0 additions & 128 deletions kwave/reconstruction/beamform.py
Original file line number Diff line number Diff line change
@@ -1,139 +1,11 @@
import logging
import os
from typing import Tuple, Optional
from warnings import warn

import numpy as np
import scipy
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
from scipy.signal import hilbert
from uff import ChannelData
from uff.position import Position

from kwave.utils.conversion import cart2pol
from kwave.utils.data import scale_time
from kwave.utils.tictoc import TicToc
from .shifted_transform import ShiftedTransform
from .tools import make_time_vector, get_t0, get_origin_array, apodize


def beamform(channel_data: ChannelData) -> None:
"""
Args:
channel_data: shape => (1, 96, 32, 1585)
Returns:
"""
warn("This method is no longer supported and will be depricated in v0.4.0 of k-wave-python.", DeprecationWarning, stacklevel=2)
f_number = 1.2
num_px_z = 256
imaging_depth = 40e-3
# apodization_window = 'boxcar'
apodization_window = "none"
number_samples = np.size(channel_data.data, axis=-1)

# create depth vector
z = np.linspace(0, imaging_depth, num_px_z)

# allocate memory for beamformed image
beamformed_data = np.zeros((len(z), len(channel_data.sequence)), dtype=complex)

# hilbert transform rf data to get envelope
channel_data.data = hilbert(channel_data.data, axis=3)

# allocate memory for
wave_origin_x = np.empty(len(channel_data.sequence))

for e_id, event in enumerate(channel_data.sequence):
# todo event.event should be event.event_id or event.key
# todo make itteratable getter for events
event = channel_data.unique_events[e_id]
probe = event.receive_setup.probe
sampling_freq = event.receive_setup.sampling_frequency
# We assume one transmit wave per transmit event... hence 0 index
transmit_wave = event.transmit_setup.transmit_waves[0]

# make time vector
time_vector = make_time_vector(num_samples=number_samples, sampling_freq=sampling_freq,
time_offset=event.receive_setup.time_offset)

# todo: make indexing 0 min and not 1 min
wave_origin_x[e_id] = channel_data.unique_waves[transmit_wave.wave - 1].origin.position.x

# todo: make position objects
pixel_positions = np.stack([wave_origin_x[e_id] * np.ones(len(z)), np.zeros(len(z)), z]).T
expanding_aperture = pixel_positions[:, 2] / f_number

# time zero delays for spherical waves
origin = get_origin_array(channel_data, transmit_wave)
t0_point = get_t0(transmit_wave)

# logging.log(logging.INFO, origin, t0_point)

transmit_distance = np.sign(pixel_positions[:, 2] - origin[2]) * \
np.sqrt(np.sum((pixel_positions - origin) ** 2, axis=1)) + \
np.abs(1.2 * t0_point[0])
# np.sqrt(np.sum((origin - t0_point) ** 2))

probe = channel_data.probes[probe - 1]
# todo: why are element positions saved as transforms and not positions?
transform = ShiftedTransform.deserialize(probe.transform.serialize())
# todo: remove list from channel mapping. currently [[<element_number>,]...]

# dataset.channel_data.unique_waves[transmit_wave.wave - 1].origin.position.x

# event.transmit_setup.channel_mapping = np.arange(1, 33) # Added by Farid
plt.plot(transmit_distance)

for element_number in event.transmit_setup.channel_mapping:
element_number = element_number[0] # Changed by Farid

# todo: why are element positions saved as transformations?
element_position = Position.deserialize(
probe.element[element_number - 1].transform.translation.serialize())
element_location = Position.deserialize(transform(element_position).serialize())

pixel_element_lateral_distance = abs(pixel_positions[:, 0] - element_location[0])
# logging.log(logging.INFO, pixel_element_lateral_distance)
receive_apodization = apodize(pixel_element_lateral_distance, expanding_aperture, apodization_window)

# receive distance
receive_distance = np.sqrt(np.sum((pixel_positions - np.array(element_location)) ** 2, axis=1))

t0 = transmit_wave.time_offset

# round trip delay
delay = (transmit_distance + receive_distance) / channel_data.sound_speed + t0

# beamformed data
chan_id = element_number - 1 - event.transmit_setup.channel_mapping[0][0] # tricky part
signal = np.squeeze(channel_data.data[:, e_id, chan_id, :])
interp = interp1d(x=time_vector, y=signal, kind='cubic', bounds_error=False, fill_value=0)
beamformed_data[:, e_id] += np.squeeze(receive_apodization * interp(delay).T)

# Envelope and plot
envelope_beamformed_data = np.absolute(beamformed_data)
compressed_beamformed_data = 20 * np.log10(envelope_beamformed_data / np.amax(envelope_beamformed_data) + 1e-12)

plt.figure
x_dis = 1e3 * wave_origin_x
z_dis = 1e3 * z
plt.imshow(compressed_beamformed_data, vmin=-60, vmax=0, cmap='Greys_r',
extent=[min(x_dis), max(x_dis), max(z_dis), min(z_dis)])
plt.xlabel('x[mm]', fontsize=12)
plt.ylabel('z[mm]', fontsize=12)
plt.title(channel_data.description)
plt.colorbar()

filename = "example_bmode.png"
plt.savefig(os.path.join(os.getcwd(), filename))
plt.plot()
logging.log(logging.INFO, f"Plot saved to {os.path.join(os.getcwd(), filename)}")

pass


def focus(kgrid, input_signal, source_mask, focus_position, sound_speed):
Expand Down
175 changes: 0 additions & 175 deletions kwave/reconstruction/converter.py

This file was deleted.

11 changes: 0 additions & 11 deletions kwave/reconstruction/shifted_transform.py

This file was deleted.

12 changes: 0 additions & 12 deletions kwave/reconstruction/tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
from uff import Position


def log_compression(signal, cf, normalize=False):
Expand Down Expand Up @@ -58,17 +57,6 @@ def apodize(distance, aperture, window):
return apod


def get_t0(transmit_wave):
serialized_tx_wave = transmit_wave.time_zero_reference_point.serialize()
return np.array(Position.deserialize(serialized_tx_wave))


def get_origin_array(channel_data, transmit_wave):
serialized_origin = channel_data.unique_waves[transmit_wave.wave - 1].origin.position.serialize()
return np.array(
Position.deserialize(serialized_origin))


def make_time_vector(num_samples, sampling_freq, time_offset):
return np.linspace(start=0, num=num_samples,
stop=num_samples / sampling_freq) + time_offset
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies = [
"matplotlib==3.7.2",
"numpy>=1.22.2,<1.25.0",
"scikit-image==0.21.0",
"uff-reader==0.0.2",
]

[project.urls]
Expand Down

0 comments on commit af0cca2

Please sign in to comment.