Repository: quantopian/zipline
File: tests/pipeline/test_factor.py (Python)

Code before:
        returns = Returns(window_length=window_length)

        today = datetime64(1, 'ns')
        assets = arange(3)

        seed(seed_value)  # Seed so we get deterministic results.
        test_data = abs(randn(window_length, 3))

        # Calculate the expected returns
        expected = (test_data[-1] - test_data[0]) / test_data[0]

        out = empty((3,), dtype=float)
        returns.compute(today, assets, out, test_data)

        check_allclose(expected, out)

    @parameterized.expand([
        (100, 15),
        (101, 4),
        (102, 100),
        ])
    def test_percentchange(self, seed_value, window_length):

        pct_change = PercentChange(
            inputs=(), 
            window_length=window_length
        )

        today = datetime64(1, 'ns')
        assets = arange(7)

        seed(seed_value)  # Seed so we get deterministic results.
        test_data = randn(window_length, 7)
        test_data[0] = array([1, 2, 1, -1, -1, 0, nan])
        test_data[-1] = array([2, 1, -2, 2, -2, 1, 1])

        # Calculate the expected percent change
        expected = array([1, -0.5, -3, 3, -1, inf, nan])

        out = empty((7,), dtype=float)
        pct_change.compute(today, assets, out, test_data)

        check_allclose(expected, out)

    def gen_ranking_cases():
        seeds = range(int(1e4), int(1e5), int(1e4))
        methods = ('ordinal', 'average')
        use_mask_values = (True, False)
        set_missing_values = (True, False)
        ascending_values = (True, False)
        return product(

Review comment (suggestion):
Another small style note, I like to leave trailing commas at the end of multiline calls of functions, import lines, or lists so that if a new parameter ever got added the git diff would only be +1 line instead of -1/+2 lines. I also tend to think it looks nicer 🙂 

Code after:
        returns = Returns(window_length=window_length)

        today = datetime64(1, 'ns')
        assets = arange(3)

        seed(seed_value)  # Seed so we get deterministic results.
        test_data = abs(randn(window_length, 3))

        # Calculate the expected returns
        expected = (test_data[-1] - test_data[0]) / test_data[0]

        out = empty((3,), dtype=float)
        returns.compute(today, assets, out, test_data)

        check_allclose(expected, out)

    @parameterized.expand([
        (100, 15),
        (101, 4),
        (102, 100),
        ])
    def test_percentchange(self, seed_value, window_length):

        pct_change = PercentChange(
            inputs=[EquityPricing.close, ],
            window_length=window_length,
        )

        today = datetime64(1, 'ns')
        assets = arange(8)

        seed(seed_value)  # Seed so we get deterministic results.
        middle_rows = randn(window_length - 2, 8)
        first_row = array([1, 2, 2, 1, -1, -1, 0, nan])
        end_row = array([2, 1, 2, -2, 2, -2, 1, 1])
        test_data = np.vstack([first_row, middle_rows, end_row])

        # Calculate the expected percent change
        expected = array([1, -0.5, 0, -3, 3, -1, inf, nan])

        out = empty((8,), dtype=float)
        pct_change.compute(today, assets, out, test_data)

        check_allclose(expected, out)

        with self.assertRaises(ValueError):
            PercentChange(inputs=(), window_length=2)

        with self.assertRaises(ValueError):
            PercentChange(inputs=[EquityPricing.close], window_length=1)