Repository navigation
Expand file tree
/
Copy pathread_write.py
More file actions
178 lines (138 loc) · 4.9 KB
/
Copy pathread_write.py
File metadata and controls
178 lines (138 loc) · 4.9 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
from functools import singledispatch
from pathlib import Path
import imageio
import numpy as np
import scipy
import tensorflow as tf
from deprecated import deprecated # type: ignore[import]
from multipledispatch import dispatch
def dimString(arr, prefix="", postfix="", keepLength1Dims=False):
return dimStringFromShape(
arr.shape, prefix=prefix, postfix=postfix, keepLength1Dims=keepLength1Dims
)
def dimStringFromShape(shape, prefix="", postfix="", keepLength1Dims=False):
if keepLength1Dims:
shape = [x for x in shape]
else:
shape = [x for x in shape if x != 1]
dimStr = ""
for ii, dim in enumerate(shape[::-1]):
if ii == 0:
dimStr += str(dim)
else:
dimStr += "x" + str(dim)
dimStr = prefix + dimStr + postfix
return dimStr
def convertArrayTypeSafely(arr, dtype, rangeCheck=True):
if arr.dtype != dtype:
finfo = np.finfo(dtype)
assert finfo.min <= np.min(arr), "Value not in the range of {}: {} > {}".format(
dtype, finfo.min, np.min(arr)
)
assert finfo.max >= np.max(arr), "Value not in the range of {}: {} < {}".format(
dtype, finfo.max, np.max(arr)
)
return arr.astype(dtype)
else:
return arr
def getDimsOfPath(path, squeeze=False):
dims = (path.split(".")[-2].split("_")[-1]).split("x")
dims = [int(dim) for dim in dims[::-1] if dim != ""]
if squeeze:
dims = [x for x in dims if x != 1]
return dims
@deprecated(version="", reason="You should use writeArray")
def writeNumpy(array, path, makeParentsIfNotExist=False, keepLength1DimsInSuffix=False):
writeArray(
array,
path,
makeParentsIfNotExist=makeParentsIfNotExist,
keepLength1DimsInSuffix=keepLength1DimsInSuffix,
)
@dispatch(
np.ndarray,
(str, Path),
makeParentsIfNotExist=bool,
keepLength1DimsInSuffix=bool,
dtype=type,
)
def writeArray(
array, path, makeParentsIfNotExist=False, keepLength1DimsInSuffix=False, dtype=None
):
path = str(path)
if path[-4:] != ".raw":
path += dimString(
array, prefix="_", postfix=".raw", keepLength1Dims=keepLength1DimsInSuffix
)
if dtype is not None:
array = convertArrayTypeSafely(array, dtype, rangeCheck=True)
try:
array.tofile(path)
except FileNotFoundError as e:
if makeParentsIfNotExist:
directory = os.path.dirname(os.path.abspath(path))
os.makedirs(directory)
array.tofile(path)
else:
raise e
@dispatch(tf.Tensor, str, makeParentsIfNotExist=bool, keepLength1DimsInSuffix=bool) # type: ignore[no-redef]
def writeArray( # noqa: F811
array, path, makeParentsIfNotExist=False, keepLength1DimsInSuffix=False
):
writeArray(
array.numpy(),
path,
makeParentsIfNotExist=makeParentsIfNotExist,
keepLength1DimsInSuffix=keepLength1DimsInSuffix,
)
def readNumpy(path, dtype=np.float32):
dims = getDimsOfPath(path)
return np.fromfile(path, dtype=dtype, count=-1, sep="").reshape(dims)
def NumpyArrayToRaw(array, path, endIsDim=True):
shape = np.shape(array)
shape = [x for x in shape if x != 1]
if endIsDim:
end = ""
for dim in range(len(shape)):
if dim == 0:
end += "_" + str(shape[-(dim + 1)])
else:
end += "x" + str(shape[-(dim + 1)])
outPath = path + end + ".raw"
else:
outPath = path + ".raw"
array.tofile(outPath)
return outPath
def numpyToImage_noInterpolation(arr, path, upscaleFactor=4):
arr = np.repeat(arr, upscaleFactor, axis=0)
arr = np.repeat(arr, upscaleFactor, axis=1)
imageio.imwrite(path, arr)
def saveAsNpz(arr, path, keepLength1DimsInSuffix=False, dtype=None):
if dtype is not None:
arr = convertArrayTypeSafely(arr, dtype, rangeCheck=True)
# Append shape information to file name
dimDesc = dimString(
arr, prefix="_", postfix="", keepLength1Dims=keepLength1DimsInSuffix
)
np.savez_compressed(path + dimDesc, a=arr)
def loadNpz(path, dtype=np.float32):
try:
arr_sparse = scipy.sparse.load_npz(path)
arr = arr_sparse.toarray()
shape = getDimsOfPath(path)
arr = arr.reshape(shape)
except ValueError:
arr = np.load(path)["a"]
if dtype is not None:
if arr.dtype != dtype:
arr.astype(dtype)
return arr
def readArray(path, dtype=np.float32):
path = str(path)
if path.endswith(".raw"):
return readNumpy(path, dtype=dtype)
elif path.endswith(".npz"):
return loadNpz(path, dtype=dtype)
else:
raise ValueError("Cannot determine format of file:\n{}".format(path))