NIRS Analysis

For NIRS Analysis, make sure the data file is the right .csv format:

a) Headings on Row 1, DELETE EVERYTHING ELSE
b) Open the csv file through excel and fill in the dates in Col A.
An easy way to do this is to have both desired dates in A2 and A3.
Select A2 and A3 and double click the black box on the bottom right
of the cell. Repeat for multiple days.
c) Delete the last row in your NIRS data where Column A is not a 
date.

Because of the way the coding is done, we can only analyze one column at a time.


In [33]:
#the usual beginning
import pandas as pd
from pandas import Series, DataFrame
from datetime import datetime, timedelta

In [34]:
df = pd.read_csv('/Users/John/Dropbox/LLU/ROP/NIRS/Clean/ROP009NIRS.csv',
            parse_dates={'timestamp': ['Date',' Time']},
            index_col='timestamp',
            usecols=['Date', ' Time', ' Ch2 %StO2'],
            na_values=['0'])

# 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.

In [35]:
"""NIRS date/time is 2 mins and 10 seconds slower than phone. Have to correct for it."""

TC = timedelta(minutes=2, seconds=10)

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


W = datetime(2015, 8, 26, 9, 4)-TC
# W = first eye drop starts
X = datetime(2015, 8, 26, 9, 56)-TC
# X = ROP Exam Started
Z = datetime(2015, 8, 26, 9, 59)-TC
# 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 [37]:
# Baseline Average

df0 = df[Y:W]
avg0 = df0.mean()
print avg0


 Ch2 %StO2    75.830015
dtype: float64

In [38]:
# Every 5 min Average from start of eye drops to start of exam

import numpy as np

def perdelta(start, end, delta):
    rdrop = []
    curr = start
    while curr < end:
        rdrop.append(curr)
        curr += delta
    return rdrop
    
dfdrop = df[W:W+timedelta(minutes=15)] #15 minutes? 
windrop = timedelta(minutes=5)#make the range
rdrop = perdelta(W, W+timedelta(minutes=15), windrop) #15 minutes

avgdrop = Series(index = rdrop, name = 'During Eyedrops')

for i in rdrop:
    avgdrop[i] = dfdrop[i:(i+windrop)].mean()
avgdrop


Out[38]:
2015-08-26 09:01:50    71.926667
2015-08-26 09:06:50    77.177778
2015-08-26 09:11:50          NaN
Name: During Eyedrops, dtype: float64

In [39]:
#AVERAGE DURING ROP EXAM FOR FIRST FOUR MINUTES

def perdelta(start, end, delta):
    r1 = []
    curr = start
    while curr < end:
        r1.append(curr)
        curr += delta
    return r1

df1 = df[X:X+timedelta(minutes=4)]
win1 = timedelta(seconds=10) #any unit of time
#make the range
r1 = perdelta(X, X+timedelta(minutes=4), win1)

#make the series to store
avg1 = Series(index = r1, name = 'Average During ROP Exam')

#average!
for i1 in r1:
    avg1[i1] = df1[i1:(i1+win1)].mean()
    
print "Average during ROP Exam \n", avg1


Average during ROP Exam 
2015-08-26 09:53:50    78.6
2015-08-26 09:54:00    78.8
2015-08-26 09:54:10    77.8
2015-08-26 09:54:20    78.8
2015-08-26 09:54:30    78.2
2015-08-26 09:54:40    79.4
2015-08-26 09:54:50    80.2
2015-08-26 09:55:00    81.0
2015-08-26 09:55:10    82.0
2015-08-26 09:55:20    80.8
2015-08-26 09:55:30    78.0
2015-08-26 09:55:40    76.4
2015-08-26 09:55:50    76.6
2015-08-26 09:56:00    79.0
2015-08-26 09:56:10    78.8
2015-08-26 09:56:20    79.0
2015-08-26 09:56:30    79.0
2015-08-26 09:56:40    79.6
2015-08-26 09:56:50    79.6
2015-08-26 09:57:00    79.0
2015-08-26 09:57:10    79.6
2015-08-26 09:57:20    79.8
2015-08-26 09:57:30    78.2
2015-08-26 09:57:40    77.4
Name: Average During ROP Exam, dtype: float64

In [40]:
#std deviation durin ROP exam

stdev = Series(index = r1, name = 'Std Dev During ROP Exam')

for i1 in r1:
    stdev[i1] = df1[i1:(i1+win1)].std()

print stdev


2015-08-26 09:53:50    0.547723
2015-08-26 09:54:00    0.447214
2015-08-26 09:54:10    0.836660
2015-08-26 09:54:20    0.447214
2015-08-26 09:54:30    0.447214
2015-08-26 09:54:40    0.547723
2015-08-26 09:54:50    1.095445
2015-08-26 09:55:00    0.000000
2015-08-26 09:55:10    0.000000
2015-08-26 09:55:20    0.836660
2015-08-26 09:55:30    1.414214
2015-08-26 09:55:40    0.547723
2015-08-26 09:55:50    1.341641
2015-08-26 09:56:00    0.000000
2015-08-26 09:56:10    0.447214
2015-08-26 09:56:20    0.000000
2015-08-26 09:56:30    0.000000
2015-08-26 09:56:40    0.547723
2015-08-26 09:56:50    0.547723
2015-08-26 09:57:00    0.000000
2015-08-26 09:57:10    0.547723
2015-08-26 09:57:20    0.447214
2015-08-26 09:57:30    0.447214
2015-08-26 09:57:40    0.547723
Name: Std Dev During ROP Exam, dtype: float64

In [41]:
CV = (stdev/avg1) * 100
CV


Out[41]:
2015-08-26 09:53:50    0.696848
2015-08-26 09:54:00    0.567530
2015-08-26 09:54:10    1.075398
2015-08-26 09:54:20    0.567530
2015-08-26 09:54:30    0.571884
2015-08-26 09:54:40    0.689827
2015-08-26 09:54:50    1.365892
2015-08-26 09:55:00    0.000000
2015-08-26 09:55:10    0.000000
2015-08-26 09:55:20    1.035470
2015-08-26 09:55:30    1.813094
2015-08-26 09:55:40    0.716914
2015-08-26 09:55:50    1.751489
2015-08-26 09:56:00    0.000000
2015-08-26 09:56:10    0.567530
2015-08-26 09:56:20    0.000000
2015-08-26 09:56:30    0.000000
2015-08-26 09:56:40    0.688094
2015-08-26 09:56:50    0.688094
2015-08-26 09:57:00    0.000000
2015-08-26 09:57:10    0.688094
2015-08-26 09:57:20    0.560418
2015-08-26 09:57:30    0.571884
2015-08-26 09:57:40    0.707652
dtype: float64

In [42]:
#AVERAGE EVERY 5 MINUTES ONE HOUR AFTER ROP EXAM

def perdelta(start, end, delta):
    r2 = []
    curr = start
    while curr < end:
        r2.append(curr)
        curr += delta
    return r2

# datetime(year, month, day, hour, etc.)

df2 = df[Z:(Z+timedelta(hours=1))]
win2 = timedelta(minutes=5) #any unit of time
#make the range
r2 = perdelta(Z, (Z+timedelta(hours=1)), win2)

#make the series to store
avg2 = Series(index = r2, name = '5 Min After Hour 1')

#average!
for i2 in r2:
    avg2[i2] = df2[i2:(i2+win2)].mean()
avg2


Out[42]:
2015-08-26 09:56:50    77.166667
2015-08-26 10:01:50    79.600000
2015-08-26 10:06:50    81.006667
2015-08-26 10:11:50    79.480000
2015-08-26 10:16:50    77.919463
2015-08-26 10:21:50    78.020000
2015-08-26 10:26:50    79.100000
2015-08-26 10:31:50    81.666667
2015-08-26 10:36:50    82.805369
2015-08-26 10:41:50    82.786667
2015-08-26 10:46:50    83.966667
2015-08-26 10:51:50    81.213333
Name: 5 Min After Hour 1, dtype: float64

In [43]:
#AVERAGE EVERY 15 MINUTES TWO HOURS AFTER ROP EXAM

def perdelta(start, end, delta):
    r3 = []
    curr = start
    while curr < end:
        r3.append(curr)
        curr += delta
    return r3

# datetime(year, month, day, hour, etc.)

df3 = df[(Z+timedelta(hours=1)):(Z+timedelta(hours=2))]
win3 = timedelta(minutes=15) #any unit of time
#make the range
r3 = perdelta((Z+timedelta(hours=1)), (Z+timedelta(hours=2)), win3)

#make the series to store
avg3 = Series(index = r3, name = '15 Min After Hour 2')

#average!
for i3 in r3:
    avg3[i3] = df3[i3:(i3+win3)].mean()
avg3


Out[43]:
2015-08-26 10:56:50    82.048998
2015-08-26 11:11:50    81.764444
2015-08-26 11:26:50    80.740000
2015-08-26 11:41:50    83.015590
Name: 15 Min After Hour 2, dtype: float64

In [44]:
#AVERAGE EVERY 30 MINUTES THREE HOURS AFTER ROP EXAM

def perdelta(start, end, delta):
    r4 = []
    curr = start
    while curr < end:
        r4.append(curr)
        curr += delta
    return r4

# datetime(year, month, day, hour, etc.)

df4 = df[(Z+timedelta(hours=2)):(Z+timedelta(hours=3))]
win4 = timedelta(minutes=30) #any unit of time
#make the range
r4 = perdelta((Z+timedelta(hours=2)), (Z+timedelta(hours=3)), win4)

#make the series to store
avg4 = Series(index = r4, name = '30 Min After Hour 3')

#average!
for i4 in r4:
    avg4[i4] = df4[i4:(i4+win4)].mean()
avg4


Out[44]:
2015-08-26 11:56:50    81.747497
2015-08-26 12:26:50    79.402670
Name: 30 Min After Hour 3, dtype: float64

In [45]:
#AVERAGE EVERY HOUR AFTER FOUR HOURS AFTER ROP EXAM

def perdelta(start, end, delta):
    r5 = []
    curr = start
    while curr < end:
        r5.append(curr)
        curr += delta
    return r5

# datetime(year, month, day, hour, etc.)

df5 = df[(Z+timedelta(hours=3)):(Z+timedelta(hours=24))]
win5 = timedelta(minutes=60) #any unit of time
#make the range
r5 = perdelta((Z+timedelta(hours=3)), (Z+timedelta(hours=24)), win5)

#make the series to store
avg5 = Series(index = r5, name = 'Hours After Hour 4')

#average!
for i5 in r5:
    avg5[i5] = df5[i5:(i5+win5)].mean()
avg5


Out[45]:
2015-08-26 12:56:50    76.753615
2015-08-26 13:56:50    77.065628
2015-08-26 14:56:50    76.828047
2015-08-26 15:56:50    76.678710
2015-08-26 16:56:50    77.260870
2015-08-26 17:56:50    79.925926
2015-08-26 18:56:50    82.043424
2015-08-26 19:56:50    77.470621
2015-08-26 20:56:50    79.570395
2015-08-26 21:56:50    73.150723
2015-08-26 22:56:50    76.370206
2015-08-26 23:56:50    77.829716
2015-08-27 00:56:50    77.964385
2015-08-27 01:56:50    77.694661
2015-08-27 02:56:50    76.643493
2015-08-27 03:56:50    78.319800
2015-08-27 04:56:50    79.648103
2015-08-27 05:56:50    75.022622
2015-08-27 06:56:50    74.372636
2015-08-27 07:56:50    74.830467
2015-08-27 08:56:50          NaN
Name: Hours After Hour 4, dtype: float64

Data Recording Time Length


In [46]:
print "Data Recording Time!"
print '*' * 10
print "Pre-Exam Data Recording Length\t", X - Y # start of exam - first data point
print "Post-Exam Data Recording Length\t", Q - Z #last data point - end of exam
print "Total Data Recording Length\t", Q - Y #last data point - first data point 


Pre = ['Pre',(X-Y)]
Post = ['Post',(Q-Z)]
Total = ['Total',(Q-Y)]

RTL = [Pre, Post, Total]
#creating a list for recording time length


Data Recording Time!
**********
Pre-Exam Data Recording Length	0 days 16:37:31
Post-Exam Data Recording Length	0 days 22:27:09
Total Data Recording Length	1 days 15:07:40

Export Data to CSV

Make sure the file name is changed!!!! ROP###Avgs_NIRS.csv


In [47]:
#export out all averages as a csv file with only

import csv
class excel_tab(csv.excel):
        delimiter = '\t'
csv.register_dialect("excel_tab", excel_tab)

with open('ROP009_NIRS.csv', 'w') as f:    #CHANGE CSV FILE NAME
    writer = csv.writer(f, dialect=excel_tab)
    writer.writerow()
    writer.writerow(avg0, ',NIRS Values')
    for i in rdrop:
        writer.writerow(dfdrop[i:(i+windrop)].mean())
    for i1 in r1:
        writer.writerow(df1[i1:(i1+win1)].mean())
    for i2 in r2:
        writer.writerow(df2[i2:(i2+win2)].mean())
    for i3 in r3:
        writer.writerow(df3[i3:(i3+win3)].mean())
    for i4 in r4:    
        writer.writerow(df4[i4:(i4+win4)].mean())
    for i5 in r5:
        writer.writerow(df5[i5:(i5+win5)].mean())
    writer.writerow(['Data Recording Time Length'])
    writer.writerows(RTL)