{ "cells": [ { "cell_type": "markdown", "id": "9b9a1d2c-7664-4fd9-b5cb-3a766d907fe7", "metadata": {}, "source": [ "# Working with Jupyter notebooks\n", "\n", "{func}`~recursive_diff.display_diffs` can be used to compare two NumPy, Pandas, or Xarray objects in a Jupyter notebook:" ] }, { "cell_type": "code", "execution_count": null, "id": "737df1de-16b9-40d2-a704-29022846bbc0", "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "sys.path.insert(0, \"..\")\n", "\n", "import xarray\n", "\n", "from recursive_diff import display_diffs\n", "\n", "a = xarray.Dataset(\n", " {\n", " \"v1\": ((\"r\", \"c\"), [[1, 2], [3, 4]]),\n", " \"v2\": (\"r\", [\"foo\", \"bar\"]),\n", " \"r\": [\"r1\", \"r2\"],\n", " \"extra\": [5],\n", " },\n", " attrs={\"some_tag\": \"Hello\"},\n", ")\n", "\n", "b = xarray.Dataset(\n", " {\n", " \"v1\": ((\"r\", \"c\"), [[1, 5], [3.1, 4]]),\n", " \"v2\": (\"r\", [\"bar\", \"bar\"]),\n", " \"r\": [\"r1\", \"r2\"],\n", " },\n", " attrs={\"some_tag\": \"World\"},\n", ")\n", "\n", "\n", "display_diffs(a, b)" ] }, { "cell_type": "markdown", "id": "cc84e5ad-4b39-4602-9da7-7aa63bbe6cb9", "metadata": {}, "source": [ "Just like {func}`recursive_diff.recursive_diff`, you may use it to visualize differences in nested structures too:\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7565dc16-7dc3-4ade-b8dd-0abfef712677", "metadata": {}, "outputs": [], "source": [ "c = {\"foo\": [1, 2, [3, 4]]}\n", "d = {\"foo\": [1.0000000001, 5, [3]], \"bar\": 6}\n", "\n", "display_diffs(c, d)" ] }, { "cell_type": "markdown", "id": "b85aa876-ad19-4393-bcf3-4c0dd866cbec", "metadata": {}, "source": [ "## Comparing directories\n", "\n", "If you have two directories full of data, you can compare them in one go with {func}`~recursive_diff.recursive_open`:" ] }, { "cell_type": "code", "execution_count": null, "id": "a6324359-3d59-4250-b70d-f9cd0d0bbde0", "metadata": {}, "outputs": [], "source": [ "import json\n", "import tempfile\n", "\n", "lhs = tempfile.TemporaryDirectory()\n", "rhs = tempfile.TemporaryDirectory()\n", "\n", "a.to_zarr(f\"{lhs.name}/array.zarr\", mode=\"w\", zarr_format=2)\n", "b.to_zarr(f\"{rhs.name}/array.zarr\", mode=\"w\", zarr_format=2)\n", "with open(f\"{lhs.name}/nested.json\", \"w\") as fh:\n", " json.dump(c, fh)\n", "with open(f\"{rhs.name}/nested.json\", \"w\") as fh:\n", " json.dump(d, fh)" ] }, { "cell_type": "code", "execution_count": null, "id": "1dc6c4ba-ba4a-4baf-8f48-933a0fa83717", "metadata": {}, "outputs": [], "source": [ "from recursive_diff import recursive_open\n", "\n", "display_diffs(recursive_open(lhs.name), recursive_open(rhs.name))" ] }, { "cell_type": "code", "execution_count": null, "id": "42ac0b27-178c-4c59-89c8-dd7c37464656", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 5 }