def testgetSources(self): s1 = Source('t1') s2 = Source('t2') cmd = Command('tag', None, s1) cmd2 = Command('tag', None, s2=s2) cp = CommandPack([cmd, cmd2]) tset = set(cp.getSources()) self.failUnlessEqual(tset, set([s1, s2]))
def source_or_num(cls, obj): """ struc.source_or_num( obj ) -> result returns a source if obj is a SLIMpy scalar, or returns obj if obj is a number """ if isinstance(obj, Scalar): return Source(obj) elif hasattr(obj, 'container'): return Source(getattr(obj, 'container')) else: return obj
def setunusedsource( self, source ): """ if this command contains an un-instanciated target class then replace it with @parameter: Target returns True if un-instanciated Source is found and false if not """ # is_str = lambda par: isinstance( par, str) # sus = "${SLIMPY_UNUSEDSOURCE}" # is_str_src = lambda par: ( is_str(par) and sus in par) if not isinstance( source, Node ): source = Source( source ) for i,val in enumerate(self.params): if val == Source: self.params[i] = source return if Source in self._other_dependants: self._other_dependants.remove( Source ) self._other_dependants.add( source ) return for k,val in self.kparams.iteritems(): if val == Source: self.kparams[k] = source return raise Exception( 'No source to set' )
def test_get_source_cont(self): foo_src = Source('foo') bar_src = Source('bar') spam_tgt = Target('spam') c1 = Command('foo', None, foo_src, x=bar_src, src=spam_tgt) sc = c1.source_containers self.failUnlessEqual(len(sc), 2) self.failUnless('foo' in sc) self.failUnless('bar' in sc) self.failIf('spam' in sc)
def __mul__( self, other ): """ vec1.__mul__(vec2) <==> vec1 * vec2 vec2 may be a scalar or a vector """ if other is 0: return 0 if other is 1: return self if other is self: return self ** 2 par = other if isinstance( other, Vector ): if self.is_transp ^ other.is_transp: if self.is_transp: return inner_product( self, other ) else: return outer_product( self, other ) par = Source( other.getContainer() ) return self.generateNew( 'mul', vec=par ) else: par = self.source_or_num( par ) return self.generateNew( 'mul', par )
def __init__( self, inSpace, h=None, L=2, adj=False, **kparams ): """ Takes h, L as paramaters. h as source so it creates it. """ assert h is not None kparams.update( h=Source(h.getContainer()), L=L, adj=adj ) LinearOperatorStruct.__init__( self, inSpace, **kparams )
def cmplx(real, imag): """ cmplx( real, imag ) -> complex vector create a complex vector from real and imaginary parts """ struct = Structure() cmnd = "cmplx" return struct.generateNew(real, cmnd, Source, Source(imag.container))
def setUp(self): """ create commands """ self.sourceOf1 = Source(1) self.sourceOfBob = Source('bob') self.targetOf2 = Target(2) self.targetOfFoo = Target('foo') self.c1 = Command('testFunc', '__adder', 1, a=2) self.c2 = Command('testFunc', '__adder', 3, b=Source) self.c3 = Command('testFunc', '__adder', self.sourceOf1, self.sourceOfBob) self.c4 = Command('testFunc', '__adder', self.targetOfFoo, self.targetOf2) # for cmd in [self.c1,self.c2,self.c3,self.c4]: self.c1.func = testFunc
def __init__(self, wvec, inSpace): """ The param wvec must be able to be applied to another vector. Or is it is out of core, can be a string TODO if wvec is None, it is the Identity operator. TODO - error trap that if wvec is same type as other when applying. """ msg = "weightoper:Dont use this class!!! use DiagonalWeight" import warnings warnings.warn(msg, DeprecationWarning) self.wvec = wvec if is_vector(wvec): par = Source(wvec.container) LinearOperatorStruct.__init__(self, inSpace, vec=par) if isinstance(wvec, DataContainer): par = Source(wvec) LinearOperatorStruct.__init__(self, inSpace, vec=par) else: LinearOperatorStruct.__init__(self, inSpace, wvec)
def __init__(self, inSpace, filt=1, input=None, adj=0, **kparams): """ Takes filt and input as paramaters. Input as source so it creates it. """ kparams.update(filt=filt, adj=adj) if filt == 1: assert input is not None kparams.update(input=Source(input.getContainer())) LinearOperatorStruct.__init__(self, inSpace, **kparams)
def testgetSources(self): foo_src = Source('foo') bar_src = Source('bar') spam_src = Source('spam') spam_tgt = Target('eggs') c1 = Command('foo', None, foo_src, x=bar_src, ssc=spam_tgt) c1.add_other_dep(spam_src) sources = c1.getSources() self.failUnless(foo_src in sources) self.failUnless(bar_src in sources) self.failUnless(spam_src in sources) # print sources self.failIf(spam_tgt in sources) c1 = Command('foo', None) targets = c1.getSources() self.failUnlessEqual(targets, [])
def __rdiv__( self, other ): """ vec1.__rdiv__(vec2) <==> vec2 / vec1 vec2 may be a scalar or a vector """ par = other if isinstance( other, Vector ): par = Source( other.getContainer() ) return self.generateNew( 'rdiv', vec=par ) else: par = self.source_or_num( par ) return self.generateNew( 'rdiv', par )
def __init__(self, inSpace, mask): from slimpy_base.Core.User.Structures.serial_vector import Vector if isinstance(mask, str): from slimpy_base import vector mask = vector(mask) elif not isinstance(mask, Vector): raise TypeError( "argument 'mask' must be a 'Vector' or a 'str' instance got '%s'" % type(mask)) LinearOperatorStruct.__init__(self, inSpace, mask=Source(mask.container))
def thr( self, obj, mode='soft' ): """ Returns a thresholded vector. obj - may be a scalar or a vector. mode - may be 'soft', 'hard' or 'nng' """ par = obj if isinstance( obj, Vector ): par = Source( obj.getContainer() ) return self.generateNew( 'thr' , mode=mode, fthr=par ) else: par = self.source_or_num( par ) return self.generateNew( 'thr' , mode=mode, thr=par )
def __add__( self, other ): """ vec1.__add__(vec2) <==> vec1 + vec2 """ if other is 0: return self if other is self: return self *2 par = other if isinstance( other, Vector ): par = Source( other.getContainer() ) return self.generateNew( 'add', vec=par ) else: par = self.source_or_num( par ) return self.generateNew( 'add', par )
def __pow__( self, other ): """ vec1.__pow__(vec2) <==> vec1 ** vec2 vec2 may be a scalar or a vector """ if other is 1: return self par = other if isinstance( other, Vector ): par = Source( other.getContainer() ) return self.generateNew( 'pow', vec=par ) else: par = self.source_or_num( par ) return self.generateNew( 'pow', par )
def __init__(self, space, matrix, adj=False): from slimpy_base.Core.User.Structures.serial_vector import Vector if isinstance(matrix, str): from slimpy_base import vector matrix = vector(matrix) elif not isinstance(matrix, Vector): raise TypeError( "argument 'matrix' must be a 'Vector' or a 'str' instance got '%s'" % type(matrix)) self.inSpace = space kparams = dict(mat=Source(matrix.container), adj=adj) LinearOperatorStruct.__init__(self, self.inSpace, **kparams)
def __sub__( self, other ): """ self..__sub__(other) <==> self - other vec2 may be a scalar or a vector """ if other is 0: return self if other is self: return self.space.zeros() par = other if isinstance( other, Vector ): par = Source( other.getContainer() ) return self.generateNew( 'sub', vec=par ) else: par = self.source_or_num( par ) return self.generateNew( 'sub', par )
def test_get_target_cont(self): foo_tgt = Target('foo') bar_tgt = Target('bar') spam_src = Source('spam') c1 = Command('foo', None, foo_tgt, x=bar_tgt, src=spam_src) tc = c1.target_containers self.failUnlessEqual(len(tc), 2) self.failUnless('foo' in tc) self.failUnless('bar' in tc) self.failIf('spam' in tc)
def __div__( self, other ): """ vec1.__div__(vec2) <==> vec1 / vec2 vec2 may be a scalar or a vector """ if other is 1: return self if other is self: return self.space.ones() par = other if isinstance( other, Vector ): par = Source( other.getContainer() ) return self.generateNew( 'div', vec=par ) else: par = self.source_or_num( par ) return self.generateNew( 'div', par )
def cat(catlist, axis): """ Concatenate datasets catlist in the axis dinention. cat( axis, catlist ) -> vector """ struct = Structure() cat_array = array(catlist) if not cat_array.shape: return catlist elif cat_array.size == 1: return catlist.item() else: catlist = cat_array.ravel().tolist() cat1, catlist = catlist[0], catlist[1:] sourcelist = [Source(catitem.container) for catitem in catlist] cmnd = "cat" return struct.generateNew(cat1, cmnd, *sourcelist, **{'axis': axis})
def map(cls, source, command): """ map a SLIMpy command to a rsf command """ # the RSF bin and 'sf' will be automaticaly prepended to fft1 command = sfConverter.mpi_function(command, "create_meta", num_proc='all') meta_nX = source.space.meta.shape assert meta_nX is not None for i in range(3): command["N%s" % (i + 1)] = 1 for X, val in enumerate(meta_nX): meta_n = "N%s" % (X + 1) command[meta_n] = val numb_nX = source.space.meta['num_blocks'] command.pop_kw('num_blocks', None) for i in range(3): command["p%s" % (i + 1)] = 1 for X, val in enumerate(numb_nX): block = "p%s" % (X + 1) command[block] = val if 'eps' in source.space.meta: command['eps'] = source.space.meta['eps'] source_list = source.ravel().tolist() command.add_other_dep(Source) sources = [Source(srtuc.container) for srtuc in source_list] command.params.extend(sources) slen = len(source_list) for i, val in enumerate([slen, 1, 1]): command['n%s' % (i + 1)] = val command.runtime_map = windowm_mpi_runtime_map command = cls.truefalseHelper(command) return cls.pack(command, stdin=None)
def testsetSource(self): cp = CommandPack(['comm']) self.failUnlessEqual(cp.source, True) cp.source = 'tgt' self.failUnlessEqual(cp.source, 'tgt') cmd = Command('tag', None) cp = CommandPack([cmd], in_cont=None) self.failUnlessRaises(Exception, setattr, cp, 'source', 'src') cmd = Command('tag', None, Source) cp = CommandPack([cmd], in_cont=None) src = Source('tgt') cp.source = src self.failUnlessEqual(cp.source, None) self.failUnlessEqual(cmd.getSources(), [src])
def testsetunusedsource(self): foo_src = Source('foo') cmd = Command('foo', None, Source) cmd.setunusedsource(foo_src) sources = cmd.getSources() self.failUnlessEqual(sources, [foo_src]) self.failUnlessRaises(Exception, cmd.setunusedsource, foo_src) cmd = Command('foo', None, Target) self.failUnlessRaises(Exception, cmd.setunusedsource, foo_src) cmd.add_other_dep(foo_src) self.failUnlessEqual(sources, [foo_src])
def create_aug(self, other): already_exists = other.container.has_built_meta_info() cm = other.container.expand_meta() lst = [] push = lst.append for cont in cm: push(Vector(cont)) aug_vec = AugVector(lst) aug_vec.resize(self.kparams['num_blocks']) #IGNORE:E1101 aug_vec.meta = self.domain() aug_vec.meta['num_blocks'] = self.kparams['num_blocks'] aug_vec.meta_vector = other if already_exists: return aug_vec struct = Structure() command = struct.generate_command('meta', Source(other.container), **self.kparams) # command.add_other_dep( ) for cont in cm: command.add_other_dep(Target(cont)) converter = other.container.get_converter(command) commpack = converter.convert(other, command) commpack.target = None commpack.source = None self.env['graphmgr'].graphAppend(commpack) return aug_vec
def scalar_reduction(cls, container, tag, *args, **kargs): """ generate a Scalar from a command """ scalar_methods = container.scalar_methods scmd = scalar_methods.get_command(tag) scalar = Scalar() cont = Source(container) scal = Target(scalar) if cls.env['slimvars']['use_scalar_obj']: command = general_command(tag, None, cont, scal, *args, **kargs) # set command command.func = scmd compack = CommandPack([command], None, None) cls.env['graphmgr'].graphAppend(compack) else: scalar = scal.data return scalar
def testcopy(self): foo_tgt = Target('foo') bar_tgt = Target('bar') spam_src = Source('spam') c1 = Command('foo', None, foo_tgt, x=bar_tgt, src=spam_src) c2 = c1.copy() self.failUnlessEqual(c1, c2) self.failIfEqual(id(c1), id(c2)) func = lambda x: x c2.func = func self.failIfEqual(c1, c2) c1.func = func self.failUnlessEqual(c1, c2)
def gen_new_scal(self,other,func): scalar = Scalar( ) trg_scal = Target( scalar ) src_self = Source( self ) other_obj = self._num_src( other ) # new_func = scalar_helper(func,func_name=func_name) tag = str(func) command = general_command( tag , None, src_self,other_obj,trg_scal ) command.func = func if self.env['slimvars']['keep_tb_info']: stack = traceback.extract_stack( ) st_list = traceback.format_list( stack[:-3] ) command.tb_info = "".join(st_list) compack = CommandPack([command], None, None) self.env['graphmgr'].graphAppend( compack ) return scalar
def __init__(self, inSpace, mask, axis=2): self.axis = axis kparams = dict(mask=Source(mask.container)) LinearOperatorStruct.__init__(self, inSpace, **kparams)
def _num_src(self,num): if is_scalar_obj(num): return Source( num ) else: return Scalar( num )