Completed
Push — master ( 827ac1...0f86da )
by Tinghui
01:06
created

main()   B

Complexity

Conditions 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
1
"""CASAS download script to get CASAS smart home datasets
2
"""
3
4
from __future__ import absolute_import
5
import os
6
import logging
7
import zipfile
8
import argparse
9
from fuel.downloaders.base import default_downloader
10
11
logger = logging.getLogger(__file__)
12
13
master_url = 'http://eecs.wsu.edu/~twang3/datasets/'
14
dataset_dict = {
15
    'B1': 'B1.zip',
16
    'B2': 'B2.zip',
17
    'B3': 'B3.zip',
18
    'bosch_legacy': 'bosch_legacy.zip'
19
}
20
21
22
def CASAS_download(directory, datasets):
23
    """Download CASAS datasets to directory
24
25
    Args:
26
        directory (:obj:`str`): path to directory to store the downloaded
27
        datasets (:obj:`tuple` of :obj:`str`): list of datasets to download
28
    """
29
    for dataset in datasets:
30
        filename = dataset_dict.get(dataset, None)
31
        if filename is None:
32
            print('Cannot find dataset %s' % dataset)
33
            print('Here are the available datasets:')
34
            for key in dataset_dict.keys():
35
                print('  * %s' % key)
36
        else:
37
            # Download zipped files
38
            default_downloader(directory=directory,
39
                               urls=[master_url + filename],
40
                               filenames=[filename],
41
                               clear=False)
42
            # Expand it in place
43
            file_path = os.path.join(directory, filename)
44
            if os.path.exists(file_path):
45
                zip_ref = zipfile.ZipFile(file_path, 'r')
46
                zip_ref.extractall(directory)
47
                zip_ref.close()
48
49
50
def main():
51
    """
52
    usage: casas_download.py [-h] [-d DIR] datasets [datasets ...]
53
54
    Download CASAS datasets to a directory.
55
56
    positional arguments:
57
      datasets           Dataset Names
58
59
    optional arguments:
60
      -h, --help         show this help message and exit
61
      -d DIR, --dir DIR  Directory to store datasets
62
    """
63
    parser = argparse.ArgumentParser(description='Download CASAS datasets to a directory.')
64
    parser.add_argument('-d', '--dir', help='Directory to store datasets')
65
    parser.add_argument('datasets', nargs='+', type=str, help='Dataset Names')
66
    args = parser.parse_args()
67
    dir_abs_path = os.path.abspath(os.path.expanduser(args.dir))
68
    if not os.path.isdir(dir_abs_path):
69
        user_input = input('Directory %s does not exist. Do you want to create it? [Y/n] ' %
70
                           dir_abs_path)
71
        if str.capitalize(user_input) == 'N':
72
            exit()
73
    CASAS_download(args.dir, tuple(args.datasets))
74
75
if __name__ == '__main__':
76
    main()
77