33
44import subprocess as sp
55import unittest
6+ from typing import List , Tuple , Union
67
78import astropy .units as u
89import numpy as np
1314from 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
75107class 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
0 commit comments