{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Welcome to Seeq Data Lab!\n",
    "Seeq Data Lab is a rich Python-based environment for exploring and manipulating time series and industrial process data. It is built on the [Jupyter](https://jupyter.org/) interactive Python platform and [Pandas](https://pandas.pydata.org/) data structures, making extensive use of the features and conventions that their user communities have established.\n",
    "\n",
    "Any datasources that are connected to your Seeq Server are easy to access via Seeq Data Lab's `spy` library. Several simple function calls like `search`, `pull` and `push` consume and/or produce [DataFrames](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) whenever possible so that you can use their powerful data manipulation capabilities and concise syntax.\n",
    "\n",
    "This tutorial walks you through the basics of searching for data, getting it into a DataFrame and then writing it back to a Seeq Workbench workbook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Started\n",
    "This section will walk you through searching for signals in Seeq and pulling data.\n",
    "\n",
    "## Importing the SPy Module\n",
    "The **SPy** module (short for Seeq PYthon) is what you'll use to interact with Seeq Server and access data. Start by importing it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from seeq import spy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's important to set `spy.options.compatibility` in your script to the current major version of SPy so that you minimize the chance that a SPy version upgrade breaks your notebook or script code. By telling SPy what version you have been using and have tested with, it can make choices about what behavior is most likely to be compatible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "spy.options.compatibility = 193"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You'll also want to import Pandas so that you can work with the DataFrame structures that the **spy** library consumes/produces."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Quick note: Pandas is installed with SPy core, but some requirements are not always installed by default. You can see an example of installing SPy extras in the section of this notebook titled \"Plotting the Results\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Logging in to Seeq\n",
    "\n",
    "**If you are using Seeq Data Lab, then this step is NOT necessary.** If you are using the SPy module with Anaconda, AWS SageMaker, Azure Notebooks or any other Jupyter Notebook solution, you will need to execute this step.\n",
    "\n",
    "Create a file in the root folder of your Jupyter project called `credentials.key` and put your username on the first line and your password on the second.\n",
    "\n",
    "If you log in to Seeq Server using your corporate credentials (aka \"Single Sign-On\"), please take a look at the [spy.login](spy.login.ipynb) documentation for information about logging in with an Access Key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Logged in to <strong>http://localhost:34216</strong> as <strong>agent_api_key</strong>.<br>Seeq Server Version: <strong>R66.0.0-SNAPSHOT</strong><br>Seeq SDK Module Version: <strong>66.0.0</strong> @ C:\\git\\crab\\sdk\\pypi\\seeq\\sdk<br>Seeq SPy Module Version: <strong>193.0</strong> @ C:\\git\\crab\\sdk\\pypi\\seeq\\spy</div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "spy.login(url='http://localhost:34216', credentials_file='../credentials.key', force=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Searching for Signals\n",
    "Now let's find some data. We're going to use the built-in Seeq example data to retrieve a few signals and look at them as DataFrames.\n",
    "\n",
    "First we'll use the `spy.search()` function to retrieve metadata."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Query successful</div><table class=\"tex2jax_ignore\" style=\"color:black;\"><tr><td style=\"background-color: #EEFFEE;\"></td><td style=\"background-color: #EEFFEE; text-align: left;\">Path</td><td style=\"background-color: #EEFFEE; text-align: right;\">Time</td><td style=\"background-color: #EEFFEE; text-align: right;\">Count</td><td style=\"background-color: #EEFFEE; text-align: right;\">Pages</td><td style=\"background-color: #EEFFEE; text-align: left;\">Result</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">0</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1 >> Area A</td><td style=\"vertical-align: top;\">00:00:00.14</td><td style=\"text-align: right; vertical-align: top;\">6</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Path</th>\n",
       "      <th>Asset</th>\n",
       "      <th>Name</th>\n",
       "      <th>Description</th>\n",
       "      <th>Type</th>\n",
       "      <th>Value Unit Of Measure</th>\n",
       "      <th>Datasource Name</th>\n",
       "      <th>Archived</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0EF58FC2-7E6E-7160-ACE8-E1D5408B23E1</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Relative Humidity</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>%</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0EF58FC2-7F31-6660-9AFE-AF177FC9FAB3</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Optimizer</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0EF58FC2-7F42-77D0-86AC-96C7027C1771</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>°F</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0EF58FC2-8762-F910-A3F7-157C1D6689FB</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Compressor Power</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>kW</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0EF58FC2-8AD8-ECC0-9FE2-65CCCE18FAA3</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Wet Bulb</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>°F</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0EF58FC2-8CFB-EAC0-A6FA-3B586A7AEA13</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Compressor Stage</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>string</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     ID                        Path   Asset  \\\n",
       "0  0EF58FC2-7E6E-7160-ACE8-E1D5408B23E1  Example >> Cooling Tower 1  Area A   \n",
       "1  0EF58FC2-7F31-6660-9AFE-AF177FC9FAB3  Example >> Cooling Tower 1  Area A   \n",
       "2  0EF58FC2-7F42-77D0-86AC-96C7027C1771  Example >> Cooling Tower 1  Area A   \n",
       "3  0EF58FC2-8762-F910-A3F7-157C1D6689FB  Example >> Cooling Tower 1  Area A   \n",
       "4  0EF58FC2-8AD8-ECC0-9FE2-65CCCE18FAA3  Example >> Cooling Tower 1  Area A   \n",
       "5  0EF58FC2-8CFB-EAC0-A6FA-3B586A7AEA13  Example >> Cooling Tower 1  Area A   \n",
       "\n",
       "                Name  Description          Type Value Unit Of Measure  \\\n",
       "0  Relative Humidity          NaN  StoredSignal                     %   \n",
       "1          Optimizer          NaN  StoredSignal                   NaN   \n",
       "2        Temperature          NaN  StoredSignal                    °F   \n",
       "3   Compressor Power          NaN  StoredSignal                    kW   \n",
       "4           Wet Bulb          NaN  StoredSignal                    °F   \n",
       "5   Compressor Stage          NaN  StoredSignal                string   \n",
       "\n",
       "  Datasource Name  Archived  \n",
       "0    Example Data     False  \n",
       "1    Example Data     False  \n",
       "2    Example Data     False  \n",
       "3    Example Data     False  \n",
       "4    Example Data     False  \n",
       "5    Example Data     False  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = spy.search({\n",
    "    \"Path\": \"Example >> Cooling Tower 1 >> Area A\"\n",
    "})\n",
    "\n",
    "# Print the output to the Jupyter page\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Refining the Results\n",
    "\n",
    "Let's pare down the results to just three signals before we pull some data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Path</th>\n",
       "      <th>Asset</th>\n",
       "      <th>Name</th>\n",
       "      <th>Description</th>\n",
       "      <th>Type</th>\n",
       "      <th>Value Unit Of Measure</th>\n",
       "      <th>Datasource Name</th>\n",
       "      <th>Archived</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0EF58FC2-7E6E-7160-ACE8-E1D5408B23E1</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Relative Humidity</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>%</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0EF58FC2-7F42-77D0-86AC-96C7027C1771</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>°F</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0EF58FC2-8762-F910-A3F7-157C1D6689FB</td>\n",
       "      <td>Example &gt;&gt; Cooling Tower 1</td>\n",
       "      <td>Area A</td>\n",
       "      <td>Compressor Power</td>\n",
       "      <td>NaN</td>\n",
       "      <td>StoredSignal</td>\n",
       "      <td>kW</td>\n",
       "      <td>Example Data</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     ID                        Path   Asset  \\\n",
       "0  0EF58FC2-7E6E-7160-ACE8-E1D5408B23E1  Example >> Cooling Tower 1  Area A   \n",
       "2  0EF58FC2-7F42-77D0-86AC-96C7027C1771  Example >> Cooling Tower 1  Area A   \n",
       "3  0EF58FC2-8762-F910-A3F7-157C1D6689FB  Example >> Cooling Tower 1  Area A   \n",
       "\n",
       "                Name  Description          Type Value Unit Of Measure  \\\n",
       "0  Relative Humidity          NaN  StoredSignal                     %   \n",
       "2        Temperature          NaN  StoredSignal                    °F   \n",
       "3   Compressor Power          NaN  StoredSignal                    kW   \n",
       "\n",
       "  Datasource Name  Archived  \n",
       "0    Example Data     False  \n",
       "2    Example Data     False  \n",
       "3    Example Data     False  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_signals = results.loc[results['Name'].isin(['Compressor Power', 'Temperature', 'Relative Humidity'])]\n",
    "my_signals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's pull in the data using Seeq. The data can come from any connected datasource, including historians like *OSIsoft PI*, time series stores like *InfluxDB*, and SQL databases like *Microsoft SQL Server*.\n",
    "\n",
    "You can specify the `grid` parameter to control the granularity of the data. Seeq's calculation engine will interpolate as needed to produce a uniform DataFrame. Specify `grid=None` if you want the raw data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Pull successful from <strong>2019-01-01 00:00:00+00:00</strong> to <strong>2019-01-07 00:00:00+00:00</strong></div><table class=\"tex2jax_ignore\" style=\"color:black;\"><tr><td style=\"background-color: #EEFFEE;\"></td><td style=\"background-color: #EEFFEE; text-align: left;\">ID</td><td style=\"background-color: #EEFFEE; text-align: left;\">Type</td><td style=\"background-color: #EEFFEE; text-align: left;\">Path</td><td style=\"background-color: #EEFFEE; text-align: left;\">Asset</td><td style=\"background-color: #EEFFEE; text-align: left;\">Name</td><td style=\"background-color: #EEFFEE; text-align: right;\">Time</td><td style=\"background-color: #EEFFEE; text-align: right;\">Count</td><td style=\"background-color: #EEFFEE; text-align: right;\">Pages</td><td style=\"background-color: #EEFFEE; text-align: left;\">Data Processed</td><td style=\"background-color: #EEFFEE; text-align: left;\">Result</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">0</td><td style=\"text-align: left; vertical-align: top;\">0EF58FC2-7E6E-7160-ACE8-E1D5408B23E1</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1</td><td style=\"text-align: left; vertical-align: top;\">Area A</td><td style=\"text-align: left; vertical-align: top;\">Relative Humidity</td><td style=\"vertical-align: top;\">00:00:00.11</td><td style=\"text-align: right; vertical-align: top;\">289</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">69 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">2</td><td style=\"text-align: left; vertical-align: top;\">0EF58FC2-7F42-77D0-86AC-96C7027C1771</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1</td><td style=\"text-align: left; vertical-align: top;\">Area A</td><td style=\"text-align: left; vertical-align: top;\">Temperature</td><td style=\"vertical-align: top;\">00:00:00.11</td><td style=\"text-align: right; vertical-align: top;\">289</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">69 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">3</td><td style=\"text-align: left; vertical-align: top;\">0EF58FC2-8762-F910-A3F7-157C1D6689FB</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1</td><td style=\"text-align: left; vertical-align: top;\">Area A</td><td style=\"text-align: left; vertical-align: top;\">Compressor Power</td><td style=\"vertical-align: top;\">00:00:00.09</td><td style=\"text-align: right; vertical-align: top;\">289</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">69 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Relative Humidity</th>\n",
       "      <th>Temperature</th>\n",
       "      <th>Compressor Power</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:00:00+00:00</th>\n",
       "      <td>42.729850</td>\n",
       "      <td>93.861500</td>\n",
       "      <td>39.189300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:30:00+00:00</th>\n",
       "      <td>39.848041</td>\n",
       "      <td>95.056341</td>\n",
       "      <td>39.949065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 01:00:00+00:00</th>\n",
       "      <td>38.005187</td>\n",
       "      <td>97.057959</td>\n",
       "      <td>39.326496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 01:30:00+00:00</th>\n",
       "      <td>36.986019</td>\n",
       "      <td>97.894497</td>\n",
       "      <td>39.977703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 02:00:00+00:00</th>\n",
       "      <td>36.274775</td>\n",
       "      <td>97.858900</td>\n",
       "      <td>40.223175</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Relative Humidity  Temperature  Compressor Power\n",
       "2019-01-01 00:00:00+00:00          42.729850    93.861500         39.189300\n",
       "2019-01-01 00:30:00+00:00          39.848041    95.056341         39.949065\n",
       "2019-01-01 01:00:00+00:00          38.005187    97.057959         39.326496\n",
       "2019-01-01 01:30:00+00:00          36.986019    97.894497         39.977703\n",
       "2019-01-01 02:00:00+00:00          36.274775    97.858900         40.223175"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_data = spy.pull(my_signals, start='2019-01-01', end='2019-01-07', grid='30min', header='Name')\n",
    "my_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting the Results\n",
    "Let's take a quick peek at the data we just pulled using the built in `DataFrame.plot()` function.\n",
    "\n",
    "Note: MatPlotLib is not included in SPy core. You may need to uncomment and run the cell below to install templates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install seeq-spy['templates']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "keep_output_in_docs": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: >"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 129420 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Make the plot render at a bigger size than default\n",
    "matplotlib.rcParams['figure.figsize'] = [12, 8]\n",
    "\n",
    "my_data.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE: Sometimes you have to execute the cell above twice for the plot to show. Not sure why.**\n",
    "\n",
    "How about a matrix of scatterplots, with each variable plotted against each other? Pandas can do that easily."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "keep_output_in_docs": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 138772 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pd.plotting.scatter_matrix(my_data)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Seeq's Calculation Engine\n",
    "\n",
    "What if we want to use Seeq's calculation engine to reduce the noise in the **Temperature** and **Relative Humidity** signals?\n",
    "\n",
    "We can apply a calculation by specifying a formula as the `calculation` argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Pull successful from <strong>2019-01-01 00:00:00+00:00</strong> to <strong>2019-01-07 00:00:00+00:00</strong></div><table class=\"tex2jax_ignore\" style=\"color:black;\"><tr><td style=\"background-color: #EEFFEE;\"></td><td style=\"background-color: #EEFFEE; text-align: left;\">ID</td><td style=\"background-color: #EEFFEE; text-align: left;\">Type</td><td style=\"background-color: #EEFFEE; text-align: left;\">Path</td><td style=\"background-color: #EEFFEE; text-align: left;\">Asset</td><td style=\"background-color: #EEFFEE; text-align: left;\">Name</td><td style=\"background-color: #EEFFEE; text-align: right;\">Time</td><td style=\"background-color: #EEFFEE; text-align: right;\">Count</td><td style=\"background-color: #EEFFEE; text-align: right;\">Pages</td><td style=\"background-color: #EEFFEE; text-align: left;\">Data Processed</td><td style=\"background-color: #EEFFEE; text-align: left;\">Result</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">0</td><td style=\"text-align: left; vertical-align: top;\">0EF58FC2-7E6E-7160-ACE8-E1D5408B23E1</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1</td><td style=\"text-align: left; vertical-align: top;\">Area A</td><td style=\"text-align: left; vertical-align: top;\">Relative Humidity</td><td style=\"vertical-align: top;\">00:00:00.14</td><td style=\"text-align: right; vertical-align: top;\">1729</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">77 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">2</td><td style=\"text-align: left; vertical-align: top;\">0EF58FC2-7F42-77D0-86AC-96C7027C1771</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1</td><td style=\"text-align: left; vertical-align: top;\">Area A</td><td style=\"text-align: left; vertical-align: top;\">Temperature</td><td style=\"vertical-align: top;\">00:00:00.15</td><td style=\"text-align: right; vertical-align: top;\">1729</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">77 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">3</td><td style=\"text-align: left; vertical-align: top;\">0EF58FC2-8762-F910-A3F7-157C1D6689FB</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td><td style=\"text-align: left; vertical-align: top;\">Example >> Cooling Tower 1</td><td style=\"text-align: left; vertical-align: top;\">Area A</td><td style=\"text-align: left; vertical-align: top;\">Compressor Power</td><td style=\"vertical-align: top;\">00:00:00.17</td><td style=\"text-align: right; vertical-align: top;\">1729</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">77 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Relative Humidity</th>\n",
       "      <th>Temperature</th>\n",
       "      <th>Compressor Power</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:00:00+00:00</th>\n",
       "      <td>41.582681</td>\n",
       "      <td>95.011676</td>\n",
       "      <td>39.521423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:05:00+00:00</th>\n",
       "      <td>41.257888</td>\n",
       "      <td>95.175356</td>\n",
       "      <td>39.560133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:10:00+00:00</th>\n",
       "      <td>40.943507</td>\n",
       "      <td>95.333398</td>\n",
       "      <td>39.591796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:15:00+00:00</th>\n",
       "      <td>40.639047</td>\n",
       "      <td>95.485623</td>\n",
       "      <td>39.615497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:20:00+00:00</th>\n",
       "      <td>40.345067</td>\n",
       "      <td>95.631086</td>\n",
       "      <td>39.629214</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Relative Humidity  Temperature  Compressor Power\n",
       "2019-01-01 00:00:00+00:00          41.582681    95.011676         39.521423\n",
       "2019-01-01 00:05:00+00:00          41.257888    95.175356         39.560133\n",
       "2019-01-01 00:10:00+00:00          40.943507    95.333398         39.591796\n",
       "2019-01-01 00:15:00+00:00          40.639047    95.485623         39.615497\n",
       "2019-01-01 00:20:00+00:00          40.345067    95.631086         39.629214"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calculated_data = spy.pull(\n",
    "    my_signals,\n",
    "    start='2019-01-01',\n",
    "    end='2019-01-07',\n",
    "    calculation='$signal.lowPassFilter(200min, 3min, 333)',\n",
    "    grid='5min',\n",
    "    header='Name')\n",
    "calculated_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "keep_output_in_docs": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 141192 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "calculated_data.plot()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pushing a New Condition to Seeq\n",
    "\n",
    "Let's push a Seeq *Condition* into our workbook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Pushed successfully to datasource <strong>Seeq Data Lab [Datasource ID: Seeq Data Lab]</strong> and scoped to workbook ID <strong>0EF5C002-DE39-6660-8D2F-03B6C79E3B15</strong><br>Click the following link to see what you pushed in Seeq:<br><a href=\"http://localhost:34216/0EF5BE2D-2738-FFA0-81E0-D2B058859C4D/workbook/0EF5C002-DE39-6660-8D2F-03B6C79E3B15/worksheet/0EF5C002-DE9D-77F0-BF8E-52235290AEAF\" target=\"_blank\">http://localhost:34216/0EF5BE2D-2738-FFA0-81E0-D2B058859C4D/workbook/0EF5C002-DE39-6660-8D2F-03B6C79E3B15/worksheet/0EF5C002-DE9D-77F0-BF8E-52235290AEAF</a></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Type</th>\n",
       "      <th>Formula</th>\n",
       "      <th>Formula Parameters</th>\n",
       "      <th>Scoped To</th>\n",
       "      <th>Datasource Class</th>\n",
       "      <th>Datasource ID</th>\n",
       "      <th>Data ID</th>\n",
       "      <th>ID</th>\n",
       "      <th>Push Result</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>CalculatedCondition</td>\n",
       "      <td>$cp.valueSearch(isGreaterThan(25kW))</td>\n",
       "      <td>[cp=0EF58FC2-8762-F910-A3F7-157C1D6689FB]</td>\n",
       "      <td>0EF5C002-DE39-6660-8D2F-03B6C79E3B15</td>\n",
       "      <td>Seeq Data Lab</td>\n",
       "      <td>Seeq Data Lab</td>\n",
       "      <td>[0EF5C002-DE39-6660-8D2F-03B6C79E3B15] {Condit...</td>\n",
       "      <td>0EF5C002-E253-7340-85DC-6BDC3AA5B4E0</td>\n",
       "      <td>Success</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 Name                 Type  \\\n",
       "0  Compressor on High  CalculatedCondition   \n",
       "\n",
       "                                Formula  \\\n",
       "0  $cp.valueSearch(isGreaterThan(25kW))   \n",
       "\n",
       "                          Formula Parameters  \\\n",
       "0  [cp=0EF58FC2-8762-F910-A3F7-157C1D6689FB]   \n",
       "\n",
       "                              Scoped To Datasource Class  Datasource ID  \\\n",
       "0  0EF5C002-DE39-6660-8D2F-03B6C79E3B15    Seeq Data Lab  Seeq Data Lab   \n",
       "\n",
       "                                             Data ID  \\\n",
       "0  [0EF5C002-DE39-6660-8D2F-03B6C79E3B15] {Condit...   \n",
       "\n",
       "                                     ID Push Result  \n",
       "0  0EF5C002-E253-7340-85DC-6BDC3AA5B4E0     Success  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "push_results = spy.push(metadata=pd.DataFrame([{\n",
    "    'Name': 'Compressor on High',\n",
    "    'Type': 'Condition',\n",
    "    'Formula': '$cp.valueSearch(isGreaterThan(25kW))',\n",
    "    'Formula Parameters': {\n",
    "        # Note here that we are just using a row from our search results. The SPy module will figure\n",
    "        # out that it contains an identifier that we can use.\n",
    "        '$cp': results[results['Name'] == 'Compressor Power']\n",
    "    }\n",
    "}]), workbook='SPy Documentation Examples >> Tutorial')\n",
    "\n",
    "push_results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pulling the Capsules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Pull successful from <strong>2019-01-01 00:00:00+00:00</strong> to <strong>2019-01-07 00:00:00+00:00</strong></div><table class=\"tex2jax_ignore\" style=\"color:black;\"><tr><td style=\"background-color: #EEFFEE;\"></td><td style=\"background-color: #EEFFEE; text-align: left;\">ID</td><td style=\"background-color: #EEFFEE; text-align: left;\">Type</td><td style=\"background-color: #EEFFEE; text-align: left;\">Name</td><td style=\"background-color: #EEFFEE; text-align: right;\">Time</td><td style=\"background-color: #EEFFEE; text-align: right;\">Count</td><td style=\"background-color: #EEFFEE; text-align: right;\">Pages</td><td style=\"background-color: #EEFFEE; text-align: left;\">Data Processed</td><td style=\"background-color: #EEFFEE; text-align: left;\">Result</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">0</td><td style=\"text-align: left; vertical-align: top;\">0EF5C002-E253-7340-85DC-6BDC3AA5B4E0</td><td style=\"text-align: left; vertical-align: top;\">CalculatedCondition</td><td style=\"text-align: left; vertical-align: top;\">Compressor on High</td><td style=\"vertical-align: top;\">00:00:00.11</td><td style=\"text-align: right; vertical-align: top;\">7</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: right; vertical-align: top;\">69 KB</td><td style=\"text-align: left; vertical-align: top;\">Success</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Condition</th>\n",
       "      <th>Capsule Start</th>\n",
       "      <th>Capsule End</th>\n",
       "      <th>Capsule Is Uncertain</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>NaT</td>\n",
       "      <td>2019-01-01 05:01:31.176600146+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>2019-01-02 00:16:37.331031739+00:00</td>\n",
       "      <td>2019-01-02 05:00:33.238973103+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>2019-01-03 00:13:18.731947840+00:00</td>\n",
       "      <td>2019-01-03 02:55:03.027869976+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>2019-01-03 17:28:04.251451273+00:00</td>\n",
       "      <td>2019-01-03 18:21:58.603314005+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>2019-01-04 17:20:33.782090068+00:00</td>\n",
       "      <td>2019-01-04 18:46:41.571969511+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>2019-01-05 02:18:11.253715409+00:00</td>\n",
       "      <td>2019-01-05 05:01:23.108876186+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Compressor on High</td>\n",
       "      <td>2019-01-05 17:20:29.256597642+00:00</td>\n",
       "      <td>2019-01-05 18:43:39.192264349+00:00</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Condition                       Capsule Start  \\\n",
       "0  Compressor on High                                 NaT   \n",
       "1  Compressor on High 2019-01-02 00:16:37.331031739+00:00   \n",
       "2  Compressor on High 2019-01-03 00:13:18.731947840+00:00   \n",
       "3  Compressor on High 2019-01-03 17:28:04.251451273+00:00   \n",
       "4  Compressor on High 2019-01-04 17:20:33.782090068+00:00   \n",
       "5  Compressor on High 2019-01-05 02:18:11.253715409+00:00   \n",
       "6  Compressor on High 2019-01-05 17:20:29.256597642+00:00   \n",
       "\n",
       "                          Capsule End  Capsule Is Uncertain  \n",
       "0 2019-01-01 05:01:31.176600146+00:00                 False  \n",
       "1 2019-01-02 05:00:33.238973103+00:00                 False  \n",
       "2 2019-01-03 02:55:03.027869976+00:00                 False  \n",
       "3 2019-01-03 18:21:58.603314005+00:00                 False  \n",
       "4 2019-01-04 18:46:41.571969511+00:00                 False  \n",
       "5 2019-01-05 05:01:23.108876186+00:00                 False  \n",
       "6 2019-01-05 18:43:39.192264349+00:00                 False  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "capsules = spy.pull(push_results, start='2019-01-01', end='2019-01-07')\n",
    "capsules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting Conditions Alongside Signals\n",
    "Now let's use the new condition in a plot, this time using `spy.plot()`, which has a set of predefined plot configurations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "keep_output_in_docs": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 143424 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "samples = calculated_data\n",
    "\n",
    "spy.plot(samples=samples, capsules=capsules)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculating a New Signal using Python\n",
    "One reason you wanted to get the data into Python in the first place is to create new data. Let's clip **Compressor Power** by 10 so it simplifies to just representing power on or off."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Compressor Power</th>\n",
       "      <th>Attenuated Power</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:00:00+00:00</th>\n",
       "      <td>39.189300</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:30:00+00:00</th>\n",
       "      <td>39.949065</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 01:00:00+00:00</th>\n",
       "      <td>39.326496</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 01:30:00+00:00</th>\n",
       "      <td>39.977703</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 02:00:00+00:00</th>\n",
       "      <td>40.223175</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Compressor Power  Attenuated Power\n",
       "2019-01-01 00:00:00+00:00         39.189300              10.0\n",
       "2019-01-01 00:30:00+00:00         39.949065              10.0\n",
       "2019-01-01 01:00:00+00:00         39.326496              10.0\n",
       "2019-01-01 01:30:00+00:00         39.977703              10.0\n",
       "2019-01-01 02:00:00+00:00         40.223175              10.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def attenuate_power(sample):\n",
    "    return min(sample, 10)\n",
    "\n",
    "\n",
    "clipped_data = pd.DataFrame({'Compressor Power': my_data['Compressor Power'],\n",
    "                             'Attenuated Power': my_data['Compressor Power'].apply(attenuate_power)})\n",
    "clipped_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "keep_output_in_docs": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: >"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 75184 characters]\n",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "clipped_data.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pushing Data Into Seeq\n",
    "Jupyter is a great environment for programming, but let's go back to Seeq Workbench and make things truly interactive."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"background-color: #EEFFEE;color:black; text-align: left;\">Pushed successfully to datasource <strong>Seeq Data Lab [Datasource ID: Seeq Data Lab]</strong> and scoped to workbook ID <strong>0EF5C002-DE39-6660-8D2F-03B6C79E3B15</strong><br>Click the following link to see what you pushed in Seeq:<br><a href=\"http://localhost:34216/0EF5BE2D-2738-FFA0-81E0-D2B058859C4D/workbook/0EF5C002-DE39-6660-8D2F-03B6C79E3B15/worksheet/0EF5C002-DE9D-77F0-BF8E-52235290AEAF\" target=\"_blank\">http://localhost:34216/0EF5BE2D-2738-FFA0-81E0-D2B058859C4D/workbook/0EF5C002-DE39-6660-8D2F-03B6C79E3B15/worksheet/0EF5C002-DE9D-77F0-BF8E-52235290AEAF</a></div><table class=\"tex2jax_ignore\" style=\"color:black;\"><tr><td style=\"background-color: #EEFFEE;\"></td><td style=\"background-color: #EEFFEE; text-align: right;\">Count</td><td style=\"background-color: #EEFFEE; text-align: right;\">Pages</td><td style=\"background-color: #EEFFEE; text-align: left;\">Result</td><td style=\"background-color: #EEFFEE; text-align: right;\">Time</td><td style=\"background-color: #EEFFEE; text-align: left;\">Type</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">Compressor Power</td><td style=\"text-align: right; vertical-align: top;\">289</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: left; vertical-align: top;\">Success</td><td style=\"vertical-align: top;\">00:00:00.05</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td></tr><tr style=\"background-color: #EEFFEE;\"><td style=\"vertical-align: top;\">Attenuated Power</td><td style=\"text-align: right; vertical-align: top;\">289</td><td style=\"text-align: right; vertical-align: top;\">1</td><td style=\"text-align: left; vertical-align: top;\">Success</td><td style=\"vertical-align: top;\">00:00:00.04</td><td style=\"text-align: left; vertical-align: top;\">StoredSignal</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Push Count</th>\n",
       "      <th>Push Time</th>\n",
       "      <th>Push Result</th>\n",
       "      <th>Name</th>\n",
       "      <th>ID</th>\n",
       "      <th>Type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Compressor Power</th>\n",
       "      <td>289.0</td>\n",
       "      <td>0 days 00:00:00.045528</td>\n",
       "      <td>Success</td>\n",
       "      <td>Compressor Power</td>\n",
       "      <td>0EF5C002-F5FF-FD30-8D17-4856440C3289</td>\n",
       "      <td>StoredSignal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Attenuated Power</th>\n",
       "      <td>289.0</td>\n",
       "      <td>0 days 00:00:00.044528</td>\n",
       "      <td>Success</td>\n",
       "      <td>Attenuated Power</td>\n",
       "      <td>0EF5C002-F5FF-FD30-B5E3-3AE830E75B23</td>\n",
       "      <td>StoredSignal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Push Count              Push Time Push Result  \\\n",
       "Compressor Power       289.0 0 days 00:00:00.045528     Success   \n",
       "Attenuated Power       289.0 0 days 00:00:00.044528     Success   \n",
       "\n",
       "                              Name                                    ID  \\\n",
       "Compressor Power  Compressor Power  0EF5C002-F5FF-FD30-8D17-4856440C3289   \n",
       "Attenuated Power  Attenuated Power  0EF5C002-F5FF-FD30-B5E3-3AE830E75B23   \n",
       "\n",
       "                          Type  \n",
       "Compressor Power  StoredSignal  \n",
       "Attenuated Power  StoredSignal  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spy.push(clipped_data, workbook='SPy Documentation Examples >> Tutorial')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The data is now in Seeq's internal time series database and is scoped to a workbook. Click the link above to see it!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11",
   "language": "python",
   "name": "python311"
  },
  "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
