Skip to main content

License: Apache 2.0 codecov PyPI Documentation Status Publish Package in PyPI CI/CD Pipeline Code Coverage Python version

TrasgoDP implements different mechanims for ε-differential privacy (numerical and categorical data), (ε, δ)-differential privacy (numerical data) and metric-privacy (location-based data). The mechanisms are implemented for being used under a local approach, adding noise directly to the raw data. Two types of mechanims are implemented:

  • For numerical records: Laplace and Gaussian mechanisms. The implementation includes a final clipping applyied on the data with DP.
  • For categorical records: Exponential mechanism and Randomized Response (both for binary attributes and the k-ary version).
  • For location-based records: geo-indistinguishability mechanism for metric-privacy.

This library provides dedicated function designed for being applied on both pandas dataframes and lists/numpy arrays.

Installation

You can install trasgoDP using pip. We recommend to use Python3 with virtualenv:

virtualenv .venv -p python3
source .venv/bin/activate
pip install trasgoDP

Mechanisms implemented

Mechanism Type of the attribute Function in trasgoDP
Laplace Numerical numerical.dp_clip_laplace()
Gaussian Numerical numerical.dp_clip_gaussian()
Exponential Categorical categorical.dp_exponential()
Randomized response Categorical (binary) categorical.dp_randomized_response_binary()
k-ary randomized response Categorical categorical.dp_randomized_response_kary()
Ggeo-indistinguishability Location geoindis.metric_privacy()

Getting started

For applying DP mechanisms to a column of a dataframe you need to introduce:

  • The pandas dataframe with the data.
  • The column in the dataframe to be privatized.
  • The privacy budget (ε).
  • The probability of exceeding the privacy budget (δ) in case of numerical attributes and the Gaussian mechanism.
  • The uper and lower bounds for numerical attributes (optional).

Example: apply DP to the adult dataset with the Laplace mechanism for the column age and the Exponential mechanism for the column workclass:

import pandas as pd
from trasgodp.numerical import dp_clip_laplace
from trasgodp.categorical import dp_exponential

# Read and process the data
data = pd.read_csv("examples/adult.csv")
data.columns = data.columns.str.strip()
cols = [
    "workclass",
    "education",
    "marital-status",
    "occupation",
    "sex",
    "native-country",
]
for col in cols:
    data[col] = data[col].str.strip()

# Apply DP for the attribute age:
column_num = "age"
epsilon1 = 10
df = dp_clip_laplace(data, column_num, epsilon1, new_column=True)

# Apply DP for the attribute workclass:
column_cat = "workclass"
epsilon2 = 5
df = dp_exponential(data, column_cat, epsilon2, new_column=True)

For applying metric privacy to location data, you need to introduce:

  • The pandas dataframe with the data.
  • The column with the latitude data column_lat in the dataframe.
  • The column with the longitude data column_lon in the dataframe.
  • The privacy budget (ε).
  • Whether or not to create two new columns containing the privatized latitude and longitude coordinates.

Example: apply metric privacy to the earthquake dataset and plot the map:

import pandas as pd
from trasgodp.geoindis import metric_privacy, plot_metric_dp_map

# Read the data
data = pd.read_csv("./examples/earthquake_data.csv")
column_lat = "latitude"
column_lon = "longitude"

# Apply metric privacy creating new columns for lat and lon:
epsilon =1.e-3
data_priv = metric_privacy(data, column_lat, column_lon, epsilon, new_cols=True)

# Plot and save the map:
plot_metric_dp_map(data_priv, column_lat, column_lon, save_file="example_map.html")

Example: resulting interactive map Map preview

Warning

This project is under active development.

License

This project is licensed under the Apache 2.0 license.

Related work

If you are using trasgoDP, you may also be interested in:

  • pyCANON: a Python library for checking the level of anonymity of a dataset.
  • anjana: a Python library for anonymizing tabular datasets.

Funding and acknowledgments

This work is funded by European Union through the SIESTA project (Horizon Europe) under Grant number 101131957.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trasgodp-2.0.2.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trasgodp-2.0.2-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file trasgodp-2.0.2.tar.gz.

File metadata

  • Download URL: trasgodp-2.0.2.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for trasgodp-2.0.2.tar.gz
Algorithm Hash digest
SHA256 ca3c4f67bfa37be221dd2f9dcadaf5595f6d4d7636cf521d8339f663e904c604
MD5 acad44354d2a60b63d975c5337f6f4c3
BLAKE2b-256 28c10744d3df74653a1bfb6f954dfbb5b6945726615913fde2d6528de9442ba5

See more details on using hashes here.

File details

Details for the file trasgodp-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: trasgodp-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for trasgodp-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 80d418372a7d688d4dba799ff159eb29b13bdcd5f119f7538adedc7d6f20b1d2
MD5 9120827584f9f83ee6d94f6ce801c3ab
BLAKE2b-256 cc3ada3436de9c3558c36ace14ad619f9fa693852aead5f0beed5a6afec5f955

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.3

2 files

This release

2.0.2 This release

2 files

2.0.1

2 files

2.0.0

2 files

1.0.0

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

0.0.1

2 files

0.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page