In [35]:
# Import Modules and Identify Low Signals
import numpy as np
import pyeeg
import matplotlib.pyplot as plt
from pylab import *
import pandas as pd
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 [46]:
def definedf(Baby, BabyNumber):
dfNIRSpre0 = pd.read_csv('/Users/John/Dropbox/LLU/Projects/Retinopathy of Prematurity/NIRS/Clean/' + 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/Projects/Retinopathy of Prematurity/Pulse Ox/' + 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/Projects/Retinopathy of Prematurity/Pulse Ox/' + 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
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 '\nBoth NIRS and PO indices are even.'
elif dfPOpre.index[:1].second % 2 != 0:
print '\nNIRS 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 '\nNIRS odd, PO even. NIRS index corrected.'
dfNIRSpre = dfNIRSpre.set_index(ncorr)
elif dfPOpre.index[:1].second % 2 != 0:
print '\nBoth 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')
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
#for babies that only had one machine
if len(dfNIRS) > 5:
if len(dfPO) > 5:
df = dfNIRS.combine_first(dfPO) #Combine two DataFrame objects and default to non-null values in frame
print 'Both machines on'
elif len(dfPO) < 5:
df = dfNIRS
print 'Only NIRS on'
elif len(dfNIRS) < 5:
df = dfPO
print 'Only Masimo on'
else:
print 'Check yo files'
a = -(df.index[0]-Baby['ExamStart']).total_seconds()*0.000277778
print 'Total (h) Baseline recording = %s' % a
b = ((df.index[-1])-Baby['ExamEnd']).total_seconds()*0.000277778
print 'Total (h) After Exam recording = %s' % b
In [49]:
definedf(ROP003, 'ROP003')
In [50]:
definedf(ROP005, 'ROP005')
In [51]:
definedf(ROP006, 'ROP006')
In [52]:
definedf(ROP007, 'ROP007')
In [53]:
definedf(ROP008, 'ROP008')
In [54]:
definedf(ROP009, 'ROP009')
In [55]:
definedf(ROP010, 'ROP010')
In [56]:
definedf(ROP011, 'ROP011')
In [57]:
definedf(ROP012, 'ROP012')
In [58]:
definedf(ROP013, 'ROP013')
In [59]:
definedf(ROP014, 'ROP014')
In [60]:
definedf(ROP015, 'ROP015')
In [61]:
definedf(ROP017, 'ROP017')
In [62]:
definedf(ROP018, 'ROP018')
In [63]:
definedf(ROP019, 'ROP019')
In [65]:
definedf(ROP020, 'ROP020')
In [67]:
definedf(ROP021, 'ROP021')
In [68]:
definedf(ROP022, 'ROP022')