def test_fingerprint_similarity_parallel(test_method, expected_score):
    """Test score matrix with known values for the provided methods."""
    spectrum0 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array(
        [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array(
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarity(similarity_measure=test_method)
    score_matrix = similarity_measure.matrix([spectrum0, spectrum1, spectrum2],
                                             [spectrum0, spectrum1, spectrum2])
    expected_matrix = numpy.array([[1., expected_score], [expected_score, 1.]])
    assert score_matrix[1:, 1:] == pytest.approx(
        expected_matrix, 0.001), "Expected different values."
    assert numpy.all(numpy.isnan(score_matrix[:,
                                              0])), "Expected 'nan' entries."
    assert numpy.all(numpy.isnan(
        score_matrix[0, :])), "Expected 'nan' entries."
def test_fingerprint_similarity_parallel_cosine_set_empty_to_0():
    """Test cosine score matrix with known values. Set non-exising values to 0."""
    spectrum0 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array(
        [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array(
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarity(set_empty_scores=0,
                                               similarity_measure="cosine")
    score_matrix = similarity_measure.matrix([spectrum0, spectrum1, spectrum2],
                                             [spectrum0, spectrum1, spectrum2])
    assert score_matrix == pytest.approx(
        numpy.array([[0, 0, 0], [0, 1., 0.84515425], [0, 0.84515425, 1.]]),
        0.001), "Expected different values."
def test_fingerprint_similarity_with_scores_sorting():
    """Test if score works with Scores.scores_by_query and sorting."""
    spectrum0 = Spectrum(mz=numpy.array([100.0, 101.0], dtype="float"),
                         intensities=numpy.array([0.4, 0.5], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array(
        [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([100.0, 101.0], dtype="float"),
                         intensities=numpy.array([0.4, 0.5], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array(
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([100.0, 101.0], dtype="float"),
                         intensities=numpy.array([0.4, 0.5], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarity(set_empty_scores=0,
                                               similarity_measure="cosine")

    scores = calculate_scores([spectrum0, spectrum1, spectrum2],
                              [spectrum0, spectrum1, spectrum2],
                              similarity_measure)

    scores_by_ref_sorted = scores.scores_by_query(spectrum1, sort=True)
    expected_scores = numpy.array([1.0, 0.84515425, 0.0])
    assert numpy.allclose(numpy.array([x[1] for x in scores_by_ref_sorted]), expected_scores, atol=1e-6), \
        "Expected different scores and/or order."
Esempio n. 4
0
def create_dummy_scores_symmetric():
    spectrums = create_dummy_spectrums()

    # Create Scores object by calculating dice scores
    similarity_measure = FingerprintSimilarity("dice")
    scores = calculate_scores(spectrums, spectrums, similarity_measure)
    return scores
def test_fingerprint_similarity_pair_calculations(test_method, expected_score):
    """Test cosine score pair with two fingerprint."""
    fingerprint1 = numpy.array(
        [1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array(
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarity(similarity_measure=test_method)
    score_pair = similarity_measure.pair(spectrum1, spectrum2)
    assert score_pair == pytest.approx(expected_score,
                                       1e-6), "Expected different score."
Esempio n. 6
0
def create_dummy_scores():
    spectrums = create_dummy_spectrums()
    references = spectrums[:5]
    queries = spectrums[5:]

    # Create Scores object by calculating dice scores
    similarity_measure = FingerprintSimilarity("dice")
    scores = calculate_scores(references, queries, similarity_measure)
    return scores
def test_fingerprint_similarity_parallel_empty_fingerprint(test_method):
    """Test score matrix with empty fingerprint using the provided methods."""
    fingerprint1 = numpy.array(
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array(
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarity(similarity_measure=test_method)
    score_matrix = similarity_measure.matrix([spectrum1, spectrum2],
                                             [spectrum1, spectrum2])
    assert score_matrix == pytest.approx(numpy.array([[0, 0], [0, 1.]]),
                                         0.001), "Expected different values."