Example #1
0
 def immediate(self):
     value = super(ADC, self).immediate()
     result = value + self.cpu.acc + self.cpu.ps['carry_flag']
     self.cpu.ps['carry_flag'] = result > 0xff
     self.cpu.ps['overflow_flag'] = (
         (value >> 7) == (self.cpu.acc >> 7)) != (np.ubyte(result) >> 7)
     self.cpu.acc = np.ubyte(result)
Example #2
0
 def test_cpu_write_word_ok(self, setup_cpu, address, value):
     pc_start = setup_cpu.pc
     setup_cpu.write_word(address, value)
     assert setup_cpu.memory[address] == np.ubyte(value)
     assert setup_cpu.memory[address + 1] == np.ubyte(value >> 8)
     assert setup_cpu.clock.total_clock_cycles == 2
     assert setup_cpu.pc == pc_start
Example #3
0
    def decode(self) -> str:
        output_bits = list()
        for i in range(self.im.size[0]):
            for j in range(self.im.size[1]):
                # outcode
                c1, c2, c3 = self.px[i, j]
                output_bits.append(
                    int(
                        round(
                            mean((round(mean(unpackbits(ubyte(c1))[-3:])),
                                  round(mean(unpackbits(ubyte(c2))[-3:])),
                                  round(mean(unpackbits(ubyte(c3))[-3:])))))))
                if output_bits.__len__() == self.bitlength:
                    break
            else:
                continue
            break

        output_bits = output_bits[::-1]

        output_bytes = bytearray()
        byte = list()
        for b in output_bits:
            if byte.__len__() == 8:
                output_bytes.append(packbits(byte)[0])
                byte = list()
            byte.append(b)
        output_bytes.append(packbits(byte)[0])
        return output_bytes.decode(self.encoding)
Example #4
0
def int2TwosC(value):
    """2 byte twos compliment representation of value given"""
    ##    b1, b0 = divmod(value, 1<<8)
    ##    if b1<0 :
    ##        b1 += 1<<8
    b1, b0 = divmod(np.int16(value), 1 << 8)
    return (np.ubyte(b1), np.ubyte(b0))
    def _generate_round_keys(self, private_key: np.array) -> list:
        working_array = np.zeros((4, 44), dtype=np.ubyte)

        round_constants = [
            None,
            int('1', 16),
            int('2', 16),
            int('4', 16),
            int('8', 16),
            int('10', 16),
            int('20', 16),
            int('40', 16),
            int('80', 16),
            int('1b', 16),
            int('36', 16)
        ]

        for i in range(4):
            for j in range(4):
                working_array[i][j] = int(private_key[i][j], 16)

        for key_number in range(1, 11):
            for column_number in range(1, 5):
                if column_number == 1:
                    tmp_column = working_array[:, 4 * key_number +
                                               column_number - 2]
                    tmp_column = [
                        tmp_column[1], tmp_column[2], tmp_column[3],
                        tmp_column[0]
                    ]
                    tmp_column = [hex(item)[2:] for item in tmp_column]
                    tmp_column = [
                        self.sbox.sbox_substitution(item)
                        for item in tmp_column
                    ]
                    tmp_column = np.array(
                        [int(item, 16) for item in tmp_column], dtype=np.ubyte)

                    previous_column = working_array[:, 4 * key_number +
                                                    column_number - 5]
                    tmp_column = previous_column ^ tmp_column ^ np.array([
                        np.ubyte(round_constants[key_number]),
                        np.ubyte(0),
                        np.ubyte(0),
                        np.ubyte(0)
                    ])
                    for i in range(4):
                        working_array[i][4 * key_number + column_number -
                                         1] = tmp_column[i]
                else:
                    tmp_column = working_array[:, 4 * key_number +
                                               column_number - 2]
                    previous_column = working_array[:, 4 * key_number +
                                                    column_number - 5]
                    tmp_column = previous_column ^ tmp_column
                    for i in range(4):
                        working_array[i][4 * key_number + column_number -
                                         1] = tmp_column[i]

        return np.hsplit(working_array, 11)
Example #6
0
def hub75_decompose(data):
    pixels = np.frombuffer(data,
                           dtype=[('r', 'B'), ('g', 'B'), ('b', 'B'),
                                  ('a', 'B')])

    channels = {
        'D': ('r', 0),
        'LAT': ('r', 1),
        'A': ('r', 2),
        'B2': ('r', 3),
        'E': ('r', 4),
        'B': ('r', 6),
        'C': ('r', 7),
        'R2': ('g', 0),
        'G1': ('g', 1),
        'G2': ('g', 4),
        'CLK': ('g', 5),
        'OE': ('b', 0),
        'R1': ('b', 1),
        'B1': ('b', 2)
    }

    output = {}

    for name, source in channels.items():
        channel = np.bitwise_and(pixels[source[0]], 1 << source[1])
        channel = np.where(channel > 0, np.ubyte(ord('1')), np.ubyte(ord('_')))

        output[name] = channel.tobytes().decode('utf-8')

    return output
Example #7
0
    def encode(self, input_text: str) -> Image:
        input_bytes = bytearray(input_text.encode(self.encoding))
        input_bits = list()
        for q in input_bytes:
            for z in unpackbits(ubyte(q)):
                input_bits.append(z)
        if self.im.size[0] * self.im.size[1] < input_bits.__len__() + ceil(
                input_bits.__len__() / 255):
            raise OutOfSpace('Image is not large enough for a bitwise encode')
        # encode LSB
        self.bitlength = input_bits.__len__()

        for i in range(self.im.size[0]):
            for j in range(self.im.size[1]):
                c1, c2, c3 = self.px[i, j]
                # encode here
                b = input_bits.pop()
                c1 = packbits(list(unpackbits(ubyte(c1))[:-3]) + [b, b, b])[0]
                c2 = packbits(list(unpackbits(ubyte(c2))[:-3]) + [b, b, b])[0]
                c3 = packbits(list(unpackbits(ubyte(c3))[:-3]) + [b, b, b])[0]
                self.px[i, j] = (c1, c2, c3)
                if input_bits.__len__() == 0:
                    break
            else:
                continue
            break
        return True
Example #8
0
 def indirect_indexed(self):
     address = super(ADC, self).indirect_indexed()
     value = int(self.cpu.read_byte(address), base=0)
     result = value + self.cpu.acc + self.cpu.ps['carry_flag']
     self.cpu.ps['carry_flag'] = result > 0xff
     self.cpu.ps['overflow_flag'] = (
         (value >> 7) == (self.cpu.acc >> 7)) != (np.ubyte(result) >> 7)
     self.cpu.acc = np.ubyte(result)
Example #9
0
 def indexed_indirect(self):
     address = super(SBC, self).indexed_indirect()
     value = int(self.cpu.read_byte(address), base=0)
     result = self.cpu.acc - value - (1 - self.cpu.ps['carry_flag'])
     self.cpu.ps['carry_flag'] = result > 0xff
     self.cpu.ps['overflow_flag'] = (
         (value >> 7) == (self.cpu.acc >> 7)) != (np.ubyte(result) >> 7)
     self.cpu.acc = np.ubyte(result)
Example #10
0
def _list2bits(arg):
    # return a numpy.ubyte with bits set based on integers 0..7 in arg
    if type(arg) == int and 0 <= arg < 8:
        return ubyte(pow2[arg])
    elif hasattr(arg, '__iter__'):
        return ubyte(sum([pow2[btn] for btn in arg]))
    else:  # None
        return ubyte(0)
Example #11
0
def _list2bits(arg):
    # return a numpy.ubyte with bits set based on integers 0..7 in arg
    if type(arg) == int and 0 <= arg < 8:
        return ubyte(pow2[arg])
    elif hasattr(arg, '__iter__'):
        return ubyte(sum([pow2[btn] for btn in arg]))
    else:  # None
        return ubyte(0)
Example #12
0
 def test_cpu_pop_word_from_stack_ok(self, setup_cpu, value, sp):
     pc_start = setup_cpu.pc
     setup_cpu.sp = sp
     setup_cpu.memory[setup_cpu.sp + 0x0102] = np.ubyte(value)
     setup_cpu.memory[setup_cpu.sp + 0x0101] = np.ubyte(value >> 8)
     assert hex(value) == setup_cpu.pull_word_from_stack()
     assert setup_cpu.sp == sp + 2
     assert setup_cpu.clock.total_clock_cycles == 4
     assert setup_cpu.pc == pc_start
Example #13
0
def crc8(byte, crc):
    for i in range(8):
        first_bit = crc & bit_mask
        crc = crc << np.ubyte(1)
        if np.ubyte(byte & (bit_mask >> i)):
            crc = crc | np.ubyte(1)
        if first_bit:
            crc = crc ^ crc_poly
    return crc
Example #14
0
 def test_cpu_push_word_on_stack_ok(self, setup_cpu, value, sp):
     pc_start = setup_cpu.pc
     setup_cpu.sp = sp
     setup_cpu.push_word_on_stack(value)
     assert setup_cpu.memory[setup_cpu.sp + 0x0102] == np.ubyte(value >> 8)
     assert setup_cpu.memory[setup_cpu.sp + 0x0101] == np.ubyte(value)
     assert setup_cpu.sp == sp - 2
     assert setup_cpu.clock.total_clock_cycles == 3
     assert setup_cpu.pc == pc_start
Example #15
0
File: utils.py Project: afcarl/f2py
def get_char_bit():
    import numpy
    one = numpy.ubyte(1)
    two = numpy.ubyte(2)
    n = numpy.ubyte(2)
    i = 1
    while n >= two:
        n <<= one
        i += 1
    return i
Example #16
0
def get_char_bit():
    import numpy
    one = numpy.ubyte(1)
    two = numpy.ubyte(2)
    n = numpy.ubyte(2)
    i = 1
    while n>=two:
        n <<= one
        i += 1
    return i
Example #17
0
 def test_lda_indexed_indirect(self, setup_cpu, value, zero_flag, neg_flag):
     setup_cpu.idx = 0x5a
     setup_cpu.memory[0x0200] = 0xa1  # LDA instruction
     setup_cpu.memory[0x201] = 0xb8
     setup_cpu.memory[np.ubyte(0xb8 + setup_cpu.idx)] = 0x88
     setup_cpu.memory[np.ubyte(0xb8 + setup_cpu.idx) + 1] = 0x7a
     setup_cpu.memory[0x7a88] = value
     setup_cpu.execute(1)
     assert setup_cpu.acc == value
     assert setup_cpu.clock.total_clock_cycles == 6
     assert setup_cpu.ps['negative_flag'] == neg_flag
     assert setup_cpu.ps['zero_flag'] == zero_flag
    def data2code(self, data):
        '''
        converts data from its value to a code (byte)
        data is a numpy array
        '''
        data[data < -self.offset] = -self.offset / self.scale
        #        # No zero vel or sw
        #        if self.units == 'm/s':
        #            data[data == 0]  = -self.offset/self.scale

        data = np.ubyte(data * self.scale + self.offset)
        data[data < 2] = np.ubyte(0)  # No range folded data
        data[data > 255] = np.ubyte(0)  # No overflow
        return data
Example #19
0
 def accumulator(self):
     carry_flag_value = self.cpu.ps['carry_flag']
     self.cpu.ps['carry_flag'] = self.cpu.acc % 2
     self.cpu.acc = np.ubyte((self.cpu.acc >> 1) + (carry_flag_value << 7))
     ~self.cpu.clock
     self.cpu.ps['zero_flag'] = self.cpu.acc == 0
     self.cpu.ps['negative_flag'] = (self.cpu.acc >> 7) == 1
Example #20
0
def dcoupe(img_gray,n):
    window_size = (n,n)
    img_size=img_gray.shape
    new_gray=np.zeros(img_size)
    quant=np.zeros(img_size)
    dctblock=np.zeros(window_size)
    newim=np.zeros(window_size)
    t1=np.zeros((8,8))
    t2=np.zeros((8,8))
# get the largest dimension
    max_dim = max(img_size)
    # Crop out the window and calculate
    for r in range(0,img_size[0]-n+1, n):# X-(n-1)
        for c in range(0,img_size[1]-n+1, n):
            window = img_gray[r:r+n,c:c+n]#!!!!!   i:i+n
#            dctblock=np.zeros(window_size)
            dctblock=dct2(window)

	    t1=quantification(dctblock, 5)
	    t2=DEquantification(t1, 5)
#            newim=np.zeros(window_size)
            newim=idct2(t2)#quant[r:r+n,c:c+n]
            new_gray[r:r+n,c:c+n]=newim
#    quantIm=pil.Image.fromarray(np.ubyte(np.round(255.0*quant,0)))
#pour sauver l'image en format jpeg pour une qualité voulue
#    quantIm.save('quantif.jpeg',quality=20)
    monIm=pil.Image.fromarray(np.ubyte(np.round(new_gray,0)))
#    fich=open('madct.dat','wb')
#    fich.write(np.reshape(quantIm,-1)) 
#on étend le tableau en 1D pour pouvoir enregistrer chaque octet
#    fich.close()
    return monIm
def resize(src, dstsize):
    if src.ndim == 3:
        dstsize.append(3)
    dst = np.array(np.zeros(dstsize), src.dtype)
    factory = float(np.size(src, 0)) / dstsize[0]
    factorx = float(np.size(src, 1)) / dstsize[1]
    print 'factory', factory, 'factorx', factorx
    srcheight = np.size(src, 0)
    srcwidth = np.size(src, 1)
    print 'srcwidth', srcwidth, 'srcheight', srcheight
    for i in range(dstsize[0]):
        for j in range(dstsize[1]):
            y = float(i) * factory
            x = float(j) * factorx
            if y + 1 > srcheight:
                y -= 1
            if x + 1 > srcwidth:
                x -= 1
            cy = np.ceil(y)
            fy = cy - 1
            cx = np.ceil(x)
            fx = cx - 1
            w1 = (cx - x) * (cy - y)
            w2 = (x - fx) * (cy - y)
            w3 = (cx - x) * (y - fy)
            w4 = (x - fx) * (y - fy)
            if (x - np.floor(x) > 1e-6 or y - np.floor(y) > 1e-6):
                t = src[fy, fx] * w1 + src[fy, cx] * w2 + src[
                    cy, fx] * w3 + src[cy, cx] * w4
                t = np.ubyte(np.floor(t))
                dst[i, j] = t

            else:
                dst[i, j] = (src[y, x])
    return dst
Example #22
0
def write_header( weights, f ):
    """
    Writes header to the file. File must be open
    """
    
    # write version header
    f.write( np.ubyte( 0 ).tobytes() )
    # write reserved field
    f.write( np.ubyte( 0 ).tobytes() )
    # write the number of dimensions
    f.write( np.uint32( len( weights.shape ) ).tobytes() )
    # write the dimension info
    for d in weights.shape:
        f.write( np.uint32( d ).tobytes() )
    # write datatype info (currently it is only float32
    f.write( np.ubyte( 1 ).tobytes() )
Example #23
0
def test_segments():
    with pynrn.Context():
        # Test .segments and .nodes iterators
        s1 = pynrn.Section()
        assert s1.nseg == 1
        assert [s.x for s in s1.segments] == [0.5]
        assert [s.x for s in s1.nodes] == [0, 1]
        
        s1.nseg = 2
        assert s1.nseg == 2
        assert [s.x for s in s1.segments] == [0.25, 0.75]
        assert [s.x for s in s1.nodes] == [0, 0.5, 1]
        
        s1.nseg = 3
        assert s1.nseg == 3
        assert [s.x for s in s1.segments] == [1./6., 3./6., 5./6.]
        assert [s.x for s in s1.nodes] == [0, 1./3., 2./3., 1]
        
        # test x type does not cause segment cache miss
        assert s1(0) is s1(0.0)
        assert s1(0) is s1(np.float(0))
        assert s1(0) is s1(np.ubyte(0))

        # test non-numeric locations 
        for x in ['x', s1, np.array([1,2,3])]:
            with pytest.raises(TypeError):
                s1(x)
Example #24
0
def yuv_to_rgb(y_dec, u_dec, v_dec):
    r = clip(y_dec + 1.402 * (v_dec - 128), 0, 255)
    g = clip(y_dec - 0.34414 * (u_dec - 128) - 0.71414 * (v_dec - 128), 0, 255)
    b = clip(y_dec + 1.772 * (u_dec - 128), 0, 255)
    rgb = ubyte(dstack((r, g, b)))
    imsave('rgb_parrot.jpg', rgb)
    return rgb
Example #25
0
def imslant(img):
    aa = np.transpose(img)
    dimx = aa.shape[0]
    dimy = aa.shape[1]
    dimxh = aa.shape[0] / 2
    dimyh = aa.shape[1] / 2
    ii = np.where(aa > 0)
    cc = len(ii[0])
    xa = np.mean(ii[0])
    ya = np.mean(ii[1])
    slopenum = np.sum(ii[1] * (ii[0] - xa))
    slopeden = np.sum(ii[1] * (ii[1] - ya))
    slope = slopenum / slopeden
    #print slope, xa, ya
    ii = np.outer(range(28), np.ones(28))
    jj = np.outer(np.ones(28), range(28))
    fx = ii - xa + (jj - ya) * slope + dimxh
    fy = jj - ya + dimyh
    x = np.int16(fx)
    y = np.int16(fy)
    a = fx - x
    b = fy - y
    xx1 = x + 1
    yy1 = y + 1
    timg = np.zeros((2 * dimx + img.shape[0], 2 * dimy + img.shape[1]))
    timg[dimx:(dimx + img.shape[0]), dimy:(dimy + img.shape[1])] = aa
    imout = a * b * timg[xx1 + dimx, yy1 + dimy] + a * (1. - b) * timg[
        xx1 + dimx, y + dimy] + (1. - a) * b * timg[x + dimx, yy1 + dimy] + (
            1. - a) * (1. - b) * timg[x + dimx, y + dimy]
    #imout=timg[x+dimxh,y+dimyh]

    return (np.ubyte(np.transpose(imout)))
Example #26
0
def calc_crc8():
    crc = np.ubyte(0)
    for i in b:
        crc = crc8(i, crc)
    crc = crc8(0b00000000, crc)
    print('Контрольнаяя сумма')
    print(hex(crc))
Example #27
0
def pre_process_video(folder, social):
    
     # Load Video
     aviFiles = glob.glob(folder+'/*.avi')
     aviFile = aviFiles[0]
     vid = cv2.VideoCapture(aviFile)
     numFrames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
   
     # Read First Frame
     ret, im = vid.read()
     previous = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
     width = np.size(previous, 1)
     height = np.size(previous, 0)
   
     # Alloctae Image Space
     stepFrames = 250 # Add a background frame every 2.5 seconds for 50 seconds
     bFrames = 50
     thresholdValue=10
     accumulated_diff = np.zeros((height, width), dtype = float)
     backgroundStack = np.zeros((height, width, bFrames), dtype = float)
     background = np.zeros((height, width), dtype = float)
     bCount = 0
     for i, f in enumerate(range(0, numFrames, stepFrames)):
       
         vid.set(cv2.CAP_PROP_POS_FRAMES, f)
         ret, im = vid.read()
         current = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
         absDiff = cv2.absdiff(previous, current)
         level, threshold = cv2.threshold(absDiff,thresholdValue,255,cv2.THRESH_TOZERO)
         previous = current
      
         # Accumulate differences
         accumulated_diff = accumulated_diff + threshold
       
         # Add to background stack
         if(bCount < bFrames):
             backgroundStack[:,:,bCount] = current
             bCount = bCount + 1
       
         print (numFrames-f)
         print (bCount)

     vid.release()  
    # Normalize accumulated difference image
     accumulated_diff = accumulated_diff/np.max(accumulated_diff)
     accumulated_diff = np.ubyte(accumulated_diff*255)
    
    # Enhance Contrast (Histogram Equalize)
     equ = cv2.equalizeHist(accumulated_diff)

     # Compute Background Frame (median or mode)
     background = np.median(backgroundStack, axis = 2)

     saveFolder = folder
     imageio.imwrite(saveFolder + r'/difference.png', equ)    
     cv2.imwrite(saveFolder + r'/background.png', background)
     # Using SciPy to save caused a weird rescaling when the images were dim.
     # This will change not only the background in the beginning but the threshold estimate

     return 0
Example #28
0
 def add_light_source(self, x, y, intensity=ubyte(255)):
     """
     Apply light algorythm to given position
     x, y - position
     intensity - light value for source block
     """
     self._light_block(x, y, intensity=intensity)
Example #29
0
 def test_numpy(self):
     """NumPy objects get serialized to readable JSON."""
     l = [
         np.float32(12.5),
         np.float64(2.0),
         np.float16(0.5),
         np.bool(True),
         np.bool(False),
         np.bool_(True),
         np.unicode_("hello"),
         np.byte(12),
         np.short(12),
         np.intc(-13),
         np.int_(0),
         np.longlong(100),
         np.intp(7),
         np.ubyte(12),
         np.ushort(12),
         np.uintc(13),
         np.ulonglong(100),
         np.uintp(7),
         np.int8(1),
         np.int16(3),
         np.int32(4),
         np.int64(5),
         np.uint8(1),
         np.uint16(3),
         np.uint32(4),
         np.uint64(5),
     ]
     l2 = [l, np.array([1, 2, 3])]
     roundtripped = loads(dumps(l2, cls=EliotJSONEncoder))
     self.assertEqual([l, [1, 2, 3]], roundtripped)
Example #30
0
 def test_zero_page_y(self, setup_cpu, zp_address, idy, address):
     setup_cpu.pc = address
     setup_cpu.idy = idy
     setup_cpu.memory[address] = zp_address
     addressing = cpu6502.instructions.AbstractInstruction(setup_cpu)
     assert addressing.zero_page_y() == np.ubyte(zp_address + idy)
     assert setup_cpu.clock.total_clock_cycles == 2
Example #31
0
def essentials_skeletonize():
    image = skimage.data.imread('holsteiner-stute.png', as_gray=True) > 0
    image_inv = skimage.util.invert(image)

    distance = scipy.ndimage.distance_transform_edt(image_inv)
    distance = skimage.util.invert(distance.astype(np.uint8))
    distance_border = skimage.measure.find_contours(image, 0.5)

    bg: np.ndarray = skimage.img_as_ubyte(distance)
    bg_min, bg_max = np.amin(bg), np.amax(bg)
    bg_mid = np.ubyte((int(bg_min) + int(bg_max)) / 2)

    border: np.ndarray = np.zeros(bg.shape, dtype=np.bool_)
    for b in distance_border:
        for (x0, y0), (x1, y1) in pairwise(b):
            rr, cc = skimage.draw.line(int(x0), int(y0), int(x1), int(y1))
            border[rr, cc] = True

    def finish(name: str, skeleton: np.ndarray):
        skeleton = skimage.morphology.binary_dilation(skeleton)
        skeleton_border = skimage.morphology.binary_dilation(skeleton)

        out = bg.copy()
        out[skeleton_border & image_inv] = bg_min
        out[skeleton & image_inv] = 255
        out[border] = bg_mid

        write_image(name, out)

    finish('essentials_skeletonize_medial_axis.png',
           skimage.morphology.medial_axis(image_inv))
    finish('essentials_skeletonize_thinning.png',
           skimage.morphology.skeletonize(image_inv))
Example #32
0
def imslant(img):
    aa=np.transpose(img)
    dimx=aa.shape[0]
    dimy=aa.shape[1]
    dimxh=aa.shape[0]/2
    dimyh=aa.shape[1]/2
    ii=np.where(aa>0)
    cc=len(ii[0])
    xa=np.mean(ii[0])
    ya=np.mean(ii[1])
    slopenum=np.sum(ii[1]*(ii[0]-xa))
    slopeden=np.sum(ii[1]*(ii[1]-ya))
    slope=slopenum/slopeden
    #print slope, xa, ya
    ii=np.outer(range(28),np.ones(28))
    jj=np.outer(np.ones(28),range(28))
    fx=ii-xa+(jj-ya)*slope+dimxh
    fy=jj-ya+dimyh
    x=np.int16(fx)
    y=np.int16(fy)
    a=fx-x
    b=fy-y
    xx1=x+1
    yy1=y+1
    timg=np.zeros((2*dimx+img.shape[0],2*dimy+img.shape[1]))
    timg[dimx:(dimx+img.shape[0]),dimy:(dimy+img.shape[1])]=aa
    imout=a*b*timg[xx1+dimx,yy1+dimy]+a*(1.-b)*timg[xx1+dimx,y+dimy]+(1.-a)*b*timg[x+dimx,yy1+dimy]+(1.-a)*(1.-b)*timg[x+dimx,y+dimy]
    #imout=timg[x+dimxh,y+dimyh]
    
    return(np.ubyte(np.transpose(imout)))
Example #33
0
 def test_numpy(self):
     """NumPy objects get serialized to readable JSON."""
     l = [
         np.float32(12.5),
         np.float64(2.0),
         np.float16(0.5),
         np.bool(True),
         np.bool(False),
         np.bool_(True),
         np.unicode_("hello"),
         np.byte(12),
         np.short(12),
         np.intc(-13),
         np.int_(0),
         np.longlong(100),
         np.intp(7),
         np.ubyte(12),
         np.ushort(12),
         np.uintc(13),
         np.ulonglong(100),
         np.uintp(7),
         np.int8(1),
         np.int16(3),
         np.int32(4),
         np.int64(5),
         np.uint8(1),
         np.uint16(3),
         np.uint32(4),
         np.uint64(5),
     ]
     l2 = [l, np.array([1, 2, 3])]
     roundtripped = loads(dumps(l2, cls=EliotJSONEncoder))
     self.assertEqual([l, [1, 2, 3]], roundtripped)
Example #34
0
def findObstacle(depth):
    start = time.clock()

    ret, depth = cv2.threshold(depth, depth_range[0], 255, cv2.THRESH_TOZERO)
    ret, depth = cv2.threshold(depth, depth_range[1], 255,
                               cv2.THRESH_TOZERO_INV)
    depth = np.ubyte(depth)

    # Get a 15x15 kernel filled with 1
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))
    # Open operation, separate depth, suppress insignificant blocks
    opening = cv2.morphologyEx(depth, cv2.MORPH_OPEN, kernel)

    # Search contours
    ret, binary_depth = cv2.threshold(depth, depth_thresh, 255,
                                      cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(binary_depth, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)

    # Find convex hull
    hulls = contoursOut = []
    for i in range(len(contours)):
        if cv2.contourArea(contours[i]) >= area_thresh:
            hulls.append(cv2.convexHull(contours[i], False))
            contoursOut.append(contours[i])

    return (depth, binary_depth, hulls, contours, hierarchy)
Example #35
0
def test_sequential_runner_not_matching_answer2():
    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
            int i = blockIdx.x * block_size_x + threadIdx.x;
            if (i<n) {
                c[i] = a[i] + b[i];
            }
        } """
    args = get_vector_add_args()
    answer = [np.ubyte([12]), None, None, None]
    tune_params = {"block_size_x": [128 + 64 * i for i in range(5)]}

    try:
        kernel_tuner.tune_kernel("vector_add",
                                 kernel_string,
                                 args[-1],
                                 args,
                                 tune_params,
                                 method="diff_evo",
                                 verbose=True,
                                 answer=answer)

        print("Expected a TypeError to be raised")
        assert False
    except TypeError as expected_error:
        print(str(expected_error))
        assert "Element 0" in str(expected_error)
    except Exception:
        print("Expected a TypeError to be raised")
        assert False
Example #36
0
def Numpy2QImage(i_image):
    """ Converts Numpy to QImage that can be displayed in a QLabel
        Only supports displaying grayscale Numpy and QImages. limited  type checking!"""
    image = numpy.ubyte(i_image).tostring()
    o_image = QtGui.QImage( image, i_image.shape[1], i_image.shape[0], i_image.shape[1], 
                            QtGui.QImage.Format_Indexed8 ).copy()
    o_image.setColorTable( g_color_table )
    return QtGui.QImage.convertToFormat ( o_image.copy(), QtGui.QImage.Format_RGB32 )
Example #37
0
    def flatten_matrix(matrix):
        # each entry in the flattened matrix is an uchar4
        f_matrix = []

        count = 0
        for i in range(matrix.shape[0]):
            for j in range(matrix.shape[1]):
                if matrix[i][j] != 0:
                    entry = numpy.array(
                        [numpy.byte(i), numpy.byte(j), numpy.byte(matrix[i][j]),
                         numpy.byte(0)])
                    f_matrix.append(entry)
                    count += 1
        return numpy.array(f_matrix), numpy.ubyte(count)
Example #38
0
 def byte_to_bit(ar_byte):
     '''
     convert a byte array (ubyte) to a a bit array (ubyte)
     
     Parameters
     ----------
     ar_byte : 1d-array, ubyte (from 0 to 255)
     
     Returns
     -------
     ar_bit : 1d-array, ubyte (only 0 and 1)
     '''
     return np.ravel( \
         np.fromfunction( \
             lambda i, j: np.ubyte(ar_byte[i] >> _BIT_INDEX[j] & 1), 
             (len(ar_byte), 8),
             dtype=np.int)
         )
Example #39
0
def _getExifValue(data, data_type):
    if data_type==1:
        return np.ubyte(data)
    elif data_type==2:
        return str(data)
    elif data_type==3:
        return np.uint16(data)
    elif data_type==4:
        return np.uint32(data)
    elif data_type==5:
        n=np.uint32(0xffffffff&data)
        d=np.uint32(((0xffffffff<<32)&data)>>32)
        if n==0:
            return 0
        elif d==0:
            return "nan"
        else:
            return (n,d)
    elif data_type==6:
        return np.byte(data)
    elif data_type==7:
        return data
    elif data_type==8:
        return np.int16(data)
    elif data_type==9:
        return np.int32(data)
    elif data_type==10:
        n=np.int32(0xffffffff&data)
        d=np.int32(((0xffffffff<<32)&data)>>32)
        if n==0:
            return 0
        elif d==0:
            return "nan"
        else:
            return (n,d)
    elif data_type==11:
        return np.float32(data)
    elif data_type==12:
        return np.float64(data)
    else:
        return data
def test_sequential_runner_not_matching_answer2():
    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
            int i = blockIdx.x * block_size_x + threadIdx.x;
            if (i<n) {
                c[i] = a[i] + b[i];
            }
        } """
    args = get_vector_add_args()
    answer = [np.ubyte([12]), None, None, None]
    tune_params = {"block_size_x": [128 + 64 * i for i in range(5)]}

    try:
        kernel_tuner.tune_kernel(
            "vector_add", kernel_string, args[-1], args, tune_params,
            method="diff_evo", verbose=True, answer=answer)

        print("Expected a TypeError to be raised")
        assert False
    except TypeError as expected_error:
        print(str(expected_error))
        assert "Element 0" in str(expected_error)
    except Exception:
        print("Expected a TypeError to be raised")
        assert False
Example #41
0
def Numpy2Ipl(i_image):
    image = numpy.ubyte(i_image)
    return cv.NumPy2Ipl(image)
Example #42
0
 def createTestBuffer(self):
     data = numpy.random.rand(self._samplesPerChannel)
     data = numpy.ubyte(data * 255)
     return data
def construct_dtype_and_converters(header, delimiter=INDEX_VALUES.sep_dipcp):
    dtype = []
    converters = {}
    header = header.rstrip().split(delimiter)
    for i, f in enumerate(header):
        f = re.sub('-[\d]+s-', '-', f)
        if f == '':
            t = '|S1' #(np.str_, 1)
            conv=np.str_
        elif  re.match('Time|\w*PacketDate|DIP[-\w]+Milli[sS]econds', f):
            # time format
            t = np.float_
            conv = lambda s: np.float_(s or 0)
        elif re.match('FlowIP|LastPacketIPSource', f):
            t = '|S16' #(np.str_, 16)
            conv=np.str_
        elif re.match('FlowEth', f):
            t = '|S17' #(np.str_, 17)
            conv=np.str_
        elif re.match('FlowPort', f):
            t = np.uint16
            conv = lambda s: np.uint16(s or 0)
        elif re.match('DIP[-\w]+Number-Packets-', f):
            t = np.uint32
            conv = lambda s: np.uint32(s or 0)
        elif re.match('DIP-Volume-Sum-Bytes-', f):
            t = np.uint64
            conv = lambda s: np.uint64(s or 0)
        elif re.match('DIP-Thp-Number-Kbps-|ts-', f):
            t = np.float_
            conv = lambda s: np.float_(s or 0)
        elif f == 'LastPacketProtocol':
            t = '|S5' #(np.str_, 5)
            conv=np.str_
        elif re.match('[\w]*LastTcpPacketType', f):
            t = '|S5' #(np.str_, 5)
            conv=np.str_
        elif re.match('SynCounter-', f):
            t = np.uint8
            conv = lambda s: np.uint8(s or 0)
        elif re.match('(?!Size)[A-Z][a-z]+-[1-9]', f):
            t = np.ubyte
            conv = lambda s: np.ubyte(s or 0)
        elif re.match('Size-[1-9]', f):
            t = np.uint16
            conv = lambda s: np.uint16(s or 0)
        elif f == 'TOS':
            t = np.ubyte
            conv = lambda s: np.ubyte(s or 0)
        elif f == 'LastPacketSize':
            t = np.uint16
            conv = lambda s: np.uint16(s or 0)
        elif re.match('DIP[-\w]+NbMes-', f):
            t = np.uint32
            conv = lambda s: np.uint32(s or 0)
        elif re.match('DIP[-\w]+Mean-', f):
            t = np.float_
            conv = lambda s: np.float_(s or 0)
        elif re.match('DIP[-\w]+(?:Min|Max)-ms-', f):
            t = np.float_
            conv = lambda s: np.float_(s or 0)
            # added for radius analysis
        elif f == 'IMSI':
            # IMSI and IP address both have 16 chars
            t = '|S16' #(np.str_, 16) #'|S16'
            conv=np.str_
        elif f == 'RAT':
            t = np.uint8
            conv = lambda s: np.uint8(s or 0)
        elif f == 'SGSN':
            t = '|S16' #(np.str_, 16)
            conv=np.str_
        elif f == 'IMEI':
            t = '|S48' #(np.str_, 48)
            conv=np.str_
        elif f == 'CellId':
            t = np.int_
            conv = lambda s: np.int_(s or 0)
        elif f == 'Constructor':
            t = '|S30'
            conv=np.str_
        elif f == 'DevType':
            t = '|S10'
            conv=np.str_
        elif re.match('DIP[-\w]+(?:Min|Max)-s-', f):
            t = np.float_
            conv = lambda s: np.float_(s or 0)
        else:
            raise Exception, "unknown format: " + f
        name = f
        while name in [fi for (fi, ty) in dtype]:
            name = '_'.join((name, 'TWICE'))
        dtype.append((name, t))
#        print t, type(t)
        converters[i] = conv
#        if type(t) is not tuple:
#            # automatic converter does not work for tuples
#            converters[i] = lambda s: t(s or 0)
#        else:
#            converters[i] = lambda s: str(s)
    return dtype, converters
Example #44
0
def dofilter(dlg, input, output, refband=1, memuse=100):
    try:
        from osgeo import gdal
    except ImportError:
        import gdal
    from gdalconst import GA_ReadOnly, GDT_Float32

    gdal.AllRegister()
    w = 5
    w2 = (w + 1) / 2
    refband = int(refband)
    memuse = int(memuse)
    tif = gdal.Open(str(input), GA_ReadOnly)
    nbands = tif.RasterCount
    driver = tif.GetDriver()
    xsize = tif.RasterXSize
    ysize = tif.RasterYSize
    out = driver.Create(str(output), xsize, ysize, nbands, gdal.GDT_Byte)
    out.SetGeoTransform(tif.GetGeoTransform())
    out.SetProjection(tif.GetProjection())
    band = [None] * nbands
    oband = [None] * nbands
    for i in range(0, nbands):
        band[i] = tif.GetRasterBand(i + 1)
        oband[i] = out.GetRasterBand(i + 1)
    nr = numpy.roll
    na = numpy.add
    band = nr(band, (1 - refband))
    oband = nr(oband, (1 - refband))
    refband2 = None
    readrows = int((((memuse - 67) * 48036) / xsize) / 2)
    oband[0].WriteArray(numpy.repeat(0, xsize * 2).reshape(2, -1), 0, 0)
    arr = [None] * nbands
    for i in range(0, nbands):
        arr[i] = band[i].ReadAsArray(0, 0, xsize, 4)
    for y in range(4, ysize, readrows):
        QCoreApplication.processEvents()
        if ysize - y < readrows:
            readrows = ysize - y
        arr1 = band[0].ReadAsArray(0, y, xsize, readrows)
        arr1 = numpy.uint32(numpy.vstack((arr[0], arr1)))
        arr[0] = arr1[readrows : readrows + 5,]
        sum = na(
            na(
                na(
                    na(na(arr1, nr(arr1, 1, 1)), nr(arr1, -1, 1)),
                    na(nr(nr(arr1, -1, 1), -1, 0), nr(nr(arr1, -1, 1), 1, 0)),
                ),
                na(nr(nr(arr1, 1, 1), -1, 0), nr(nr(arr1, 1, 1), 1, 0)),
            ),
            na(nr(arr1, -1, 0), nr(arr1, 1, 0)),
        )
        sum2 = arr1 ** 2
        sum2 = na(
            na(
                na(
                    na(na(sum2, nr(sum2, 1, 1)), nr(sum2, -1, 1)),
                    na(nr(nr(sum2, -1, 1), -1, 0), nr(nr(sum2, -1, 1), 1, 0)),
                ),
                na(nr(nr(sum2, 1, 1), -1, 0), nr(nr(sum2, 1, 1), 1, 0)),
            ),
            na(nr(sum2, -1, 0), nr(sum2, 1, 0)),
        )
        sum2 = numpy.uint16(numpy.round((sum2 - ((sum ** 2) / 9.0)) / 9.0))
        sum = numpy.ubyte((sum / 9.0) + 0.5)
        sum23 = nr(sum2, -2, 1)
        t = numpy.vstack((sum2.flatten(), sum23.flatten(), nr(sum2, 2, 0).flatten(), nr(sum23, 2, 0).flatten()))
        t = t == numpy.min(t, 0)
        sum23 = nr(sum, -2, 1)
        arr1 = nr(
            nr(
                numpy.ubyte(
                    numpy.reshape(
                        numpy.max(
                            (t)
                            * numpy.vstack(
                                (sum.flatten(), sum23.flatten(), nr(sum, 2, 0).flatten(), nr(sum23, 2, 0).flatten())
                            ),
                            0,
                        ),
                        (readrows + 4, -1),
                    )
                ),
                -1,
                0,
            ),
            1,
            1,
        )[2 : (readrows + 2), 2 : (xsize - 2)]
        oband[0].WriteArray(arr1, 2, y - 2)
        dlg.progressBar.setValue(int((100 * (y + (readrows / nbands) + 4)) / ysize))
        for i in range(1, nbands):
            arr1 = band[i].ReadAsArray(0, y, xsize, readrows)
            arr1 = numpy.uint32(numpy.vstack((arr[i], arr1)))
            arr[i] = arr1[readrows : readrows + 5,]
            sum = numpy.ubyte(
                (
                    na(
                        na(
                            na(
                                na(na(arr1, nr(arr1, 1, 1)), nr(arr1, -1, 1)),
                                na(nr(nr(arr1, -1, 1), -1, 0), nr(nr(arr1, -1, 1), 1, 0)),
                            ),
                            na(nr(nr(arr1, 1, 1), -1, 0), nr(nr(arr1, 1, 1), 1, 0)),
                        ),
                        na(nr(arr1, -1, 0), nr(arr1, 1, 0)),
                    )
                )
                / 9.0
                + 0.5
            )
            sum23 = nr(sum, -2, 1)
            arr1 = nr(
                nr(
                    numpy.ubyte(
                        numpy.reshape(
                            numpy.max(
                                (t)
                                * numpy.vstack(
                                    (sum.flatten(), sum23.flatten(), nr(sum, 2, 0).flatten(), nr(sum23, 2, 0).flatten())
                                ),
                                0,
                            ),
                            (readrows + 4, -1),
                        )
                    ),
                    -1,
                    0,
                ),
                1,
                1,
            )[2 : (readrows + 2), 2 : (xsize - 2)]
            oband[i].WriteArray(arr1, 2, y - 2)
            dlg.progressBar.setValue(int((100 * (y + (((i + 1) * readrows) / nbands) + 4)) / ysize))
    out = None
    tif = None
    return True
Example #45
0
import numpy

C_CONTIGUOUS = numpy.ubyte(1)
F_CONTIGUOUS = numpy.ubyte(1) << 1
OWNDATA = numpy.ubyte(1) << 2

C_DIRTY = numpy.ubyte(1) << 3  # internal use only
F_DIRTY = numpy.ubyte(1) << 4  # internal use only


class Flags(object):

    __slots__ = ['_value']

    def __init__(self, value):
        self._value = value

    def __getitem__(self, name):
        if name == 'C_CONTIGUOUS':
            return self.c_contiguous
        elif name == 'F_CONTIGUOUS':
            return self.f_contiguous
        elif name == 'OWNDATA':
            return self.owndata
        else:
            raise KeyError('%s is not defined for cupy.ndarray.flags' % name)

    def __repr__(self):
        t = '  %s : %s'
        ret = []
        for name in 'C_CONTIGUOUS', 'F_CONTIGUOUS', 'OWNDATA':
Example #46
0
 def __init__(self,J,Jfb):
     self.JJ=np.ubyte(J)
     self.JJfb=np.ubyte(Jfb)
Example #47
0
def compress_nets(NN):

    for Nc in NN:
        for P in Nc:
            P.JJ=np.ubyte(P.JJ)
###########################################################################
# linear kernel on byte features
###########################################################################
from tools.load import LoadMatrix
from numpy import ubyte
lm=LoadMatrix()

traindat = ubyte(lm.load_numbers('../data/fm_train_byte.dat'))
testdat = ubyte(lm.load_numbers('../data/fm_test_byte.dat'))

parameter_list=[[traindat,testdat],[traindat,testdat]]

def kernel_linear_byte_modular(fm_train_byte=traindat,fm_test_byte=testdat):
	from shogun.Kernel import LinearKernel
	from shogun.Features import ByteFeatures

	feats_train=ByteFeatures(fm_train_byte)
	feats_test=ByteFeatures(fm_test_byte)

	kernel=LinearKernel(feats_train, feats_train)
	km_train=kernel.get_kernel_matrix()

	kernel.init(feats_train, feats_test)
	km_test=kernel.get_kernel_matrix()
	return kernel

if __name__=='__main__':
	print('LinearByte')
	kernel_linear_byte_modular(*parameter_list[0])
 def WBool(value):
     if value:
         outfile.write(np.ubyte(1))
     else:
         outfile.write(np.ubyte(0))
Example #50
0
 def SetPatternDefs(self, dataFrames):
     patternSettings = np.zeros(1,PATTERN_INFO_DTYPE)
     patternSettings['depth'] = 1
     patternSettings['nPatterns'] = len(dataFrames)
     patternSettings['invert'] = 0
     patternSettings['trigger'] = PATTERN_TRIGGER.COMMAND
     #patternSettings[']
     h, d = self._ExecCommand(LC_PACKET_TYPE.HOST_WRITE, CMD_PATTERN_SETTING, patternSettings)
     print(( h, d))
     
     for index, data in enumerate(dataFrames):
         im = Image.fromarray(data)#.convert('1')
         output = StringIO.StringIO()
         im.save(output, format='BMP')
         contents = output.getvalue()
         output.close()
         #print contents[:50], np.fromstring(contents, 'u1')[:50]
         h, d = self._ExecCommand(LC_PACKET_TYPE.HOST_WRITE, CMD_PATTERN_DEFINITION, np.hstack([np.ubyte(index),np.fromstring(contents, 'u1')]))
         print(( h, d))
     return h, d