Get screen data and look at counts of events.

communication
junos 2021-05-04 17:45:48 +02:00
parent f37e8dc0eb
commit db66d2201b
2 changed files with 106 additions and 20 deletions

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -14,46 +14,100 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import pandas as pd\n", "from features.screen import *\n",
"\n", "import participants.query_db"
"from config.models import Participant, Screen\n",
"from setup import db_engine, session"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"query_screen = (\n", "df_screen_nokia = get_screen_data([\"nokia_0000003\"])"
" session.query(Screen, Participant.username)\n",
" .filter(Participant.id == Screen.participant_id)\n",
" .filter(Participant.username.in_([\"nokia_0000003\"]))\n",
" )\n",
"with db_engine.connect() as connection:\n",
" df_screen = pd.read_sql(query_screen.statement, connection)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" id _id timestamp device_id \\\n",
"0 245456 155 1615456297079 12324354-e195-4e93-a2d5-268556e3ea5d \n",
"1 245455 154 1615456297069 12324354-e195-4e93-a2d5-268556e3ea5d \n",
"2 245454 153 1615456288219 12324354-e195-4e93-a2d5-268556e3ea5d \n",
"3 245453 152 1615455357213 12324354-e195-4e93-a2d5-268556e3ea5d \n",
"4 245452 151 1615455357190 12324354-e195-4e93-a2d5-268556e3ea5d \n",
"... ... ... ... ... \n",
"1911 33221 5 1583329949659 d5fb52e1-7df8-44b5-a805-8d04ca008061 \n",
"1912 33171 4 1583327341863 d5fb52e1-7df8-44b5-a805-8d04ca008061 \n",
"1913 33170 3 1583327340983 d5fb52e1-7df8-44b5-a805-8d04ca008061 \n",
"1914 33169 2 1583327340739 d5fb52e1-7df8-44b5-a805-8d04ca008061 \n",
"1915 33168 1 1583327340713 d5fb52e1-7df8-44b5-a805-8d04ca008061 \n",
"\n",
" screen_status participant_id username \n",
"0 2 21 nokia_0000003 \n",
"1 0 21 nokia_0000003 \n",
"2 1 21 nokia_0000003 \n",
"3 2 21 nokia_0000003 \n",
"4 0 21 nokia_0000003 \n",
"... ... ... ... \n",
"1911 3 21 nokia_0000003 \n",
"1912 3 21 nokia_0000003 \n",
"1913 1 21 nokia_0000003 \n",
"1914 2 21 nokia_0000003 \n",
"1915 0 21 nokia_0000003 \n",
"\n",
"[1916 rows x 7 columns]\n"
]
}
],
"source": [ "source": [
"print(df_screen)" "print(df_screen_nokia)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
"participants_inactive_usernames = participants.query_db.get_usernames()\n",
"df_screen_inactive = get_screen_data(participants_inactive_usernames)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"screen_status\n",
"0 70243\n",
"1 70012\n",
"2 63080\n",
"3 36666\n",
"dtype: int64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_screen_inactive.value_counts(\"screen_status\")"
]
} }
], ],
"metadata": { "metadata": {
@ -72,7 +126,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.1" "version": "3.9.2"
} }
}, },
"nbformat": 4, "nbformat": 4,

32
features/screen.py 100644
View File

@ -0,0 +1,32 @@
from typing import List
import pandas as pd
from config.models import Screen, Participant
from setup import db_engine, session
screen_status = {0: "off", 1: "on", 2: "locked", 3: "unlocked"}
def get_screen_data(usernames: List) -> pd.DataFrame:
"""
Read the data from the screen table and return it in a dataframe.
Parameters
----------
usernames: List
A list of usernames to put into the WHERE condition.
Returns
-------
df_screen: pd.DataFrame
A dataframe of screen data.
"""
query_screen = (
session.query(Screen, Participant.username)
.filter(Participant.id == Screen.participant_id)
.filter(Participant.username.in_(usernames))
)
with db_engine.connect() as connection:
df_screen = pd.read_sql(query_screen.statement, connection)
return df_screen