-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.py
65 lines (49 loc) · 1.8 KB
/
utils.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""utils.py
Helping code for rest of repo.
"""
import pickle
import os
import shutil
import torch
import json
def get_place_to_index_mapping():
place_to_index_mapping = {}
file1 = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "categories/places.txt"), "r")
lines = [line.rstrip() for line in file1.readlines()]
for idx, place in enumerate(lines):
place_to_index_mapping[place] = idx
file1.close()
return place_to_index_mapping
def get_index_to_place_mapping():
x = get_place_to_index_mapping()
# https://dev.to/renegadecoder94/how-to-invert-a-dictionary-in-python-2150
x = dict(map(reversed, x.items()))
return x
def get_incident_to_index_mapping():
incident_to_index_mapping = {}
file1 = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "categories/incidents.txt"), "r")
lines = [line.rstrip() for line in file1.readlines()]
for idx, incident in enumerate(lines):
incident_to_index_mapping[incident] = idx
file1.close()
return incident_to_index_mapping
def get_index_to_incident_mapping():
x = get_incident_to_index_mapping()
# https://dev.to/renegadecoder94/how-to-invert-a-dictionary-in-python-2150
x = dict(map(reversed, x.items()))
return x
def get_loaded_pickle_file(path):
with open(path, 'rb') as f:
return pickle.load(f)
def save_checkpoint(state,
is_best,
session_name,
filename='checkpoint'):
path = os.path.join(session_name, "{}.pth.tar".format(filename))
best_path = os.path.join(session_name, "{}_best.pth.tar".format(filename))
torch.save(state, path)
if is_best:
shutil.copyfile(path, best_path)
def get_loaded_json_file(path):
with open(path, "r") as fp:
return json.load(fp)