{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "fd6821ad",
"metadata": {},
"source": [
"# Battery Data"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a08806c0",
"metadata": {},
"source": [
"## Introduction\n",
"\n",
"Battery data can be recorded by many devices, including smartphones and watches. Battery information can provide insight about activity and sleep patterns. It is also necessary to interpret other data recorded by the device.\n",
"\n",
"Battery data includes information about when the device is shut down, if applicable.\n",
"\n",
"Battery data is expected to have the following columns (column names can be different, but in that case they must be provided as parameters):\n",
"- `user`: Subject ID\n",
"- `device`: Device ID\n",
"- `battery_level`: The charge level if the device as percentage of full charge.\n",
"- `battery_status`: Integer indicator of device status. Fully documented at https://awareframework.com/battery/.\n",
" - Status `2`: charging\n",
" - Status `3`: discharging\n",
" - Status `5`: battery full\n",
" - Status `4`: not charging\n",
" - Status `1`: unknown\n",
" - Status `-1`: shutdown\n",
" - Status `-2`: rebooted\n",
"\n",
"\n",
"In this notebook, we will extract battery data from the Aware platform and infer users' behavioral patterns from their interaction with the phone. The below functions will be described in this notebook:\n",
"\n",
"- `niimpy.preprocessing.battery.battery_shutdown_info`: returns the timestamp when the device is shutdown or rebooted\n",
"- `niimpy.preprocessing.battery.battery_occurrences`: returns the number of battery samples within a time range\n",
"- `niimpy.preprocessing.battery.battery_gaps`: returns the time gaps between two battery sample\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e2081f66",
"metadata": {},
"source": [
"## Read data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "35cad0fc",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/u/24/rantahj1/unix/miniconda3/envs/niimpy/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import niimpy\n",
"import niimpy.preprocessing.battery as battery\n",
"from niimpy import config\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a6082b29",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user | \n",
" device | \n",
" time | \n",
" battery_level | \n",
" battery_status | \n",
" battery_health | \n",
" battery_adaptor | \n",
" datetime | \n",
"
\n",
" \n",
" \n",
" \n",
" 2020-01-09 02:20:02.924999952+02:00 | \n",
" jd9INuQ5BBlW | \n",
" 3p83yASkOb_B | \n",
" 1.578529e+09 | \n",
" 74 | \n",
" 3 | \n",
" 2 | \n",
" 0 | \n",
" 2020-01-09 02:20:02.924999952+02:00 | \n",
"
\n",
" \n",
" 2020-01-09 02:21:30.405999899+02:00 | \n",
" jd9INuQ5BBlW | \n",
" 3p83yASkOb_B | \n",
" 1.578529e+09 | \n",
" 73 | \n",
" 3 | \n",
" 2 | \n",
" 0 | \n",
" 2020-01-09 02:21:30.405999899+02:00 | \n",
"
\n",
" \n",
" 2020-01-09 02:24:12.805999994+02:00 | \n",
" jd9INuQ5BBlW | \n",
" 3p83yASkOb_B | \n",
" 1.578529e+09 | \n",
" 72 | \n",
" 3 | \n",
" 2 | \n",
" 0 | \n",
" 2020-01-09 02:24:12.805999994+02:00 | \n",
"
\n",
" \n",
" 2020-01-09 02:35:38.561000109+02:00 | \n",
" jd9INuQ5BBlW | \n",
" 3p83yASkOb_B | \n",
" 1.578530e+09 | \n",
" 72 | \n",
" 2 | \n",
" 2 | \n",
" 0 | \n",
" 2020-01-09 02:35:38.561000109+02:00 | \n",
"
\n",
" \n",
" 2020-01-09 02:35:38.953000069+02:00 | \n",
" jd9INuQ5BBlW | \n",
" 3p83yASkOb_B | \n",
" 1.578530e+09 | \n",
" 72 | \n",
" 2 | \n",
" 2 | \n",
" 2 | \n",
" 2020-01-09 02:35:38.953000069+02:00 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user device time \\\n",
"2020-01-09 02:20:02.924999952+02:00 jd9INuQ5BBlW 3p83yASkOb_B 1.578529e+09 \n",
"2020-01-09 02:21:30.405999899+02:00 jd9INuQ5BBlW 3p83yASkOb_B 1.578529e+09 \n",
"2020-01-09 02:24:12.805999994+02:00 jd9INuQ5BBlW 3p83yASkOb_B 1.578529e+09 \n",
"2020-01-09 02:35:38.561000109+02:00 jd9INuQ5BBlW 3p83yASkOb_B 1.578530e+09 \n",
"2020-01-09 02:35:38.953000069+02:00 jd9INuQ5BBlW 3p83yASkOb_B 1.578530e+09 \n",
"\n",
" battery_level battery_status \\\n",
"2020-01-09 02:20:02.924999952+02:00 74 3 \n",
"2020-01-09 02:21:30.405999899+02:00 73 3 \n",
"2020-01-09 02:24:12.805999994+02:00 72 3 \n",
"2020-01-09 02:35:38.561000109+02:00 72 2 \n",
"2020-01-09 02:35:38.953000069+02:00 72 2 \n",
"\n",
" battery_health battery_adaptor \\\n",
"2020-01-09 02:20:02.924999952+02:00 2 0 \n",
"2020-01-09 02:21:30.405999899+02:00 2 0 \n",
"2020-01-09 02:24:12.805999994+02:00 2 0 \n",
"2020-01-09 02:35:38.561000109+02:00 2 0 \n",
"2020-01-09 02:35:38.953000069+02:00 2 2 \n",
"\n",
" datetime \n",
"2020-01-09 02:20:02.924999952+02:00 2020-01-09 02:20:02.924999952+02:00 \n",
"2020-01-09 02:21:30.405999899+02:00 2020-01-09 02:21:30.405999899+02:00 \n",
"2020-01-09 02:24:12.805999994+02:00 2020-01-09 02:24:12.805999994+02:00 \n",
"2020-01-09 02:35:38.561000109+02:00 2020-01-09 02:35:38.561000109+02:00 \n",
"2020-01-09 02:35:38.953000069+02:00 2020-01-09 02:35:38.953000069+02:00 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = niimpy.read_csv(config.MULTIUSER_AWARE_BATTERY_PATH, tz='Europe/Helsinki')\n",
"data.head()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a882ebae",
"metadata": {},
"source": [
"## Feature extraction\n",
"\n",
"By default, Niimpy data should be ordered by the timestamp in ascending order. We start by sorting the data to make sure it's compatible."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "198de765",
"metadata": {},
"outputs": [],
"source": [
"data = data.sort_index()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "de6a1c23",
"metadata": {},
"source": [
"Next, we will use Niimpy to extract features from the data. These are useful for inspecting the data and can be part of a full analysis workflow.\n",
"\n",
"Usin the `battery_occurrences` function, we can count the amount the battery samples every 10 minutes. This function requires the index to be sorted."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fdf4a04c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" device | \n",
" user | \n",
" occurrences | \n",
"
\n",
" \n",
" \n",
" \n",
" 2020-01-09 02:20:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 3 | \n",
"
\n",
" \n",
" 2020-01-09 02:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 5 | \n",
"
\n",
" \n",
" 2020-01-09 02:40:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 6 | \n",
"
\n",
" \n",
" 2020-01-09 02:50:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 6 | \n",
"
\n",
" \n",
" 2020-01-09 03:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 5 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2019-08-08 23:50:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 6 | \n",
"
\n",
" \n",
" 2019-08-09 00:00:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 6 | \n",
"
\n",
" \n",
" 2019-08-09 00:10:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 6 | \n",
"
\n",
" \n",
" 2019-08-09 00:20:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 6 | \n",
"
\n",
" \n",
" 2019-08-09 00:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 4 | \n",
"
\n",
" \n",
"
\n",
"
1169 rows × 3 columns
\n",
"
"
],
"text/plain": [
" device user occurrences\n",
"2020-01-09 02:20:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 3\n",
"2020-01-09 02:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 5\n",
"2020-01-09 02:40:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 6\n",
"2020-01-09 02:50:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 6\n",
"2020-01-09 03:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 5\n",
"... ... ... ...\n",
"2019-08-08 23:50:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a 6\n",
"2019-08-09 00:00:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a 6\n",
"2019-08-09 00:10:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a 6\n",
"2019-08-09 00:20:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a 6\n",
"2019-08-09 00:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a 4\n",
"\n",
"[1169 rows x 3 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"battery.battery_occurrences(data, resample_args = {\"rule\": \"10min\"})"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "eea1e17a",
"metadata": {},
"source": [
"The above dataframe gives the battery information of all users. You can also get the information for an individual by passing a filtered dataframe."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b4268096",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" {'resample_args': {'rule': '10min'}}\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" device | \n",
" user | \n",
" occurrences | \n",
"
\n",
" \n",
" \n",
" \n",
" 2020-01-09 02:20:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 3 | \n",
"
\n",
" \n",
" 2020-01-09 02:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 5 | \n",
"
\n",
" \n",
" 2020-01-09 02:40:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 6 | \n",
"
\n",
" \n",
" 2020-01-09 02:50:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 6 | \n",
"
\n",
" \n",
" 2020-01-09 03:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" device user occurrences\n",
"2020-01-09 02:20:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 3\n",
"2020-01-09 02:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 5\n",
"2020-01-09 02:40:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 6\n",
"2020-01-09 02:50:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 6\n",
"2020-01-09 03:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = niimpy.preprocessing.battery.battery_occurrences\n",
"data_filtered = data.query('user == \"jd9INuQ5BBlW\"')\n",
"individual_occurrences = battery.extract_features_battery(data_filtered, features={f: {\"resample_args\": {\"rule\": \"10min\"}}})\n",
"individual_occurrences.head()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3a05452d",
"metadata": {},
"source": [
"Next, you can extract the gaps between two consecutive battery samples with the `battery_gaps` function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d9b96878",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" device | \n",
" user | \n",
" battery_gap | \n",
"
\n",
" \n",
" \n",
" \n",
" 2020-01-09 02:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:01:23.293666680 | \n",
"
\n",
" \n",
" 2020-01-09 02:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:02:03.573882355 | \n",
"
\n",
" \n",
" 2020-01-09 03:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:02:09.004461526 | \n",
"
\n",
" \n",
" 2020-01-09 03:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:02:09.319333346 | \n",
"
\n",
" \n",
" 2020-01-09 04:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:00:39.763869565 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2019-08-08 22:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:02:21.165571434 | \n",
"
\n",
" \n",
" 2019-08-08 23:00:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:35.393473687 | \n",
"
\n",
" \n",
" 2019-08-08 23:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:38.987111104 | \n",
"
\n",
" \n",
" 2019-08-09 00:00:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:40.123666670 | \n",
"
\n",
" \n",
" 2019-08-09 00:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:42.400250017 | \n",
"
\n",
" \n",
"
\n",
"
393 rows × 3 columns
\n",
"
"
],
"text/plain": [
" device user \\\n",
"2020-01-09 02:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 02:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 03:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 03:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 04:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"... ... ... \n",
"2019-08-08 22:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-08 23:00:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-08 23:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-09 00:00:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-09 00:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"\n",
" battery_gap \n",
"2020-01-09 02:00:00+02:00 0 days 00:01:23.293666680 \n",
"2020-01-09 02:30:00+02:00 0 days 00:02:03.573882355 \n",
"2020-01-09 03:00:00+02:00 0 days 00:02:09.004461526 \n",
"2020-01-09 03:30:00+02:00 0 days 00:02:09.319333346 \n",
"2020-01-09 04:00:00+02:00 0 days 00:00:39.763869565 \n",
"... ... \n",
"2019-08-08 22:30:00+03:00 0 days 00:02:21.165571434 \n",
"2019-08-08 23:00:00+03:00 0 days 00:01:35.393473687 \n",
"2019-08-08 23:30:00+03:00 0 days 00:01:38.987111104 \n",
"2019-08-09 00:00:00+03:00 0 days 00:01:40.123666670 \n",
"2019-08-09 00:30:00+03:00 0 days 00:01:42.400250017 \n",
"\n",
"[393 rows x 3 columns]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = niimpy.preprocessing.battery.battery_gaps\n",
"gaps = battery.battery_gaps(data)\n",
"gaps"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "04d5aa8a",
"metadata": {},
"source": [
"Knowing when the phone is shutdown is essential if we want to infer the usage behaviour of the subjects. This can be done by calling the `shutdown_info` function. The function returns the timestamp when the phone is shut down or rebooted (e.g: battery_status = -1)."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8363ca8b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user | \n",
" device | \n",
" time | \n",
" battery_level | \n",
" battery_status | \n",
" battery_health | \n",
" battery_adaptor | \n",
" datetime | \n",
"
\n",
" \n",
" \n",
" \n",
" 2019-08-07 10:37:11.308000088+03:00 | \n",
" dvWdLQesv21a | \n",
" i8jmoIuoe12Mo | \n",
" 1.565163e+09 | \n",
" 2 | \n",
" -1 | \n",
" 2 | \n",
" 0 | \n",
" 2019-08-07 10:37:11.308000088+03:00 | \n",
"
\n",
" \n",
" 2019-08-07 10:37:11.308000088+03:00 | \n",
" iGyXetHE3S8u | \n",
" Cq9vueHh3zVs | \n",
" 1.565163e+09 | \n",
" 2 | \n",
" -1 | \n",
" 2 | \n",
" 0 | \n",
" 2019-08-07 10:37:11.308000088+03:00 | \n",
"
\n",
" \n",
" 2019-08-07 10:37:11.322999954+03:00 | \n",
" dvWdLQesv21a | \n",
" i8jmoIuoe12Mo | \n",
" 1.565163e+09 | \n",
" 2 | \n",
" -1 | \n",
" 2 | \n",
" 0 | \n",
" 2019-08-07 10:37:11.322999954+03:00 | \n",
"
\n",
" \n",
" 2019-08-07 10:37:11.322999954+03:00 | \n",
" iGyXetHE3S8u | \n",
" Cq9vueHh3zVs | \n",
" 1.565163e+09 | \n",
" 2 | \n",
" -1 | \n",
" 2 | \n",
" 0 | \n",
" 2019-08-07 10:37:11.322999954+03:00 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user device \\\n",
"2019-08-07 10:37:11.308000088+03:00 dvWdLQesv21a i8jmoIuoe12Mo \n",
"2019-08-07 10:37:11.308000088+03:00 iGyXetHE3S8u Cq9vueHh3zVs \n",
"2019-08-07 10:37:11.322999954+03:00 dvWdLQesv21a i8jmoIuoe12Mo \n",
"2019-08-07 10:37:11.322999954+03:00 iGyXetHE3S8u Cq9vueHh3zVs \n",
"\n",
" time battery_level \\\n",
"2019-08-07 10:37:11.308000088+03:00 1.565163e+09 2 \n",
"2019-08-07 10:37:11.308000088+03:00 1.565163e+09 2 \n",
"2019-08-07 10:37:11.322999954+03:00 1.565163e+09 2 \n",
"2019-08-07 10:37:11.322999954+03:00 1.565163e+09 2 \n",
"\n",
" battery_status battery_health \\\n",
"2019-08-07 10:37:11.308000088+03:00 -1 2 \n",
"2019-08-07 10:37:11.308000088+03:00 -1 2 \n",
"2019-08-07 10:37:11.322999954+03:00 -1 2 \n",
"2019-08-07 10:37:11.322999954+03:00 -1 2 \n",
"\n",
" battery_adaptor \\\n",
"2019-08-07 10:37:11.308000088+03:00 0 \n",
"2019-08-07 10:37:11.308000088+03:00 0 \n",
"2019-08-07 10:37:11.322999954+03:00 0 \n",
"2019-08-07 10:37:11.322999954+03:00 0 \n",
"\n",
" datetime \n",
"2019-08-07 10:37:11.308000088+03:00 2019-08-07 10:37:11.308000088+03:00 \n",
"2019-08-07 10:37:11.308000088+03:00 2019-08-07 10:37:11.308000088+03:00 \n",
"2019-08-07 10:37:11.322999954+03:00 2019-08-07 10:37:11.322999954+03:00 \n",
"2019-08-07 10:37:11.322999954+03:00 2019-08-07 10:37:11.322999954+03:00 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shutdown = battery.shutdown_info(data, battery_column_name = 'battery_status')\n",
"shutdown"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c153cdf4",
"metadata": {},
"source": [
"## Extracting features with the extract_features call"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "84cc0fdc",
"metadata": {},
"source": [
"We have seen above how to extract battery features using `niimpy`. Sometimes, we need more than one features and it would be inconvenient to extract everything one by one. `niimpy` provides a `extract_feature` call to allow you extracting all the features available and combining them into a single data frame. The extractable features must start with the prefix `battery_`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cae53558",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" {'rule': '10min'}\n",
" {}\n",
" {}\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" device | \n",
" user | \n",
" occurrences | \n",
" battery_gap | \n",
" charge/discharge | \n",
" bdelta | \n",
"
\n",
" \n",
" \n",
" \n",
" 2020-01-09 02:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 3 | \n",
" 0 days 00:01:23.293666680 | \n",
" -0.008794 | \n",
" -0.666667 | \n",
"
\n",
" \n",
" 2020-01-09 02:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 17 | \n",
" 0 days 00:02:03.573882355 | \n",
" 0.014987 | \n",
" 0.764706 | \n",
"
\n",
" \n",
" 2020-01-09 03:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 13 | \n",
" 0 days 00:02:09.004461526 | \n",
" 0.008331 | \n",
" 1.000000 | \n",
"
\n",
" \n",
" 2020-01-09 03:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 15 | \n",
" 0 days 00:02:09.319333346 | \n",
" 0.034008 | \n",
" 0.133333 | \n",
"
\n",
" \n",
" 2020-01-09 04:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 46 | \n",
" 0 days 00:00:39.763869565 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" device user occurrences \\\n",
"2020-01-09 02:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 3 \n",
"2020-01-09 02:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 17 \n",
"2020-01-09 03:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 13 \n",
"2020-01-09 03:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 15 \n",
"2020-01-09 04:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW 46 \n",
"\n",
" battery_gap charge/discharge \\\n",
"2020-01-09 02:00:00+02:00 0 days 00:01:23.293666680 -0.008794 \n",
"2020-01-09 02:30:00+02:00 0 days 00:02:03.573882355 0.014987 \n",
"2020-01-09 03:00:00+02:00 0 days 00:02:09.004461526 0.008331 \n",
"2020-01-09 03:30:00+02:00 0 days 00:02:09.319333346 0.034008 \n",
"2020-01-09 04:00:00+02:00 0 days 00:00:39.763869565 0.000000 \n",
"\n",
" bdelta \n",
"2020-01-09 02:00:00+02:00 -0.666667 \n",
"2020-01-09 02:30:00+02:00 0.764706 \n",
"2020-01-09 03:00:00+02:00 1.000000 \n",
"2020-01-09 03:30:00+02:00 0.133333 \n",
"2020-01-09 04:00:00+02:00 0.000000 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Start by defining the feature name\n",
"f0 = niimpy.preprocessing.battery.battery_occurrences\n",
"f1 = niimpy.preprocessing.battery.battery_gaps\n",
"f2 = niimpy.preprocessing.battery.battery_charge_discharge\n",
"\n",
"# The extract_feature function requires a features parameter. \n",
"# This parameter accepts a dictionary where the key is the feature name and value\n",
"# is a dictionary containing values passed to the function.\n",
"features = battery.extract_features_battery(\n",
" data,\n",
" features={\n",
" f0: {'rule': \"10min\"},\n",
" f1: {},\n",
" f2: {}\n",
" }\n",
")\n",
"features.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9247bcf9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" device | \n",
" user | \n",
" battery_gap | \n",
"
\n",
" \n",
" \n",
" \n",
" 2020-01-09 02:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:01:23.293666680 | \n",
"
\n",
" \n",
" 2020-01-09 02:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:02:03.573882355 | \n",
"
\n",
" \n",
" 2020-01-09 03:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:02:09.004461526 | \n",
"
\n",
" \n",
" 2020-01-09 03:30:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:02:09.319333346 | \n",
"
\n",
" \n",
" 2020-01-09 04:00:00+02:00 | \n",
" 3p83yASkOb_B | \n",
" jd9INuQ5BBlW | \n",
" 0 days 00:00:39.763869565 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2019-08-08 22:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:02:21.165571434 | \n",
"
\n",
" \n",
" 2019-08-08 23:00:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:35.393473687 | \n",
"
\n",
" \n",
" 2019-08-08 23:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:38.987111104 | \n",
"
\n",
" \n",
" 2019-08-09 00:00:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:40.123666670 | \n",
"
\n",
" \n",
" 2019-08-09 00:30:00+03:00 | \n",
" i8jmoIuoe12Mo | \n",
" dvWdLQesv21a | \n",
" 0 days 00:01:42.400250017 | \n",
"
\n",
" \n",
"
\n",
"
393 rows × 3 columns
\n",
"
"
],
"text/plain": [
" device user \\\n",
"2020-01-09 02:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 02:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 03:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 03:30:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"2020-01-09 04:00:00+02:00 3p83yASkOb_B jd9INuQ5BBlW \n",
"... ... ... \n",
"2019-08-08 22:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-08 23:00:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-08 23:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-09 00:00:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"2019-08-09 00:30:00+03:00 i8jmoIuoe12Mo dvWdLQesv21a \n",
"\n",
" battery_gap \n",
"2020-01-09 02:00:00+02:00 0 days 00:01:23.293666680 \n",
"2020-01-09 02:30:00+02:00 0 days 00:02:03.573882355 \n",
"2020-01-09 03:00:00+02:00 0 days 00:02:09.004461526 \n",
"2020-01-09 03:30:00+02:00 0 days 00:02:09.319333346 \n",
"2020-01-09 04:00:00+02:00 0 days 00:00:39.763869565 \n",
"... ... \n",
"2019-08-08 22:30:00+03:00 0 days 00:02:21.165571434 \n",
"2019-08-08 23:00:00+03:00 0 days 00:01:35.393473687 \n",
"2019-08-08 23:30:00+03:00 0 days 00:01:38.987111104 \n",
"2019-08-09 00:00:00+03:00 0 days 00:01:40.123666670 \n",
"2019-08-09 00:30:00+03:00 0 days 00:01:42.400250017 \n",
"\n",
"[393 rows x 3 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f1(data)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "niimpy",
"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.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}