skbio.table.Table.transform#
- Table.transform(f, axis='sample', inplace=True)[source]#
Iterate over axis, applying a function f to each vector.
Only non null values can be modified and the density of the table can’t increase. However, zeroing values is fine.
- Parameters:
- ffunction(data, id, metadata) -> new data
A function that takes three values: an array of nonzero values corresponding to each observation or sample, an observation or sample id, and an observation or sample metadata entry. It must return an array of transformed values that replace the original values.
- axis{‘sample’, ‘observation’}, optional
The axis to operate on. Can be “sample” or “observation”.
- inplacebool, optional
Defaults to
True
. Whether to return a new table or modify itself.
- Returns:
- biom.Table
Returns itself if inplace, else returns a new transformed table.
- Raises:
- UnknownAxisError
If provided an unrecognized axis.
Examples
>>> import numpy as np >>> from biom.table import Table
Create a 2x3 table
>>> data = np.asarray([[0, 0, 1], [1, 3, 42]]) >>> table = Table(data, ['O1', 'O2'], ['S1', 'S2', 'S3'], ... [{'foo': 'bar'}, {'x': 'y'}], None) >>> print(table) # Constructed from biom file #OTU ID S1 S2 S3 O1 0.0 0.0 1.0 O2 1.0 3.0 42.0
Create a transform function
>>> f = lambda data, id_, md: data / 2
Transform to a new table on samples
>>> table2 = table.transform(f, 'sample', False) >>> print(table2) # Constructed from biom file #OTU ID S1 S2 S3 O1 0.0 0.0 0.5 O2 0.5 1.5 21.0
table hasn’t changed
>>> print(table) # Constructed from biom file #OTU ID S1 S2 S3 O1 0.0 0.0 1.0 O2 1.0 3.0 42.0
Tranform in place on observations
>>> table3 = table.transform(f, 'observation', True)
table is different now
>>> print(table) # Constructed from biom file #OTU ID S1 S2 S3 O1 0.0 0.0 0.5 O2 0.5 1.5 21.0
but the table returned (table3) is the same as table
>>> print(table3) # Constructed from biom file #OTU ID S1 S2 S3 O1 0.0 0.0 0.5 O2 0.5 1.5 21.0