k-wave-python icon indicating copy to clipboard operation
k-wave-python copied to clipboard

[BUG] Can not use kspaceFirstOrder-CUDA

Open yangzhongii opened this issue 5 months ago • 6 comments

Describe the bug Hi, I am new to this project and I encounter some problems when I try to use the kspaceFirstOrder-CUDA. Here are my codes:

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import numpy as np
from kwave import kWaveGrid, kSensor, kSource, medium as kw_medium, kspaceFirstOrder3D

class PhotoacousticReconstruction:
    def __init__(self):
        self.bridge = CvBridge()
        self.sub = rospy.Subscriber('/slice_image', Image, self.callback)
        self.pub = rospy.Publisher('/reconstructed_image', Image, queue_size=1)

        # Define k-Wave grid
        dx = 0.2e-3  # [m]
        Nx, Ny, Nz = 250, 200, 200
        self.kgrid = kWaveGrid(Nx=Nx, dx=dx, Ny=Ny, dy=dx, Nz=Nz, dz=dx)

        # Define medium
        self.medium = kw_medium(sound_speed=1540, density=1000, alpha_coeff=0.75, alpha_power=1.5)

        # Define sensor (placeholder for sensor mask)
        element_num = 128
        element_pitch = 0.3e-3  # [m]
        self.sensor = kSensor()
        self.sensor.mask = np.zeros((Nx, Ny, Nz), dtype=bool)
        # Define sensor mask based on transducer array
        for ind in range(element_num):
            x_pos = -(element_num * element_pitch / 2 - element_pitch / 2) + (ind * element_pitch)
            y_idx = int((x_pos + (Ny * dx) / 2) / dx)  # Center grid in y
            if 0 <= y_idx < Ny:
                self.sensor.mask[0, y_idx, 0] = True  # Approximate each transducer as a point
        self.sensor.record = {'p': True}  # Record pressure during time reversal

        # Define source (zero for time reversal)
        self.source = kSource()
        self.source.p0 = np.zeros((Nx, Ny, Nz))

        # Define time array
        self.kgrid.t_array = self.kgrid.makeTime(self.medium.sound_speed)

    def callback(self, data):
        try:
            # Convert ROS Image to NumPy array
            cv_image = self.bridge.imgmsg_to_cv2(data, "32FC1")  # Assuming 32-bit float
            num_sensor_points, Nt = cv_image.shape

            # Verify sensor data matches mask
            if num_sensor_points != np.sum(self.sensor.mask):
                rospy.logwarn("Sensor data size does not match sensor mask points")
                return

            # Set time reversal boundary data
            self.sensor.time_reversal_boundary_data = cv_image

            # Run time reversal simulation
            p0_recon = kspaceFirstOrder3D(self.kgrid, self.medium, self.source, self.sensor)

            # Extract reconstructed initial pressure
            reconstructed_p0 = p0_recon.p

            # Extract 2D slice at z = Nz/2
            slice_image = reconstructed_p0[:, :, self.kgrid.Nz // 2]

            # Publish reconstructed image
            slice_image_msg = self.bridge.cv2_to_imgmsg(slice_image.astype(np.float32), "32FC1")
            self.pub.publish(slice_image_msg)
        except Exception as e:
            rospy.logerr(f"Error processing image: {str(e)}")

    def run(self):
        rospy.spin()

if __name__ == '__main__':
    try:
        rospy.init_node('photoacoustic_reconstruction', anonymous=True)
        node = PhotoacousticReconstruction()
        node.run()
    except rospy.ROSInterruptException:
        pass

And here are the errors I encounter:

home/thera/anaconda3/envs/kwave/lib/python3.10/site-packages/skimage/transform/_warps.py:2: UserWarning: A NumPy version >=1.23.5 and <2.5.0 is required for this version of SciPy (detected version 1.23.0)
  from scipy import ndimage as ndi
[INFO] [1752999585.895047]: sensor_mask shape: (250, 200, 200), non-zero points: 7000
[WARN] [1752999585.901278]: sensor_mask has 7000 non-zero points, expected 128. Adjusting mask...
[INFO] [1752999585.908868]: Adjusted sensor_mask non-zero points: 128
┌───────────────────────────────────────────────────────────────┐
│                  kspaceFirstOrder-CUDA v1.3                   │
├───────────────────────────────────────────────────────────────┤
│ Reading simulation configuration:                        Done │
│ Selected GPU device id:                                Failed │
└───────────────────────────────────────────────────────────────┘

┌───────────────────────────────────────────────────────────────┐
│            !!! K-Wave experienced a fatal error !!!           │
├───────────────────────────────────────────────────────────────┤
│ Error: All CUDA-capable devices are busy or unavailable.      │
├───────────────────────────────────────────────────────────────┤
│                      Execution terminated                     │
└───────────────────────────────────────────────────────────────┘

[ERROR] [1752999588.451475]: bad callback: <function main.<locals>.image_callback at 0x7fcc170c8820>
Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/thera/expert_simulator_pa_7_5/src/ct_to_us_simulator/scripts/kwave_ros_simulator.py", line 96, in image_callback
    sensor_data = kspaceFirstOrder3DG(kgrid, source, sensor, medium, simulation_options, execution_options)
  File "/home/thera/Downloads/k-wave-python-master/kwave/kspaceFirstOrder3D.py", line 70, in kspaceFirstOrder3DG
    sensor_data = kspaceFirstOrder3D(
  File "/home/thera/Downloads/k-wave-python-master/kwave/kspaceFirstOrder3D.py", line 375, in kspaceFirstOrder3D
    sensor_data = executor.run_simulation(k_sim.options.input_filename, k_sim.options.output_filename, options=executor_options)
  File "/home/thera/Downloads/k-wave-python-master/kwave/executor.py", line 53, in run_simulation
    raise subprocess.CalledProcessError(proc.returncode, command, stdout, stderr)
subprocess.CalledProcessError: Command '['/home/thera/Downloads/k-wave-python-master/kwave/bin/linux/kspaceFirstOrder-CUDA', '-i', '/tmp/20-Jul-2025-16-19-45_kwave_input.h5', '-o', '/tmp/20-Jul-2025-16-19-45_kwave_output.h5', '-t', '20', '--p_raw', '-s', '1']' returned non-zero exit status 1.

To Reproduce Steps to reproduce the behavior: just run the codes and reproduce the behavior.

Expected behavior The output should generate a generated image.

Screenshots If applicable, add screenshots to help explain your problem.

Image

Desktop (please complete the following information):

  • OS: Linux
  • Version 20
  • Python 3.10.0
  • numpy 1.23.0 Thank you for your help

yangzhongii avatar Jul 20 '25 08:07 yangzhongii