Exemple #1
0
 def encode(self, word):
     val = BMap1D()
     val.encode(WordEncoder.cmap, word.lower())
     rv = BMap1D(self.nbits)
     length = len(val) if len(val) < self.nbits else self.nbits
     rv[0:length] = val[0:length]  #keep correct size
     return rv
Exemple #2
0
	def encode(self, value):
		i = int( math.floor(self.buckets * ((value - self.vmin)/float(self.vrange)) ) )
		rv = BMap1D(self.nbits)
		rv.setall(0)
		end = i + self.width
		rv[i : end] = 1
		return rv
Exemple #3
0
    def get_byidxs(self, idxs):
        #print ">>> %s" % (idxs,)
        #In case 2D indecies are specified
        if isinstance(idxs, tuple):
            rows, cols = idxs

            if BMap2D.is_empty_slice(rows):  #cols only case
                return self.get_cols(BMap2D.slice2range(cols, self.ncols))
            else:
                rows = self.slice2range(rows, self.nrows)

            if BMap2D.is_empty_slice(cols):  #rows only case
                return self.get_rows(BMap2D.slice2range(rows, self.nrows))
            else:
                cols = self.slice2range(cols, self.ncols)

            #they are no longer slices but ranges
            bm = self.get_rows(rows)
            rv = bm.get_cols(cols)
            rv.rotate()
            return rv

        else:  #1D index

            if isinstance(idxs, (list, np.ndarray)):
                rv = []
                for i in idxs:
                    rv.append(self.bmap[i])
                return rv
            else:
                return BMap1D(self.bmap[idxs])
Exemple #4
0
 def rotate(self, deg=90):
     tmp = BMap1D(self.len)
     for r in xrange(self.ncols):
         pos = r * self.nrows
         tmp[pos:pos + self.nrows] = self.bmap[r:self.len:self.ncols]
     self.bmap = tmp
     cols = self.ncols
     self.ncols = self.nrows
     self.nrows = cols
Exemple #5
0
 def encode_bucket(self, value):
     if value < self.vmin or value > self.vmax:
         warnings.warn("Value '%s' outside of range : [%s <=> %s]" %
                       (value, self.vmin, self.vmax))
     #corner case for vmax
     i = self.buckets - 1 if value == self.vmax else self.pos(value)
     rv = BMap1D(self.buckets)
     rv[i] = 1
     return rv
Exemple #6
0
    def init_cmap(cls, nbits=26):
        log.debug('Building charachter map : %sbit code ...' % nbits)
        if nbits > 26:
            cls.ce = CategoryEncoder(nbits=nbits, ncats=26)

        for i, c in enumerate(string.ascii_lowercase):
            if nbits == 5:
                cls.cmap[c] = BMap1D(format(
                    i + 1, 'b').zfill(nbits))  #5bits for 26 chars
                continue
            if nbits == 26:  #26 bits for 26 chars
                bmap = BMap1D(nbits)
                bmap[i:] = 1
                cls.cmap[c] = bmap
                continue
            if nbits > 26:
                cls.cmap[c] = cls.ce.encode(i + 1)
                continue
Exemple #7
0
 def encode(self, value):
     if value < self.vmin or value > self.vmax:
         warnings.warn("Value '%s' outside of range : [%s <=> %s]" %
                       (value, self.vmin, self.vmax))
     i = self.pos(value)
     rv = BMap1D(self.nbits)
     #rv.setall(0)
     end = i + self.width
     rv[i:end] = 1
     return rv
Exemple #8
0
	def __init__(self, nrows, nbits=None, val=None, randomize=0, ncols=None):
		self.nrows = nrows
		self.ncols = ncols if ncols else nbits
		assert self.ncols , "When creating BMap2D, please provide ncols|nbits"
		self.len = self.nrows * self.ncols
		self.ext = 'bmap'
		self.bmap = BMap1D(self.len)
		self.index = 0
		self.erase()
		if randomize :#should we generate random noise
			many = int(self.len * randomize)
			idxs = np.random.randint(0,self.len,many)
			for i in np.nditer(idxs) : self.bmap[i] = 1

		self.set(val)
Exemple #9
0
 def rand_bmap(self):
     return BMap1D(list(np.random.randint(0, 2, (self.nrows * self.ncols))))
Exemple #10
0
 def rand_item(self):
     return BMap1D(list(np.random.randint(0, 2, self.ncols)))
Exemple #11
0
 def mk_item(self):
     val = BMap1D(self.ncols)
     val.setall(0)
     return val
Exemple #12
0
def idxs2bits(idxs, nbits):
    bit_ary = BMap1D(nbits)
    bit_ary.setall(0)
    for i in idxs:
        bit_ary[i] = 1
    return bit_ary
Exemple #13
0
def np2bits(ary):
    b = BMap1D()
    b.pack(ary.astype(np.bool).tostring())
    return b
Exemple #14
0
	def add_rows(self, nrows):
		bits = nrows * self.ncols
		ba = BMap1D(bits)
		self.bmap.extend(ba)
		self.nrows += nrows