Skip to content

Commit

Permalink
removed forward from most places
Browse files Browse the repository at this point in the history
  • Loading branch information
iancze committed Feb 21, 2023
1 parent 82f3386 commit fb84d3e
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions docs/ci-tutorials/crossvalidation.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ k_fold_datasets = [(train, test) for (train, test) in cv]

```{code-cell}
flayer = fourier.FourierCube(coords=coords)
flayer.forward(torch.zeros(dset.nchan, coords.npix, coords.npix))
flayer(torch.zeros(dset.nchan, coords.npix, coords.npix))
```

The following plots visualize how we've split up the data. For each $K$-fold, we have the "training" visibilities and the "test" visibilities which will be used to evaluate the predictive ability of the model.
Expand Down Expand Up @@ -240,7 +240,7 @@ def train(model, dset, config, optimizer, writer=None):
model.zero_grad()
# get the predicted model
vis = model.forward()
vis = model()
# get the sky cube too
sky_cube = model.icube.sky_cube
Expand Down Expand Up @@ -268,7 +268,7 @@ We also create a separate "test" function to evaluate the trained model against
def test(model, dset):
model.train(False)
# evaluate test score
vis = model.forward()
vis = model()
loss = losses.nll_gridded(vis, dset)
return loss.item()
```
Expand Down
2 changes: 1 addition & 1 deletion docs/ci-tutorials/fakedata.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ image = ImageCube.from_image_properties(cell_size=cell_size, npix=npix, nchan=1,
If you want to double-check that the image was correctly inserted, you can do
```
# double check it went in correctly
plt.imshow(np.squeeze(utils.packed_cube_to_sky_cube(image.forward()).detach().numpy()), origin="lower")
plt.imshow(np.squeeze(utils.packed_cube_to_sky_cube(image()).detach().numpy()), origin="lower")
```
to see that it's upright and not flipped.

Expand Down
2 changes: 1 addition & 1 deletion docs/ci-tutorials/gpu_setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ step our optimizer.
model.zero_grad()
# forward pass
vis = model.forward()
vis = model()
# get skycube from our forward model
sky_cube = model.icube.sky_cube
Expand Down
2 changes: 1 addition & 1 deletion docs/ci-tutorials/initializedirtyimage.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ for iteration in range(50):
optimizer.zero_grad()
rml.forward()
rml()
sky_cube = rml.icube.sky_cube
Expand Down
4 changes: 2 additions & 2 deletions docs/ci-tutorials/loose-visibilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ icube = images.ImageCube(coords=coords, nchan=nchan, cube=img_packed_tensor)
The interesting part of the NuFFT is that it will carry an image plane model all the way to the Fourier plane in loose visibilities, resulting in a model visibility array the same shape as the original visibility data.

```{code-cell}
vis_model_loose = nufft.forward(icube.forward())
vis_model_loose = nufft(icube())
print("Loose model visibilities from the NuFFT have shape {:}".format(vis_model_loose.shape))
print("The original loose data visibilities have shape {:}".format(data.shape))
```

By comparison, the {class}`~mpol.gridding.Gridder` object puts the visibilities onto a grid and exports a {class}`~mpol.datasets.GriddedDataset` object. These gridded data visibilities have the same dimensionality as the gridded model visibilities produced by the {class}`~mpol.fourier.FourierCube` layer

```{code-cell}
vis_model_gridded = flayer.forward(icube.forward())
vis_model_gridded = flayer(icube())
print("Gridded model visibilities from FourierCube have shape {:}".format(vis_model_gridded.shape))
print("Gridded data visibilities have shape {:}".format(gridded_dset.vis_gridded.shape))
```
Expand Down
4 changes: 2 additions & 2 deletions docs/ci-tutorials/optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ rml.zero_grad()
Most modules in MPoL are designed to work in a "feed forward" manner, which means base parameters are processed through the network to predict model visibilites for comparison with data. We can calculate the full visibility cube corresponding to the current pixel values of the {class}`mpol.images.BaseCube`.

```{code-cell} ipython3
vis = rml.forward()
vis = rml()
print(vis)
```

Expand Down Expand Up @@ -251,7 +251,7 @@ for i in range(300):
rml.zero_grad()
# get the predicted model
vis = rml.forward()
vis = rml()
# calculate a loss
loss = losses.nll_gridded(vis, dset)
Expand Down
6 changes: 3 additions & 3 deletions docs/large-tutorials/HD143006_part_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ for iteration in range(500):
optimizer.zero_grad()
model.forward() # get the predicted model
model() # get the predicted model
sky_cube = model.icube.sky_cube
loss = loss_fn(sky_cube, dirty_image) # calculate the loss
Expand Down Expand Up @@ -264,7 +264,7 @@ def train(model, dataset, optimizer, config, writer=None, logevery=50):
model.train()
for iteration in range(config["epochs"]):
optimizer.zero_grad()
vis = model.forward() # calculate the predicted model
vis = model() # calculate the predicted model
sky_cube = model.icube.sky_cube
loss = (
Expand Down Expand Up @@ -347,7 +347,7 @@ Cross validation requires a `test` function (to determine the cross validation s
```{code-cell} ipython3
def test(model, dataset):
model.eval()
vis = model.forward()
vis = model()
loss = losses.nll_gridded(
vis, dataset
) # calculates the loss function that goes to make up the cross validation score
Expand Down
4 changes: 2 additions & 2 deletions examples/HD143006/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def train(
for iteration in range(config["epochs"]):

optimizer.zero_grad()
vis = model.forward()
vis = model()
sky_cube = model.icube.sky_cube

loss = (
Expand All @@ -66,7 +66,7 @@ def test(model, dataset, device):
model = model.to(device)
model.eval()
dataset = dataset.to(device)
vis = model.forward()
vis = model()
loss = losses.nll_gridded(vis, dataset)
return loss.item()

Expand Down
2 changes: 1 addition & 1 deletion examples/HD143006/optimize_to_dirty_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

optimizer.zero_grad()

model.forward()
model()
sky_cube = model.icube.sky_cube

loss = loss_fn(sky_cube, dirty_image)
Expand Down
2 changes: 1 addition & 1 deletion src/mpol/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def make_fake_data(imageCube, uu, vv, weight):
weight = np.atleast_2d(weight)

# carry it forward to the visibilities, which will be (nchan, nvis)
vis_noiseless = nufft.forward(imageCube.forward()).detach().numpy()
vis_noiseless = nufft(imageCube()).detach().numpy()

# generate complex noise
sigma = 1 / np.sqrt(weight)
Expand Down
6 changes: 3 additions & 3 deletions src/mpol/precomposed.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def forward(self):
Returns: 1D complex torch tensor of model visibilities.
"""
x = self.bcube.forward()
x = self.bcube()
x = self.conv_layer(x)
x = self.icube.forward(x)
vis = self.fcube.forward(x)
x = self.icube(x)
vis = self.fcube(x)
return vis
4 changes: 2 additions & 2 deletions src/mpol/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def train(self, model, dataset):

# calculate model visibility cube (corresponding to current pixel
# values of mpol.images.BaseCube)
vis = model.forward()
vis = model()

# get predicted sky cube corresponding to model visibilities
sky_cube = model.icube.sky_cube
Expand Down Expand Up @@ -315,7 +315,7 @@ def test(self, model, dataset):
model.eval()

# calculate model visibility cube
vis = model.forward()
vis = model()

# calculate loss used for a cross-validation score
loss = self.loss_eval(vis, dataset)
Expand Down
4 changes: 2 additions & 2 deletions test/connectors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_index_vis(coords, dataset):
imagecube = images.ImageCube(coords=coords, nchan=nchan, passthrough=True)

# produce model visibilities
vis = flayer.forward(imagecube.forward(basecube.forward()))
vis = flayer(imagecube(basecube()))

# take a basecube, imagecube, and GriddedDataset and predict corresponding visibilities.
datasets.index_vis(vis, dataset)
Expand All @@ -45,7 +45,7 @@ def test_connector_grad(coords, dataset):
imagecube = images.ImageCube(coords=coords, nchan=nchan, passthrough=True)

# produce model visibilities
vis = flayer.forward(imagecube.forward(basecube.forward()))
vis = flayer(imagecube(basecube()))
samples = datasets.index_vis(vis, dataset)

print(samples)
Expand Down
6 changes: 3 additions & 3 deletions test/crossval_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_dartboardsplit_iterate_images(crossvalidation_products, tmp_path):
# visualize dirty images
# create mock fourier layer
flayer = FourierCube(coords=coords)
flayer.forward(torch.zeros(dataset.nchan, coords.npix, coords.npix))
flayer(torch.zeros(dataset.nchan, coords.npix, coords.npix))

# TODO: rewrite with Briggs framework

Expand All @@ -143,8 +143,8 @@ def test_dartboardsplit_iterate_images(crossvalidation_products, tmp_path):
# rtrain = GriddedResidualConnector(flayer, train)
# rtest = GriddedResidualConnector(flayer, test)

# train_chan = packed_cube_to_sky_cube(rtrain.forward())[chan]
# test_chan = packed_cube_to_sky_cube(rtest.forward())[chan]
# train_chan = packed_cube_to_sky_cube(rtrain())[chan]
# test_chan = packed_cube_to_sky_cube(rtest())[chan]

# im = ax[k, 0].imshow(
# train_chan.real.detach().numpy(), interpolation="none", origin="lower"
Expand Down
14 changes: 7 additions & 7 deletions test/fourier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_fourier_cube(coords, tmp_path):
flayer = fourier.FourierCube(coords=coords)
# convert img_packed to pytorch tensor
img_packed_tensor = torch.from_numpy(img_packed[np.newaxis, :, :])
fourier_packed_num = np.squeeze(flayer.forward(img_packed_tensor).numpy())
fourier_packed_num = np.squeeze(flayer(img_packed_tensor).numpy())

# calculate the analytical FFT
fourier_packed_an = utils.fourier_gaussian_klambda_arcsec(
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_fourier_cube_grad(coords):
# calculated the packed FFT using the FourierLayer
flayer = fourier.FourierCube(coords=coords)

output = flayer.forward(img_packed_tensor)
output = flayer(img_packed_tensor)
loss = torch.sum(torch.abs(output))

loss.backward()
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_predict_vis_nufft(coords, mock_visibility_data_cont):
layer = fourier.NuFFT(coords=coords, nchan=nchan, uu=uu, vv=vv)

# predict the values of the cube at the u,v locations
output = layer.forward(imagecube.forward())
output = layer(imagecube())

# make sure we got back the number of visibilities we expected
assert output.shape == (nchan, len(uu))
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_nufft_predict_GPU(coords, mock_visibility_data_cont):
layer = fourier.NuFFT(coords=coords, nchan=nchan, uu=uu, vv=vv).to(device=device)

# predict the values of the cube at the u,v locations
output = layer.forward(imagecube.forward())
output = layer(imagecube())

# make sure we got back the number of visibilities we expected
assert output.shape == (nchan, len(uu))
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_nufft_accuracy_single_chan(coords, mock_visibility_data_cont, tmp_path)
img_packed_tensor = torch.tensor(img_packed[np.newaxis, :, :], requires_grad=True)

# use the NuFFT to predict the values of the cube at the u,v locations
num_output = layer.forward(img_packed_tensor)[0].detach().numpy() # take the channel dim out
num_output = layer(img_packed_tensor)[0].detach().numpy() # take the channel dim out

# calculate the values analytically
an_output = utils.fourier_gaussian_klambda_arcsec(
Expand Down Expand Up @@ -291,7 +291,7 @@ def test_nufft_accuracy_coil_broadcast(coords, mock_visibility_data_cont):
img_packed_tensor = torch.tensor(img_packed[np.newaxis, :, :] * np.ones((nchan, coords.npix, coords.npix)), requires_grad=True)

# use the NuFFT to predict the values of the cube at the u,v locations
num_output = layer.forward(img_packed_tensor).detach().numpy()
num_output = layer(img_packed_tensor).detach().numpy()

# calculate the values analytically, for a single channel
an_output = utils.fourier_gaussian_klambda_arcsec(
Expand Down Expand Up @@ -334,7 +334,7 @@ def test_nufft_accuracy_batch_broadcast(coords, mock_visibility_data, tmp_path):
img_packed_tensor = torch.tensor(img_packed[np.newaxis, :, :] * np.ones((nchan, coords.npix, coords.npix)), requires_grad=True)

# use the NuFFT to predict the values of the cube at the u,v locations
num_output = layer.forward(img_packed_tensor).detach().numpy()
num_output = layer(img_packed_tensor).detach().numpy()

# plot a single channel, to check
ichan = 3
Expand Down
10 changes: 5 additions & 5 deletions test/images_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_single_chan():

def test_basecube_grad():
bcube = images.BaseCube.from_image_properties(npix=800, cell_size=0.015)
loss = torch.sum(bcube.forward())
loss = torch.sum(bcube())
loss.backward()


Expand All @@ -44,7 +44,7 @@ def test_imagecube_grad(coords):
imagecube = images.ImageCube(coords=coords, passthrough=True)

# send things through this layer
loss = torch.sum(imagecube.forward(bcube.forward()))
loss = torch.sum(imagecube(bcube()))

loss.backward()

Expand All @@ -58,7 +58,7 @@ def test_imagecube_tofits(coords, tmp_path):
imagecube = images.ImageCube(coords=coords, passthrough=True)

# sending the basecube through the imagecube
imagecube.forward(bcube.forward())
imagecube(bcube())

# creating output fits file with name 'test_cube_fits_file39.fits'
# file will be deleted after testing
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_basecube_imagecube(coords, tmp_path):
basecube = images.BaseCube(coords=coords, nchan=nchan, base_cube=base_cube)

# the default softplus function should map everything to positive values
output = basecube.forward()
output = basecube()

fig, ax = plt.subplots(ncols=2, nrows=1)

Expand All @@ -110,7 +110,7 @@ def test_basecube_imagecube(coords, tmp_path):
imagecube = images.ImageCube(coords=coords, nchan=nchan, passthrough=True)

# send things through this layer
imagecube.forward(basecube.forward())
imagecube(basecube())

fig, ax = plt.subplots(ncols=1)
im = ax.imshow(
Expand Down
4 changes: 2 additions & 2 deletions test/losses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def loose_visibilities(mock_visibility_data, image_cube):
vv_chan = vv[chan]

nufft = fourier.NuFFT(coords=image_cube.coords, nchan=nchan, uu=uu_chan, vv=vv_chan)
return nufft.forward(image_cube.forward())
return nufft(image_cube())


@pytest.fixture
def gridded_visibilities(image_cube):

# use the FourierCube to produce model visibilities
flayer = fourier.FourierCube(coords=image_cube.coords)
return flayer.forward(image_cube.forward())
return flayer(image_cube())


def test_chi_squared_evaluation(
Expand Down
6 changes: 3 additions & 3 deletions test/train_test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_standalone_init_train(coords, dataset):
nchan = dataset.nchan
rml = precomposed.SimpleNet(coords=coords, nchan=nchan)

vis = rml.forward()
vis = rml()

rml.zero_grad()

Expand All @@ -88,7 +88,7 @@ def test_standalone_train_loop(coords, dataset_cont, tmp_path):
rml.zero_grad()

# get the predicted model
vis = rml.forward()
vis = rml()

# calculate a loss
loss = losses.nll_gridded(vis, dataset_cont)
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_tensorboard(coords, dataset_cont, tmp_path):
rml.zero_grad()

# get the predicted model
vis = rml.forward()
vis = rml()

# calculate a loss
loss = losses.nll_gridded(vis, dataset_cont)
Expand Down

0 comments on commit fb84d3e

Please sign in to comment.