proxtoolbox.experiments package

This package contains the different experiments to which the proxtoolbox can be applied. It defines the abstract Experiment class which contains all the information that describes a given experiment. Concrete experiment classes are derived from this class. There are currently four families of experiments: the phase retrieval experiments, computed tomography (CT), ptychography, and Sudoku. The orbital tomography experiment will be integrated soon.

Submodules

proxtoolbox.experiments.experiment module

class proxtoolbox.experiments.experiment.ExperimentMetaClass[source]

Bases: type

Metaclass associated with the Experiment class

class proxtoolbox.experiments.experiment.JSONNumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

json encoder for numpy types

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class proxtoolbox.experiments.experiment.Experiment(experiment_name=None, constraint='amplitude only', object='complex', formulation='product space', product_space_dimension=1, sets=None, Nx=None, Ny=None, Nz=None, noise=False, algorithm=None, numruns=1, MAXIT=1000, TOL=1e-08, TOL2=None, lambda_0=0.85, lambda_max=0.85, lambda_switch=20, data_ball=0.001, diagnostic=True, iterate_monitor_name='IterateMonitor', accelerator_name=None, rotate=False, verbose=0, graphics=0, anim=False, anim_step=2, anim_colorbar=False, graphics_display=None, figure_width=9, figure_height=7, figure_dpi=100, debug=False, save_output=False, json_output=True, matlab_output=True, rnd_seed=1234, **kwargs)[source]

Bases: object

Base class for ProxToolbox Experiments

The experiment class contains all the information that describes a given experiment.

Essentially, it loads or generates the data used in the experiment and instanciates the algorithm that works on this data. This class is meant to be derived from to implement concrete experiments. Derived classes must implement the static method getDefaultParameters() which returns a dictionary containing the default parameters used by this experiment. Also required, is the implementation of the loadData() method to load or generate the data.

Initialize the data members of the experiment object. The real work will be done later in the initialize() method. The latter is called automatically after the experiment object is fully constructed.

initialize()[source]

Initialize experiment object. This method is automatically called by the metaclass (ExperimentMetaClass) once the experiment object is fully constructed. This method calls the method loadData() that loads or generates the data required for this experiment. Additionally, it finds the classes of the prox operators, and instantiate the iterate monitor and the algorithm used to run this experiment.

run()[source]

Run the algorithm associated with this experiment. The initial iterate is obtained from the loadData() method.

runAlgorithm(u)[source]

Run the algorithm associated with this experiment. Called by the run() method. May be overriden to provide additional behaviour, such as running a warmup algorithm before the main algorithm.

Parameters:
u: ndarray or cell of ndarray objects

Initial iterate.

Returns:
dictionary containing the output of the algorithm. In
particular, it contains the last iterate and various
statistics.
loadData()[source]

Load or generate the dataset that will be used for this experiment. Create the initial iterate.

setupProxOperators()[source]

Determine the prox operators to be used based on the given constraint. This method is called during the initialization process. Derived classes can override this method to specify their own prox operators. Only the names of the prox operators need to be provided. The actual objects will be instantiated later.

retrieveProxOperatorClasses()[source]

Retrieve the Python classes corresponding to the prox operators defined by the setupProxOperators() method.

This is a helper method called during the initialization process. It should not be overriden, unless the derived experiment class uses an additional category of prox operators not covered in the Experiment class.

reshapeData(Nx, Ny, Nz)[source]

Reshape data based on the given arguments. This method is called during the initialization of the Experiment object, after the call to loadData(). This optional. Default implementation does nothing.

Parameters:
Nx, Ny, Nz: int

The new dimensions to be used.

Notes

TODO: need to revisit this - there shouldn’t be any need for arguments

instanciateAlgorithm()[source]

Instanciate algorithm and some objects used by the algorithm, namely the iterate monitor and accelerator, based on the names provided.

This is a helper method called during the initialization process. It should not be overriden.

postprocess()[source]

Optional processing of the data returned by the algorithm. Default implementation does nothing. May be overriden by derived classes.

saveOutput()[source]

Save the algorithm output. if the data member matlab_output is set to True, then the ouput is saved as a Matlab .mat file. If the data member json_output is set to True, then the output is saved using the json format. The output file is saved in a subdirectory of the OutputData folder. The name of the subdirectory is generated using the current date. The filename is obtained by concatenating the name of the experiment and the name of the algorithm used.

show()[source]

Generate graphical output from the solution Default implementation does nothing.

animate(alg)[source]

Display animation. This method is called by the iterate monitor if the data member anim is set to True.

This method is meant to be overriden in derived classes. In particular,the class PhaseExperiment provides an implementation which is used by all phase experiments.

Parameters:
alg : instance of Algorithm class

Algorithm that is running.

printInput()[source]

Print all the data members of this experiment

printBenchmark()[source]

Create and print a benchmark, which is a summary the statistics collected during the algorithm run.

createBenchmark()[source]

Create a summary of the statistics collected during the algorithm run.

Returns:
benchmark: dictionary

The benchmark.

setFattening()[source]

Optional method for fattening/regularizing sets Called by initialization() method after instanciating prox operators but before instanciating the algorithm

estimateGap()[source]

Estimate the gap in the sequential formulation. TODO: deprecated in Cone_and_Sphere

animateFigure(image, title)[source]

Display animated figure. This is a helper method that is called by the animate() method to display an animated image (drawn using imshow()). Other plots are not supported yet.

This method takes care of creating or updating the figure

Parameters:
image : 2D scalar array

The image to draw

title : str

The title of the figure

onAnimationWindowClosing(evt)[source]
static getDefaultParameters()[source]

Return the default parameters used by this experiment. The name of the parameters must match the arguments of the __init__() methods of the Experiment class and/or derived classes. Essentially this corresponds to the old config files. This static method must be implemented in the derived classes (the implementation given here only throws an exception). See example below.

Returns:
dictionary

Default parameters to be used for this experiment. Each entry must match an argument from the __init__() methods of the Experiment class and/or derived classes.

Notes

The following shows a typical implementation of this method:

@staticmethod def getDefaultParameters():

defaultParams = {
‘object’: ‘complex’, ‘constraint’: ‘amplitude only’, ‘distance’: ‘far field’, ‘Nx’: 128, ‘Ny’: 128, ‘Nz’: 1, ‘product_space_dimension’: 4, ‘MAXIT’: 6000, ‘TOL’: 5e-5, ‘lambda_0’: 0.85, ‘lambda_max’: 0.85, ‘lambda_switch’: 20, ‘data_ball’: 1e-30, ‘diagnostic’: True, ‘iterate_monitor_name’: ‘FeasibilityIterateMonitor’, ‘verbose’: 1, ‘graphics’: 1, ‘anim’: False

} return defaultParams