def test_reshape(aia171_test_map, shape): imagebytwo = reshape_image_to_4d_superpixel(aia171_test_map.data, (2, 2)) assert imagebytwo.shape == (shape[0] / 2, 2, shape[1] / 2, 2) with pytest.raises(ValueError) as error_msg: reshape_image_to_4d_superpixel(aia171_test_map.data, (3, 3)) assert 'New dimensions must divide original image size exactly.' in str( error_msg.value)
def test_reshape(aia171_test_map, shape): for factor in np.arange(2, 10): if np.all((shape % factor) == 0): # it is a factor therefore should work reshape_image_to_4d_superpixel(aia171_test_map.data, shape/factor) else: # it is not a factor so function should raise an error with pytest.raises(ValueError): reshape_image_to_4d_superpixel(aia171_test_map.data, shape/factor)
def test_reshape(aia171_test_map, shape): for factor in np.arange(2, 10): if np.all((shape % factor) == 0): # it is a factor therefore should work reshape_image_to_4d_superpixel(aia171_test_map.data, shape / factor) else: # it is not a factor so function should raise an error with pytest.raises(ValueError): reshape_image_to_4d_superpixel(aia171_test_map.data, shape / factor)
def superpixel(self, dimensions, method="sum"): """Returns a new map consisting of superpixels formed from the original data. Useful for increasing signal to noise ratio in images. Parameters ---------- dimensions : tuple One superpixel in the new map is equal to (dimension[0], dimension[1]) pixels of the original map Note: the first argument corresponds to the 'x' axis and the second argument corresponds to the 'y' axis. method : {'sum' | 'average'} What each superpixel represents compared to the original data * sum - add up the original data * average - average the sum over the number of original pixels Returns ------- out : Map A new Map which has superpixels of the required size. References ---------- | http://mail.scipy.org/pipermail/numpy-discussion/2010-July/051760.html """ # Note: because the underlying ndarray is transposed in sense when # compared to the Map, the ndarray is transposed, resampled, then # transposed back # Note: "center" defaults to True in this function because data # coordinates in a Map are at pixel centers # Make a copy of the original data and perform reshaping reshaped = reshape_image_to_4d_superpixel(self.data.copy().T, dimensions) if method == "sum": new_data = reshaped.sum(axis=3).sum(axis=1) elif method == "average": new_data = (reshaped.sum(axis=3).sum(axis=1)) / np.float32(dimensions[0] * dimensions[1]) new_data = new_data.T # Update image scale and number of pixels new_map = deepcopy(self) new_meta = new_map.meta # Note that 'x' and 'y' correspond to 1 and 0 in self.shape, # respectively new_nx = self.shape[1] / dimensions[0] new_ny = self.shape[0] / dimensions[1] # Update metadata new_meta["cdelt1"] = dimensions[0] * self.scale["x"] new_meta["cdelt2"] = dimensions[1] * self.scale["y"] new_meta["crpix1"] = (new_nx + 1) / 2.0 new_meta["crpix2"] = (new_ny + 1) / 2.0 new_meta["crval1"] = self.center["x"] new_meta["crval2"] = self.center["y"] # Create new map instance new_map.data = new_data return new_map
def test_reshape(aia171_test_map, shape): def _n(a, b, c): return int(np.floor((a-b)/c)) # Dimension divides the array shape exactly with no remainder im = reshape_image_to_4d_superpixel(aia171_test_map.data, (2, 2), (0, 0)) assert im.shape == (shape[0]/2, 2, shape[1]/2, 2) # Dimension divides the array shape exactly with remainder im = reshape_image_to_4d_superpixel(aia171_test_map.data, (7, 5), (0, 0)) assert im.shape == (np.int(shape[0]/7), 7, np.int(shape[1]/5), 5) # Dimension divides the array shape exactly with no remainder, and there is # an offset im = reshape_image_to_4d_superpixel(aia171_test_map.data, (2, 2), (1, 1)) assert im.shape == (np.int(shape[0]/2) - 1, 2, np.int(shape[1]/2) - 1, 2) # Dimension divides the array shape exactly with remainder, and there is # an offset d = (9, 7) o = (1, 4) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1]) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1]) d = (9, 7) o = (5, 4) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1]) d = (9, 7) o = (4, 4) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1])
def test_reshape(aia171_test_map, shape): def _n(a, b, c): return int(np.floor((a - b) / c)) # Dimension divides the array shape exactly with no remainder im = reshape_image_to_4d_superpixel(aia171_test_map.data, (2, 2), (0, 0)) assert im.shape == (shape[0] / 2, 2, shape[1] / 2, 2) # Dimension divides the array shape exactly with remainder im = reshape_image_to_4d_superpixel(aia171_test_map.data, (7, 5), (0, 0)) assert im.shape == (np.int(shape[0] / 7), 7, np.int(shape[1] / 5), 5) # Dimension divides the array shape exactly with no remainder, and there is # an offset im = reshape_image_to_4d_superpixel(aia171_test_map.data, (2, 2), (1, 1)) assert im.shape == (np.int(shape[0] / 2) - 1, 2, np.int(shape[1] / 2) - 1, 2) # Dimension divides the array shape exactly with remainder, and there is # an offset d = (9, 7) o = (1, 4) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1]) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1]) d = (9, 7) o = (5, 4) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1]) d = (9, 7) o = (4, 4) im = reshape_image_to_4d_superpixel(aia171_test_map.data, d, o) assert im.shape == (_n(shape[0], o[0], d[0]), d[0], _n(shape[1], o[1], d[1]), d[1])
def superpixel(self, dimensions, method='sum'): """Returns a new map consisting of superpixels formed from the original data. Useful for increasing signal to noise ratio in images. Parameters ---------- dimensions : tuple One superpixel in the new map is equal to (dimension[0], dimension[1]) pixels of the original map Note: the first argument corresponds to the 'x' axis and the second argument corresponds to the 'y' axis. method : {'sum' | 'average'} What each superpixel represents compared to the original data * sum - add up the original data * average - average the sum over the number of original pixels Returns ------- out : Map A new Map which has superpixels of the required size. References ---------- | http://mail.scipy.org/pipermail/numpy-discussion/2010-July/051760.html """ # Note: because the underlying ndarray is transposed in sense when # compared to the Map, the ndarray is transposed, resampled, then # transposed back # Note: "center" defaults to True in this function because data # coordinates in a Map are at pixel centers # Make a copy of the original data and perform reshaping reshaped = reshape_image_to_4d_superpixel(self.data.copy().T, dimensions) if method == 'sum': new_data = reshaped.sum(axis=3).sum(axis=1) elif method == 'average': new_data = ((reshaped.sum(axis=3).sum(axis=1)) / np.float32(dimensions[0] * dimensions[1])) new_data = new_data.T # Update image scale and number of pixels new_map = deepcopy(self) new_meta = new_map.meta # Note that 'x' and 'y' correspond to 1 and 0 in self.shape, # respectively new_nx = self.shape[1] / dimensions[0] new_ny = self.shape[0] / dimensions[1] # Update metadata new_meta['cdelt1'] = dimensions[0] * self.scale['x'] new_meta['cdelt2'] = dimensions[1] * self.scale['y'] new_meta['crpix1'] = (new_nx + 1) / 2. new_meta['crpix2'] = (new_ny + 1) / 2. new_meta['crval1'] = self.center['x'] new_meta['crval2'] = self.center['y'] # Create new map instance new_map.data = new_data return new_map
def superpixel(self, dimensions, method='sum'): """Returns a new map consisting of superpixels formed from the original data. Useful for increasing signal to noise ratio in images. Parameters ---------- dimensions : tuple One superpixel in the new map is equal to (dimension[0], dimension[1]) pixels of the original map Note: the first argument corresponds to the 'x' axis and the second argument corresponds to the 'y' axis. method : {'sum' | 'average'} What each superpixel represents compared to the original data * sum - add up the original data * average - average the sum over the number of original pixels Returns ------- out : Map A new Map which has superpixels of the required size. References ---------- | http://mail.scipy.org/pipermail/numpy-discussion/2010-July/051760.html """ # Note: because the underlying ndarray is transposed in sense when # compared to the Map, the ndarray is transposed, resampled, then # transposed back # Note: "center" defaults to True in this function because data # coordinates in a Map are at pixel centers # Make a copy of the original data and perform reshaping reshaped = reshape_image_to_4d_superpixel(self.data.copy(), [dimensions.value[1], dimensions.value[0]]) if method == 'sum': new_data = reshaped.sum(axis=3).sum(axis=1) elif method == 'average': new_data = ((reshaped.sum(axis=3).sum(axis=1)) / np.float32(dimensions[0] * dimensions[1])) # Update image scale and number of pixels new_map = deepcopy(self) new_meta = new_map.meta new_nx = (self.dimensions[0] / dimensions[0]).value new_ny = (self.dimensions[1] / dimensions[1]).value # Update metadata new_meta['cdelt1'] = (dimensions[0] * self.scale.x).value new_meta['cdelt2'] = (dimensions[1] * self.scale.y).value if 'CD1_1' in new_meta: new_meta['CD1_1'] *= dimensions[0].value new_meta['CD2_1'] *= dimensions[0].value new_meta['CD1_2'] *= dimensions[1].value new_meta['CD2_2'] *= dimensions[1].value new_meta['crpix1'] = (new_nx + 1) / 2. new_meta['crpix2'] = (new_ny + 1) / 2. new_meta['crval1'] = self.center.x.value new_meta['crval2'] = self.center.y.value # Create new map instance new_map.data = new_data return new_map
def test_reshape(): assert reshape_image_to_4d_superpixel(AIA_MAP.data, (512, 512)) != None assert reshape_image_to_4d_superpixel(AIA_MAP.data, (600, 512)) == None assert reshape_image_to_4d_superpixel(AIA_MAP.data, (512, 600)) == None
def superpixel(self, dimensions, method='sum'): """Returns a new map consisting of superpixels formed from the original data. Useful for increasing signal to noise ratio in images. Parameters ---------- dimensions : tuple One superpixel in the new map is equal to (dimension[0], dimension[1]) pixels of the original map Note: the first argument corresponds to the 'x' axis and the second argument corresponds to the 'y' axis. method : {'sum' | 'average'} What each superpixel represents compared to the original data * sum - add up the original data * average - average the sum over the number of original pixels Returns ------- out : Map A new Map which has superpixels of the required size. References ---------- | http://mail.scipy.org/pipermail/numpy-discussion/2010-July/051760.html """ from sunpy.image.rescale import reshape_image_to_4d_superpixel # Note: because the underlying ndarray is transposed in sense when # compared to the Map, the ndarray is transposed, resampled, then # transposed back # Note: "center" defaults to True in this function because data # coordinates in a Map are at pixel centers # Make a copy of the original data and perform reshaping reshaped = reshape_image_to_4d_superpixel(np.asarray(self).copy().T, dimensions) if method == 'sum': data = reshaped.sum(axis=3).sum(axis=1) elif method == 'average': data = ((reshaped.sum(axis=3).sum(axis=1)) / np.float32(dimensions[0] * dimensions[1])) #data = resample(np.asarray(self).copy().T, dimensions, # method, center=True) # Update image scale and number of pixels header = self._original_header.copy() # Note that 'x' and 'y' correspond to 1 and 0 in self.shape, # respectively new_nx = self.shape[1] / dimensions[0] new_ny = self.shape[0] / dimensions[1] # Create new map instance new_map = self.__class__(data.T, header) # Update metadata new_map.scale['x'] = dimensions[0] * self.scale['x'] new_map.scale['y'] = dimensions[1] * self.scale['y'] new_map.reference_pixel['x'] = (new_nx + 1) / 2. new_map.reference_pixel['y'] = (new_ny + 1) / 2. new_map.reference_coordinate['x'] = self.center['x'] new_map.reference_coordinate['y'] = self.center['y'] return new_map
def test_reshape(): assert reshape_image_to_4d_superpixel(AIA_MAP.data, (512, 512)) != None assert reshape_image_to_4d_superpixel(AIA_MAP.data, (600, 512)) == None assert reshape_image_to_4d_superpixel(AIA_MAP.data, (512, 600)) == None
def test_reshape(aia171_test_map, shape): imagebytwo = reshape_image_to_4d_superpixel(aia171_test_map.data, (2, 2)) assert imagebytwo.shape == (shape[0]/2, 2, shape[1]/2, 2) with pytest.raises(ValueError) as error_msg: reshape_image_to_4d_superpixel(aia171_test_map.data, (3, 3)) assert 'New dimensions must divide original image size exactly.' in str(error_msg.value)