anonypyx


Nameanonypyx JSON
Version 0.2.7 PyPI version JSON
download
home_pageNone
SummaryAnonymisation library for python, fork of anonypy
upload_time2025-02-25 16:52:49
maintainerNone
docs_urlNone
authorquestforwisdom, glassonion1
requires_python>=3.9
licenseMIT License Copyright (c) 2021 Taisuke Fujita 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.
keywords k-anonymity l-diversity t-closeness anonymization mondrian microaggregation mdav ksame
VCS
bugtrack_url
requirements pytest pandas numpy scikit-learn scipy exact_multiset_cover
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AnonyPyx

This is a fork of the python library [AnonyPy](https://pypi.org/project/anonypy/) providing data anonymisation techniques. 
AnonyPyx adds further algorithms (see below), introduces a declarative interface and adds attacks on the anonymised data.
If you consider migrating from AnonyPy, keep in mind that AnonyPyx is not compatible with its original API.

## Features

- partion-based anonymisation algorithm Mondrian [1] supporting
    - k-anonymity
    - l-diversity 
    - t-closeness
- microclustering based anonsmization algorithm MDAV-Generic [2] supporting
    - k-anonymity
- interoperability with pandas data frames
- supports both continuous and categorical attributes 
- image anonymisation via the k-Same family of algorithms

## Install

```bash
pip install anonypyx
```


## Usage

**Disclaimer**: AnonyPyX does not shuffle the input data currently. In some applications, records can be re-identified based on the order in which they appear in the anonymised data set when shuffling is not used. 

Mondrian:

```python
import anonypyx
import pandas as pd

# Step 1: Prepare data as pandas data frame:

columns = ["age", "sex", "zip code", "diagnosis"]
data = [
    [50, "male", "02139", "stroke"],
    [33, "female", "10023", "flu"],
    [66, "intersex", "20001", "flu"],
    [28, "female", "33139", "diarrhea"],
    [92, "male", "94130", "cancer"],
    [19, "female", "96850", "diabetes"],
]

df = pd.DataFrame(data=data, columns=columns)

for column in ("sex", "zip code", "diagnosis"):
    df[column] = df[column].astype("category")

# Step 2: Prepare anonymiser

anonymiser = anonypyx.Anonymiser(
    df, k=3, l=2, algorithm="Mondrian", 
    feature_columns=["age", "sex", "zip code"], 
    sensitive_column="diagnosis",
    generalisation_strategy="human-readable
)

# Step 3: Anonymise data (this might take a while for large data sets)

anonymised_records = anonymiser.anonymise()

# Print results:

anonymised_df = pd.DataFrame(anonymised_records)
print(anonymised_df)
```

Output: 

```bash
     age            sex           zip code diagnosis  count
0  19-33         female  10023,33139,96850  diabetes      1
1  19-33         female  10023,33139,96850  diarrhea      1
2  19-33         female  10023,33139,96850       flu      1
3  50-92  male,intersex  02139,20001,94130    cancer      1
4  50-92  male,intersex  02139,20001,94130       flu      1
5  50-92  male,intersex  02139,20001,94130    stroke      1
```

MDAV-generic:

```python
# Step 2: Prepare anonymiser
anonymiser = anonypyx.Anonymiser(
    df, k=3, algorithm="MDAV-generic", 
    feature_columns=["age", "zip code", "income"], 
    generalisation_strategy="microaggregation"
)
```

k-Same-Eigen:

```python
import anonypyx
import numpy as np
import cv2

from os import listdir
from os.path import isfile, join

# Step 1: Load images into single numpy array

# images are loaded in grayscale
# every image must have the same height and width

path_to_dir = 'directory/containing/images/'
height = 120
width = 128
files = [f for f in listdir(path_to_dir) if isfile(join(path_to_dir, f))]
images = [cv2.imread(join(path_to_dir, f), flags = cv2.IMREAD_GRAYSCALE) for f in listdir(path_to_dir) if isfile(join(path_to_dir, f))]
images = np.array(images)

# Step 2: Prepare anonymiser

anonymiser = anonypyx.kSame(images, width, height, k=5, variant='eigen')

# Step 3: Anonymisation

anonymised, mapping = anonymiser.anonymise()

# Display the first image and its anonymised version

sample_image = np.concatenate((images[0], anonymised[mapping[0]]), axis=1).astype('uint8')
sample_image = cv2.cvtColor(sample_image, cv2.COLOR_GRAY2BGR)
cv2.imshow("k-same-eigen", sample_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```

## Contributing

Clone the repository:

```bash
git clone https://github.com/questforwisdom/anonypyx.git
```

Set a virtual python environment up and install dependencies:

```bash
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

Run tests:

```bash
pytest
```

## Changelog

### 0.2.0

- added the microaggregation algorithm MDAV-generic [2]
- added the Anonymizer class as the new API 
- removed Preserver class which was superseded by Anonymizer

### 0.2.1 - 0.2.3

- minor bugfixes

### 0.2.4

- added k-Same family of algorithms for image anonymisation [3]
- added the microaggregation algorithm used by k-Same

### 0.2.5

- renamed `Anonymizer` and its `anonymize` method to BE spelling
- added the `generalisation_strategy` parameter to Anonymiser which determines how generalised data is represented (old behaviour is `"human-readable"`)
- added the generalisation strategies `"machine-readable"` and `"microaggregation"`
- added some attacks on anonymised data which can be found in the submodule `attackers`

## References

- [1]: LeFevre, K., DeWitt, D. J., & Ramakrishnan, R. (2006). Mondrian multidimensional K-anonymity. 22nd International Conference on Data Engineering (ICDE’06), 25–25. https://doi.org/10.1109/ICDE.2006.101
- [2]: Domingo-Ferrer, J., & Torra, V. (2005). Ordinal, continuous and heterogeneous k-anonymity through microaggregation. Data Mining and Knowledge Discovery, 11, 195–212.
- [3]: E. M. Newton, L. Sweeney, and B. Malin, ‘Preserving privacy by de-identifying face images’, IEEE Transactions on Knowledge and Data Engineering, vol. 17, no. 2, pp. 232–243, Feb. 2005, doi: 10.1109/TKDE.2005.32.


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "anonypyx",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "k-anonymity, l-diversity, t-closeness, anonymization, mondrian, microaggregation, mdav, ksame",
    "author": "questforwisdom, glassonion1",
    "author_email": null,
    "download_url": null,
    "platform": null,
    "description": "# AnonyPyx\n\nThis is a fork of the python library [AnonyPy](https://pypi.org/project/anonypy/) providing data anonymisation techniques. \nAnonyPyx adds further algorithms (see below), introduces a declarative interface and adds attacks on the anonymised data.\nIf you consider migrating from AnonyPy, keep in mind that AnonyPyx is not compatible with its original API.\n\n## Features\n\n- partion-based anonymisation algorithm Mondrian [1] supporting\n    - k-anonymity\n    - l-diversity \n    - t-closeness\n- microclustering based anonsmization algorithm MDAV-Generic [2] supporting\n    - k-anonymity\n- interoperability with pandas data frames\n- supports both continuous and categorical attributes \n- image anonymisation via the k-Same family of algorithms\n\n## Install\n\n```bash\npip install anonypyx\n```\n\n\n## Usage\n\n**Disclaimer**: AnonyPyX does not shuffle the input data currently. In some applications, records can be re-identified based on the order in which they appear in the anonymised data set when shuffling is not used. \n\nMondrian:\n\n```python\nimport anonypyx\nimport pandas as pd\n\n# Step 1: Prepare data as pandas data frame:\n\ncolumns = [\"age\", \"sex\", \"zip code\", \"diagnosis\"]\ndata = [\n    [50, \"male\", \"02139\", \"stroke\"],\n    [33, \"female\", \"10023\", \"flu\"],\n    [66, \"intersex\", \"20001\", \"flu\"],\n    [28, \"female\", \"33139\", \"diarrhea\"],\n    [92, \"male\", \"94130\", \"cancer\"],\n    [19, \"female\", \"96850\", \"diabetes\"],\n]\n\ndf = pd.DataFrame(data=data, columns=columns)\n\nfor column in (\"sex\", \"zip code\", \"diagnosis\"):\n    df[column] = df[column].astype(\"category\")\n\n# Step 2: Prepare anonymiser\n\nanonymiser = anonypyx.Anonymiser(\n    df, k=3, l=2, algorithm=\"Mondrian\", \n    feature_columns=[\"age\", \"sex\", \"zip code\"], \n    sensitive_column=\"diagnosis\",\n    generalisation_strategy=\"human-readable\n)\n\n# Step 3: Anonymise data (this might take a while for large data sets)\n\nanonymised_records = anonymiser.anonymise()\n\n# Print results:\n\nanonymised_df = pd.DataFrame(anonymised_records)\nprint(anonymised_df)\n```\n\nOutput: \n\n```bash\n     age            sex           zip code diagnosis  count\n0  19-33         female  10023,33139,96850  diabetes      1\n1  19-33         female  10023,33139,96850  diarrhea      1\n2  19-33         female  10023,33139,96850       flu      1\n3  50-92  male,intersex  02139,20001,94130    cancer      1\n4  50-92  male,intersex  02139,20001,94130       flu      1\n5  50-92  male,intersex  02139,20001,94130    stroke      1\n```\n\nMDAV-generic:\n\n```python\n# Step 2: Prepare anonymiser\nanonymiser = anonypyx.Anonymiser(\n    df, k=3, algorithm=\"MDAV-generic\", \n    feature_columns=[\"age\", \"zip code\", \"income\"], \n    generalisation_strategy=\"microaggregation\"\n)\n```\n\nk-Same-Eigen:\n\n```python\nimport anonypyx\nimport numpy as np\nimport cv2\n\nfrom os import listdir\nfrom os.path import isfile, join\n\n# Step 1: Load images into single numpy array\n\n# images are loaded in grayscale\n# every image must have the same height and width\n\npath_to_dir = 'directory/containing/images/'\nheight = 120\nwidth = 128\nfiles = [f for f in listdir(path_to_dir) if isfile(join(path_to_dir, f))]\nimages = [cv2.imread(join(path_to_dir, f), flags = cv2.IMREAD_GRAYSCALE) for f in listdir(path_to_dir) if isfile(join(path_to_dir, f))]\nimages = np.array(images)\n\n# Step 2: Prepare anonymiser\n\nanonymiser = anonypyx.kSame(images, width, height, k=5, variant='eigen')\n\n# Step 3: Anonymisation\n\nanonymised, mapping = anonymiser.anonymise()\n\n# Display the first image and its anonymised version\n\nsample_image = np.concatenate((images[0], anonymised[mapping[0]]), axis=1).astype('uint8')\nsample_image = cv2.cvtColor(sample_image, cv2.COLOR_GRAY2BGR)\ncv2.imshow(\"k-same-eigen\", sample_image)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n```\n\n## Contributing\n\nClone the repository:\n\n```bash\ngit clone https://github.com/questforwisdom/anonypyx.git\n```\n\nSet a virtual python environment up and install dependencies:\n\n```bash\npython -m venv venv\nsource venv/bin/activate\npip install -r requirements.txt\n```\n\nRun tests:\n\n```bash\npytest\n```\n\n## Changelog\n\n### 0.2.0\n\n- added the microaggregation algorithm MDAV-generic [2]\n- added the Anonymizer class as the new API \n- removed Preserver class which was superseded by Anonymizer\n\n### 0.2.1 - 0.2.3\n\n- minor bugfixes\n\n### 0.2.4\n\n- added k-Same family of algorithms for image anonymisation [3]\n- added the microaggregation algorithm used by k-Same\n\n### 0.2.5\n\n- renamed `Anonymizer` and its `anonymize` method to BE spelling\n- added the `generalisation_strategy` parameter to Anonymiser which determines how generalised data is represented (old behaviour is `\"human-readable\"`)\n- added the generalisation strategies `\"machine-readable\"` and `\"microaggregation\"`\n- added some attacks on anonymised data which can be found in the submodule `attackers`\n\n## References\n\n- [1]: LeFevre, K., DeWitt, D. J., & Ramakrishnan, R. (2006). Mondrian multidimensional K-anonymity. 22nd International Conference on Data Engineering (ICDE\u201906), 25\u201325. https://doi.org/10.1109/ICDE.2006.101\n- [2]: Domingo-Ferrer, J., & Torra, V. (2005). Ordinal, continuous and heterogeneous k-anonymity through microaggregation. Data Mining and Knowledge Discovery, 11, 195\u2013212.\n- [3]: E. M. Newton, L. Sweeney, and B. Malin, \u2018Preserving privacy by de-identifying face images\u2019, IEEE Transactions on Knowledge and Data Engineering, vol. 17, no. 2, pp. 232\u2013243, Feb. 2005, doi: 10.1109/TKDE.2005.32.\n\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2021 Taisuke Fujita\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.\n        ",
    "summary": "Anonymisation library for python, fork of anonypy",
    "version": "0.2.7",
    "project_urls": {
        "Homepage": "https://github.com/questforwisdom/anonypyx"
    },
    "split_keywords": [
        "k-anonymity",
        " l-diversity",
        " t-closeness",
        " anonymization",
        " mondrian",
        " microaggregation",
        " mdav",
        " ksame"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "01d1d09832c1f535c72bb344a1e3c68244f6b27e8ee6bcc60338335c0d501536",
                "md5": "e5561d88940197b1ea15d740f17f99b4",
                "sha256": "15a492167e154695fd1c8583ad28310b6e93596ea3e910032a28ef0a6c5fa2c9"
            },
            "downloads": -1,
            "filename": "anonypyx-0.2.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e5561d88940197b1ea15d740f17f99b4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 14733,
            "upload_time": "2025-02-25T16:52:49",
            "upload_time_iso_8601": "2025-02-25T16:52:49.233625Z",
            "url": "https://files.pythonhosted.org/packages/01/d1/d09832c1f535c72bb344a1e3c68244f6b27e8ee6bcc60338335c0d501536/anonypyx-0.2.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-25 16:52:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "questforwisdom",
    "github_project": "anonypyx",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pytest",
            "specs": [
                [
                    ">=",
                    "6.2.5"
                ]
            ]
        },
        {
            "name": "pandas",
            "specs": [
                [
                    ">=",
                    "1.5.0"
                ]
            ]
        },
        {
            "name": "numpy",
            "specs": [
                [
                    ">=",
                    "1.21.3"
                ]
            ]
        },
        {
            "name": "scikit-learn",
            "specs": [
                [
                    ">=",
                    "1.0.1"
                ]
            ]
        },
        {
            "name": "scipy",
            "specs": [
                [
                    ">=",
                    "1.11.3"
                ]
            ]
        },
        {
            "name": "exact_multiset_cover",
            "specs": [
                [
                    ">=",
                    "1.5.0"
                ]
            ]
        }
    ],
    "lcname": "anonypyx"
}
        
Elapsed time: 1.69131s