npl.calculators package
Submodules
npl.calculators.base_calculator module
npl.calculators.energy_calculator module
- class npl.calculators.energy_calculator.BayesianRRCalculator(feature_key)[source]
Bases:
EnergyCalculatorBayesianRRCalculator is a class for performing Bayesian Ridge Regression (BRR) on nanoparticle datasets. Attributes: ———– ridge : BayesianRidge
The Bayesian Ridge Regression model. The key used to store energy values in the nanoparticles.
- feature_keystr
The key used to extract feature vectors from the nanoparticles.
Methods:
- __init__(self, feature_key):
Initializes the BayesianRRCalculator with a given feature key.
- fit(self, training_set, energy_key, validation_set=None):
Fits the Bayesian Ridge Regression model using the provided training set.
- validate(self, validation_set, energy_key):
Validates the Bayesian Ridge Regression model using the provided validation set.
- get_coefficients(self):
Returns the coefficients of the Bayesian Ridge Regression model.
- set_coefficients(self, new_coefficients):
Sets the coefficients of the Bayesian Ridge Regression model.
- set_feature_key(self, feature_key):
Sets the feature key used to extract feature vectors from the nanoparticles.
- compute_energy(self, particle):
Computes the energy of a given nanoparticle using the Bayesian Ridge Regression model.
Examples:
>>> from npl.calculators.energy_calculator import BayesianRRCalculator >>> calculator = BayesianRRCalculator(feature_key='some_feature_key') >>> training_set = [...] # List of Nanoparticles for training >>> validation_set = [...] # List of Nanoparticles for validation >>> calculator.fit(training_set, energy_key='some_energy_key', validation_set=validation_set) >>> coefficients = calculator.get_coefficients() >>> calculator.set_coefficients(new_coefficients) >>> calculator.set_feature_key('new_feature_key') >>> energy = calculator.compute_energy(some_nanoparticle)
- compute_energy(particle)[source]
Compute the energy using BRR.
Assumes that a feature vector with key=self.feature_key is present in the particle.
- Parameters:
particle : Nanoparticle
- fit(training_set, energy_key, validation_set=None)[source]
Fit the Bayesian Ridge Regression (BRR) model.
Parameters:
- training_setlist of Nanoparticles
The dataset used for training the model.
- energy_keystr
The key used to extract energy values from the nanoparticles.
- validation_setfloat or list of Nanoparticles, optional
If a float is provided, it represents the fraction of the training set to be used as the validation set. If a list is provided, it is used as the validation set directly. Default is None.
Returns:
None
- class npl.calculators.energy_calculator.EMTCalculator(fmax=0.01, steps=50, relax_atoms=False)[source]
Bases:
EnergyCalculatorEMTCalculator is a class for calculating the energy of a nanoparticle using the Effective Medium Theory (EMT) method.
- Attributes:
fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. energy_key (str): The key used to store the calculated energy in the particle object. Default is ‘EMT’. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.
- Methods:
- compute_energy(particle):
Compute the energy of the given nanoparticle using EMT.
Initialize the EMTCalculator with the given parameters.
fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.
Compute the energy using EMT.
BFGS is used for relaxation. By default, the atoms are NOT relaxed, i.e., the
particle (Nanoparticle): The nanoparticle object for which the energy is to be calculated.
- Example:
>>> from npl.calculators.energy_calculator import EMTCalculator >>> from npl.nanoparticle import Nanoparticle >>> particle = Nanoparticle() >>> particle.truncated_octahedron(7,2, {'Au' : 0.5, 'Ag' : 0.5}) >>> calculator = EMTCalculator(fmax=0.02, steps=100, relax_atoms=True) >>> calculator.compute_energy(particle) >>> energy = particle.get_energy('EMT') >>> print(f"Computed energy: {energy}")
- class npl.calculators.energy_calculator.EnergyCalculator[source]
Bases:
objectBase class for an energy calculator.
Valid implementations have to implement the compute_energy(particle) function. Energies are saved in the particle object with the key of the respective calculator.
- class npl.calculators.energy_calculator.GPRCalculator(feature_key, kernel=None, alpha=0.01, normalize_y=True)[source]
Bases:
EnergyCalculatorEnergy calculator using global feature vectors and Gaussian Process Regression.
- class npl.calculators.energy_calculator.MixingEnergyCalculator(base_calculator=None, mixing_parameters=None, recompute_energies=False)[source]
Bases:
EnergyCalculatorCompute the mixing energy using an arbitrary energy model.
For the original energy model it is assumed that all previous steps in the energy pipeline, e.g. calculation of local environment, feature vectors etc. has been carried out.
npl.calculators.top_calculator module
- class npl.calculators.top_calculator.TOPCalculator(feature_key: str, stoichiometry: str = None, model_paths: list | str = None, feature_classifier: <module 'npl.descriptors' from '/home/docs/checkouts/readthedocs.org/user_builds/nplib/checkouts/stable/npl/descriptors/__init__.py'> = None, **kwargs)[source]
Bases:
CalculatorA class representing a calculator for performing relaxation calculations using the ASE library.
- Parameters:
calculator (Calculator): The calculator object used for performing the calculations. fmax (float): The maximum force tolerance for the relaxation.
- calculate(atoms)[source]
Do the calculation.
- properties: list of str
List of what needs to be calculated. Can be any combination of ‘energy’, ‘forces’, ‘stress’, ‘dipole’, ‘charges’, ‘magmom’ and ‘magmoms’.
- system_changes: list of str
List of what has changed since last calculation. Can be any combination of these six: ‘positions’, ‘numbers’, ‘cell’, ‘pbc’, ‘initial_charges’ and ‘initial_magmoms’.
Subclasses need to implement this, but can ignore properties and system_changes if they want. Calculated properties should be inserted into results dictionary like shown in this dummy example:
self.results = {'energy': 0.0, 'forces': np.zeros((len(atoms), 3)), 'stress': np.zeros(6), 'dipole': np.zeros(3), 'charges': np.zeros(len(atoms)), 'magmom': 0.0, 'magmoms': np.zeros(len(atoms))}
The subclass implementation should first call this implementation to set the atoms attribute and create any missing directories.
Module contents
- class npl.calculators.BayesianRRCalculator(feature_key)[source]
Bases:
EnergyCalculatorBayesianRRCalculator is a class for performing Bayesian Ridge Regression (BRR) on nanoparticle datasets. Attributes: ———– ridge : BayesianRidge
The Bayesian Ridge Regression model. The key used to store energy values in the nanoparticles.
- feature_keystr
The key used to extract feature vectors from the nanoparticles.
Methods:
- __init__(self, feature_key):
Initializes the BayesianRRCalculator with a given feature key.
- fit(self, training_set, energy_key, validation_set=None):
Fits the Bayesian Ridge Regression model using the provided training set.
- validate(self, validation_set, energy_key):
Validates the Bayesian Ridge Regression model using the provided validation set.
- get_coefficients(self):
Returns the coefficients of the Bayesian Ridge Regression model.
- set_coefficients(self, new_coefficients):
Sets the coefficients of the Bayesian Ridge Regression model.
- set_feature_key(self, feature_key):
Sets the feature key used to extract feature vectors from the nanoparticles.
- compute_energy(self, particle):
Computes the energy of a given nanoparticle using the Bayesian Ridge Regression model.
Examples:
>>> from npl.calculators.energy_calculator import BayesianRRCalculator >>> calculator = BayesianRRCalculator(feature_key='some_feature_key') >>> training_set = [...] # List of Nanoparticles for training >>> validation_set = [...] # List of Nanoparticles for validation >>> calculator.fit(training_set, energy_key='some_energy_key', validation_set=validation_set) >>> coefficients = calculator.get_coefficients() >>> calculator.set_coefficients(new_coefficients) >>> calculator.set_feature_key('new_feature_key') >>> energy = calculator.compute_energy(some_nanoparticle)
- compute_energy(particle)[source]
Compute the energy using BRR.
Assumes that a feature vector with key=self.feature_key is present in the particle.
- Parameters:
particle : Nanoparticle
- fit(training_set, energy_key, validation_set=None)[source]
Fit the Bayesian Ridge Regression (BRR) model.
Parameters:
- training_setlist of Nanoparticles
The dataset used for training the model.
- energy_keystr
The key used to extract energy values from the nanoparticles.
- validation_setfloat or list of Nanoparticles, optional
If a float is provided, it represents the fraction of the training set to be used as the validation set. If a list is provided, it is used as the validation set directly. Default is None.
Returns:
None
- class npl.calculators.EMTCalculator(fmax=0.01, steps=50, relax_atoms=False)[source]
Bases:
EnergyCalculatorEMTCalculator is a class for calculating the energy of a nanoparticle using the Effective Medium Theory (EMT) method.
- Attributes:
fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. energy_key (str): The key used to store the calculated energy in the particle object. Default is ‘EMT’. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.
- Methods:
- compute_energy(particle):
Compute the energy of the given nanoparticle using EMT.
Initialize the EMTCalculator with the given parameters.
fmax (float): The maximum force tolerance for the BFGS optimizer. Default is 0.01. steps (int): The maximum number of steps for the BFGS optimizer. Default is 50. relax_atoms (bool): Flag indicating whether to relax the atoms during energy calculation. Default is False.
Compute the energy using EMT.
BFGS is used for relaxation. By default, the atoms are NOT relaxed, i.e., the
particle (Nanoparticle): The nanoparticle object for which the energy is to be calculated.
- Example:
>>> from npl.calculators.energy_calculator import EMTCalculator >>> from npl.nanoparticle import Nanoparticle >>> particle = Nanoparticle() >>> particle.truncated_octahedron(7,2, {'Au' : 0.5, 'Ag' : 0.5}) >>> calculator = EMTCalculator(fmax=0.02, steps=100, relax_atoms=True) >>> calculator.compute_energy(particle) >>> energy = particle.get_energy('EMT') >>> print(f"Computed energy: {energy}")
- class npl.calculators.EnergyCalculator[source]
Bases:
objectBase class for an energy calculator.
Valid implementations have to implement the compute_energy(particle) function. Energies are saved in the particle object with the key of the respective calculator.
- class npl.calculators.TOPCalculator(feature_key: str, stoichiometry: str = None, model_paths: list | str = None, feature_classifier: <module 'npl.descriptors' from '/home/docs/checkouts/readthedocs.org/user_builds/nplib/checkouts/stable/npl/descriptors/__init__.py'> = None, **kwargs)[source]
Bases:
CalculatorA class representing a calculator for performing relaxation calculations using the ASE library.
- Parameters:
calculator (Calculator): The calculator object used for performing the calculations. fmax (float): The maximum force tolerance for the relaxation.
- calculate(atoms)[source]
Do the calculation.
- properties: list of str
List of what needs to be calculated. Can be any combination of ‘energy’, ‘forces’, ‘stress’, ‘dipole’, ‘charges’, ‘magmom’ and ‘magmoms’.
- system_changes: list of str
List of what has changed since last calculation. Can be any combination of these six: ‘positions’, ‘numbers’, ‘cell’, ‘pbc’, ‘initial_charges’ and ‘initial_magmoms’.
Subclasses need to implement this, but can ignore properties and system_changes if they want. Calculated properties should be inserted into results dictionary like shown in this dummy example:
self.results = {'energy': 0.0, 'forces': np.zeros((len(atoms), 3)), 'stress': np.zeros(6), 'dipole': np.zeros(3), 'charges': np.zeros(len(atoms)), 'magmom': 0.0, 'magmoms': np.zeros(len(atoms))}
The subclass implementation should first call this implementation to set the atoms attribute and create any missing directories.