Skip to content

Commit f8ac9ee

Browse files
authored
Merge pull request #33 from AlecThomson/docs
Clean up the docs
2 parents 0b0cf9e + e201be3 commit f8ac9ee

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

tests/test_2d.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import subprocess as sp
55
import unittest
6+
from typing import List, Tuple, Union
67

78
import astropy.units as u
89
import numpy as np
@@ -13,7 +14,15 @@
1314
from racs_tools import beamcon_2D
1415

1516

16-
def make_2d_image(beam):
17+
def make_2d_image(beam: Beam) -> str:
18+
"""Make a fake 2D image from with a Gaussian beam.
19+
20+
Args:
21+
beam (Beam): Gaussian beam.
22+
23+
Returns:
24+
str: Name of the output FITS file.
25+
"""
1726
pix_scale = 2.5 * u.arcsec
1827

1928
data = beam.as_kernel(pixscale=pix_scale, x_size=100, y_size=100).array
@@ -44,7 +53,16 @@ def make_2d_image(beam):
4453
return outf
4554

4655

47-
def mirsmooth(outf, target_beam):
56+
def mirsmooth(outf: str, target_beam: Beam) -> Tuple[str, str, str]:
57+
"""Smooth a FITS image to a target beam using MIRIAD.
58+
59+
Args:
60+
outf (str): FITS image to smooth.
61+
target_beam (Beam): Target beam.
62+
63+
Returns:
64+
Tuple[str, str, str]: Names of the output images.
65+
"""
4866
outim = outf.replace(".fits", ".im")
4967
cmd = f"fits op=xyin in={outf} out={outim}"
5068
sp.run(cmd.split())
@@ -60,20 +78,37 @@ def mirsmooth(outf, target_beam):
6078
return outim, smoothim, smoothfits
6179

6280

63-
def check_images(fname_1, fname_2):
81+
def check_images(fname_1: str, fname_2: str) -> bool:
82+
"""Compare two FITS images.
83+
84+
Args:
85+
fname_1 (str): Image 1.
86+
fname_2 (str): Image 2.
87+
88+
Returns:
89+
bool: True if the images are the same.
90+
"""
6491
data_1 = fits.getdata(fname_1)
6592
data_2 = fits.getdata(fname_2)
6693

6794
return np.allclose(data_1, data_2, atol=1e-5)
6895

6996

70-
def cleanup(files):
97+
def cleanup(files: List[str]):
98+
"""Remove files.
99+
100+
Args:
101+
files (List[str]): List of files to remove.
102+
"""
71103
for f in files:
72104
sp.run(f"rm -rfv {f}".split())
73105

74106

75107
class test_Beamcon2D(unittest.TestCase):
108+
"""Test the 2D beam convolution."""
109+
76110
def setUp(self) -> None:
111+
"""Set up the test."""
77112
self.orginal_beam = Beam(20 * u.arcsec, 10 * u.arcsec, 10 * u.deg)
78113
test_image = make_2d_image(self.orginal_beam)
79114

@@ -90,6 +125,7 @@ def setUp(self) -> None:
90125
]
91126

92127
def test_robust(self):
128+
"""Test the robust convolution."""
93129
with schwimmbad.SerialPool() as pool:
94130
beamcon_2D.main(
95131
pool=pool,
@@ -103,9 +139,12 @@ def test_robust(self):
103139

104140
fname_beamcon = self.test_image.replace(".fits", ".robust.fits")
105141
self.files.append(fname_beamcon)
106-
assert check_images(self.test_mir, fname_beamcon), "Beamcon does not match miriad"
142+
assert check_images(
143+
self.test_mir, fname_beamcon
144+
), "Beamcon does not match miriad"
107145

108146
def test_astropy(self):
147+
"""Test the astropy convolution."""
109148
print(f"{self.test_image=}")
110149
with schwimmbad.SerialPool() as pool:
111150
beamcon_2D.main(
@@ -120,9 +159,12 @@ def test_astropy(self):
120159

121160
fname_beamcon = self.test_image.replace(".fits", ".astropy.fits")
122161
self.files.append(fname_beamcon)
123-
assert check_images(self.test_mir, fname_beamcon), "Beamcon does not match miriad"
162+
assert check_images(
163+
self.test_mir, fname_beamcon
164+
), "Beamcon does not match miriad"
124165

125166
def test_scipy(self):
167+
"""Test the scipy convolution."""
126168
with schwimmbad.SerialPool() as pool:
127169
beamcon_2D.main(
128170
pool=pool,
@@ -136,9 +178,12 @@ def test_scipy(self):
136178

137179
fname_beamcon = self.test_image.replace(".fits", ".scipy.fits")
138180
self.files.append(fname_beamcon)
139-
assert check_images(self.test_mir, fname_beamcon), "Beamcon does not match miriad"
181+
assert check_images(
182+
self.test_mir, fname_beamcon
183+
), "Beamcon does not match miriad"
140184

141185
def tearDown(self) -> None:
186+
"""Clean up."""
142187
cleanup(self.files)
143188

144189

tests/test_3d.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
import subprocess as sp
5-
import unittest
64
import os
75
import shutil
6+
import subprocess as sp
7+
import unittest
88

99
import astropy.units as u
1010
import numpy as np
1111
import schwimmbad
1212
from astropy.io import fits
1313
from astropy.table import Table
1414
from radio_beam import Beam, Beams
15+
from test_2d import check_images, cleanup, mirsmooth
1516

1617
from racs_tools import beamcon_3D
17-
from test_2d import mirsmooth, check_images, cleanup
1818

19-
def smoothcube(outf: str, target_beam: Beam):
2019

20+
def smoothcube(outf: str, target_beam: Beam) -> str:
21+
"""Smooth a FITS cube to a target beam.
22+
23+
Args:
24+
outf (str): FITS cube to smooth.
25+
target_beam (Beam): Target beam.
26+
27+
Returns:
28+
str: Output FITS cube.
29+
"""
2130
cube = np.squeeze(fits.getdata(outf))
2231
header = fits.getheader(outf)
2332
with fits.open(outf) as hdulist:
@@ -36,7 +45,6 @@ def smoothcube(outf: str, target_beam: Beam):
3645
shutil.rmtree(outim)
3746
shutil.rmtree(smoothim)
3847

39-
4048
smoothcube = np.array(smoothcube)
4149
cube_hdu = fits.PrimaryHDU(data=smoothcube, header=header)
4250
cube_hdu.header = target_beam.attach_to_header(cube_hdu.header)
@@ -47,7 +55,15 @@ def smoothcube(outf: str, target_beam: Beam):
4755
return smooth_outf
4856

4957

50-
def make_3d_image(beams: Beams):
58+
def make_3d_image(beams: Beams) -> str:
59+
"""Make a fake 3D image from with a Gaussian beam.
60+
61+
Args:
62+
beams (Beams): Gaussian beams.
63+
64+
Returns:
65+
str: FITS cube filename.
66+
"""
5167
pix_scale = 2.5 * u.arcsec
5268

5369
freqs = np.linspace(1, 2, len(beams)) * u.GHz
@@ -60,7 +76,6 @@ def make_3d_image(beams: Beams):
6076

6177
cube = np.array(cube)[np.newaxis]
6278

63-
6479
hdu = fits.PrimaryHDU(data=cube)
6580
hdu.header["BUNIT"] = "Jy/beam"
6681
hdu.header["CDELT1"] = -pix_scale.to(u.deg).value
@@ -117,8 +132,12 @@ def make_3d_image(beams: Beams):
117132

118133
return outf
119134

120-
class test_Beamcon2D(unittest.TestCase):
135+
136+
class test_Beamcon3D(unittest.TestCase):
137+
"""Test the beamcon_3D script."""
138+
121139
def setUp(self) -> None:
140+
"""Set up the test."""
122141
self.orginal_beams = Beams(
123142
major=np.linspace(50, 10, 10) * u.arcsec,
124143
minor=np.linspace(10, 10, 10) * u.arcsec,
@@ -137,6 +156,7 @@ def setUp(self) -> None:
137156
]
138157

139158
def test_robust(self):
159+
"""Test the robust mode."""
140160
beamcon_3D.main(
141161
infile=[self.test_image],
142162
suffix="robust",
@@ -152,6 +172,7 @@ def test_robust(self):
152172
check_images(self.test_cube, fname_beamcon), "Beamcon does not match Miriad"
153173

154174
def test_astropy(self):
175+
"""Test the astropy mode."""
155176
beamcon_3D.main(
156177
infile=[self.test_image],
157178
suffix="astropy",
@@ -167,6 +188,7 @@ def test_astropy(self):
167188
check_images(self.test_cube, fname_beamcon), "Beamcon does not match Miriad"
168189

169190
def test_scipy(self):
191+
"""Test the scipy mode."""
170192
beamcon_3D.main(
171193
infile=[self.test_image],
172194
suffix="scipy",
@@ -181,4 +203,11 @@ def test_scipy(self):
181203
check_images(self.test_cube, fname_beamcon), "Beamcon does not match Miriad"
182204

183205
def tearDown(self) -> None:
184-
cleanup(self.files)
206+
"""Tear down the test."""
207+
cleanup(self.files)
208+
209+
210+
if __name__ == "__main__":
211+
unittest.TestLoader.sortTestMethodsUsing = None
212+
suite = unittest.TestLoader().loadTestsFromTestCase(test_Beamcon3D)
213+
unittest.TextTestRunner(verbosity=1).run(suite)

0 commit comments

Comments
 (0)