{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipp as sc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# How to...\n", "## Create variables\n", "### Scalar variable using operators with units" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1.2 * sc.units.kg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scalar variable from Python object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.scalar(1.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.scalar(\"string\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.scalar([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialized with default values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.zeros(dims=['y', 'x'], shape=(2,3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.zeros(dims=['x'], shape=(2,), dtype=sc.dtype.bool)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From Python list or NumPy array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.Variable(dims=['x'], values=[1.2, 2.3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "sc.Variable(dims=['y', 'x'], values=np.array([[1, 2], [3, 4]]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a data array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.Variable(dims=['x'], values=[1.0, 2.0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = sc.DataArray(var)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an empty dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = sc.Dataset()\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add / insert a data item, a coord, a mask, or an attr\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['data'] = var\n", "d.coords['x'] = var\n", "d.coords['lab'] = var\n", "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.coords['x'] = var\n", "a.coords['lab'] = var\n", "a.masks['mask'] = sc.Variable(dims=['x'], values=[False, True])\n", "a.attrs['info'] = sc.scalar(\"text\")\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get a data item, a coord, or a mask" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['data']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.coords['x']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.coords['lab']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.masks['mask']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.attrs['info']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete / remove a data item, a coord, a mask, or an attr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del d['data']\n", "del d.coords['x']\n", "del d.coords['lab']\n", "del a.masks['mask']\n", "del a.attrs['info']\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Copy variables, data arrays, or dataset\n", "\n", "Note that **scipp always makes a deep copy**.\n", "This includes the case of storing native Python objects in a variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deep_copy = d.copy() # Option 1\n", "import copy\n", "deep_copy = copy.copy(d) # Option 2\n", "deep_copy = copy.deepcopy(d) # Option 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, note that all three options result in a deep copy.\n", "The syntax is the same for variables, data arrays, and datasets." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create stand-alone objects from (slice) views\n", "\n", "Views created by slicing, or from selecting an item in a dataset reference data in the original container.\n", "Use `copy` to convert a view into a stand alone object:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deep_copy = var['x', 0:1].copy()\n", "deep_copy = copy.copy(var['x', 0:1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['data'] = var\n", "deep_copy = d['data'].copy()\n", "deep_copy = copy.copy(d['data'])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }