How takikoi/nirscanner-python works
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).