-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·37 lines (28 loc) · 864 Bytes
/
utils.py
File metadata and controls
executable file
·37 lines (28 loc) · 864 Bytes
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
import json
import scipy
import pickle
def load_lines(path):
with open(path) as fd:
return fd.read().splitlines()
def dump_lines(lines, path):
with open(path, 'w') as fd:
fd.write('\n'.join(lines)+'\n')
def load_json(path):
with open(path) as fd:
return json.load(fd)
def dump_json(obj, path):
with open(path, 'w') as fd:
json.dump(obj, fd, ensure_ascii=False)
def load_pickle(path):
with open(path, 'rb') as fd:
return pickle.load(fd)
def dump_pickle(obj, path):
with open(path, 'wb') as fd:
pickle.dump(obj, fd, protocol=pickle.HIGHEST_PROTOCOL)
def load_sparse(path):
return scipy.sparse.load_npz(path)
def save_sparse(mat, path):
scipy.sparse.save_npz(path, mat)
def encode_labels(s):
labels = sorted(s.unique().tolist())
return {v:k for k,v in enumerate(labels)}