Beispiel #1
0
def test_cosine_score_swapped(random_spectrum, another_random_spectrum,
                              mz_tolerance, min_matched_peaks):
    """Cosine scores should not change if comparing spectrum 1 with spectrum 2
        or spectrum 2 with spectrum 1."""

    mz1, data1 = random_spectrum
    mz2, data2 = another_random_spectrum

    cosine = cosine_score(mz1, data1, mz2, data2, mz_tolerance,
                          min_matched_peaks)
    cosine_swapped = cosine_score(mz2, data2, mz1, data1, mz_tolerance,
                                  min_matched_peaks)
    assert pytest.approx(cosine) == cosine_swapped
Beispiel #2
0
def test_query_analog(db):
    """Build an analog and try to find the original spectrum in the database.
    """
    
    p, (mzs, spectra) = db
    
    for i, (mz, data) in enumerate(zip(mzs, spectra)):
        mz = mzs[i] - 50
        data = spectra[i].copy()
        data[:, MZ] = data[:, MZ] - 50
        
        filtered_analog = filter_data(mz, data, 0, 17, 50, 6)
        filtered_orig = filter_data(mzs[i], spectra[i], 0, 17, 50, 6)
        score = cosine_score(mzs[i], filtered_orig,
                             mz, filtered_analog,
                             0.02, 0)
        results = query(str(p), [i], [mz], [filtered_analog], [],
                        0.02, 0, 0, 17, 50, 6, 0., 100.)
        assert i in results
        seen_i = False
        for r in results[i]:
            assert 'id' in r
            assert 'bank_id' in r
            assert 'name' in r
            assert 'score' in r
                
            if r['id'] == i:
                seen_i = True
                assert r['score'] == pytest.approx(score)
        assert seen_i
 def get_cosine(self, params):
     parent_mass = [p[0].parent_mass for p in params]
     spectrum = [
         p[0].fragmolspectrum_set.get(energy=p[1]).spectrum for p in params
     ]
     # filter_data(data, mz_parent, min_intensity, parent_filter_tolerance, matched_peaks_window,min_matched_peaks_search)
     spectrum = [
         filter_data(
             np.array(spectrum[i]),
             parent_mass[i],
             self.frag_compare_conf.filter_min_intensity,
             self.frag_compare_conf.filter_parent_filter_tolerance,
             self.frag_compare_conf.filter_matched_peaks_window,
             self.frag_compare_conf.filter_min_matched_peaks_search,
         ) for i in range(2)
     ]
     # cosine_score(spectrum1_mz, spectrum1_data, spectrum2_mz, spectrum2_data, mz_tolerance, min_matched_peaks)
     return cosine_score(
         parent_mass[0],
         spectrum[0],
         parent_mass[1],
         spectrum[1],
         self.frag_compare_conf.cosine_mz_tolerance,
         self.frag_compare_conf.cosine_min_matched_peaks,
     )
Beispiel #4
0
def test_cosine_score_identity(random_spectrum, mz_tolerance,
                               min_matched_peaks):
    """Cosine scores between one spectra and itself should always be 1."""

    mz, data = random_spectrum

    cosine = cosine_score(mz, data, mz, data, mz_tolerance, min_matched_peaks)
    assert pytest.approx(cosine) == 1.0
Beispiel #5
0
def test_cosine_score_empty(random_spectrum, mz_tolerance, min_matched_peaks):
    """Empty spectra should result in a null cosine score"""

    mz, _ = random_spectrum

    data = np.empty((0, 2), dtype=np.float32)
    cosine = cosine_score(mz, data, mz, data, mz_tolerance, min_matched_peaks)
    assert pytest.approx(cosine) == 0.0
Beispiel #6
0
def test_cosine_score_known(known_cosines):
    """`cosine_score` should return `expected` values"""

    mz1, mz2, data1, data2, mz_tolerance, \
        min_matched_peaks, expected = known_cosines

    cosine = cosine_score(mz1, data1, mz2, data2, mz_tolerance,
                          min_matched_peaks)
    assert pytest.approx(cosine, rel=1e-4) == expected
Beispiel #7
0
def test_cosine_score_python_cython(random_spectrum, another_random_spectrum,
                                    mz_tolerance, min_matched_peaks):
    """Cythonized `cosine_score` and it's fallback Python version should give
        the same results.
    """

    args = (*random_spectrum, *another_random_spectrum, mz_tolerance,
            min_matched_peaks)
    cosine_p = cosine_score.__wrapped__(*args)
    cosine_c = cosine_score(*args)
    assert pytest.approx(cosine_p) == cosine_c