|
| 1 | +"""Test esrun pipeline.""" |
| 2 | + |
| 3 | +import shutil |
| 4 | +from datetime import UTC, datetime |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import rasterio |
| 9 | +import shapely |
| 10 | +import yaml |
| 11 | +from rslearn.const import WGS84_PROJECTION |
| 12 | +from rslearn.utils.feature import Feature |
| 13 | +from rslearn.utils.geometry import STGeometry |
| 14 | +from rslearn.utils.vector_format import GeojsonVectorFormat |
| 15 | +from upath import UPath |
| 16 | + |
| 17 | +from rslp.esrun.esrun import esrun |
| 18 | + |
| 19 | + |
| 20 | +def test_esrun_solar_farm(tmp_path: Path) -> None: |
| 21 | + """Test ESRun pipeline by applying solar farm on small request geometry.""" |
| 22 | + # For now this is fixed but we should figure out how to have standardized path for |
| 23 | + # each application later, similar to RSLP_PREFIX. |
| 24 | + checkpoint_path = "gs://ai2-rslearn-projects-data/projects/2025_06_06_helios_finetuning/v2_satlas_solar_farm_128_ts_helios_per_mod_patchdisc_contrastive_fix_esrun/checkpoints/epoch=9999-step=99999.ckpt" |
| 25 | + |
| 26 | + # Copy the configuration files. We use the tmp_path as the config dir that we will |
| 27 | + # initialize from since we will customize the request geometry. |
| 28 | + src_dir = Path("esrun_data/satlas/solar_farm_oe/") |
| 29 | + config_dir = tmp_path / "config" |
| 30 | + config_dir.mkdir(parents=True) |
| 31 | + for fname in ["dataset.json", "model.yaml"]: |
| 32 | + with ( |
| 33 | + (src_dir / fname).open("rb") as src, |
| 34 | + (config_dir / fname).open("wb") as dst, |
| 35 | + ): |
| 36 | + shutil.copyfileobj(src, dst) |
| 37 | + |
| 38 | + # This request geometry should be at least 10% solar farm (and at most 50%). |
| 39 | + # It is centered at a solar farm and we extend beyond the solar farm. |
| 40 | + solar_farm_center = (-111.613, 33.267) |
| 41 | + request_geometry = STGeometry( |
| 42 | + WGS84_PROJECTION, |
| 43 | + shapely.box( |
| 44 | + solar_farm_center[0] - 0.01, |
| 45 | + solar_farm_center[1] - 0.01, |
| 46 | + solar_farm_center[0] + 0.01, |
| 47 | + solar_farm_center[1] + 0.01, |
| 48 | + ), |
| 49 | + None, |
| 50 | + ) |
| 51 | + feat = Feature( |
| 52 | + request_geometry, |
| 53 | + { |
| 54 | + "es_start_time": datetime(2024, 12, 1, tzinfo=UTC).isoformat(), |
| 55 | + "es_end_time": datetime(2025, 7, 1, tzinfo=UTC).isoformat(), |
| 56 | + }, |
| 57 | + ) |
| 58 | + GeojsonVectorFormat().encode_to_file( |
| 59 | + UPath(config_dir / "prediction_request_geometry.geojson"), [feat] |
| 60 | + ) |
| 61 | + |
| 62 | + # We also customize the esrun.yaml since we want to use a single window. |
| 63 | + with (src_dir / "esrun.yaml").open() as f: |
| 64 | + esrun_config = yaml.safe_load(f) |
| 65 | + esrun_config["partition_strategies"]["partition_request_geometry"] = { |
| 66 | + "class_path": "esrun.runner.tools.partitioners.noop_partitioner.NoopPartitioner", |
| 67 | + } |
| 68 | + esrun_config["partition_strategies"]["prepare_window_geometries"] = { |
| 69 | + "class_path": "esrun.runner.tools.partitioners.reprojection_partitioner.ReprojectionPartitioner", |
| 70 | + "init_args": { |
| 71 | + "output_projection": { |
| 72 | + "class_path": "rslearn.utils.geometry.Projection", |
| 73 | + "init_args": { |
| 74 | + "crs": "EPSG:3857", |
| 75 | + "x_resolution": 10, |
| 76 | + "y_resolution": -10, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "use_utm": True, |
| 80 | + }, |
| 81 | + } |
| 82 | + with (config_dir / "esrun.yaml").open("w") as f: |
| 83 | + yaml.safe_dump(esrun_config, f) |
| 84 | + |
| 85 | + scratch_dir = tmp_path / "scratch" |
| 86 | + esrun( |
| 87 | + config_path=config_dir, |
| 88 | + scratch_path=scratch_dir, |
| 89 | + checkpoint_path=checkpoint_path, |
| 90 | + ) |
| 91 | + |
| 92 | + result_fname = scratch_dir / "results" / "results_raster" / "result_0.tif" |
| 93 | + assert result_fname.exists() |
| 94 | + with rasterio.open(result_fname) as raster: |
| 95 | + array = raster.read() |
| 96 | + assert 0.1 < (np.count_nonzero(array == 1) / np.size(array)) < 0.5 |
0 commit comments