Metadata-Version: 2.4
Name: sigma-pi
Version: 0.1.0
Summary: A lightweight tool to calculate Predictive Integrity (PI) for PyTorch models.
Project-URL: Homepage, https://github.com/dmf-archive/SigmaPI
Project-URL: Bug Tracker, https://github.com/dmf-archive/SigmaPI/issues
Author-email: dmf-archive <Lin.Rui.ipwt@proton.me>
License: MIT License
        
        Copyright (c) 2025
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT of OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Requires-Dist: hatch>=1.14.1
Requires-Dist: mypy>=1.14.1
Requires-Dist: timm
Requires-Dist: torch==2.5.1+cu121
Requires-Dist: torchvision>=0.20.1
Description-Content-Type: text/markdown

# SigmaPI: Observe the Mind of Your Model

[![PyPI version](https://badge.fury.io/py/sigma-pi.svg)](https://badge.fury.io/py/sigma-pi)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**SigmaPI** is a lightweight, universal SDK to calculate Predictive Integrity (PI), a metric from the Integrated Predictive Workspace Theory (IPWT) of consciousness. It provides a powerful, real-time proxy for your model's "cognitive state" during training.

Stop just looking at `loss`. Start observing how your model _learns_.

## Why Use SigmaPI?

- **Early Warning for Training Instability:** Detects subtle shifts in model "cognition" before loss metrics diverge.
- **Insight into OOD Impact:** Quantifies the "surprise" your model experiences when encountering out-of-distribution data.
- **Understanding Model Overfitting:** Reveals when your model's internal world becomes too rigid or too chaotic.
- **Quantifying Cognitive Load:** Provides a novel metric for the "effort" your model expends to integrate new information.

## What is Predictive Integrity (PI)?

PI is a single score from 0 to 1 that reflects the integrity of a model's internal world model. It's calculated from three core components:

1. **Epsilon (ε):** The raw prediction error (scalar loss value).
2. **Tau (τ):** The model's own uncertainty, derived from its output logits.
3. **Surprise (S):** The global gradient norm. How much does the model need to change its "worldview" to accommodate new data?

A high PI score indicates a healthy learning state: the model is accurate, confident, and stable. A sudden drop in PI can signal overfitting, bad data, or an impending collapse in training stability, often _before_ the loss function shows clear signs of trouble.

## Model Zoo: Comparative Results

Here's a comparison of how different architectures handle in-distribution (CIFAR-10) vs. out-of-distribution (SVHN) data.

| SimpleCNN (on MNIST) | ResNet (on CIFAR-10) | Vision Transformer (on CIFAR-10) |
| :---: | :---: | :---: |
| *A simple CNN on a simple task.* | *Deeper architecture, but struggles with OOD.* | *ViT shows more robust PI on OOD data.* |
| <img src="output/20250625-SimpleCNN_Step.png" width="300"> | <img src="output/20250625-ResNet-CIFAR10-OOD_Step.png" width="300"> | <img src="output\20250625-ViT-CIFAR10-OOD_Step.png" width="300"> |

## Installation

```bash
pip install sigma-pi
```

## How to Use

Integrate it into your PyTorch training loop in just three steps:

```python
from main import SigmaPI

# 1. Initialize the monitor
pi_monitor = SigmaPI(alpha=1.0, gamma=0.5)

# 2. In your training loop
logits = model(data)
loss = loss_fn(logits, target)
loss.backward()  # Compute gradients before PI calculation

# 3. Calculate PI metrics
pi_metrics = pi_monitor.calculate(
    model=model,
    loss_epsilon=loss,
    logits=logits
)

# Access metrics
print(f"Loss: {loss.item():.4f}, PI: {pi_metrics['pi_score']:.4f}, Surprise: {pi_metrics['surprise']:.4f}")
```

The returned `pi_metrics` dictionary contains:

- `pi_score`: The overall predictive integrity (0-1)
- `surprise`: Gradient norm indicating model adaptation
- `normalized_error`: Error scaled by model uncertainty
- `cognitive_cost`: Combined cost of error and surprise
- Additional component metrics for detailed analysis

## Further Reading

PI is a concept derived from the **Integrated Predictive Workspace Theory (IPWT)**, a computational theory of consciousness. To understand the deep theory behind this tool, please refer to:

- **IPWT:** <https://github.com/dmf-archive/IPWT>
- **OpenPoPI:** [Private] - The original research-grade implementation used to validate the theory on EEG data.

## License

This project is licensed under the MIT License.
