<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 NIRS Analysis
For NIRS Analysis, make sure the data file is the right .csv format
Step1:  Export Data to CSV
Make sure the file name is changed!!!! ROP###Avgs_NIRS.csv
Step2:  Data Recording Time Length
<ASSISTANT_TASK:>
Python Code:
#the usual beginning
import pandas as pd
from pandas import Series, DataFrame
from datetime import datetime, timedelta
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.
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
# Baseline Average
df0 = df[Y:X]
avg0 = df0.mean()
print avg0
#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
#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
#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
#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
#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
Explanation: 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.
End of explanation
#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())
Explanation: Export Data to CSV
Make sure the file name is changed!!!! ROP###Avgs_NIRS.csv
End of explanation
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 
Explanation: Data Recording Time Length
End of explanation
<END_TASK>