{"action":"created","issue":{"url":"https://api.github.com/repos/nipy/nipy/issues/283","labels_url":"https://api.github.com/repos/nipy/nipy/issues/283/labels{/name}","comments_url":"https://api.github.com/repos/nipy/nipy/issues/283/comments","events_url":"https://api.github.com/repos/nipy/nipy/issues/283/events","html_url":"https://github.com/nipy/nipy/issues/283","id":18298570,"number":283,"title":"resample with nearest neighbor interpolation","user":{"login":"satra","id":184063,"avatar_url":"https://avatars.githubusercontent.com/u/184063?v=3","gravatar_id":"","url":"https://api.github.com/users/satra","html_url":"https://github.com/satra","followers_url":"https://api.github.com/users/satra/followers","following_url":"https://api.github.com/users/satra/following{/other_user}","gists_url":"https://api.github.com/users/satra/gists{/gist_id}","starred_url":"https://api.github.com/users/satra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/satra/subscriptions","organizations_url":"https://api.github.com/users/satra/orgs","repos_url":"https://api.github.com/users/satra/repos","events_url":"https://api.github.com/users/satra/events{/privacy}","received_events_url":"https://api.github.com/users/satra/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2013-08-20T14:23:11Z","updated_at":"2015-10-02T23:28:56Z","closed_at":null,"body":"i have a function that's meant to take a timeseries file and mask file, resample the mask file to the FOV of the timeseries data (they are both aligned) and then extract time series. but i'm having some trouble with the resample function. \r\n\r\nthe mask file has a bunch of labeled rois (is an int32 file; values range from 11-) but when i save the data, the values are all scaled. \r\n\r\nthe function below is based on a suggestion from @alexis-roche on the mailing list and he noted there was an issue with resample and unsigned ints, but these are signed ints. (http://webcache.googleusercontent.com/search?q=cache:http://mail.scipy.org/pipermail/nipy-devel/2012-July/008121.html)\r\n\r\n```\r\ndef extract_subrois(timeseries_file, mask_file, indices):\r\n    import os\r\n    import nibabel as nb\r\n    import nibabel as nb\r\n    import numpy as np\r\n    from nipy.core.image.image_spaces import xyz_affine\r\n    from nipy.algorithms.registration import resample\r\n    from nipy import save_image\r\n\r\n    def adjust_affine(aff, shape, target_shape):\r\n        b = np.zeros((4, 4))\r\n        b[:, 3] = aff[:, 3]\r\n        for i in range(3):\r\n            c = float(shape[i] - 1) / float(target_shape[i] - 1)\r\n            b[0:3, i] = c * aff[0:3, i]\r\n        return b\r\n\r\n    img = nb.load(timeseries_file)\r\n    data = img.get_data()\r\n\r\n    maskimg = nb.load(mask_file)\r\n    print maskimg.get_data().dtype\r\n    t_shape = img.shape[:3]\r\n    t_aff = adjust_affine(xyz_affine(maskimg), maskimg.shape, t_shape)\r\n    resliced_mask  = resample(maskimg, reference=(t_shape, t_aff),\r\n                              interp_order=0)\r\n    print np.histogram(maskimg.get_data())\r\n    mask = resliced_mask.get_data()\r\n    save_image(resliced_mask, 'mask.nii.gz')\r\n```\r\n\r\n### original values\r\n\r\n```\r\nIn [8]: img2 = nb.load('/Applications/freesurfer/subjects/fsaverage/mri/aparc+aseg.mgz')\r\n\r\nIn [9]: np.unique(img2.get_data().flatten())\r\nOut[9]: \r\narray([   0,    2,    4,    5,    7,    8,   10,   11,   12,   13,   14,\r\n         15,   16,   17,   18,   24,   26,   28,   30,   31,   41,   43,\r\n         44,   46,   47,   49,   50,   51,   52,   53,   54,   58,   60,\r\n         62,   63,   77,   85,  251,  252,  253,  254,  255, 1000, 1001,\r\n       1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012,\r\n       1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,\r\n       1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034,\r\n       1035, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,\r\n       2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020,\r\n       2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031,\r\n       2032, 2033, 2034, 2035], dtype=int32)\r\n```\r\n\r\n### new values\r\n\r\n```\r\nIn [11]: np.unique(img1.get_data().flatten())\r\nOut[11]: \r\narray([-804847616, -788070400, -771293184, -754515968, -737738752,\r\n       -720961536, -704184320, -687407104, -670629888, -653852672,\r\n       -637075456, -620298240, -603521024, -586743808, -569966592,\r\n       -553189376, -536412160, -519634944, -502857728, -486080512,\r\n       -469303296, -452526080, -435748864, -418971648, -402456576,\r\n       -402194432, -385679360, -385417216, -368902144, -368640000,\r\n       -352124928, -351862784, -335347712, -335085568, -318570496,\r\n       -318308352, -301793280, -301531136, -285016064, -284753920,\r\n       -268238848, -267976704, -251461632, -251199488, -234684416,\r\n       -234422272, -217907200, -217645056, -201129984, -184352768,\r\n       -167575552, -150798336, -134021120, -117243904, -100466688,\r\n        -83886080,  -83689472,  -67108864,  -66912256,  -50331648,\r\n        -50135040,  -33554432,  -33357824,  -16777216,  -16580608,\r\n                0,     262144,   17039360,   33554432,   33816576,\r\n         50593792,   67108864,   67371008,   83886080,   84148224,\r\n        100925440,  117440512,  117702656,  134217728,  134479872,\r\n        151257088,  167772160,  168034304,  184549376,  184811520,\r\n        201326592,  218103808,  234881024,  251658240,  268435456,\r\n        285212672,  301989888,  402653184,  436207616,  469762048,\r\n        503316480,  520093696,  687865856,  721420288,  738197504,\r\n        771751936,  788529152,  822083584,  838860800,  855638016,\r\n        872415232,  889192448,  905969664,  973078528, 1006632960,\r\n       1040187392, 1056964608, 1291845632, 1426063360], dtype=int32)\r\n```"},"comment":{"url":"https://api.github.com/repos/nipy/nipy/issues/comments/145182757","html_url":"https://github.com/nipy/nipy/issues/283#issuecomment-145182757","issue_url":"https://api.github.com/repos/nipy/nipy/issues/283","id":145182757,"user":{"login":"matthew-brett","id":67612,"avatar_url":"https://avatars.githubusercontent.com/u/67612?v=3","gravatar_id":"","url":"https://api.github.com/users/matthew-brett","html_url":"https://github.com/matthew-brett","followers_url":"https://api.github.com/users/matthew-brett/followers","following_url":"https://api.github.com/users/matthew-brett/following{/other_user}","gists_url":"https://api.github.com/users/matthew-brett/gists{/gist_id}","starred_url":"https://api.github.com/users/matthew-brett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthew-brett/subscriptions","organizations_url":"https://api.github.com/users/matthew-brett/orgs","repos_url":"https://api.github.com/users/matthew-brett/repos","events_url":"https://api.github.com/users/matthew-brett/events{/privacy}","received_events_url":"https://api.github.com/users/matthew-brett/received_events","type":"User","site_admin":false},"created_at":"2015-10-02T23:28:56Z","updated_at":"2015-10-02T23:28:56Z","body":"If it is broken, it would be good to fix before the release.  I'm currently planning to release on Saturday."}}