<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def top_segment_proportions(mtx, ns):
 """ Calculates total percentage of counts in top ns genes. Parameters mtx : `Union[np.array, sparse.spmatrix]` Matrix, where each row is a sample, each column a feature. ns : `Container[Int]` Positions to calculate cumulative proportion at. Values are considered 1-indexed, e.g. `ns=[50]` will calculate cumulative proportion up to the 50th most expressed gene. """

    # Pretty much just does dispatch
    if not (max(ns) <= mtx.shape[1] and min(ns) > 0):
        raise IndexError("Positions outside range of features.")
    if issparse(mtx):
        if not isspmatrix_csr(mtx):
            mtx = csr_matrix(mtx)
        return top_segment_proportions_sparse_csr(mtx.data, mtx.indptr,
                                                  np.array(ns, dtype=np.int))
    else:
        return top_segment_proportions_dense(mtx, ns)