papermill_step1   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 34
dl 0
loc 78
rs 10
c 0
b 0
f 0
1
# ---
2
# jupyter:
3
#   jupytext:
4
#     cell_metadata_json: true
5
#     formats: ipynb,py:percent
6
#     notebook_metadata_filter: language_info
7
#     text_representation:
8
#       extension: .py
9
#       format_name: percent
10
#       format_version: '1.3'
11
#       jupytext_version: 1.5.2
12
#   kernelspec:
13
#     display_name: Python 3
14
#     language: python
15
#     name: python3
16
#   language_info:
17
#     codemirror_mode:
18
#       name: ipython
19
#       version: 3
20
#     file_extension: .py
21
#     mimetype: text/x-python
22
#     name: python
23
#     nbconvert_exporter: python
24
#     pygments_lexer: ipython3
25
#     version: 3.6.6
26
# ---
27
28
# %% {"tags": ["parameters"]}
29
# Our default parameters
30
# This cell has a "parameters" tag, means that it defines the parameters for use in the notebook
31
run_date = "2018-04-28"
32
source_id = 'sensor1'
33
34
# %%
35
import matplotlib.pyplot as plt
36
import matplotlib.dates as mdates
37
import numpy as np
38
import pandas as pd
39
import scrapbook as sb
40
from datetime import datetime, timedelta
41
import time
42
import os
43
plt.ioff()
44
45
# %%
46
run_datetime = datetime.strptime(run_date, '%Y-%m-%d')
47
ts = pd.date_range("00:00", "23:59", freq="5min")
48
td = ts - timedelta((datetime.now() - run_datetime).days)
49
data = pd.DataFrame(np.random.randn(len(td)), columns=['mydata'])
50
data = data.rolling(70, min_periods=1, center=True).mean()  # Smooth it so it looks purdy
51
data['date'] = td
52
data['hour'] = data['date'].apply(lambda x: datetime.strftime(x, "%H"))
53
54
# %%
55
print(data['date'].describe())
56
data.describe()
57
58
# %%
59
data = data.sort_values('date').set_index('date', drop=True)
60
data.head(5)
61
62
# %%
63
fig, ax = plt.subplots()
64
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
65
plt.gcf().autofmt_xdate()
66
ax.plot(data.index, data['mydata'], c='k', alpha=.5)
67
ax.set(title="Activity for the day of {}".format(run_date))
68
sb.glue('activity_day_fig', fig, display=True)
69
70
# %%
71
month_partition = run_datetime.strftime("%Y-%m")
72
output_file = "../data/output/step1/" + month_partition + "/" + run_date + '-' + source_id + '.csv'
73
print(output_file)
74
75
# %%
76
os.makedirs(os.path.dirname(output_file), exist_ok=True)
77
data.to_csv(output_file)
78
79
80