strength¶
champalimaud.strength
¶
Connection strength from groups of cells to their partners.
A connection table has one row per pair of cells, with the root ids
pre_pt_root_id and post_pt_root_id and the synapse count
weight.
The strength of a group of cells onto a partner is the heaviest
single connection, so one strong cell pair marks the partner whether
or not the rest of the group connects to it.
CLASSIFIERS = {'max': strong_by_max, 'sum': strong_by_sum}
module-attribute
¶
The ways to call a partner strong, by name.
Each takes (connections, *, cutoff, by="pre_type") and returns the
strong relationships as a table of by and partner, so a notebook
can take any of them.
strong_by_filtered_sum also needs min_weight; bind it with
functools.partial to use it in the same way.
label_partners(partners, census, proofread_ids, *, lateral_pattern)
¶
Label each partner as proofread or not, by type, and lateral.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has a column |
required |
census
|
DataFrame
|
Has columns |
required |
proofread_ids
|
DataFrame
|
Has a column |
required |
lateral_pattern
|
str
|
Regular expression for the cell types that count as lateral, such as the types of the group being studied. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
See Also
proofread_nonlateral : Keeps the rows that are neither.
Examples:
>>> import polars as pl
>>> partners = pl.DataFrame({"partner": [1, 2, 3]})
>>> census = pl.DataFrame(
... {"root_id": [1, 2], "primary_type": ["LC9", "Tm3"]}
... )
>>> proofread = pl.DataFrame({"root_id": [1, 2]})
>>> labeled = label_partners(
... partners, census, proofread, lateral_pattern="^LC"
... )
>>> labeled["partner_type"].to_list()
['LC9', 'Tm3', 'unproofread fragment']
>>> labeled["lateral"].to_list()
[True, False, False]
Source code in champalimaud/strength.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
labeled_partners(connections, cells_by_type, census, proofread_ids, *, lateral_pattern)
¶
The partners of each type, with strength and labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
Connections that start at the cells of the types, with
|
required |
cells_by_type
|
dict of str to list
|
Maps a type name to the ids of its cells. |
required |
census
|
DataFrame
|
Has columns |
required |
proofread_ids
|
DataFrame
|
Has a column |
required |
lateral_pattern
|
str
|
Regular expression for the partner types that count as lateral. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table of |
See Also
strong_partners_of : Keeps the partners a classifier calls strong.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["T", "T"],
... "post_pt_root_id": [10, 11],
... "weight": [12, 4],
... }
... )
>>> partners = labeled_partners(
... connections,
... {"T": [1, 2]},
... pl.DataFrame({"root_id": [10], "primary_type": ["X"]}),
... pl.DataFrame({"root_id": [10]}),
... lateral_pattern="^LC",
... )
>>> partners.select("partner", "s", "partner_type").rows()
[(10, 12, 'X'), (11, 4, 'unproofread fragment')]
Source code in champalimaud/strength.py
partner_populations(partners)
¶
The partners under four ways of choosing them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has boolean columns |
required |
Returns:
| Type | Description |
|---|---|
dict of str to polars.DataFrame
|
The rows of |
Examples:
>>> import polars as pl
>>> partners = pl.DataFrame(
... {
... "partner": [1, 2, 3],
... "proofread": [True, True, False],
... "lateral": [False, True, False],
... }
... )
>>> {
... name: frame["partner"].to_list()
... for name, frame in partner_populations(partners).items()
... }
{'proofread': [1, 2], 'proofread, no lateral': [1], 'with fragments': [1, 2, 3], 'with fragments, no lateral': [1, 3]}
Source code in champalimaud/strength.py
proofread_nonlateral(partners)
¶
The proofread, non-lateral rows of a labeled partner table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has boolean columns |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The rows with |
See Also
label_partners : Adds the two columns.
Examples:
>>> import polars as pl
>>> partners = pl.DataFrame(
... {
... "partner": [1, 2, 3],
... "proofread": [True, True, False],
... "lateral": [False, True, False],
... }
... )
>>> proofread_nonlateral(partners)["partner"].to_list()
[1]
Source code in champalimaud/strength.py
restrict_to(partners, relationships)
¶
The rows of a partner table that appear in a table of pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has columns |
required |
relationships
|
DataFrame
|
Has columns |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The rows of |
See Also
CLASSIFIERS : Where the strong relationships come from.
Examples:
>>> import polars as pl
>>> partners = pl.DataFrame(
... {
... "type": ["A", "A", "B"],
... "partner": [1, 2, 1],
... "s": [3, 9, 4],
... }
... )
>>> strong = pl.DataFrame({"type": ["A"], "partner": [2]})
>>> restrict_to(partners, strong).to_dicts()
[{'type': 'A', 'partner': 2, 's': 9}]
Source code in champalimaud/strength.py
strengths_to_partners(connections, *, by='pre_type')
¶
Strength from each group of presynaptic cells to each partner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
One row per cell pair, with columns |
required |
by
|
str
|
Column that labels the group of each connection's presynaptic cell, usually the cell type. Rows where it is null are dropped. |
"pre_type"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per group and partner, sorted by
|
See Also
strong_partners : Root ids of the partners at or above a cutoff. strong_share : The share of partners and synapses that are strong.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["A", "A", "A"],
... "post_pt_root_id": [7, 7, 8],
... "weight": [2, 25, 1],
... }
... )
>>> strengths_to_partners(connections).to_dicts()
...
[{'pre_type': 'A', 'partner': 7, 's': 25,
'synapses': 27, 'cells': 2},
{'pre_type': 'A', 'partner': 8, 's': 1,
'synapses': 1, 'cells': 1}]
Source code in champalimaud/strength.py
strengths_to_partners_with_cell_fraction(connections, cells_by_type)
¶
Strength of every partner of each type, with the cell fraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
As for |
required |
cells_by_type
|
dict of str to list
|
Maps a type name to the ids of its cells.
The sizes are the denominators of |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table of |
See Also
strengths_to_partners : The table this adds the fraction to.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["A", "A"],
... "post_pt_root_id": [7, 7],
... "weight": [2, 25],
... }
... )
>>> table = strengths_to_partners_with_cell_fraction(
... connections, {"A": [1, 2, 3, 4]}
... )
>>> table["cell_fraction"].to_list()
[0.5]
Source code in champalimaud/strength.py
strong_by_filtered_sum(connections, *, cutoff, min_weight, by='pre_type')
¶
Partners whose heavy connections add up to a cutoff.
A partner is strong for a group when the connections of at least
min_weight synapses onto it add up to at least cutoff.
With min_weight equal to cutoff this is strong_by_max; a
lower min_weight lets several lighter connections count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
As for |
required |
cutoff
|
int
|
Smallest filtered sum, in synapses, that counts as strong. |
required |
min_weight
|
int
|
Smallest connection, in synapses, that is added. |
required |
by
|
str
|
Column that labels the group of each presynaptic cell. |
"pre_type"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The strong relationships, in the shape |
See Also
strong_by_max : The heaviest single connection. strong_by_sum : Every connection added. strong_connection_sums : The filtered sums themselves.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["A", "A", "A", "A"],
... "post_pt_root_id": [7, 7, 8, 8],
... "weight": [6, 5, 11, 4],
... }
... )
>>> strong_by_filtered_sum(
... connections, cutoff=10, min_weight=5
... ).to_dicts()
[{'pre_type': 'A', 'partner': 7}, {'pre_type': 'A', 'partner': 8}]
Source code in champalimaud/strength.py
strong_by_max(connections, *, cutoff, by='pre_type')
¶
Partners that one presynaptic cell alone connects to strongly.
A partner is strong for a group when its heaviest single
connection from the group has at least cutoff synapses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
As for |
required |
cutoff
|
int
|
Smallest strength, in synapses, that counts as strong. |
required |
by
|
str
|
Column that labels the group of each presynaptic cell. |
"pre_type"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The strong relationships: columns |
See Also
strong_by_sum : The same with the strength summed over the group. CLASSIFIERS : Both, by name.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["A", "A", "A", "A"],
... "post_pt_root_id": [7, 7, 8, 8],
... "weight": [6, 5, 11, 4],
... }
... )
>>> strong_by_max(connections, cutoff=10).to_dicts()
[{'pre_type': 'A', 'partner': 8}]
Source code in champalimaud/strength.py
strong_by_sum(connections, *, cutoff, by='pre_type')
¶
Partners that a group of presynaptic cells drives strongly.
A partner is strong for a group when the connections onto it from
the whole group add up to at least cutoff synapses, however many
cells they come from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
As for |
required |
cutoff
|
int
|
Smallest strength, in synapses, that counts as strong. |
required |
by
|
str
|
Column that labels the group of each presynaptic cell. |
"pre_type"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The strong relationships, in the shape |
See Also
strong_by_max : The same with the heaviest single connection. CLASSIFIERS : Both, by name.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["A", "A", "A", "A"],
... "post_pt_root_id": [7, 7, 8, 8],
... "weight": [6, 5, 11, 4],
... }
... )
>>> strong_by_sum(connections, cutoff=10).to_dicts()
[{'pre_type': 'A', 'partner': 7}, {'pre_type': 'A', 'partner': 8}]
Source code in champalimaud/strength.py
strong_connection_sums(connections, *, min_weight, by='pre_type')
¶
Summed weight of the heavy connections onto each partner.
Only connections of at least min_weight synapses are added, so a
partner reached only by lighter connections has no row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connections
|
DataFrame
|
As for |
required |
min_weight
|
int
|
Smallest connection, in synapses, that is added. |
required |
by
|
str
|
Column that labels the group of each presynaptic cell. |
"pre_type"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Columns |
See Also
strong_by_filtered_sum : Partners where this sum reaches a cutoff.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["A", "A", "A", "A"],
... "post_pt_root_id": [7, 7, 8, 8],
... "weight": [6, 5, 11, 4],
... }
... )
>>> strong_connection_sums(connections, min_weight=10).to_dicts()
[{'pre_type': 'A', 'partner': 8, 's_sum_filtered': 11}]
Source code in champalimaud/strength.py
strong_partners(partners, cutoff)
¶
Root ids of the partners whose strength reaches a cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has columns |
required |
cutoff
|
int
|
Smallest strength, in synapses, that counts as strong. |
required |
Returns:
| Type | Description |
|---|---|
set of int
|
The |
See Also
strengths_to_partners : The table this filters.
Examples:
>>> import polars as pl
>>> partners = pl.DataFrame(
... {"partner": [1, 2, 3], "s": [9, 10, 15]}
... )
>>> sorted(strong_partners(partners, 10))
[2, 3]
Source code in champalimaud/strength.py
strong_partners_of(partners, connections, classify, cutoff)
¶
Proofread, non-lateral partners that a classifier calls strong.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has columns |
required |
connections
|
DataFrame
|
The connections |
required |
classify
|
callable
|
An entry of |
required |
cutoff
|
int
|
Passed to |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The rows of |
See Also
CLASSIFIERS : The classifiers to pass. restrict_to : Keeps the rows that are in a table of pairs. proofread_nonlateral : The proofread and lateral filter.
Examples:
>>> import polars as pl
>>> connections = pl.DataFrame(
... {
... "pre_type": ["T", "T"],
... "post_pt_root_id": [10, 11],
... "weight": [12, 4],
... }
... )
>>> partners = labeled_partners(
... connections,
... {"T": [1, 2]},
... pl.DataFrame(
... {"root_id": [10, 11], "primary_type": ["X", "Y"]}
... ),
... pl.DataFrame({"root_id": [10, 11]}),
... lateral_pattern="^LC",
... )
>>> strong_partners_of(
... partners, connections, CLASSIFIERS["max"], 10
... )["partner"].to_list()
[10]
Source code in champalimaud/strength.py
strong_share(partners, *, cutoff)
¶
The share of each type's partners and output that are strong.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partners
|
DataFrame
|
Has columns |
required |
cutoff
|
int
|
Smallest strength, in synapses, that counts as strong. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per type, in order of first appearance, with columns:
|
See Also
strong_partners : The ids of the strong partners.
Examples:
>>> import polars as pl
>>> partners = pl.DataFrame(
... {
... "type": ["A", "A", "A", "B"],
... "s": [10, 9, 1, 1],
... "synapses": [30, 10, 10, 4],
... }
... )
>>> strong_share(partners, cutoff=10).to_dicts()
...
[{'type': 'A', 'n_partners': 3, 'n_strong': 1,
'partner_fraction': 0.3333333333333333, 'synapse_fraction': 0.6},
{'type': 'B', 'n_partners': 1, 'n_strong': 0,
'partner_fraction': 0.0, 'synapse_fraction': 0.0}]
Source code in champalimaud/strength.py
type_mass(strong)
¶
Synapses between each type and each partner type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strong
|
DataFrame
|
Has columns |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Columns |
Examples:
>>> import polars as pl
>>> strong = pl.DataFrame(
... {
... "type": ["T", "T", "T"],
... "partner_type": ["X", "X", "Y"],
... "synapses": [5, 7, 20],
... }
... )
>>> type_mass(strong).rows()
[('T', 'Y', 20), ('T', 'X', 12)]