audformat 1.3.4__tar.gz → 1.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {audformat-1.3.4 → audformat-1.4.1}/.github/workflows/doc.yml +1 -1
- {audformat-1.3.4 → audformat-1.4.1}/.github/workflows/publish.yml +1 -1
- {audformat-1.3.4 → audformat-1.4.1}/.github/workflows/test.yml +1 -2
- {audformat-1.3.4 → audformat-1.4.1}/CHANGELOG.rst +49 -0
- {audformat-1.3.4 → audformat-1.4.1}/PKG-INFO +2 -2
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/database.py +7 -4
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/scheme.py +120 -7
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/table.py +109 -27
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/utils.py +125 -71
- {audformat-1.3.4 → audformat-1.4.1}/audformat.egg-info/PKG-INFO +2 -2
- {audformat-1.3.4 → audformat-1.4.1}/audformat.egg-info/SOURCES.txt +1 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat.egg-info/requires.txt +1 -1
- audformat-1.4.1/benchmarks/benchmark_hash.py +118 -0
- {audformat-1.3.4 → audformat-1.4.1}/pyproject.toml +4 -1
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_database.py +261 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_index.py +69 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_misc_table.py +52 -2
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_scheme.py +138 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_table.py +217 -23
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_utils.py +193 -28
- {audformat-1.3.4 → audformat-1.4.1}/.github/workflows/linter.yml +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/.gitignore +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/.pre-commit-config.yaml +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/CONTRIBUTING.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/LICENSE +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/README.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/__init__.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/__init__.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/attachment.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/column.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/common.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/conftest.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/define.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/errors.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/index.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/media.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/rater.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/split.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/testing.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/core/typing.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/define/__init__.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/errors/__init__.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/testing/__init__.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat/utils/__init__.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat.egg-info/dependency_links.txt +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/audformat.egg-info/top_level.txt +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/benchmarks/benchmark_union.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/accessing-data.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api/audformat.define.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api/audformat.errors.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api/audformat.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api/audformat.testing.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api/audformat.utils.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api-src/audformat.define.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api-src/audformat.errors.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api-src/audformat.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api-src/audformat.testing.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/api-src/audformat.utils.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/changelog.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/combine-tables.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/conf.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/conftest.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/contributing.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/create-database.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-conventions.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-example.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-format.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-header.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-introduction.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-misc-tables.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/data-tables.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/emodb-example.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/genindex.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/index.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/install.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/map-scheme.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/pics/audformat-database.dot +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/pics/audformat-misc-table.dot +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/pics/audformat-table.dot +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/docs/update-database.rst +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/setup.cfg +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/conftest.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_attachment.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_column.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_common.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_database_get.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_eq.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_errors.py +0 -0
- {audformat-1.3.4 → audformat-1.4.1}/tests/test_utils_concat.py +0 -0
|
@@ -7,6 +7,55 @@ The format is based on `Keep a Changelog`_,
|
|
|
7
7
|
and this project adheres to `Semantic Versioning`_.
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
Version 1.4.1 (2026-04-13)
|
|
11
|
+
--------------------------
|
|
12
|
+
|
|
13
|
+
* Added: include table ID in error messages in the methods
|
|
14
|
+
``drop_index()``,
|
|
15
|
+
``extend_index()``,
|
|
16
|
+
``pick_index()``,
|
|
17
|
+
``update()``
|
|
18
|
+
of ``audformat.Table``
|
|
19
|
+
and ``audformat.MiscTable``
|
|
20
|
+
if the receiving table is assigned to a database
|
|
21
|
+
* Changed: black list ``pandas``
|
|
22
|
+
versions 3.0.0, 3.0.1, and 3.0.2
|
|
23
|
+
as those contain a serious issue in ``pd.to_timedelta()``
|
|
24
|
+
(`pandas#65150 <https://github.com/pandas-dev/pandas/issues/65150>`_)
|
|
25
|
+
* Fixed: avoid warning when loading a table
|
|
26
|
+
with data points not matching scheme labels.
|
|
27
|
+
As before those points are set to ``None``.
|
|
28
|
+
This is now documented in ``audformat.Table.load()``
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Version 1.4.0 (2026-04-08)
|
|
32
|
+
--------------------------
|
|
33
|
+
|
|
34
|
+
* Added: ``audformat.Scheme.set_labels()``
|
|
35
|
+
to add labels to an existing scheme
|
|
36
|
+
that has none.
|
|
37
|
+
To replace labels
|
|
38
|
+
use ``audformat.Scheme.replace_labels()``
|
|
39
|
+
* Added: store ``audformat`` version in table files.
|
|
40
|
+
For parquet files it is stored under the ``audformat-version`` metadata key.
|
|
41
|
+
Pickle files store now a dictionary with the keys
|
|
42
|
+
``audformat-version`` and ``df``,
|
|
43
|
+
whereas ``df`` contains the table data
|
|
44
|
+
* Fixed: ``audformat.utils.hash()`` for large dataframes.
|
|
45
|
+
Before it resulted in the same hash
|
|
46
|
+
for large dataframes
|
|
47
|
+
that differed only in rows in their center.
|
|
48
|
+
This is a breaking change
|
|
49
|
+
and will result in different hash values.
|
|
50
|
+
When using ``audb.publish()``
|
|
51
|
+
to update a database
|
|
52
|
+
not published with ``audformat>=1.4.0``,
|
|
53
|
+
it will result in republishing all existing tables,
|
|
54
|
+
which is the desired behavior
|
|
55
|
+
* Fixed: loading scheme labels from misc table,
|
|
56
|
+
when using ``audformat.Database.load(..., load_data=True)``
|
|
57
|
+
|
|
58
|
+
|
|
10
59
|
Version 1.3.4 (2026-02-18)
|
|
11
60
|
--------------------------
|
|
12
61
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: audformat
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.4.1
|
|
4
4
|
Summary: Python implementation of audformat
|
|
5
5
|
Author: Johannes Wagner, BahaEddine Abrougui
|
|
6
6
|
Author-email: Hagen Wierstorf <hwierstorf@audeering.com>, Christian Geng <cgeng@audeering.com>
|
|
@@ -28,7 +28,7 @@ Requires-Dist: audiofile>=0.4.0
|
|
|
28
28
|
Requires-Dist: iso639-lang
|
|
29
29
|
Requires-Dist: iso3166
|
|
30
30
|
Requires-Dist: oyaml
|
|
31
|
-
Requires-Dist: pandas
|
|
31
|
+
Requires-Dist: pandas!=3.0.0,!=3.0.1,!=3.0.2,>=2.1.0
|
|
32
32
|
Requires-Dist: pyarrow>=10.0.1
|
|
33
33
|
Requires-Dist: pyyaml>=5.4.1
|
|
34
34
|
Dynamic: license-file
|
|
@@ -1444,14 +1444,17 @@ class Database(HeaderBase):
|
|
|
1444
1444
|
params = []
|
|
1445
1445
|
table_ids = []
|
|
1446
1446
|
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1447
|
+
# Load misc_tables first,
|
|
1448
|
+
# as they might be used as scheme labels
|
|
1449
|
+
# by regular tables
|
|
1451
1450
|
if "misc_tables" in header and header["misc_tables"]:
|
|
1452
1451
|
for table_id in header["misc_tables"]:
|
|
1453
1452
|
table_ids.append(table_id)
|
|
1454
1453
|
|
|
1454
|
+
if "tables" in header and header["tables"]:
|
|
1455
|
+
for table_id in header["tables"]:
|
|
1456
|
+
table_ids.append(table_id)
|
|
1457
|
+
|
|
1455
1458
|
for table_id in table_ids:
|
|
1456
1459
|
table = db[table_id]
|
|
1457
1460
|
if load_data:
|
|
@@ -363,20 +363,133 @@ class Scheme(common.HeaderBase):
|
|
|
363
363
|
)
|
|
364
364
|
|
|
365
365
|
self.labels = labels
|
|
366
|
+
self._update_column_categories(labels)
|
|
367
|
+
|
|
368
|
+
def set_labels(
|
|
369
|
+
self,
|
|
370
|
+
labels: dict | list | str,
|
|
371
|
+
):
|
|
372
|
+
r"""Set labels on a scheme that has none.
|
|
373
|
+
|
|
374
|
+
If scheme is part of a :class:`audformat.Database`
|
|
375
|
+
the dtype of all :class:`audformat.Column` objects
|
|
376
|
+
that reference the scheme will be converted
|
|
377
|
+
to :class:`pandas.CategoricalDtype`.
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
labels: new labels
|
|
381
|
+
|
|
382
|
+
Raises:
|
|
383
|
+
ValueError: if scheme already defines labels
|
|
384
|
+
ValueError: if dtype of labels does not match dtype of scheme
|
|
385
|
+
ValueError: if ``labels`` is not a list, dictionary,
|
|
386
|
+
or ID of a misc table
|
|
387
|
+
ValueError: if ``labels`` is a misc table ID
|
|
388
|
+
and the scheme is already assigned to a database,
|
|
389
|
+
but the corresponding misc table is not part of the database,
|
|
390
|
+
or the given table ID is not a misc table,
|
|
391
|
+
or its index is multi-dimensional,
|
|
392
|
+
or its index contains duplicates,
|
|
393
|
+
or the misc table has a column
|
|
394
|
+
that is already assigned to a scheme
|
|
395
|
+
with labels from another misc table
|
|
396
|
+
ValueError: if existing column data contains values
|
|
397
|
+
not covered by ``labels``
|
|
398
|
+
|
|
399
|
+
Examples:
|
|
400
|
+
>>> emotion = Scheme("str")
|
|
401
|
+
>>> emotion
|
|
402
|
+
{dtype: str}
|
|
403
|
+
>>> emotion.set_labels(["happy", "sad", "angry"])
|
|
404
|
+
>>> emotion
|
|
405
|
+
dtype: str
|
|
406
|
+
labels: [happy, sad, angry]
|
|
407
|
+
>>> emotion = Scheme("int")
|
|
408
|
+
>>> emotion.set_labels(
|
|
409
|
+
... {
|
|
410
|
+
... 0: {"emotion": "happy"},
|
|
411
|
+
... 1: {"emotion": "sad"},
|
|
412
|
+
... }
|
|
413
|
+
... )
|
|
414
|
+
>>> emotion
|
|
415
|
+
dtype: int
|
|
416
|
+
labels:
|
|
417
|
+
0: {emotion: happy}
|
|
418
|
+
1: {emotion: sad}
|
|
419
|
+
|
|
420
|
+
"""
|
|
421
|
+
if self.labels is not None:
|
|
422
|
+
raise ValueError(
|
|
423
|
+
"Cannot set labels when scheme already defines labels. "
|
|
424
|
+
"Use replace_labels() instead."
|
|
425
|
+
)
|
|
426
|
+
self._check_labels(labels)
|
|
427
|
+
|
|
428
|
+
if not isinstance(labels, str) or self._db is not None:
|
|
429
|
+
# Check change of data type
|
|
430
|
+
# for list, dict and assigned misc table
|
|
431
|
+
dtype_labels = self._dtype_from_labels(labels)
|
|
432
|
+
if dtype_labels != self.dtype:
|
|
433
|
+
raise ValueError(
|
|
434
|
+
"Data type of labels "
|
|
435
|
+
f"'{dtype_labels}' "
|
|
436
|
+
f"does not match data type of scheme "
|
|
437
|
+
f"'{self.dtype}'."
|
|
438
|
+
)
|
|
366
439
|
|
|
367
440
|
if self._db is not None and self._id is not None:
|
|
368
|
-
|
|
441
|
+
# Check existing data match new labels
|
|
442
|
+
labels_set = set(self._labels_to_list(labels))
|
|
369
443
|
for table in list(self._db.tables.values()) + list(
|
|
370
444
|
self._db.misc_tables.values()
|
|
371
445
|
):
|
|
372
446
|
for column in table.columns.values():
|
|
373
447
|
if column.scheme_id == self._id:
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
448
|
+
values = set(table.df[column._id].dropna().unique())
|
|
449
|
+
bad_values = values - labels_set
|
|
450
|
+
if bad_values:
|
|
451
|
+
raise ValueError(
|
|
452
|
+
f"Column '{column._id}' "
|
|
453
|
+
f"of table '{table._id}' "
|
|
454
|
+
f"contains values not in labels: "
|
|
455
|
+
f"{sorted(bad_values)}."
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
self.labels = labels
|
|
459
|
+
self._update_column_categories(labels, convert_to_categorical=True)
|
|
460
|
+
|
|
461
|
+
def _update_column_categories(
|
|
462
|
+
self,
|
|
463
|
+
labels: dict | list | str,
|
|
464
|
+
*,
|
|
465
|
+
convert_to_categorical: bool = False,
|
|
466
|
+
):
|
|
467
|
+
r"""Update categories of columns referencing this scheme.
|
|
468
|
+
|
|
469
|
+
Args:
|
|
470
|
+
labels: new labels
|
|
471
|
+
convert_to_categorical: if ``True``,
|
|
472
|
+
convert columns from plain dtype
|
|
473
|
+
to categorical first
|
|
474
|
+
|
|
475
|
+
"""
|
|
476
|
+
if self._db is None or self._id is None:
|
|
477
|
+
return
|
|
478
|
+
|
|
479
|
+
labels_list = self._labels_to_list(labels)
|
|
480
|
+
for table in list(self._db.tables.values()) + list(
|
|
481
|
+
self._db.misc_tables.values()
|
|
482
|
+
):
|
|
483
|
+
for column in table.columns.values():
|
|
484
|
+
if column.scheme_id == self._id:
|
|
485
|
+
y = column._table.df[column._id]
|
|
486
|
+
if convert_to_categorical:
|
|
487
|
+
y = y.astype(self.to_pandas_dtype())
|
|
488
|
+
y = y.cat.set_categories(
|
|
489
|
+
new_categories=labels_list,
|
|
490
|
+
ordered=False,
|
|
491
|
+
)
|
|
492
|
+
column._table.df[column._id] = y
|
|
380
493
|
|
|
381
494
|
def _check_labels(
|
|
382
495
|
self,
|
|
@@ -26,11 +26,18 @@ from audformat.core.index import filewise_index
|
|
|
26
26
|
from audformat.core.index import index_type
|
|
27
27
|
from audformat.core.index import is_filewise_index
|
|
28
28
|
from audformat.core.index import is_segmented_index
|
|
29
|
+
from audformat.core.index import to_timedelta
|
|
29
30
|
from audformat.core.media import Media
|
|
30
31
|
from audformat.core.split import Split
|
|
31
32
|
from audformat.core.typing import Values
|
|
32
33
|
|
|
33
34
|
|
|
35
|
+
# Keys used in parquet metadata and pickle wrapper dict
|
|
36
|
+
_KEY_VERSION = "audformat-version"
|
|
37
|
+
_KEY_DATAFRAME = "df"
|
|
38
|
+
_KEY_HASH = "hash"
|
|
39
|
+
|
|
40
|
+
|
|
34
41
|
class Base(HeaderBase):
|
|
35
42
|
r"""Table base class."""
|
|
36
43
|
|
|
@@ -312,7 +319,7 @@ class Base(HeaderBase):
|
|
|
312
319
|
table = self if inplace else self.copy()
|
|
313
320
|
|
|
314
321
|
index = _maybe_convert_dtype_to_string(index)
|
|
315
|
-
_assert_table_index(
|
|
322
|
+
_assert_table_index(self, index, "drop rows from")
|
|
316
323
|
|
|
317
324
|
index = utils.intersect([table.index, index])
|
|
318
325
|
new_index = utils.difference([table.index, index])
|
|
@@ -349,7 +356,7 @@ class Base(HeaderBase):
|
|
|
349
356
|
table = self if inplace else self.copy()
|
|
350
357
|
|
|
351
358
|
index = _maybe_convert_dtype_to_string(index)
|
|
352
|
-
_assert_table_index(
|
|
359
|
+
_assert_table_index(self, index, "extend")
|
|
353
360
|
|
|
354
361
|
new_index = utils.union([table.index, index])
|
|
355
362
|
table._df = table.df.reindex(new_index)
|
|
@@ -454,6 +461,9 @@ class Base(HeaderBase):
|
|
|
454
461
|
otherwise it will raise an error
|
|
455
462
|
and ask to delete one of the files.
|
|
456
463
|
|
|
464
|
+
If a scheme with labels is assigned to a table column,
|
|
465
|
+
values not matching those labels are set to ``None``.
|
|
466
|
+
|
|
457
467
|
Args:
|
|
458
468
|
path: file path without extension
|
|
459
469
|
|
|
@@ -568,7 +578,7 @@ class Base(HeaderBase):
|
|
|
568
578
|
table = self if inplace else self.copy()
|
|
569
579
|
|
|
570
580
|
index = _maybe_convert_dtype_to_string(index)
|
|
571
|
-
_assert_table_index(
|
|
581
|
+
_assert_table_index(self, index, "pick rows from")
|
|
572
582
|
|
|
573
583
|
new_index = utils.intersect([table.index, index])
|
|
574
584
|
table._df = table.df.reindex(new_index)
|
|
@@ -923,9 +933,9 @@ class Base(HeaderBase):
|
|
|
923
933
|
dtypes_wo_converters = {}
|
|
924
934
|
for column, dtype in columns_and_dtypes.items():
|
|
925
935
|
if dtype == define.DataType.DATE:
|
|
926
|
-
converters[column] =
|
|
936
|
+
converters[column] = pd.to_datetime
|
|
927
937
|
elif dtype == define.DataType.TIME:
|
|
928
|
-
converters[column] =
|
|
938
|
+
converters[column] = to_timedelta
|
|
929
939
|
else:
|
|
930
940
|
dtypes_wo_converters[column] = to_pandas_dtype(dtype)
|
|
931
941
|
|
|
@@ -953,6 +963,13 @@ class Base(HeaderBase):
|
|
|
953
963
|
table = parquet.read_table(path)
|
|
954
964
|
df = self._pyarrow_table_to_dataframe(table)
|
|
955
965
|
|
|
966
|
+
# Read audformat version from metadata.
|
|
967
|
+
# Files written before 1.4.0 don't have this key.
|
|
968
|
+
metadata = table.schema.metadata or {}
|
|
969
|
+
self._audformat_version = (
|
|
970
|
+
metadata.get(_KEY_VERSION.encode(), b"").decode() or None
|
|
971
|
+
)
|
|
972
|
+
|
|
956
973
|
self._df = df
|
|
957
974
|
|
|
958
975
|
def _load_pickled(self, path: str):
|
|
@@ -969,22 +986,47 @@ class Base(HeaderBase):
|
|
|
969
986
|
# but was slower.
|
|
970
987
|
# The try-except statement allows backward compatibility
|
|
971
988
|
try:
|
|
972
|
-
|
|
989
|
+
data = pd.read_pickle(path)
|
|
973
990
|
except pickle.UnpicklingError:
|
|
974
|
-
|
|
991
|
+
data = pd.read_pickle(path, compression="xz")
|
|
992
|
+
|
|
993
|
+
# Since 1.4.0, pickle files store a dict
|
|
994
|
+
# with audformat version and dataframe.
|
|
995
|
+
# Older files store a bare dataframe.
|
|
996
|
+
if isinstance(data, dict) and _KEY_DATAFRAME in data:
|
|
997
|
+
version = data.get(_KEY_VERSION)
|
|
998
|
+
df = data[_KEY_DATAFRAME]
|
|
999
|
+
else:
|
|
1000
|
+
version = None
|
|
1001
|
+
df = data if isinstance(data, pd.DataFrame) else None
|
|
1002
|
+
|
|
1003
|
+
if df is None:
|
|
1004
|
+
raise RuntimeError(
|
|
1005
|
+
f"Cannot load pickle file '{path}', "
|
|
1006
|
+
"expected a pandas DataFrame "
|
|
1007
|
+
"or a dictionary with an "
|
|
1008
|
+
f"'{_KEY_DATAFRAME}' entry."
|
|
1009
|
+
)
|
|
1010
|
+
|
|
1011
|
+
self._audformat_version = version
|
|
975
1012
|
|
|
976
1013
|
# Older versions of audformat stored columns
|
|
977
1014
|
# assigned to a string scheme as 'object',
|
|
978
|
-
# so we need to convert those to 'string'
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1015
|
+
# so we need to convert those to 'string'.
|
|
1016
|
+
# This was fixed in 1.4.0.
|
|
1017
|
+
if _needs_legacy_fixup(version, "1.4.0"):
|
|
1018
|
+
for column_id, column in self.columns.items():
|
|
1019
|
+
if (
|
|
1020
|
+
column.scheme_id is not None
|
|
1021
|
+
and (
|
|
1022
|
+
self.db.schemes[column.scheme_id].dtype
|
|
1023
|
+
== define.DataType.STRING
|
|
1024
|
+
)
|
|
1025
|
+
and df[column_id].dtype == "object"
|
|
1026
|
+
):
|
|
1027
|
+
df[column_id] = df[column_id].astype("string")
|
|
1028
|
+
# Fix index entries as well
|
|
1029
|
+
df.index = _maybe_convert_dtype_to_string(df.index)
|
|
988
1030
|
|
|
989
1031
|
self._df = df
|
|
990
1032
|
|
|
@@ -1014,7 +1056,10 @@ class Base(HeaderBase):
|
|
|
1014
1056
|
``"bool"``, and ``"int"`` audformat dtypes
|
|
1015
1057
|
|
|
1016
1058
|
Returns:
|
|
1017
|
-
dataframe with converted dtypes
|
|
1059
|
+
dataframe with converted dtypes.
|
|
1060
|
+
For columns with scheme labels,
|
|
1061
|
+
values not in the scheme's labels
|
|
1062
|
+
are set to NaN
|
|
1018
1063
|
|
|
1019
1064
|
"""
|
|
1020
1065
|
# Collect columns with dtypes,
|
|
@@ -1072,12 +1117,14 @@ class Base(HeaderBase):
|
|
|
1072
1117
|
for column in time_columns:
|
|
1073
1118
|
df[column] = df[column].astype("timedelta64[ns]")
|
|
1074
1119
|
for column in object_columns:
|
|
1075
|
-
df[column] = df[column].astype("object")
|
|
1076
|
-
df[column] = df[column].replace(pd.NA, None)
|
|
1120
|
+
df[column] = df[column].astype("object").replace(pd.NA, None)
|
|
1077
1121
|
for column in labeled_columns:
|
|
1078
1122
|
scheme = self.db.schemes[self.columns[column].scheme_id]
|
|
1079
1123
|
labels = scheme._labels_to_list()
|
|
1080
1124
|
dtype = to_categorical_dtype(labels)
|
|
1125
|
+
# Set values not in scheme labels to NaN
|
|
1126
|
+
# https://github.com/audeering/audformat/issues/525
|
|
1127
|
+
df[column] = df[column].where(df[column].isin(labels))
|
|
1081
1128
|
df[column] = df[column].astype(dtype)
|
|
1082
1129
|
return df
|
|
1083
1130
|
|
|
@@ -1190,20 +1237,29 @@ class Base(HeaderBase):
|
|
|
1190
1237
|
path: path, including file extension
|
|
1191
1238
|
|
|
1192
1239
|
"""
|
|
1193
|
-
table = pa.Table.from_pandas(self.df.reset_index(), preserve_index=False)
|
|
1194
|
-
|
|
1195
1240
|
# Create hash of table
|
|
1196
1241
|
table_hash = utils.hash(self.df, strict=True)
|
|
1197
1242
|
|
|
1198
|
-
|
|
1243
|
+
table = pa.Table.from_pandas(self.df.reset_index(), preserve_index=False)
|
|
1244
|
+
|
|
1245
|
+
# Store hash in metadata of file,
|
|
1199
1246
|
# see https://stackoverflow.com/a/58978449
|
|
1200
|
-
|
|
1247
|
+
import audformat
|
|
1248
|
+
|
|
1249
|
+
metadata = {_KEY_HASH: table_hash, _KEY_VERSION: audformat.__version__}
|
|
1201
1250
|
table = table.replace_schema_metadata({**metadata, **table.schema.metadata})
|
|
1202
1251
|
|
|
1203
1252
|
parquet.write_table(table, path, compression="snappy")
|
|
1204
1253
|
|
|
1205
1254
|
def _save_pickled(self, path: str):
|
|
1206
|
-
|
|
1255
|
+
import audformat
|
|
1256
|
+
|
|
1257
|
+
data = {
|
|
1258
|
+
_KEY_VERSION: audformat.__version__,
|
|
1259
|
+
_KEY_DATAFRAME: self.df,
|
|
1260
|
+
}
|
|
1261
|
+
pd.to_pickle(
|
|
1262
|
+
data,
|
|
1207
1263
|
path,
|
|
1208
1264
|
protocol=4, # supported by Python >= 3.4
|
|
1209
1265
|
)
|
|
@@ -1846,17 +1902,43 @@ class Table(Base):
|
|
|
1846
1902
|
return levels_and_dtypes
|
|
1847
1903
|
|
|
1848
1904
|
|
|
1905
|
+
def _needs_legacy_fixup(version: str | None, fixed_in: str) -> bool:
|
|
1906
|
+
r"""Return if a file needs a legacy fixup.
|
|
1907
|
+
|
|
1908
|
+
Returns ``True`` if ``version`` is ``None``
|
|
1909
|
+
(file written before version tracking was added)
|
|
1910
|
+
or if ``version`` is older than ``fixed_in``.
|
|
1911
|
+
|
|
1912
|
+
Args:
|
|
1913
|
+
version: audformat version that wrote the file,
|
|
1914
|
+
or ``None`` for unknown
|
|
1915
|
+
fixed_in: version in which the fixup was applied
|
|
1916
|
+
|
|
1917
|
+
Returns:
|
|
1918
|
+
``True`` if the legacy fixup should be run
|
|
1919
|
+
|
|
1920
|
+
"""
|
|
1921
|
+
if version is None:
|
|
1922
|
+
return True
|
|
1923
|
+
from packaging.version import Version
|
|
1924
|
+
|
|
1925
|
+
return Version(version) < Version(fixed_in)
|
|
1926
|
+
|
|
1927
|
+
|
|
1849
1928
|
def _assert_table_index(
|
|
1850
1929
|
table: Base,
|
|
1851
1930
|
index: pd.Index,
|
|
1852
1931
|
operation: str,
|
|
1853
1932
|
):
|
|
1854
1933
|
r"""Raise error if index does not match table."""
|
|
1934
|
+
table_id = getattr(table, "_id", None)
|
|
1935
|
+
table_ref = f" '{table_id}'" if table_id is not None else ""
|
|
1855
1936
|
if isinstance(table, Table):
|
|
1856
1937
|
input_type = index_type(index)
|
|
1857
1938
|
if table.type != input_type:
|
|
1858
1939
|
raise ValueError(
|
|
1859
|
-
f"Cannot {operation} a {table.type}
|
|
1940
|
+
f"Cannot {operation} a {table.type} "
|
|
1941
|
+
f"table{table_ref} with a {input_type} index."
|
|
1860
1942
|
)
|
|
1861
1943
|
elif not utils.is_index_alike([table.index, index]):
|
|
1862
1944
|
want = (
|
|
@@ -1876,7 +1958,7 @@ def _assert_table_index(
|
|
|
1876
1958
|
raise ValueError(
|
|
1877
1959
|
f"Cannot "
|
|
1878
1960
|
f"{operation} "
|
|
1879
|
-
f"table if input index and table index are not alike.\n"
|
|
1961
|
+
f"table{table_ref} if input index and table index are not alike.\n"
|
|
1880
1962
|
f"Expected index:\n"
|
|
1881
1963
|
f"\t{want}"
|
|
1882
1964
|
f"\nbut yours is:\n"
|