From 71d91f1696d687543103ee373b164a15ce15223a Mon Sep 17 00:00:00 2001 From: Mihai Cara Date: Wed, 17 Apr 2024 03:55:02 -0400 Subject: [PATCH] Fix a bug in how a image group name is determined --- CHANGES.rst | 12 ++++--- jwst/tweakreg/tests/test_tweakreg.py | 29 ++++++++++++---- jwst/tweakreg/tweakreg_step.py | 52 ++++++++++++++++------------ 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index b912f10ceb..c3a2cd99db 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -16,7 +16,7 @@ assign_wcs associations ------------ -- Ensure NRS IFU exposures don't make a spec2 association for grating/filter combinations +- Ensure NRS IFU exposures don't make a spec2 association for grating/filter combinations where the nrs2 detector isn't illuminated. Remove dupes in mkpool. [#8395] - Match NIRSpec imprint observations to science exposures on mosaic tile location @@ -39,7 +39,7 @@ extract_1d general ------- -- Removed deprecated stdatamodels model types ``DrizProductModel``, +- Removed deprecated stdatamodels model types ``DrizProductModel``, ``MIRIRampModel``, and ``MultiProductModel``. [#8388] outlier_detection @@ -79,6 +79,8 @@ tweakreg - Output source catalog file now respects ``output_dir`` parameter. [#8386] +- Improved how a image group name is determined. [#8426] + 1.14.0 (2024-03-29) =================== @@ -94,7 +96,7 @@ ami - Additional optional input arguments for greater user processing flexibility. See documentation for details. [#7862] -- Bad pixel correction applied to data using new NRM reference file to calculate +- Bad pixel correction applied to data using new NRM reference file to calculate complex visibility support (M. Ireland method implemented by J. Kammerer). [#7862] - Make ``AmiAnalyze`` and ``AmiNormalize`` output conform to the OIFITS standard. [#7862] @@ -129,7 +131,7 @@ charge_migration as DO_NOT_USE. This group, and all subsequent groups, are then flagged as CHARGELOSS and DO_NOT_USE. The four nearest pixel neighbor are then flagged in the same group. [#8336] - + - Added warning handler for expected NaN and inf clipping in the ``sigma_clip`` function. [#8320] @@ -415,7 +417,7 @@ tweakreg - Fixed a bug that caused failures instead of warnings when no GAIA sources were found within the bounding box of the input image. [#8334] - + - Suppress AstropyUserWarnings regarding NaNs in the input data. [#8320] wfs_combine diff --git a/jwst/tweakreg/tests/test_tweakreg.py b/jwst/tweakreg/tests/test_tweakreg.py index a240fca281..2e8c9daf39 100644 --- a/jwst/tweakreg/tests/test_tweakreg.py +++ b/jwst/tweakreg/tests/test_tweakreg.py @@ -41,20 +41,21 @@ def test_is_wcs_correction_small(offset, is_good): @pytest.mark.parametrize( "groups, all_group_names, common_name", [ - ([['abc1_cal.fits', 'abc2_cal.fits']], [], ['abc']), + ([['abc1_cal.fits', 'abc2_cal.fits']], {}, ['abc #1']), ( [ ['abc1_cal.fits', 'abc2_cal.fits'], ['abc1_cal.fits', 'abc2_cal.fits'], ['abc1_cal.fits', 'abc2_cal.fits'], ['def1_cal.fits', 'def2_cal.fits'], + ['cba1_cal.fits', 'cba2_cal.fits'], ], - [], - ["abc", "abc1", "abc2", "def"], + {'cba': 1}, + ["abc #1", "abc #2", "abc #3", "def #1", "cba #2"], ), - ([['cba1_cal.fits', 'abc2_cal.fits']], [], ['Group #1']), - ([['cba1_cal.fits', 'abc2_cal.fits']], ['Group #1'], ['Group #2']), - ([['cba1_cal.fits', 'abc2_cal.fits']], None, ['Unnamed Group']), + ([['cba1_cal.fits', 'abc2_cal.fits']], {}, ['Group #1']), + ([['cba1_cal.fits', 'abc2_cal.fits']], {'Group': 1}, ['Group #2']), + ([['cba1_cal.fits', 'abc2_cal.fits']], None, ['Group #1']), ] ) def test_common_name(groups, all_group_names, common_name): @@ -69,6 +70,22 @@ def test_common_name(groups, all_group_names, common_name): assert cn == cn_truth +def test_common_name_special(): + all_group_names = {} + group = [] + cn = tweakreg_step._common_name(group, all_group_names) + assert cn == 'Group #1' + + group = [] + all_group_names = {'abc': 1, 'abc #2': 1} + for fname in ['abc1', 'abc2']: + model = ImageModel() + model.meta.filename = fname + group.append(model) + cn = tweakreg_step._common_name(group, all_group_names) + assert len(cn.rsplit('_', 1)[1]) == 6 + + def test_expected_failure_bad_starfinder(): model = ImageModel() diff --git a/jwst/tweakreg/tweakreg_step.py b/jwst/tweakreg/tweakreg_step.py index 8971010d45..ddaed78d54 100644 --- a/jwst/tweakreg/tweakreg_step.py +++ b/jwst/tweakreg/tweakreg_step.py @@ -5,6 +5,7 @@ """ from os import path +import uuid from astropy.table import Table from astropy import units as u @@ -109,6 +110,7 @@ class TweakRegStep(Step): reference_file_types = [] def process(self, input): + _common_name.gid = 0 use_custom_catalogs = self.use_custom_catalogs if use_custom_catalogs: @@ -294,8 +296,8 @@ def process(self, input): elif len(grp_img) > 1: # create a list of WCS-Catalog-Images Info and/or their Groups: imcats = [] - all_group_names = [] - for g in grp_img: + all_group_names = {} + for k, g in enumerate(grp_img): if len(g) == 0: raise AssertionError("Logical error in the pipeline code.") else: @@ -511,7 +513,7 @@ def process(self, input): ) return images - + def _write_catalog(self, image_model, catalog, filename): ''' @@ -614,31 +616,35 @@ def _imodel2wcsim(self, image_model): def _common_name(group, all_group_names=None): - file_names = [path.splitext(im.meta.filename)[0].strip('_- ') - for im in group] + file_names = [ + path.splitext(im.meta.filename)[0].strip('_- ') for im in group + ] cn = path.commonprefix(file_names) if all_group_names is None: - if not cn: - return 'Unnamed Group' + if cn: + return cn + else: + _common_name.gid += 1 + return f"Group #{_common_name.gid}" else: - if not cn or cn in all_group_names: - # find the smallest group number to make "Group #..." unique - max_id = 1 - if not cn: - cn = "Group #" - for name in all_group_names: - try: - cid = int(name.lstrip(cn)) - if cid >= max_id: - max_id = cid + 1 - except ValueError: - pass - cn = f"{cn}{max_id}" - all_group_names.append(cn) - - return cn + if not cn: + cn = "Group" + + grp_no = all_group_names.get(cn, 0) + 1 + grp_id = f"{cn} #{grp_no}" + if grp_id in all_group_names: + # just try some shortened uuid until it is unique: + while grp_id in all_group_names: + grp_id = f"{cn}_{str(uuid.uuid4())[-6:]}" + all_group_names[grp_id] = 0 + else: + all_group_names[cn] = grp_no + return grp_id + + +_common_name.gid = 0 def _parse_catfile(catfile):