def testMovingAverageWithNan(self): # ------------ # single image # ------------ img1 = np.array([[1, np.nan, 3], [np.nan, 4, 5]], dtype=np.float32) img2 = np.array([[2, 3, 4], [np.nan, 5, 6]], dtype=np.float32) movingAvgImageData(img1, img2, 2) ma_gt = np.array([[1.5, np.nan, 3.5], [np.nan, 4.5, 5.5]], dtype=np.float32) np.testing.assert_array_equal(ma_gt, img1) # ------------ # train images # ------------ imgs1 = np.array( [[[1, np.nan, 3], [np.nan, 4, 5]], [[1, 2, 3], [np.nan, 4, 5]]], dtype=np.float32) imgs2 = np.array([[[2, 3, 4], [4, 5, 6]], [[2, 3, 4], [4, 5, 6]]], dtype=np.float32) movingAvgImageData(imgs1, imgs2, 2) ma_gt = np.array([[[1.5, np.nan, 3.5], [np.nan, 4.5, 5.5]], [[1.5, 2.5, 3.5], [np.nan, 4.5, 5.5]]], dtype=np.float32) np.testing.assert_array_equal(ma_gt, imgs1)
def _run_moving_average_image_array(data_type): imgs = np.ones((64, 1024, 512), dtype=data_type) t0 = time.perf_counter() movingAvgImageData(imgs, imgs, 5) dt_cpp = time.perf_counter() - t0 t0 = time.perf_counter() imgs + (imgs - imgs) / 5 dt_py = time.perf_counter() - t0 print(f"\nmoving average with {data_type} - " f"dt (cpp para): {dt_cpp:.4f}, dt (numpy): {dt_py:.4f}")
def _run_moving_average_image_array(data, new_data, data_type): data = data.astype(data_type) new_data = new_data.astype(data_type) data_cpp = data.copy() t0 = time.perf_counter() movingAvgImageData(data_cpp, new_data, 5) dt_cpp = time.perf_counter() - t0 data_py = data.copy() t0 = time.perf_counter() data_py += (new_data - data_py) / 5 dt_py = time.perf_counter() - t0 np.testing.assert_array_equal(data_cpp, data_py) print(f"\nmoving average with {data_type} - " f"dt (cpp para): {dt_cpp:.4f}, dt (numpy): {dt_py:.4f}")
def __set__(self, instance, data): if data is None: self._data = None self._count = 0 return if self._data is not None and self._window > 1 and \ self._count <= self._window and data.shape == self._data.shape: if self._count < self._window: self._count += 1 if data.ndim in (2, 3): movingAvgImageData(self._data, data, self._count) else: self._data += (data - self._data) / self._count else: # self._count == self._window # here is an approximation if data.ndim in (2, 3): movingAvgImageData(self._data, data, self._count) else: self._data += (data - self._data) / self._count else: self._data = data self._count = 1
def testMovingAverage(self): arr1d = np.ones(2, dtype=np.float32) arr2d = np.ones((2, 2), dtype=np.float32) arr3d = np.ones((2, 2, 2), dtype=np.float32) arr4d = np.ones((2, 2, 2, 2), dtype=np.float32) # test invalid input with self.assertRaises(TypeError): movingAvgImageData() with self.assertRaises(TypeError): movingAvgImageData(arr1d, arr1d, 2) with self.assertRaises(TypeError): movingAvgImageData(arr4d, arr4d, 2) # count is 0 with self.assertRaises(ValueError): movingAvgImageData(arr2d, arr2d, 0) with self.assertRaises(ValueError): movingAvgImageData(arr3d, arr3d, 0) # inconsistent shape with self.assertRaises(TypeError): movingAvgImageData(arr2d, arr3d) with self.assertRaises(ValueError): movingAvgImageData(arr2d, np.ones((2, 3), dtype=np.float32), 2) with self.assertRaises(ValueError): movingAvgImageData(arr3d, np.ones((2, 3, 2), dtype=np.float32), 2) # inconsistent dtype with self.assertRaises(TypeError): movingAvgImageData(arr2d, np.ones((2, 2), dtype=np.float64), 2) with self.assertRaises(TypeError): movingAvgImageData(arr3d, np.ones((2, 2, 2), dtype=np.float64), 2) # ------------ # single image # ------------ img1 = np.array([[1, 2, 3], [3, 4, 5]], dtype=np.float32) img2 = np.array([[2, 3, 4], [4, 5, 6]], dtype=np.float32) movingAvgImageData(img1, img2, 2) ma_gt = np.array([[1.5, 2.5, 3.5], [3.5, 4.5, 5.5]], dtype=np.float32) np.testing.assert_array_equal(ma_gt, img1) # ------------ # train images # ------------ imgs1 = np.array([[[1, 2, 3], [3, 4, 5]], [[1, 2, 3], [3, 4, 5]]], dtype=np.float32) imgs2 = np.array([[[2, 3, 4], [4, 5, 6]], [[2, 3, 4], [4, 5, 6]]], dtype=np.float32) movingAvgImageData(imgs1, imgs2, 2) ma_gt = np.array([[[1.5, 2.5, 3.5], [3.5, 4.5, 5.5]], [[1.5, 2.5, 3.5], [3.5, 4.5, 5.5]]], dtype=np.float32) np.testing.assert_array_equal(ma_gt, imgs1)