Skip to content

Commit

Permalink
add utility function to convert np to ImageCube
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffjennings committed Oct 31, 2023
1 parent 9655277 commit 062e2f1
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/mpol/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from .constants import arcsec, c_ms, cc, deg, kB

from mpol.images import ImageCube

def torch2npy(tensor):
"""Make a copy of a PyTorch tensor on the CPU in numpy format, e.g. for plotting"""
return tensor.detach().cpu().numpy()
Expand Down Expand Up @@ -34,6 +36,42 @@ def center_np_image(image):
return image_wrapped


def np_to_imagecube(image, coords, nchan=1, wrap=False):
"""Convenience function for converting a numpy image into an MPoL ImageCube
tensor (see mpol.images.ImageCube)
Parameters
----------
image : array
An image in numpy format
coords : `mpol.coordinates.GridCoords` object
Instance of the `mpol.coordinates.GridCoords` class
nchan : int, default=1
Number of channels in the image. Default assumes a single 2D image
wrap : bool, default=False
Whether to wrap the numpy image so that index 0 is in the image center
(FFT algorithms typically place index 0 in the image corner)
Returns
-------
icube : `mpol.images.ImageCube` object
The image cube tensor
"""
if wrap:
# move the 0 index to the image center
image = center_np_image(image)

# broadcast image to (nchan, npix, npix)
img_packed_cube = np.broadcast_to(image,
(nchan, coords.npix, coords.npix)).copy()

# convert to pytorch tensor
img_packed_tensor = torch.from_numpy(img_packed_cube)

# insert into ImageCube layer
return ImageCube(coords=coords, nchan=nchan, cube=img_packed_tensor)


def ground_cube_to_packed_cube(ground_cube):
r"""
Converts a Ground Cube to a Packed Visibility Cube for visibility-plane work. See Units and Conventions for more details.
Expand Down

0 comments on commit 062e2f1

Please sign in to comment.