Skip to content

Commit

Permalink
Add Dataset.tables_rows
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Sep 26, 2024
1 parent 72330b4 commit 1ea4b99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
41 changes: 35 additions & 6 deletions audbcards/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,7 @@ def tables_preview(self) -> typing.Dict[str, typing.List[typing.List[str]]]:
"""
preview = {}
for table in list(self.header):
df = audb.load_table(
self.name,
table,
version=self.version,
verbose=False,
)
df = self._tables[table]
df = df.reset_index()
header = [df.columns.tolist()]
body = df.head(5).astype("string").values.tolist()
Expand All @@ -555,6 +550,26 @@ def tables_preview(self) -> typing.Dict[str, typing.List[typing.List[str]]]:
preview[table] = header + body
return preview

@functools.cached_property
def tables_rows(self) -> typing.Dict[str, int]:
"""Number of rows for each table of the dataset.
Returns:
dictionary with table IDs as keys
and number of rows as values
Examples:
>>> ds = Dataset("emodb", "1.4.1")
>>> ds.tables_rows["speaker"]
10
"""
rows = {}
for table in list(self.header):
df = self._tables[table]
rows[table] = len(df)
return rows

@functools.cached_property
def tables_table(self) -> typing.List[str]:
"""Tables of the dataset."""
Expand Down Expand Up @@ -751,6 +766,20 @@ def _segments(self) -> pd.MultiIndex:
index = audformat.utils.union([index, df.index])
return index

@functools.cached_property
def _tables(self) -> typing.Dict[str, pd.DataFrame]:
"""Dataframes of tables in the dataset."""
tables = {}
for table in list(self.header):
df = audb.load_table(
self.name,
table,
version=self.version,
verbose=False,
)
tables[table] = df
return tables

@staticmethod
def _map_iso_languages(languages: typing.List[str]) -> typing.List[str]:
r"""Calculate ISO languages for a list of languages.
Expand Down
1 change: 1 addition & 0 deletions audbcards/core/templates/datacard_tables.j2
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Tables
</tr>
{% endif %}
{% endfor %}
<tr><td><p>{{ tables_rows[row[0]] }} rows</p></td></tr>
</tbody>
</table>

Expand Down
6 changes: 6 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ def test_dataset(audb_cache, tmpdir, repository, db, request):
expected_tables = list(db)
assert dataset.tables == expected_tables

# tables_rows
expected_tables_rows = {}
for table_id in list(db):
expected_tables_rows[table_id] = len(db[table_id])
assert dataset.tables_rows == expected_tables_rows

# tables_table
expected_tables_table = [["ID", "Type", "Columns"]]
for table_id in list(db):
Expand Down

0 comments on commit 1ea4b99

Please sign in to comment.