Mouse-Imaging-Centre / RMINC

Statistics for MINC volumes: A library to integrate voxel-based statistics for MINC volumes into the R environment. Supports getting and writing of MINC volumes, running voxel-wise linear models, correlations, etc.; correcting for multiple comparisons using the False Discovery Rate, and more. With contributions from Jason Lerch, Chris Hammill, Jim Nikelski and Matthijs van Eede. Some additional information can be found here:
https://mouse-imaging-centre.github.io/RMINC
Other
23 stars 17 forks source link

anatCreateVolume fails for left/right labels, only works for symmetric labels #267

Open gdevenyi opened 5 years ago

gdevenyi commented 5 years ago

Fixed the function up with this:

anatCreateVolume2 <- function (anat, column = 1) 
{
  labels <- read.csv(attr(anat, "definitions"))
  volume <- mincGetVolume(attr(anat, "atlas"))
  newvolume <- volume
  for (i in 1:nrow(labels)) {
    if (labels$right.label[i] == labels$left.label[i] ) {
      newvolume[volume < labels$right.label[i] + 0.5 & volume > 
                  labels$right.label[i] - 0.5] <- anat[labels$Structure[i], 
                                                       column]
    } else {
      newvolume[volume < labels$right.label[i] + 0.5 & volume > 
                  labels$right.label[i] - 0.5] <- anat[paste0("right ",labels$Structure[i]), 
                                                       column]
      newvolume[volume < labels$left.label[i] + 0.5 & volume > 
                  labels$left.label[i] - 0.5] <- anat[paste0("left ",labels$Structure[i]), 
                                                      column]
    }
  }
  return(invisible(mincArray(newvolume)))
}
goedan-cic commented 3 years ago

Above code fails when labels have same left and right label value When labels$Structure[i] is referenced on line 9 it needs to be converted to a character string. Updated function below:

anatCreateVolume2 <- function (anat, column = 1) 
{
  labels <- read.csv(attr(anat, "definitions"))
  volume <- mincGetVolume(attr(anat, "atlas"))
  newvolume <- volume
  for (i in 1:nrow(labels)) {
    if (labels$right.label[i] == labels$left.label[i] ) {
      newvolume[volume < labels$right.label[i] + 0.5 & volume > 
                  labels$right.label[i] - 0.5] <- anat[as.character(labels$Structure[i]), 
                                                       column]
    } else {
      newvolume[volume < labels$right.label[i] + 0.5 & volume > 
                  labels$right.label[i] - 0.5] <- anat[paste0("right ",labels$Structure[i]), 
                                                       column]
      newvolume[volume < labels$left.label[i] + 0.5 & volume > 
                  labels$left.label[i] - 0.5] <- anat[paste0("left ",labels$Structure[i]), 
                                                      column]
    }
  }
  return(invisible(mincArray(newvolume)))
}
gdevenyi commented 3 years ago

Right, the label set I used had all unique numbers so we didn't have this issue.