garmentiq.utils.check_filenames_metadata

Checking that dataset filenames agree with the metadata table.

 1"""Checking that dataset filenames agree with the metadata table."""
 2import os
 3import shutil
 4
 5
 6def check_filenames_metadata(output_dir, file_dir, metadata_df):
 7    """
 8    Validates that the filenames in a directory match those listed in a metadata DataFrame.
 9
10    This function compares the filenames found in the specified directory with the filenames
11    listed in the provided metadata DataFrame. If the filenames do not match exactly (after sorting),
12    the output directory is deleted and a `ValueError` is raised. If they match, a confirmation
13    message is printed.
14
15    Args:
16        output_dir (str): Path to the output directory that will be deleted if filenames do not match.
17        file_dir (str): Path to the directory containing the files to check.
18        metadata_df (pandas.DataFrame): A pandas DataFrame containing a 'filename' column with expected filenames.
19
20    Raises:
21        ValueError: If the filenames in the directory do not match those in the metadata.
22
23    Returns:
24        None
25    """
26    file_list = os.listdir(file_dir)
27    metadata_filenames = metadata_df["filename"].tolist()
28    file_list.sort()
29    metadata_filenames.sort()
30
31    if file_list != metadata_filenames:
32        shutil.rmtree(output_dir)
33        raise ValueError(
34            f"Mismatch between directory filenames and metadata filenames. Maybe try again."
35        )
36    else:
37        print(f"\nAll filenames in {file_dir} match the metadata.\n")
def check_filenames_metadata(output_dir, file_dir, metadata_df):
 7def check_filenames_metadata(output_dir, file_dir, metadata_df):
 8    """
 9    Validates that the filenames in a directory match those listed in a metadata DataFrame.
10
11    This function compares the filenames found in the specified directory with the filenames
12    listed in the provided metadata DataFrame. If the filenames do not match exactly (after sorting),
13    the output directory is deleted and a `ValueError` is raised. If they match, a confirmation
14    message is printed.
15
16    Args:
17        output_dir (str): Path to the output directory that will be deleted if filenames do not match.
18        file_dir (str): Path to the directory containing the files to check.
19        metadata_df (pandas.DataFrame): A pandas DataFrame containing a 'filename' column with expected filenames.
20
21    Raises:
22        ValueError: If the filenames in the directory do not match those in the metadata.
23
24    Returns:
25        None
26    """
27    file_list = os.listdir(file_dir)
28    metadata_filenames = metadata_df["filename"].tolist()
29    file_list.sort()
30    metadata_filenames.sort()
31
32    if file_list != metadata_filenames:
33        shutil.rmtree(output_dir)
34        raise ValueError(
35            f"Mismatch between directory filenames and metadata filenames. Maybe try again."
36        )
37    else:
38        print(f"\nAll filenames in {file_dir} match the metadata.\n")

Validates that the filenames in a directory match those listed in a metadata DataFrame.

This function compares the filenames found in the specified directory with the filenames listed in the provided metadata DataFrame. If the filenames do not match exactly (after sorting), the output directory is deleted and a ValueError is raised. If they match, a confirmation message is printed.

Arguments:
  • output_dir (str): Path to the output directory that will be deleted if filenames do not match.
  • file_dir (str): Path to the directory containing the files to check.
  • metadata_df (pandas.DataFrame): A pandas DataFrame containing a 'filename' column with expected filenames.
Raises:
  • ValueError: If the filenames in the directory do not match those in the metadata.
Returns:

None