Skip to content

Commit

Permalink
Merge branch 'main' into nmonte
Browse files Browse the repository at this point in the history
  • Loading branch information
moustakas committed Nov 1, 2024
2 parents 0fcae46 + 2827be4 commit 8091a1a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 21 deletions.
4 changes: 3 additions & 1 deletion doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Change Log
3.1.0 (not released yet)
------------------------

*
* Fix issues with log level retention [`PR #187`_].

.. _`PR #187`: https://github.com/desihub/fastspecfit/pull/187

3.0.0 (2024-10-30)
------------------
Expand Down
2 changes: 1 addition & 1 deletion py/fastspecfit/continuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ def continuum_fastspec(redshift, objflam, objflamivar, CTools,
snrmsg = f"Median spectral S/N_{data['cameras']}={data['snr'][0]:.2f}"
else:
snrmsg = f"Median spectral S/N_{data['cameras'][0]}={data['snr'][0]:.2f}"
for icam in np.arange(ncam-1)+1:
for icam in range(1,ncam):
snrmsg += f" S/N_{data['cameras'][icam]}={data['snr'][icam]:.2f}"
log.info(snrmsg)

Expand Down
5 changes: 1 addition & 4 deletions py/fastspecfit/fastspecfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False
if verbose:
args.verbose = True

if args.verbose:
from fastspecfit.logger import DEBUG
log.setLevel(DEBUG)

targetids = None
input_redshifts = None

Expand All @@ -126,6 +122,7 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False
'template_file': args.templates,
'template_version': args.templateversion,
'template_imf': args.imf,
'log_verbose': args.verbose
}

sc_data.initialize(**init_sc_args)
Expand Down
2 changes: 1 addition & 1 deletion py/fastspecfit/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import fitsio
from astropy.table import Table

from fastspecfit.logger import log, DEBUG
from fastspecfit.logger import log
from fastspecfit.singlecopy import sc_data
from fastspecfit.photometry import Photometry
from fastspecfit.util import FLUXNORM, ZWarningMask
Expand Down
2 changes: 1 addition & 1 deletion py/fastspecfit/linemasker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
from astropy.table import Table

from fastspecfit.logger import log, DEBUG
from fastspecfit.logger import log
from fastspecfit.util import C_LIGHT, quantile, median


Expand Down
43 changes: 40 additions & 3 deletions py/fastspecfit/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,51 @@
fastspecfit.logger
==================
Unique logger object, allocated at startup and used throughout code base.
Unique logger object, distinct from the one used by the DESI libraries,
allocated at startup and used throughout code base. We need to create
our own logger to avoid the DESI libraries accidentally changing the
log level out from under us.
Having just one logger allows us to initialize its level on startup and have
those changes propagate everywhere.
This needs to be in its own file to prevent circular imports with other code.
"""
from desiutil.log import get_logger, DEBUG
from logging import DEBUG

log = get_logger()#DEBUG)
def getFastspecLogger():
"""
Create a logging object unique to the fastspec tools.
Configure it to send its log messages to stdout and to
format them identically to the DESIUtil defaults.
Note that every call to this function returns the *same*
log object, so will reset its properties including log level.
Hence, it should really only be called once at the start
of the program, as we do here, to create a singleton
log object.
Returns a log object with default level INFO.
"""

import logging
import sys

root_name = 'fastspec'

ch = logging.StreamHandler(sys.stdout)
fmtfields = ['%(levelname)s', '%(filename)s', '%(lineno)s', '%(funcName)s']
fmtfields.append(' %(message)s')
formatter = logging.Formatter(':'.join(fmtfields),
datefmt='%Y-%m-%dT%H:%M:%S')
ch.setFormatter(formatter)

log = logging.getLogger(root_name)
log.addHandler(ch)
log.setLevel(logging.INFO)

return log


log = getFastspecLogger()
10 changes: 2 additions & 8 deletions py/fastspecfit/photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import fitsio
from astropy.table import Table

from fastspecfit.logger import log, DEBUG
from fastspecfit.logger import log
from fastspecfit.util import trapz, C_LIGHT, FLUXNORM


Expand Down Expand Up @@ -1164,7 +1164,7 @@ def _gather_tractorphot_onebrick(input_cat, legacysurveydir, radius_match, racol

def gather_tractorphot(input_cat, racolumn='TARGET_RA', deccolumn='TARGET_DEC',
legacysurveydir=None, dr9dir=None, radius_match=1.0,
restrict_region=None, columns=None, verbose=False):
restrict_region=None, columns=None):
"""Retrieve the Tractor catalog for all the objects in this catalog (one brick).
Args:
Expand All @@ -1186,12 +1186,6 @@ def gather_tractorphot(input_cat, racolumn='TARGET_RA', deccolumn='TARGET_DEC',
"""
from desitarget.targets import decode_targetid
from desiutil.brick import brickname
from desiutil.log import get_logger, DEBUG

if verbose:
log = get_logger(DEBUG)
else:
log = get_logger()

if len(input_cat) == 0:
log.warning('No objects in input catalog.')
Expand Down
1 change: 1 addition & 0 deletions py/fastspecfit/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,7 @@ def fastqa(args=None, comm=None):
'template_file': args.templates,
'template_version': args.templateversion,
'template_imf': args.imf,
'log_verbose': False,
}

sc_data.initialize(**init_sc_args)
Expand Down
10 changes: 8 additions & 2 deletions py/fastspecfit/singlecopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from fastspecfit.photometry import Photometry
from fastspecfit.linetable import LineTable
from fastspecfit.templates import Templates
from fastspecfit.logger import log
from fastspecfit.logger import log, DEBUG

class Singletons(object):

Expand All @@ -25,7 +25,13 @@ def initialize(self,
ignore_photometry=False,
template_file=None,
template_version=None,
template_imf=None):
template_imf=None,
log_verbose=False,
):

# adjust logging level if requested
if log_verbose:
log.setLevel(DEBUG)

# templates for continnuum fitting
self.templates = Templates(template_file=template_file,
Expand Down

0 comments on commit 8091a1a

Please sign in to comment.