From 5c9fb1e587ec4dcbc696119c289cd46f3ffa5d9e Mon Sep 17 00:00:00 2001 From: Yida <77777027+yida-tao@users.noreply.github.com> Date: Sat, 12 Nov 2022 17:54:04 +0100 Subject: [PATCH 1/9] Update internal.py --- geolib/models/dstability/internal.py | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/geolib/models/dstability/internal.py b/geolib/models/dstability/internal.py index 3169f1a6..95f35920 100644 --- a/geolib/models/dstability/internal.py +++ b/geolib/models/dstability/internal.py @@ -141,6 +141,39 @@ def add_head_line( self.PhreaticLineId = head_line.Id return head_line + + def edit_head_line(self, + head_line_id: str, + label: str or None, + notes: str or None, + points: List[Point], + is_phreatic_line: bool or None) -> PersistableHeadLine: + """ + Update a headline + + Args: + head_line_id (str): id of the headline + points (list of Point): ordered points of the headline + + Returns: + PersistableHeadLine: the edited headline + """ + + for persistable_headline in self.HeadLines: + if persistable_headline.Id == head_line_id: + if label is None: + label = persistable_headline.Label + if notes is None: + notes = persistable_headline.Notes + if is_phreatic_line is None: + is_phreatic_line = False + head_line = PersistableHeadLine(Id=head_line_id, Label=label, Notes=notes) + head_line.Points = [PersistablePoint(X=p.x, Z=p.z) for p in points] + if is_phreatic_line: + self.PhreaticLineId = head_line.Id + return persistable_headline + + raise ValueError(f"Head line id '{head_line_id}' not found in Waternets/PersistableHeadlines") def add_reference_line( self, From d8bf24b2ebf4ef138f2b3eee0448eca4b9436f73 Mon Sep 17 00:00:00 2001 From: Yida <77777027+yida-tao@users.noreply.github.com> Date: Mon, 14 Nov 2022 14:36:32 +0100 Subject: [PATCH 2/9] edit_reference_line --- geolib/models/dstability/internal.py | 49 +++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/geolib/models/dstability/internal.py b/geolib/models/dstability/internal.py index 95f35920..21736d13 100644 --- a/geolib/models/dstability/internal.py +++ b/geolib/models/dstability/internal.py @@ -144,9 +144,9 @@ def add_head_line( def edit_head_line(self, head_line_id: str, + points: List[Point], label: str or None, notes: str or None, - points: List[Point], is_phreatic_line: bool or None) -> PersistableHeadLine: """ Update a headline @@ -204,6 +204,53 @@ def add_reference_line( self.ReferenceLines.append(reference_line) return reference_line + + def edit_reference_line(self, + reference_line_id: str, + points: List[Point], + label: str or None, + notes: str or None, + bottom_head_line_id: str or None, + top_head_line_id: str or None, + ) -> PersistableReferenceLine: + """ + Update a reference line + + Args: + reference_line_id (str): id of the reference line + points (list of Point): ordered points of the reference line + + Returns: + PersistableHeadLine: the edited headline + """ + + for persistable_reference_line in self.ReferenceLines: + if persistable_reference_line.Id == reference_line_id: + if label is None: + label = persistable_reference_line.Label + if notes is None: + notes = persistable_reference_line.Notes + if bottom_head_line_id is None: + is_phreatic_line = persistable_reference_line.BottomHeadLineId + if top_head_line_id is None: + top_head_line_id = persistable_reference_line.TopHeadLineId + reference_line = PersistableReferenceLine(Id=head_line_id, Label=label, Notes=notes) + reference_line.Points = [PersistablePoint(X=p.x, Z=p.z) for p in points] + + if not self.has_head_line_id(bottom_head_line_id): + raise ValueError( + f"Unknown headline id {bottom_head_line_id} for bottom_head_line_id" + ) + + if not self.has_head_line_id(top_head_line_id): + raise ValueError( + f"Unknown headline id {top_head_line_id} for top_head_line_id" + ) + + reference_line.BottomHeadLineId = bottom_head_line_id + reference_line.TopHeadLineId = top_head_line_id + + return persistable_reference_line class PersistableDitchCharacteristics(DStabilityBaseModelStructure): From e7e2bdeb6fb2bb7431e3318b9a66a48b307656dc Mon Sep 17 00:00:00 2001 From: Yida <77777027+yida-tao@users.noreply.github.com> Date: Mon, 14 Nov 2022 16:43:19 +0100 Subject: [PATCH 3/9] Update test waternets with edit head & reference line --- tests/models/dstability/test_waternets.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/models/dstability/test_waternets.py b/tests/models/dstability/test_waternets.py index 3acaa3a5..33fa24ec 100644 --- a/tests/models/dstability/test_waternets.py +++ b/tests/models/dstability/test_waternets.py @@ -16,13 +16,16 @@ class TestDStabilityHeadLine: def test_add_head_line(self): dsm = DStabilityModel() points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] - headline_id = dsm.add_head_line( + head_line_id = dsm.add_head_line( label="TestHL", points=points, is_phreatic_line=True ) - headline = dsm.datastructure.waternets[0].get_head_line(str(headline_id)) - assert isinstance(headline_id, int) - assert pytest.approx(headline.Points[0].X) == -20.0 - assert dsm.waternets[0].PhreaticLineId == headline.Id + head_line = dsm.datastructure.waternets[0].get_head_line(str(headline_id)) + assert isinstance(head_line_id, int) + assert pytest.approx(head_line.Points[0].X) == -20.0 + assert dsm.waternets[0].PhreaticLineId == head_line.Id + points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] + dsm.edit_head_line(head_line_id=head_line_id, points=points_v2) + assert pytest.approx(headline.Points[0].Z) == -3.0 class TestDStabilityReferenceLine: @@ -51,6 +54,10 @@ def test_add(self): assert isinstance(reference_line_id, int) assert len(dsm.waternets[0].ReferenceLines) == 1 assert pytest.approx(reference_line.Points[-1].Z) == -2.0 + + points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] + dsm.edit_reference_line(reference_line_id=headline_id, points=points_v2) + assert pytest.approx(reference_line.Points[0].Z) == -3.0 # add ref line with invalid headline id with pytest.raises(ValueError): From e5b0c37e89ec205169b05dd0c7f1b168f0880d50 Mon Sep 17 00:00:00 2001 From: Yida <77777027+yida-tao@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:15:53 +0100 Subject: [PATCH 4/9] test waterlines with new structure --- tests/models/dstability/test_waternets.py | 72 ++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/tests/models/dstability/test_waternets.py b/tests/models/dstability/test_waternets.py index 33fa24ec..d889f6f8 100644 --- a/tests/models/dstability/test_waternets.py +++ b/tests/models/dstability/test_waternets.py @@ -23,17 +23,32 @@ def test_add_head_line(self): assert isinstance(head_line_id, int) assert pytest.approx(head_line.Points[0].X) == -20.0 assert dsm.waternets[0].PhreaticLineId == head_line.Id - points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] - dsm.edit_head_line(head_line_id=head_line_id, points=points_v2) - assert pytest.approx(headline.Points[0].Z) == -3.0 + + @pytest.mark.unittest + def test_edit_head_line(self): + dsm = DStabilityModel() + points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] + head_line_id = dsm.add_head_line( + label="TestHL", points=points, is_phreatic_line=True + ) + head_line_id_2 = dsm.add_head_line( + label="TestHL", points=points, is_phreatic_line=True + ) + head_line_2 = dsm.datastructure.waternets[0].get_head_line(str(headline_id_2)) + points_2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] + dsm.edit_head_line(head_line_id=head_line_id_2, points=points_2) + assert pytest.approx(head_line_2.Points[0].Z) == -3.0 + assert dsm.waternets[0].PhreaticLineId == head_line_id_2 class TestDStabilityReferenceLine: @pytest.mark.unittest - def test_add(self): + def test_add_reference_line(self): dsm = DStabilityModel() - + points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] + + # add head lines head_line_1_id = dsm.add_head_line( label="TestHL_1", points=points, is_phreatic_line=True ) @@ -54,12 +69,55 @@ def test_add(self): assert isinstance(reference_line_id, int) assert len(dsm.waternets[0].ReferenceLines) == 1 assert pytest.approx(reference_line.Points[-1].Z) == -2.0 + + # add ref line with invalid headline id + with pytest.raises(ValueError): + dsm.add_reference_line( + label="TestRL", + points=points, + bottom_headline_id=-1, + top_head_line_id=head_line_2_id, + ) + + @pytest.mark.unittest + def test_edit_reference_line(self): + dsm = DStabilityModel() + + points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] + # add head lines + head_line_1_id = dsm.add_head_line( + label="TestHL_1", points=points, is_phreatic_line=True + ) + head_line_2_id = dsm.add_head_line( + label="TestHL_2", points=points, is_phreatic_line=False + ) + head_line_3_id = dsm.add_head_line( + label="TestHL_2", points=points, is_phreatic_line=False + ) + + # adding valid reference line + reference_line_id = dsm.add_reference_line( + label="TestRL", + points=points, + bottom_headline_id=head_line_1_id, + top_head_line_id=head_line_2_id, + ) + reference_line_id_2 = dsm.add_reference_line( + label="TestRL", + points=points, + bottom_headline_id=head_line_1_id, + top_head_line_id=head_line_2_id, + ) + + # add new points, edit reference line with existing reference line id points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] - dsm.edit_reference_line(reference_line_id=headline_id, points=points_v2) + dsm.edit_reference_line(reference_line_id=reference_line_id_2, points=points_v2, + bottom_head_line_id=head_line_3_id) assert pytest.approx(reference_line.Points[0].Z) == -3.0 + assert dsm.waternets[0].ReferenceLines[1].BottomHeadLineId == head_line_3_id - # add ref line with invalid headline id + # edith ref line with invalid headline id with pytest.raises(ValueError): dsm.add_reference_line( label="TestRL", From 4bbdbd537fe0c4c28fccb886d1cc416056774d68 Mon Sep 17 00:00:00 2001 From: Yida <77777027+yida-tao@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:17:25 +0100 Subject: [PATCH 5/9] Update test_waternets.py --- tests/models/dstability/test_waternets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/models/dstability/test_waternets.py b/tests/models/dstability/test_waternets.py index d889f6f8..08f0b5c4 100644 --- a/tests/models/dstability/test_waternets.py +++ b/tests/models/dstability/test_waternets.py @@ -117,9 +117,9 @@ def test_edit_reference_line(self): assert pytest.approx(reference_line.Points[0].Z) == -3.0 assert dsm.waternets[0].ReferenceLines[1].BottomHeadLineId == head_line_3_id - # edith ref line with invalid headline id + # edit ref line with invalid headline id with pytest.raises(ValueError): - dsm.add_reference_line( + dsm.edit_reference_line( label="TestRL", points=points, bottom_headline_id=-1, From aa093f1a8fc10007d7f720a51f948ea007c2c9af Mon Sep 17 00:00:00 2001 From: Yida <77777027+yida-tao@users.noreply.github.com> Date: Tue, 15 Nov 2022 13:01:44 +0100 Subject: [PATCH 6/9] code smells --- tests/models/dstability/test_waternets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/models/dstability/test_waternets.py b/tests/models/dstability/test_waternets.py index 08f0b5c4..5e96fb8c 100644 --- a/tests/models/dstability/test_waternets.py +++ b/tests/models/dstability/test_waternets.py @@ -28,7 +28,7 @@ def test_add_head_line(self): def test_edit_head_line(self): dsm = DStabilityModel() points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] - head_line_id = dsm.add_head_line( + dsm.add_head_line( label="TestHL", points=points, is_phreatic_line=True ) head_line_id_2 = dsm.add_head_line( @@ -97,7 +97,7 @@ def test_edit_reference_line(self): ) # adding valid reference line - reference_line_id = dsm.add_reference_line( + dsm.add_reference_line( label="TestRL", points=points, bottom_headline_id=head_line_1_id, From aa10285dfbd8b972fd6411998077242e91f9b46b Mon Sep 17 00:00:00 2001 From: Yida Date: Thu, 17 Nov 2022 16:32:36 +0100 Subject: [PATCH 7/9] small changes --- .gitignore | 2 + geolib/models/dstability/internal.py | 111 ++++++++++++---------- tests/models/dstability/test_waternets.py | 8 +- 3 files changed, 65 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 15b6f12c..7a779739 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,9 @@ requests.egg-info/ *.swp *.egg env/ +wenv/ .venv/ +.wenv/ .eggs/ .tox/ .pytest_cache/ diff --git a/geolib/models/dstability/internal.py b/geolib/models/dstability/internal.py index 21736d13..6bcc5bcf 100644 --- a/geolib/models/dstability/internal.py +++ b/geolib/models/dstability/internal.py @@ -126,12 +126,12 @@ def has_head_line_id(self, head_line_id: str) -> bool: return head_line_id in {head_line.Id for head_line in self.HeadLines} def add_head_line( - self, - head_line_id: str, - label: str, - notes: str, - points: List[Point], - is_phreatic_line: bool, + self, + head_line_id: str, + label: str, + notes: str, + points: List[Point], + is_phreatic_line: bool, ) -> PersistableHeadLine: head_line = PersistableHeadLine(Id=head_line_id, Label=label, Notes=notes) head_line.Points = [PersistablePoint(X=p.x, Z=p.z) for p in points] @@ -141,7 +141,7 @@ def add_head_line( self.PhreaticLineId = head_line.Id return head_line - + def edit_head_line(self, head_line_id: str, points: List[Point], @@ -153,7 +153,10 @@ def edit_head_line(self, Args: head_line_id (str): id of the headline - points (list of Point): ordered points of the headline + points (list of Point): ordered points of the headline + label + notes + is_phreatic_line Returns: PersistableHeadLine: the edited headline @@ -172,17 +175,17 @@ def edit_head_line(self, if is_phreatic_line: self.PhreaticLineId = head_line.Id return persistable_headline - + raise ValueError(f"Head line id '{head_line_id}' not found in Waternets/PersistableHeadlines") def add_reference_line( - self, - reference_line_id: str, - label: str, - notes: str, - points: List[Point], - bottom_head_line_id: str, - top_head_line_id: str, + self, + reference_line_id: str, + label: str, + notes: str, + points: List[Point], + bottom_head_line_id: str, + top_head_line_id: str, ) -> PersistableReferenceLine: reference_line = PersistableReferenceLine( Id=reference_line_id, Label=label, Notes=notes @@ -204,21 +207,24 @@ def add_reference_line( self.ReferenceLines.append(reference_line) return reference_line - - def edit_reference_line(self, - reference_line_id: str, - points: List[Point], - label: str or None, - notes: str or None, - bottom_head_line_id: str or None, - top_head_line_id: str or None, - ) -> PersistableReferenceLine: - """ - Update a reference line + + def edit_reference_line(self, + reference_line_id: str, + points: List[Point], + label: str or None, + notes: str or None, + bottom_head_line_id: str or None, + top_head_line_id: str or None, + ) -> PersistableReferenceLine: + """Update a reference line Args: reference_line_id (str): id of the reference line points (list of Point): ordered points of the reference line + label + notes + bottom_head_line_id + top_head_line_id Returns: PersistableHeadLine: the edited headline @@ -231,10 +237,10 @@ def edit_reference_line(self, if notes is None: notes = persistable_reference_line.Notes if bottom_head_line_id is None: - is_phreatic_line = persistable_reference_line.BottomHeadLineId + bottom_head_line_id = persistable_reference_line.BottomHeadLineId if top_head_line_id is None: top_head_line_id = persistable_reference_line.TopHeadLineId - reference_line = PersistableReferenceLine(Id=head_line_id, Label=label, Notes=notes) + reference_line = PersistableReferenceLine(Id=reference_line_id, Label=label, Notes=notes) reference_line.Points = [PersistablePoint(X=p.x, Z=p.z) for p in points] if not self.has_head_line_id(bottom_head_line_id): @@ -385,9 +391,9 @@ def add_state_point(self, state_point: PersistableStatePoint) -> None: self.StatePoints.append(state_point) def add_state_line( - self, - points: List[PersistablePoint], - state_points: List[PersistableStateLinePoint], + self, + points: List[PersistablePoint], + state_points: List[PersistableStateLinePoint], ): self.StateLines.append(PersistableStateLine(Points=points, Values=state_points)) @@ -777,7 +783,7 @@ def add_soil(self, soil: Soil) -> PersistableSoil: @staticmethod def __to_global_stochastic_parameter( - persistable_stochastic_parameter: PersistableStochasticParameter, + persistable_stochastic_parameter: PersistableStochasticParameter, ): from geolib.soils import StochasticParameter @@ -790,17 +796,17 @@ def __to_global_stochastic_parameter( def __determine_strength_increase_exponent(self, persistable_soil: PersistableSoil): # shear increase exponent taken from persistable_soil.SuTable or just from persistable_soil if ( - persistable_soil.ShearStrengthModelTypeAbovePhreaticLevel.value == "Su" - or persistable_soil.ShearStrengthModelTypeBelowPhreaticLevel.value == "Su" + persistable_soil.ShearStrengthModelTypeAbovePhreaticLevel.value == "Su" + or persistable_soil.ShearStrengthModelTypeBelowPhreaticLevel.value == "Su" ): # SHANSEP model is selected so the StrengthIncreaseExponentStochasticParameter from persistable_soil should be used return self.__to_global_stochastic_parameter( persistable_soil.StrengthIncreaseExponentStochasticParameter ) elif ( - persistable_soil.ShearStrengthModelTypeAbovePhreaticLevel.value == "SuTable" - or persistable_soil.ShearStrengthModelTypeBelowPhreaticLevel.value - == "SuTable" + persistable_soil.ShearStrengthModelTypeAbovePhreaticLevel.value == "SuTable" + or persistable_soil.ShearStrengthModelTypeBelowPhreaticLevel.value + == "SuTable" ): # SU table is selected so the StrengthIncreaseExponentStochasticParameter from SuTable should be used return self.__to_global_stochastic_parameter( @@ -961,7 +967,7 @@ class Reinforcements(DStabilitySubStructure): Nails: List[PersistableNail] = [] def add_reinforcement( - self, reinforcement: "DStabilityReinforcement" + self, reinforcement: "DStabilityReinforcement" ) -> Union[PersistableForbiddenLine, PersistableGeotextile, PersistableNail]: internal_datastructure = reinforcement._to_internal_datastructure() plural_class_name = f"{reinforcement.__class__.__name__}s" @@ -1087,7 +1093,7 @@ class Loads(DStabilitySubStructure): UniformLoads: Optional[List[Optional[PersistableUniformLoad]]] = [] def add_load( - self, load: "DStabilityLoad", consolidations: List["Consolidation"] + self, load: "DStabilityLoad", consolidations: List["Consolidation"] ) -> Union[PersistableUniformLoad, PersistableLineLoad, PersistableLayerLoad]: internal_datastructure = load.to_internal_datastructure() @@ -1107,7 +1113,7 @@ def add_load( return internal_datastructure def add_layer_load( - self, soil_layer_id: int, consolidations: List["Consolidation"] + self, soil_layer_id: int, consolidations: List["Consolidation"] ) -> PersistableLayerLoad: layer_load = PersistableLayerLoad( LayerId=str(soil_layer_id), @@ -1176,7 +1182,7 @@ def get_layer(self, id: int) -> PersistableLayer: raise ValueError(f"Layer id {id} not found in this geometry") def add_layer( - self, id: str, label: str, notes: str, points: List[Point] + self, id: str, label: str, notes: str, points: List[Point] ) -> PersistableLayer: """ Add a new layer to the model. Layers are expected; @@ -1398,7 +1404,7 @@ def set_bishop(self, bishop_settings: PersistableBishopSettings) -> None: self.AnalysisType = AnalysisType.BISHOP def set_bishop_brute_force( - self, bishop_brute_force_settings: PersistableBishopBruteForceSettings + self, bishop_brute_force_settings: PersistableBishopBruteForceSettings ) -> None: self.BishopBruteForce = bishop_brute_force_settings self.AnalysisType = AnalysisType.BISHOP_BRUTE_FORCE @@ -1408,7 +1414,7 @@ def set_spencer(self, spencer_settings: PersistableSpencerSettings) -> None: self.AnalysisType = AnalysisType.SPENCER def set_spencer_genetic( - self, spencer_genetic_settings: PersistableSpencerGeneticSettings + self, spencer_genetic_settings: PersistableSpencerGeneticSettings ) -> None: self.SpencerGenetic = spencer_genetic_settings self.AnalysisType = AnalysisType.SPENCER_GENETIC @@ -1418,8 +1424,8 @@ def set_uplift_van(self, uplift_van_settings: PersistableUpliftVanSettings) -> N self.AnalysisType = AnalysisType.UPLIFT_VAN def set_uplift_van_particle_swarm( - self, - uplift_van_particle_swarm_settings: PersistableUpliftVanParticleSwarmSettings, + self, + uplift_van_particle_swarm_settings: PersistableUpliftVanParticleSwarmSettings, ) -> None: self.UpliftVanParticleSwarm = uplift_van_particle_swarm_settings self.AnalysisType = AnalysisType.UPLIFT_VAN_PARTICLE_SWARM @@ -1815,6 +1821,7 @@ def get_slipcircle_output(self) -> UpliftVanSlipCircleResult: BishopResult, ] + ########################### # INPUT AND OUTPUT COMBINED ########################### @@ -1908,8 +1915,8 @@ def ensure_validity_foreign_keys(cls, values): if stage.StateCorrelationsId != values.get("statecorrelations")[i].Id: raise ValueError("StateCorrelationsIds not linked!") if ( - stage.WaternetCreatorSettingsId - != values.get("waternetcreatorsettings")[i].Id + stage.WaternetCreatorSettingsId + != values.get("waternetcreatorsettings")[i].Id ): raise ValueError("WaternetCreatorSettingsIds not linked!") if stage.WaternetId != values.get("waternets")[i].Id: @@ -1933,7 +1940,7 @@ def stage_specific_fields(self): ] def get_stage_specific_fields( - self, stage=0 + self, stage=0 ) -> Generator[Tuple[str, DStabilitySubStructure], None, None]: """Yield stage specific fields for given stage.""" for fieldname in self.stage_specific_fields: @@ -1967,7 +1974,7 @@ def get_correct_key(key, mapping): return unique_id def duplicate_stage( - self, current_stage: int, label: str, notes: str, unique_start_id: int + self, current_stage: int, label: str, notes: str, unique_start_id: int ): """Duplicates an existing stage. Copies the specific stage fields for a stage and renumbers all Ids, @@ -1998,7 +2005,7 @@ def duplicate_stage( return len(self.stages) - 1, unique_start_id def add_default_stage( - self, label: str, notes: str, unique_start_id=500 + self, label: str, notes: str, unique_start_id=500 ) -> Tuple[int, int]: """Add a new default (empty) stage to DStability.""" self.waternets += [Waternet(Id=str(unique_start_id + 1))] @@ -2109,7 +2116,7 @@ def has_reinforcements(self, stage_id: int) -> bool: return False def get_result_substructure( - self, analysis_type: AnalysisTypeEnum, calculation_type: CalculationTypeEnum + self, analysis_type: AnalysisTypeEnum, calculation_type: CalculationTypeEnum ) -> List[DStabilityResult]: result_types_mapping = { diff --git a/tests/models/dstability/test_waternets.py b/tests/models/dstability/test_waternets.py index 5e96fb8c..941a48dc 100644 --- a/tests/models/dstability/test_waternets.py +++ b/tests/models/dstability/test_waternets.py @@ -19,7 +19,7 @@ def test_add_head_line(self): head_line_id = dsm.add_head_line( label="TestHL", points=points, is_phreatic_line=True ) - head_line = dsm.datastructure.waternets[0].get_head_line(str(headline_id)) + head_line = dsm.datastructure.waternets[0].get_head_line(str(head_line_id)) assert isinstance(head_line_id, int) assert pytest.approx(head_line.Points[0].X) == -20.0 assert dsm.waternets[0].PhreaticLineId == head_line.Id @@ -34,7 +34,7 @@ def test_edit_head_line(self): head_line_id_2 = dsm.add_head_line( label="TestHL", points=points, is_phreatic_line=True ) - head_line_2 = dsm.datastructure.waternets[0].get_head_line(str(headline_id_2)) + head_line_2 = dsm.datastructure.waternets[0].get_head_line(str(head_line_id_2)) points_2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] dsm.edit_head_line(head_line_id=head_line_id_2, points=points_2) assert pytest.approx(head_line_2.Points[0].Z) == -3.0 @@ -84,6 +84,7 @@ def test_edit_reference_line(self): dsm = DStabilityModel() points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] + points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] # add head lines head_line_1_id = dsm.add_head_line( @@ -111,10 +112,9 @@ def test_edit_reference_line(self): ) # add new points, edit reference line with existing reference line id - points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] dsm.edit_reference_line(reference_line_id=reference_line_id_2, points=points_v2, bottom_head_line_id=head_line_3_id) - assert pytest.approx(reference_line.Points[0].Z) == -3.0 + assert pytest.approx(reference_line_id_2.Points[0].Z) == -3.0 assert dsm.waternets[0].ReferenceLines[1].BottomHeadLineId == head_line_3_id # edit ref line with invalid headline id From 42ebd1f998750eb319da39d0fc5a6e40a83b5188 Mon Sep 17 00:00:00 2001 From: Yida Date: Thu, 17 Nov 2022 16:56:14 +0100 Subject: [PATCH 8/9] added methods from internal to dstabilitymodel --- geolib/models/dstability/dstability_model.py | 172 ++++++++++++++----- geolib/models/dstability/internal.py | 18 +- tests/models/dstability/test_waternets.py | 26 +-- 3 files changed, 150 insertions(+), 66 deletions(-) diff --git a/geolib/models/dstability/dstability_model.py b/geolib/models/dstability/dstability_model.py index 8c6d1525..4a0a0ec3 100644 --- a/geolib/models/dstability/dstability_model.py +++ b/geolib/models/dstability/dstability_model.py @@ -132,7 +132,7 @@ def _get_result_substructure(self, stage_id: int) -> DStabilityResult: raise ValueError(f"No result found for result id {stage_id}") def get_slipcircle_result( - self, stage_id: int + self, stage_id: int ) -> Union[BishopSlipCircleResult, UpliftVanSlipCircleResult]: """ Get the slipcircle(s) of the calculation result of a given stage. @@ -260,12 +260,12 @@ def points(self): """Enables easy access to the points in the internal dict-like datastructure. Also enables edit/delete for individual points.""" def add_layer( - self, - points: List[Point], - soil_code: str, - label: str = "", - notes: str = "", - stage_id: int = None, + self, + points: List[Point], + soil_code: str, + label: str = "", + notes: str = "", + stage_id: int = None, ) -> int: """ Add a soil layer to the model @@ -306,12 +306,12 @@ def add_layer( return int(persistable_layer.Id) def add_head_line( - self, - points: List[Point], - label: str = "", - notes: str = "", - is_phreatic_line: bool = False, - stage_id: int = None, + self, + points: List[Point], + label: str = "", + notes: str = "", + is_phreatic_line: bool = False, + stage_id: int = None, ) -> int: """ Add head line to the model @@ -338,14 +338,54 @@ def add_head_line( ) return int(persistable_headline.Id) + def edit_head_line( + self, + head_line_id: int, + points: List[Point], + label: Optional[str] = None, + notes: Optional[str] = None, + is_phreatic_line: Optional[bool] = None, + stage_id: int = None, + ) -> int: + """ + Edit head line in a model + + Args: + head_line_id (str): id of headline + points (List[Point]): list of Point classes + label (str): label defaults to empty string + notes (str): notes defaults to empty string + is_phreatic_line (bool): set as phreatic line, defaults to False + stage_id (int): stage to add to, defaults to current stage + + Returns: + bool: id of the edited headline + """ + + stage_id = stage_id if stage_id else self.current_stage + + if not self.datastructure.has_stage(stage_id): + raise IndexError(f"stage {stage_id} is not available") + + waternet = self.waternets[stage_id] + + persistable_headline = waternet.edit_head_line( + head_line_id=head_line_id, + points=points, + label=label, + notes=notes, + is_phreatic_line=is_phreatic_line + ) + return int(persistable_headline.Id) + def add_reference_line( - self, - points: List[Point], - bottom_headline_id: int, - top_head_line_id: int, - label: str = "", - notes: str = "", - stage_id: int = None, + self, + points: List[Point], + bottom_headline_id: int, + top_head_line_id: int, + label: str = "", + notes: str = "", + stage_id: int = None, ) -> int: """ Add reference line to the model @@ -378,10 +418,52 @@ def add_reference_line( ) return int(persistable_referenceline.Id) + def edit_reference_line( + self, + reference_line_id: int, + points: List[Point], + label: Optional[str] = None, + notes: Optional[str] = None, + bottom_head_line_id: Optional[int] = None, + top_head_line_id: Optional[int] = None, + stage_id: int = None,) -> int: + """ + Edit reference line to the model + + Args: + reference_line_id (int): id of to be edited reference line + points (List[Point]): list of Point classes + bottom_head_line_id (int): id of the headline to use as the bottom headline + top_head_line_id (int): id of the headline to use as the top headline + label (str): label defaults to empty string + notes (str): notes defaults to empty string + stage_id (int): stage to add to, defaults to 0 + + Returns: + int: id of the edited reference line + """ + + stage_id = stage_id if stage_id else self.current_stage + + if not self.datastructure.has_stage(stage_id): + raise IndexError(f"stage {stage_id} is not available") + + waternet = self.waternets[stage_id] + + persistable_reference_line = waternet.edit_reference_line( + reference_line_id=reference_line_id, + points=points, + label=label, + notes=notes, + bottom_head_line_id=bottom_head_line_id, + top_head_line_id=top_head_line_id + ) + return int(persistable_reference_line.Id) + def add_state_point( - self, - state_point: DStabilityStatePoint, - stage_id: int = None, + self, + state_point: DStabilityStatePoint, + stage_id: int = None, ) -> int: """ Add state point to the model @@ -419,10 +501,10 @@ def add_state_point( return int(persistable_statepoint.Id) def add_state_line( - self, - points: List[Point], - state_points: List[DStabilityStateLinePoint], - stage_id: int = None, + self, + points: List[Point], + state_points: List[DStabilityStateLinePoint], + stage_id: int = None, ) -> None: """ Add state line. From the Soils, only the state parameters are used. @@ -461,10 +543,10 @@ def add_state_line( states.add_state_line(persistable_points, persistable_state_line_points) def add_load( - self, - load: DStabilityLoad, - consolidations: Optional[List[Consolidation]] = None, - stage_id: Optional[int] = None, + self, + load: DStabilityLoad, + consolidations: Optional[List[Consolidation]] = None, + stage_id: Optional[int] = None, ) -> None: """Add a load to the object. @@ -487,7 +569,7 @@ def add_load( f"load should be a subclass of DstabilityReinforcement, received {load}" ) if self.datastructure.has_soil_layers(stage_id) and self.datastructure.has_loads( - stage_id + stage_id ): if consolidations is None: consolidations = self._get_default_consolidations(stage_id) @@ -498,10 +580,10 @@ def add_load( raise ValueError(f"No loads found for stage id {stage_id}") def add_soil_layer_consolidations( - self, - soil_layer_id: int, - consolidations: Optional[List[Consolidation]] = None, - stage_id: int = None, + self, + soil_layer_id: int, + consolidations: Optional[List[Consolidation]] = None, + stage_id: int = None, ) -> None: """Add consolidations for a layer (layerload). @@ -520,7 +602,7 @@ def add_soil_layer_consolidations( stage_id = stage_id if stage_id is not None else self.current_stage if self.datastructure.has_soil_layer( - stage_id, soil_layer_id + stage_id, soil_layer_id ) and self.datastructure.has_loads(stage_id): if consolidations is None: consolidations = self._get_default_consolidations(stage_id, soil_layer_id) @@ -534,7 +616,7 @@ def add_soil_layer_consolidations( raise ValueError(f"No soil layers found found for stage id {stage_id}") def _get_default_consolidations( - self, stage_id: int, exclude_soil_layer_id: Optional[int] = None + self, stage_id: int, exclude_soil_layer_id: Optional[int] = None ) -> List[Consolidation]: """Length of the consolidations is equal to the amount of soil layers. @@ -549,10 +631,10 @@ def _get_default_consolidations( raise ValueError(f"No soil layers found for stage id {stage_id}") def _verify_consolidations( - self, - consolidations: List[Consolidation], - stage_id: int, - exclude_soil_layer_id: Optional[int] = None, + self, + consolidations: List[Consolidation], + stage_id: int, + exclude_soil_layer_id: Optional[int] = None, ) -> None: if self.datastructure.has_soil_layers(stage_id): consolidation_soil_layer_ids: Set[str] = { @@ -570,9 +652,9 @@ def _verify_consolidations( raise ValueError(f"No soil layers found for stage id {stage_id}") def add_reinforcement( - self, - reinforcement: DStabilityReinforcement, - stage_id: Optional[int] = None, + self, + reinforcement: DStabilityReinforcement, + stage_id: Optional[int] = None, ) -> None: """Add a reinforcement to the model. diff --git a/geolib/models/dstability/internal.py b/geolib/models/dstability/internal.py index 6bcc5bcf..3279bd09 100644 --- a/geolib/models/dstability/internal.py +++ b/geolib/models/dstability/internal.py @@ -143,11 +143,11 @@ def add_head_line( return head_line def edit_head_line(self, - head_line_id: str, + head_line_id: int, points: List[Point], - label: str or None, - notes: str or None, - is_phreatic_line: bool or None) -> PersistableHeadLine: + label: Optional[str] = None, + notes: Optional[str] = None, + is_phreatic_line: Optional[bool] = None) -> PersistableHeadLine: """ Update a headline @@ -209,12 +209,12 @@ def add_reference_line( return reference_line def edit_reference_line(self, - reference_line_id: str, + reference_line_id: int, points: List[Point], - label: str or None, - notes: str or None, - bottom_head_line_id: str or None, - top_head_line_id: str or None, + label: Optional[str], + notes: Optional[str], + bottom_head_line_id: Optional[int], + top_head_line_id: Optional[int], ) -> PersistableReferenceLine: """Update a reference line diff --git a/tests/models/dstability/test_waternets.py b/tests/models/dstability/test_waternets.py index 941a48dc..bc537869 100644 --- a/tests/models/dstability/test_waternets.py +++ b/tests/models/dstability/test_waternets.py @@ -23,7 +23,7 @@ def test_add_head_line(self): assert isinstance(head_line_id, int) assert pytest.approx(head_line.Points[0].X) == -20.0 assert dsm.waternets[0].PhreaticLineId == head_line.Id - + @pytest.mark.unittest def test_edit_head_line(self): dsm = DStabilityModel() @@ -45,9 +45,9 @@ class TestDStabilityReferenceLine: @pytest.mark.unittest def test_add_reference_line(self): dsm = DStabilityModel() - + points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] - + # add head lines head_line_1_id = dsm.add_head_line( label="TestHL_1", points=points, is_phreatic_line=True @@ -78,14 +78,14 @@ def test_add_reference_line(self): bottom_headline_id=-1, top_head_line_id=head_line_2_id, ) - - @pytest.mark.unittest + + @pytest.mark.unittest def test_edit_reference_line(self): dsm = DStabilityModel() - + points = [Point(x=-20.0, z=-2.0), Point(x=50.0, z=-2.0)] points_v2 = [Point(x=-25.0, z=-3.0), Point(x=55.0, z=-3.0)] - + # add head lines head_line_1_id = dsm.add_head_line( label="TestHL_1", points=points, is_phreatic_line=True @@ -96,7 +96,7 @@ def test_edit_reference_line(self): head_line_3_id = dsm.add_head_line( label="TestHL_2", points=points, is_phreatic_line=False ) - + # adding valid reference line dsm.add_reference_line( label="TestRL", @@ -110,18 +110,20 @@ def test_edit_reference_line(self): bottom_headline_id=head_line_1_id, top_head_line_id=head_line_2_id, ) - + # add new points, edit reference line with existing reference line id - dsm.edit_reference_line(reference_line_id=reference_line_id_2, points=points_v2, - bottom_head_line_id=head_line_3_id) + dsm.edit_reference_line(reference_line_id=reference_line_id_2, + points=points_v2, + bottom_head_line_id=head_line_3_id) assert pytest.approx(reference_line_id_2.Points[0].Z) == -3.0 assert dsm.waternets[0].ReferenceLines[1].BottomHeadLineId == head_line_3_id # edit ref line with invalid headline id with pytest.raises(ValueError): dsm.edit_reference_line( + reference_line_id=reference_line_id_2, label="TestRL", points=points, - bottom_headline_id=-1, + bottom_head_line_id=-1, top_head_line_id=head_line_2_id, ) From c4eed5058a9ed9e9d9f271f6b5c1bf4509ce3c9c Mon Sep 17 00:00:00 2001 From: Yida Date: Fri, 18 Nov 2022 14:38:47 +0100 Subject: [PATCH 9/9] Update dstability_model.py --- geolib/models/dstability/dstability_model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/geolib/models/dstability/dstability_model.py b/geolib/models/dstability/dstability_model.py index 4a0a0ec3..2b5255e7 100644 --- a/geolib/models/dstability/dstability_model.py +++ b/geolib/models/dstability/dstability_model.py @@ -25,6 +25,7 @@ Stage, UpliftVanSlipCircleResult, Waternet, + PersistableReferenceLine ) from .loads import Consolidation, DStabilityLoad from .reinforcements import DStabilityReinforcement @@ -450,7 +451,7 @@ def edit_reference_line( waternet = self.waternets[stage_id] - persistable_reference_line = waternet.edit_reference_line( + persistable_reference_line: PersistableReferenceLine = waternet.edit_reference_line( reference_line_id=reference_line_id, points=points, label=label,