Getting Houndsfield or Gray Value data from an image stack

I am currently attempting to access the HU data directly from python. I found the get_voxel_buffer() method in the mimics.ImageData class, which returns a multi-dimensional memoryview of gray values. I planned to convert these to HU next, however I found that the returned object was quite difficult to convert to a convenient form (numpy array would be preferred). I thought I’d ask the group while I continued to try and solve the problem on my end. I can return a 1D list of integers, but making an array that allows for fast processing keeps eluding me. To be clear, this is the code I’m running to get to a list.

import mimics
import numpy as np

images = mimics.data.images.get_active()
buffer = images.get_voxel_buffer()
casted = buffer.cast('b').cast('i')
lst = casted.tolist()

I think part of the problem is that the obj attribute of buffer is None. I’m guessing that hinders my ability to easily access that data. I’ve tried everything I could think of and most everything I could find online. This iteration process using grouper technically works, it just takes a long, long, long, long time.

import mimics
import numpy as np
from itertools import zip_longest

use_actual_array = False
images = mimics.data.images.get_active()
buffer = images.get_voxel_buffer()
flat_list = buffer.cast('b').cast(buffer.format)
test_arr = np.empty(shape=buffer.shape)

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return zip_longest(fillvalue=fillvalue, *args)

if use_actual_array:
    arr = buffer
else:
    arr = np.array(range(2*3*4)).reshape((4, 3, 2))
    flat_list = arr.flatten().tolist()
    test_arr = np.zeros_like(arr)

for row_idx, row in enumerate(grouper(arr.shape[-1], flat_list)):
    test_arr[row_idx // arr.shape[1],row_idx % arr.shape[1]] = row

Hello Nathaniel,

You can easily transform the memoryview into a numpy array using the function asarray from numpy.

Here is the example of the code I use:

import numpy as np

im = mimics.data.images["fixed"]  #selecting the image
mem_view = im.get_voxel_buffer()  #get the buffer in the memory view
buffer = np.asarray(mem_view)   #transform the memory view in a numpy array

print(buffer)

Regarding the conversion, we have the build-in mimics functions mimics.segment.GV2HU() and mimics.segment.HU2GV() that you can use to make the conversion between HU and Gray Value if needed.

Hope this helps.

Clément

I had planned on using the conversion functions once I got to that point.

I tried using np.asarray (first thing I tried yesterday) and I get this error:
I’m using numpy version 1.18.4 with python 3.7.6.

arr = np.asarray(buffer)
Traceback (most recent call last):
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-92-3c9f6e397906>", line 1, in <module>
    arr = np.asarray(buffer)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\netref.py", line 220, in method
    return syncreq(_self, consts.HANDLE_CALLATTR, name, args, kwargs)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\netref.py", line 75, in syncreq
    return conn.sync_request(handler, proxy, *args)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 471, in sync_request
    return self.async_request(handler, *args, timeout=timeout).value
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\async_.py", line 97, in value
    raise self._obj
_get_exception_class.<locals>.Derived: multi-dimensional sub-views are not implemented
========= Remote Traceback (1) =========
Traceback (most recent call last):
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 329, in _dispatch_request
    res = self._HANDLERS[handler](self, *args)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 603, in _handle_callattr
    return self._handle_call(obj, args, kwargs)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 590, in _handle_call
    return obj(*args, **dict(kwargs))
NotImplementedError: multi-dimensional sub-views are not implemented

I am currently looking at updating rpyc to see if that helps. I’m using 4.0.2 and the latest version is 4.1.5

Well, I just learned about the new instructions to install the mimics wheel, per your python installation instructions. I upgraded that to mimics 23 and upgraded rpyc to 4.1.5, and I’m still getting the same error.

Hey Nathaniel,

This issue might be linked to Conda and/or the use of the Script Listener. Are your using the Mimics Script Listener to run your code ?
(In general the Script Listener has difficulties working with large objects like pixel buffer)

I can reproduce the same error by running my code from an external IDE (with the Script Listener) while it works if ran directly from the Mimics Editor. That might do the trick for now.

We are going to investiguate this further.

Regards,

Clément

I have been using the Script Listener to run from Pycharm. I made myself a work-around macro to save the data as a .npy file from mimics without the listener, allowing me to import it quickly later from anywhere.

I can send y’all a YAML file with my conda environment, if you would like! I can’t upload it here, but I suppose I could just paste it in as text.

I’m encountering this same error when I try and get the points and triangles using part.get_triangles().

import numpy as np

points, tris = part.get_triangles()
points_arr = np.asarray(points)

Traceback (most recent call last):
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-76-a25dd9064df0>", line 1, in <module>
    np.asarray(points)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\netref.py", line 274, in method
    return syncreq(_self, consts.HANDLE_CALLATTR, name, args, kwargs)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\netref.py", line 76, in syncreq
    return conn.sync_request(handler, proxy, *args)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 469, in sync_request
    return self.async_request(handler, *args, timeout=timeout).value
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\async_.py", line 102, in value
    raise self._obj
_get_exception_class.<locals>.Derived: multi-dimensional sub-views are not implemented
========= Remote Traceback (1) =========
Traceback (most recent call last):
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 320, in _dispatch_request
    res = self._HANDLERS[handler](self, *args)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 619, in _handle_callattr
    return self._handle_call(obj, args, kwargs)
  File "C:\Users\Npyle1\.conda\envs\three_mat\lib\site-packages\rpyc\core\protocol.py", line 593, in _handle_call
    return obj(*args, **dict(kwargs))
NotImplementedError: multi-dimensional sub-views are not implemented

Hello,

This is indeed similar as when you are trying to get the voxels from an image. The similar function np.asarray() is used.

We continue to look into this and see if we can make it work better from external IDE.

Regards,

Clément

1 Like

I’ll note that I am able to get points and triangles from an ssm/part in 3-matic.