# takikoi/nirscanner-python architecture

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

- Interactive diagram: https://gitdiagram.com/takikoi/nirscanner-python
- Repository: https://github.com/takikoi/nirscanner-python

## Overview

##### Overall Project Context
- The project is not a full-stack web application but a hardware interface library. Its primary role is to allow Python programs to “talk” to the NIRScanner Nano.
- The README explains that the user compiles the native code using provided shell scripts (for Python 2 or Python 3) to produce a shared object file (_NIRScanner.so) that, together with the Python wrapper (NIRS.py), becomes the front-facing API to the hardware.

##### Main Components
a. Client Application Layer:
- This is the user’s Python code (or Jupyter notebooks) that imports and instantiates the NIRS class from NIRS.py.

b. Python Wrapper:
- The NIRS.py file is the Python class that wraps around the compiled library and abstracts its functionalities.
- Highlighting that it directly loads the shared object (_NIRScanner.so) to provide methods like “scan()”, “config”, etc.

c. Compiled Extension Module:
- The shared object (_NIRScanner.so) is produced by compiling the C/C++ source code (found inside the src/ directory). This module contains:
- API Layer (e.g., API.cpp, API.h)
- NIRScanner Implementation (e.g., NIRScanner.cpp, NIRScanner.h, and the SWIG-generated wrap file NIRScanner_wrap.cxx).
- Lower-level communications including Serial communications (Serial.c, Serial.h), HID communications (hid.c, hidapi.h), and USB interfacing.
- This component should be labeled “Native Extension (C/C++).”

d. Build & Pre-Compilation Artifacts:
- The src/scripts directory contains scripts for compiling the native code, and the CMakeLists.txt in the src directory is used to build the library.
- The “pre-compile” folder contains Hadamard matrices and related calibration files that are likely used during scanning or data processing.
- As “Build & Calibration Assets.”

e. Underlying Hardware Interface:
- Although not explicitly part of the source, the ultimate target is the NIRScanner Nano hardware which the lower-level code communicates with via USB/serial (using libusb and libudev).
- This as “NIRScanner Hardware” with a note on the USB/Serial interface.

##### Relationships and Data Flow
- Inside the native extension module, show internal subdivisions or sub-components:
- API and Scanner logic (including NIRScanner.cpp and API.cpp)
- Communication & Data Acquisition (Serial.c/h, hid.c, etc.)
- Calibration/Configuration Layer (using Hadamard matrix files from the pre-compile folder)

##### Architectural Patterns/Principles
- Layered Architecture: The library is built in layers—from high-level Python interface down to low-level hardware communication.
- Separation of Concerns: There is a clear distinction between the Python wrapper (user interface) and the compiled native code (implementation details, performance-critical scanning operations).

a. Top Layer: “Python Client” (User Code / Jupyter Notebook)
b. Middle Layer: “Python Wrapper (NIRS.py)” that loads _NIRScanner.so.
c. Next Layer: “Native Extension (C/C++ Code)” which itself can be subdivided into:
- API Module (API.cpp/h, NIRScanner.cpp/h)
- Communication Module (Serial.c/h, hid.c, usb.cpp/h)
- Calibration Data (pre-compiled Hadamard matrices)
d. Bottom: “NIRScanner Hardware” illustrating the external device interface.
e. Side Note: Build & Deployment Process (CMake, compile_py2.sh/py3.sh).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Python Client Layer
    subgraph "Python Client"
        A1["Test Notebook"]:::client
        A2["Test Script"]:::client
    end

    %% Python Wrapper Layer
    subgraph "Python Wrapper Layer"
        B["Python Wrapper (NIRS.py)"]:::wrapper
    end

    %% Native Extension (C/C++ Code)
    subgraph "Native Extension (C/C++ Code)"
        %% API Module
        subgraph "API Module"
            C1["API.cpp"]:::native
            C2["API.h"]:::native
            C3["NIRScanner.cpp"]:::native
            C4["NIRScanner.h"]:::native
            C5["SWIG Wrapper (NIRScanner_wrap.cxx)"]:::native
        end
        %% Communications Module
        subgraph "Communications Module"
            D1["Serial.c"]:::native
            D2["Serial.h"]:::native
            D3["hid.c"]:::native
            D4["hidapi.h"]:::native
            D5["usb.cpp"]:::native
            D6["usb.h"]:::native
            D7["tpl.c"]:::native
            D8["tpl.h"]:::native
        end
        %% Calibration Data
        subgraph "Calibration Data"
            E["Calibration Files (pre-compile)"]:::calibration
        end
    end

    %% Build & Calibration Assets
    subgraph "Build & Calibration Assets"
        G["CMake Config"]:::build
        H["Compile Py2 Script"]:::build
        I["Compile Py3 Script"]:::build
        J["GDB Server Script"]:::build
        K["Pre-compile Directory"]:::build
    end

    %% Underlying Hardware Interface
    subgraph "NIRScanner Hardware"
        F["NIRScanner Nano Hardware"]:::hardware
    end

    %% Relationships and Data Flow
    A1 -->|"calls"| B
    A2 -->|"calls"| B
    B -->|"loads"| C1

    G -->|"builds"| C1
    H -->|"builds"| C1
    I -->|"builds"| C1
    J -->|"builds"| C1

    K -->|"supplies"| E
    E -->|"calibrates"| C1

    C1 -->|"sendsCommand"| F
    D1 -->|"communicates"| F
    D3 -->|"communicates"| F

    %% Styles
    classDef client fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef wrapper fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef native fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef build fill:#ffe4b5,stroke:#000,stroke-width:2px;
    classDef calibration fill:#f0e68c,stroke:#000,stroke-width:2px;
    classDef hardware fill:#ff7f7f,stroke:#000,stroke-width:2px;

    %% Click Events
    click A1 "https://github.com/takikoi/nirscanner-python/blob/master/test.ipynb"
    click A2 "https://github.com/takikoi/nirscanner-python/blob/master/test.py"
    click B "https://github.com/takikoi/nirscanner-python/blob/master/NIRS.py"
    click C1 "https://github.com/takikoi/nirscanner-python/blob/master/src/API.cpp"
    click C2 "https://github.com/takikoi/nirscanner-python/blob/master/src/API.h"
    click C3 "https://github.com/takikoi/nirscanner-python/blob/master/src/NIRScanner.cpp"
    click C4 "https://github.com/takikoi/nirscanner-python/blob/master/src/NIRScanner.h"
    click C5 "https://github.com/takikoi/nirscanner-python/blob/master/src/NIRScanner_wrap.cxx"
    click D1 "https://github.com/takikoi/nirscanner-python/blob/master/src/Serial.c"
    click D2 "https://github.com/takikoi/nirscanner-python/blob/master/src/Serial.h"
    click D3 "https://github.com/takikoi/nirscanner-python/blob/master/src/hid.c"
    click D4 "https://github.com/takikoi/nirscanner-python/blob/master/src/hidapi.h"
    click D5 "https://github.com/takikoi/nirscanner-python/blob/master/src/usb.cpp"
    click D6 "https://github.com/takikoi/nirscanner-python/blob/master/src/usb.h"
    click D7 "https://github.com/takikoi/nirscanner-python/blob/master/src/tpl.c"
    click D8 "https://github.com/takikoi/nirscanner-python/blob/master/src/tpl.h"
    click G "https://github.com/takikoi/nirscanner-python/blob/master/src/CMakeLists.txt"
    click H "https://github.com/takikoi/nirscanner-python/blob/master/src/scripts/compile_py2.sh"
    click I "https://github.com/takikoi/nirscanner-python/blob/master/src/scripts/compile_py3.sh"
    click J "https://github.com/takikoi/nirscanner-python/blob/master/src/scripts/run_gdbserver.sh"
    click K "https://github.com/takikoi/nirscanner-python/tree/master/src/pre-compile"
```
