2020-12-01 20:21:33 +01:00
import pandas as pd
import plotly . express as px
2021-06-17 18:27:32 +02:00
time_segments_type = snakemake . params [ " time_segments_type " ]
2020-12-01 20:21:33 +01:00
phone_data_yield = pd . read_csv ( snakemake . input [ 0 ] )
2021-06-17 18:27:32 +02:00
if time_segments_type == " FREQUENCY " :
phone_data_yield [ " local_segment_label " ] = phone_data_yield [ " local_segment_label " ] . str . split ( " \ d+ " , expand = True , n = 1 ) [ 0 ]
if time_segments_type == " EVENT " :
phone_data_yield [ " local_segment_label " ] = " event "
2020-12-01 20:21:33 +01:00
# make sure the input file contains "phone_data_yield_rapids_ratiovalidyieldedminutes" and "phone_data_yield_rapids_ratiovalidyieldedhours" columns
if ( " phone_data_yield_rapids_ratiovalidyieldedminutes " not in phone_data_yield . columns ) or ( " phone_data_yield_rapids_ratiovalidyieldedhours " not in phone_data_yield . columns ) :
raise ValueError ( " Please make sure [PHONE_DATA_YIELD][RAPIDS][COMPUTE] is True AND [PHONE_DATA_YIELD][RAPIDS][FEATURES] contains [ratiovalidyieldedminutes, ratiovalidyieldedhours]. " )
2020-12-04 03:00:32 +01:00
html_file = open ( snakemake . output [ 0 ] , " a " , encoding = " utf-8 " )
if phone_data_yield . empty :
html_file . write ( " There is no sensor data for the sensors in [PHONE_DATA_YIELD][SENSORS]. " )
else :
# plot ratio valid yielded minutes histogram
fig_ratiovalidyieldedminutes = px . histogram ( phone_data_yield , x = " phone_data_yield_rapids_ratiovalidyieldedminutes " , color = " local_segment_label " )
fig_ratiovalidyieldedminutes . update_layout ( title = " Histogram of valid yielded minutes ratio per time segment. " )
2020-12-01 20:21:33 +01:00
html_file . write ( fig_ratiovalidyieldedminutes . to_html ( full_html = False , include_plotlyjs = " cdn " ) )
2020-12-04 03:00:32 +01:00
# plot ratio valid yielded hours histogram
fig_ratiovalidyieldedhours = px . histogram ( phone_data_yield , x = " phone_data_yield_rapids_ratiovalidyieldedhours " , color = " local_segment_label " )
fig_ratiovalidyieldedhours . update_layout ( title = " Histogram of valid yielded hours ratio per time segment. " )
2020-12-01 20:21:33 +01:00
html_file . write ( fig_ratiovalidyieldedhours . to_html ( full_html = False , include_plotlyjs = " cdn " ) )
2020-12-04 03:00:32 +01:00
html_file . close ( )