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