Skip to content

Commit

Permalink
Remove old entries when using save_compressed_nblast_mat
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbates committed Dec 13, 2020
1 parent 8b46bc5 commit 37ed0d1
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ save_compressed_nblast_mat <- function(x,
threshold=-0.5,
digits=3,
format=c("rda", "rds"),
remove = NULL,
...) {
format=match.arg(format)
overwrite=match.arg(overwrite)
Expand All @@ -265,29 +266,37 @@ save_compressed_nblast_mat <- function(x,
}else if(overwrite=="combine"){
combine = TRUE
if(format=="rds"){
old = readRDS(fname)
old = tryCatch(readRDS(fname), function(e) NULL)
}else{
old = load_assign(fname)
old = tryCatch(load_assign(fname), function(e) NULL)
}
}
}
if(is.null(old)){
warning("No extant NBLAST at ", fname)
}
colnames(x) = gsub("_m$","",colnames(x)) # factor in mirrored hemibrain neurons
rownames(x) = gsub("_m$","",rownames(x))
x = apply(x, 2, function(i) tapply(i, rownames(x), sum, na.rm = TRUE))
x = t(apply(t(x), 2, function(i) tapply(i, colnames(x), sum, na.rm = TRUE)))
x[x<threshold]=threshold
x=round(x, digits=digits)
y = x
if(combine){
old = old[!rownames(old)%in%rownames(x),]
if(nrow(old)){
warning("combining with extant file: ", fname)
y = plyr::rbind.fill.matrix(old, x)
rownames(y) = c(rownames(old), rownames(x))
}else{
y = x
if(!is.null(remove)){
old = old[!rownames(old)%in%remove,!colnames(old)%in%remove]
}
if(!is.null(old)){
if(nrow(old)){
warning("combining with extant file: ", fname)
y = plyr::rbind.fill.matrix(old, x)
rownames(y) = c(rownames(old), rownames(x))
}else{
warning("original NBLAST depleted")
y = x
}
}
}else{
y = x
}
message("Resaving a compressed version of ", objname, " to ", fname)
if(format=="rds") {
Expand Down

0 comments on commit 37ed0d1

Please sign in to comment.