In [5]:
# Import Modules and Identify Low Signals
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
''' -------------------------- '''
BabyNumber = raw_input("Baby's ROP Number? ")
def BabyName():
if len(BabyNumber) == 3:
return
else:
raise NameError("The syntax is incorrect. It needs to have three digits.")
BabyName()
dfNIRSpre0 = pd.read_csv('/Users/John/Dropbox/LLU/ROP/NIRS/Clean/ROP' + BabyNumber + 'NIRS.csv',
parse_dates={'timestamp': ['Date',' Time']},
index_col='timestamp',
usecols=['Date', ' Time', ' Ch2 %StO2'],
na_values=['0'])
dfPOpre0 = pd.read_csv('/Users/John/Dropbox/LLU/ROP/Pulse Ox/ROP' + BabyNumber + 'PO.csv',
parse_dates={'timestamp': ['Date','Time']},
index_col='timestamp',
usecols=['Date', 'Time', 'SpO2', 'PR'],
na_values=['0'])
dfPIpre0 = pd.read_csv('/Users/John/Dropbox/LLU/ROP/Pulse Ox/ROP' + BabyNumber + 'PO.csv',
parse_dates={'timestamp': ['Date','Time']},
index_col='timestamp',
usecols=['Date', 'Time', 'PI', 'Exceptions'],
na_values=['0'],
converters={'Exceptions': readD}
) #make a PI df as well to clean it better
dfNIRSpre = dfNIRSpre0.rename(columns={' Ch2 %StO2': 'StO2'}) #rename NIRS column
#Clean the PI dataframe to get rid of rows that have NaN
dfPIpre = dfPIpre0[dfPIpre0.loc[:, ['PI', 'Exceptions']].apply(pd.notnull).all(1)]
dfPOpre = dfPIpre.combine_first(dfPOpre0)
#combine PI dataframe with the rest of the Pulse Ox dataframe after cleaning
In [7]:
dfNIRSpre[3216:3200]
Out[7]:
In [10]:
TCcorrect = timedelta(seconds=1)
ncorr = dfPOpre.index-TCcorrect
dfPOpre[:5]
Out[10]:
In [11]:
dfPOpre.set_index(ncorr)
Out[11]:
In [12]:
dfPOpre[3216:3200]
Out[12]:
In [61]:
TCcorrect = timedelta(seconds=1)
if dfNIRSpre.index[:1].second % 2 == 0:
if dfPOpre.index[:1].second % 2 == 0:
print 'Both NIRS and PO indices are even'
elif dfPOpre.index[:1].second % 2 != 0:
print 'NIRS even, PO odd. PO index corrected'
dfPOpre = dfPOpre.set_index([dfPOpre.index-TCcorrect])
else:
raise NameError('Indices are messed up')
elif dfNIRSpre.index[:1].second % 2 != 0:
if dfPOpre.index[:1].second % 2 == 0:
print 'NIRS odd, PO even. NIRS index corrected'
dfNIRSpre = dfNIRSpre.set_index([dfNIRSpre.index+TCcorrect])
elif dfPOpre.index[:1].second % 2 != 0:
print 'Both NIRS and PO indices are odd. Both corrected'
dfNIRSpre = dfNIRSpre.set_index([dfNIRSpre.index+TCcorrect])
dfPOpre = dfPOpre.set_index([dfPOpre.index-TCcorrect])
else:
raise NameError('Indices are messed up')
In [62]:
TCnirs = timedelta(minutes=2, seconds=10)
TCpo = timedelta(minutes=1, seconds=32)
# NIRS is slower than correct time, need to add TCnirs to catch it up
# PO is faster than correct time, need to subtract TCpo to slow it down
dfNIRS = dfNIRSpre.set_index([dfNIRSpre.index+TCnirs]) #NIRS Time Correction
dfPO = dfPOpre.set_index([dfPOpre.index-TCpo]) #PO Time Correction
df = dfNIRS.combine_first(dfPO) #Combine two DataFrame objects and default to non-null values in frame calling the method.
df_first = df.first_valid_index #get the first number from index
#Y = pd.to_datetime(df_first) #convert index to datetime
In [67]:
dfPOpre
Out[67]:
In [ ]: