Utilities

Utility functions for manipulating neuprint-python output.

merge_neuron_properties(neuron_df, conn_df)

Merge neuron properties to a connection table.

connection_table_to_matrix(conn_df[, ...])

Given a weighted connection table, produce a weighted adjacency matrix.

Reference

neuprint.utils.merge_neuron_properties(neuron_df, conn_df, properties=['type', 'instance'])[source]

Merge neuron properties to a connection table.

Given a table of neuron properties and a connection table, append _pre and _post columns to the connection table for each of the given properties via the appropriate merge operations.

Parameters
  • neuron_df – DataFrame with columns for ‘bodyId’ and any properties you want to merge

  • conn_df – DataFrame with columns bodyId_pre and bodyId_post

  • properties – Column names from neuron_df to merge onto conn_df.

Returns

Updated conn_df with new columns.

Example

In [1]: from neuprint import fetch_adjacencies, NeuronCriteria as NC, merge_neuron_properties
   ...: neuron_df, conn_df = fetch_adjacencies(rois='PB', min_roi_weight=120)
   ...: print(conn_df)
   bodyId_pre  bodyId_post roi  weight
0   880875736   1631450739  PB     123
1   880880259    849421763  PB     141
2   910442723    849421763  PB     139
3   910783961   5813070465  PB     184
4   911129204    724280817  PB     127
5   911134009    849421763  PB     125
6   911565419   5813070465  PB     141
7   911911004   1062526223  PB     125
8   911919044    973566036  PB     122
9  5813080838    974239375  PB     136

In [2]: merge_neuron_properties(neuron_df, conn_df, 'type')
Out[2]:
   bodyId_pre  bodyId_post roi  weight  type_pre    type_post
0   880875736   1631450739  PB     123  Delta7_a  PEN_b(PEN2)
1   880880259    849421763  PB     141  Delta7_a  PEN_b(PEN2)
2   910442723    849421763  PB     139  Delta7_a  PEN_b(PEN2)
3   910783961   5813070465  PB     184  Delta7_a  PEN_b(PEN2)
4   911129204    724280817  PB     127  Delta7_a  PEN_b(PEN2)
5   911134009    849421763  PB     125  Delta7_a  PEN_b(PEN2)
6   911565419   5813070465  PB     141  Delta7_a  PEN_b(PEN2)
7   911911004   1062526223  PB     125  Delta7_b  PEN_b(PEN2)
8   911919044    973566036  PB     122  Delta7_a  PEN_b(PEN2)
9  5813080838    974239375  PB     136       EPG          PEG
neuprint.utils.connection_table_to_matrix(conn_df, group_cols='bodyId', weight_col='weight', sort_by=None, make_square=False)[source]

Given a weighted connection table, produce a weighted adjacency matrix.

Parameters
  • conn_df – A DataFrame with columns for pre- and post- identifiers (e.g. bodyId, type or instance), and a column for the weight of the connection.

  • group_cols

    Which two columns to use as the row index and column index of the returned matrix, respetively. Or give a single string (e.g. "body", in which case the two column names are chosen by appending the suffixes _pre and _post to your string.

    If a pair of pre/post values occurs more than once in the connection table, all of its weights will be summed in the output matrix.

  • weight_col – Which column holds the connection weight, to be aggregated for each unique pre/post pair.

  • sort_by – How to sort the rows and columns of the result. Can be two strings, e.g. ("type_pre", "type_post"), or a single string, e.g. "type" in which case the suffixes are assumed.

  • make_square – If True, insert rows and columns to ensure that the same IDs exist in the rows and columns. Inserted entries will have value 0.0

Returns

DataFrame, shape NxM, where N is the number of unique values in the ‘pre’ group column, and M is the number of unique values in the ‘post’ group column.

Example

In [1]: from neuprint import fetch_simple_connections, NeuronCriteria as NC
   ...: kc_criteria = NC(type='KC.*')
   ...: conn_df = fetch_simple_connections(kc_criteria, kc_criteria)
In [1]: conn_df.head()
Out[1]:
   bodyId_pre  bodyId_post  weight type_pre type_post instance_pre instance_post                                       conn_roiInfo
0  1224137495   5813032771      29      KCg       KCg          KCg    KCg(super)  {'MB(R)': {'pre': 26, 'post': 26}, 'gL(R)': {'...
1  1172713521   5813067826      27      KCg       KCg   KCg(super)         KCg-d  {'MB(R)': {'pre': 26, 'post': 26}, 'PED(R)': {...
2   517858947   5813032943      26   KCab-p    KCab-p       KCab-p        KCab-p  {'MB(R)': {'pre': 25, 'post': 25}, 'PED(R)': {...
3   642680826   5812980940      25   KCab-p    KCab-p       KCab-p        KCab-p  {'MB(R)': {'pre': 25, 'post': 25}, 'PED(R)': {...
4  5813067826   1172713521      24      KCg       KCg        KCg-d    KCg(super)  {'MB(R)': {'pre': 23, 'post': 23}, 'gL(R)': {'...

In [2]: from neuprint.utils import connection_table_to_matrix
   ...: connection_table_to_matrix(conn_df, 'type')
Out[2]:
type_post   KC  KCa'b'  KCab-p  KCab-sc     KCg
type_pre
KC           3     139       6        5     365
KCa'b'     154  102337     245      997    1977
KCab-p       7     310   17899     3029     127
KCab-sc      4    2591    3975   247038    3419
KCg        380    1969      79     1526  250351