Skip to content

Commit b1f5489

Browse files
authored
Add option to treat temporal warnings as errors in eoexecutions (#760)
* Add option to treat temporal warnings as errors in eoexecutions * adjust changelog for this MR * rename param * (quietly fix test)
1 parent e9eecf3 commit b1f5489

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- `MorphologicalFilterTask` adapted to work on boolean values.
44
- Added `temporal_subset` method to `EOPatch`, which can be used to extract a subset of an `EOPatch` by filtering out temporal slices. Also added a corresponding `TemporalSubsetTask`.
5+
- `EOExecutor` now has an option to treat `TemporalDimensionWarning` as an exception.
56

67
## [Version 1.5.0] - 2023-09-06
78

eolearn/core/eoexecution.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from .eonode import EONode
2929
from .eoworkflow import EOWorkflow, WorkflowResults
30-
from .exceptions import EORuntimeWarning
30+
from .exceptions import EORuntimeWarning, TemporalDimensionWarning
3131
from .utils.fs import get_base_filesystem_and_path, get_full_path, pickle_fs, unpickle_fs
3232
from .utils.logging import LogFileFilter
3333
from .utils.parallelize import _decide_processing_type, _ProcessingType, parallelize
@@ -55,6 +55,7 @@ class _ProcessingData:
5555
filter_logs_by_thread: bool
5656
logs_filter: Filter | None
5757
logs_handler_factory: _HandlerFactoryType
58+
raise_on_temporal_mismatch: bool
5859

5960

6061
@dataclass(frozen=True)
@@ -86,6 +87,7 @@ def __init__(
8687
filesystem: FS | None = None,
8788
logs_filter: Filter | None = None,
8889
logs_handler_factory: _HandlerFactoryType = FileHandler,
90+
raise_on_temporal_mismatch: bool = False,
8991
):
9092
"""
9193
:param workflow: A prepared instance of EOWorkflow class
@@ -108,6 +110,7 @@ def __init__(
108110
object.
109111
110112
The 2nd option is chosen only if `filesystem` parameter exists in the signature.
113+
:param raise_on_temporal_mismatch: Whether to treat `TemporalDimensionWarning` as an exception.
111114
"""
112115
self.workflow = workflow
113116
self.execution_kwargs = self._parse_and_validate_execution_kwargs(execution_kwargs)
@@ -116,6 +119,7 @@ def __init__(
116119
self.filesystem, self.logs_folder = self._parse_logs_filesystem(filesystem, logs_folder)
117120
self.logs_filter = logs_filter
118121
self.logs_handler_factory = logs_handler_factory
122+
self.raise_on_temporal_mismatch = raise_on_temporal_mismatch
119123

120124
self.start_time: dt.datetime | None = None
121125
self.report_folder: str | None = None
@@ -193,6 +197,7 @@ def run(self, workers: int | None = 1, multiprocess: bool = True, **tqdm_kwargs:
193197
filter_logs_by_thread=filter_logs_by_thread,
194198
logs_filter=self.logs_filter,
195199
logs_handler_factory=self.logs_handler_factory,
200+
raise_on_temporal_mismatch=self.raise_on_temporal_mismatch,
196201
)
197202
for workflow_kwargs, log_path in zip(self.execution_kwargs, log_paths)
198203
]
@@ -263,7 +268,10 @@ def _execute_workflow(cls, data: _ProcessingData) -> WorkflowResults:
263268
data.logs_handler_factory,
264269
)
265270

266-
results = data.workflow.execute(data.workflow_kwargs, raise_errors=False)
271+
with warnings.catch_warnings():
272+
if data.raise_on_temporal_mismatch:
273+
warnings.simplefilter("error", TemporalDimensionWarning)
274+
results = data.workflow.execute(data.workflow_kwargs, raise_errors=False)
267275

268276
cls._try_remove_logging(data.log_path, logger, handler)
269277
return results

tests/core/test_eoexecutor.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@
1919
import pytest
2020
from fs.base import FS
2121

22-
from eolearn.core import EOExecutor, EONode, EOTask, EOWorkflow, OutputTask, WorkflowResults, execute_with_mp_lock
22+
from sentinelhub import CRS, BBox
23+
24+
from eolearn.core import (
25+
CreateEOPatchTask,
26+
EOExecutor,
27+
EONode,
28+
EOTask,
29+
EOWorkflow,
30+
FeatureType,
31+
InitializeFeatureTask,
32+
OutputTask,
33+
WorkflowResults,
34+
execute_with_mp_lock,
35+
linearly_connect_tasks,
36+
)
2337
from eolearn.core.utils.fs import get_full_path
2438

2539
FULL_LOG_LINE_COUNT = 12
@@ -251,3 +265,21 @@ def test_without_lock(num_workers):
251265
assert len(lines) == 2 * num_workers
252266
assert len(set(lines[:num_workers])) == num_workers, "All processes should start"
253267
assert len(set(lines[num_workers:])) == num_workers, "All processes should finish"
268+
269+
270+
@pytest.mark.parametrize("multiprocess", [True, False])
271+
def test_temporal_dim_error(multiprocess):
272+
workflow = EOWorkflow(
273+
linearly_connect_tasks(
274+
CreateEOPatchTask(bbox=BBox((0, 0, 1, 1), CRS.POP_WEB)),
275+
InitializeFeatureTask([FeatureType.DATA, "data"], (2, 5, 5, 1)),
276+
)
277+
)
278+
279+
executor = EOExecutor(workflow, [{}, {}])
280+
for result in executor.run(workers=2, multiprocess=multiprocess):
281+
assert result.error_node_uid is None
282+
283+
executor = EOExecutor(workflow, [{}, {}], raise_on_temporal_mismatch=True)
284+
for result in executor.run(workers=2, multiprocess=multiprocess):
285+
assert result.error_node_uid is not None

0 commit comments

Comments
 (0)