def test_bbb_build_frame_container(): """Tests the generation of a ``FrameContainer`` via function.""" # very simple case fco = build_frame_container(0, 1, 0) assert fco.camId == 0 assert fco.fromTimestamp == 0 assert fco.toTimestamp == 1 ts1 = to_timestamp(datetime(1970, 1, 1, tzinfo=pytz.utc)) ts2 = to_timestamp(datetime(2015, 8, 15, 12, 0, 40, tzinfo=pytz.utc)) # more advanced fco = build_frame_container(ts1, ts2, 1) assert fco.camId == 1 assert fco.fromTimestamp == ts1 assert fco.toTimestamp == ts2 # try to set every parameter tfm = [1. / 3, 2.5, 4] fco = build_frame_container(ts1, ts2, 1, hive_id=5, transformation_matrix=tfm, data_source_fname="testname") assert fco.hiveId == 5 assert np.all(np.isclose(fco.transformationMatrix, tfm)) assert len(fco.dataSources) == 1 assert fco.dataSources[0].filename == "testname" assert fco.dataSources[0].videoPreviewFilename == "" # filename and video are both strings fco = build_frame_container(ts1, ts2, 1, data_source_fname="testname", video_preview_fname="test_video_name") assert len(fco.dataSources) == 1 assert fco.dataSources[0].filename == "testname" assert fco.dataSources[0].videoPreviewFilename == "test_video_name" # try combination of filenames and preview names fnames = ["testname", "testname 1"] vnames = ["test_video_name", "test_video_name 1"] fco = build_frame_container(ts1, ts2, 1, data_source_fname=fnames, video_preview_fname=vnames) assert len(fco.dataSources) == 2 assert fco.dataSources[0].filename == "testname" assert fco.dataSources[0].videoPreviewFilename == "test_video_name" assert fco.dataSources[1].filename == "testname 1" assert fco.dataSources[1].videoPreviewFilename == "test_video_name 1"
def test_bbb_repo_iter_frames_filtered(tmpdir): repo = Repository(str(tmpdir.join('frames_from_to_filtered'))) repo_start = 0 nFC = 10 span = 1000 nFrames = nFC * span repo_end = repo_start + nFrames begin_end_cam_id = [(ts, ts + span, 0) for ts in range(repo_start, repo_end, span)] for begin, end, cam_id in begin_end_cam_id: fc = build_frame_container(begin, end, cam_id) fc.init('frames', span) for i, tstamp in enumerate(range(begin, end)): frame = fc.frames[i] frame.id = tstamp frame.timestamp = tstamp repo.add(fc) timestamps = [f[0].timestamp for f in repo.iter_frames(begin, end)] selected_timestamps = set(np.random.choice(timestamps, size=5, replace=False)) class TimestepFilter(): def __init__(self, timesteps): self.timesteps = timesteps def __call__(self, frame): return frame.timestamp in self.timesteps filtered_frames = list(repo.iter_frames(begin, end, frame_filter=TimestepFilter(selected_timestamps))) filtered_timestamps = set([f[0].timestamp for f in filtered_frames]) assert(filtered_timestamps == selected_timestamps)
def test_bbb_iter_small_frame_window(tmpdir): """Tests that iter_frame returns frames if time window is small.""" repo = Repository(str(tmpdir.join('frames_from_to_small_window'))) repo_start = 0 nFC = 10 span = 1000 nFrames = nFC * span repo_end = repo_start + nFrames begin_end_cam_id = [(ts, ts + span, 0) for ts in range(repo_start, repo_end, span)] for begin, end, cam_id in begin_end_cam_id: fc = build_frame_container(begin, end, cam_id) fc.init('frames', span) for i, tstamp in enumerate(range(begin, end)): frame = fc.frames[i] frame.id = tstamp frame.timestamp = tstamp repo.add(fc) begin = 1 end = 10 fnames = list(repo.iter_frames(begin, end)) assert (len(fnames) > 0) begin = 1001 end = 1011 fnames = list(repo.iter_fnames(begin, end)) assert (len(fnames) > 0)
def test_bbb_iter_small_frame_window(tmpdir): """Tests that iter_frame returns frames if time window is small.""" repo = Repository(str(tmpdir.join('frames_from_to_small_window'))) repo_start = 0 nFC = 10 span = 1000 nFrames = nFC * span repo_end = repo_start + nFrames begin_end_cam_id = [(ts, ts + span, 0) for ts in range(repo_start, repo_end, span)] for begin, end, cam_id in begin_end_cam_id: fc = build_frame_container(begin, end, cam_id) fc.init('frames', span) for i, tstamp in enumerate(range(begin, end)): frame = fc.frames[i] frame.id = tstamp frame.timestamp = tstamp repo.add(fc) begin = 1 end = 10 fnames = list(repo.iter_frames(begin, end)) assert(len(fnames) > 0) begin = 1001 end = 1011 fnames = list(repo.iter_fnames(begin, end)) assert(len(fnames) > 0)
def test_bbb_repo_open_frame_container(tmpdir): repo = Repository(str(tmpdir)) cam_id = 1 fc = build_frame_container(1000, 5000, cam_id) repo.add(fc) open_fc = repo.open(2000, 1) assert fc.fromTimestamp == open_fc.fromTimestamp assert fc.toTimestamp == open_fc.toTimestamp
def test_bbb_build_frame_container(): """Tests the generation of a ``FrameContainer`` via function.""" # very simple case fco = build_frame_container(0, 1, 0) assert fco.camId == 0 assert fco.fromTimestamp == 0 assert fco.toTimestamp == 1 ts1 = to_timestamp(datetime(1970, 1, 1, tzinfo=pytz.utc)) ts2 = to_timestamp(datetime(2015, 8, 15, 12, 0, 40, tzinfo=pytz.utc)) # more advanced fco = build_frame_container(ts1, ts2, 1) assert fco.camId == 1 assert fco.fromTimestamp == ts1 assert fco.toTimestamp == ts2 # try to set every parameter tfm = [1./3, 2.5, 4] fco = build_frame_container(ts1, ts2, 1, hive_id=5, transformation_matrix=tfm, data_source_fname="testname") assert fco.hiveId == 5 assert np.all(np.isclose(fco.transformationMatrix, tfm)) assert len(fco.dataSources) == 1 assert fco.dataSources[0].filename == "testname" assert fco.dataSources[0].videoPreviewFilename == "" # filename and video are both strings fco = build_frame_container(ts1, ts2, 1, data_source_fname="testname", video_preview_fname="test_video_name") assert len(fco.dataSources) == 1 assert fco.dataSources[0].filename == "testname" assert fco.dataSources[0].videoPreviewFilename == "test_video_name" # try combination of filenames and preview names fnames = ["testname", "testname 1"] vnames = ["test_video_name", "test_video_name 1"] fco = build_frame_container(ts1, ts2, 1, data_source_fname=fnames, video_preview_fname=vnames) assert len(fco.dataSources) == 2 assert fco.dataSources[0].filename == "testname" assert fco.dataSources[0].videoPreviewFilename == "test_video_name" assert fco.dataSources[1].filename == "testname 1" assert fco.dataSources[1].videoPreviewFilename == "test_video_name 1"
def test_bbb_iter_frames_from_to(tmpdir): """Tests that only frames in given range are iterated.""" repo = Repository(str(tmpdir.join('frames_from_to'))) repo_start = 0 nFC = 10 span = 1000 nFrames = nFC * span repo_end = repo_start + nFrames begin_end_cam_id = [(ts, ts + span, 0) for ts in range(repo_start, repo_end, span)] for begin, end, cam_id in begin_end_cam_id: fc = build_frame_container(begin, end, cam_id) fc.init('frames', span) for i, tstamp in enumerate(range(begin, end)): frame = fc.frames[i] frame.id = tstamp frame.timestamp = tstamp repo.add(fc) def check_tstamp_invariant(begin, end): """Helper to check if begin <= tstamp < end is true for all frames.""" for frame, fc in repo.iter_frames(begin, end): # frame container invariant assert begin < fc.toTimestamp assert fc.fromTimestamp < end # frame invariant assert begin <= frame.timestamp < end # repo_start < start < end < repo_end check_tstamp_invariant(repo_start + 10, repo_end - 10) # start < repo_start < end < repo_end check_tstamp_invariant(repo_start - 10, repo_end - 10) # start < end < repo_start < repo_end check_tstamp_invariant(repo_start - 20, repo_start - 10) # repo_start < start < repo_end < end check_tstamp_invariant(repo_start + 10, repo_end + 10) # repo_start < repo_end < start < end check_tstamp_invariant(repo_end + 10, repo_end + 20) # check whole length all_frames = [f for f, _ in repo.iter_frames()] assert len(all_frames) == nFrames # check with begin = None skip_end = [f for f, _ in repo.iter_frames(end=repo_end - span)] assert len(skip_end) == nFrames - span # check with end = None skip_start = [f for f, _ in repo.iter_frames(begin=span)] assert len(skip_start) == nFrames - span
def test_bbb_repo_add_frame_container(tmpdir): repo = Repository(str(tmpdir)) cam_id = 1 fc = build_frame_container(1000, 5000, 1) repo.add(fc) fnames = repo.find(1000) expected_fname = repo._get_filename(fc.fromTimestamp, fc.toTimestamp, cam_id, 'bbb') expected_fname = os.path.basename(expected_fname) assert os.path.basename(fnames[0]) == expected_fname fnames = repo.find(1500) assert os.path.basename(fnames[0]) == expected_fname fnames = repo.find(2500) assert os.path.basename(fnames[0]) == expected_fname
def test_bbb_repo_iter_frames_filtered(tmpdir): repo = Repository(str(tmpdir.join('frames_from_to_filtered'))) repo_start = 0 nFC = 10 span = 1000 nFrames = nFC * span repo_end = repo_start + nFrames begin_end_cam_id = [(ts, ts + span, 0) for ts in range(repo_start, repo_end, span)] for begin, end, cam_id in begin_end_cam_id: fc = build_frame_container(begin, end, cam_id) fc.init('frames', span) for i, tstamp in enumerate(range(begin, end)): frame = fc.frames[i] frame.id = tstamp frame.timestamp = tstamp repo.add(fc) timestamps = [f[0].timestamp for f in repo.iter_frames(begin, end)] selected_timestamps = set( np.random.choice(timestamps, size=5, replace=False)) class TimestepFilter(): def __init__(self, timesteps): self.timesteps = timesteps def __call__(self, frame): return frame.timestamp in self.timesteps filtered_frames = list( repo.iter_frames(begin, end, frame_filter=TimestepFilter(selected_timestamps))) filtered_timestamps = set([f[0].timestamp for f in filtered_frames]) assert (filtered_timestamps == selected_timestamps)
def test_benchmark_add(benchmark, example_experiment_repo): repo, begin, end = example_experiment_repo ts = random.randint(begin, end) duration = 500 + random.randint(0, 250) cam_id = 0 nb_bits = 12 frame_container = build_frame_container(ts, ts + duration, cam_id) frames = frame_container.init('frames', 1024) frame_ts = ts for i, frame in enumerate(frames): nb_detections = random.randint(75, 150) detections = np.random.uniform(0, 1, (nb_detections, nb_parameters(nb_bits))) build_frame(frame, frame_ts, detections, frame_idx=i) def add(): ts = random.randint(begin, end) duration = 500 + random.randint(0, 250) frame_container.fromTimestamp = ts frame_container.toTimestamp = ts + duration repo.add(frame_container) benchmark(add)