delayed_image.helpers module¶
- delayed_image.helpers.dequantize(quant_data, quantization)[source]¶
Helper for dequantization
- Parameters:
quant_data (ndarray) – data to dequantize
quantization (Dict[str, Any]) – quantization information dictionary to undo. Expected keys are: orig_dtype (str) orig_min (float) orig_max (float) quant_min (float) quant_max (float) nodata (None | int)
- Returns:
dequantized data
- Return type:
ndarray
Example
>>> quant_data = (np.random.rand(4, 4) * 256).astype(np.uint8) >>> quantization = { >>> 'orig_dtype': 'float32', >>> 'orig_min': 0, >>> 'orig_max': 1, >>> 'quant_min': 0, >>> 'quant_max': 255, >>> 'nodata': None, >>> } >>> dequantize(quant_data, quantization)
Example
>>> quant_data = np.ones((4, 4), dtype=np.uint8) >>> quantization = { >>> 'orig_dtype': 'float32', >>> 'orig_min': 0, >>> 'orig_max': 1, >>> 'quant_min': 1, >>> 'quant_max': 1, >>> 'nodata': None, >>> } >>> dequantize(quant_data, quantization)
- delayed_image.helpers.quantize_float01(imdata, old_min=0, old_max=1, quantize_dtype=<class 'numpy.int16'>)[source]¶
Note
Setting old_min / old_max indicates the possible extend of the input data (and it will be clipped to it). It does not mean that the input data has to have those min and max values, but it should be between them.
Example
>>> from delayed_image.helpers import * # NOQA >>> # Test error when input is not nicely between 0 and 1 >>> imdata = (np.random.randn(32, 32, 3) - 1.) * 2.5 >>> quant1, quantization1 = quantize_float01(imdata, old_min=0, old_max=1) >>> recon1 = dequantize(quant1, quantization1) >>> error1 = np.abs((recon1 - imdata)).sum() >>> print('error1 = {!r}'.format(error1)) >>> # >>> for i in range(1, 20): >>> print('i = {!r}'.format(i)) >>> quant2, quantization2 = quantize_float01(imdata, old_min=-i, old_max=i) >>> recon2 = dequantize(quant2, quantization2) >>> error2 = np.abs((recon2 - imdata)).sum() >>> print('error2 = {!r}'.format(error2))
Example
>>> # Test dequantize with uint8 >>> from delayed_image.helpers import dequantize >>> imdata = np.random.randn(32, 32, 3) >>> quant1, quantization1 = quantize_float01(imdata, old_min=0, old_max=1, quantize_dtype=np.uint8) >>> recon1 = dequantize(quant1, quantization1) >>> error1 = np.abs((recon1 - imdata)).sum() >>> print('error1 = {!r}'.format(error1))
Example
>>> # Test quantization with different signed / unsigned combos >>> print(quantize_float01(None, 0, 1, np.int16)) >>> print(quantize_float01(None, 0, 1, np.int8)) >>> print(quantize_float01(None, 0, 1, np.uint8)) >>> print(quantize_float01(None, 0, 1, np.uint16))
- class delayed_image.helpers.mkslice_cls[source]¶
Bases:
objectHelper to make slice syntax easier to construct
Example
>>> from delayed_image.helpers import mkslice_cls >>> m = mkslice_cls() >>> m[0:3] slice(0, 3, None) >>> m[0:3, 0:5] (slice(0, 3, None), slice(0, 5, None)) >>> m()[0:3, 0:5] (slice(0, 3, None), slice(0, 5, None))