Masimo Analysis

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

a) Headings on Row 1
b) Open the csv file through Notepad or TextEdit and delete extra 
row commas (non-printable characters)
c) There are always Dates in Column A and Time in Column B. 
d) There might be a row that says "Time Gap Present". Delete this row from Notepad 
or TextEdit

In [5]:
#the usual beginning
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
from datetime import datetime, timedelta
from pandas import concat

#define any string with 'C' as NaN
def readD(val):
    if 'C' in val:
        return np.nan
    return val

Import File into Python

Change File Name!


In [6]:
df = pd.read_csv('/Users/John/Dropbox/LLU/ROP/Pulse Ox/ROP007PO.csv',
            parse_dates={'timestamp': ['Date','Time']},
            index_col='timestamp',
            usecols=['Date', 'Time', 'SpO2', 'PR', 'PI', 'Exceptions'],
            na_values=['0'],
            converters={'Exceptions':  readD}
            )

#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 [7]:
#dfclean = df[27:33][df[27:33].loc[:, ['SpO2', 'PR', 'PI', 'Exceptions']].apply(pd.notnull).all(1)]
#clean the dataframe to get rid of rows that have NaN for PI purposes
df_clean = df[df.loc[:, ['PI', 'Exceptions']].apply(pd.notnull).all(1)]

In [8]:
"""Pulse ox date/time is 1 mins and 32 seconds faster than phone. Have to correct for it."""

TC = timedelta(minutes=1, seconds=32)

Set Date and Time of ROP Exam and Eye Drops


In [9]:
df_first = df.first_valid_index() #get the first number from index

Y = datetime(2015, 6, 30, 19, 2, 48)
#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, 7, 1, 8, 30)+TC
# W = first eye drop starts
X = datetime(2015, 7, 1, 9, 49)+TC
# X = ROP Exam Started
Z = datetime(2015, 7, 1, 9, 53)+TC
# Z = ROP Exam Ended

df_last = df.last_valid_index() #get the last number from index

Q = datetime(2015, 7, 1, 13, 51, 19)
#Q = pd.to_datetime(df_last) 

# Q = TIME DATA COLLECTION ENDED / Last Data point on CSV

In [10]:
df_clean[W:X]


Out[10]:
SpO2 PR PI Exceptions
timestamp
2015-07-01 08:31:33 100 156 0.8 8000
2015-07-01 08:31:35 100 155 0.8 8000
2015-07-01 08:31:37 100 153 0.8 8000
2015-07-01 08:31:39 100 156 0.8 8000
2015-07-01 08:31:41 100 154 0.8 8000
2015-07-01 08:31:43 100 154 0.8 8000
2015-07-01 08:31:45 100 153 0.8 8000
2015-07-01 08:31:47 100 155 0.8 8000
2015-07-01 08:31:49 100 155 0.8 8000
2015-07-01 08:31:51 100 155 1.4 8000
2015-07-01 08:31:53 100 155 1.7 8000
2015-07-01 08:31:55 100 156 1.9 8000
2015-07-01 08:31:57 100 160 1.4 8000
2015-07-01 08:31:59 100 160 0.6 8000
2015-07-01 08:32:01 100 161 0.4 8000
2015-07-01 08:32:03 100 161 0.4 8000
2015-07-01 08:32:05 100 167 0.4 8000
2015-07-01 08:32:07 100 169 0.4 8000
2015-07-01 08:32:09 100 173 0.7 8000
2015-07-01 08:32:11 100 170 0.8 8000
2015-07-01 08:32:13 100 156 0.8 8000
2015-07-01 08:32:15 100 152 0.8 8000
2015-07-01 08:32:17 100 147 0.9 8000
2015-07-01 08:32:19 100 152 0.9 8000
2015-07-01 08:32:21 100 154 0.8 8000
2015-07-01 08:32:23 99 154 0.8 8000
2015-07-01 08:32:25 99 157 0.8 8000
2015-07-01 08:32:27 98 156 0.8 8000
2015-07-01 08:32:29 99 156 0.8 8000
2015-07-01 08:32:31 99 156 0.8 8000
... ... ... ... ...
2015-07-01 09:49:33 97 171 0.3 8000
2015-07-01 09:49:35 97 171 0.3 8000
2015-07-01 09:49:37 98 171 0.3 8000
2015-07-01 09:49:39 99 176 0.3 8000
2015-07-01 09:49:41 99 176 0.3 8000
2015-07-01 09:49:43 99 177 0.3 8000
2015-07-01 09:49:45 99 179 0.3 8000
2015-07-01 09:49:47 99 179 0.3 8000
2015-07-01 09:49:49 99 179 0.3 8000
2015-07-01 09:49:51 98 185 0.6 8000
2015-07-01 09:49:53 98 190 0.6 8000
2015-07-01 09:49:55 98 194 0.5 8000
2015-07-01 09:49:57 98 188 0.5 8000
2015-07-01 09:49:59 98 184 0.5 8000
2015-07-01 09:50:01 98 183 0.5 8000
2015-07-01 09:50:03 98 183 0.6 8000
2015-07-01 09:50:05 98 183 0.6 8000
2015-07-01 09:50:07 97 180 0.6 8000
2015-07-01 09:50:09 97 179 0.7 8000
2015-07-01 09:50:11 99 180 0.7 8000
2015-07-01 09:50:13 100 180 0.7 8000
2015-07-01 09:50:15 100 180 0.7 8000
2015-07-01 09:50:17 100 183 0.7 8000
2015-07-01 09:50:19 100 181 0.6 8000
2015-07-01 09:50:21 100 181 0.6 8000
2015-07-01 09:50:23 100 182 0.6 8000
2015-07-01 09:50:25 100 183 0.5 8000
2015-07-01 09:50:27 100 182 0.5 8000
2015-07-01 09:50:29 100 182 0.5 8000
2015-07-01 09:50:31 100 180 0.5 8000

2315 rows × 4 columns

Baseline Averages


In [11]:
avg0PI = df_clean.PI[Y:W].mean()
avg0O2 = df.SpO2[Y:W].mean()
avg0PR = df.PR[Y:W].mean()

print 'Baseline Averages\n', 'PI :\t',avg0PI, '\nSpO2 :\t',avg0O2,'\nPR :\t',avg0PR,
#df.std() for standard deviation


Baseline Averages
PI :	0.902966540186 
SpO2 :	99.7222149152 
PR :	170.694544024

In [12]:
str(avg0PI)


Out[12]:
'0.902966540186'

In [ ]:

Average q 5 Min for 1 hour after 1st Eye Drops


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

def perdeltadrop(start, end, delta):
    rdrop = []
    curr = start
    while curr < end:
        rdrop.append(curr)
        curr += delta
    return rdrop
    
dfdropPI = df_clean.PI[W:W+timedelta(hours=1)]
dfdropO2 = df.SpO2[W:W+timedelta(hours=1)]
dfdropPR = df.PR[W:W+timedelta(hours=1)]
windrop = timedelta(minutes=5)#make the range
rdrop = perdeltadrop(W, W+timedelta(minutes=15), windrop)

avgdropPI = Series(index = rdrop, name = 'PI DurEyeD')
avgdropO2 = Series(index = rdrop, name = 'SpO2 DurEyeD')
avgdropPR = Series(index = rdrop, name = 'PR DurEyeD')

for i in rdrop:
    avgdropPI[i] = dfdropPI[i:(i+windrop)].mean()
    avgdropO2[i] = dfdropO2[i:(i+windrop)].mean()
    avgdropPR[i] = dfdropPR[i:(i+windrop)].mean()
    
resultdrops = concat([avgdropPI, avgdropO2, avgdropPR], axis=1, join='inner')
print resultdrops


                     PI DurEyeD  SpO2 DurEyeD  PR DurEyeD
2015-07-01 08:31:32    0.803252     99.393333  157.753333
2015-07-01 08:36:32    0.665333     99.986667  153.193333
2015-07-01 08:41:32    0.518000     99.706667  155.700000

Average Every 10 Sec During ROP Exam for first 4 minutes


In [14]:
#AVERAGE DURING ROP EXAM FOR FIRST FOUR MINUTES
def perdelta1(start, end, delta):
    r1 = []
    curr = start
    while curr < end:
        r1.append(curr)
        curr += delta
    return r1

df1PI = df_clean.PI[X:X+timedelta(minutes=4)]
df1O2 = df.SpO2[X:X+timedelta(minutes=4)]
df1PR = df.PR[X:X+timedelta(minutes=4)]
win1 = timedelta(seconds=10) #any unit of time & make the range

r1 = perdelta1(X, X+timedelta(minutes=4), win1)

#make the series to store
avg1PI = Series(index = r1, name = 'PI DurEx')
avg1O2 = Series(index = r1, name = 'SpO2 DurEx')
avg1PR = Series(index = r1, name = 'PR DurEX')
#average!
for i1 in r1:
    avg1PI[i1] = df1PI[i1:(i1+win1)].mean()
    avg1O2[i1] = df1O2[i1:(i1+win1)].mean()
    avg1PR[i1] = df1PR[i1:(i1+win1)].mean()

result1 = concat([avg1PI, avg1O2, avg1PR], axis=1, join='inner')
print result1


                     PI DurEx  SpO2 DurEx  PR DurEX
2015-07-01 09:50:32      0.56       100.0     178.6
2015-07-01 09:50:42      0.56       100.0     170.2
2015-07-01 09:50:52      0.52       100.0     172.8
2015-07-01 09:51:02      0.60       100.0     127.8
2015-07-01 09:51:12      0.72        98.8      74.0
2015-07-01 09:51:22      0.44        96.0     106.8
2015-07-01 09:51:32      0.44        92.4     181.0
2015-07-01 09:51:42       NaN        94.6     185.2
2015-07-01 09:51:52      0.70        97.8     183.4
2015-07-01 09:52:02      0.60        98.0     177.4
2015-07-01 09:52:12      0.68        98.8     182.2
2015-07-01 09:52:22      0.80       100.0     183.6
2015-07-01 09:52:32      1.08        95.2     133.8
2015-07-01 09:52:42      0.86        89.0     180.0
2015-07-01 09:52:52      0.72        98.0     154.6
2015-07-01 09:53:02      0.76        89.6     169.8
2015-07-01 09:53:12      0.76        97.4     173.8
2015-07-01 09:53:22      0.66        99.0     178.0
2015-07-01 09:53:32      0.68       100.0     175.0
2015-07-01 09:53:42      0.66        99.6     177.2
2015-07-01 09:53:52      0.66        99.8     170.2
2015-07-01 09:54:02      0.52       100.0     176.6
2015-07-01 09:54:12      0.54       100.0     180.6
2015-07-01 09:54:22      0.56       100.0     186.8

Average Every 5 Mins Hour 1-2 After ROP Exam


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

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

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

df2PI = df_clean.PI[Z:(Z+timedelta(hours=1))]
df2O2 = df.SpO2[Z:(Z+timedelta(hours=1))]
df2PR = df.PR[Z:(Z+timedelta(hours=1))]
win2 = timedelta(minutes=5) #any unit of time, make the range

r2 = perdelta2(Z, (Z+timedelta(hours=1)), win2) #define the average using function

#make the series to store
avg2PI = Series(index = r2, name = 'PI q5MinHr1')
avg2O2 = Series(index = r2, name = 'O2 q5MinHr1')
avg2PR = Series(index = r2, name = 'PR q5MinHr1')

#average!
for i2 in r2:
    avg2PI[i2] = df2PI[i2:(i2+win2)].mean()
    avg2O2[i2] = df2O2[i2:(i2+win2)].mean()
    avg2PR[i2] = df2PR[i2:(i2+win2)].mean()

result2 = concat([avg2PI, avg2O2, avg2PR], axis=1, join='inner')
print result2


                     PI q5MinHr1  O2 q5MinHr1  PR q5MinHr1
2015-07-01 09:54:32     0.578000    99.466667   165.953333
2015-07-01 09:59:32     1.362810    99.640000   171.413333
2015-07-01 10:04:32     0.962000    99.993333   186.400000
2015-07-01 10:09:32     0.733333    99.846667   175.840000
2015-07-01 10:14:32     1.172000    99.906667   158.686667
2015-07-01 10:19:32     1.460667    99.906667   158.420000
2015-07-01 10:24:32     1.574667    99.973333   159.653333
2015-07-01 10:29:32     1.904000    99.880000   156.746667
2015-07-01 10:34:32     1.606000    99.606667   174.020000
2015-07-01 10:39:32     1.683333    99.786667   164.940000
2015-07-01 10:44:32     1.366667    99.886667   166.066667
2015-07-01 10:49:32     1.437333    99.953333   159.966667

Average Every 15 Mins Hour 2-3 After ROP Exam


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

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

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

df3PI = df_clean.PI[(Z+timedelta(hours=1)):(Z+timedelta(hours=2))]
df3O2 = df.SpO2[(Z+timedelta(hours=1)):(Z+timedelta(hours=2))]
df3PR = df.PR[(Z+timedelta(hours=1)):(Z+timedelta(hours=2))]
win3 = timedelta(minutes=15) #any unit of time, make the range

r3 = perdelta3((Z+timedelta(hours=1)), (Z+timedelta(hours=2)), win3)

#make the series to store
avg3PI = Series(index = r3, name = 'PI q15MinHr2')
avg3O2 = Series(index = r3, name = 'O2 q15MinHr2')
avg3PR = Series(index = r3, name = 'PR q15MinHr2')

#average!
for i3 in r3:
    avg3PI[i3] = df3PI[i3:(i3+win3)].mean()
    avg3O2[i3] = df3O2[i3:(i3+win3)].mean()
    avg3PR[i3] = df3PR[i3:(i3+win3)].mean()
    
result3 = concat([avg3PI, avg3O2, avg3PR], axis=1, join='inner')
print result3


                     PI q15MinHr2  O2 q15MinHr2  PR q15MinHr2
2015-07-01 10:54:32      1.812889     99.964444    154.808889
2015-07-01 11:09:32      1.230193     99.633333    175.817778
2015-07-01 11:24:32      1.395024     99.562222    171.086667
2015-07-01 11:39:32      0.850667     99.635556    160.300000

Average Every 30 Mins Hour 3-4 After ROP Exam


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

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

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

df4PI = df_clean.PI[(Z+timedelta(hours=2)):(Z+timedelta(hours=3))]
df4O2 = df.SpO2[(Z+timedelta(hours=2)):(Z+timedelta(hours=3))]
df4PR = df.PR[(Z+timedelta(hours=2)):(Z+timedelta(hours=3))]
win4 = timedelta(minutes=30) #any unit of time, make the range

r4 = perdelta4((Z+timedelta(hours=2)), (Z+timedelta(hours=3)), win4)

#make the series to store
avg4PI = Series(index = r4, name = 'PI q30MinHr3')
avg4O2 = Series(index = r4, name = 'O2 q30MinHr3')
avg4PR = Series(index = r4, name = 'PR q30MinHr3')

#average!
for i4 in r4:
    avg4PI[i4] = df4PI[i4:(i4+win4)].mean()
    avg4O2[i4] = df4O2[i4:(i4+win4)].mean()
    avg4PR[i4] = df4PR[i4:(i4+win4)].mean()
    
result4 = concat([avg4PI, avg4O2, avg4PR], axis=1, join='inner')
print result4


                     PI q30MinHr3  O2 q30MinHr3  PR q30MinHr3
2015-07-01 11:54:32      0.672444     99.975556    158.583333
2015-07-01 12:24:32      0.738556     99.997778    159.124444

Average Every Hour 4-24 Hours Post ROP Exam


In [18]:
#AVERAGE EVERY 60 MINUTES 4-24 HOURS AFTER ROP EXAM

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

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

df5PI = df_clean.PI[(Z+timedelta(hours=3)):(Z+timedelta(hours=24))]
df5O2 = df.SpO2[(Z+timedelta(hours=3)):(Z+timedelta(hours=24))]
df5PR = df.PR[(Z+timedelta(hours=3)):(Z+timedelta(hours=24))]
win5 = timedelta(minutes=60) #any unit of time, make the range

r5 = perdelta5((Z+timedelta(hours=3)), (Z+timedelta(hours=24)), win5)

#make the series to store
avg5PI = Series(index = r5, name = 'PI q60MinHr4+')
avg5O2 = Series(index = r5, name = 'O2 q60MinHr4+')
avg5PR = Series(index = r5, name = 'PR q60MinHr4+')

#average!
for i5 in r5:
    avg5PI[i5] = df5PI[i5:(i5+win5)].mean()
    avg5O2[i5] = df5O2[i5:(i5+win5)].mean()
    avg5PR[i5] = df5PR[i5:(i5+win5)].mean()

result5 = concat([avg5PI, avg5O2, avg5PR], axis=1, join='inner')
print result5


                     PI q60MinHr4+  O2 q60MinHr4+  PR q60MinHr4+
2015-07-01 12:54:32       1.344689      99.778782     160.834138
2015-07-01 13:54:32            NaN            NaN            NaN
2015-07-01 14:54:32            NaN            NaN            NaN
2015-07-01 15:54:32            NaN            NaN            NaN
2015-07-01 16:54:32            NaN            NaN            NaN
2015-07-01 17:54:32            NaN            NaN            NaN
2015-07-01 18:54:32            NaN            NaN            NaN
2015-07-01 19:54:32            NaN            NaN            NaN
2015-07-01 20:54:32            NaN            NaN            NaN
2015-07-01 21:54:32            NaN            NaN            NaN
2015-07-01 22:54:32            NaN            NaN            NaN
2015-07-01 23:54:32            NaN            NaN            NaN
2015-07-02 00:54:32            NaN            NaN            NaN
2015-07-02 01:54:32            NaN            NaN            NaN
2015-07-02 02:54:32            NaN            NaN            NaN
2015-07-02 03:54:32            NaN            NaN            NaN
2015-07-02 04:54:32            NaN            NaN            NaN
2015-07-02 05:54:32            NaN            NaN            NaN
2015-07-02 06:54:32            NaN            NaN            NaN
2015-07-02 07:54:32            NaN            NaN            NaN
2015-07-02 08:54:32            NaN            NaN            NaN

Mild, Moderate, and Severe Desaturation Events


In [19]:
df_O2_pre = df[Y:W]


#Find count of these ranges
below = 0 # v <=80
middle = 0 #v >= 81 and v<=84
above = 0 #v >=85 and v<=89
ls = []

b_dict = {}
m_dict = {}
a_dict = {}

for i, v in df_O2_pre['SpO2'].iteritems():
    
    if v <= 80: #below block
        
        if not ls: 
            ls.append(v)
        else:
            if ls[0] >= 81: #if the range before was not below 80

                if len(ls) >= 5: #if the range was greater than 10 seconds, set to 5 because data points are every 2

                    if ls[0] <= 84: #was it in the middle range?
                        m_dict[middle] = ls
                        middle += 1
                        ls = [v]
                    elif ls[0] >= 85 and ls[0] <=89: #was it in the above range?
                        a_dict[above] = ls
                        above += 1
                        ls = [v]

                else: #old list wasn't long enough to count
                    ls = [v]
            else: #if in the same range
                ls.append(v)
                
    elif v >= 81 and v<= 84: #middle block
        
        if not ls:
            ls.append(v)
        else:
            if ls[0] <= 80 or (ls[0]>=85 and ls[0]<= 89): #if not in the middle range
                if len(ls) >= 5: #if range was greater than 10 seconds

                    if ls[0] <= 80: #was it in the below range?
                        b_dict[below] = ls
                        below += 1
                        ls = [v]
                    elif ls[0] >= 85 and ls[0] <=89: #was it in the above range?
                        a_dict[above] = ls
                        above += 1
                        ls = [v]
                else: #old list wasn't long enough to count
                    ls = [v]

            else:
                ls.append(v)
    
    elif v >= 85 and v <=89: #above block
        
        if not ls:
            ls.append(v)
        else:
            if ls[0] <=84 : #if not in the above range

                if len(ls) >= 5: #if range was greater than 
                    if ls[0] <= 80: #was it in the below range?
                        b_dict[below] = ls
                        below += 1
                        ls = [v]
                    elif ls[0] >= 81 and ls[0] <=84: #was it in the middle range?
                        m_dict[middle] = ls
                        middle += 1
                        ls = [v]
                else: #old list wasn't long enough to count
                    ls = [v]
            else:
                ls.append(v)
    
    else: #v>90 or something else weird. start the list over
        ls = []
#final list check
if len(ls) >= 5:
    if ls[0] <= 80: #was it in the below range?
        b_dict[below] = ls
        below += 1
        ls = [v]
    elif ls[0] >= 81 and ls[0] <=84: #was it in the middle range?
        m_dict[middle] = ls
        middle += 1
        ls = [v]
    elif ls[0] >= 85 and ls[0] <=89: #was it in the above range?
        a_dict[above] = ls
        above += 1
        
b_len = 0.0
for key, val in b_dict.iteritems():
    b_len += len(val)

m_len = 0.0
for key, val in m_dict.iteritems():
    m_len += len(val)
    
a_len = 0.0
for key, val in a_dict.iteritems():
    a_len += len(val)

In [20]:
df_O2_post = df[Z:Q]


#Find count of these ranges
below2 = 0 # v <=80
middle2= 0 #v >= 81 and v<=84
above2 = 0 #v >=85 and v<=89
ls2 = []

b_dict2 = {}
m_dict2 = {}
a_dict2 = {}

for i2, v2 in df_O2_post['SpO2'].iteritems():
    
    if v2 <= 80: #below block
        
        if not ls2: 
            ls2.append(v2)
        else:
            if ls2[0] >= 81: #if the range before was not below 80

                if len(ls2) >= 5: #if the range was greater than 10 seconds, set to 5 because data points are every 2

                    if ls2[0] <= 84: #was it in the middle range?
                        m_dict2[middle2] = ls2
                        middle2 += 1
                        ls2 = [v2]
                    elif ls2[0] >= 85 and ls2[0] <=89: #was it in the above range?
                        a_dict2[above2] = ls2
                        above2 += 1
                        ls2 = [v2]

                else: #old list wasn't long enough to count
                    ls2 = [v2]
            else: #if in the same range
                ls2.append(v2)
                
    elif v2 >= 81 and v2<= 84: #middle block
        
        if not ls2:
            ls2.append(v2)
        else:
            if ls2[0] <= 80 or (ls2[0]>=85 and ls2[0]<= 89): #if not in the middle range
                if len(ls2) >= 5: #if range was greater than 10 seconds

                    if ls2[0] <= 80: #was it in the below range?
                        b_dict2[below2] = ls2
                        below2 += 1
                        ls2 = [v2]
                    elif ls2[0] >= 85 and ls2[0] <=89: #was it in the above range?
                        a_dict2[above2] = ls2
                        above2 += 1
                        ls2 = [v2]
                else: #old list wasn't long enough to count
                    ls2 = [v2]

            else:
                ls2.append(v2)
    
    elif v2 >= 85 and v2 <=89: #above block
        
        if not ls2:
            ls2.append(v2)
        else:
            if ls2[0] <=84 : #if not in the above range

                if len(ls2) >= 5: #if range was greater than 
                    if ls2[0] <= 80: #was it in the below range?
                        b_dict2[below2] = ls2
                        below2 += 1
                        ls2 = [v2]
                    elif ls2[0] >= 81 and ls2[0] <=84: #was it in the middle range?
                        m_dict2[middle2] = ls2
                        middle2 += 1
                        ls2 = [v2]
                else: #old list wasn't long enough to count
                    ls2 = [v2]
            else:
                ls2.append(v2)
    
    else: #v2>90 or something else weird. start the list over
        ls2 = []
#final list check
if len(ls2) >= 5:
    if ls2[0] <= 80: #was it in the below range?
        b_dict2[below2] = ls2
        below2 += 1
        ls2= [v2]
    elif ls2[0] >= 81 and ls2[0] <=84: #was it in the middle range?
        m_dict2[middle2] = ls2
        middle2 += 1
        ls2 = [v2]
    elif ls2[0] >= 85 and ls2[0] <=89: #was it in the above range?
        a_dict2[above2] = ls2
        above2 += 1
        
b_len2 = 0.0
for key, val2 in b_dict2.iteritems():
    b_len2 += len(val2)

m_len2 = 0.0
for key, val2 in m_dict2.iteritems():
    m_len2 += len(val2)
    
a_len2 = 0.0
for key, val2 in a_dict2.iteritems():
    a_len2 += len(val2)

In [21]:
#print results from count and min

print "Desat Counts for X mins\n"    
print "Pre Mild Desat (85-89) Count: %s\t" %above, "for %s min" %((a_len*2)/60.)
print "Pre Mod Desat (81-84) Count: %s\t" %middle, "for %s min" %((m_len*2)/60.) 
print "Pre Sev Desat (=< 80) Count: %s\t" %below, "for %s min\n" %((b_len*2)/60.)

print "Post Mild Desat (85-89) Count: %s\t" %above2, "for %s min" %((a_len2*2)/60.) 
print "Post Mod Desat (81-84) Count: %s\t" %middle2, "for %s min" %((m_len2*2)/60.) 
print "Post Sev Desat (=< 80) Count: %s\t" %below2, "for %s min\n" %((b_len2*2)/60.) 



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]

PreMild = ['Pre Mild Desats \t',(above), 'for', (a_len*2)/60., 'mins']
PreMod = ['Pre Mod Desats \t',(middle), 'for', (m_len*2)/60., 'mins']
PreSev = ['Pre Sev Desats \t',(below), 'for', (b_len*2)/60., 'mins']
PreDesats = [PreMild, PreMod, PreSev]

PostMild = ['Post Mild Desats \t',(above2), 'for', (a_len2*2)/60., 'mins']
PostMod = ['Post Mod Desats \t',(middle2), 'for', (m_len2*2)/60., 'mins']
PostSev = ['Post Sev Desats \t',(below2), 'for', (b_len2*2)/60., 'mins']
PostDesats = [PostMild, PostMod, PostSev]

#creating a list for recording time length


Desat Counts for X mins

Pre Mild Desat (85-89) Count: 2	for 0.433333333333 min
Pre Mod Desat (81-84) Count: 1	for 0.266666666667 min
Pre Sev Desat (=< 80) Count: 2	for 0.833333333333 min

Post Mild Desat (85-89) Count: 0	for 0.0 min
Post Mod Desat (81-84) Count: 0	for 0.0 min
Post Sev Desat (=< 80) Count: 0	for 0.0 min

Data Recording Time!
**********
Pre-Exam Data Recording Length	14:47:44
Post-Exam Data Recording Length	3:56:47
Total Data Recording Length	18:48:31

In [22]:
#did it count check sort correctly? get rid of the ''' if you want to check your values
'''
print "Mild check"
for key, val in b_dict.iteritems():
    print all(i <=80 for i in val)

print "Moderate check"
for key, val in m_dict.iteritems():
    print all(i >= 81 and i<=84 for i in val)
    
print "Severe check"
for key, val in a_dict.iteritems():
    print all(i >= 85 and i<=89 for i in val)
'''


Out[22]:
'\nprint "Mild check"\nfor key, val in b_dict.iteritems():\n    print all(i <=80 for i in val)\n\nprint "Moderate check"\nfor key, val in m_dict.iteritems():\n    print all(i >= 81 and i<=84 for i in val)\n    \nprint "Severe check"\nfor key, val in a_dict.iteritems():\n    print all(i >= 85 and i<=89 for i in val)\n'

In [23]:
print Q
print Z


2015-07-01 13:51:19
2015-07-01 09:54:32

Export to CSV


In [38]:
leest = [(avg0PI), 'PI Start']

In [39]:
print leest[:1]


[0.9029665401862712]

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

with open('test.csv', 'w') as f:    #CHANGE CSV FILE NAME, saves in same directory
    writer = csv.writer(f, dialect=excel_tab)
    #writer.writerow(['PI, O2, PR']) accidently found this out but using commas = gives me columns YAY! fix this
    #to make code look nice ok nice
    writer.writerow([leest])

In [ ]: