{ "cells": [ { "cell_type": "markdown", "id": "c68003ae", "metadata": {}, "source": [ "Appending Data to xarray.Datasets\n", "================================" ] }, { "cell_type": "markdown", "id": "8bda8da5", "metadata": {}, "source": [ "When working with scientific data, you'll often need to combine or append datasets from different experiments or measurements. This tutorial demonstrates various methods to append data to existing xarray.Dataset objects, with a focus on scattering and composition data.\n" ] }, { "cell_type": "markdown", "id": "9a8ef101", "metadata": {}, "source": [ "\n", "Setup\n", "-----\n" ] }, { "cell_type": "markdown", "id": "21e0776d", "metadata": {}, "source": [ "## Google Colab Setup\n", "\n", "Only uncomment and run the next cell if you are running this notebook in Google Colab or if don't already have the AFL-agent package installed." ] }, { "cell_type": "code", "execution_count": null, "id": "aedb2ae3", "metadata": {}, "outputs": [], "source": [ "# !pip install git+https://github.com/usnistgov/AFL-agent.git" ] }, { "cell_type": "markdown", "id": "05f3daf4", "metadata": {}, "source": [ "Next, let's import the necessary support modules and load data from AFL.double_agent" ] }, { "cell_type": "code", "execution_count": 2, "id": "d71bef10", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xarray version: 2025.1.2\n", "Dataset dimensions: {'sample': 100, 'component': 2, 'x': 150, 'grid': 2500}\n", "Dataset variables: ['composition', 'ground_truth_labels', 'measurement', 'composition_grid']\n", "Dataset coordinates: ['component', 'x']\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "\n", "print(f\"xarray version: {xr.__version__}\")\n", "\n", "# Import the example dataset from AFL.double_agent.data\n", "from AFL.double_agent.data import example_dataset1\n", "\n", "# Load the example dataset\n", "ds = example_dataset1()\n", "\n", "# Print basic information about the dataset\n", "print(f\"Dataset dimensions: {dict(ds.sizes)}\")\n", "print(f\"Dataset variables: {list(ds.data_vars)}\")\n", "print(f\"Dataset coordinates: {list(ds.coords)}\")" ] }, { "cell_type": "markdown", "id": "8418da99", "metadata": {}, "source": [ "Understanding the Dataset\n", "------------------------\n", "\n", "Let's first understand the structure of our example dataset:" ] }, { "cell_type": "code", "execution_count": 3, "id": "49a69bea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Composition data shape: (100, 2)\n", "Sample of composition data:\n" ] }, { "data": { "text/html": [ "
<xarray.DataArray 'composition' (sample: 3, component: 2)> Size: 48B\n", "[6 values with dtype=float64]\n", "Coordinates:\n", " * component (component) <U1 8B 'A' 'B'\n", "Dimensions without coordinates: sample
<xarray.DataArray 'measurement' (sample: 2, x: 5)> Size: 80B\n", "[10 values with dtype=float64]\n", "Coordinates:\n", " * x (x) float64 40B 0.001 0.001047 0.001097 0.001149 0.001204\n", "Dimensions without coordinates: sample" ], "text/plain": [ "
<xarray.Dataset> Size: 400B\n", "Dimensions: (sample: 50)\n", "Dimensions without coordinates: sample\n", "Data variables:\n", " measurement_mean (sample) float64 400B 8.046e+04 7.622e+04 ... 7.786e+04