Skip to content

Commit

Permalink
initial pass at single logger
Browse files Browse the repository at this point in the history
  • Loading branch information
tgalvin committed Aug 22, 2023
1 parent 4b9cacd commit cca8910
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 101 deletions.
33 changes: 17 additions & 16 deletions AegeanTools/AeRes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from astropy.io import fits

from AegeanTools import catalogs, fitting, wcs_helpers
from AegeanTools.logging import logger

__author__ = "Paul Hancock"

Expand Down Expand Up @@ -46,18 +47,18 @@ def load_sources(filename,
good = True
for c in required_cols:
if c not in table.colnames:
logging.error("Column {0} not found".format(c))
logger.error("Column {0} not found".format(c))
good = False
if not good:
logging.error("Some required columns missing or mis-labeled")
logger.error("Some required columns missing or mis-labeled")
return None
# rename the table columns
for old, new in zip([ra_col, dec_col, peak_col, a_col, b_col, pa_col],
['ra', 'dec', 'peak_flux', 'a', 'b', 'pa']):
table.rename_column(old, new)

catalog = catalogs.table_to_source_list(table)
logging.info("read {0} sources from {1}".format(len(catalog), filename))
logger.info("read {0} sources from {1}".format(len(catalog), filename))
return catalog


Expand Down Expand Up @@ -105,10 +106,10 @@ def make_model(sources, shape, wcshelper, mask=False, frac=None, sigma=4):

# skip sources that have a center that is outside of the image
if not 0 < xo < shape[0]:
logging.debug("source {0} is not within image".format(src.island))
logger.debug("source {0} is not within image".format(src.island))
continue
if not 0 < yo < shape[1]:
logging.debug("source {0} is not within image".format(src.island))
logger.debug("source {0} is not within image".format(src.island))
continue

# pixels over which this model is calculated
Expand All @@ -130,15 +131,15 @@ def make_model(sources, shape, wcshelper, mask=False, frac=None, sigma=4):
if not np.all(np.isfinite([ymin, ymax, xmin, xmax])):
continue

if logging.getLogger().isEnabledFor(logging.DEBUG): # pragma: no cover
logging.debug("Source ({0},{1})".format(src.island, src.source))
logging.debug(" xo, yo: {0} {1}".format(xo, yo))
logging.debug(" sx, sy: {0} {1}".format(sx, sy))
logging.debug(" theta, phi: {0} {1}".format(theta, phi))
logging.debug(" xoff, yoff: {0} {1}".format(xoff, yoff))
logging.debug(" xmin, xmax, ymin, ymax: {0}:{1} {2}:{3}".format(
if logging.getLogger().isEnabledFor(logger.debug): # pragma: no cover
logger.debug("Source ({0},{1})".format(src.island, src.source))
logger.debug(" xo, yo: {0} {1}".format(xo, yo))
logger.debug(" sx, sy: {0} {1}".format(sx, sy))
logger.debug(" theta, phi: {0} {1}".format(theta, phi))
logger.debug(" xoff, yoff: {0} {1}".format(xoff, yoff))
logger.debug(" xmin, xmax, ymin, ymax: {0}:{1} {2}:{3}".format(
xmin, xmax, ymin, ymax))
logging.debug(" flux, sx, sy: {0} {1} {2}".format(
logger.debug(" flux, sx, sy: {0} {1} {2}".format(
src.peak_flux, sx, sy))

# positions for which we want to make the model
Expand All @@ -163,7 +164,7 @@ def make_model(sources, shape, wcshelper, mask=False, frac=None, sigma=4):
else:
m[x, y] += model
i_count += 1
logging.info("modeled {0} sources".format(i_count))
logger.info("modeled {0} sources".format(i_count))
return m


Expand Down Expand Up @@ -238,9 +239,9 @@ def make_residual(fitsfile, catalog, rfile, mfile=None,

hdulist[0].data = residual
hdulist.writeto(rfile, overwrite=True)
logging.info("wrote residual to {0}".format(rfile))
logger.info("wrote residual to {0}".format(rfile))
if mfile is not None:
hdulist[0].data = model
hdulist.writeto(mfile, overwrite=True)
logging.info("wrote model to {0}".format(mfile))
logger.info("wrote model to {0}".format(mfile))
return
57 changes: 29 additions & 28 deletions AegeanTools/BANE.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from scipy.interpolate import RegularGridInterpolator

from .fits_tools import compress
from AegeanTools.logging import logger

__author__ = 'Paul Hancock'
__version__ = 'v1.10.0'
Expand Down Expand Up @@ -92,7 +93,7 @@ def sigmaclip(arr, lo, hi, reps=10):
mean = np.mean(clipped)
prev_valid = curr_valid
else:
logging.debug(
logger.debug(
"No stopping criteria was reached after {0} cycles".format(count))

return mean, std
Expand Down Expand Up @@ -157,7 +158,7 @@ def sigma_filter(filename, region, step_size, box_size, shape, domask,
"""

ymin, ymax = region
logging.debug('rows {0}-{1} starting at {2}'.format(ymin,
logger.debug('rows {0}-{1} starting at {2}'.format(ymin,
ymax, strftime("%Y-%m-%d %H:%M:%S", gmtime())))

# cut out the region of interest plus 1/2 the box size
Expand All @@ -184,8 +185,8 @@ def sigma_filter(filename, region, step_size, box_size, shape, domask,
data_row_min:data_row_max, 0:shape[1]]
)
else:
logging.error("Too many NAXIS for me {0}".format(NAXIS))
logging.error("fix your file to be more sane")
logger.error("Too many NAXIS for me {0}".format(NAXIS))
logger.error("fix your file to be more sane")
raise Exception("Too many NAXIS")

# Manually scale the data if BSCALE is not 1.0
Expand All @@ -198,8 +199,8 @@ def sigma_filter(filename, region, step_size, box_size, shape, domask,

# row_len = shape[1]

logging.debug('data size is {0}'.format(data.shape))
logging.debug('data format is {0}'.format(data.dtype))
logger.debug('data size is {0}'.format(data.shape))
logger.debug('data format is {0}'.format(data.dtype))

def box(r, c):
"""
Expand Down Expand Up @@ -238,22 +239,22 @@ def box(r, c):
irms_shm = SharedMemory(name=f'irms_{memory_id}', create=False)
irms = np.ndarray(shape, dtype=np.float64, buffer=irms_shm.buf)

logging.debug("Interpolating bkg to sharemem")
logger.debug("Interpolating bkg to sharemem")
ifunc = RegularGridInterpolator((rows, cols), vals)
interp_bkg = np.array(ifunc((gr, gc)), dtype=np.float64)
ibkg[ymin:ymax, :] = interp_bkg
del ifunc, interp_bkg
logging.debug(" ... done writing bkg")
logger.debug(" ... done writing bkg")

# wait for all to complete
i = barrier.wait()
if i == 0:
barrier.reset()

logging.debug("background subtraction")
logger.debug("background subtraction")
data[0 + ymin - data_row_min: data.shape[0] -
(data_row_max - ymax), :] -= ibkg[ymin:ymax, :]
logging.debug(".. done ")
logger.debug(".. done ")

# reset/recycle the vals array
vals[:] = 0
Expand All @@ -266,27 +267,27 @@ def box(r, c):
_, rms = sigmaclip(new, 3, 3)
vals[i, j] = rms

logging.debug("Interpolating rms to sharemem")
logger.debug("Interpolating rms to sharemem")
ifunc = RegularGridInterpolator((rows, cols), vals)
interp_rms = np.array(ifunc((gr, gc)), dtype=np.float64)
irms[ymin:ymax, :] = interp_rms
del ifunc, interp_rms
logging.debug(" .. done writing rms")
logger.debug(" .. done writing rms")

if domask:
# wait for all to complete
i = barrier.wait()
if i == 0:
barrier.reset()

logging.debug("applying mask")
logger.debug("applying mask")
mask = ~np.isfinite(
data[0 + ymin - data_row_min: data.shape[0] -
(data_row_max - ymax), :])
ibkg[ymin:ymax, :][mask] = np.nan
irms[ymin:ymax, :][mask] = np.nan
logging.debug("... done applying mask")
logging.debug('rows {0}-{1} finished at {2}'.format(ymin,
logger.debug("... done applying mask")
logger.debug('rows {0}-{1} finished at {2}'.format(ymin,
ymax, strftime("%Y-%m-%d %H:%M:%S", gmtime())))
return

Expand Down Expand Up @@ -339,8 +340,8 @@ def filter_mc_sharemem(filename, step_size, box_size, cores, shape,

img_y, img_x = shape

logging.info("using {0} cores".format(cores))
logging.info("using {0} stripes".format(nslice))
logger.info("using {0} cores".format(cores))
logger.info("using {0} stripes".format(nslice))

if nslice > 1:
# box widths should be multiples of the step_size, and not zero
Expand All @@ -354,8 +355,8 @@ def filter_mc_sharemem(filename, step_size, box_size, cores, shape,
ymins = [0]
ymaxs = [img_y]

logging.debug("ymins {0}".format(ymins))
logging.debug("ymaxs {0}".format(ymaxs))
logger.debug("ymins {0}".format(ymins))
logger.debug("ymaxs {0}".format(ymaxs))

args = []
for region in zip(ymins, ymaxs):
Expand Down Expand Up @@ -384,7 +385,7 @@ def filter_mc_sharemem(filename, step_size, box_size, cores, shape,
# process
pool.map_async(_sf2, args, chunksize=1).get(timeout=10000000)
except KeyboardInterrupt:
logging.error("Caught keyboard interrupt")
logger.error("Caught keyboard interrupt")
pool.close()
exit = True
else:
Expand Down Expand Up @@ -465,7 +466,7 @@ def filter_image(im_name, out_base, step_size=None, box_size=None,
if naxis > 2:
naxis3 = header['NAXIS3']
if cube_index >= naxis3:
logging.error(
logger.error(
"3rd dimension has len {0} but index {1} was passed".format(
naxis3, cube_index)
)
Expand All @@ -481,19 +482,19 @@ def filter_image(im_name, out_base, step_size=None, box_size=None,
if compressed:
if not step_size[0] == step_size[1]:
step_size = (min(step_size), min(step_size))
logging.info(
logger.info(
"Changing grid to be {0} so we can compress the output".format(
step_size)
)

logging.info("using grid_size {0}, box_size {1}".format(
logger.info("using grid_size {0}, box_size {1}".format(
step_size, box_size))
logging.info("on data shape {0}".format(shape))
logger.info("on data shape {0}".format(shape))
bkg, rms = filter_mc_sharemem(im_name,
step_size=step_size, box_size=box_size,
cores=cores, shape=shape, nslice=nslice,
domask=mask, cube_index=cube_index)
logging.info("done")
logger.info("done")

if out_base is not None:
# add a comment to the fits header
Expand Down Expand Up @@ -563,8 +564,8 @@ def get_step_size(header):
# default to 4x the synthesized beam width
step_size = int(np.ceil(4*beam_size/pix_scale))
else:
logging.info("BMAJ and/or BMIN not in fits header.")
logging.info("Assuming 4 pix/beam, so we have step_size = 16 pixels")
logger.info("BMAJ and/or BMIN not in fits header.")
logger.info("Assuming 4 pix/beam, so we have step_size = 16 pixels")
step_size = 16
step_size = (step_size, step_size)
return step_size
Expand Down Expand Up @@ -593,5 +594,5 @@ def write_fits(data, header, file_name):
hdu.header = header
hdulist = fits.HDUList([hdu])
hdulist.writeto(file_name, overwrite=True)
logging.info("Wrote {0}".format(file_name))
logger.info("Wrote {0}".format(file_name))
return
2 changes: 1 addition & 1 deletion AegeanTools/CLI/AeReg.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def main(argv=()):
invocation_string = " ".join(argv)

# configure logging
logging_level = logging.DEBUG if options.debug else logging.INFO
logging_level = logger.debug if options.debug else logger.info
log = logging.getLogger("Aegean")
logging.basicConfig(level=logging_level,
format="%(process)d:%(levelname)s %(message)s")
Expand Down
14 changes: 7 additions & 7 deletions AegeanTools/CLI/AeRes.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,31 @@ def main(argv=()):

options = parser.parse_args(args=argv)

logging_level = logging.DEBUG if options.debug else logging.INFO
logging_level = logger.debug if options.debug else logger.info
logging.basicConfig(level=logging_level,
format="%(process)d:%(levelname)s %(message)s")
logging.info("This is AeRes {0}-({1})".format(__version__, __date__))
logger.info("This is AeRes {0}-({1})".format(__version__, __date__))

if options.catalog is None:
logging.error("input catalog is required")
logger.error("input catalog is required")
parser.print_help()
return 1
if options.fitsfile is None:
logging.error("input fits file is required")
logger.error("input fits file is required")
parser.print_help()
return 1
if options.rfile is None:
logging.error("output residual filename is required")
logger.error("output residual filename is required")
parser.print_help()
return 1
# convert default value of 0 to be None.
if options.frac <= 0:
options.frac = None

logging.info("Using {0} and {1} to make {2}".format(
logger.info("Using {0} and {1} to make {2}".format(
options.fitsfile, options.catalog, options.rfile))
if options.mfile is not None:
logging.info(" and writing model to {0}".format(options.mfile))
logger.info(" and writing model to {0}".format(options.mfile))

colmap = {'ra_col': options.ra_col,
'dec_col': options.dec_col,
Expand Down
10 changes: 5 additions & 5 deletions AegeanTools/CLI/BANE.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def main(argv=()):

# Get the BANE logger.
logging = BANE.logging
logging_level = logging.DEBUG if options.debug else logging.INFO
logging_level = logger.debug if options.debug else logger.info
logging.basicConfig(level=logging_level,
format="%(process)d:%(levelname)s %(message)s")
logging.info(
logger.info(
"This is BANE {0}-({1})".format(BANE.__version__, BANE.__date__))

if not os.path.exists(options.image):
logging.error("File not found: {0} ".format(options.image))
logger.error("File not found: {0} ".format(options.image))
return 1

if options.out_base is None:
Expand All @@ -79,9 +79,9 @@ def main(argv=()):
bkgout = options.out_base + '_bkg.fits'
rmsout = options.out_base + '_rms.fits'
if os.path.exists(bkgout) and os.path.exists(rmsout):
logging.error("{0} and {1} exist and you said noclobber"
logger.error("{0} and {1} exist and you said noclobber"
"".format(bkgout, rmsout))
logging.error("Not running")
logger.error("Not running")
return 1

BANE.filter_image(im_name=options.image, out_base=options.out_base,
Expand Down
6 changes: 3 additions & 3 deletions AegeanTools/CLI/MIMAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ def main(argv=()):

# get the MIMAS logger
logging = MIMAS.logging
logging_level = logging.DEBUG if results.debug else logging.INFO
logging_level = logger.debug if results.debug else logger.info
logging.basicConfig(level=logging_level,
format="%(process)d:%(levelname)s %(message)s")
logging.info(
logger.info(
"This is MIMAS {0}-({1})".format(MIMAS.__version__, MIMAS.__date__))

if len(results.fits_mask) > 0:
logging.info("The --fitsmask option is not yet implemented.")
logger.info("The --fitsmask option is not yet implemented.")
return 1

if len(results.mim2reg) > 0:
Expand Down
Loading

0 comments on commit cca8910

Please sign in to comment.