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
14 stars 8 forks source link

Voxel-wise math returns hyperslabs instead of scalar values #15

Open gdevenyi opened 8 years ago

gdevenyi commented 8 years ago

Rather than trying to manipulate minc volumes, I'm trying to do some math with the voxels.

I would've expected numpy array operations to work as expected, however they still seem to return hyperslabs, even if they would've returned a scalar without minc stuff.

Example

> infile = volumeFromFile("/home/cic/devgab/projects/OASIS/input/OAS1_0001_MR1_mpr_n4_anon_sbj_111.mnc")
> np.sum(infile.data)
        array(data=
              3188845312.0,
        start=[0 0 0], count=[160 256 256],
        separations=[-1.0, 1.0, 1.0], dimnames=[]

I would've expected just a value.

Because it's a hyperslab, I can't do further math with that value.

jasonlerch commented 8 years ago

Hrm - you’re right. Not the obvious way to handle your scenario.

Quick workaround: wrap in float, i.e. float(np.sum(infile.data))

On Feb 9, 2016, at 12:59 PM, Gabriel A. Devenyi notifications@github.com wrote:

Rather than trying to manipulate minc volumes, I'm trying to do some math with the voxels.

I would've expected numpy array operations to work as expected, however they still seem to return hyperslabs, even if they would've returned a scalar without minc stuff.

Example

infile = volumeFromFile("/home/cic/devgab/projects/OASIS/input/OAS1_0001_MR1_mpr_n4_anon_sbj_111.mnc") np.sum(infile.data) array(data= 3188845312.0, start=[0 0 0], count=[160 256 256], separations=[-1.0, 1.0, 1.0], dimnames=[] I would've expected just a value.

Because it's a hyperslab, I can't do further math with that value.

— Reply to this email directly or view it on GitHub https://github.com/Mouse-Imaging-Centre/pyminc/issues/15.

gdevenyi commented 8 years ago

Workaround worked! Thanks.

mcvaneede commented 8 years ago

I guess the question is how do we want to store data in a mincVolume? We can quite easily fix this issue by storing the .data part as a numpy array and storing the metadata about the hyperslab (start, count, separations and dimnames) somewhere else. That does seem like a better way to handle the data in my opinion, because it will allow you to do what Gabe was trying here more intuitively. Without the need for any casting.

Thoughts? Objections to me adding in changes to reflect this?

gdevenyi commented 8 years ago

That seems okay to me, does that mean extra attributes that are ignored by numpy but minc operators may handle?

mcvaneede commented 8 years ago

I've talked with Ben about this, and the real issue seems to be with subclassing in python. The HyperSlab class is a subclass of numpy's ndarray, but it doesn't fully behave as you'd expect. In that sum(), min() and max() for instance return the subclass, and not what an ndarray would return. We'll pose this question to the python/numpy world and see if they have a solution for it.

bcdarwin commented 8 years ago

I've posted an issue on the numpy docs here; let's see what they say ...

bcdarwin commented 8 years ago

A couple people have recently replied, but there doesn't seem to be a clear fix (the suggestion we override many methods doesn't seem right) ... perhaps going with Matthijs's original suggestion would be OK, or we could leave as-is.