numpy.packbits

Here are the examples of the python api numpy.packbits taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

11 Examples 7

Example 1

Project: GPy Source File: netpbmfile.py
Function: to_file
    def _tofile(self, fh, pam=False):
        """Write Netbm file."""
        fh.seek(0)
        fh.write(self._header(pam))
        data = self.asarray(copy=False)
        if self.maxval == 1:
            data = numpy.packbits(data, axis=-1)
        data.tofile(fh)

Example 2

Project: peregrine Source File: encoder_base.py
Function: flush
  def flush(self):
    '''
    Flushes the data in the buffer.

    Returns
    -------
    ndarray
      Array of type uint8 containing the encoded data.
    '''
    if self.n_bits > 0 and self.n_bits % 8 != 0:
      self.n_bits += (8 - self.n_bits % 8)
    if self.n_bits:
      res = numpy.packbits(self.bits[0:self.n_bits])
      self.bits[0:self.n_bits].fill(0)
      self.n_bits = 0
      return res
    else:
      # Pack bits returns incorrect result in case of empty source bit set.
      return Encoder.EMPTY_RESULT

Example 3

Project: peregrine Source File: encoder_base.py
  def encodeValues(self):
    '''
    Converts buffered bit data into packed array.

    The method converts multiple of 8 bits into single output byte.

    Returns
    -------
    ndarray
      Array of type uint8 containing the encoded data.
    '''
    n_bytes = self.n_bits / 8
    n_offset = n_bytes * 8
    n_left = self.n_bits - n_offset
    res = numpy.packbits(self.bits[0: n_offset])
    self.bits[0:n_left] = self.bits[n_offset:n_offset + n_left]
    self.bits[n_left:].fill(0)
    self.n_bits = n_left
    return res

Example 4

Project: hdidx Source File: sh.py
    @staticmethod
    def compactbit(b):
        nSamples, nbits = b.shape
        nwords = (nbits + 7) / 8
        B = np.hstack([np.packbits(b[:, i*8:(i+1)*8][:, ::-1], 1)
                       for i in xrange(nwords)])
        residue = nbits % 8
        if residue != 0:
            B[:, -1] = np.right_shift(B[:, -1], 8 - residue)

        return B

Example 5

Project: deep-go-wrap Source File: cubes.py
@register(reg_label, 'expanded_label_packed')
def get_label_exp_packed(s, player):
    assert s.future
    player_next, (row, col) = s.future[0]
    assert player == player_next
    label = get_label_exp((row, col), s.board.side)
    return np.packbits(label)

Example 6

Project: pybrain Source File: utilities.py
def binArr2int(arr):
    """ Convert a binary array into its (long) integer representation. """
    from numpy import packbits
    tmp2 = packbits(arr.astype(int))
    return sum(val * 256 ** i for i, val in enumerate(tmp2[::-1])) 

Example 7

Project: python-ivi Source File: hprtl.py
def generate_bmp(img_data):
    """Generate a BMP format image from a numpy array"""
    bmp = io.BytesIO()

    width = img_data.shape[1]
    height = img_data.shape[0]

    if img_data.shape[2] == 1:
        # monochrome
        bpp = 1
        color_table_entries = 2

    else:
        # rgb
        bpp = 24
        color_table_entries = 0

    row_size = int((bpp*width + 31)/32)*4
    image_size = row_size * height
    header_size = 14+40
    color_table_size = color_table_entries*4
    image_offset = header_size+color_table_size
    file_size = image_offset+image_size

    # bitmap header
    bmp.write(b'BM')
    bmp.write(struct.pack('<L', file_size)) # file size
    bmp.write(struct.pack('<H', 0)) # reserved
    bmp.write(struct.pack('<H', 0)) # reserved
    bmp.write(struct.pack('<L', image_offset)) # offset to bitmap data
    # bitmapinfoheader
    bmp.write(struct.pack('<L', 40)) # size of header
    bmp.write(struct.pack('<l', width)) # image width
    bmp.write(struct.pack('<l', height)) # image height
    bmp.write(struct.pack('<H', 1)) # number of color planes
    bmp.write(struct.pack('<H', bpp)) # bits per pixel
    bmp.write(struct.pack('<L', 0)) # compression method
    bmp.write(struct.pack('<L', image_size)) # image size
    bmp.write(struct.pack('<L', 1)) # horizontal resolution
    bmp.write(struct.pack('<L', 1)) # vertical resolution
    bmp.write(struct.pack('<L', color_table_entries)) # number of colors in palette (0 = 2^n)
    bmp.write(struct.pack('<L', 0)) # number of important colors in palette (0 = all)

    if img_data.shape[2] == 1:
        # monochrome

        # color table
        bmp.write(struct.pack('<BBBx', 255, 255, 255)) # color 0 red, green, blue
        bmp.write(struct.pack('<BBBx', 0, 0, 0)) # color 1 red, green, blue

        # image data
        plane_data = np.packbits(img_data, axis=1)

        for y in range(plane_data.shape[0]-1, -1, -1):
            for x in range(plane_data.shape[1]):
                bmp.write(struct.pack('<B', plane_data[y][x][0]))
            if plane_data.shape[1] % 4 > 0:
                for x in range(4 - (plane_data.shape[1] % 4)):
                    bmp.write(b'\0')

    else:
        # rgb
        
        # color table
        # no color table for RGB

        # image data
        for y in range(img_data.shape[0]-1, -1, -1):
            for x in range(img_data.shape[1]):
                bmp.write(struct.pack('<BBB', img_data[y][x][2], img_data[y][x][1], img_data[y][x][0]))
            if (img_data.shape[1]*3) % 4 > 0:
                for x in range(4 - ((img_data.shape[1]*3) % 4)):
                    bmp.write(b'\0')

    return bmp.getvalue()

Example 8

Project: peregrine Source File: message_glo.py
  def updateParity(self, dataBits):
    '''
    Updates data bits and computes parity.

    When 85 bits are provided, they are used for parity computation and for
    bit inversion.

    Parameters
    ----------
    dataBits : numpy.ndarray(dtype=numpy.uint8)
      85 element array
    '''
    packed = numpy.packbits(dataBits)
    assert len(packed) == 11

    hc = _HAMMING_COEFFS
    for bIdx in range(8):
      p = 0
      for i in range(11):
        p ^= parity(packed[i] & hc[bIdx][i])
      dataBits[-(bIdx + 1)] = p
      packed[10] |= p << bIdx

Example 9

Project: peregrine Source File: message_lnav.py
  def updateParity(self, dataBits, resolve=False):
    '''
    Updates data bits and computes parity.

    When 32 bits are provided, they are used for parity computation and for
    bit inversion.

    Parameters
    ----------
    dataBits : numpy.ndarray(dtype=numpy.uint8)
      30 or 32 element array
    resolve: bool, optional
      When specified, bits d23 and d24 of the GPS word are updated to ensure
      that parity bits d29 and d30 are zeros.
    '''
    packed = numpy.packbits(dataBits)
    acc = (packed[0] << 24) | (packed[1] << 16) | \
          (packed[2] << 8) | packed[3]
    if len(dataBits) == 30:
      acc >>= 2
    elif acc & 0x40000000:
      acc ^= 0x3FFFFFC0
      dataBits[-30:-6] ^= 1

    # D29 = D30*^d1^d3^d5^d6^d7^d9^d10^d14^d15^d16^d17^d18^d21^d22^d24
    d29 = parity(acc & 0b01101011101100011111001101000000)
    # D30 = D29*^d3^d5^d6^d8^d9^d10^d11^d13^d15^d19^d22^d23^d24
    d30 = parity(acc & 0b10001011011110101000100111000000)

    if resolve:
      if d29:
        acc ^= 0x80
        d29 = False
        d30 = not d30
        dataBits[-8] ^= 1
      if d30:
        acc ^= 0x40
        d30 = False
        dataBits[-7] ^= 1

    # D25 = D29*^d1^d2^d3^d5^d6^d10^d11^d12^d13^d14^d17^d18^d20^d23
    dataBits[-6] = parity(acc & 0b10111011000111110011010010000000)
    # D26 = D30*^d2^d3^d4^d6^d7^d11^d12^d13^d14^d15^d18^d19^d21^d24
    dataBits[-5] = parity(acc & 0b01011101100011111001101001000000)
    # D27 = D29*^d1^d3^d4^d5^d7^d8^d12^d13^d14^d15^d16^d19^d20^d22
    dataBits[-4] = parity(acc & 0b10101110110001111100111000000000)
    # D28 = D30*^d2^d4^d5^d6^d8^d9^d13^d14^d15^d16^d17^d20^d21^d23
    dataBits[-3] = parity(acc & 0b01010111011000111110011010000000)
    # D29 = D30*^d1^d3^d5^d6^d7^d9^d10^d14^d15^d16^d17^d18^d21^d22^d24
    dataBits[-2] = d29
    # D30 = D29*^d3^d5^d6^d8^d9^d10^d11^d13^d15^d19^d22^d23^d24
    dataBits[-1] = d30

    return

Example 10

Project: deep-go-wrap Source File: cubes.py
@register(reg_cube, 'clark_storkey_2014_packed')
def get_cube_clark_storkey_2014_packed(*args):
    cube = get_cube_clark_storkey_2014(*args)
    return np.packbits(cube)

Example 11

Project: ndstore Source File: maskcube.py
Function: to_npz
  def toNPZ ( self ):
    """Pickle and zip the object"""

    self.data = np.packbits ( self.data )
    super(MaskCube,self).toNPZ()