2021-10-13 16:57:38 +02:00
|
|
|
from collections.abc import Collection
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
2021-10-14 17:59:33 +02:00
|
|
|
from config.models import (
|
|
|
|
Barometer,
|
|
|
|
BarometerSensor,
|
|
|
|
LightSensor,
|
|
|
|
Participant,
|
|
|
|
Temperature,
|
|
|
|
TemperatureSensor,
|
|
|
|
)
|
2021-10-13 16:57:38 +02:00
|
|
|
from setup import db_engine, session
|
|
|
|
|
2021-10-22 18:09:17 +02:00
|
|
|
MINIMUM_PRESSURE_MB = 870
|
|
|
|
# The lowest measurable sea-level pressure is found at the centers of tropical cyclones and tornadoes,
|
|
|
|
# with a record low of 870 mbar (87 kPa; 26 inHg).
|
|
|
|
|
2021-10-13 16:57:38 +02:00
|
|
|
|
|
|
|
def get_ambient_data(usernames: Collection, sensor=None) -> pd.DataFrame:
|
|
|
|
"""
|
|
|
|
Read the data from any of the ambient sensor tables and return it in a dataframe.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
usernames: Collection
|
|
|
|
A list of usernames to put into the WHERE condition.
|
|
|
|
sensor: str
|
|
|
|
One of: barometer, barometer_sensor, light, temperature, temperature_sensor.
|
|
|
|
Here, the _sensor tables describe the phone sensors, such as their range, dela, resolution, vendor etc.,
|
|
|
|
whereas barometer, light, and temperature describe the measured characteristics of the environment.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
df_ambient: pd.DataFrame
|
|
|
|
A dataframe of ambient sensor data.
|
|
|
|
"""
|
|
|
|
if sensor == "barometer":
|
2021-10-14 17:59:33 +02:00
|
|
|
query_ambient = session.query(Barometer, Participant.username).filter(
|
|
|
|
Participant.id == Barometer.participant_id
|
2021-10-13 16:57:38 +02:00
|
|
|
)
|
|
|
|
elif sensor == "barometer_sensor":
|
2021-10-14 17:59:33 +02:00
|
|
|
query_ambient = session.query(BarometerSensor, Participant.username).filter(
|
|
|
|
Participant.id == BarometerSensor.participant_id
|
2021-10-13 16:57:38 +02:00
|
|
|
)
|
|
|
|
elif sensor == "light":
|
2021-10-14 17:59:33 +02:00
|
|
|
query_ambient = session.query(LightSensor, Participant.username).filter(
|
|
|
|
Participant.id == LightSensor.participant_id
|
2021-10-13 16:57:38 +02:00
|
|
|
)
|
|
|
|
# Note that LightSensor and its light_sensor table are incorrectly named.
|
|
|
|
# In this table, we actually find light data, i.e. double_light_lux, the ambient luminance in lux,
|
|
|
|
# and NOT light sensor data (its range, dela, resolution, vendor etc.) as the name suggests.
|
|
|
|
# We do not have light sensor data saved in the database.
|
|
|
|
elif sensor == "temperature":
|
2021-10-14 17:59:33 +02:00
|
|
|
query_ambient = session.query(Temperature, Participant.username).filter(
|
|
|
|
Participant.id == Temperature.participant_id
|
2021-10-13 16:57:38 +02:00
|
|
|
)
|
|
|
|
elif sensor == "temperature_sensor":
|
2021-10-14 17:59:33 +02:00
|
|
|
query_ambient = session.query(TemperatureSensor, Participant.username).filter(
|
|
|
|
Participant.id == TemperatureSensor.participant_id
|
2021-10-13 16:57:38 +02:00
|
|
|
)
|
|
|
|
else:
|
2021-10-14 17:59:33 +02:00
|
|
|
raise KeyError(
|
2021-10-22 18:09:17 +02:00
|
|
|
"Specify one of the ambient sensors: "
|
|
|
|
"barometer, barometer_sensor, light, temperature, or temperature_sensor."
|
2021-10-14 17:59:33 +02:00
|
|
|
)
|
2021-10-13 16:57:38 +02:00
|
|
|
|
|
|
|
query_ambient = query_ambient.filter(Participant.username.in_(usernames))
|
|
|
|
with db_engine.connect() as connection:
|
|
|
|
df_ambient = pd.read_sql(query_ambient.statement, connection)
|
|
|
|
return df_ambient
|
2021-10-22 18:09:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def clean_pressure(df_ambient: pd.DataFrame) -> pd.DataFrame:
|
|
|
|
"""
|
|
|
|
Simply removes values lower than MINIMUM_PRESSURE_MB (lowest measured pressure).
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
df_ambient: pd.DataFrame
|
|
|
|
A dataframe of barometer data, which includes measured pressure in double_values_0.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
df_ambient: pd.DataFrame
|
|
|
|
The same dataframe with rows with low values of pressure removed.
|
|
|
|
"""
|
|
|
|
if "double_values_0" not in df_ambient:
|
|
|
|
raise KeyError("The DF does not seem to hold barometer data.")
|
|
|
|
df_ambient = df_ambient[df_ambient["double_values_0"] > MINIMUM_PRESSURE_MB]
|
|
|
|
return df_ambient
|