def test_normalize_tpms(tmpdir): tempdir = str(tmpdir) in_mask = example_data('tpms_msk.nii.gz') mskdata = nb.load(in_mask, mmap=NUMPY_MMAP).get_data() mskdata[mskdata > 0.0] = 1.0 mapdata = [] in_files = [] out_files = [] for i in range(3): mapname = example_data('tpm_%02d.nii.gz' % i) filename = os.path.join(tempdir, 'modtpm_%02d.nii.gz' % i) out_files.append(os.path.join(tempdir, 'normtpm_%02d.nii.gz' % i)) im = nb.load(mapname, mmap=NUMPY_MMAP) data = im.get_data() mapdata.append(data.copy()) nb.Nifti1Image(2.0 * (data * mskdata), im.affine, im.header).to_filename(filename) in_files.append(filename) normalize_tpms(in_files, in_mask, out_files=out_files) sumdata = np.zeros_like(mskdata) for i, tstfname in enumerate(out_files): normdata = nb.load(tstfname, mmap=NUMPY_MMAP).get_data() sumdata += normdata assert np.all(normdata[mskdata == 0] == 0) assert np.allclose(normdata, mapdata[i]) assert np.allclose(sumdata[sumdata > 0.0], 1.0)
def test_normalize_tpms(tmpdir): in_mask = example_data("tpms_msk.nii.gz") mskdata = np.asanyarray(nb.load(in_mask).dataobj) mskdata[mskdata > 0.0] = 1.0 mapdata = [] in_files = [] out_files = [] for i in range(3): mapname = example_data("tpm_%02d.nii.gz" % i) filename = tmpdir.join("modtpm_%02d.nii.gz" % i).strpath out_files.append(tmpdir.join("normtpm_%02d.nii.gz" % i).strpath) im = nb.load(mapname) data = im.get_fdata() mapdata.append(data) nb.Nifti1Image(2.0 * (data * mskdata), im.affine, im.header).to_filename(filename) in_files.append(filename) normalize_tpms(in_files, in_mask, out_files=out_files) sumdata = np.zeros_like(mskdata) for i, tstfname in enumerate(out_files): normdata = nb.load(tstfname).get_fdata() sumdata += normdata assert np.all(normdata[mskdata == 0] == 0) assert np.allclose(normdata, mapdata[i]) assert np.allclose(sumdata[sumdata > 0.0], 1.0)
def test_normalize_tpms(): tempdir = mkdtemp() in_mask = example_data('tpms_msk.nii.gz' ) mskdata = nb.load( in_mask ).get_data() mskdata[mskdata>0.0] = 1.0 mapdata = [] in_files = [] out_files = [] for i in range(3): mapname = example_data('tpm_%02d.nii.gz' % i) filename = os.path.join(tempdir, 'modtpm_%02d.nii.gz' % i ) out_files.append(os.path.join(tempdir, 'normtpm_%02d.nii.gz' % i )) im = nb.load(mapname) data = im.get_data() mapdata.append( data.copy() ) nb.Nifti1Image(2.0 * (data * mskdata), im.get_affine(), im.get_header() ).to_filename(filename) in_files.append( filename ) normalize_tpms( in_files, in_mask, out_files=out_files ) sumdata = np.zeros_like(mskdata) for i,tstfname in enumerate( out_files ): normdata = nb.load( tstfname ).get_data() sumdata+=normdata yield assert_equal, np.all( normdata[mskdata==0]==0 ), True yield assert_equal, np.allclose( normdata, mapdata[i] ), True yield assert_equal, np.allclose(sumdata[sumdata>0.0], 1.0 ), True rmtree(tempdir)