Skip to content

Commit

Permalink
removed from_image_properties and updated tests and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
iancze committed Dec 27, 2023
1 parent 06f3ba5 commit 1157074
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 330 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## v0.2.1

- *Placeholder* Planned changes described by Architecture GitHub Project.
- Removed convenience classmethods `from_image_properties` from across the code base. From [#233](https://github.com/MPoL-dev/MPoL/issues/233). The recommended workflow is to create a :class:`mpol.coordinates.GridCoords` object and pass that to instantiate these objects as needed. For nearly all but trivially short workflows, this simplifies the number of variables the user needs to keep track and pass around revealing the central role of the :class:`mpol.coordinates.GridCoords` object and its useful attributes for image extent, visibility extent, etc. Most importantly, this significantly reduces the size of the codebase and the burden to maintain and document multiple entry points to key `nn.modules`. We removed `from_image_properties` from
- Removed convenience classmethods `from_image_properties` from across the code base. From [#233](https://github.com/MPoL-dev/MPoL/issues/233). The recommended workflow is to create a :class:`mpol.coordinates.GridCoords` object and pass that to instantiate these objects as needed, rather than passing `cell_size` and `npix` separately. For nearly all but trivially short workflows, this simplifies the number of variables the user needs to keep track and pass around revealing the central role of the :class:`mpol.coordinates.GridCoords` object and its useful attributes for image extent, visibility extent, etc. Most importantly, this significantly reduces the size of the codebase and the burden to maintain, test, and document multiple entry points to key `nn.modules`. We removed `from_image_properties` from
- :class:`mpol.datasets.GriddedDataset`
- :class:`mpol.datasets.Dartboard`
- :class:`mpol.fourier.NuFFT`
Expand Down
4 changes: 3 additions & 1 deletion docs/ci-tutorials/fakedata.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ img_tensor_packed = utils.sky_cube_to_packed_cube(img_tensor)

```{code-cell} ipython3
from mpol.images import ImageCube
image = ImageCube.from_image_properties(cell_size=cell_size, npix=npix, nchan=1, cube=img_tensor_packed)
from mpol import coordinates
coords = coordinates.GridCoords(cell_size=cell_size, npix=npix)
image = ImageCube(coords=coords, nchan=1, cube=img_tensor_packed)
```

If you want to double-check that the image was correctly inserted, you can do
Expand Down
16 changes: 1 addition & 15 deletions docs/ci-tutorials/gridder.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,7 @@ imager = gridding.DirtyImager(
)
```

Instantiating the {class}`~mpol.gridding.DirtyImager` object attaches the {class}`~mpol.coordinates.GridCoords` object and the loose visibilities. There is also a convenience method to create the {class}`~mpol.coordinates.GridCoords` and {class}`~mpol.gridding.DirtyImager` object in one shot by

```{code-cell}
imager = gridding.DirtyImager.from_image_properties(
cell_size=0.005, # [arcsec]
npix=800,
uu=uu,
vv=vv,
weight=weight,
data_re=data_re,
data_im=data_im,
)
```

if you don't want to specify your {class}`~mpol.coordinates.GridCoords` object separately.
Instantiating the {class}`~mpol.gridding.DirtyImager` object attaches the {class}`~mpol.coordinates.GridCoords` object and the loose visibilities.

As we saw, the raw visibility dataset is a set of complex-valued Fourier samples. Our objective is to make images of the sky-brightness distribution and do astrophysics. We'll cover how to do this with MPoL and RML techniques in later tutorials, but it is possible to get a rough idea of the sky brightness by calculating the inverse Fourier transform of the visibility values.

Expand Down
7 changes: 3 additions & 4 deletions docs/large-tutorials/HD143006_part_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,10 @@ The FITS image was a full 3000x3000 pixels. In general, it is good practice to s
Since the DSHARP team has already checked there are no bright sub-mm sources in the FOV, we can save time and just make a smaller image corresponding to the protoplanetary emission. If `cell_size` is 0.003 arcseconds, `npix=512` pixels should be sufficient to make an image approximately 1.5 arcseconds on a side. Now, let's import the relevant MPoL routines and instantiate the Gridder.

```{code-cell}
from mpol import gridding
from mpol import coordinates, gridding
imager = gridding.DirtyImager.from_image_properties(
cell_size=cell_size,
npix=512,
coords = coordinates.GridCoords(cell_size=cell_size, npix=512)
imager = gridding.DirtyImager(
uu=uu,
vv=vv,
weight=weight,
Expand Down
67 changes: 0 additions & 67 deletions src/mpol/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,46 +73,6 @@ def __init__(
self.vis_indexed: torch.Tensor
self.weight_indexed: torch.Tensor

@classmethod
def from_image_properties(
cls,
cell_size: float,
npix: int,
*,
vis_gridded: torch.Tensor,
weight_gridded: torch.Tensor,
mask: torch.Tensor,
nchan: int = 1,
) -> GriddedDataset:
"""
Alternative method to instantiate a GriddedDataset object from cell_size
and npix.
Parameters
----------
cell_size : float
the width of a pixel [arcseconds]
npix : int
the number of pixels per image side
vis_gridded : :class:`torch.Tensor` of :class:`torch.complex128`
the gridded visibility data stored in a "packed" format (pre-shifted for fft)
weight_gridded : :class:`torch.Tensor` of :class:`torch.double`
the weights corresponding to the gridded visibility data,
also in a packed format
mask : :class:`torch.Tensor` of :class:`torch.bool`
a boolean mask to index the non-zero locations of ``vis_gridded`` and
``weight_gridded`` in their packed format.
nchan : int
the number of channels in the image (default = 1).
"""
return cls(
coords=GridCoords(cell_size, npix),
vis_gridded=vis_gridded,
weight_gridded=weight_gridded,
mask=mask,
nchan=nchan,
)

def add_mask(
self,
mask: ArrayLike,
Expand Down Expand Up @@ -247,33 +207,6 @@ def cartesian_phis(self) -> NDArray[floating[Any]]:
def q_max(self) -> float:
return self.coords.q_max

@classmethod
def from_image_properties(
cls,
cell_size: float,
npix: int,
q_edges: NDArray[floating[Any]] | None = None,
phi_edges: NDArray[floating[Any]] | None = None,
) -> Dartboard:
"""Alternative method to instantiate a Dartboard object from cell_size
and npix.
Args:
cell_size (float): the width of a pixel [arcseconds]
npix (int): the number of pixels per image side
q_edges (1D numpy array): an array of radial bin edges to set the
dartboard cells in :math:`[\mathrm{k}\lambda]`. If ``None``, defaults
to 12 log-linearly radial bins stretching from 0 to the
:math:`q_\mathrm{max}` represented by ``coords``.
phi_edges (1D numpy array): an array of azimuthal bin edges to set the
dartboard cells in [radians], over the domain :math:`[0, \pi]`, which
is also implicitly mapped to the domain :math:`[-\pi, \pi]` to preserve
the Hermitian nature of the visibilities. If ``None``, defaults to 8
equal-spaced azimuthal bins stretched from :math:`0` to :math:`\pi`.
"""
coords = GridCoords(cell_size, npix)
return cls(coords, q_edges, phi_edges)

def get_polar_histogram(
self, qs: NDArray[floating[Any]], phis: NDArray[floating[Any]]
) -> NDArray[floating[Any]]:
Expand Down
66 changes: 0 additions & 66 deletions src/mpol/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,6 @@ def __init__(self, coords: GridCoords, persistent_vis: bool = False):
self.register_buffer("vis", None, persistent=persistent_vis)
self.vis: torch.Tensor

@classmethod
def from_image_properties(
cls, cell_size: float, npix: int, persistent_vis: bool = False
) -> FourierCube:
"""
Alternative method for instantiating a FourierCube from ``cell_size`` and
``npix``
Parameters
----------
cell_size : float
the width of an image-plane pixel [arcseconds]
npix : int)
the number of pixels per image side
persistent_vis : bool
should the visibility cube be stored as part of
the modules `state_dict`? If `True`, the state of the UV grid will be
stored. It is recommended to use `False` for most applications, since
the visibility cube will rarely be a direct parameter of the model.
Returns
-------
:class:`mpol.fourier.FourierCube`
"""
coords = GridCoords(cell_size, npix)
return cls(coords, persistent_vis)

def forward(self, cube: torch.Tensor) -> torch.Tensor:
"""
Perform the FFT of the image cube on each channel.
Expand Down Expand Up @@ -314,32 +287,6 @@ def __init__(
im_size=(self.coords.npix, self.coords.npix)
)

@classmethod
def from_image_properties(
cls,
cell_size: float,
npix: int,
nchan: int = 1,
):
"""
Instantiate a :class:`mpol.fourier.NuFFT` object from image properties rather
than a :meth:`mpol.coordinates.GridCoords` instance.
Args:
cell_size (float): the width of an image-plane pixel [arcseconds]
npix (int): the number of pixels per image side
nchan (int): the number of channels in the :class:`mpol.images.ImageCube`.
Default = 1.
Returns:
an instance of the :class:`mpol.fourier.NuFFT`
"""
coords = GridCoords(cell_size, npix)
return cls(
coords,
nchan,
)

def _klambda_to_radpix(self, klambda: torch.Tensor) -> torch.Tensor:
"""Convert a spatial frequency in units of klambda to 'radians/sky pixel,'
using the pixel cell_size provided by ``self.coords.dl``.
Expand Down Expand Up @@ -752,19 +699,6 @@ def __init__(
self.real_interp_mat: torch.Tensor
self.imag_interp_mat: torch.Tensor

@classmethod
def from_image_properties(
cls,
cell_size,
npix,
uu,
vv,
nchan=1,
sparse_matrices=True,
):
coords = GridCoords(cell_size, npix)
return cls(coords, uu, vv, nchan, sparse_matrices)

def forward(self, cube):
r"""
Perform the FFT of the image cube for each channel and interpolate to the
Expand Down
Loading

0 comments on commit 1157074

Please sign in to comment.