check_dataset.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os.path as osp
  15. from collections import defaultdict
  16. from pathlib import Path
  17. import pandas as pd
  18. from .....utils.errors import DatasetFileNotFoundError
  19. def check(dataset_dir, output, sample_num=10):
  20. """check dataset"""
  21. dataset_dir = osp.abspath(dataset_dir)
  22. if not osp.exists(dataset_dir) or not osp.isdir(dataset_dir):
  23. raise DatasetFileNotFoundError(file_path=dataset_dir)
  24. sample_cnts = dict()
  25. tables = defaultdict(list)
  26. vis_save_dir = osp.join(output, "demo_data")
  27. tags = ["train", "val"]
  28. for _, tag in enumerate(tags):
  29. file_list = osp.join(dataset_dir, f"{tag}.csv")
  30. if not osp.exists(file_list):
  31. if tag in ("train", "val"):
  32. # train and val file lists must exist
  33. raise DatasetFileNotFoundError(
  34. file_path=file_list,
  35. solution=f"Ensure that both `train.csv` and `val.csv` exist in \
  36. {dataset_dir}",
  37. )
  38. else:
  39. continue
  40. else:
  41. df = pd.read_csv(file_list)
  42. sample_cnts[tag] = len(df)
  43. vis_path = osp.join(vis_save_dir, f"{tag}.csv")
  44. Path(vis_path).parent.mkdir(parents=True, exist_ok=True)
  45. vis_df = df.iloc[:sample_num, :]
  46. vis_df.to_csv(vis_path, index=False)
  47. header_list = df.columns.to_list()
  48. data_list = df.head(10).values.tolist()
  49. tables[tag] = [header_list] + data_list
  50. attrs = {}
  51. attrs["train_samples"] = sample_cnts["train"]
  52. attrs["train_table"] = tables["train"]
  53. attrs["val_samples"] = sample_cnts["val"]
  54. attrs["val_table"] = tables["val"]
  55. return attrs