示例#1
0
    def run(self):
        tiff_ring_reader = TiffRingReader()
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])

        # analyze all the first image files
        image_files = get_image_files_from_folder(self.flatfield_folder)
        image_files = filter(lambda image_name: "F0" in image_name,
                             image_files)

        # helper function to take maximum of two images
        def maximum_of_two_images(image_1, image_name_2):
            image_2 = tiff_ring_reader.read(
                os.path.join(self.flatfield_folder, image_name_2),
                nr_filters)[0].get_image()
            return np.maximum(image_1, image_2)

        # now reduce to maximum of all the single images
        flat_maximum = reduce(lambda x, y: maximum_of_two_images(x, y),
                              image_files, 0)
        msi = Msi(image=flat_maximum)
        msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"])

        # write flatfield as nrrd
        writer = NrrdWriter(msi)
        writer.write(self.output().path)
示例#2
0
    def run(self):
        tiff_ring_reader = TiffRingReader()
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])

        # analyze all the first image files
        image_files = get_image_files_from_folder(self.dark_folder,
                                                  suffix="F0.tiff")

        # returns the mean dark image vector of all inputted dark image
        # overly complicated TODO SW: make this simple code readable.
        dark_means = map(
            lambda image_name: msimani.calculate_mean_spectrum(
                tiff_ring_reader.read(
                    os.path.join(self.dark_folder, image_name), nr_filters)[0]
            ), image_files)
        dark_means_sum = reduce(lambda x, y: x + y.get_image(), dark_means, 0)
        final_dark_mean = dark_means_sum / len(dark_means)

        msi = Msi(image=final_dark_mean)
        msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"])

        # write flatfield as nrrd
        writer = NrrdWriter(msi)
        writer.write(self.output().path)
示例#3
0
    def run(self):
        tiff_ring_reader = TiffRingReader()
        nr_filters = len(sc.other["RECORDED_WAVELENGTHS"])

        # analyze all the first image files
        image_files = get_image_files_from_folder(self.dark_folder,
                                                  suffix="F0.tiff")

        # returns the mean dark image vector of all inputted dark image
        # overly complicated TODO SW: make this simple code readable.
        dark_means = map(lambda image_name:
                            msimani.calculate_mean_spectrum(
                                tiff_ring_reader.read(os.path.join(self.dark_folder, image_name),
                                                      nr_filters)[0]),
                         image_files)
        dark_means_sum = reduce(lambda x, y: x+y.get_image(), dark_means, 0)
        final_dark_mean = dark_means_sum / len(dark_means)

        msi = Msi(image=final_dark_mean)
        msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"])

        # write flatfield as nrrd
        writer = NrrdWriter(msi)
        writer.write(self.output().path)
    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")