Ejemplo n.º 1
0
    def test_imageWriterCreatesCorrectFile(self):

        writer = NrrdWriter(self.msi)
        writer.write(self.fileUriToWrite)

        reader = NrrdReader()
        msi = reader.read(self.fileUriToWrite)
        self.assertTrue(msi == helpers.getFakeMsi(),
                        "image correctly written and read")
Ejemplo n.º 2
0
    def test_imageWriterCreatesCorrectFile(self):

        writer = NrrdWriter(self.msi)
        writer.write(self.fileUriToWrite)

        reader = NrrdReader()
        msi = reader.read(self.fileUriToWrite)
        self.assertTrue(msi == helpers.getFakeMsi(),
                                       "image correctly written and read")
Ejemplo n.º 3
0
    def test_write_one_d_image_works(self):
        writer = NrrdWriter(self.msi)
        msimani.calculate_mean_spectrum(self.msi)
        writer.write(self.fileUriToWrite)

        reader = NrrdReader()
        msi = reader.read(self.fileUriToWrite)
        np.testing.assert_array_equal(msi.get_image(),
                                      np.array([1, 2, 3, 4, 5]),
                                      "1d image correctly written and read")
Ejemplo n.º 4
0
    def test_write_one_d_image_works(self):
        writer = NrrdWriter(self.msi)
        msimani.calculate_mean_spectrum(self.msi)
        writer.write(self.fileUriToWrite)

        reader = NrrdReader()
        msi = reader.read(self.fileUriToWrite)
        np.testing.assert_array_equal(msi.get_image(),
                                      np.array([1, 2, 3, 4, 5]),
                                      "1d image correctly written and read")
Ejemplo n.º 5
0
class TestNrrdReader(unittest.TestCase):

    def setUp(self):
        self.nrrdReader = NrrdReader()
        self.msi = self.nrrdReader.read('./msi/data/testMsi.nrrd')

    def test_read_does_not_crash(self):
        # if we got this far, at least an image was read.
        self.assertTrue(len(self.msi.get_image().shape) == 3,
                        "read image has correct basic shape dimensions")
        self.assertTrue(self.msi.get_image().shape[-1] == 5,
                        "read image has correct number of image stacks")
        self.assertTrue(np.array_equal(self.msi.get_image()[2, 2, :],
                        np.array([1, 2, 3, 4, 5])),
                        "read image contains correct data")

    def test_read_non_existing_image_returns_exception(self):
        with self.assertRaises(RuntimeError):
            self.nrrdReader.read("./msi/data/asdf.nrrd")
Ejemplo n.º 6
0
class TestNrrdReader(unittest.TestCase):

    def setUp(self):
        self.nrrdReader = NrrdReader()
        self.msi = self.nrrdReader.read('./msi/data/testMsi.nrrd')

    def test_read_does_not_crash(self):
        # if we got this far, at least an image was read.
        self.assertTrue(len(self.msi.get_image().shape) == 3,
                        "read image has correct basic shape dimensions")
        self.assertTrue(self.msi.get_image().shape[-1] == 5,
                        "read image has correct number of image stacks")
        self.assertTrue(np.array_equal(self.msi.get_image()[2, 2, :],
                        np.array([1, 2, 3, 4, 5])),
                        "read image contains correct data")

    def test_read_non_existing_image_returns_exception(self):
        with self.assertRaises(RuntimeError):
            self.nrrdReader.read("./msi/data/asdf.nrrd")
    def run(self):
        nrrd_reader = NrrdReader()
        tiff_ring_reader = TiffRingReader()
        # read the flatfield
        flat = nrrd_reader.read(self.input()[1].path)
        dark = nrrd_reader.read(self.input()[3].path)
        # read the msi
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])
        msi, segmentation = tiff_ring_reader.read(self.input()[2].path,
                                                  nr_filters)
        # only take into account not saturated pixels.
        segmentation = np.logical_and(segmentation,
                                      (np.max(msi.get_image(),
                                              axis=-1) < 1000.))

        # read the regressor
        e_file = open(self.input()[0].path, 'r')
        e = pickle.load(e_file)

        # correct image setup
        filter_nr = int(self.image_name[-6:-5])
        original_order = np.arange(nr_filters)
        new_image_order = np.concatenate((
                                original_order[nr_filters - filter_nr:],
                                original_order[:nr_filters - filter_nr]))
        # resort msi to restore original order
        msimani.get_bands(msi, new_image_order)
        # correct by flatfield
        msimani.image_correction(msi, flat, dark)

        # create artificial rgb
        rgb_image = msi.get_image()[:, :, [2, 3, 1]]
        rgb_image /= np.max(rgb_image)
        rgb_image *= 255.

        # preprocess the image
        # sortout unwanted bands
        print "1"
        # zero values would lead to infinity logarithm, thus clip.
        msi.set_image(np.clip(msi.get_image(), 0.00001, 2. ** 64))
        # normalize to get rid of lighting intensity
        norm.standard_normalizer.normalize(msi)
        # transform to absorption
        msi.set_image(-np.log(msi.get_image()))
        # normalize by l2 for stability
        norm.standard_normalizer.normalize(msi, "l2")
        print "2"
        # estimate
        sitk_image, time = estimate_image(msi, e)
        image = sitk.GetArrayFromImage(sitk_image)

        plt.figure()
        print "3"
        rgb_image = rgb_image.astype(np.uint8)
        im = Image.fromarray(rgb_image, 'RGB')
        enh_brightness = ImageEnhance.Brightness(im)
        im = enh_brightness.enhance(10.)
        plotted_image = np.array(im)
        top_left_axis = plt.gca()
        top_left_axis.imshow(plotted_image, interpolation='nearest')
        top_left_axis.xaxis.set_visible(False)
        top_left_axis.yaxis.set_visible(False)

        plt.set_cmap("jet")
        print "4"
        # plot parametric maps
        segmentation[0, 0] = 1
        segmentation[0, 1] = 1
        oxy_image = np.ma.masked_array(image[:, :, 0], ~segmentation)
        oxy_image[np.isnan(oxy_image)] = 0.
        oxy_image[np.isinf(oxy_image)] = 0.
        oxy_mean = np.mean(oxy_image)
        oxy_image[0, 0] = 0.0
        oxy_image[0, 1] = 1.

        plot_image(oxy_image[:, :], plt.gca())

        df_image_results = pd.DataFrame(data=np.expand_dims([self.image_name,
                                                             oxy_mean * 100.,
                                                             time], 0),
                                        columns=["image name",
                                                 "oxygenation mean [%]",
                                                 "time to estimate"])

        results_file = os.path.join(sc.get_full_dir("SMALL_BOWEL_RESULT"),
                                    "results.csv")
        if os.path.isfile(results_file):
            df_results = pd.read_csv(results_file, index_col=0)
            df_results = pd.concat((df_results, df_image_results)).reset_index(
                drop=True
            )
        else:
            df_results = df_image_results

        df_results.to_csv(results_file)

        plt.savefig(self.output().path,
                    dpi=250, bbox_inches='tight')
        plt.close("all")
    def run(self):

        print "... read data"

        segmentation_file = os.path.join(
                sc.get_full_dir("COLORCHECKER_DATA"), "seg.tiff")
        segmentation_file2 = os.path.join(
                sc.get_full_dir("COLORCHECKER_DATA"), "seg2.tiff")

        nrrd_reader = NrrdReader()
        tiff_ring_reader = TiffRingReader()
        # read the flatfield
        flat = nrrd_reader.read(self.input()[0].path)
        dark = nrrd_reader.read(self.input()[2].path)
        # read the msi
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])
        msi, segmentation = tiff_ring_reader.read(self.input()[1].path,
                                                  nr_filters,
                                                  segmentation=segmentation_file)
        msi_copy = copy.deepcopy(msi)  # copy to be able to apply both
        # segmentations
        msimani.apply_segmentation(msi, segmentation)
        msi2, segmentation2 = tiff_ring_reader.read(self.input()[1].path,
                                                    nr_filters,
                                                    segmentation=segmentation_file2)
        msimani.apply_segmentation(msi2, segmentation2)

        msimani.apply_segmentation(msi_copy, segmentation + segmentation2)

        # read the spectrometer measurement
        msi_spectrometer = nrrd_reader.read(self.input()[3].path)

        # correct by flatfield and dark image
        #msimani.image_correction(msi, flat, dark)
        #msimani.image_correction(msi2, flat, dark)
        #msimani.image_correction(msi_copy, flat, dark)
        msimani.dark_correction(msi, dark)
        msimani.dark_correction(msi2, dark)
        msimani.dark_correction(msi_copy, dark)

        # create artificial rgb
        rgb_image = msi_copy.get_image()[:, :, [2, 3, 1]]
        rgb_image /= np.max(rgb_image)
        rgb_image *= 255.

        # preprocess the image
        # sortout unwanted bands
        print "... apply normalizations"
        # normalize to get rid of lighting intensity
        norm.standard_normalizer.normalize(msi)
        norm.standard_normalizer.normalize(msi2)

        print "... plot"

        # plot of the rgb image
        rgb_image = rgb_image.astype(np.uint8)
        im = Image.fromarray(rgb_image, 'RGB')
        enh_brightness = ImageEnhance.Brightness(im)
        im = enh_brightness.enhance(2.)
        plotted_image = np.array(im)

        plt.figure()
        f, (ax_rgb, ax_spectra) = plt.subplots(1, 2)
        plot_image(plotted_image, ax_rgb, title="false rgb")

        msiplot.plotMeanError(msi, ax_spectra)
        msiplot.plotMeanError(msi2, ax_spectra)

        standard_normalizer.normalize(msi_spectrometer)
        msiplot.plot(msi_spectrometer, ax_spectra)

        mean_spectrometer = msi_spectrometer.get_image()
        mean_msi = msimani.calculate_mean_spectrum(msi).get_image()
        mean_msi2 = msimani.calculate_mean_spectrum(msi2).get_image()
        r2_msi = r2_score(mean_spectrometer, mean_msi)
        r2_msi2 = r2_score(mean_spectrometer, mean_msi2)

        ax_spectra.legend(["spectrocam 1 r2: " + str(r2_msi),
                           "spectrocam 2 r2: " + str(r2_msi2), "spectrometer"],
                  bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
                  ncol=2, mode="expand", borderaxespad=0.,
                  fontsize=5)

        plt.savefig(self.output().path,
                    dpi=250, bbox_inches='tight')
    def run(self):

        print "... read data"

        segmentation_file = os.path.join(
                sc.get_full_dir("COLORCHECKER_DATA"), "seg.tiff")
        segmentation_file2 = os.path.join(
                sc.get_full_dir("COLORCHECKER_DATA"), "seg2.tiff")

        nrrd_reader = NrrdReader()
        tiff_ring_reader = TiffRingReader()
        # read the flatfield
        flat = nrrd_reader.read(self.input()[0].path)
        dark = nrrd_reader.read(self.input()[2].path)
        # read the msi
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])
        msi, segmentation = tiff_ring_reader.read(self.input()[1].path,
                                                  nr_filters,
                                                  segmentation=segmentation_file)
        msi_copy = copy.deepcopy(msi)  # copy to be able to apply both
        # segmentations
        msimani.apply_segmentation(msi, segmentation)
        msi2, segmentation2 = tiff_ring_reader.read(self.input()[1].path,
                                                    nr_filters,
                                                    segmentation=segmentation_file2)
        msimani.apply_segmentation(msi2, segmentation2)

        msimani.apply_segmentation(msi_copy, segmentation + segmentation2)

        # read the spectrometer measurement
        msi_spectrometer = nrrd_reader.read(self.input()[3].path)

        # correct by flatfield and dark image
        #msimani.image_correction(msi, flat, dark)
        #msimani.image_correction(msi2, flat, dark)
        #msimani.image_correction(msi_copy, flat, dark)
        msimani.dark_correction(msi, dark)
        msimani.dark_correction(msi2, dark)
        msimani.dark_correction(msi_copy, dark)

        # create artificial rgb
        rgb_image = msi_copy.get_image()[:, :, [2, 3, 1]]
        rgb_image /= np.max(rgb_image)
        rgb_image *= 255.

        # preprocess the image
        # sortout unwanted bands
        print "... apply normalizations"
        # normalize to get rid of lighting intensity
        norm.standard_normalizer.normalize(msi)
        norm.standard_normalizer.normalize(msi2)

        print "... plot"

        # plot of the rgb image
        rgb_image = rgb_image.astype(np.uint8)
        im = Image.fromarray(rgb_image, 'RGB')
        enh_brightness = ImageEnhance.Brightness(im)
        im = enh_brightness.enhance(2.)
        plotted_image = np.array(im)

        plt.figure()
        f, (ax_rgb, ax_spectra) = plt.subplots(1, 2)
        plot_image(plotted_image, ax_rgb, title="false rgb")

        msiplot.plotMeanError(msi, ax_spectra)
        msiplot.plotMeanError(msi2, ax_spectra)

        standard_normalizer.normalize(msi_spectrometer)
        msiplot.plot(msi_spectrometer, ax_spectra)

        mean_spectrometer = msi_spectrometer.get_image()
        mean_msi = msimani.calculate_mean_spectrum(msi).get_image()
        mean_msi2 = msimani.calculate_mean_spectrum(msi2).get_image()
        r2_msi = r2_score(mean_spectrometer, mean_msi)
        r2_msi2 = r2_score(mean_spectrometer, mean_msi2)

        ax_spectra.legend(["spectrocam 1 r2: " + str(r2_msi),
                           "spectrocam 2 r2: " + str(r2_msi2), "spectrometer"],
                  bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
                  ncol=2, mode="expand", borderaxespad=0.,
                  fontsize=5)

        plt.savefig(self.output().path,
                    dpi=250, bbox_inches='tight')
    def run(self):
        nrrd_reader = NrrdReader()
        tiff_ring_reader = TiffRingReader()
        # read the flatfield
        flat = nrrd_reader.read(self.input()[1].path)
        dark = nrrd_reader.read(self.input()[3].path)
        # read the msi
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])
        msi, segmentation = tiff_ring_reader.read(self.input()[2].path,
                                                  nr_filters)
        # only take into account not saturated pixels.
        segmentation = np.logical_and(
            segmentation, (np.max(msi.get_image(), axis=-1) < 1000.))

        # read the regressor
        e_file = open(self.input()[0].path, 'r')
        e = pickle.load(e_file)

        # correct image setup
        filter_nr = int(self.image_name[-6:-5])
        original_order = np.arange(nr_filters)
        new_image_order = np.concatenate(
            (original_order[nr_filters - filter_nr:],
             original_order[:nr_filters - filter_nr]))
        # resort msi to restore original order
        msimani.get_bands(msi, new_image_order)
        # correct by flatfield
        msimani.image_correction(msi, flat, dark)

        # create artificial rgb
        rgb_image = msi.get_image()[:, :, [2, 3, 1]]
        rgb_image /= np.max(rgb_image)
        rgb_image *= 255.

        # preprocess the image
        # sortout unwanted bands
        print "1"
        # zero values would lead to infinity logarithm, thus clip.
        msi.set_image(np.clip(msi.get_image(), 0.00001, 2.**64))
        # normalize to get rid of lighting intensity
        norm.standard_normalizer.normalize(msi)
        # transform to absorption
        msi.set_image(-np.log(msi.get_image()))
        # normalize by l2 for stability
        norm.standard_normalizer.normalize(msi, "l2")
        print "2"
        # estimate
        sitk_image, time = estimate_image(msi, e)
        image = sitk.GetArrayFromImage(sitk_image)

        plt.figure()
        print "3"
        rgb_image = rgb_image.astype(np.uint8)
        im = Image.fromarray(rgb_image, 'RGB')
        enh_brightness = ImageEnhance.Brightness(im)
        im = enh_brightness.enhance(10.)
        plotted_image = np.array(im)
        top_left_axis = plt.gca()
        top_left_axis.imshow(plotted_image, interpolation='nearest')
        top_left_axis.xaxis.set_visible(False)
        top_left_axis.yaxis.set_visible(False)

        plt.set_cmap("jet")
        print "4"
        # plot parametric maps
        segmentation[0, 0] = 1
        segmentation[0, 1] = 1
        oxy_image = np.ma.masked_array(image[:, :, 0], ~segmentation)
        oxy_image[np.isnan(oxy_image)] = 0.
        oxy_image[np.isinf(oxy_image)] = 0.
        oxy_mean = np.mean(oxy_image)
        oxy_image[0, 0] = 0.0
        oxy_image[0, 1] = 1.

        plot_image(oxy_image[:, :], plt.gca())

        df_image_results = pd.DataFrame(
            data=np.expand_dims([self.image_name, oxy_mean * 100., time], 0),
            columns=["image name", "oxygenation mean [%]", "time to estimate"])

        results_file = os.path.join(sc.get_full_dir("SMALL_BOWEL_RESULT"),
                                    "results.csv")
        if os.path.isfile(results_file):
            df_results = pd.read_csv(results_file, index_col=0)
            df_results = pd.concat(
                (df_results, df_image_results)).reset_index(drop=True)
        else:
            df_results = df_image_results

        df_results.to_csv(results_file)

        plt.savefig(self.output().path, dpi=250, bbox_inches='tight')
        plt.close("all")
Ejemplo n.º 11
0
 def setUp(self):
     self.nrrdReader = NrrdReader()
     self.msi = self.nrrdReader.read('./msi/data/testMsi.nrrd')
Ejemplo n.º 12
0
 def setUp(self):
     self.nrrdReader = NrrdReader()
     self.msi = self.nrrdReader.read('./msi/data/testMsi.nrrd')