Skip to content
Snippets Groups Projects
Commit b7ddb3ff authored by Blaß, Michael's avatar Blaß, Michael :speech_balloon:
Browse files

Updated umatrix and test.

parent 8b3c9e2e
Branches
Tags
No related merge requests found
...@@ -289,10 +289,14 @@ class SomBase: ...@@ -289,10 +289,14 @@ class SomBase:
for i, nhd_idx in enumerate(nhd_per_unit): for i, nhd_idx in enumerate(nhd_per_unit):
cwv = self._weights[[i]] cwv = self._weights[[i]]
nhd = self._weights[nhd_idx] nhd = self._weights[nhd_idx]
u_height[i] = distance.cdist(cwv, nhd).sum() u_height[i] = distance.cdist(cwv, nhd, self.metric).sum()
if scale: if scale:
u_height[i] /= len(nhd_idx) u_height[i] /= len(nhd_idx)
if norm: if norm:
umax = u_height.max()
if umax == 0:
u_height = np.zeros_like(u_height)
else:
u_height /= u_height.max() u_height /= u_height.max()
return u_height.reshape(self.shape) return u_height.reshape(self.shape)
......
...@@ -9,7 +9,7 @@ import scipy as sp ...@@ -9,7 +9,7 @@ import scipy as sp
from apollon.som.som import SomBase, SomGrid from apollon.som.som import SomBase, SomGrid
SomDim = Tuple[int, int, int] SomDim = Tuple[int, int, int]
dimension = hst.integers(min_value=1, max_value=100) dimension = hst.integers(min_value=2, max_value=50)
som_dims = hst.tuples(dimension, dimension, dimension) som_dims = hst.tuples(dimension, dimension, dimension)
...@@ -67,23 +67,27 @@ class TestSomBase(unittest.TestCase): ...@@ -67,23 +67,27 @@ class TestSomBase(unittest.TestCase):
data = np.random.rand(100, dims[2]) data = np.random.rand(100, dims[2])
self.assertIsInstance(som.match(data), np.ndarray) self.assertIsInstance(som.match(data), np.ndarray)
@given(som_dims)
def test_umatrix_has_map_shape(self, dims: SomDim) -> None:
som = SomBase(dims, 100, 0.1, 10, 'gaussian', 'uniform', 'euclidean')
um = som.umatrix()
self.assertEqual(um.shape, som.shape)
""" @given(som_dims)
class TestSelfOrganizingMap(unittest.TestCase): def test_umatrix_scale(self, dims: SomDim) -> None:
def setUp(self): som = SomBase(dims, 100, 0.1, 10, 'gaussian', 'uniform', 'euclidean')
N = 100 som._weights = np.tile(np.arange(som.n_features), (som.n_units, 1))
som._weights[:, -1] = np.arange(som.n_units)
m1 = (0, 0) um = som.umatrix(scale=True, norm=False)
m2 = (10, 15) self.assertEqual(um[0, 0], um[-1, -1])
c1 = ((10, 0), (0, 10)) self.assertEqual(um[0, -1], um[-1, 0])
c2 = ((2, 0), (0, 2))
seg1 = np.random.multivariate_normal(m1, c1, N) @given(som_dims)
seg2 = np.random.multivariate_normal(m2, c2, N) def test_umatrix_norm(self, dims: SomDim) -> None:
som = SomBase(dims, 100, 0.1, 10, 'gaussian', 'uniform', 'euclidean')
um = som.umatrix(norm=True)
self.assertEqual(um.max(), 1.0)
self.data = np.vstack((seg1, seg2))
self.dims = (10, 10, 2)
"""
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment