Test method
Bases: sas.data_util.calcthread.CalcThread
Example of a calculation thread.
Threaded calculation class. Inherit from here and specialize the compute() method to perform the appropriate operations for the class.
If you specialize the __init__ method be sure to call CalcThread.__init__, passing it the keyword arguments for yieldtime, worktime, update and complete.
When defining the compute() method you need to include code which allows the GUI to run. They are as follows:
self.isquit() # call frequently to check for interrupts
self.update(kw=...) # call when the GUI could be updated
self.complete(kw=...) # call before exiting compute()
The update() and complete() calls accept field=value keyword arguments which are passed to the called function. complete() should be called before exiting the GUI function. A KeyboardInterrupt event is triggered if the GUI signals that the computation should be halted.
The following documentation should be included in the description of the derived class.
The user of this class will call the following:
thread = Work(...,kw=...) # prepare the work thread.
thread.queue(...,kw=...) # queue a work unit
thread.requeue(...,kw=...) # replace work unit on the end of queue
thread.reset(...,kw=...) # reset the queue to the given work unit
thread.stop() # clear the queue and halt
thread.interrupt() # halt the current work unit but continue
thread.ready(delay=0.) # request an update signal after delay
thread.isrunning() # returns true if compute() is running
Use queue() when all work must be done. Use requeue() when intermediate work items don’t need to be done (e.g., in response to a mouse move event). Use reset() when the current item doesn’t need to be completed before the new event (e.g., in response to a mouse release event). Use stop() to halt the current and pending computations (e.g., in response to a stop button).
The methods queue(), requeue() and reset() are proxies for the compute() method in the subclass. Look there for a description of the arguments. The compute() method can be called directly to run the computation in the main thread, but it should not be called if isrunning() returns true.
The constructor accepts additional keywords yieldtime=0.01 and worktime=0.01 which determine the cooperative multitasking behaviour. Yield time is the duration of the sleep period required to give other processes a chance to run. Work time is the duration between sleep periods.
Notifying the GUI thread of work in progress and work complete is done with updatefn=updatefn and completefn=completefn arguments to the constructor. Details of the parameters to the functions depend on the particular calculation class, but they will all be passed as keyword arguments. Details of how the functions should be implemented vary from framework to framework.
For wx, something like the following is needed:
import wx, wx.lib.newevent
(CalcCompleteEvent, EVT_CALC_COMPLETE) = wx.lib.newevent.NewEvent()
# methods in the main window class of your application
def __init__():
...
# Prepare the calculation in the GUI thread.
self.work = Work(completefn=self.CalcComplete)
self.Bind(EVT_CALC_COMPLETE, self.OnCalcComplete)
...
# Bind work queue to a menu event.
self.Bind(wx.EVT_MENU, self.OnCalcStart, id=idCALCSTART)
...
def OnCalcStart(self,event):
# Start the work thread from the GUI thread.
self.work.queue(...work unit parameters...)
def CalcComplete(self,**kwargs):
# Generate CalcComplete event in the calculation thread.
# kwargs contains field1, field2, etc. as defined by
# the Work thread class.
event = CalcCompleteEvent(**kwargs)
wx.PostEvent(self, event)
def OnCalcComplete(self,event):
# Process CalcComplete event in GUI thread.
# Use values from event.field1, event.field2 etc. as
# defined by the Work thread class to show the results.
...
Update the GUI with the completed results from a work unit.
Perform a work unit. The subclass will provide details of the arguments.
Stop the current work item. To clear the work queue as well call the stop() method.
Check for interrupts. Should be called frequently to provide user responsiveness. Also yields to other running threads, which is required for good performance on OS X.
Add a work unit to the end of the queue. See the compute() method for details of the arguments to the work unit.
Ready for another update after delay=t seconds. Call this for threads which can show intermediate results from long calculations.
Replace the work unit on the end of the queue. See the compute() method for details of the arguments to the work unit.
Clear the queue and start a new work unit. See the compute() method for details of the arguments to the work unit.
Clear the queue and stop the thread. New items may be queued after stop. To stop just the current work item, and continue the rest of the queue call the interrupt method
Update GUI with the lastest results from the current work unit.
Error propogation algorithms for simple arithmetic
Warning: like the underlying numpy library, the inplace operations may return values of the wrong type if some of the arguments are integers, so be sure to create them with floating point inputs.
Addition with error propagation
In-place addition with error propagation
Division with error propagation
In-place division with error propagation
Exponentiation with error propagation
Logarithm with error propagation
Multiplication with error propagation
In-place multiplication with error propagation
X**n with error propagation
In-place X**n with error propagation
Subtraction with error propagation
In-place subtraction with error propagation
Format values and uncertainties nicely for printing.
:func:`format_uncertainty_pm`(v,err) produces the expanded format v +/- err.
:func:`format_uncertainty_compact`(v,err) produces the compact format v(##), where the number in parenthesis is the uncertainty in the last two digits of v.
:func:`format_uncertainty`(v,err) uses the compact format by default, but this can be changed to use the expanded +/- format by setting format_uncertainty.compact to False.
The formatted string uses only the number of digits warranted by the uncertainty in the measurement.
If the uncertainty is 0 or not otherwise provided, the simple %g floating point format option is used.
Infinite and indefinite numbers are represented as inf and NaN.
Example:
>>> v,dv = 757.2356,0.01032
>>> print format_uncertainty_pm(v,dv)
757.236 +/- 0.010
>>> print format_uncertainty_compact(v,dv)
757.236(10)
>>> print format_uncertainty(v,dv)
757.236(10)
>>> format_uncertainty.compact = False
>>> print format_uncertainty(v,dv)
757.236 +/- 0.010
UncertaintyFormatter() returns a private formatter with its own formatter.compact flag.
Given value v and uncertainty dv, return a string v +/- dv.
Given value v and uncertainty dv, return the compact representation v(##), where ## are the first two digits of the uncertainty.
Define unit conversion support for NeXus style units.
The unit format is somewhat complicated. There are variant spellings and incorrect capitalization to worry about, as well as forms such as “mili*metre” and “1e-7 seconds”.
This is a minimal implementation of units including only what I happen to need now. It does not support the complete dimensional analysis provided by the package udunits on which NeXus is based, or even the units used in the NeXus definition files.
Unlike other units packages, this package does not carry the units along with the value but merely provides a conversion function for transforming values.
Usage example:
import nxsunit
u = nxsunit.Converter('mili*metre') # Units stored in mm
v = u(3000,'m') # Convert the value 3000 mm into meters
NeXus example:
# Load sample orientation in radians regardless of how it is stored.
# 1. Open the path
file.openpath('/entry1/sample/sample_orientation')
# 2. scan the attributes, retrieving 'units'
units = [for attr,value in file.attrs() if attr == 'units']
# 3. set up the converter (assumes that units actually exists)
u = nxsunit.Converter(units[0])
# 4. read the data and convert to the correct units
v = u(file.read(),'radians')
This is a standalone module, not relying on either DANSE or NeXus, and can be used for other unit conversion tasks.
Note: minutes are used for angle and seconds are used for time. We cannot tell what the correct interpretation is without knowing something about the fields themselves. If this becomes an issue, we will need to allow the application to set the dimension for the unit rather than inferring the dimension from an example unit.
Bases: object
Unit converter for NeXus style units.
A dict that keeps keys in insertion order
Bases: dict
A class of dictionary that keeps the insertion order of keys.
All appropriate methods return keys, items, or values in an ordered way.
All normal dictionary methods are available. Update and comparison is restricted to other OrderedDict objects.
Various sequence methods are available, including the ability to explicitly mutate the key ordering.
__contains__ tests:
>>> d = OrderedDict(((1, 3),))
>>> 1 in d
1
>>> 4 in d
0
__getitem__ tests:
>>> OrderedDict(((1, 3), (3, 2), (2, 1)))[2]
1
>>> OrderedDict(((1, 3), (3, 2), (2, 1)))[4]
Traceback (most recent call last):
KeyError: 4
__len__ tests:
>>> len(OrderedDict())
0
>>> len(OrderedDict(((1, 3), (3, 2), (2, 1))))
3
get tests:
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.get(1)
3
>>> d.get(4) is None
1
>>> d.get(4, 5)
5
>>> d
OrderedDict([(1, 3), (3, 2), (2, 1)])
has_key tests:
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.has_key(1)
1
>>> d.has_key(4)
0
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.clear()
>>> d
OrderedDict([])
>>> OrderedDict(((1, 3), (3, 2), (2, 1))).copy()
OrderedDict([(1, 3), (3, 2), (2, 1)])
Return the position of the specified key in the OrderedDict.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.index(3)
1
>>> d.index(4)
Traceback (most recent call last):
ValueError: list.index(x): x not in list
Takes index, key, and value as arguments.
Sets key to value, so that key is at position index in the OrderedDict.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.insert(0, 4, 0)
>>> d
OrderedDict([(4, 0), (1, 3), (3, 2), (2, 1)])
>>> d.insert(0, 2, 1)
>>> d
OrderedDict([(2, 1), (4, 0), (1, 3), (3, 2)])
>>> d.insert(8, 8, 1)
>>> d
OrderedDict([(2, 1), (4, 0), (1, 3), (3, 2), (8, 1)])
items returns a list of tuples representing all the (key, value) pairs in the dictionary.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.items()
[(1, 3), (3, 2), (2, 1)]
>>> d.clear()
>>> d.items()
[]
>>> ii = OrderedDict(((1, 3), (3, 2), (2, 1))).iteritems()
>>> ii.next()
(1, 3)
>>> ii.next()
(3, 2)
>>> ii.next()
(2, 1)
>>> ii.next()
Traceback (most recent call last):
StopIteration
>>> ii = OrderedDict(((1, 3), (3, 2), (2, 1))).iterkeys()
>>> ii.next()
1
>>> ii.next()
3
>>> ii.next()
2
>>> ii.next()
Traceback (most recent call last):
StopIteration
>>> iv = OrderedDict(((1, 3), (3, 2), (2, 1))).itervalues()
>>> iv.next()
3
>>> iv.next()
2
>>> iv.next()
1
>>> iv.next()
Traceback (most recent call last):
StopIteration
Return a list of keys in the OrderedDict.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.keys()
[1, 3, 2]
No dict.pop in Python 2.2, gotta reimplement it
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.pop(3)
2
>>> d
OrderedDict([(1, 3), (2, 1)])
>>> d.pop(4)
Traceback (most recent call last):
KeyError: 4
>>> d.pop(4, 0)
0
>>> d.pop(4, 0, 1)
Traceback (most recent call last):
TypeError: pop expected at most 2 arguments, got 3
Delete and return an item specified by index, not a random one as in dict. The index is -1 by default (the last item).
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.popitem()
(2, 1)
>>> d
OrderedDict([(1, 3), (3, 2)])
>>> d.popitem(0)
(1, 3)
>>> OrderedDict().popitem()
Traceback (most recent call last):
KeyError: 'popitem(): dictionary is empty'
>>> d.popitem(2)
Traceback (most recent call last):
IndexError: popitem(): index 2 not valid
Rename the key for a given value, without modifying sequence order.
For the case where new_key already exists this raise an exception, since if new_key exists, it is ambiguous as to what happens to the associated values, and the position of new_key in the sequence.
>>> od = OrderedDict()
>>> od['a'] = 1
>>> od['b'] = 2
>>> od.items()
[('a', 1), ('b', 2)]
>>> od.rename('b', 'c')
>>> od.items()
[('a', 1), ('c', 2)]
>>> od.rename('c', 'a')
Traceback (most recent call last):
ValueError: New key already exists: 'a'
>>> od.rename('d', 'b')
Traceback (most recent call last):
KeyError: 'd'
Reverse the order of the OrderedDict.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.reverse()
>>> d
OrderedDict([(2, 1), (3, 2), (1, 3)])
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.setdefault(1)
3
>>> d.setdefault(4) is None
True
>>> d
OrderedDict([(1, 3), (3, 2), (2, 1), (4, None)])
>>> d.setdefault(5, 0)
0
>>> d
OrderedDict([(1, 3), (3, 2), (2, 1), (4, None), (5, 0)])
This method allows you to set the items in the dict.
It takes a list of tuples - of the same sort returned by the items method.
>>> d = OrderedDict()
>>> d.setitems(((3, 1), (2, 3), (1, 2)))
>>> d
OrderedDict([(3, 1), (2, 3), (1, 2)])
setkeys all ows you to pass in a new list of keys which will replace the current set. This must contain the same set of keys, but need not be in the same order.
If you pass in new keys that don’t match, a KeyError will be raised.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.keys()
[1, 3, 2]
>>> d.setkeys((1, 2, 3))
>>> d
OrderedDict([(1, 3), (2, 1), (3, 2)])
>>> d.setkeys(['a', 'b', 'c'])
Traceback (most recent call last):
KeyError: 'Keylist is not the same as current keylist.'
You can pass in a list of values, which will replace the current list. The value list must be the same len as the OrderedDict.
(Or a ValueError is raised.)
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.setvalues((1, 2, 3))
>>> d
OrderedDict([(1, 1), (3, 2), (2, 3)])
>>> d.setvalues([6])
Traceback (most recent call last):
ValueError: Value list is not the same length as the OrderedDict.
Sort the key order in the OrderedDict.
This method takes the same arguments as the list.sort method on your version of Python.
>>> d = OrderedDict(((4, 1), (2, 2), (3, 3), (1, 4)))
>>> d.sort()
>>> d
OrderedDict([(1, 4), (2, 2), (3, 3), (4, 1)])
Update from another OrderedDict or sequence of (key, value) pairs
>>> d = OrderedDict(((1, 0), (0, 1)))
>>> d.update(OrderedDict(((1, 3), (3, 2), (2, 1))))
>>> d
OrderedDict([(1, 3), (0, 1), (3, 2), (2, 1)])
>>> d.update({4: 4})
Traceback (most recent call last):
TypeError: undefined order, cannot get items from dict
>>> d.update((4, 4))
Traceback (most recent call last):
TypeError: cannot convert dictionary update sequence element "4" to a 2-item sequence
Return a list of all the values in the OrderedDict.
Optionally you can pass in a list of values, which will replace the current list. The value list must be the same len as the OrderedDict.
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.values()
[3, 2, 1]
Bases: sas.data_util.odict.OrderedDict
Experimental version of OrderedDict that has a custom object for keys, values, and items.
These are callable sequence objects that work as methods, or can be manipulated directly as sequences.
Test for keys, items and values.
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.keys
[1, 2, 3]
>>> d.keys()
[1, 2, 3]
>>> d.setkeys((3, 2, 1))
>>> d
SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
>>> d.setkeys((1, 2, 3))
>>> d.keys[0]
1
>>> d.keys[:]
[1, 2, 3]
>>> d.keys[-1]
3
>>> d.keys[-2]
2
>>> d.keys[0:2] = [2, 1]
>>> d
SequenceOrderedDict([(2, 3), (1, 2), (3, 4)])
>>> d.keys.reverse()
>>> d.keys
[3, 1, 2]
>>> d.keys = [1, 2, 3]
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.keys = [3, 1, 2]
>>> d
SequenceOrderedDict([(3, 4), (1, 2), (2, 3)])
>>> a = SequenceOrderedDict()
>>> b = SequenceOrderedDict()
>>> a.keys == b.keys
1
>>> a['a'] = 3
>>> a.keys == b.keys
0
>>> b['a'] = 3
>>> a.keys == b.keys
1
>>> b['b'] = 3
>>> a.keys == b.keys
0
>>> a.keys > b.keys
0
>>> a.keys < b.keys
1
>>> 'a' in a.keys
1
>>> len(b.keys)
2
>>> 'c' in d.keys
0
>>> 1 in d.keys
1
>>> [v for v in d.keys]
[3, 1, 2]
>>> d.keys.sort()
>>> d.keys
[1, 2, 3]
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)), strict=True)
>>> d.keys[::-1] = [1, 2, 3]
>>> d
SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
>>> d.keys[:2]
[3, 2]
>>> d.keys[:2] = [1, 3]
Traceback (most recent call last):
KeyError: 'Keylist is not the same as current keylist.'
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.values
[2, 3, 4]
>>> d.values()
[2, 3, 4]
>>> d.setvalues((4, 3, 2))
>>> d
SequenceOrderedDict([(1, 4), (2, 3), (3, 2)])
>>> d.values[::-1]
[2, 3, 4]
>>> d.values[0]
4
>>> d.values[-2]
3
>>> del d.values[0]
Traceback (most recent call last):
TypeError: Can't delete items from values
>>> d.values[::2] = [2, 4]
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> 7 in d.values
0
>>> len(d.values)
3
>>> [val for val in d.values]
[2, 3, 4]
>>> d.values[-1] = 2
>>> d.values.count(2)
2
>>> d.values.index(2)
0
>>> d.values[-1] = 7
>>> d.values
[2, 3, 7]
>>> d.values.reverse()
>>> d.values
[7, 3, 2]
>>> d.values.sort()
>>> d.values
[2, 3, 7]
>>> d.values.append('anything')
Traceback (most recent call last):
TypeError: Can't append items to values
>>> d.values = (1, 2, 3)
>>> d
SequenceOrderedDict([(1, 1), (2, 2), (3, 3)])
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.items()
[(1, 2), (2, 3), (3, 4)]
>>> d.setitems([(3, 4), (2 ,3), (1, 2)])
>>> d
SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
>>> d.items[0]
(3, 4)
>>> d.items[:-1]
[(3, 4), (2, 3)]
>>> d.items[1] = (6, 3)
>>> d.items
[(3, 4), (6, 3), (1, 2)]
>>> d.items[1:2] = [(9, 9)]
>>> d
SequenceOrderedDict([(3, 4), (9, 9), (1, 2)])
>>> del d.items[1:2]
>>> d
SequenceOrderedDict([(3, 4), (1, 2)])
>>> (3, 4) in d.items
1
>>> (4, 3) in d.items
0
>>> len(d.items)
2
>>> [v for v in d.items]
[(3, 4), (1, 2)]
>>> d.items.count((3, 4))
1
>>> d.items.index((1, 2))
1
>>> d.items.index((2, 1))
Traceback (most recent call last):
ValueError: list.index(x): x not in list
>>> d.items.reverse()
>>> d.items
[(1, 2), (3, 4)]
>>> d.items.reverse()
>>> d.items.sort()
>>> d.items
[(1, 2), (3, 4)]
>>> d.items.append((5, 6))
>>> d.items
[(1, 2), (3, 4), (5, 6)]
>>> d.items.insert(0, (0, 0))
>>> d.items
[(0, 0), (1, 2), (3, 4), (5, 6)]
>>> d.items.insert(-1, (7, 8))
>>> d.items
[(0, 0), (1, 2), (3, 4), (7, 8), (5, 6)]
>>> d.items.pop()
(5, 6)
>>> d.items
[(0, 0), (1, 2), (3, 4), (7, 8)]
>>> d.items.remove((1, 2))
>>> d.items
[(0, 0), (3, 4), (7, 8)]
>>> d.items.extend([(1, 2), (5, 6)])
>>> d.items
[(0, 0), (3, 4), (7, 8), (1, 2), (5, 6)]
Backport from python2.7 to python <= 2.6.
Bases: dict
Bases: unittest.case.TestCase
Utilities for path manipulation. Not to be confused with the pathutils module from the pythonutils package (http://groups.google.com/group/pythonutils).
Compute the relative path of p1 with respect to p2.
File extension registry.
This provides routines for opening files based on extension, and registers the built-in file extensions.
Bases: object
Associate a file loader with an extension.
Note that there may be multiple loaders for the same extension.
Example:
registry = ExtensionRegistry()
# Add an association by setting an element
registry['.zip'] = unzip
# Multiple extensions for one loader
registry['.tgz'] = untar
registry['.tar.gz'] = untar
# Generic extensions to use after trying more specific extensions;
# these will be checked after the more specific extensions fail.
registry['.gz'] = gunzip
# Multiple loaders for one extension
registry['.cx'] = cx1
registry['.cx'] = cx2
registry['.cx'] = cx3
# Show registered extensions
print registry.extensions()
# Can also register a format name for explicit control from caller
registry['cx3'] = cx3
print registry.formats()
# Retrieve loaders for a file name
registry.lookup('hello.cx') -> [cx3,cx2,cx1]
# Run loader on a filename
registry.load('hello.cx') ->
try:
return cx3('hello.cx')
except:
try:
return cx2('hello.cx')
except:
return cx1('hello.cx')
# Load in a specific format ignoring extension
registry.load('hello.cx',format='cx3') ->
return cx3('hello.cx')
Return a sorted list of registered extensions.
Return a sorted list of the registered formats.
Call the loader for the file type of path.
Raises ValueError if no loader is available. Raises KeyError if format is not available. May raise a loader-defined exception if loader fails.
Return the loader associated with the file type of path.
Raises ValueError if file type is not known.
Uncertainty propagation class, and log() and exp() functions.
Based on scalars or numpy vectors, this class allows you to store and manipulate values+uncertainties, with propagation of gaussian error for addition, subtraction, multiplication, division, power, exp() and log().
Storage properties are determined by the numbers used to set the value and uncertainty. Be sure to use floating point uncertainty vectors for inplace operations since numpy does not do automatic type conversion. Normal operations can use mixed integer and floating point. In place operations (a *= b, etc.) create at most one extra copy for each operation. c = a*b by contrast uses four intermediate vectors, so shouldn’t be used for huge arrays.
remove redunduant elements from the give list and return a list of unique elements.
inputlist: input list hash: use this function to make the items in the list hashable.