-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
55 lines (46 loc) · 1.67 KB
/
utils.py
File metadata and controls
55 lines (46 loc) · 1.67 KB
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
import argparse
import os
import torch
from suep.generator import CalorimeterDataset
from torch.utils.data import DataLoader
class IsReadableDir(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
prospective_dir = values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError(
'{0} is not a valid path'.format(prospective_dir))
if os.access(prospective_dir, os.R_OK):
setattr(namespace, self.dest, prospective_dir)
else:
raise argparse.ArgumentTypeError(
'{0} is not a readable directory'.format(prospective_dir))
class IsValidFile(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
prospective_file = values
if not os.path.exists(prospective_file):
raise argparse.ArgumentTypeError(
'{0} is not a valid file'.format(prospective_file))
else:
setattr(namespace, self.dest, prospective_file)
def collate_fn(batch):
data = list(zip(*batch))
return torch.stack(data[0], 0), data[1], data[2], data[3]
def get_data_loader(hdf5_source_path,
batch_size,
num_workers,
in_dim,
rank=0,
boosted=False,
shuffle=True):
dataset = CalorimeterDataset(
torch.device(rank),
hdf5_source_path,
in_dim,
boosted=boosted)
return DataLoader(
dataset,
batch_size=batch_size,
collate_fn=collate_fn,
num_workers=num_workers,
shuffle=shuffle
)