UPDATE
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
from datetime import date
|
||||
import os
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
from django.test import TestCase
|
||||
|
||||
from location_data.data_driven_subdivision import (
|
||||
EmptyObservationDatasetError,
|
||||
_persist_remote_sensing_diagnostic_artifacts,
|
||||
build_clustering_dataset,
|
||||
sync_block_subdivision_with_result,
|
||||
)
|
||||
@@ -137,6 +142,78 @@ class DataDrivenSubdivisionSyncTests(TestCase):
|
||||
self.subdivision.metadata["data_driven_subdivision"]["cluster_count"],
|
||||
2,
|
||||
)
|
||||
self.assertIn("diagnostic_artifacts", self.subdivision.metadata["data_driven_subdivision"])
|
||||
|
||||
def test_persist_remote_sensing_diagnostic_artifacts_saves_expected_images(self):
|
||||
cell = AnalysisGridCell.objects.create(
|
||||
soil_location=self.location,
|
||||
block_subdivision=self.subdivision,
|
||||
block_code="block-1",
|
||||
cell_code="cell-1",
|
||||
chunk_size_sqm=900,
|
||||
geometry=self.boundary,
|
||||
centroid_lat="35.689200",
|
||||
centroid_lon="51.389200",
|
||||
)
|
||||
observation = AnalysisGridObservation.objects.create(
|
||||
cell=cell,
|
||||
run=self.run,
|
||||
temporal_start=date(2025, 1, 1),
|
||||
temporal_end=date(2025, 1, 31),
|
||||
ndvi=0.5,
|
||||
ndwi=0.2,
|
||||
soil_vv_db=-8.0,
|
||||
)
|
||||
result = RemoteSensingSubdivisionResult.objects.create(
|
||||
soil_location=self.location,
|
||||
run=self.run,
|
||||
block_subdivision=self.subdivision,
|
||||
block_code="block-1",
|
||||
chunk_size_sqm=900,
|
||||
temporal_start=date(2025, 1, 1),
|
||||
temporal_end=date(2025, 1, 31),
|
||||
cluster_count=1,
|
||||
selected_features=["ndvi", "ndwi", "soil_vv_db"],
|
||||
metadata={"inertia_curve": [{"k": 1, "sse": 0.0}]},
|
||||
)
|
||||
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
with patch.dict(os.environ, {"REMOTE_SENSING_DIAGNOSTIC_DIR": temp_dir}, clear=False), patch(
|
||||
"location_data.data_driven_subdivision.render_elbow_plot",
|
||||
return_value=ContentFile(b"elbow"),
|
||||
), patch(
|
||||
"location_data.data_driven_subdivision._render_cluster_map_plot",
|
||||
return_value=ContentFile(b"map"),
|
||||
), patch(
|
||||
"location_data.data_driven_subdivision._render_cluster_size_plot",
|
||||
return_value=ContentFile(b"sizes"),
|
||||
), patch(
|
||||
"location_data.data_driven_subdivision._render_feature_pair_plot",
|
||||
return_value=ContentFile(b"pairs"),
|
||||
):
|
||||
artifacts = _persist_remote_sensing_diagnostic_artifacts(
|
||||
result=result,
|
||||
observations=[observation],
|
||||
labels=[0],
|
||||
cluster_summaries=[
|
||||
{
|
||||
"cluster_label": 0,
|
||||
"cell_count": 1,
|
||||
"centroid_lat": 35.6892,
|
||||
"centroid_lon": 51.3892,
|
||||
"cell_codes": ["cell-1"],
|
||||
}
|
||||
],
|
||||
selected_features=["ndvi", "ndwi", "soil_vv_db"],
|
||||
scaled_matrix=[[0.0, 0.0, 0.0]],
|
||||
inertia_curve=[{"k": 1, "sse": 0.0}],
|
||||
)
|
||||
self.assertEqual(
|
||||
sorted(artifacts["files"].keys()),
|
||||
["cluster_map", "cluster_sizes", "elbow_plot", "feature_pairs"],
|
||||
)
|
||||
for path in artifacts["files"].values():
|
||||
self.assertTrue(os.path.exists(path))
|
||||
|
||||
def test_build_clustering_dataset_raises_clear_error_when_all_selected_features_are_null(self):
|
||||
cell = AnalysisGridCell.objects.create(
|
||||
@@ -164,7 +241,7 @@ class DataDrivenSubdivisionSyncTests(TestCase):
|
||||
):
|
||||
build_clustering_dataset(
|
||||
observations=[observation],
|
||||
selected_features=["ndvi", "ndwi", "lst_c", "soil_vv_db"],
|
||||
selected_features=["ndvi", "ndwi", "soil_vv_db"],
|
||||
run=self.run,
|
||||
location=self.location,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user