import numpy as np
from plotly.offline import *
from plotly.graph_objs import *
import datetime
import pandas as pd
# read csv file
df_whole = pd.read_csv('mockupdata.csv', sep=",")
# start_time = 1496836200 = 06/07/2017 @ 11:50am
hour = 3600
start_time = 1496836200
end_time = start_time + 28800 # 8h
df = df_whole[(df_whole['time'] < end_time ) & (df_whole['time'] > start_time )]
# show csv file column names with first lines
df.head()
# read data from csv file
timestamps_10 = df['time'].values
dust = df['dust'].values
# calculate data in interval 1 hours
# hourly time
timestamps = timestamps_10[::6]
# other values
# create hourly lists
def slice(l, n):
for i in range(0, len(l), n):
slices = yield l[i:i + n]
return slices
# calculate hourly average
def avg(values):
avgs = []
for val in values:
avg = sum(val) / len(val)
avgs.append(avg)
return avgs
# calculate values
dust = avg(list(slice(dust, 6)))
# transfer timestamps to datetime
end_timestamp = timestamps[7]
start_timestamp = timestamps[0]
def timestamp_to_datetime(timestamps):
dates = []
for timestamp in timestamps:
date = datetime.datetime.fromtimestamp(
int(timestamp)
).strftime('%Y-%m-%d %H:%M:%S')
dates.append(str(date))
return dates
dates = timestamp_to_datetime(timestamps)
# calculating trendline values
# creating prediction times
def create_times(start_timestamp):
time_prediction = []
pre_end_time = start_timestamp + 43200 #12h
for x in range (start_timestamp, pre_end_time, 3600):
time_prediction.append(x)
return time_prediction
def trendline(time, score):
x = np.array(time)
y = np.array(score)
#m0, m, b = np.polyfit(x, y, 2)
m, b = np.polyfit(x, y, 1)
# calculating prediction values
score_prediction = []
pre_end_time = start_timestamp + 43200 #12h
for x in range (start_timestamp, pre_end_time, 3600):
#y = m0 * x ** 2 + m * x + b
y = m*x + b
score_prediction.append(y)
dates_prediction = timestamp_to_datetime(create_times(start_timestamp))
return dates_prediction, score_prediction
# plot creation
# create custom colors for bars
red = 'rgb(222,0,0)'
dark_green = 'rgb(0,153,51)'
orange = 'rgb(255,153,51)'
green = 'rgb(51,204,51)'
light_green = 'rgb(204,255,153)'
yellow = 'rgb(255,255,0)'
def make_colors_dust(scores):
clrs = []
for score in scores:
if float(score) < 0.03:
color = dark_green
elif float(score) < 0.09:
color = green
elif float(score) < 0.18:
color = light_green
elif float(score) < 0.36:
color = yellow
elif float(score) < 0.51:
color = orange
else:
color = red
clrs.append(color)
return clrs
# plotly
# use offline version
init_notebook_mode(connected=True)
def create_y(val):
y_vals = []
for i in range(0, 12):
y_vals.append(val)
return y_vals
# make plot
def create_plot(x_known, y_known, x_predicted, y_predicted, title, yaxis, min, max):
known = Bar(
x = x_known,
y = y_known,
name='Measured attributes',
marker=dict(color = make_colors_dust(y_known))
)
predicted = Scatter(
x = x_predicted,
y = y_predicted,
name='Predicted',
line = dict(
color = ('#79c2c9'))
)
data = [known, predicted]
layout = dict(title = title,
xaxis = dict(title = 'time, h'),
yaxis = dict(title = yaxis,
range=[min, max]),
)
return iplot(dict(data=data, layout=layout), filename='basic-bar')
# Dust measurement
colors = make_colors_dust(dust)
dates_prediction, dust_prediction = trendline(timestamps, dust)
create_plot(dates, dust, dates_prediction, dust_prediction, 'Dust level', 'Dust level, mg/m3', 0, 0.6)