From efa17a5729a973eb2523274a17e0fd76e3f25384 Mon Sep 17 00:00:00 2001 From: Dilan Pathirana Date: Thu, 30 Nov 2023 14:21:14 +0100 Subject: [PATCH] remove switching methods --- petab_select/candidate_space.py | 123 ------------------ petab_select/constants.py | 28 ----- test/candidate_space/test_candidate_space.py | 126 ------------------- 3 files changed, 277 deletions(-) diff --git a/petab_select/candidate_space.py b/petab_select/candidate_space.py index 73aa17bc..7fe62bba 100644 --- a/petab_select/candidate_space.py +++ b/petab_select/candidate_space.py @@ -560,102 +560,6 @@ class BackwardCandidateSpace(ForwardCandidateSpace): direction = -1 -#class BidirectionalCandidateSpace(ForwardCandidateSpace): -# """The bidirectional method class. -# -# Attributes: -# method_history: -# The history of models that were found at each search. -# A list of dictionaries, where each dictionary contains keys for the `METHOD` -# and the list of `MODELS`. -# """ -# -# # TODO refactor method to inherit from governing_method if not specified -# # by constructor argument -- remove from here. -# method = Method.BIDIRECTIONAL -# governing_method = Method.BIDIRECTIONAL -# retry_model_space_search_if_no_models = True -# -# def __init__( -# self, -# *args, -# initial_method: Method = Method.FORWARD, -# **kwargs, -# ): -# super().__init__(*args, **kwargs) -# -# # FIXME cannot access from CLI -# # FIXME probably fine to replace `self.initial_method` -# # with `self.method` here. i.e.: -# # 1. change `method` to `Method.FORWARD -# # 2. change signature to `initial_method: Method = None` -# # 3. change code here to `if initial_method is not None: self.method = initial_method` -# self.initial_method = initial_method -# -# self.method_history: List[Dict[str, Union[Method, List[Model]]]] = [] -# -# def update_method(self, method: Method): -# if method == Method.FORWARD: -# self.direction = 1 -# elif method == Method.BACKWARD: -# self.direction = -1 -# else: -# raise NotImplementedError( -# f'Bidirectional direction must be either `Method.FORWARD` or `Method.BACKWARD`, not {method}.' -# ) -# -# self.method = method -# -# def switch_method(self): -# if self.method == Method.FORWARD: -# method = Method.BACKWARD -# elif self.method == Method.BACKWARD: -# method = Method.FORWARD -# -# self.update_method(method=method) -# -# def setup_before_model_subspaces_search(self): -# # If previous search found no models, then switch method. -# previous_search = ( -# None if not self.method_history else self.method_history[-1] -# ) -# if previous_search is None: -# self.update_method(self.initial_method) -# return -# -# self.update_method(previous_search[METHOD]) -# if not previous_search[MODELS]: -# self.switch_method() -# self.retry_model_space_search_if_no_models = False -# -# def setup_after_model_subspaces_search(self): -# current_search = { -# METHOD: self.method, -# MODELS: self.models, -# } -# self.method_history.append(current_search) -# self.method = self.governing_method -# -# def wrap_search_subspaces(self, search_subspaces): -# def wrapper(): -# def iterate(): -# self.setup_before_model_subspaces_search() -# search_subspaces() -# self.setup_after_model_subspaces_search() -# -# # Repeat until models are found or switching doesn't help. -# iterate() -# while ( -# not self.models and self.retry_model_space_search_if_no_models -# ): -# iterate() -# -# # Reset flag for next time. -# self.retry_model_space_search_if_no_models = True -# -# return wrapper - - class FamosCandidateSpace(CandidateSpace): """The FAMoS method class. @@ -1256,33 +1160,6 @@ def wrapper(): return wrapper -## TODO rewrite so BidirectionalCandidateSpace inherits from ForwardAndBackwardCandidateSpace -## instead -#class ForwardAndBackwardCandidateSpace(BidirectionalCandidateSpace): -# method = Method.FORWARD_AND_BACKWARD -# governing_method = Method.FORWARD_AND_BACKWARD -# retry_model_space_search_if_no_models = False -# -# def __init__(self, *args, **kwargs): -# super().__init__(*args, **kwargs, initial_method=None) -# -# def wrap_search_subspaces(self, search_subspaces): -# def wrapper(): -# for method in [Method.FORWARD, Method.BACKWARD]: -# self.update_method(method=method) -# search_subspaces() -# self.setup_after_model_subspaces_search() -# -# return wrapper -# -# # Disable unused interface -# setup_before_model_subspaces_search = None -# switch_method = None -# -# def setup_after_model_space_search(self): -# pass - - class LateralCandidateSpace(CandidateSpace): """Find models with the same number of estimated parameters.""" diff --git a/petab_select/constants.py b/petab_select/constants.py index 4570f98a..1cd74ff3 100644 --- a/petab_select/constants.py +++ b/petab_select/constants.py @@ -48,12 +48,6 @@ # COMPARED_MODEL_ID = 'compared_'+MODEL_ID YAML_FILENAME = 'yaml' -# FORWARD = 'forward' -# BACKWARD = 'backward' -# BIDIRECTIONAL = 'bidirectional' -# LATERAL = 'lateral' - - # DISTANCES = { # FORWARD: { # 'l1': 1, @@ -70,20 +64,6 @@ # } CRITERIA = 'criteria' -# FIXME remove, change all uses to Enum below -# AIC = 'AIC' -# AICC = 'AICc' -# BIC = 'BIC' -# AKAIKE_INFORMATION_CRITERION = AIC -# CORRECTED_AKAIKE_INFORMATION_CRITERION = AICC -# BAYESIAN_INFORMATION_CRITERION = BIC -# LH = 'LH' -# LLH = 'LLH' -# NLLH = 'NLLH' -# LIKELIHOOD = LH -# LOG_LIKELIHOOD = LLH -# NEGATIVE_LOG_LIKELIHOOD = NLLH - PARAMETERS = 'parameters' # PARAMETER_ESTIMATE = 'parameter_estimate' @@ -119,11 +99,9 @@ class Method(str, Enum): """String literals for model selection methods.""" BACKWARD = 'backward' - #BIDIRECTIONAL = 'bidirectional' BRUTE_FORCE = 'brute_force' FAMOS = 'famos' FORWARD = 'forward' - #FORWARD_AND_BACKWARD = 'forward_and_backward' LATERAL = 'lateral' MOST_DISTANT = 'most_distant' @@ -142,17 +120,13 @@ class Criterion(str, Enum): # Methods that move through model space by taking steps away from some model. STEPWISE_METHODS = [ Method.BACKWARD, - #Method.BIDIRECTIONAL, Method.FORWARD, - #Method.FORWARD_AND_BACKWARD, Method.LATERAL, ] # Methods that require an initial model. INITIAL_MODEL_METHODS = [ Method.BACKWARD, - #Method.BIDIRECTIONAL, Method.FORWARD, - #Method.FORWARD_AND_BACKWARD, Method.LATERAL, ] @@ -160,7 +134,5 @@ class Criterion(str, Enum): VIRTUAL_INITIAL_MODEL = 'virtual_initial_model' VIRTUAL_INITIAL_MODEL_METHODS = [ Method.BACKWARD, - #Method.BIDIRECTIONAL, Method.FORWARD, - #Method.FORWARD_AND_BACKWARD, ] diff --git a/test/candidate_space/test_candidate_space.py b/test/candidate_space/test_candidate_space.py index 6b63e9e9..4512dbc0 100644 --- a/test/candidate_space/test_candidate_space.py +++ b/test/candidate_space/test_candidate_space.py @@ -107,129 +107,3 @@ def model_space(calibrated_model_space) -> pd.DataFrame: df = get_model_space_df(df) model_space = ModelSpace.from_df(df) return model_space - - -#def test_bidirectional( -# model_space, calibrated_model_space, ordered_model_parameterizations -#): -# criterion = Criterion.AIC -# model_id_length = one( -# set([len(model_id) for model_id in calibrated_model_space]) -# ) -# -# candidate_space = BidirectionalCandidateSpace() -# calibrated_models = [] -# -# # Perform bidirectional search until no more models are found. -# search_iterations = 0 -# while True: -# new_calibrated_models = [] -# search_iterations += 1 -# -# # Get models. -# model_space.search(candidate_space) -# -# # Calibrate models. -# for model in candidate_space.models: -# model_id = model.model_subspace_id[-model_id_length:] -# model.set_criterion( -# criterion=criterion, value=calibrated_model_space[model_id] -# ) -# new_calibrated_models.append(model) -# -# # End if no more models were found. -# if not new_calibrated_models: -# break -# -# # Get next predecessor model as best of new models. -# best_new_model = None -# for model in new_calibrated_models: -# if best_new_model is None: -# best_new_model = model -# continue -# if default_compare( -# model0=best_new_model, model1=model, criterion=criterion -# ): -# best_new_model = model -# -# # Set next predecessor and exclusions. -# calibrated_model_hashes = [ -# model.get_hash() for model in calibrated_models -# ] -# candidate_space.reset( -# predecessor_model=best_new_model, -# exclusions=calibrated_model_hashes, -# ) -# -# # exclude calibrated model hashes from model space too? -# model_space.exclude_model_hashes(model_hashes=calibrated_model_hashes) -# -# # Check that expected models are found at each iteration. -# ( -# good_model_parameterizations_ascending, -# bad_model_parameterizations, -# ) = ordered_model_parameterizations -# search_iteration = 0 -# for history_item in candidate_space.method_history: -# models = history_item[MODELS] -# if not models: -# continue -# model_parameterizations = [ -# model.model_subspace_id[-5:] for model in models -# ] -# -# good_model_parameterization = good_model_parameterizations_ascending[ -# search_iteration -# ] -# # The expected good model was found. -# assert good_model_parameterization in model_parameterizations -# model_parameterizations.remove(good_model_parameterization) -# -# if search_iteration == 0: -# # All parameterizations have been correctly identified and removed. -# assert not model_parameterizations -# search_iteration += 1 -# continue -# -# previous_model_parameterization = ( -# good_model_parameterizations_ascending[search_iteration - 1] -# ) -# -# # The expected bad model is also found. -# # If a bad model is the same dimension and also represents a similar stepwise move away from the previous -# # model parameterization, it should also be in the parameterizations. -# for bad_model_parameterization in bad_model_parameterizations: -# # Skip if different dimensions -# if sum(map(int, bad_model_parameterization)) != sum( -# map(int, good_model_parameterization) -# ): -# continue -# # Skip if different distances from previous model parameterization -# if sum( -# [ -# a != b -# for a, b in zip( -# bad_model_parameterization, -# previous_model_parameterization, -# ) -# ] -# ) != sum( -# [ -# a != b -# for a, b in zip( -# good_model_parameterization, -# previous_model_parameterization, -# ) -# ] -# ): -# continue -# assert bad_model_parameterization in model_parameterizations -# model_parameterizations.remove(bad_model_parameterization) -# -# # All parameterizations have been correctly identified and removed. -# assert not model_parameterizations -# search_iteration += 1 -# -# # End test if all good models were found in the correct order. -# if search_iteration >= len(good_model_parameterizations_ascending): -# break