-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathutils.py
813 lines (658 loc) · 31.4 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
'''
---------------------------------------------------------------------------
OpenCap processing: utils.py
---------------------------------------------------------------------------
Copyright 2022 Stanford University and the Authors
Author(s): Antoine Falisse, Scott Uhlrich
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import os
import requests
import urllib.request
import shutil
import numpy as np
import pandas as pd
import yaml
import pickle
import glob
import zipfile
import platform
import opensim
from utilsAPI import get_api_url
from utilsAuthentication import get_token
import matplotlib.pyplot as plt
from scipy.signal.windows import gaussian
API_URL = get_api_url()
API_TOKEN = get_token()
def download_file(url, file_name):
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
def get_session_json(session_id):
resp = requests.get(
API_URL + "sessions/{}/".format(session_id),
headers = {"Authorization": "Token {}".format(API_TOKEN)})
if resp.status_code == 500:
raise Exception('No server response. Likely not a valid session id.')
sessionJson = resp.json()
if 'trials' not in sessionJson.keys():
raise Exception('This session is not in your username, nor is it public. You do not have access.')
# Sort trials by time recorded.
def get_created_at(trial):
return trial['created_at']
sessionJson['trials'].sort(key=get_created_at)
return sessionJson
# Returns a list of all sessions of the user.
def get_user_sessions():
sessions = requests.get(
API_URL + "sessions/valid/",
headers = {"Authorization": "Token {}".format(API_TOKEN)}).json()
return sessions
# Returns a list of all sessions of the user.
# TODO: this also contains public sessions of other users.
def get_user_sessions_all(user_token=API_TOKEN):
sessions = requests.get(
API_URL + "sessions/",
headers = {"Authorization": "Token {}".format(user_token)}).json()
return sessions
# Returns a list of all subjects of the user.
def get_user_subjects(user_token=API_TOKEN):
subjects = requests.get(
API_URL + "subjects/",
headers = {"Authorization": "Token {}".format(user_token)}).json()
return subjects
# Returns a list of all sessions of a subject.
def get_subject_sessions(subject_id, user_token=API_TOKEN):
sessions = requests.get(
API_URL + "subjects/{}/".format(subject_id),
headers = {"Authorization": "Token {}".format(user_token)}).json()['sessions']
return sessions
def get_trial_json(trial_id):
trialJson = requests.get(
API_URL + "trials/{}/".format(trial_id),
headers = {"Authorization": "Token {}".format(API_TOKEN)}).json()
return trialJson
def get_neutral_trial_id(session_id):
session = get_session_json(session_id)
neutral_ids = [t['id'] for t in session['trials'] if t['name']=='neutral']
if len(neutral_ids)>0:
neutralID = neutral_ids[-1]
elif session['meta']['neutral_trial']:
neutralID = session['meta']['neutral_trial']['id']
else:
raise Exception('No neutral trial in session.')
return neutralID
def get_calibration_trial_id(session_id):
session = get_session_json(session_id)
calib_ids = [t['id'] for t in session['trials'] if t['name'] == 'calibration']
if len(calib_ids)>0:
calibID = calib_ids[-1]
elif session['meta']['sessionWithCalibration']:
calibID = get_calibration_trial_id(session['meta']['sessionWithCalibration']['id'])
else:
raise Exception('No calibration trial in session.')
return calibID
def get_camera_mapping(session_id, session_path):
calibration_id = get_calibration_trial_id(session_id)
trial = get_trial_json(calibration_id)
resultTags = [res['tag'] for res in trial['results']]
mappingPath = os.path.join(session_path,'Videos','mappingCamDevice.pickle')
os.makedirs(os.path.join(session_path,'Videos'), exist_ok=True)
if not os.path.exists(mappingPath):
mappingURL = trial['results'][resultTags.index('camera_mapping')]['media']
download_file(mappingURL, mappingPath)
def get_model_and_metadata(session_id, session_path):
neutral_id = get_neutral_trial_id(session_id)
trial = get_trial_json(neutral_id)
resultTags = [res['tag'] for res in trial['results']]
# Metadata.
metadataPath = os.path.join(session_path,'sessionMetadata.yaml')
if not os.path.exists(metadataPath) :
metadataURL = trial['results'][resultTags.index('session_metadata')]['media']
download_file(metadataURL, metadataPath)
# Model.
modelURL = trial['results'][resultTags.index('opensim_model')]['media']
modelName = modelURL[modelURL.rfind('-')+1:modelURL.rfind('?')]
modelFolder = os.path.join(session_path, 'OpenSimData', 'Model')
modelPath = os.path.join(modelFolder, modelName)
if not os.path.exists(modelPath):
os.makedirs(modelFolder, exist_ok=True)
download_file(modelURL, modelPath)
return modelName
def get_main_settings(session_folder,trial_name):
settings_path = os.path.join(session_folder,'MarkerData',
'Settings','settings_' + trial_name + '.yaml')
main_settings = import_metadata(settings_path)
return main_settings
def get_model_name_from_metadata(sessionFolder,appendText='_scaled'):
metadataPath = os.path.join(sessionFolder,'sessionMetadata.yaml')
if os.path.exists(metadataPath):
metadata = import_metadata(os.path.join(sessionFolder,'sessionMetadata.yaml'))
modelName = metadata['openSimModel'] + appendText + '.osim'
else:
raise Exception('Session metadata not found, could not identify OpenSim model.')
return modelName
def get_motion_data(trial_id, session_path):
trial = get_trial_json(trial_id)
trial_name = trial['name']
resultTags = [res['tag'] for res in trial['results']]
# Marker data.
if 'marker_data' in resultTags:
markerFolder = os.path.join(session_path, 'MarkerData')
markerPath = os.path.join(markerFolder, trial_name + '.trc')
os.makedirs(markerFolder, exist_ok=True)
if not os.path.exists(markerPath):
markerURL = trial['results'][resultTags.index('marker_data')]['media']
download_file(markerURL, markerPath)
# IK data.
if 'ik_results' in resultTags:
ikFolder = os.path.join(session_path, 'OpenSimData', 'Kinematics')
ikPath = os.path.join(ikFolder, trial_name + '.mot')
os.makedirs(ikFolder, exist_ok=True)
if not os.path.exists(ikPath):
ikURL = trial['results'][resultTags.index('ik_results')]['media']
download_file(ikURL, ikPath)
# Main settings
if 'main_settings' in resultTags:
settingsFolder = os.path.join(session_path, 'MarkerData', 'Settings')
settingsPath = os.path.join(settingsFolder, 'settings_' + trial_name + '.yaml')
os.makedirs(settingsFolder, exist_ok=True)
if not os.path.exists(settingsPath):
settingsURL = trial['results'][resultTags.index('main_settings')]['media']
download_file(settingsURL, settingsPath)
def get_geometries(session_path, modelName='LaiUhlrich2022_scaled'):
geometryFolder = os.path.join(session_path, 'OpenSimData', 'Model', 'Geometry')
try:
# Download.
os.makedirs(geometryFolder, exist_ok=True)
if 'Lai' in modelName:
modelType = 'LaiArnold'
vtpNames = [
'capitate_lvs','capitate_rvs','hamate_lvs','hamate_rvs',
'hat_jaw','hat_ribs_scap','hat_skull','hat_spine','humerus_lv',
'humerus_rv','index_distal_lvs','index_distal_rvs',
'index_medial_lvs', 'index_medial_rvs','index_proximal_lvs',
'index_proximal_rvs','little_distal_lvs','little_distal_rvs',
'little_medial_lvs','little_medial_rvs','little_proximal_lvs',
'little_proximal_rvs','lunate_lvs','lunate_rvs','l_bofoot',
'l_femur','l_fibula','l_foot','l_patella','l_pelvis','l_talus',
'l_tibia','metacarpal1_lvs','metacarpal1_rvs',
'metacarpal2_lvs','metacarpal2_rvs','metacarpal3_lvs',
'metacarpal3_rvs','metacarpal4_lvs','metacarpal4_rvs',
'metacarpal5_lvs','metacarpal5_rvs','middle_distal_lvs',
'middle_distal_rvs','middle_medial_lvs','middle_medial_rvs',
'middle_proximal_lvs','middle_proximal_rvs','pisiform_lvs',
'pisiform_rvs','radius_lv','radius_rv','ring_distal_lvs',
'ring_distal_rvs','ring_medial_lvs','ring_medial_rvs',
'ring_proximal_lvs','ring_proximal_rvs','r_bofoot','r_femur',
'r_fibula','r_foot','r_patella','r_pelvis','r_talus','r_tibia',
'sacrum','scaphoid_lvs','scaphoid_rvs','thumb_distal_lvs',
'thumb_distal_rvs','thumb_proximal_lvs','thumb_proximal_rvs',
'trapezium_lvs','trapezium_rvs','trapezoid_lvs','trapezoid_rvs',
'triquetrum_lvs','triquetrum_rvs','ulna_lv','ulna_rv']
else:
raise ValueError("Geometries not available for this model")
for vtpName in vtpNames:
url = 'https://mc-opencap-public.s3.us-west-2.amazonaws.com/geometries_vtp/{}/{}.vtp'.format(modelType, vtpName)
filename = os.path.join(geometryFolder, '{}.vtp'.format(vtpName))
download_file(url, filename)
except:
pass
def import_metadata(filePath):
myYamlFile = open(filePath)
parsedYamlFile = yaml.load(myYamlFile, Loader=yaml.FullLoader)
return parsedYamlFile
def download_kinematics(session_id, folder=None, trialNames=None):
# Login to access opencap data from server.
# Create folder.
if folder is None:
folder = os.getcwd()
os.makedirs(folder, exist_ok=True)
# Model and metadata.
neutral_id = get_neutral_trial_id(session_id)
get_motion_data(neutral_id, folder)
modelName = get_model_and_metadata(session_id, folder)
# Remove extension from modelName
modelName = modelName.replace('.osim','')
# Session trial names.
sessionJson = get_session_json(session_id)
sessionTrialNames = [t['name'] for t in sessionJson['trials']]
if trialNames != None:
[print(t + ' not in session trial names.')
for t in trialNames if t not in sessionTrialNames]
# Motion data.
loadedTrialNames = []
for trialDict in sessionJson['trials']:
if trialNames is not None and trialDict['name'] not in trialNames:
continue
trial_id = trialDict['id']
get_motion_data(trial_id,folder)
loadedTrialNames.append(trialDict['name'])
# Remove 'calibration' and 'neutral' from loadedTrialNames.
loadedTrialNames = [i for i in loadedTrialNames if i!='neutral' and i!='calibration']
# Geometries.
get_geometries(folder, modelName=modelName)
return loadedTrialNames, modelName
# Download pertinent trial data.
def download_trial(trial_id, folder, session_id=None):
trial = get_trial_json(trial_id)
if session_id is None:
session_id = trial['session_id']
os.makedirs(folder,exist_ok=True)
# download model
get_model_and_metadata(session_id, folder)
# download trc and mot
get_motion_data(trial_id,folder)
return trial['name']
# Get trial ID from name.
def get_trial_id(session_id,trial_name):
session = get_session_json(session_id)
trial_id = [t['id'] for t in session['trials'] if t['name'] == trial_name]
return trial_id[0]
# %% Storage file to numpy array.
def storage_to_numpy(storage_file, excess_header_entries=0):
"""Returns the data from a storage file in a numpy format. Skips all lines
up to and including the line that says 'endheader'.
Parameters
----------
storage_file : str
Path to an OpenSim Storage (.sto) file.
Returns
-------
data : np.ndarray (or numpy structure array or something?)
Contains all columns from the storage file, indexable by column name.
excess_header_entries : int, optional
If the header row has more names in it than there are data columns.
We'll ignore this many header row entries from the end of the header
row. This argument allows for a hacky fix to an issue that arises from
Static Optimization '.sto' outputs.
Examples
--------
Columns from the storage file can be obtained as follows:
>>> data = storage2numpy('<filename>')
>>> data['ground_force_vy']
"""
# What's the line number of the line containing 'endheader'?
f = open(storage_file, 'r')
header_line = False
for i, line in enumerate(f):
if header_line:
column_names = line.split()
break
if line.count('endheader') != 0:
line_number_of_line_containing_endheader = i + 1
header_line = True
f.close()
# With this information, go get the data.
if excess_header_entries == 0:
names = True
skip_header = line_number_of_line_containing_endheader
else:
names = column_names[:-excess_header_entries]
skip_header = line_number_of_line_containing_endheader + 1
data = np.genfromtxt(storage_file, names=names,
skip_header=skip_header)
return data
# %% Storage file to dataframe.
def storage_to_dataframe(storage_file, headers):
# Extract data
data = storage_to_numpy(storage_file)
out = pd.DataFrame(data=data['time'], columns=['time'])
for count, header in enumerate(headers):
out.insert(count + 1, header, data[header])
return out
# %% Load storage and output as dataframe or numpy
def load_storage(file_path,outputFormat='numpy'):
table = opensim.TimeSeriesTable(file_path)
data = table.getMatrix().to_numpy()
time = np.asarray(table.getIndependentColumn()).reshape(-1, 1)
data = np.hstack((time,data))
headers = ['time'] + list(table.getColumnLabels())
if outputFormat == 'numpy':
return data,headers
elif outputFormat == 'dataframe':
return pd.DataFrame(data, columns=headers)
else:
return None
# %% Numpy array to storage file.
def numpy_to_storage(labels, data, storage_file, datatype=None):
assert data.shape[1] == len(labels), "# labels doesn't match columns"
assert labels[0] == "time"
f = open(storage_file, 'w')
# Old style
if datatype is None:
f = open(storage_file, 'w')
f.write('name %s\n' %storage_file)
f.write('datacolumns %d\n' %data.shape[1])
f.write('datarows %d\n' %data.shape[0])
f.write('range %f %f\n' %(np.min(data[:, 0]), np.max(data[:, 0])))
f.write('endheader \n')
# New style
else:
if datatype == 'IK':
f.write('Coordinates\n')
elif datatype == 'ID':
f.write('Inverse Dynamics Generalized Forces\n')
elif datatype == 'GRF':
f.write('%s\n' %storage_file)
elif datatype == 'muscle_forces':
f.write('ModelForces\n')
f.write('version=1\n')
f.write('nRows=%d\n' %data.shape[0])
f.write('nColumns=%d\n' %data.shape[1])
if datatype == 'IK':
f.write('inDegrees=yes\n\n')
f.write('Units are S.I. units (second, meters, Newtons, ...)\n')
f.write("If the header above contains a line with 'inDegrees', this indicates whether rotational values are in degrees (yes) or radians (no).\n\n")
elif datatype == 'ID':
f.write('inDegrees=no\n')
elif datatype == 'GRF':
f.write('inDegrees=yes\n')
elif datatype == 'muscle_forces':
f.write('inDegrees=yes\n\n')
f.write('This file contains the forces exerted on a model during a simulation.\n\n')
f.write("A force is a generalized force, meaning that it can be either a force (N) or a torque (Nm).\n\n")
f.write('Units are S.I. units (second, meters, Newtons, ...)\n')
f.write('Angles are in degrees.\n\n')
f.write('endheader \n')
for i in range(len(labels)):
f.write('%s\t' %labels[i])
f.write('\n')
for i in range(data.shape[0]):
for j in range(data.shape[1]):
f.write('%20.8f\t' %data[i, j])
f.write('\n')
f.close()
def download_videos_from_server(session_id,trial_id,
isCalibration=False, isStaticPose=False,
trial_name= None, session_path = None):
if session_path is None:
data_dir = os.getcwd()
session_path = os.path.join(data_dir,'Data', session_id)
if not os.path.exists(session_path):
os.makedirs(session_path, exist_ok=True)
resp = requests.get("{}trials/{}/".format(API_URL,trial_id),
headers = {"Authorization": "Token {}".format(API_TOKEN)})
trial = resp.json()
if trial_name is None:
trial_name = trial['name']
trial_name = trial_name.replace(' ', '')
print("\nDownloading {}".format(trial_name))
# The videos are not always organized in the same order. Here, we save
# the order during the first trial processed in the session such that we
# can use the same order for the other trials.
if not os.path.exists(os.path.join(session_path, "Videos", 'mappingCamDevice.pickle')):
mappingCamDevice = {}
for k, video in enumerate(trial["videos"]):
os.makedirs(os.path.join(session_path, "Videos", "Cam{}".format(k), "InputMedia", trial_name), exist_ok=True)
video_path = os.path.join(session_path, "Videos", "Cam{}".format(k), "InputMedia", trial_name, trial_name + ".mov")
download_file(video["video"], video_path)
mappingCamDevice[video["device_id"].replace('-', '').upper()] = k
with open(os.path.join(session_path, "Videos", 'mappingCamDevice.pickle'), 'wb') as handle:
pickle.dump(mappingCamDevice, handle)
else:
with open(os.path.join(session_path, "Videos", 'mappingCamDevice.pickle'), 'rb') as handle:
mappingCamDevice = pickle.load(handle)
# ensure upper on deviceID
for dID in mappingCamDevice.keys():
mappingCamDevice[dID.upper()] = mappingCamDevice.pop(dID)
for video in trial["videos"]:
k = mappingCamDevice[video["device_id"].replace('-', '').upper()]
videoDir = os.path.join(session_path, "Videos", "Cam{}".format(k), "InputMedia", trial_name)
os.makedirs(videoDir, exist_ok=True)
video_path = os.path.join(videoDir, trial_name + ".mov")
if not os.path.exists(video_path):
if video['video'] :
download_file(video["video"], video_path)
return trial_name
def get_calibration(session_id,session_path):
calibration_id = get_calibration_trial_id(session_id)
resp = requests.get("{}trials/{}/".format(API_URL,calibration_id),
headers = {"Authorization": "Token {}".format(API_TOKEN)})
trial = resp.json()
calibResultTags = [res['tag'] for res in trial['results']]
videoFolder = os.path.join(session_path,'Videos')
os.makedirs(videoFolder, exist_ok=True)
if trial['status'] != 'done':
return
mapURL = trial['results'][calibResultTags.index('camera_mapping')]['media']
mapLocalPath = os.path.join(videoFolder,'mappingCamDevice.pickle')
download_and_switch_calibration(session_id,session_path,calibTrialID=calibration_id)
# Download mapping
if len(glob.glob(mapLocalPath)) == 0:
download_file(mapURL,mapLocalPath)
def download_and_switch_calibration(session_id,session_path,calibTrialID = None):
if calibTrialID == None:
calibTrialID = get_calibration_trial_id(session_id)
resp = requests.get("https://api.opencap.ai/trials/{}/".format(calibTrialID),
headers = {"Authorization": "Token {}".format(API_TOKEN)})
trial = resp.json()
calibURLs = {t['device_id']:t['media'] for t in trial['results'] if t['tag'] == 'calibration_parameters_options'}
calibImgURLs = {t['device_id']:t['media'] for t in trial['results'] if t['tag'] == 'calibration-img'}
_,imgExtension = os.path.splitext(calibImgURLs[list(calibImgURLs.keys())[0]])
lastIdx = imgExtension.find('?')
if lastIdx >0:
imgExtension = imgExtension[:lastIdx]
if 'meta' in trial.keys() and trial['meta'] is not None and 'calibration' in trial['meta'].keys():
calibDict = trial['meta']['calibration']
calibImgFolder = os.path.join(session_path,'CalibrationImages')
os.makedirs(calibImgFolder,exist_ok=True)
for cam,calibNum in calibDict.items():
camDir = os.path.join(session_path,'Videos',cam)
os.makedirs(camDir,exist_ok=True)
file_name = os.path.join(camDir,'cameraIntrinsicsExtrinsics.pickle')
img_fileName = os.path.join(calibImgFolder,'calib_img' + cam + imgExtension)
if calibNum == 0:
download_file(calibURLs[cam+'_soln0'], file_name)
download_file(calibImgURLs[cam],img_fileName)
elif calibNum == 1:
download_file(calibURLs[cam+'_soln1'], file_name)
download_file(calibImgURLs[cam + '_altSoln'],img_fileName)
def post_file_to_trial(filePath,trial_id,tag,device_id):
files = {'media': open(filePath, 'rb')}
data = {
"trial": trial_id,
"tag": tag,
"device_id" : device_id
}
requests.post("{}results/".format(API_URL), files=files, data=data,
headers = {"Authorization": "Token {}".format(API_TOKEN)})
files["media"].close()
def post_video_to_trial(filePath,trial_id,device_id,parameters):
files = {'video': open(filePath, 'rb')}
data = {
"trial": trial_id,
"device_id" : device_id,
"parameters": parameters
}
requests.post("{}videos/".format(API_URL), files=files, data=data,
headers = {"Authorization": "Token {}".format(API_TOKEN)})
files["video"].close()
def delete_video_from_trial(video_id):
requests.delete("{}videos/{}/".format(API_URL, video_id),
headers = {"Authorization": "Token {}".format(API_TOKEN)})
def delete_results(trial_id, tag=None, resultNum=None):
# Delete specific result number, or all results with a specific tag, or all results if tag==None
if resultNum != None:
resultNums = [resultNum]
elif tag != None:
trial = get_trial_json(trial_id)
resultNums = [r['id'] for r in trial['results'] if r['tag']==tag]
elif tag == None:
trial = get_trial_json(trial_id)
resultNums = [r['id'] for r in trial['results']]
for rNum in resultNums:
requests.delete(API_URL + "results/{}/".format(rNum),
headers = {"Authorization": "Token {}".format(API_TOKEN)})
def set_trial_status(trial_id, status):
# Available statuses: 'done', 'error', 'stopped', 'reprocess'
# 'processing' and 'recording also exist, but it does not make sense to set them manually.
# Throw error if status is not one of the above.
if status not in ['done', 'error', 'stopped', 'reprocess']:
raise ValueError('Invalid status. Available statuses: done, error, stopped, reprocess')
requests.patch(API_URL+"trials/{}/".format(trial_id), data={'status': status},
headers = {"Authorization": "Token {}".format(API_TOKEN)})
def set_session_subject(session_id, subject_id):
requests.patch(API_URL+"sessions/{}/".format(session_id), data={'subject': subject_id},
headers = {"Authorization": "Token {}".format(API_TOKEN)})
def get_syncd_videos(trial_id,session_path):
trial = requests.get("{}trials/{}/".format(API_URL,trial_id),
headers = {"Authorization": "Token {}".format(API_TOKEN)}).json()
trial_name = trial['name']
if trial['results']:
for result in trial['results']:
if result['tag'] == 'video-sync':
url = result['media']
cam,suff = os.path.splitext(url[url.rfind('_')+1:])
lastIdx = suff.find('?')
if lastIdx >0:
suff = suff[:lastIdx]
syncVideoPath = os.path.join(session_path,'Videos',cam,'InputMedia',trial_name,trial_name + '_sync' + suff)
download_file(url,syncVideoPath)
def download_session(session_id, sessionBasePath= None,
zipFolder=False,writeToDB=False, downloadVideos=True):
print('\nDownloading {}'.format(session_id))
if sessionBasePath is None:
sessionBasePath = os.path.join(os.getcwd(),'Data')
session = get_session_json(session_id)
session_path = os.path.join(sessionBasePath,'OpenCapData_' + session_id)
calib_id = get_calibration_trial_id(session_id)
neutral_id = get_neutral_trial_id(session_id)
dynamic_ids = [t['id'] for t in session['trials'] if (t['name'] != 'calibration' and t['name'] !='neutral')]
# Calibration
try:
get_camera_mapping(session_id, session_path)
if downloadVideos:
download_videos_from_server(session_id,calib_id,
isCalibration=True,isStaticPose=False,
session_path = session_path)
get_calibration(session_id,session_path)
except:
pass
# Neutral
try:
modelName = get_model_and_metadata(session_id,session_path)
get_motion_data(neutral_id,session_path)
if downloadVideos:
download_videos_from_server(session_id,neutral_id,
isCalibration=False,isStaticPose=True,
session_path = session_path)
get_syncd_videos(neutral_id,session_path)
except:
pass
# Dynamic
for dynamic_id in dynamic_ids:
try:
get_motion_data(dynamic_id,session_path)
if downloadVideos:
download_videos_from_server(session_id,dynamic_id,
isCalibration=False,isStaticPose=False,
session_path = session_path)
get_syncd_videos(dynamic_id,session_path)
except:
pass
repoDir = os.path.dirname(os.path.abspath(__file__))
# Readme
try:
pathReadme = os.path.join(repoDir, 'Resources', 'README.txt')
pathReadmeEnd = os.path.join(session_path, 'README.txt')
shutil.copy2(pathReadme, pathReadmeEnd)
except:
pass
# Geometry
try:
if 'Lai' in modelName:
modelType = 'LaiArnold'
else:
raise ValueError("Geometries not available for this model, please contact us")
if platform.system() == 'Windows':
geometryDir = os.path.join(repoDir, 'tmp', modelType, 'Geometry')
else:
geometryDir = "/tmp/{}/Geometry".format(modelType)
# If not in cache, download from s3.
if not os.path.exists(geometryDir):
os.makedirs(geometryDir, exist_ok=True)
get_geometries(session_path, modelName=modelName)
geometryDirEnd = os.path.join(session_path, 'OpenSimData', 'Model', 'Geometry')
shutil.copytree(geometryDir, geometryDirEnd)
except:
pass
# Zip
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(path, '..')))
session_zip = '{}.zip'.format(session_path)
if os.path.isfile(session_zip):
os.remove(session_zip)
if zipFolder:
zipf = zipfile.ZipFile(session_zip, 'w', zipfile.ZIP_DEFLATED)
zipdir(session_path, zipf)
zipf.close()
# Write zip as a result to last trial for now
if writeToDB:
post_file_to_trial(session_zip,dynamic_ids[-1],tag='session_zip',
device_id='all')
def cross_corr(y1, y2,multCorrGaussianStd=None,visualize=False):
"""Calculates the cross correlation and lags without normalization.
The definition of the discrete cross-correlation is in:
https://www.mathworks.com/help/matlab/ref/xcorr.html
Args:
y1, y2: Should have the same length.
Returns:
max_corr: Maximum correlation without normalization.
lag: The lag in terms of the index.
"""
# Pad shorter signal with 0s
if len(y1) > len(y2):
temp = np.zeros(len(y1))
temp[0:len(y2)] = y2
y2 = np.copy(temp)
elif len(y2)>len(y1):
temp = np.zeros(len(y2))
temp[0:len(y1)] = y1
y1 = np.copy(temp)
y1_auto_corr = np.dot(y1, y1) / len(y1)
y2_auto_corr = np.dot(y2, y2) / len(y1)
corr = np.correlate(y1, y2, mode='same')
# The unbiased sample size is N - lag.
unbiased_sample_size = np.correlate(np.ones(len(y1)), np.ones(len(y1)), mode='same')
corr = corr / unbiased_sample_size / np.sqrt(y1_auto_corr * y2_auto_corr)
shift = len(y1) // 2
max_corr = np.max(corr)
argmax_corr = np.argmax(corr)
if visualize:
plt.figure()
plt.plot(corr)
plt.title('vertical velocity correlation')
# Multiply correlation curve by gaussian (prioritizing lag solution closest to 0)
if multCorrGaussianStd is not None:
corr = np.multiply(corr,gaussian(len(corr),multCorrGaussianStd))
if visualize:
plt.plot(corr,color=[.4,.4,.4])
plt.legend(['corr','corr*gaussian'])
argmax_corr = np.argmax(corr)
max_corr = np.nanmax(corr)
lag = argmax_corr-shift
return max_corr, lag
def downsample(data,time,framerate_in,framerate_out):
# Calculate the downsampling factor
downsampling_factor = framerate_in / framerate_out
# Create new indices for downsampling
original_indices = np.arange(len(data))
new_indices = np.arange(0, len(data), downsampling_factor)
# Perform downsampling with interpolation
downsampled_data = np.ndarray((len(new_indices), data.shape[1]))
for i in range(data.shape[1]):
downsampled_data[:,i] = np.interp(new_indices, original_indices, data[:,i])
downsampled_time = np.interp(new_indices, original_indices, time)
return downsampled_time, downsampled_data