In [2]:
#the usual beginning
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from pandas import Series, DataFrame, concat
#define any string with 'C' as NaN. 'C' is an indicator for the Masimo as a low signal reading
def readD(val):
if 'C' in val:
return np.nan
return val
In [17]:
# MAKE SURE YOU CHANGE THIS!!!
#
#
#
BabyNumber = 'ROP018'
In [4]:
dfNIRSpre0 = pd.read_csv('/Users/John/Dropbox/LLU/ROP/NIRS/Clean/' + BabyNumber + 'NIRS.csv',
parse_dates={'timestamp': ['Date',' Time']},
index_col='timestamp',
usecols=['Date', ' Time', ' Ch2 %StO2'],
na_values=['0'])
dfPOpre = pd.read_csv('/Users/John/Dropbox/LLU/ROP/Pulse Ox/' + BabyNumber + 'PO.csv',
parse_dates={'timestamp': ['Date','Time']},
index_col='timestamp',
usecols=['Date', 'Time', 'SpO2', 'PR', 'PI', 'Exceptions'],
na_values=['0'],
converters={'Exceptions': readD}
)
dfNIRSpre = dfNIRSpre0.rename(columns={' Ch2 %StO2': 'StO2'}) #rename NIRS column
#parse_dates tells the read_csv function to combine the date and time column
#into one timestamp column and parse it as a timestamp.
# pandas is smart enough to know how to parse a date in various formats
#index_col sets the timestamp column to be the index.
#usecols tells the read_csv function to select only the subset of the columns.
#na_values is used to turn 0 into NaN
#converters: readD is the dict that means any string with 'C' with be NaN (for PI)
In [5]:
#phone almost always equals Philips monitor
"""NIRS date/time is 2 mins and 10 seconds slower than phone. Have to correct for it."""
TCnirs = timedelta(minutes=2, seconds=10)
"""Pulse ox date/time is 1 mins and 32 seconds faster than phone. Have to correct for it."""
TCpo = timedelta(minutes=1, seconds=32)
In [6]:
#time correct NIRS dataframe
dfNIRS = dfNIRSpre.set_index([dfNIRSpre.index-TCnirs])
#df.set_index(['STK_ID','RPT_Date'], inplace = True)
#inplace has to be false because otherwise it won't create a new dataframe and it'll return "None" instead
#time correct PO dataframe
dfPO = dfPOpre.set_index([dfPOpre.index+TCpo])
In [7]:
#Clean the dataframe to get rid of rows that have NaN for PI purposes
dfPO_clean = dfPO[dfPO.loc[:, ['PI', 'Exceptions']].apply(pd.notnull).all(1)]
In [8]:
#Combine two DataFrame objects and default to non-null values in frame calling the method.
#Result index columns will be the union of the respective indexes and columns
df = dfNIRS.combine_first(dfPO_clean)
In [9]:
df_first = df.first_valid_index() #get the first number from index
Y = pd.to_datetime(df_first) #convert index to datetime
# Y = TIME DATA COLLECTION BEGAN / First data point on CSV
# SYNTAX:
# datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
W1 = datetime(2016, 1, 20, 7, 35)
# W = first eye drop start
X = datetime(2016, 1, 20, 8, 42)
# X = ROP Exam Started
Z = datetime(2016, 1, 20, 8, 46)
# Z = ROP Exam Ended
df_last = df.last_valid_index() #get the last number from index
Q = pd.to_datetime(df_last)
# Q = TIME DATA COLLECTION ENDED / Last Data point on CSV
In [10]:
W2 = datetime(2016, 1, 20, 7, 35)
#W2 = second eyedrops
W3 = datetime(2016, 1, 20, 7, 40)
#W3 = third eyedrops
In [11]:
avg0NIRS = df.StO2[Y:W1].mean()
avg0PI = df.PI[Y:W1].mean()
avg0O2 = df.SpO2[Y:W1].mean()
avg0PR = df.PR[Y:W1].mean()
print 'Baseline Averages\n', 'NIRS :\t', avg0NIRS, '\nPI :\t',avg0PI, '\nSpO2 :\t',avg0O2,'\nPR :\t',avg0PR,
#df.std() for standard deviation
In [12]:
def perdeltadrop1(start, end, delta):
rdrop1 = []
curr = start
while curr < end:
rdrop1.append(curr)
curr += delta
return rdrop1
dfdrop1NI = df.StO2[W1:W1+timedelta(minutes=5)]
dfdrop1PI = df.PI[W1:W1+timedelta(minutes=5)]
dfdrop1O2 = df.SpO2[W1:W1+timedelta(minutes=5)]
dfdrop1PR = df.PR[W1:W1+timedelta(minutes=5)]
windrop1 = timedelta(seconds=10)
rdrop1 = perdeltadrop1(W1, W1+timedelta(minutes=5), windrop1)
avgdrop1NI = Series(index = rdrop1, name = 'StO2 1st ED')
avgdrop1PI = Series(index = rdrop1, name = 'PI 1st ED')
avgdrop1O2 = Series(index = rdrop1, name = 'SpO2 1st ED')
avgdrop1PR = Series(index = rdrop1, name = 'PR 1st ED')
for i in rdrop1:
avgdrop1NI[i] = dfdrop1NI[i:(i+windrop1)].mean()
avgdrop1PI[i] = dfdrop1PI[i:(i+windrop1)].mean()
avgdrop1O2[i] = dfdrop1O2[i:(i+windrop1)].mean()
avgdrop1PR[i] = dfdrop1PR[i:(i+windrop1)].mean()
resultdrops1 = concat([avgdrop1NI, avgdrop1PI, avgdrop1O2, avgdrop1PR], axis=1, join='inner')
print resultdrops1
In [13]:
def perdeltadrop2(start, end, delta):
rdrop2 = []
curr = start
while curr < end:
rdrop2.append(curr)
curr += delta
return rdrop2
dfdrop2NI = df.StO2[W2:W2+timedelta(minutes=5)]
dfdrop2PI = df.PI[W2:W2+timedelta(minutes=5)]
dfdrop2O2 = df.SpO2[W2:W2+timedelta(minutes=5)]
dfdrop2PR = df.PR[W2:W2+timedelta(minutes=5)]
windrop2 = timedelta(seconds=10)
rdrop2 = perdeltadrop2(W2, W2+timedelta(minutes=5), windrop2)
avgdrop2NI = Series(index = rdrop2, name = 'StO2 2nd ED')
avgdrop2PI = Series(index = rdrop2, name = 'PI 2nd ED')
avgdrop2O2 = Series(index = rdrop2, name = 'SpO2 2nd ED')
avgdrop2PR = Series(index = rdrop2, name = 'PR 2nd ED')
for i in rdrop2:
avgdrop2NI[i] = dfdrop2NI[i:(i+windrop2)].mean()
avgdrop2PI[i] = dfdrop2PI[i:(i+windrop2)].mean()
avgdrop2O2[i] = dfdrop2O2[i:(i+windrop2)].mean()
avgdrop2PR[i] = dfdrop2PR[i:(i+windrop2)].mean()
resultdrops2 = concat([avgdrop2NI, avgdrop2PI, avgdrop2O2, avgdrop2PR], axis=1, join='inner')
print resultdrops2
In [15]:
def perdeltadrop3(start, end, delta):
rdrop3 = []
curr = start
while curr < end:
rdrop3.append(curr)
curr += delta
return rdrop3
dfdrop3NI = df.StO2[W3:W3+timedelta(minutes=5)]
dfdrop3PI = df.PI[W3:W3+timedelta(minutes=5)]
dfdrop3O2 = df.SpO2[W3:W3+timedelta(minutes=5)]
dfdrop3PR = df.PR[W3:W3+timedelta(minutes=5)]
windrop3 = timedelta(seconds=10)
rdrop3 = perdeltadrop3(W3, W3+timedelta(minutes=5), windrop3)
avgdrop3NI = Series(index = rdrop3, name = 'StO2 3rd ED')
avgdrop3PI = Series(index = rdrop3, name = 'PI 3rd ED')
avgdrop3O2 = Series(index = rdrop3, name = 'SpO2 3rd ED')
avgdrop3PR = Series(index = rdrop3, name = 'PR 3rd ED')
for i in rdrop3:
avgdrop3NI[i] = dfdrop3NI[i:(i+windrop3)].mean()
avgdrop3PI[i] = dfdrop3PI[i:(i+windrop3)].mean()
avgdrop3O2[i] = dfdrop3O2[i:(i+windrop3)].mean()
avgdrop3PR[i] = dfdrop3PR[i:(i+windrop3)].mean()
resultdrops3 = concat([avgdrop3NI, avgdrop3PI, avgdrop3O2, avgdrop3PR], axis=1, join='inner')
print resultdrops3
In [41]:
import csv
import os
#we change the directory that python looks at to the new place.
os.chdir("/Users/John/Dropbox/LLU/ROP/Python Output Files")
#csv properties
class excel_tab(csv.excel):
delimiter = '\t'
csv.register_dialect("excel_tab", excel_tab)
#CSV file
with open(BabyNumber+'EyeDrops.csv', 'w') as f:
writer = csv.writer(f, dialect=excel_tab)
writer.writerow([avg0NIRS, ',NIRS Start']) #NIRS data
for i in rdrop1:
writer.writerow([avgdrop1NI[i]])
for i in rdrop2:
writer.writerow([avgdrop2NI[i]])
for i in rdrop3:
writer.writerow([avgdrop3NI[i]])
writer.writerow([avg0PI, ',PI Start']) #PI data
for i in rdrop1:
writer.writerow([avgdrop1PI[i]])
for i in rdrop2:
writer.writerow([avgdrop2PI[i]])
for i in rdrop3:
writer.writerow([avgdrop3PI[i]])
writer.writerow([avg0O2, ',SpO2 Start']) #SpO2 data
for i in rdrop1:
writer.writerow([avgdrop1O2[i]])
for i in rdrop2:
writer.writerow([avgdrop2O2[i]])
for i in rdrop3:
writer.writerow([avgdrop3O2[i]])
writer.writerow([avg0PR, ',PR Start']) #PR Data
for i in rdrop1:
writer.writerow([avgdrop1PR[i]])
for i in rdrop2:
writer.writerow([avgdrop2PR[i]])
for i in rdrop3:
writer.writerow([avgdrop3PR[i]])
In [ ]: