Skip to content

Commit

Permalink
Fix _fit_estimators extraction
Browse files Browse the repository at this point in the history
_fit_estimators
fails on
E       UnboundLocalError: cannot access local variable 'u_nk' where it is not associated with a value

Since the goal is to reduce branches I revert that and extract the more isolated check_estimators_availability instead
  • Loading branch information
evidencebp committed Oct 10, 2024
1 parent a1ea9d9 commit f017972
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/alchemlyb/workflows/abfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,7 @@ def estimate(self, estimators=("MBAR", "BAR", "TI"), **kwargs):
if isinstance(estimators, str):
estimators = (estimators,)

for estimator in estimators:
if estimator not in (FEP_ESTIMATORS + TI_ESTIMATORS):
msg = f"Estimator {estimator} is not available in {FEP_ESTIMATORS + TI_ESTIMATORS}."
logger.error(msg)
raise ValueError(msg)
self.check_estimators_availability(estimators)

logger.info(f"Start running estimator: {','.join(estimators)}.")
self.estimator = {}
Expand All @@ -469,9 +465,6 @@ def estimate(self, estimators=("MBAR", "BAR", "TI"), **kwargs):
logger.warning("u_nk has not been preprocessed.")
logger.info(f"A total {len(u_nk)} lines of u_nk is used.")

self._fit_estimators(dHdl, estimators, kwargs, u_nk)

def _fit_estimators(self, dHdl, estimators, kwargs, u_nk):
for estimator in estimators:
if estimator == "MBAR":
logger.info("Run MBAR estimator.")
Expand All @@ -487,6 +480,13 @@ def _fit_estimators(self, dHdl, estimators, kwargs, u_nk):
logger.info("Run TI estimator.")
self.estimator[estimator] = TI(**kwargs).fit(dHdl)

def check_estimators_availability(self, estimators):
for estimator in estimators:
if estimator not in (FEP_ESTIMATORS + TI_ESTIMATORS):
msg = f"Estimator {estimator} is not available in {FEP_ESTIMATORS + TI_ESTIMATORS}."
logger.error(msg)
raise ValueError(msg)

def generate_result(self):
"""Summarise the result into a dataframe.
Expand Down

0 comments on commit f017972

Please sign in to comment.