Skip to content

Instantly share code, notes, and snippets.

@rdmtinez
Last active October 12, 2017 13:50
Show Gist options
  • Save rdmtinez/a9d043ccb34415ac428b9164e3af451d to your computer and use it in GitHub Desktop.
Save rdmtinez/a9d043ccb34415ac428b9164e3af451d to your computer and use it in GitHub Desktop.
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
wdir = "1-Humidity/python/condensed_CSVs/"
os.chdir(wdir+'basic/')
basic_list = os.listdir(wdir+'basic')
premium_list = os.listdir(wdir+'premium')
#subsample each dataframe for high and low values per day
#subsample dataframe for
for csv in basic_list[0:2]:
with open(csv) as f:
df = pd.read_csv(f)
df.head()
df['DateTime'] = pd.to_datetime(df['DateTime'])
#DataFrames with different indexing schemes
#df_timeID = df.set_index(['DateTime'])
#df_timeMonthID = df.set_index(['DateTime', 'Month'])
#df_monthID = df.set_index(['Month'])
#df_weekID = df.set_index(['Week'])
df_monthWeekID = df.set_index(['Month', 'Week',])
df_monthWeekDayID = df.set_index(['Month', 'Week', 'Day'])
#df_weekDayID = df.set_index(['Week', 'Day'])
daylight = {'Jan':[('08:17', '16:03'),('07:47', '16:52')],
'Feb':[('07:47', '16:52'),('06:52', '17:45')],
'Mar':[('06:52', '17:45'),('06:40', '19:40')],
'Apr':[('06:40', '19:40'),('05:35', '20:33')],
'May':[('05:35', '20:33'),('04:49', '21:19')],
'Jun':[('04:49', '21:19'),('04:48', '21:32')],
'Jul':[('04:48', '21:32'),('05:26', '20:58')],
'Aug':[('05:26', '20:58'),('06:17', '19:54')],
'Sep':[('06:17', '19:54'),('07:07', '18:43')],
'Oct':[('07:07', '18:43'),('07:02', '16:36')],
'Nov':[('07:02', '16:36'),('07:54', '15:56')],
'Dec':[('07:54', '15:56'),('08:17', '16:01')]}
mon_list = list(df['Month'].unique())
df_timeID.between_time(daylight['Apr'][0][0], daylight['Apr'][0][1])
mon_week_dic = {}
for m in mon_list:
if m not in mon_week_dic:
mon_week_dic[m] = []
'April'
df_monthweekID.loc['Apr'].plot(x='DateTime', y=['VWC','dVWC'], figsize=(16,9))
mon_week_dic[m] = list(df_monthID.loc[m]['Week'].unique())
break
"""'df_monthweekID'
df_monthweekID.head()
df_monthweekID.loc['Apr':'May']['DateTime']
#To Make Monthly Graph
mon_list = list(df['Month'].unique())
#to make Month weekly graph
mon_week_dic = {}
for m in mon_list:
if m not in mon_week_dic:
mon_week_dic[m] = []
'April'
df_monthweekID.loc['Apr'].plot(x='DateTime', y=['VWC','dVWC'], figsize=(16,9))
mon_week_dic[m] = list(df_monthID.loc[m]['Week'].unique()"""
df_monthWeekID.sort_index(inplace=True)
df_monthWeekDayID.sort_index(inplace=True)
'April'
#df_monthweekID.loc['Apr':'May'].plot(x='DateTime', y=['VWC','dVWC'], figsize=(16,9))
#df_monthweekID.loc['Apr',15].plot(x='DateTime', y=['VWC', 'dVWC'], figsize=(16,9))
#df_monthWeekDayID = df.set_index(['Month', 'Week', 'Day'])
df_monthWeekDayID.head()
#days in 'April_15' -> 10-to-16
#give 'DateTime' where the following hold true
df_monthWeekDayID.loc['Apr', 15, 10]['VWC'].max()
df_monthWeekDayID.loc['Apr', 15, 10]['VWC'].min()
#plot daily mix and max dVWC for month
"""
'May'
df_monthWeekID.loc['May', 15].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
'Jun'
df_monthWeekID.loc['Jun', 15].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
'Jul'
df_monthWeekID.loc['Jul', 15].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))"""
m='Apr'
w=15
d=10
'April'
df_monthWeekID.loc['Apr', 15].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
df_monthWeekID.loc['Apr', 15].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
df_monthWeekID.loc['Apr', 16].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
df_monthWeekID.loc['Apr', 17].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
'April'
df_monthWeekID.loc['Apr', 15].plot(x='DateTime', y=['VWC','dVWC'], figsize=(20,4))
'M:April, W:15, D:10'
df_monthWeekDayID.loc[m, w, d].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
df_monthWeekDayID.loc[m, w, d+1].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
df_monthWeekDayID.loc[m, w, d+2].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
df_monthWeekDayID.loc[m, w, d+3].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
df_monthWeekDayID.loc[m, w, d+4].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
df_monthWeekDayID.loc[m, w, d+5].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
df_monthWeekDayID.loc[m, w, d+6].plot(x='DateTime', y=['VWC','dVWC'], figsize=(8,3))
plt.show()
#get unique list of months for each instrument
mon_list = list(df['Month'].unique())
#save to a list which you can access and update
###BOOLEAN MASK
#for each month get a unique list of weeks
mon_week_dic = {}
for m in mon_list:
if m not in mon_week_dic:
mon_week_dic[m] = []
mon_week_dic[m] = list(df_monthID.loc[m]['Week'].unique())
mon_list
mon_week_dic
#for each week get a list of the days
#for each day and within the daylight hours
for m in mon_list:
#plot the min, +3points, median, +3 points, max values
#this returns a new DF with rows satisfying this
df_timeID.between_time(daylight['Apr'][0][0], daylight['Apr'][0][1])
#for each month in a CSV
# OR
#for each week in a CSV
#using the daylight hours values
#create a new dataframe
#plot these daylight values
#note that some of these days will have some irrigation events
#find the irrigation event...
#these are probably the sharp spikes in dVWC
#plot the daylight plots against the dVWC plots
"""df.sort_index(inplace=True)
idx = pd.IndexSlice
#dfmi.loc[idx[:, :, ['C1', 'C3']], idx[:, 'foo']]
#time_mask = df['TimeStamp']
#df.loc[idx[ID,weekID,dayID], s_col:e_col]
df.loc[idx[2,14,], :].plot(x='TimeStamp', y='Analog_Voltage')
df.loc[idx[]]
plt.show()
"""
#daylight hours between the the first and last days of each month
daylight = {'Jan':[('08:17', '16:03'),('07:47', '16:52')],
'Feb':[('07:47', '16:52'),('06:52', '17:45')],
'Mar':[('06:52', '17:45'),('06:40', '19:40')],
'Apr':[('06:40', '19:40'),('05:35', '20:33')],
'May':[('05:35', '20:33'),('04:49', '21:19')],
'Jun':[('04:49', '21:19'),('04:48', '21:32')],
'Jul':[('04:48', '21:32'),('05:26', '20:58')],
'Aug':[('05:26', '20:58'),('06:17', '19:54')],
'Sep':[('06:17', '19:54'),('07:07', '18:43')],
'Oct':[('07:07', '18:43'),('07:02', '16:36')],
'Nov':[('07:02', '16:36'),('07:54', '15:56')],
'Dec':[('07:54', '15:56'),('08:17', '16:01')]}
print(daylight['Apr'][0][0],daylight['Apr'][0][1])
#for the week select the values that are in the daylight hours
#df_monthweekID.loc['Apr',14].head()
#df_monthweekID.xs(('Apr',16:18)).plot(x='DateTime', y='VWC')
#df_monthweekID.loc['Apr',14]
#f1=df_monthweekID.loc['Apr',14].plot(x='DateTime', y='VWC')
#f2=df_monthweekID.loc['Apr',14].plot(x='DateTime', y='dVWC')
#df_monthweekID.loc['Apr',15].plot(x='DateTime', y='VWC')
#df_monthweekID.loc['Apr',16].plot(x='DateTime', y='VWC')
#plt.show()
#df_monthweekID.loc['Apr',17].plot(x='DateTime', y='VWC')
#df_monthweekID.loc['Apr'].plot(x='DateTime', y='VWC', )
#"""x=df_monthweekID.loc['Apr',16]['DateTime']
#y=df_monthweekID.loc['Apr',16]['VWC']
#y2=df_monthweekID.loc['Apr',16]['dVWC']
#plt.plot()
#apr_14=df_monthweekID.loc['Apr',14].plot('DateTime', 'VWC', figsize=(12, 3))
df.sort_index(inplace=True)
idx = pd.IndexSlice
#dfmi.loc[idx[:, :, ['C1', 'C3']], idx[:, 'foo']]
#time_mask = df['TimeStamp']
#df.loc[idx[ID,weekID,dayID], s_col:e_col]
df.loc[idx[2,14], :].plot(x='TimeStamp', y='Analog_Voltage')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment