{ "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", " | a | \n", "b | \n", "
---|---|---|
date | \n", "\n", " | \n", " |
2020-01-01 | \n", "2 | \n", "10.5 | \n", "
2020-02-01 | \n", "2 | \n", "10.5 | \n", "
2020-03-01 | \n", "2 | \n", "10.5 | \n", "
2020-04-01 | \n", "2 | \n", "10.5 | \n", "
2020-05-01 | \n", "2 | \n", "10.5 | \n", "
2020-06-01 | \n", "2 | \n", "10.5 | \n", "
2020-07-01 | \n", "2 | \n", "10.5 | \n", "
2020-08-01 | \n", "2 | \n", "10.5 | \n", "
2020-09-01 | \n", "2 | \n", "10.5 | \n", "
2020-10-01 | \n", "8 | \n", "10.5 | \n", "
2020-11-01 | \n", "8 | \n", "10.5 | \n", "
2020-12-01 | \n", "8 | \n", "10.5 | \n", "
2021-01-01 | \n", "8 | \n", "10.5 | \n", "