def test_range_normalization_whole_image(self):
        print('Test test_range_normalization_whole_image. [START]')
        data = 10 * np.random.random((32, 4, 64, 56, 48))
        data[:, 0, :, :] = 3 * data[:, 0, :, :]

        data3 = 5 * np.ones((8, 2, 64, 56, 48))
        data4 = np.array([])

        rng1 = (0, 1)
        rng2 = (-2, 2)
        rng3 = (0, 1)

        data_normalized1 = range_normalization(data, rnge=rng1, per_channel=False, eps=0)
        data_normalized2 = range_normalization(data, rnge=rng2, per_channel=False)
        data_normalized3 = range_normalization(data3, rnge=rng3, per_channel=False)
        data_normalized4 = range_normalization(data4, rnge=rng1, per_channel=False)


        print('Test normalization with range [0,1]. [START]')
        for b in range(data.shape[0]):
            self.assertAlmostEqual(data_normalized1[b].min(), rng1[0], delta=1e-4, msg="not scaled to correct min range limit")
            self.assertAlmostEqual(data_normalized1[b].max(), rng1[1], delta=1e-4, msg="not scaled to correct max range limit")
            self.assertEqual(np.unravel_index(np.argmax(data_normalized1[b], axis=None), data_normalized1[b].shape)[0], 0, msg="max not in the right channel")
        print('Test normalization with range [0,1]. [DONE]')

        print('Test normalization with range [-2, 2]. [START]')
        for b in range(data.shape[0]):
            self.assertAlmostEqual(data_normalized2[b].min(), rng2[0], delta=1e-4, msg="not scaled to correct min range limit")
            self.assertAlmostEqual(data_normalized2[b].max(), rng2[1], delta=1e-4, msg="not scaled to correct max range limit")
            self.assertEqual(np.unravel_index(np.argmax(data_normalized2[b], axis=None), data_normalized1[b].shape)[0], 0, msg="max not in the right channel")
        print('Test normalization with range [-2, 2]. [DONE]')

        print('Test normalization of constant data with range [0,1]. [START]')
        for b in range(data3.shape[0]):
            self.assertAlmostEqual(data_normalized3[b].min(), rng3[0], delta=1e-4, msg="not scaled to correct min range limit")
            self.assertAlmostEqual(data_normalized3[b].max(), rng3[0], delta=1e-4, msg="not scaled to correct max range limit")
            # self.assertEqual(np.unravel_index(np.argmax(data_normalized3[b], axis=None), data_normalized1[b].shape)[0], 0, msg="max not in the right channel")
        print('Test normalization of constant data  with range [0,1]. [DONE]')

        print('Test normalization of empty data array with range [0,1]. [START]')
        self.assertEqual(data_normalized4.size, 0, msg="not an empty array")
        print('Test normalization of empty data array with range [0,1]. [DONE]')

        # print('Test RuntimeWarning of constant data with zero eps. [START]')
        # self.assertWarns(RuntimeWarning, range_normalization, data3, rnge = rng3, per_channel = False, eps = 0)
        # print('Test RuntimeWarning of constant data with zero eps. [DONE]')

        print('Test test_range_normalization_whole_image. [DONE]')
    def test_range_normalization_per_channel(self):
        print('Test test_range_normalization_per_channel. [START]')
        data = 10*np.random.random((32, 4, 64, 56, 48))
        data[:,0,:,:] = 30*data[:,0,:,:]

        data3 = 5 * np.ones((8, 2, 64, 56, 48))
        data4 = np.array([])

        rng1 = (0,1)
        rng2 = (-2,2)
        rng3 = (0,1)

        data_normalized = range_normalization(data, rnge=rng1, per_channel=True)
        data_normalized2 = range_normalization(data, rnge=rng2, per_channel=True)
        data_normalized3 = range_normalization(data3, rnge=rng3, per_channel=True)
        data_normalized4 = range_normalization(data4, rnge=rng1, per_channel=True)

        print('Test normalization with range [0,1]. [START]')
        for b in range(data.shape[0]):
            for c in range(data.shape[1]):
                self.assertAlmostEqual(data_normalized[b, c, :, :].max(), rng1[1], msg="not scaled to correct max range limit")
                self.assertAlmostEqual(data_normalized[b, c, :, :].min(), rng1[0], msg="not scaled to correct min range limit")
        print('Test normalization with range [0,1]. [DONE]')

        print('Test normalization with range [-2,2]. [START]')
        for b in range(data.shape[0]):
            for c in range(data.shape[1]):
                self.assertAlmostEqual(data_normalized2[b, c, :, :].max(), rng2[1], msg="not scaled to correct max range limit")
                self.assertAlmostEqual(data_normalized2[b, c, :, :].min(), rng2[0], msg="not scaled to correct min range limit")
        print('Test normalization with range [-2,2]. [DONE]')

        print('Test normalization of constant data with range [0,1]. [START]')
        for b in range(data3.shape[0]):
            for c in range(data3.shape[1]):
                self.assertAlmostEqual(data_normalized3[b, c, :, :].max(), rng3[0], msg="not scaled to correct max range limit")
                self.assertAlmostEqual(data_normalized3[b, c, :, :].min(), rng3[0], msg="not scaled to correct min range limit")
        print('Test normalization of constant data with range [0,1]. [DONE]')

        print('Test normalization of empty data array with range [0,1]. [START]')
        self.assertEqual(data_normalized4.size, 0, msg="not an empty array")
        print('Test normalization of empty data array with range [0,1]. [DONE]')

        # print('Test RuntimeWarning of constant data with zero eps. [START]')
        # self.assertWarns(RuntimeWarning, range_normalization, data3, rnge = rng3, per_channel = True, eps = 0)
        # print('Test RuntimeWarning of constant data with zero eps. [DONE]')

        print('Test test_range_normalization_per_channel. [DONE]')
    def test_range_normalization_per_channel(self):
        data = 10*np.random.random((32, 4, 64, 56, 48))
        data[:,0,:,:] = 30*data[:,0,:,:]

        rng = (0,1)
        d = range_normalization(data, rnge=rng, per_channel=True)

        for b in range(data.shape[0]):
            for c in range(data.shape[1]):
                self.assertAlmostEqual(d[b, c, :, :].max(), rng[1], msg="not scaled to correct max range limit")
                self.assertAlmostEqual(d[b, c, :, :].min(), rng[0], msg="not scaled to correct min range limit")
    def test_range_normalization_whole_image(self):
        data = 10 * np.random.random((32, 4, 64, 56, 48))

        data[:, 0, :, :] = 3 * data[:, 0, :, :]
        data2 = data.copy()

        rng = (0, 1)
        d = range_normalization(data, rnge=rng, per_channel=False, eps=0)
        for b in range(data.shape[0]):
            for c in range(data.shape[1]):
                if c==0:
                    self.assertAlmostEqual(d[b, c, :, :].max(), rng[1], msg="not scaled to correct max range limit")
                else:
                    weight = data2[b,c,:,:].max()/30
                    self.assertAlmostEqual(d[b, c, :, :].max(), weight*rng[1], delta=1e-5, msg="not scaled to correct max range limit")
                self.assertAlmostEqual(d[b, c, :, :].min(), rng[0], delta=1e-4, msg="not scaled to correct min range limit")
 def __call__(self, **data_dict):
     data_dict['data'] = range_normalization(data_dict['data'],
                                             self.rnge,
                                             per_channel=self.per_channel)
     return data_dict
 def __call__(self, **data_dict):
     data_dict[self.data_key] = range_normalization(data_dict[self.data_key], self.rnge,
                                                    per_channel=self.per_channel)
     return data_dict