Last active
September 18, 2021 01:53
-
-
Save TakehikoShimojima/f947732cd86516fc0de2d921b5fe91b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# 箱ひげ図を描画するために、最小値、第1四分位数、中央値、第3四分位数、最大値を計算し、 | |
# Ambientに書き戻す | |
# python3 quantile.py chIn readKeyIn chOut writeKeyOut field date | |
# YYYY-mm-dd 形式で指定した日のfield(d1〜d8のいずれか)に対して値を計算し、d1〜d5に書く | |
# {'d1': 最小値, 'd2': 第1四分位数, 'd3': 中央値, 'd4: 第3四分位数, 'd5': 最大値} | |
import ambient | |
import numpy as np | |
import pandas as pd | |
from datetime import datetime, timedelta | |
import sys | |
if len(sys.argv) != 7: | |
print('\'python3 quantile.py chIn readKeyIn chOut writeKeyOut field date\'') | |
sys.exit(1) | |
chIn = sys.argv[1] | |
readKeyIn = sys.argv[2] | |
chOut = sys.argv[3] | |
writeKeyOut = sys.argv[4] | |
amIn = ambient.Ambient(chIn, '', readKeyIn) | |
amOut = ambient.Ambient(chOut, writeKeyOut) | |
field = sys.argv[5] | |
if field not in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8']: | |
print('フィールドはd1からd8のいずれか') | |
sys.exit(1) | |
try: | |
date = datetime.strptime(sys.argv[6], '%Y-%m-%d') | |
except ValueError as e: | |
print('引数形式エラー: python3 %s chIn readKeyIn chOut writeKeyOut field YYYY-mm-dd' % sys.argv[0]); | |
sys.exit(1) | |
d = amIn.read(date = date.strftime('%Y-%m-%d')) | |
df = pd.DataFrame(d) | |
df['created'] = pd.to_datetime(list(df['created'])).tz_convert('Asia/Tokyo') | |
df = df.set_index('created') | |
q = df[field].quantile([0, 0.25, 0.5, 0.75, 1.0]) | |
data = {'created': date.strftime('%Y-%m-%d 23:59:59')} | |
for i, j in enumerate([0, 0.25, 0.5, 0.75, 1.0]): | |
data['d' + str(i + 1)] = q[j] | |
print(data) | |
r = amOut.send(data) | |
print(r.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment