rapids/src/visualization/heatmap_rows.py

63 lines
2.8 KiB
Python
Raw Normal View History

2019-10-25 17:12:55 +02:00
import pandas as pd
2019-12-17 23:07:35 +01:00
import numpy as np
2019-10-25 17:12:55 +02:00
import plotly.io as pio
import plotly.graph_objects as go
import datetime
2019-10-25 17:12:55 +02:00
def getComplianceMatrix(dates, compliance_bins):
compliance_matrix = []
2019-10-25 17:12:55 +02:00
for date in dates:
date_bins = compliance_bins[compliance_bins["local_date"] == date]["count"].tolist()
compliance_matrix.append(date_bins)
return compliance_matrix
2019-10-25 17:12:55 +02:00
2020-02-07 17:14:19 +01:00
def getRowCountHeatmap(dates, row_count_per_bin, sensor_name, pid, output_path, bin_size):
bins_per_hour = int(60 / bin_size)
x_axis_labels = ["{0:0=2d}".format(x // bins_per_hour) + ":" + \
"{0:0=2d}".format(x % bins_per_hour * bin_size) for x in range(24 * bins_per_hour)]
plot = go.Figure(data=go.Heatmap(z=row_count_per_bin,
x=x_axis_labels,
y=[datetime.datetime.strftime(date, '%Y/%m/%d') for date in dates],
2020-02-07 17:14:19 +01:00
colorscale="Viridis"))
2019-11-11 21:03:56 +01:00
plot.update_layout(title="Row count heatmap for " + sensor_name + " of " + pid)
2019-10-25 17:12:55 +02:00
pio.write_html(plot, file=output_path, auto_open=False)
2019-12-16 17:13:32 +01:00
sensor_data = pd.read_csv(snakemake.input[0], encoding="ISO-8859-1")
2019-10-25 17:12:55 +02:00
sensor_name = snakemake.params["table"]
pid = snakemake.params["pid"]
2020-02-07 17:14:19 +01:00
bin_size = snakemake.params["bin_size"]
# check if we have sensor data
if sensor_data.empty:
empty_html = open(snakemake.output[0], "w")
empty_html.write("There is no "+ sensor_name + " data for "+pid)
empty_html.close()
else:
start_date = sensor_data["local_date"][0]
end_date = sensor_data.at[sensor_data.index[-1],"local_date"]
2020-02-07 17:14:19 +01:00
sensor_data["local_date_time"] = pd.to_datetime(sensor_data["local_date_time"])
sensor_data = sensor_data[["local_date_time"]]
sensor_data["count"] = 1
# Add first and last day boundaries for resampling
2020-02-07 17:14:19 +01:00
sensor_data = sensor_data.append([pd.Series([datetime.datetime.strptime(start_date + " 00:00:00", "%Y-%m-%d %H:%M:%S"), 0], sensor_data.columns),
pd.Series([datetime.datetime.strptime(end_date + " 23:59:59", "%Y-%m-%d %H:%M:%S"), 0], sensor_data.columns)])
2020-02-07 17:14:19 +01:00
# Resample into bins with the size of bin_size
resampled_bins = pd.DataFrame(sensor_data.resample(str(bin_size) + "T", on="local_date_time")["count"].sum())
# Extract list of dates for creating the heatmap
2020-02-07 17:14:19 +01:00
resampled_bins.reset_index(inplace=True)
resampled_bins["local_date"] = resampled_bins["local_date_time"].dt.date
dates = resampled_bins["local_date"].drop_duplicates().tolist()
# Create heatmap
2020-02-07 17:14:19 +01:00
row_count_per_bin = getComplianceMatrix(dates, resampled_bins)
row_count_per_bin = np.asarray(row_count_per_bin)
row_count_per_bin = np.where(row_count_per_bin == 0, np.nan, row_count_per_bin)
getRowCountHeatmap(dates, row_count_per_bin, sensor_name, pid, snakemake.output[0], bin_size)