A modular Python application for Small-Angle X-ray Scattering (SAXS) Guinier analysis, featuring traditional and machine learning-based fitting algorithms.
- Multiple Fitting Algorithms: Traditional least squares, robust methods, and scikit-learn implementations
- Interactive GUI: User-friendly interface with real-time plotting and modern icon
- Data Processing: Background subtraction, normalization, and SNR filtering
- Physical Validation: Automatic q·Rg range checking and quality assessment
- Cross-validation: Model validation and comparison capabilities
- Export Functionality: Save results and fitted data to CSV files
- Clone or download the repository
- Install required dependencies:
pip install -r requirements.txtnumpy- Numerical computationsscipy- Scientific computing and optimizationpandas- Data manipulation and analysismatplotlib- Plotting and visualizationscikit-learn- Machine learning algorithms (optional but recommended)PySide6- Modern GUI framework (recommended)tkinter- Classic GUI framework (fallback, usually included with Python)
python guinier_analysis.pyThe application automatically detects and uses the best available GUI framework:
- PySide6 Interface (recommended): Modern, professional interface with enhanced styling
- Tkinter Interface (fallback): Classic interface for compatibility
For the best experience, install PySide6:
pip install PySide6from guinier_core import GuinierAnalyzer
# Initialize analyzer
analyzer = GuinierAnalyzer()
# Load SAXS data
analyzer.load_data('your_data.grad')
# Apply corrections
analyzer.apply_corrections(bg_value=0.1, norm_factor=1.0)
# Set fitting range
analyzer.auto_range() # Automatic range based on q·Rg ≤ 1.3
# Perform fitting
result = analyzer.perform_fit() # Traditional method
sklearn_result = analyzer.fit_with_sklearn('huber') # Sklearn method
# Compare methods
comparison = analyzer.compare_methods()guinier/
├── guinier_analysis.py # Main entry point (auto-detects GUI)
├── guinier_core.py # Core analysis engine
├── guinier_gui.py # Classic tkinter GUI
├── guinier_gui_pyside6.py # Modern PySide6 GUI (recommended)
├── guinier_icon.png # Application icon (PNG format)
├── guinier_icon.ico # Application icon (ICO format)
├── requirements.txt # Dependencies
├── README.md # This file
├── examples/ # Usage examples
│ └── example_usage.py
└── tests/ # Test files
└── test_gui_sklearn.py
- traditional: Standard least squares fitting using
numpy.polyfit - traditional_robust: Robust fitting using Theil-Sen and Huber methods
- linear: Linear regression
- huber: Huber regression (robust to outliers)
- ridge: Ridge regression (L2 regularization)
- theilsen: Theil-Sen regression (very robust)
- ransac: RANSAC regression (random sample consensus)
- For clean data: Use
traditionalorlinear - For noisy data: Use
huberortraditional_robust - For data with outliers: Use
theilsenorransac - For overfitting concerns: Use
ridge
Main analysis class with comprehensive functionality:
Key Methods:
load_data(filename): Load SAXS data from various formatsapply_corrections(bg_value, norm_factor, snr_threshold): Process raw dataauto_range(q_rg_limit=1.3): Automatically set Guinier rangeperform_fit(use_robust=True): Traditional Guinier fittingfit_with_sklearn(algorithm, cross_validate=False): ML-based fittingcompare_methods(): Compare all available methodssave_results(filename): Export results to CSV
Results Properties:
Rg: Radius of gyration (Å)I0: Zero-angle intensityr_squared: Coefficient of determinationchi_squared: Reduced chi-squaredsklearn_models: Dictionary of fitted ML models
- PySide6 Interface (recommended): Modern, professional styling with enhanced user experience
- Tkinter Interface (fallback): Classic interface for compatibility
- Support for
.grad,.txt, and.csvformats - Automatic format detection
- Data validation and error handling
- Progress indicators for long operations
- Background subtraction
- Normalization factor adjustment
- SNR threshold filtering
- Manual and automatic range selection
- Real-time parameter updates
- Algorithm selection dropdown
- Cross-validation toggle
- Robust fitting option
- Method comparison tool
- Best model recommendations
- Raw SAXS data plot with error bars
- Guinier plot (ln(I) vs q²) with fit visualization
- Residuals analysis with statistics
- Real-time updates and interactive plots
- High-quality plot export (PDF)
- GRAD files (
.grad): Standard SAXS analysis format - Text files (
.txt,.csv): Column-separated data- Column 1: q values (Å⁻¹)
- Column 2: I(q) values
- Column 3: dI (errors) - optional
- Results CSV: Fitting parameters and quality metrics
- Data CSV: Processed data points and fitted curves
The application automatically validates Guinier fitting according to established principles:
- q·Rg limit: Ensures q·Rg ≤ 1.3 for valid approximation
- Positive intensity: Validates I(q) > 0
- Negative slope: Ensures proper Guinier behavior
- Error propagation: Handles uncertainties in fitting
When enabled, the application performs k-fold cross-validation:
- Evaluates model stability and generalization
- Provides mean and standard deviation of scores
- Helps in algorithm selection
- Detects overfitting
The application includes comprehensive error handling:
- Invalid data format detection
- Numerical computation errors
- File I/O errors
- Algorithm convergence issues
See examples/example_usage.py for detailed usage examples including:
- Basic analysis workflow
- Advanced parameter tuning
- Method comparison
- Results interpretation
Run the test suite:
python tests/test_gui_sklearn.py- Scikit-learn not found: Install with
pip install scikit-learn - PySide6 not found: Install with
pip install PySide6for the modern interface - Fitting fails: Check data quality and range selection
- GUI not starting: Ensure either PySide6 or tkinter is available
The application automatically selects the best available GUI framework:
- First tries PySide6 (modern interface)
- Falls back to tkinter (classic interface)
- If neither is available, displays installation instructions
- Poor fit quality: Try different algorithms or adjust range
- GUI not responding: Restart the application or check system requirements
- Use automatic range selection for optimal results
- Enable cross-validation for method comparison
- Choose robust algorithms for noisy data
- Validate q·Rg limits for physical accuracy
This is a focused analysis tool. For enhancements:
- Follow the modular architecture
- Add tests for new features
- Update documentation
- Ensure backward compatibility
This project is provided as-is for scientific and educational use.