Skip to content

Commit

Permalink
made some changes to CalculationHandler and TwoIMUs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmw54 committed Apr 18, 2022
1 parent f4a5725 commit 0fa81d7
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 19 deletions.
Binary file modified .DS_Store
Binary file not shown.
64 changes: 64 additions & 0 deletions CSVs/CSVwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import csv
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '../api')
import CalculationHandler
import TwoIMUs
import IMUDataProcessing
import MoodClassifier


JSON_PATH = "../SavedJSONs/"
CSV_PATH = "./"
tail_file_name = "butterbean_4_16_alert-tail.json"
body_file_name = "butterbean_4_16_alert-body.json"
write_file_name = "butterbean_4_16_alert.csv"

tailFilteredData = IMUDataProcessing.filterFile(JSON_PATH + tail_file_name)
bodyFilteredData = IMUDataProcessing.filterFile(JSON_PATH + body_file_name)

quatsList, timesList = TwoIMUs.get_quats_from_filtered_data(tailFilteredData, bodyFilteredData)
xVecsList = TwoIMUs.quats_to_vectors(quatsList)


calculation_module = CalculationHandler.Calculation_Module()


# open the file in the write mode
with open(CSV_PATH + write_file_name, 'w') as f:
# create the csv writer
writer = csv.writer(f)
labelrow = ["Frequency","Amplitude","Pitches","Angles","Side Bias","Mood","Image Url","Timestamps"]
# write a row to the csv file
writer.writerow(labelrow)

for i in range(len(xVecsList)):
# window i
# row i+1
pitches, angles = calculation_module.get_pitches_angles_from_vectors(xVecsList[i])
frequency = calculation_module.calculate_frequency(angles, timesList[i])
amplitude = calculation_module.calculate_average_amplitude(angles)
side_bias = calculation_module.calculate_side_bias_from_vectors(xVecsList[i])
mood = MoodClassifier.get_mood(frequency, amplitude, pitches, angles, side_bias)
image_url = ""
row = [frequency, amplitude, pitches, angles, side_bias, mood, image_url, timesList[i]]
# write a row to the csv file
writer.writerow(row)



# with open('temp.csv') as csv_file:
# csv_reader = csv.reader(csv_file, delimiter=',')
# line_count = 0
# for row in csv_reader:
# row_count = 0
# for cell in row:
# print(cell)
# print(type(cell))
# if(row_count == 2):
# cell = cell[1:-1].split(", ")
# print(cell)
# print(type(cell))
# row_count+=1
# line_count+=1

82 changes: 82 additions & 0 deletions CSVs/butterbean_4_16_alert.csv

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions api/CalculationHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@ def plot_vectors(self, vectors, timestamps):
calculation_module = Calculation_Module()
#tail_data, body_data = FirebaseConfig.get_tail_and_body_data_from_firebase(folder_name, data_name, direction)

vectorsList, timestampsList = TwoIMUs.get_vectors_from_JSON()
quatsList, timestampsList = TwoIMUs.get_quats_from_JSON()
# IMUDataProcessing.plotFilterOutput(timestampsList[0], quatsList[0])
JSONpath = '../SavedJSONs/'
tail_file = "butterbean_4_16_happy-tail.json"
body_file = "butterbean_4_16_happy-body.json"

quatsList, timestampsList = TwoIMUs.get_quats_from_JSON(JSONpath + tail_file, JSONpath + body_file)
vectorsList = TwoIMUs.quats_to_vectors(quatsList)
for i in range(len(timestampsList)):
IMUDataProcessing.plotFilterOutput(timestampsList[i], quatsList[i])
# calculation_module.plot_vectors(vectorsList[0], timestampsList[0])

23 changes: 7 additions & 16 deletions api/TwoIMUs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ def get_quats_from_filtered_data(tailFilteredData, bodyFilteredData):
quatsList = []
timesList = []

# Note: body and tail measurements aren't taken at the exact same time, and through the interpolation process,
# they are slightly different lengths. I'm choosing to ignore this for now, but it should be addressed. The two
# series also don't seem to start at the same time. When the sample period is about 0.08 seconds, the body seems
# to start recording about 0.04 seconds before the tail

# Note: body and tail measurements aren't taken at the exact same time.
# I'm choosing to ignore this for now.
for i in range(len(tailFilteredData)):
tailTimes, tailQuats = tailFilteredData[i]
bodyTimes, bodyQuats = bodyFilteredData[i]
print(len(tailTimes), len(bodyTimes))

if len(tailTimes) != len(bodyTimes): raise ValueError("""Window %d (out of %d) for Body and tail have different number of timestamps
Body: %d timestamps
Expand Down Expand Up @@ -76,17 +72,12 @@ def get_vectors(body_data, tail_data):
return xVecsList, timesList


def get_quats_from_JSON():
JSONpath = '../SavedJSONs/'
tail_file = "april14run-tail.json"
body_file = "april14run-body.json"

tailFilteredData = IMUDataProcessing.filterFile(JSONpath + tail_file)
bodyFilteredData = IMUDataProcessing.filterFile(JSONpath + body_file)

def get_quats_from_JSON(tail_file, body_file):
tailFilteredData = IMUDataProcessing.filterFile(tail_file)
bodyFilteredData = IMUDataProcessing.filterFile(body_file)
return get_quats_from_filtered_data(tailFilteredData, bodyFilteredData)

def get_vectors_from_JSON():
quatsList, timesList = get_quats_from_JSON()
def get_vectors_from_JSON(tail_file, body_file):
quatsList, timesList = get_quats_from_JSON(tail_file, body_file)
xVecsList = quats_to_vectors(quatsList)
return xVecsList, timesList
Binary file added api/__pycache__/CalculationHandler.cpython-37.pyc
Binary file not shown.
Binary file added api/__pycache__/MoodClassifier.cpython-37.pyc
Binary file not shown.
Binary file modified api/__pycache__/TwoIMUs.cpython-37.pyc
Binary file not shown.

0 comments on commit 0fa81d7

Please sign in to comment.