Mouse-Imaging-Centre / pyminc

A python interface to the MINC 2 library, allowing use of numpy arrays to access MINC data, and other such similar goodies, developed by Jason Lerch
http://en.wikibooks.org/wiki/MINC/Tutorials
Other
13 stars 8 forks source link

Fix for the definition of miget_dimension_sizes. #5

Closed carlohamalainen closed 10 years ago

carlohamalainen commented 10 years ago

The full definition from MINC-2.2.00 is:

extern int miget_dimension_sizes(const midimhandle_t dimensions[], int array_length,
             unsigned int sizes[]);

The definition in libpyminc2.py has int_sizes (C's unsigned long long) for the last parameter, but it should be uint_sizes.

Test script:

from pyminc.volumes.factory import *
import numpy as np
import os

tmp = '/tmp/test.mnc'

try:
    os.remove(tmp)
except OSError:
    pass

shape = (10, 20)

vol = volumeFromDescription(tmp, ['yspace', 'xspace'], shape, [-5, -5], [0.2, 0.2])
vol.data = np.random.random(shape)
print vol.data.shape # should say (10, 20)
vol.writeFile()
vol.closeVolume()

Result:

$ python test_creation.py
Traceback (most recent call last):
  File "test_creation.py", line 15, in <module>
    vol.data = np.random.random(shape)
  File "/usr/local/lib/python2.7/dist-packages/pyminc/volumes/volumes.py", line 63, in setdata
    raise IncorrectDimsException
pyminc.volumes.volumes.IncorrectDimsException

After the fix:

$ python test_creation.py
(10, 20)

I tested scripts/pyminc_test2.py and it seemed to run successfully:

$ ./pyminc_test2.py /tmp/test.mnc numpy /tmp/test_out.mnc
4
Opening volume took 0.01000 seconds
Voxel: 0.000000
sizes: 110 217 181
0
Getting hyperslab took 0.15000 seconds
55
0
(4320470,)
(110, 217, 181)
0
computing ten neighbourhood averages using numpy method
Iterations 0 took 0.260 seconds
Iterations 1 took 0.230 seconds
Iterations 2 took 0.220 seconds
Iterations 3 took 0.230 seconds
Iterations 4 took 0.230 seconds
Iterations 5 took 0.220 seconds
Iterations 6 took 0.230 seconds
Iterations 7 took 0.230 seconds
Iterations 8 took 0.220 seconds
Iterations 9 took 0.230 seconds
done averaging. Took a total of 2.300 seconds, averaging 0.230 seconds
before volume
after volume
max and min 87 0
(110, 217, 181)
Creating new volume took 0.36000 seconds
before setting hyperslab
0
0.0
0 255 0 63.2519589304
int32 uint8
[[[0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  ...,
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]]

 [[0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  ...,
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]]

 [[0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  ...,
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]]

 ...,
 [[0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  ...,
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]]

 [[0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  ...,
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]]

 [[0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  ...,
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]
  [0 0 0 ..., 0 0 0]]]
Setting hyperslab took 0.09000 seconds

$ mincstats /tmp/test.mnc
File:              /tmp/test.mnc
Mask file:         (null)
Total voxels:      4320470
# voxels:          4320470
% of total:        100
Volume (mm3):      4320470
Min:               0
Max:               102.5024482
Sum:               102940629.4
Sum^2:             5570867979
Mean:              23.82625718
Variance:          721.7221255
Stddev:            26.86488648
CoM_voxel(z,y,x):  42.81716948 105.8562036 90.67176645
CoM_real(x,y,z):   0.6717664491 -20.14379639 30.81716948

Histogram:         (null)
Total voxels:      4320470
# voxels:          4320470
% of total:        100
Median:            0.03855289613
Majority:          0.02562561259
BiModalT:          28.26505089
PctT [  0%]:       0.02562561259
Entropy :          5.90236189

$ mincstats /tmp/test_out.mnc
File:              /tmp/test_out.mnc
Mask file:         (null)
Total voxels:      4320470
# voxels:          4320470
% of total:        100
Volume (mm3):      4320470
Min:               0
Max:               87
Sum:               93236088.69
Sum^2:             4408692362
Mean:              21.58008011
Variance:          554.7198926
Stddev:            23.55249228
CoM_voxel(z,y,x):  42.56045625 105.8436785 90.68415457
CoM_real(x,y,z):   0.6841545723 -20.15632149 30.56045625

Histogram:         (null)
Total voxels:      4320470
# voxels:          4320470
% of total:        100
Median:            8.808750357
Majority:          0.02174999937
BiModalT:          24.90374947
PctT [  0%]:       0.02174999937
Entropy :          3.946002744

Note: all testing was done on Debian Wheezy 64bit; I got segfaults on Debian Wheezy 32bit with the latest commit and also my patches. Is pyminc compatible with 32bit systems?

mcvaneede commented 10 years ago

Hi Carlo,

Currently pyminc is geared towards Vladimir's development branch from libminc, because that is the one that is installed when you use the minc-tookit repository (https://github.com/BIC-MNI/minc-toolkit). There the function definition for miget_dimension_sizes has changed:

https://github.com/BIC-MNI/libminc/blob/fa3ea0a6c2758db6b0fbc2ed0737e92b9382ebfb/libsrc2/minc2_api.h#L552-553

and uses unsigned long long. There are a number of other functions that will have the same discrepancy with the master branch. Potentially this repository could have a master and develop branch that mimic the changes in limbic.

Cheers, Matthijs

carlohamalainen commented 10 years ago

Hi Matthijs,

Thanks for clarifying. For the moment I'll switch to the BIC-MNI/minc-toolkit repository so you can close this pull request.

Cheers,

-- Carlo

mcvaneede commented 10 years ago

I've updated the README of the repository to indicate that pyminc is currently tailored to libminc's development branch. Closing this issue.