Skip to content

Commit ed1cc4a

Browse files
committed
fix: refactor to avoid repetitive code
1 parent 25732b4 commit ed1cc4a

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

map2loop/thickness_calculator.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,17 @@ def compute(
8484
)
8585
return units
8686

87+
def _check_thickness_percentage_calculations(self, thicknesses: pandas.DataFrame):
88+
units_with_no_thickness = len(thicknesses[thicknesses['ThicknessMedian'] == -1])
89+
total_units = len(thicknesses)
8790

91+
if total_units > 0 and (units_with_no_thickness / total_units) >= 0.75:
92+
logger.warning(
93+
f"More than {int(0.75 * 100)}% of units ({units_with_no_thickness}/{total_units}) "
94+
f"have a calculated thickness of -1. This may indicate that {self.thickness_calculator_label} "
95+
f"is not suitable for this dataset."
96+
)
97+
8898
class ThicknessCalculatorAlpha(ThicknessCalculator):
8999
"""
90100
ThicknessCalculator class which estimates unit thickness based on units, basal_contacts and stratigraphic order
@@ -176,15 +186,7 @@ def compute(
176186
val = min(distance, thicknesses.at[idx, "ThicknessMean"])
177187
thicknesses.loc[idx, "ThicknessMean"] = val
178188

179-
# add check more than 75% of the unit thicknesses are -1
180-
units_with_no_thickness = len(thicknesses[thicknesses['ThicknessMedian'] == -1])
181-
182-
if units_with_no_thickness / len(thicknesses) >= 0.75:
183-
logger.warning(
184-
f"More than 75% of units ({units_with_no_thickness}/{len(thicknesses)}) have a calculated thickness of -1. "
185-
f"This may indicate that {self.thickness_calculator_label} is not suitable for this dataset."
186-
)
187-
189+
self._check_thickness_percentage_calculations(thicknesses)
188190

189191
return thicknesses
190192

@@ -394,16 +396,7 @@ def compute(
394396

395397
self.lines = geopandas.GeoDataFrame(geometry=[line[0] for line in _lines], crs = basal_contacts.crs)
396398
self.lines['dip'] = _dips
397-
398-
# add check more than 75% of the unit thicknesses are -1
399-
units_with_no_thickness = len(thicknesses[thicknesses['ThicknessMedian'] == -1])
400-
401-
if units_with_no_thickness / len(thicknesses) >= 0.75:
402-
logger.warning(
403-
f"More than 75% of units ({units_with_no_thickness}/{len(thicknesses)}) have a calculated thickness of -1. "
404-
f"This may indicate that {self.thickness_calculator_label} is not suitable for this dataset."
405-
)
406-
399+
self._check_thickness_percentage_calculations(thicknesses)
407400
return thicknesses
408401

409402

@@ -688,12 +681,6 @@ def compute(
688681
output_units.loc[output_units["name"] == unit, "ThicknessMean"] = -1
689682
output_units.loc[output_units["name"] == unit, "ThicknessStdDev"] = -1
690683

691-
# check if more than 75% of the unit thicknesses are -1
692-
units_with_no_thickness = len(output_units[output_units['ThicknessMedian'] == -1])
693-
694-
if units_with_no_thickness / len(output_units) >= 0.75:
695-
logger.warning(
696-
f"More than 75% of units ({units_with_no_thickness}/{len(output_units)}) have a calculated thickness of -1. "
697-
f"This may indicate that {self.thickness_calculator_label} is not suitable for this dataset."
698-
)
684+
self._check_thickness_percentage_calculations(output_units)
685+
699686
return output_units

0 commit comments

Comments
 (0)