forked from daviddaiweizhang/istar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
134 lines (97 loc) · 2.9 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import itertools
from PIL import Image
import pickle
import os
import numpy as np
import pandas as pd
import yaml
Image.MAX_IMAGE_PIXELS = None
def mkdir(path):
dirname = os.path.dirname(path)
if dirname != '':
os.makedirs(dirname, exist_ok=True)
def load_image(filename, verbose=True):
img = Image.open(filename)
img = np.array(img)
if img.ndim == 3 and img.shape[-1] == 4:
img = img[..., :3] # remove alpha channel
if verbose:
print(f'Image loaded from {filename}')
return img
def load_mask(filename, verbose=True):
mask = load_image(filename, verbose=verbose)
mask = mask > 0
if mask.ndim == 3:
mask = mask.any(2)
return mask
def save_image(img, filename):
mkdir(filename)
Image.fromarray(img).save(filename)
print(filename)
def read_lines(filename):
with open(filename, 'r') as file:
lines = [line.rstrip() for line in file]
return lines
def read_string(filename):
return read_lines(filename)[0]
def write_lines(strings, filename):
mkdir(filename)
with open(filename, 'w') as file:
for s in strings:
file.write(f'{s}\n')
print(filename)
def write_string(string, filename):
return write_lines([string], filename)
def save_pickle(x, filename):
mkdir(filename)
with open(filename, 'wb') as file:
pickle.dump(x, file)
print(filename)
def load_pickle(filename, verbose=True):
with open(filename, 'rb') as file:
x = pickle.load(file)
if verbose:
print(f'Pickle loaded from {filename}')
return x
def load_tsv(filename, index=True):
if index:
index_col = 0
else:
index_col = None
df = pd.read_csv(filename, sep='\t', header=0, index_col=index_col)
print(f'Dataframe loaded from {filename}')
return df
def save_tsv(x, filename, **kwargs):
mkdir(filename)
if 'sep' not in kwargs.keys():
kwargs['sep'] = '\t'
x.to_csv(filename, **kwargs)
print(filename)
def load_yaml(filename, verbose=False):
with open(filename, 'r') as file:
content = yaml.safe_load(file)
if verbose:
print(f'YAML loaded from {filename}')
return content
def save_yaml(filename, content):
with open(filename, 'w') as file:
yaml.dump(content, file)
print(file)
def join(x):
return list(itertools.chain.from_iterable(x))
def get_most_frequent(x):
# return the most frequent element in array
uniqs, counts = np.unique(x, return_counts=True)
return uniqs[counts.argmax()]
def sort_labels(labels, descending=True):
labels = labels.copy()
isin = labels >= 0
labels_uniq, labels[isin], counts = np.unique(
labels[isin], return_inverse=True, return_counts=True)
c = counts
if descending:
c = c * (-1)
order = c.argsort()
rank = order.argsort()
labels[isin] = rank[labels[isin]]
return labels, labels_uniq[order]