diff --git a/exploration/screen.ipynb b/exploration/screen.ipynb index 043933a..c69682c 100644 --- a/exploration/screen.ipynb +++ b/exploration/screen.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 11, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -14,46 +14,100 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "import pandas as pd\n", - "\n", - "from config.models import Participant, Screen\n", - "from setup import db_engine, session" + "from features.screen import *\n", + "import participants.query_db" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "query_screen = (\n", - " 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)" + "df_screen_nokia = get_screen_data([\"nokia_0000003\"])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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": [ - "print(df_screen)" + "print(df_screen_nokia)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "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": { @@ -72,7 +126,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.9.2" } }, "nbformat": 4, diff --git a/features/screen.py b/features/screen.py new file mode 100644 index 0000000..facdb0c --- /dev/null +++ b/features/screen.py @@ -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