class TestSpectrometer(unittest.TestCase):

    def setUp(self):
        self.exampleFileName = "./msi/data/Transmission_15-49-35-978_filter700nm.txt"
        self.reader = SpectrometerReader()

    def tearDown(self):
        pass

    def test_create(self):
        self.assertTrue(True, "Created empty reader during setup")

    def test_read_spectrum(self):
        msi = self.reader.read(self.exampleFileName)

        self.assertAlmostEqual(msi.get_image()[0],
                               70.50,
                               msg="first spectral element is read correctly")
        self.assertAlmostEqual(msi.get_image()[-1],
                               68.13,
                               msg="last sprectral element is read correctly")
        self.assertTrue(msi.get_image().size == 2048,
                               "correct number of elements read")

    def test_read_wavelengths(self):
        msi = self.reader.read(self.exampleFileName)

        self.assertAlmostEqual(msi.get_wavelengths()[0],
                               187.255 * 10 ** -9,
                               msg="first wavelength element is read correctly")
        self.assertAlmostEqual(msi.get_wavelengths()[-1],
                               1103.852 * 10 ** -9,
                               msg="last wavelength element is read correctly")
        self.assertTrue(msi.get_wavelengths().size == 2048,
                               "correct number of elements read")
Beispiel #2
0
class TestSpectrometer(unittest.TestCase):
    def setUp(self):
        self.exampleFileName = "./msi/data/Transmission_15-49-35-978_filter700nm.txt"
        self.reader = SpectrometerReader()

    def tearDown(self):
        pass

    def test_create(self):
        self.assertTrue(True, "Created empty reader during setup")

    def test_read_spectrum(self):
        msi = self.reader.read(self.exampleFileName)

        self.assertAlmostEqual(msi.get_image()[0],
                               70.50,
                               msg="first spectral element is read correctly")
        self.assertAlmostEqual(msi.get_image()[-1],
                               68.13,
                               msg="last sprectral element is read correctly")
        self.assertTrue(msi.get_image().size == 2048,
                        "correct number of elements read")

    def test_read_wavelengths(self):
        msi = self.reader.read(self.exampleFileName)

        self.assertAlmostEqual(
            msi.get_wavelengths()[0],
            187.255 * 10**-9,
            msg="first wavelength element is read correctly")
        self.assertAlmostEqual(msi.get_wavelengths()[-1],
                               1103.852 * 10**-9,
                               msg="last wavelength element is read correctly")
        self.assertTrue(msi.get_wavelengths().size == 2048,
                        "correct number of elements read")
Beispiel #3
0
    def run(self):
        # load spectrometer measurement
        spectrometer_reader = SpectrometerReader()
        spectrometer_msi = spectrometer_reader.read(self.input()[0].path)

        # the wavelengths recorded by the spectrometer
        spectrometer_wavelengths = spectrometer_msi.get_wavelengths()

        spectrometer_white = spectrometer_reader.read(
            os.path.join(sc.get_full_dir("DATA_FOLDER"),
                         "spectrometer_whitebalance",
                         "white_IL_1_OO_20ms.txt"))
        spectrometer_dark = spectrometer_reader.read(
            os.path.join(sc.get_full_dir("DATA_FOLDER"),
                         "spectrometer_whitebalance", "dark_1_OO_20ms.txt"))
        msimani.dark_correction(spectrometer_white, spectrometer_dark)
        white_interpolator = interp1d(spectrometer_white.get_wavelengths(),
                                      spectrometer_white.get_image(),
                                      bounds_error=False,
                                      fill_value=0.)
        white_interpolated = white_interpolator(spectrometer_wavelengths)

        camera_qe = pd.read_csv(self.input()[2].path)
        camera_qe_interpolator = interp1d(camera_qe["wavelengths"] * 10**-9,
                                          camera_qe["quantum efficiency"],
                                          bounds_error=False,
                                          fill_value=0.)
        camera_qe_interpolated = \
            camera_qe_interpolator(spectrometer_wavelengths)

        # camera batch creation:
        new_reflectances = []
        for band in self.input()[1]:
            df_filter = pd.read_csv(band.path)
            interpolator = interp1d(df_filter["wavelengths"],
                                    df_filter["reflectances"],
                                    assume_sorted=False,
                                    bounds_error=False)
            # use this to create new reflectances
            interpolated_filter = interpolator(spectrometer_wavelengths)
            # if a wavelength cannot be interpolated, set it to 0
            interpolated_filter = np.nan_to_num(interpolated_filter)
            # account for cameras quantum efficiency
            interpolated_filter *= camera_qe_interpolated * white_interpolated
            # normalize band response
            #normalize(interpolated_filter.reshape(1, -1), norm='l1', copy=False)
            folded_reflectance = np.dot(spectrometer_msi.get_image(),
                                        interpolated_filter)
            new_reflectances.append(folded_reflectance)
            plt.plot(interpolated_filter)
        new_reflectances = np.array(new_reflectances).T
        spectrometer_msi.set_image(
            new_reflectances, wavelengths=sc.other["RECORDED_WAVELENGTHS"])

        # write it
        nrrd_writer = NrrdWriter(spectrometer_msi)
        nrrd_writer.write(self.output().path)
    def run(self):
        # load spectrometer measurement
        spectrometer_reader = SpectrometerReader()
        spectrometer_msi = spectrometer_reader.read(self.input()[0].path)

        # the wavelengths recorded by the spectrometer
        spectrometer_wavelengths = spectrometer_msi.get_wavelengths()

        spectrometer_white = spectrometer_reader.read(os.path.join(
                sc.get_full_dir("DATA_FOLDER"), "spectrometer_whitebalance",
                "white_IL_1_OO_20ms.txt"))
        spectrometer_dark = spectrometer_reader.read(os.path.join(
                sc.get_full_dir("DATA_FOLDER"), "spectrometer_whitebalance",
                "dark_1_OO_20ms.txt"))
        msimani.dark_correction(spectrometer_white, spectrometer_dark)
        white_interpolator = interp1d(spectrometer_white.get_wavelengths(),
                                      spectrometer_white.get_image(),
                                      bounds_error=False, fill_value=0.)
        white_interpolated = white_interpolator(spectrometer_wavelengths)

        camera_qe = pd.read_csv(self.input()[2].path)
        camera_qe_interpolator = interp1d(camera_qe["wavelengths"] * 10**-9,
                                          camera_qe["quantum efficiency"],
                                          bounds_error=False,
                                          fill_value=0.)
        camera_qe_interpolated = \
            camera_qe_interpolator(spectrometer_wavelengths)

        # camera batch creation:
        new_reflectances = []
        for band in self.input()[1]:
            df_filter = pd.read_csv(band.path)
            interpolator = interp1d(df_filter["wavelengths"],
                                    df_filter["reflectances"],
                                    assume_sorted=False, bounds_error=False)
            # use this to create new reflectances
            interpolated_filter = interpolator(spectrometer_wavelengths)
            # if a wavelength cannot be interpolated, set it to 0
            interpolated_filter = np.nan_to_num(interpolated_filter)
            # account for cameras quantum efficiency
            interpolated_filter *= camera_qe_interpolated * white_interpolated
            # normalize band response
            #normalize(interpolated_filter.reshape(1, -1), norm='l1', copy=False)
            folded_reflectance = np.dot(spectrometer_msi.get_image(),
                                        interpolated_filter)
            new_reflectances.append(folded_reflectance)
            plt.plot(interpolated_filter)
        new_reflectances = np.array(new_reflectances).T
        spectrometer_msi.set_image(new_reflectances,
                                   wavelengths=sc.other["RECORDED_WAVELENGTHS"])

        # write it
        nrrd_writer = NrrdWriter(spectrometer_msi)
        nrrd_writer.write(self.output().path)
Beispiel #5
0
    def run(self):
        reader = SpectrometerReader()
        filter_transmission = reader.read(self.input().path)
        # filter high and low _wavelengths
        wavelengths = filter_transmission.get_wavelengths()
        fi_image = filter_transmission.get_image()
        fi_image[wavelengths < 450 * 10 ** -9] = 0.0
        fi_image[wavelengths > 720 * 10 ** -9] = 0.0
        # filter elements farther away than +- 30nm
        file_name = os.path.split(self.input_file)[1]
        name_to_float = float(os.path.splitext(file_name)[0])
        fi_image[wavelengths < (name_to_float - 30) * 10 ** -9] = 0.0
        fi_image[wavelengths > (name_to_float + 30) * 10 ** -9] = 0.0
        # elements < 0 are set to 0.
        fi_image[fi_image < 0.0] = 0.0

        # write it to a dataframe
        df = pd.DataFrame()
        df["wavelengths"] = wavelengths
        df["reflectances"] = fi_image
        df.to_csv(self.output().path, index=False)
Beispiel #6
0
    def run(self):
        reader = SpectrometerReader()
        filter_transmission = reader.read(self.input().path)
        # filter high and low _wavelengths
        wavelengths = filter_transmission.get_wavelengths()
        fi_image = filter_transmission.get_image()
        fi_image[wavelengths < 450 * 10**-9] = 0.0
        fi_image[wavelengths > 720 * 10**-9] = 0.0
        # filter elements farther away than +- 30nm
        file_name = os.path.split(self.input_file)[1]
        name_to_float = float(os.path.splitext(file_name)[0])
        fi_image[wavelengths < (name_to_float - 30) * 10**-9] = 0.0
        fi_image[wavelengths > (name_to_float + 30) * 10**-9] = 0.0
        # elements < 0 are set to 0.
        fi_image[fi_image < 0.0] = 0.0

        # write it to a dataframe
        df = pd.DataFrame()
        df["wavelengths"] = wavelengths
        df["reflectances"] = fi_image
        df.to_csv(self.output().path, index=False)