cuisto.utils
utils module, part of cuisto.
Contains utilities functions.
add_brain_region(df, atlas, col='Parent')
#
Add brain region to a DataFrame with Atlas_X
, Atlas_Y
and Atlas_Z
columns.
This uses Brainglobe Atlas API to query the atlas. It does not use the structure_from_coords() method, instead it manually converts the coordinates in stack indices, then get the corresponding annotation id and query the corresponding acronym -- because brainglobe-atlasapi is not vectorized at all.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame with atlas coordinates in microns. |
required |
atlas
|
BrainGlobeAtlas
|
|
required |
col
|
str
|
Column in which to put the regions acronyms. Default is "Parent". |
'Parent'
|
Returns:
Name | Type | Description |
---|---|---|
df |
DataFrame
|
Same DataFrame with a new "Parent" column. |
Source code in cuisto/utils.py
add_channel(df, object_type, channel_names)
#
Add channel as a measurement for detections DataFrame.
The channel is read from the Classification column, the latter having to be formatted as "object_type: channel".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame with detections measurements. |
required |
object_type
|
str
|
Object type (primary classification). |
required |
channel_names
|
dict
|
Map between original channel names to something else. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Same DataFrame with a "channel" column. |
Source code in cuisto/utils.py
add_hemisphere(df, hemisphere_names, midline=5700, col='Atlas_Z', atlas_type='brain')
#
Add hemisphere (left/right) as a measurement for detections or annotations.
The hemisphere is read in the "Classification" column for annotations. The latter
needs to be in the form "Right: Name" or "Left: Name". For detections, the input
col
of df
is compared to midline
to assess if the object belong to the left or
right hemispheres.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame with detections or annotations measurements. |
required |
hemisphere_names
|
dict
|
Map between "Left" and "Right" to something else. |
required |
midline
|
float
|
Used only for "detections" |
5700
|
col
|
str
|
Name of the column containing the Z coordinate (medio-lateral) in microns. Default is "Atlas_Z". |
'Atlas_Z'
|
atlas_type
|
(brain, cord)
|
Type of atlas used for registration. Required because the brain atlas is swapped between left and right while the spinal cord atlas is not. Default is "brain". |
"brain"
|
Returns:
Name | Type | Description |
---|---|---|
df |
DataFrame
|
The same DataFrame with a new "hemisphere" column |
Source code in cuisto/utils.py
ccf_to_stereo(x_ccf, y_ccf, z_ccf=0)
#
Convert X, Y, Z coordinates in CCFv3 to stereotaxis coordinates (as in Paxinos-Franklin atlas).
Coordinates are shifted, rotated and squeezed, see (1) for more info. Input must be
in mm.
x_ccf
corresponds to the anterio-posterior (rostro-caudal) axis.
y_ccf
corresponds to the dorso-ventral axis.
z_ccf
corresponds to the medio-lateral axis (left-right) axis.
Warning : it is a rough estimation.
(1) https://community.brain-map.org/t/how-to-transform-ccf-x-y-z-coordinates-into-stereotactic-coordinates/1858
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_ccf
|
floats or ndarray
|
Coordinates in CCFv3 space in mm. |
required |
y_ccf
|
floats or ndarray
|
Coordinates in CCFv3 space in mm. |
required |
z_ccf
|
float or ndarray
|
Coordinate in CCFv3 space in mm. Default is 0. |
0
|
Returns:
Type | Description |
---|---|
ap, dv, ml : floats or np.ndarray
|
Stereotaxic coordinates in mm. |
Source code in cuisto/utils.py
filter_df_classifications(df, filter_list, mode='keep', col='Classification')
#
Filter a DataFrame whether specified col
column entries contain elements in
filter_list
. Case insensitive.
If mode
is "keep", keep entries only if their col
in is in the list (default).
If mode
is "remove", remove entries if their col
is in the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
|
required |
filter_list
|
list | tuple | str
|
List of words that should be present to trigger the filter. |
required |
mode
|
keep or remove
|
Keep or remove entries from the list. Default is "keep". |
'keep'
|
col
|
str
|
Key in |
'Classification'
|
Returns:
Type | Description |
---|---|
DataFrame
|
Filtered DataFrame. |
Source code in cuisto/utils.py
filter_df_regions(df, filter_list, mode='keep', col='Parent')
#
Filters entries in df
based on wether their col
is in filter_list
or not.
If mode
is "keep", keep entries only if their col
in is in the list (default).
If mode
is "remove", remove entries if their col
is in the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
|
required |
filter_list
|
list - like
|
List of regions to keep or remove from the DataFrame. |
required |
mode
|
keep or remove
|
Keep or remove entries from the list. Default is "keep". |
'keep'
|
col
|
str
|
Key in |
'Parent'
|
Returns:
Name | Type | Description |
---|---|---|
df |
DataFrame
|
Filtered DataFrame. |
Source code in cuisto/utils.py
get_blacklist(file, atlas)
#
Build a list of regions to exclude from file.
File must be a TOML with [WITH_CHILDS] and [EXACT] sections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
Full path the atlas_blacklist.toml file. |
required |
atlas
|
BrainGlobeAtlas
|
Atlas to extract regions from. |
required |
Returns:
Name | Type | Description |
---|---|---|
black_list |
list
|
Full list of acronyms to discard. |
Source code in cuisto/utils.py
get_data_coverage(df, col='Atlas_AP', by='animal')
#
Get min and max in col
for each by
.
Used to get data coverage for each animal to plot in distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
description |
required |
col
|
str
|
Key in |
'Atlas_AP'
|
by
|
str
|
Key in |
'animal'
|
Returns:
Type | Description |
---|---|
DataFrame
|
min and max of |
Source code in cuisto/utils.py
get_df_kind(df)
#
Get DataFrame kind, eg. Annotations or Detections.
It is based on reading the Object Type of the first entry, so the DataFrame must have only one kind of object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
kind |
str
|
"detection" or "annotation". |
Source code in cuisto/utils.py
get_injection_site(animal, info_file, channel, stereo=False)
#
Get the injection site coordinates associated with animal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
animal
|
str
|
Animal ID. |
required |
info_file
|
str
|
Path to TOML info file. |
required |
channel
|
str
|
Channel ID as in the TOML file. |
required |
stereo
|
bool
|
Wether to convert coordinates in stereotaxis coordinates. Default is False. |
False
|
Returns:
Type | Description |
---|---|
x, y, z : floats
|
Injection site coordinates. |
Source code in cuisto/utils.py
get_leaves_list(atlas)
#
Get the list of leaf brain regions.
Leaf brain regions are defined as regions without childs, eg. regions that are at the bottom of the hiearchy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
atlas
|
BrainGlobeAtlas
|
Atlas to extract regions from. |
required |
Returns:
Name | Type | Description |
---|---|---|
leaves_list |
list
|
Acronyms of leaf brain regions. |
Source code in cuisto/utils.py
get_mapping_fusion(fusion_file)
#
Get mapping dictionnary between input brain regions and new regions defined in
atlas_fusion.toml
file.
The returned dictionnary can be used in DataFrame.replace().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fusion_file
|
str
|
Path to the TOML file with the merging rules. |
required |
Returns:
Name | Type | Description |
---|---|---|
m |
dict
|
Mapping as {old: new}. |
Source code in cuisto/utils.py
get_starter_cells(animal, channel, info_file)
#
Get the number of starter cells associated with animal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
animal
|
str
|
Animal ID. |
required |
channel
|
str
|
Channel ID. |
required |
info_file
|
str
|
Path to TOML info file. |
required |
Returns:
Name | Type | Description |
---|---|---|
n_starters |
int
|
Number of starter cells. |
Source code in cuisto/utils.py
merge_regions(df, col, fusion_file)
#
Merge brain regions following rules in the fusion_file.toml
file.
Apply this merging on col
of the input DataFrame. col
whose value is found in
the members
sections in the file will be changed to the new acronym.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
|
required |
col
|
str
|
Column of |
required |
fusion_file
|
str
|
Path to the toml file with the merging rules. |
required |
Returns:
Name | Type | Description |
---|---|---|
df |
DataFrame
|
Same DataFrame with regions renamed. |
Source code in cuisto/utils.py
renormalize_per_key(df, by, on)
#
Renormalize on
column by its sum for each by
.
Use case : relative density is computed for both hemispheres, so if one wants to
plot only one hemisphere, the sum of the bars corresponding to one channel (by
)
should be 1. So :
df = df[df["hemisphere"] == "Ipsi."] df = renormalize_per_key(df, "channel", "relative density") Then, the sum of "relative density" for each "channel" equals 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
|
required |
by
|
str
|
Key in |
required |
on
|
str
|
Key in |
required |
Returns:
Name | Type | Description |
---|---|---|
df |
DataFrame
|
Same DataFrame with normalized |
Source code in cuisto/utils.py
select_hemisphere_channel(df, hue, hue_filter, hue_mirror)
#
Select relevant data given hue and filters.
Returns the DataFrame with only things to be used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame to filter. |
required |
hue
|
(hemisphere, channel)
|
hue that will be used in seaborn plots. |
"hemisphere"
|
hue_filter
|
str
|
Selected data. |
required |
hue_mirror
|
bool
|
Instead of keeping only hue_filter values, they will be plotted in mirror. |
required |
Returns:
Name | Type | Description |
---|---|---|
dfplt |
DataFrame
|
DataFrame to be used in plots. |