예제 #1
0
 def test_example(self, example_name):
     actual_perf = examples.run_example(
         EXAMPLE_MODULES,
         example_name,
         # This should match the invocation in
         # zipline/tests/resources/rebuild_example_data
         environ={
             'ZIPLINE_ROOT': self.tmpdir.getpath('example_data/root'),
         },
         benchmark_returns=self.benchmark_returns,
     )
     expected_perf = self.expected_perf[example_name]
     # Exclude positions column as the positions do not always have the
     # same order
     columns = [
         column for column in examples._cols_to_check
         if column != 'positions'
     ]
     assert_equal(
         actual_perf[columns],
         expected_perf[columns],
         # There is a difference in the datetime columns in pandas
         # 0.16 and 0.17 because in 16 they are object and in 17 they are
         # datetime[ns, UTC]. We will just ignore the dtypes for now.
         check_dtype=False,
     )
     # Sort positions by SID before comparing
     assert_equal(
         expected_perf['positions'].apply(sorted, key=itemgetter('sid')),
         actual_perf['positions'].apply(sorted, key=itemgetter('sid')),
     )
예제 #2
0
 def test_example(self, example_name):
     actual_perf = examples.run_example(
         example_name,
         # This should match the invocation in
         # zipline/tests/resources/rebuild_example_data
         environ={"ZIPLINE_ROOT": self.tmpdir.getpath("example_data/root")},
     )
     assert_equal(
         actual_perf[examples._cols_to_check],
         self.expected_perf[example_name][examples._cols_to_check],
         # There is a difference in the datetime columns in pandas
         # 0.16 and 0.17 because in 16 they are object and in 17 they are
         # datetime[ns, UTC]. We will just ignore the dtypes for now.
         check_dtype=False,
     )
예제 #3
0
 def test_example(self, example_name):
     actual_perf = examples.run_example(
         example_name,
         # This should match the invocation in
         # zipline/tests/resources/rebuild_example_data
         environ={
             'ZIPLINE_ROOT': self.tmpdir.getpath('example_data/root'),
         },
     )
     assert_equal(
         actual_perf[examples._cols_to_check],
         self.expected_perf[example_name][examples._cols_to_check],
         # There is a difference in the datetime columns in pandas
         # 0.16 and 0.17 because in 16 they are object and in 17 they are
         # datetime[ns, UTC]. We will just ignore the dtypes for now.
         check_dtype=False,
     )
def main(ctx, rebuild_input):
    """Rebuild the perf data for test_examples"""
    example_path = test_resource_path("example_data.tar.gz")

    with tmp_dir() as d:
        with tarfile.open(example_path) as tar:
            tar.extractall(d.path)

        # The environ here should be the same (modulo the tempdir location)
        # as we use in test_examples.py.
        environ = {"ZIPLINE_ROOT": d.getpath("example_data/root")}

        if rebuild_input:
            raise NotImplementedError(
                "We cannot rebuild input for Yahoo because of "
                "changes Yahoo made to their API, so we cannot "
                "use Yahoo data bundles anymore. This will be fixed in "
                "a future release",
            )

        # we need to register the bundle; it is already ingested and saved in
        # the example_data.tar.gz file
        @register("test")
        def nop_ingest(*args, **kwargs):
            raise NotImplementedError("we cannot rebuild the test buindle")

        new_perf_path = d.getpath(
            "example_data/new_perf/%s" % pd.__version__.replace(".", "-"),
        )
        c = dataframe_cache(
            new_perf_path,
            serialization="pickle:2",
        )
        with c:
            for name in EXAMPLE_MODULES:
                c[name] = examples.run_example(
                    EXAMPLE_MODULES,
                    name,
                    environ=environ,
                    benchmark_returns=read_checked_in_benchmark_data(),
                )

            correct_called = [False]

            console = None

            def _exit(*args, **kwargs):
                console.raw_input = eof

            def correct():
                correct_called[0] = True
                _exit()

            expected_perf_path = d.getpath(
                "example_data/expected_perf/%s"
                % pd.__version__.replace(".", "-"),
            )

            # allow users to run some analysis to make sure that the new
            # results check out
            console = InteractiveConsole(
                {
                    "correct": correct,
                    "exit": _exit,
                    "incorrect": _exit,
                    "new": c,
                    "np": np,
                    "old": dataframe_cache(
                        expected_perf_path,
                        serialization="pickle",
                    ),
                    "pd": pd,
                    "cols_to_check": examples._cols_to_check,
                    "changed_results": changed_results,
                }
            )
            console.interact(banner)

            if not correct_called[0]:
                ctx.fail(
                    "`correct()` was not called! This means that the new"
                    " results will not be written",
                )

            # move the new results to the expected path
            shutil.rmtree(expected_perf_path)
            shutil.copytree(new_perf_path, expected_perf_path)

        # Clear out all the temporary new perf so it doesn't get added to the
        # tarball.
        shutil.rmtree(d.getpath("example_data/new_perf/"))

        with tarfile.open(example_path, "w|gz") as tar:
            tar.add(d.getpath("example_data"), "example_data")