Jump to content

User:Kithira/Course Pages/CSCI 12/Assignment 2/Group 3/Homework 4

From Wikipedia, the free encyclopedia

Code

[edit]
#Intensity Classification, written by Mo Soussi, Roger Vargas, Hartley Greenwald

from matplotlib.pylab import plot, show, arange, ylabel

def summarizedvalue(x,y,z):
    return abs(((x**2 + y**2 + z**2)**0.5)-1)

def filter(list,j):
    if abs(list[j-1]-list[j]) > 1.5:
        list[j-1] = list[j]
    return list[j]
    

infile = open('/usr/local/share/cs12/Test3.csv', 'r')
lines = infile.readlines()[100:]
infile.close()

list = []
ys = []
for i in range(len(lines)):
    x = float(lines[i].split(",")[1])
    y = float(lines[i].split(",")[2])
    z = float(lines[i].split(",")[3])
    sumval = summarizedvalue(x,y,z)
    list.append(sumval)  

m = 0
sum = 0
listsummarizedvalue = []
while m < len(list):
    sum += list[m]  
    m += 1
    if m % 40 == 0:
        listsummarizedvalue.append(sum)
        sum = 0

m = 0
sum = 0
averageminute = []
while m < len(listsummarizedvalue):
    sum += listsummarizedvalue[m]
    m += 1
    if m % 60 == 0:
        averageminute.append(sum/60)
        sum = 0

classific = 0
for i in range(len(averageminute)):
    classific += averageminute[i]

print classific, '= intensity classification number'

if classific < 7500:
    print 'This person is Sedentary'
    
elif classific >= 7500 and classific <22500:
    print 'This person is Moderate'
    
elif classific >= 22500:
    print 'This person is Vigorous'

Explanation

[edit]

Function 1: Summarizedvalue

[edit]
def summarizedvalue(x,y,z):
    return abs(((x**2 + y**2 + z**2)**0.5)-1)

This function takes three arguments and returns their summarized value. X represents the acceleration in the x-direction, y represents the acceleration in the y-direction and z is the acceleration in the z-direction. Their summarized value, which is the absolute value of the square root of the sum of their squares minus 1, represents the equivalent acceleration in 3-dimensions.

Function 2: Filter

[edit]
def filter(list,j):
    if abs(list[j-1]-list[j]) > 1.5:
        list[j-1] = list[j]
    return list[j]

Filter takes two arguments: a list and an integer which represents the order of a potential spike in the list. If the difference between the element j and the element j-1 is greater than a certain limit, the function assigns to the element j the value of the element j-1.

Main Program

[edit]
infile = open('/usr/local/share/cs12/Test3.csv', 'r')
lines = infile.readlines()[100:]
infile.close()

This set of instructions reads the file where data is stored. The first 100 lines are skipped.

list = []
ys = []
for i in range(len(lines)):
    x = float(lines[i].split(",")[1])
    y = float(lines[i].split(",")[2])
    z = float(lines[i].split(",")[3])
    sumval = summarizedvalue(x,y,z)
    list.append(sumval)

This set of instructions goes through every row in the data file, splits x, y, and z from every row, computes their summarized value and stores it in a list.

m = 0
sum = 0
listsummarizedvalue = []
while m < len(list):
    sum += list[m]  
    m += 1
    if m % 40 == 0:
        listsummarizedvalue.append(sum)
        sum = 0

After computing summarized values for every 1/40th of a second, we add up every 40 consecutive values to get one summarized value for every second. The goal behind this is to compress the original data file. First 0 is assigned to variables sum and m. The while loop runs as long as m is less than the length of the list of summarized values we created earlier. For every iteration, the mth value in list is added to sum, and 1 is added to m. When m reaches a multiple of 40, sum is added to listsummarizedvalue before being set back equal to zero to store the sum of the next 40 values, and so on until m is greater then the length of list. For the first 40 values for example, m starts out as 0 and the program starts adding the mth item to sum. When m is equal to 39, the 39th value is added up to sum, which means that (39-0+1)=40 values have been added to sum. After item 39 is added to sum, m becomes equal to 40 which satisfies the condition m % 40 == 0. At that moment, sum is appended to listsummarized and then is set back equal to zero and the program goes back to the while loop, with m starting out as 40. This second subloop stops when m is equal to 79, and so on.

m = 0
sum = 0
averageminute = []
while m < len(listsummarizedvalue):
    sum += listsummarizedvalue[m]
    m += 1
    if m % 60 == 0:
        averageminute.append(sum/60)
        sum = 0

This loop works like the one explained above. As long as m is less than the length of listsummarizedvalue, the mth value of listsummarizedvalue is added to the variable sum. Whenever we have 60 values ( m is a multiple of 60), sum divided by 60 (average activity per minute) is added to a new list, averageminute.

classific = 0
for i in range(len(averageminute)):
    classific += averageminute[i]
 
print classific, '= intensity classification number'
 
if classific < 7500:
    print 'This person is Sedentary'
 
elif classific >= 7500 and classific <22500:
    print 'This person is Moderate'
 
elif classific >= 22500:
    print 'This person is Vigorous'

Now all that's left for us to do is computing classific, the value that would allow us to classify our test subject. To do this we create a loop that goes through averageminute and add up all the values contained in it. We compare classific to 7500 and 22500. If classific is less than 7500, our subject is sedentary. If classific is between 7500 and 22500, our test subject is moderate. If classific is greater than 22500, then our subject is vigorous.