Skip to content

Candidates

rionid.candidates

Candidate selection and cached AME2020 mass-to-charge searches.

matches_selector(ion_name, selectors)

Match exact ions, an isotope in every charge state, or an element.

Source code in src/rionid/candidates.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def matches_selector(ion_name, selectors):
    """Match exact ions, an isotope in every charge state, or an element."""
    if not selectors:
        return False
    text = selectors if isinstance(selectors, str) else " ".join(selectors)
    ion = parse_ion(str(ion_name))
    return any(
        (kind == "ion" and value == ion.name)
        or (kind == "nuclide" and value == (ion.A, ion.element))
        or (kind == "element" and value == ion.element)
        for kind, value in _selectors(text)
    )

parse_ion(text) cached

Accept 205Au76+ and 205Au+76, requiring a physical positive charge.

A trailing Nubase isomer label (m, n, p, q, r, x) names an excited state, as in 180Hfm76+.

Source code in src/rionid/candidates.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@lru_cache(maxsize=8192)
def parse_ion(text: str) -> Ion:
    """Accept 205Au76+ and 205Au+76, requiring a physical positive charge.

    A trailing Nubase isomer label (m, n, p, q, r, x) names an excited state,
    as in 180Hfm76+.
    """
    match = re.fullmatch(r"(\d+)([A-Za-z]{1,3})(?:(\d+)\+|\+(\d+))", text.strip())
    if not match:
        raise ValueError(f"Invalid ion {text!r}; use an explicit charge, for example 205Au76+")
    mass, letters, q1, q2 = match.groups()
    q = int(q1 or q2)
    readings = [(letters.capitalize(), "")]
    if len(letters) > 1 and letters[-1].lower() in ISOMER_LABELS:
        readings.append((letters[:-1].capitalize(), letters[-1].lower()))
    known = [(e, i) for e, i in readings if e in ATOMIC_NUMBER and e != "n"]
    if not known:
        raise ValueError(f"Unknown element in {text!r}")
    for element, isomer in known:
        if int(mass) >= ATOMIC_NUMBER[element] and 1 <= q <= ATOMIC_NUMBER[element]:
            return Ion(int(mass), element, q, isomer)
    raise ValueError(f"Invalid mass number or charge in {text!r}; require 1 <= q <= Z")

search_moq(center_ion, tolerance_percent=1.0, charge_min=1, charge_max=None, ame=None)

Return every AME2020 ionic combination within an inclusive m/q interval.

Search includes AME extrapolations and labels them in the returned metadata. Entries are candidate ground states, without a production-yield assumption.

Source code in src/rionid/candidates.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def search_moq(center_ion, tolerance_percent=1.0, charge_min=1, charge_max=None, ame=None):
    """Return every AME2020 ionic combination within an inclusive m/q interval.

    Search includes AME extrapolations and labels them in the returned metadata.
    Entries are candidate ground states, without a production-yield assumption.
    """
    if not math.isfinite(tolerance_percent) or not 0 <= tolerance_percent < 100:
        raise ValueError("Mass-to-charge tolerance must be finite and in [0,100) percent")
    if not math.isfinite(charge_min) or charge_min < 1 or int(charge_min) != charge_min:
        raise ValueError("Minimum charge must be a positive integer")
    if charge_max is not None and (
        not math.isfinite(charge_max) or int(charge_max) != charge_max or charge_max < charge_min
    ):
        raise ValueError("Maximum charge must be an integer >= minimum charge")
    ame = ame or get_ame_data()
    center, _, mass = ion_from_ame(center_ion, ame)
    target = mass / center.q
    moqs, names, charges, rows = _catalogue(ame)
    low, high = target * (1 - tolerance_percent / 100), target * (1 + tolerance_percent / 100)
    first = np.searchsorted(moqs, low, side="left")
    last = np.searchsorted(moqs, high, side="right")
    chosen = np.arange(first, last)
    mask = charges[chosen] >= charge_min
    if charge_max is not None:
        mask &= charges[chosen] <= charge_max
    result = []
    for i in chosen[mask]:
        result.append(
            {
                "ion": str(names[i]),
                "moq_u_per_e": float(moqs[i]),
                "mass_u": float(moqs[i] * charges[i]),
                "Z": int(rows[i][4]),
                "offset_percent": float(100 * (moqs[i] / target - 1)),
                **mass_metadata(rows[i], "ame_search"),
            }
        )
    return result