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

Moved SOM shape attribute to SomGrid

parent b033d3ea
No related branches found
No related tags found
No related merge requests found
# Licensed under the terms of the BSD-3-Clause license. # Licensed under the terms of the BSD-3-Clause license.
# Copyright (C) 2019 Michael Blaß # Copyright (C) 2019 Michael Blaß
# mblass@posteo.net # mblass@posteo.net
import collections
from typing import Dict, List, Optional, Tuple from typing import Dict, List, Optional, Tuple
import numpy as np import numpy as np
...@@ -16,7 +15,11 @@ from .. types import Array, Shape, Coord ...@@ -16,7 +15,11 @@ from .. types import Array, Shape, Coord
class SomGrid: class SomGrid:
def __init__(self, shape: Shape): def __init__(self, shape: Shape) -> None:
if not all(isinstance(val, int) and val >= 1 for val in shape):
raise ValueError('Dimensions must be integer > 0.')
self.shape = shape
self.pos = np.asarray(list(np.ndindex(shape)), dtype=int) self.pos = np.asarray(list(np.ndindex(shape)), dtype=int)
self.tree = cKDTree(self.pos) self.tree = cKDTree(self.pos)
self.rows, self.cols = np.indices(shape) self.rows, self.cols = np.indices(shape)
...@@ -63,12 +66,8 @@ class SomBase: ...@@ -63,12 +66,8 @@ class SomBase:
nhr: float, nh_shape: str, init_distr: str, metric: str, nhr: float, nh_shape: str, init_distr: str, metric: str,
seed: Optional[float] = None): seed: Optional[float] = None):
# check dimensions self._grid = SomGrid(dims[:2])
for d in dims: self.n_features = dims[2]
if not isinstance(d, int) or not d >= 1:
raise ValueError('Dimensions must be integer > 0.')
self._dims = dims
self._hit_counts = np.zeros(self.n_units) self._hit_counts = np.zeros(self.n_units)
self.n_iter = n_iter self.n_iter = n_iter
self.metric = metric self.metric = metric
...@@ -110,28 +109,27 @@ class SomBase: ...@@ -110,28 +109,27 @@ class SomBase:
raise ValueError(f'Unknown initializer "{init_distr}". Use' raise ValueError(f'Unknown initializer "{init_distr}". Use'
'"uniform", "simplex", or "pca".') '"uniform", "simplex", or "pca".')
self._grid = SomGrid(self.shape)
self._dists: Optional[Array] = None self._dists: Optional[Array] = None
@property @property
def dims(self) -> Tuple[int, int, int]: def dims(self) -> Tuple[int, int, int]:
"""Return the SOM dimensions.""" """Return the SOM dimensions."""
return self._dims return (*self._grid.shape, self.n_features)
@property @property
def dx(self) -> int: def dx(self) -> int:
"""Return the number of units along the first dimension.""" """Return the number of units along the first dimension."""
return self.dims[0] return self._grid.shape[0]
@property @property
def dy(self) -> int: def dy(self) -> int:
"""Return the number of units along the second dimension.""" """Return the number of units along the second dimension."""
return self.dims[1] return self._grid.shape[1]
@property @property
def dw(self) -> int: def dw(self) -> int:
"""Return the dimension of the weight vectors.""" """Return the dimension of the weight vectors."""
return self.dims[2] return self.n_features
@property @property
def n_units(self) -> int: def n_units(self) -> int:
...@@ -141,7 +139,7 @@ class SomBase: ...@@ -141,7 +139,7 @@ class SomBase:
@property @property
def shape(self) -> Shape: def shape(self) -> Shape:
"""Return the map shape.""" """Return the map shape."""
return (self.dx, self.dy) return self._grid.shape
@property @property
def grid(self) -> Array: def grid(self) -> Array:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment