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')


Only NIRS on
Total (h) Baseline recording = 2.24027957
Total (h) After Exam recording = 25.114742314

In [50]:
definedf(ROP005, 'ROP005')


NIRS even, PO odd. PO index corrected.
Only NIRS on
Total (h) Baseline recording = 15.487790168
Total (h) After Exam recording = 8.514451256

In [51]:
definedf(ROP006, 'ROP006')


Only Masimo on
Total (h) Baseline recording = 16.39029089
Total (h) After Exam recording = 3.964447616

In [52]:
definedf(ROP007, 'ROP007')


NIRS even, PO odd. PO index corrected.
Both machines on
Total (h) Baseline recording = 14.887789688
Total (h) After Exam recording = 4.072781036

In [53]:
definedf(ROP008, 'ROP008')


Both machines on
Total (h) Baseline recording = 13.917511134
Total (h) After Exam recording = 24.521964062

In [54]:
definedf(ROP009, 'ROP009')


NIRS odd, PO even. NIRS index corrected.
Both machines on
Total (h) Baseline recording = 17.089458116
Total (h) After Exam recording = 29.48891248

In [55]:
definedf(ROP010, 'ROP010')


Both NIRS and PO indices are even.
Only Masimo on
Total (h) Baseline recording = 0.968334108
Total (h) After Exam recording = 21.464461616

In [56]:
definedf(ROP011, 'ROP011')


NIRS even, PO odd. PO index corrected.
Both machines on
Total (h) Baseline recording = 27.568355388
Total (h) After Exam recording = 23.89168578

In [57]:
definedf(ROP012, 'ROP012')


NIRS even, PO odd. PO index corrected.
Both machines on
Total (h) Baseline recording = 18.792792812
Total (h) After Exam recording = 22.755018204

In [58]:
definedf(ROP013, 'ROP013')


NIRS odd, PO even. NIRS index corrected.
Both machines on
Total (h) Baseline recording = 20.843905564
Total (h) After Exam recording = 22.756684872

In [59]:
definedf(ROP014, 'ROP014')


Both machines on
Total (h) Baseline recording = 11.740842726
Total (h) After Exam recording = 26.89585485

In [60]:
definedf(ROP015, 'ROP015')


Both NIRS and PO indices are even.
Both machines on
Total (h) Baseline recording = 15.748345932
Total (h) After Exam recording = 25.46390926

In [61]:
definedf(ROP017, 'ROP017')


Both NIRS and PO indices are even.
Both machines on
Total (h) Baseline recording = 11.5416759
Total (h) After Exam recording = 27.045021636

In [62]:
definedf(ROP018, 'ROP018')


NIRS odd, PO even. NIRS index corrected.
Both machines on
Total (h) Baseline recording = 16.608902176
Total (h) After Exam recording = 23.178351876

In [63]:
definedf(ROP019, 'ROP019')


NIRS even, PO odd. PO index corrected.
Both machines on
Total (h) Baseline recording = 9.28889632
Total (h) After Exam recording = 28.348911568

In [65]:
definedf(ROP020, 'ROP020')


Both NIRS and PO indices are even.
Both machines on
Total (h) Baseline recording = 12.698343492
Total (h) After Exam recording = 23.031685092

In [67]:
definedf(ROP021, 'ROP021')


NIRS even, PO odd. PO index corrected.
Both machines on
Total (h) Baseline recording = 13.990566748
Total (h) After Exam recording = 32.542526034

In [68]:
definedf(ROP022, 'ROP022')


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-68-19f274a36203> in <module>()
----> 1 definedf(ROP022, 'ROP022')

<ipython-input-46-ff5e1012ec41> in definedf(Baby, BabyNumber)
      4                          index_col='timestamp',
      5                          usecols=['Date', ' Time', ' Ch2 %StO2'],
----> 6                          na_values=['0'])
      7 
      8     dfPOpre0 = pd.read_csv('/Users/John/Dropbox/LLU/Projects/Retinopathy of Prematurity/Pulse Ox/' + BabyNumber + 'PO.csv',

/Users/John/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/io/parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doublequote, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, skipfooter, skip_footer, na_values, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whitespace, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, keep_default_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, float_precision, nrows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols, infer_datetime_format, skip_blank_lines)
    496                     skip_blank_lines=skip_blank_lines)
    497 
--> 498         return _read(filepath_or_buffer, kwds)
    499 
    500     parser_f.__name__ = name

/Users/John/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/io/parsers.pyc in _read(filepath_or_buffer, kwds)
    273 
    274     # Create the parser.
--> 275     parser = TextFileReader(filepath_or_buffer, **kwds)
    276 
    277     if (nrows is not None) and (chunksize is not None):

/Users/John/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/io/parsers.pyc in __init__(self, f, engine, **kwds)
    588             self.options['has_index_names'] = kwds['has_index_names']
    589 
--> 590         self._make_engine(self.engine)
    591 
    592     def _get_options_with_defaults(self, engine):

/Users/John/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/io/parsers.pyc in _make_engine(self, engine)
    729     def _make_engine(self, engine='c'):
    730         if engine == 'c':
--> 731             self._engine = CParserWrapper(self.f, **self.options)
    732         else:
    733             if engine == 'python':

/Users/John/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/io/parsers.pyc in __init__(self, src, **kwds)
   1101         kwds['allow_leading_cols'] = self.index_col is not False
   1102 
-> 1103         self._reader = _parser.TextReader(src, **kwds)
   1104 
   1105         # XXX

pandas/parser.pyx in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3246)()

pandas/parser.pyx in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:6111)()

IOError: File /Users/John/Dropbox/LLU/Projects/Retinopathy of Prematurity/NIRS/Clean/ROP022NIRS.csv does not exist