Skip to content

Commit fce673c

Browse files
committed
Tidy up use of progress bar display markers.
* Move progressbar stuff to subroutine * Rename pbar -> progress_bar Resist temptation towards short, meaningless variable names... even when they're just a progress bar object.
1 parent ce89189 commit fce673c

File tree

10 files changed

+64
-33
lines changed

10 files changed

+64
-33
lines changed

docs/source/notebooks/simulate_and_image.ipynb

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@
162162
"source": [
163163
"kernel_support = 3\n",
164164
"kernel_func = GaussianSinc(trunc=kernel_support)\n",
165-
"with Tqdm() as pbar:\n",
165+
"with Tqdm() as progress_bar:\n",
166166
" image, beam = imager.image_visibilities(data_vis, uvw_lambda,\n",
167167
" image_size=image_size,\n",
168168
" cell_size=cell_size,\n",
169169
" kernel_func=kernel_func,\n",
170170
" kernel_support=kernel_support,\n",
171171
" kernel_exact=True,\n",
172172
" kernel_oversampling=None,\n",
173-
" pbar=pbar\n",
173+
" progress_bar=progress_bar\n",
174174
" )\n",
175175
"image = np.real(image)"
176176
]
@@ -247,5 +247,5 @@
247247
}
248248
},
249249
"nbformat": 4,
250-
"nbformat_minor": 1
251-
}
250+
"nbformat_minor": 0
251+
}

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python
22

3-
import versioneer
43
from setuptools import find_packages, setup
54

5+
import versioneer
6+
67
install_requires = [
78
'astropy',
89
'attrs>=16.3.0',

src/fastimgproto/fixtures/profiling.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from memory_profiler import profile
2-
31
from fastimgproto.sourcefind.image import SourceFindImage
2+
from memory_profiler import profile
43

54

65
@profile

src/fastimgproto/gridder/gridder.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
import tqdm
88

99
from fastimgproto.gridder.kernel_generation import Kernel
10+
from fastimgproto.utils import reset_progress_bar
1011

1112
logger = logging.getLogger(__name__)
1213

1314

14-
def convolve_to_grid(kernel_func, support,
15+
def convolve_to_grid(kernel_func,
16+
support,
1517
image_size,
16-
uv, vis,
18+
uv,
19+
vis,
20+
vis_weights,
1721
exact=True,
1822
oversampling=0,
1923
raise_bounds=True,
20-
pbar=None):
24+
progress_bar=None):
2125
"""
2226
Grid visibilities, calculating the exact kernel distribution for each.
2327
@@ -60,7 +64,7 @@ def convolve_to_grid(kernel_func, support,
6064
Larger values give a finer-sampled set of pre-cached kernels.
6165
raise_bounds (bool): Raise an exception if any of the UV
6266
samples lie outside (or too close to the edge) of the grid.
63-
pbar (tqdm.tqdm): [Optional] progressbar to update.
67+
progress_bar (tqdm.tqdm): [Optional] progressbar to update.
6468
6569
Returns:
6670
tuple: (vis_grid, sampling_grid)
@@ -105,10 +109,9 @@ def convolve_to_grid(kernel_func, support,
105109
oversampled_offset = calculate_oversampled_kernel_indices(
106110
uv_frac, oversampling)
107111
logger.debug("Gridding {} visibilities".format(len(good_vis_idx)))
108-
if pbar is not None:
109-
pbar.total = len(good_vis_idx)
110-
pbar.n = 0
111-
pbar.set_description('Gridding visibilities')
112+
if progress_bar is not None:
113+
reset_progress_bar(progress_bar, len(good_vis_idx),
114+
'Gridding visibilities')
112115
for idx in good_vis_idx:
113116
gc_x, gc_y = kernel_centre_on_grid[idx]
114117
# Generate a convolution kernel with the precise offset required:

src/fastimgproto/imager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def image_visibilities(vis, uvw_lambda,
1414
kernel_exact=True,
1515
kernel_oversampling=0,
1616
normalize=True,
17-
pbar=None):
17+
progress_bar=None):
1818
"""
1919
Args:
2020
vis (numpy.ndarray): Complex visibilities.
@@ -49,7 +49,7 @@ def image_visibilities(vis, uvw_lambda,
4949
should be normalized such that the beam peaks at a value of
5050
1.0 Jansky. You normally want this to be true, but it may be
5151
interesting to check the raw values for debugging purposes.
52-
pbar (tqdm.tqdm): [Optional] progressbar to update.
52+
progress_bar (tqdm.tqdm): [Optional] progressbar to update.
5353
5454
Returns:
5555
tuple: (image, beam)
@@ -76,7 +76,7 @@ def image_visibilities(vis, uvw_lambda,
7676
vis=vis,
7777
exact=kernel_exact,
7878
oversampling=kernel_oversampling,
79-
pbar=pbar
79+
progress_bar=progress_bar
8080
)
8181
image = fft_to_image_plane(vis_grid)
8282
beam = fft_to_image_plane(sample_grid)

src/fastimgproto/scripts/image.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ def cli(config_json, in_vis, out_img):
4444
kernel_support = 3
4545
kernel_func = GaussianSinc(trunc=kernel_support)
4646

47-
with Tqdm() as pbar:
48-
image, beam = imager.image_visibilities(vis, uvw_lambda,
47+
with Tqdm() as progress_bar:
48+
image, beam = imager.image_visibilities(vis,
49+
uvw_lambda=uvw_lambda,
4950
image_size=image_size,
5051
cell_size=cell_size,
5152
kernel_func=kernel_func,
5253
kernel_support=kernel_support,
5354
kernel_exact=True,
5455
kernel_oversampling=None,
55-
pbar=pbar
56+
progress_bar=progress_bar
5657
)
5758

5859
np.savez(out_img, image=image, beam=beam)

src/fastimgproto/scripts/reduce.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@ def main(uvw_lambda,
114114
kernel_support = 3
115115
kernel_func = GaussianSinc(trunc=kernel_support)
116116
logger.info("Imaging residual visibilities")
117-
with Tqdm() as pbar:
118-
image, beam = imager.image_visibilities(residual_vis, uvw_lambda,
117+
with Tqdm() as progress_bar:
118+
image, beam = imager.image_visibilities(residual_vis,
119+
uvw_lambda=uvw_lambda,
119120
image_size=image_size,
120121
cell_size=cell_size,
121122
kernel_func=kernel_func,
122123
kernel_support=kernel_support,
123124
kernel_exact=True,
124-
pbar=pbar)
125+
progress_bar=progress_bar)
125126
logger.info("Running sourcefinder on image")
126127
sfimage = SourceFindImage(data=np.real(image),
127128
detection_n_sigma=detection_n_sigma,

src/fastimgproto/scripts/simulate_data.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def cli(output_npz, nstep):
4242
start_time=Time('2017-01-01'))
4343
obs_times = transit_time + np.linspace(-1, 1, nstep) * u.hr
4444
logger.info("Generating UVW-baselines for {} timesteps".format(nstep))
45-
with Tqdm() as pbar:
45+
with Tqdm() as progress_bar:
4646
uvw_m = telescope.uvw_tracking_skycoord(
4747
pointing_centre, obs_times,
48-
pbar=pbar
48+
progress_bar=progress_bar
4949
)
5050
# From here on we use UVW as multiples of wavelength, lambda:
5151
uvw_lambda = (uvw_m / wavelength).to(u.dimensionless_unscaled).value

src/fastimgproto/telescope/base.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
xyz_to_uvw_rotation_matrix,
1717
z_rotation_matrix,
1818
)
19+
from fastimgproto.utils import reset_progress_bar
1920

2021
_validator_optional_ndarray = attr.validators.optional(
2122
attr.validators.instance_of(np.ndarray))
@@ -208,7 +209,7 @@ def _uvw_tracking_skycoord_by_lha(self, pointing_centre, obs_times,
208209
return lha_uvw_map
209210

210211
def uvw_tracking_skycoord(self, pointing_centre, obs_times,
211-
pbar=None):
212+
progress_bar=None):
212213
"""
213214
Calculate the UVW-array towards pointing centre for all obs_times.
214215
@@ -222,25 +223,24 @@ def uvw_tracking_skycoord(self, pointing_centre, obs_times,
222223
for UVW calculation.
223224
obs_times (list): List of :class:`astropy.time.Time`, the instants
224225
of observation.
225-
pbar (tqdm.tqdm): [Optional] progressbar to update.
226+
progress_bar (tqdm.tqdm): [Optional] progressbar to update.
226227
Returns:
227228
astropy.units.Quantity: UVW-array, with units of metres.
228229
"""
229230
n_baselines = len(self.baseline_local_xyz)
230231
uvw_array = np.zeros((len(obs_times) * n_baselines, 3),
231232
dtype=np.float_) * self.baseline_local_xyz.unit
232-
if pbar is not None:
233-
pbar.total=len(obs_times)
234-
pbar.desc = 'Generating UVW-baselines'
233+
if progress_bar is not None:
234+
reset_progress_bar(progress_bar, len(obs_times), 'Generating UVW-baselines')
235235

236236
for idx, time in enumerate(obs_times):
237237
lha = self.lha(pointing_centre.ra, time)
238238
output_slice = slice(idx * n_baselines, (idx + 1) * n_baselines)
239239
uvw_array[output_slice] = self.uvw_at_local_hour_angle(
240240
local_hour_angle=lha, dec=pointing_centre.dec
241241
)
242-
if pbar is not None:
243-
pbar.update()
242+
if progress_bar is not None:
243+
progress_bar.update()
244244
return uvw_array
245245

246246

src/fastimgproto/utils.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
def reset_progress_bar(progress_bar, total, description="Doing something"):
4+
"""
5+
Short subroutine to reset a progress bar.
6+
7+
Currently setup for tqdm progress bars, but we could imagine this
8+
function evolving for a different libary or even handling progress bars
9+
polymorphically.
10+
11+
Mainly it just tidies 3 irrelevant lines into one clear function call,
12+
which is less distracting from key logic.
13+
14+
Currently
15+
Args:
16+
progress_bar (tqdm.tqdm): Progress display class
17+
total (int): total number of iterations being stepped through
18+
description (str): Description to display
19+
20+
Returns:
21+
22+
"""
23+
progress_bar.n = 0
24+
progress_bar.total=total
25+
progress_bar.desc=description
26+
return

0 commit comments

Comments
 (0)