Get an adjacency matrix for the synaptic connectivity within a set of specified bodies

neuprint_get_adjacency_matrix(
  bodyids = NULL,
  inputids = NULL,
  outputids = NULL,
  threshold = 1L,
  dataset = NULL,
  chunksize = 1000L,
  all_segments = FALSE,
  conn = NULL,
  sparse = FALSE,
  cache = FALSE,
  ...
)

Arguments

bodyids

the body IDs for neurons/segments (bodies) you wish to query. This can be in any form understood by neuprint_ids.

inputids, outputids

identifiers for input and output bodies (use as an alternative to bodyids)

threshold

Return only connections greater than or equal to the indicated strength (default 1 returns all connections).

dataset

optional, a dataset you want to query. If NULL, the default specified by your R environ file is used or, failing that the current connection, is used. See neuprint_login for details.

chunksize

Split large queries into chunks of this many ids to prevent server timeouts. The default of 1000 seems to be a reasonable compromise. Set to Inf to insist that the query is always sent in one pass only.

all_segments

if TRUE, all bodies are considered, if FALSE, only 'Neurons', i.e. bodies with a status roughly traced status.

conn

optional, a neuprintr connection object, which also specifies the neuPrint server. If NULL, the defaults set in your .Rprofile or .Renviron are used. See neuprint_login for details.

sparse

Whether to return a sparse adjacency matrix (of class CsparseMatrix). This may be a particularly good idea for large matrices of >5000 neurons, especially if a threshold is used to eliminate very numerous weak connections. Default FALSE.

cache

the query to neuPrint server, so that it does not need to be repeated. Of course you can save the results, but this may be helpful e.g. inside a wrapper function that post-processes the results like hemibrainr::grouped_adjacency_matrix.

...

methods passed to neuprint_login

Value

a n x n matrix, where the rows are input neurons and the columns are their targets. Only neurons supplied as the argument `bodyids` are considered.

Examples

# \donttest{
# these will mostly be axo-axonic connections between DA2 PNs
neuprint_get_adjacency_matrix('DA2 lPN')
#>            1796817841 1827516355 818983130 1797505019 1796818119
#> 1796817841          0          6         7          7          1
#> 1827516355          3          0         3          1          5
#> 818983130           3          7         0          6          5
#> 1797505019          6          4         6          0         11
#> 1796818119          3          6         7         10          0

# rectangular matrix with different in/out neurons
neuprint_get_adjacency_matrix(inputids='DA2 lPN', outputids='DL4 adPN')
#>            1670934213
#> 1796817841          9
#> 1827516355          9
#> 818983130           0
#> 1797505019          4
#> 1796818119          9
# }
# \donttest{
# Note the use of cache=T, which will avoid a subsequent query to the
# neuPrint server if the same information is requested
pnkc=neuprint_get_adjacency_matrix(inputids='name:mPN', outputids='/KC.*',
  cache=TRUE)
hist(colSums(pnkc), xlab = 'PN inputs / KC', br=100)

sum(rowSums(pnkc)>0)
#> [1] 146
# }
if (FALSE) {
# sparse adjacency matrix
pnkcs=neuprint_get_adjacency_matrix(inputids='name:mPN',
  outputids='/KC.*', sparse=TRUE, cache=TRUE)
library(Matrix)
# PN-KC connectivity is itself sparse, so < 2% of entries are non zero
nnzero(pnkcs)/length(pnkcs)
# while memory requirements are ~ 5%
as.numeric(object.size(pnkcs)/object.size(pnkc))
}