SyneRBI / SIRF

Main repository for the CCP SynerBI software
http://www.ccpsynerbi.ac.uk
Other
58 stars 29 forks source link

When using Numpy translate SIRF ignores new strides etc #823

Closed ALEXJAZZ008008 closed 3 years ago

ALEXJAZZ008008 commented 3 years ago

I found that when using Numpy translate if the matrix is not hard copied then SIRF fill does not reflect the translation. This is possibly due to the fact that Numpy translate only changes the way the data is read not the data itself.

It seems that Evgueni has tried an experiment:

nz, ny, nx = image.dimensions()
img_arr = numpy.ndarray((nx, ny, nz), dtype=numpy.float32) # data dimensions in wrong order for SIRF
img_arr_t = img_arr.transpose((2, 1, 0))
img_arr_t[:, :, :] = image.as_array() # numpy fills data correctly using strides; for SIRF, data order is wrong
image.fill(img_arr_t)
image.show(20) # image displayed correctly, so fill() did its job ok

and he did not find the same as me.

My code was:

def flip_image(paths):
    for i in range(len(paths)):
        current_volume = pet.ImageData(paths[i])
        current_volume.fill(np.transpose(current_volume.as_array(), (0, 2, 1)))
        current_volume.write(paths[i])

    return paths

The following fixed the issue:

def flip_image(paths):
    for i in range(len(paths)):
        current_volume = pet.ImageData(paths[i])
        current_volume.fill(np.transpose(current_volume.as_array(), (0, 2, 1)).copy())
        current_volume.write(paths[i])

    return paths
evgueni-ovtchinnikov commented 3 years ago

I did

from sirf.STIR import *
image = ImageData('my_image.hv')
image.fill(numpy.transpose(image.as_array(), (0, 2, 1)))
image.write('my_image_zxy.hv')

and I got images with x and y axes swapped in my_image_zxy.

You must be using some old version of SIRF, please check.

ClaireDelplancke commented 3 years ago

I also need to pass transposed and flipped images although to STIR, not SIRF, and the equivalent of @evgueni-ovtchinnikov's code above works for me.