pt-loop


Namept-loop JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryLocal Outlier Probabilities (LoOP) implementation in PyTorch
upload_time2025-08-01 18:48:10
maintainerNone
docs_urlNone
authorOfer Hasson
requires_python>=3.11
licenseNone
keywords pytorch loop outlier-detection anomaly-detection machine-learning unsupervised-learning
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyTorch LoOP

## Introduction

`pt_loop` is a pure PyTorch implementation of the Local Outlier Probabilities (LoOP) algorithm, designed for seamless integration into PyTorch-based machine learning pipelines.
It offers high performance on both CPU and GPU (CUDA), leveraging PyTorch's tensor capabilities throughout the entire computation.

Unlike traditional implementations that might require data transfers to other libraries, `pt_loop` keeps your data on the PyTorch device (CPU or GPU) from start to finish, minimizing overhead and maximizing efficiency for large-scale anomaly detection tasks.

The original paper can be found here: <https://www.dbs.ifi.lmu.de/Publikationen/Papers/LoOP1649.pdf>

## Installation

`pt_loop` requires PyTorch.

First, ensure you have PyTorch installed (refer to the [official PyTorch website](https://pytorch.org/get-started/locally/) for installation instructions specific to your system and CUDA version).

Then, install `pt_loop` directly from PyPI:

```bash
pip install pt_loop
```

## Quick Start & Usage Examples

Here's how to get started with `pt_loop`.

```python
import torch
import matplotlib.pyplot as plt

from pt_loop import loop
```

### Basic LoOP Usage

```python
# 1. Generate some synthetic data with an obvious outlier
data = torch.cat([
    torch.randn(100, 2) * 0.5 + torch.tensor([0.0, 0.0]), # Cluster 1
    torch.randn(100, 2) * 0.5 + torch.tensor([5.0, 5.0]), # Cluster 2
    torch.tensor([[2.5, 2.5]]),                           # Clear outlier
])

# Move data to GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
data = data.to(device)

# 2. Run LoOP
print(f"Running LoOP on {device}...")
loop_scores = loop(
    data,
    k=10,                      # Number of nearest neighbors
    lambda_=3.0,               # Scaling factor
    distance_metric="l2",      # or "cosine"
)

print("\nLoOP Results:")
print(f"Scores Shape: {loop_scores.shape}")
print(f"First 5 Scores: {loop_scores[:5].tolist()}")
print(f"Last score (outlier candidate): {loop_scores[-1].item()}")

# 3. (Optional) Visualize the scores
plt.figure(figsize=(8, 6))
scatter = plt.scatter(data[:, 0].cpu(), data[:, 1].cpu(), c=loop_scores.cpu(), cmap="plasma", s=50, alpha=0.8)
plt.colorbar(scatter, label="LoOP Score (Outlier Probability)")
plt.title("Local Outlier Probabilities (LoOP)")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.grid(True)
plt.show()
```

## Contributing

Contributions are very welcome! If you find a bug, have a feature request, or want to contribute code, please feel free to:

1. Open an issue on the [GitLab Issues page](https://gitlab.com/hassonofer/pt_loop/-/issues).
1. Submit a Pull Request.

Please ensure your code adheres to the existing style (Black, isort) and passes all tests.

## License

This project is licensed under the Apache-2.0 License - see the [LICENSE](https://gitlab.com/hassonofer/pt_loop/blob/main/LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pt-loop",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "pytorch, loop, outlier-detection, anomaly-detection, machine-learning, unsupervised-learning",
    "author": "Ofer Hasson",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/59/0b/5b06c6928ca2ccdb184463a1248a8764f51d51d7b6cf9bd2dbcdc70b12d3/pt_loop-0.1.0.tar.gz",
    "platform": null,
    "description": "# PyTorch LoOP\n\n## Introduction\n\n`pt_loop` is a pure PyTorch implementation of the Local Outlier Probabilities (LoOP) algorithm, designed for seamless integration into PyTorch-based machine learning pipelines.\nIt offers high performance on both CPU and GPU (CUDA), leveraging PyTorch's tensor capabilities throughout the entire computation.\n\nUnlike traditional implementations that might require data transfers to other libraries, `pt_loop` keeps your data on the PyTorch device (CPU or GPU) from start to finish, minimizing overhead and maximizing efficiency for large-scale anomaly detection tasks.\n\nThe original paper can be found here: <https://www.dbs.ifi.lmu.de/Publikationen/Papers/LoOP1649.pdf>\n\n## Installation\n\n`pt_loop` requires PyTorch.\n\nFirst, ensure you have PyTorch installed (refer to the [official PyTorch website](https://pytorch.org/get-started/locally/) for installation instructions specific to your system and CUDA version).\n\nThen, install `pt_loop` directly from PyPI:\n\n```bash\npip install pt_loop\n```\n\n## Quick Start & Usage Examples\n\nHere's how to get started with `pt_loop`.\n\n```python\nimport torch\nimport matplotlib.pyplot as plt\n\nfrom pt_loop import loop\n```\n\n### Basic LoOP Usage\n\n```python\n# 1. Generate some synthetic data with an obvious outlier\ndata = torch.cat([\n    torch.randn(100, 2) * 0.5 + torch.tensor([0.0, 0.0]), # Cluster 1\n    torch.randn(100, 2) * 0.5 + torch.tensor([5.0, 5.0]), # Cluster 2\n    torch.tensor([[2.5, 2.5]]),                           # Clear outlier\n])\n\n# Move data to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndata = data.to(device)\n\n# 2. Run LoOP\nprint(f\"Running LoOP on {device}...\")\nloop_scores = loop(\n    data,\n    k=10,                      # Number of nearest neighbors\n    lambda_=3.0,               # Scaling factor\n    distance_metric=\"l2\",      # or \"cosine\"\n)\n\nprint(\"\\nLoOP Results:\")\nprint(f\"Scores Shape: {loop_scores.shape}\")\nprint(f\"First 5 Scores: {loop_scores[:5].tolist()}\")\nprint(f\"Last score (outlier candidate): {loop_scores[-1].item()}\")\n\n# 3. (Optional) Visualize the scores\nplt.figure(figsize=(8, 6))\nscatter = plt.scatter(data[:, 0].cpu(), data[:, 1].cpu(), c=loop_scores.cpu(), cmap=\"plasma\", s=50, alpha=0.8)\nplt.colorbar(scatter, label=\"LoOP Score (Outlier Probability)\")\nplt.title(\"Local Outlier Probabilities (LoOP)\")\nplt.xlabel(\"Feature 1\")\nplt.ylabel(\"Feature 2\")\nplt.grid(True)\nplt.show()\n```\n\n## Contributing\n\nContributions are very welcome! If you find a bug, have a feature request, or want to contribute code, please feel free to:\n\n1. Open an issue on the [GitLab Issues page](https://gitlab.com/hassonofer/pt_loop/-/issues).\n1. Submit a Pull Request.\n\nPlease ensure your code adheres to the existing style (Black, isort) and passes all tests.\n\n## License\n\nThis project is licensed under the Apache-2.0 License - see the [LICENSE](https://gitlab.com/hassonofer/pt_loop/blob/main/LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Local Outlier Probabilities (LoOP) implementation in PyTorch",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://gitlab.com/hassonofer/pt_loop",
        "Issues": "https://gitlab.com/hassonofer/pt_loop/-/issues"
    },
    "split_keywords": [
        "pytorch",
        " loop",
        " outlier-detection",
        " anomaly-detection",
        " machine-learning",
        " unsupervised-learning"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "dabf6c1c67a99f1b181ac4a3bbaeb5d2004a703d4a81bbc37cd9706e2e124e62",
                "md5": "45b1ac9eae2b66e6c495368cd4f4e04a",
                "sha256": "f0c7bffebab38febbcd9710d31ded07419f83555f920efd0315fa53fc2ced212"
            },
            "downloads": -1,
            "filename": "pt_loop-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "45b1ac9eae2b66e6c495368cd4f4e04a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 8588,
            "upload_time": "2025-08-01T18:48:09",
            "upload_time_iso_8601": "2025-08-01T18:48:09.426431Z",
            "url": "https://files.pythonhosted.org/packages/da/bf/6c1c67a99f1b181ac4a3bbaeb5d2004a703d4a81bbc37cd9706e2e124e62/pt_loop-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "590b5b06c6928ca2ccdb184463a1248a8764f51d51d7b6cf9bd2dbcdc70b12d3",
                "md5": "a462c48c988b6671d8f1f15dee0f6046",
                "sha256": "dfe89b602665b428bc05200b9a8047604c605397bc26eb485376190b9ff436b8"
            },
            "downloads": -1,
            "filename": "pt_loop-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a462c48c988b6671d8f1f15dee0f6046",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 8518,
            "upload_time": "2025-08-01T18:48:10",
            "upload_time_iso_8601": "2025-08-01T18:48:10.482187Z",
            "url": "https://files.pythonhosted.org/packages/59/0b/5b06c6928ca2ccdb184463a1248a8764f51d51d7b6cf9bd2dbcdc70b12d3/pt_loop-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-01 18:48:10",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "codeberg": false,
    "gitlab_user": "hassonofer",
    "gitlab_project": "pt_loop",
    "lcname": "pt-loop"
}
        
Elapsed time: 3.90725s