{ "cells": [ { "cell_type": "markdown", "id": "f5beb2ae-97e7-49e4-ba63-1275ba68519c", "metadata": {}, "source": [ "## Accessing yt methods \n", "\n", "A subset of methods available from the yt API are directly accessible when using `yt_xarray`. \n", "\n", "### Plotting methods \n", "\n", "Currently, the following plotting methods are available: \n", "\n", "\n", "* `SlicePlot`\n", "* `ProjectionPlot`\n", "* `PhasePlot`\n", "* `ProfilePlot`\n", "\n", "When loading an xarray dataset:" ] }, { "cell_type": "code", "execution_count": 1, "id": "19bb3b82-c0e3-49d9-a108-99eb93d625a4", "metadata": {}, "outputs": [], "source": [ "from yt_xarray.sample_data import load_random_xr_data\n", "import yt_xarray \n", "\n", "fields = {'temperature': ('x', 'y', 'z'), 'pressure': ('x', 'y', 'z')}\n", "dims = {'x': (0,1,15), 'y': (0, 1, 10), 'z': (0, 1, 15)}\n", "ds = load_random_xr_data(fields, dims, length_unit='m')" ] }, { "cell_type": "markdown", "id": "c125c31a-a373-4403-98ee-a47048ac142b", "metadata": {}, "source": [ "The methods will be available under the `yt` accessor, for example `ds.yt.SlicePlot` will create a slice normal to the supplied axis name for a given field:" ] }, { "cell_type": "code", "execution_count": 2, "id": "f197500e-33cc-401c-8175-fcc3b7c336c5", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2024-01-08 14:43:53,235: Inferred geometry type is cartesian. To override, use ds.yt.set_geometry\n", "yt_xarray : [INFO ] 2024-01-08 14:43:53,245: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2024-01-08 14:43:53,245: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2024-01-08 14:43:53,301 Parameters: current_time = 0.0\n", "yt : [INFO ] 2024-01-08 14:43:53,301 Parameters: domain_dimensions = [15 10 15]\n", "yt : [INFO ] 2024-01-08 14:43:53,302 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:53,302 Parameters: domain_right_edge = [1.03571429 1.05555556 1.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:53,302 Parameters: cosmological_simulation = 0\n", "yt : [INFO ] 2024-01-08 14:43:53,407 xlim = -0.055556 1.055556\n", "yt : [INFO ] 2024-01-08 14:43:53,407 ylim = -0.035714 1.035714\n", "yt : [INFO ] 2024-01-08 14:43:53,409 xlim = -0.055556 1.055556\n", "yt : [INFO ] 2024-01-08 14:43:53,410 ylim = -0.035714 1.035714\n", "yt : [INFO ] 2024-01-08 14:43:53,415 Making a fixed resolution buffer of (('stream', 'pressure')) 800 by 800\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.yt.SlicePlot('x', 'pressure')" ] }, { "cell_type": "markdown", "id": "89abc66b-79ec-412e-b2a5-dd0516021b6c", "metadata": {}, "source": [ "Importantly, all of these methods will operate on the full range of data avaialable. Most yt methods accept optional data selector objects to make plots for subsets of data without loading the full dataset. To take advantage of that, you'll want to first create a yt dataset object with `ds.yt.load_grid` and then use the standard `yt` API. \n", "\n", "`ProjectionPlot` calculates a projection over the provided normal access. " ] }, { "cell_type": "code", "execution_count": 3, "id": "c25654d2-98f8-4c35-9d6a-ee68a3f92ac5", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2024-01-08 14:43:54,390: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2024-01-08 14:43:54,391: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2024-01-08 14:43:54,433 Parameters: current_time = 0.0\n", "yt : [INFO ] 2024-01-08 14:43:54,434 Parameters: domain_dimensions = [15 10 15]\n", "yt : [INFO ] 2024-01-08 14:43:54,434 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:54,435 Parameters: domain_right_edge = [1.03571429 1.05555556 1.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:54,435 Parameters: cosmological_simulation = 0\n", "yt : [INFO ] 2024-01-08 14:43:54,516 Projection completed\n", "yt : [INFO ] 2024-01-08 14:43:54,516 xlim = -0.055556 1.055556\n", "yt : [INFO ] 2024-01-08 14:43:54,517 ylim = -0.035714 1.035714\n", "yt : [INFO ] 2024-01-08 14:43:54,518 xlim = -0.055556 1.055556\n", "yt : [INFO ] 2024-01-08 14:43:54,519 ylim = -0.035714 1.035714\n", "yt : [INFO ] 2024-01-08 14:43:54,521 Making a fixed resolution buffer of (('stream', 'pressure')) 800 by 800\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.yt.ProjectionPlot('x', 'pressure')" ] }, { "cell_type": "markdown", "id": "1d89b466-34f8-4c1c-9377-80ccd59833c3", "metadata": {}, "source": [ "The accumulated value is controlled by the `method` arguement (see the `yt.AxisAlignedProjectionPlot` docstring for a full description of the other keyword arguments): \n", "```\n", "method : string\n", " The method of projection. Valid methods are:\n", "\n", " \"integrate\" with no weight_field specified : integrate the requested\n", " field along the line of sight.\n", "\n", " \"integrate\" with a weight_field specified : weight the requested\n", " field by the weighting field and integrate along the line of sight.\n", "\n", " \"max\" : pick out the maximum value of the field in the line of sight.\n", " \"min\" : pick out the minimum value of the field in the line of sight.\n", "\n", " \"sum\" : This method is the same as integrate, except that it does not\n", " multiply by a path length when performing the integration, and is\n", " just a straight summation of the field along the given axis. WARNING:\n", " This should only be used for uniform resolution grid datasets, as other\n", " datasets may result in unphysical images.\n", "```\n" ] }, { "cell_type": "markdown", "id": "80ce42ec-bd42-42d9-b9ac-028c4ffc3e6f", "metadata": {}, "source": [ "The other visualization methods available at present are binned-statistic plots. `ProfilePlot` bins a single variable and finds values (or weighted-values) of a second variable within those bins:" ] }, { "cell_type": "code", "execution_count": 4, "id": "96f01e7b-d853-4ab0-ae3d-f6d1632b67e8", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2024-01-08 14:43:54,996: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2024-01-08 14:43:54,997: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2024-01-08 14:43:55,038 Parameters: current_time = 0.0\n", "yt : [INFO ] 2024-01-08 14:43:55,039 Parameters: domain_dimensions = [15 10 15]\n", "yt : [INFO ] 2024-01-08 14:43:55,039 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:55,040 Parameters: domain_right_edge = [1.03571429 1.05555556 1.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:55,041 Parameters: cosmological_simulation = 0\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.yt.ProfilePlot('temperature', 'pressure')" ] }, { "cell_type": "markdown", "id": "d4096739-eb76-4b41-85c6-aff5517a9800", "metadata": {}, "source": [ "`PhasePlot` instead bins 2 variables and then finds values (or weighted-values) of a 3rd variable within those bins:" ] }, { "cell_type": "code", "execution_count": 5, "id": "c7821e37-3d4b-4aec-b2c3-6882c83d16d2", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2024-01-08 14:43:55,474: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2024-01-08 14:43:55,475: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2024-01-08 14:43:55,510 Parameters: current_time = 0.0\n", "yt : [INFO ] 2024-01-08 14:43:55,511 Parameters: domain_dimensions = [15 10 15]\n", "yt : [INFO ] 2024-01-08 14:43:55,512 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:55,512 Parameters: domain_right_edge = [1.03571429 1.05555556 1.03571429]\n", "yt : [INFO ] 2024-01-08 14:43:55,513 Parameters: cosmological_simulation = 0\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.yt.PhasePlot('temperature', 'pressure', 'temperature')" ] }, { "cell_type": "markdown", "id": "0c86c6bb-505b-4b8f-ab39-81247dae71d5", "metadata": {}, "source": [ "For a full description of plotting in `yt`, see [the yt documentation](https://yt-project.org/doc/visualizing/plots.html). " ] } ], "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.10.11" } }, "nbformat": 4, "nbformat_minor": 5 }