Repository: scipy/scipy
File: scipy/signal/tests/test_spectral.py (Python)

Code before:
        t = [t + 1 for t in range(0, 32)]
        y = np.ones(len(t))
        freqs = [2.0*np.pi] * 2  # must have 2+ elements
        lombscargle(t, y, freqs)

        # second, test with example that will cause first CC sum to be 0.0
        t = [t*4 + 1 for t in range(0, 32)]
        y = np.ones(len(t))
        freqs = [np.pi/2.0] * 2  # must have 2+ elements

        lombscargle(t, y, freqs)
    
    def test_input_mutation(self):
        # this tests for mutation of the input arrays
        # https://github.com/scipy/scipy/issues/23474

        # Input parameters
        ampl = 2.
        w = 1.
        phi = 0.5 * np.pi
        nin = 100
        nout = 1000
        p = 0.7  # Fraction of points to select

        # Randomly select a fraction of an array with timesteps
        rng = np.random.RandomState(2353425)
        r = rng.rand(nin)
        t = np.linspace(0.01*np.pi, 10.*np.pi, nin)[r >= p]

        # Plot a sine wave for the selected times
        y = ampl * np.sin(w*t + phi)

        # Define the array of frequencies for which to compute the periodogram
        f = np.linspace(0.01, 10., nout)

        weights = np.ones_like(y)

        # create original copies before passing
        t_org = t.copy()
        y_org = y.copy()
        f_org = f.copy()
        weights_org = weights.copy()

        pgram = lombscargle(t, y, f, precenter=True, weights=weights)

        # check all 4 array inputs
        assert_array_equal(t_org, t)
        assert_array_equal(y_org, y)
        assert_array_equal(f_org, f)
        assert_array_equal(weights_org, weights)


Review comment (suggestion):
we usually try to use `default_rng` in new tests for a variety of reasons

Code after:
        t = [t + 1 for t in range(0, 32)]
        y = np.ones(len(t))
        freqs = [2.0*np.pi] * 2  # must have 2+ elements
        lombscargle(t, y, freqs)

        # second, test with example that will cause first CC sum to be 0.0
        t = [t*4 + 1 for t in range(0, 32)]
        y = np.ones(len(t))
        freqs = [np.pi/2.0] * 2  # must have 2+ elements

        lombscargle(t, y, freqs)

    def test_input_mutation(self):
        # this tests for mutation of the input arrays
        # https://github.com/scipy/scipy/issues/23474

        # Input parameters
        ampl = 2.
        w = 1.
        phi = 0.5 * np.pi
        nin = 100
        nout = 1000
        p = 0.7  # Fraction of points to select

        # Randomly select a fraction of an array with timesteps
        rng = np.random.default_rng()
        r = rng.random(nin)
        t = np.linspace(0.01*np.pi, 10.*np.pi, nin)[r >= p]

        # Plot a sine wave for the selected times
        y = ampl * np.sin(w*t + phi)

        # Define the array of frequencies for which to compute the periodogram
        f = np.linspace(0.01, 10., nout)

        weights = np.ones_like(y)

        # create original copies before passing
        t_org = t.copy()
        y_org = y.copy()
        f_org = f.copy()
        weights_org = weights.copy()

        lombscargle(t, y, f, precenter=True, weights=weights)

        # check all 4 array inputs
        assert_array_equal(t, t_org)
        assert_array_equal(y, y_org)
        assert_array_equal(f, f_org)
        assert_array_equal(weights, weights_org)