-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ResultReader filters, moving into Filter object owned by Res1D
- Loading branch information
1 parent
9e5c5b9
commit ac3d2d5
Showing
11 changed files
with
221 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Filter module for building Filter objects.""" | ||
|
||
from .filter import Filter | ||
from .filter import SubFilter | ||
from .name_filter import NameFilter | ||
from .time_filter import TimeFilter | ||
from .step_every_filter import StepEveryFilter | ||
|
||
__all__ = ["Filter", "SubFilter", "NameFilter", "TimeFilter", "StepEveryFilter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Filter Class.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Protocol | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from DHI.Mike1D.ResultDataAccess import ResultData | ||
|
||
from DHI.Mike1D.ResultDataAccess import Filter as Mike1DFilter | ||
|
||
|
||
class Filter: | ||
"""Wrapper class for applying subfilters to a Filter object.""" | ||
|
||
def __init__( | ||
self, | ||
sub_filters: list[SubFilter], | ||
): | ||
self._filter = Mike1DFilter() | ||
self.sub_filters = sub_filters | ||
|
||
def use_filter(self) -> bool: | ||
"""Whether the filter should be applied.""" | ||
return any([f.use_filter() for f in self.sub_filters]) | ||
|
||
def apply(self, result_data: ResultData): | ||
"""Apply filter.""" | ||
for sub_filter in self.sub_filters: | ||
sub_filter.apply(self._filter, result_data) | ||
result_data.Parameters.Filter = self._filter | ||
|
||
@property | ||
def m1d_filter(self) -> Filter: | ||
""".NET DHI.Mike1D.ResultDataAccess.Filter object.""" | ||
return self._filter | ||
|
||
@property | ||
def filtered_reaches(self) -> list[str]: | ||
"""List of filtered reach names.""" | ||
return self._filter.FilteredReaches | ||
|
||
|
||
class SubFilter(Protocol): | ||
"""Class for configuring Filter objects.""" | ||
|
||
def apply(self, filter: Filter, result_data: ResultData | None) -> None: | ||
"""Apply the filter to the provided Filter object.""" | ||
pass | ||
|
||
def use_filter(self) -> bool: | ||
"""Check if the filter should be used.""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Module for the NameFilter class.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from DHI.Mike1D.ResultDataAccess import ResultData | ||
from DHI.Mike1D.ResultDataAccess import Filter | ||
|
||
|
||
from .filter import SubFilter | ||
|
||
from DHI.Mike1D.ResultDataAccess import DataItemFilterName | ||
|
||
|
||
class NameFilter(SubFilter): | ||
"""Wrapper class for applying time filters to a Filter object.""" | ||
|
||
def __init__( | ||
self, | ||
reaches: None | list[str], | ||
nodes: None | list[str], | ||
catchments: None | list[str], | ||
): | ||
self._reaches = reaches if reaches else [] | ||
self._nodes = nodes if nodes else [] | ||
self._catchments = catchments if catchments else [] | ||
|
||
def use_filter(self) -> bool: | ||
"""Check if the filter should be used.""" | ||
return any((self._reaches, self._nodes, self._catchments)) | ||
|
||
def apply(self, filter: Filter, result_data: ResultData | None): | ||
"""Apply the filter to the provided Filter object.""" | ||
if not self.use_filter(): | ||
return | ||
|
||
data_item_filter = self.create_data_item_filter(result_data) | ||
filter.AddDataItemFilter(data_item_filter) | ||
|
||
def create_data_item_filter(self, result_data: ResultData) -> DataItemFilterName: | ||
"""Create DataItemFilterName object.""" | ||
data_item_filter = DataItemFilterName(result_data) | ||
|
||
for reach in self._reaches: | ||
data_item_filter.Reaches.Add(reach) | ||
for node in self._nodes: | ||
data_item_filter.Nodes.Add(node) | ||
for catchment in self._catchments: | ||
data_item_filter.Catchments.Add(catchment) | ||
|
||
return data_item_filter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Module for the TimeFilter class.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from DHI.Mike1D.ResultDataAccess import ResultData | ||
from DHI.Mike1D.ResultDataAccess import Filter | ||
|
||
|
||
class StepEveryFilter: | ||
"""Wrapper class for applying step every filter to a Filter object.""" | ||
|
||
def __init__(self, step_every: int | None): | ||
self._step_every = step_every | ||
|
||
def use_filter(self) -> bool: | ||
"""Check if the filter should be used.""" | ||
return self._step_every is not None | ||
|
||
def apply(self, filter: Filter, result_data: ResultData | None = None): | ||
"""Apply the filter to the provided Filter object.""" | ||
if self.use_filter(): | ||
filter.LoadStep = self._step_every |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.