def __init__(self, scrappy_matrix_obj, slice): """Container to provide view of `ScrappyMatrix`. Won't free underlying data upon garbage collection. :param scrappy_matrix_obj: `ScrappyMatrix` instance. :param slice: slice """ n_elems = scrappy_matrix_obj.shape[0] start = slice.start if slice.start is not None else 0 stop = slice.stop if slice.stop is not None else n_elems if slice.step is not None and slice.step != 1: raise ValueError('stride must be 1 for a view') if start > stop: raise IndexError('start should be smaller than stop') for i in start, stop: if i > n_elems: raise IndexError( 'index {} is out of bounds for axis 0 with size {}'.format( i, n_elems)) attrs = ['nr', 'nrq', 'nc', 'stride', 'data'] init_data = [getattr(scrappy_matrix_obj._data, a) for a in attrs] self._data = ffi.new("scrappie_matrix", init=init_data) self._data.data.f += start * self._data.stride self._data.nc = stop - start
def _decode_post(post, stay_pen=0.0, skip_pen=0.0, local_pen=2.0, use_slip=False): """Decode a posterior using Viterbi algorithm for transducer. :param post: a `ScrappyMatrix` containing transducer posteriors. :param stay_pen: penalty for staying. :param skip_pen: penalty for skipping a base. :param local_pen: penalty for local basecalling. :param use_slip: allow slipping (movement more than 2 bases). :returns: tuple containing (call, score, call positions per raw block). """ nblock, nstate = post.shape path = ffi.new("int[{}]".format(nblock + 1)) score = lib.decode_transducer(post.data(), stay_pen, skip_pen, local_pen, path, use_slip) pos = np.zeros(nblock + 1, dtype=np.int32) p_pos = ffi.cast("int *", pos.ctypes.data) basecall = lib.overlapper(path, nblock + 1, nstate - 1, p_pos) return ffi.string(basecall).decode(), score, pos
def _decode_post_crf(post): """Decode a posterior using Viterbi algorithm for conditional random field. :param post: a `ScrappyMatrix` containing CRF transitions. :returns: tuple containing (basecall, score, call positions per raw data block). """ nblock, nstate = post.shape path = ffi.new("int[{}]".format(nblock + 1)) score = lib.decode_crf(post.data(), path) pos = np.ascontiguousarray(np.zeros(nblock + 1, dtype=np.int32)) p_pos = ffi.cast("int *", ffi.from_buffer(pos)) basecall = lib.crfpath_to_basecall(path, nblock, p_pos) return ffi.string(basecall).decode(), score, pos
def __init__(self, data, start=0, end=None): """Representation of a scrappie `raw_table`. :param data: `nd.array` containing raw data. ..note:: The class stores a reference to a contiguous numpy array of the correct type to be passed to the extension library. The class provides safety against the original data being garbage collected. To obtain an up-to-date (possibly trimmed and scaled) copy of the data use `raw_table.data(as_numpy=True)`. """ if end is None: end = len(data) self._data = np.ascontiguousarray(data.astype(ftype, order='C', copy=True)) rt = ffi.new('raw_table *') rt.n = len(self._data) rt.start = start rt.end = end rt.raw = ffi.cast("float *", ffi.from_buffer(self._data)) self._rt = rt[0]