def write_clipped_array( self, numpy_array: NumpyArray, name: str, a_min: float, a_max: float = None, attrs: Dict[str, str] = None, ): # pylint: disable=R0913 """Write array with min and max values capped. Args: numpy_array: Numpy array name: name to use for the OMX key a_min: minimum value to clip array data a_max: optional maximum value to clip array data attrs: additional attribute key value pairs to write to OMX file """ if a_max is not None: numpy_array = numpy_array.clip(a_min, a_max) else: numpy_array = numpy_array.clip(a_min) self.write_array(numpy_array, name, attrs)
def __init__(self, data: numpy.array, samplerate: float) -> None: if len(data.shape) > 2: raise ValueError( 'data dimensions must be 1 or 2 but got {}'.format( len(data.shape))) if len(data.shape) == 1: data = data.reshape([-1, 1]) _assertSamplerate(samplerate) _assertDuration(data.shape[0]) self._data = data.clip(-1.0, 1.0) self._samplerate = samplerate
def preprocess_minmax(img: np.array) -> np.array: """ Normalize image data to 98-2 percentiles """ im_min = np.percentile(img, 2) im_max = np.percentile(img, 98) im_range = im_max - im_min # print(f'percentile 2 {im_min}, percentile 98 {im_max}, im_range {im_range}') # Normalise to the percentile img = img.astype(np.float32) img = (img - im_min) / im_range img = img.clip(0, 1) return img
def float_2_label(x: np.array, bits: float) -> np.array: assert abs(x).max() <= 1.0 x = (x + 1.) * (2**bits - 1) / 2 return x.clip(0, 2**bits - 1)