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

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



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)


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


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.


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.


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


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.


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.


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


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.


# \donttest{
# these will mostly be axo-axonic connections between DA2 PNs
neuprint_get_adjacency_matrix('DA2 lPN')
#> Error in neuprint_ids(bodyids, conn = conn, dataset = dataset, cache = cache): No valid ids provided!

# rectangular matrix with different in/out neurons
neuprint_get_adjacency_matrix(inputids='DA2 lPN', outputids='DL4 adPN')
#> Error in neuprint_ids(inputids, conn = conn, dataset = dataset, cache = cache): No valid ids provided!
# }
# \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.*',
hist(colSums(pnkc), xlab = 'PN inputs / KC', br=100)

#> [1] 2
# }
if (FALSE) { # \dontrun{
# sparse adjacency matrix
  outputids='/KC.*', sparse=TRUE, cache=TRUE)
# PN-KC connectivity is itself sparse, so < 2% of entries are non zero
# while memory requirements are ~ 5%
} # }