Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cytepool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
- Dose response: :class:`DoseAxis`; whether a dose series supports tests in each
cell type, :func:`check_dose_design`, :class:`DoseDesignReport`; which genes
respond to dose in each cell type, :func:`dose_response`,
:class:`DoseResponseResult`; simulated dose series with known curves,
:class:`DoseResponseResult`, with the candidate shapes of its MCP step, the test of
MCP-Mod (multiple comparison procedure and modelling), :class:`CandidateShapes`;
simulated dose series with known curves,
:func:`simulate_dose_series`, :class:`DoseTruth`.

The building blocks, which work on arrays, are in ``cytepool.stats`` (linear and
Expand Down Expand Up @@ -65,6 +67,7 @@ def _installed_version() -> str:

from .activity import Activities, mlm, ulm, weighted_aggregate # noqa: E402
from .composition import CompositionResult, propeller # noqa: E402
from .candidate_shapes import CandidateShapes # noqa: E402
from .cube import Cube, CubeCollection, from_bulk, from_cells # noqa: E402
from .design import Design # noqa: E402
from .differential_expression import ( # noqa: E402
Expand Down Expand Up @@ -184,6 +187,7 @@ def _installed_version() -> str:
"DoseDesignReport",
"dose_response",
"DoseResponseResult",
"CandidateShapes",
"simulate_dose_series",
"DoseTruth",
# The version.
Expand Down
36 changes: 33 additions & 3 deletions cytepool/_doses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The reading of doses that dose axes, dose designs and simulated dose series share.
"""The reading of doses that dose axes, dose designs, candidate shapes and simulated
dose series share.

A dose column of a sample table holds real numbers, finite and at least 0, with 0 for
the vehicle. Distinct doses closer than 1e-8 of the larger are refused as one dose
Expand All @@ -9,13 +10,16 @@
which separate any two doses that the 1e-8 rule keeps apart.
"""

from collections.abc import Mapping
from typing import Any

import numpy as np
import numpy.typing as npt

from ._masked_arrays import Missing, unmasked_array
from ._columns import is_data_frame
from ._masked_arrays import Missing, reject_masked, unmasked_array
from ._messages import close_matches, listing
from .cube import Cube
from .table import Table

FloatArray = npt.NDArray[np.float64]
Expand Down Expand Up @@ -73,6 +77,27 @@ def column_doses(table: Table, column: str) -> FloatArray:
return checked_doses(table[column], f"samples[{column!r}]")


def sample_doses(samples: Any, column: str) -> FloatArray:
"""The checked doses of the dose column of a sample table: a ``Table``, a mapping of
column name to 1-D values, or a pandas DataFrame.

Raises:
TypeError: If ``samples`` is not a table or holds a numpy masked array, or the
column does not hold real numbers.
ValueError: If the table has no such column, or a dose is not finite or is
negative.
"""
reject_masked(samples, "samples", missing="None or NaN")
if not (isinstance(samples, Table | Mapping) or is_data_frame(samples)):
hint = "; pass cube.samples" if isinstance(samples, Cube) else ""
raise TypeError(
"samples must be a Table, a mapping of column names to 1-D values or "
f"a pandas DataFrame, got {type(samples).__name__}{hint}"
)
table = samples if isinstance(samples, Table) else Table(samples)
return column_doses(table, column)


def distinct_doses(doses: FloatArray, unit: str) -> FloatArray:
"""The distinct doses, ascending, refusing two that differ only by rounding.

Expand Down Expand Up @@ -107,10 +132,15 @@ def spline_column(column: str, j: int) -> str:
return f"{column}{SPLINE_SUFFIX}{j}"


def dose_label(dose: float) -> str:
"""A dose as names show it, with 12 significant digits, such as ``0.03``."""
return f"{dose:.12g}"


def dose_group_column(column: str, dose: float) -> str:
"""The name of the design column that is 1 for the samples given a treated dose,
such as ``dose_0.03``."""
return f"{column}_{dose:.12g}"
return f"{column}_{dose_label(dose)}"


def is_axis_column(name: str, column: str) -> bool:
Expand Down
Loading