Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

download.py 2.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2021. Jeffrey Nirschl. All rights reserved.
  3. #
  4. # Licensed under the MIT license. See the LICENSE file in the project
  5. # root directory for license information.
  6. #
  7. # Time-stamp: <>
  8. # ======================================================================
  9. import argparse
  10. import os
  11. from pathlib import Path
  12. from zipfile import ZipFile
  13. from kaggle.api.kaggle_api_extended import KaggleApi
  14. from src.data import load_params
  15. def download_data(train_data, test_data,
  16. competition=None,
  17. output_dir="./data/raw",
  18. credentials=".kaggle/kaggle.json"):
  19. """Download raw dataset from Kaggle"""
  20. credentials = Path.home().joinpath(credentials)
  21. output_dir = Path(output_dir).resolve()
  22. assert (os.path.isfile(credentials)), FileNotFoundError(credentials)
  23. assert (os.path.isdir(output_dir)), NotADirectoryError(output_dir)
  24. # load competition from params
  25. if competition is None:
  26. params = load_params()
  27. competition = params["competition"]
  28. # initialize kaggle api
  29. api = KaggleApi()
  30. api.authenticate()
  31. # downloading from kaggle.com/c/digit-recognizer
  32. valid_files = [str(elem) for elem in api.competition_list_files(competition)]
  33. for elem in [train_data, test_data]:
  34. if elem in valid_files:
  35. api.competition_download_file(competition, elem,
  36. force=True, quiet=False,
  37. path=output_dir)
  38. # unzip files
  39. for elem in [train_data, test_data]:
  40. with ZipFile(output_dir.joinpath(elem+'.zip'), 'r') as zip:
  41. zip.extractall(output_dir)
  42. os.remove(output_dir.joinpath(elem+'.zip'))
  43. if __name__ == '__main__':
  44. parser = argparse.ArgumentParser()
  45. parser.add_argument("-tr", "--train_data", dest="train_data",
  46. required=True, help="Train CSV file")
  47. parser.add_argument("-te", "--test_data", dest="test_data",
  48. required=True, help="Test CSV file")
  49. parser.add_argument("-c", "--competition", dest="competition", default=None,
  50. required=False, help="Kaggle competition to download")
  51. parser.add_argument("-o", "--out-dir", dest="output_dir",
  52. default=os.path.dirname(Path(__file__).resolve()),
  53. required=False, help="output directory")
  54. args = parser.parse_args()
  55. # set vars
  56. args.output_dir = Path(args.output_dir).resolve()
  57. train_path = args.output_dir.joinpath(args.train_data)
  58. test_path = args.output_dir.joinpath(args.test_data)
  59. # download dataset from kaggle
  60. download_data(args.train_data, args.test_data,
  61. competition=args.competition, output_dir=args.output_dir)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...