<div dir="ltr"><div>:O My hero. It works!<br><br></div>But then why does fmri_dataset work without this step?<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 10, 2014 at 4:10 PM, Yaroslav Halchenko <span dir="ltr"><<a href="mailto:debian@onerussian.com" target="_blank">debian@onerussian.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
On Wed, 10 Dec 2014, Thomas Nickson wrote:<br>
<br>
</span><span class="">> My main problem is that this isn't at all consistent with how the function<br>
> for constructing fmri datasets works. Which way do you consider to be<br>
> correct?<br>
<br>
</span>I thought "correct way" is when mask dimensionality (be it a boolean<br>
array or tuple of indices) matches the dimensionality of the data<br>
array. That is I think how all our functions work<br>
<span class=""><br>
> I think the fmri way of handling it makes more sense. This way I<br>
> have to understand that the mask needs to be flattened and I need to<br>
> provide an array of nonzero indices.<br>
<br>
</span>that is where you are loosing me ;) why mask needs to be flattened?<br>
<span class=""><br>
> The other way does not work. No other<br>
</span>> way works.A I can't just provide a flattened boolean mask, if by that you<br>
<span class="">> mean 1's and 0's.<br>
<br>
</span>I mean dtype=bool array of True and False's. Array of indices 1 and 0<br>
is just that -- array of indicies ;)<br>
<span class=""><br>
> I can't provide a mask of the same shape as the original<br>
> data.<br>
<br>
</span>that is what confuses me -- it must work. looking back to our<br>
correspondence -- you never showed that dtype of your mask is bool...<br>
may be that is the catch?<br>
<span class=""><br>
> This is confusing because the original data will be automatically<br>
> flattened but the mask won't.<br>
<br>
</span>confused<br>
<span class=""><br>
> Why does that make any sense?<br>
<br>
</span>dunno since it doesn't<br>
<span class=""><br>
> Surely you<br>
> would at least want the inputs to be consistent for the mask and the<br>
> original data?<br>
<br>
</span>yeah<br>
<span class=""><br>
> It's neither consistent within the function itself nor is<br>
> it consistent between other functions that do the same thing.<br>
<br>
</span>hm... confused<br>
<span class=""><br>
> The fmri data provided for the tutorial does exactly the same thing. It<br>
> hasn't got anything to do with the data. I can provide the code.<br>
<br>
</span>oy-- lets check that hypothesis of mine about dtype of your mask first<br>
<span class=""><br>
> Anyway, I don't mean to non-constructively criticise but I found it<br>
> confusing and frustrating and thought it might be nice to alleviate some<br>
> of that.<br>
> #Open the main VBM data - some functions to help<br>
<br>
> import numpy as np<br>
> import nibabel as nb<br>
> import os<br>
> from functools import partial<br>
> from operator import mul<br>
> import pickle as p<br>
> from mvpa2.datasets.base import Dataset<br>
> from mvpa2.mappers.zscore import zscore<br>
> from mvpa2.datasets.mri import fmri_dataset<br>
<br>
> """4ddata is fddata and 2ddata is tddata"""<br>
<br>
> def getImages(imagePath):<br>
</span>> A A A """load a nifti"""<br>
> A A A return nb.load(imagePath)<br>
<br>
> def writeDatatoImages(writePath, Data):<br>
> A A A """Write a numpy array to a nifti1 image"""<br>
> A A A N = nb.Nifti1Image(Data, np.eye(4))<br>
> A A A N.to_filename(writePath)<br>
<br>
> def getImageData(imagePath):<br>
> A A A """load a nifti as numpy data"""<br>
> A A A return getImages(imagePath).get_data()<br>
> A A A<br>
> def rotateShape(FDData):<br>
> A A A """we need to rotate the shape of the data as other wise the time<br>
<span class="">> dimension (here denoting the subjects) is actually last"""<br>
</span>> A A A if len(FDData.shape) == 4:<br>
> A A A A A A A FDData = np.rollaxis(FDData, -1)<br>
> A A A return FDData<br>
<br>
> def reshapeFlatten(FDData):<br>
> A A A """Reshape the data and then flatten each member of the array"""<br>
> A A A Data = rotateShape(FDData)<br>
> A A A return Data.reshape(Data.shape[0],reduce(mul,Data.shape[1:],1))<br>
<span class=""><br>
> #======================================Helper functions<br>
> above=====================================<br>
<br>
> DATA_LOCATION = ./pymvpa/PyMVPA/tutorial_data/data/bold.nii.gz'<br>
<br>
> MASK = './pymvpa/PyMVPA/tutorial_data/data/mask_brain.nii.gz'<br>
<br>
> #Load the metadata pickle<br>
> #metaData = p.load(open('VBMMeta.pkl'))<br>
<br>
> #get image data and reshape<br>
> rotatedImageData = rotateShape(getImageData(DATA_LOCATION))<br>
<br>
> #Get mask data<br>
> mask = getImageData(MASK).flatten()#.nonzero()[0]#.flatten().nonzero()[0]<br>
<br>
</span>just try<br>
<br>
getImageData(MASK).astype(bool)<br>
<span class="im HOEnZb"><br>
?<br>
<br>
> #mask = np.float32(mask)<br>
> #print type(mask)<br>
<br>
> #Load dataset<br>
> ds = Dataset.from_wizard(rotatedImageData,<br>
> mask=mask)#targets=metaData['diagnosesT1'], mask=mask)<br>
> #ds = fmri_dataset(rotatedImageData)#, targets=metaData['diagnosesT1'],<br>
> mask=mask)<br>
> #ds = fmri_dataset(DATA_LOCATION, mask=mask)#,<br>
> targets=metaData['diagnosesT1'], mask=mask, chunks=np.array(range(133)))<br>
<br>
> #zscore normalise the data<br>
> #zscore(ds, chunks_attr=None)<br>
</span><div class="HOEnZb"><div class="h5">--<br>
Yaroslav O. Halchenko, Ph.D.<br>
<a href="http://neuro.debian.net" target="_blank">http://neuro.debian.net</a> <a href="http://www.pymvpa.org" target="_blank">http://www.pymvpa.org</a> <a href="http://www.fail2ban.org" target="_blank">http://www.fail2ban.org</a><br>
Research Scientist, Psychological and Brain Sciences Dept.<br>
Dartmouth College, 419 Moore Hall, Hinman Box 6207, Hanover, NH 03755<br>
Phone: <a href="tel:%2B1%20%28603%29%20646-9834" value="+16036469834">+1 (603) 646-9834</a> Fax: <a href="tel:%2B1%20%28603%29%20646-1419" value="+16036461419">+1 (603) 646-1419</a><br>
WWW: <a href="http://www.linkedin.com/in/yarik" target="_blank">http://www.linkedin.com/in/yarik</a><br>
<br>
_______________________________________________<br>
Pkg-ExpPsy-PyMVPA mailing list<br>
<a href="mailto:Pkg-ExpPsy-PyMVPA@lists.alioth.debian.org">Pkg-ExpPsy-PyMVPA@lists.alioth.debian.org</a><br>
<a href="http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-exppsy-pymvpa" target="_blank">http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-exppsy-pymvpa</a><br>
</div></div></blockquote></div><br></div></div></div>