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 [1]:
#the usual beginning
import pandas as pd
from pandas import Series, DataFrame
from datetime import datetime, timedelta

In [2]:
df = pd.read_csv('/Users/John/Dropbox/LLU/ROP/NIRS/Clean/ROP013NIRS.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 [3]:
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, 10, 14, 8, 30)
# W = first eye drop dtarts
X = datetime(2015, 10, 14, 9, 36)
# X = ROP Exam Started

Z = datetime(2015, 10, 14, 9, 40)
# 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 [4]:
# Baseline Average

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


 Ch2 %StO2    80.887584
dtype: float64

In [ ]:


In [5]:
#AVERAGE DURING ROP EXAM FOR FIRST FOUR MINUTES
#SET X+timedelta(minutes=4) to Z if you want full exam
import numpy as np
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 = 'During ROP Exam')

#average!
for i1 in r1:
    avg1[i1] = df1[i1:(i1+win1)].mean()
avg1


Out[5]:
2015-10-14 09:36:00    83.20
2015-10-14 09:36:10    79.80
2015-10-14 09:36:20    80.20
2015-10-14 09:36:30    79.25
2015-10-14 09:36:40    76.20
2015-10-14 09:36:50    73.60
2015-10-14 09:37:00    75.20
2015-10-14 09:37:10    72.40
2015-10-14 09:37:20    70.20
2015-10-14 09:37:30    71.60
2015-10-14 09:37:40    73.60
2015-10-14 09:37:50    74.00
2015-10-14 09:38:00    74.60
2015-10-14 09:38:10    77.40
2015-10-14 09:38:20    78.80
2015-10-14 09:38:30    80.60
2015-10-14 09:38:40    80.80
2015-10-14 09:38:50    80.80
2015-10-14 09:39:00    80.60
2015-10-14 09:39:10    79.20
2015-10-14 09:39:20    75.40
2015-10-14 09:39:30    71.60
2015-10-14 09:39:40    69.60
2015-10-14 09:39:50    67.40
Name: During ROP Exam, dtype: float64

In [6]:
#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[6]:
2015-10-14 09:40:00    82.973333
2015-10-14 09:45:00    80.426667
2015-10-14 09:50:00    78.040268
2015-10-14 09:55:00    80.213333
2015-10-14 10:00:00    79.620000
2015-10-14 10:05:00    80.080000
2015-10-14 10:10:00    80.900000
2015-10-14 10:15:00    83.993333
2015-10-14 10:20:00    82.380000
2015-10-14 10:25:00    81.973154
2015-10-14 10:30:00    81.613333
2015-10-14 10:35:00    85.006667
Name: 5 Min After Hour 1, dtype: float64

In [7]:
#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[7]:
2015-10-14 10:40:00    80.135556
2015-10-14 10:55:00    77.581292
2015-10-14 11:10:00    71.182628
2015-10-14 11:25:00    75.631111
Name: 15 Min After Hour 2, dtype: float64

In [8]:
#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[8]:
2015-10-14 11:40:00    74.893215
2015-10-14 12:10:00    74.416481
Name: 30 Min After Hour 3, dtype: float64

In [9]:
#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[9]:
2015-10-14 12:40:00    77.132296
2015-10-14 13:40:00    75.221480
2015-10-14 14:40:00    75.887653
2015-10-14 15:40:00    79.762514
2015-10-14 16:40:00    75.717464
2015-10-14 17:40:00    81.201335
2015-10-14 18:40:00    80.944352
2015-10-14 19:40:00    78.491101
2015-10-14 20:40:00    80.955506
2015-10-14 21:40:00    80.516129
2015-10-14 22:40:00    79.287145
2015-10-14 23:40:00    78.156198
2015-10-15 00:40:00    77.065109
2015-10-15 01:40:00    77.593437
2015-10-15 02:40:00    78.845384
2015-10-15 03:40:00    76.128476
2015-10-15 04:40:00    72.784761
2015-10-15 05:40:00    75.779633
2015-10-15 06:40:00    75.348721
2015-10-15 07:40:00    77.580695
2015-10-15 08:40:00          NaN
Name: Hours After Hour 4, dtype: float64

Export Data to CSV

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


In [10]:
#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('ROP013_NIRSAvgs.csv', 'w') as f:    #CHANGE CSV FILE NAME
    writer = csv.writer(f, dialect=excel_tab)
    writer.writerow(avg0)
    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())

Data Recording Time Length


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


Data Recording Time!
**********
Pre-Exam Data Recording Length	0 days 20:52:49
Post-Exam Data Recording Length	0 days 22:43:11
Total Data Recording Length	1 days 19:40:00