Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in how a image group name is determined #8426

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
===================
Expand All @@ -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]
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions jwst/tweakreg/tests/test_tweakreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down
52 changes: 29 additions & 23 deletions jwst/tweakreg/tweakreg_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"""
from os import path
import uuid

from astropy.table import Table
from astropy import units as u
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -511,7 +513,7 @@ def process(self, input):
)

return images


def _write_catalog(self, image_model, catalog, filename):
'''
Expand Down Expand Up @@ -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):
Expand Down
Loading