Utilities¶
This module contains utilities for use with various aspects of the Amp calculator.
Module contents¶
- class amp.utilities.Annealer(calc, images, Tmax=None, Tmin=None, steps=None, updates=None, train_forces=True)[source]¶
Bases:
objectInspired by the simulated annealing implementation of Richard J. Wagner <wagnerr@umich.edu> and Matthew T. Perry <perrygeo@gmail.com> at https://github.com/perrygeo/simanneal.
Performs simulated annealing by calling functions to calculate loss and make moves on a state. The temperature schedule for annealing may be provided manually or estimated automatically.
Can be used by something like:
>>> from amp import Amp >>> from amp.descriptor.gaussian import Gaussian >>> from amp.model.neuralnetwork import NeuralNetwork >>> calc = Amp(descriptor=Gaussian(), model=NeuralNetwork())
which will initialize tha calc object as usual, and then
>>> from amp.utilities import Annealer >>> Annealer(calc=calc, images=images)
which will perform simulated annealing global search in parameters space, and finally
>>> calc.train(images=images)
for gradient descent optimization.
- Parameters:
- Tmax = 20.0¶
- Tmin = 2.5¶
- anneal()[source]¶
Minimizes the loss of a system by simulated annealing.
- Parameters:
state – An initial arrangement of the system
- Returns:
The best state and loss found.
- Return type:
state, loss
- auto(minutes, steps=2000)[source]¶
Minimizes the loss of a system by simulated annealing with automatic selection of the temperature schedule.
Keyword arguments: state – an initial arrangement of the system minutes – time to spend annealing (after exploring temperatures) steps – number of steps to spend on each stage of exploration
Returns the best state and loss found.
- copy_state(state)[source]¶
Returns an exact copy of the provided state Implemented according to self.copy_strategy, one of
deepcopy : use copy.deepcopy (slow but reliable)
slice: use list slices (faster but only works if state is list-like)
method: use the state’s copy() method
- copy_strategy = 'copy'¶
- save_state_on_exit = False¶
- steps = 10000¶
- update(step, T, L, acceptance, improvement)[source]¶
Prints the current temperature, loss, acceptance rate, improvement rate, elapsed time, and remaining time.
The acceptance rate indicates the percentage of moves since the last update that were accepted by the Metropolis algorithm. It includes moves that decreased the loss, moves that left the loss unchanged, and moves that increased the loss yet were reached by thermal excitation.
The improvement rate indicates the percentage of moves since the last update that strictly decreased the loss. At high temperatures it will include both moves that improved the overall state and moves that simply undid previously accepted moves that increased the loss by thermal excititation. At low temperatures it will tend toward zero as the moves that can decrease the loss are exhausted and moves that would increase the loss are no longer thermally accessible.
- updates = 50.0¶
- user_exit = False¶
- exception amp.utilities.ConvergenceOccurred[source]¶
Bases:
ExceptionKludge to decide when scipy’s optimizers are complete.
- class amp.utilities.Data(filename, db=<class 'amp.utilities.FileDatabase'>, calculator=None)[source]¶
Bases:
objectServes as a container (dictionary-like) for (key, value) pairs that also serves to calculate them.
Works by default with FileDatabase, which writes each entry to disk as a pickle file. Pass db=EphemeralDatabase for inference or MD runs where images are never revisited; that backend holds only the most recent entry in memory and never writes to disk.
Designed to hold things like neighborlists, which have a hash, value format.
This will work like a dictionary in that items can be accessed with data[key], but other advanced dictionary functions should be accessed with through the .d attribute:
>>> data = Data(...) >>> data.open() >>> keys = data.d.keys() >>> values = data.d.values()
- class amp.utilities.EphemeralDatabase(filename)[source]¶
Bases:
objectIn-memory drop-in for FileDatabase that never writes to disk.
Intended for inference / MD use, where each image is visited once and will never be revisited. Using FileDatabase in that context writes a growing pile of pickle files that wastes disk I/O and process memory.
All data for a given filename is shared across open()/close() cycles via a class-level store, so fingerprints computed in one phase of a single calculate() call remain accessible in later phases (important for KRR, which needs both test and training fingerprints simultaneously). The store is cleared once per Amp.calculate() call via clear_all(), keeping memory bounded to the working set of one inference step.
The interface mirrors FileDatabase / shelve so it can be passed as the
dbargument toData.
- class amp.utilities.FileDatabase(filename)[source]¶
Bases:
objectUsing a database file, such as shelve or sqlitedict, that can handle multiple processes writing to the file is hard.
Therefore, we take the stupid approach of having each database entry be a separate file. This class behaves essentially like shelve, but saves each dictionary entry as a plain pickle file within the directory, with the filename corresponding to the dictionary key (which must be a string).
Like shelve, this also keeps an internal (memory dictionary) representation of the variables that have been accessed.
Also includes an archive feature, where files are instead added to a file called ‘archive.tar.gz’ to save disk space. If an entry exists in both the loose and archive formats, the loose is taken to be the new (correct) value.
- archive()[source]¶
Cleans up to save disk space and reduce huge number of files.
That is, puts all files into an archive. Compresses all files in <path>/loose and places them in <path>/archive.tar.gz. If archive exists, appends/modifies.
- class amp.utilities.Logger(file, overwrite=False)[source]¶
Bases:
objectLogger that can also deliver timing information.
- Parameters:
file (str) – File object or path to the file to write to. Or set to None for a logger that does nothing.
- class amp.utilities.MessageDictionary(process_id)[source]¶
Bases:
objectStandard container for all messages (typically requests, via zmq.context.socket.send_pyobj) sent from the workers to the master.
This returns a simple dictionary. This is roughly email format. Initialize with process id (e.g., ‘from’). Call with subject and data (body).
- class amp.utilities.MetaDict[source]¶
Bases:
dictDictionary that can also store metadata. Useful for images dictionary so that images can still be iterated by keys.
- metadata = {}¶
- exception amp.utilities.MissingDataError[source]¶
Bases:
ExceptionError to be raised if any images are missing key data, like energy or forces.
- exception amp.utilities.TrainingConvergenceError[source]¶
Bases:
ExceptionError to be raised if training does not converge.
- amp.utilities.assign_cores(cores, log=None)[source]¶
Tries to guess cores from environment.
If fed a log object, will write its progress.
- amp.utilities.check_images(images, forces, charges)[source]¶
Checks that all images have energies, and optionally forces and charges, calculated, so that they can be used for training. Raises a MissingDataError if any are missing.
- amp.utilities.extract_an_atomic_chunk(atoms, index, parent_calc=None, cutoff=6.5, vacuum=5.0)[source]¶
Extract a chunk from atoms centering on the atom with a given index. cutoff defines the range within which atoms are included in the atomic chunk. vacuum represents the thickness of surrounded vacuum layers.
- amp.utilities.get_atomic_uncertainties(load, atoms, force=True, label='amp', threshold=None)[source]¶
Compute atomic uncertainties based on ensemble predictions of either atomic energies or forces. If threshold is specificed, indices of atoms whose atomic uncertainty is larger than the threshold will be identified.
- amp.utilities.get_gc_hash(atoms, electrode_potential=None)[source]¶
- Creates a unique signature for a particular ASE atoms object
calculated within grand-canonical framework SJM.
This is used to check whether an image has been seen before. Two identical images are defined as identical both in geometry and electrode potential. This is just an md5 hash of a string representation of the atoms object and target electrode potential.
- Parameters:
atoms (ASE dict) – ASE atoms object.
electrode_potential (float or None) – If provided, use this value directly (inference path: potential was set on the Amp calculator via calc.set(electrode_potential=…)). If None, read from atoms.calc.results[‘electrode_potential’] (training path: images from SJM/GPAW trajectory).
- Return type:
Hash string key of ‘atoms’.
- amp.utilities.get_hash(atoms)[source]¶
Creates a unique signature for a particular ASE atoms object.
This is used to check whether an image has been seen before. This is just an md5 hash of a string representation of the atoms object.
- Parameters:
atoms (ASE dict) – ASE atoms object.
- Return type:
Hash string key of ‘atoms’.
- amp.utilities.get_overfit_mask(nn_model, parametervector)[source]¶
Compute masked indices for overfit parameters in a raveled parametervector. The basic principle for overfit/regularization is that only weights and slopes should be regularized. In other words, biases and intercepts should be removed. This function is designed for the neural network model in Amp.
- amp.utilities.hash_gc_images(images, log=None, ordered=False)[source]¶
Converts input images calculated within grand-canonical framework SJM including geometry and electrode potential info – which may be a list, a trajectory file, or a database – into a dictionary indexed by their hashes.
Returns this dictionary. If ordered is True, returns an OrderedDict. When duplicate images are encountered (based on encountering an identical hash), a warning is written to the logfile. The number of duplicates of each image can be accessed by examinging dict_images.metadata[‘duplicates’], where dict_images is the returned dictionary.
- amp.utilities.hash_images(images, log=None, ordered=False)[source]¶
Converts input images – which may be a list, a trajectory file, or a database – into a dictionary indexed by their hashes.
Returns this dictionary. If ordered is True, returns an OrderedDict. When duplicate images are encountered (based on encountering an identical hash), a warning is written to the logfile. The number of duplicates of each image can be accessed by examinging dict_images.metadata[‘duplicates’], where dict_images is the returned dictionary.
- amp.utilities.importer(name)[source]¶
Handles strange import cases, like pxssh which might show up in either the package pexpect or pxssh.
- amp.utilities.make_filename(label, base_filename)[source]¶
Creates a filename from the label and the base_filename which should be a string.
Returns None if label is None; that is, it only saves output if a label is specified.
- amp.utilities.make_sublists(masterlist, n)[source]¶
Randomly divides the masterlist into n sublists of roughly equal size.
The intended use is to divide a keylist and assign keys to each task in parallel processing. This also destroys the masterlist (to save some memory).
- amp.utilities.parse_slurm_allocation(env_vars=None, log=None)[source]¶
If debugging you can pass in a dictionary with custom environment variables.
- amp.utilities.randomize_images(images, fraction=0.8)[source]¶
Randomly assigns ‘fraction’ of the images to a training set and (1 - ‘fraction’) to a test set. Returns two lists of ASE images.
- amp.utilities.read_amp_log_status(logfile)[source]¶
Reads a partially or fully written amp-log.txt for live job monitoring.
Designed to be safe on logs at any stage of writing: reads only the first 30 lines for the start time and tails the last ~2 KB for the step count, so it does not block or fail on large or incomplete log files.
Uses the ‘Date:’ header line (written at Python process startup, before fingerprinting) as the start-time anchor — giving the true wall-clock age of the job including all setup time. See read_trainlog() in analysis.py for full post-analysis parsing.
- Parameters:
logfile (str) – Path to the amp-log.txt file.
- Returns:
start_time (float or None) – UTC timestamp (seconds since epoch) parsed from the ‘Date:’ header, or None if the line has not yet been written.
last_step (int or None) – Most recent optimizer step number, or None if training has not yet reached the parameter optimization stage.
- amp.utilities.setup_parallel(parallel, workercommand, log, setup_publisher=False)[source]¶
Starts the worker processes and the master to control them.
This makes an SSH connection to each node (including the one the master process runs on), then creates the specified number of processes on each node through its SSH connection. Then sets up ZMQ for efficienty communication between the worker processes and the master process.
Uses the parallel dictionary as defined in amp.Amp. log is an Amp logger. module is the name of the module to be called, which is usually given by self.calc.__module, etc. workercommand is stub of the command used to start the servers, typically like “python -m amp.descriptor.gaussian”. Appended to this will be “ <pid> <serversocket> &” where <pid> is the unique ID assigned to each process and <serversocket> is the address of the server, like ‘node321:34292’.
If setup_publisher is True, also sets up a publisher instead of just a reply socket.
- Returns:
server – The ssh connections (pxssh instances; if these objects are destroyed pxssh will close the sessions)
the pid_count, which is the total number of workers started. Each worker can be communicated directly through its PID, an integer between 0 and pid_count
- Return type:
(a ZMQ socket)