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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ inventory's, not the Stage 2 MC-1/MC-2 of the experiment description.
A `next_fov` table without `uninformative` and a `final` table without `warning_list`
are no longer accepted. The joined `warnings` text is no longer read as a fallback.

### Fixed

- **#642.** `html_report`, `plots` and `plots_of_data` with `group_cols=("condition",
"repeat_group")`, the groups of MorphoFeature's notebooks, no longer raise "group_cols ...
hold ['repeat_group'], which the figure does not place". The figures place `repeat_group`
as their biological-repeat level (`_repeat_groups.repeat_level`), labelled by its values;
the pairwise matrix pairs by it and the HTML report's interactive figures read it. Nothing
falls back to the folder label `biological_repeat`; `group_cols` naming both columns are
refused, since a figure has one repeat level. The `biological_repeat` path is unchanged.

### Changed

- **Q-MC-a.** `microscopist_report` refuses (`ValueError`) a result that records no acquisition
Expand Down
19 changes: 10 additions & 9 deletions docs/common/function_index.jsonl

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions docs/report/figures.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ figures = plots(objects, fovs, setup, features=["YAPTAZ_Ratio", "Nuclei_Area"])
```

PlotsOfData-style figures (`morphocoverage.plots`), one per feature:
within each condition the biological repeats (the `biological_repeat` column) sit side
by side, labelled above their cluster, so that a difference between
within each condition the biological repeats sit side by side (the `biological_repeat`
column, or the canonical `repeat_group` when the analysis groups name it, as MorphoFeature's
`("condition", "repeat_group")` do: the repeats are then labelled by their repeat groups,
never by folder label, and the pairwise matrix pairs them by repeat group; `group_cols`
naming both columns are refused, since a figure has one repeat level), labelled above their
cluster, so that a difference between
conditions can be read against the spread between repeats; each condition is
only as wide as the repeats it holds, so repeats imaged for other conditions
take no room. The figure's title is the feature; what it shows is its legend, at
Expand Down
25 changes: 13 additions & 12 deletions src/morphocoverage/_plots_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from matplotlib.axes import Axes

from ._palette import MUTED
from ._repeat_groups import REPEAT_LEVEL_COLUMNS
from ._wording import ordinal, rate
from .metadata import REPEAT_COLUMN, TECHNICAL_REPEAT_COLUMN
from .metadata import TECHNICAL_REPEAT_COLUMN


def legend(ax: Axes, layout: Any, shapes: Sequence[str], n_technical: int) -> None:
Expand All @@ -36,7 +37,7 @@ def legend(ax: Axes, layout: Any, shapes: Sequence[str], n_technical: int) -> No
frameon=False,
fontsize=9.5,
title=(
f"{_named('well', layout.technical_col, TECHNICAL_REPEAT_COLUMN)}"
f"{_named('well', layout.technical_col, (TECHNICAL_REPEAT_COLUMN,))}"
",\nin order within each\nbiological repeat"
),
title_fontsize=9.5,
Expand All @@ -49,7 +50,7 @@ def legend(ax: Axes, layout: Any, shapes: Sequence[str], n_technical: int) -> No
0.01,
0.99,
f"up to {n_technical} "
f"{_named('wells', layout.technical_col, TECHNICAL_REPEAT_COLUMN)} per "
f"{_named('wells', layout.technical_col, (TECHNICAL_REPEAT_COLUMN,))} per "
f"biological repeat: the first {len(shapes)} shaped in order, the others small dots",
transform=ax.transAxes,
va="top",
Expand All @@ -63,10 +64,10 @@ def _span(counts: Sequence[int]) -> str:
return f"{min(counts)}" if min(counts) == max(counts) else f"{min(counts)} to {max(counts)}"


def _named(word: str, column: str | None, standard: str) -> str:
def _named(word: str, column: str | None, standard: Sequence[str]) -> str:
"""``word``, with its column in brackets when the column has another name than the
MorphoBricks one (``technical repeat (well)``)."""
if column in (None, standard) or str(column).lower() in (word, word.rstrip("s")):
MorphoBricks ones, ``standard`` (``technical repeat (well)``)."""
if column is None or column in standard or str(column).lower() in (word, word.rstrip("s")):
return word
return f"{word} ({column})"

Expand All @@ -75,9 +76,9 @@ def _pooled_name(layout: Any) -> str:
"""The pooled top-level units in words: ``biological repeats`` or their columns."""
if len(layout.pooled_cols) == 1 and layout.pooled_cols[0] in (
layout.repeat_col,
REPEAT_COLUMN,
*REPEAT_LEVEL_COLUMNS,
):
return _named("biological repeats", layout.pooled_cols[0], REPEAT_COLUMN)
return _named("biological repeats", layout.pooled_cols[0], REPEAT_LEVEL_COLUMNS)
return f"units of {'/'.join(layout.pooled_cols)}"


Expand All @@ -101,7 +102,7 @@ def title(
the time with a well SD above the field SD).
"""
# A technical repeat is a well, dish or coverslip: "well" in lab words (owner, 2026-10-01).
technical_word = _named("well", layout.technical_col, TECHNICAL_REPEAT_COLUMN)
technical_word = _named("well", layout.technical_col, (TECHNICAL_REPEAT_COLUMN,))
# A cluster is the points of one condition, or of one condition in one repeat.
cluster = "condition" if layout.pooled_repeats or not layout.repeat_col else "repeat"
repeats = _split_phrase(layout)
Expand All @@ -120,7 +121,7 @@ def title(
def _split_phrase(layout: Any) -> str:
"""How the plot is split: biological repeats pooled or side by side, and the units
pooled within each cluster."""
repeats_word = _named("biological repeat", layout.repeat_col, REPEAT_COLUMN)
repeats_word = _named("biological repeat", layout.repeat_col, REPEAT_LEVEL_COLUMNS)
repeats = ""
if layout.pooled_repeats:
repeats = (
Expand Down Expand Up @@ -171,8 +172,8 @@ def _technical_resampled(
) -> str:
"""Add to ``phrases`` what is resampled within clusters of one top-level unit (whole
technical repeats, or the fields of a single one); the caveat, updated."""
technical_word = _named("well", layout.technical_col, TECHNICAL_REPEAT_COLUMN)
technicals = _named("wells", layout.technical_col, TECHNICAL_REPEAT_COLUMN)
technical_word = _named("well", layout.technical_col, (TECHNICAL_REPEAT_COLUMN,))
technicals = _named("wells", layout.technical_col, (TECHNICAL_REPEAT_COLUMN,))
several = [n for n in single if n > 1]
if not single:
phrases.append(
Expand Down
29 changes: 28 additions & 1 deletion src/morphocoverage/_repeat_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
``named_by_id`` names the conditions by their IDs, the repeats as given, for the analyses
that do not pair (#628).

The figures place one column as their biological-repeat level (``repeat_level``, #642): the
canonical ``repeat_group`` when the analysis groups name it, else ``biological_repeat``. Both
in the analysis groups are refused rather than one chosen without a word.

Shared-private: called by the paired analyses (``sampling_unit``, ``retrospective``,
``normalize``) and the workflows.
``normalize``), the workflows and the figures (``plots``, ``plots_pairwise``, ``_plots_text``).
"""

from __future__ import annotations
Expand Down Expand Up @@ -100,6 +104,8 @@
NOT_FOUND,
)

# The columns a figure's biological-repeat level can be, the canonical one first (``repeat_level``).
REPEAT_LEVEL_COLUMNS = (REPEAT_GROUP_COLUMN, REPEAT_COLUMN)
_BY_GROUP = (
f"repeats are paired by their confirmed repeat group ({REPEAT_GROUP_COLUMN!r}), never by "
f"the folder label in {REPEAT_COLUMN!r}"
Expand Down Expand Up @@ -197,6 +203,25 @@ def pair_by_repeat_group(
return objects, fovs, repeat_col, pairing


def repeat_level(group_cols: Iterable[str] | None) -> str:
"""The column a figure places as its biological-repeat level, from the analysis groups.

``repeat_group`` when ``group_cols`` name it: the repeats are kept apart, and labelled, by
their confirmed repeat groups. Else ``biological_repeat``, as before (absent from the tables,
each condition is one cluster). ``group_cols`` naming both are refused (``ValueError``): a
figure has one repeat level, and keeping the folder label apart beside the repeat group
would split or pair repeats by folder label, so neither is chosen in silence.
"""
named = [c for c in REPEAT_LEVEL_COLUMNS if c in (group_cols or ())]
if len(named) > 1:
raise ValueError(
f"group_cols hold both {REPEAT_GROUP_COLUMN!r} and {REPEAT_COLUMN!r}, and a figure "
f"has one biological-repeat level: {_BY_GROUP}. Leave {REPEAT_COLUMN!r} out of "
"group_cols"
)
return named[0] if named else REPEAT_COLUMN


def named_by_id(
objects: pd.DataFrame,
fovs: pd.DataFrame | None = None,
Expand Down Expand Up @@ -497,6 +522,7 @@ def pairing_note(pairing: Pairing) -> str | None:
"NOT_FOUND",
"NO_SOURCE",
"REPEAT_GROUP_COLUMN",
"REPEAT_LEVEL_COLUMNS",
"SPLIT",
"Pairing",
"PairingRefused",
Expand All @@ -510,5 +536,6 @@ def pairing_note(pairing: Pairing) -> str | None:
"refuse_folder_names",
"refuse_unmatched",
"repeat_groups",
"repeat_level",
"with_folders",
]
24 changes: 18 additions & 6 deletions src/morphocoverage/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
pooled repeats, which the legend says. An analysis-group column (``group_cols``)
that the figure does not place raises instead, since the figure would pool
the groups the caller asked to keep apart: name it as ``condition_col``,
``repeat_col`` or ``technical_col``, or leave it out of ``group_cols``.
``repeat_col`` or ``technical_col``, or leave it out of ``group_cols``. A ``repeat_group``
in ``group_cols`` is the biological-repeat level: the figure places it where it places
``biological_repeat`` otherwise, labelled by its values (``_repeat_groups.repeat_level``;
both in ``group_cols`` raise, since a figure has one repeat level).

Limits keep the figures legible and are arguments: at most ``max_conditions``
conditions per figure (more are split evenly over several figures), at most
Expand Down Expand Up @@ -80,12 +83,13 @@
from ._plots_clusters import _cluster_units, _draw_cluster, _mask
from ._plots_text import legend, title
from ._repeat_alias import refuse_repeat_alias
from ._repeat_groups import declared_conditions, named_by_id
from ._repeat_groups import declared_conditions, named_by_id, repeat_level
from ._seeds import group_rng
from ._style import low_to_high
from ._wording import shortened
from .data_check import validate_input_tables
from .metadata import (
CONDITION_COLUMN,
MORPHOBRICKS_GROUP_COLUMNS,
REPEAT_COLUMN,
TECHNICAL_REPEAT_COLUMN,
Expand Down Expand Up @@ -197,14 +201,18 @@ def _layout(
in the order of the table's rows.
"""
columns = set(data.objects.columns) | set(data.fovs.columns)
# The biological-repeat level: repeat_group when the analysis groups name it (#642).
level = repeat_level(data.group_cols)
if repeat_col == REPEAT_COLUMN:
repeat_col = level
# condition_col=None or repeat_col=None asks for that column to be pooled.
pooled_on_request = [
default
for default, argument in zip(MORPHOBRICKS_GROUP_COLUMNS[:2], (condition_col, repeat_col))
for default, argument in zip((CONDITION_COLUMN, level), (condition_col, repeat_col))
if argument is None
]
condition_col = _column(columns, condition_col, MORPHOBRICKS_GROUP_COLUMNS[0], "condition_col")
repeat_col = _column(columns, repeat_col, MORPHOBRICKS_GROUP_COLUMNS[1], "repeat_col")
condition_col = _column(columns, condition_col, CONDITION_COLUMN, "condition_col")
repeat_col = _column(columns, repeat_col, level, "repeat_col")
if technical_col is None:
technical_col = TECHNICAL_REPEAT_COLUMN if TECHNICAL_REPEAT_COLUMN in columns else None
else:
Expand Down Expand Up @@ -262,7 +270,11 @@ def plots_of_data(
"""One or more figures per feature (see the module); keys are the feature names.

``features`` must be columns of the tables and at most ``max_features``.
``repeat_col`` holds the biological repeats, drawn side by side.
``repeat_col`` holds the biological repeats, drawn side by side. At
``biological_repeat`` (the default) it is the biological-repeat level of the analysis
groups (``_repeat_groups.repeat_level``): ``repeat_group`` when ``group_cols`` name it,
the repeats then labelled by their repeat groups, never by folder label; ``group_cols``
naming both columns raise.
``technical_col`` names the technical-repeat column (a well, a dish;
never a biological repeat), in the FOV table or the observations; by
default the MorphoBricks ``technical_repeat`` column, else none (another
Expand Down
8 changes: 5 additions & 3 deletions src/morphocoverage/plots_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
from ._checks import as_count, check_seed
from ._figure_layout import grow_bottom, panel_letter, remove_legend, sentences, set_legend
from ._palette import MUTED
from ._repeat_groups import named_by_id
from ._repeat_groups import named_by_id, repeat_level
from ._seeds import group_rng
from ._wording import p_value, shortened
from .data_check import validate_input_tables
from .metadata import CONDITION_COLUMN, REPEAT_COLUMN, sorted_labels
from .metadata import CONDITION_COLUMN, sorted_labels
from .sampling_unit.contrast_cells import cell_randomization
from .sampling_unit.contrast_repeats import MIN_REPEATS
from .schema import fov_positions
Expand Down Expand Up @@ -206,10 +206,12 @@ def pairwise_contrasts(
return pd.DataFrame(columns=PAIR_COLUMNS)
values = data.feature_values(feature)
keep = ~np.isnan(values)
# The figure's biological-repeat level: repeat_group when the analysis groups name it.
repeat = repeat_level(data.group_cols)
frame = pd.DataFrame(
{
"condition": meta[CONDITION_COLUMN].to_numpy()[keep],
"repeat": meta[REPEAT_COLUMN].to_numpy()[keep] if REPEAT_COLUMN in meta else 0,
"repeat": meta[repeat].to_numpy()[keep] if repeat in meta else 0,
"value": values[keep],
}
)
Expand Down
4 changes: 3 additions & 1 deletion src/morphocoverage/workflows/_html_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pandas as pd

from .._palette import COLOURS
from .._repeat_groups import repeat_level
from ..data_check import validate_input_tables
from ..metadata import CONDITION_COLUMN, REPEAT_COLUMN, TECHNICAL_REPEAT_COLUMN
from ..plots_pairwise import ALPHA, APART, APART_MARK, D_CAP, format_p, p_shade
Expand Down Expand Up @@ -58,7 +59,8 @@ def _observations(
frame = pd.DataFrame({"value": data.feature_values(feature)})
for name, column in (
("condition", CONDITION_COLUMN),
(REPEAT_COLUMN, REPEAT_COLUMN),
# The static figure's biological-repeat level, under the clusters' key (#642).
(REPEAT_COLUMN, repeat_level(data.group_cols)),
("technical", TECHNICAL_REPEAT_COLUMN),
("field", data.fov_col),
):
Expand Down
Loading
Loading