Esempio n. 1
0
class FrozenSimilarityMatrixInMemory(object):
    def __init__(self):
        self.matrix_fn = tmpname()
        self.matrix = FrozenSimilarityMatrix(self.matrix_fn, 'a', driver='H5FD_CORE', driver_core_backing_store=0)

    def __enter__(self):
        return self.matrix

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def close(self):
        self.matrix.close()
        if os.path.isfile(self.matrix_fn):
            os.remove(self.matrix_fn)
Esempio n. 2
0
 def setUp(self):
     self.pair_matrix_fn = tmpname()
     self.pair_matrix = SimilarityMatrix(self.pair_matrix_fn, "a", driver="H5FD_CORE", driver_core_backing_store=0)
     labels = {"a": 0, "b": 1, "c": 2, "d": 3}
     similarities = [("a", "b", 0.9), ("a", "c", 0.5), ("b", "c", 0.6), ("d", "c", 0.7)]
     self.pair_matrix.update(similarities, labels)
     self.matrix_fn = tmpname()
     self.matrix = FrozenSimilarityMatrix(self.matrix_fn, "a", driver="H5FD_CORE", driver_core_backing_store=0)
Esempio n. 3
0
class FrozenSimilarityMatrixInMemory(object):
    def __init__(self):
        self.matrix_fn = tmpname()
        self.matrix = FrozenSimilarityMatrix(self.matrix_fn,
                                             'a',
                                             driver='H5FD_CORE',
                                             driver_core_backing_store=0)

    def __enter__(self):
        return self.matrix

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def close(self):
        self.matrix.close()
        if os.path.isfile(self.matrix_fn):
            os.remove(self.matrix_fn)
Esempio n. 4
0
def open_similarity_matrix(fn):
    """Open read-only similarity matrix file.

    Args:
        fn (str): Filename of similarity matrix

    Returns:
        SimilarityMatrix | FrozenSimilarityMatrix: A read-only similarity matrix object

    """
    # peek in file to detect format
    f = tables.open_file(fn, 'r')
    is_frozen = 'scores' in f.root
    f.close()
    if is_frozen:
        matrix = FrozenSimilarityMatrix(fn)
    else:
        matrix = SimilarityMatrix(fn, cache_labels=True)
    return matrix
Esempio n. 5
0
 def __init__(self):
     self.matrix_fn = tmpname()
     self.matrix = FrozenSimilarityMatrix(self.matrix_fn,
                                          'a',
                                          driver='H5FD_CORE',
                                          driver_core_backing_store=0)
Esempio n. 6
0
 def __init__(self):
     self.matrix_fn = tmpname()
     self.matrix = FrozenSimilarityMatrix(self.matrix_fn, 'a', driver='H5FD_CORE', driver_core_backing_store=0)
Esempio n. 7
0
class TestFrozenSimilarityMatrix(object):
    pair_matrix_fn = None
    pair_matrix = None
    matrix_fn = None
    matrix = None

    def setUp(self):
        self.pair_matrix_fn = tmpname()
        self.pair_matrix = SimilarityMatrix(self.pair_matrix_fn, "a", driver="H5FD_CORE", driver_core_backing_store=0)
        labels = {"a": 0, "b": 1, "c": 2, "d": 3}
        similarities = [("a", "b", 0.9), ("a", "c", 0.5), ("b", "c", 0.6), ("d", "c", 0.7)]
        self.pair_matrix.update(similarities, labels)
        self.matrix_fn = tmpname()
        self.matrix = FrozenSimilarityMatrix(self.matrix_fn, "a", driver="H5FD_CORE", driver_core_backing_store=0)

    def tearDown(self):
        self.pair_matrix.close()
        if os.path.isfile(self.pair_matrix_fn):
            os.remove(self.pair_matrix_fn)
        self.matrix.close()
        if os.path.isfile(self.matrix_fn):
            os.remove(self.matrix_fn)

    def test_from_pairs_defaults(self):
        self.matrix.from_pairs(self.pair_matrix, 10)

        result = self.matrix.to_pandas()
        labels = ["a", "b", "c", "d"]
        expected = pd.DataFrame(
            [[0.0, 0.9, 0.5, 0.0], [0.9, 0.0, 0.6, 0.0], [0.5, 0.6, 0.0, 0.7], [0.0, 0.0, 0.7, 0.0]],
            index=labels,
            columns=labels,
        )
        pdt.assert_almost_equal(result, expected)

    def test_from_pairs_multiframe(self):
        self.matrix.from_pairs(self.pair_matrix, 1, None, False)

        result = self.matrix.to_pandas()
        labels = ["a", "b", "c", "d"]
        expected = pd.DataFrame(
            [[0.0, 0.9, 0.5, 0.0], [0.9, 0.0, 0.6, 0.0], [0.5, 0.6, 0.0, 0.7], [0.0, 0.0, 0.7, 0.0]],
            index=labels,
            columns=labels,
        )
        pdt.assert_almost_equal(result, expected)

    def test_from_pairs_limited(self):
        self.matrix.from_pairs(self.pair_matrix, 1, 1, False)

        result = self.matrix.to_pandas()
        labels = ["a", "b", "c", "d"]
        expected = pd.DataFrame(
            [[0.0, 0.9, 0.0, 0.0], [0.9, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]],
            index=labels,
            columns=labels,
        )
        pdt.assert_almost_equal(result, expected)

    def test_from_pairs_singlesided(self):
        self.matrix.from_pairs(self.pair_matrix, 10, None, True)

        result = self.matrix.to_pandas()
        print(result)
        labels = ["a", "b", "c", "d"]
        expected = pd.DataFrame(
            [[0.0, 0.9, 0.5, 0.0], [0.0, 0.0, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.7, 0.0]],
            index=labels,
            columns=labels,
        )
        pdt.assert_almost_equal(result, expected)

    def test_find_defaults(self):
        self.matrix.from_pairs(self.pair_matrix, 10)

        hits = self.matrix.find("c", 0.55)
        expected = [("d", 0.7), ("b", 0.6)]
        eq_(hits, expected)

    def test_find_limit(self):
        self.matrix.from_pairs(self.pair_matrix, 10)

        hits = self.matrix.find("c", 0.55, 1)
        expected = [("d", 0.7)]
        eq_(hits, expected)

    def test_find_cutoffhigh_nohits(self):
        self.matrix.from_pairs(self.pair_matrix, 10)

        hits = self.matrix.find("c", 0.9)
        expected = []
        eq_(hits, expected)

    def test_find_badkey_keyerror(self):
        self.matrix.from_pairs(self.pair_matrix, 10)

        with assert_raises(KeyError):
            self.matrix.find("f", 0.45)

    def test_find_singlesided(self):
        self.matrix.from_pairs(self.pair_matrix, 10, None, True)
        print(self.matrix.scores.read())
        hits = self.matrix.find("c", 0.0)
        expected = []
        eq_(hits, expected)