def safe_read_key(spectrum: Spectrum, key: str) -> Optional[float]: """ Read key from spectrum and convert to float or return 'None'. Tries to read the given key from the spectrum metadata and convert it to a float. In case an exception is thrown or the key is not present, returns 'None'. Parameters ---------- spectrum: Spectrum from which to read the key. key: Key to be read from the spectrum metadata. Returns ------- Either the key's value converted to float or 'None'. """ value = spectrum.get(key, default=None) if value is not None: try: value = float(value) except ValueError: # RT is in format that can't be converted to float -> set rt to None value = None return value
def test_derive_formula_from_name_remove_formula_false(): spectrum_in = Spectrum(mz=numpy.array([], dtype="float"), intensities=numpy.array([], dtype="float"), metadata={"compound_name": "peptideXYZ [M+H+K] C5H12NO2"}) spectrum = derive_formula_from_name(spectrum_in, remove_formula_from_name=False) assert spectrum.get("formula") == "C5H12NO2", "Expected different formula." assert spectrum.get("compound_name") == spectrum_in.get("compound_name"), "Expected no name change."
def test_spectrum_getters_return_copies(): """Test if getters return (deep)copies so that edits won't change the original entries.""" spectrum = Spectrum(mz=numpy.array([100.0, 101.0], dtype="float"), intensities=numpy.array([0.4, 0.5], dtype="float"), metadata={"testdata": 1}) # Get entries and modify testdata = spectrum.get("testdata") testdata += 1 assert spectrum.get("testdata") == 1, "Expected different entry" peaks_mz = spectrum.peaks.mz peaks_mz += 100.0 assert numpy.all(spectrum.peaks.mz == numpy.array( [100.0, 101.0])), "Expected different peaks.mz" metadata = spectrum.metadata metadata["added_info"] = "this" assert spectrum.metadata == { 'testdata': 1 }, "Expected metadata to remain unchanged"
def test_derive_adduct_from_name_dont_remove_from_name(): spectrum_in = Spectrum(mz=numpy.array([], dtype="float"), intensities=numpy.array([], dtype="float"), metadata={"compound_name": "peptideXYZ [M+H+K]"}) spectrum = derive_adduct_from_name(spectrum_in, remove_adduct_from_name=False) assert spectrum.get("adduct") == "[M+H+K]", "Expected different adduct." assert spectrum.get("compound_name") == spectrum_in.get( "compound_name"), "Expected no change to name."
def test_if_spectrum_is_cloned(): """Test if filter is correctly cloning the input spectrum.""" mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([0, 1, 10, 100], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum = require_minimum_of_high_peaks(spectrum_in, no_peaks=2) spectrum.set("testfield", "test") assert not spectrum_in.get( "testfield"), "Expected input spectrum to remain unchanged."
def test_if_spectrum_is_cloned(): """Test if filter is correctly cloning the input spectrum.""" mz = numpy.array([], dtype="float") intensities = numpy.array([], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum = remove_peaks_outside_top_k(spectrum_in) spectrum.set("testfield", "test") assert not spectrum_in.get( "testfield"), "Expected input spectrum to remain unchanged."
def test_if_spectrum_is_cloned(): """Test if filter is correctly cloning the input spectrum.""" mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([0, 1, 10, 100], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum_in.set("precursor_mz", 1.) spectrum = require_precursor_mz(spectrum_in) spectrum.set("testfield", "test") assert not spectrum_in.get("testfield"), \ "Expected input spectrum to remain unchanged."