# one_vs_group

# Introduction

This vignette illustrates a minimal example of a single patient vs. group (1 vs Group) SCC analysis with the `neuroSCC` package.

Ensure you have executed the steps from the “Getting Started” vignette, or follow the conditional steps below to create required data.

# Data Preparation (Conditional)

If you haven’t executed the previous vignette, let’s conditionally recreate necessary matrices.

    cat("Creating required matrices from sample data...\n")
    #> Creating required matrices from sample data...

    databaseControls <- databaseCreator(pattern = "^syntheticControl.*\\.nii\\.gz$", control = TRUE, quiet = TRUE)
    databasePathological <- databaseCreator(pattern = "^syntheticPathological1\\.nii\\.gz$", control = FALSE, quiet = TRUE)

    matrixControls <- matrixCreator(databaseControls, paramZ = 35, quiet = TRUE)
    matrixPathological <- matrixCreator(databasePathological, paramZ = 35, quiet = TRUE)

    normalizedMatrix <- meanNormalization(matrixControls)
    #>
    #>  Mean before normalization: 4.345851
    #>
    #>  Normalization completed.
    normalizedPathological <- meanNormalization(matrixPathological)
    #>
    #>  Mean before normalization: 4.243371
    #>
    #>  Normalization completed.

    niftiPath <- system.file("extdata", "syntheticControl1.nii.gz", package = "neuroSCC")
    contours <- neuroContour(niftiPath, paramZ = 35, plotResult = FALSE)

    cat("Data prepared successfully.\n")
    #> Data prepared successfully.

# Single Patient Data and Cloning

Prepare data for a single patient and generate synthetic Poisson clones to enable SCC computation:

    # Select the single pathological patient
    SCC_AD <- normalizedPathological[1, , drop = FALSE]
    SCC_CN <- normalizedMatrix

    # Toy parameters for Poisson clone generation (modifiable by user)
    numClones <- 2
    factorLambda <- 0.1

    # Generate synthetic clones
    SCC_AD_clones <- generatePoissonClones(SCC_AD, numClones, factorLambda)

    # Combine the patient with generated clones
    SCC_AD_expanded <- rbind(SCC_AD, SCC_AD_clones)
    SCC_AD_expanded <- meanNormalization(SCC_AD_expanded)
    #>
    #>  Mean before normalization: 1.065800
    #>
    #>  Normalization completed.

# SCC Estimation (Conditional Execution)

This step requires the external `ImageSCC` package, currently not on CRAN. Ensure you have this package installed. If not, install it using:

    if (requireNamespace("ImageSCC", quietly = TRUE)) {
      message("'ImageSCC' package is available.")
    } else {
      message("This vignette requires the 'ImageSCC' package.")
      message("You can install it from GitHub with:")
      message("  remotes::install_github('FIRST-Data-Lab/ImageSCC')")
    }
    #> 'ImageSCC' package is available.

The SCC computation is typically computationally intensive. Here, we skip actual computation and load a precomputed result from the package (`SCCcomp.RData`):

    # Check for Triangulation package
    triangulation_available <- requireNamespace("Triangulation", quietly = TRUE)

    if (triangulation_available) {
      message("'Triangulation' package is available.")
    } else {
      message("'Triangulation' package not available.")
      message("Install with: remotes::install_github('FIRST-Data-Lab/Triangulation')")
    }
    #> 'Triangulation' package is available.

    # Proceed only if both packages are available
    if (triangulation_available) {

      # Try loading precomputed SCC object from data/
      if (!exists("SCCcomp", inherits = FALSE) &&
          "SCCcomp" %in% data(package = "neuroSCC")$results[, "Item"]) {

        message("Loading precomputed SCC object from package data...")
        suppressMessages(data("SCCcomp", package = "neuroSCC"))

      } else if (!exists("SCCcomp", inherits = FALSE)) {

        message("Precomputed object not found. Running SCC estimation...")

        # 1. Prepare contour and triangulation
        Z <- as.matrix(contours[[1]][, c("x", "y")])
        VT <- Triangulation::TriMesh(contours[[1]], n = 15)
        V <- as.matrix(VT[[1]])
        Tr <- as.matrix(VT[[2]])

        # 2. Run SCC estimation
        SCCcomp <- ImageSCC::scc.image(
          Ya = SCC_AD_expanded,
          Yb = SCC_CN,
          Z = Z,
          d.est = 5,
          d.band = 2,
          r = 1,
          V.est.a = V,
          Tr.est.a = Tr,
          V.band.a = V,
          Tr.band.a = Tr,
          penalty = TRUE,
          lambda = 10^{seq(-6, 3, 0.5)},
          alpha.grid = c(0.10, 0.05, 0.01),
          adjust.sigma = TRUE
        )
      }
    }
    #> Loading precomputed SCC object from package data...

    if (!exists("SCCcomp", inherits = FALSE)) {
      data("SCCcomp", package = "neuroSCC")
    }

# Visualizing the SCC Result

Although the main value of a one-vs-group SCC analysis is inferential rather than visual, it can still be useful to inspect the estimated mean function and its SCC bands. The following plot provides a compact visual summary of the one-group SCC object included with the package.

    data("sccOneGroup", package = "neuroSCC")

    plotSCCpanel(
      scc = sccOneGroup,
      title = "SCC FOR CONTROL GROUP"
    )

# Visualizing Group Means and SCC Differences

Once the SCC result is available, it is often useful to visualize both group mean estimates together with the SCC-detected differences. The panel below provides a compact summary of the two-group comparison.

    plotSCCcomparisonPanel(
      scc = SCCcomp,
      title = "SCC COMPARISON PANEL"
    )

# Extracting Significant Points and Evaluating Metrics

Once the SCC computation results (`SCCcomp`) are available, we can extract significant points and compute relevant performance metrics:

    # Extract significant points
    significantPoints <- getPoints(SCCcomp)

    # Load example true ROI data from the package
    roi_path <- system.file("extdata", "ROIsample_Region2_18.nii.gz", package = "neuroSCC")
    trueROI <- processROIs(roi_path, region = "Region2", number = "18", save = FALSE)
    #> Loading NIfTI file...

    # Get dimensions for total coordinates
    dimensions <- getDimensions(roi_path)
    totalCoords <- expand.grid(x = 1:dimensions$xDim, y = 1:dimensions$yDim)

    # Calculate performance metrics
    metrics <- calculateMetrics(
      detectedPoints = significantPoints$positivePoints,
      truePoints = trueROI,
      totalCoords = dimensions,
      regionName = "SinglePatient_vs_Group"
    )

    # Display metrics
    print(metrics)
    #>                   region sensitivity specificity      PPV      NPV
    #> 1 SinglePatient_vs_Group    85.12872    95.62596 97.22839 78.10603

# Visual Validation Against ROI and SPM

In addition to numerical metrics, it can be useful to visually compare the ground-truth ROI, SCC detections, and SPM detections over the same anatomical background. The following panel provides that comparison.

    paramZ <- 35

    controlPattern <- "^syntheticControl.*\\.nii\\.gz$"
    databaseCN <- databaseCreator(pattern = controlPattern, control = TRUE, quiet = TRUE)
    matrixCN <- matrixCreator(database = databaseCN, paramZ = paramZ, quiet = TRUE)
    matrixCN <- meanNormalization(matrixCN)
    #>
    #>  Mean before normalization: 4.345851
    #>
    #>  Normalization completed.

    roiFile <- system.file("extdata", "ROIsample_Region2_18.nii.gz", package = "neuroSCC")
    truePoints <- processROIs(roiFile, region = "Region2", number = "18", save = FALSE)
    #> Loading NIfTI file...
    roiPoints <- subset(truePoints, z == paramZ & pet == 1, select = c("x", "y"))

    spmFile <- system.file("extdata", "binary.nii.gz", package = "neuroSCC")
    spmPoints <- getSPMbinary(spmFile, paramZ = paramZ)

    plotValidationPanel(
      template = roiFile,
      backgroundMatrix = matrixCN,
      roiPoints = roiPoints,
      sccPoints = list(
        positivePoints = getPoints(SCCcomp)$positivePoints,
        negativePoints = data.frame(x = numeric(0), y = numeric(0))
      ),
      spmPoints = spmPoints,
      title = "Performance Validation Panel",
      label1 = "Ground Truth (ROI)",
      label2 = "SCC Detected",
      label3 = "SPM Detected"
    )

# Conclusion

This vignette provides a reproducible example of conducting a single patient vs group SCC analysis using `neuroSCC`. Due to computational constraints, actual SCC computations are skipped and precomputed data is loaded instead. Users are encouraged to adapt parameters and explore further on their datasets.