[ HOME ]===== How to compute like a brain =====[ Programming ]

2D binary map

This module implement 2D bit array, where every element is one or zero and takes single bit. Based on bitarray. Still work in progress..

It is not complete yet, not fully tested (wrote several test in /test, but lazy for now the TM is more fun :).

There are many possibilities for optimizations.

In [1]:
import sys
sys.path.append('../lib')
import numpy as np
from bmap2D import *
In [2]:
bm = BMap2D(nrows=5, ncols=20)
bm
Out[2]:
0| 00000000000000000000
1| 00000000000000000000
2| 00000000000000000000
3| 00000000000000000000
4| 00000000000000000000
In [3]:
bm.rand_item
Out[3]:
bitarray('01111110110000010001')
In [4]:
bm.rand_bmap
Out[4]:
bitarray('0001001110010000000111101010100000011110011000101111001101001000100010001111110100110010010100111101')
In [5]:
bm.set(bm.rand_bmap)
bm
Out[5]:
0| 10001010010110110000
1| 10000010101100111110
2| 11001001000111100000
3| 11001010001100101101
4| 11101001000110111100
In [6]:
bm[1,:] = 0
bm
Out[6]:
0| 10001010010110110000
1| 00000000000000000000
2| 11001001000111100000
3| 11001010001100101101
4| 11101001000110111100
In [7]:
bm[:,2] = 1
bm
Out[7]:
0| 10101010010110110000
1| 00100000000000000000
2| 11101001000111100000
3| 11101010001100101101
4| 11101001000110111100
In [8]:
bm[:,[4,5,6,7,8]] = 1
bm
Out[8]:
0| 10101111110110110000
1| 00101111100000000000
2| 11101111100111100000
3| 11101111101100101101
4| 11101111100110111100
In [9]:
bm[:,4:8] = 0
bm
Out[9]:
0| 10100000010110110000
1| 00100000000000000000
2| 11100000000111100000
3| 11100000001100101101
4| 11100000000110111100
In [10]:
bm[1:40] = 0 #1D access
bm
Out[10]:
0| 10000000000000000000
1| 00000000000000000000
2| 11100000000111100000
3| 11100000001100101101
4| 11100000000110111100
In [11]:
bm.erase()
bm
Out[11]:
0| 00000000000000000000
1| 00000000000000000000
2| 00000000000000000000
3| 00000000000000000000
4| 00000000000000000000
In [12]:
bm[1:3,5:10] = 1
bm
Out[12]:
0| 00000000000000000000
1| 00000111111000000000
2| 00000111111000000000
3| 00000111111000000000
4| 00000000000000000000
In [13]:
bm[0,:] = bm.rand_item
bm
Out[13]:
0| 11011111010010100101
1| 00000111111000000000
2| 00000111111000000000
3| 00000111111000000000
4| 00000000000000000000
In [14]:
bm.rotate()
bm
Out[14]:
0| 10000
1| 10000
2| 00000
3| 10000
4| 10000
5| 11110
6| 11110
7| 11110
8| 01110
9| 11110
10| 01110
11| 00000
12| 10000
13| 00000
14| 10000
15| 00000
16| 00000
17| 10000
18| 00000
19| 10000
In [15]:
bm.rotate()
bm
Out[15]:
0| 11011111010010100101
1| 00000111111000000000
2| 00000111111000000000
3| 00000111111000000000
4| 00000000000000000000
In [16]:
bm >> 2
bm
Out[16]:
0| 00000000000000000000
1| 00000000000000000000
2| 11011111010010100101
3| 00000111111000000000
4| 00000111111000000000
In [17]:
bm.bmap = bm.bmap << 40
bm
Out[17]:
0| 11011111010010100101
1| 00000111111000000000
2| 00000111111000000000
3| 00000000000000000000
4| 00000000000000000000
In [18]:
bm[1,:] & bm.rand_item
Out[18]:
0| 00000110111000000000
In [19]:
bm[0,:] | bm[1,:]
Out[19]:
0| 11011111111010100101
In [22]:
bm.coords1D([(1,1),(5,5),(0,7)])
Out[22]:
[6, 30, 7]
In [21]:
bm.coords2D([5,30,42,55])
Out[21]:
[(1, 5), (6, 10), (8, 2), (11, 15)]
In [ ]: