def test_intersect_sum():
    truth = _load_truth()
    isetmap = MmapIntervalSetMapping(DATA_PATH)

    for _ in range(N_REPEAT):
        i = random.choice(list(truth.keys()))
        true_sum = sum(b - a for a, b in truth[i])
        assert true_sum == isetmap.intersect_sum(i, [(0, MAX_T)], False)
def test_sum():
    truth = _load_truth()
    isetmap = MmapIntervalSetMapping(DATA_PATH)
    true_sum = sum(
        sum(b - a for a, b in intervals)
        for intervals in truth.values())
    calc_sum = isetmap.sum()
    assert _is_close(true_sum, calc_sum), \
        'diff: {} -- {}'.format(true_sum, calc_sum)
def test_contains():
    truth = _load_truth()
    isetmap = MmapIntervalSetMapping(DATA_PATH)

    i = random.choice(list(truth.keys()))

    def truth_contains(v):
        for (a, b) in truth[i]:
            if v >= a and v < b:
                return True
        return False

    for v in range(MAX_T):
        assert truth_contains(v) == isetmap.is_contained(i, v, False), \
            'diff: {} -- {}'.format(v, isetmap.get_intervals(i, False))
def test_has_intersection():
    truth = _load_truth()
    isetmap = MmapIntervalSetMapping(DATA_PATH)

    def has_intersection(intervals, a, b):
        for x, y in intervals:
            if min(b, y) - max(a, x) > 0:
                return True
        return False

    for _ in range(N_REPEAT):
        i = random.choice(list(truth.keys()))
        intervals = isetmap.get_intervals(i, False)
        for j in range(MAX_T):
            a = j
            b = j + 1
            assert isetmap.has_intersection(i, a, b, False) == has_intersection(intervals, a, b)
def test_integrity():
    truth = _load_truth()
    isetmap = MmapIntervalSetMapping(DATA_PATH)
    assert len(truth) == isetmap.len()
    assert set(truth.keys()) == set(isetmap.get_ids())
    for i in truth:
        assert isetmap.has_id(i)
        assert len(truth[i]) == isetmap.get_interval_count(i)
        assert len(truth[i]) == len(isetmap.get_intervals(i, False))
        for j, interval in enumerate(truth[i]):
            assert interval == isetmap.get_interval(i, j)
def test_minus():
    truth = _load_truth()
    isetmap = MmapIntervalSetMapping(DATA_PATH)

    def naive_minus(intervals, min_val, max_val):
        result = []
        i = min_val
        for a, b in intervals:
            if a - i > 0:
                result.append((i, a))
            i = b
        if i < max_val:
            result.append((i, max_val))
        return result

    for _ in range(N_REPEAT):
        i = random.choice(list(truth.keys()))
        assert naive_minus(truth[i], 0, MAX_T) == \
            isetmap.minus(i, [(0, MAX_T)], False)
    #     guest_only = guest_all.minus(guest_with_host)

    #     host_only = interviews.overlaps(host).minus(guest_with_host)

    return interviews, guest_only, host_only, guest_with_host


if __name__ == '__main__':

    assert len(sys.argv) == 2, 'Missing input argument'

    hosts = load_hosts()
    guests = load_guests()
    videos = load_videos()
    num_faces = load_num_faces()
    commercials = MmapIntervalSetMapping(COMMERCIALS_FILE)

    guest = [h for h in guests if h.name == sys.argv[1].replace('-', ' ')][0]
    print("Detecting interview for %s..." % guest.name)

    result = {guest.name: []}
    for host in hosts:
        print(host.name)
        host_ids = set(host.ilist.get_ids())
        guest_ids = set(guest.ilist.get_ids())
        video_ids = host_ids & guest_ids
        print(len(video_ids))

        host_ism = rs_to_rekall(host.ilist, video_ids)
        guest_ism = rs_to_rekall(guest.ilist, video_ids)
        num_faces_ism = rs_to_rekall(num_faces, video_ids)
def test_empty_isetmap():
    isetmap = MmapIntervalSetMapping(DATA_PATH)
    assert isetmap.sum() == 0