{ "cells": [ { "cell_type": "markdown", "id": "66d74b51", "metadata": {}, "source": [ "# Bisect Plot Examples\n", "\n", "## Introduction\n", "This notebook explains the use case of the `prepare_datasets`, and `plot_bisect` functions from the Plaid library. The function is used to generate bisect plots for different scenarios using file paths and PLAID objects.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8cc83db7", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "# Importing Required Libraries\n", "from pathlib import Path\n", "import os\n", "\n", "from plaid import Dataset\n", "from plaid.post.bisect import plot_bisect, prepare_datasets\n", "from plaid import ProblemDefinition" ] }, { "cell_type": "code", "execution_count": null, "id": "a5463897", "metadata": {}, "outputs": [], "source": [ "# Setting up Directories\n", "try:\n", " dataset_directory = Path(__file__).parent.parent.parent / \"tests\" / \"post\"\n", "except NameError:\n", " dataset_directory = Path(\"..\") / \"..\" / \"..\" / \"..\" / \"tests\" / \"post\"" ] }, { "cell_type": "markdown", "id": "e2d0b6ba", "metadata": {}, "source": [ "## Prepare Datasets for comparision\n", "\n", "Assuming you have reference and predicted datasets, and a problem definition, The `prepare_datasets` function is used to obtain output scalars for subsequent analysis.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "62cd2d75", "metadata": {}, "outputs": [], "source": [ "# Load PLAID datasets and problem metadata objects\n", "ref_ds = Dataset(dataset_directory / \"dataset_ref\")\n", "pred_ds = Dataset(dataset_directory / \"dataset_near_pred\")\n", "problem = ProblemDefinition(dataset_directory / \"problem_definition\")\n", "\n", "# Get output scalars from reference and prediction dataset\n", "ref_out_scalars, pred_out_scalars, out_scalars_names = prepare_datasets(\n", " ref_ds, pred_ds, problem, verbose=True\n", ")\n", "\n", "print(f\"{out_scalars_names = }\\n\")" ] }, { "cell_type": "code", "execution_count": null, "id": "a97cadde", "metadata": {}, "outputs": [], "source": [ "# Get output scalar\n", "key = out_scalars_names[0]\n", "\n", "print(f\"KEY '{key}':\\n\")\n", "print(f\"ID{' ' * 5}--REF_out_scalars--{' ' * 7}--PRED_out_scalars--\")\n", "\n", "# Print output scalar values for both datasets\n", "index = 0\n", "for item1, item2 in zip(ref_out_scalars[key], pred_out_scalars[key]):\n", " print(\n", " f\"{str(index).ljust(2)} | {str(item1).ljust(20)} | {str(item2).ljust(20)}\"\n", " )\n", " index += 1" ] }, { "cell_type": "markdown", "id": "b5f8f097", "metadata": {}, "source": [ "## Plotting with File Paths\n", "\n", "Here, we load the datasets and problem metadata from file paths and use the `plot_bisect` function to generate a bisect plot for a specific scalar, in this case, \"scalar_2.\"" ] }, { "cell_type": "code", "execution_count": null, "id": "d7efe426", "metadata": {}, "outputs": [], "source": [ "print(\"=== Plot with file paths ===\")\n", "\n", "# Load PLAID datasets and problem metadata from files\n", "ref_path = dataset_directory / \"dataset_ref\"\n", "pred_path = dataset_directory / \"dataset_pred\"\n", "problem_path = dataset_directory / \"problem_definition\"\n", "\n", "# Using file paths to generate bisect plot on feature_2\n", "plot_bisect(ref_path, pred_path, problem_path, \"feature_2\", \"differ_bisect_plot\")" ] }, { "cell_type": "markdown", "id": "98ded9db", "metadata": {}, "source": [ "## Plotting with PLAID\n", "\n", "In this section, we demonstrate how to use PLAID objects directly to generate a bisect plot. This can be advantageous when working with PLAID datasets in memory." ] }, { "cell_type": "code", "execution_count": null, "id": "a02cb933", "metadata": {}, "outputs": [], "source": [ "print(\"=== Plot with PLAID objects ===\")\n", "\n", "# Load PLAID datasets and problem metadata objects\n", "ref_path = Dataset(dataset_directory / \"dataset_ref\")\n", "pred_path = Dataset(dataset_directory / \"dataset_pred\")\n", "problem_path = ProblemDefinition(dataset_directory / \"problem_definition\")\n", "\n", "# Using PLAID objects to generate bisect plot on feature_2\n", "plot_bisect(ref_path, pred_path, problem_path, \"feature_2\", \"equal_bisect_plot\")" ] }, { "cell_type": "markdown", "id": "233a28a4", "metadata": {}, "source": [ "## Mixing with Scalar Index and Verbose\n", "\n", "In this final section, we showcase a mix of file paths and PLAID objects, incorporating a scalar index and enabling the verbose option when generating a bisect plot. This can provide more detailed information during the plotting process." ] }, { "cell_type": "code", "execution_count": null, "id": "59d5e0a5", "metadata": {}, "outputs": [], "source": [ "print(\"=== Mix with scalar index and verbose ===\")\n", "\n", "# Mix\n", "ref_path = dataset_directory / \"dataset_ref\"\n", "pred_path = dataset_directory / \"dataset_near_pred\"\n", "problem_path = ProblemDefinition(dataset_directory / \"problem_definition\")\n", "\n", "# Using scalar index and verbose option to generate bisect plot\n", "scalar_index = 0\n", "plot_bisect(\n", " ref_path,\n", " pred_path,\n", " problem_path,\n", " scalar_index,\n", " \"converge_bisect_plot\",\n", " verbose=True,\n", ")\n", "\n", "os.remove(\"converge_bisect_plot.png\")\n", "os.remove(\"differ_bisect_plot.png\")\n", "os.remove(\"equal_bisect_plot.png\")" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:percent" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }