-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/108 apply a measure selection strategy to all locations #111
Merged
Carsopre
merged 12 commits into
master
from
feature/108-apply-a-measure-selection-strategy-to-all-locations
Oct 25, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b91b46b
docs: Added documentation for strategies module
Carsopre 1823845
feat: Added new class to better handle / generate the location-reinfo…
Carsopre ed5c5a9
chore: Fixed builder reference
Carsopre beff199
chore: Fixed summary location matrix builder by overriding hash and e…
Carsopre 5ee492e
feat: Added order_strategy submodule for determining reinforcement fe…
Carsopre b03a237
chore: Added logic to apply `OrderStrategy`
Carsopre 537c15a
chore: Added first loop to determine default chosen measure
Carsopre be0b636
chore: Added dataclass to handle strategy input
Carsopre 352f18e
chore: Added output to the summary csv. Added logic on how to sort th…
Carsopre 13804f2
chore: Updated logic so that the columns are correctly placed
Carsopre a731ada
chore: removed commented out code
Carsopre 65d1215
chore: Removed irrelevant comment
Carsopre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
from dataclasses import field, dataclass | ||
|
||
from koswat.cost_report.multi_location_profile import MultiLocationProfileCostReport | ||
from koswat.dike.surroundings.point.point_surroundings import PointSurroundings | ||
from koswat.strategies.strategy_location_matrix import StrategyLocationReinforcements | ||
|
||
|
||
@dataclass | ||
class KoswatSummary: | ||
locations_profile_report_list: List[MultiLocationProfileCostReport] | ||
available_locations: List[PointSurroundings] | ||
|
||
def __init__(self) -> None: | ||
self.locations_profile_report_list = [] | ||
self.available_locations = [] | ||
locations_profile_report_list: list[MultiLocationProfileCostReport] = field(default_factory=lambda: []) | ||
available_locations: list[PointSurroundings] = field(default_factory=lambda: []) | ||
reinforcement_per_locations: list[StrategyLocationReinforcements] = field(default_factory=lambda: []) |
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
24 changes: 24 additions & 0 deletions
24
koswat/cost_report/summary/koswat_summary_location_matrix.py
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,24 @@ | ||
from __future__ import annotations | ||
from typing import Type | ||
from koswat.dike.surroundings.point.point_surroundings import PointSurroundings | ||
from koswat.dike_reinforcements.reinforcement_profile.reinforcement_profile import ( | ||
ReinforcementProfile, | ||
) | ||
|
||
|
||
class KoswatSummaryLocationMatrix: | ||
locations_matrix: dict[PointSurroundings, list[Type[ReinforcementProfile]]] | ||
|
||
def __init__(self) -> None: | ||
self.locations_matrix = [] | ||
|
||
def sort_by_traject_order(self): | ||
self.locations_matrix = dict( | ||
sorted(self.locations_matrix.items(), key=lambda x: x[0].traject_order) | ||
) | ||
|
||
@classmethod | ||
def from_point_surroundings_list(cls, point_surroundings: list[PointSurroundings]): | ||
_this_cls = cls() | ||
_this_cls.locations_matrix = dict((_ps, []) for _ps in point_surroundings) | ||
return _this_cls |
80 changes: 80 additions & 0 deletions
80
koswat/cost_report/summary/koswat_summary_location_matrix_builder.py
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,80 @@ | ||
import logging | ||
from koswat.core.protocols.builder_protocol import BuilderProtocol | ||
from koswat.cost_report.multi_location_profile.multi_location_profile_cost_report import ( | ||
MultiLocationProfileCostReport, | ||
) | ||
from koswat.cost_report.summary.koswat_summary_location_matrix import ( | ||
KoswatSummaryLocationMatrix, | ||
) | ||
from koswat.dike.surroundings.point.point_surroundings import PointSurroundings | ||
from koswat.dike_reinforcements.reinforcement_profile.reinforcement_profile import ( | ||
ReinforcementProfile, | ||
) | ||
|
||
|
||
class KoswatSummaryLocationMatrixBuilder(BuilderProtocol): | ||
""" | ||
NOTE: Although this 'problem' could be easily solved with `pandas` | ||
or `numpy`, I prefer not to include external (heavy) dependencies unless | ||
strictily necessary. | ||
If / when performance would become a problem, then this builder could (perhaps) | ||
benefit from using the aforementioned libraries. | ||
""" | ||
|
||
locations_profile_report_list: list[MultiLocationProfileCostReport] | ||
available_locations: list[PointSurroundings] | ||
|
||
def __init__( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this match with the design principle of parameterless init? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
self, | ||
locations_profile: list[MultiLocationProfileCostReport], | ||
available_locations: list[PointSurroundings], | ||
) -> None: | ||
self.locations_profile_report_list = locations_profile | ||
self.available_locations = available_locations | ||
|
||
def _get_multi_location_profile_to_dict_matrix( | ||
self, locations_profile: MultiLocationProfileCostReport | ||
) -> dict[PointSurroundings, ReinforcementProfile]: | ||
return dict( | ||
(_location, type(locations_profile.profile_cost_report.reinforced_profile)) | ||
for _location in locations_profile.locations | ||
) | ||
|
||
def _get_list_summary_matrix_for_locations_with_reinforcements( | ||
self, | ||
) -> list[dict[PointSurroundings, ReinforcementProfile]]: | ||
return list( | ||
map( | ||
self._get_multi_location_profile_to_dict_matrix, | ||
self.locations_profile_report_list, | ||
) | ||
) | ||
|
||
def build(self) -> KoswatSummaryLocationMatrix: | ||
# 1. First we get all the possible reinforcements per point. | ||
|
||
logging.info("Initalizing locations-reinforcements matrix.") | ||
_reinforce_matrix_dict_list = ( | ||
self._get_list_summary_matrix_for_locations_with_reinforcements() | ||
) | ||
|
||
# 2. Then we initialize the matrix with all available locations, | ||
# but no reinforcements. | ||
|
||
_summary_matrix = KoswatSummaryLocationMatrix.from_point_surroundings_list( | ||
self.available_locations | ||
) | ||
|
||
# 3. Last, we merge the reinforcements dictionary into the matrix. | ||
for _location in _summary_matrix.locations_matrix.keys(): | ||
for _reinforce_matrix_dict in _reinforce_matrix_dict_list: | ||
if _location in _reinforce_matrix_dict: | ||
_summary_matrix.locations_matrix[_location].append( | ||
_reinforce_matrix_dict[_location] | ||
) | ||
|
||
_summary_matrix.sort_by_traject_order() | ||
|
||
logging.info("Finalized locations-reinforcements matrix.") | ||
|
||
return _summary_matrix |
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better!