check_dataset.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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
  15. import os.path as osp
  16. from collections import defaultdict
  17. from pathlib import Path
  18. import pandas as pd
  19. from .....utils.errors import DatasetFileNotFoundError
  20. def check(dataset_dir, output_dir, sample_num=10):
  21. """ check dataset """
  22. dataset_dir = osp.abspath(dataset_dir)
  23. if not osp.exists(dataset_dir) or not osp.isdir(dataset_dir):
  24. raise DatasetFileNotFoundError(file_path=dataset_dir)
  25. sample_cnts = dict()
  26. tables = defaultdict(list)
  27. vis_save_dir = osp.join(output_dir, 'demo_data')
  28. tags = ['train', 'val']
  29. for _, tag in enumerate(tags):
  30. file_list = osp.join(dataset_dir, f'{tag}.csv')
  31. if not osp.exists(file_list):
  32. if tag in ('train', 'val'):
  33. # train and val file lists must exist
  34. raise DatasetFileNotFoundError(
  35. file_path=file_list,
  36. solution=f"Ensure that both `train.csv` and `val.csv` exist in \
  37. {dataset_dir}")
  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