Ultrasound Cone Calibration Algorithm in Python

Dear ImFusion Team,

I am trying to use the UltrasoundConeCalibrationAlgorithm in Python. The correct way of using it in C++ is

UltrasoundConeCalibrationAlgorithm (std::vector< UltrasoundSweep * > sweeps)

What should be the corresponding variable ‘sweeps’ in Python? I can construct two sweep variables:

imfusion.UltrasoundSweep() sweep1, sweep2

And define ‘sweeps’ as a list, ’ [sweep1, sweep2]'. Then run

imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’,[sweeps])

But then I got the error:

Traceback (most recent call last):

File “D:\Python\lib\site-packages\IPython\core\interactiveshell.py”, line 3460, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’,[sweeps])
TypeError: sequence item 0: Unable to cast Python instance to C++ type (compile in debug mode for details)

Obviously, the passed parameter is not correct. But I dont know what kind of variable corresponds to std::vector< UltrasoundSweep * > in C++

Environment:
Windows10 + ImFusionSDK v2.42.4 +Python 3.9

Thanks in advance for your help!

Hi Luohong,

The error message there is indeed cryptic. The error occurs because you are passing in a list that contains a list. You can either do (notice the missing brackets in the call of the algorithm:

sweeps = [sweep1, sweep2]
imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’,sweeps)

or directly

imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’,[sweep1, sweep2])

Don’t hesitate to reach out if you encounter any other issue!

Hi VIktoria Markova,

Thanks a lot for your help. I got new error after adapting your suggestion. However, after learning more about the ImFusionSDK, I believe the error source is the passed UltrasoundSweep data. I construct them myself wrongly. I already found other relevant posts and will look into it.

1 Like

Hi VIktoria Markova,

I found a sweep file from other post in the forum.

And I tried the following code:

sweep=imfusion.io.open(‘./data/Sweep.imf’)[0]
imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’, [sweep,sweep])

But still got this error

Traceback (most recent call last):
File “D:\PythonProjects\US2PCD\main.py”, line 54, in
imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’, [sweep_gt,sweep_gt])
imfusion._bindings.IncompatibleError: No algorithm called ‘Ultrasound;Cone Calibration’ is compatible with the given data.

Could you please provide some help? Currently, I don’t have two sweeps taken with a “Cone Phantom” as described in the paper “PRO-TIP: Phantom for RObust automatic ultrasound calibration by TIP detection” (Ronchetti et al. 2022). We already order the phantom, and will have it soon. But I just want to make sure the code runs well before we have it. I checked the sweep variable, there is tracking matrix in it. I didn’t expect the above code return correct results because there are no tips in it. It should be at least ‘runable’. I couldn’t find Python documentation about this algorithm. Compared to the C++ SDK documentation, many things are missing in the Python version.

I also tried to generate a UlrasoundSweep myself by providing ‘png’ images and a trackingStream. But still failed.

Hi Luohong Wu,

I believe the problem must be that you are trying to run the algorithm with the same imfusion object. Try this code:

sweep0=imfusion.io.open(‘./data/Sweep.imf’)[0]
sweep1=imfusion.io.open(‘./data/Sweep.imf’)[0]
imfusion.executeAlgorithm(‘Ultrasound;Cone Calibration’, [sweep0,sweep1])

Let me know if I can further help you!

Hi VIktoria Markova,

Thank you so much! It is working. WoW, I thought the data is wrong. But never thought this could be the error source.

Have a nice day!

1 Like