-
Notifications
You must be signed in to change notification settings - Fork 2
/
c3d.py
1095 lines (915 loc) · 37.9 KB
/
c3d.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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''A Python module for reading and writing C3D files.
original implementation here: https://github.com/EmbodiedCognition/py-c3d
modified by Bergamini Luca'''
import array
import io
import numpy as np
import struct
import warnings
PROCESSOR_INTEL = 84
PROCESSOR_DEC = 85
PROCESSOR_MIPS = 86
class Header(object):
'''Header information from a C3D file.
Attributes
----------
label_block : int
Index of the 512-byte block where labels (metadata) are found.
parameter_block : int
Index of the 512-byte block where parameters (metadata) are found.
data_block : int
Index of the 512-byte block where data starts.
point_count : int
Number of motion capture channels recorded in this file.
analog_count : int
Number of analog values recorded per frame of 3D point data.
first_frame : int
Index of the first frame of data.
last_frame : int
Index of the last frame of data.
analog_per_frame : int
Number of analog frames per frame of 3D point data. The analog frame
rate (ANALOG:RATE) apparently equals the point frame rate (POINT:RATE)
times this value.
frame_rate : float
The frame rate of the recording, in frames per second.
scale_factor : float
Multiply values in the file by this scale parameter.
long_event_labels : bool
max_gap : int
.. note::
The ``scale_factor`` attribute is not used in Phasespace C3D files;
instead, use the POINT.SCALE parameter.
.. note::
The ``first_frame`` and ``last_frame`` header attributes are not used in
C3D files generated by Phasespace. Instead, the first and last
frame numbers are stored in the POINTS:ACTUAL_START_FIELD and
POINTS:ACTUAL_END_FIELD parameters.
'''
BINARY_FORMAT = '<BBHHHHHfHHf270sHH214s'
def __init__(self, handle=None):
'''Create a new Header object.
Parameters
----------
handle : file handle, optional
If given, initialize attributes for the Header from this file
handle. The handle must be seek-able and readable. If `handle` is
not given, Header attributes are initialized with default values.
'''
self.label_block = 0
self.parameter_block = 2
self.data_block = 3
self.point_count = 50
self.analog_count = 0
self.first_frame = 1
self.last_frame = 1
self.analog_per_frame = 0
self.frame_rate = 60.0
self.max_gap = 0
self.scale_factor = -1.0
self.long_event_labels = False
if handle:
self.read(handle)
def write(self, handle):
'''Write binary header data to a file handle.
This method writes exactly 512 bytes to the beginning of the given file
handle.
Parameters
----------
handle : file handle
The given handle will be reset to 0 using `seek` and then 512 bytes
will be written to describe the parameters in this Header. The
handle must be writeable.
'''
handle.seek(0)
handle.write(struct.pack(self.BINARY_FORMAT,
self.parameter_block,
0x50,
self.point_count,
self.analog_count,
self.first_frame,
self.last_frame,
self.max_gap,
self.scale_factor,
self.data_block,
self.analog_per_frame,
self.frame_rate,
b'',
self.long_event_labels and 0x3039 or 0x0,
self.label_block,
b''))
def __str__(self):
'''Return a string representation of this Header's attributes.'''
return '''\
parameter_block: {0.parameter_block}
point_count: {0.point_count}
analog_count: {0.analog_count}
first_frame: {0.first_frame}
last_frame: {0.last_frame}
max_gap: {0.max_gap}
scale_factor: {0.scale_factor}
data_block: {0.data_block}
analog_per_frame: {0.analog_per_frame}
frame_rate: {0.frame_rate}
long_event_labels: {0.long_event_labels}
label_block: {0.label_block}'''.format(self)
def read(self, handle):
'''Read and parse binary header data from a file handle.
This method reads exactly 512 bytes from the beginning of the given file
handle.
Parameters
----------
handle : file handle
The given handle will be reset to 0 using `seek` and then 512 bytes
will be read to initialize the attributes in this Header. The handle
must be readable.
Raises
------
AssertionError
If the magic byte from the header is not 80 (the C3D magic value).
'''
handle.seek(0)
(self.parameter_block,
magic,
self.point_count,
self.analog_count,
self.first_frame,
self.last_frame,
self.max_gap,
self.scale_factor,
self.data_block,
self.analog_per_frame,
self.frame_rate,
_,
self.long_event_labels,
self.label_block,
_) = struct.unpack(self.BINARY_FORMAT, handle.read(512))
assert magic == 80, 'C3D magic {} != 80 !'.format(magic)
class Param(object):
'''A class representing a single named parameter from a C3D file.
Attributes
----------
name : str
Name of this parameter.
desc : str
Brief description of this parameter.
bytes_per_element : int, optional
For array data, this describes the size of each element of data. For
string data (including arrays of strings), this should be -1.
dimensions : list of int
For array data, this describes the dimensions of the array, stored in
column-major order. For arrays of strings, the dimensions here will be
the number of columns (length of each string) followed by the number of
rows (number of strings).
bytes : str
Raw data for this parameter.
'''
def __init__(self,
name,
desc='',
bytes_per_element=1,
dimensions=None,
bytes=b'',
handle=None):
'''Set up a new parameter, only the name is required.'''
self.name = name
self.desc = desc
self.bytes_per_element = bytes_per_element
self.dimensions = dimensions or []
self.bytes = bytes
if handle:
self.read(handle)
def __repr__(self):
return '<Param: {}>'.format(self.desc)
@property
def num_elements(self):
'''Return the number of elements in this parameter's array value.'''
e = 1
for d in self.dimensions:
e *= d
return e
@property
def total_bytes(self):
'''Return the number of bytes used for storing this parameter's data.'''
return self.num_elements * abs(self.bytes_per_element)
def binary_size(self):
'''Return the number of bytes needed to store this parameter.'''
return (
1 + # group_id
2 + # next offset marker
1 + len(self.name.encode('utf-8')) + # size of name and name bytes
1 + # data size
1 + len(self.dimensions) + # size of dimensions and dimension bytes
self.total_bytes + # data
1 + len(self.desc.encode('utf-8')) # size of desc and desc bytes
)
def write(self, group_id, handle):
'''Write binary data for this parameter to a file handle.
Parameters
----------
group_id : int
The numerical ID of the group that holds this parameter.
handle : file handle
An open, writable, binary file handle.
'''
name = self.name.encode('utf-8')
handle.write(struct.pack('bb', len(name), group_id))
handle.write(name)
handle.write(struct.pack('<h', self.binary_size() - 2 - len(name)))
handle.write(struct.pack('b', self.bytes_per_element))
handle.write(struct.pack('B', len(self.dimensions)))
handle.write(struct.pack('B' * len(self.dimensions), *self.dimensions))
if self.bytes:
handle.write(self.bytes)
desc = self.desc.encode('utf-8')
handle.write(struct.pack('B', len(desc)))
handle.write(desc)
def read(self, handle):
'''Read binary data for this parameter from a file handle.
This reads exactly enough data from the current position in the file to
initialize the parameter.
'''
self.bytes_per_element, = struct.unpack('b', handle.read(1))
dims, = struct.unpack('B', handle.read(1))
self.dimensions = [struct.unpack('B', handle.read(1))[0] for _ in range(dims)]
self.bytes = ''
if self.total_bytes:
self.bytes = handle.read(self.total_bytes)
size, = struct.unpack('B', handle.read(1))
self.desc = size and handle.read(size).decode('utf-8') or ''
def _as(self, fmt):
'''Unpack the raw bytes of this param using the given struct format.'''
return struct.unpack('<' + fmt, self.bytes)[0]
@property
def int8_value(self):
'''Get the param as an 8-bit signed integer.'''
return self._as('b')
@property
def uint8_value(self):
'''Get the param as an 8-bit unsigned integer.'''
return self._as('B')
@property
def int16_value(self):
'''Get the param as a 16-bit signed integer.'''
return self._as('h')
@property
def uint16_value(self):
'''Get the param as a 16-bit unsigned integer.'''
return self._as('H')
@property
def int32_value(self):
'''Get the param as a 32-bit signed integer.'''
return self._as('i')
@property
def uint32_value(self):
'''Get the param as a 32-bit unsigned integer.'''
return self._as('I')
@property
def float_value(self):
'''Get the param as a 32-bit float.'''
return self._as('f')
@property
def bytes_value(self):
'''Get the param as a raw byte string.'''
return self.bytes
@property
def string_value(self):
'''Get the param as a unicode string.'''
return self.bytes.decode('utf-8')
def _as_array(self, fmt):
'''Unpack the raw bytes of this param using the given data format.'''
assert self.dimensions, \
'{}: cannot get value as {} array!'.format(self.name, fmt)
elems = array.array(fmt)
elems.fromstring(self.bytes)
return np.array(elems).reshape(self.dimensions)
@property
def int8_array(self):
'''Get the param as an array of 8-bit signed integers.'''
return self._as_array('b')
@property
def uint8_array(self):
'''Get the param as an array of 8-bit unsigned integers.'''
return self._as_array('B')
@property
def int16_array(self):
'''Get the param as an array of 16-bit signed integers.'''
return self._as_array('h')
@property
def uint16_array(self):
'''Get the param as an array of 16-bit unsigned integers.'''
return self._as_array('H')
@property
def int32_array(self):
'''Get the param as an array of 32-bit signed integers.'''
return self._as_array('i')
@property
def uint32_array(self):
'''Get the param as an array of 32-bit unsigned integers.'''
return self._as_array('I')
@property
def float_array(self):
'''Get the param as an array of 32-bit floats.'''
return self._as_array('f')
@property
def bytes_array(self):
'''Get the param as an array of raw byte strings.'''
assert len(self.dimensions) == 2, \
'{}: cannot get value as bytes array!'.format(self.name)
l, n = self.dimensions
return [self.bytes[i*l:(i+1)*l] for i in range(n)]
@property
def string_array(self):
'''Get the param as a array of unicode strings.'''
assert len(self.dimensions) == 2, \
'{}: cannot get value as string array!'.format(self.name)
l, n = self.dimensions
return [self.bytes[i*l:(i+1)*l].decode('utf-8') for i in range(n)]
class Group(object):
'''A group of parameters from a C3D file.
In C3D files, parameters are organized in groups. Each group has a name, a
description, and a set of named parameters.
Attributes
----------
name : str
Name of this parameter group.
desc : str
Description for this parameter group.
'''
def __init__(self, name=None, desc=None):
self.name = name
self.desc = desc
self.params = {}
def __repr__(self):
return '<Group: {}>'.format(self.desc)
def get(self, key, default=None):
'''Get a parameter by key.
Parameters
----------
key : any
Parameter key to look up in this group.
default : any, optional
Value to return if the key is not found. Defaults to None.
Returns
-------
param : :class:`Param`
A parameter from the current group.
'''
return self.params.get(key, default)
def add_param(self, name, **kwargs):
'''Add a parameter to this group.
Parameters
----------
name : str
Name of the parameter to add to this group. The name will
automatically be case-normalized.
Additional keyword arguments will be passed to the `Param` constructor.
'''
self.params[name.upper()] = Param(name.upper(), **kwargs)
def binary_size(self):
'''Return the number of bytes to store this group and its parameters.'''
return (
1 + # group_id
1 + len(self.name.encode('utf-8')) + # size of name and name bytes
2 + # next offset marker
1 + len(self.desc.encode('utf-8')) + # size of desc and desc bytes
sum(p.binary_size() for p in self.params.values()))
def write(self, group_id, handle):
'''Write this parameter group, with parameters, to a file handle.
Parameters
----------
group_id : int
The numerical ID of the group.
handle : file handle
An open, writable, binary file handle.
'''
name = self.name.encode('utf-8')
desc = self.desc.encode('utf-8')
handle.write(struct.pack('bb', len(name), -group_id))
handle.write(name)
handle.write(struct.pack('<h', 3 + len(desc)))
handle.write(struct.pack('B', len(desc)))
handle.write(desc)
for param in self.params.values():
param.write(group_id, handle)
def get_int8(self, key):
'''Get the value of the given parameter as an 8-bit signed integer.'''
return self.params[key.upper()].int8_value
def get_uint8(self, key):
'''Get the value of the given parameter as an 8-bit unsigned integer.'''
return self.params[key.upper()].uint8_value
def get_int16(self, key):
'''Get the value of the given parameter as a 16-bit signed integer.'''
return self.params[key.upper()].int16_value
def get_uint16(self, key):
'''Get the value of the given parameter as a 16-bit unsigned integer.'''
return self.params[key.upper()].uint16_value
def get_int32(self, key):
'''Get the value of the given parameter as a 32-bit signed integer.'''
return self.params[key.upper()].int32_value
def get_uint32(self, key):
'''Get the value of the given parameter as a 32-bit unsigned integer.'''
return self.params[key.upper()].uint32_value
def get_float(self, key):
'''Get the value of the given parameter as a 32-bit float.'''
return self.params[key.upper()].float_value
def get_bytes(self, key):
'''Get the value of the given parameter as a byte array.'''
return self.params[key.upper()].bytes_value
def get_string(self, key):
'''Get the value of the given parameter as a string.'''
return self.params[key.upper()].string_value
class Manager(object):
'''A base class for managing C3D file metadata.
This class manages a C3D header (which contains some stock metadata fields)
as well as a set of parameter groups. Each group is accessible using its
name.
Attributes
----------
header : `Header`
Header information for the C3D file.
'''
def __init__(self, header=None):
'''Set up a new Manager with a Header.'''
self.header = header or Header()
self.groups = {}
def check_metadata(self):
'''Ensure that the metadata in our file is self-consistent.'''
assert self.header.point_count == self.point_used, (
'inconsistent point count! {} header != {} POINT:USED'.format(
self.header.point_count,
self.point_used,
))
assert self.header.scale_factor == self.point_scale, (
'inconsistent scale factor! {} header != {} POINT:SCALE'.format(
self.header.scale_factor,
self.point_scale,
))
assert self.header.frame_rate == self.point_rate, (
'inconsistent frame rate! {} header != {} POINT:RATE'.format(
self.header.frame_rate,
self.point_rate,
))
ratio = self.analog_rate / self.point_rate
assert True or self.header.analog_per_frame == ratio, (
'inconsistent analog rate! {} header != {} analog-fps / {} point-fps'.format(
self.header.analog_per_frame,
self.analog_rate,
self.point_rate,
))
count = self.analog_used * self.header.analog_per_frame
assert True or self.header.analog_count == count, (
'inconsistent analog count! {} header != {} analog used * {} per-frame'.format(
self.header.analog_count,
self.analog_used,
self.header.analog_per_frame,
))
start = self.get_uint16('POINT:DATA_START')
assert self.header.data_block == start, (
'inconsistent data block! {} header != {} POINT:DATA_START'.format(
self.header.data_block, start))
for name in ('POINT:LABELS', 'POINT:DESCRIPTIONS',
'ANALOG:LABELS', 'ANALOG:DESCRIPTIONS'):
if self.get(name) is None:
warnings.warn('missing parameter {}'.format(name))
def add_group(self, group_id, name, desc):
'''Add a new parameter group.
Parameters
----------
group_id : int
The numeric ID for a group to check or create.
name : str, optional
If a group is created, assign this name to the group.
desc : str, optional
If a group is created, assign this description to the group.
Returns
-------
group : :class:`Group`
A group with the given ID, name, and description.
Raises
------
KeyError
If a group with a duplicate ID or name already exists.
'''
if group_id in self.groups:
raise KeyError(group_id)
name = name.upper()
if name in self.groups:
raise KeyError(name)
group = self.groups[name] = self.groups[group_id] = Group(name, desc)
return group
def get(self, group, default=None):
'''Get a group or parameter.
Parameters
----------
group : str
If this string contains a period (.), then the part before the
period will be used to retrieve a group, and the part after the
period will be used to retrieve a parameter from that group. If this
string does not contain a period, then just a group will be
returned.
default : any
Return this value if the named group and parameter are not found.
Returns
-------
value : :class:`Group` or :class:`Param`
Either a group or parameter with the specified name(s). If neither
is found, returns the default value.
'''
if isinstance(group, int):
return self.groups.get(group, default)
group = group.upper()
param = None
if '.' in group:
group, param = group.split('.', 1)
if ':' in group:
group, param = group.split(':', 1)
if group not in self.groups:
return default
group = self.groups[group]
if param is not None:
return group.get(param, default)
return group
def get_int8(self, key):
'''Get a parameter value as an 8-bit signed integer.'''
return self.get(key).int8_value
def get_uint8(self, key):
'''Get a parameter value as an 8-bit unsigned integer.'''
return self.get(key).uint8_value
def get_int16(self, key):
'''Get a parameter value as a 16-bit signed integer.'''
return self.get(key).int16_value
def get_uint16(self, key):
'''Get a parameter value as a 16-bit unsigned integer.'''
return self.get(key).uint16_value
def get_int32(self, key):
'''Get a parameter value as a 32-bit signed integer.'''
return self.get(key).int32_value
def get_uint32(self, key):
'''Get a parameter value as a 32-bit unsigned integer.'''
return self.get(key).uint32_value
def get_float(self, key):
'''Get a parameter value as a 32-bit float.'''
return self.get(key).float_value
def get_bytes(self, key):
'''Get a parameter value as a byte string.'''
return self.get(key).bytes_value
def get_string(self, key):
'''Get a parameter value as a string.'''
return self.get(key).string_value
def parameter_blocks(self):
'''Compute the size (in 512B blocks) of the parameter section.'''
bytes = 4. + sum(g.binary_size() for g in self.groups.values())
return int(np.ceil(bytes / 512))
@property
def point_rate(self):
return self.get_float('POINT:RATE')
@property
def point_scale(self):
return self.get_float('POINT:SCALE')
@property
def point_used(self):
return self.get_uint16('POINT:USED')
@property
def analog_used(self):
try:
return self.get_uint16('ANALOG:USED')
except AttributeError:
return 0
@property
def analog_rate(self):
try:
return self.get_float('ANALOG:RATE')
except AttributeError:
return 0
@property
def point_labels(self):
return self.get('POINT:LABELS').string_array
def first_frame(self):
# this is a hack for phasespace files ... should put it in a subclass.
param = self.get('TRIAL:ACTUAL_START_FIELD')
if param is not None:
return param.int32_value
return self.header.first_frame
def last_frame(self):
# this is a hack for phasespace files ... should put it in a subclass.
param = self.get('TRIAL:ACTUAL_END_FIELD')
if param is not None:
return param.int32_value
return self.header.last_frame
class Reader(Manager):
'''This class provides methods for reading the data in a C3D file.
A C3D file contains metadata and frame-based data describing 3D motion.
You can iterate over the frames in the file by calling `read_frames()` after
construction:
>>> r = c3d.Reader(open('capture.c3d', 'rb'))
>>> for frame_no, points, analog in r.read_frames():
... print('{0.shape} points in this frame'.format(points))
'''
def __init__(self, handle):
'''Initialize this C3D file by reading header and parameter data.
Parameters
----------
handle : file handle
Read metadata and C3D motion frames from the given file handle. This
handle is assumed to be `seek`-able and `read`-able. The handle must
remain open for the life of the `Reader` instance. The `Reader` does
not `close` the handle.
Raises
------
ValueError
If the processor metadata in the C3D file is anything other than 84
(Intel format).
'''
super(Reader, self).__init__(Header(handle))
self._handle = handle
self._handle.seek((self.header.parameter_block - 1) * 512)
# metadata header
buf = self._handle.read(4)
_, _, parameter_blocks, processor = struct.unpack('BBBB', buf)
if processor != PROCESSOR_INTEL:
raise ValueError(
'we only read Intel C3D files (got processor {})'.
format(processor))
# read all parameter blocks as a single chunk to avoid block
# boundary issues.
bytes = self._handle.read(512 * parameter_blocks - 4)
while bytes:
buf = io.BytesIO(bytes)
chars_in_name, group_id = struct.unpack('bb', buf.read(2))
if group_id == 0 or chars_in_name == 0:
# we've reached the end of the parameter section.
break
name = buf.read(abs(chars_in_name)).decode('utf-8').upper()
offset_to_next, = struct.unpack('<h', buf.read(2))
if group_id > 0:
# we've just started reading a parameter. if its group doesn't
# exist, create a blank one. add the parameter to the group.
self.groups.setdefault(group_id, Group()).add_param(name, handle=buf)
else:
# we've just started reading a group. if a group with the
# appropriate id exists already (because we've already created
# it for a parameter), just set the name of the group.
# otherwise, add a new group.
group_id = abs(group_id)
size, = struct.unpack('B', buf.read(1))
desc = size and buf.read(size) or ''
if self.get(group_id) is not None or self.get(name) is not None:
if self.get(group_id) is not None:
group = self.get(group_id)
else:
group = self.get(name)
group.name = name
group.desc = desc
self.groups[name] = group
else:
self.add_group(group_id, name, desc)
bytes = bytes[2 + abs(chars_in_name) + offset_to_next:]
self.check_metadata()
def read_frames(self, copy=True):
'''Iterate over the data frames from our C3D file handle.
Parameters
----------
copy : bool
If False, the reader returns a reference to the same data buffers
for every frame. The default is True, which causes the reader to
return a unique data buffer for each frame. Set this to False if you
consume frames as you iterate over them, or True if you store them
for later.
Returns
-------
frames : sequence of (frame number, points, analog)
This method generates a sequence of (frame number, points, analog)
tuples, one tuple per frame. The first element of each tuple is the
frame number. The second is a numpy array of parsed, 5D point data
and the third element of each tuple is a numpy array of analog
values that were recorded during the frame. (Often the analog data
are sampled at a higher frequency than the 3D point data, resulting
in multiple analog frames per frame of point data.)
The first three columns in the returned point data are the (x, y, z)
coordinates of the observed motion capture point. The fourth column
is an estimate of the error for this particular point, and the fifth
column is the number of cameras that observed the point in question.
Both the fourth and fifth values are -1 if the point is considered
to be invalid.
'''
scale = abs(self.point_scale)
is_float = self.point_scale < 0
point_bytes = [2, 4][is_float]
point_dtype = [np.int16, np.float32][is_float]
point_scale = [scale, 1][is_float]
points = np.zeros((self.point_used, 5), float)
# TODO: handle ANALOG:BITS parameter here!
p = self.get('ANALOG:FORMAT')
analog_unsigned = p and p.string_value.strip().upper() == 'UNSIGNED'
analog_dtype = np.int16
analog_bytes = 2
if is_float:
analog_dtype = np.float32
analog_bytes = 4
elif analog_unsigned:
analog_dtype = np.uint16
analog_bytes = 2
analog = np.array([], float)
offsets = np.zeros((self.analog_used, 1), int)
param = self.get('ANALOG:OFFSET')
if param is not None:
offsets = param.int16_array[:self.analog_used, None]
scales = np.ones((self.analog_used, 1), float)
param = self.get('ANALOG:SCALE')
if param is not None:
scales = param.float_array[:self.analog_used, None]
gen_scale = 1.
param = self.get('ANALOG:GEN_SCALE')
if param is not None:
gen_scale = param.float_value
self._handle.seek((self.header.data_block - 1) * 512)
for frame_no in range(self.first_frame(), self.last_frame() + 1):
n = 4 * self.header.point_count
raw = np.fromstring(self._handle.read(n * point_bytes),
dtype=point_dtype,
count=n).reshape((self.point_used, 4))
points[:, :3] = raw[:, :3] * point_scale
valid = raw[:, 3] > -1
points[~valid, 3:5] = -1
c = raw[valid, 3].astype(np.uint16)
# fourth value is floating-point (scaled) error estimate
points[valid, 3] = (c & 0xff).astype(float) * scale
# fifth value is number of bits set in camera-observation byte
points[valid, 4] = sum((c & (1 << k)) >> k for k in range(8, 17))
if self.header.analog_count > 0:
n = self.header.analog_count
raw = np.fromstring(self._handle.read(n * analog_bytes),
dtype=analog_dtype,
count=n).reshape((self.analog_used, -1))
analog = (raw.astype(float) - offsets) * scales * gen_scale
if copy:
yield frame_no, points.copy(), analog.copy()
else:
yield frame_no, points, analog
class Writer(Manager):
'''This class writes metadata and frames to a C3D file.
For example, to read an existing C3D file, apply some sort of data
processing to the frames, and write out another C3D file::
>>> r = c3d.Reader(open('data.c3d', 'rb'))
>>> w = c3d.Writer()
>>> w.add_frames(process_frames_somehow(r.read_frames()))
>>> with open('smoothed.c3d', 'wb') as handle:
>>> w.write(handle)
Parameters
----------
point_rate : float, optional
The frame rate of the data. Defaults to 480.
analog_rate : float, optional
The number of analog samples per frame. Defaults to 0.
point_scale : float, optional
The scale factor for point data. Defaults to -1 (i.e., "check the
POINT:SCALE parameter").
point_units : str, optional
The units that the point numbers represent. Defaults to ``'mm '``.
gen_scale : float, optional
General scaling factor for data. Defaults to 1.
'''
def __init__(self,
point_rate=480.,
analog_rate=0.,
point_scale=-1.,
point_units='mm ',
gen_scale=1.):
'''Set metadata for this writer.
'''
super(Writer, self).__init__()
self._point_rate = point_rate
self._analog_rate = analog_rate
self._point_scale = point_scale
self._point_units = point_units
self._gen_scale = gen_scale
self._frames = []
def add_frames(self, frames):
'''Add frames to this writer instance.
Parameters
----------
frames : sequence of (point, analog) tuples
A sequence of frame data to add to the writer.
'''
self._frames.extend(frames)
def _pad_block(self, handle):
'''Pad the file with 0s to the end of the next block boundary.'''
extra = handle.tell() % 512
if extra:
handle.write(b'\x00' * (512 - extra))
def _write_metadata(self, handle):
'''Write metadata to a file handle.
Parameters
----------
handle : file
Write metadata and C3D motion frames to the given file handle. The
writer does not close the handle.
'''
self.check_metadata()
# header
self.header.write(handle)
self._pad_block(handle)
assert handle.tell() == 512
# groups
handle.write(struct.pack(
'BBBB', 0, 0, self.parameter_blocks(), PROCESSOR_INTEL))
id_groups = sorted(
(i, g) for i, g in self.groups.items() if isinstance(i, int))
for group_id, group in id_groups:
group.write(group_id, handle)
# padding
self._pad_block(handle)
while handle.tell() != 512 * (self.header.data_block - 1):
handle.write(b'\x00' * 512)
def _write_frames(self, handle):
'''Write our frame data to the given file handle.
Parameters
----------
handle : file
Write metadata and C3D motion frames to the given file handle. The
writer does not close the handle.
'''
assert handle.tell() == 512 * (self.header.data_block - 1)