def test_c_sgi(self): ''' Compare identical SGI/MIPS files encoded using both floating-point and integer representations. ''' FMT_SGI_INT = c3d.Reader(Zipload._get(self.ZIP, self.MIPS_INT)) FMT_SGI_REAL = c3d.Reader(Zipload._get(self.ZIP, self.MIPS_REAL)) proc_type = 'SGI' test_id = '{} format test'.format(proc_type) verify.equal_headers(test_id, FMT_SGI_INT, FMT_SGI_REAL, 'INT', 'REAL', False, True) verify.data_is_equal(FMT_SGI_INT, FMT_SGI_REAL, 'INT', 'REAL') print('SGI FORMAT: OK')
def test_Manager_renumber_group(self): '''Test if renaming (renumbering) groups acts as intended.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) writer = reader.to_writer() ref = GroupSample(writer) grp_ids = [k for (k, g) in ref.group_listed] max_key = ref.max_key for i, key in enumerate(grp_ids): test_num = max_key + i + 1 grp = writer.get(key) writer.rename_group(key, test_num) grp2 = writer.get(test_num) assert grp2 is not None, "Rename failed, group with name '%s' does not exist." assert grp == grp2, 'Rename failed, group acquired from new name is not identical.' ref.assert_entry_count() ref.assert_group_items() try: writer.rename_group(max_key + 1, max_key + 2) raise RuntimeError( 'Overwriting existing numerical ID should raise a KeyError.') except ValueError as e: pass # Correct
def test_set_params(self): r = c3d.Reader(Zipload._get('sample08.zip', 'TESTDPI.c3d')) w = c3d.Writer( point_rate=r.point_rate, analog_rate=r.analog_rate, point_scale=r.point_scale, ) w.add_frames([(p, a) for _, p, a in r.read_frames()]) h = io.BytesIO() w.set_start_frame(255) w.set_point_labels(r.point_labels) w.set_analog_labels(r.analog_labels) w.set_analog_general_scale(r.get('ANALOG:GEN_SCALE').float_value) # Screen axis X, Y = '-Y', '+Z' w.set_screen_axis() w.set_screen_axis(X, Y) X_v, Y_v = w.get_screen_xy_strings() assert X_v == X and Y == Y_v, 'Mismatch between set & get screen axis.' assert np.all(np.equal(r.point_labels, w.point_labels)), 'Expected labels to be equal.' test_name = 'TEST_PARAM' test_string = 'lorem ipsum' w.point_group.add_str(test_name, 'void descriptor', test_string) assert w.point_group.get(test_name).total_bytes == len(test_string), \ "Mismatch in number of bytes encoded by 'Group.add_str'" w.write(h)
def test_Manager_group_listed(self): '''Test Manager.group_listed''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) grp_list = [k for (k, g) in reader.listed()] assert len( grp_list ) > 0, 'No group items in file or Manager.group_listed failed'
def test_Manager_rename_group(self): '''Test if renaming groups acts as intended.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) writer = reader.to_writer() ref = GroupSample(writer) grp_keys = [k for (k, g) in ref.group_items] new_names = ['TEST_NAME' + str(i) for i in range(len(grp_keys))] for key, test_name in zip(grp_keys, new_names): grp = writer.get(key) writer.rename_group(key, test_name) grp2 = writer.get(test_name) assert grp2 is not None, "Rename failed, group with name '%s' does not exist." assert grp == grp2, 'Rename failed, group acquired from new name is not identical.' ref.assert_entry_count() ref.assert_group_list() try: writer.rename_group(new_names[0], new_names[1]) raise RuntimeError( 'Overwriting existing numerical ID should raise a KeyError.') except ValueError as e: pass # Correct
def test_Manager_group_items(self): '''Test Manager.group_items''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) grp_keys = [k for (k, g) in reader.items()] assert len( grp_keys ) > 0, 'No group items in file or Manager.group_items failed'
def test_Group_remove_param(self): '''Test if removing groups acts as intended.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) writer = reader.to_writer() for g in writer.values(): ref = ParamSample(g) ref.verify_remove_all() ref.verify_add_parameter(100)
def test_a_intel(self): ''' Compare identical INTEL files encoded using both floating-point and integer representations. ''' print('----------------------------') print(type(self)) print('----------------------------') FMT_INTEL_INT = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_INT)) FMT_INTEL_REAL = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) proc_type = 'INTEL' test_id = '{} format test'.format(proc_type) verify.equal_headers(test_id, FMT_INTEL_INT, FMT_INTEL_REAL, 'INT', 'REAL', False, True) verify.data_is_equal(FMT_INTEL_INT, FMT_INTEL_REAL, 'INT', 'REAL') print('INTEL FORMAT: OK')
def test_Group_readonly_add_param(self): '''Test if adding parameter to a readonly group fails.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) for g in reader.values(): try: add_dummy_param(g) raise RuntimeError( 'Adding to readonly should not be possible.') except AttributeError: pass
def test_b_encoded_sample_count(self): for file, sample_count in zip(self.zip_files, self.sample_count): reader = c3d.Reader(Zipload._get(self.ZIP, file)) self._log(reader) # Verify file count assert reader.analog_sample_count == sample_count,\ 'Wrong analog sample count read from file {}/{}. Expected {} was {}'.format( self.ZIP, file, sample_count, reader.analog_sample_count) print('{} | SAMPLE_COUNT: OK'.format(file))
def test_d_int_formats(self): ''' Compare identical files for different processor formats. All files are encoded using the integer representations. ''' FMT_INTEL_INT = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_INT)) FMT_DEC_INT = c3d.Reader(Zipload._get(self.ZIP, self.DEC_INT)) FMT_SGI_INT = c3d.Reader(Zipload._get(self.ZIP, self.MIPS_INT)) verify.equal_headers('INTEL-DEC INT format test', FMT_INTEL_INT, FMT_DEC_INT, 'INTEL', 'DEC', False, False) verify.equal_headers('INTEL-SGI INT format test', FMT_INTEL_INT, FMT_SGI_INT, 'INTEL', 'SGI', False, False) verify.equal_headers('DEC-SGI INT format test', FMT_DEC_INT, FMT_SGI_INT, 'DEC', 'SGI', False, False) verify.data_is_equal(FMT_INTEL_INT, FMT_DEC_INT, 'INTEL', 'DEC') verify.data_is_equal(FMT_INTEL_INT, FMT_SGI_INT, 'INTEL', 'SGI') verify.data_is_equal(FMT_DEC_INT, FMT_SGI_INT, 'DEC', 'SGI') print('INTEL-DEC-SGI INT FORMAT COMPARISON: OK')
def test_e_real_formats(self): ''' Compare identical files for different processor formats. All files are encoded using the floating-point representations. ''' FMT_INTEL_REAL = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) FMT_DEC_REAL = c3d.Reader(Zipload._get(self.ZIP, self.DEC_REAL)) FMT_SGI_REAL = c3d.Reader(Zipload._get(self.ZIP, self.MIPS_REAL)) verify.equal_headers('INTEL-DEC INT format test', FMT_INTEL_REAL, FMT_DEC_REAL, 'INTEL', 'DEC', True, True) verify.equal_headers('INTEL-SGI INT format test', FMT_INTEL_REAL, FMT_SGI_REAL, 'INTEL', 'SGI', True, True) verify.equal_headers('DEC-SGI INT format test', FMT_DEC_REAL, FMT_SGI_REAL, 'DEC', 'SGI', True, True) verify.data_is_equal(FMT_INTEL_REAL, FMT_DEC_REAL, 'INTEL', 'DEC') verify.data_is_equal(FMT_INTEL_REAL, FMT_SGI_REAL, 'INTEL', 'SGI') verify.data_is_equal(FMT_DEC_REAL, FMT_SGI_REAL, 'DEC', 'SGI') print('INTEL-DEC-SGI REAL FORMAT COMPARISON: OK')
def test_paramsd(self): r = c3d.Reader(Zipload._get('sample08.zip', 'TESTDPI.c3d')) w = c3d.Writer( point_rate=r.point_rate, analog_rate=r.analog_rate, point_scale=r.point_scale, gen_scale=r.get_float('ANALOG:GEN_SCALE'), ) w.add_frames((p, a) for _, p, a in r.read_frames()) h = io.BytesIO() w.write(h, r.point_labels)
def test_frames(self): r = c3d.Reader(Zipload._get('sample08.zip', 'TESTDPI.c3d')) self._log(r) frames = list(r.read_frames()) assert len(frames) == 450 frame_no, points, analog = frames[0] assert frame_no == 1, frame_no expected = (r.point_used, 5) assert points.shape == expected, \ 'point shape: got {}, expected {}'.format(points.shape, expected) expected = (r.analog_used, r.header.analog_per_frame) assert analog.shape == expected, \ 'analog shape: got {}, expected {}'.format(analog.shape, expected)
def test_add_frames(self): r = c3d.Reader(Zipload._get('sample08.zip', 'TESTDPI.c3d')) w = c3d.Writer( point_rate=r.point_rate, analog_rate=r.analog_rate, point_scale=r.point_scale, ) w.add_frames([(p, a) for _, p, a in r.read_frames()]) w.add_frames([(p, a) for _, p, a in r.read_frames()], index=5) h = io.BytesIO() w.set_point_labels(r.point_labels) w.set_analog_labels(r.analog_labels) w.set_analog_general_scale(r.get('ANALOG:GEN_SCALE').float_value) w.write(h)
def test_a_encoded_frame_count(self): print('----------------------------') print(type(self)) print('----------------------------') for file, frame_count in zip(self.zip_files, self.frame_count): reader = c3d.Reader(Zipload._get(self.ZIP, file)) self._log(reader) # Verify file count assert reader.frame_count == frame_count,\ 'Wrong frame count read from file {}/{}. Expected {} was {}'.format( self.ZIP, file, frame_count, reader.frame_count) print('{} | FRAME_COUNT: OK'.format(file))
def test_Manager_add_group(self): '''Test if renaming groups acts as intended.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) keys = reader.keys() try: reader.add_group(0) raise ValueError('Reader should not allow adding groups.') except AttributeError: pass try: reader.remove_group(keys[0]) raise ValueError('Reader should not allow removing groups.') except AttributeError: pass try: reader.rename_group(keys[0], 'TEST_NAME') raise ValueError('Reader should not allow renaming groups.') except AttributeError: pass
def check_zipfile(file_path): ''' Loads file within a zipfile and execute 'c3d.read_frames()' ''' reader = c3d.Reader(Zipload._get(self.ZIP, file_path)) nframe_read, nanalog_read = 0, 0 # Read point frames and analog samples for i, points, analog in reader.read_frames(copy=False): nframe_read += 1 nanalog_read += analog.shape[1] assert nframe_read == reader.frame_count,\ "Failed reading file, mismatch in number of frames, read {} expected {}".format( nframe_read, reader.frame_count) assert nanalog_read == reader.analog_sample_count,\ "Failed reading file, mismatch in number of analog samples, read {} expected {}".format( nanalog_read, reader.analog_sample_count) print('{} | READ: OK'.format(file))
def test_paramsb(self): r = c3d.Reader(Zipload._get('sample08.zip', 'TESTBPI.c3d')) self._log(r) for g in r.values(): for p in g.values(): if len(p.dimensions) == 0: val = None width = p.bytes_per_element if width == 2: val = p.int16_value elif width == 4: val = p.float_value else: val = p.int8_value print('{0.name}.{1.name} = {2}'.format(g, p, val)) assert r.point_used == 26 assert r.point_rate == 50 assert r.analog_used == 16 assert r.get('POINT:RATE').float_value == 50 assert r.get('ANALOG:RATE').float_value == 200
def verify_read_write(zip, file_path, proc_type='INTEL', real=True): ''' Compare read write ouput to original read file. ''' A = c3d.Reader(Zipload._get(zip, file_path)) cpy_mode = 'copy' if proc_type != 'INTEL': cpy_mode = 'shallow_copy' writer = A.to_writer(cpy_mode) tmp_path = os.path.join(TEMP, 'write_test.c3d') with open(tmp_path, 'wb') as handle: writer.write(handle) aname = 'Original' bname = 'WriteRead' test_id = '{} write read test'.format(proc_type) with open(tmp_path, 'rb') as handle: B = c3d.Reader(handle) verify.equal_headers(test_id, A, B, aname, bname, real, real) verify.data_is_equal(A, B, aname, bname)
def check_zipfile(file_path): ''' Loads file within a zipfile and execute 'c3d.read_frames()' ''' reader = c3d.Reader(Zipload._get(self.ZIP, file_path)) self._log(reader) npoint = np.zeros((reader.point_used, 5), dtype=np.int64) nanalog = np.zeros((reader.analog_used), dtype=np.int64) point_min, point_max = 1e10, 1e-10 analog_min, analog_max = point_min, point_max has_point = reader.point_used > 0 has_analog = reader.analog_used > 0 # Read point frames and analog samples for frame, points, analog in reader.read_frames(copy=False): if has_point: npoint += value_in_range(points, min_range, max_range) point_min = np.min(points) point_max = np.max(points) if has_analog: nanalog += np.sum(value_in_range(analog, min_range, max_range), axis=1) analog_min = np.min(analog) analog_max = np.max(analog) assert np.all(npoint == reader.frame_count),\ """Failed verifying POINT data in range ({}, {}), found {} number of mismatches in each axis for all samples. Range for data was ({}, {})."""\ .format(min_range, max_range, np.sum(np.abs(npoint - reader.frame_count), axis=0), point_min, point_max) assert np.all(nanalog == reader.analog_sample_count),\ """Failed verifying ANALOG data in range ({}, {}), found {} number of mismatches for each channel for all samples. Range for data was ({}, {})."""\ .format(min_range, max_range, np.abs(nanalog - reader.analog_sample_count), analog_min, analog_max) print('{} | READ: OK'.format(file))
def check_zipfile(file_path): ''' Loads file within a zipfile and execute 'c3d.read_frames()' ''' reader = c3d.Reader(Zipload._get(self.ZIP, file_path)) self._log(reader) npoint = np.zeros((reader.point_used, 5), dtype=np.int64) nanalog = np.zeros((reader.analog_used), dtype=np.int64) point_min, point_max = 1e10, 1e-10 analog_min, analog_max = point_min, point_max has_point = reader.point_used > 0 has_analog = reader.analog_used > 0 # Read point frames and analog samples for frame, points, analog in reader.read_frames(copy=False): if has_point: npoint += value_in_range(points, min_range, max_range) point_min = np.min(points) point_max = np.max(points) if has_analog: nanalog += np.sum(value_in_range(analog, min_range, max_range), axis=1) analog_min = np.min(analog) analog_max = np.max(analog) assert np.all(npoint == reader.frame_count), '\n' +\ 'Failed verifying POINT data in range ({}, {}).\n'.format(min_range, max_range) +\ 'Found a total of {} values outside plausible range.\n'.format( np.sum(np.abs(npoint - reader.frame_count), axis=0)) +\ 'Range for data was ({}, {})'.format(point_min, point_max) assert np.all(nanalog == reader.analog_sample_count), '\n' +\ 'Failed verifying ANALOG data in range ({}, {}).\n'.format(min_range, max_range) +\ 'Found a total of {} values outside plausible range.\n'.format( np.abs(nanalog - reader.analog_sample_count)) +\ 'Range for data was ({}, {})'.format(analog_min, analog_max) print('{} | READ: OK'.format(file))
def test_Group_rename_param(self): ''' Test if renaming groups acts as intended.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) writer = reader.to_writer() for g in writer.values(): ref = ParamSample(g) prm_keys = ref.keys new_names = ['TEST_NAME' + str(i) for i in range(len(prm_keys))] for key, nname in zip(prm_keys, new_names): prm = g.get(key) g.rename_param(key, nname) prm2 = g.get(nname) assert prm2 is not None, "Rename failed, renamed param does not exist." assert prm == prm2, 'Rename failed, param acquired from new name is not identical.' ref.assert_entry_count() try: g.rename_param(new_names[0], new_names[1]) raise RuntimeError( 'Overwriting existing numerical ID should raise a ValueError.' ) except ValueError as e: pass # Correct
def test_Group_values(self): '''Test Group.values()''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) for g in reader.values(): N = len([v for v in g.values()]) assert N > 0, 'No group values in file or GroupReadonly.values() failed'
def test_Manager_removing_group_from_name(self): '''Test if removing groups acts as intended.''' reader = c3d.Reader(Zipload._get(self.ZIP, self.INTEL_REAL)) ref = GroupSample(reader.to_writer()) ref.verify_remove_all_using_name() ref.verify_add_group(100)
def test_format_pr(self): r = c3d.Reader(Zipload._get('sample01.zip', 'Eb015pr.c3d')) self._log(r) assert r.point_used == 26 assert r.point_rate == 50
def test_paramsd(self): r = c3d.Reader(Zipload._get('sample08.zip', 'TESTDPI.c3d')) self._log(r) assert r.point_used == 26 assert r.point_rate == 50
def setUp(self): Zipload.download()
def setUp(self): Zipload.download() Zipload.extract('sample00.zip')