Пример #1
0
def test_ContiguousSequenceLabels_init_conti_fail_nonconti(init_small_seqdata):
    """ Test ContiguousSequenceLabels class initializes
        - w/o errors if labels are contiguous
        - raises AssertionError when non-contiguous
    """
    se = init_small_seqdata['starts_ends']
    sr = init_small_seqdata['samplerate']
    l = init_small_seqdata['labels']
    if init_small_seqdata['isconti']:
        s = lu.ContiguousSequenceLabels(se, l, samplerate=sr)

        npt.assert_equal(s.starts_ends, se)
        assert s.samplerate == sr
        assert s.orig_samplerate == sr
        assert s.min_start == init_small_seqdata['minstart']
        assert s.max_end == init_small_seqdata['maxend']
        assert all([e == r for e, r in zip(l, s.labels)]), list(zip(l, s.labels))
        assert len(s) == len(l)

        for ose, ol, (sse, sl) in zip(se[-2:, ...], l[-2:], s[-2:]):
            assert all(x == y for x, y in zip(ose, sse))
            assert ol == sl

        print(s)
    else:
        with pytest.raises(AssertionError):
            lu.ContiguousSequenceLabels(se, l, samplerate=sr)
Пример #2
0
def ContiSequenceLabels_small_seqdata_labels_at_allwithin(request, init_small_seqdata):
    """ fixture with labels_at at different samplerates

    And of course instance of SequenceLabels class that handles both
    contiguous and non-contiguous seqdata
    """
    se = init_small_seqdata['starts_ends']
    sr = init_small_seqdata['samplerate']
    _l = init_small_seqdata['labels']

    la_ends, la_labels = [], []
    if not init_small_seqdata['isconti']:
        pytest.skip(
            "Non-Contiguous Sequence data for ContiguousSequenceLabels "
            "will fail to initialize"
        )
    else:
        for e, l in init_small_seqdata['labels_at']:
            la_ends.append(e)
            la_labels.append(l)

    la_sr = request.param
    # ends are more than likely to be provided as np.ndarray
    la_ends = np.array(la_ends) * la_sr

    s = lu.ContiguousSequenceLabels(se, _l, samplerate=sr)

    return {
        'seqlabelinst': s,
        'ends': la_ends,
        'at_sr': la_sr,
        'target_labels': la_labels,
    }
Пример #3
0
def ContiSequenceLabels_small_seqdata_labels_at_general_with_deflabel(
        request, init_small_seqdata
):  # yapf: disable
    """ fixture with labels_at at different samplerates for general case

    General case where ends can be outside the starts_ends as well

    And of course instance of SequenceLabels class that handles both
    contiguous and non-contiguous seqdata
    """
    if not init_small_seqdata['isconti']:
        pytest.skip(
            "Non-Contiguous Sequence data for ContiguousSequenceLabels "
            "will fail to initialize"
        )

    se = init_small_seqdata['starts_ends']
    sr = init_small_seqdata['samplerate']
    l = init_small_seqdata['labels']

    s = lu.ContiguousSequenceLabels(se, l, samplerate=sr)

    la_sr = 1.0
    with s.samplerate_as(la_sr):
        _se = s.starts_ends
        mins = _se[:, 0].min() - (1 / la_sr)
        maxe = _se[:, 1].max()

    la_ends, la_labels = [], []
    if not init_small_seqdata['isconti']:
        pytest.skip(
            "Non-Contiguous Sequence data for ContiguousSequenceLabels "
            "will fail to initialize"
        )
    else:
        for e, l in init_small_seqdata['labels_at']:
            la_ends.append(e)
            la_labels.append(l)

        la_ends.extend([mins, maxe])
        la_labels.extend([request.param, request.param])

    # ends are more than likely to be provided as np.ndarray
    la_ends = np.array(la_ends) * la_sr

    return {
        'seqlabelinst': s,
        'ends': la_ends,
        'at_sr': la_sr,
        'target_labels': la_labels,
        'deflabel': request.param,
    }
Пример #4
0
def ContiSequenceLabels_small_seqdata_labels_at_outside(request, init_small_seqdata):
    """ fixture with labels_at at different samplerates

    And of course instance of SequenceLabels class that handles both
    contiguous and non-contiguous seqdata
    """
    if not init_small_seqdata['isconti']:
        pytest.skip(
            "Non-Contiguous Sequence data for ContiguousSequenceLabels "
            "will fail to initialize"
        )

    se = init_small_seqdata['starts_ends']
    sr = init_small_seqdata['samplerate']
    _l = init_small_seqdata['labels']

    s = lu.ContiguousSequenceLabels(se, _l, samplerate=sr)

    sminstart = s.starts_ends[:, 0].min()
    smaxend = s.starts_ends[:, 1].max()
    la_ends = [sminstart - (1 / sr), smaxend, smaxend + (1 / sr)]
    # Yes, there is no label for smaxend. So the default_label is expected
    # Why? We are looking at the label for the segment between
    # (x) and (x + (1/samplerate)) when finding labels_at
    # and we don't have any info about the label after smaxend
    # It's like array indexing (there is no element at len(arr)),
    # or 24-hr clocks (there is 24:00:00 for a date)

    la_labels = [request.param for _ in range(len(la_ends))]

    return {
        'seqlabelinst': s,
        'ends': la_ends,
        'target_labels': la_labels,
        'default_label': request.param
    }
Пример #5
0
def test_ContiguousSequenceLabels_doesnt_init():
    # Non-iterables
    with pytest.raises(TypeError):
        lu.ContiguousSequenceLabels(0, 0, 1)

    # iterables of mismatching length
    with pytest.raises(AssertionError):
        lu.ContiguousSequenceLabels([0], [0, 1])

    # starts_ends not like starts_ends
    with pytest.raises(AssertionError):
        lu.ContiguousSequenceLabels([0], [[0, 1]])
    with pytest.raises(AssertionError):
        lu.ContiguousSequenceLabels([[0, 1, 2]], [[0, 1]])

    # ends before starts
    with pytest.raises(ValueError):
        lu.ContiguousSequenceLabels([[1, 0]], [[0, 1]])

    # bad samplerate
    with pytest.raises(ValueError):
        lu.ContiguousSequenceLabels([[0, 1]], [[0, 1]], 0)
    with pytest.raises(ValueError):
        lu.ContiguousSequenceLabels([[0, 1]], [[0, 1]], -1)