Skip to content

Commit

Permalink
Small final adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomDefaultUser committed Nov 27, 2024
1 parent 632fe26 commit 1790092
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions mala/network/acsd_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def _update_logging(self, score, index):
self.best_score = score
self.best_trial_index = index

def get_best_trial(self):
"""Different from best_trial because of parallelization."""
def _get_best_trial(self):
"""Determine the best trial as given by this study."""
return self._study[np.argmin(self._study[:, -1])]

@staticmethod
Expand Down
30 changes: 22 additions & 8 deletions mala/network/descriptor_scoring_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DescriptorScoringOptimizer(HyperOpt, ABC):
"""
Base class for all training-free descriptor hyperparameter optimizers.
These optimizer use alternative metrics (ACSD, mutual information, etc.
These optimizer use alternative metrics ACSD, mutual information, etc.
to tune descriptor hyperparameters.
Parameters
Expand All @@ -50,6 +50,14 @@ class DescriptorScoringOptimizer(HyperOpt, ABC):
Target calculator used for parsing/converting target data. If None,
the target calculator will be created by this object using the
parameters provided. Default: None
Attributes
----------
best_score : float
Score associated with best-performing trial.
best_trial_index : int
Index of best-performing trial
"""

def __init__(
Expand Down Expand Up @@ -199,7 +207,7 @@ def perform_study(
Perform the study, i.e. the optimization.
This is done by sampling different descriptors, calculated with
different hyperparameters and then calculating the ACSD.
different hyperparameters and then calculating some surrogate score.
"""
# Prepare the hyperparameter lists.
self._construct_hyperparam_list()
Expand Down Expand Up @@ -317,14 +325,20 @@ def perform_study(
}

def set_optimal_parameters(self):
"""
Set optimal parameters.
This function will write the determined hyperparameters directly to
MALA parameters object referenced in this class.
"""
if get_rank() == 0:
best_trial = self.get_best_trial()
best_trial = self._get_best_trial()
minimum_score = self._study[np.argmin(self._study[:, -1])]
if isinstance(self._descriptor_calculator, Bispectrum):
self.params.descriptors.bispectrum_cutoff = best_trial[0]
self.params.descriptors.bispectrum_twojmax = int(best_trial[1])
printout(
"ACSD analysis finished, optimal parameters: ",
"Descriptor scoring analysis finished, optimal parameters: ",
)
printout(
"Bispectrum twojmax: ",
Expand All @@ -338,7 +352,7 @@ def set_optimal_parameters(self):
self.params.descriptors.atomic_density_cutoff = best_trial[0]
self.params.descriptors.atomic_density_sigma = best_trial[1]
printout(
"ACSD analysis finished, optimal parameters: ",
"Descriptor scoring analysis finished, optimal parameters: ",
)
printout(
"Atomic density sigma: ",
Expand All @@ -350,8 +364,8 @@ def set_optimal_parameters(self):
)

@abstractmethod
def get_best_trial(self):
"""Different from best_trial because of parallelization."""
def _get_best_trial(self):
"""Determine the best trial as given by this study."""
pass

def _construct_hyperparam_list(self):
Expand Down Expand Up @@ -486,7 +500,7 @@ def _load_target(
self.params.descriptors._configuration["mpi"]
and file_based_communication
):
memmap = "acsd.out.npy_temp"
memmap = "descriptor_scoring.out.npy_temp"

target_calculator_kwargs = {}

Expand Down
10 changes: 5 additions & 5 deletions mala/network/mutual_information_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def __init__(
descriptor_calculator=descriptor_calculator,
)

def get_best_trial(self):
"""Different from best_trial because of parallelization."""
def _get_best_trial(self):
"""Determine the best trial as given by this study."""
return self._study[np.argmax(self._study[:, -1])]

def _update_logging(self, score, index):
Expand Down Expand Up @@ -131,7 +131,7 @@ def _calculate_mutual_information(
return mi

@staticmethod
def normalize(data):
def _normalize(data):
mean = np.mean(data, axis=0)
std = np.std(data, axis=0)
std_nonzero = std > 1e-6
Expand Down Expand Up @@ -161,8 +161,8 @@ def _mutual_information(
dim_X = X.shape[-1]
rand_subset = np.random.permutation(n)[:n_samples]
if normalize_data:
X = MutualInformationAnalyzer.normalize(X)
Y = MutualInformationAnalyzer.normalize(Y)
X = MutualInformationAnalyzer._normalize(X)
Y = MutualInformationAnalyzer._normalize(Y)
X = X[rand_subset]
Y = Y[rand_subset]
XY = np.concatenate([X, Y], axis=1)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

extras = {
"dev": ["bump2version"],
"opt": ["oapackage"],
"opt": ["oapackage", "scikit-learn"],
"test": ["pytest", "pytest-cov"],
"doc": open("docs/requirements.txt").read().splitlines(),
"experimental": ["asap3", "dftpy", "minterpy"],
Expand Down

0 comments on commit 1790092

Please sign in to comment.