Skip to content

Commit

Permalink
new function to find diagonals from on disk matrix
Browse files Browse the repository at this point in the history
* Ashley pointed out that would be nmuch faster to read in chunks
* ... and indeed it is
  • Loading branch information
jefferis authored and jdmanton committed Sep 4, 2014
1 parent f00022b commit c05d304
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions R/scoremats.r
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ diagonal <- function(x, indices=NULL) {
}


# Utility function to get diagonal of on disk faster than it can manage by itself
# by reading in reasonble size chunks
fast_disk_diag<-function(x, indices=NULL, chunksize=300, use.names=TRUE) {
if(is.null(indices)) indices=seq_len(nrow(x))
ninds=length(indices)
diags=rep(NA,ninds)
if(ninds>chunksize){
for(i in seq.int(from=0,by=chunksize,to=ninds-chunksize)) {
sq=x[indices[i+1:chunksize],indices[i+1:chunksize]]
diags[i+1:chunksize]=diag(sq)
}
# next index will be
i=i+chunksize+1
} else {
# we'll be starting from scratch
i=1
}

if(i<=ninds){
sq=x[indices[i:ninds],indices[i:ninds]]
diags[i:ninds]=diag(sq)
}
if(use.names) names(diags)=colnames(x)[indices]
diags
}

#' Convert a subset of a square score matrix to a sparse representation
#'
#' This can be useful for storing raw forwards and reverse NBLAST scores for a
Expand Down

0 comments on commit c05d304

Please sign in to comment.