From 08ab51f76a9ea99208fcb013838684dcdae4e8c7 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 23 Oct 2026 09:11:46 -0700 Subject: [PATCH] fix(obslist): keep compound designations apart in name resolution An imported observing list's "M 2-9" (the Twin Jet, a Minkowski planetary nebula) resolved to M 29. resolve_by_name keyed the name index on ui_utils.normalize(), which strips spaces and hyphens, so a compound designation collapsed into another catalog's sequence. _name_key() keys it on the normalized name and its digit groups, so "M 2-9" (2, 9) stays apart from "M 29" (29). "Sh 2-71" and "Sh2 71" (2, 71), and "NGC 0497" and "NGC 497" (497,), still match. Against the committed database, this also stops "M 3-2" resolving to M 32, "M 1-1" to M 11, "NGC 650-1" to NGC 6501 and "Hu 1-2" to the double star Hu 12. It separates five pairs of names on different objects that normalize() collapsed, such as South 291 ("Sh 291") and Sharpless 91 ("Sh2 91"). One form stops matching: an unspaced name whose catalog code ends in a digit. "Sh2155" now has the digit groups (2155,), where the catalog's "Sh2 155" has (2, 155). Extracted from mrosseel's #379 (squash commit 954a83c46 on nixos) with his agreement. It is adapted to main, whose name index maps a name to the object, where nixos's maps it to a function that returns the object. Co-authored-by: mrosseel Co-Authored-By: Claude Opus 5.5 --- python/PiFinder/obslist.py | 36 +++++++++++++++++++--------- python/tests/test_obslist_resolve.py | 25 +++++++++++++------ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/python/PiFinder/obslist.py b/python/PiFinder/obslist.py index e1ed28020..4f11a452b 100644 --- a/python/PiFinder/obslist.py +++ b/python/PiFinder/obslist.py @@ -14,6 +14,7 @@ import os import logging +import re from PiFinder import utils from PiFinder.calc_utils import sf_utils from PiFinder.obj_types import OBJ_TYPES @@ -245,35 +246,48 @@ def _normalize_designation(name: str): return None +def _name_key(name: str) -> tuple: + """ + The name-index key of ``name``: the normalized name (ui_utils.normalize, + which ignores case, spaces and hyphens, with leading zeros removed) and + its digit groups as numbers. + The digit groups keep "M 2-9" (2, 9), a Minkowski nebula, apart from + "M 29" (29). "Sh 2-71" and "Sh2 71" both give (2, 71), and "NGC 0497" and + "NGC 497" both give (497,), so these still match. + """ + text = re.sub(r"\d+", lambda m: str(int(m.group())), normalize(name)) + return (text, tuple(int(d) for d in re.findall(r"\d+", name))) + + def _build_name_index(catalogs: Catalogs) -> dict: """ Map every catalog object's names to the object, for name-based resolution. - Names are normalized (ui_utils.normalize) so spacing/case variants like - "NGC 6205" and "NGC6205" collapse together. First writer wins, so a name - resolves to one stable object. + The key is _name_key, so spacing/case variants like "NGC 6205" and + "NGC6205" collapse together. First writer wins, so a name resolves to one + stable object. """ index: dict = {} for obj in catalogs.get_objects(only_selected=False, filtered=False): for nm in obj.names: - key = normalize(nm) - if key and key not in index: + key = _name_key(nm) + if key[0] and key not in index: index[key] = obj return index def resolve_by_name(name: str, name_index: dict): """ - Resolve an entry to a catalog object by normalized name (ui_utils.normalize: - case-, space- and hyphen-insensitive), also trying a constellation-normalized - variant ("VY Andromedae" -> "VY And"). Returns the shared catalog object or - None. + Resolve an entry to a catalog object by name (see _name_key: case-, + space- and hyphen-insensitive, but the digit groups must match), also + trying a constellation-normalized variant ("VY Andromedae" -> "VY And"). + Returns the shared catalog object or None. """ if not name: return None - keys = [normalize(name)] + keys = [_name_key(name)] designation = _normalize_designation(name) if designation: - keys.append(normalize(designation)) + keys.append(_name_key(designation)) for key in keys: obj = name_index.get(key) if obj: diff --git a/python/tests/test_obslist_resolve.py b/python/tests/test_obslist_resolve.py index 1b94ebc34..ff7436ce1 100644 --- a/python/tests/test_obslist_resolve.py +++ b/python/tests/test_obslist_resolve.py @@ -13,9 +13,8 @@ import pytest from PiFinder import obslist -from PiFinder.obslist import _normalize_designation, resolve_by_name +from PiFinder.obslist import _name_key, _normalize_designation, resolve_by_name from PiFinder.obslist_formats import ObsList, ObsListEntry -from PiFinder.ui.ui_utils import normalize @pytest.mark.unit @@ -40,26 +39,38 @@ def test_case_insensitive(self): @pytest.mark.unit class TestResolveByName: def test_exact_match(self): - index = {normalize("Andromeda Galaxy"): "M31", normalize("VY And"): "SaR7"} + index = {_name_key("Andromeda Galaxy"): "M31", _name_key("VY And"): "SaR7"} assert resolve_by_name("Andromeda Galaxy", index) == "M31" def test_normalized_match(self): - index = {normalize("VY And"): "SaR7"} + index = {_name_key("VY And"): "SaR7"} assert resolve_by_name("VY Andromedae", index) == "SaR7" def test_exact_preferred_over_normalized(self): - index = {normalize("VY Andromedae"): "EXACT", normalize("VY And"): "NORM"} + index = {_name_key("VY Andromedae"): "EXACT", _name_key("VY And"): "NORM"} assert resolve_by_name("VY Andromedae", index) == "EXACT" def test_no_match(self): - assert resolve_by_name("CGCS135", {normalize("VY And"): "SaR7"}) is None + assert resolve_by_name("CGCS135", {_name_key("VY And"): "SaR7"}) is None def test_spacing_insensitive(self): # A CSV "M 13" matches an object stored as "M13" (no space), and vice versa. - index = {normalize("M13"): "OBJ"} + index = {_name_key("M13"): "OBJ"} assert resolve_by_name("M 13", index) == "OBJ" assert resolve_by_name("M13", index) == "OBJ" + def test_a_minkowski_name_does_not_resolve_to_messier(self): + # "M 2-9" and "M 29" normalize to the same text; the digit groups + # keep them apart. + index = {_name_key("M 29"): "MESSIER", _name_key("M 2-9"): "PK"} + assert resolve_by_name("M 2-9", index) == "PK" + assert resolve_by_name("M 3-1", {_name_key("M 31"): "M31"}) is None + + def test_hyphen_and_space_variants_still_match(self): + index = {_name_key("Sh2 71"): "SH2", _name_key("NGC 497"): "N"} + assert resolve_by_name("Sh 2-71", index) == "SH2" + assert resolve_by_name("NGC 0497", index) == "N" + def test_empty_name(self): assert resolve_by_name("", {"x": 1}) is None