{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3a8710e8", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:33:59.521185Z", "iopub.status.busy": "2023-06-06T01:33:59.520751Z", "iopub.status.idle": "2023-06-06T01:33:59.577471Z", "shell.execute_reply": "2023-06-06T01:33:59.576806Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "8371f151", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:33:59.582366Z", "iopub.status.busy": "2023-06-06T01:33:59.581172Z", "iopub.status.idle": "2023-06-06T01:34:01.690989Z", "shell.execute_reply": "2023-06-06T01:34:01.690319Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "   SpectroChemPy's API - v.0.6.5
© Copyright 2014-2023 - A.Travert & C.Fernandez @ LCS
\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import spectrochempy as scp\n", "from spectrochempy import Quantity, ur # to simplify further writing" ] }, { "cell_type": "markdown", "id": "e9fe4d28", "metadata": {}, "source": [ "# Units & Quantities" ] }, { "cell_type": "markdown", "id": "7d70b0d8", "metadata": {}, "source": [ "SpectroChemPy can do calculations with units - it uses [pint](https://pint.readthedocs.io) to define and perform\n", "operation on data with units.\n", "\n", "Two objects, `ur` and `Quantity` , allow the manipulation of data with units:\n", "\n", "* `ur`: stands for **unit registry**, is used to define and handle many type of units as well as making conversion\n", " between them.\n", "* `Quantity`: is a scalar or an array of scalars with some units." ] }, { "cell_type": "markdown", "id": "ad275562", "metadata": { "lines_to_next_cell": 0 }, "source": [ "## Units\n", "For instance, a unit of speed can be defined as:" ] }, { "cell_type": "code", "execution_count": 3, "id": "7def6676", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.694664Z", "iopub.status.busy": "2023-06-06T01:34:01.694007Z", "iopub.status.idle": "2023-06-06T01:34:01.701678Z", "shell.execute_reply": "2023-06-06T01:34:01.700995Z" }, "lines_to_next_cell": 2 }, "outputs": [ { "data": { "text/html": [ "cm.s-1" ], "text/latex": [ "$\\mathrm{cm} \\cdot \\mathrm{s}^{-1}$" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ur.cm / ur.s" ] }, { "cell_type": "markdown", "id": "7fa04baa", "metadata": {}, "source": [ "## Create quantities\n", "\n", "to create quantity, use for instance, one of the following expression:" ] }, { "cell_type": "code", "execution_count": 4, "id": "550a9e33", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.704643Z", "iopub.status.busy": "2023-06-06T01:34:01.704229Z", "iopub.status.idle": "2023-06-06T01:34:01.709610Z", "shell.execute_reply": "2023-06-06T01:34:01.708950Z" } }, "outputs": [ { "data": { "text/html": [ "10.0 cm-1" ], "text/latex": [ "$10.0\\ \\mathrm{cm}^{-1}$" ], "text/plain": [ "10.0 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Quantity(\"10.0 cm^-1\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "00a198ea", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.713170Z", "iopub.status.busy": "2023-06-06T01:34:01.712790Z", "iopub.status.idle": "2023-06-06T01:34:01.718275Z", "shell.execute_reply": "2023-06-06T01:34:01.717582Z" } }, "outputs": [ { "data": { "text/html": [ "1.0 cm-1.h-1" ], "text/latex": [ "$1.0\\ \\mathrm{cm}^{-1} \\cdot \\mathrm{h}^{-1}$" ], "text/plain": [ "1.0 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Quantity(1.0, \"cm^-1/hour\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "2ad12297", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.721228Z", "iopub.status.busy": "2023-06-06T01:34:01.720687Z", "iopub.status.idle": "2023-06-06T01:34:01.726941Z", "shell.execute_reply": "2023-06-06T01:34:01.726288Z" } }, "outputs": [ { "data": { "text/html": [ "10.0 scaled-dimensionless (1e-05)" ], "text/latex": [ "$10.0\\ \\mathrm{scaled-dimensionless (1e-05)}$" ], "text/plain": [ "10.0 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Quantity(10.0, ur.cm / ur.km)" ] }, { "cell_type": "markdown", "id": "f4b4f893", "metadata": {}, "source": [ "or may be (?) simpler," ] }, { "cell_type": "code", "execution_count": 7, "id": "5f9a637b", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.730619Z", "iopub.status.busy": "2023-06-06T01:34:01.730081Z", "iopub.status.idle": "2023-06-06T01:34:01.735620Z", "shell.execute_reply": "2023-06-06T01:34:01.734969Z" } }, "outputs": [ { "data": { "text/html": [ "10.0 scaled-dimensionless (1e-05)" ], "text/latex": [ "$10.0\\ \\mathrm{scaled-dimensionless (1e-05)}$" ], "text/plain": [ "10.0 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10.0 * ur.cm / ur.km" ] }, { "cell_type": "markdown", "id": "c3222ba3", "metadata": {}, "source": [ "## Do arithmetic with units" ] }, { "cell_type": "code", "execution_count": 8, "id": "c1e03f2f", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.739078Z", "iopub.status.busy": "2023-06-06T01:34:01.738715Z", "iopub.status.idle": "2023-06-06T01:34:01.744259Z", "shell.execute_reply": "2023-06-06T01:34:01.743617Z" } }, "outputs": [ { "data": { "text/html": [ "200.0 km.h-1" ], "text/latex": [ "$200.0\\ \\mathrm{km} \\cdot \\mathrm{h}^{-1}$" ], "text/plain": [ "200.0 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 900 * ur.km\n", "b = 4.5 * ur.hours\n", "a / b" ] }, { "cell_type": "markdown", "id": "895fefaf", "metadata": {}, "source": [ "Such calculations can also be done using the following syntax, using a string expression" ] }, { "cell_type": "code", "execution_count": 9, "id": "dc4d1c97", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.747699Z", "iopub.status.busy": "2023-06-06T01:34:01.747172Z", "iopub.status.idle": "2023-06-06T01:34:01.752510Z", "shell.execute_reply": "2023-06-06T01:34:01.751851Z" } }, "outputs": [ { "data": { "text/html": [ "200.0 km.h-1" ], "text/latex": [ "$200.0\\ \\mathrm{km} \\cdot \\mathrm{h}^{-1}$" ], "text/plain": [ "200.0 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Quantity(\"900 km / (4.5 hours)\")" ] }, { "cell_type": "markdown", "id": "601521a8", "metadata": {}, "source": [ "## Conversion between units" ] }, { "cell_type": "code", "execution_count": 10, "id": "08e87972", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.755870Z", "iopub.status.busy": "2023-06-06T01:34:01.755348Z", "iopub.status.idle": "2023-06-06T01:34:01.759085Z", "shell.execute_reply": "2023-06-06T01:34:01.758442Z" }, "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = a / b\n", "d = c.to(\"cm/s\")" ] }, { "cell_type": "markdown", "id": "a5cb22e4", "metadata": {}, "source": [ "As shown below `to()` has generated a new variable and does not affect the initial one:" ] }, { "cell_type": "code", "execution_count": 11, "id": "e05a2f62", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.762218Z", "iopub.status.busy": "2023-06-06T01:34:01.761717Z", "iopub.status.idle": "2023-06-06T01:34:01.765662Z", "shell.execute_reply": "2023-06-06T01:34:01.765014Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "initial quantity: c = 200.0 km.h⁻¹\n", "converted quantity: d = 5555.555555555556 cm.s⁻¹\n" ] } ], "source": [ "print(f\"initial quantity: c = {c}\")\n", "print(f\"converted quantity: d = {d}\")" ] }, { "cell_type": "markdown", "id": "4b979943", "metadata": {}, "source": [ "We can make the conversion *inplace* using `ito()` instead of `to()`:" ] }, { "cell_type": "code", "execution_count": 12, "id": "b5417c81", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.769501Z", "iopub.status.busy": "2023-06-06T01:34:01.768978Z", "iopub.status.idle": "2023-06-06T01:34:01.773014Z", "shell.execute_reply": "2023-06-06T01:34:01.772361Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "converted quantity: c = 55.55555555555556 m.s⁻¹\n" ] } ], "source": [ "c.ito(\"m/s\")\n", "print(f\"converted quantity: c = {c}\")" ] }, { "cell_type": "markdown", "id": "81c96cdd", "metadata": {}, "source": [ "## Do math operations with consistent units\n", "The units are transformed consistently in maths operations:" ] }, { "cell_type": "code", "execution_count": 13, "id": "61b50379", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.776528Z", "iopub.status.busy": "2023-06-06T01:34:01.776014Z", "iopub.status.idle": "2023-06-06T01:34:01.781180Z", "shell.execute_reply": "2023-06-06T01:34:01.780530Z" } }, "outputs": [ { "data": { "text/html": [ "3.1622776601683795 m0.5" ], "text/latex": [ "$3.1622776601683795\\ \\mathrm{m}^{0.5}$" ], "text/plain": [ "3.1622776601683795 " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10 * ur.meters\n", "np.sqrt(x)" ] }, { "cell_type": "code", "execution_count": 14, "id": "21f391fb", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.784703Z", "iopub.status.busy": "2023-06-06T01:34:01.784125Z", "iopub.status.idle": "2023-06-06T01:34:01.789442Z", "shell.execute_reply": "2023-06-06T01:34:01.788790Z" } }, "outputs": [ { "data": { "text/html": [ "-0.5440211108893698" ], "text/latex": [ "$-0.5440211108893698\\$" ], "text/plain": [ "-0.5440211108893698 " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10 * ur.radians\n", "np.sin(x)" ] }, { "cell_type": "markdown", "id": "d4965a49", "metadata": {}, "source": [ "Consistency of the units are checked and errors are generated if quantities have not appropriate units\n", "with the math operation..." ] }, { "cell_type": "code", "execution_count": 15, "id": "e2ea3c60", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.793054Z", "iopub.status.busy": "2023-06-06T01:34:01.792395Z", "iopub.status.idle": "2023-06-06T01:34:01.797390Z", "shell.execute_reply": "2023-06-06T01:34:01.796744Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " ERROR | DimensionalityError: Cannot convert from 'meter' ([length]) to 'radian' (dimensionless)\n" ] } ], "source": [ "x = 10 * ur.meters\n", "try:\n", " np.cos(x)\n", "except scp.DimensionalityError as e: # catch the error\n", " scp.error_(\n", " scp.DimensionalityError, e\n", " ) # generate the error message (see API configuration)\n", "\n", "# Consistency of the units are checked and errors are generated if quantities have not appropriate units\n", "# with the math operation..." ] }, { "cell_type": "markdown", "id": "e685ef19", "metadata": {}, "source": [ "## Stripping the units\n", "\n", "If for any reason - including quick and dirty checks -\n", "unitless numbers are needed, the `magnitude` field can be used:" ] }, { "cell_type": "code", "execution_count": 16, "id": "3de8d32c", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.800441Z", "iopub.status.busy": "2023-06-06T01:34:01.800006Z", "iopub.status.idle": "2023-06-06T01:34:01.804618Z", "shell.execute_reply": "2023-06-06T01:34:01.803975Z" } }, "outputs": [ { "data": { "text/plain": [ "-0.8390715290764524" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10 * ur.meters\n", "np.cos(x.magnitude)" ] }, { "cell_type": "markdown", "id": "70e4fdde", "metadata": {}, "source": [ "Units can be set for NDDataset data and/or Coordinates" ] }, { "cell_type": "code", "execution_count": 17, "id": "da5efcf7", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.807862Z", "iopub.status.busy": "2023-06-06T01:34:01.807437Z", "iopub.status.idle": "2023-06-06T01:34:01.815575Z", "shell.execute_reply": "2023-06-06T01:34:01.814933Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
name NDDataset_373ec80a
author runner@fv-az626-878
created 2023-06-06 01:34:01+00:00
DATA
title concentration
values
[ 1 2 3] g.cm⁻³
size 3
" ], "text/plain": [ "NDDataset: [float64] g.cm⁻³ (size: 3)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = scp.NDDataset([1.0, 2.0, 3.0], units=\"g/cm^3\", title=\"concentration\")\n", "ds" ] }, { "cell_type": "code", "execution_count": 18, "id": "77935758", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.818819Z", "iopub.status.busy": "2023-06-06T01:34:01.818404Z", "iopub.status.idle": "2023-06-06T01:34:01.825881Z", "shell.execute_reply": "2023-06-06T01:34:01.825235Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
name NDDataset_374025ba
author runner@fv-az626-878
created 2023-06-06 01:34:01+00:00
DATA
title concentration
values
[ 1000 2000 3000] kg.m⁻³
size 3
" ], "text/plain": [ "NDDataset: [float64] kg.m⁻³ (size: 3)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.to(\"kg/m^3\")" ] }, { "cell_type": "markdown", "id": "bee34f54", "metadata": {}, "source": [ "One can do transparent calculation using the units" ] }, { "cell_type": "code", "execution_count": 19, "id": "0012dfcc", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.829221Z", "iopub.status.busy": "2023-06-06T01:34:01.828793Z", "iopub.status.idle": "2023-06-06T01:34:01.836864Z", "shell.execute_reply": "2023-06-06T01:34:01.836228Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
name NDDataset_3741fb4c
author runner@fv-az626-878
created 2023-06-06 01:34:01+00:00
history
2023-06-06 01:34:01+00:00> Binary operation mul with `2 m³` has been performed
DATA
title concentration
values
[ 2 4 6] g.m³.cm⁻³
size 3
" ], "text/plain": [ "NDDataset: [float64] g.m³.cm⁻³ (size: 3)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume = Quantity(\"2 m^3\")\n", "ds1 = ds * volume\n", "ds1" ] }, { "cell_type": "code", "execution_count": 20, "id": "bfcc667c", "metadata": { "execution": { "iopub.execute_input": "2023-06-06T01:34:01.840205Z", "iopub.status.busy": "2023-06-06T01:34:01.839775Z", "iopub.status.idle": "2023-06-06T01:34:01.847832Z", "shell.execute_reply": "2023-06-06T01:34:01.847190Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
name NDDataset_3743afb4
author runner@fv-az626-878
created 2023-06-06 01:34:01+00:00
history
2023-06-06 01:34:01+00:00> Binary operation mul with `2 m³` has been performed
2023-06-06 01:34:01+00:00> Binary operation truediv with `NDDataset_373ec80a` has been performed
DATA
title concentration
values
[ 2 2 2] m³
size 3
" ], "text/plain": [ "NDDataset: [float64] m³ (size: 3)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds1 / ds" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:percent", "notebook_metadata_filter": "all" }, "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.9.16" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }