From cac5f3f9dbf1728915d422a5222fa450c56a95a0 Mon Sep 17 00:00:00 2001 From: Sergio Pascual Date: Thu, 19 Oct 2023 11:09:00 +0200 Subject: [PATCH] Fix deprecations * utcnow to now(UTC) * ndimage.filters is deprecated --- .../instrument/components/instrument.py | 2 +- src/megaradrp/processing/aperture.py | 3 ++- src/megaradrp/processing/diffuselight.py | 5 ++-- src/megaradrp/processing/extractobj.py | 2 +- src/megaradrp/processing/fiberflat.py | 6 +++-- src/megaradrp/processing/fluxcalib.py | 3 ++- src/megaradrp/processing/slitflat.py | 3 ++- .../processing/tests/test_modelmap.py | 4 +-- src/megaradrp/processing/trimover.py | 26 +++++++++---------- src/megaradrp/processing/twilight.py | 4 +-- src/megaradrp/processing/wavecalibration.py | 4 +-- src/megaradrp/recipes/calibration/bpm.py | 13 +++++----- src/megaradrp/recipes/calibration/slitflat.py | 4 +-- src/megaradrp/recipes/calibration/trace.py | 4 +-- src/megaradrp/simulation/factory.py | 12 +++++---- 15 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/megaradrp/instrument/components/instrument.py b/src/megaradrp/instrument/components/instrument.py index b9afe9c..250c933 100644 --- a/src/megaradrp/instrument/components/instrument.py +++ b/src/megaradrp/instrument/components/instrument.py @@ -12,7 +12,7 @@ import numpy as np from scipy.stats import norm import scipy.interpolate as interpol -from scipy.ndimage.filters import convolve1d +from scipy.ndimage import convolve1d from numina.instrument.hwdevice import HWDevice from numina.instrument.simulation.efficiency import Efficiency diff --git a/src/megaradrp/processing/aperture.py b/src/megaradrp/processing/aperture.py index 2b3c95a..5375756 100644 --- a/src/megaradrp/processing/aperture.py +++ b/src/megaradrp/processing/aperture.py @@ -200,7 +200,8 @@ def run(self, img): hdr['history'] = f'Aperture extraction method {method_name}' hdr['history'] = f'Aperture extraction with {self.calibid}' hdr['history'] = f'Aperture extraction offsets are {self.trace_repr.global_offset.coef.tolist()}' - hdr['history'] = f'Aperture extraction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Aperture extraction time {tnow.isoformat()}' # Update Fibers fibers_ext = img['FIBERS'] diff --git a/src/megaradrp/processing/diffuselight.py b/src/megaradrp/processing/diffuselight.py index 58c541f..2ad92e0 100644 --- a/src/megaradrp/processing/diffuselight.py +++ b/src/megaradrp/processing/diffuselight.py @@ -1,5 +1,5 @@ # -# Copyright 2019-2021 Universidad Complutense de Madrid +# Copyright 2019-2023 Universidad Complutense de Madrid # # This file is part of Megara DRP # @@ -39,7 +39,8 @@ def __init__(self, diffuse, datamodel=None, calibid='calibid-unknown', def header_update(self, hdr, imgid): hdr['NUM-DFL'] = self.calibid hdr['history'] = f'Diffuse light correction {imgid}' - hdr['history'] = f'Diffuse light correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Diffuse light correction time {tnow.isoformat()}' def run(self, img): imgid = self.get_imgid(img) diff --git a/src/megaradrp/processing/extractobj.py b/src/megaradrp/processing/extractobj.py index 5013550..4f5d076 100644 --- a/src/megaradrp/processing/extractobj.py +++ b/src/megaradrp/processing/extractobj.py @@ -17,7 +17,7 @@ import astropy.io.fits as fits import astropy.units as u from scipy.spatial import KDTree -from scipy.ndimage.filters import gaussian_filter +from scipy.ndimage import gaussian_filter from numina.array.wavecalib.crosscorrelation import periodic_corr1d # import megaradrp.datamodel as dm diff --git a/src/megaradrp/processing/fiberflat.py b/src/megaradrp/processing/fiberflat.py index 501796d..141829c 100644 --- a/src/megaradrp/processing/fiberflat.py +++ b/src/megaradrp/processing/fiberflat.py @@ -55,7 +55,8 @@ def run(self, img): def header_update(self, hdr, imgid): hdr['NUM-FLT'] = self.calibid hdr['history'] = f'Flat correction {imgid}' - hdr['history'] = f'Flat correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Flat correction time {tnow.isoformat()}' class FiberFlatCorrector(CommonFlatCorrector): @@ -74,7 +75,8 @@ def __init__(self, fiberflat, datamodel=None, dtype='float32'): def header_update(self, hdr, imgid): hdr['NUM-FIBF'] = self.calibid hdr['history'] = f'Fiber flat correction {imgid}' - hdr['history'] = f'Fiber flat correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Fiber flat correction time {tnow.isoformat()}' class Splitter(Corrector): diff --git a/src/megaradrp/processing/fluxcalib.py b/src/megaradrp/processing/fluxcalib.py index 6dd8d57..7d24589 100644 --- a/src/megaradrp/processing/fluxcalib.py +++ b/src/megaradrp/processing/fluxcalib.py @@ -68,7 +68,8 @@ def header_update(self, hdr, imgid): update_flux_limits(hdr, self.pixlims, ref=0) hdr['history'] = f'Flux calibration in {imgid}' - hdr['history'] = f'Flux calibration time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Flux calibration time {tnow.isoformat()}' def update_flux_limits(header, pixlims, wcs=None, ref=1): diff --git a/src/megaradrp/processing/slitflat.py b/src/megaradrp/processing/slitflat.py index 32f736d..e377090 100644 --- a/src/megaradrp/processing/slitflat.py +++ b/src/megaradrp/processing/slitflat.py @@ -28,4 +28,5 @@ def __init__(self, slitflat, datamodel=None, calibid='calibid-unknown', def header_update(self, hdr, imgid): hdr['NUM-SLTF'] = self.calibid hdr['history'] = f'Slit flat correction {imgid}' - hdr['history'] = f'Slit flat correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Slit flat correction time {tnow.isoformat()}' diff --git a/src/megaradrp/processing/tests/test_modelmap.py b/src/megaradrp/processing/tests/test_modelmap.py index 9ed79a5..418ccf7 100644 --- a/src/megaradrp/processing/tests/test_modelmap.py +++ b/src/megaradrp/processing/tests/test_modelmap.py @@ -76,7 +76,7 @@ def test_2(): def test_calc1(): - import scipy.sparse.csr + import scipy.sparse model = GaussBoxModelDescription().model_cls g_mean = 100 + 6 * np.arange(623) g_std = 1 + np.zeros_like(g_mean) @@ -86,4 +86,4 @@ def test_calc1(): params['mean'] = g_mean params['stddev'] = g_std wm = calc_matrix(wshape, model, params, valid) - assert isinstance(wm, scipy.sparse.csr.csr_matrix) + assert isinstance(wm, scipy.sparse.csr_matrix) diff --git a/src/megaradrp/processing/trimover.py b/src/megaradrp/processing/trimover.py index 1c865d9..6230603 100644 --- a/src/megaradrp/processing/trimover.py +++ b/src/megaradrp/processing/trimover.py @@ -7,7 +7,7 @@ # License-Filename: LICENSE.txt # -from __future__ import print_function, division +from __future__ import division import logging import datetime @@ -29,10 +29,9 @@ def trimOut(img, detconf, direction='normal', out='trimmed.fits'): """ - :param data: original data to be trimmed. Can be either a string , an ImageHDU or a numpy array + :param img: original data to be trimmed. Can be either a string , an ImageHDU or a numpy array :param direction: Can be either 'normal' or 'mirror' :param out: When the original data is a path, the returned value is a file too - :param bins: is the index of the _binning dictionary :return: """ @@ -92,8 +91,8 @@ def direcfun(x): return x return finaldata -def get_conf_value(confFile, key): - return confFile[key] +def get_conf_value(conf_file, key): + return conf_file[key] def apextract_weights(data, weights, size=4096): @@ -103,7 +102,7 @@ def apextract_weights(data, weights, size=4096): def decompress(tar_file): """ - :param tar_name: name of the tar file + :param tar_file: name of the tar file :return: None """ @@ -198,10 +197,10 @@ def __init__(self, detconf, datamodel=None, calibid='calibid-unknown', dtype='fl dtype=dtype) def data_binning(self, data, binning): - ''' + """ Axis:x --> factorX Axis:y --> factorY - ''' + """ factorX = 1.0 / binning[1] factorY = 1.0 / binning[0] x = int(factorY * data[0][0]) @@ -212,8 +211,6 @@ def data_binning(self, data, binning): return x, y, z, t def test_image(self): - import astropy.io.fits as fits - data = np.empty((4212, 4196), dtype='float32') data[self.pcol1] += 1 data[self.orow1] += 10 @@ -253,7 +250,8 @@ def run(self, img): hdr = img['primary'].header hdr['NUM-OVPE'] = self.calibid hdr['history'] = f'Overscan correction with {self.calibid}' - hdr['history'] = f'Overscan correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Overscan correction time {tnow.isoformat()}' # hdr['history'] = 'Mean of prescan1 is %f' % p1 hdr['history'] = f'Median of col overscan1 is {oc1}' hdr['history'] = "Overscan1 correction is spline3" @@ -320,7 +318,8 @@ def run(self, img): hdr = img['primary'].header hdr['NUM-TRIM'] = self.calibid hdr['history'] = f'Trimming correction with {self.calibid}' - hdr['history'] = f'Trimming correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Trimming correction time {tnow.isoformat()}' return img @@ -353,7 +352,8 @@ def run(self, img): img[0].data[part:] *= self.gain2 hdr['history'] = f'Gain correction with {self.calibid}' - hdr['history'] = f'Gain correction time {datetime.datetime.utcnow().isoformat()}' + tnow = datetime.datetime.now(datetime.UTC) + hdr['history'] = f'Gain correction time {tnow.isoformat()}' hdr['history'] = f'Gain1 correction value {self.gain1}' hdr['history'] = f'Gain2 correction value {self.gain2}' return img diff --git a/src/megaradrp/processing/twilight.py b/src/megaradrp/processing/twilight.py index 4e0953c..20e4152 100644 --- a/src/megaradrp/processing/twilight.py +++ b/src/megaradrp/processing/twilight.py @@ -1,5 +1,5 @@ # -# Copyright 2011-2021 Universidad Complutense de Madrid +# Copyright 2011-2023 Universidad Complutense de Madrid # # This file is part of Megara DRP # @@ -59,6 +59,6 @@ def run(self, img): def header_update(self, hdr, imgid): hdr['NUM-TWIF'] = self.calibid cap = self.flattag.capitalize() - now = datetime.datetime.utcnow().isoformat() + now = datetime.datetime.now(datetime.UTC).isoformat() hdr['history'] = f'{cap} flat correction {imgid}' hdr['history'] = f'{cap} flat correction time {now}' diff --git a/src/megaradrp/processing/wavecalibration.py b/src/megaradrp/processing/wavecalibration.py index 397b87d..4b20820 100644 --- a/src/megaradrp/processing/wavecalibration.py +++ b/src/megaradrp/processing/wavecalibration.py @@ -10,7 +10,7 @@ """Corrector for wavelength calibration""" import logging -import datetime +from datetime import datetime, UTC import warnings import uuid @@ -185,7 +185,7 @@ def calibrate_wl_rss(rss, solutionwl, npix, targetwcs, dtype='float32', span=0, hdr['history'] = f'Wavelength calibration with {solutionwl.calibid}' hdr['history'] = 'Aperture extraction offsets are {}'.format( solutionwl.global_offset.coef.tolist()) - hdr['history'] = f'Wavelength calibration time {datetime.datetime.utcnow().isoformat()}' + hdr['history'] = f'Wavelength calibration time {datetime.now(UTC).isoformat()}' hdr['history'] = f'Resample span={span}' # Update UUID hdr['UUID'] = str(uuid.uuid1()) diff --git a/src/megaradrp/recipes/calibration/bpm.py b/src/megaradrp/recipes/calibration/bpm.py index e8110fe..488b9fa 100644 --- a/src/megaradrp/recipes/calibration/bpm.py +++ b/src/megaradrp/recipes/calibration/bpm.py @@ -56,12 +56,12 @@ class BadPixelsMaskRecipe(MegaraBaseRecipe): def run(self, rinput): self.logger.info('start BPM recipe') - N = len(rinput.obresult.frames) + nframes = len(rinput.obresult.frames) - self.logger.debug('we have %d images', N) - half = N // 2 + self.logger.debug('we have %d images', nframes) + half = nframes // 2 flow = self.init_filters(rinput, rinput.obresult.configuration) - self.logger.debug('we have %d images', N) + self.logger.debug('we have %d images', nframes) reduced1 = basic_processing_with_combination_frames( rinput.obresult.frames[:half], flow, @@ -86,9 +86,8 @@ def run(self, rinput): self.set_base_headers(hdu.header) - hdu.header['history'] = 'BPM creation time {}'.format( - datetime.datetime.utcnow().isoformat() - ) + tnow = datetime.datetime.now(datetime.UTC) + hdu.header['history'] = 'BPM creation time {}'.format(tnow.isoformat()) for frame in rinput.obresult.frames: hdu.header['history'] = "With image {}".format( diff --git a/src/megaradrp/recipes/calibration/slitflat.py b/src/megaradrp/recipes/calibration/slitflat.py index aaaf1b8..9576399 100644 --- a/src/megaradrp/recipes/calibration/slitflat.py +++ b/src/megaradrp/recipes/calibration/slitflat.py @@ -7,12 +7,12 @@ # License-Filename: LICENSE.txt # -from __future__ import division, print_function +from __future__ import division import uuid import numpy -from scipy.ndimage.filters import median_filter +from scipy.ndimage import median_filter from astropy.io import fits from numina.array import combine from numina.core import Result diff --git a/src/megaradrp/recipes/calibration/trace.py b/src/megaradrp/recipes/calibration/trace.py index 63d9071..5658f57 100644 --- a/src/megaradrp/recipes/calibration/trace.py +++ b/src/megaradrp/recipes/calibration/trace.py @@ -9,7 +9,7 @@ """Fiber tracing Recipe.""" -from __future__ import division, print_function +from __future__ import division import logging import warnings @@ -27,7 +27,7 @@ from numina.array.wavecalib.crosscorrelation import convolve_comb_lines from skimage.filters import threshold_otsu from skimage.feature import peak_local_max -from scipy.ndimage.filters import minimum_filter +from scipy.ndimage import minimum_filter from numina.frame.utils import copy_img from megaradrp.processing.aperture import ApertureExtractor diff --git a/src/megaradrp/simulation/factory.py b/src/megaradrp/simulation/factory.py index 8f1d859..c77c250 100644 --- a/src/megaradrp/simulation/factory.py +++ b/src/megaradrp/simulation/factory.py @@ -9,9 +9,9 @@ """Simple monocromatic simulation""" -import uuid +import datetime import math -from datetime import datetime +import uuid import astropy.wcs as wcs import astropy.io.fits as fits @@ -153,7 +153,8 @@ def create_from_instrument(self, data, name, instrument, mode=''): # pheader['FILENAME'] = name # OBS mode pheader['OBSMODE'] = mode - pheader['DATE-OBS'] = datetime.utcnow().isoformat() + tnow = datetime.datetime.now(datetime.UTC) + pheader['DATE-OBS'] = tnow.isoformat() exptime = meta[instrument.name].get('exposed', 1.0) pheader['EXPTIME'] = exptime pheader['EXPOSED'] = exptime @@ -169,9 +170,10 @@ def create(self, data, name, control): pheader['OBSMODE'] = control.mode pheader['UUID'] = str(uuid.uuid4()) # Date of simulation - pheader['DATE'] = datetime.utcnow().isoformat() + tnow = datetime.datetime.now(datetime.UTC) + pheader['DATE'] = tnow.isoformat() # Date of simulated observation, not set yet - pheader['DATE-OBS'] = datetime.utcnow().isoformat() + pheader['DATE-OBS'] = tnow.isoformat() # Seqs metacontrol = control.config_info()