{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# View data\n", "\n", "ParamTools provides 2 primary ways for you to view and access your parameter values:\n", "\n", "1. Using `.sel`: `params.sel[\"parameter_name\"]`\n", "2. As NumPy arrays: `params.parameter_name`\n", "\n", "`.sel` is for querying parameter values based on their labels or values. Accessing values as NumPy arrays is for using parameter values for computational tasks. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's create some data." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import paramtools\n", "\n", "\n", "class Params(paramtools.Parameters):\n", " defaults = {\n", " \"schema\": {\n", " \"labels\": {\n", " \"date\": {\n", " \"type\": \"date\", \n", " \"validators\": {\n", " \"range\": {\"min\": \"2020-01-01\", \"max\": \"2021-01-01\", \"step\": {\"months\": 1}}\n", " }\n", " }\n", " },\n", " },\n", " \"a\": {\n", " \"title\": \"A\",\n", " \"type\": \"int\",\n", " \"value\": [{\"date\": \"2020-01-01\", \"value\": 2}, {\"date\": \"2020-10-01\", \"value\": 8},]\n", " },\n", " \"b\": {\n", " \"title\": \"B\",\n", " \"type\": \"float\",\n", " \"value\": [{\"date\": \"2020-01-01\", \"value\": 10.5}]\n", " }\n", " }\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Access values using `.sel`\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "params = Params()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "View all of the values of `a`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Values([\n", " {'date': datetime.date(2020, 1, 1), 'value': 2},\n", " {'date': datetime.date(2020, 10, 1), 'value': 8},\n", "])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.sel[\"a\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Access parameters directly using `isel`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'date': datetime.date(2020, 1, 1), 'value': 2}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.sel[\"a\"].isel[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Access labels and values directly:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2020, 1, 1)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.sel[\"a\"][\"date\"][0]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.sel[\"a\"][\"value\"][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ParamTools supports slicing:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[datetime.date(2020, 1, 1), datetime.date(2020, 10, 1)]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.sel[\"a\"][\"date\"][:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use `label_to_extend` to extend a and b's values over multiple months." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "Values([\n", " {'date': datetime.date(2020, 1, 1), 'value': 2},\n", " {'date': datetime.date(2020, 2, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 3, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 4, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 5, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 6, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 7, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 8, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 9, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 10, 1), 'value': 8},\n", " {'date': datetime.date(2020, 11, 1), 'value': 8, '_auto': True},\n", " {'date': datetime.date(2020, 12, 1), 'value': 8, '_auto': True},\n", " {'date': datetime.date(2021, 1, 1), 'value': 8, '_auto': True},\n", "])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params = Params(label_to_extend=\"date\")\n", "params.sel[\"a\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get all values with a date greater than August 1, 2020:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "QueryResult([\n", " {'date': datetime.date(2020, 9, 1), 'value': 2, '_auto': True}\n", " {'date': datetime.date(2020, 10, 1), 'value': 8}\n", " {'date': datetime.date(2020, 11, 1), 'value': 8, '_auto': True}\n", " {'date': datetime.date(2020, 12, 1), 'value': 8, '_auto': True}\n", " {'date': datetime.date(2021, 1, 1), 'value': 8, '_auto': True}\n", "])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datetime import date\n", "\n", "params.sel[\"a\"][\"date\"] > date(2020, 8, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterate over the query results:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OrderedDict([('date', datetime.date(2020, 9, 1)), ('value', 2), ('_auto', True)])\n", "OrderedDict([('date', datetime.date(2020, 10, 1)), ('value', 8)])\n", "OrderedDict([('date', datetime.date(2020, 11, 1)), ('value', 8), ('_auto', True)])\n", "OrderedDict([('date', datetime.date(2020, 12, 1)), ('value', 8), ('_auto', True)])\n", "OrderedDict([('date', datetime.date(2021, 1, 1)), ('value', 8), ('_auto', True)])\n" ] } ], "source": [ "for val in params.sel[\"a\"][\"date\"] > date(2020, 8, 1):\n", " print(val)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert first two values of the results to a list:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'date': datetime.date(2020, 9, 1), 'value': 2, '_auto': True},\n", " {'date': datetime.date(2020, 10, 1), 'value': 8}]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(params.sel[\"a\"][\"date\"] > date(2020, 8, 1)).isel[:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Access values directly as numpy arrays\n", "\n", "ParamTools uses information about the parameter labels to convert values from a list of dictionaries to a NumPy array. Set `array_first` to `True` for ParamTools to convert parameter values to arrays by default." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 8, 8, 8])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params = Params(label_to_extend=\"date\", array_first=True)\n", "\n", "params.a" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([21., 21., 21., 21., 21., 21., 21., 21., 21., 84., 84., 84., 84.])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.a * params.b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Select the values of the array:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 8])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datetime import date\n", "\n", "arr = params.to_array(\"a\", date=[date(2020, 1, 1), date(2020, 10, 1)])\n", "\n", "arr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert back to a list of dictionaries:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'date': datetime.date(2020, 1, 1), 'value': 2},\n", " {'date': datetime.date(2020, 10, 1), 'value': 8}]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.from_array(\"a\", arr, date=[date(2020, 1, 1), date(2020, 10, 1)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert parameter values to a dictionary:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': array([2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 8, 8, 8]),\n", " 'b': array([10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5,\n", " 10.5, 10.5])}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "param_dict = params.to_dict()\n", "\n", "param_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert `params` to a pandas DataFrame:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
date
2020-01-01210.5
2020-02-01210.5
2020-03-01210.5
2020-04-01210.5
2020-05-01210.5
2020-06-01210.5
2020-07-01210.5
2020-08-01210.5
2020-09-01210.5
2020-10-01810.5
2020-11-01810.5
2020-12-01810.5
2021-01-01810.5
\n", "
" ], "text/plain": [ " a b\n", "date \n", "2020-01-01 2 10.5\n", "2020-02-01 2 10.5\n", "2020-03-01 2 10.5\n", "2020-04-01 2 10.5\n", "2020-05-01 2 10.5\n", "2020-06-01 2 10.5\n", "2020-07-01 2 10.5\n", "2020-08-01 2 10.5\n", "2020-09-01 2 10.5\n", "2020-10-01 8 10.5\n", "2020-11-01 8 10.5\n", "2020-12-01 8 10.5\n", "2021-01-01 8 10.5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "param_dict[\"date\"] = params.label_grid[\"date\"]\n", "\n", "\n", "df = pd.DataFrame.from_dict(param_dict)\n", "df.set_index(\"date\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Miscellaneous" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get parameter names:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "odict_keys(['a', 'b'])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterate over parameter values:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a [2 2 2 2 2 2 2 2 2 8 8 8 8]\n", "b [10.5 10.5 10.5 10.5 10.5 10.5 10.5 10.5 10.5 10.5 10.5 10.5 10.5]\n" ] } ], "source": [ "for key, value in params.items():\n", " print(key, value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dump all parameter data:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'schema': {'labels': {'date': {'number_dims': 0,\n", " 'validators': {'range': {'step': {'months': 1},\n", " 'max': '2021-01-01',\n", " 'min': '2020-01-01'}},\n", " 'type': 'date'}},\n", " 'additional_members': {},\n", " 'operators': {'label_to_extend': 'date',\n", " 'uses_extend_func': False,\n", " 'array_first': True}},\n", " 'a': OrderedDict([('title', 'A'),\n", " ('type', 'int'),\n", " ('number_dims', 0),\n", " ('value',\n", " [OrderedDict([('date', '2020-01-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-02-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-03-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-04-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-05-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-06-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-07-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-08-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-09-01'), ('value', 2)]),\n", " OrderedDict([('date', '2020-10-01'), ('value', 8)]),\n", " OrderedDict([('date', '2020-11-01'), ('value', 8)]),\n", " OrderedDict([('date', '2020-12-01'), ('value', 8)]),\n", " OrderedDict([('date', '2021-01-01'), ('value', 8)])]),\n", " ('validators', {})]),\n", " 'b': OrderedDict([('title', 'B'),\n", " ('type', 'float'),\n", " ('number_dims', 0),\n", " ('value',\n", " [OrderedDict([('date', '2020-01-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-02-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-03-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-04-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-05-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-06-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-07-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-08-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-09-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-10-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-11-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2020-12-01'), ('value', 10.5)]),\n", " OrderedDict([('date', '2021-01-01'), ('value', 10.5)])]),\n", " ('validators', {})])}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params.dump()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.0" } }, "nbformat": 4, "nbformat_minor": 4 }