Ejemplo n.º 1
0
 def test_out_parameter(self):
     part = self.image[20:22, 20:22]
     out = np.empty_like(part)
     cnt = spectral_continuum(part, self.bands, out=out)
     assert(cnt is out)
     # And just do a quick check if result is sane.
     assert(out[1, 1, 200] == 1491)
Ejemplo n.º 2
0
 def test_3d_array(self):
     part = self.image[20:22, 20:22]
     cnt = spectral_continuum(part, self.bands)
     # Check some values to make sure results are sane.
     assert(cnt[0, 0, 200] == 1422)
     assert(cnt[0, 1, 200] == 1421)
     assert(cnt[1, 0, 200] == 1469)
     assert(cnt[1, 1, 200] == 1491)
Ejemplo n.º 3
0
    def test_few_simple_cases(self):
        spectrum = np.array([1., 2., 2.5, 1.6, 0.75, 1.5, 2.2, 2.9, 1.8])
        bands = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9.])
        expected = np.array([1., 2., 2.5, 2.58, 2.66, 2.74, 2.82, 2.9, 1.8])
        assert_allclose(expected, spectral_continuum(spectrum, bands))

        spectrum = np.array([0.6, 1., 2.45, 3.1, 3.25, 4.15,
                             4.35, 4.1, 3.1, 2.7, 2., 2.85, 3.75, 3., 2., 0.9])
        bands = np.array([0.3, 1., 1.8, 3., 4.5, 5.2, 6.45,
                          7., 7.1, 8., 8.1, 9., 9.3, 10.2, 10.5, 10.6])
        expected = np.array([0.6, 1.46333333, 2.45, 3.1, 3.81590909, 4.15, 4.35, 4.23421053,
                             4.21315789, 4.02368421, 4.00263158, 3.81315789, 3.75, 3., 2., 0.9])
        assert_allclose(expected, spectral_continuum(spectrum, bands))

        spectrum = np.array([0.5, 1.1, 1.5, 2.4, 1.9, 1.0])
        bands = np.array([0.5, 1.0, 1.7, 2.0, 3.0, 3.5])
        expected = np.array([0.5, 1.13333333, 2.02, 2.4, 1.9, 1.])
        assert_allclose(expected, spectral_continuum(spectrum, bands))

        spectrum = np.array([0.5, 1.1, 1.8, 2.0, 1.1, 0.9, 0.4])
        bands = np.array([0.5, 0.9, 1.6, 2.0, 2.1, 2.8, 3.0])
        expected = np.array([0.5, 1.1, 1.8, 2., 1.8625, 0.9, 0.4])
        assert_allclose(expected, spectral_continuum(spectrum, bands))
Ejemplo n.º 4
0
    def test_simple_segmented(self):
        # A case without local maximum inside concave regions.
        # Same as convex hull.
        spectrum = np.array([0.6, 1., 2.45, 3.1, 3.25, 4.15,
                             4.35, 4.1, 3.1, 2.7, 2., 2.85, 3.75, 3., 2., 0.9])
        bands = np.array([0.3, 1., 1.8, 3., 4.5, 5.2, 6.45,
                          7., 7.1, 8., 8.1, 9., 9.3, 10.2, 10.5, 10.6])
        expected = np.array([0.6, 1.46333333, 2.45, 3.1, 3.81590909, 4.15, 4.35,
                             4.23421053, 4.21315789, 4.02368421, 4.00263158, 3.81315789, 3.75, 3.,
                             2., 0.9, ])
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # A case with single local maxima that gets filtered out, because it
        # does not satisfy quasi-convexity.
        spectrum = np.array([0.6, 1., 2.45, 3.1, 3.25, 4.15,
                             4.35, 4.1, 3.1, 3.7, 2., 2.85, 3.75, 3., 2., 0.9])
        bands = np.array([0.3, 1., 1.8, 3., 4.5, 5.2, 6.45,
                          7., 7.1, 8., 8.1, 9., 9.3, 10.2, 10.5, 10.6])
        expected = np.array([0.6, 1.46333333, 2.45, 3.1, 3.81590909,
                             4.15, 4.35, 4.23421053, 4.21315789, 4.02368421,
                             4.00263158, 3.81315789, 3.75, 3., 2.,
                             0.9])
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # Reversed case.
        spectrum = spectrum[::-1]
        bands = np.cumsum(np.concatenate(
            (np.array([0.3]), (bands[1:] - bands[:-1])[::-1])))
        expected = expected[::-1]
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # A case with single valid local maxima inside concave region,
        # and one invalid.
        spectrum = np.array([0.60, 1.00, 2.45, 3.10, 3.25, 4.15, 4.35, 4.10,
                             3.10, 3.80, 3.50, 3.60, 2.00, 2.85, 3.75, 3.00, 2.00, 0.90])
        bands = np.array([0.30, 1.00, 1.80, 3.00, 4.50, 5.20, 6.45, 7.00,
                          7.10, 8.00, 8.03, 8.07, 8.10, 9.00, 9.30, 10.20, 10.50, 10.6])
        expected = np.array([0.6, 1.46333333, 2.45, 3.1, 3.81590909,
                             4.15, 4.35, 4.15483871, 4.11935484, 3.8,
                             3.79884615, 3.79730769, 3.79615385, 3.76153846, 3.75,
                             3., 2., 0.9])
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # Reversed case.
        spectrum = spectrum[::-1]
        bands = np.cumsum(np.concatenate(
            (np.array([0.3]), (bands[1:] - bands[:-1])[::-1])))
        expected = expected[::-1]
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # A case with two valid local maxima.
        spectrum = np.array([0.60, 1.00, 2.45, 3.10, 3.25, 4.15, 4.35, 4.10,
                             3.10, 3.80, 3.50, 3.75, 2.00, 2.85, 3.70, 3.00, 2.00, 0.90])
        bands = np.array([0.30, 1.00, 1.80, 3.00, 4.50, 5.20, 6.45, 7.00,
                          7.10, 8.00, 8.03, 8.07, 8.10, 9.00, 9.30, 10.20, 10.50, 10.6])
        expected = np.array([0.6, 1.46333333, 2.45, 3.1, 3.81590909,
                             4.15, 4.35, 4.15483871, 4.11935484, 3.8,
                             3.77857143, 3.75, 3.74878049, 3.71219512, 3.7,
                             3., 2., 0.9])
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # Reversed case.
        spectrum = spectrum[::-1]
        bands = np.cumsum(np.concatenate(
            (np.array([0.3]), (bands[1:] - bands[:-1])[::-1])))
        expected = expected[::-1]
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # A case with two valid local maxima, but one covering eliminating the
        # other.
        spectrum = np.array([0.60, 1.00, 2.45, 3.10, 3.25, 4.15, 4.35, 4.10,
                             3.10, 3.80, 3.50, 3.85, 2.00, 2.85, 3.70, 3.00, 2.00, 0.90])
        bands = np.array([0.30, 1.00, 1.80, 3.00, 4.50, 5.20, 6.45, 7.00,
                          7.10, 8.00, 8.03, 8.07, 8.10, 9.00, 9.30, 10.20, 10.50, 10.6])
        expected = np.array([0.6, 1.46333333, 2.45, 3.1, 3.81590909,
                             4.15, 4.35, 4.18024691, 4.14938272, 3.87160494,
                             3.86234568, 3.85, 3.84634146, 3.73658537, 3.7,
                             3., 2., 0.9])
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))

        # Reversed case.
        spectrum = spectrum[::-1]
        bands = np.cumsum(np.concatenate(
            (np.array([0.3]), (bands[1:] - bands[:-1])[::-1])))
        expected = expected[::-1]
        assert_allclose(expected, spectral_continuum(
            spectrum, bands, 'segmented'))