|
8 | 8 | import audformat
|
9 | 9 | import audiofile
|
10 | 10 | import audiofile as af
|
| 11 | +import audmath |
11 | 12 | import audobject
|
12 | 13 |
|
13 | 14 | import audinterface
|
@@ -1521,102 +1522,146 @@ def process_func(signal, sampling_rate, idx, file, root):
|
1521 | 1522 |
|
1522 | 1523 |
|
1523 | 1524 | @pytest.mark.parametrize(
|
1524 |
| - 'segment', |
| 1525 | + # `starts` and `ends` |
| 1526 | + # are used to create a segment object |
| 1527 | + # using audinterface.utils.signal_index() |
| 1528 | + 'starts, ends', |
1525 | 1529 | [
|
1526 |
| - audinterface.Segment( |
1527 |
| - process_func=lambda x, sr: audinterface.utils.signal_index() |
1528 |
| - ), |
1529 |
| - audinterface.Segment( |
1530 |
| - process_func=lambda x, sr: |
1531 |
| - audinterface.utils.signal_index( |
1532 |
| - pd.to_timedelta(0), |
1533 |
| - pd.to_timedelta(x.shape[1] / sr, unit='s') / 2, |
1534 |
| - ) |
1535 |
| - ), |
1536 |
| - audinterface.Segment( |
1537 |
| - process_func=lambda x, sr: |
1538 |
| - audinterface.utils.signal_index( |
1539 |
| - pd.to_timedelta(x.shape[1] / sr, unit='s') / 2, |
1540 |
| - pd.to_timedelta(x.shape[1] / sr, unit='s'), |
1541 |
| - ) |
1542 |
| - ), |
1543 |
| - audinterface.Segment( |
1544 |
| - process_func=lambda x, sr: |
1545 |
| - audinterface.utils.signal_index( |
1546 |
| - [ |
1547 |
| - pd.to_timedelta(0), |
1548 |
| - pd.to_timedelta(x.shape[1] / sr, unit='s') / 2, |
1549 |
| - ], |
1550 |
| - [ |
1551 |
| - pd.to_timedelta(x.shape[1] / sr, unit='s') / 2, |
1552 |
| - pd.to_timedelta(x.shape[1] / sr), |
1553 |
| - ], |
1554 |
| - ) |
1555 |
| - ) |
| 1530 | + (None, None), |
| 1531 | + (0, 1.5), |
| 1532 | + (1.5, 3), |
| 1533 | + ([0, 1.5], [1.5, 3]), |
| 1534 | + # Blocked by https://github.com/audeering/audinterface/issues/134 |
| 1535 | + # or a similar issue |
| 1536 | + # ([0, 1.5], [1, 2.000000003]), |
| 1537 | + ([0, 2], [1, 3]), |
| 1538 | + ([0, 1], [2, 2]), |
| 1539 | + # https://github.com/audeering/audinterface/issues/135 |
| 1540 | + ([0, 1], [3, 2]), |
1556 | 1541 | ]
|
1557 | 1542 | )
|
1558 |
| -def test_process_with_segment(tmpdir, segment): |
| 1543 | +def test_process_with_segment(tmpdir, starts, ends): |
1559 | 1544 |
|
1560 |
| - process = audinterface.Process() |
1561 |
| - process_with_segment = audinterface.Process( |
1562 |
| - segment=segment, |
| 1545 | + # Segment and process objects |
| 1546 | + segment = audinterface.Segment( |
| 1547 | + process_func=lambda x, sr: |
| 1548 | + audinterface.utils.signal_index(starts, ends) |
1563 | 1549 | )
|
| 1550 | + process = audinterface.Process() |
| 1551 | + process_with_segment = audinterface.Process(segment=segment) |
1564 | 1552 |
|
1565 |
| - # create signal and file |
| 1553 | + # Create signal and file |
1566 | 1554 | sampling_rate = 8000
|
1567 |
| - signal = np.zeros((1, sampling_rate)) |
| 1555 | + if ends is None: |
| 1556 | + duration = 1 |
| 1557 | + else: |
| 1558 | + duration = audmath.duration_in_seconds( |
| 1559 | + max(audeer.to_list(ends)) |
| 1560 | + ) |
| 1561 | + signal = np.zeros((1, audmath.samples(duration, sampling_rate))) |
1568 | 1562 | root = tmpdir
|
1569 | 1563 | file = 'file.wav'
|
1570 | 1564 | path = os.path.join(root, file)
|
1571 | 1565 | audiofile.write(path, signal, sampling_rate)
|
1572 | 1566 |
|
| 1567 | + # Expected index |
| 1568 | + if starts is None: |
| 1569 | + files = None |
| 1570 | + files_abs = None |
| 1571 | + else: |
| 1572 | + files = [file] * len(audeer.to_list(starts)) |
| 1573 | + files_abs = [audeer.path(root, file) for file in files] |
| 1574 | + expected = audformat.segmented_index(files, starts, ends) |
| 1575 | + expected_folder_index = audformat.segmented_index(files_abs, starts, ends) |
| 1576 | + expected_signal_index = audinterface.utils.signal_index(starts, ends) |
| 1577 | + |
1573 | 1578 | # process signal
|
1574 |
| - index = segment.process_signal( |
| 1579 | + index = segment.process_signal(signal, sampling_rate) |
| 1580 | + pd.testing.assert_index_equal(index, expected_signal_index) |
| 1581 | + |
| 1582 | + # process signal with start argument |
| 1583 | + index = segment.process_signal(signal, sampling_rate, start=0) |
| 1584 | + pd.testing.assert_index_equal(index, expected_signal_index) |
| 1585 | + |
| 1586 | + # process signal with file argument |
| 1587 | + index = segment.process_signal(signal, sampling_rate, file=file) |
| 1588 | + pd.testing.assert_index_equal(index, expected) |
| 1589 | + |
| 1590 | + pd.testing.assert_series_equal( |
| 1591 | + process.process_index(index, root=root, preserve_index=True), |
| 1592 | + process_with_segment.process_signal(signal, sampling_rate, file=file) |
| 1593 | + ) |
| 1594 | + |
| 1595 | + # process signal from index |
| 1596 | + index = segment.process_signal_from_index( |
1575 | 1597 | signal,
|
1576 | 1598 | sampling_rate,
|
1577 |
| - file=file, |
| 1599 | + audinterface.utils.signal_index(0, duration), |
1578 | 1600 | )
|
1579 |
| - pd.testing.assert_series_equal( |
1580 |
| - process.process_index(index, root=root), |
1581 |
| - process_with_segment.process_signal( |
1582 |
| - signal, |
1583 |
| - sampling_rate, |
1584 |
| - file=file, |
1585 |
| - ) |
| 1601 | + pd.testing.assert_index_equal(index, expected_signal_index) |
| 1602 | + index = segment.process_signal_from_index( |
| 1603 | + signal, |
| 1604 | + sampling_rate, |
| 1605 | + audformat.segmented_index(file, 0, duration), |
1586 | 1606 | )
|
| 1607 | + pd.testing.assert_index_equal(index, expected) |
1587 | 1608 | index = segment.process_signal_from_index(
|
1588 | 1609 | signal,
|
1589 | 1610 | sampling_rate,
|
1590 | 1611 | audformat.filewise_index(file),
|
1591 | 1612 | )
|
| 1613 | + pd.testing.assert_index_equal(index, expected) |
| 1614 | + |
1592 | 1615 | pd.testing.assert_series_equal(
|
1593 |
| - process.process_index(index, root=root), |
| 1616 | + process.process_index(index, root=root, preserve_index=True), |
1594 | 1617 | process_with_segment.process_signal_from_index(
|
1595 | 1618 | signal,
|
1596 | 1619 | sampling_rate,
|
1597 | 1620 | audformat.filewise_index(file),
|
1598 |
| - ) |
| 1621 | + ), |
1599 | 1622 | )
|
1600 | 1623 |
|
1601 | 1624 | # process file
|
1602 | 1625 | index = segment.process_file(file, root=root)
|
| 1626 | + pd.testing.assert_index_equal(index, expected) |
| 1627 | + |
1603 | 1628 | pd.testing.assert_series_equal(
|
1604 |
| - process.process_index(index, root=root), |
1605 |
| - process_with_segment.process_file(file, root=root) |
1606 |
| - ) |
1607 |
| - index = segment.process_index( |
1608 |
| - audformat.filewise_index(file), |
1609 |
| - root=root, |
| 1629 | + process.process_index(index, root=root, preserve_index=True), |
| 1630 | + process_with_segment.process_file(file, root=root), |
1610 | 1631 | )
|
| 1632 | + |
| 1633 | + # process files |
| 1634 | + index = segment.process_files([file], root=root) |
| 1635 | + pd.testing.assert_index_equal(index, expected) |
| 1636 | + |
| 1637 | + # https://github.com/audeering/audinterface/issues/138 |
| 1638 | + # pd.testing.assert_series_equal( |
| 1639 | + # process.process_index(index, root=root, preserve_index=True), |
| 1640 | + # process_with_segment.process_files([file], root=root) |
| 1641 | + # ) |
| 1642 | + |
| 1643 | + # process folder |
| 1644 | + index = segment.process_folder(root) |
| 1645 | + pd.testing.assert_index_equal(index, expected_folder_index) |
| 1646 | + |
| 1647 | + # https://github.com/audeering/audinterface/issues/139 |
| 1648 | + # pd.testing.assert_series_equal( |
| 1649 | + # process.process_index(index, root=root, preserve_index=True), |
| 1650 | + # process_with_segment.process_folder(root), |
| 1651 | + # ) |
| 1652 | + |
| 1653 | + # process index |
| 1654 | + index = segment.process_index(audformat.filewise_index(file), root=root) |
| 1655 | + pd.testing.assert_index_equal(index, expected) |
| 1656 | + |
1611 | 1657 | pd.testing.assert_series_equal(
|
1612 |
| - process.process_index(index, root=root), |
| 1658 | + process.process_index(index, root=root, preserve_index=True), |
1613 | 1659 | process_with_segment.process_index(
|
1614 | 1660 | audformat.filewise_index(file),
|
1615 | 1661 | root=root,
|
1616 |
| - ) |
| 1662 | + ), |
1617 | 1663 | )
|
1618 | 1664 |
|
1619 |
| - |
1620 | 1665 | def test_read_audio(tmpdir):
|
1621 | 1666 | sampling_rate = 8000
|
1622 | 1667 | signal = np.ones((1, 8000))
|
|
0 commit comments