Metadata-Version: 2.5
Name: spata
Version: 0.5.0
Summary: SPATA
Author-email: João Vitorino <jpmvo@outlook.com>
Requires-Python: >=3.9
Description-Content-Type: text/x-rst
License-Expression: MIT
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Adaptive Technologies
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
License-File: LICENSE.txt
Requires-Dist: numpy>=2.0.2,<3
Requires-Dist: joblib>=1.4.2,<2 ; extra == "jobs"
Requires-Dist: matplotlib>=3.7.5,<4 ; extra == "plots"
Project-URL: Documentation, https://spata.readthedocs.io/en/latest/
Project-URL: Homepage, https://github.com/vitorinojoao/spata
Provides-Extra: jobs
Provides-Extra: plots
Import-Name: spata

This is SPATA
=============

A deterministic method to analyze the statistical patterns of tabular datasets and the decision boundaries of machine learning models.

-----

If you use SPATA, please cite the primary conference paper: `https://doi.org/10.1007/978-3-032-19102-1_22 <https://doi.org/10.1007/978-3-032-19102-1_22>`_

Check out the official documentation: `https://spata.readthedocs.io/en/latest <https://spata.readthedocs.io/en/latest/>`_

Explore the open source code repository: `https://github.com/vitorinojoao/spata <https://github.com/vitorinojoao/spata>`_

Install it from the Python Package Index: `https://pypi.org/project/spata <https://pypi.org/project/spata/>`_

-----

How it works
------------

The summarization of any tabular dataset or model is provided in a **SPATA Card**, which defines a domain-independent representation.

Each feature (column) is converted into a set of hierarchical **projections**, and all the data instances (rows) are aggregated into **regions** of a discrete space.

It is saved in the following JSON structure:

.. code:: python

    {
        "type": "spata",
        "subtype": "card",
        "hexdigest": hash of base Card object,
        "subhexdigest": hash of base Card object,
        "info": {
            "name": str,
            "timestamp": int,
            "granularity": int,
            "dtype_optimized": numpy.dtype for "int8" or "int16" or "int32",
            "dtype_safe": numpy.dtype for "int8" or "int16" or "int32" or "int64",
            "n_features": int,
            "n_projections": int,
            "n_inverses": int,
            "n_regions": int,
        },
        "features": {
            f1: {
                "name": str,
                "init": "inferred" or "configured",
                "type": "continuous" or "discrete" or "categorical",
                "dtype": numpy.dtype for any numeric scalar type,
                "n_projections": int,
                "projection_min": p1,
                "projection_max": p2,
            },
            f2: { ... },
            ...
        },
        "projections": {
            f1: {
                p1: {
                    "bin_min": float,
                    "bin_max": float,
                    "n_inverses": int,
                    "n_regions": int,
                },
                p2: { ... },
                ...
            },
            f2: {
                p1: { ... },
                p2: { ... },
                ...
            },
            ...
        },
        "regions": {
            (p1, p1, ...): {
                "n_inverses": int,
            },
            (p1, p2, ...): { ... },
            (p2, p1, ...): { ... },
            (p2, p2, ...): { ... },
            ...
        }
    }

The analysis of the regions of a tabular dataset is provided in a **SPATA Data Card**, using its target labels.

The analysis of all the possible regions of a model's decision boundaries is provided in a **SPATA Model Card**, using its prediction function.

Since each **projection** of each **feature** can have a different effect across each **class**, independent neutrosophic sets are computed with Truth, Indeterminacy and Falsity values.

New fields are added to the JSON structure:

.. code:: python

    {
        "type": "spata",
        "subtype": "datacard" or "modelcard",
        "hexdigest": hash of base Card object,
        "subhexdigest": hash of DataCard or ModelCard object,
        "info": {
            "n_classes": int,
            "n_generated": int,
            ...
        },
        "features": {
            f1: {
                "truth_min": float,
                "truth_max": float,
                "indeterminacy_min": float,
                "indeterminacy_max": float,
                "falsity_min": float,
                "falsity_max": float,
                ...
            },
            ...
        },
        "classes": {
            k1: {
                "name": str,
                "init": "inferred" or "configured",
                "truth_min": float,
                "truth_max": float,
                "indeterminacy_min": float,
                "indeterminacy_max": float,
                "falsity_min": float,
                "falsity_max": float,
                "n_regions": int,
                "region_min": (p1, p1, ...),
                "region_max": (p2, p2, ...),
                "n_inverses": int,
                "n_generated": int,
            },
            k2: { ... },
            ...
        },
        "projections": {
            f1: {
                p1: {
                    "n_generated": int,
                    "class_projections": {
                        k1: {
                            "truth": float,
                            "indeterminacy": float,
                            "falsity": float,
                        },
                        k2: { ... },
                        ...
                    },
                    ...
                },
                ...
            },
            ...
        },
        "regions": {
            (p1, p1, ...): {
                "n_generated": int,
                "class_regions": {
                    k1: {
                        "n_inverses": int,
                        "n_generated": int,
                    },
                    k2: { ... },
                    ...
                },
                ...
            },
            ...
        }
    }

(Note: "n_generated" fields are only available in a ModelCard)

-----

How to use
----------

This package provides an optimized open-source implementation of SPATA, and an additional Plotter object for interactive visualization:

.. code:: python

    from spata import Card, Plotter

    from spata.data import DataCard
    from spata.model import ModelCard

To save and visualize the analysis, you can use the following functions:

.. code:: python

    # Create a base card with custom granularity for the discrete space
    # Using X_train
    card = Card(X_train, granularity=3, name="This is SPATA", keep_base=True)

    # Analyze the regions of a tabular dataset
    # Using y_train
    d_card = DataCard(card, y_train, classification=True)
    
    d_json = d_card.save(filepath="d_card.json")
    d_summary, d_fig = d_card.summary(plotter=True, features=None, classes=None)

    # Analyze all the possible regions of a model's decision boundaries
    # Using model_prediction_function
    m_card = ModelCard(card, model_prediction_function, classification=True)

    m_json = m_card.save(filepath="m_card.json")
    m_summary, m_fig = m_card.summary(plotter=True, features=None, classes=None)

To perform synthetic data generation, you can use the following functions:

.. code:: python

    # Convert new data into the discrete space
    # Using X_test
    regions = card.convert(X_test)

    # Generate only for specific regions
    X_test_gen = card.generate(regions, quantity=9, realistic=True)

    # Generate across all the possible regions
    X_gen = card.generate(card.permutate(), quantity=9, realistic=True)

