In [1]:
%load_ext pymatbridge


Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge
Send 'exit' command to kill the server
.MATLAB started and connected!

In [2]:
import numpy as np

In [59]:
a = np.arange(64).reshape(4,4,4) + 1
mask = np.zeros((4,4,4))
mask[1:3,1:3,1:3] = 1
mask = mask == 1

In [60]:
mask


Out[60]:
array([[[False, False, False, False],
        [False, False, False, False],
        [False, False, False, False],
        [False, False, False, False]],

       [[False, False, False, False],
        [False,  True,  True, False],
        [False,  True,  True, False],
        [False, False, False, False]],

       [[False, False, False, False],
        [False,  True,  True, False],
        [False,  True,  True, False],
        [False, False, False, False]],

       [[False, False, False, False],
        [False, False, False, False],
        [False, False, False, False],
        [False, False, False, False]]], dtype=bool)

In [61]:
a


Out[61]:
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12],
        [13, 14, 15, 16]],

       [[17, 18, 19, 20],
        [21, 22, 23, 24],
        [25, 26, 27, 28],
        [29, 30, 31, 32]],

       [[33, 34, 35, 36],
        [37, 38, 39, 40],
        [41, 42, 43, 44],
        [45, 46, 47, 48]],

       [[49, 50, 51, 52],
        [53, 54, 55, 56],
        [57, 58, 59, 60],
        [61, 62, 63, 64]]])

In [62]:
%%matlab
a = reshape(1:64, [4,4,4]);
mask = zeros(4,4,4);
mask(2:3,2:3,2:3) = 1;
a
mask = logical(mask)


a(:,:,1) =

     1     5     9    13
     2     6    10    14
     3     7    11    15
     4     8    12    16


a(:,:,2) =

    17    21    25    29
    18    22    26    30
    19    23    27    31
    20    24    28    32


a(:,:,3) =

    33    37    41    45
    34    38    42    46
    35    39    43    47
    36    40    44    48


a(:,:,4) =

    49    53    57    61
    50    54    58    62
    51    55    59    63
    52    56    60    64


mask(:,:,1) =

     0     0     0     0
     0     0     0     0
     0     0     0     0
     0     0     0     0


mask(:,:,2) =

     0     0     0     0
     0     1     1     0
     0     1     1     0
     0     0     0     0


mask(:,:,3) =

     0     0     0     0
     0     1     1     0
     0     1     1     0
     0     0     0     0


mask(:,:,4) =

     0     0     0     0
     0     0     0     0
     0     0     0     0
     0     0     0     0


In [63]:
vec = a[mask]

In [64]:
vec


Out[64]:
array([22, 23, 26, 27, 38, 39, 42, 43])

In [65]:
%%matlab
vec = a(mask)


vec =

    22
    23
    26
    27
    38
    39
    42
    43

Well, this is the same. So why the hell do I get different results when I do the same with nifti files?


In [66]:
import nibabel as nib

In [81]:
test_file = '/data1/scores/retest/out/sc07/stability_maps/_fmri_sub39529_session2_rest_stability_maps.nii.gz'
test_img = nib.load(test_file)
t_data = test_img.get_data()
t_net = t_data[...,0]
in_mask = '/data1/cambridge/template/template_cambridge_basc_multiscale_sym_scale007.nii.gz'
mask_img = nib.load(in_mask)
mask = mask_img.get_data() > 0

In [79]:
%%matlab
test_file = '/data1/scores/retest/out/sc07/stability_maps/_fmri_sub39529_session2_rest_stability_maps.nii.gz';
[t_hdr, t_data] = niak_read_vol(test_file);
t_net = t_data(:,:,:,1);
mask_template = '/data1/cambridge/template/template_cambridge_basc_multiscale_sym_scale007.nii.gz';
[m_hdr, m_vol] = niak_read_vol(mask_template);
mask = logical(m_vol);

In [70]:
mask.shape


Out[70]:
(53, 64, 52)

In [71]:
%%matlab
size(mask)


ans =

    53    64    52


In [73]:
mask[10,20:30,20]


Out[73]:
array([ True,  True,  True,  True, False, False, False,  True,  True,  True], dtype=bool)

In [74]:
%%matlab
mask(11,21:30,21)


ans =

     1     1     1     1     0     0     0     1     1     1

Still the same story. What is up with the test data?


In [82]:
t_net.shape


Out[82]:
(53, 64, 52)

In [83]:
%%matlab
size(t_net)


ans =

    53    64    52


In [85]:
t_net[10,20:30,30]


Out[85]:
array([ 0.11,  0.1 ,  0.  ,  0.  ,  0.  ,  0.  ,  0.03,  0.  ,  0.  ,  0.  ], dtype=float32)

In [86]:
%%matlab
t_net(11,21:30,31)


ans =

  Columns 1 through 7

    0.1100    0.1000         0         0         0         0    0.0300

  Columns 8 through 10

         0         0         0

Still the same story


In [92]:
net_mask = t_net[mask]
net_mask[:5]


Out[92]:
array([ 0.18000001,  0.14      ,  0.16      ,  0.12      ,  0.16      ], dtype=float32)

In [91]:
%%matlab
net_mask = t_net(mask);
net_mask(1:5)


ans =

    0.0100
    0.6000
    0.1100
    0.4100
    0.0300

And now it is different. Are they using the rows and columns differently?


In [ ]: