Repository navigation
[FIX] pyOpenMS: ion mobility unit accessors mixed the two IM representations (#9792) - #9809
Merged
Merged
Conversation
…tations (#9792) MSSpectrum stores ion mobility two different ways. A spectrum acquired at a single drift time keeps its unit in the scalar drift_time_unit_ member; a frame carrying one mobility per peak keeps it in the *name* of a FloatDataArray, from which containsIMData()/getIMData() derive it. get_drift_time_unit() guarded on the per-peak array but returned the scalar, so it answered DriftTimeUnit.NONE for a frame whose array was perfectly well labelled, and None -- "no ion mobility here" -- for a spectrum whose scalar unit was set, which is the very field it returned. It was correct only when neither representation was present. Concretely, that made it unstable across a file round-trip on real TIMS data. BrukerTimsFile::frameToSpectrum sets the scalar unit to VSSC and attaches a VSSC array but never sets a scalar drift time, and mzML writes the scalar unit only when the time is set. The same frame therefore reported VSSC in memory and NONE after a store/load, with the array byte-identical throughout. It is replaced by get_drift_time_array_unit(), array-derived throughout and belonging to the get_drift_time_array()/get_drift_time_array_view() family. get_drift_time_unit() remains for one release as a deprecated alias with the corrected behaviour; its name was in any case ambiguous against the drift_time_unit scalar property sitting next to it. The scalar accessors and properties are unchanged and continue to report the scalar. Where a vendor reader populates both representations they can legitimately disagree, and each is now reported honestly rather than one masquerading as the other. DriftTimeUnit.NONE and Python None stay distinct: NONE means an array was found but its name did not pin down a unit, None means there is no array. The same defect appeared twice more, from one cause: the array-derived unit was reachable only as a bare int inside a tuple, so callers reached instead for the scalar accessor that returns a proper enum or string. - MSSpectrum.to_df()/get_data_dict() took the ion_mobility values from the array selected by getIMData() but the ion_mobility_unit column from getDriftTimeUnitAsString(), so the column read '<NONE>' for exactly the frames it exists to describe. It now uses IMTypes.driftTimeUnitToString() on the unit returned by the same getIMData() call. - The getIMData() binding downcast the DriftTimeUnit enum to a plain int. It now returns the enum, which removes the incentive that produced the other two. That last one is an API change, but a narrow one: DriftTimeUnit is bound with nb::is_arithmetic(), so equality against an int, int(), isinstance(x, int), hashing and dict lookup all still work; only str()/repr() and type() identity differ. No in-tree consumer used the value at all -- the three that unpack it discard it. 21 regression tests covering all five combinations of the two representations plus the mzML round-trip; 12 fail against the previous behaviour. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughChangesIon mobility unit reporting
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Contributor
Author
|
still not great but that is how they are called in the lib |
This was referenced Jul 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Spun out of the discussion on #9808. Independent of that PR and based on
develop.The defect
MSSpectrumstores ion mobility two different ways, and a spectrum may carry either — or, on some vendor readers, both:drift_time_/drift_time_unit_, viagetDriftTime()/getDriftTimeUnit());FloatDataArraywhose name is a PSI-MS CV term, viacontainsIMData()/getIMData().get_drift_time_unit()guarded on the array but returned the scalar:Measured against a build, it was correct only when neither representation was present:
DriftTimeUnit.NONEVSSCNoneNoneVSSC(coincidence)VSSCMILLISECONDVSSCNoneNoneThe second row is the sharpest: it returned
None, meaning "no ion mobility data", for a spectrum whose drift time unit is set and is exactly the field it returns.Why it mattered in practice, not just on paper
BrukerTimsFile::frameToSpectrumsets the scalar unit to VSSC and attaches a VSSC array but never sets a scalar drift time (BrukerTimsFile.cpp:1548,:1583), and mzML writes the scalar unit only when the time is set. So on real TIMS data the accessor answered differently before and after a save/load, with the array unchanged:That round-trip is now a regression test.
The fix
get_drift_time_array_unit()— array-derived throughout, belonging to theget_drift_time_array()/get_drift_time_array_view()family.get_drift_time_unit()stays for one release as a deprecated alias with the corrected behaviour, following the existingdeprecated_mv_aliases.pymechanism; its name was in any case ambiguous against thedrift_time_unitscalar property sitting right next to it.The scalar accessors are untouched and still report the scalar. Where both representations are populated they can legitimately disagree, and each is now reported honestly instead of one masquerading as the other.
DriftTimeUnit.NONEand PythonNonestay distinct:NONEmeans an array was found but its name did not pin down a unit;Nonemeans there is no array.The same defect twice more, from one cause
The array-derived unit was reachable only as a bare
intinside a tuple, so callers reached instead for the scalar accessor that returns a proper enum or string:to_df()/get_data_dict()took theion_mobilityvalues from the array selected bygetIMData()but theion_mobility_unitcolumn fromgetDriftTimeUnitAsString()— so that column read'<NONE>'for exactly the frames it exists to describe. It now usesIMTypes.driftTimeUnitToString()on the unit from the samegetIMData()call. ('<NONE>'→'1/K0'.)getIMData()downcast the enum toint. It now returnsDriftTimeUnit, which removes the incentive that produced the other two.API change
getIMData()[1]changes frominttoDriftTimeUnit. Narrow, becauseDriftTimeUnitis bound withnb::is_arithmetic()— I measured it:unit == 2,unit in (1,2),int(unit)type(unit) is intisinstance(unit, int),hash(), dict lookup,json.dumpsstr(unit)→"DriftTimeUnit.VSSC"not"2"No in-tree consumer used the value: the three sites that unpack it discard it (
addons/msexperiment.py:477,:556,addons/msspectrum.py:89), andtest000.pyreads only the index.Tests
21 in
test_ion_mobility_unit.py, covering all five combinations of the two representations, the enum/int interop, the deprecated alias, the DataFrame column in both agreeing and disagreeing cases, and the mzML round-trip. 12 fail against the previous behaviour. Full pyOpenMS suite: 5324 passed, 0 failed.test_MSSpectrum.py:375previously assertedget_drift_time_unit() is not None, which passes while the value is literallyNONE— that is how this survived. It now asserts the actual unit.Not addressed here
IMDataConverter::getIMUnit()infers the unit from the CV term's allowed units and tries milliseconds first, rather than reading the array's actualunit_accessionwhich mzML does preserve; the term permits seconds too, so a seconds-valued array is reported as milliseconds. That is a core defect and wants its own fix — a binding-level workaround would be wrong.Design reviewed with Codex, which independently reached the same array-derived conclusion and contributed the round-trip argument, the
get_drift_time_array_unitnaming, and theNONE-vs-Nonedistinction.Summary by CodeRabbit
get_drift_time_array_unit()to report ion mobility units associated with per-peak data.getIMData()now returns a typedDriftTimeUnitenum.get_drift_time_unit()remains available temporarily as a deprecated alias.