Skip to content

Commit

Permalink
Fix test errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaarnio committed Oct 9, 2023
1 parent 86da0c9 commit 4ddfd4b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 21 deletions.
6 changes: 3 additions & 3 deletions eis_toolkit/exploratory_analyses/k_means_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ def _k_means_clustering(
# The elbow method
k_max = 10
inertia = np.array(
[KMeans(n_clusters=k, random_state=0).fit(coordinates).inertia_ for k in range(1, k_max + 1)]
[KMeans(n_clusters=k, random_state=0, n_init=10).fit(coordinates).inertia_ for k in range(1, k_max + 1)]
)

inertia = np.diff(inertia, 2)
scaled_derivatives = [i * 100 for i in inertia]
k_optimal = scaled_derivatives.index(min(scaled_derivatives))

kmeans = KMeans(n_clusters=k_optimal, random_state=random_state)
kmeans = KMeans(n_clusters=k_optimal, random_state=random_state, n_init=10)

else:
kmeans = KMeans(n_clusters=number_of_clusters, random_state=random_state)
kmeans = KMeans(n_clusters=number_of_clusters, random_state=random_state, n_init=10)

kmeans.fit(coordinates)
data["cluster"] = kmeans.labels_
Expand Down
4 changes: 1 addition & 3 deletions tests/conversions/raster_to_dataframe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np
import pandas as pd
import pytest
import rasterio

from eis_toolkit.conversions.raster_to_dataframe import raster_to_dataframe
Expand All @@ -11,7 +10,6 @@
test_dir = Path(__file__).parent.parent


@pytest.mark.skip
def test_raster_to_dataframe():
"""Test raster to pandas conversion by converting pandas dataframe and then back to raster data."""
raster = rasterio.open(SMALL_RASTER_PATH)
Expand All @@ -32,7 +30,7 @@ def test_raster_to_dataframe():
"""Convert back to raster image."""
df["id"] = df.index
long_df = pd.wide_to_long(df, ["band_"], i="id", j="band").reset_index()
long_df.loc[:, ["col", "row"]] = long_df.loc[:, ["col", "row"]].astype(int)
long_df = long_df.astype({"col": int, "row": int})
raster_img = np.empty((multiband_raster.count, multiband_raster.height, multiband_raster.width))
raster_img[(long_df.band - 1).to_list(), long_df.row.to_list(), long_df.col.to_list()] = long_df.band_

Expand Down
1 change: 0 additions & 1 deletion tests/exploratory_analyses/k_means_cluster_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
gdf = gdp.GeoDataFrame(df, geometry=gdp.points_from_xy(df.Longitude, df.Latitude), crs="EPSG:4326")


@pytest.mark.skip
def test_k_means_clustering_output():
"""Test that k-means function assings data points into correct clusters."""
kmeans_gdf = k_means_clustering(data=gdf, number_of_clusters=2, random_state=0)
Expand Down
14 changes: 0 additions & 14 deletions tests/statistical_analyses/descriptive_statistics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ def test_descriptive_statistics_dataframe():
np.testing.assert_almost_equal(test["skew"], 1.6136246)


def test_zero_values_column():
"""Test column with all values set to 0."""
test = descriptive_statistics_dataframe(test_zero_values, "random_number")
np.testing.assert_almost_equal(test["min"], 0)
np.testing.assert_almost_equal(test["max"], 0)
np.testing.assert_almost_equal(test["mean"], 0)
np.testing.assert_almost_equal(test["25%"], 0)
np.testing.assert_almost_equal(test["50%"], 0)
np.testing.assert_almost_equal(test["75%"], 0)
np.testing.assert_almost_equal(test["standard_deviation"], 0)
assert pd.isna(test["relative_standard_deviation"]) is True
assert pd.isna(test["skew"]) is True


def test_invalid_column_name_df():
"""Test that invalid column name raises exception."""
with pytest.raises(InvalidColumnException):
Expand Down

0 comments on commit 4ddfd4b

Please sign in to comment.