{"id":19176004,"url":"https://github.com/astropy/pytest-arraydiff","last_synced_at":"2025-04-05T10:08:45.167Z","repository":{"id":9595442,"uuid":"62708151","full_name":"astropy/pytest-arraydiff","owner":"astropy","description":"pytest plugin to facilitate comparison of results to a pre-defined reference","archived":false,"fork":false,"pushed_at":"2024-10-14T18:26:40.000Z","size":112,"stargazers_count":21,"open_issues_count":5,"forks_count":18,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-10-29T16:02:41.584Z","etag":null,"topics":["arrays","pytest","pytest-plugin"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/astropy.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"numfocus","custom":"https://numfocus.org/donate-to-astropy"}},"created_at":"2016-07-06T09:19:12.000Z","updated_at":"2024-10-14T18:26:45.000Z","dependencies_parsed_at":"2024-04-12T10:20:37.576Z","dependency_job_id":"05c03183-f2f1-446a-8f49-cac00825dc53","html_url":"https://github.com/astropy/pytest-arraydiff","commit_stats":{"total_commits":115,"total_committers":13,"mean_commits":8.846153846153847,"dds":0.5304347826086957,"last_synced_commit":"84e5f2058702b2d65de287c5322e4d2492f65aea"},"previous_names":["astrofrog/pytest-arraydiff"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astropy%2Fpytest-arraydiff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astropy%2Fpytest-arraydiff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astropy%2Fpytest-arraydiff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astropy%2Fpytest-arraydiff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astropy","download_url":"https://codeload.github.com/astropy/pytest-arraydiff/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318744,"owners_count":20919484,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arrays","pytest","pytest-plugin"],"created_at":"2024-11-09T10:26:48.986Z","updated_at":"2025-04-05T10:08:45.131Z","avatar_url":"https://github.com/astropy.png","language":"Python","funding_links":["https://github.com/sponsors/numfocus","https://numfocus.org/donate-to-astropy"],"categories":[],"sub_categories":[],"readme":".. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.5811772.svg\n   :target: https://doi.org/10.5281/zenodo.5811772\n   :alt: 10.5281/zenodo.5811772\n\n.. image:: https://github.com/astropy/pytest-arraydiff/workflows/CI/badge.svg\n    :target: https://github.com/astropy/pytest-arraydiff/actions\n    :alt: CI Status\n\n.. image:: https://img.shields.io/pypi/v/pytest-arraydiff.svg\n    :target: https://pypi.org/project/pytest-arraydiff\n    :alt: PyPI Status\n\nAbout\n-----\n\nThis is a `py.test \u003chttp://pytest.org\u003e`__ plugin to facilitate the\ngeneration and comparison of data arrays produced during tests, in particular\nin cases where the arrays are too large to conveniently hard-code them\nin the tests (e.g. ``np.testing.assert_allclose(x, [1, 2, 3])``).\n\nThe basic idea is that you can write a test that generates a Numpy array (or\nother related objects depending on the format, e.g. pandas DataFrame).\nYou can then either run the\ntests in a mode to **generate** reference files from the arrays, or you can run\nthe tests in **comparison** mode, which will compare the results of the tests to\nthe reference ones within some tolerance.\n\nAt the moment, the supported file formats for the reference files are:\n\n-  A plain text-based format (based on Numpy ``loadtxt`` output)\n-  The FITS format (requires `astropy \u003chttp://www.astropy.org\u003e`__). With this\n   format, tests can return either a Numpy array for a FITS HDU object.\n-  A pandas HDF5 format using the pandas HDFStore\n\nFor more information on how to write tests to do this, see the **Using**\nsection below.\n\nInstalling\n----------\n\nThis plugin is compatible with Python 2.7, and 3.5 and later, and\nrequires `pytest \u003chttp://pytest.org\u003e`__ and\n`numpy \u003chttp://www.numpy.org\u003e`__ to be installed.\n\nTo install, you can do::\n\n    pip install pytest-arraydiff\n\nYou can check that the plugin is registered with pytest by doing::\n\n    py.test --version\n\nwhich will show a list of plugins::\n\n    This is pytest version 2.7.1, imported from ...\n    setuptools registered plugins:\n      pytest-arraydiff-0.1 at ...\n\nUsing\n-----\n\nTo use, you simply need to mark the function where you want to compare\narrays using ``@pytest.mark.array_compare``, and make sure that the\nfunction returns a plain Numpy array::\n\n    python\n    import pytest\n    import numpy as np\n\n    @pytest.mark.array_compare\n    def test_succeeds():\n        return np.arange(3 * 5 * 4).reshape((3, 5, 4))\n\nTo generate the reference data files, run the tests with the\n``--arraydiff-generate-path`` option with the name of the directory\nwhere the generated files should be placed::\n\n    py.test --arraydiff-generate-path=reference\n\nIf the directory does not exist, it will be created. The directory will\nbe interpreted as being relative to where you are running ``py.test``.\nMake sure you manually check the reference arrays to ensure they are\ncorrect.\n\nOnce you are happy with the generated data files, you should move them\nto a sub-directory called ``reference`` relative to the test files (this\nname is configurable, see below). You can also generate the baseline\narrays directly in the right directory.\n\nYou can then run the tests simply with::\n\n    py.test --arraydiff\n\nand the tests will pass if the arrays are the same. If you omit the\n``--arraydiff`` option, the tests will run but will only check that the\ncode runs without checking the output arrays.\n\nOptions\n-------\n\nThe ``@pytest.mark.array_compare`` marker take an argument to specify\nthe format to use for the reference files:\n\n.. code:: python\n\n    @pytest.mark.array_compare(file_format='text')\n    def test_array():\n        ...\n\nThe default file format can also be specified using the\n``--arraydiff-default-format=\u003cformat\u003e`` flag when running ``py.test``,\nand ``\u003cformat\u003e`` should be either ``fits`` or ``text``.\n\nThe supported formats at this time are ``text`` and ``fits``, and\ncontributions for other formats are welcome. The default format is\n``text``.\n\nAdditional arguments are the relative and absolute tolerances for floating\npoint values (which default to 1e-7 and 0, respectively):\n\n.. code:: python\n\n    @pytest.mark.array_compare(rtol=20, atol=0.1)\n    def test_array():\n        ...\n\nYou can also pass keyword arguments to the writers using the\n``write_kwargs``. For the ``text`` format, these arguments are passed to\n``savetxt`` while for the ``fits`` format they are passed to Astropy's\n``fits.writeto`` function.\n\n.. code:: python\n\n    @pytest.mark.array_compare(file_format='fits', write_kwargs={'output_verify': 'silentfix'})\n    def test_array():\n        ...\n\nOther options include the name of the reference directory (which\ndefaults to ``reference`` ) and the filename for the reference file\n(which defaults to the name of the test with a format-dependent\nextension).\n\n.. code:: python\n\n    @pytest.mark.array_compare(reference_dir='baseline_arrays',\n                                   filename='other_name.fits')\n    def test_array():\n        ...\n\nThe reference directory in the decorator above will be interpreted as\nbeing relative to the test file. Note that the baseline directory can\nalso be a URL (which should start with ``http://`` or ``https://`` and\nend in a slash).\n\nFinally, you can also set a custom baseline directory globally when\nrunning tests by running ``py.test`` with::\n\n    py.test --arraydiff --arraydiff-reference-path=baseline_arrays\n\nThis directory will be interpreted as being relative to where the tests\nare run. In addition, if both this option and the ``reference_dir``\noption in the ``array_compare`` decorator are used, the one in the\ndecorator takes precedence.\n\nTest failure example\n--------------------\n\nIf the arrays produced by the tests are correct, then the test will\npass, but if they are not, the test will fail with a message similar to\nthe following::\n\n    E               AssertionError:\n    E\n    E               a: /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmpbvjkzt_q/test_to_mask_rect-mode_subpixels-subpixels_18.txt\n    E               b: /var/folders/zy/t1l3sx310d3d6p0kyxqzlrnr0000gr/T/tmpbvjkzt_q/reference-test_to_mask_rect-mode_subpixels-subpixels_18.txt\n    E\n    E               Not equal to tolerance rtol=1e-07, atol=0\n    E\n    E               (mismatch 47.22222222222222%)\n    E                x: array([[ 0.      ,  0.      ,  0.      ,  0.      ,  0.404012,  0.55    ,\n    E                        0.023765,  0.      ,  0.      ],\n    E                      [ 0.      ,  0.      ,  0.      ,  0.112037,  1.028704,  1.1     ,...\n    E                y: array([[ 0.      ,  0.      ,  0.      ,  0.      ,  0.367284,  0.5     ,\n    E                        0.021605,  0.      ,  0.      ],\n    E                      [ 0.      ,  0.      ,  0.      ,  0.101852,  0.935185,  1.      ,...\n\nThe file paths included in the exception are then available for\ninspection.\n\nRunning the tests for pytest-arraydiff\n--------------------------------------\n\nIf you are contributing some changes and want to run the tests, first\ninstall the latest version of the plugin then do::\n\n    cd tests\n    py.test --arraydiff\n\nThe reason for having to install the plugin first is to ensure that the\nplugin is correctly loaded as part of the test suite.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastropy%2Fpytest-arraydiff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastropy%2Fpytest-arraydiff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastropy%2Fpytest-arraydiff/lists"}