6. Helper modules
The code described here is only relevant for developers.
6.1. mdpow.log — Configure logging for POW analysis
Import this module if logging is desired in application code and
create the logger in __init__.py:
import log
logger = log.create(logname, logfile)
In modules simply use:
import logging
logger = logging.getLogger(logname)
6.2. mdpow.restart — Restarting and checkpointing
The module provides classes and functions to keep track of which stages of a
simulation protocol have been completed. It uses a Journal class for
the book-keeping. Together with saving the current state of a protocol to a
checkpoint file (using Journalled.save()) it is possible to implement
restartable simulation protocols (for example mdpow-equilibrium).
- exception mdpow.restart.JournalSequenceError[source]
Raised when a stage is started without another one having been completed.
- class mdpow.restart.Journal(stages)[source]
Class that keeps track of the stage in a protocol.
Transaction blocks have to be bracketed by calls to
start()andcompleted(). If a block is started before completion, aJournalSequenceErrorwill be raised.Other methods such as
has_completed()andhas_not_completed()can be used to query the status. The attributeincompleteflags the state of the current stage (current).All completed stages are recorded in the attribute
history.The current (incomplete) stage can be reset to its initial state with
Journal.clear().Example:
J = Journal(['pre', 'main', 'post']) J.start('pre') ... J.completed('pre') J.start('main') ... # main does not finish properly print(J.incomplete) # --> 'main' J.start('post') # raises JournalSequenceError
Initialise the journal that keeps track of stages.
- Arguments:
- stages
list of the stage identifiers, in the order that they should per performed. Stage identifiers are checked against this list before they are accepted as arguments to most methods.
- completed(stage)[source]
Record completed stage and reset
Journal.current
- property current
Current stage identifier
- has_not_completed(stage)[source]
Returns
Trueif the stage had been started but not completed yet.This is subtly different from
nothas_completed()in that two things have to be true:No stage is active (which is the case when a restart is attempted).
The stage has not been completed previously (i.e.,
has_completed()returnsFalse)
- property history
List of stages completed
- property incomplete
This last stage was not completed.
- class mdpow.restart.Journalled(*args, **kwargs)[source]
A base class providing methods for journalling and restarts.
It installs an instance of
Journalin the attributeJournalled.journalif it does not exist already.- get_protocol(protocol)[source]
Return method for protocol.
If protocol is a real method of the class then the method is returned. This method should implement its own use of
Journal.start()andJournal.completed().If protocol is a registered protocol name but no method of the name exists (i.e. protocol is a “dummy protocol”) then a wrapper function is returned. The wrapper has the signature
- dummy_protocol(func, *args, **kwargs)
Runs func with the arguments and keywords between calls to
Journal.start()andJournal.completed(), with the stage set to protocol.The function should return
Trueon success andFalseon failure.
Raises a
ValueErrorif the protocol is not registered (i.e. not found inJournalled.protocols).
- load(filename=None)[source]
Re-instantiate class from pickled file.
If no filename was supplied then the filename is taken from the attribute
filename.Changed in version 0.7.1: Can read pickle files with either Python 2.7 or 3.x, regardless of the Python version that created the pickle.
- protocols = []
Class-attribute that contains the names of computation protocols supported by the class. These are either method names or dummy names, whose logic is provided by an external callback function. The method
get_protocol()raises aValueErrorif a protocol is not listed inprotocols.
- mdpow.restart.checkpoint(name, sim, filename)[source]
Execute the
Journalled.save()method and log the event.