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/ROP008NIRS.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]]]]])

X = datetime(2015, 8, 19, 8, 1)
# X = ROP Exam Started

Z = datetime(2015, 8, 19, 8, 5)
# 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:X]
avg0 = df0.mean()
print avg0


 Ch2 %StO2    86.069773
dtype: float64

In [5]:
#AVERAGE DURING ROP 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:Z]
win1 = timedelta(seconds=10) #any unit of time
#make the range
r1 = perdelta(X, Z, 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-08-19 08:01:00    86.0
2015-08-19 08:01:10    82.8
2015-08-19 08:01:20    81.4
2015-08-19 08:01:30    81.4
2015-08-19 08:01:40    80.0
2015-08-19 08:01:50    80.0
2015-08-19 08:02:00    83.4
2015-08-19 08:02:10    83.8
2015-08-19 08:02:20    83.0
2015-08-19 08:02:30    85.6
2015-08-19 08:02:40    85.0
2015-08-19 08:02:50    84.6
2015-08-19 08:03:00    89.4
2015-08-19 08:03:10    86.2
2015-08-19 08:03:20    86.4
2015-08-19 08:03:30    85.0
2015-08-19 08:03:40    85.6
2015-08-19 08:03:50    83.8
2015-08-19 08:04:00    87.6
2015-08-19 08:04:10    85.0
2015-08-19 08:04:20    83.6
2015-08-19 08:04:30    81.2
2015-08-19 08:04:40    84.0
2015-08-19 08:04:50    85.8
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-08-19 08:05:00    83.255034
2015-08-19 08:10:00    83.181208
2015-08-19 08:15:00    86.263889
2015-08-19 08:20:00    86.260000
2015-08-19 08:25:00    86.180000
2015-08-19 08:30:00    85.553333
2015-08-19 08:35:00    85.248322
2015-08-19 08:40:00    85.364865
2015-08-19 08:45:00    85.753333
2015-08-19 08:50:00    86.181208
2015-08-19 08:55:00    84.480000
2015-08-19 09:00:00    86.766667
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-08-19 09:05:00    85.897550
2015-08-19 09:20:00    84.924829
2015-08-19 09:35:00    86.071111
2015-08-19 09:50:00    86.155902
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-08-19 10:05:00    87.619577
2015-08-19 10:35:00    88.339266
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-08-19 11:05:00    88.799778
2015-08-19 12:05:00    88.370968
2015-08-19 13:05:00    85.498888
2015-08-19 14:05:00    86.381879
2015-08-19 15:05:00    85.610670
2015-08-19 16:05:00    83.878857
2015-08-19 17:05:00    85.159910
2015-08-19 18:05:00    83.989518
2015-08-19 19:05:00    87.236842
2015-08-19 20:05:00    86.445317
2015-08-19 21:05:00    86.389292
2015-08-19 22:05:00    86.203003
2015-08-19 23:05:00    88.879800
2015-08-20 00:05:00    87.481379
2015-08-20 01:05:00    88.912681
2015-08-20 02:05:00    88.195217
2015-08-20 03:05:00    85.806452
2015-08-20 04:05:00    85.118465
2015-08-20 05:05:00    88.684093
2015-08-20 06:05:00    88.056698
2015-08-20 07:05:00    88.383204
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('ROP009_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 13:57:13
Post-Exam Data Recording Length	1 days 00:29:09
Total Data Recording Length	1 days 14:30:22