Example #1
0
  def ingest ( self ):
    """Read the stack and ingest"""
    
    with closing (ocpcaproj.OCPCAProjectsDB()) as projdb:
      proj = projdb.loadToken(self.token)
    
    with closing (ocpcadb.OCPCADB(proj)) as db:

      ch = proj.getChannelObj(self.channel)
      # get the dataset configuration
      [[ximagesz, yimagesz, zimagesz],(starttime,endtime)] = proj.datasetcfg.imageSize(self.resolution)
      [xcubedim, ycubedim, zcubedim] = cubedim = proj.datasetcfg.getCubeDims()[self.resolution]
      [xoffset, yoffset, zoffset] = proj.datasetcfg.getOffset()[self.resolution]
    
      # for all specified resolutions
      for resolution in range(0,1,1):

        # extract parameters for iteration
        numxtiles = ximagesz/self.tilesz[0]
        numytiles = yimagesz/self.tilesz[1]

        # Ingest in database aligned slabs in the z dimension
        for slice_number in range(0, zimagesz, zcubedim):

          slab = np.zeros ( [zcubedim,yimagesz,ximagesz], dtype=np.uint32 )
          # over all tiles in that slice
          for b in range(zcubedim):
            for ytile in range(numytiles):
              for xtile in range(numxtiles):

                # if we are at the end of the space, quit
                if slice_number+b <= zimagesz:
                  try:
                    filename = '{}{}/{}/{}/{}.png'.format(self.tilepath, resolution, slice_number+b+zoffset, ytile+17, xtile+16)
                    print "Opening filename {}".format(filename)
                    # add tile to stack
                    imgdata = np.asarray ( Image.open(filename, 'r').convert('RGBA') )
                    imgdata = np.left_shift(imgdata[:,:,3], 24, dtype=np.uint32) | np.left_shift(imgdata[:,:,2], 16, dtype=np.uint32) | np.left_shift(imgdata[:,:,1], 8, dtype=np.uint32) | np.uint32(imgdata[:,:,0])
                    slab [b,ytile*self.tilesz[1]:(ytile+1)*self.tilesz[1],xtile*self.tilesz[0]:(xtile+1)*self.tilesz[0]] = imgdata
                  except IOError, e:
                    print "Failed to open file {}".format(filename)
                    slab [b,ytile*self.tilesz[1]:(ytile+1)*self.tilesz[1],xtile*self.tilesz[0]:(xtile+1)*self.tilesz[0]] = np.zeros([self.tilesz[1], self.tilesz[0]], dtype=np.uint32)

          for y in range (0, yimagesz+1, ycubedim):
            for x in range (0, ximagesz+1, xcubedim):
              
              # getting the cube id and ingesting the data one cube at a time
              zidx = ocplib.XYZMorton ([x/xcubedim, y/ycubedim, (slice_number)/zcubedim])
              cube = Cube.getCube(cubedim, ch.getChannelType(), ch.getDataType())
              cube.zeros()

              xmin, ymin = x, y
              xmax = min (ximagesz, x+xcubedim)
              ymax = min (yimagesz, y+ycubedim)
              zmin = 0
              zmax = min(slice_number+zcubedim, zimagesz+1)
              cube.data[0:zmax-zmin,0:ymax-ymin,0:xmax-xmin] = slab[zmin:zmax, ymin:ymax, xmin:xmax]
              
              if cube.isNotZeros():
                db.putCube(ch, zidx, self.resolution, cube, update=True)
Example #2
0
def load_spc(fname):
    """Load data from Becker & Hickl SPC files.

    Returns:
        3 numpy arrays (timestamps, detector, nanotime) and a float
        (timestamps_unit).
    """

    f = open(fname, 'rb')
    # We first decode the first 6 bytes which is a header...
    header = np.fromfile(f, dtype='u2', count=3)
    timestamps_unit = header[1] * 0.1e-9
    num_routing_bits = np.bitwise_and(header[0], 0x000F)  # unused

    # ...and then the remaining records containing the photon data
    spc_dtype = np.dtype([('field0', '<u2'), ('b', '<u1'), ('c', '<u1'),
                          ('a', '<u2')])
    data = np.fromfile(f, dtype=spc_dtype)

    nanotime =  4095 - np.bitwise_and(data['field0'], 0x0FFF)
    detector = data['c']

    # Build the macrotime (timestamps) using in-place operation for efficiency
    timestamps = data['b'].astype('int64')
    np.left_shift(timestamps, 16, out=timestamps)
    timestamps += data['a']

    # extract the 13-th bit from data['field0']
    overflow = np.bitwise_and(np.right_shift(data['field0'], 13), 1)
    overflow = np.cumsum(overflow, dtype='int64')

    # Add the overflow bits
    timestamps += np.left_shift(overflow, 24)

    return timestamps, detector, nanotime, timestamps_unit
Example #3
0
    def __invertibleToRGB(self, rgbVarr, varrIdx, colorLutStruct):
        '''
        Decode an RGB image rendered in INVERTIBLE_LUT mode. The image encodes
        float values as colors, so the RGB value is first decoded into its
        represented float value and then the color table is applied.
        '''
        w0 = np.left_shift(
            rgbVarr[varrIdx[0], varrIdx[1], 0].astype(np.uint32), 16)
        w1 = np.left_shift(
            rgbVarr[varrIdx[0], varrIdx[1], 1].astype(np.uint32), 8)
        w2 = rgbVarr[varrIdx[0], varrIdx[1], 2]

        value = np.bitwise_or(w0, w1)
        value = np.bitwise_or(value, w2)
        value = np.subtract(value.astype(np.int32), 1)
        normalized_val = np.divide(value.astype(float), 0xFFFFFE)

        # Map float value to color lut (use a histogram to support non-uniform
        # colormaps (fetch bin indices))
        colorLut = colorLutStruct.lut
        bins = colorLutStruct.adjustedBins

        idx = np.digitize(normalized_val, bins)
        idx = np.subtract(idx, 1)

        valueImage = np.zeros([rgbVarr.shape[0], rgbVarr.shape[1]],
                              dtype=np.uint32)
        valueImage[varrIdx[0], varrIdx[1]] = idx

        return colorLut[valueImage]
Example #4
0
    def _make_DO_array(self, data, channels):
        """ Get the port ordering in the final integer """
        ports = []
        for ch in channels:
            for port_name, line_name in ch.line_pairs:
                if port_name not in ports:
                    ports.append(port_name)

        # Final int array
        out = np.zeros(len(data.values()[0]), dtype=np.uint32)

        for ch in channels:
            arr = data[ch.name]
            for i, (port_name, line_name) in enumerate(ch.line_pairs):
                line_num = int(line_name[4:])
                bits = np.bitwise_and(arr, (1 << i))  # Mask out the user-input bits

                left_shift_amount = line_num - i
                if left_shift_amount > 0:
                    byts = np.left_shift(bits, left_shift_amount)
                elif left_shift_amount < 0:
                    byts = np.right_shift(bits, -left_shift_amount)
                else:
                    byts = bits

                byte_num = ports.index(port_name)
                out += np.left_shift(byts, 8*byte_num)
        return out
Example #5
0
def load_spc(fname):
    """Load data from Becker&Hickl SPC files.

    Returns:
        3 numpy arrays: timestamps, detector, nanotime
    """
    spc_dtype = np.dtype([("field0", "<u2"), ("b", "<u1"), ("c", "<u1"), ("a", "<u2")])
    data = np.fromfile(fname, dtype=spc_dtype)

    nanotime = 4095 - np.bitwise_and(data["field0"], 0x0FFF)
    detector = data["c"]

    # Build the macrotime (timestamps) using in-place operation for efficiency
    timestamps = data["b"].astype("int64")
    np.left_shift(timestamps, 16, out=timestamps)
    timestamps += data["a"]

    # extract the 13-th bit from data['field0']
    overflow = np.bitwise_and(np.right_shift(data["field0"], 13), 1)
    overflow = np.cumsum(overflow, dtype="int64")

    # Add the overflow bits
    timestamps += np.left_shift(overflow, 24)

    return timestamps, detector, nanotime
Example #6
0
def amagacolor(secret, red, green, blue):
    red = np.left_shift(np.right_shift(red,2),2)
    green = np.left_shift(np.right_shift(green,2),2)
    blue = np.left_shift(np.right_shift(blue,2),2)
    
    secretred,secretgreen,secretblue = separaimatge(secret,2,2,2)
    return red+secretred, green+secretgreen, blue+secretblue
Example #7
0
    def valuewriter(self, imageSlice, fname, vrange):
        """ Takes in either a (1C) float or a RGB (3C) buffer and writes it as
        an image file."""

        # Adjust the filename, replace png with .im
        baseName, ext = os.path.splitext(fname)
        adjustedName = baseName + self.floatExtension()

        dimensions = imageSlice.shape
        if len(dimensions) == 2 and imageSlice.dtype == numpy.float32:
            # Given as single channel floating point buffer.
            self.zwriter(imageSlice, adjustedName)

        elif (len(dimensions) > 2) and (dimensions[2] == 3):
            #if self.dontConvertValsToFloat
            #    self.rgbwriter(imageSlice, fname)
            #    return

            # Given an RGB buffer
            # Convert it to a floating point buffer for consistency
            # TODO: just one copy of this code in cinema
            w0 = numpy.left_shift(imageSlice[:,:,0].astype(numpy.uint32), 16)
            w1 = numpy.left_shift(imageSlice[:,:,1].astype(numpy.uint32), 8)
            w2 = imageSlice[:,:,2].astype(numpy.uint32)
            value = numpy.bitwise_or(w0,w1)
            value = numpy.bitwise_or(value,w2)
            value = numpy.subtract(value, 1)
            value = value.astype(numpy.float32)
            adjusted_val = numpy.divide(value, float(0xFFFFFE))

            self.zwriter(adjusted_val, adjustedName)

        else:
            raise ValueError("Invalid dimensions for a value raster.")
Example #8
0
    def rearrange_bits(array):
        # Do bit rearrangement for the 10-bit lytro raw format
        # Normalize output to 1.0 as float64
        t0 = array[0::5]
        t1 = array[1::5]
        t2 = array[2::5]
        t3 = array[3::5]
        lsb = array[4::5]

        t0 = np.left_shift(t0, 2) + np.bitwise_and(lsb, 3)
        t1 = np.left_shift(t1, 2) + np.right_shift(np.bitwise_and(lsb, 12), 2)
        t2 = np.left_shift(t2, 2) + np.right_shift(np.bitwise_and(lsb, 48), 4)
        t3 = np.left_shift(t3, 2) + np.right_shift(np.bitwise_and(lsb, 192), 6)

        image = np.zeros(LYTRO_ILLUM_IMAGE_SIZE, dtype=np.uint16)
        image[:, 0::4] = t0.reshape(
            (LYTRO_ILLUM_IMAGE_SIZE[0], LYTRO_ILLUM_IMAGE_SIZE[1] // 4)
        )
        image[:, 1::4] = t1.reshape(
            (LYTRO_ILLUM_IMAGE_SIZE[0], LYTRO_ILLUM_IMAGE_SIZE[1] // 4)
        )
        image[:, 2::4] = t2.reshape(
            (LYTRO_ILLUM_IMAGE_SIZE[0], LYTRO_ILLUM_IMAGE_SIZE[1] // 4)
        )
        image[:, 3::4] = t3.reshape(
            (LYTRO_ILLUM_IMAGE_SIZE[0], LYTRO_ILLUM_IMAGE_SIZE[1] // 4)
        )

        # Normalize data to 1.0 as 64-bit float.
        # Division is by 1023 as the Lytro Illum saves 10-bit raw data.
        return np.divide(image, 1023.0).astype(np.float64)
Example #9
0
    def _make_DO_array(self, data, channels):
        """ Get the port ordering in the final integer
        Parameters
        ----------
        data: dict
            Mapping from channel names to per-channel data arrays. Each array is a series of
            integer samples. Each sample is an integer representation of the output state of the
            corresponding channel.
        """
        ports = []
        for ch in channels:
            for port_name, line_name in ch.line_pairs:
                if port_name not in ports:
                    ports.append(port_name)

        # Final int array
        out = np.zeros(len(data.values()[0]), dtype=np.uint32)

        for ch in channels:
            arr = data[ch.name]
            for i, (port_name, line_name) in enumerate(ch.line_pairs):
                line_num = int(line_name[4:])
                bits = np.bitwise_and(arr, (1 << i))  # Mask out the user-input bits

                left_shift_amount = line_num - i
                if left_shift_amount > 0:
                    byts = np.left_shift(bits, left_shift_amount)
                elif left_shift_amount < 0:
                    byts = np.right_shift(bits, -left_shift_amount)
                else:
                    byts = bits

                byte_num = ports.index(port_name)
                out += np.left_shift(byts, 8*byte_num)
        return out
Example #10
0
def _wrapx(input, output, nx):
    """
    Wrap the X format column Boolean array into an ``UInt8`` array.

    Parameters
    ----------
    input
        input Boolean array of shape (`s`, `nx`)

    output
        output ``Uint8`` array of shape (`s`, `nbytes`)

    nx
        number of bits
    """

    output[...] = 0  # reset the output
    nbytes = ((nx - 1) // 8) + 1
    unused = nbytes * 8 - nx
    for i in range(nbytes):
        _min = i * 8
        _max = min((i + 1) * 8, nx)
        for j in range(_min, _max):
            if j != _min:
                np.left_shift(output[..., i], 1, output[..., i])
            np.add(output[..., i], input[..., j], output[..., i])

    # shift the unused bits
    np.left_shift(output[..., i], unused, output[..., i])
Example #11
0
def _dec10216(inbuf):
    inbuf = np.fromstring(inbuf, dtype=np.uint8)
    arr10 = inbuf.astype(np.uint16)
    arr16 = np.zeros((len(arr10) / 5 * 4,), dtype=np.uint16)
    arr10_len = (len(arr16) * 5) / 4
    arr10 = arr10[:arr10_len] # adjust size
    """
    /*
     * pack 4 10-bit words in 5 bytes into 4 16-bit words
     * 
     * 0       1       2       3       4       5
     * 01234567890123456789012345678901234567890
     * 0         1         2         3         4
     */      
    ip = &in_buffer[i];
    op = &out_buffer[j];
    op[0] = ip[0]*4 + ip[1]/64;
    op[1] = (ip[1] & 0x3F)*16 + ip[2]/16;
    op[2] = (ip[2] & 0x0F)*64 + ip[3]/4;
    op[3] = (ip[3] & 0x03)*256 +ip[4];
    """
    arr16.flat[::4] = np.left_shift(arr10[::5], 2) + \
        np.right_shift((arr10[1::5]), 6)
    arr16.flat[1::4] = np.left_shift((arr10[1::5] & 63), 4) + \
        np.right_shift((arr10[2::5]), 4)
    arr16.flat[2::4] = np.left_shift(arr10[2::5] & 15, 6) + \
        np.right_shift((arr10[3::5]), 2)
    arr16.flat[3::4] = np.left_shift(arr10[3::5] & 3, 8) + \
        arr10[4::5]    
    return arr16.tostring()
Example #12
0
def _3dby8toRGB ( indata ):
  """Convert a numpy array of 3d, 8-bit data to 32bit RGB"""

  rgbdata = np.zeros ( indata.shape[0:2], dtype=np.uint32)

  rgbdata = np.uint32(0xFF000000) + np.left_shift(np.uint32(indata[:,:,:,0]),16) + np.left_shift(np.uint32(indata[:,:,:,1]),8) + np.uint32(indata[:,:,:,2])

  return rgbdata
def partBy2(x):
    x = np.bitwise_and(x, 0x1f00000000ffff)
    x = np.bitwise_and(np.bitwise_or(x, np.left_shift(x, 32)), 0x1f00000000ffff)
    x = np.bitwise_and(np.bitwise_or(x, np.left_shift(x, 16)), 0x1f0000ff0000ff)
    x = np.bitwise_and(np.bitwise_or(x, np.left_shift(x, 8)), 0x100f00f00f00f00f)
    x = np.bitwise_and(np.bitwise_or(x, np.left_shift(x, 4)), 0x10c30c30c30c30c3)
    x = np.bitwise_and(np.bitwise_or(x, np.left_shift(x, 2)), 0x1249249249249249)
    return x
Example #14
0
 def _applytwo(self, f1, f2, offset, poses, num, rem):
     bts1 = f1 if self._subset is None else f1[self._subset]
     bts2 = f2 if self._subset is None else f2[self._subset]
     mask = 2**(num-rem)-1
     out = np.bitwise_and(bts2, mask)
     np.left_shift(out, rem, out)
     out += np.right_shift(bts1, offset)
     return out
Example #15
0
def _read_3(fid):
    """ Read 3 byte integer from file
    """
    data = np.fromfile(fid, dtype=np.uint8, count=3).astype(np.int32)

    out = np.left_shift(data[0], 16) + np.left_shift(data[1], 8) + data[2]

    return out
Example #16
0
    def test_shift(self):
        from numpy import left_shift, right_shift, dtype

        assert (left_shift([5, 1], [2, 13]) == [20, 2 ** 13]).all()
        assert (right_shift(10, range(5)) == [10, 5, 2, 1, 0]).all()
        bool_ = dtype("bool").type
        assert left_shift(bool(1), 3) == left_shift(1, 3)
        assert right_shift(bool(1), 3) == right_shift(1, 3)
Example #17
0
def fliplr(x,length):
	x1 = array(x)
	x[:] = 0
	for i in xrange(length):
		x2 = array(x1)
		x2 = right_shift(x2,i)
		bitwise_and(x2,1,out=x2)
		left_shift(x2,length-1-i,out=x2)
		x += x2
Example #18
0
def main():

  parser = argparse.ArgumentParser(description='Ingest the TIFF data')
  parser.add_argument('token', action="store", type=str, help='Token for the project')
  parser.add_argument('channel', action="store", type=str, help='Channel for the project')
  parser.add_argument('path', action="store", type=str, help='Directory with the image files')
  parser.add_argument('resolution', action="store", type=int, help='Resolution of data')

  result = parser.parse_args()
  
  # Load a database
  with closing (ocpcaproj.OCPCAProjectsDB()) as projdb:
    proj = projdb.loadToken(result.token)

  with closing (ocpcadb.OCPCADB(proj)) as db:

    ch = proj.getChannelObj(result.channel)
    # get the dataset configuration
    [[ximagesz, yimagesz, zimagesz],(starttime,endtime)] = proj.datasetcfg.imageSize(result.resolution)
    [xcubedim,ycubedim,zcubedim] = cubedim = proj.datasetcfg.getCubeDims()[result.resolution]
    [xoffset, yoffset, zoffset] = proj.datasetcfg.getOffset()[result.resolution]

    # Get a list of the files in the directories
    for slice_number in range (zoffset, zimagesz+1, zcubedim):
      slab = np.zeros([zcubedim, yimagesz, ximagesz ], dtype=np.uint32)
      for b in range(zcubedim):
        if (slice_number + b <= zimagesz):
          try:
            # reading the raw data
            file_name = "{}BrainBowExM_{:0>4}.tif".format(result.path, slice_number+b)
            print "Open filename {}".format(file_name)
            img = Image.open(file_name, 'r').convert("RGBA")
            imgdata = np.asarray(img)
            slab[b,:,:] = np.left_shift(imgdata[:,:,3], 24, dtype=np.uint32) | np.left_shift(imgdata[:,:,2], 16, dtype=np.uint32) | np.left_shift(imgdata[:,:,1], 8, dtype=np.uint32) | np.uint32(imgdata[:,:,0])
          except IOError, e:
            print e
            imgdata = np.zeros((yimagesz, ximagesz), dtype=np.uint32)
            slab[b,:,:] = imgdata

      for y in range ( 0, yimagesz+1, ycubedim ):
        for x in range ( 0, ximagesz+1, xcubedim ):

          # Getting a Cube id and ingesting the data one cube at a time
          zidx = ocplib.XYZMorton ( [x/xcubedim, y/ycubedim, (slice_number-zoffset)/zcubedim] )
          cube = Cube.getCube(cubedim, ch.getChannelType(), ch.getDataType())
          cube.zeros()

          xmin = x
          ymin = y
          xmax = min ( ximagesz, x+xcubedim )
          ymax = min ( yimagesz, y+ycubedim )
          zmin = 0
          zmax = min(slice_number+zcubedim, zimagesz+1)

          cube.data[0:zmax-zmin,0:ymax-ymin,0:xmax-xmin] = slab[zmin:zmax, ymin:ymax, xmin:xmax]
          db.putCube(ch, zidx, result.resolution, cube, update=True)
Example #19
0
 def _pack_bin(self, pixel_list):
     """
     Internal. Encodes [R,G,B] into 16 bit RGB565
     works on a numpy array (H, W, 3) returns flattened bytes string.
     """
     bits16 = np.zeros(pixel_list.shape[:2], dtype=np.uint16)
     bits16 += np.left_shift(np.bitwise_and(pixel_list[:,:,0], 0xF8), 8)
     bits16 += np.left_shift(np.bitwise_and(pixel_list[:,:,1], 0xFC), 3)
     bits16 += np.right_shift(pixel_list[:,:,2], 3)
     return bits16.tostring()
Example #20
0
 def func (src, dest):
     mapped = mapper (src)
     dest.fill (0xFF000000)
     effscratch = (mapped[:,:,0] * 0xFF).astype (np.uint32)
     np.left_shift (effscratch, 16, effscratch)
     np.bitwise_or (dest, effscratch, dest)
     effscratch = (mapped[:,:,1] * 0xFF).astype (np.uint32)
     np.left_shift (effscratch, 8, effscratch)
     np.bitwise_or (dest, effscratch, dest)
     effscratch = (mapped[:,:,2] * 0xFF).astype (np.uint32)
     np.bitwise_or (dest, effscratch, dest)
Example #21
0
def dec10to16(data):
    arr10 = data.astype(np.uint16).flat
    new_shape = list(data.shape[:-1]) + [(data.shape[-1] * 8) / 10]
    arr16 = np.zeros(new_shape, dtype=np.uint16)
    arr16.flat[::4] = np.left_shift(arr10[::5], 2) + \
        np.right_shift((arr10[1::5]), 6)
    arr16.flat[1::4] = np.left_shift((arr10[1::5] & 63), 4) + \
        np.right_shift((arr10[2::5]), 4)
    arr16.flat[2::4] = np.left_shift(arr10[2::5] & 15, 6) + \
        np.right_shift((arr10[3::5]), 2)
    arr16.flat[3::4] = np.left_shift(arr10[3::5] & 3, 8) + \
        arr10[4::5]
    return arr16   
Example #22
0
def _get_voc_color_map(n=256):
    color_map = np.zeros((n, 3))
    for i in xrange(n):
        r = b = g = 0
        cid = i
        for j in xrange(0, 8):
            r = np.bitwise_or(r, np.left_shift(np.unpackbits(np.array([cid], dtype=np.uint8))[-1], 7-j))
            g = np.bitwise_or(g, np.left_shift(np.unpackbits(np.array([cid], dtype=np.uint8))[-2], 7-j))
            b = np.bitwise_or(b, np.left_shift(np.unpackbits(np.array([cid], dtype=np.uint8))[-3], 7-j))
            cid = np.right_shift(cid, 3)

        color_map[i][0] = r
        color_map[i][1] = g
        color_map[i][2] = b
    return color_map
Example #23
0
  def ErrorCorrection(self,syndrome,inBits):
  # LUT based error correction
  # errVector = find( errorSyndromes == syndrome );
  # if numel(errVector)>1
  #   disp('Help!');
  # end
  # if isempty(errVector)
  #   failure = true;
  # else
  #   inBits = bitxor(inBits,errorVectors(errVector(1),:));
  #   failure = false;
  # end

    corrected = 0;
    if(syndrome):
      # Meggitt algorithm, only correct data bits (16), not the parity bits
      for ndx in range(16):
        
        # The first (most significant) bit is one
        if (np.bitwise_and(syndrome,512)):
          # If the first bit is a one and the last 5 (least significant bits)
          # are zero, this indicates an error at the current bit (ndx) position
          if (np.bitwise_and(syndrome,31) == 0):
            # The code can correct bursts up to 5 bits long.  Check to see if
            # the error is a burst or not.  If it isn't a burst, it isn't
            # correctable, return immediately with a failure.
            tmp = np.bitwise_and(syndrome,480);
            if ~(tmp == 480 | tmp == 448 | tmp == 384 | tmp == 256 | tmp == 0):
              break;
            # The error appears to be a burst error, attempt to correct
            inBits[ndx] = np.bitwise_xor(inBits(ndx),1);
            # Shift the syndrome
            syndrome = np.left_shift(syndrome,1);
          else:
            # Least significant bits do not indicate the current bit (ndx) is
            # an error in a burst.  Continue shifting the syndrome and then apply the
            # generator polynomial.
            syndrome = np.left_shift(syndrome,1);
            syndrome = np.bitwise_xor(syndrome,441);
        else:
          # Not a one at the first (most significant) syndrome bit, applying generator polynomial
          # is trivial in this case.
          syndrome = np.left_shift(syndrome,1);
      # If after this process the syndrome is not zero, there was a
      # uncorrectable error.
      if (np.bitwise_and(syndrome,1023)==0):
        corrected = 1;
    return (inBits, corrected);
Example #24
0
def unpack_raw12_image(img):
    """Unpack a raw-12 image to a raw-16 image.

    Output image will have the 12 LSBs filled in each 16b word, and the 4 MSBs
    will be set to zero.

    Args:
        img: A raw-12 image, as a uint8 numpy array.

    Returns:
        Image as a uint16 numpy array, with all row padding stripped.
    """
    if img.shape[1] % 3 != 0:
        raise its.error.Error('Invalid raw-12 buffer width')
    w = img.shape[1]*2/3
    h = img.shape[0]
    # Cut out the 2x8b MSBs and shift to bits [11:4] in 16b words.
    msbs = numpy.delete(img, numpy.s_[2::3], 1)
    msbs = msbs.astype(numpy.uint16)
    msbs = numpy.left_shift(msbs, 4)
    msbs = msbs.reshape(h,w)
    # Cut out the 2x4b LSBs and put each in bits [3:0] of their own 8b words.
    lsbs = img[::, 2::3].reshape(h,w/2)
    lsbs = numpy.right_shift(
            numpy.packbits(numpy.unpackbits(lsbs).reshape(h,w/2,2,4),3), 4)
    lsbs = lsbs.reshape(h,w)
    # Fuse the MSBs and LSBs back together
    img16 = numpy.bitwise_or(msbs, lsbs).reshape(h,w)
    return img16
def unpack_raw10_image(img):
    """Unpack a raw-10 image to a raw-16 image.

    Output image will have the 10 LSBs filled in each 16b word, and the 6 MSBs
    will be set to zero.

    Args:
        img: A raw-10 image, as a uint8 numpy array.

    Returns:
        Image as a uint16 numpy array, with all row padding stripped.
    """
    if img.shape[1] % 5 != 0:
        raise its.error.Error('Invalid raw-10 buffer width')
    w = img.shape[1]*4/5
    h = img.shape[0]
    # Cut out the 4x8b MSBs and shift to bits [10:2] in 16b words.
    msbs = numpy.delete(img, numpy.s_[4::5], 1)
    msbs = msbs.astype(numpy.uint16)
    msbs = numpy.left_shift(msbs, 2)
    msbs = msbs.reshape(h,w)
    # Cut out the 4x2b LSBs and put each in bits [2:0] of their own 8b words.
    lsbs = img[::, 4::5].reshape(h,w/4)
    lsbs = numpy.right_shift(
            numpy.packbits(numpy.unpackbits(lsbs).reshape(h,w/4,4,2),3), 6)
    lsbs = lsbs.reshape(h,w)
    # Fuse the MSBs and LSBs back together
    img16 = numpy.bitwise_or(msbs, lsbs).reshape(h,w)
    return img16
Example #26
0
 def _applyone(self, field, offset, poses, num):
     bts = field if self._subset is None else field[self._subset]
     out = np.right_shift(bts, offset)
     mask = 2**num-1
     np.bitwise_and(out, mask, out)
     out += np.left_shift(poses, num)        
     return out
Example #27
0
            def func (src, dest):
                effscratch2 = scratch2[:dest.shape[0],:dest.shape[1]]
                mapped = mapper (src)

                dest.fill (0xFF000000)
                effscratch = (mapped[:,:,0] * 0xFF).astype (np.uint32)
                np.left_shift (effscratch, 16, effscratch)
                np.bitwise_or (dest, effscratch, dest)
                effscratch = (mapped[:,:,1] * 0xFF).astype (np.uint32)
                np.left_shift (effscratch, 8, effscratch)
                np.bitwise_or (dest, effscratch, dest)
                effscratch = (mapped[:,:,2] * 0xFF).astype (np.uint32)
                np.bitwise_or (dest, effscratch, dest)

                np.invert (src.mask, effscratch2)
                np.multiply (dest, effscratch2, dest)
Example #28
0
 def get_uniqueId(self, nShift=10):
     arr = self.column_by_name(self.refIdCol)
     if len(arr) > 0:
         return np.left_shift(self.column_by_name(self.refIdCol), nShift) + \
                self.db_obj.getObjectTypeId()
     else:
         return arr
Example #29
0
  def testIntOps(self):
    for dtype in self.int_types:
      self._testBinary(
          gen_math_ops._truncate_div,
          np.array([3, 3, -1, -9, -8], dtype=dtype),
          np.array([2, -2, 7, 2, -4], dtype=dtype),
          expected=np.array([1, -1, 0, -4, 2], dtype=dtype))
      self._testSymmetricBinary(
          bitwise_ops.bitwise_and,
          np.array([0b1, 0b101, 0b1000], dtype=dtype),
          np.array([0b0, 0b101, 0b1001], dtype=dtype),
          expected=np.array([0b0, 0b101, 0b1000], dtype=dtype))
      self._testSymmetricBinary(
          bitwise_ops.bitwise_or,
          np.array([0b1, 0b101, 0b1000], dtype=dtype),
          np.array([0b0, 0b101, 0b1001], dtype=dtype),
          expected=np.array([0b1, 0b101, 0b1001], dtype=dtype))

      lhs = np.array([0, 5, 3, 14], dtype=dtype)
      rhs = np.array([5, 0, 7, 11], dtype=dtype)
      self._testBinary(
          bitwise_ops.left_shift, lhs, rhs,
          expected=np.left_shift(lhs, rhs))
      self._testBinary(
          bitwise_ops.right_shift, lhs, rhs,
          expected=np.right_shift(lhs, rhs))

      if dtype in [np.int8, np.int16, np.int32, np.int64]:
        lhs = np.array([-1, -5, -3, -14], dtype=dtype)
        rhs = np.array([5, 0, 1, 11], dtype=dtype)
        self._testBinary(
            bitwise_ops.right_shift, lhs, rhs,
            expected=np.right_shift(lhs, rhs))
Example #30
0
    def write_screen_numpy(self, image: np.ndarray) -> None:
        """
        Convert a numpy array and write it to the screen

        Converts a (320, 240, 3)-shaped numpy array to the display format and
        sends it to the display.

        If the dtype of the image is float it is assumed to be RGB data
        in the range [0., 1.] and the input is clamped to these values.

        If the dtype of the image is uint8 it is assumed to be RGB data
        in the range [0, 255] and the lowest bits are discarded during conversion.

        Other dtypes are not supported yet.
        @param image: numpy.ndarray of shape (320, 240, 3)
        """
        if not isinstance(image, np.ndarray):
            raise TypeError('image must be a numpy array')
        if image.shape != (320, 240, 3):
            raise TypeError('image must have shape (320, 240, 3)')

        if image.dtype == np.float:
            clamped = np.clip(image, 0.0, 1.0)
            conv = (clamped * np.array([31., 63., 31.])).astype(np.uint8)
        elif image.dtype == np.uint8:
            conv = np.right_shift(image, [3, 2, 3])
        else:
            raise TypeError('image must be an numpy.ndarray of type npumpy.float or numpy.uint8')

        packed = np.left_shift(conv, [11, 5, 0]).sum(2).flatten().astype(np.uint16)
        self.write_screen_raw(packed.tobytes())
Example #31
0
def _get_voc_color_map(n=256):
    color_map = np.zeros((n, 3))
    for i in xrange(n):
        r = b = g = 0
        cid = i
        for j in xrange(0, 8):
            r = np.bitwise_or(
                r,
                np.left_shift(
                    np.unpackbits(np.array([cid], dtype=np.uint8))[-1], 7 - j))
            g = np.bitwise_or(
                g,
                np.left_shift(
                    np.unpackbits(np.array([cid], dtype=np.uint8))[-2], 7 - j))
            b = np.bitwise_or(
                b,
                np.left_shift(
                    np.unpackbits(np.array([cid], dtype=np.uint8))[-3], 7 - j))
            cid = np.right_shift(cid, 3)

        color_map[i][0] = r
        color_map[i][1] = g
        color_map[i][2] = b
    return color_map
Example #32
0
 def weight_update(synapse, delta_ws, rng=None):
     synapse_fmt = synapse.synapse_fmt
     wgt_exp = synapse_fmt.realWgtExp
     shift_bits = synapse_fmt.shift_bits
     overflow = learn_overflow_bits(n_factors=2)
     for w, delta_w in zip(synapse.weights, delta_ws):
         product = shift(delta_w * synapse._lr_int,
                         LEARN_FRAC + synapse._lr_exp - overflow)
         learn_w = shift(w, LEARN_FRAC - wgt_exp) + product
         learn_w[:] = stochastic_round(
             learn_w * 2**(-LEARN_FRAC - shift_bits),
             clip=2**(8 - shift_bits) - 1,
             rng=rng,
             name="learning weights")
         w[:] = np.left_shift(learn_w, wgt_exp + shift_bits)
Example #33
0
def _update_pi():
    """Writes new LED values to the Raspberry Pi's LED strip

    Raspberry Pi uses the rpi_ws281x to control the LED strip directly.
    This function updates the LED strip with new values.
    """
    global pixels, _prev_pixels
    # Truncate values and cast to integer
    pixels = np.clip(pixels, 0, 255).astype(int)
    # Optional gamma correction
    p = _gamma[pixels] if config.SOFTWARE_GAMMA_CORRECTION else np.copy(pixels)
    # Encode 24-bit LED values in 32 bit integers
    r = np.left_shift(p[0][:].astype(int), 8)
    g = np.left_shift(p[1][:].astype(int), 16)
    b = p[2][:].astype(int)
    rgb = np.bitwise_or(np.bitwise_or(r, g), b)
    # Update the pixels
    for i in range(config.N_PIXELS):
        # Ignore pixels if they haven't changed (saves bandwidth)
        if np.array_equal(p[:, i], _prev_pixels[:, i]):
            continue
        strip._led_data[i] = rgb[i]
    _prev_pixels = np.copy(p)
    strip.show()
Example #34
0
    def to_flat_representation(self):
        """
        returns the flat representation of the given state
        """
        qState = np.uint32(0)
        idx = 0

        for row in self.Rows:
            for pearl in row:
                if pearl:
                    qState = np.bitwise_or(qState, np.left_shift(1, idx))

                idx += 1

        return qState
Example #35
0
    def decode_hgt(self, src_bytes, wrapped):

        # split array into high and low bytes
        low_b = src_bytes[::2]
        high_b = src_bytes[1::2]
        heights = None

        if not wrapped:
            heights = np.left_shift(high_b, 8) + low_b
        else:
            heights = low_b

        # convert NULL values to 0
        heights[heights > 9000] = 0
        return heights
Example #36
0
def applyMasks(x, y, z, m, o, o2, md, filter_options=(), maskSuffix=None):
    """
    Apply masks to the scene DEM, matchtag, and ortho matrices.
    """
    if sys.version_info[0] < 3:
        from filter_scene import mask_v2, MASKCOMP_EDGE_BIT, MASKCOMP_WATER_BIT, MASKCOMP_CLOUD_BIT
    else:
        from lib.filter_scene import mask_v2, MASKCOMP_EDGE_BIT, MASKCOMP_WATER_BIT, MASKCOMP_CLOUD_BIT

    mask_select = md

    if len(filter_options) > 0:
        mask_select = np.copy(md)
        mask_ones = np.ones_like(mask_select)
        for opt in filter_options:
            unmask_bit = None
            if opt == 'nowater':
                unmask_bit = MASKCOMP_WATER_BIT
            elif opt == 'nocloud':
                unmask_bit = MASKCOMP_CLOUD_BIT
            if unmask_bit is not None:
                np.bitwise_and(mask_select,
                               ~np.left_shift(mask_ones, unmask_bit),
                               out=mask_select)

    mask_select = (mask_select > 0)

    if maskSuffix in ('mask.tif', 'bitmask.tif'):
        mask_select = mask_v2(postprocess_mask=mask_select,
                              postprocess_res=abs(x[1] - x[0]))

    z[mask_select] = np.nan
    m[mask_select] = 0

    # If there is any good data, crop the matrices of bordering NaNs.
    if np.any(~np.isnan(z)):
        rowcrop, colcrop = cropBorder(z, np.nan)

        x = x[colcrop[0]:colcrop[1]]
        y = y[rowcrop[0]:rowcrop[1]]
        z = z[rowcrop[0]:rowcrop[1], colcrop[0]:colcrop[1]]
        m = m[rowcrop[0]:rowcrop[1], colcrop[0]:colcrop[1]]
        o = o[rowcrop[0]:rowcrop[1], colcrop[0]:colcrop[1]]
        o2 = o2[rowcrop[0]:rowcrop[1],
                colcrop[0]:colcrop[1]] if o2 is not None else None
        md = md[rowcrop[0]:rowcrop[1], colcrop[0]:colcrop[1]]

    return x, y, z, m, o, o2, md
Example #37
0
    def __rx_non_complex(self):
        if not self.__rxbuf:
            self._rx_init_channels()
        self.__rxbuf.refill()
        data = self.__rxbuf.read()
        x = np.frombuffer(data, dtype=self._rx_data_type)
        if self._rx_mask != 0:
            x = np.bitwise_and(x, self._rx_mask)
        if self._rx_shift > 0:
            x = np.right_shift(x, self._rx_shift)
        elif self._rx_shift < 0:
            x = np.left_shift(x, -(self._rx_shift))

        sig = []
        stride = len(self.rx_enabled_channels)

        if self.rx_output_type == "raw":
            for c in range(stride):
                sig.append(x[c::stride])
        elif self.rx_output_type == "SI":
            rx_scale = []
            rx_offset = []
            for i in self.rx_enabled_channels:
                v = self._rxadc.find_channel(self._rx_channel_names[i])
                if "scale" in v.attrs:
                    scale = self._get_iio_attr(self._rx_channel_names[i],
                                               "scale", False)
                else:
                    scale = 1.0

                if "offset" in v.attrs:
                    offset = self._get_iio_attr(self._rx_channel_names[i],
                                                "offset", False)
                else:
                    offset = 0.0
                rx_scale.append(scale)
                rx_offset.append(offset)

            for c in range(stride):
                raw = x[c::stride]
                sig.append(raw * rx_scale[c] + rx_offset[c])
        else:
            raise Exception("rx_output_type undefined")

        # Don't return list if a single channel
        if len(self.rx_enabled_channels) == 1:
            return sig[0]
        return sig
Example #38
0
def make_uniform_randoms_fast(sparse_map,
                              n_random,
                              nside_randoms=2**23,
                              rng=None):
    """
    Make an array of uniform randoms.

    Parameters
    ----------
    sparse_map: `healsparse.HealSparseMap`
       Sparse map object
    n_random: `int`
       Number of randoms to generate
    nside_randoms: `int`, optional
       Nside for pixel centers to select random points
    rng: `np.random.RandomState`, optional
       Pre-set Random number generator.  Default is None.

    Returns
    -------
    ra_array: `np.ndarray`
       Float array of RAs (degrees)
    dec_array: `np.ndarray`
       Float array of declinations (degrees)
    """

    if rng is None:
        rng = np.random.RandomState()

    # get the valid pixels
    valid_pixels = sparse_map.valid_pixels

    # Select which "coarse" valid pixels are selected
    ipnest_coarse = rng.choice(valid_pixels, size=n_random, replace=True)

    # What is the bitshift from the sparse_map nside to nside_randoms?
    bit_shift = _compute_bitshift(sparse_map.nside_sparse, nside_randoms)

    # The sub-pixels are random from bit_shift
    sub_pixels = rng.randint(0, high=2**bit_shift - 1, size=n_random)

    ra_rand, dec_rand = hp.pix2ang(nside_randoms,
                                   np.left_shift(ipnest_coarse, bit_shift) +
                                   sub_pixels,
                                   lonlat=True,
                                   nest=True)

    return ra_rand, dec_rand
Example #39
0
def test_left_shift():
    aee(
        Expression(IDs.SQRT, UC('2')) << Expression(IDs.SQRT, UC('3')),
        Expression(IDs.LSHIFT, Expression(IDs.SQRT, UC('2')),
                   Expression(IDs.SQRT, UC('3'))))
    expression = Expression(IDs.SQRT, UC('2'))
    expression <<= Expression(IDs.SQRT, UC('3'))
    aee(
        expression,
        Expression(IDs.LSHIFT, Expression(IDs.SQRT, UC('2')),
                   Expression(IDs.SQRT, UC('3'))))
    aee(
        np.left_shift(Expression(IDs.SQRT, UC('2')),
                      Expression(IDs.SQRT, UC('3'))),
        Expression(IDs.LSHIFT, Expression(IDs.SQRT, UC('2')),
                   Expression(IDs.SQRT, UC('3'))))
Example #40
0
    def image_to_data(self, image, rotation=0):
        """Generator function to convert a PIL image to 16-bit 565 RGB bytes."""
        # NumPy is much faster at doing this. NumPy code provided by:
        # Keith (https://www.blogger.com/profile/02555547344016007163)
        if not isinstance(image, np.ndarray):
            image = np.array(image.convert('RGB'))

        pb = np.rot90(image, rotation // 90).astype('uint8')

        result = np.zeros((self._width, self._height, 2), dtype=np.uint8)
        result[..., [0]] = np.add(np.bitwise_and(pb[..., [0]], 0xF8),
                                  np.right_shift(pb[..., [1]], 5))
        result[..., [1]] = np.add(
            np.bitwise_and(np.left_shift(pb[..., [1]], 3), 0xE0),
            np.right_shift(pb[..., [2]], 3))
        return result.flatten().tolist()
Example #41
0
 def ShowImage(self,Image,Xstart,Ystart):
     """Set buffer to value of Python Imaging Library image."""
     """Write display buffer to physical display"""
     imwidth, imheight = Image.size
     if imwidth != self.width or imheight != self.height:
         raise ValueError('Image must be same dimensions as display \
             ({0}x{1}).' .format(self.width, self.height))
     img = np.asarray(Image)
     pix = np.zeros((self.width,self.height,2), dtype = np.uint8)
     pix[...,[0]] = np.add(np.bitwise_and(img[...,[0]],0xF8),np.right_shift(img[...,[1]],5))
     pix[...,[1]] = np.add(np.bitwise_and(np.left_shift(img[...,[1]],3),0xE0),np.right_shift(img[...,[2]],3))
     pix = pix.flatten().tolist()
     self.SetWindows ( 0, 0, self.width, self.height)
     GPIO.output(self._dc,GPIO.HIGH)
     for i in range(0,len(pix),4096):
         self._spi.writebytes(pix[i:i+4096])		
Example #42
0
def get_head(all_head):
    '''
    Get sequence and channel numbers from the data
    '''
    pattern = np.zeros(4096, dtype=np.uint16)
    seq = np.zeros(4096, dtype=np.uint64)
    for i in range(16):
        header = all_head[i * 256:i * 256 + 8]
        chan = 0
        for ii in range(8):
            chan += np.left_shift(header[ii], ii * 8)
        cstart = np.right_shift(((chan) & 0x3fff), 2)
        seq[i * 256:(i + 1) * 256] = np.right_shift(chan, 10)
        pattern[i * 256:(i + 1) * 256] = list(range(cstart, cstart + 256))
        #print(numba.typeof(seq),numba.typeof(pattern),numba.typeof(all_head))
    return seq, pattern
Example #43
0
    def compact_low_bit_data_to_8_bit(data: np.ndarray, bit_width: int):
        if data.ndim != 1:
            raise RuntimeError("Flatten data before data compaction.")
        if bit_width == 4:
            high_4_bits_data = data[::2]
            low_4_bits_data = data[1::2]
            if len(high_4_bits_data) > len(low_4_bits_data):
                low_4_bits_data = np.append(low_4_bits_data, 0)
            high_4_bits_data = np.left_shift(high_4_bits_data, 4)
            low_4_bits_data = np.bitwise_and(low_4_bits_data, 15)
            data = np.bitwise_or(high_4_bits_data, low_4_bits_data)
        else:
            NotImplementedError(
                "Only support compacting 4 bit data to 8 bit data.")

        return data
Example #44
0
 def _read_depth(self, image_file: str):
     depth=cv.imread(image_file,cv.IMREAD_ANYDEPTH)
     #print(['depth min max', depth.min(), depth.max()])
     if 'Princeton' in image_file: #depth.max()>=60000: # bug found, we need to bitshift depth.
         #print(['depth max=', depth.max()])
         depth=np.bitwise_or(np.right_shift(depth,3),np.left_shift(depth,13))
     #print(['_read_depth', depth.max(), depth.mean(), depth.std()])
     depth_hole=0
     depth_max=8
     depth=depth/1000.0
     depth[depth>=depth_max]=depth_max
     #depth[depth==depth_hole]=depth_max
     #depth=depth_max-depth
     depth=np.uint8(depth/depth_max*255.0)
     #depth=np.log(depth+1)
     return depth
Example #45
0
def translate_dq(ref_hdul):

    dq = ref_hdul['DQ'].data.astype(np.uint32)
    expected_dq = np.zeros_like(dq)
    for row in ref_hdul['DQ_DEF'].data:
        try:
            # find which pixels have the bit set
            flagged = (np.bitwise_and(1, np.right_shift(dq, row['BIT'])))
            # shift them to the correct bit for PIXELDQ
            flagged = np.left_shift(flagged, dq_dict[row['NAME']])
            # propagate into the PIXELDQ extension
            expected_dq = np.bitwise_or(expected_dq, flagged)
        except KeyError:
            pass
            # print("No DQ mnemonic "+row['NAME'])
    return expected_dq
Example #46
0
def reg16_to_value(reg_dict, reg0, reg1):
    """
    Convert the input two 8 bit registers to a single 16bit value 

    Parameters
    ----------
    reg_dict : dict, the dictionary that contains all the register information 
    reg0 : str, the name of the register in the dictionary of the low value 
    reg1 : str, the name of the register in the dictionary of the high value 

    Returns 
    ----------
    value : int, the combined 16bit value 
    """
    value = np.left_shift(reg_dict[reg1][2], 8) | reg_dict[reg0][2]
    return value
Example #47
0
    def left_shift_byte(cls, byte_x, int_y):
        """
        Simulates a Java << for a byte.

        :param byte_x: expected byte value in python code
        :param int_y: expected int value in python
        :returns: left shift result for, x << y
        :rtype: int
        """
        x = np.int8(
            byte_x)  # converts to signed byte, since byte is signed in java
        y = np.int(int_y)
        z = np.left_shift(x, y)

        # In Java, (byte)128 << 3 produces an int.
        return z.item()
Example #48
0
    def fun(dtype, fs, ch, A=None):
        N = int(fs * T)
        t = np.arange(N).reshape((N, 1)) / fs
        f0 = np.tile(np.array(((1001, ), )), (1, ch))
        C = np.cos(2 * np.pi * (f0 * t + np.random.rand(1, ch)))
        if ch == 1:
            C = C.ravel()
        if A is None:
            A = xmax(dtype)

        x = (A * C).astype(dtype)

        if dtype == np.int32:
            x = np.left_shift(np.right_shift(x, 8), 8)

        return x
Example #49
0
def extract(chr_rom, tile):
    """
    Extracts a single 8x8 image from CHR ROM as a 8x8 numpy array.

    Args:
        chr_rom (array): The 4096 numpy array representing CHR ROM
        tile (int): The tile number to extract

    Returns:
        array: The 8x8 image from CHR ROM.
    """
    address = tile * 16
    plane1 = numpy.unpackbits(chr_rom[address:address + 8])
    plane2 = numpy.unpackbits(chr_rom[address + 8:address + 16])
    plane2 = numpy.left_shift(plane2, 1)
    return numpy.bitwise_or(plane1, plane2).reshape((8, 8))
	def LCD_ShowImage(self,Image,Xstart,Ystart):
		if (Image == None):
			return
		imwidth, imheight = Image.size
		if imwidth != self.width or imheight != self.height:
			raise ValueError('Image must be same dimensions as display \
				({0}x{1}).' .format(self.width, self.height))
		img = np.asarray(Image)
		pix = np.zeros((self.width,self.height,2), dtype = np.uint8)
		pix[...,[0]] = np.add(np.bitwise_and(img[...,[0]],0xF8),np.right_shift(img[...,[1]],5))
		pix[...,[1]] = np.add(np.bitwise_and(np.left_shift(img[...,[1]],3),0xE0),np.right_shift(img[...,[2]],3))
		pix = pix.flatten().tolist()
		self.LCD_SetWindows(0, 0, self.width , self.height)
		GPIO.output(LCD_Config.LCD_DC_PIN, GPIO.HIGH)
		for i in range(0,len(pix),4096):
			LCD_Config.SPI_Write_Byte(pix[i:i+4096])
Example #51
0
def Quantize1D(signal, numBits):

    if (type(signal[0]) != np.uint8):
        tmpSig = (signal * 255).astype('uint8')
    else:
        tmpSig = signal

    quantized = np.right_shift(tmpSig, 8 - numBits)

    quantized = np.left_shift(quantized, 8 - numBits)

    quantized += np.floor(pow(2, (8 - numBits) - 1)).astype('uint8')
    if (type(signal[0]) != np.uint8):
        return quantized.astype('float64') / 255
    else:
        return quantized
    def testShiftsWithPositiveLHS(self):
        dtype_list = [
            np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16,
            np.uint32, np.uint64
        ]

        with self.test_session(use_gpu=True) as sess:
            for dtype in dtype_list:
                lhs = np.array([0, 5, 3, 14], dtype=dtype)
                rhs = np.array([5, 0, 7, 3], dtype=dtype)
                left_shift_result, right_shift_result = sess.run([
                    bitwise_ops.left_shift(lhs, rhs),
                    bitwise_ops.right_shift(lhs, rhs)
                ])
                self.assertAllEqual(left_shift_result, np.left_shift(lhs, rhs))
                self.assertAllEqual(right_shift_result,
                                    np.right_shift(lhs, rhs))
Example #53
0
def filter_image(img, image_filter):
    print ('filtering')
    print (img.size)
    # Convert to BW
    img_grey = img.convert('L')
    imgnp = np.asarray(img_grey)

    # Gaussian + Median Filters
    imgnp = ndimage.gaussian_filter(imgnp, 2)
    imgnp = [np.left_shift(np.right_shift(x, 6), 7) for x in imgnp]
    imgnp = ndimage.median_filter(imgnp, 3)

    if cfg["DEF_INVERT"] is True:
        imgnp = np.invert(imgnp)

    print ((imgnp[0]))
    return imgnp
Example #54
0
def at_norm(val, norm):
    if isinstance(norm, np.ndarray):
        if np.any(norm < 0):
            raise ValueError("negative normalization")
        if Rounding.DO_ROUNDING:
            return (val + np.left_shift(1, norm - 1, dtype=val.dtype)) >> norm
            # broadcast = np.broadcast(val, norm)
            # res = np.empty(broadcast.shape, dtype=val.dtype)
            # res.flat = [(v + (1 << n - 1)) >> n if n > 0 else v for v, n in broadcast]
            # return res
        return val >> norm
    else:
        if norm < 0:
            raise ValueError("negative normalization")
        if Rounding.DO_ROUNDING and norm > 0:
            return (val + (1 << (norm - 1))) >> norm
        return val >> norm
def export_wav_using_shift(samples, sound, is_right_shift, shift_val, figure_use, figure_no, figure_rate, to_file):
    if is_right_shift:
        shifted_samples = np.right_shift(samples, shift_val)
    else:
        shifted_samples = np.left_shift(samples, shift_val)

    # figure - new
    if figure_use == True:
        # plot amplitude (or loudness) over time
        time = np.arange(0, len(shifted_samples), 1) / figure_rate
        global_figure_no = print_figure(figure_no, time, shifted_samples, "Time (s)", "Amplitude")

    # now you have to convert back to an array.array
    shifted_samples_array = array.array(sound.array_type, shifted_samples)
    new_sound = sound._spawn(shifted_samples_array)
    new_sound.export(to_file, format="wav")
    return
Example #56
0
 def image(self, Image):
     imwidth, imheight = Image.size
     if imwidth != self.width or imheight != self.height:
         raise ValueError('Image must be same dimensions as display \
             ({0}x{1}).'.format(self.width, self.height))
     img = np.asarray(Image)
     pix = np.zeros((self.width, self.height, 2), dtype=np.uint8)
     pix[..., [0]] = np.add(np.bitwise_and(img[..., [0]], 0xF8),
                            np.right_shift(img[..., [1]], 5))
     pix[..., [1]] = np.add(
         np.bitwise_and(np.left_shift(img[..., [1]], 3), 0xE0),
         np.right_shift(img[..., [2]], 3))
     pix = pix.flatten().tolist()
     self.set_window(0, 0, self.width, self.height)
     GPIO.output(self._dc, GPIO.HIGH)
     for i in range(0, len(pix), 4096):
         self._spi.writebytes(pix[i:i + 4096])
Example #57
0
def bitwise_left_shift(x1: Array, x2: Array, /) -> Array:
    """
    Array API compatible wrapper for :py:func:`np.left_shift <numpy.left_shift>`.

    See its docstring for more information.
    """
    if x1.dtype not in _integer_dtypes or x2.dtype not in _integer_dtypes:
        raise TypeError(
            "Only integer dtypes are allowed in bitwise_left_shift")
    # Call result type here just to raise on disallowed type combinations
    _result_type(x1.dtype, x2.dtype)
    x1, x2 = Array._normalize_two_args(x1, x2)
    # Note: bitwise_left_shift is only defined for x2 nonnegative.
    if np.any(x2._array < 0):
        raise ValueError(
            "bitwise_left_shift(x1, x2) is only defined for x2 >= 0")
    return Array._new(np.left_shift(x1._array, x2._array))
Example #58
0
def test_overflow_signed(b):
    x = np.arange(-2**(b + 2), 2**(b + 2), dtype=np.int32)

    # compute check values
    b2 = 2**b
    z = x % b2
    zmask = np.right_shift(x, b) % 2  # sign bit, the b-th bit

    # if the sign bit is set, subtract 2**b to make it negative
    z -= np.left_shift(zmask, b)

    # compute whether it's overflowed
    q = (x < -b2) | (x >= b2)

    y, o = overflow_signed(x, bits=b)
    assert np.array_equal(y, z)
    assert np.array_equal(o, q)
Example #59
0
def loadDepthMap(base_dir,base_labels,base_bbox,base_centers,idx,return_bbox=False,return_aug=False,aug_img=None):
    """
    Read a depth-map
    :param filename: file name to load
    :return: image data of depth image
    """
    file = 'depth_1_{}.png'.format(str(idx).zfill(7))
    filename = os.path.join(base_dir,file)
#    print(filename)
#    img = cv2.imread(filename)
    img = Image.open(filename)
    r, g, b = img.split()
    r = np.asarray(r, np.int32)
    g = np.asarray(g, np.int32)
    b = np.asarray(b, np.int32)
    dpt = np.bitwise_or(np.left_shift(g, 8), b)
    imgdata = np.asarray(dpt, np.float32)
    idx= max(idx-1,0)
#    width, height, channels = img.shape
#    print(img.shape)
    center = base_centers[idx,:]
    left,right,top,bottom = base_bbox[idx,:]
    
    imCrop = imgdata.copy()[int(top):int(bottom), int(left):int(right)] # image crop

    imgResize = cv2.resize(imCrop, (cropWidth, cropHeight), interpolation=cv2.INTER_NEAREST)
    imgResize = np.asarray(imgResize,dtype = 'float32')
#    imgResize = imgResize*-1
    imgResize[np.where(imgResize >= int(center[2]) + depth_thres)] = int(center[2])
    imgResize[np.where(imgResize <= int(center[2]) - depth_thres)] = int(center[2])
#    
    imgResize = (imgResize - int(center[2]))
    
    # Normalize image
#    r = np.max(imgResize) - np.min(imgResize)
#    imgResize = imgResize - np.min(imgResize)
#    imgResize = imgResize*255 / r
#    imgResize = imgResize.astype(int)
    joints = resizeJoints(base_labels[:,idx,:].copy(),left,right,top,bottom,center)
    if return_aug:
        imgResize = aug_img
    if return_bbox:
        return imgResize,joints,img,(left,right,top,bottom),center
    else:
        return imgResize, joints
Example #60
0
    def loadDepthMap(self, filename):
        """
        Read a depth-map
        :param filename: file name to load
        :return: image data of depth image
        """

        img = Image.open(filename)
        # top 8 bits of depth are packed into green channel and lower 8 bits into blue
        assert len(img.getbands()) == 3
        r, g, b = img.split()
        r = np.asarray(r, np.int32)
        g = np.asarray(g, np.int32)
        b = np.asarray(b, np.int32)
        dpt = np.bitwise_or(np.left_shift(g, 8), b)
        imgdata = np.asarray(dpt, np.float32)

        return imgdata