|
31 | 31 |
|
32 | 32 | from os.path import join as pjoin
|
33 | 33 | import warnings
|
| 34 | +from itertools import product |
34 | 35 | from io import BytesIO
|
35 | 36 |
|
36 | 37 | import numpy as np
|
@@ -194,72 +195,74 @@ def obj_params(self):
|
194 | 195 | offsets = (self.header_class().get_data_offset(),)
|
195 | 196 | else:
|
196 | 197 | offsets = (0, 16)
|
197 |
| - for shape in self.shapes: |
| 198 | + slopes = (1., 2.) if self.has_slope else (1.,) |
| 199 | + inters = (0., 10.) if self.has_inter else (0.,) |
| 200 | + dtypes = (np.uint8, np.int16, np.float32) |
| 201 | + for shape, dtype, offset, slope, inter in product(self.shapes, |
| 202 | + dtypes, |
| 203 | + offsets, |
| 204 | + slopes, |
| 205 | + inters): |
198 | 206 | n_els = np.prod(shape)
|
199 |
| - for dtype in (np.uint8, np.int16, np.float32): |
200 |
| - dt = np.dtype(dtype).newbyteorder(self.data_endian) |
201 |
| - arr = np.arange(n_els, dtype=dt).reshape(shape) |
202 |
| - data = arr.tostring(order=self.array_order) |
203 |
| - for offset in offsets: |
204 |
| - slopes = (1., 2.) if self.has_slope else (1.,) |
205 |
| - inters = (0., 10.) if self.has_inter else (0.,) |
206 |
| - for slope in slopes: |
207 |
| - for inter in inters: |
208 |
| - hdr = self.header_class() |
209 |
| - hdr.set_data_dtype(dtype) |
210 |
| - hdr.set_data_shape(shape) |
211 |
| - if self.settable_offset: |
212 |
| - hdr.set_data_offset(offset) |
213 |
| - if (slope, inter) == (1, 0): # No scaling applied |
214 |
| - # dtype from array |
215 |
| - dtype_out = dtype |
216 |
| - else: # scaling or offset applied |
217 |
| - # out dtype predictable from apply_read_scaling |
218 |
| - # and datatypes of slope, inter |
219 |
| - hdr.set_slope_inter(slope, inter) |
220 |
| - s, i = hdr.get_slope_inter() |
221 |
| - tmp = apply_read_scaling(arr, |
222 |
| - 1. if s is None else s, |
223 |
| - 0. if i is None else i) |
224 |
| - dtype_out = tmp.dtype.type |
225 |
| - |
226 |
| - def sio_func(): |
227 |
| - fio = BytesIO() |
228 |
| - fio.truncate(0) |
229 |
| - fio.seek(offset) |
230 |
| - fio.write(data) |
231 |
| - # Use a copy of the header to avoid changing |
232 |
| - # global header in test functions. |
233 |
| - new_hdr = hdr.copy() |
234 |
| - return (self.proxy_class(fio, new_hdr), |
235 |
| - fio, |
236 |
| - new_hdr) |
237 |
| - params = dict( |
238 |
| - dtype=dtype, |
239 |
| - dtype_out=dtype_out, |
240 |
| - arr=arr.copy(), |
241 |
| - arr_out=arr * slope + inter, |
242 |
| - shape=shape, |
243 |
| - offset=offset, |
244 |
| - slope=slope, |
245 |
| - inter=inter) |
246 |
| - yield sio_func, params |
247 |
| - # Same with filenames |
248 |
| - with InTemporaryDirectory(): |
249 |
| - fname = 'data.bin' |
250 |
| - |
251 |
| - def fname_func(): |
252 |
| - with open(fname, 'wb') as fio: |
253 |
| - fio.seek(offset) |
254 |
| - fio.write(data) |
255 |
| - # Use a copy of the header to avoid changing |
256 |
| - # global header in test functions. |
257 |
| - new_hdr = hdr.copy() |
258 |
| - return (self.proxy_class(fname, new_hdr), |
259 |
| - fname, |
260 |
| - new_hdr) |
261 |
| - params = params.copy() |
262 |
| - yield fname_func, params |
| 207 | + dt = np.dtype(dtype).newbyteorder(self.data_endian) |
| 208 | + arr = np.arange(n_els, dtype=dt).reshape(shape) |
| 209 | + data = arr.tostring(order=self.array_order) |
| 210 | + hdr = self.header_class() |
| 211 | + hdr.set_data_dtype(dtype) |
| 212 | + hdr.set_data_shape(shape) |
| 213 | + if self.settable_offset: |
| 214 | + hdr.set_data_offset(offset) |
| 215 | + if (slope, inter) == (1, 0): # No scaling applied |
| 216 | + # dtype from array |
| 217 | + dtype_out = dtype |
| 218 | + else: # scaling or offset applied |
| 219 | + # out dtype predictable from apply_read_scaling |
| 220 | + # and datatypes of slope, inter |
| 221 | + hdr.set_slope_inter(slope, inter) |
| 222 | + s, i = hdr.get_slope_inter() |
| 223 | + tmp = apply_read_scaling(arr, |
| 224 | + 1. if s is None else s, |
| 225 | + 0. if i is None else i) |
| 226 | + dtype_out = tmp.dtype.type |
| 227 | + |
| 228 | + def sio_func(): |
| 229 | + fio = BytesIO() |
| 230 | + fio.truncate(0) |
| 231 | + fio.seek(offset) |
| 232 | + fio.write(data) |
| 233 | + # Use a copy of the header to avoid changing |
| 234 | + # global header in test functions. |
| 235 | + new_hdr = hdr.copy() |
| 236 | + return (self.proxy_class(fio, new_hdr), |
| 237 | + fio, |
| 238 | + new_hdr) |
| 239 | + |
| 240 | + params = dict( |
| 241 | + dtype=dtype, |
| 242 | + dtype_out=dtype_out, |
| 243 | + arr=arr.copy(), |
| 244 | + arr_out=arr * slope + inter, |
| 245 | + shape=shape, |
| 246 | + offset=offset, |
| 247 | + slope=slope, |
| 248 | + inter=inter) |
| 249 | + yield sio_func, params |
| 250 | + # Same with filenames |
| 251 | + with InTemporaryDirectory(): |
| 252 | + fname = 'data.bin' |
| 253 | + |
| 254 | + def fname_func(): |
| 255 | + with open(fname, 'wb') as fio: |
| 256 | + fio.seek(offset) |
| 257 | + fio.write(data) |
| 258 | + # Use a copy of the header to avoid changing |
| 259 | + # global header in test functions. |
| 260 | + new_hdr = hdr.copy() |
| 261 | + return (self.proxy_class(fname, new_hdr), |
| 262 | + fname, |
| 263 | + new_hdr) |
| 264 | + params = params.copy() |
| 265 | + yield fname_func, params |
263 | 266 |
|
264 | 267 | def validate_slope_inter_offset(self, pmaker, params):
|
265 | 268 | # Check slope, inter, offset
|
|
0 commit comments