Source code for mala.descriptors.minterpy_descriptors
-"""Gaussian descriptor class."""
+"""Minterpy descriptor class."""
import os
@@ -93,12 +93,16 @@ Source code for mala.descriptors.minterpy_descriptors
from mala.descriptors.lammps_utils import extract_compute_np
from mala.descriptors.descriptor import Descriptor
from mala.descriptors.atomic_density import AtomicDensity
+from mala.common.parallelizer import parallel_warn
[docs]
class MinterpyDescriptors(Descriptor):
- """Class for calculation and parsing of Gaussian descriptors.
+ """
+ Class for calculation and parsing of Minterpy descriptors.
+
+ Marked for deprecation.
Parameters
----------
@@ -108,18 +112,16 @@ Source code for mala.descriptors.minterpy_descriptors
def __init__(self, parameters):
super(MinterpyDescriptors, self).__init__(parameters)
- self.verbosity = parameters.verbosity
+ parallel_warn(
+ "Minterpy descriptors will be deprecated starting with MALA v1.4.0",
+ category=FutureWarning,
+ )
@property
def data_name(self):
"""Get a string that describes the target (for e.g. metadata)."""
return "Minterpy"
- @property
- def feature_size(self):
- """Get the feature dimension of this data."""
- return self.fingerprint_length
-
[docs]
@staticmethod
@@ -240,11 +242,11 @@ Source code for mala.descriptors.minterpy_descriptors
],
dtype=np.float64,
)
- self.fingerprint_length = (
+ self.feature_size = (
len(self.parameters.minterpy_point_list) + coord_length
)
- self.fingerprint_length = len(self.parameters.minterpy_point_list)
+ self.feature_size = len(self.parameters.minterpy_point_list)
# Perform one LAMMPS call for each point in the Minterpy point list.
for idx, point in enumerate(self.parameters.minterpy_point_list):
# Shift the atoms in negative direction of the point(s) we actually
@@ -257,7 +259,7 @@ Source code for mala.descriptors.minterpy_descriptors
self.setup_lammps_tmp_files("minterpy", outdir)
ase.io.write(
- self.lammps_temporary_input, self.atoms, format=lammps_format
+ self._lammps_temporary_input, self._atoms, format=lammps_format
)
# Create LAMMPS instance.
diff --git a/_modules/mala/interfaces/ase_calculator.html b/_modules/mala/interfaces/ase_calculator.html
index 3b02c33bf..e750f77f8 100644
--- a/_modules/mala/interfaces/ase_calculator.html
+++ b/_modules/mala/interfaces/ase_calculator.html
@@ -119,9 +119,21 @@ Source code for mala.interfaces.ase_calculator
the neural network), calculator can access all important data such as
temperature, number of electrons, etc. that might not be known simply
from the atomic positions.
+
+ predictor : mala.network.predictor.Predictor
+ A Predictor class object to be used for the underlying MALA
+ predictions.
+
+ Attributes
+ ----------
+ mala_parameters : mala.common.parameters.Parameters
+ MALA parameters used for predictions.
+
+ last_energy_contributions : dict
+ Contains all total energy contributions for the last prediction.
"""
- implemented_properties = ["energy", "forces"]
+ implemented_properties = ["energy"]
def __init__(
self,
@@ -140,21 +152,21 @@
Source code for mala.interfaces.ase_calculator
"The MALA calculator currently only works with the LDOS."
)
- self.network: Network = network
- self.data_handler: DataHandler = data
+ self._network: Network = network
+ self._data_handler: DataHandler = data
# Prepare for prediction.
if predictor is None:
- self.predictor = Predictor(
- self.mala_parameters, self.network, self.data_handler
+ self._predictor = Predictor(
+ self.mala_parameters, self._network, self._data_handler
)
else:
- self.predictor = predictor
+ self._predictor = predictor
if reference_data is not None:
# Get critical values from a reference file (cutoff,
# temperature, etc.)
- self.data_handler.target_calculator.read_additional_calculation_data(
+ self._data_handler.target_calculator.read_additional_calculation_data(
reference_data
)
@@ -178,6 +190,11 @@
Source code for mala.interfaces.ase_calculator
path : str
Path where the model is saved.
+
+ Returns
+ -------
+ calculator : mala.interfaces.calculator.Calculator
+ The calculator object.
"""
parallel_warn(
"MALA.load_model() will be deprecated in MALA v1.4.0."
@@ -205,6 +222,11 @@
Source code for mala.interfaces.ase_calculator
path : str
Path where the model is saved.
+
+ Returns
+ -------
+ calculator : mala.interfaces.calculator.Calculator
+ The calculator object.
"""
loaded_params, loaded_network, new_datahandler, loaded_runner = (
Predictor.load_run(run_name, path=path)
@@ -245,10 +267,10 @@
Source code for mala.interfaces.ase_calculator
Calculator.calculate(self, atoms, properties, system_changes)
# Get the LDOS from the NN.
- ldos = self.predictor.predict_for_atoms(atoms)
+ ldos = self._predictor.predict_for_atoms(atoms)
# Use the LDOS determined DOS and density to get energy and forces.
- ldos_calculator: LDOS = self.data_handler.target_calculator
+ ldos_calculator: LDOS = self._data_handler.target_calculator
ldos_calculator.read_from_array(ldos)
self.results["energy"] = ldos_calculator.total_energy
energy, self.last_energy_contributions = (
@@ -293,19 +315,19 @@
Source code for mala.interfaces.ase_calculator
if "rdf" in properties:
self.results["rdf"] = (
- self.data_handler.target_calculator.get_radial_distribution_function(
+ self._data_handler.target_calculator.get_radial_distribution_function(
atoms
)
)
if "tpcf" in properties:
self.results["tpcf"] = (
- self.data_handler.target_calculator.get_three_particle_correlation_function(
+ self._data_handler.target_calculator.get_three_particle_correlation_function(
atoms
)
)
if "static_structure_factor" in properties:
self.results["static_structure_factor"] = (
- self.data_handler.target_calculator.get_static_structure_factor(
+ self._data_handler.target_calculator.get_static_structure_factor(
atoms
)
)
@@ -332,7 +354,7 @@
Source code for mala.interfaces.ase_calculator
Path where the calculator should be saved.
"""
- self.predictor.save_run(
+ self._predictor.save_run(
filename, path=path, additional_calculation_data=True
)
diff --git a/_modules/mala/network/acsd_analyzer.html b/_modules/mala/network/acsd_analyzer.html
index 837d520ee..5dbed83b5 100644
--- a/_modules/mala/network/acsd_analyzer.html
+++ b/_modules/mala/network/acsd_analyzer.html
@@ -134,16 +134,18 @@ Source code for mala.network.acsd_analyzer
):
super(ACSDAnalyzer, self).__init__(params)
# Calculators used to parse data from compatible files.
- self.target_calculator = target_calculator
- if self.target_calculator is None:
- self.target_calculator = Target(params)
- self.descriptor_calculator = descriptor_calculator
- if self.descriptor_calculator is None:
- self.descriptor_calculator = Descriptor(params)
+ self._target_calculator = target_calculator
+ if self._target_calculator is None:
+ self._target_calculator = Target(params)
+ self._descriptor_calculator = descriptor_calculator
+ if self._descriptor_calculator is None:
+ self._descriptor_calculator = Descriptor(params)
if (
- not isinstance(self.descriptor_calculator, Bispectrum)
- and not isinstance(self.descriptor_calculator, AtomicDensity)
- and not isinstance(self.descriptor_calculator, MinterpyDescriptors)
+ not isinstance(self._descriptor_calculator, Bispectrum)
+ and not isinstance(self._descriptor_calculator, AtomicDensity)
+ and not isinstance(
+ self._descriptor_calculator, MinterpyDescriptors
+ )
):
raise Exception(
"Cannot calculate ACSD for the selected descriptors."
@@ -155,10 +157,10 @@ Source code for mala.network.acsd_analyzer
self.__snapshot_units = []
# Filled after the analysis.
- self.labels = []
- self.study = []
- self.reduced_study = None
- self.internal_hyperparam_list = None
+ self._labels = []
+ self._study = []
+ self._reduced_study = None
+ self._internal_hyperparam_list = None
[docs]
@@ -282,7 +284,7 @@ Source code for mala.network.acsd_analyzer
# Prepare the hyperparameter lists.
self._construct_hyperparam_list()
hyperparameter_tuples = list(
- itertools.product(*self.internal_hyperparam_list)
+ itertools.product(*self._internal_hyperparam_list)
)
# Perform the ACSD analysis separately for each snapshot.
@@ -301,14 +303,14 @@ Source code for mala.network.acsd_analyzer
)
for idx, hyperparameter_tuple in enumerate(hyperparameter_tuples):
- if isinstance(self.descriptor_calculator, Bispectrum):
+ if isinstance(self._descriptor_calculator, Bispectrum):
self.params.descriptors.bispectrum_cutoff = (
hyperparameter_tuple[0]
)
self.params.descriptors.bispectrum_twojmax = (
hyperparameter_tuple[1]
)
- elif isinstance(self.descriptor_calculator, AtomicDensity):
+ elif isinstance(self._descriptor_calculator, AtomicDensity):
self.params.descriptors.atomic_density_cutoff = (
hyperparameter_tuple[0]
)
@@ -316,7 +318,7 @@ Source code for mala.network.acsd_analyzer
hyperparameter_tuple[1]
)
elif isinstance(
- self.descriptor_calculator, MinterpyDescriptors
+ self._descriptor_calculator, MinterpyDescriptors
):
self.params.descriptors.atomic_density_cutoff = (
hyperparameter_tuple[0]
@@ -362,11 +364,11 @@ Source code for mala.network.acsd_analyzer
)
outstring = "["
- for label_id, label in enumerate(self.labels):
+ for label_id, label in enumerate(self._labels):
outstring += (
label + ": " + str(hyperparameter_tuple[label_id])
)
- if label_id < len(self.labels) - 1:
+ if label_id < len(self._labels) - 1:
outstring += ", "
outstring += "]"
best_trial_string = ". No suitable trial found yet."
@@ -388,34 +390,34 @@ Source code for mala.network.acsd_analyzer
)
if get_rank() == 0:
- self.study.append(current_list)
+ self._study.append(current_list)
if get_rank() == 0:
- self.study = np.mean(self.study, axis=0)
+ self._study = np.mean(self._study, axis=0)
# TODO: Does this even make sense for the minterpy descriptors?
if return_plotting:
results_to_plot = []
- if len(self.internal_hyperparam_list) == 2:
- len_first_dim = len(self.internal_hyperparam_list[0])
- len_second_dim = len(self.internal_hyperparam_list[1])
+ if len(self._internal_hyperparam_list) == 2:
+ len_first_dim = len(self._internal_hyperparam_list[0])
+ len_second_dim = len(self._internal_hyperparam_list[1])
for i in range(0, len_first_dim):
results_to_plot.append(
- self.study[
+ self._study[
i * len_second_dim : (i + 1) * len_second_dim,
2:,
]
)
- if isinstance(self.descriptor_calculator, Bispectrum):
+ if isinstance(self._descriptor_calculator, Bispectrum):
return results_to_plot, {
- "twojmax": self.internal_hyperparam_list[1],
- "cutoff": self.internal_hyperparam_list[0],
+ "twojmax": self._internal_hyperparam_list[1],
+ "cutoff": self._internal_hyperparam_list[0],
}
- if isinstance(self.descriptor_calculator, AtomicDensity):
+ if isinstance(self._descriptor_calculator, AtomicDensity):
return results_to_plot, {
- "sigma": self.internal_hyperparam_list[1],
- "cutoff": self.internal_hyperparam_list[0],
+ "sigma": self._internal_hyperparam_list[1],
+ "cutoff": self._internal_hyperparam_list[0],
}
@@ -429,9 +431,9 @@ Source code for mala.network.acsd_analyzer
hyperparameter optimizer was created.
"""
if get_rank() == 0:
- minimum_acsd = self.study[np.argmin(self.study[:, -1])]
- if len(self.internal_hyperparam_list) == 2:
- if isinstance(self.descriptor_calculator, Bispectrum):
+ minimum_acsd = self._study[np.argmin(self._study[:, -1])]
+ if len(self._internal_hyperparam_list) == 2:
+ if isinstance(self._descriptor_calculator, Bispectrum):
self.params.descriptors.bispectrum_cutoff = minimum_acsd[0]
self.params.descriptors.bispectrum_twojmax = int(
minimum_acsd[1]
@@ -447,7 +449,7 @@ Source code for mala.network.acsd_analyzer
"Bispectrum cutoff: ",
self.params.descriptors.bispectrum_cutoff,
)
- if isinstance(self.descriptor_calculator, AtomicDensity):
+ if isinstance(self._descriptor_calculator, AtomicDensity):
self.params.descriptors.atomic_density_cutoff = (
minimum_acsd[0]
)
@@ -465,8 +467,10 @@ Source code for mala.network.acsd_analyzer
"Atomic density cutoff: ",
self.params.descriptors.atomic_density_cutoff,
)
- elif len(self.internal_hyperparam_list) == 5:
- if isinstance(self.descriptor_calculator, MinterpyDescriptors):
+ elif len(self._internal_hyperparam_list) == 5:
+ if isinstance(
+ self._descriptor_calculator, MinterpyDescriptors
+ ):
self.params.descriptors.atomic_density_cutoff = (
minimum_acsd[0]
)
@@ -508,7 +512,7 @@ Source code for mala.network.acsd_analyzer
def _construct_hyperparam_list(self):
- if isinstance(self.descriptor_calculator, Bispectrum):
+ if isinstance(self._descriptor_calculator, Bispectrum):
if (
list(
map(
@@ -549,10 +553,10 @@ Source code for mala.network.acsd_analyzer
).index(True)
].choices
- self.internal_hyperparam_list = [first_dim_list, second_dim_list]
- self.labels = ["cutoff", "twojmax"]
+ self._internal_hyperparam_list = [first_dim_list, second_dim_list]
+ self._labels = ["cutoff", "twojmax"]
- elif isinstance(self.descriptor_calculator, AtomicDensity):
+ elif isinstance(self._descriptor_calculator, AtomicDensity):
if (
list(
map(
@@ -596,10 +600,10 @@ Source code for mala.network.acsd_analyzer
)
).index(True)
].choices
- self.internal_hyperparam_list = [first_dim_list, second_dim_list]
- self.labels = ["cutoff", "sigma"]
+ self._internal_hyperparam_list = [first_dim_list, second_dim_list]
+ self._labels = ["cutoff", "sigma"]
- elif isinstance(self.descriptor_calculator, MinterpyDescriptors):
+ elif isinstance(self._descriptor_calculator, MinterpyDescriptors):
if (
list(
map(
@@ -708,14 +712,14 @@ Source code for mala.network.acsd_analyzer
).index(True)
].choices
- self.internal_hyperparam_list = [
+ self._internal_hyperparam_list = [
first_dim_list,
second_dim_list,
third_dim_list,
fourth_dim_list,
fifth_dim_list,
]
- self.labels = [
+ self._labels = [
"cutoff",
"sigma",
"minterpy_cutoff",
@@ -735,7 +739,7 @@ Source code for mala.network.acsd_analyzer
if description["input"] == "espresso-out":
descriptor_calculation_kwargs["units"] = original_units["input"]
tmp_input, local_size = (
- self.descriptor_calculator.calculate_from_qe_out(
+ self._descriptor_calculator.calculate_from_qe_out(
snapshot["input"], **descriptor_calculation_kwargs
)
)
@@ -749,7 +753,7 @@ Source code for mala.network.acsd_analyzer
"Unknown file extension, cannot convert descriptor"
)
if self.params.descriptors._configuration["mpi"]:
- tmp_input = self.descriptor_calculator.gather_descriptors(
+ tmp_input = self._descriptor_calculator.gather_descriptors(
tmp_input
)
@@ -773,7 +777,7 @@ Source code for mala.network.acsd_analyzer
target_calculator_kwargs["units"] = original_units["output"]
target_calculator_kwargs["use_memmap"] = memmap
# If no units are provided we just assume standard units.
- tmp_output = self.target_calculator.read_from_cube(
+ tmp_output = self._target_calculator.read_from_cube(
snapshot["output"], **target_calculator_kwargs
)
@@ -781,19 +785,19 @@ Source code for mala.network.acsd_analyzer
target_calculator_kwargs["units"] = original_units["output"]
target_calculator_kwargs["use_memmap"] = memmap
# If no units are provided we just assume standard units.
- tmp_output = self.target_calculator.read_from_xsf(
+ tmp_output = self._target_calculator.read_from_xsf(
snapshot["output"], **target_calculator_kwargs
)
elif description["output"] == "numpy":
if get_rank() == 0:
- tmp_output = self.target_calculator.read_from_numpy_file(
+ tmp_output = self._target_calculator.read_from_numpy_file(
snapshot["output"], units=original_units["output"]
)
elif description["output"] == "openpmd":
if get_rank() == 0:
- tmp_output = self.target_calculator.read_from_numpy_file(
+ tmp_output = self._target_calculator.read_from_numpy_file(
snapshot["output"], units=original_units["output"]
)
else:
diff --git a/_modules/mala/network/hyper_opt.html b/_modules/mala/network/hyper_opt.html
index 5d9537bfe..6d8f6a6a6 100644
--- a/_modules/mala/network/hyper_opt.html
+++ b/_modules/mala/network/hyper_opt.html
@@ -109,6 +109,11 @@ Source code for mala.network.hyper_opt
use_pkl_checkpoints : bool
If true, .pkl checkpoints will be created.
+
+ Attributes
+ ----------
+ params : mala.common.parametes.Parameters
+ MALA Parameters object.
"""
def __new__(cls, params: Parameters, data=None, use_pkl_checkpoints=False):
@@ -158,9 +163,9 @@ Source code for mala.network.hyper_opt
self, params: Parameters, data=None, use_pkl_checkpoints=False
):
self.params: Parameters = params
- self.data_handler = data
- self.objective = ObjectiveBase(self.params, self.data_handler)
- self.use_pkl_checkpoints = use_pkl_checkpoints
+ self._data_handler = data
+ self._objective = ObjectiveBase(self.params, self._data_handler)
+ self._use_pkl_checkpoints = use_pkl_checkpoints
[docs]
@@ -252,7 +257,7 @@ Source code for mala.network.hyper_opt
The parameters will be written to the parameter object with which the
hyperparameter optimizer was created.
"""
- self.objective.parse_trial(trial)
+ self._objective.parse_trial(trial)
def _save_params_and_scaler(self):
@@ -263,12 +268,12 @@ Source code for mala.network.hyper_opt
oscaler_name = (
self.params.hyperparameters.checkpoint_name + "_oscaler.pkl"
)
- self.data_handler.input_data_scaler.save(iscaler_name)
- self.data_handler.output_data_scaler.save(oscaler_name)
+ self._data_handler.input_data_scaler.save(iscaler_name)
+ self._data_handler.output_data_scaler.save(oscaler_name)
# For the parameters we have to make sure we choose the correct
# format.
- if self.use_pkl_checkpoints:
+ if self._use_pkl_checkpoints:
param_name = (
self.params.hyperparameters.checkpoint_name + "_params.pkl"
)
@@ -300,7 +305,6 @@ Source code for mala.network.hyper_opt
-------
checkpoint_exists : bool
True if the checkpoint exists, False otherwise.
-
"""
iscaler_name = checkpoint_name + "_iscaler.pkl"
oscaler_name = checkpoint_name + "_oscaler.pkl"
diff --git a/_modules/mala/network/hyper_opt_naswot.html b/_modules/mala/network/hyper_opt_naswot.html
index 7dd7b1be4..d3080d1e3 100644
--- a/_modules/mala/network/hyper_opt_naswot.html
+++ b/_modules/mala/network/hyper_opt_naswot.html
@@ -85,8 +85,9 @@ Source code for mala.network.hyper_opt_naswot
import itertools
-import optuna
+from functools import cached_property
import numpy as np
+import optuna
from mala.common.parallelizer import (
printout,
@@ -94,6 +95,7 @@ Source code for mala.network.hyper_opt_naswot
get_size,
get_comm,
barrier,
+ parallel_warn,
)
from mala.network.hyper_opt import HyperOpt
from mala.network.objective_naswot import ObjectiveNASWOT
@@ -118,11 +120,10 @@ Source code for mala.network.hyper_opt_naswot
def __init__(self, params, data):
super(HyperOptNASWOT, self).__init__(params, data)
- self.objective = None
- self.trial_losses = None
- self.best_trial = None
- self.trial_list = None
- self.ignored_hyperparameters = [
+ self._objective = None
+ self._trial_losses = None
+ self._trial_list = None
+ self._ignored_hyperparameters = [
"learning_rate",
"optimizer",
"mini_batch_size",
@@ -132,8 +133,68 @@ Source code for mala.network.hyper_opt_naswot
]
# For parallelization.
- self.first_trial = None
- self.last_trial = None
+ self._first_trial = None
+ self._last_trial = None
+
+ @property
+ def best_trial_index(self):
+ """
+ Get the index and loss of best trial determined in this NASWOT run.
+
+ This property is read only, and will be recomputed.
+
+ Returns
+ -------
+ best_trial_index : list
+ A list containing [0] the best trial index and [1] the best
+ trial loss.
+ """
+ if self._trial_losses is None:
+ parallel_warn(
+ "Trial list is not yet computed, cannot determine "
+ "best trial."
+ )
+ return [-1, np.inf]
+
+ if self.params.use_mpi:
+ comm = get_comm()
+ local_result = np.array(
+ [
+ float(np.argmax(self._trial_losses) + self._first_trial),
+ np.max(self._trial_losses),
+ ]
+ )
+ all_results = comm.allgather(local_result)
+ max_on_node = np.argmax(np.array(all_results)[:, 1])
+ return [
+ int(all_results[max_on_node][0]),
+ all_results[max_on_node][1],
+ ]
+ else:
+ return [np.argmax(self._trial_losses), np.max(self._trial_losses)]
+
+ @best_trial_index.setter
+ def best_trial_index(self, value):
+ pass
+
+ @property
+ def best_trial(self):
+ """
+ Get the best trial determined in this NASWOT run.
+
+ This property is read only, and will be recomputed.
+ """
+ if self._trial_losses is None:
+ parallel_warn(
+ "Trial list is not yet computed, cannot determine "
+ "best trial."
+ )
+ return None
+ return self._trial_list[self.best_trial_index[0]]
+
+ @best_trial.setter
+ def best_trial(self, value):
+ pass
[docs]
@@ -149,6 +210,11 @@ Source code for mala.network.hyper_opt_naswot
----------
trial_list : list
A list containing trials from either HyperOptOptuna or HyperOptOAT.
+
+ Returns
+ -------
+ best_trial_loss : float
+ Loss of the best trial.
"""
# The minibatch size can not vary in the analysis.
# This check ensures that e.g. optuna results can be used.
@@ -163,29 +229,29 @@ Source code for mala.network.hyper_opt_naswot
# Ideally, this type of HO is called with a list of trials for which