diff --git a/autonerves/fitsable.py b/autonerves/fitsable.py index 67dc162..16876eb 100644 --- a/autonerves/fitsable.py +++ b/autonerves/fitsable.py @@ -9,11 +9,6 @@ except ImportError: pass -try: - from astropy.io import fits -except ImportError: - pass - import numpy as np from pathlib import Path from typing import Dict, Optional, Union, List @@ -58,8 +53,10 @@ def hdu_list_for_output_from( ext_name_list=["data", "noise_map"] ) """ + from astropy.io import fits + hdu_list = [] - + header = fits.Header() if header_dict is not None: @@ -207,6 +204,8 @@ def ndarray_via_fits_from( -------- array_2d = ndarray_via_fits_from(file_path='/path/to/file/filename.fits', hdu=0) """ + from astropy.io import fits + with fits.open( file_path, do_not_scale_image_data=do_not_scale_image_data ) as hdu_list: @@ -235,6 +234,8 @@ def header_obj_from(file_path: Union[Path, str], hdu: int) -> Dict: -------- array_2d = ndarray_via_fits_from(file_path='/path/to/file/filename.fits', hdu=0) """ + from astropy.io import fits + with fits.open(file_path) as hdu_list: return hdu_list[hdu].header diff --git a/autonerves/workspace.py b/autonerves/workspace.py index 2cd621e..34fa932 100644 --- a/autonerves/workspace.py +++ b/autonerves/workspace.py @@ -17,6 +17,19 @@ class WorkspaceVersionMismatchError(exc.ConfigException): # enough to suggest the clone is genuinely stale. _STALENESS_WINDOW_DAYS = 30 +# Every library init (autofit, autogalaxy, autolens, ...) calls check_version, +# so without dedup a byte-identical warning prints once per library in the +# import chain. Python's own warning registry does not dedupe here because +# third-party imports between the calls invalidate it. +_warned_messages = set() + + +def _warn_once(message): + if message in _warned_messages: + return + _warned_messages.add(message) + warnings.warn(message) + def _read_general_yaml(workspace_root): """ @@ -224,7 +237,7 @@ def check_version(library_version, workspace_root=None): if floor_version is None or floor_version == "": if _is_source_checkout(root): return - warnings.warn(_missing_version_warning(root, library_version)) + _warn_once(_missing_version_warning(root, library_version)) return if floor_version == library_version: @@ -234,7 +247,7 @@ def check_version(library_version, workspace_root=None): library_parsed = _parse_version(library_version) if floor_parsed is None or library_parsed is None: - warnings.warn( + _warn_once( _unparseable_mismatch_message(floor_version, library_version, root) ) return @@ -252,4 +265,4 @@ def check_version(library_version, workspace_root=None): and library_date is not None and (library_date - floor_date).days > _STALENESS_WINDOW_DAYS ): - warnings.warn(_stale_workspace_message(floor_version, library_version, root)) + _warn_once(_stale_workspace_message(floor_version, library_version, root))