本文整理汇总了Python中skbio.stats.distance.DissimilarityMatrix类的典型用法代码示例。如果您正苦于以下问题:Python DissimilarityMatrix类的具体用法?Python DissimilarityMatrix怎么用?Python DissimilarityMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DissimilarityMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_from_iterable_with_key_and_keys
def test_from_iterable_with_key_and_keys(self):
iterable = (x for x in range(4))
with self.assertRaises(ValueError):
DissimilarityMatrix.from_iterable(iterable,
lambda a, b: abs(b - a),
key=str,
keys=['1', '2', '3', '4'])
示例2: test_filter_asymmetric
def test_filter_asymmetric(self):
# 2x2
ids = ['b', 'a']
exp = DissimilarityMatrix([[0, -2], [1, 0]], ids)
obs = self.dm_2x2_asym.filter(ids)
self.assertEqual(obs, exp)
# 3x3
dm = DissimilarityMatrix([[0, 10, 53], [42, 0, 22.5], [53, 1, 0]],
('bro', 'brah', 'breh'))
ids = ['breh', 'brah']
exp = DissimilarityMatrix([[0, 1], [22.5, 0]], ids)
obs = dm.filter(ids)
self.assertEqual(obs, exp)
示例3: test_filter_subset
def test_filter_subset(self):
ids = ('c', 'a')
exp = DissimilarityMatrix([[0, 4.2], [4.2, 0]], ids)
obs = self.dm_3x3.filter(ids)
self.assertEqual(obs, exp)
ids = ('b', 'a')
exp = DissimilarityMatrix([[0, 0.01], [0.01, 0]], ids)
obs = self.dm_3x3.filter(ids)
self.assertEqual(obs, exp)
# 4x4
dm = DissimilarityMatrix([[0, 1, 55, 7], [1, 0, 16, 1],
[55, 16, 0, 23], [7, 1, 23, 0]])
ids = np.asarray(['3', '0', '1'])
exp = DissimilarityMatrix([[0, 7, 1], [7, 0, 1], [1, 1, 0]], ids)
obs = dm.filter(ids)
self.assertEqual(obs, exp)
示例4: test_from_iterable_asymmetric_data
def test_from_iterable_asymmetric_data(self):
iterable = (x for x in range(4))
exp = DissimilarityMatrix([[0, 1, 2, 3],
[-1, 0, 1, 2],
[-2, -1, 0, 1],
[-3, -2, -1, 0]])
res = DissimilarityMatrix.from_iterable(iterable, lambda a, b: b - a)
self.assertEqual(res, exp)
示例5: test_from_iterable_non_hollow_data
def test_from_iterable_non_hollow_data(self):
iterable = (x for x in range(4))
exp = DissimilarityMatrix([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
res = DissimilarityMatrix.from_iterable(iterable, lambda a, b: 1)
self.assertEqual(res, exp)
示例6: setUp
def setUp(self):
super(DissimilarityMatrixTests, self).setUp()
self.dm_1x1 = DissimilarityMatrix(self.dm_1x1_data, ['a'])
self.dm_2x2 = DissimilarityMatrix(self.dm_2x2_data, ['a', 'b'])
self.dm_2x2_asym = DissimilarityMatrix(self.dm_2x2_asym_data,
['a', 'b'])
self.dm_3x3 = DissimilarityMatrix(self.dm_3x3_data, ['a', 'b', 'c'])
self.dms = [self.dm_1x1, self.dm_2x2, self.dm_2x2_asym, self.dm_3x3]
self.dm_shapes = [(1, 1), (2, 2), (2, 2), (3, 3)]
self.dm_sizes = [1, 4, 4, 9]
self.dm_transposes = [
self.dm_1x1, self.dm_2x2,
DissimilarityMatrix([[0, -2], [1, 0]], ['a', 'b']), self.dm_3x3]
self.dm_redundant_forms = [np.array(self.dm_1x1_data),
np.array(self.dm_2x2_data),
np.array(self.dm_2x2_asym_data),
np.array(self.dm_3x3_data)]
示例7: test_plot_no_default
def test_plot_no_default(self):
ids = ['0', 'one', '2', 'three', '4.000']
data = ([0, 1, 2, 3, 4], [1, 0, 1, 2, 3], [2, 1, 0, 1, 2],
[3, 2, 1, 0, 1], [4, 3, 2, 1, 0])
dm = DissimilarityMatrix(data, ids)
fig = dm.plot(cmap='Reds', title='Testplot')
self.assertIsInstance(fig, mpl.figure.Figure)
axes = fig.get_axes()
self.assertEqual(len(axes), 2)
ax = axes[0]
self.assertEqual(ax.get_title(), 'Testplot')
xticks = []
for tick in ax.get_xticklabels():
xticks.append(tick.get_text())
self.assertEqual(xticks, ['0', 'one', '2', 'three', '4.000'])
yticks = []
for tick in ax.get_yticklabels():
yticks.append(tick.get_text())
self.assertEqual(yticks, ['0', 'one', '2', 'three', '4.000'])
示例8: test_io
def test_io(self):
# Very basic check that read/write public API is present and appears to
# be functioning. Roundtrip from memory -> disk -> memory and ensure
# results match.
fh = StringIO()
self.dm_3x3.write(fh)
fh.seek(0)
deserialized = DissimilarityMatrix.read(fh)
self.assertEqual(deserialized, self.dm_3x3)
self.assertTrue(type(deserialized) == DissimilarityMatrix)
示例9: test_from_iterable_no_key
def test_from_iterable_no_key(self):
iterable = (x for x in range(4))
exp = DissimilarityMatrix([[0, 1, 2, 3],
[1, 0, 1, 2],
[2, 1, 0, 1],
[3, 2, 1, 0]])
res = DissimilarityMatrix.from_iterable(iterable,
lambda a, b: abs(b - a))
self.assertEqual(res, exp)
示例10: test_from_iterable_with_key
def test_from_iterable_with_key(self):
iterable = (x for x in range(4))
exp = DissimilarityMatrix([[0, 1, 2, 3],
[1, 0, 1, 2],
[2, 1, 0, 1],
[3, 2, 1, 0]], ['0', '1', '4', '9'])
res = DissimilarityMatrix.from_iterable(iterable,
lambda a, b: abs(b - a),
key=lambda x: str(x ** 2))
self.assertEqual(res, exp)
示例11: test_init_from_dm
def test_init_from_dm(self):
ids = ['foo', 'bar', 'baz']
# DissimilarityMatrix -> DissimilarityMatrix
exp = DissimilarityMatrix(self.dm_3x3_data, ids)
obs = DissimilarityMatrix(self.dm_3x3, ids)
self.assertEqual(obs, exp)
# Test that copy of data is not made.
self.assertTrue(obs.data is self.dm_3x3.data)
obs.data[0, 1] = 424242
self.assertTrue(np.array_equal(obs.data, self.dm_3x3.data))
# DistanceMatrix -> DissimilarityMatrix
exp = DissimilarityMatrix(self.dm_3x3_data, ids)
obs = DissimilarityMatrix(
DistanceMatrix(self.dm_3x3_data, ('a', 'b', 'c')), ids)
self.assertEqual(obs, exp)
# DissimilarityMatrix -> DistanceMatrix
with self.assertRaises(DistanceMatrixError):
DistanceMatrix(self.dm_2x2_asym, ['foo', 'bar'])
示例12: test_from_iterable_skbio_hamming_metric_with_metadata
def test_from_iterable_skbio_hamming_metric_with_metadata(self):
# test for #1254
seqs = [
Sequence('ACGT'),
Sequence('ACGA', metadata={'id': 'seq1'}),
Sequence('AAAA', metadata={'id': 'seq2'}),
Sequence('AAAA', positional_metadata={'qual': range(4)})
]
exp = DissimilarityMatrix([
[0, 0.25, 0.75, 0.75],
[0.25, 0.0, 0.5, 0.5],
[0.75, 0.5, 0.0, 0.0],
[0.75, 0.5, 0.0, 0.0]], ['a', 'b', 'c', 'd'])
dm = DissimilarityMatrix.from_iterable(
seqs,
metric=skbio.sequence.distance.hamming,
keys=['a', 'b', 'c', 'd'])
self.assertEqual(dm, exp)
示例13: DissimilarityMatrixTests
class DissimilarityMatrixTests(DissimilarityMatrixTestData):
def setUp(self):
super(DissimilarityMatrixTests, self).setUp()
self.dm_1x1 = DissimilarityMatrix(self.dm_1x1_data, ['a'])
self.dm_2x2 = DissimilarityMatrix(self.dm_2x2_data, ['a', 'b'])
self.dm_2x2_asym = DissimilarityMatrix(self.dm_2x2_asym_data,
['a', 'b'])
self.dm_3x3 = DissimilarityMatrix(self.dm_3x3_data, ['a', 'b', 'c'])
self.dms = [self.dm_1x1, self.dm_2x2, self.dm_2x2_asym, self.dm_3x3]
self.dm_shapes = [(1, 1), (2, 2), (2, 2), (3, 3)]
self.dm_sizes = [1, 4, 4, 9]
self.dm_transposes = [
self.dm_1x1, self.dm_2x2,
DissimilarityMatrix([[0, -2], [1, 0]], ['a', 'b']), self.dm_3x3]
self.dm_redundant_forms = [np.array(self.dm_1x1_data),
np.array(self.dm_2x2_data),
np.array(self.dm_2x2_asym_data),
np.array(self.dm_3x3_data)]
def test_deprecated_io(self):
fh = StringIO()
npt.assert_warns(UserWarning, self.dm_3x3.to_file, fh)
fh.seek(0)
deserialized = npt.assert_warns(UserWarning,
DissimilarityMatrix.from_file, fh)
self.assertEqual(deserialized, self.dm_3x3)
self.assertTrue(type(deserialized) == DissimilarityMatrix)
def test_init_from_dm(self):
ids = ['foo', 'bar', 'baz']
# DissimilarityMatrix -> DissimilarityMatrix
exp = DissimilarityMatrix(self.dm_3x3_data, ids)
obs = DissimilarityMatrix(self.dm_3x3, ids)
self.assertEqual(obs, exp)
# Test that copy of data is not made.
self.assertTrue(obs.data is self.dm_3x3.data)
obs.data[0, 1] = 424242
self.assertTrue(np.array_equal(obs.data, self.dm_3x3.data))
# DistanceMatrix -> DissimilarityMatrix
exp = DissimilarityMatrix(self.dm_3x3_data, ids)
obs = DissimilarityMatrix(
DistanceMatrix(self.dm_3x3_data, ('a', 'b', 'c')), ids)
self.assertEqual(obs, exp)
# DissimilarityMatrix -> DistanceMatrix
with self.assertRaises(DistanceMatrixError):
DistanceMatrix(self.dm_2x2_asym, ['foo', 'bar'])
def test_init_no_ids(self):
exp = DissimilarityMatrix(self.dm_3x3_data, ('0', '1', '2'))
obs = DissimilarityMatrix(self.dm_3x3_data)
self.assertEqual(obs, exp)
self.assertEqual(obs['1', '2'], 12.0)
def test_init_invalid_input(self):
# Empty data.
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix([], [])
# Another type of empty data.
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix(np.empty((0, 0)), [])
# Invalid number of dimensions.
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix([1, 2, 3], ['a'])
# Dimensions don't match.
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix([[1, 2, 3]], ['a'])
data = [[0, 1], [1, 0]]
# Duplicate IDs.
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix(data, ['a', 'a'])
# Number of IDs don't match dimensions.
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix(data, ['a', 'b', 'c'])
# Non-hollow.
data = [[0.0, 1.0], [1.0, 0.01]]
with self.assertRaises(DissimilarityMatrixError):
DissimilarityMatrix(data, ['a', 'b'])
def test_data(self):
for dm, exp in zip(self.dms, self.dm_redundant_forms):
obs = dm.data
self.assertTrue(np.array_equal(obs, exp))
with self.assertRaises(AttributeError):
self.dm_3x3.data = 'foo'
def test_ids(self):
obs = self.dm_3x3.ids
#.........这里部分代码省略.........
示例14: print
#!/usr/bin/env python
from __future__ import print_function
from skbio.stats.distance import DissimilarityMatrix
from os import path
import sys
for distfile in sys.argv[1:]:
prefix = path.splitext(distfile)[0]
print(prefix)
dist = DissimilarityMatrix.read(distfile)
fig = dist.plot(title=prefix)
fig.tight_layout()
fig.savefig("{prefix}.pdf".format(prefix=prefix))
示例15: test_from_iterable_single
def test_from_iterable_single(self):
exp = DissimilarityMatrix([[100]])
res = DissimilarityMatrix.from_iterable(["boo"], lambda a, b: 100)
self.assertEqual(res, exp)