Exemplo n.º 1
0
    def test_append(self):
        # Simple append operation.
        p0 = swprocess.Peaks([1, 2, 3], [4, 5, 6], "p0")
        p1 = swprocess.Peaks([[7, np.nan, 9], [4, 5, 6]],
                             [[1, np.nan, 2], [7, 8, 9]], "p1")
        suite = swprocess.PeaksSuite(p0)
        suite.append(p1)

        for returned, expected in zip(suite, [p0, p1]):
            self.assertEqual(expected, returned)

        # Bad: replicated identifier
        self.assertRaises(KeyError, suite.append, p1)

        # Bad: append non-Peaks object
        self.assertRaises(TypeError, suite.append, "not a Peaks object")
Exemplo n.º 2
0
    def test_reject_box_inside(self):
        # Create PeaksSuite
        peak0 = swprocess.Peaks([1, 3, 5, np.nan, 7], [1, 3, 5, np.nan, 7],
                                identifier="0")
        peak1 = swprocess.Peaks([1, 3, 5, np.nan, 7], [1, 3, 5, np.nan, 7],
                                identifier="1")
        suite = swprocess.PeaksSuite(peak0)
        suite.append(peak1)

        # Perform rejections.
        suite.reject_box_inside("frequency", (4, 8), "velocity", (4, 8))
        expected = swprocess.Peaks([1, 3], [1, 3])

        # Check result.
        for returned, _id in zip(suite, suite.ids):
            expected.identifier = _id
            self.assertEqual(expected, returned)
Exemplo n.º 3
0
    def test_reject_limits_outside(self):
        # Create PeaksSuite.
        peak0 = swprocess.Peaks([1, 2, np.nan, 3], [4, 5, np.nan, 6],
                                identifier="0")
        peak1 = swprocess.Peaks([1, 2, np.nan, 3], [4, 5, np.nan, 6],
                                identifier="1")
        suite = swprocess.PeaksSuite(peak0)
        suite.append(peak1)

        # Perform rejections.
        suite.reject_limits_outside("frequency", (0.5, 2.5))
        expected = swprocess.Peaks([1, 2], [4, 5])

        # Check result.
        for returned, _id in zip(suite, suite.ids):
            expected.identifier = _id
            self.assertEqual(expected, returned)
Exemplo n.º 4
0
 def test_plot_statistics(self):
     # Mock ax
     ax = MagicMock(spec=plt.Axes)
     suite = swprocess.PeaksSuite(swprocess.Peaks([1, 2, 3], [0, 1, 2]))
     suite.plot_statistics(ax, [1, 2, 3], [0, 1, 2], [4, 5, 6])
     ax.errorbar.assert_called_once()
Exemplo n.º 5
0
    def test_init(self):
        p0 = swprocess.Peaks([1, np.nan, 3], [4, np.nan, 6], "p0")
        suite = swprocess.PeaksSuite(p0)

        self.assertEqual(suite[0], p0)
        self.assertEqual(suite.ids[0], p0.identifier)
Exemplo n.º 6
0
    def test_interactive_trimming(self):
        # Create simple suite, composed of two Peaks.
        peaks_a = swprocess.Peaks(frequency=[0.5, 0.5],
                                  velocity=[0.5, 1.5],
                                  identifier="a")
        peaks_b = swprocess.Peaks(frequency=[1.5, 1.5],
                                  velocity=[0.5, 1.5],
                                  identifier="b")
        suite = swprocess.PeaksSuite(peaks_a)
        suite.append(peaks_b)

        # Create a response generator.
        def response_generator(responses):
            index = 0
            while index < len(responses):
                yield responses[index]
                index += 1

        # Use a closure to wrap generator.
        def wrap_generator(generator):
            def wrapper(*args, **kwargs):
                return next(generator)

            return wrapper

        # Define generator to replace _draw_box()
        xlims, ylims, axclicked = (1., 2.), (0., 1.), 0
        response_0 = (xlims, ylims, axclicked)
        xlims, ylims, axclicked = (1., 1.), (1., 1.), 0
        response_1 = (xlims, ylims, axclicked)
        pick_generator = response_generator([response_0, response_1])
        _draw_box_responses = wrap_generator(generator=pick_generator)

        with patch("swprocess.peakssuite.PeaksSuite._draw_box",
                   side_effect=_draw_box_responses):
            with patch('builtins.input', return_value="0"):
                suite.interactive_trimming(xtype="frequency", ytype="velocity")
        self.assertArrayEqual(np.array([0.5, 0.5]), peaks_a.frequency)
        self.assertArrayEqual(np.array([0.5, 1.5]), peaks_a.velocity)
        self.assertArrayEqual(np.array([1.5]), peaks_b.frequency)
        self.assertArrayEqual(np.array([1.5]), peaks_b.velocity)

        # Redefine generator for _draw_box()
        pick_generator = response_generator([response_0, response_1])
        _draw_box_responses = wrap_generator(generator=pick_generator)

        # Define generator to replace input()
        input_generator = response_generator(["5", "0"])
        _input_responses = wrap_generator(generator=input_generator)

        # Check with bad user entry at input().
        with patch("swprocess.peakssuite.PeaksSuite._draw_box",
                   side_effect=_draw_box_responses):
            with patch("builtins.input", side_effect=_input_responses):
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    suite.interactive_trimming(xtype="frequency",
                                               ytype="velocity")

        # Redefine generator for _draw_box()
        pick_generator = response_generator([response_1])
        _draw_box_responses = wrap_generator(generator=pick_generator)
        mock = MagicMock()

        # Check with bad user entry at input().
        with patch("swprocess.peakssuite.PeaksSuite._draw_box",
                   side_effect=_draw_box_responses):
            with patch("builtins.input", return_value="0"):
                with patch(
                        "swprocess.peakssuite.PeaksSuite.plot_resolution_limits",
                        side_effect=mock):
                    suite.interactive_trimming(
                        xtype="frequency",
                        ytype="velocity",
                        resolution_limits=["wavelength", (1, 10)])
                    mock.assert_called()