Skip to content

Commit f6f19d0

Browse files
authored
Add extra return and types (#72)
1 parent 3814a48 commit f6f19d0

File tree

3 files changed

+101
-95
lines changed

3 files changed

+101
-95
lines changed

racs_tools/beamcon_2D.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env python
22
""" Convolve ASKAP images to common resolution """
3+
from __future__ import annotations
4+
35
__author__ = "Alec Thomson"
46

7+
58
import logging
69
import sys
710
from pathlib import Path
8-
from typing import List, Literal, NamedTuple, Optional, Tuple
11+
from typing import Literal, NamedTuple, Optional
912

1013
import numpy as np
1114
from astropy import units as u
@@ -79,16 +82,16 @@ class BeamLogInfo(NamedTuple):
7982
def check_target_beam(
8083
target_beam: Beam,
8184
all_beams: Beams,
82-
files: List[Path],
83-
cutoff: Optional[float] = None,
85+
files: list[Path],
86+
cutoff: float | None = None,
8487
) -> bool:
8588
"""Check that target beam will deconvolve
8689
8790
Args:
8891
target_beam (Beam): Target beam.
8992
all_beams (Beams): All the beams to check.
90-
files (List[Path]): All the FITS files to check.
91-
cutoff (Optional[float], optional): Cutoff of beam in arcsec. Defaults to None.
93+
files (list[Path]): All the FITS files to check.
94+
cutoff (float | None, optional): Cutoff of beam in arcsec. Defaults to None.
9295
9396
Raises:
9497
BeamError: If beam deconvolution fails.
@@ -200,8 +203,8 @@ def beamcon_2d_on_fits(
200203
conv_mode: Literal["robust", "scipy", "astropy", "astropy_fft"],
201204
suffix: str = "",
202205
prefix: str = "",
203-
outdir: Optional[Path] = None,
204-
cutoff: Optional[float] = None,
206+
outdir: Path | None = None,
207+
cutoff: float | None = None,
205208
dryrun: bool = False,
206209
) -> BeamLogInfo:
207210
"""Run beamcon_2d on a FITS file
@@ -212,8 +215,8 @@ def beamcon_2d_on_fits(
212215
conv_mode (str): Convolution mode.
213216
suffix (str, optional): Filename suffix. Defaults to "".
214217
prefix (str, optional): Filename prefix. Defaults to "".
215-
outdir (Optional[Path], optional): Ouput directory. Defaults to None (will be same as input).
216-
cutoff (Optional[float], optional): Cutoff for beamsize in arcsec. Defaults to None.
218+
outdir (Path | None, optional): Ouput directory. Defaults to None (will be same as input).
219+
cutoff (float | None, optional): Cutoff for beamsize in arcsec. Defaults to None.
217220
dryrun (bool, optional): Don't save any images. Defaults to False.
218221
219222
Returns:
@@ -281,27 +284,27 @@ def beamcon_2d_on_fits(
281284

282285

283286
def get_common_beam(
284-
files: List[Path],
287+
files: list[Path],
285288
conv_mode: Literal["robust", "scipy", "astropy", "astropy_fft"] = "robust",
286289
target_beam: Optional[Beam] = None,
287-
cutoff: Optional[float] = None,
290+
cutoff: float | None = None,
288291
tolerance: float = 0.0001,
289292
nsamps: float = 200,
290293
epsilon: float = 0.0005,
291-
) -> Tuple[Beam, Beams]:
294+
) -> tuple[Beam, Beams]:
292295
"""Get the smallest common beam.
293296
294297
Args:
295-
files (List[Path]): FITS files to convolve.
298+
files (list[Path]): FITS files to convolve.
296299
conv_mode (Literal["robust", "scipy", "astropy", "astropy_fft"], optional): _description_. Defaults to "robust".
297300
target_beam (Optional[Beam], optional): Target beam. Defaults to None.
298-
cutoff (Optional[float], optional): Cutoff for beamsize in arcse. Defaults to None.
301+
cutoff (float | None, optional): Cutoff for beamsize in arcse. Defaults to None.
299302
tolerance (float, optional): Radio beam tolerance. Defaults to 0.0001.
300303
nsamps (float, optional): Radio beam nsamps. Defaults to 200.
301304
epsilon (float, optional): Radio beam epsilon. Defaults to 0.0005.
302305
303306
Returns:
304-
Tuple[Beam, Beams]: Common beam and all beams.
307+
tuple[Beam, Beams]: Common beam and all beams.
305308
"""
306309
beams_list = []
307310
for file in files:
@@ -359,52 +362,52 @@ def get_common_beam(
359362
return target_beam, beams
360363

361364

362-
def writelog(output: List[BeamLogInfo], commonbeam_log: Path):
365+
def writelog(output: list[BeamLogInfo], commonbeam_log: Path):
363366
"""Write beamlog file.
364367
365368
Args:
366-
output (List[BeamLogInfo]): List of beamlog information.
369+
output (list[BeamLogInfo]): list of beamlog information.
367370
commonbeam_log (Path): Name of log file.
368371
"""
369372
commonbeam_tab = Table()
370-
commonbeam_tab.add_column([out["filename"] for out in output], name="FileName")
373+
commonbeam_tab.add_column([out.filename for out in output], name="FileName")
371374
# Origina
372375
commonbeam_tab.add_column(
373-
[out["old_beam"].major.to(u.deg).value for out in output] * u.deg,
376+
[out.old_beam.major.to(u.deg).value for out in output] * u.deg,
374377
name="Original BMAJ",
375378
)
376379
commonbeam_tab.add_column(
377-
[out["old_beam"].minor.to(u.deg).value for out in output] * u.deg,
380+
[out.old_beam.minor.to(u.deg).value for out in output] * u.deg,
378381
name="Original BMIN",
379382
)
380383
commonbeam_tab.add_column(
381-
[out["old_beam"].pa.to(u.deg).value for out in output] * u.deg,
384+
[out.old_beam.pa.to(u.deg).value for out in output] * u.deg,
382385
name="Original BPA",
383386
)
384387
# Target
385388
commonbeam_tab.add_column(
386-
[out["new_beam"].major.to(u.deg).value for out in output] * u.deg,
389+
[out.new_beam.major.to(u.deg).value for out in output] * u.deg,
387390
name="Target BMAJ",
388391
)
389392
commonbeam_tab.add_column(
390-
[out["new_beam"].minor.to(u.deg).value for out in output] * u.deg,
393+
[out.new_beam.minor.to(u.deg).value for out in output] * u.deg,
391394
name="Target BMIN",
392395
)
393396
commonbeam_tab.add_column(
394-
[out["new_beam"].pa.to(u.deg).value for out in output] * u.deg,
397+
[out.new_beam.pa.to(u.deg).value for out in output] * u.deg,
395398
name="Target BPA",
396399
)
397400
# Convolving
398401
commonbeam_tab.add_column(
399-
[out["conv_beam"].major.to(u.deg).value for out in output] * u.deg,
402+
[out.conv_beam.major.to(u.deg).value for out in output] * u.deg,
400403
name="Convolving BMAJ",
401404
)
402405
commonbeam_tab.add_column(
403-
[out["conv_beam"].minor.to(u.deg).value for out in output] * u.deg,
406+
[out.conv_beam.minor.to(u.deg).value for out in output] * u.deg,
404407
name="Convolving BMIN",
405408
)
406409
commonbeam_tab.add_column(
407-
[out["conv_beam"].pa.to(u.deg).value for out in output] * u.deg,
410+
[out.conv_beam.pa.to(u.deg).value for out in output] * u.deg,
408411
name="Convolving BPA",
409412
)
410413

@@ -422,45 +425,45 @@ def writelog(output: List[BeamLogInfo], commonbeam_log: Path):
422425

423426

424427
def smooth_fits_files(
425-
infile_list: List[Path] = [],
426-
prefix: Optional[str] = None,
427-
suffix: Optional[str] = None,
428-
outdir: Optional[Path] = None,
428+
infile_list: list[Path] = [],
429+
prefix: str | None = None,
430+
suffix: str | None = None,
431+
outdir: Path | None = None,
429432
conv_mode: Literal["robust", "scipy", "astropy", "astropy_fft"] = "robust",
430433
dryrun: bool = False,
431-
bmaj: Optional[float] = None,
432-
bmin: Optional[float] = None,
433-
bpa: Optional[float] = None,
434-
log: Optional[str] = None,
434+
bmaj: float | None = None,
435+
bmin: float | None = None,
436+
bpa: float | None = None,
437+
log: Path | None = None,
435438
circularise: bool = False,
436-
cutoff: Optional[float] = None,
439+
cutoff: float | None = None,
437440
listfile: bool = False,
438441
tolerance: float = 0.0001,
439442
nsamps: int = 200,
440443
epsilon: float = 0.0005,
441-
ncores: Optional[int] = None,
444+
ncores: int | None = None,
442445
executor_type: Literal["thread", "process", "mpi"] = "thread",
443446
verbosity: int = 0,
444447
) -> Beam:
445448
"""Smooth a field of 2D images to a common resolution.
446449
447450
Args:
448-
infile_list (List[Path], optional): List of FITS files to convolve. Defaults to [].
449-
prefix (Optional[str], optional): Output filename prefix. Defaults to None.
450-
suffix (Optional[str], optional): Output filename suffix. Defaults to None.
451-
outdir (Optional[Path], optional): Output directory. Defaults to None - same as input.
451+
infile_list (list[Path], optional): list of FITS files to convolve. Defaults to [].
452+
prefix (str | None, optional): Output filename prefix. Defaults to None.
453+
suffix (str | None, optional): Output filename suffix. Defaults to None.
454+
outdir (Path | None, optional): Output directory. Defaults to None - same as input.
452455
conv_mode (Literal["robust", "scipy", "astropy", "astropy_fft"], optional): Convolution mode. Defaults to "robust".
453456
dryrun (bool, optional): Don't save any images. Defaults to False.
454-
bmaj (Optional[float], optional): Target beam major axis in arcsec. Defaults to None.
455-
bmin (Optional[float], optional): Target beam minor axis in arcsec. Defaults to None.
456-
bpa (Optional[float], optional): Target beam poistion angle in deg. Defaults to None.
457-
log (Optional[str], optional): Ouput logfile. Defaults to None.
457+
bmaj (float | None, optional): Target beam major axis in arcsec. Defaults to None.
458+
bmin (float | None, optional): Target beam minor axis in arcsec. Defaults to None.
459+
bpa (float | None, optional): Target beam poistion angle in deg. Defaults to None.
460+
log (str | None, optional): Ouput logfile. Defaults to None.
458461
circularise (bool, optional): Set minor axis to same as major. Defaults to False.
459-
cutoff (Optional[float], optional): Cutoff for beamsize in arcse. Defaults to None.
462+
cutoff (float | None, optional): Cutoff for beamsize in arcse. Defaults to None.
460463
tolerance (float, optional): Radio beam tolerance. Defaults to 0.0001.
461464
nsamps (int, optional): Radio beam nsamp. Defaults to 200.
462465
epsilon (float, optional): Radio beam epsilon. Defaults to 0.0005.
463-
ncores (Optional[int], optional): Maximum number of cores to use. Defaults to None.
466+
ncores (int | None, optional): Maximum number of cores to use. Defaults to None.
464467
executor_type (Literal["thread", "process", "mpi"], optional): Executor to use. Defaults to "thread".
465468
466469
Raises:
@@ -676,7 +679,7 @@ def cli():
676679
parser.add_argument(
677680
"--logfile",
678681
default=None,
679-
type=str,
682+
type=Path,
680683
help="Save logging output to file",
681684
)
682685

0 commit comments

Comments
 (0)