audformat 1.2.0__tar.gz → 1.3.0__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.2.0 → audformat-1.3.0}/.github/workflows/test.yml +3 -0
- {audformat-1.2.0 → audformat-1.3.0}/CHANGELOG.rst +19 -0
- {audformat-1.2.0 → audformat-1.3.0}/LICENSE +2 -1
- {audformat-1.2.0 → audformat-1.3.0}/PKG-INFO +5 -4
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/database.py +1 -1
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/table.py +76 -72
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/utils.py +58 -8
- {audformat-1.2.0 → audformat-1.3.0}/audformat.egg-info/PKG-INFO +5 -4
- {audformat-1.2.0 → audformat-1.3.0}/pyproject.toml +2 -1
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_table.py +73 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_utils.py +156 -6
- {audformat-1.2.0 → audformat-1.3.0}/.github/workflows/doc.yml +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/.github/workflows/linter.yml +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/.github/workflows/publish.yml +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/.gitignore +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/.pre-commit-config.yaml +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/CONTRIBUTING.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/README.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/__init__.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/__init__.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/attachment.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/column.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/common.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/conftest.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/define.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/errors.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/index.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/media.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/rater.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/scheme.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/split.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/testing.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/core/typing.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/define/__init__.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/errors/__init__.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/testing/__init__.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat/utils/__init__.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat.egg-info/SOURCES.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat.egg-info/dependency_links.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat.egg-info/requires.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/audformat.egg-info/top_level.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/benchmarks/benchmark_union.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/accessing-data.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api/audformat.define.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api/audformat.errors.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api/audformat.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api/audformat.testing.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api/audformat.utils.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api-src/audformat.define.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api-src/audformat.errors.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api-src/audformat.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api-src/audformat.testing.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/api-src/audformat.utils.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/changelog.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/combine-tables.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/conf.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/contributing.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/create-database.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-conventions.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-example.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-format.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-header.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-introduction.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-misc-tables.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/data-tables.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/emodb-example.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/genindex.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/index.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/install.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/map-scheme.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/pics/audformat-database.dot +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/pics/audformat-misc-table.dot +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/pics/audformat-table.dot +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/requirements.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/docs/update-database.rst +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/requirements.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/setup.cfg +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/conftest.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/requirements.txt +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_attachment.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_column.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_common.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_database.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_database_get.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_eq.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_errors.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_index.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_misc_table.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_scheme.py +0 -0
- {audformat-1.2.0 → audformat-1.3.0}/tests/test_utils_concat.py +0 -0
|
@@ -59,6 +59,9 @@ jobs:
|
|
|
59
59
|
pip install "pandas==2.1.0"
|
|
60
60
|
pip install "pyarrow==10.0.1"
|
|
61
61
|
pip install "pyyaml==5.4.1"
|
|
62
|
+
# need to use an audb version in the test,
|
|
63
|
+
# that supports audeer==2.0.0
|
|
64
|
+
pip install "audb==1.7.4"
|
|
62
65
|
if: matrix.requirements == 'minimum'
|
|
63
66
|
|
|
64
67
|
- name: Test with pytest
|
|
@@ -7,6 +7,25 @@ The format is based on `Keep a Changelog`_,
|
|
|
7
7
|
and this project adheres to `Semantic Versioning`_.
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
Version 1.3.0 (2024-07-18)
|
|
11
|
+
--------------------------
|
|
12
|
+
|
|
13
|
+
* Added: ``strict`` argument
|
|
14
|
+
to ``audformat.utils.hash()``.
|
|
15
|
+
If set to ``True``,
|
|
16
|
+
the order of the data,
|
|
17
|
+
and its level/column names
|
|
18
|
+
are taken into account
|
|
19
|
+
when calculating the hash
|
|
20
|
+
* Changed: store tables per default as parquet files,
|
|
21
|
+
by changing the default value of ``storage_format``
|
|
22
|
+
to ``"parquet"``
|
|
23
|
+
in ``audformat.Table.save()``
|
|
24
|
+
and ``audformat.Database.save()``
|
|
25
|
+
* Fixed: load csv tables with ``pandas.read_csv()``,
|
|
26
|
+
if ``pyarrow.csv.read_csv()`` fails
|
|
27
|
+
|
|
28
|
+
|
|
10
29
|
Version 1.2.0 (2024-06-25)
|
|
11
30
|
--------------------------
|
|
12
31
|
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: audformat
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Python implementation of audformat
|
|
5
|
-
Author: BahaEddine Abrougui
|
|
6
|
-
Author-email: Hagen Wierstorf <hwierstorf@audeering.com>,
|
|
5
|
+
Author: Johannes Wagner, BahaEddine Abrougui
|
|
6
|
+
Author-email: Hagen Wierstorf <hwierstorf@audeering.com>, Christian Geng <cgeng@audeering.com>
|
|
7
7
|
License: MIT License
|
|
8
8
|
|
|
9
9
|
Copyright (c) 2018-present audEERING GmbH and Contributors
|
|
10
10
|
|
|
11
11
|
Authors:
|
|
12
|
-
Johannes Wagner
|
|
13
12
|
Hagen Wierstorf
|
|
13
|
+
Johannes Wagner
|
|
14
|
+
Christian Geng
|
|
14
15
|
Baha Eddine Abrougui
|
|
15
16
|
|
|
16
17
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
@@ -970,7 +970,7 @@ class Database(HeaderBase):
|
|
|
970
970
|
*,
|
|
971
971
|
name: str = "db",
|
|
972
972
|
indent: int = 2,
|
|
973
|
-
storage_format: str = define.TableStorageFormat.
|
|
973
|
+
storage_format: str = define.TableStorageFormat.PARQUET,
|
|
974
974
|
update_other_formats: bool = True,
|
|
975
975
|
header_only: bool = False,
|
|
976
976
|
num_workers: typing.Optional[int] = 1,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations # allow typing without string
|
|
2
2
|
|
|
3
3
|
import copy
|
|
4
|
-
import hashlib
|
|
5
4
|
import os
|
|
6
5
|
import pickle
|
|
7
6
|
import typing
|
|
@@ -18,6 +17,7 @@ from audformat.core import utils
|
|
|
18
17
|
from audformat.core.column import Column
|
|
19
18
|
from audformat.core.common import HeaderBase
|
|
20
19
|
from audformat.core.common import HeaderDict
|
|
20
|
+
from audformat.core.common import to_pandas_dtype
|
|
21
21
|
from audformat.core.errors import BadIdError
|
|
22
22
|
from audformat.core.index import filewise_index
|
|
23
23
|
from audformat.core.index import index_type
|
|
@@ -579,13 +579,35 @@ class Base(HeaderBase):
|
|
|
579
579
|
self,
|
|
580
580
|
path: str,
|
|
581
581
|
*,
|
|
582
|
-
storage_format: str = define.TableStorageFormat.
|
|
582
|
+
storage_format: str = define.TableStorageFormat.PARQUET,
|
|
583
583
|
update_other_formats: bool = True,
|
|
584
584
|
):
|
|
585
585
|
r"""Save table data to disk.
|
|
586
586
|
|
|
587
587
|
Existing files will be overwritten.
|
|
588
588
|
|
|
589
|
+
When using ``"parquet"`` as ``storage_format``
|
|
590
|
+
a hash,
|
|
591
|
+
based on the content of the table,
|
|
592
|
+
is stored under the key ``b"hash"``
|
|
593
|
+
in the metadata of the schema of the parquet file.
|
|
594
|
+
This provides a deterministic hash for the file,
|
|
595
|
+
as md5 sums of parquet files,
|
|
596
|
+
containing identical information,
|
|
597
|
+
often differ.
|
|
598
|
+
Reasons include factors like the library
|
|
599
|
+
that wrote the parquet file,
|
|
600
|
+
the chosen compression codec
|
|
601
|
+
and metadata written by the library.
|
|
602
|
+
|
|
603
|
+
The hash can be accessed with ``pyarrow`` by::
|
|
604
|
+
|
|
605
|
+
pyarrow.parquet.read_schema(f"{path}.parquet").metadata[b"hash"].decode()
|
|
606
|
+
|
|
607
|
+
The hash is used by :mod:`audb`
|
|
608
|
+
when publishing a database
|
|
609
|
+
to track changes of database files.
|
|
610
|
+
|
|
589
611
|
Args:
|
|
590
612
|
path: file path without extension
|
|
591
613
|
storage_format: storage format of table.
|
|
@@ -858,6 +880,8 @@ class Base(HeaderBase):
|
|
|
858
880
|
than the method applied here.
|
|
859
881
|
We first load the CSV file as a :class:`pyarrow.Table`
|
|
860
882
|
and convert it to a dataframe afterwards.
|
|
883
|
+
If this fails,
|
|
884
|
+
we fall back to :func:`pandas.read_csv()`.
|
|
861
885
|
|
|
862
886
|
Args:
|
|
863
887
|
path: path to table, including file extension
|
|
@@ -865,18 +889,54 @@ class Base(HeaderBase):
|
|
|
865
889
|
"""
|
|
866
890
|
levels = list(self._levels_and_dtypes.keys())
|
|
867
891
|
columns = list(self.columns.keys())
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
892
|
+
try:
|
|
893
|
+
table = csv.read_csv(
|
|
894
|
+
path,
|
|
895
|
+
read_options=csv.ReadOptions(
|
|
896
|
+
column_names=levels + columns,
|
|
897
|
+
skip_rows=1,
|
|
898
|
+
),
|
|
899
|
+
convert_options=csv.ConvertOptions(
|
|
900
|
+
column_types=self._pyarrow_csv_schema(),
|
|
901
|
+
strings_can_be_null=True,
|
|
902
|
+
),
|
|
903
|
+
)
|
|
904
|
+
df = self._pyarrow_table_to_dataframe(table, from_csv=True)
|
|
905
|
+
except pa.lib.ArrowInvalid:
|
|
906
|
+
# If pyarrow fails to parse the CSV file
|
|
907
|
+
# https://github.com/audeering/audformat/issues/449
|
|
908
|
+
|
|
909
|
+
# Collect csv file columns and data types.
|
|
910
|
+
# index
|
|
911
|
+
columns_and_dtypes = self._levels_and_dtypes
|
|
912
|
+
# columns
|
|
913
|
+
for column_id, column in self.columns.items():
|
|
914
|
+
if column.scheme_id is not None:
|
|
915
|
+
columns_and_dtypes[column_id] = self.db.schemes[
|
|
916
|
+
column.scheme_id
|
|
917
|
+
].dtype
|
|
918
|
+
else:
|
|
919
|
+
columns_and_dtypes[column_id] = define.DataType.OBJECT
|
|
920
|
+
|
|
921
|
+
# Replace data type with converter for dates or timestamps
|
|
922
|
+
converters = {}
|
|
923
|
+
dtypes_wo_converters = {}
|
|
924
|
+
for column, dtype in columns_and_dtypes.items():
|
|
925
|
+
if dtype == define.DataType.DATE:
|
|
926
|
+
converters[column] = lambda x: pd.to_datetime(x)
|
|
927
|
+
elif dtype == define.DataType.TIME:
|
|
928
|
+
converters[column] = lambda x: pd.to_timedelta(x)
|
|
929
|
+
else:
|
|
930
|
+
dtypes_wo_converters[column] = to_pandas_dtype(dtype)
|
|
931
|
+
|
|
932
|
+
df = pd.read_csv(
|
|
933
|
+
path,
|
|
934
|
+
usecols=list(columns_and_dtypes.keys()),
|
|
935
|
+
dtype=dtypes_wo_converters,
|
|
936
|
+
index_col=levels,
|
|
937
|
+
converters=converters,
|
|
938
|
+
float_precision="round_trip",
|
|
939
|
+
)
|
|
880
940
|
|
|
881
941
|
self._df = df
|
|
882
942
|
|
|
@@ -1137,13 +1197,11 @@ class Base(HeaderBase):
|
|
|
1137
1197
|
table = pa.Table.from_pandas(self.df.reset_index(), preserve_index=False)
|
|
1138
1198
|
|
|
1139
1199
|
# Create hash of table
|
|
1140
|
-
table_hash =
|
|
1141
|
-
table_hash.update(_schema_hash(table))
|
|
1142
|
-
table_hash.update(_dataframe_hash(self.df))
|
|
1200
|
+
table_hash = utils.hash(self.df, strict=True)
|
|
1143
1201
|
|
|
1144
1202
|
# Store in metadata of file,
|
|
1145
1203
|
# see https://stackoverflow.com/a/58978449
|
|
1146
|
-
metadata = {"hash": table_hash
|
|
1204
|
+
metadata = {"hash": table_hash}
|
|
1147
1205
|
table = table.replace_schema_metadata({**metadata, **table.schema.metadata})
|
|
1148
1206
|
|
|
1149
1207
|
parquet.write_table(table, path, compression="snappy")
|
|
@@ -1844,40 +1902,6 @@ def _assert_table_index(
|
|
|
1844
1902
|
)
|
|
1845
1903
|
|
|
1846
1904
|
|
|
1847
|
-
def _dataframe_hash(df: pd.DataFrame) -> bytes:
|
|
1848
|
-
"""Hash a dataframe.
|
|
1849
|
-
|
|
1850
|
-
The hash value takes into account:
|
|
1851
|
-
|
|
1852
|
-
* index of dataframe
|
|
1853
|
-
* values of the dataframe
|
|
1854
|
-
* order of dataframe rows
|
|
1855
|
-
|
|
1856
|
-
It does not consider:
|
|
1857
|
-
|
|
1858
|
-
* column names of dataframe
|
|
1859
|
-
* dtypes of dataframe
|
|
1860
|
-
|
|
1861
|
-
Args:
|
|
1862
|
-
df: dataframe
|
|
1863
|
-
|
|
1864
|
-
Returns:
|
|
1865
|
-
MD5 hash in bytes
|
|
1866
|
-
|
|
1867
|
-
"""
|
|
1868
|
-
md5 = hashlib.md5()
|
|
1869
|
-
for _, y in df.reset_index().items():
|
|
1870
|
-
# Convert every column to a numpy array,
|
|
1871
|
-
# and hash its string representation
|
|
1872
|
-
if y.dtype == "Int64":
|
|
1873
|
-
# Enforce consistent conversion to numpy.array
|
|
1874
|
-
# for integers across different pandas versions
|
|
1875
|
-
# (since pandas 2.2.x, Int64 is converted to float if it contains <NA>)
|
|
1876
|
-
y = y.astype("float")
|
|
1877
|
-
md5.update(bytes(str(y.to_numpy()), "utf-8"))
|
|
1878
|
-
return md5.digest()
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
1905
|
def _maybe_convert_dtype_to_string(
|
|
1882
1906
|
index: pd.Index,
|
|
1883
1907
|
) -> pd.Index:
|
|
@@ -1900,23 +1924,3 @@ def _maybe_update_scheme(
|
|
|
1900
1924
|
for scheme in table.db.schemes.values():
|
|
1901
1925
|
if table._id == scheme.labels:
|
|
1902
1926
|
scheme.replace_labels(table._id)
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
def _schema_hash(table: pa.Table) -> bytes:
|
|
1906
|
-
r"""Hash pyarrow table schema.
|
|
1907
|
-
|
|
1908
|
-
Args:
|
|
1909
|
-
table: pyarrow table
|
|
1910
|
-
|
|
1911
|
-
Returns:
|
|
1912
|
-
MD5 hash in bytes
|
|
1913
|
-
|
|
1914
|
-
"""
|
|
1915
|
-
schema_str = table.schema.to_string(
|
|
1916
|
-
# schema.metadata contains pandas related information,
|
|
1917
|
-
# and the used pyarrow and pandas version,
|
|
1918
|
-
# and needs to be excluded
|
|
1919
|
-
show_field_metadata=False,
|
|
1920
|
-
show_schema_metadata=False,
|
|
1921
|
-
)
|
|
1922
|
-
return hashlib.md5(schema_str.encode()).digest()
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import collections
|
|
2
2
|
import errno
|
|
3
|
+
import hashlib
|
|
3
4
|
import os
|
|
4
5
|
import platform
|
|
5
6
|
import re
|
|
@@ -10,6 +11,7 @@ import iso639
|
|
|
10
11
|
import iso3166
|
|
11
12
|
import numpy as np
|
|
12
13
|
import pandas as pd
|
|
14
|
+
import pyarrow as pa
|
|
13
15
|
|
|
14
16
|
import audeer
|
|
15
17
|
import audiofile
|
|
@@ -664,10 +666,12 @@ def expand_file_path(
|
|
|
664
666
|
|
|
665
667
|
def hash(
|
|
666
668
|
obj: typing.Union[pd.Index, pd.Series, pd.DataFrame],
|
|
669
|
+
strict: bool = False,
|
|
667
670
|
) -> str:
|
|
668
671
|
r"""Create hash from object.
|
|
669
672
|
|
|
670
|
-
|
|
673
|
+
If ``strict`` is ``False``,
|
|
674
|
+
objects with the same elements
|
|
671
675
|
produce the same hash string
|
|
672
676
|
independent of the ordering of the elements,
|
|
673
677
|
and level or column names.
|
|
@@ -676,29 +680,75 @@ def hash(
|
|
|
676
680
|
|
|
677
681
|
If ``obj`` is a dataframe or series
|
|
678
682
|
with data type ``"Int64"``,
|
|
683
|
+
and ``strict`` is ``False``,
|
|
679
684
|
the returned hash value changes with ``pandas>=2.2.0``.
|
|
680
685
|
|
|
681
686
|
Args:
|
|
682
687
|
obj: object
|
|
688
|
+
strict: if ``True``,
|
|
689
|
+
the hash takes into account
|
|
690
|
+
the order of rows
|
|
691
|
+
and column/level names
|
|
683
692
|
|
|
684
693
|
Returns:
|
|
685
|
-
hash string
|
|
694
|
+
hash string with 19 characters,
|
|
695
|
+
or 32 characters if ``strict`` is ``True``
|
|
686
696
|
|
|
687
697
|
Examples:
|
|
688
698
|
>>> index = filewise_index(["f1", "f2"])
|
|
689
699
|
>>> hash(index)
|
|
690
700
|
'-4231615416436839963'
|
|
701
|
+
>>> hash(index[::-1]) # reversed index
|
|
702
|
+
'-4231615416436839963'
|
|
691
703
|
>>> y = pd.Series(0, index)
|
|
692
704
|
>>> hash(y)
|
|
693
705
|
'5251663970176285425'
|
|
706
|
+
>>> hash(index, strict=True)
|
|
707
|
+
'0741235e2250e0fcd9ab7b64972f5047'
|
|
708
|
+
>>> hash(index[::-1], strict=True) # reversed index
|
|
709
|
+
'c6639d377897dd9353dc3e8b2968170d'
|
|
694
710
|
|
|
695
711
|
"""
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
712
|
+
if strict:
|
|
713
|
+
if isinstance(obj, pd.Index):
|
|
714
|
+
df = obj.to_frame()
|
|
715
|
+
elif isinstance(obj, pd.Series):
|
|
716
|
+
df = obj.to_frame().reset_index()
|
|
717
|
+
else:
|
|
718
|
+
df = obj.reset_index()
|
|
719
|
+
# Handle column names and dtypes
|
|
720
|
+
table = pa.Table.from_pandas(df, preserve_index=False)
|
|
721
|
+
schema_str = table.schema.to_string(
|
|
722
|
+
# schema.metadata contains pandas related information,
|
|
723
|
+
# and the used pyarrow and pandas version,
|
|
724
|
+
# and needs to be excluded
|
|
725
|
+
show_field_metadata=False,
|
|
726
|
+
show_schema_metadata=False,
|
|
727
|
+
)
|
|
728
|
+
schema_md5 = hashlib.md5(schema_str.encode())
|
|
729
|
+
# Handle index, values, and row order
|
|
730
|
+
data_md5 = hashlib.md5()
|
|
731
|
+
for _, y in df.items():
|
|
732
|
+
# Convert every column to a numpy array,
|
|
733
|
+
# and hash its string representation
|
|
734
|
+
if y.dtype == "Int64":
|
|
735
|
+
# Enforce consistent conversion to numpy.array
|
|
736
|
+
# for integers across different pandas versions
|
|
737
|
+
# (since pandas 2.2.x, Int64 is converted to float if it contains <NA>)
|
|
738
|
+
y = y.astype("float")
|
|
739
|
+
data_md5.update(bytes(str(y.to_numpy()), "utf-8"))
|
|
740
|
+
md5 = hashlib.md5()
|
|
741
|
+
md5.update(schema_md5.digest())
|
|
742
|
+
md5.update(data_md5.digest())
|
|
743
|
+
md5 = md5.hexdigest()
|
|
744
|
+
else:
|
|
745
|
+
# Convert to int64
|
|
746
|
+
# to enforce same behavior
|
|
747
|
+
# across different pandas versions,
|
|
748
|
+
# see
|
|
749
|
+
# https://github.com/pandas-dev/pandas/issues/55452
|
|
750
|
+
md5 = str(pd.util.hash_pandas_object(obj).astype("int64").sum())
|
|
751
|
+
return md5
|
|
702
752
|
|
|
703
753
|
|
|
704
754
|
def index_has_overlap(
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: audformat
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Python implementation of audformat
|
|
5
|
-
Author: BahaEddine Abrougui
|
|
6
|
-
Author-email: Hagen Wierstorf <hwierstorf@audeering.com>,
|
|
5
|
+
Author: Johannes Wagner, BahaEddine Abrougui
|
|
6
|
+
Author-email: Hagen Wierstorf <hwierstorf@audeering.com>, Christian Geng <cgeng@audeering.com>
|
|
7
7
|
License: MIT License
|
|
8
8
|
|
|
9
9
|
Copyright (c) 2018-present audEERING GmbH and Contributors
|
|
10
10
|
|
|
11
11
|
Authors:
|
|
12
|
-
Johannes Wagner
|
|
13
12
|
Hagen Wierstorf
|
|
13
|
+
Johannes Wagner
|
|
14
|
+
Christian Geng
|
|
14
15
|
Baha Eddine Abrougui
|
|
15
16
|
|
|
16
17
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
name = 'audformat'
|
|
5
5
|
authors = [
|
|
6
6
|
{name = 'Hagen Wierstorf', email = 'hwierstorf@audeering.com'},
|
|
7
|
-
{name = 'Johannes Wagner'
|
|
7
|
+
{name = 'Johannes Wagner'},
|
|
8
|
+
{name = 'Christian Geng', email = 'cgeng@audeering.com'},
|
|
8
9
|
{name = 'BahaEddine Abrougui'},
|
|
9
10
|
]
|
|
10
11
|
description = 'Python implementation of audformat'
|
|
@@ -1143,6 +1143,79 @@ def test_load(tmpdir):
|
|
|
1143
1143
|
os.remove(f"{path_no_ext}.{ext}")
|
|
1144
1144
|
|
|
1145
1145
|
|
|
1146
|
+
class TestLoadBrokenCsv:
|
|
1147
|
+
r"""Test loading of malformed csv files.
|
|
1148
|
+
|
|
1149
|
+
If csv files contain a lot of special characters,
|
|
1150
|
+
or a different number of columns,
|
|
1151
|
+
than specified in the database header,
|
|
1152
|
+
loading of them should not fail.
|
|
1153
|
+
|
|
1154
|
+
See https://github.com/audeering/audformat/issues/449
|
|
1155
|
+
|
|
1156
|
+
"""
|
|
1157
|
+
|
|
1158
|
+
def database_with_hidden_columns(self) -> audformat.Database:
|
|
1159
|
+
r"""Database with hidden columns.
|
|
1160
|
+
|
|
1161
|
+
Create database with hidden columns
|
|
1162
|
+
that are stored in csv,
|
|
1163
|
+
but not in the header of the table.
|
|
1164
|
+
|
|
1165
|
+
Ensure:
|
|
1166
|
+
|
|
1167
|
+
* it contains an empty table
|
|
1168
|
+
* the columns use schemes with time and date data types
|
|
1169
|
+
* at least one column has no scheme
|
|
1170
|
+
|
|
1171
|
+
as those cases needed special care with csv files,
|
|
1172
|
+
before switching to use pyarrow.csv.read_csv()
|
|
1173
|
+
in https://github.com/audeering/audformat/pull/419.
|
|
1174
|
+
|
|
1175
|
+
Returns:
|
|
1176
|
+
database
|
|
1177
|
+
|
|
1178
|
+
"""
|
|
1179
|
+
db = audformat.Database("mydb")
|
|
1180
|
+
db.schemes["date"] = audformat.Scheme("date")
|
|
1181
|
+
db.schemes["time"] = audformat.Scheme("time")
|
|
1182
|
+
db["table"] = audformat.Table(audformat.filewise_index("file.wav"))
|
|
1183
|
+
db["table"]["date"] = audformat.Column(scheme_id="date")
|
|
1184
|
+
db["table"]["date"].set([pd.to_datetime("2018-10-26")])
|
|
1185
|
+
db["table"]["time"] = audformat.Column(scheme_id="time")
|
|
1186
|
+
db["table"]["time"].set([pd.Timedelta(1)])
|
|
1187
|
+
db["table"]["no-scheme"] = audformat.Column()
|
|
1188
|
+
db["table"]["no-scheme"].set(["label"])
|
|
1189
|
+
db["empty-table"] = audformat.Table(audformat.filewise_index())
|
|
1190
|
+
db["empty-table"]["column"] = audformat.Column()
|
|
1191
|
+
# Add a hidden column to the table dataframes,
|
|
1192
|
+
# without adding it to the table header
|
|
1193
|
+
db["table"].df["hidden"] = ["hidden"]
|
|
1194
|
+
db["empty-table"].df["hidden"] = []
|
|
1195
|
+
return db
|
|
1196
|
+
|
|
1197
|
+
def test_load_broken_csv(self, tmpdir):
|
|
1198
|
+
r"""Test loading a database table from broken csv files.
|
|
1199
|
+
|
|
1200
|
+
Broken csv files
|
|
1201
|
+
refer to csv tables,
|
|
1202
|
+
that raise an error
|
|
1203
|
+
when loading with ``pyarrow.csv.read_csv()``.
|
|
1204
|
+
|
|
1205
|
+
Args:
|
|
1206
|
+
tmpdir: tmpdir fixture
|
|
1207
|
+
|
|
1208
|
+
"""
|
|
1209
|
+
db = self.database_with_hidden_columns()
|
|
1210
|
+
build_dir = audeer.mkdir(tmpdir, "build")
|
|
1211
|
+
db.save(build_dir, storage_format="csv")
|
|
1212
|
+
db_loaded = audformat.Database.load(build_dir, load_data=True)
|
|
1213
|
+
assert "table" in db_loaded
|
|
1214
|
+
assert "empty-table" in db_loaded
|
|
1215
|
+
assert "hidden" not in db_loaded["table"].df
|
|
1216
|
+
assert "hidden-column" not in db_loaded["empty-table"].df
|
|
1217
|
+
|
|
1218
|
+
|
|
1146
1219
|
def test_load_old_pickle(tmpdir):
|
|
1147
1220
|
# We have stored string dtype as object dtype before
|
|
1148
1221
|
# and have to fix this when loading old PKL files from cache.
|
|
@@ -440,80 +440,219 @@ def test_expand_file_path(tmpdir, index, root, expected):
|
|
|
440
440
|
|
|
441
441
|
|
|
442
442
|
@pytest.mark.parametrize(
|
|
443
|
-
"obj, expected",
|
|
443
|
+
"obj, strict, mutable, expected",
|
|
444
444
|
[
|
|
445
445
|
(
|
|
446
446
|
audformat.filewise_index(),
|
|
447
|
+
False,
|
|
448
|
+
True,
|
|
447
449
|
"0",
|
|
448
450
|
),
|
|
451
|
+
(
|
|
452
|
+
audformat.filewise_index(),
|
|
453
|
+
True,
|
|
454
|
+
True,
|
|
455
|
+
"890fa7e5864779b7c3bd85c9ede31657",
|
|
456
|
+
),
|
|
449
457
|
(
|
|
450
458
|
audformat.segmented_index(),
|
|
459
|
+
False,
|
|
460
|
+
True,
|
|
451
461
|
"0",
|
|
452
462
|
),
|
|
463
|
+
(
|
|
464
|
+
audformat.segmented_index(),
|
|
465
|
+
True,
|
|
466
|
+
True,
|
|
467
|
+
"10d5e40fca4e40c6c70ff64495916059",
|
|
468
|
+
),
|
|
453
469
|
(
|
|
454
470
|
audformat.filewise_index(["f1", "f2"]),
|
|
471
|
+
False,
|
|
472
|
+
True,
|
|
455
473
|
"-4231615416436839963",
|
|
456
474
|
),
|
|
475
|
+
(
|
|
476
|
+
audformat.filewise_index(["f1", "f2"]),
|
|
477
|
+
True,
|
|
478
|
+
False,
|
|
479
|
+
"0741235e2250e0fcd9ab7b64972f5047",
|
|
480
|
+
),
|
|
457
481
|
(
|
|
458
482
|
audformat.segmented_index(["f1", "f2"]),
|
|
483
|
+
False,
|
|
484
|
+
True,
|
|
459
485
|
"-2363261461673824215",
|
|
460
486
|
),
|
|
461
487
|
(
|
|
462
488
|
audformat.segmented_index(["f1", "f2"]),
|
|
489
|
+
True,
|
|
490
|
+
False,
|
|
491
|
+
"0e99d54165e6cc2dad2737982853f8c7",
|
|
492
|
+
),
|
|
493
|
+
(
|
|
494
|
+
audformat.segmented_index(["f1", "f2"]),
|
|
495
|
+
False,
|
|
496
|
+
True,
|
|
463
497
|
"-2363261461673824215",
|
|
464
498
|
),
|
|
465
499
|
(
|
|
466
500
|
audformat.segmented_index(["f1", "f2"], [0, 0], [1, 1]),
|
|
501
|
+
False,
|
|
502
|
+
True,
|
|
467
503
|
"-3831446135233514455",
|
|
468
504
|
),
|
|
505
|
+
(
|
|
506
|
+
audformat.segmented_index(["f1", "f2"], [0, 0], [1, 1]),
|
|
507
|
+
True,
|
|
508
|
+
False,
|
|
509
|
+
"396fda484a46686b2b5c41b0ae9c94bd",
|
|
510
|
+
),
|
|
469
511
|
(
|
|
470
512
|
pd.Series([0, 1], audformat.filewise_index(["f1", "f2"])),
|
|
513
|
+
False,
|
|
514
|
+
True,
|
|
471
515
|
"-8245754232361677810",
|
|
472
516
|
),
|
|
517
|
+
(
|
|
518
|
+
pd.Series([0, 1], audformat.filewise_index(["f1", "f2"])),
|
|
519
|
+
True,
|
|
520
|
+
False,
|
|
521
|
+
"28c5f6feb0682079b127d8ce8debebe9",
|
|
522
|
+
),
|
|
473
523
|
(
|
|
474
524
|
pd.DataFrame(
|
|
475
525
|
{"a": [0, 1], "b": [2, 3]},
|
|
476
526
|
audformat.segmented_index(["f1", "f2"], [0, 0], [1, 1]),
|
|
477
527
|
),
|
|
528
|
+
False,
|
|
529
|
+
True,
|
|
478
530
|
"-103439349488189352",
|
|
479
531
|
),
|
|
480
|
-
(
|
|
481
|
-
|
|
532
|
+
(
|
|
533
|
+
pd.DataFrame(
|
|
534
|
+
{"a": [0, 1], "b": [2, 3]},
|
|
535
|
+
audformat.segmented_index(["f1", "f2"], [0, 0], [1, 1]),
|
|
536
|
+
),
|
|
537
|
+
True,
|
|
538
|
+
False,
|
|
539
|
+
"69785e94447fab79f2b65b1dcb4a2122",
|
|
540
|
+
),
|
|
541
|
+
(
|
|
542
|
+
pd.Index([0, 1], name="idx"),
|
|
543
|
+
False,
|
|
544
|
+
True,
|
|
545
|
+
"6238072747940578789",
|
|
546
|
+
),
|
|
547
|
+
(
|
|
548
|
+
pd.Index([0, 1], name="idx"),
|
|
549
|
+
True,
|
|
550
|
+
False,
|
|
551
|
+
"a02406270880cde74e66c07278b765a0",
|
|
552
|
+
),
|
|
553
|
+
(
|
|
554
|
+
pd.Index([0, 1], name="name"),
|
|
555
|
+
False,
|
|
556
|
+
True,
|
|
557
|
+
"6238072747940578789",
|
|
558
|
+
),
|
|
559
|
+
(
|
|
560
|
+
pd.Index([0, 1], name="name"),
|
|
561
|
+
True,
|
|
562
|
+
False,
|
|
563
|
+
"7a8303866a35ececb4ae76a4aa050209",
|
|
564
|
+
),
|
|
482
565
|
(
|
|
483
566
|
pd.MultiIndex.from_arrays(
|
|
484
567
|
[[0, 1], ["a", "b"]],
|
|
485
568
|
names=["idx1", "idx2"],
|
|
486
569
|
),
|
|
570
|
+
False,
|
|
571
|
+
True,
|
|
487
572
|
"8378370490910668918",
|
|
488
573
|
),
|
|
574
|
+
(
|
|
575
|
+
pd.MultiIndex.from_arrays(
|
|
576
|
+
[[0, 1], ["a", "b"]],
|
|
577
|
+
names=["idx1", "idx2"],
|
|
578
|
+
),
|
|
579
|
+
True,
|
|
580
|
+
False,
|
|
581
|
+
"be5373f6d8f801b902d8cf0e2f2a1914",
|
|
582
|
+
),
|
|
489
583
|
(
|
|
490
584
|
pd.MultiIndex.from_arrays(
|
|
491
585
|
[[0, 1], ["a", "b"]],
|
|
492
586
|
names=["name1", "name2"],
|
|
493
587
|
),
|
|
588
|
+
False,
|
|
589
|
+
True,
|
|
494
590
|
"8378370490910668918",
|
|
495
591
|
),
|
|
592
|
+
(
|
|
593
|
+
pd.MultiIndex.from_arrays(
|
|
594
|
+
[[0, 1], ["a", "b"]],
|
|
595
|
+
names=["name1", "name2"],
|
|
596
|
+
),
|
|
597
|
+
True,
|
|
598
|
+
False,
|
|
599
|
+
"3726b3c39fc2c1453bb45a0460630ff7",
|
|
600
|
+
),
|
|
496
601
|
(
|
|
497
602
|
pd.Series([0, 1], name="series"),
|
|
603
|
+
False,
|
|
604
|
+
True,
|
|
498
605
|
"-7179254265801896228",
|
|
499
606
|
),
|
|
607
|
+
(
|
|
608
|
+
pd.Series([0, 1], name="series"),
|
|
609
|
+
True,
|
|
610
|
+
False,
|
|
611
|
+
"ad0f9900c0e2f3954bde3abb6f4a9b61",
|
|
612
|
+
),
|
|
500
613
|
(
|
|
501
614
|
pd.Series([0, 1], name="name"),
|
|
615
|
+
False,
|
|
616
|
+
True,
|
|
502
617
|
"-7179254265801896228",
|
|
503
618
|
),
|
|
619
|
+
(
|
|
620
|
+
pd.Series([0, 1], name="name"),
|
|
621
|
+
True,
|
|
622
|
+
False,
|
|
623
|
+
"648314808f0b27e5c04479ba8509fc25",
|
|
624
|
+
),
|
|
504
625
|
(
|
|
505
626
|
pd.DataFrame([0, 1], columns=["frame"]),
|
|
627
|
+
False,
|
|
628
|
+
True,
|
|
506
629
|
"-7179254265801896228",
|
|
507
630
|
),
|
|
631
|
+
(
|
|
632
|
+
pd.DataFrame([0, 1], columns=["frame"]),
|
|
633
|
+
True,
|
|
634
|
+
False,
|
|
635
|
+
"36d9779e257319ff69515af6b4ade8ad",
|
|
636
|
+
),
|
|
508
637
|
(
|
|
509
638
|
pd.DataFrame([0, 1], columns=["name"]),
|
|
639
|
+
False,
|
|
640
|
+
True,
|
|
510
641
|
"-7179254265801896228",
|
|
511
642
|
),
|
|
643
|
+
(
|
|
644
|
+
pd.DataFrame([0, 1], columns=["name"]),
|
|
645
|
+
True,
|
|
646
|
+
False,
|
|
647
|
+
"648314808f0b27e5c04479ba8509fc25",
|
|
648
|
+
),
|
|
512
649
|
pytest.param(
|
|
513
650
|
pd.DataFrame(
|
|
514
651
|
[0, 1, 2],
|
|
515
652
|
pd.Index([0, 1, 2], dtype="Int64"),
|
|
516
653
|
),
|
|
654
|
+
False,
|
|
655
|
+
True,
|
|
517
656
|
"5440931770055407318",
|
|
518
657
|
marks=pytest.mark.skipif(
|
|
519
658
|
pd.__version__ >= "2.2.0",
|
|
@@ -525,6 +664,8 @@ def test_expand_file_path(tmpdir, index, root, expected):
|
|
|
525
664
|
[0, 1, 2],
|
|
526
665
|
pd.Index([0, 1, 2], dtype="Int64"),
|
|
527
666
|
),
|
|
667
|
+
False,
|
|
668
|
+
True,
|
|
528
669
|
"-5491649331962632325",
|
|
529
670
|
marks=pytest.mark.skipif(
|
|
530
671
|
pd.__version__ < "2.2.0",
|
|
@@ -536,6 +677,8 @@ def test_expand_file_path(tmpdir, index, root, expected):
|
|
|
536
677
|
[0, 1, 2],
|
|
537
678
|
pd.Index([0, 1, 2], dtype="Int64"),
|
|
538
679
|
),
|
|
680
|
+
False,
|
|
681
|
+
True,
|
|
539
682
|
"5440931770055407318",
|
|
540
683
|
marks=pytest.mark.skipif(
|
|
541
684
|
pd.__version__ >= "2.2.0",
|
|
@@ -547,6 +690,8 @@ def test_expand_file_path(tmpdir, index, root, expected):
|
|
|
547
690
|
[0, 1, 2],
|
|
548
691
|
pd.Index([0, 1, 2], dtype="Int64"),
|
|
549
692
|
),
|
|
693
|
+
False,
|
|
694
|
+
True,
|
|
550
695
|
"-5491649331962632325",
|
|
551
696
|
marks=pytest.mark.skipif(
|
|
552
697
|
pd.__version__ < "2.2.0",
|
|
@@ -555,9 +700,14 @@ def test_expand_file_path(tmpdir, index, root, expected):
|
|
|
555
700
|
),
|
|
556
701
|
],
|
|
557
702
|
)
|
|
558
|
-
def test_hash(obj, expected):
|
|
559
|
-
|
|
560
|
-
|
|
703
|
+
def test_hash(obj, strict, mutable, expected):
|
|
704
|
+
md5 = utils.hash(obj, strict=strict)
|
|
705
|
+
reverse_md5 = utils.hash(obj[::-1], strict=strict)
|
|
706
|
+
assert md5 == expected
|
|
707
|
+
if mutable:
|
|
708
|
+
assert reverse_md5 == md5
|
|
709
|
+
else:
|
|
710
|
+
assert reverse_md5 != md5
|
|
561
711
|
|
|
562
712
|
|
|
563
713
|
@pytest.mark.parametrize(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|