pymaid.adjacency_matrix¶
- pymaid.adjacency_matrix(sources, targets=None, source_grp={}, target_grp={}, fractions=False, syn_threshold=None, syn_cutoff=None, use_connectors=False, volume_filter=None, remote_instance=None)[source]¶
Generate adjacency matrix between source and target neurons.
Directional: sources = rows, targets = columns.
- Parameters:
sources –
Source neurons as single or list of either:
skeleton IDs (int or str)
neuron name (str, exact match)
annotation: e.g. ‘annotation:PN right’
CatmaidNeuron or CatmaidNeuronList object
targets –
Optional. Target neurons as single or list of either:
skeleton IDs (int or str)
neuron name (str, exact match)
annotation: e.g. ``’annotation:PN right’`
CatmaidNeuron or CatmaidNeuronList object
If not provided,
source neurons = target neurons
.fractions (bool, optional) – If True, will return connectivity as fraction of total number of postsynaptic links to target neuron.
syn_cutoff (int, optional) – If set, will cut off connections ABOVE given value.
syn_threshold (int, optional) – If set, will ignore connections with LESS synapses.
source_grp (dict, optional) –
- Use to collapse sources into groups. Can be either:
{group1: [neuron1, neuron2, ... ], ..}
{neuron1: group1, neuron2 : group2, ..}
syn_cutoff
andsyn_threshold
are applied BEFORE grouping!target_grp (dict, optional) – See
source_grp
for possible formats.use_connectors (bool, optional) – If True AND
s
ort
areCatmaidNeuron/List
, restrict adjacency matrix to their connectors. Use if e.g. you are using pruned neurons.volume_filter (Volume | list of Volumes, optional) – Volume(s) to restrict connections to. Can be a pymaid.Volume, the name of a CATMAID volume or a list thereof.
remote_instance (CatmaidInstance, optional) – If not passed, will try using globally defined.
- Returns:
matrix
- Return type:
pandas.Dataframe
See also
group_matrix()
More fine-grained control over matrix grouping.
adjacency_from_connectors()
Use this function if you are working with multiple fragments per neuron.
Examples
Generate and plot a adjacency matrix:
>>> import seaborn as sns >>> import matplotlib.pyplot as plt >>> neurons = pymaid.get_neurons('annotation:test') >>> mat = pymaid.adjacency_matrix(neurons) >>> g = sns.heatmap(adj_mat, square=True) >>> g.set_yticklabels(g.get_yticklabels(), rotation=0, fontsize=7) >>> g.set_xticklabels(g.get_xticklabels(), rotation=90, fontsize=7) >>> plt.show()
Cut neurons into axon dendrites and compare their connectivity:
>>> # Get a set of neurons >>> nl = pymaid.get_neurons('annnotation:type_16_candidates') >>> # Split into axon dendrite by using a tag >>> nl.reroot(nl.soma) >>> nl_axon = nl.prune_proximal_to('axon', inplace=False) >>> nl_dend = nl.prune_distal_to('axon', inplace=False) >>> # Get a list of the downstream partners >>> cn_table = pymaid.get_partners(nl) >>> ds_partners = cn_table[cn_table.relation == 'downstream'] >>> # Take the top 10 downstream partners >>> top_ds = ds_partners.iloc[:10].skeleton_id.values >>> # Generate separate adjacency matrices for axon and dendrites >>> adj_axon = pymaid.adjacency_matrix(nl_axon, top_ds, ... use_connectors=True) >>> adj_dend = pymaid.adjacency_matrix(nl_dend, top_ds, ... use_connectors=True) >>> # Rename rows and merge dataframes >>> adj_axon.index += '_axon' >>> adj_dend.index += '_dendrite' >>> adj_merged = pd.concat([adj_axon, adj_dend], axis=0) >>> # Plot heatmap using seaborn >>> ax = sns.heatmap(adj_merged) >>> plt.show()
Restrict adjacency matrix to a given volume:
>>> neurons = pymaid.get_neurons('annotation:glomerulus DA1') >>> lh = pymaid.get_volume('LH_R') >>> adj = pymaid.adjacency_matrix(neurons, volume_filter=lh)
Get adjacency matrix with fraction of inputs instead of total synapse count:
>>> neurons = pymaid.get_neurons('annotation:glomerulus DA1') >>> adj = pymaid.adjacency_matrix(neurons, fractions=True)