def loadlns(fname: Union[str, Path], mode: str = 'r', strip: bool = True) -> List[str]: checkInputFile(fname) with open(fname, mode) as f: lns = f.readlines() lns = smap(lns, lambda x: x.rstrip('\n')) if strip: lns = drop(lns, lambda x: x.strip() == '') return lns
def onload(self, removefile: bool = False) -> Table: if not isinstance(self._dmatx, np.memmap): logging.warning('Table not offloaded, skip'); return self checkInputFile(self._memmap.file) mdmatx = np.memmap(self._memmap.file, dtype = self._memmap.dtype, mode = 'r', shape = self._memmap.shape) self._dmatx = np.array(mdmatx) del mdmatx if removefile: self._memmap.file.unlink() self._memmap = None return self
def load(fname: Union[str, Path], *, mode: str = 'r', skips: Optional[int] = None, comment: Optional[str] = None, strip: bool = False, **kwargs: Any) -> List[List[str]]: checkInputFile(fname) with open(fname, mode) as f: if available(skips) and skips > 0: for _ in range(skips): next(f) tb = csv.reader(f, **kwargs) if strip: tb = drop(tb, lambda x: len(x) == 0 or (len(x) == 1 and x[0].strip() == '')) if available(comment): tb = drop(tb, lambda x: x[0].startswith(comment)) tb = l(tb) return tb
def loadhdf(cls, fname: Union[str, Path]) -> Table: checkInputFile(fname) hdf = ptb.open_file(fname, mode = 'r') darr = hdf.root.DataMatx.read() if darr.dtype.kind == 'S': darr = np.array(darr, dtype = str) meta = [(n, getattr(hdf.root.DataMatx.attrs, n)) for n in hdf.root.DataMatx.attrs._f_list('user')] rnam = np.array(hdf.root.RowNames.read(), dtype = str) if hasattr(hdf.root, 'RowNames') else None cnam = np.array(hdf.root.ColNames.read(), dtype = str) if hasattr(hdf.root, 'ColNames') else None ridx = StructuredArray.fromhtable(hdf.root.RowIndex) if hasattr(hdf.root, 'RowIndex') else None cidx = StructuredArray.fromhtable(hdf.root.ColIndex) if hasattr(hdf.root, 'ColIndex') else None hdf.close() return Table(darr, rownames = rnam, colnames = cnam, rowindex = ridx, colindex = cidx, metadata = meta)
def loadrdata(cls, fname: Union[str, Path], dataobj: str, *, ridxobj: Optional[str] = None, cidxobj: Optional[str] = None, transposed: bool = True) -> Table: if missing(rw): raise RuntimeError('RWrapper not available for this installation') checkInputFile(fname) rw.r.load(fname) dm, rn, cn = np.array(rw.r[dataobj]), rw.run(f'rownames({dataobj})'), rw.run(f'colnames({dataobj})') # stupid numpy conversion rn = None if rn is rw.null else np.array(rn) cn = None if cn is rw.null else np.array(cn) def _parseidx(iname): idx = rw.r[iname] return zip(idx.dtype.names, zip(*idx)) ri = _parseidx(ridxobj) if available(ridxobj) else None ci = _parseidx(cidxobj) if available(cidxobj) else None if transposed: dm, rn, cn, ri, ci = dm.T, cn, rn, ci, ri ntab = Table(dm, rownames = rn, colnames = cn, rowindex = ri, colindex = ci) return ntab
def load(fname: Union[str, Path], mode: str = 'r') -> str: checkInputFile(fname) with open(fname, mode) as f: txt = f.read() return txt
def loadhdf(cls, fname: Union[str, Path]) -> StructuredArray: checkInputFile(fname) with tb.open_file(fname, mode='r') as hdf: arr = cls.fromhtable(hdf.root.StructuredArray) return arr