python-seabreeze

repository·main·Indexed 42 days ago

https://github.com/ap--/python-seabreeze

A Python library for accessing Ocean Optics spectrometers. It provides a wrapper around the official SeaBreeze C library (cseabreeze) and a pyUSB-based pure Python reimplementation (pyseabreeze) for cross-platform compatibility and easier extension. The library includes the Spectrometer class for model-independent hardware interaction and the seabreeze_os_setup tool for automating driver and udev rule installation on Windows and Linux.

Tokens
9.6K
Snippets
30
Records
39
Agent score
54%

What's inside python-seabreeze

  1. What is python-seabreeze

    main

    python-seabreeze is a Python module designed to provide easy access to Ocean Optics spectrometers. It acts as a wrapper around the official SeaBreeze library provided by Ocean Optics to facilitate communication with the hardware.

    If the official SeaBreeze library is not available on the system, the module can fall back to a pyUSB-based reimplementation to maintain functionality.

  2. How seabreeze_os_setup works on Linux and Windows

    main

    The seabreeze_os_setup tool performs different actions based on your operating system:

    Linux

    It automates the installation of udev rules to grant permission to access the spectrometer:

    1. Downloads the 10-oceanoptics.rules file.
    2. Displays the contents for your review.
    3. Upon confirmation, copies the file to /etc/udev/rules.d/ (with mode 644).
    4. Executes sudo udevadm control --reload-rules to apply the changes.

    Windows

    It automates the driver installation process:

    1. Downloads and extracts the windows-driver-files.zip archive.
    2. Runs pnputil -i -a *.inf within the extracted folder using an administrative shell.

    macOS (OSX)

    No additional setup is required. Running seabreeze_os_setup on macOS is a no-op.

  3. Choose between cseabreeze and pyseabreeze backends

    main

    python-seabreeze provides two backend interfaces for communicating with spectrometers:

    1. cseabreeze: The default backend. It is a wrapper around the official Ocean Optics seabreeze C++ library. It is highly feature-complete but difficult to extend if you are not familiar with C++ and Cython.
    2. pyseabreeze: A pure Python implementation that uses pyUSB to communicate via the Ocean Binary Protocol. It is easier to extend for Python developers but is currently less feature-complete than cseabreeze.
  4. Access spectrometer features via SeaBreezeDevice

    main

    Functionality for specific spectrometer tasks is organized into feature classes. You do not instantiate these classes directly. Instead, they are provided through the features property or the shorthand f property on a SeaBreezeDevice instance.

    Example access pattern:

    # Accessing features via the 'features' property
    feature = device.features.SeaBreezeSpectrometerFeature
    
    # Accessing features via the 'f' shorthand
    feature = device.f.SeaBreezeSpectrometerFeature
  5. Open a spectrometer

    main

    You can initialize a Spectrometer object using several methods depending on how you want to identify the device:

    1. First available: Use Spectrometer.from_first_available() to quickly connect to the first detected device.
    2. Specific device from list: Use list_devices() to get a list of all available SeaBreezeDevice objects, then pass a specific device to the Spectrometer constructor.
    3. Serial number: Use Spectrometer.from_serial_number("SERIAL_NUMBER") to connect to a specific device via its unique serial number.
    # option 1: first available
    from seabreeze.spectrometers import Spectrometer
    spec = Spectrometer.from_first_available()
    
    # option 2: from a list of devices
    from seabreeze.spectrometers import Spectrometer, list_devices
    devices = list_devices()
    spec = Spectrometer(devices[1])
    
    # option 3: by serial number
    from seabreeze.spectrometers import Spectrometer
    spec = Spectrometer.from_serial_number("F01234")
  6. Install seabreeze via pip

    main

    You can install seabreeze using pip. Depending on which backend you require, use one of the following commands:

    • cseabreeze backend only: The default installation.
    • Both cseabreeze and pyseabreeze backends: Installs both available backends.
    • pyseabreeze backend only: Installs only the pure-python backend using specific flags to exclude the C backend.
    # install only cseabreeze backend
    pip install seabreeze
    
    # install both cseabreeze and pyseabreeze backend
    pip install seabreeze[pyseabreeze]
    
    # install only pyseabreeze backend
    pip install seabreeze[pyseabreeze] --no-use-pep517 --no-build-isolation --install-option="--without-cseabreeze"
  7. Build seabreeze manually

    main

    If you need to build python-seabreeze from source, follow these steps to install dependencies and compile the package.

    Step 1: Install System Dependencies

    Linux Install a compiler and libusb development headers based on your distribution:

    • Debian/Ubuntu: sudo apt-get install git-all build-essential libusb-dev
    • RPM (Fedora/CentOS): sudo yum install git-all gcc gcc-c++ make libusb-devel
    • Arch: sudo pacman -S base-devel libusb libusb-compat libusb-devel make gcc

    OSX Ensure you have the latest XCode installed.

    Windows Using chocolatey, install the build environment and Windows Driver Kit:

    choco install visualcpp-build-tools
    choco install windows-sdk-10.1
    choco install windowsdriverkit10

    Step 2: Prepare pip

    Upgrade pip to the latest version:

    python -m pip install --upgrade pip

    Step 3: Clone and Install

    Clone the repository and install using pip:

    git clone https://github.com/ap--/python-seabreeze.git python-seabreeze
    cd python-seabreeze
    python -m pip install .
    # Example for Debian/Ubuntu
    sudo apt-get install git-all build-essential libusb-dev
    
    # Example for Windows (via Chocolatey)
    choco install visualcpp-build-tools
    choco install windows-sdk-10.1
    choco install windowsdriverkit10
    
    # Final build steps
    git clone https://github.com/ap--/python-seabreeze.git python-seabreeze
    cd python-seabreeze
    python -m pip install .
  8. Perform OS-specific setup for spectrometer access

    main

    To allow user-space access to USB spectrometers, you must run the seabreeze_os_setup script provided with the installation. This script performs the following:

    • Linux: Installs udev rules.
    • Windows: Installs necessary spectrometer drivers.

    Run the following command in your terminal:

    $ seabreeze_os_setup
  9. Install seabreeze via conda

    main

    It is recommended to install the conda-forge package when working within a conda environment instead of using pip. Note that the package name was changed from python-seabreeze to seabreeze to match PyPI naming.

    Use the following command to install from the conda-forge channel:

    conda install -c conda-forge seabreeze
  10. Use the pyseabreeze backend

    main

    To use the pyseabreeze backend (which requires pyusb), you must call seabreeze.use('pyseabreeze') before importing seabreeze.spectrometers.

    import seabreeze
    seabreeze.use('pyseabreeze')
    import seabreeze.spectrometers as sb