コード例 #1
0
ファイル: test_nifti_rw.py プロジェクト: yang20085936/MONAI
    def test_write_5d(self):
        out_dir = tempfile.mkdtemp()
        image_name = os.path.join(out_dir, "test.nii.gz")
        img = np.arange(12).reshape((1, 1, 3, 2, 2))
        write_nifti(img,
                    image_name,
                    affine=np.diag([1]),
                    target_affine=np.diag([1.4]))
        out = nib.load(image_name)
        np.testing.assert_allclose(
            out.get_fdata(),
            np.array([[[[[0.0, 1.0], [2.0, 3.0]], [[4.0, 5.0], [6.0, 7.0]],
                        [[8.0, 9.0], [10.0, 11.0]]]]]),
        )
        np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))

        image_name = os.path.join(out_dir, "test1.nii.gz")
        img = np.arange(10).reshape((1, 1, 5, 1, 2))
        write_nifti(img,
                    image_name,
                    affine=np.diag([1, 1, 1, 3, 3]),
                    target_affine=np.diag([1.4, 2.0, 2, 3, 5]))
        out = nib.load(image_name)
        np.testing.assert_allclose(
            out.get_fdata(),
            np.array([[[[[0.0, 1.0]], [[4.0, 5.0]], [[8.0, 9.0]]]]]))
        np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]))
        shutil.rmtree(out_dir)
コード例 #2
0
ファイル: test_nifti_rw.py プロジェクト: yang20085936/MONAI
    def test_orientation(self, array, affine, reader_param, expected):
        test_image = make_nifti_image(array, affine)

        # read test cases
        loader = LoadNifti(**reader_param)
        load_result = loader(test_image)
        if isinstance(load_result, tuple):
            data_array, header = load_result
        else:
            data_array = load_result
            header = None
        if os.path.exists(test_image):
            os.remove(test_image)

        # write test cases
        if header is not None:
            write_nifti(data_array, test_image, header["affine"],
                        header.get("original_affine", None))
        elif affine is not None:
            write_nifti(data_array, test_image, affine)
        saved = nib.load(test_image)
        saved_affine = saved.affine
        saved_data = saved.get_fdata()
        if os.path.exists(test_image):
            os.remove(test_image)

        if affine is not None:
            np.testing.assert_allclose(saved_affine, affine)
        np.testing.assert_allclose(saved_data, expected)
コード例 #3
0
    def test_write_5d(self):
        with tempfile.TemporaryDirectory() as out_dir:
            image_name = os.path.join(out_dir, "test.nii.gz")
            for p in TEST_NDARRAYS:
                img = p(np.arange(12).reshape((1, 1, 3, 2, 2)))
                write_nifti(img,
                            image_name,
                            affine=np.diag([1]),
                            target_affine=np.diag([1.4]))
                out = nib.load(image_name)
                np.testing.assert_allclose(
                    out.get_fdata(),
                    np.array([[[[[0.0, 1.0], [2.0, 3.0]],
                                [[4.0, 5.0], [6.0, 7.0]],
                                [[8.0, 9.0], [10.0, 11.0]]]]]),
                )
                np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))

                image_name = os.path.join(out_dir, "test1.nii.gz")
                img = p(np.arange(10).reshape((1, 1, 5, 1, 2)))
                write_nifti(img,
                            image_name,
                            affine=np.diag([1, 1, 1, 3, 3]),
                            target_affine=np.diag([1.4, 2.0, 2, 3, 5]))
                out = nib.load(image_name)
                np.testing.assert_allclose(
                    out.get_fdata(),
                    np.array([[[[[0.0, 1.0]], [[4.0, 5.0]], [[8.0, 9.0]]]]]))
                np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]))
コード例 #4
0
ファイル: test_nifti_rw.py プロジェクト: zimaxeg/MONAI
 def test_write_5d(self):
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, 'test.nii.gz')
         img = np.arange(12).reshape((1, 1, 3, 2, 2))
         write_nifti(img,
                     image_name,
                     affine=np.diag([1]),
                     target_affine=np.diag([1.4]))
         out = nib.load(image_name)
         np.testing.assert_allclose(
             out.get_fdata(),
             np.array([[[[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]],
                         [[8., 9.], [10., 11.]]]]]))
         np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, 'test.nii.gz')
         img = np.arange(10).reshape((1, 1, 5, 1, 2))
         write_nifti(img,
                     image_name,
                     affine=np.diag([1, 1, 1, 3, 3]),
                     target_affine=np.diag([1.4, 2., 2, 3, 5]))
         out = nib.load(image_name)
         np.testing.assert_allclose(
             out.get_fdata(),
             np.array([[[[[0., 1.]], [[4., 5.]], [[8., 9.]]]]]))
         np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]))
コード例 #5
0
    def __call__(self, data):
        logger.setLevel(data.get("logging", "INFO").upper())

        ext = file_ext(data.get("image_path"))
        dtype = data.get(self.key_dtype, None)
        compress = data.get(self.key_compress, False)
        write_to_file = data.get(self.key_write_to_file, True)
        ext = data.get(self.key_extension) if data.get(self.key_extension) else ext
        logger.info(f"Result ext: {ext}; write_to_file: {write_to_file}; dtype: {dtype}")

        image_np = data[self.label]
        meta_dict = data.get(f"{self.ref_image}_{self.meta_key_postfix}")
        affine = meta_dict.get("affine") if meta_dict else None
        logger.debug(f"Image: {image_np.shape}; Data Image: {data[self.label].shape}")

        output_file = None
        output_json = data.get(self.json, {})
        if write_to_file:
            output_file = tempfile.NamedTemporaryFile(suffix=ext).name
            logger.debug(f"Saving Image to: {output_file}")

            # Issue with slicer:: https://discourse.itk.org/t/saving-non-orthogonal-volume-in-nifti-format/2760/22
            if self.nibabel and ext.lower() in [".nii", ".nii.gz"]:
                logger.debug("Using MONAI write_nifti...")
                write_nifti(image_np, output_file, affine=affine, output_dtype=dtype)
            else:
                write_itk(image_np, output_file, affine, dtype, compress)

        return output_file, output_json
コード例 #6
0
 def test_consistency(self):
     np.set_printoptions(suppress=True, precision=3)
     test_image = make_nifti_image(np.arange(64).reshape(1, 8, 8), np.diag([1.5, 1.5, 1.5, 1]))
     data, header = LoadNifti(as_closest_canonical=False)(test_image)
     data, original_affine, new_affine = Spacing([0.8, 0.8, 0.8])(
         data[None], header["affine"], interp_order="nearest"
     )
     data, _, new_affine = Orientation("ILP")(data, new_affine)
     if os.path.exists(test_image):
         os.remove(test_image)
     write_nifti(data[0], test_image, new_affine, original_affine, interp_order="nearest", mode="border")
     saved = nib.load(test_image)
     saved_data = saved.get_fdata()
     np.testing.assert_allclose(saved_data, np.arange(64).reshape(1, 8, 8), atol=1e-7)
     if os.path.exists(test_image):
         os.remove(test_image)
     write_nifti(
         data[0],
         test_image,
         new_affine,
         original_affine,
         interp_order="nearest",
         mode="border",
         output_shape=(1, 8, 8),
     )
     saved = nib.load(test_image)
     saved_data = saved.get_fdata()
     np.testing.assert_allclose(saved_data, np.arange(64).reshape(1, 8, 8), atol=1e-7)
     if os.path.exists(test_image):
         os.remove(test_image)
コード例 #7
0
ファイル: convert.py プロジェクト: Project-MONAI/MONAILabel
def binary_to_image(reference_image,
                    label,
                    dtype=np.uint16,
                    file_ext=".nii.gz",
                    use_itk=True):
    start = time.time()

    image_np, meta_dict = LoadImage()(reference_image)
    label_np = np.fromfile(label, dtype=dtype)

    logger.info(f"Image: {image_np.shape}")
    logger.info(f"Label: {label_np.shape}")

    label_np = label_np.reshape(image_np.shape, order="F")
    logger.info(f"Label (reshape): {label_np.shape}")

    output_file = tempfile.NamedTemporaryFile(suffix=file_ext).name
    affine = meta_dict.get("affine")
    if use_itk:
        write_itk(label_np,
                  output_file,
                  affine=affine,
                  dtype=None,
                  compress=True)
    else:
        write_nifti(label_np, output_file, affine=affine)

    logger.info(f"binary_to_image latency : {time.time() - start} (sec)")
    return output_file
コード例 #8
0
    def test_write_4d(self):
        with tempfile.TemporaryDirectory() as out_dir:
            image_name = os.path.join(out_dir, "test.nii.gz")
            img = np.arange(6).reshape((1, 1, 3, 2))
            write_nifti(img, image_name, affine=np.diag([1.4, 1]), target_affine=np.diag([1, 1.4, 1]))
            out = nib.load(image_name)
            np.testing.assert_allclose(out.get_fdata(), [[[[0, 1], [2, 3], [4, 5]]]])
            np.testing.assert_allclose(out.affine, np.diag([1, 1.4, 1, 1]))

            image_name = os.path.join(out_dir, "test1.nii.gz")
            img = np.arange(5).reshape((1, 1, 5, 1))
            write_nifti(img, image_name, affine=np.diag([1, 1, 1, 3, 3]), target_affine=np.diag([1.4, 2.0, 2, 3, 5]))
            out = nib.load(image_name)
            np.testing.assert_allclose(out.get_fdata(), [[[[0], [2], [4]]]])
            np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]))
コード例 #9
0
    def test_write_3d(self):
        out_dir = tempfile.mkdtemp()
        image_name = os.path.join(out_dir, "test.nii.gz")
        img = np.arange(6).reshape((1, 2, 3))
        write_nifti(img, image_name, affine=np.diag([1]), target_affine=np.diag([1.4]))
        out = nib.load(image_name)
        np.testing.assert_allclose(out.get_fdata(), [[[0, 1, 2], [3, 4, 5]]])
        np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))

        image_name = os.path.join(out_dir, "test1.nii.gz")
        img = np.arange(5).reshape((1, 1, 5))
        write_nifti(img, image_name, affine=np.diag([1, 1, 1, 3, 3]), target_affine=np.diag([1.4, 2.0, 2, 3, 5]))
        out = nib.load(image_name)
        np.testing.assert_allclose(out.get_fdata(), [[[0, 2, 4]]])
        np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 2, 1]))
        shutil.rmtree(out_dir)
コード例 #10
0
ファイル: test_nifti_rw.py プロジェクト: zimaxeg/MONAI
 def test_write_2d(self):
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, 'test.nii.gz')
         img = np.arange(6).reshape((2, 3))
         write_nifti(img,
                     image_name,
                     affine=np.diag([1]),
                     target_affine=np.diag([1.4]))
         out = nib.load(image_name)
         np.testing.assert_allclose(out.get_fdata(), [[0, 1, 2], [0, 0, 0]])
         np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, 'test.nii.gz')
         img = np.arange(5).reshape((1, 5))
         write_nifti(img,
                     image_name,
                     affine=np.diag([1, 1, 1, 3, 3]),
                     target_affine=np.diag([1.4, 2., 1, 3, 5]))
         out = nib.load(image_name)
         np.testing.assert_allclose(out.get_fdata(), [[0, 2, 4]])
         np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 1, 1]))
コード例 #11
0
ファイル: test_nifti_rw.py プロジェクト: yang20085936/MONAI
    def test_write_1d(self):
        out_dir = tempfile.mkdtemp()
        image_name = os.path.join(out_dir, "test.nii.gz")
        img = np.arange(5).reshape(-1)
        write_nifti(img,
                    image_name,
                    affine=np.diag([1, 1, 1]),
                    target_affine=np.diag([1.4, 2.0, 1]))
        out = nib.load(image_name)
        np.testing.assert_allclose(out.get_fdata(), [0, 1, 3, 0])
        np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))

        image_name = os.path.join(out_dir, "test1.nii.gz")
        img = np.arange(5).reshape(-1)
        write_nifti(img, image_name, affine=[[1]], target_affine=[[1.4]])
        out = nib.load(image_name)
        np.testing.assert_allclose(out.get_fdata(), [0, 1, 3, 0])
        np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))

        image_name = os.path.join(out_dir, "test2.nii.gz")
        img = np.arange(5).reshape(-1)
        write_nifti(img,
                    image_name,
                    affine=np.diag([1.5, 1.5, 1.5]),
                    target_affine=np.diag([1.5, 1.5, 1.5]))
        out = nib.load(image_name)
        np.testing.assert_allclose(out.get_fdata(), np.arange(5).reshape(-1))
        np.testing.assert_allclose(out.affine, np.diag([1.5, 1, 1, 1]))
        shutil.rmtree(out_dir)
コード例 #12
0
ファイル: test_nifti_rw.py プロジェクト: gagandaroach/MONAI
 def test_consistency(self):
     np.set_printoptions(suppress=True, precision=3)
     test_image = make_nifti_image(
         np.arange(64).reshape(1, 8, 8), np.diag([1.5, 1.5, 1.5, 1]))
     data, header = LoadImage(reader="NibabelReader",
                              as_closest_canonical=False)(test_image)
     data, original_affine, new_affine = Spacing([0.8, 0.8,
                                                  0.8])(data[None],
                                                        header["affine"],
                                                        mode="nearest")
     data, _, new_affine = Orientation("ILP")(data, new_affine)
     if os.path.exists(test_image):
         os.remove(test_image)
     write_nifti(data[0],
                 test_image,
                 new_affine,
                 original_affine,
                 mode="nearest",
                 padding_mode="border")
     saved = nib.load(test_image)
     saved_data = saved.get_fdata()
     np.testing.assert_allclose(saved_data,
                                np.arange(64).reshape(1, 8, 8),
                                atol=1e-7)
     if os.path.exists(test_image):
         os.remove(test_image)
     write_nifti(
         data[0],
         test_image,
         new_affine,
         original_affine,
         mode="nearest",
         padding_mode="border",
         output_spatial_shape=(1, 8, 8),
     )
     saved = nib.load(test_image)
     saved_data = saved.get_fdata()
     np.testing.assert_allclose(saved_data,
                                np.arange(64).reshape(1, 8, 8),
                                atol=1e-7)
     if os.path.exists(test_image):
         os.remove(test_image)
     # test the case that only correct orientation but don't resample
     write_nifti(data[0],
                 test_image,
                 new_affine,
                 original_affine,
                 resample=False)
     saved = nib.load(test_image)
     # compute expected affine
     start_ornt = nib.orientations.io_orientation(new_affine)
     target_ornt = nib.orientations.io_orientation(original_affine)
     ornt_transform = nib.orientations.ornt_transform(
         start_ornt, target_ornt)
     data_shape = data[0].shape
     expected_affine = new_affine @ nib.orientations.inv_ornt_aff(
         ornt_transform, data_shape)
     np.testing.assert_allclose(saved.affine, expected_affine)
     if os.path.exists(test_image):
         os.remove(test_image)
コード例 #13
0
ファイル: test_nifti_rw.py プロジェクト: w-j-p/MONAI
 def test_write_1d(self):
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, "test.nii.gz")
         img = np.arange(5).reshape(-1)
         write_nifti(img,
                     image_name,
                     affine=np.diag([1, 1, 1]),
                     target_affine=np.diag([1.4, 2.0, 1]))
         out = nib.load(image_name)
         np.testing.assert_allclose(out.get_fdata(), [0, 1, 3, 0])
         np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, "test.nii.gz")
         img = np.arange(5).reshape(-1)
         write_nifti(img, image_name, affine=[[1]], target_affine=[[1.4]])
         out = nib.load(image_name)
         np.testing.assert_allclose(out.get_fdata(), [0, 1, 3, 0])
         np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1]))
     with tempfile.TemporaryDirectory() as out_dir:
         image_name = os.path.join(out_dir, "test.nii.gz")
         img = np.arange(5).reshape(-1)
         write_nifti(img,
                     image_name,
                     affine=np.diag([1.5, 1.5, 1.5]),
                     target_affine=np.diag([1.5, 1.5, 1.5]))
         out = nib.load(image_name)
         np.testing.assert_allclose(out.get_fdata(),
                                    np.arange(5).reshape(-1))
         np.testing.assert_allclose(out.affine, np.diag([1.5, 1, 1, 1]))
コード例 #14
0
        s = [bb[0, 0] - 16, bb[0, 2] - 16, bb[0, 4] - 16]
        e = [bb[0, 1] + 16, bb[0, 3] + 16, bb[0, 5] + 16]

        # generate lesion patches based on the bounding boxes
        data_out = AddChanneld(keys)(data)
        data_out = SpatialCropd(keys, roi_start=s, roi_end=e)(data_out)
        resize = Resized(keys, patch_size, mode=("trilinear", "nearest"))
        data_out = resize(data_out)

        patch_out = (
            f"{folder}/patch/lesion_{s[0]}_{s[1]}_{s[2]}_{e[0]}_{e[1]}_{e[2]}_{name_id}"
        )
        label_out = (
            f"{folder}/patch/labels_{s[0]}_{s[1]}_{s[2]}_{e[0]}_{e[1]}_{e[2]}_{name_id}"
        )
        write_nifti(data_out["image"][0], file_name=patch_out)
        write_nifti(data_out["label"][0], file_name=label_out)

        # generate random negative samples
        rand_data_out = AddChanneld(keys)(data)
        rand_data_out["inv_label"] = (rand_data_out["label"] == 0
                                      )  # non-lesion sampling map
        rand_crop = RandWeightedCropd(keys,
                                      "inv_label",
                                      patch_size,
                                      num_samples=3)
        rand_data_out = rand_crop(rand_data_out)
        for idx, d in enumerate(rand_data_out):
            if np.sum(d["label"]) > 0:
                continue
            write_nifti(d["image"][0],
コード例 #15
0
def predictionAndEvaluation(params, test_loader, device, model):

    # TODO: affine matrices with different orientation -------------------------------------------------------------------------------
    # TODO: 2D affine 'ra' or 'rs'
    # TODO: respacing ---------------------------------
    test_affine = getAffineMatrix(params['orientation'], params['spacing'], params['model']['dimensions'])



    model_metrics = params['model']['metricfunction']
    model_metrics['reduction'] = 'none'
    eval_metric = eval(model_objects.concatFunctionArguments(model_metrics, prefix='monaiMetrics.'))

    original_dice_results = list()
    postprocess_dice_results = list()
    output_results = list()

    original_hd_results = list()
    postprocess_hd_results = list()

    original_mad_results = list()
    postprocess_mad_results = list()


    with torch.no_grad():
        for i, test_data in enumerate(test_loader):
            roi_size = params['model']['roi_size']
            sw_batch_size = params['model']['sw_batch_size']

            test_outputs = sliding_window_inference(test_data['IMAGE'].to(device), roi_size, sw_batch_size, model)
            argmax = torch.argmax(test_outputs, dim=1, keepdim=True)


            # Post-processing
            if params['postprocessing']:
                post_func = eval(model_objects.concatFunctionArguments(params['postprocessing_func'], prefix='monaiTrans.'))
                largest = post_func(argmax)
            else:
                largest = argmax


            # Write data out
            output_file_name = os.path.split(test_loader.dataset.data[i]['IMAGE'])[1]
            output_path = os.path.join(params['output_path'],output_file_name)
            output_results.append(output_path)

            test_data_nii = nib.load(test_loader.dataset.data[i]['IMAGE'])
            target_affine = test_data_nii.affine

            # Remove the translation component
            # TODO: 2D-----------------------------------------------------------------------------------------
            # TODO: xenos please take a look at it
            target_affine[0:params['model']['dimensions'],params['model']['dimensions']] = 0
            # TODO: xenos please take a look at it: resampling ------------------------------------------------------------------
            monaiData.write_nifti(largest.detach().cpu()[0, 0,...].numpy(), output_path,
                        mode='nearest',
                        affine=test_affine,
                        target_affine=target_affine,
                        output_spatial_shape=test_data_nii.shape,
                        dtype=np.float32
                       )

            if params['debug']:
                print(test_data['IMAGE'].shape)
                print(output_path)



            # evaluation scores
            if params['testSegm']:
                test_labels = test_data['SEGM'].to(device)


                value = eval_metric(y_pred=argmax, y=test_labels)
                print('Dice: {:.5f}'.format(value.item()))
                original_dice_results.append(value.item())

                hd_value = monaiMetrics.compute_hausdorff_distance(argmax, test_labels, label_idx=1, percentile=95)
                print('HD95: {:.5f}'.format(hd_value.item()))
                original_hd_results.append(hd_value)

                mad_value = monaiMetrics.compute_average_surface_distance(argmax, test_labels, label_idx=1)
                print('MAD: {:.5f}'.format(mad_value.item()))
                original_mad_results.append(mad_value)

                if params['postprocessing']:
                    value = eval_metric(y_pred=largest, y=test_labels)
                    postprocess_dice_results.append(value.item())
                    print('Post-processed Dice: {:.5f}'.format(value.item()))

                    hd_value = monaiMetrics.compute_hausdorff_distance(largest, test_labels, label_idx=1, percentile=95)
                    print('Post-processed HD95: {:.5f}'.format(hd_value.item()))
                    postprocess_hd_results.append(hd_value)

                    mad_value = monaiMetrics.compute_average_surface_distance(largest, test_labels, label_idx=1)
                    print('Post-processed HD95: {:.5f}'.format(mad_value.item()))
                    postprocess_mad_results.append(mad_value)



    if params['testSegm']:
        eval_results = pd.DataFrame()

        eval_results['IMAGE_DATA'] = [ i['IMAGE'] for i in test_loader.dataset.data]
        eval_results['SEGM_DATA'] = [ i['SEGM'] for i in test_loader.dataset.data]
        eval_results['SEGM_RESULTS'] = output_results

        eval_results['DICE'] = original_dice_results

        if params['postprocessing']:
            eval_results['POST_DICE'] = postprocess_dice_results

        eval_results['HD95'] = original_hd_results
        if params['postprocessing']:
            eval_results['POST_HD95'] = postprocess_hd_results

        eval_results['MAD'] = original_mad_results
        if params['postprocessing']:
            eval_results['POST_MAD'] = postprocess_mad_results


        eval_results.to_csv(os.path.join(params['output_path'],'evaluation_results.csv'), index=False)