File: _transform.pyx

package info (click to toggle)
python-biom-format 2.1.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 51,820 kB
  • sloc: python: 12,757; makefile: 155; sh: 79
file content (51 lines) | stat: -rw-r--r-- 1,778 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -----------------------------------------------------------------------------
# Copyright (c) 2011-2017, The BIOM Format Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# -----------------------------------------------------------------------------

from __future__ import division

import numpy as np
cimport numpy as cnp


def _transform(arr, ids, metadata, function, axis):
    """Transform non-zero values of a sparse array, in place.

    Only non null values can be modified: the density of the sparse
    matrix can't increase. However, zeroing values is fine.

    Parameters
    ----------
    arr : csr_matrix or csc_matrix
        Matrix whose rows or columns (respectively) are to be
        transformed.
    ids : 1D array_like
        ids along the given axis.
    metadata : 1D array_like or None
        metadata along the given axis.
    function : function
        A function that takes three values: an array of nonzero values
        for each column or row of `arr`, an id string and a metadata
        dictionary. It must return an array of transformed values.
    axis : int
        Transform rows of `arr` if 0, columns if 1.
    """
    cdef:
        Py_ssize_t n, row_or_col
        cnp.ndarray[cnp.int32_t, ndim=1] indptr = arr.indptr
        cnp.ndarray[cnp.float64_t, ndim=1] data = arr.data
        cdef cnp.int32_t start, end

    if metadata is None:
        metadata = (None,) * len(ids)

    n = arr.shape[axis]
    for row_or_col in range(n):
        start, end = indptr[row_or_col], indptr[row_or_col+1]
        id_ = ids[row_or_col]
        md = metadata[row_or_col]
        data[start:end] = function(data[start:end], id_, md)