crantpy.utils.ordering module#
Private axis-ordering engine for NestedMatrix.
The public surface is NestedMatrix.order / NestedMatrix.by (and the
same names on DirectedNestedMatrix). This module is not a user-facing
toolkit.
- class crantpy.utils.ordering.AxisOrdering(ordered_neurons, type_boundaries, neuron_to_type)[source]#
Bases:
objectOrdered neurons and type metadata for one matrix axis.
- Parameters:
- class crantpy.utils.ordering.By(columns, rank=None, extract=None, key=None, na='last')[source]#
Bases:
objectThe rule
by()returns. Construct it throughby().- Parameters:
- class crantpy.utils.ordering.MatrixOrder(type_rule='label', default_rule=None, within_rules=None)[source]#
Bases:
objectHow one matrix axis is ordered, at both of its levels.
Construct through
order()(NestedMatrix.order).typesorders the cell type blocks;defaultorders neurons inside a block thatwithindoes not name;withinis a per-type override. Neither can move a neuron across a block boundary.- default(rule)[source]#
Return a copy whose unnamed types use rule.
- Parameters:
rule (Any)
- Return type:
- class crantpy.utils.ordering.ResolvedAnnotations(relevant, typed, id_map, untyped_ids, missing_ids)[source]#
Bases:
NamedTupleResult of resolving neuron annotations against one axis’ neuron IDs.
- Parameters:
relevant (pandas.DataFrame)
typed (pandas.DataFrame)
- relevant: pandas.DataFrame#
Alias for field number 0
- typed: pandas.DataFrame#
Alias for field number 1
- crantpy.utils.ordering.as_matrix_order(spec)[source]#
Coerce an
order=argument into aMatrixOrder.- Parameters:
spec (MatrixOrder | Iterable[Any] | None)
- Return type:
- crantpy.utils.ordering.build_axis_ordering(axis_ids, annotations, id_col, type_col, order=None)[source]#
Resolve annotations and lay out one matrix axis end to end.
- Parameters:
annotations (pandas.DataFrame)
id_col (str)
type_col (str)
order (MatrixOrder | Iterable[Any] | None)
- Return type:
- crantpy.utils.ordering.build_ordered_neurons(typed_annotations, type_col, sorted_types, neuron_id_column, within=None, default=None)[source]#
Lay the typed neurons out block by block, returning order and boundaries.
- crantpy.utils.ordering.by(*columns, rank=None, extract=None, key=None, na='last')[source]#
Sort a cell type’s neurons by annotation column(s).
A string at the order layer is only a named rule (
"id","annotation","label","size"). Column names live here.- Parameters:
columns (str) – Annotation columns to read, in priority order. The first column that yields a usable value wins.
rank (sequence of str, optional) – Explicit label order. Without extract, the leftmost rank label that appears in the cell is used, so
"EPG/PEG_R1"and"Δ7_L8R1R9"both rank without a regex.extract (str or compiled pattern, optional) – Regex whose first group is the label. A plain string is compiled.
key (callable, optional) –
value -> sort keywhen rank is omitted. Defaults to a natural sort, so"ER_g2"precedes"ER_g10".na ({"last", "first", "raise"}, default "last") – What an unrankable or null value costs.
"raise"errors; the others put those neurons after or before the ranked ones, keeping annotation row order inside that group.
- Return type:
Examples
>>> NestedMatrix.by("cell_instance") >>> NestedMatrix.by("cell_instance", "cell_subtype", rank=EB_RING) >>> NestedMatrix.by("cell_instance", extract=r"([LR]\d+)", rank=PB)
- crantpy.utils.ordering.order(types='label', default=None, within=None)[source]#
Build a reusable neuron order for a nested matrix axis.
Nested call:
NestedMatrix.order( types=["ER2", "EPG/PEG", "delta7"], default="id", within={"EPG/PEG": NestedMatrix.by("cell_instance", rank=EB_RING)}, )
Builder:
NestedMatrix.order().types(["ER2", "EPG/PEG"]).default("id").within( "EPG/PEG", NestedMatrix.by("cell_instance", rank=EB_RING) )
A bare sequence passed as
order=to a matrix constructor is still types-only shorthand forNestedMatrix.order(types=...).- Parameters:
- Return type:
- crantpy.utils.ordering.resolve_relevant_annotations(matrix_ids, annotations, id_col, type_col)[source]#
Narrow annotations to matrix_ids, de-duplicating by neuron.
- Parameters:
annotations (pandas.DataFrame)
id_col (str)
type_col (str)
- Return type: