From fb84d3ed461ef227b31c643892211e2fe19affda Mon Sep 17 00:00:00 2001 From: Ian Czekala Date: Mon, 20 Feb 2023 20:21:42 -0500 Subject: [PATCH] removed forward from most places --- docs/ci-tutorials/crossvalidation.md | 6 +++--- docs/ci-tutorials/fakedata.md | 2 +- docs/ci-tutorials/gpu_setup.rst | 2 +- docs/ci-tutorials/initializedirtyimage.md | 2 +- docs/ci-tutorials/loose-visibilities.md | 4 ++-- docs/ci-tutorials/optimization.md | 4 ++-- docs/large-tutorials/HD143006_part_2.md | 6 +++--- examples/HD143006/common_functions.py | 4 ++-- examples/HD143006/optimize_to_dirty_image.py | 2 +- src/mpol/fourier.py | 2 +- src/mpol/precomposed.py | 6 +++--- src/mpol/training.py | 4 ++-- test/connectors_test.py | 4 ++-- test/crossval_test.py | 6 +++--- test/fourier_test.py | 14 +++++++------- test/images_test.py | 10 +++++----- test/losses_test.py | 4 ++-- test/train_test_test.py | 6 +++--- 18 files changed, 44 insertions(+), 44 deletions(-) diff --git a/docs/ci-tutorials/crossvalidation.md b/docs/ci-tutorials/crossvalidation.md index c737d6e5..f18236b1 100644 --- a/docs/ci-tutorials/crossvalidation.md +++ b/docs/ci-tutorials/crossvalidation.md @@ -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. @@ -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 @@ -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() ``` diff --git a/docs/ci-tutorials/fakedata.md b/docs/ci-tutorials/fakedata.md index 72d5dea9..0e3a948e 100644 --- a/docs/ci-tutorials/fakedata.md +++ b/docs/ci-tutorials/fakedata.md @@ -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. diff --git a/docs/ci-tutorials/gpu_setup.rst b/docs/ci-tutorials/gpu_setup.rst index da7e0f28..32f333d6 100644 --- a/docs/ci-tutorials/gpu_setup.rst +++ b/docs/ci-tutorials/gpu_setup.rst @@ -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 diff --git a/docs/ci-tutorials/initializedirtyimage.md b/docs/ci-tutorials/initializedirtyimage.md index 197ac31b..1857f815 100644 --- a/docs/ci-tutorials/initializedirtyimage.md +++ b/docs/ci-tutorials/initializedirtyimage.md @@ -135,7 +135,7 @@ for iteration in range(50): optimizer.zero_grad() - rml.forward() + rml() sky_cube = rml.icube.sky_cube diff --git a/docs/ci-tutorials/loose-visibilities.md b/docs/ci-tutorials/loose-visibilities.md index 158ea7de..a39dea62 100644 --- a/docs/ci-tutorials/loose-visibilities.md +++ b/docs/ci-tutorials/loose-visibilities.md @@ -162,7 +162,7 @@ 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)) ``` @@ -170,7 +170,7 @@ 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)) ``` diff --git a/docs/ci-tutorials/optimization.md b/docs/ci-tutorials/optimization.md index b3bbeb89..caf72e1b 100644 --- a/docs/ci-tutorials/optimization.md +++ b/docs/ci-tutorials/optimization.md @@ -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) ``` @@ -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) diff --git a/docs/large-tutorials/HD143006_part_2.md b/docs/large-tutorials/HD143006_part_2.md index 54e302e8..ec1c8df8 100644 --- a/docs/large-tutorials/HD143006_part_2.md +++ b/docs/large-tutorials/HD143006_part_2.md @@ -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 @@ -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 = ( @@ -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 diff --git a/examples/HD143006/common_functions.py b/examples/HD143006/common_functions.py index e26cf19f..52230e9e 100644 --- a/examples/HD143006/common_functions.py +++ b/examples/HD143006/common_functions.py @@ -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 = ( @@ -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() diff --git a/examples/HD143006/optimize_to_dirty_image.py b/examples/HD143006/optimize_to_dirty_image.py index 14da4d8e..5036a8b7 100644 --- a/examples/HD143006/optimize_to_dirty_image.py +++ b/examples/HD143006/optimize_to_dirty_image.py @@ -31,7 +31,7 @@ optimizer.zero_grad() - model.forward() + model() sky_cube = model.icube.sky_cube loss = loss_fn(sky_cube, dirty_image) diff --git a/src/mpol/fourier.py b/src/mpol/fourier.py index 313b643b..caa67526 100644 --- a/src/mpol/fourier.py +++ b/src/mpol/fourier.py @@ -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) diff --git a/src/mpol/precomposed.py b/src/mpol/precomposed.py index 7a636e06..149fae73 100644 --- a/src/mpol/precomposed.py +++ b/src/mpol/precomposed.py @@ -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 diff --git a/src/mpol/training.py b/src/mpol/training.py index 630793c6..9ee190eb 100644 --- a/src/mpol/training.py +++ b/src/mpol/training.py @@ -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 @@ -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) diff --git a/test/connectors_test.py b/test/connectors_test.py index 03deac12..5c5ff707 100644 --- a/test/connectors_test.py +++ b/test/connectors_test.py @@ -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) @@ -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) diff --git a/test/crossval_test.py b/test/crossval_test.py index f2dcd77f..9e8e1986 100644 --- a/test/crossval_test.py +++ b/test/crossval_test.py @@ -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 @@ -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" diff --git a/test/fourier_test.py b/test/fourier_test.py index 798279d0..2d751806 100644 --- a/test/fourier_test.py +++ b/test/fourier_test.py @@ -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( @@ -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() @@ -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)) @@ -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)) @@ -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( @@ -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( @@ -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 diff --git a/test/images_test.py b/test/images_test.py index b0d988e9..a72825c4 100644 --- a/test/images_test.py +++ b/test/images_test.py @@ -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() @@ -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() @@ -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 @@ -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) @@ -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( diff --git a/test/losses_test.py b/test/losses_test.py index a8ccab8d..138e9c6b 100644 --- a/test/losses_test.py +++ b/test/losses_test.py @@ -51,7 +51,7 @@ 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 @@ -59,7 +59,7 @@ 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( diff --git a/test/train_test_test.py b/test/train_test_test.py index 058fe7f8..3aaa2776 100644 --- a/test/train_test_test.py +++ b/test/train_test_test.py @@ -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() @@ -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) @@ -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)