In [28]:
# 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


Baby's ROP Number? 007

In [41]:
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 [46]:
dfPOpre[3000:3010]


Out[46]:
Exceptions PI PR SpO2
timestamp
2015-06-30 20:37:17 8000 1.1 181 100
2015-06-30 20:37:19 8000 0.9 185 100
2015-06-30 20:37:21 8000 0.9 188 100
2015-06-30 20:37:23 8000 0.9 188 100
2015-06-30 20:37:25 8000 1.9 189 100
2015-06-30 20:37:27 8000 2.1 189 100
2015-06-30 20:37:29 8000 2.1 189 100
2015-06-30 20:37:31 8000 1.7 192 100
2015-06-30 20:37:33 8000 1.7 192 100
2015-06-30 20:37:35 8000 1.7 192 100

In [43]:
TCcorrect = timedelta(seconds=1)
ncorr = dfPOpre.index-TCcorrect

In [49]:
dfPOnewindex = dfPOpre.set_index(ncorr)

In [57]:
dfPOpre[3000:3010]


Out[57]:
Exceptions PI PR SpO2
timestamp
2015-06-30 20:37:16 8000 1.1 181 100
2015-06-30 20:37:18 8000 0.9 185 100
2015-06-30 20:37:20 8000 0.9 188 100
2015-06-30 20:37:22 8000 0.9 188 100
2015-06-30 20:37:24 8000 1.9 189 100
2015-06-30 20:37:26 8000 2.1 189 100
2015-06-30 20:37:28 8000 2.1 189 100
2015-06-30 20:37:30 8000 1.7 192 100
2015-06-30 20:37:32 8000 1.7 192 100
2015-06-30 20:37:34 8000 1.7 192 100

In [53]:
TCcorrect = timedelta(seconds=1)

ncorr = dfNIRSpre.index+TCcorrect
pcorr = dfPOpre.index-TCcorrect
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(pcorr)
    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(ncorr)
    elif dfPOpre.index[:1].second % 2 != 0:
        print 'Both NIRS and PO indices are odd. Both corrected'
        dfNIRSpre = dfNIRSpre.set_index(ncorr)
        dfPOpre = dfPOpre.set_index(pcorr)
else:
    raise NameError('Indices are messed up')


NIRS even, PO odd. PO index corrected

In [71]:
df_first[3000:3010]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-71-190ad21cb5fb> in <module>()
----> 1 df_first[3000:3010]

TypeError: 'instancemethod' object has no attribute '__getitem__'

In [63]:
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

In [67]:
df = dfNIRS.combine_first(dfPO) #Combine two DataFrame objects and default to non-null values in frame calling the method.

In [76]:
df_first = df.first_valid_index() #get the first number from index
Y = pd.to_datetime(df_first) #convert index to datetime

In [72]:
df_first_


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-338a9828a51e> in <module>()
----> 1 df_first[:1]

TypeError: 'instancemethod' object has no attribute '__getitem__'

In [ ]: