Eye Drops Analysis for NIRS and Pulse Ox

Writing a new notebook to analyze eye drops only


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

ROP Subject Number. CHANGE THIS BLOCK


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)

Date and Time of Baseline and Eye Drops


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

Baseline Average Calculation


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


Baseline Averages
NIRS :	87.5329586101 
PI :	1.35690874794 
SpO2 :	99.3392288645 
PR :	165.025744831

First Eye Drop Avg Every 10 Sec For 5 Minutes


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


                     StO2 1st ED  PI 1st ED  SpO2 1st ED   PR 1st ED
2016-01-20 07:35:00         93.0   1.566667   100.000000  176.833333
2016-01-20 07:35:10         92.6   1.616667   100.000000  176.166667
2016-01-20 07:35:20         92.0   1.966667    99.666667  171.500000
2016-01-20 07:35:30         92.0   1.950000    99.000000  166.500000
2016-01-20 07:35:40         92.6   1.700000   100.000000  176.666667
2016-01-20 07:35:50         93.0   1.800000    99.666667  172.000000
2016-01-20 07:36:00         92.8   1.583333    99.666667  176.333333
2016-01-20 07:36:10         93.0   1.550000    99.833333  171.833333
2016-01-20 07:36:20         91.8   1.583333    99.666667  176.500000
2016-01-20 07:36:30         92.0   1.566667    99.833333  176.833333
2016-01-20 07:36:40         92.6   1.666667   100.000000  177.833333
2016-01-20 07:36:50         92.8   1.600000   100.000000  183.333333
2016-01-20 07:37:00         93.0   1.633333   100.000000  178.166667
2016-01-20 07:37:10         93.0   1.683333    99.833333  179.000000
2016-01-20 07:37:20         93.0   1.766667    99.833333  175.000000
2016-01-20 07:37:30         93.0   1.700000    99.833333  171.166667
2016-01-20 07:37:40         92.2   1.716667    99.833333  174.166667
2016-01-20 07:37:50         92.2   1.616667    98.666667  173.000000
2016-01-20 07:38:00         92.2   1.600000    99.000000  173.166667
2016-01-20 07:38:10         92.0   1.400000    97.500000  177.500000
2016-01-20 07:38:20         93.0   1.733333    99.000000  176.333333
2016-01-20 07:38:30         93.0   1.850000    98.666667  178.666667
2016-01-20 07:38:40         93.0   1.533333    99.833333  167.833333
2016-01-20 07:38:50         92.2   1.683333    99.500000  158.000000
2016-01-20 07:39:00         92.2   1.733333   100.000000  156.500000
2016-01-20 07:39:10         92.4   1.833333    99.500000  160.333333
2016-01-20 07:39:20         93.0   1.766667    99.833333  157.166667
2016-01-20 07:39:30         93.0   1.800000   100.000000  160.166667
2016-01-20 07:39:40         93.0   1.666667    99.166667  165.333333
2016-01-20 07:39:50         93.0   2.083333    98.166667  162.000000

Second Eye Drop Avg Every 10 Sec For 5 Minutes


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


                     StO2 2nd ED  PI 2nd ED  SpO2 2nd ED   PR 2nd ED
2016-01-20 07:35:00         93.0   1.566667   100.000000  176.833333
2016-01-20 07:35:10         92.6   1.616667   100.000000  176.166667
2016-01-20 07:35:20         92.0   1.966667    99.666667  171.500000
2016-01-20 07:35:30         92.0   1.950000    99.000000  166.500000
2016-01-20 07:35:40         92.6   1.700000   100.000000  176.666667
2016-01-20 07:35:50         93.0   1.800000    99.666667  172.000000
2016-01-20 07:36:00         92.8   1.583333    99.666667  176.333333
2016-01-20 07:36:10         93.0   1.550000    99.833333  171.833333
2016-01-20 07:36:20         91.8   1.583333    99.666667  176.500000
2016-01-20 07:36:30         92.0   1.566667    99.833333  176.833333
2016-01-20 07:36:40         92.6   1.666667   100.000000  177.833333
2016-01-20 07:36:50         92.8   1.600000   100.000000  183.333333
2016-01-20 07:37:00         93.0   1.633333   100.000000  178.166667
2016-01-20 07:37:10         93.0   1.683333    99.833333  179.000000
2016-01-20 07:37:20         93.0   1.766667    99.833333  175.000000
2016-01-20 07:37:30         93.0   1.700000    99.833333  171.166667
2016-01-20 07:37:40         92.2   1.716667    99.833333  174.166667
2016-01-20 07:37:50         92.2   1.616667    98.666667  173.000000
2016-01-20 07:38:00         92.2   1.600000    99.000000  173.166667
2016-01-20 07:38:10         92.0   1.400000    97.500000  177.500000
2016-01-20 07:38:20         93.0   1.733333    99.000000  176.333333
2016-01-20 07:38:30         93.0   1.850000    98.666667  178.666667
2016-01-20 07:38:40         93.0   1.533333    99.833333  167.833333
2016-01-20 07:38:50         92.2   1.683333    99.500000  158.000000
2016-01-20 07:39:00         92.2   1.733333   100.000000  156.500000
2016-01-20 07:39:10         92.4   1.833333    99.500000  160.333333
2016-01-20 07:39:20         93.0   1.766667    99.833333  157.166667
2016-01-20 07:39:30         93.0   1.800000   100.000000  160.166667
2016-01-20 07:39:40         93.0   1.666667    99.166667  165.333333
2016-01-20 07:39:50         93.0   2.083333    98.166667  162.000000

Third Eye Drop Avg Every 10 Sec For 5 Minutes


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


                     StO2 3rd ED  PI 3rd ED  SpO2 3rd ED   PR 3rd ED
2016-01-20 07:40:00         93.0   1.766667    99.833333  163.833333
2016-01-20 07:40:10         93.0   1.583333    99.333333  161.666667
2016-01-20 07:40:20         93.0   1.550000    98.833333  169.000000
2016-01-20 07:40:30         93.0   1.600000    98.333333  166.166667
2016-01-20 07:40:40         93.0   1.500000    99.666667  168.000000
2016-01-20 07:40:50         93.0   1.433333    99.000000  174.500000
2016-01-20 07:41:00         93.0   1.450000    98.666667  168.833333
2016-01-20 07:41:10         92.8   1.200000    99.166667  175.333333
2016-01-20 07:41:20         92.4   1.583333   100.000000  162.166667
2016-01-20 07:41:30         92.0   1.633333   100.000000  158.000000
2016-01-20 07:41:40         92.4   1.783333   100.000000  148.166667
2016-01-20 07:41:50         91.6   1.716667    99.666667  157.833333
2016-01-20 07:42:00         91.6   1.566667    99.833333  156.833333
2016-01-20 07:42:10         92.0   1.716667    99.500000  153.166667
2016-01-20 07:42:20         91.0   1.716667   100.000000  157.333333
2016-01-20 07:42:30         91.8   1.700000    99.166667  154.333333
2016-01-20 07:42:40         92.0   1.700000   100.000000  152.666667
2016-01-20 07:42:50         92.0   1.833333    99.833333  157.833333
2016-01-20 07:43:00         92.0   2.000000    99.833333  151.833333
2016-01-20 07:43:10         90.8   1.850000    99.500000  155.500000
2016-01-20 07:43:20         88.6   1.916667    96.666667  143.833333
2016-01-20 07:43:30         89.2   1.733333    96.333333  144.333333
2016-01-20 07:43:40         90.4   1.600000   100.000000  162.666667
2016-01-20 07:43:50         91.2   1.533333    99.333333  157.333333
2016-01-20 07:44:00         92.2   1.766667   100.000000  145.666667
2016-01-20 07:44:10         92.2   1.716667    97.666667  159.500000
2016-01-20 07:44:20         92.0   1.466667    98.833333  160.833333
2016-01-20 07:44:30         92.0   1.400000    98.666667  169.833333
2016-01-20 07:44:40         92.8   1.216667   100.000000  175.833333
2016-01-20 07:44:50         92.4   1.450000   100.000000  168.500000

Export to CSV


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