Skip to content

Commit

Permalink
Now tile dataframe is created using a dict not pd.concat, reducing RA…
Browse files Browse the repository at this point in the history
…M usage
  • Loading branch information
AmbroxMr committed Jul 15, 2024
1 parent 09024e3 commit 3a8c821
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
9 changes: 3 additions & 6 deletions src/landcoverpy/utilities/raster.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import math

from itertools import compress
from pathlib import Path
Expand Down Expand Up @@ -53,12 +52,13 @@ def _read_raster(
"""

if window is not None and (mask_geometry is not None or path_to_disk is not None):
print("If a window is provided, the raster can't be saved to disk or cropped")
raise ValueError("If a window is provided, mask_geometry and path_to_disk must be None")

band_name = _get_raster_name_from_path(str(band_path))
print(f"Reading raster {band_name}")

with rasterio.open(band_path) as band_file:


spatial_resolution = _get_spatial_resolution_raster(band_path)
if window is not None and spatial_resolution != 10:
# Transform the window to the actual resolution of the raster
Expand Down Expand Up @@ -100,7 +100,6 @@ def _read_raster(
path_to_disk = path_to_disk[:-3] + "tif"

if normalize_range is not None:
print(f"Normalizing band {band_name}")
value1, value2 = normalize_range
band = _normalize(band, value1, value2)

Expand All @@ -111,7 +110,6 @@ def _read_raster(
# Create a temporal memory file to mask the band
# This is necessary because the band is previously read to scale its resolution
if mask_geometry:
print(f"Cropping raster {band_name}")
projected_geometry = _project_shape(mask_geometry, dcs=destination_crs)
with rasterio.io.MemoryFile() as memfile:
with memfile.open(**kwargs) as memfile_band:
Expand Down Expand Up @@ -430,7 +428,6 @@ def _rescale_band(
rescaled_raster = np.ndarray(
shape=(kwargs["count"], new_kwargs["height"], new_kwargs["width"]), dtype=np.float32)

print(f"Rescaling raster {band_name}, from: {img_resolution}m to {str(spatial_resol)}.0m")
reproject(
source=band,
destination=rescaled_raster,
Expand Down
24 changes: 11 additions & 13 deletions src/landcoverpy/workflow_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def _process_tile_predict(tile, execution_mode, used_columns=None, use_block_win
rasters_by_season = defaultdict(dict)

for season, products_metadata in product_per_season.items():
print(season)
bucket_products = settings.MINIO_BUCKET_NAME_PRODUCTS
bucket_composites = settings.MINIO_BUCKET_NAME_COMPOSITES
current_bucket = None
Expand Down Expand Up @@ -218,7 +217,6 @@ def _process_tile_predict(tile, execution_mode, used_columns=None, use_block_win
temp_product_folder = Path(settings.TMP_DIR, product_name + ".SAFE")
if not temp_product_folder.exists():
Path.mkdir(temp_product_folder)
print(f"Processing product {product_name}")

rasters_by_season[season]["raster_paths"] = rasters_paths
rasters_by_season[season]["is_band"] = is_band
Expand Down Expand Up @@ -249,8 +247,8 @@ def _process_tile_predict(tile, execution_mode, used_columns=None, use_block_win
window_kwargs["height"] = window.height
window_kwargs["transform"] = rasterio.windows.transform(window, kwargs_s2["transform"])

# Dataframe for storing data of a window
window_tile_df = None
# Dict for storing data of a window (all rasters and indexes)
window_tile_dict = {}

crop_mask = np.zeros(shape=(int(window_kwargs["height"]), int(window_kwargs["width"])), dtype=np.uint8)

Expand Down Expand Up @@ -306,9 +304,7 @@ def _process_tile_predict(tile, execution_mode, used_columns=None, use_block_win
raster_masked = np.ma.masked_array(raster[0], mask=crop_mask)
raster_masked = np.ma.compressed(raster_masked)

window_raster_df = pd.DataFrame({f"{season}_{raster_name}": raster_masked})

window_tile_df = pd.concat([window_tile_df, window_raster_df], axis=1)
window_tile_dict.update({f"{season}_{raster_name}": raster_masked})

for dem_name in dems_raster_names:
# Add dem and aspect data
Expand All @@ -333,13 +329,9 @@ def _process_tile_predict(tile, execution_mode, used_columns=None, use_block_win
)
raster_masked = np.ma.masked_array(raster, mask=crop_mask)
raster_masked = np.ma.compressed(raster_masked).flatten()
window_raster_df = pd.DataFrame({dem_name: raster_masked})
window_tile_df = pd.concat([window_tile_df, window_raster_df], axis=1)


window_tile_dict.update({dem_name: raster_masked})

print("Dataframe information:")
print(window_tile_df.info())
window_tile_df = pd.DataFrame(window_tile_dict)

if execution_mode == ExecutionMode.LAND_COVER_PREDICTION:

Expand Down Expand Up @@ -404,13 +396,19 @@ def _process_tile_predict(tile, execution_mode, used_columns=None, use_block_win
) as classification_file:
classification_file.write(encoded_sl_predictions, window=window)

print(f"Window {window} processed, output raster {classification_path} updated")

minio_client.fput_object(
bucket_name=settings.MINIO_BUCKET_CLASSIFICATIONS,
object_name=f"{settings.MINIO_DATA_FOLDER_NAME}/{classification_name}",
file_path=classification_path,
content_type="image/tif",
)

print(f"Classification raster {classification_name} uploaded to Minio")
print(f"Tile {tile} processed")
print("Cleaning up temporary files")

for path in Path(settings.TMP_DIR).glob("**/*"):
if path.is_file():
path.unlink()
Expand Down

0 comments on commit 3a8c821

Please sign in to comment.