def test_iterating_over_queue():
    inps = [str(i) for i in range(50)]
    q = SimpleLabellingQueue()
    q.enqueue_many(inps)

    for i, (id_, val) in enumerate(q):
        assert i == id_
def test_shuffling():
    inps = [str(i) for i in range(50)]
    q = SimpleLabellingQueue()
    q.enqueue_many(inps)
    q.shuffle()
    # assert the order is not the same:
    assert any(val != inp for inp, (id_, val) in zip(inps, q))
def test_progress():
    inps = [str(i) for i in range(50)]
    q = SimpleLabellingQueue()
    # assert my little hack for dividing by zero
    assert q.progress == 0
    q.enqueue_many(inps)

    for i, (id_, val) in enumerate(q):
        assert q.progress == i / len(inps)
        q.submit(id_, str(i))
def test_enqueue_many(inputs):
    n = len(inputs)
    q = SimpleLabellingQueue()
    q.enqueue_many(inputs)
    assert len(q.data) == n
    # assert we can pop everything:
    for _ in range(n):
        q.pop()
    # assert there's nothing else to pop:
    with pytest.raises(IndexError):
        q.pop()
def test_reordering():
    inp = ["b", "a", "d", "c"]
    q = SimpleLabellingQueue()
    q.enqueue_many(inp)
    q.reorder(OrderedDict([(0, 1), (1, 0), (2, 3), (3, 2)]))

    id_, val = q.pop()
    assert val == "a" and id_ == 1
    id_, val = q.pop()
    assert val == "b" and id_ == 0
    id_, val = q.pop()
    assert val == "c" and id_ == 3
    id_, val = q.pop()
    assert val == "d" and id_ == 2
def test_list_uncompleted(inputs, labels):
    q = SimpleLabellingQueue()
    q.enqueue_many(inputs)

    popped_ids = []
    for i in range(5):
        id_, val = q.pop()
        q.submit(id_, labels[i])

        popped_ids.append(id_)

    ids, x = q.list_uncompleted()

    assert len(ids) == (len(inputs) - 5)
    # test that the popped IDs and completed IDs don't share members
    assert pytest.helpers.no_shared_members(ids, popped_ids)
    assert pytest.helpers.same_elements(x, inputs[5:])
def test_list_completed(inputs, labels):
    q = SimpleLabellingQueue()
    q.enqueue_many(inputs)

    popped_ids = []
    for i in range(5):
        id_, val = q.pop()
        q.submit(id_, labels[i])

        popped_ids.append(id_)

    ids, x, y = q.list_completed()

    assert len(ids) == 5
    # test that the popped IDs and completed IDs have the same members
    assert pytest.helpers.same_elements(ids, popped_ids)
    assert pytest.helpers.same_elements(y, labels[:5])
def test_enqueue_array(inputs):
    n = inputs.shape[0]
    q = SimpleLabellingQueue()
    q.enqueue_many(inputs)
    assert len(q.data) == n
    # assert we can pop everything:
    for _ in range(n):
        id_, val = q.pop()
        assert isinstance(val, np.ndarray)
        assert len(val.shape) == 1
        assert val.size == inputs.shape[-1]
    # assert there's nothing else to pop:
    with pytest.raises(IndexError):
        q.pop()
    # assert it re-constructs a df on list all
    if n > 0:
        ids, X, y = q.list_all()
        assert isinstance(X, np.ndarray)
def test_list_all(inputs, labels):
    q = SimpleLabellingQueue()
    q.enqueue_many(inputs)

    popped_ids = []
    for i in range(5):
        id_, val = q.pop()
        q.submit(id_, labels[i])

        popped_ids.append(id_)

    ids, x, y = q.list_all()

    assert len(ids) == len(inputs)
    assert all(label in labels for label in y if label is not None)
    assert all(label is None or id_ in popped_ids
               for id_, label in zip(ids, y))
    assert Counter(y)[None] == (len(inputs) - 5)
    assert pytest.helpers.same_elements(ids, range(len(inputs)))
def test_enqueue_with_labels(inputs, labels):

    if len(labels) > len(inputs):
        labels = labels[:len(inputs)]
    elif len(labels) < len(inputs):
        labels += [None] * (len(inputs) - len(labels))

    n = len(inputs) - (len(labels) - Counter(labels)[None])

    q = SimpleLabellingQueue()
    q.enqueue_many(inputs, labels)

    assert len(q.data) == len(inputs)
    # assert we can pop everything where the label was not none:
    for _ in range(n):
        q.pop()
    # assert there's nothing else to pop:
    with pytest.raises(IndexError):
        q.pop()
def test_enqueue_dataframe(inputs):
    n = len(inputs)
    q = SimpleLabellingQueue()
    q.enqueue_many(inputs)
    assert len(q.data) == n
    # assert we can pop everything:
    for _ in range(n):
        id_, val = q.pop()
        assert isinstance(val, pd.Series)
    # assert there's nothing else to pop:
    with pytest.raises(IndexError):
        q.pop()
    # assert it re-constructs a df on list all
    if n > 0:
        ids, X, y = q.list_all()
        assert isinstance(X, pd.DataFrame) or len(X) == 0
        # assert it re-constructs a df on list uncomplete
        q.submit(ids[0], "hello")
        ids, X = q.list_uncompleted()
        assert isinstance(X, pd.DataFrame) or len(X) == 0
        # assert it re-constructs a df on list uncomplete
        ids, X, y = q.list_completed()
        assert isinstance(X, pd.DataFrame) or len(X) == 0
def test_length_of_queue():
    inps = [str(i) for i in range(50)]
    q = SimpleLabellingQueue()
    assert len(q) == 0
    q.enqueue_many(inps)
    assert len(q) == len(inps)