Skip to content

by_type

lab.connectome.by_type

Collect the values of a table column for each cell type.

in_order(table, order, *, by='type')

Sort the rows of a table by the position of their label.

Parameters:

Name Type Description Default
table DataFrame

Has the column named by by.

required
order sequence of str

The labels in the order the rows should come.

required
by str

Column that holds each row's label.

"type"

Returns:

Type Description
DataFrame

The rows sorted by the position of their by value in order; rows with the same value keep their order, and a value not in order goes last.

Examples:

>>> import polars as pl
>>> table = pl.DataFrame({"type": ["B", "A", "C"], "n": [1, 2, 3]})
>>> in_order(table, ["A", "B"])["type"].to_list()
['A', 'B', 'C']
Source code in lab/connectome/by_type.py
def in_order(
    table: pl.DataFrame, order: Sequence[str], *, by: str = "type"
) -> pl.DataFrame:
    """Sort the rows of a table by the position of their label.

    Parameters
    ----------
    table : polars.DataFrame
        Has the column named by `by`.
    order : sequence of str
        The labels in the order the rows should come.
    by : str, default "type"
        Column that holds each row's label.

    Returns
    -------
    polars.DataFrame
        The rows sorted by the position of their `by` value in
        `order`; rows with the same value keep their order, and a
        value not in `order` goes last.

    Examples
    --------
    >>> import polars as pl
    >>> table = pl.DataFrame({"type": ["B", "A", "C"], "n": [1, 2, 3]})
    >>> in_order(table, ["A", "B"])["type"].to_list()
    ['A', 'B', 'C']
    """
    return table.sort(
        pl.col(by).replace_strict(
            {value: n for n, value in enumerate(order)}, default=len(order)
        ),
        maintain_order=True,
    )

values_by_type(table, column, types)

Collect the values of a column among the rows of each type.

Parameters:

Name Type Description Default
table DataFrame

Has a column type and the column named by column.

required
column str

Column whose values are collected.

required
types sequence of str

The types to collect, in order.

required

Returns:

Type Description
dict of str to polars.Series

One series per type; a type with no row has an empty series.

See Also

lab.connectome.overlap.sets_by_type : The same, as sets.

Examples:

>>> import polars as pl
>>> table = pl.DataFrame({"type": ["P", "P", "Q"], "s": [3, 5, 4]})
>>> by_type = values_by_type(table, "s", ["P", "Q"])
>>> {t: values.to_list() for t, values in by_type.items()}
{'P': [3, 5], 'Q': [4]}
Source code in lab/connectome/by_type.py
def values_by_type(
    table: pl.DataFrame, column: str, types: Sequence[str]
) -> dict[str, pl.Series]:
    """Collect the values of a column among the rows of each type.

    Parameters
    ----------
    table : polars.DataFrame
        Has a column ``type`` and the column named by `column`.
    column : str
        Column whose values are collected.
    types : sequence of str
        The types to collect, in order.

    Returns
    -------
    dict of str to polars.Series
        One series per type; a type with no row has an empty series.

    See Also
    --------
    lab.connectome.overlap.sets_by_type : The same, as sets.

    Examples
    --------
    >>> import polars as pl
    >>> table = pl.DataFrame({"type": ["P", "P", "Q"], "s": [3, 5, 4]})
    >>> by_type = values_by_type(table, "s", ["P", "Q"])
    >>> {t: values.to_list() for t, values in by_type.items()}
    {'P': [3, 5], 'Q': [4]}
    """
    return {t: table.filter(pl.col("type") == t)[column] for t in types}