コード例 #1
0
ファイル: test_masking.py プロジェクト: saby9996/nilearn
def test__extrapolate_out_mask():
    # Input data:
    initial_data = np.zeros((5, 5, 5))
    initial_data[1, 2, 2] = 1
    initial_data[2, 1, 2] = 2
    initial_data[2, 2, 1] = 3
    initial_data[3, 2, 2] = 4
    initial_data[2, 3, 2] = 5
    initial_data[2, 2, 3] = 6
    initial_mask = initial_data.copy() != 0

    # Expected result
    target_data = np.array([[[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.],
                             [0., 0., 1., 0., 0.], [0., 0., 0., 0., 0.],
                             [0., 0., 0., 0., 0.]],
                            [[0., 0., 0., 0., 0.], [0., 0., 1.5, 0., 0.],
                             [0., 2., 1., 3.5, 0.], [0., 0., 3., 0., 0.],
                             [0., 0., 0., 0., 0.]],
                            [[0., 0., 2., 0., 0.], [0., 2.5, 2., 4., 0.],
                             [3., 3., 3.5, 6., 6.], [0., 4., 5., 5.5, 0.],
                             [0., 0., 5., 0., 0.]],
                            [[0., 0., 0., 0., 0.], [0., 0., 3., 0., 0.],
                             [0., 3.5, 4., 5., 0.], [0., 0., 4.5, 0., 0.],
                             [0., 0., 0., 0., 0.]],
                            [[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.],
                             [0., 0., 4., 0., 0.], [0., 0., 0., 0., 0.],
                             [0., 0., 0., 0., 0.]]])
    target_mask = np.array([[[False, False, False, False, False],
                             [False, False, False, False, False],
                             [False, False, True, False, False],
                             [False, False, False, False, False],
                             [False, False, False, False, False]],
                            [[False, False, False, False, False],
                             [False, False, True, False, False],
                             [False, True, True, True, False],
                             [False, False, True, False, False],
                             [False, False, False, False, False]],
                            [[False, False, True, False, False],
                             [False, True, True, True, False],
                             [True, True, True, True, True],
                             [False, True, True, True, False],
                             [False, False, True, False, False]],
                            [[False, False, False, False, False],
                             [False, False, True, False, False],
                             [False, True, True, True, False],
                             [False, False, True, False, False],
                             [False, False, False, False, False]],
                            [[False, False, False, False, False],
                             [False, False, False, False, False],
                             [False, False, True, False, False],
                             [False, False, False, False, False],
                             [False, False, False, False, False]]])

    # Test:
    extrapolated_data, extrapolated_mask = _extrapolate_out_mask(initial_data,
                                                                 initial_mask,
                                                                 iterations=1)
    assert_array_equal(extrapolated_data, target_data)
    assert_array_equal(extrapolated_mask, target_mask)
コード例 #2
0
    def _resample_image(self, image_file, output_file, target_nii):
        # Compute the background and extrapolate outside of the mask
        log.info("Extrapolating %s", image_file)

        niimg = nb.load(image_file)
        affine = niimg.get_affine()
        data = niimg.get_data().squeeze()
        niimg = nb.Nifti1Image(data, affine, header=niimg.get_header())
        bg_mask = compute_background_mask(niimg).get_data()

        # Test if the image has been masked:
        out_of_mask = data[np.logical_not(bg_mask)]
        if np.all(np.isnan(out_of_mask)) or len(np.unique(out_of_mask)) == 1:
            # Need to extrapolate
            data = _extrapolate_out_mask(data.astype(np.float),
                                         bg_mask, iterations=3)[0]
        niimg = nb.Nifti1Image(data, affine, header=niimg.get_header())
        del out_of_mask, bg_mask

        log.info("Resampling %s", image_file)
        resampled_nii = resample_img(
            niimg, target_nii.get_affine(), target_nii.shape)
        resampled_nii = nb.Nifti1Image(resampled_nii.get_data().squeeze(),
                                       resampled_nii.get_affine(),
                                       header=niimg.get_header())
        if len(resampled_nii.shape) == 3:
            resampled_nii.to_filename(output_file)
        else:
            # We have a 4D file
            raise Exception('4D File.')
            # assert len(resampled_nii.shape) == 4
            # resampled_data = resampled_nii.get_data()
            # affine = resampled_nii.get_affine()
            # for index in range(resampled_nii.shape[-1]):
            #     # First save the files separately
            #     this_nii = nb.Nifti1Image(resampled_data[..., index], affine)
            #     this_id = int("%i%i" % (-row[1]['image_id'], index))
            #     this_file = os.path.join(output_dir, "%06d%s" % (this_id, ext))
            #     this_nii.to_filename(this_file)
            #     # Second, fix the dataframe
            #     out_df = out_df[out_df.image_id != row[1]['image_id']]
            #     this_row = row[1].copy()
            #     this_row.image_id = this_id
            #     out_df = out_df.append(this_row)
        return output_file
コード例 #3
0
ファイル: api.py プロジェクト: ljchang/pyneurovault
def download_images(dest_dir,images_df=None,target=None,resample=True):
    """Downloads images dataframe and resamples them to a common space"""
    orig_path = os.path.join(dest_dir, "original")
    mkdir_p(orig_path)
    if resample == True:
        if not target:
            print "To resample you must specify a target!"
            return
        resampled_path = os.path.join(dest_dir, "resampled")
        mkdir_p(resampled_path)
        target_nii = nb.load(target)  

    if not isinstance(images_df,pandas.DataFrame):
        images_df = get_images()

    out_df = images_df.copy()

    for row in images_df.iterrows():
        # Downloading the file to the "original" subfolder
        _, _, ext = split_filename(row[1]['file'])
        orig_file = os.path.join(orig_path, "%04d%s" % (row[1]['image_id'], ext))
        if not os.path.exists(orig_file):
            try:
                print "Downloading %s" % orig_file
                urllib.urlretrieve(row[1]['file'], orig_file)

                if resample == True:
                    # Compute the background and extrapolate outside of the mask
                    print "Extrapolating %s" % orig_file
                    niimg = nb.load(orig_file)
                    affine = niimg.get_affine()
                    data = niimg.get_data().squeeze()
                    niimg = nb.Nifti1Image(data, affine,header=niimg.get_header())
                    bg_mask = compute_background_mask(niimg).get_data()
                    # Test if the image has been masked:
                    out_of_mask = data[np.logical_not(bg_mask)]
                    if np.all(np.isnan(out_of_mask)) or len(np.unique(out_of_mask)) == 1:
                        # Need to extrapolate
                        data = _extrapolate_out_mask(data.astype(np.float), bg_mask,iterations=3)[0]
                    niimg = nb.Nifti1Image(data, affine,header=niimg.get_header())
                    del out_of_mask, bg_mask
                    # Resampling the file to target and saving the output in the "resampled" folder
                    resampled_file = os.path.join(resampled_path,"%06d%s" % (row[1]['image_id'], ext))
                    print "Resampling %s" % orig_file
                    resampled_nii = resample_img(niimg, target_nii.get_affine(),target_nii.shape)
                    resampled_nii = nb.Nifti1Image(resampled_nii.get_data().squeeze(),
                                                   resampled_nii.get_affine(),
                                                     header=niimg.get_header())
                    if len(resampled_nii.shape) == 3: 
                        resampled_nii.to_filename(resampled_file)
                    else:
                        # We have a 4D file
                        assert len(resampled_nii.shape) == 4
                        resampled_data = resampled_nii.get_data()
                        affine = resampled_nii.get_affine()
                        for index in range(resampled_nii.shape[-1]):
                            # First save the files separately
                            this_nii = nb.Nifti1Image(resampled_data[..., index],affine)
                            this_id = int("%i%i" % (-row[1]['image_id'], index))
                            this_file = os.path.join(resampled_path,"%06d%s" % (this_id, ext))
                            this_nii.to_filename(this_file)
                            # Second, fix the dataframe
                            out_df = out_df[out_df.image_id != row[1]['image_id']]
                            this_row = row[1].copy()
                            this_row.image_id = this_id
                            out_df = out_df.append(this_row)
            except:
                print "Error downloading image id %s, retry this image." %(row[1]["image_id"])
    return out_df
コード例 #4
0
ファイル: api.py プロジェクト: chrisfilo/pyneurovault
  def download_and_resample(self, dest_dir, target,collection_ids=None,image_ids=None):
    """Downloads all stat maps and resamples them to a common space"""
    target_nii = nb.load(target)
    orig_path = os.path.join(dest_dir, "original")
    mkdir_p(orig_path)
    resampled_path = os.path.join(dest_dir, "resampled")
    mkdir_p(resampled_path)
    combined_df = self.get_images_with_collections_df()
    # If the user has specified specific images
    if image_ids:
      combined_df = combined_df.loc[combined_df.image_id.isin(image_ids)]
    # If the user wants to subset to a set of collections
    if collection_ids:
      if isinstance(collection_ids,str): collection_ids = [collection_ids]
      combined_df = combined_df[combined_df['collection_id'].isin(collection_ids)]
    out_df = combined_df.copy()

    for row in combined_df.iterrows():
      # Downloading the file to the "original" subfolder
      _, _, ext = split_filename(row[1]['file'])
      orig_file = os.path.join(orig_path, "%04d%s" % (row[1]['image_id'], ext))
      if not os.path.exists(orig_file):
        try:
          print "Downloading %s" % orig_file
          urllib.urlretrieve(row[1]['file'], orig_file)

          # Compute the background and extrapolate outside of the mask
          print "Extrapolating %s" % orig_file
          niimg = nb.load(orig_file)
          affine = niimg.get_affine()
          data = niimg.get_data().squeeze()
          niimg = nb.Nifti1Image(data, affine,header=niimg.get_header())
          bg_mask = compute_background_mask(niimg).get_data()
          # Test if the image has been masked:
          out_of_mask = data[np.logical_not(bg_mask)]
          if np.all(np.isnan(out_of_mask)) or len(np.unique(out_of_mask)) == 1:
            # Need to extrapolate
            data = _extrapolate_out_mask(data.astype(np.float), bg_mask,iterations=3)[0]
          niimg = nb.Nifti1Image(data, affine,header=niimg.get_header())
          del out_of_mask, bg_mask
          # Resampling the file to target and saving the output in the "resampled" folder
          resampled_file = os.path.join(resampled_path,"%06d%s" % (row[1]['image_id'], ext))
          print "Resampling %s" % orig_file
          resampled_nii = resample_img(niimg, target_nii.get_affine(),target_nii.shape)
          resampled_nii = nb.Nifti1Image(resampled_nii.get_data().squeeze(),
                                         resampled_nii.get_affine(),
                                         header=niimg.get_header())
          if len(resampled_nii.shape) == 3: 
            resampled_nii.to_filename(resampled_file)
          else:
            # We have a 4D file
            assert len(resampled_nii.shape) == 4
            resampled_data = resampled_nii.get_data()
            affine = resampled_nii.get_affine()
            for index in range(resampled_nii.shape[-1]):
              # First save the files separately
              this_nii = nb.Nifti1Image(resampled_data[..., index],affine)
              this_id = int("%i%i" % (-row[1]['image_id'], index))
              this_file = os.path.join(resampled_path,"%06d%s" % (this_id, ext))
              this_nii.to_filename(this_file)
              # Second, fix the dataframe
              out_df = out_df[out_df.image_id != row[1]['image_id']]
              this_row = row[1].copy()
              this_row.image_id = this_id
              out_df = out_df.append(this_row)
        except:
          print "Error downloading image id %s, retry this image." %(row[1]["image_id"])
    return out_df
コード例 #5
0
def download_images(dest_dir, images_df=None, target=None, resample=True):
    """Downloads images dataframe and resamples them to a common space"""
    orig_path = os.path.join(dest_dir, "original")
    mkdir_p(orig_path)
    if resample == True:
        if not target:
            print("To resample you must specify a target!")
            return
        resampled_path = os.path.join(dest_dir, "resampled")
        mkdir_p(resampled_path)
        target_nii = nb.load(target)

    if not isinstance(images_df, pandas.DataFrame):
        images_df = get_images()

    out_df = images_df.copy()

    for row in images_df.iterrows():
        # Downloading the file to the "original" subfolder
        _, _, ext = split_filename(row[1]['file'])
        orig_file = os.path.join(orig_path,
                                 "%04d%s" % (row[1]['image_id'], ext))
        if not os.path.exists(orig_file):
            try:
                print("Downloading %s" % orig_file)
                urlretrieve(row[1]['file'], orig_file)

                if resample == True:
                    # Compute the background and extrapolate outside of the mask
                    print("Extrapolating %s" % orig_file)
                    niimg = nb.load(orig_file)
                    affine = niimg.get_affine()
                    data = niimg.get_data().squeeze()
                    niimg = nb.Nifti1Image(data,
                                           affine,
                                           header=niimg.get_header())
                    bg_mask = compute_background_mask(niimg).get_data()
                    # Test if the image has been masked:
                    out_of_mask = data[np.logical_not(bg_mask)]
                    if np.all(np.isnan(out_of_mask)) or len(
                            np.unique(out_of_mask)) == 1:
                        # Need to extrapolate
                        data = _extrapolate_out_mask(data.astype(np.float),
                                                     bg_mask,
                                                     iterations=3)[0]
                    niimg = nb.Nifti1Image(data,
                                           affine,
                                           header=niimg.get_header())
                    del out_of_mask, bg_mask
                    # Resampling the file to target and saving the output in the "resampled" folder
                    resampled_file = os.path.join(
                        resampled_path, "%06d%s" % (row[1]['image_id'], ext))
                    print("Resampling %s" % orig_file)
                    resampled_nii = resample_img(niimg,
                                                 target_nii.get_affine(),
                                                 target_nii.shape)
                    resampled_nii = nb.Nifti1Image(
                        resampled_nii.get_data().squeeze(),
                        resampled_nii.get_affine(),
                        header=niimg.get_header())
                    if len(resampled_nii.shape) == 3:
                        resampled_nii.to_filename(resampled_file)
                    else:
                        # We have a 4D file
                        assert len(resampled_nii.shape) == 4
                        resampled_data = resampled_nii.get_data()
                        affine = resampled_nii.get_affine()
                        for index in range(resampled_nii.shape[-1]):
                            # First save the files separately
                            this_nii = nb.Nifti1Image(
                                resampled_data[..., index], affine)
                            this_id = int("%i%i" %
                                          (-row[1]['image_id'], index))
                            this_file = os.path.join(resampled_path,
                                                     "%06d%s" % (this_id, ext))
                            this_nii.to_filename(this_file)
                            # Second, fix the dataframe
                            out_df = out_df[
                                out_df.image_id != row[1]['image_id']]
                            this_row = row[1].copy()
                            this_row.image_id = this_id
                            out_df = out_df.append(this_row)
            except:
                print("Error downloading image id %s, retry this image." %
                      (row[1]["image_id"]))
    return out_df
コード例 #6
0
ファイル: test_masking.py プロジェクト: bthirion/nilearn
def test__extrapolate_out_mask():
    # Input data:
    initial_data = np.zeros((5,5,5))
    initial_data[1,2,2] = 1
    initial_data[2,1,2] = 2
    initial_data[2,2,1] = 3
    initial_data[3,2,2] = 4
    initial_data[2,3,2] = 5
    initial_data[2,2,3] = 6
    initial_mask = initial_data.copy() != 0

    # Expected result
    target_data = np.array([[[0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 1. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ]],

                            [[0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 1.5, 0. , 0. ],
                             [0. , 2. , 1. , 3.5, 0. ],
                             [0. , 0. , 3. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ]],

                            [[0. , 0. , 2. , 0. , 0. ],
                             [0. , 2.5, 2. , 4. , 0. ],
                             [3. , 3. , 3.5, 6. , 6. ],
                             [0. , 4. , 5. , 5.5, 0. ],
                             [0. , 0. , 5. , 0. , 0. ]],

                            [[0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 3. , 0. , 0. ],
                             [0. , 3.5, 4. , 5. , 0. ],
                             [0. , 0. , 4.5, 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ]],

                            [[0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 4. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ],
                             [0. , 0. , 0. , 0. , 0. ]]])
    target_mask = np.array([[[False, False, False, False, False],
                             [False, False, False, False, False],
                             [False, False,  True, False, False],
                             [False, False, False, False, False],
                             [False, False, False, False, False]],

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

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

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

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


    # Test:
    extrapolated_data, extrapolated_mask = _extrapolate_out_mask(initial_data, 
                                                                 initial_mask, 
                                                                 iterations=1)
    assert_array_equal(extrapolated_data, target_data)
    assert_array_equal(extrapolated_mask, target_mask)