def invert_with_raster_iterator(vis, im, dopsf=False, normalize=True, invert_function=invert_2d_base, facets=1, **kwargs) -> (Image, numpy.ndarray): """ Predict using image partitions, calling specified predict function :param vis: Visibility to be inverted :param im: image template (not changed) :param image_iterator: Iterator to use for partitioning :param dopsf: Make the psf instead of the dirty image :param normalize: Normalize by the sum of weights (True) :return: resulting image[nchan, npol, ny, nx], sum of weights[nchan, npol] """ log.info("invert_with_image_iterator: Inverting by image partitions") i = 0 nchan, npol, _, _ = im.shape totalwt = numpy.zeros([nchan, npol]) for dpatch in image_raster_iter(im, facets=facets): result, sumwt = invert_function(vis, dpatch, dopsf, normalize=False, **kwargs) totalwt = sumwt # Ensure that we fill in the elements of dpatch instead of creating a new numpy arrray dpatch.data[...] = result.data[...] assert numpy.max(numpy.abs(dpatch.data)), "Partition image %d appears to be empty" % i i += 1 assert numpy.max(numpy.abs(im.data)), "Output image appears to be empty" if normalize: im = normalize_sumwt(im, totalwt) return im, totalwt
def image_gather_facets(image_list: List[Image], im: Image, facets=1) -> Image: """Gather a list of subimages back into an image using the image_raster_iterator :param image_list: List of subimages :param im: Output image :param facets: Number of image partitions on each axis (2) :return: list of subimages """ for i, facet in enumerate(image_raster_iter(im, facets=facets)): facet.data[...] = image_list[i].data[...] return im
def image_scatter_facets(im: Image, facets=1) -> List[Image]: """Scatter an image into a list of subimages using the image_raster_iterator :param im: Image :param facets: Number of image partitions on each axis (2) :return: list of subimages """ image_list = list() for facet in image_raster_iter(im, facets=facets): image_list.append(facet) return image_list
def predict_with_raster_iterator(vis: Visibility, model: Image, predict_function=predict_2d_base, facets=1, **kwargs) -> Visibility: """ Predict using image partitions, calling specified predict function :param vis: Visibility to be predicted :param model: model image :param image_iterator: Image iterator used to access the image :param predict_function: Function to be used for prediction (allows nesting) :return: resulting visibility (in place works) """ log.info("predict_with_image_iterator: Predicting by image partitions") result = copy_visibility(vis) for dpatch in image_raster_iter(model, facets=facets): result.data['vis'][...] = 0.0 result = predict_function(result, dpatch, **kwargs) vis.data['vis'] += result.data['vis'] return vis
def test_rasterise(self): m31original = create_test_image(polarisation_frame=PolarisationFrame('stokesI')) assert numpy.max(numpy.abs(m31original.data)), "Original is empty" for nraster in [2, 4, 8]: m31model = create_test_image(polarisation_frame=PolarisationFrame('stokesI')) for patch in image_raster_iter(m31model, facets=nraster): assert patch.data.shape[3] == (m31model.data.shape[3] // nraster), \ "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[3], (m31model.data.shape[3] // nraster)) assert patch.data.shape[2] == (m31model.data.shape[2] // nraster), \ "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[2], (m31model.data.shape[2] // nraster)) patch.data *= 2.0 diff = m31model.data - 2.0 * m31original.data assert numpy.max(numpy.abs(m31model.data)), "Raster is empty for %d" % nraster assert numpy.max(numpy.abs(diff)) == 0.0, "Raster set failed for %d" % nraster