Package topo :: Package misc :: Module utils
[hide private]
[frames] | no frames]

Module utils

source code

General utility functions and classes.

$Id: utils.py 8026 2008-02-19 14:30:05Z ceball $




Version: $Revision: 8026 $

Classes [hide private]
  Struct
A simple structure class, taking keyword args and assigning them to attributes.
  Singleton
The singleton pattern.
  ExtraPickler
Provides a simple means to include arbitrary attributes from modules when pickling.
Functions [hide private]
 
NxN(tuple)
Converts a tuple (X1,X2,...,Xn) to a string 'X1xX2x...xXn'
source code
 
flat_indices(shape)
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.
source code
 
flatten(l)
Flattens a list.
source code
 
eval_atof(str)
Evaluates the given string in __main__, and converts it to a float.
source code
 
cross_product(ss, row=[], level=0) source code
 
frange(start, end=None, inc=1.0, inclusive=False)
A range function that accepts float increments.
source code
 
values_sorted_by_key(d)
Return the values of dictionary d sorted by key.
source code
 
keys_sorted_by_value(d)
Return the keys of dictionary d sorted by value.
source code
 
inverse(dict_)
Return the inverse of dictionary dict_.
source code
 
shortclassname(x)
Returns the class name of x as a string with the leading package information removed.
source code
 
profile(command, n=50, sorting=('cumulative', 'time'), strip_dirs=False)
Profile the given command (supplied as a string), printing statistics about the top n functions when ordered according to sorting.
source code
 
weighted_sample(seq, weights=[])
Select randomly from the given sequence.
source code
 
weighted_sample_idx(weights)
Return an integer generated by sampling the discrete distribution represented by the sequence of weights.
source code
 
idx2rowcol(idx, shape)
Given a flat matrix index and a 2D matrix shape, return the (row,col) coordinates of the index.
source code
 
rowcol2idx(r, c, shape)
Given a row, column, and matrix shape, return the corresponding index into the flattened (raveled) matrix.
source code
 
centroid(pts, weights)
Return the centroid of a weighted set of points as an array.
source code
 
signabs(x)
Split x into its sign and absolute value.
source code
Function Details [hide private]

flat_indices(shape)

source code 

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]

flatten(l)

source code 

Flattens a list.

Written by Bearophile as published on the www.python.org newsgroups. Pulled into Topographica 3/5/2005.

eval_atof(str)

source code 

Evaluates the given string in __main__, and converts it to a float.

The string can contain any expression that will evaluate to a number. The expression can use any variables or functions that are defined in the main namespace.

See string.atof() for more details about the conversion from the evaluated string into a float.

frange(start, end=None, inc=1.0, inclusive=False)

source code 

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

inverse(dict_)

source code 

Return the inverse of dictionary dict_.

(I.e. return a dictionary with keys that are the values of dict_,
and values that are the corresponding keys from dict_.)

The values of dict_ must be unique.

shortclassname(x)

source code 

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"

profile(command, n=50, sorting=('cumulative', 'time'), strip_dirs=False)

source code 

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 loading a simulation:

profile('execfile("examples/hierarchical.ty")')

  • profile running an already loaded simulation:

profile('topo.sim.run(10)')

  • profile running a whole simulation:

profile('execfile("examples/lissom_oo_or.ty");topo.sim.run(20000)')

  • profile running a simulation, but from the commandline:

./topographica examples/hierarchical.ty -c "from topo.misc.utils import profile; profile('topo.sim.run(10)')"

weighted_sample(seq, weights=[])

source code 

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().

weighted_sample_idx(weights)

source code 

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).

centroid(pts, weights)

source code 

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.

signabs(x)

source code 

Split x into its sign and absolute value.

Returns a tuple (sign(x),abs(x)). Note: sign(0) = 1, unlike numpy.sign.