Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_transform(n_rows, n_columns, X):
som_clustering = susi.SOMClustering(
n_rows=n_rows, n_columns=n_columns)
som_clustering.fit(X)
bmus = som_clustering.transform(X)
assert(len(bmus) == X.shape[0])
assert(len(bmus[0]) == 2)
def test_fit(X, n_rows, n_columns, train_mode_unsupervised, random_state,
expected):
som = susi.SOMClustering(
n_rows=n_rows,
n_columns=n_columns,
train_mode_unsupervised=train_mode_unsupervised,
random_state=random_state)
som.fit(X)
assert isinstance(som.unsuper_som_, np.ndarray)
assert som.unsuper_som_.shape == (n_rows, n_columns, X.shape[1])
assert np.allclose(som.unsuper_som_, expected, atol=1e-20)
with pytest.raises(Exception):
som = susi.SOMClustering(train_mode_unsupervised="alsdkf")
som.fit(X)
def test_som_clustering_init(n_rows, n_columns):
som_clustering = susi.SOMClustering(
n_rows=n_rows, n_columns=n_columns)
assert som_clustering.n_rows == n_rows
assert som_clustering.n_columns == n_columns
def test_get_bmus(som_array, X, n_jobs, expected):
som_clustering = susi.SOMClustering(n_jobs=n_jobs)
assert np.array_equal(som_clustering.get_bmus(X, som_array), expected)
def test_get_datapoints_from_node(n_rows, n_columns, som_array, X, node,
expected):
som = susi.SOMClustering(n_rows=n_rows, n_columns=n_columns)
som.set_bmus(X, som_array)
assert(np.array_equal(som.get_datapoints_from_node(node), expected))
def test_init_unsuper_som(X, init_mode):
som_clustering = susi.SOMClustering(init_mode_unsupervised=init_mode)
som_clustering.X_ = X
if init_mode in ["random", "random_data", "pca"]:
som_clustering.init_unsuper_som()
# test type
assert isinstance(som_clustering.unsuper_som_, np.ndarray)
# test shape
n_rows = som_clustering.n_rows
n_columns = som_clustering.n_columns
assert som_clustering.unsuper_som_.shape == (n_rows, n_columns, X.shape[1])
else:
with pytest.raises(Exception):
som_clustering.init_unsuper_som()
def test_calc_learning_rate(learning_rate_start, learning_rate_end, max_it,
curr_it, mode, expected):
som_clustering = susi.SOMClustering(
learning_rate_start=learning_rate_start,
learning_rate_end=learning_rate_end)
som_clustering.max_iterations_ = max_it
assert som_clustering.calc_learning_rate(curr_it, mode) == expected
def test_get_node_distance_matrix(datapoint, som_array, distance_metric,
expected):
som_clustering = susi.SOMClustering()
som_clustering.distance_metric = distance_metric
som_clustering.X_ = np.array([datapoint, datapoint])
som_clustering.n_rows = som_array.shape[0]
som_clustering.n_columns = som_array.shape[1]
som_clustering.init_unsuper_som()
assert np.allclose(som_clustering.get_node_distance_matrix(
datapoint, som_array), expected, rtol=1e-2)
def test_get_nbh_distance_weight_matrix(n_rows, n_columns, random_state,
neighborhood_func, bmu_pos, X,
mode, expected):
som_clustering = susi.SOMClustering(
n_rows=n_rows, n_columns=n_columns,
nbh_dist_weight_mode=mode, random_state=random_state)
som_clustering.X_ = X
som_clustering.init_unsuper_som()
print(som_clustering.get_nbh_distance_weight_matrix(
neighborhood_func, bmu_pos)
)
print(expected)
assert np.allclose(som_clustering.get_nbh_distance_weight_matrix(
neighborhood_func, bmu_pos), expected, atol=1e-8)
def test_fit_transform(n_rows, n_columns, X):
som_clustering = susi.SOMClustering(
n_rows=n_rows, n_columns=n_columns)
bmus = som_clustering.fit_transform(X)
assert(len(bmus) == X.shape[0])
assert(len(bmus[0]) == 2)