# nowosad/hsitools architecture

> Architecture diagram of the GitHub repository nowosad/hsitools, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nowosad/hsitools
- Repository: https://github.com/nowosad/hsitools

## Overview

##### Project Type & Purpose
- HSItools is a specialized R package. Its primary purpose is to provide an analytical toolkit aimed at processing hyperspectral core scanning data. It offers a Shiny-based interactive user interface, a suite of functions for spectral calculations, various data manipulation and filtering routines, and visualization tools.
- The package is modularized into functions that handle different aspects of data ingestion, processing (e.g., normalization, filtering, extraction), and visualization (e.g., RGB plotting, spectral profiles).

##### Analyzing the File Structure for Architectural Insights
- Top-Level Files: Standard R package files such as DESCRIPTION, NAMESPACE, and LICENSE verify that this is a well-structured R package.
- R Directory: Contains multiple source scripts that implement key features:
- app.R, simpleShiny.R → Handle Shiny app integration.
- data.R, spectral_calculations.R, extractors.R, filters.R, normalization.R → Core processing and analysis pipeline.
- plotting.R → Visualization routines.
- utils.R, app_helpers.R → Provide helper functions and utilities.
- workflow.R → Likely orchestrates the execution flow between processing steps.
- Data-Related Directories:
- inst/extdata and data → Contain sample data (e.g., TIFF images, RDS objects) used for testing, examples, or demonstrations.
- Documentation:
- man and vignettes directories provide in-depth function documentation and tutorials, indicating a high level of usability and maintainability.
- Infrastructure Scripts:
- GitHub workflows and installation scripts (install.R) reveal how the package is built and tested.

##### Main Components
a. User Interface (UI)
- Shiny Application: Represent the interactive UI that allows users to load and choose analysis options (e.g., run_core() and simpleShiny functions).
b. Input & Data Management
- Data Sources: Components that load raw hyperspectral images along with auxiliary metadata from ‘data’, ‘inst/extdata’, and ‘data-raw’ directories.
- Preprocessing: Modules for initial data preparation (R/data.R, prepare_core() in the workflow).
c. Processing Pipeline
- Spectral Calculations (R/spectral_calculations.R)
- Data Normalization (R/normalization.R)
- Extraction Functions (R/extractors.R)
- Filtering (R/filters.R)
- Additional data manipulation (R/app_helpers.R, R/utils.R)
- Workflow Management: A component representing the orchestration of these operations (R/workflow.R) which sequences tasks such as reflectance calculation, applying filters, and extracting indices.
d. Visualization & Output
- Plotting Modules: Components for rendering figures and interactive plots (R/plotting.R, functions like plot_raster_rgb).
- Export/Write Modules: Functions that save outputs (e.g., stretch_raster_full writes TIFF files).

##### Relationships & Interactions
- Data Flow:
- From these modules, the processed data is passed to Visualization components and also stored/saved as outputs.
- UI Integration:
- Connectivity between the Shiny application (UI component) and the processing backend. User inputs via the UI trigger the workflow pipeline.
- Workflow Coordination:
- The workflow (orchestrator) governs the step-by-step data transformation, linking data input, processing functions, and final outputs.

##### Architectural Patterns & Design Principles
- Modular Design: Each script in the R/ directory encapsulates separate concerns (e.g., calculation, extraction, plotting) leading to a clean separation of functionality.
- Pipeline/Workflow Pattern: The analysis is performed as a series of operations applied sequentially; this is highlighted by components like prepare_core() and workflow.R.
- Interactive vs. Batch Processing: The package supports an interactive user journey via Shiny as well as scripted operations, indicating a dual-mode operation architecture.

##### Technologies, Frameworks & Libraries
- R Language: Core language for the package.
- Shiny: Used for building the interactive application.
- Likely R packages for raster data manipulation (e.g., raster, terra) and plotting (e.g., ggplot2, base plot functions) are implicitly involved.
- R package development tools: The presence of GitHub actions and R CMD check workflows denote CI/CD integration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Subgraph
    subgraph "User Interface"
        UI_App["Shiny App"]:::ui
        UI_Simple["SimpleShiny"]:::ui
        UI_Vignette["Shiny GUI (Vignette)"]:::ui
    end

    %% Data Input & Management Subgraph
    subgraph "Data Input & Management"
        DataDir["Data Directory"]:::data
        ExtData["Inst/extdata"]:::data
        DataRaw["Data-Raw Example"]:::data
    end

    %% Preprocessing Module Subgraph
    subgraph "Preprocessing Module"
        PrecScript["Data Script"]:::processing
        PrecDoc["Preprocessing Doc"]:::processing
    end

    %% Core Processing Modules Subgraph
    subgraph "Core Processing Modules"
        Norm["Normalization Module"]:::core
        Filt["Filtering Module"]:::core
        Extract["Extraction Module"]:::core
        SpecCalc["Spectral Calculation Module"]:::core
        AppHelper["App Helpers"]:::core
        Util["Utility Functions"]:::core
    end

    %% Workflow Coordination Subgraph
    subgraph "Workflow Coordination"
        Workflow["Workflow Coordinator"]:::workflow
    end

    %% Visualization & Output Subgraph
    subgraph "Visualization & Output"
        Plot["Plotting Module"]:::visualization
        PlotDoc["Visualization Doc"]:::visualization
    end

    %% Data Flow Connections
    %% Data Input to Preprocessing
    DataDir --> PrecScript
    ExtData --> PrecScript
    DataRaw --> PrecScript

    %% Preprocessing to Core Processing Modules (Chain for main processing functions)
    PrecScript --> Norm
    Norm --> Filt
    Filt --> Extract
    Extract --> SpecCalc

    %% Preprocessing also feeds Utility Functions
    PrecScript --> AppHelper
    PrecScript --> Util

    %% Core processing modules converge to Workflow
    SpecCalc --> Workflow
    AppHelper --> Workflow
    Util --> Workflow

    %% UI directly triggers Workflow Coordination
    UI_App --> Workflow
    UI_Simple --> Workflow
    UI_Vignette --> Workflow

    %% Workflow outputs to Visualization
    Workflow --> Plot
    Workflow --> PlotDoc

    %% Click Events for User Interface
    click UI_App "https://github.com/nowosad/hsitools/blob/main/R/app.R"
    click UI_Simple "https://github.com/nowosad/hsitools/blob/main/R/simpleShiny.R"
    click UI_Vignette "https://github.com/nowosad/hsitools/blob/main/vignettes/shiny_gui.Rmd"

    %% Click Events for Data Input & Management
    click DataDir "https://github.com/nowosad/hsitools/tree/main/data"
    click ExtData "https://github.com/nowosad/hsitools/tree/main/inst/extdata"
    click DataRaw "https://github.com/nowosad/hsitools/blob/main/data-raw/exampleCore.R"

    %% Click Events for Preprocessing Module
    click PrecScript "https://github.com/nowosad/hsitools/blob/main/R/data.R"
    click PrecDoc "https://github.com/nowosad/hsitools/blob/main/man/prepare_core.Rd"

    %% Click Events for Core Processing Modules
    click Norm "https://github.com/nowosad/hsitools/blob/main/R/normalization.R"
    click Filt "https://github.com/nowosad/hsitools/blob/main/R/filters.R"
    click Extract "https://github.com/nowosad/hsitools/blob/main/R/extractors.R"
    click SpecCalc "https://github.com/nowosad/hsitools/blob/main/R/spectral_calculations.R"
    click AppHelper "https://github.com/nowosad/hsitools/blob/main/R/app_helpers.R"
    click Util "https://github.com/nowosad/hsitools/blob/main/R/utils.R"

    %% Click Event for Workflow Coordination
    click Workflow "https://github.com/nowosad/hsitools/blob/main/R/workflow.R"

    %% Click Events for Visualization & Output
    click Plot "https://github.com/nowosad/hsitools/blob/main/R/plotting.R"
    click PlotDoc "https://github.com/nowosad/hsitools/blob/main/man/plot_raster_rgb.Rd"

    %% Styles Definitions
    classDef ui fill:#ffccff,stroke:#990099,stroke-width:2px;
    classDef data fill:#ccffff,stroke:#0099cc,stroke-width:2px;
    classDef processing fill:#ccffcc,stroke:#339933,stroke-width:2px;
    classDef core fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef workflow fill:#ffebcc,stroke:#ff9900,stroke-width:2px;
    classDef visualization fill:#cce5ff,stroke:#0056b3,stroke-width:2px;
```
