def toScalar(self): """ Returns a scalar if the matrix consists of only one cell """ redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) if self.shape[0] != 1 or self.shape[1] != 1: raise exceptions.MatrixOperationException('Cannot convert a matrix with more than one column and row to a scalar', 'MATRIX2SCALAR') return redwrap.get_block(self.block_name(0,0))[0,0]
def get_cell_value(self, row, col): """ Returns the value of a single matrix cell """ redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) block_row = int(math.floor(row / self.__block_size)) block_col = int(math.floor(col / self.__block_size)) offset_row = row % self.__block_size offset_col = col % self.__block_size block = redwrap.get_block(self.block_name(block_row, block_col)) return block[offset_row, offset_col]
def print_blocks(self): """ Prints each block """ redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) for row in range(0, self.row_blocks()): for col in range(0, self.col_blocks()): n = redwrap.get_block(self.block_name(row, col)) print self.block_name(row, col) print str(n) print '----'
def redis2file(name, redis, key_manager, file_format): rw = RedisWrapper(redis, key_manager) info = redis.hgetall(const.INFO_FORMAT.format(name)) rows = int(info['rows']) cols = int(info['cols']) block_size = int(info['block_size']) for row in range(0, rows / block_size): for col in range(0, cols / block_size): file = file_format.format(row, col) matrix = rw.get_block(key_manager.get_block_name(name, row, col)) numpy.savetxt(file, matrix, delimiter=';', fmt='%f')
def get_numpy_matrix(self): """ Concatenates all blocks of this matrix and returns one big numpy matrix """ m = None redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) for row in range(0,self.row_blocks()): b = redwrap.get_block(self.block_name(row, 0)) #print self.block_name(row, 0) for col in range(1,self.col_blocks()): if row == 0 and col == 0: continue #print self.block_name(row, col) #print '---' n = redwrap.get_block(self.block_name(row, col)) b = numpy.concatenate((b, n), axis=1) if m is None: m = b else: m = numpy.concatenate((m, b)) return m
def set_cell_value(self, row, col, val): """ Sets the value of a single matrix cell """ redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) block_row = int(math.floor(row / self.__block_size)) block_col = int(math.floor(col / self.__block_size)) offset_row = row % self.__block_size offset_col = col % self.__block_size block_name = self.block_name(block_row, block_col) block = redwrap.get_block(block_name) block[offset_row, offset_col] = val redwrap.create_block(block_name, block)
def slice(self, row, num_rows, col, num_cols, result_name=None): if result_name == None: result_name = MatrixFactory.getRandomMatrixName() # Check if given values are valid if row + num_rows > self.__rows or (num_rows < 0 and row + num_rows < 0): raise Exception('Row index out of bounds') if col + num_cols > self.__cols or (num_cols < 0 and col + num_cols < 0): raise Exception('Column index out of bounds') # Handle negative indices if row < 0: row = self.__rows + row if num_rows < 0: row = row + num_rows num_rows = -num_rows if col < 0: col = self.__cols + col if num_cols < 0: col = col + num_cols num_cols = -num_cols redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) row_blocks = num_rows / self.__block_size if num_rows % self.__block_size != 0: row_blocks += 1 col_blocks = num_cols / self.__block_size if num_cols % self.__block_size != 0: col_blocks += 1 # Iterate the blocks of the new slice for r in range(0, row_blocks): for c in range(0, col_blocks): start_row = row + r * self.__block_size end_row = min(row + num_rows, start_row + self.__block_size) start_col = col + c * self.__block_size end_col = min(col + num_cols, start_col + self.__block_size) # Iterate the blocks of the current matrix that intersect with the current block of the new slice # and patch them together a = None for i in range(start_row / self.__block_size, (end_row-1) / self.__block_size + 1): row_b = None for j in range(start_col / self.__block_size, (end_col-1) / self.__block_size + 1): n = redwrap.get_block(self.block_name(i, j)) if row_b == None: row_b = n else: row_b = numpy.concatenate((row_b, n), axis=1) if a == None: a = row_b else: a = numpy.concatenate((a, row_b), axis=0) # Now we have a matrix so big that the whole current block of the new slice as defined by r and c fits in it # Use numpy slicing to get the block from this matrix mr = min(row + self.__block_size, a.shape[0]) mc = min(col + self.__block_size, a.shape[1]) sc = col % self.__block_size sr = row % self.__block_size if r == row_blocks-1 and (row + num_rows) % self.__block_size != 0: mr = min(mr, (row + num_rows) % self.__block_size) if c == col_blocks-1 and (col + num_cols) % self.__block_size != 0: mc = min(mc, (col + num_cols) % self.__block_size) if mr == row and row % self.__block_size != 0: mr += 1 if mc == col and col % self.__block_size != 0: mc += 1 block = a[sr:mr,sc:mc] redwrap.create_block(self.context.key_manager.get_block_name(result_name, r, c), block) return Matrix(num_rows, num_cols, result_name, self.context)
def get_numpy_block(self, row, col): """ Returns a block as numpy matrix """ redwrap = RedisWrapper(self.context.redis_master, self.context.key_manager) return redwrap.get_block(self.block_name(row, col))