Return ndarray representing table index by margin.
433744
def data(cls, cube, weighted, prune):
        """"""
        return cls()._data(cube, weighted, prune)
296388
def rst_table(self, array):
        """"""
        # Determine cell width for each column
        cell_dict = {}
        for i, row in enumerate(array):
            for j, val in enumerate(row):
                if j not in cell_dict:
                    cell_dict[j] = []
                cell_dict[j].append(val)
        for item in cell_dict:
            cell_dict[item] = max([len(x) for x in cell_dict[item]]) + 1  # Contains adapted width for each column

        # Format top line
        num_cols = len(array[0])
        form = '+'
        for col in range(num_cols):
            form += (cell_dict[col] + 1) * '-'
            form += '+'
        form += '\n'

        # Format values
        for i, row in enumerate(array):
            form += '| '
            for j, val in enumerate(row):
                cell_width = cell_dict[j]
                form += str(val) + (cell_width - len(val)) * ' ' + '| '
            form.rstrip()
            form += '\n'

            # Seperation lines
            form += '+'
            if i == 0:
                sign = '='
            else:
                sign = '-'
            for col in range(num_cols):
                form += (cell_dict[col] + 1) * sign
                form += '+'
            form += '\n'
        return form
673454
def _arrayize_vectorized_indexer(indexer, shape):
    
    slices = [v for v in indexer.tuple if isinstance(v, slice)]
    if len(slices) == 0:
        return indexer

    arrays = [v for v in indexer.tuple if isinstance(v, np.ndarray)]
    n_dim = arrays[0].ndim if len(arrays) > 0 else 0
    i_dim = 0
    new_key = []
    for v, size in zip(indexer.tuple, shape):
        if isinstance(v, np.ndarray):
            new_key.append(np.reshape(v, v.shape + (1, ) * len(slices)))
        else:  # slice
            shape = ((1,) * (n_dim + i_dim) + (-1,) +
                     (1,) * (len(slices) - i_dim - 1))
            new_key.append(np.arange(*v.indices(size)).reshape(shape))
            i_dim += 1
    return VectorizedIndexer(tuple(new_key))
1241428
def _get_index(n_items, item_size, n):
    """"""
    index = np.arange(n_items)
    index = np.repeat(index, item_size)
    index = index.astype(np.float64)
    assert index.shape == (n,)
    return index
129561
def eigen_table(self):
        """"""
        idx = ["Eigenvalue", "Variability (%)", "Cumulative (%)"]
        table = pd.DataFrame(
            np.array(
                [self.eigenvalues, self.inertia, self.cumulative_inertia]
            ),
            columns=["F%s" % i for i in range(1, self.keep + 1)],
            index=idx,
        )

        return table
664805
def ndim(self):
        """"""
        try:
            return self.__ndim
        except AttributeError:
            ndim = len(self.coord_vectors)
            self.__ndim = ndim
            return ndim
349045
def table2val_matrix(table):
    """"""
    if not is_simpletable(table):
        raise NotSimpleTable("Not able read a cell in the table as a string")
    rows = []
    for tr in table('tr'):
        row = []
        for td in tr('td'):
            td = tdbr2EOL(td)
            try:
                val = td.contents[0]
            except IndexError:
                row.append('')
            else:
                try:
                    val = float(val)
                    row.append(val)
                except ValueError:
                    row.append(val)
        rows.append(row)
    return rows
1292491
def _reduce(self):
        """"""
        ctx = context.cpu()
        if self._stype == 'default':
            block = self.list_data()
            data = ndarray.add_n(*(w.copyto(ctx) for w in block)) / len(block)
        else:
            # fetch all rows for 'row_sparse' param
            all_row_ids = ndarray.arange(0, self.shape[0], dtype='int64', ctx=ctx)
            data = ndarray.zeros(self.shape, stype='row_sparse', ctx=ctx)
            self._trainer._row_sparse_pull(self, data, all_row_ids, full_idx=True)
        return data
30944
def _get_nd_basic_indexing(self, key):
        """"""
        shape = self.shape
        if isinstance(key, integer_types):
            if key > shape[0] - 1:
                raise IndexError(
                    'index {} is out of bounds for axis 0 with size {}'.format(
                        key, shape[0]))
            return self._at(key)
        elif isinstance(key, py_slice):
            if key.step is not None and key.step != 1:
                if key.step == 0:
                    raise ValueError("slice step cannot be zero")
                return op.slice(self, begin=(key.start,), end=(key.stop,), step=(key.step,))
            elif key.start is not None or key.stop is not None:
                return self._slice(key.start, key.stop)
            else:
                return self

        if not isinstance(key, tuple):
            raise ValueError('index=%s must be a slice, or an ineger, or a tuple'
                             ' of slices and integers to use basic indexing, received type=%s'
                             % (str(key), str(type(key))))
        assert len(key) != 0, 'basic index cannot be an empty tuple'
        begin = []
        end = []
        step = []
        kept_axes = []  # axes where slice_i is a slice
        i = -1
        for i, slice_i in enumerate(key):
            if isinstance(slice_i, integer_types):
                begin.append(slice_i)
                end.append(slice_i+1 if slice_i != -1 else self.shape[i])
                step.append(1)
            elif isinstance(slice_i, py_slice):
                if slice_i.step == 0:
                    raise ValueError('basic index=%s cannot have slice=%s with step = 0'
                                     % (str(key), str(slice_i)))
                begin.append(slice_i.start)
                end.append(slice_i.stop)
                step.append(slice_i.step)
                kept_axes.append(i)
            else:
                raise ValueError('basic_indexing does not support slicing with '
                                 'index=%s of type=%s.' % (str(slice_i), str(type(slice_i))))
        kept_axes.extend(range(i+1, len(shape)))
        sliced_nd = op.slice(self, begin, end, step)
        if len(kept_axes) == len(shape):
            return sliced_nd
        # squeeze sliced_shape to remove the axes indexed by integers
        oshape = []
        sliced_shape = sliced_nd.shape
        for axis in kept_axes:
            oshape.append(sliced_shape[axis])
        # if key is a tuple of integers, still need to keep 1 dim
        # while in Numpy, the output will become an value instead of an ndarray
        if len(oshape) == 0:
            oshape.append(1)
        oshape = tuple(oshape)
        assert np.prod(oshape) == np.prod(sliced_shape), 'oshape=%s has different size'\
                                                         ' than sliced_shape=%s'\
                                                         % (oshape, sliced_shape)
        return sliced_nd.reshape(oshape)
230773
def NdarrayToEntry(self, x):
    """"""
    row_counts = []
    for row in x:
      try:
        rc = np.count_nonzero(~np.isnan(row))
        if rc != 0:
          row_counts.append(rc)
      except TypeError:
        try:
          row_counts.append(row.size)
        except AttributeError:
          row_counts.append(1)

    data_type = self.DtypeToType(x.dtype)
    converter = self.DtypeToNumberConverter(x.dtype)
    flattened = x.ravel()
    orig_size = len(flattened)

    # Remove all None and nan values and count how many were removed.
    flattened = flattened[flattened != np.array(None)]
    if converter:
      flattened = converter(flattened)
    if data_type == self.fs_proto.STRING:
      flattened_temp = []
      for x in flattened:
        try:
          if str(x) != 'nan':
            flattened_temp.append(x)
        except UnicodeEncodeError:
          if x.encode('utf-8') != 'nan':
            flattened_temp.append(x)
      flattened = flattened_temp
    else:
      flattened = flattened[~np.isnan(flattened)].tolist()
    missing = orig_size - len(flattened)
    return {
        'vals': flattened,
        'counts': row_counts,
        'missing': missing,
        'type': data_type
    }
68052
def toSparse(self):
        """"""
        if self.isTransposed:
            values = np.ravel(self.toArray(), order='F')
        else:
            values = self.values
        indices = np.nonzero(values)[0]
        colCounts = np.bincount(indices // self.numRows)
        colPtrs = np.cumsum(np.hstack(
            (0, colCounts, np.zeros(self.numCols - colCounts.size))))
        values = values[indices]
        rowIndices = indices % self.numRows

        return SparseMatrix(self.numRows, self.numCols, colPtrs, rowIndices, values)
430908
def _ndarray_assoc(arr, k, v):
    ''
    arr = np.array(arr)
    arr[k] = v
    arr.setflags(write=False)
    return arr
435024
def iloc(cls, dataset, index):
        
        rows, cols = index
        scalar = False
        if isinstance(cols, slice):
            cols = [d.name for d in dataset.dimensions()][cols]
        elif np.isscalar(cols):
            scalar = np.isscalar(rows)
            cols = [dataset.get_dimension(cols).name]
        else:
            cols = [dataset.get_dimension(d).name for d in index[1]]
        if np.isscalar(rows):
            rows = [rows]

        data = OrderedDict()
        for c in cols:
            data[c] = dataset.data[c].compute().iloc[rows].values
        if scalar:
            return data[cols[0]][0]
        return tuple(data.values())
1048945
def index(self, sort=True):
        """"""
        from casacore.tables import tableindex
        return tableindex(self._table, [self._column], sort)
1176573
def _vector(x, type='row'):
    """"""
    if isinstance(x, (list, tuple)):
        x = np.array(x, dtype=np.float32)
    elif not isinstance(x, np.ndarray):
        x = np.array([x], dtype=np.float32)
    assert x.ndim == 1
    if type == 'column':
        x = x[:, None]
    return x
624760
def select_data(self, iteration_indices):
        """"""
        dat = self
        iteridx = iteration_indices
        dat.f = dat.f[np.where([x in iteridx for x in dat.f[:, 0]])[0], :]
        dat.D = dat.D[np.where([x in iteridx for x in dat.D[:, 0]])[0], :]
        try:
            iteridx = list(iteridx)
            iteridx.append(iteridx[-1])  # last entry is artificial
        except:
            pass
        dat.std = dat.std[np.where([x in iteridx
                                    for x in dat.std[:, 0]])[0], :]
        dat.xmean = dat.xmean[np.where([x in iteridx
                                        for x in dat.xmean[:, 0]])[0], :]
        try:
            dat.xrecent = dat.x[np.where([x in iteridx for x in
                                          dat.xrecent[:, 0]])[0], :]
        except AttributeError:
            pass
        try:
            dat.corrspec = dat.x[np.where([x in iteridx for x in
                                           dat.corrspec[:, 0]])[0], :]
        except AttributeError:
            pass
279046
def X(self) -> Optional[Union[np.ndarray, sparse.spmatrix, ArrayView]]:
        """"""
        if self.isbacked:
            if not self.file.isopen: self.file.open()
            X = self.file['X']
            if self.isview:
                X = X[self._oidx, self._vidx]
            return X
        else:
            if self.n_obs == 1 and self.n_vars == 1:
                return self._X[0, 0]
            elif self.n_obs == 1 or self.n_vars == 1:
                X = self._X
                if issparse(self._X): X = self._X.toarray()
                return X.flatten()
            else:
                return self._X