-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.py
46 lines (35 loc) · 1.31 KB
/
data.py
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
import json
import re
import os
from os.path import join
from torch.utils.data import Dataset
class JsonDataset(Dataset):
def __init__(self, split: str, path: str) -> None:
#assert split in ['train', 'val', 'test']
self._data_path = join(path, split)
self._n_data = _count_data(self._data_path)
def __len__(self) -> int:
return self._n_data
def __getitem__(self, i: int):
with open(join(self._data_path, '{}.json'.format(i))) as f:
js = json.loads(f.read())
return js
class JsonDatasetFromIdx(Dataset):
def __init__(self, split: str, path: str, start_idx: int) -> None:
#assert split in ['train', 'val', 'test']
self._data_path = join(path, split)
self._n_data = _count_data(self._data_path) - start_idx
self.start_idx = start_idx
def __len__(self) -> int:
return self._n_data
def __getitem__(self, i: int):
with open(join(self._data_path, '{}.json'.format(i + self.start_idx))) as f:
js = json.loads(f.read())
return js
def _count_data(path):
""" count number of data in the given path"""
matcher = re.compile(r'[0-9]+\.json')
match = lambda name: bool(matcher.match(name))
names = os.listdir(path)
n_data = len(list(filter(match, names)))
return n_data