def __init__( self ): """ Initialize the class, checks the inSpace and otuSpace and sets them to voidSpace if needed. """ # init Structure not necissary but to conserve consistancy Structure.__init__( self ) # self.isadj = False if self.command is None: raise TypeError( "need to define command in operator" ) if self.inSpace is None: raise TypeError( "need to define inSpace, use voidSpace if unknown" ) if self.outSpace is None: raise TypeError( "need to define outSpace, use voidSpace if unknown" ) if self.adj_kparams is None: self.adj_kparams = self.kparams if self.adj_params is None: self.adj_params = self.params if self.adj_command is None: self.adj_command = self.command LinearOperator.__init__( self, self.inSpace, self.outSpace , *self.params, **self.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 less_than(vec1, vec2, eps=0): """ less_than( vec1 , vec2 , eps=0 ) -> vec3 Test if two vectors elemnts are less than vec2 """ cmd = "lt" struct = Structure() right = struct.source_or_num(vec2) return struct.generateNew(vec1, cmd, eps=eps, right=right)
def and_(vec1, vec2, eps=0): """ and_( vec1 , vec2 , eps=0 ) -> vec3 Test if elements of either vec1 or vec2 are non-zero """ cmd = "and_" struct = Structure() right = struct.source_or_num(vec2) return struct.generateNew(vec1, cmd, eps=eps, right=right)
def greater_than_eq(vec1, vec2, eps=0): """ greater_than_eq( vec1 , vec2 , eps=0 ) -> vec3 Test if two vectors elemnts are greater than or equal to vec2 """ cmd = "ge" struct = Structure() right = struct.source_or_num(vec2) return struct.generateNew(vec1, cmd, eps=eps, right=right)
def not_equal(vec1, vec2, eps=0): """ not_equal( vec1 , vec2 , eps=0 ) -> vec3 Test if two vectors elemnts are not equal within eps precision """ cmd = "ne" struct = Structure() right = struct.source_or_num(vec2) return struct.generateNew(vec1, cmd, eps=eps, right=right)
def clnorm(A, mode="norm", angconstr=[0, 0]): """ clnorm( A ,mode="norm" , angconstr=[0,0] ) -> DiagonalWeight retruns a diagonal weight operator with domain and range in the vector space of the domain of A The weight coresponds to the norms of the coloums of A """ struct = Structure() cmnd = 'fdct2vects' if A.isadj: cInSpace = A.range() else: cInSpace = A.domain() # A[] angconstr angperwed = 360. / A.kparams['nba'] wedgconstr = [0, 0] for i in range(2): wedgconstr[i] = floor((90 - angconstr[i]) / angperwed) if mode == "zang" and not any(wedgconstr): return Identity(A.range()) command = struct.generate_command(cmnd, cInSpace=cInSpace, mode=mode, wedgconstr=wedgconstr, nbs=A.kparams['nbs'], nba=A.kparams['nba'], ac=A.kparams['ac']) # empty data container data_container = cInSpace.makeContaner(cmnd) converter = data_container.get_converter(command) commpack = converter.convert(None, command) commpack.target = data_container commpack.source = None struct.env['graphmgr'].graphAppend(commpack) from slimpy_base.Core.User.Structures.serial_vector import Vector wvec = Vector(data_container) return DiagonalWeight(wvec.space, wvec)
def testgenerate_command(self): struct = Structure() _env['slimvars']['keep_tb_info'] = False c1 = struct.generate_command('cmnd', a1=11) c2 = Command('cmnd', None, a1=11) self.failUnlessEqual(c1, c2) self.failUnlessEqual(c1.tb_info, None) _env['slimvars']['keep_tb_info'] = True c1 = struct.generate_command('cmnd', a1=11) self.failUnless(isinstance(c1.tb_info, str))
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 testCommand(self, cmd, *args, **kargs): """ returns the resulting space from applying the command but does not add the command to the graph @return new VectorSpace """ new_space = Structure.testCommand(self, cmd, *args, **kargs) assert new_space is not None if isinstance(new_space, MetaSpace): return new_space.copy() else: return self.__class__(new_space)
def testCommand(self, cmd_tag, *args, **kargs): """ returns the reulting space from applying the command but does not add the command to the graph """ """ returns the reulting space from applying the command but does not add the command to the graph """ new_space = Structure.testCommand(self, cmd_tag, *args, **kargs) assert new_space is not None return new_space
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 createmeta(self, other): if hasattr(other, 'meta_vector') and other.meta_vector is not None: return other.meta_vector struct = Structure() container = other.ravel()[0].container command = struct.generate_command('meta', **self.kparams) converter = container.get_converter(command) compack, meta_container = converter.apply_command(command, other) compack.source = container compack.target = meta_container struct.env['graphmgr'].graphAppend(compack) meta_container.contained = [ vec.container for vec in other.ravel().tolist() ] return Vector(meta_container)
def __init__(self, inspace, *params, **kparams): # init Structure not necissary but to conserve consistancy Structure.__init__(self) outspace = inspace.testCommand(self.name, *params, **kparams) LinearOperator.__init__(self, inspace, outspace, *params, **kparams)