download.py 1.24 KB
Newer Older
ziqiaomeng's avatar
ziqiaomeng committed
1
2
3
import os
import torch as th
import torch.nn as nn
4
import tqdm
ziqiaomeng's avatar
ziqiaomeng committed
5
6


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class PBar(object):
    def __enter__(self):
        self.t = None
        return self

    def __call__(self, blockno, readsize, totalsize):
        if self.t is None:
            self.t = tqdm.tqdm(total=totalsize)
        self.t.update(readsize)

    def __exit__(self, exc_type, exc_value, traceback):
        self.t.close()


class AminerDataset(object):
ziqiaomeng's avatar
ziqiaomeng committed
22
23
24
25
26
    """
    Download Aminer Dataset from Amazon S3 bucket. 
    """
    def __init__(self, path):

Jinjing Zhou's avatar
Jinjing Zhou committed
27
        self.url = 'https://data.dgl.ai/dataset/aminer.zip'
ziqiaomeng's avatar
ziqiaomeng committed
28

29
        if not os.path.exists(os.path.join(path, 'aminer.txt')):
ziqiaomeng's avatar
ziqiaomeng committed
30
31
            print('File not found. Downloading from', self.url)
            self._download_and_extract(path, 'aminer.zip')
32
        self.fn = os.path.join(path, 'aminer.txt')
ziqiaomeng's avatar
ziqiaomeng committed
33
34
35
36

    def _download_and_extract(self, path, filename):
        import shutil, zipfile, zlib
        from tqdm import tqdm
37
        import urllib.request
ziqiaomeng's avatar
ziqiaomeng committed
38
39

        fn = os.path.join(path, filename)
40
41
        with PBar() as pb:
            urllib.request.urlretrieve(self.url, fn, pb)
ziqiaomeng's avatar
ziqiaomeng committed
42
43
44
45
46
        print('Download finished. Unzipping the file...')

        with zipfile.ZipFile(fn) as zf:
            zf.extractall(path)
        print('Unzip finished.')