def _dtype_from_value(value): if isinstance(value, float): return np.dtype("float32") elif value >= 0: return utils.get_uint_dtype(value) else: return utils.get_int_dtype(value)
def process(data, bins, right): if data is None or "values" not in data: return data values = data["values"] dtype = utils.get_uint_dtype(len(bins) + 2) fillvalue = utils.get_dtype_max(dtype) result_values = np.digitize(values, bins, right).astype(dtype) result_values[values == data["no_data_value"]] = fillvalue return {"values": result_values, "no_data_value": fillvalue}
def dtype(self): # with 254 bin edges, we have 255 bins, and we need 256 possible values # to include no_data return utils.get_uint_dtype(len(self.bins) + 2)
def test_get_uint_dtype(self): self.assertRaises(ValueError, utils.get_uint_dtype, -1) for dtype in ["u1", "u2", "u4", "u8"]: hi = np.iinfo(dtype).max self.assertEqual(utils.get_uint_dtype(hi - 1), dtype)