Miscellaneous functions used by Topographica code outside of topo.base.
Several of these files are completely self-contained, not importing anything from Topographica, and can thus be copied and used in unrelated projects.
and sizes of Python objects (for Python 2.2 or later [1]).
The main changes in this version are new function calcsize(), use gc.get_objects() to get all objects and improvements in this documentation.
Public Functions [2]
Function asizeof calculates the combined (approximate) size in bytes of one or several Python objects.
Function asizesof returns a tuple containing the (approximate) size in bytes for each given Python object separately.
Function asized returns for each object an instance of class Asized containing all the size information of the object and a tuple with the referents.
Functions basicsize and itemsize return the basic respectively item size of the given object.
Function flatsize returns the flat size of a Python object in bytes defined as the basic size plus the item size times the length of the given object.
Function leng returns the length of an object, like standard len but extended for several types, e.g. the leng of a multi- precision int (or long) is the number of digits [3]. The length of most mutable sequence objects includes an estimate of the over-allocation and therefore, the leng value may differ from the standard len result.
Function refs returns (a generator for) the referents of the given object, i.e. the objects referenced by the given object.
Function calcsize is equivalent to standard struct.calcsize but handles format characters ‘z’ for signed C type Py_ssize_t and ‘Z’ for unsigned C type size_t.
Certain classes are known to be sub-classes of or to behave as dict objects. Function adict can be used to install other class objects to be treated like dict.
Public Classes [2]
An instance of class Asized is returned for each object sized with the asized function or method.
Class Asizer can be used to accumulate the results of several asizeof or asizesof calls. After creating an Asizer instance, use methods asizeof and asizesof to size additional objects.
Call methods exclude_refs and/or exclude_types to exclude references to or instances or types of certain objects.
Use one of the print_... methods to report the statistics.
Duplicate Objects
Any duplicate, given objects are sized only once and the size is included in the combined total only once. But functions asizesof and asized do return a size value respectively an Asized instance for each given object, the same for duplicates.
Definitions [4]
The size of an object is defined as the sum of the flat size of the object plus the sizes of any referents. Referents are visited recursively up to a given limit. However, the size of objects referenced multiple times is included only once.
The flat size of an object is defined as the basic size of the object plus the item size times the number of allocated items. The flat size does include the size for the items (references to the referents), but not the referents themselves.
The flat size returned by function flatsize equals the result of the asizeof function with options code=True, ignored=False, limit=0 and option align set to the same value.
The accurate flat size for an object is obtained from function sys.getsizeof() where available. Otherwise, the length and size of sequence objects as dicts, lists, sets, etc. is based on an estimate for the number of allocated items. As a result, the reported length and size may substantially differ from the actual length and size.
The basic and item sizes are obtained from the __basicsize__ respectively __itemsize__ attribute of the (type of the) object. Where necessary (e.g. sequence objects), a zero __itemsize__ is replaced by the size of a corresponding C type.
The basic size (of GC managed objects) objects includes the overhead for Python’s garbage collector (GC) as well as the space needed for refcounts (only in certain Python builds).
Optionally, sizes can be aligned to any power of 2 multiple.
Size of (byte)code
The (byte)code size of objects as classes, functions, methods, modules, etc. can be included by setting option code.
Iterators are handled similar to sequences: iterated object(s) are sized like referents if the recursion limit permits. Also, function gc.get_referents() must return the referent object of iterators.
Generators are sized as (byte)code only, but generated objects are never sized.
Old- and New-style Classes
All old- and new-style class, instance and type objects, are handled uniformly such that (a) instance and class objects can be distinguished and (b) instances of different old-style classes can be dealt with separately.
Class and type objects are represented as <class ....* def> respectively as <type ... def> where an ‘*’ indicates an old- style class and the def suffix marks the definition object. Instances of old-style classes are shown as new-style ones but with an ‘*’ at the end of the name, like <class module.name*>.
Ignored Objects
To avoid excessive sizes, several object types are ignored [4] by default, e.g. built-in functions, built-in types and classes [5], function globals and module referents. However, any instances thereof are sized and module objects will be sized when passed as given objects. Ignored object types are included if option ignored is set accordingly.
In addition, many __...__ attributes of callable objects are ignored, except crucial ones, e.g. class attributes __dict__, __doc__, __name__ and __slots__. For more details, see the type-specific _..._refs() and _len_...() functions below.
Option all can be used to size all Python objects and/or get the referents from gc.get_referents() and override the type- specific __..._refs() functions.
Notes
- [1] Tested with Python 2.2.3, 2.3.7, 2.4.5, 2.5.1, 2.5.2, 2.6.2,
- 3.0.1 or 3.1a2 on CentOS 4.6, SuSE 9.3, MacOS X 10.4.11 Tiger (Intel) and 10.3.9 Panther (PPC), Solaris 10 (Opteron) and Windows XP all 32-bit Python and on RHEL 3u7 and Solaris 10 (Opteron) both 64-bit Python.
[2] The functions and classes in this module are not thread-safe.
- [3] See Python source file .../Include/longinterp.h for the
- C typedef of digit used in multi-precision int (or long) objects. The size of digit in bytes can be obtained in Python from the int (or long) __itemsize__ attribute. Function leng (rather _len_int) below deterimines the number of digits from the int (or long) value.
- [4] These definitions and other assumptions are rather arbitrary
- and may need corrections or adjustments.
- [5] Types and classes are considered built-in if the module of
- the type or class is listed in _builtin_modules below.
Return a tuple containing an Asized instance for each object passed as positional argment using the following options.
align=8 – size alignment all=False – all current GC objects and referents clip=80 – clip repr() strings code=False – incl. (byte)code size derive=False – derive from super type detail=0 – Asized refs level ignored=True – ignore certain types infer=False – try to infer types limit=100 – recursion limit stats=0.0 – print statistics and cutoff percentage
If only one object is given, the return value is the Asized instance for that object.
Set detail to the desired referents level (recursion depth).
See function asizeof for descriptions of the other options.
The length of the returned tuple matches the number of given objects, if more than one object is given.
Return the combined size in bytes of all objects passed as positional argments.
The available options and defaults are the following.
align=8 – size alignment all=False – all current GC objects and referents clip=80 – clip repr() strings code=False – incl. (byte)code size derive=False – derive from super type ignored=True – ignore certain types infer=False – try to infer types limit=100 – recursion limit stats=0.0 – print statistics and cutoff percentage
Set align to a power of 2 to align sizes. Any value less than 2 avoids size alignment.
All current GC objects are sized if all is True and if no positional arguments are supplied. Also, if all is True the GC referents are used instead of the limited ones.
A positive clip value truncates all repr() strings to at most clip characters.
The (byte)code size of callable objects like functions, methods, classes, etc. is included only if code is True.
If derive is True, new types are handled like an existing (super) type provided there is one and only of those.
By default, certain base types like object are ignored for sizing. Set ignored to False to force all ignored types in the size of objects.
By default certain base types like object, super, etc. are ignored. Set ignored to False to include those.
If infer is True, new types are inferred from attributes (only implemented for dict types on callable attributes as get, has_key, items, keys and values).
Set limit to a positive value to accumulate the sizes of the referents of each object, recursively up to the limit. Using limit zero returns the sum of the flat [1] sizes of the given objects. High limit values may cause runtime errors and miss objects for sizing.
A positive value for stats prints up to 8 statistics, (1) a summary of the number of objects sized and seen, (2) a simple profile of the sized objects by type and (3+) up to 6 tables showing the static, dynamic, derived, ignored, inferred and dict types used, found respectively installed.
The fractional part of the stats value (x 100) is the cutoff percentage for simple profiles. Objects below the cutoff value are not reported.
Return a tuple containing the size in bytes of all objects passed as positional argments using the following options.
align=8 – size alignment all=False – use GC objects and referents clip=80 – clip repr() strings code=False – incl. (byte)code size derive=False – derive from super type ignored=True – ignore certain types infer=False – try to infer types limit=100 – recursion limit stats=0.0 – print statistics and cutoff percentage
See function asizeof for a description of the options.
The length of the returned tuple equals the number of given objects.
Bases: object
Store the results of an asized object in these 4 attributes:
size - total size of the object flat - flat size of the object name - name or repr of the object refs - tuple containing an instance
of Asized for each referent
Bases: object
Sizer state and options.
Size each object and return an Asized instance with size information and referents up to the given detail level (and with modified options, see method set).
If only one object is given, the return value is the Asized instance for that object.
Return the combined size of the given objects (with modified options, see also method set).
Return the individual sizes of the given objects (with modified options, see also method set).
Number of duplicate objects.
Exclude any references to the specified objects from sizing.
While any references to the given objects are excluded, the objects will be sized if specified as positional arguments in subsequent calls to methods asizeof and asizesof.
Exclude the specified object instances and types from sizing.
All instances and types of the given objects are excluded, even objects specified as positional arguments in subsequent calls to methods asizeof and asizesof.
Number of objects missed due to errors.
Print the profiles above cutoff percentage.
w=0 – indentation for each line cutoff=0 – minimum percentage printed print3options – print options, as in Python 3.0
Print the statistics.
w=0 – indentation for each line objs=() – optional, list of objects opts={} – optional, dict of options used sized=() – optional, tuple of Asized instances returned sizes=() – optional, tuple of sizes returned stats=3.0 – print statistics and cutoff percentage print3options – print options, as in Python 3.0
Print the summary statistics.
w=0 – indentation for each line objs=() – optional, list of objects print3options – print options, as in Python 3.0
Print the types and dict tables.
w=0 – indentation for each line print3options – print options, as in Python 3.0
Reset options, state, etc.
The available options and default values are:
align=8 – size alignment all=False – all current GC objects and referents clip=80 – clip repr() strings code=False – incl. (byte)code size derive=False – derive from super type detail=0 – Asized refs level ignored=True – ignore certain types infer=False – try to infer types limit=100 – recursion limit stats=0.0 – print statistics and cutoff percentage
See function asizeof for a description of the options.
Set some options. Any options not set remain the same as the previous setting.
align=8 – size alignment code=False – incl. (byte)code size detail=0 – Asized refs level limit=100 – recursion limit stats=0.0 – print statistics and cutoff percentage
Total size accumulated so far.
Return the basic size of an object (in bytes).
Return the flat size of an object (in bytes), optionally aligned to a given power of 2.
See function basicsize for a description of the other options. See the documentation of this module for the definition of flat size.
Return the item size of an object (in bytes).
See function basicsize for a description of the options.
Return the length of an object (in items).
See function basicsize for a description of the options.
Bases: dict
A dictionary type object that supports attribute access (e.g. for IPython tab completion).
v defaults to None.
If key is not found, d is returned if given, otherwise KeyError is raised
2-tuple; but raise KeyError if D is empty.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
Provides optimized color conversion utilities to speedup imagen color conversion (imagen.colorspaces.rgb_to_hsv and imagen.colorspaces.hsv_to_rgb.
Support functions for parsing command-line arguments and providing the Topographica command prompt. Typically called from the ‘./topographica’ script, but can be called directly if using Topographica files within a separate Python.
Bases: topo.misc.commandline.IPCommandPromptHandler
Control over input prompt.
Several predefined formats are provided, and any of these (or any arbitrary string) can be used by calling set_format() with their values.
See the IPython manual for details: http://ipython.scipy.org/doc/manual/html/config/index.html
Return the current template.
Set IPython’s prompt template to format.
Bases: topo.misc.commandline.IPCommandPromptHandler
Control over continuation prompt.
(See CommandPrompt.)
Return the current template.
Set IPython’s prompt template to format.
Bases: param.parameterized.Parameterized, topo.base.simulation.OptionalSingleton
A Parameterized class providing script-level parameters.
Script-level parameters can be set from the commandline by passing via -p, e.g. ./topographica -p retina_density=10
Within scripts, parameters can be declared by using the add() method.
Example usage in a script:
from topo.misc.commandline import global_params as p p.add(
retina_density=param.Number(default=24.0,bounds=(0,None), inclusive_bounds=(False,True),doc=”“” The nominal_density to use for the retina.”“”))
... topo.sim[‘Retina’]=sheet.GeneratorSheet(
nominal_density=p.retina_density)
Further information:
‘context’ is usually set to __main__.__dict__ and is used to find the value of a parameter as it is add()ed to this object (i.e. add() has access to values set via the commandline or in scripts).
Values set via set_in_context() or exec_in_context() (used by -p) are tracked: warnings are issued for overwritten values, and unused values can be warned about via check_for_unused_names().
The context is not saved in snapshots, but parameter values are saved.
For each parameter_name=parameter_object specified in kw: * adds the parameter_object to this object’s class * if there is an entry in context that has the same name as the parameter,
sets the value of the parameter in this object to that value, and then removes the name from context
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
exec arg in self.context, tracking new names and warning of any replacements.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Set in self.context all name=val pairs specified in **params, tracking new names and warning of any replacements.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: object
Allows control over IPython’s dynamic command prompts.
Bases: topo.misc.commandline.IPCommandPromptHandler
Control over output prompt.
(See CommandPrompt.)
Return the current template.
Set IPython’s prompt template to format.
Callback function for the -V option.
Callback function for the -a option.
Import the contents of all files in the topo/command/ directory.
Callback function for boolean-valued options that apply to the entire run.
Callback function for the -c option.
Callback function for the -d option.
Determine the appropriate default location in which to create files on this operating system.
Execute startup files.
Linux/UNIX/OS X: ~/.topographicarc Windows: %USERPROFILE% opographica.ini
Callback function for the -g option.
Start the GUI as if -g were supplied in the command used to launch Topographica.
Callback function for the -i option.
Callback function for the -l option.
Callback function for the -o option.
Callback function for the -p option.
Process command-line arguments (minus argv[0]!), rearrange and execute.
Set the default output path to the specified path, creating it if it does not exist and adding it to the input searching path (to allow just-saved files to be reloaded).
Set the simulation title from the given filename, if none has been set already.
Additional FeatureCoordinators specific to cortical modelling work, supplementing the general-purpose ones in imagen.patterncoordinator.
Bases: imagen.patterncoordinator.FeatureCoordinator
Coordinates the disparity (difference of pattern locations) of two pattern generators. The pattern_label of the generators must contain ‘Left’ or ‘Right’
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Same as Parameterized.pprint, except that X.classname(Y is replaced with X.classname.instance(Y
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Same as Parameterized.script_repr, except that X.classname(Y is replaced with X.classname.instance(Y
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: imagen.patterncoordinator.FeatureCoordinator
Coordinates the motion of patterns.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Same as Parameterized.pprint, except that X.classname(Y is replaced with X.classname.instance(Y
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Same as Parameterized.script_repr, except that X.classname(Y is replaced with X.classname.instance(Y
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: imagen.patterncoordinator.FeatureCoordinator
Coordinates the ocularity (brightness difference) of two pattern generators. The pattern_label of the generators must contain ‘Left’ or ‘Right’
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Same as Parameterized.pprint, except that X.classname(Y is replaced with X.classname.instance(Y
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Same as Parameterized.script_repr, except that X.classname(Y is replaced with X.classname.instance(Y
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: imagen.patterncoordinator.FeatureCoordinator
Coordinates the size of pattern generators. This is useful when multiple spatial frequency channels are used, to cover a wide range of sizes of pattern generators.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Same as Parameterized.pprint, except that X.classname(Y is replaced with X.classname.instance(Y
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Same as Parameterized.script_repr, except that X.classname(Y is replaced with X.classname.instance(Y
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
FixedPoint objects support decimal arithmetic with a fixed number of digits (called the object’s precision) after the decimal point. The number of digits before the decimal point is variable & unbounded.
The precision is user-settable on a per-object basis when a FixedPoint is constructed, and may vary across FixedPoint objects. The precision may also be changed after construction via FixedPoint.set_precision(p). Note that if the precision of a FixedPoint is reduced via set_precision, information may be lost to rounding.
>>> x = FixedPoint("5.55") # precision defaults to 2
>>> print x
5.55
>>> x.set_precision(1) # round to one fraction digit
>>> print x
5.6
>>> print FixedPoint("5.55", 1) # same thing setting to 1 in constructor
5.6
>>> repr(x) # returns constructor string that reproduces object exactly
"FixedPoint('5.6', 1)"
>>>
When FixedPoint objects of different precision are combined via + - * /, the result is computed to the larger of the inputs’ precisions, which also becomes the precision of the resulting FixedPoint object.
>>> print FixedPoint("3.42") + FixedPoint("100.005", 3)
103.425
>>>
When a FixedPoint is combined with other numeric types (ints, floats, strings representing a number) via + - * /, then similarly the computation is carried out using– and the result inherits –the FixedPoint’s precision.
>>> print FixedPoint(1) / 7
0.14
>>> print FixedPoint(1, 30) / 7
0.142857142857142857142857142857
>>>
The string produced by str(x) (implictly invoked by “print”) always contains at least one digit before the decimal point, followed by a decimal point, followed by exactly x.get_precision() digits. If x is negative, str(x)[0] == “-”.
The FixedPoint constructor can be passed an int, long, string, float, FixedPoint, or any object convertible to a float via float() or to a long via long(). Passing a precision is optional; if specified, the precision must be a non-negative int. There is no inherent limit on the size of the precision, but if very very large you’ll probably run out of memory.
Note that conversion of floats to FixedPoint can be surprising, and should be avoided whenever possible. Conversion from string is exact (up to final rounding to the requested precision), so is greatly preferred.
>>> print FixedPoint(1.1e30)
1099999999999999993725589651456.00
>>> print FixedPoint("1.1e30")
1100000000000000000000000000000.00
>>>
The following Python operators and functions accept FixedPoints in the expected ways:
- binary + - * / % divmod
with auto-coercion of other types to FixedPoint. + - % divmod of FixedPoints are always exact. * / of FixedPoints may lose information to rounding, in
which case the result is the infinitely precise answer rounded to the result’s precision.
- divmod(x, y) returns (q, r) where q is a long equal to
- floor(x/y) as if x/y were computed to infinite precision, and r is a FixedPoint equal to x - q * y; no information is lost. Note that q has the sign of y, and abs(r) < abs(y).
unary - == != < > <= >= cmp min max float int long (int and long truncate) abs str repr hash use as dict keys use as boolean (e.g. “if some_FixedPoint:” – true iff not zero)
Provided as-is; use at your own risk; no warranty; no promises; enjoy!
Bases: object
Basic FixedPoint object class, The exact value is self.n / 10**self.p; self.n is a long; self.p is an int
Return the precision of this FixedPoint.
The precision is the number of decimal digits carried after the decimal point, and is an int >= 0.
Return the precision of this FixedPoint.
The precision is the number of decimal digits carried after the decimal point, and is an int >= 0.
rounding via nearest-even increment the quotient if
the remainder is more than half of the divisoror the remainder is exactly half the divisor and the quotient is odd
Topographica-specific changes to the standard pydoc command.
Moves generated pydoc HTML files to the docs directory after they are created by pydoc. Intended for use with MAKE, from within the base topographica/ directory.
The generated index.html file is for the topo/__init__.py file, which does not necessarily catalog every .py file in topo/. To see all files, select the ‘index’ link at the top of one of the html docs.
To generate documentation, enter ‘make docs’ from the base topographica/ directory, which will call this file on the Topographica sources.
From the Makefile (Tabs have been stripped):
cleandocs:
- rm -r docs
docs: topo/*.py
mkdir -p docs
./topographica topo/gendocs.py
mv docs/topo.__init__.html docs/index.html
Convert a path name into the PyDoc filename it will turn into.
If the path name ends in a .py, then it is cut off. If there is no extension, the name is assumed to be a directory.
Convert a path name into the Python dotted-notation package name.
If the name ends in a .py, then cut it off. If there is no extension, the name is assumed to be a directory, and nothing is done other than to replace the ‘/’ with ‘.’
Generate all pydoc documentation files within a docs directory under ./topographica according to the constant DOCS. After generation, there is an index.html that displays all the modules. Note that if the documentation is being changed, it may be necessary to call ‘make cleandocs’ to force a regeneration of documentation. (We don’t want to regenerate all the documentation each time a source file is changed.)
Commands for running the examples files in various ways.
Like a Makefile: contains a list of targets (and groups of targets) that specify various commands to run.
E.g.
topographica -c ‘from topo.misc.genexamples import generate; generate(targets=[“all_quick”,”saved_examples”])’
Runs the ‘all_quick’ target if called without any arguments:
topographica -c ‘from topo.misc.genexamples import generate; generate()’
To add new single targets, add to the targets dictionary; for groups of targets, add to the group_targets dictionary.
Interface class for inline C/C++ functions. Based on the SciPy Weave package.
Weave (from SciPy) allows programmers to implement Python methods or functions using C code written as a string in the Python file. This is generally done to speed up code that would be slow if written directly in Python. Because not all users can be assumed to have a working C/C++ compiler, it is crucial for such optimizations to be optional. This file provides an interface for processing the inline C/C++ code in a way that can gracefully revert back to the unoptimized version when Weave is not available.
The fallback is implemented by making use of the way Python allows function names to be overwritten, as in these simple examples:
def x(y = 5): return y def x2(y = 6): return y*y print ‘x:’, x(), ‘x2:’, x2() # Result – x: 5 x2: 36 x = x2 print ‘x:’, x(), ‘x2:’, x2() # Result – x: 36 x2: 36
In this file, inline() is overwritten to call inline_weave() if Weave is available. If Weave is not available, inline() will raise a NotImplementedError exception. For a program to be usable without Weave, just test inlinec.optimized after defining each optimized component, replacing it with a non-optimized equivalent if inlinec.optimized is False.
For more information on weave, see: http://old.scipy.org/documentation/weave/weaveusersguide.html
Some of the C functions also support OpenMP, which allows them to use multiple threads automatically on multi-core machines to give better performance. To enable OpenMP support for those functions, set openmp=True in the main namespace before importing this file, and (optionally) set openmp_threads to the number of threads desired. If openmp_threads is not set, then a thread will be allocated for each available core by default.
Note that in order to use OpenMP, the C functions are obliged to use the thread-safe portions of the Python/Numpy C API. In general, the Python C API cannot be assumed to be thread safe. Calls to PyObject_SetAttrString are a common hazard which can often be avoided using LOOKUP_FROM_SLOT_OFFSET. This makes use of Python’s __slots__ mechanism with the added benefit of bypassing the GIL.
If not using optimization, replace the optimized component with its unoptimized equivalent.
The objects named by optimized_name and unoptimized_name should be plug-compatible. The local_dict argument should be given the contents of locals(), so that this function can replace the optimized version with the unoptimized one in the namespace from which it has been called.
As an example, calling this function as:
provide_unoptimized_equivalent("sort_opt","sort",locals())
is equivalent to putting the following code directly into the calling location:
if not optimized:
sort_opt = sort
print 'module: Inline-optimized components not available; using sort instead of sort_opt.'
Topographica IPython extension for notebook support. Automatically loaded when importing the topo module but may also be explicitly loaded using:
%load_ext topo.misc.ipython
Bases: IPython.core.magic.Magics
Line magic that defines a cell magic to export the cell contents to a specific file. For instance, running
%define_exporter OUT ./output.txt
will define an %%OUT cell magic that writes to the file output.txt. This cell magic takes a optional arguments ‘clear’ and ‘run’. If ‘clear’ should be specified for the first cell to be exported. If ‘run’ is specified, the cell is executed in the active notebook as well as exported.
Dynamically add trait attributes to the HasTraits instance.
Print docstring if incorrect arguments were passed
Generate rST documentation for this class’ config options.
Excludes traits defined on parent classes.
Get the config class config section
Get the help string for this class in ReST format.
If inst is given, it’s current trait values will be used in place of class defaults.
Get the help string for a single trait.
If inst is given, it’s current trait values will be used in place of the class default.
Get a dict of all the traitlets defined on this class, not a parent.
Works like class_traits, except for excluding traits from parents.
Get the help string for a single trait and print it.
Get a list of all the names of this class’ traits.
This method is just like the trait_names() method, but is unbound.
Get a dict of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.
This method is just like the traits() method, but is unbound.
The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. This does not allow for any simple way of testing that a metadata name exists and has any value because get_metadata returns None if a metadata key doesn’t exist.
Make an entry in the options_table for fn, with value optstr
Format a string for latex inclusion.
Returns True if the object has a trait with the specified name.
Context manager for bundling trait change notifications and cross validation.
Use this when doing multiple trait assignments (init, config), to avoid race conditions in trait notifiers requesting other trait values. All trait notifications will fire after all values have been assigned.
Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).
Parse options passed to an argument string.
The interface is similar to that of getopt.getopt(), but it returns a Struct with the options as keys and the stripped argument string still as a string.
arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.
return section names as a list
Get metadata values for trait by key.
Get a list of all the names of this class’ traits.
Get a dict of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.
The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. This does not allow for any simple way of testing that a metadata name exists and has any value because get_metadata returns None if a metadata key doesn’t exist.
Fire the traits events when the config is updated.
Given a v3-format .ipynb notebook file (from IPython 0.13 or later), allows the contents of labelled code cells to be exported to a plaintext file. The typical use case is for generating a runnable plain Python source file from selected cells of an IPython notebook.
Code is selected for export by placing the given identifier on the first line of the chosen code cells. By default, only the labelled cells are exported. This behavior may be inverted to exclude labelled cells by setting the invert flag.
notebook The filename of the notebook (.ipynb). output_path Optional output file path. Otherwise, uses the notebook basename. ext The file extension of the output. identifier The identifier used to label cells. diff Whether to print a diff when overwriting content. invert When set, only the non-labelled cells are exported. stale_time Number of seconds that may elapse since the last notebook
save before a staleness warning is issued. Useful when exporting from an active IPython notebook.
Helper function to repeatedly prompt the user with a list of options. If no input is given, the default value is returned. If wither the skip flag is True or the prompt is in a batch mode environment (e.g. for automated testing), the default value is immediately returned.
The Topographica Lancet extension allows Topographica simulations to be easily integrated into a Lancet workflow (see github.com/ioam/lancet). The TopoCommand is appropriate for simple runs using the default analysis function, whereas the RunBatchCommand allow for more sophisticated measurements and analysis to be executed during a simulation run using a holoviews Collector object.
Bases: lancet.core.PrettyPrinted, param.parameterized.Parameterized
BatchCollector is a wrapper class used to execute a Collector in a Topographica run_batch context, saving the HoloViews to disk as *.hvz files.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Classmethod used to load the RunBatchCommand callable into a Topographica run_batch context. Loads the pickle file based on the batch_name and root directory in batch_info.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Locates the pickle file based on the given launch info dictionary. Used by load as a classmethod and by save as an instance method.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Method to define the positional arguments and keyword order for pretty printing.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Check that a times list has been supplied, call verify_times on the Collator and if model_params has been supplied, check that a valid parameter set has been used.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: topo.misc.lancext.TopoCommand
Runs a custom analysis function specified by a Collector using run_batch. This command is far more flexible for regular usage than TopoCommand as it allows you to build a run_batch analysis incrementally.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Method to define the positional arguments and keyword order for pretty printing.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Write to file_handle if supplied, othewise print output
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: lancet.launch.Command
TopoCommand is designed to to format Lancet Args objects into run_batch commands in a general way. Note that Topographica is always invoked with the -a flag so all of topo.command is imported.
Some of the parameters duplicate those in run_batch to ensure consistency with previous run_batch usage in Topographica. As a consequence, this class sets all the necessary options for run_batch except the ‘times’ parameter which may vary specified arbitrarily by the Lancet Args object.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Optional method that allows a Command to save state before launch. The info argument is supplied by the Launcher.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Method to define the positional arguments and keyword order for pretty printing.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Write to file_handle if supplied, othewise print output
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
A succinct summary of the Command configuration. Unlike the repr, a summary does not have to be complete but must supply key information relevant to the user. Must begin by stating the executable.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Optional, final check that ensures valid arguments have been passed before launch. Allows the constant and varying_keys to be be checked and can inspect the specs attribute if an instance of Args. If invalid, raise an Exception with the appropriate error message, otherwise return None.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: param.parameterized.ParameterizedFunction
This class is closely related to the param_formatter class in topo/command/__init__.py. Like that default class, it formats parameters as a string for use in a directory name. Unlike that default class, it does not use the parameters repr methods but the exact, succinct commandline representation as returned by a Lancet Args object.
This version has several advantages over the default:
example, a value specified as 6.00 on the commandline remains this way and is never represented to higher precision or with floating point error.
(optionally) alphanumerically by default.
length for values.
varying (may be toggled).
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Same as Parameterized.pprint, except that X.classname(Y is replaced with X.classname.instance(Y
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Same as Parameterized.script_repr, except that X.classname(Y is replaced with X.classname.instance(Y
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: param.parameterized.Parameterized
Topographica specific helper function that expands on Lancet’s vcs_metadata function to generate suitable metadata information for logging with Lancet. Records Topographica version control information as well as information about all relevant submodules and the current numpy version.
No arguments should be necessary when either contructing or calling this object as the default behaviour is designed to be useful. The summary method prints out the key information collected to assist with reproducibility. For instance, this may be called to print all the relevant git revisions in an IPython Notebook before launching jobs.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Code used to support old snapshots (those created from 0.9.7/r11275 onwards).
For use when module parent.name is now actual_module.
Functions for measuring memory usage, to allow usage to be optimized.
Examples:
bin/python -c 'execfile("topo/misc/memuse.py") ; print topsize_mb()'
./topographica -c 'from topo.misc import memuse' -c 'print memuse.topsize_mb()'
./topographica -c 'from topo.misc import memuse, asizeof' -c 'print memuse.topsize_mb()'
./topographica -a -c 'from topo.misc import memuse, asizeof' -c 'print memuse.topsize_mb()'
./topographica -a -c 'from topo.misc import memuse, asizeof' examples/tiny.ty -c 'print memuse.allsizes_mb()'
./topographica -a -c 'from topo.misc import memuse, asizeof' -c 'memuse.memuse_batch("examples/tiny.ty",cortex_density=20)'
./topographica -a -c 'from topo.misc import memuse, asizeof' -c 'memuse.memuse_batch("examples/tiny.ty",times=[0,100],analysis_fn=memuse.plotting_and_saving_analysis_fn,cortex_density=20)'
Collates results from topsize, simsize, and wtsize.
Formatted to suggest that the topsize is made up of code (not currently estimated), topo.sim (apart from weights), and weights.
Run a system command as in os.system(), but capture the stdout and return it as a string.
Basic memuse function for use with memuse_batch()
Similar to run_batch, but analyzes the memory requirement of a simulation at different times.
First, the specified script file will be run using the specified parameters. Then at each of the specified times, the given analysis_fn (which calls allsizes_mb() by default) is run. The output is labeled with the script file, time, and parameters so that results from different runs can be compared.
For use with memuse_batch() to test snapshot and plotting memory usage.
Return the size of topo.sim reported by asizeof.asizeof(). This estimate does not currently include any numpy arrays, and may also be missing other important items.
Python 2.6 supports getsizeof() and a __sizeof__ attribute that user code can implement, which should provide a more accurate estimate.
String-formatted version of the value reported by asizeof(topo.sim).
Return the RES size of this process as reported by the top(1) command.
Extensions to pickle allowing items in __main__ to be saved.
Bases: object
Pickle support for types and functions defined in __main__.
When pickled, saves types and functions defined in __main__ by value (i.e. as bytecode). When unpickled, loads previously saved types and functions back into __main__.
Save an interactively defined classic class object by value
Save functions by value if they are defined interactively
Save obj as a global reference. Used for objects that pickle does not find correctly.
Save modules by reference, except __main__ which also gets its contents saved by value
Special-case __main__.__dict__. Useful for a function’s func_globals member.
Support for optional Cython .pyx files.
Bases: object
Supports pickling of Parameterized class attributes for a given module.
When requested to be pickled, stores a module’s PO classes’ attributes, and any given startup_commands. On unpickling, executes the startup commands and sets the class attributes.
Recursively search module and get attributes of Parameterized classes within it.
class_attributes is a dictionary {module.path.and.Classname: state}, where state is the dictionary {attribute: value}.
Something is considered a module for our purposes if inspect says it’s a module, and it defines __all__. We only search through modules listed in __all__.
Keeps a list of processed modules to avoid looking at the same one more than once (since e.g. __main__ contains __main__ contains __main__...)
Modules can be specifically excluded if listed in exclude.
Object classes for recording and plotting time-series data.
This module defines a set of DataRecorder object types for recording time-series data, a set of Trace object types for specifying ways of generating 1D-vs-time traces from recorded data, and a TraceGroup object that will plot a set of traces on stacked, aligned axes.
Bases: param.parameterized.Parameterized
An object encapsulating a series of movie frames displaying the value of one or more matrix-valued time-series contained in a DataRecorder object.
An ActivityMovie takes a DataRecorder object, a list of names of variables in that recorder and a sequence of timepoints at which to sample those variables. It uses that information to compose a sequence of MontageBitmap objects displaying the stored values of each variable at each timepoint. These bitmaps can then be saved to sequentially-named files that can be composited into a movie by external software.
Parameters are available to control the layout of the montage, adding timecodes to the frames, and the names of the frame files.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: topo.base.simulation.EventProcessor
Record time-series data from a simulation.
A DataRecorder instance stores a set of named time-series variables, consisting of a sequence of recorded data items of any type, along with the times at which they were recorded.
DataRecorder is an abstract class for which different implementations may exist for different means of storing recorded data. For example, the subclass InMemoryRecorder stores all the data in memory.
A DataRecorder instance can operate either as an event processor, or in a stand-alone mode. Both usage modes can be used on the same instance in the same simulation.
STAND-ALONE USAGE:
A DataRecorder instance is used as follows:
- Method .add_variable adds a named time series variable.
- Method .record_data records a new data item and timestamp.
- Method .get_data gets a time-delimited sequence of data from a variable
EVENTPROCESSOR USAGE:
A DataRecorder can also be connected to a simulation as an event processor, forming a kind of virtual recording equipment. An output port from any event processor in a simulation can be connected to a DataRecorder; the recorder will automaticall create a variable with the same name as the connection, and record any incoming data on that variable with the time it was received. For example:
topo.sim['Recorder'] = InMemoryRecorder()
topo.sim.connect('V1','Recorder',name='V1 Activity')
This script snippet will create a new DataRecorder and automatically record all activity sent from the sheet ‘V1’.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Get the named timeseries between the given times (inclusive). If fill_range is true, the returned data will have timepoints exactly at the start and end of the given timerange. The data values at these timepoints will be those of the next-earlier datapoint in the series.
(NOTE: fill_range can fail to create a beginning timepoint if the start of the time range is earlier than the first recorded datapoint.]
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
For the named variable, get the start and end indices suitable for slicing the data to include all times t:
start_time <= t <= end_time.
A start_ or end_time of None is interpreted to mean the earliest or latest available time, respectively.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Called by the simulation before advancing the simulation time. Allows the event processor to do any computation that requires that all events for this time have been delivered. Computations performed in this method should not generate any events with a zero time delay, or else causality could be violated. (By default, does nothing.)
Record the given data item with the given timestamp in the named timeseries.
Generate a runnable command for creating this EventProcessor.
Send some data out to all connections on the given src_port. The data is deepcopied before it is sent out, to ensure that future changes to the data are not reflected in events from the past.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Called by the simulation when the EventProcessor is added to the simulation.
If an EventProcessor needs to have any code run when it is added to the simulation, the code can be put into this method in the subclass.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: topo.misc.trace.Trace
A Trace that returns the data, unmodified.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: topo.misc.trace.DataRecorder
A data recorder that stores all recorded data in memory.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
For the named variable, get the start and end indices suitable for slicing the data to include all times t:
start_time <= t <= end_time.
A start_ or end_time of None is interpreted to mean the earliest or latest available time, respectively.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Called by the simulation before advancing the simulation time. Allows the event processor to do any computation that requires that all events for this time have been delivered. Computations performed in this method should not generate any events with a zero time delay, or else causality could be violated. (By default, does nothing.)
Generate a runnable command for creating this EventProcessor.
Send some data out to all connections on the given src_port. The data is deepcopied before it is sent out, to ensure that future changes to the data are not reflected in events from the past.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Called by the simulation when the EventProcessor is added to the simulation.
If an EventProcessor needs to have any code run when it is added to the simulation, the code can be put into this method in the subclass.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: topo.misc.trace.Trace
A Trace that assumes that each data item is a sequence that can be indexed with a single integer, and traces the value of one indexed element.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: topo.misc.trace.Trace
A trace that assumes that the data are sheet activity matrices, and traces the value of a given (x,y) position on the sheet.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: param.parameterized.Parameterized
A specification for generating 1D traces of data from recorded timeseries.
A Trace object is a callable object that encapsulates a method for generating a 1-dimensional trace from possibly multidimensional timeseries data, along with a specification for how to plot that data, including Y-axis boundaries and plotting arguments.
Trace is an abstract class. Subclasses implement the __call__ method to define how to extract a 1D trace from a sequence of data.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Bases: param.parameterized.Parameterized
A group of data traces to be plotted together.
A TraceGroup defines a set of associated data traces and allows them to be plotted on stacked, aligned axes. The constructor takes a DataRecorder object as a data source, and a list of Trace objects that indicate the traces to plot. The trace specifications are stored in the attribute self.traces, which can be modified at any time.
Print msg merged with args as a debugging statement.
See Python’s logging module for details of message formatting.
Return {parameter_name:parameter.default} for all non-constant Parameters.
Note that a Parameter for which instantiate==True has its default instantiated.
Return a list of name,value pairs for all Parameters of this object.
If onlychanged is True, will only return values that are not equal to the default value.
Print msg merged with args as a message.
See Python’s logging module for details of message formatting.
Return the Parameters of this class as the dictionary {name: parameter_object}
Includes Parameters from this class and its superclasses.
Plot the traces.
Requires MatPlotLib (aka pylab).
Plots the traces specified in self.traces, over the timespan specified by times. times = (start_time,end_time); if either start_time or end_time is None, it is assumed to extend to the beginning or end of the timeseries, respectively.
(Experimental) Pretty printed representation that may be evaluated with eval. See pprint() function for more details.
Print the default values of all cls’s Parameters.
Print the values of all this object’s Parameters.
Variant of __repr__ designed for generating a runnable script.
Set the default value of param_name.
Equivalent to setting param_name on the class.
Restore the most recently saved state.
See state_push() for more details.
Save this instance’s state.
For Parameterized instances, this includes the state of dynamically generated values.
Subclasses that maintain short-term state should additionally save and restore that state using state_push() and state_pop().
Generally, this method is used by operations that need to test something without permanently altering the objects’ state.
Print msg merged with args as a verbose message.
See Python’s logging module for details of message formatting.
Print msg merged with args as a warning, unless module variable warnings_as_exceptions is True, then raise an Exception containing the arguments.
See Python’s logging module for details of message formatting.
Get a time-sequence of matrix data from a DataRecorder variable and convert it to a sequence of images stored in Bitmap objects.
Parameters: name is the name of the variable to be queried. times is a sequence of timepoints at which to query the variable. recorder is the data recorder. overlays is a tuple of matrices or scalars to be added to the red, green, and blue channels of the bitmaps respectively.
Unit Support: Classes and methods to allow Unum and Quantities unit packages to interact with Topographica.
General utility functions and classes.
Bases: object
For all file_like_objs passed on initialization, provides a convenient way to call any of file’s methods (on all of them).
E.g. The following would cause ‘test’ to be written into two files, as well as to stdout:
import sys f1 = open(‘file1’,’w’) f2 = open(‘file2’,’w’) m = MultiFile(f1,f2,sys.stdout) m.write(‘test’)
A simple structure class, taking keyword args and assigning them to attributes.
For instance:
>>> s = Struct(foo='a',bar=1)
>>> s.foo
'a'
>>> s.bar
1
Return the centroid of a weighted set of points as an array.
The pts argument should be an array of points, one per row, and weights should be a vector of weights.
Returns a list of the indices needed to address or loop over all the elements of a 1D or 2D matrix with the given shape. E.g.:
flat_indices((3,)) == [0,1,2]
Flattens a list.
Written by Bearophile as published on the www.python.org newsgroups. Pulled into Topographica 3/5/2005.
A range function that accepts float increments.
Otherwise, works just as the inbuilt range() function. If inclusive is False, as in the default, the range is exclusive (not including the end value), as in the inbuilt range(). If inclusive is true, the range may include the end value.
‘All theoretic restrictions apply, but in practice this is more useful than in theory.’
From: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66472
Given a flat matrix index and a 2D matrix shape, return the (row,col) coordinates of the index.
Interpolate an appropriate value from the given list of values, by number.
Assumes the table is a list of items to be returned for integer values, and interpolates between those items for non-integer values.
Profile the given command (supplied as a string), printing statistics about the top n functions when ordered according to sorting.
sorting defaults to ordering by cumulative time and then internal time; see http://docs.python.org/lib/profile-stats.html for other sorting options.
By default, the complete paths of files are not shown. If there are multiple files with the same name, you might wish to set strip_dirs=False to make it easier to follow the output.
Examples:
profile(‘execfile(“examples/hierarchical.ty”)’)
profile(‘topo.sim.run(10)’)
profile(‘execfile(“examples/lissom_oo_or.ty”);topo.sim.run(20000)’)
./topographica examples/hierarchical.ty -c “from topo.misc.util import profile; profile(‘topo.sim.run(10)’)”
Given a row, column, and matrix shape, return the corresponding index into the flattened (raveled) matrix.
Returns the class name of x as a string with the leading package information removed.
E.g. if x is of type “<class ‘topo.base.sheet.Sheet’>”, returns “Sheet”
Split x into its sign and absolute value.
Returns a tuple (sign(x),abs(x)). Note: sign(0) = 1, unlike numpy.sign.
Select randomly from the given sequence.
The weights, if given, should be a sequence the same length as seq, as would be passed to weighted_sample_idx().
Return an integer generated by sampling the discrete distribution represented by the sequence of weights.
The integer will be in the range [0,len(weights)). The weights need not sum to unity, but can contain any non-negative values (e.g., [1 1 1 100] is a valid set of weights).
To use weights from a 2D numpy array w, specify w.ravel() (not the w.flat iterator).