Пример #1
0
    def test_load_background(self):
        # Background data
        dcalib = numpy.array([1, 2, 2, 3, 4, 5, 4, 6, 9], dtype=numpy.uint16)
        dcalib.shape += (1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        # Give one DA, the correct one, so expect to get it back
        out = calibration.get_spectrum_data([calib])
        numpy.testing.assert_equal(out, calib)
        numpy.testing.assert_almost_equal(out.metadata[model.MD_WL_LIST],
                                          calib.metadata[model.MD_WL_LIST])

        # More DataArrays, just to make it slightly harder to find the data
        data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16))
        out = calibration.get_spectrum_data([data1, calib])
        numpy.testing.assert_equal(out, calib)
        numpy.testing.assert_almost_equal(out.metadata[model.MD_WL_LIST],
                                          calib.metadata[model.MD_WL_LIST])

        # should also load spectra with more than one points (then return just
        # the first point)
        dcalibxy = numpy.ones((128, 1, 1, 24, 25), dtype=numpy.uint8)
        dcalibxy[:, :, :, :] = range(dcalibxy.shape[-1])
        dcalibxy[0, 0, 0, 0, 0] = 0
        wl_calibxy = 400e-9 + numpy.array(range(dcalibxy.shape[0])) * 10e-9
        calibxy = model.DataArray(dcalibxy, metadata={model.MD_WL_LIST: wl_calibxy})
        out = calibration.get_spectrum_data([data1, calibxy])
        eout = calibxy[:, 0:1, 0:1, 0:1, (dcalibxy.shape[-1] - 1) // 2] # middle should contain average
        eout.shape += (1,)
        numpy.testing.assert_equal(out, eout)
        numpy.testing.assert_almost_equal(out.metadata[model.MD_WL_LIST],
                                          calibxy.metadata[model.MD_WL_LIST])
Пример #2
0
    def test_spec_calib(self):
        """Test StaticSpectrumStream calibration"""
        spec = self._create_spec_data()
        specs = stream.StaticSpectrumStream("test", spec)

        # Check efficiency compensation
        prev_im2d = specs.image.value

        dbckg = numpy.ones(spec.shape, dtype=numpy.uint16)
        wl_bckg = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9
        obckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_bckg})
        bckg = calibration.get_spectrum_data([obckg])

        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 1.3, 6, 9.1],
                             dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        specs.efficiencyCompensation.value = calib

        specs.background.value = bckg

        # Control spatial spectrum
        im2d = specs.image.value
        # Check it's a RGB DataArray
        self.assertEqual(im2d.shape, spec.shape[-2:] + (3, ))
        self.assertTrue(numpy.any(im2d != prev_im2d))
Пример #3
0
    def test_spec_calib(self):
        """Test StaticSpectrumStream calibration"""
        spec = self._create_spec_data()
        specs = stream.StaticSpectrumStream("test", spec)

        # Check efficiency compensation
        prev_im2d = specs.image.value

        dbckg = numpy.ones(spec.shape, dtype=numpy.uint16)
        wl_bckg = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9
        obckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_bckg})
        bckg = calibration.get_spectrum_data([obckg])

        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 1.3, 6, 9.1], dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        specs.efficiencyCompensation.value = calib

        specs.background.value = bckg

        # Control spatial spectrum
        im2d = specs.image.value
        # Check it's a RGB DataArray
        self.assertEqual(im2d.shape, spec.shape[-2:] + (3,))
        self.assertTrue(numpy.any(im2d != prev_im2d))
Пример #4
0
    def test_load_full(self):
        """
        Check the whole sequence: saving calibration data to file, loading it
        back from file, finding it.
        """
        # Background data
        dbckg = numpy.array([1, 2, 2, 3, 4, 5, 4, 6, 9], dtype=numpy.uint16)
        dbckg.shape += (1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9
        bckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_calib})

        # Give one DA, the correct one, so expect to get it back

        # Compensation data
        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        # More DataArrays, just to make it slightly harder to find the data
        data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16))
        data2 = model.DataArray(numpy.zeros((3, 1, 1, 520, 230), dtype=numpy.uint16))

        # RGB image
        thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8))

        full_coef = [data1, calib, data2]
        full_bckg = [data1, bckg, data2]

        for fmt in dataio.get_available_formats(os.O_WRONLY):
            exporter = dataio.get_converter(fmt)
            logging.info("Trying to export/import with %s", fmt)
            fn_coef = u"test_spec" + exporter.EXTENSIONS[0]
            exporter.export(fn_coef, full_coef, thumb)
            fn_bckg = u"test_bckg" + exporter.EXTENSIONS[0]
            exporter.export(fn_bckg, full_bckg, thumb)

            if fmt in dataio.get_available_formats(os.O_RDONLY):
                data_bckg = exporter.read_data(fn_bckg)
                ibckg = calibration.get_spectrum_data(data_bckg)
                data_coef = exporter.read_data(fn_coef)
                icoef = calibration.get_spectrum_efficiency(data_coef)
                numpy.testing.assert_equal(icoef, calib)
                numpy.testing.assert_almost_equal(icoef.metadata[model.MD_WL_LIST],
                                                  calib.metadata[model.MD_WL_LIST])
                numpy.testing.assert_equal(ibckg, bckg)
                numpy.testing.assert_almost_equal(ibckg.metadata[model.MD_WL_LIST],
                                                  bckg.metadata[model.MD_WL_LIST])
            try:
                os.remove(fn_coef)
            except OSError:
                logging.exception("Failed to delete the file %s", fn_coef)
            try:
                os.remove(fn_bckg)
            except OSError:
                logging.exception("Failed to delete the file %s", fn_bckg)
Пример #5
0
    def test_load_full(self):
        """
        Check the whole sequence: saving calibration data to file, loading it
        back from file, finding it.
        """
        # Background data
        dbckg = numpy.array([1, 2, 2, 3, 4, 5, 4, 6, 9], dtype=numpy.uint16)
        dbckg.shape += (1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9
        bckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_calib})

        # Give one DA, the correct one, so expect to get it back

        # Compensation data
        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        # More DataArrays, just to make it slightly harder to find the data
        data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16))
        data2 = model.DataArray(numpy.zeros((3, 1, 1, 520, 230), dtype=numpy.uint16))

        # RGB image
        thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8))

        full_coef = [data1, calib, data2]
        full_bckg = [data1, bckg, data2]

        for fmt in dataio.get_available_formats(os.O_WRONLY):
            exporter = dataio.get_converter(fmt)
            logging.info("Trying to export/import with %s", fmt)
            fn_coef = u"test_spec" + exporter.EXTENSIONS[0]
            exporter.export(fn_coef, full_coef, thumb)
            fn_bckg = u"test_bckg" + exporter.EXTENSIONS[0]
            exporter.export(fn_bckg, full_bckg, thumb)

            if fmt in dataio.get_available_formats(os.O_RDONLY):
                data_bckg = exporter.read_data(fn_bckg)
                ibckg = calibration.get_spectrum_data(data_bckg)
                data_coef = exporter.read_data(fn_coef)
                icoef = calibration.get_spectrum_efficiency(data_coef)
                numpy.testing.assert_equal(icoef, calib)
                numpy.testing.assert_almost_equal(icoef.metadata[model.MD_WL_LIST],
                                                  calib.metadata[model.MD_WL_LIST])
                numpy.testing.assert_equal(ibckg, bckg)
                numpy.testing.assert_almost_equal(ibckg.metadata[model.MD_WL_LIST],
                                                  bckg.metadata[model.MD_WL_LIST])
            try:
                os.remove(fn_coef)
            except OSError:
                logging.exception("Failed to delete the file %s", fn_coef)
            try:
                os.remove(fn_bckg)
            except OSError:
                logging.exception("Failed to delete the file %s", fn_bckg)
Пример #6
0
    def test_compensate(self):
        """Test applying efficiency compensation"""
        # Spectrum
        data = numpy.ones((251, 1, 1, 200, 300), dtype="uint16") + 1
        wld = 433e-9 + numpy.array(range(data.shape[0])) * 0.1e-9
        spec = model.DataArray(data, metadata={model.MD_WL_LIST: wld})

        # Background data
        dbckg = numpy.ones(data.shape, dtype=numpy.uint16)
        wl_bckg = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9
        obckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_bckg})
        bckg = calibration.get_spectrum_data([obckg])

        # Compensation data
        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1],
                             dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        compensated = calibration.compensate_spectrum_efficiency(
            spec, bckg, calib)

        self.assertEqual(spec.shape, compensated.shape)
        numpy.testing.assert_equal(spec.metadata[model.MD_WL_LIST],
                                   compensated.metadata[model.MD_WL_LIST])

        for i in range(dcalib.shape[0] - 1):
            ca, cb = calib[i], calib[i + 1]
            wla, wlb = wl_calib[i], wl_calib[i + 1]
            # All the values between the 2 wavelengths should be compensated
            # between the 2 factors

            for vo, vb, vc, wl in zip(spec[..., 3, 3], bckg[..., 0, 0],
                                      compensated[..., 3, 3], wld):
                if wla <= wl <= wlb:
                    expa, expb = (vo - vb) * ca, (vo - vb) * cb
                    minc, maxc = min(expa, expb), max(expa, expb)
                    self.assertTrue(minc <= vc <= maxc)
Пример #7
0
    def test_compensate(self):
        """Test applying efficiency compensation"""
        # Spectrum
        data = numpy.ones((251, 1, 1, 200, 300), dtype="uint16") + 1
        wld = 433e-9 + numpy.array(range(data.shape[0])) * 0.1e-9
        spec = model.DataArray(data, metadata={model.MD_WL_LIST: wld})

        # Background data
        dbckg = numpy.ones(data.shape, dtype=numpy.uint16)
        wl_bckg = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9
        obckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_bckg})
        bckg = calibration.get_spectrum_data([obckg])

        # Compensation data
        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        compensated = calibration.compensate_spectrum_efficiency(spec, bckg, calib)

        self.assertEqual(spec.shape, compensated.shape)
        numpy.testing.assert_equal(spec.metadata[model.MD_WL_LIST],
                                   compensated.metadata[model.MD_WL_LIST])

        for i in range(dcalib.shape[0] - 1):
            ca, cb = calib[i], calib[i + 1]
            wla, wlb = wl_calib[i], wl_calib[i + 1]
            # All the values between the 2 wavelengths should be compensated
            # between the 2 factors

            for vo, vb, vc, wl in zip(spec[..., 3, 3], bckg[..., 0, 0], compensated[..., 3, 3], wld):
                if wla <= wl <= wlb:
                    expa, expb = (vo - vb) * ca, (vo - vb) * cb
                    minc, maxc = min(expa, expb), max(expa, expb)
                    self.assertTrue(minc <= vc <= maxc)