download.py 1.39 KB
Newer Older
ziqiaomeng's avatar
ziqiaomeng committed
1
import os
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
2

ziqiaomeng's avatar
ziqiaomeng committed
3
4
import torch as th
import torch.nn as nn
5
import tqdm
ziqiaomeng's avatar
ziqiaomeng committed
6
7


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
23
    """
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
24
    Download Aminer Dataset from Amazon S3 bucket.
ziqiaomeng's avatar
ziqiaomeng committed
25
26
    """

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
27
28
    def __init__(self, path):
        self.url = "https://data.dgl.ai/dataset/aminer.zip"
ziqiaomeng's avatar
ziqiaomeng committed
29

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

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

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
39
40
        from tqdm import tqdm

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

        with zipfile.ZipFile(fn) as zf:
            zf.extractall(path)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
48
        print("Unzip finished.")
49
50
51
52
53
54


class CustomDataset(object):
    """
    Custom dataset generated by sampler.py (e.g. NetDBIS)
    """
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
55

56
57
    def __init__(self, path):
        self.fn = path