def _process(self, source, *newtypes, **kwds): # {{{ if not source._typesKnown(): return if newtypes: if len(newtypes) > 1: assert len(newtypes) == len(source._slices), "Number of new slice types does not match number of slices" nslices = [ ops.CastOp(slice, rtypes.createType(newtype)) for slice, newtype in zip(source._slices, newtypes) ] else: nslices = [ops.CastOp(slice, rtypes.createType(newtypes[0])) for slice in source._slices] else: nslices = list(source._slices) for name, newtype in kwds.iteritems(): sels = [(pos, slice) for pos, slice in enumerate(source._slices) if slice.name == name] if isinstance(newtype, tuple): assert len(newtype) == len( sels ), "Number of new types for slice %s does not match number of slices (%d)" % (name, len(sels)) for (pos, slice), ntype in zip(sels, newtype): ntype = rtypes.createType(ntype) nslices[pos] = ops.CastOp(slice, ntype) else: for (pos, slice) in sels: rnewtype = rtypes.createType(newtype) nslices[pos] = ops.CastOp(slice, rnewtype) return self._initialize(tuple(nslices))
def _apply(cls, slices, eachfunc, dtype=rtypes.unknown, named_params=False, keep_name=False, per_slice=False, **kwargs): if(not isinstance(dtype,rtypes.TypeUnknown)): dtype = rtypes.createType(dtype,len(slices[0].dims)) if per_slice: return tuple([ops.EachOp((slice,), eachfunc, dtype, named_params, keep_name, kwargs) for slice in slices]) else: slices,plans = ops.broadcast(slices, mode='dim', partial=False) return (ops.EachOp(slices, eachfunc, dtype, named_params, keep_name, kwargs),)
def _apply( cls, slices, eachfunc, dtype=rtypes.unknown, named_params=False, keep_name=False, per_slice=False, **kwargs ): if not isinstance(dtype, rtypes.TypeUnknown): refdims = [] for slice in slices: refdims.extend(slice.dims) refdims.extend(slice.type.getAllDims()) dtype = rtypes.createType(dtype, len(slices[0].dims), refdims=refdims) if per_slice: return tuple([ops.EachOp((slice,), eachfunc, dtype, named_params, keep_name, kwargs) for slice in slices]) else: slices, plans = ops.broadcast(slices, mode="dim", partial=False) return (ops.EachOp(slices, eachfunc, dtype, named_params, keep_name, kwargs),)
def _apply(cls, slices, data, name=None, dtype=None, promotepath=False): if not dtype is None: refdims = sum([slice.dims for slice in slices], tuple()) dtype = rtypes.createType(dtype, refdims=refdims) if isinstance(data, representor.Representor): data = data() data = python.Rep(data, dtype=dtype, name=name) else: if not isinstance(data, representor.Representor): data = python.Rep(data, name=name) else: data = data / name nslices = list(slices) nslices.extend(data._slices) if not promotepath is False: nslices = repops_dim.Promote._apply(nslices, data._slices, promotepath) return nslices
def _process(self, source, name=None): if not source._typesKnown(): return #commonify dimensions cdimpath = dimpaths.commonDimPath([slice.dims for slice in source._slices]) nslices = [] for slice in source._slices: oslice = slice if(len(slice.dims) > len(cdimpath)): slice = ops.PackArrayOp(slice, ndim=len(slice.dims) - len(cdimpath)) nslices.append(slice) #cast to common type ntype = casts.castMultipleImplicitCommonType(*[slice.type for slice in nslices]) nnslices = [] for slice in nslices: if(ntype != slice.type): slice = ops.CastOp(slice,ntype) nnslices.append(slice) if name is None: dimname = util.seq_names(1, exclude=set([d.name for d in source.DimsUnique]))[0] name = 'names' else: dimname=name nslice = ops.HArrayOp(nnslices,dimname=dimname,slicename='values') nslice = ops.UnpackArrayOp(nslice) dim = nslice.dims[-1] rtype = rtypes.TypeArray(dims=dimpaths.DimPath(dim),subtypes=(rtypes.createType(str),)) oslice = ops.DataOp(source.Names, name=name, rtype=rtype) oslice = ops.UnpackArrayOp(oslice) #initialize object attributes return self._initialize((oslice,nslice))