In [3]:
import numpy as np
from plotly.offline import *
from plotly.graph_objs import *
import datetime
import pandas as pd
In [4]:
# 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 )]
In [5]:
# show csv file column names with first lines
df.head()
Out[5]:
Unnamed: 0 time light hum press temp tempd vocco2 vocvoc dust datetime
1 1496836800 1496836800 44 35.36 97071 23 3 310.5 489 0.1020 2017-06-07 15:00:00
2 1496837400 1496837400 76 35.66 38444 20 4 339.0 251 0.1085 2017-06-07 15:10:00
3 1496838000 1496838000 82 35.96 35639 20 3 345.0 297 0.1130 2017-06-07 15:20:00
4 1496838600 1496838600 51 36.26 30055 23 2 355.5 540 0.1205 2017-06-07 15:30:00
5 1496839200 1496839200 9 36.56 87941 18 3 370.5 131 0.1285 2017-06-07 15:40:00
In [6]:
# read data from csv file
timestamps_10 = df['time'].values
dust = df['dust'].values
In [7]:
# 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)))
In [8]:
# 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)
In [9]:
# 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
In [10]:
# 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
In [11]:
# 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')
In [12]:
# 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)
In [ ]: