anonymization-lib 1.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anonymization_lib-1.4.0/LICENSE +21 -0
- anonymization_lib-1.4.0/PKG-INFO +302 -0
- anonymization_lib-1.4.0/README.md +284 -0
- anonymization_lib-1.4.0/pyproject.toml +31 -0
- anonymization_lib-1.4.0/setup.cfg +4 -0
- anonymization_lib-1.4.0/src/anonymization_lib/__init__.py +7 -0
- anonymization_lib-1.4.0/src/anonymization_lib/io/__init__.py +4 -0
- anonymization_lib-1.4.0/src/anonymization_lib/io/data_import.py +90 -0
- anonymization_lib-1.4.0/src/anonymization_lib/io/export.py +87 -0
- anonymization_lib-1.4.0/src/anonymization_lib/metrics/__init__.py +5 -0
- anonymization_lib-1.4.0/src/anonymization_lib/metrics/k_anonymity.py +391 -0
- anonymization_lib-1.4.0/src/anonymization_lib/metrics/l_diversity.py +393 -0
- anonymization_lib-1.4.0/src/anonymization_lib/metrics/t_closeness.py +618 -0
- anonymization_lib-1.4.0/src/anonymization_lib/techniques/__init__.py +6 -0
- anonymization_lib-1.4.0/src/anonymization_lib/techniques/generalization.py +907 -0
- anonymization_lib-1.4.0/src/anonymization_lib/techniques/pipeline.py +134 -0
- anonymization_lib-1.4.0/src/anonymization_lib/techniques/substitution.py +343 -0
- anonymization_lib-1.4.0/src/anonymization_lib/techniques/suppression.py +255 -0
- anonymization_lib-1.4.0/src/anonymization_lib/utils.py +153 -0
- anonymization_lib-1.4.0/src/anonymization_lib/visualization/Suggestion.py +730 -0
- anonymization_lib-1.4.0/src/anonymization_lib/visualization/__init__.py +4 -0
- anonymization_lib-1.4.0/src/anonymization_lib/visualization/visualization.py +845 -0
- anonymization_lib-1.4.0/src/anonymization_lib.egg-info/PKG-INFO +302 -0
- anonymization_lib-1.4.0/src/anonymization_lib.egg-info/SOURCES.txt +36 -0
- anonymization_lib-1.4.0/src/anonymization_lib.egg-info/dependency_links.txt +1 -0
- anonymization_lib-1.4.0/src/anonymization_lib.egg-info/requires.txt +8 -0
- anonymization_lib-1.4.0/src/anonymization_lib.egg-info/top_level.txt +1 -0
- anonymization_lib-1.4.0/tests/test_export.py +265 -0
- anonymization_lib-1.4.0/tests/test_generalization.py +1092 -0
- anonymization_lib-1.4.0/tests/test_import.py +296 -0
- anonymization_lib-1.4.0/tests/test_k_anonymity.py +386 -0
- anonymization_lib-1.4.0/tests/test_l_diversity.py +409 -0
- anonymization_lib-1.4.0/tests/test_pipeline.py +367 -0
- anonymization_lib-1.4.0/tests/test_substitution.py +328 -0
- anonymization_lib-1.4.0/tests/test_suggestion.py +300 -0
- anonymization_lib-1.4.0/tests/test_suppression.py +285 -0
- anonymization_lib-1.4.0/tests/test_t_closeness.py +551 -0
- anonymization_lib-1.4.0/tests/test_visualization.py +575 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Miguel-gfdez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anonymization-lib
|
|
3
|
+
Version: 1.4.0
|
|
4
|
+
Summary: Library for data anonymization with Spark
|
|
5
|
+
Author: Miguel
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: numpy==2.2.6
|
|
11
|
+
Requires-Dist: pandas==2.3.3
|
|
12
|
+
Requires-Dist: plotly==7.1.0
|
|
13
|
+
Requires-Dist: nbformat==5.10.4
|
|
14
|
+
Requires-Dist: pyarrow==25.0.0
|
|
15
|
+
Provides-Extra: local
|
|
16
|
+
Requires-Dist: pyspark==4.2.1; extra == "local"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Data Anonymization Library for Big Data
|
|
20
|
+
|
|
21
|
+
[](https://github.com/Miguel-gfdez/anonymization-lib/actions)
|
|
22
|
+
[](https://sonarcloud.io/project/overview?id=Miguel-gfdez_anonymization-lib)
|
|
23
|
+
[](https://sonarcloud.io/project/overview?id=Miguel-gfdez_anonymization-lib)
|
|
24
|
+
|
|
25
|
+
A Python library for **scalable data anonymization using Apache Spark**, designed to reduce re-identification risks while preserving the analytical utility of large datasets.
|
|
26
|
+
|
|
27
|
+
The library combines privacy metrics, anonymization techniques, automated recommendations, and data visualization in a modular framework compatible with standard PySpark and Spark Connect environments.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
| Category | Functionality |
|
|
32
|
+
|---|---|
|
|
33
|
+
| Privacy metrics | k-anonymity, l-diversity, t-closeness |
|
|
34
|
+
| Anonymization | Suppression, substitution, generalization |
|
|
35
|
+
| Transformation pipelines | Sequential application of anonymization techniques |
|
|
36
|
+
| Anonymization advisor | Quasi-identifier analysis, risk estimation, transformation recommendations |
|
|
37
|
+
| Visualization | Exploratory analysis and privacy assessment |
|
|
38
|
+
| Data management | Import and export utilities |
|
|
39
|
+
| Distributed processing | Apache Spark and Spark Connect compatibility |
|
|
40
|
+
|
|
41
|
+
### Privacy metrics
|
|
42
|
+
|
|
43
|
+
The library provides three privacy metrics for assessing disclosure risks:
|
|
44
|
+
|
|
45
|
+
- **k-anonymity:** evaluates whether each equivalence group contains at least *k* records.
|
|
46
|
+
- **l-diversity:** evaluates the diversity of sensitive attribute values within equivalence groups.
|
|
47
|
+
- **t-closeness:** measures the distance between sensitive attribute distributions within equivalence groups and the overall dataset.
|
|
48
|
+
|
|
49
|
+
Each metric provides aggregated results and information about groups that violate the configured privacy threshold.
|
|
50
|
+
|
|
51
|
+
### Anonymization techniques
|
|
52
|
+
|
|
53
|
+
Three anonymization techniques are supported:
|
|
54
|
+
|
|
55
|
+
- **Suppression:** removes or nullifies selected attributes.
|
|
56
|
+
- **Substitution:** replaces attribute values using configurable replacement rules.
|
|
57
|
+
- **Generalization:** reduces data granularity through predefined rules.
|
|
58
|
+
|
|
59
|
+
Techniques can be applied individually or combined into sequential transformation pipelines.
|
|
60
|
+
|
|
61
|
+
### Anonymization advisor
|
|
62
|
+
|
|
63
|
+
The `AnonymizationAdvisor` provides recommendations based on the selected quasi-identifiers and the characteristics of the dataset.
|
|
64
|
+
|
|
65
|
+
Its functionality includes cardinality analysis, equivalence-group reduction, risk-level estimation, and suggested anonymization actions.
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
The library supports installation from PyPI, Wheel distributions, or source code.
|
|
70
|
+
|
|
71
|
+
### From PyPI
|
|
72
|
+
|
|
73
|
+
Once the package is published on PyPI, install it with:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install anonymization-lib
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
For local environments that require PySpark as an additional dependency:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install "anonymization-lib[local]"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### From a Wheel
|
|
86
|
+
|
|
87
|
+
Download the appropriate `.whl` file from a project distribution and install it using:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pip install path/to/anonymization_lib-<version>-py3-none-any.whl
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
For local environments requiring the optional Spark dependencies:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install "path/to/anonymization_lib-<version>-py3-none-any.whl[local]"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Replace `<version>` with the actual package version.
|
|
100
|
+
|
|
101
|
+
### From source
|
|
102
|
+
|
|
103
|
+
Clone the repository:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
git clone https://github.com/Miguel-gfdez/anonymization-lib.git
|
|
107
|
+
cd anonymization-lib
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Install the library in editable mode:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pip install -e ".[local]"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
For environments where Spark is already provided, such as Databricks:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pip install -e .
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Editable installations allow source code changes to take effect without reinstalling the package.
|
|
123
|
+
|
|
124
|
+
**Note:** In managed Spark environments, avoid installing the optional local PySpark dependency unless necessary, to prevent conflicts with the Spark version provided by the platform.
|
|
125
|
+
|
|
126
|
+
## Quick Start
|
|
127
|
+
|
|
128
|
+
The following example demonstrates how to evaluate k-anonymity on a Spark DataFrame.
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from pyspark.sql import SparkSession
|
|
132
|
+
from anonymization_lib import KAnonymity
|
|
133
|
+
|
|
134
|
+
# Create a Spark session
|
|
135
|
+
spark = (
|
|
136
|
+
SparkSession.builder
|
|
137
|
+
.appName("AnonymizationExample")
|
|
138
|
+
.getOrCreate()
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Load a dataset
|
|
142
|
+
df = spark.read.parquet("data.parquet")
|
|
143
|
+
|
|
144
|
+
# Evaluate k-anonymity
|
|
145
|
+
metric = KAnonymity(
|
|
146
|
+
quasi_identifiers=["AGE", "CITY"],
|
|
147
|
+
k_threshold=3,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
result = metric.summary(df)
|
|
151
|
+
|
|
152
|
+
# Display results
|
|
153
|
+
result.show_summary()
|
|
154
|
+
result.show_violating_groups()
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
The resulting summary provides information about equivalence groups, privacy threshold compliance, and potentially violating groups.
|
|
158
|
+
|
|
159
|
+
## Usage Examples
|
|
160
|
+
|
|
161
|
+
### Data visualization
|
|
162
|
+
|
|
163
|
+
Generate an exploratory visualization for a selected column:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from anonymization_lib import Visualization
|
|
167
|
+
|
|
168
|
+
fig = Visualization(
|
|
169
|
+
column="AGE"
|
|
170
|
+
).transform(df)
|
|
171
|
+
|
|
172
|
+
fig.show()
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Anonymization pipeline
|
|
176
|
+
|
|
177
|
+
Combine multiple anonymization techniques into a single workflow:
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from anonymization_lib import (
|
|
181
|
+
Generalization,
|
|
182
|
+
Substitution,
|
|
183
|
+
Suppression,
|
|
184
|
+
)
|
|
185
|
+
from anonymization_lib.techniques import TransformationPipeline
|
|
186
|
+
|
|
187
|
+
pipeline = [
|
|
188
|
+
Suppression(
|
|
189
|
+
columns_modes={
|
|
190
|
+
"NAME": "null",
|
|
191
|
+
"CITY": "drop",
|
|
192
|
+
}
|
|
193
|
+
),
|
|
194
|
+
Substitution(
|
|
195
|
+
column="NATIONAL_ID",
|
|
196
|
+
replacement_char="*",
|
|
197
|
+
mode="full",
|
|
198
|
+
),
|
|
199
|
+
Generalization(
|
|
200
|
+
column="AGE",
|
|
201
|
+
rules_path="data/gen_age.json",
|
|
202
|
+
),
|
|
203
|
+
Generalization(
|
|
204
|
+
column="POSTAL_CODE",
|
|
205
|
+
rules_path="data/gen_postal_code_region.json",
|
|
206
|
+
output_column="REGION",
|
|
207
|
+
),
|
|
208
|
+
]
|
|
209
|
+
|
|
210
|
+
df_anonymized = TransformationPipeline(
|
|
211
|
+
df,
|
|
212
|
+
pipeline,
|
|
213
|
+
)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Transformations are applied sequentially, with the output of each transformation serving as the input to the next.
|
|
217
|
+
|
|
218
|
+
### Anonymization recommendations
|
|
219
|
+
|
|
220
|
+
Analyze quasi-identifiers and obtain anonymization recommendations:
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
from anonymization_lib import AnonymizationAdvisor
|
|
224
|
+
|
|
225
|
+
advisor = AnonymizationAdvisor(
|
|
226
|
+
quasi_identifiers=["AGE", "REGION"],
|
|
227
|
+
k=3,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
recommendations = advisor.suggest(df_anonymized)
|
|
231
|
+
|
|
232
|
+
recommendations.get_summary_df().show()
|
|
233
|
+
recommendations.get_suggestions_df().show()
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Data export
|
|
237
|
+
|
|
238
|
+
Export an anonymized DataFrame using Spark:
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
df_anonymized.write.mode("overwrite").parquet(
|
|
242
|
+
"output/anonymized"
|
|
243
|
+
)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Development and Testing
|
|
247
|
+
|
|
248
|
+
The project uses Python's `unittest` framework for automated testing and `coverage` for measuring code coverage.
|
|
249
|
+
|
|
250
|
+
### Install development dependencies
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
pip install -e ".[local]"
|
|
254
|
+
pip install coverage
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Run tests
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
python -m unittest discover -s tests -p "test_*.py" -t .
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Generate a coverage report
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
coverage run --source=src/anonymization_lib -m unittest discover -s tests -p "test_*.py" -t .
|
|
267
|
+
coverage report -m
|
|
268
|
+
coverage html
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
The HTML coverage report is generated in `htmlcov/`.
|
|
272
|
+
|
|
273
|
+
### Continuous Integration and Delivery
|
|
274
|
+
|
|
275
|
+
The project uses GitHub Actions for automated workflows.
|
|
276
|
+
|
|
277
|
+
**Continuous Integration (CI)** performs unit testing, code coverage analysis, static analysis with SonarCloud, and package building.
|
|
278
|
+
|
|
279
|
+
**Continuous Delivery (CD)** builds Wheel and source distributions when a GitHub Release is published. PyPI publishing is planned but is not currently enabled in the release workflow.
|
|
280
|
+
|
|
281
|
+
## Technologies
|
|
282
|
+
|
|
283
|
+
- **Python:** library development and testing.
|
|
284
|
+
- **Apache Spark / PySpark:** distributed data processing.
|
|
285
|
+
- **Spark Connect:** compatibility with remote Spark environments.
|
|
286
|
+
- **Plotly:** interactive data visualization.
|
|
287
|
+
- **GitHub Actions:** CI/CD automation.
|
|
288
|
+
- **SonarCloud:** static code analysis and quality monitoring.
|
|
289
|
+
|
|
290
|
+
## Project Status
|
|
291
|
+
|
|
292
|
+
**Current stable version:** v1.4.0
|
|
293
|
+
|
|
294
|
+
The library includes implemented privacy metrics, anonymization techniques, transformation pipelines, visualization utilities, data import/export, and an anonymization advisor.
|
|
295
|
+
|
|
296
|
+
## Author
|
|
297
|
+
|
|
298
|
+
Miguel Galán Fernández
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Data Anonymization Library for Big Data
|
|
2
|
+
|
|
3
|
+
[](https://github.com/Miguel-gfdez/anonymization-lib/actions)
|
|
4
|
+
[](https://sonarcloud.io/project/overview?id=Miguel-gfdez_anonymization-lib)
|
|
5
|
+
[](https://sonarcloud.io/project/overview?id=Miguel-gfdez_anonymization-lib)
|
|
6
|
+
|
|
7
|
+
A Python library for **scalable data anonymization using Apache Spark**, designed to reduce re-identification risks while preserving the analytical utility of large datasets.
|
|
8
|
+
|
|
9
|
+
The library combines privacy metrics, anonymization techniques, automated recommendations, and data visualization in a modular framework compatible with standard PySpark and Spark Connect environments.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
| Category | Functionality |
|
|
14
|
+
|---|---|
|
|
15
|
+
| Privacy metrics | k-anonymity, l-diversity, t-closeness |
|
|
16
|
+
| Anonymization | Suppression, substitution, generalization |
|
|
17
|
+
| Transformation pipelines | Sequential application of anonymization techniques |
|
|
18
|
+
| Anonymization advisor | Quasi-identifier analysis, risk estimation, transformation recommendations |
|
|
19
|
+
| Visualization | Exploratory analysis and privacy assessment |
|
|
20
|
+
| Data management | Import and export utilities |
|
|
21
|
+
| Distributed processing | Apache Spark and Spark Connect compatibility |
|
|
22
|
+
|
|
23
|
+
### Privacy metrics
|
|
24
|
+
|
|
25
|
+
The library provides three privacy metrics for assessing disclosure risks:
|
|
26
|
+
|
|
27
|
+
- **k-anonymity:** evaluates whether each equivalence group contains at least *k* records.
|
|
28
|
+
- **l-diversity:** evaluates the diversity of sensitive attribute values within equivalence groups.
|
|
29
|
+
- **t-closeness:** measures the distance between sensitive attribute distributions within equivalence groups and the overall dataset.
|
|
30
|
+
|
|
31
|
+
Each metric provides aggregated results and information about groups that violate the configured privacy threshold.
|
|
32
|
+
|
|
33
|
+
### Anonymization techniques
|
|
34
|
+
|
|
35
|
+
Three anonymization techniques are supported:
|
|
36
|
+
|
|
37
|
+
- **Suppression:** removes or nullifies selected attributes.
|
|
38
|
+
- **Substitution:** replaces attribute values using configurable replacement rules.
|
|
39
|
+
- **Generalization:** reduces data granularity through predefined rules.
|
|
40
|
+
|
|
41
|
+
Techniques can be applied individually or combined into sequential transformation pipelines.
|
|
42
|
+
|
|
43
|
+
### Anonymization advisor
|
|
44
|
+
|
|
45
|
+
The `AnonymizationAdvisor` provides recommendations based on the selected quasi-identifiers and the characteristics of the dataset.
|
|
46
|
+
|
|
47
|
+
Its functionality includes cardinality analysis, equivalence-group reduction, risk-level estimation, and suggested anonymization actions.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
The library supports installation from PyPI, Wheel distributions, or source code.
|
|
52
|
+
|
|
53
|
+
### From PyPI
|
|
54
|
+
|
|
55
|
+
Once the package is published on PyPI, install it with:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install anonymization-lib
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
For local environments that require PySpark as an additional dependency:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install "anonymization-lib[local]"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### From a Wheel
|
|
68
|
+
|
|
69
|
+
Download the appropriate `.whl` file from a project distribution and install it using:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install path/to/anonymization_lib-<version>-py3-none-any.whl
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
For local environments requiring the optional Spark dependencies:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install "path/to/anonymization_lib-<version>-py3-none-any.whl[local]"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Replace `<version>` with the actual package version.
|
|
82
|
+
|
|
83
|
+
### From source
|
|
84
|
+
|
|
85
|
+
Clone the repository:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
git clone https://github.com/Miguel-gfdez/anonymization-lib.git
|
|
89
|
+
cd anonymization-lib
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Install the library in editable mode:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pip install -e ".[local]"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
For environments where Spark is already provided, such as Databricks:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pip install -e .
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Editable installations allow source code changes to take effect without reinstalling the package.
|
|
105
|
+
|
|
106
|
+
**Note:** In managed Spark environments, avoid installing the optional local PySpark dependency unless necessary, to prevent conflicts with the Spark version provided by the platform.
|
|
107
|
+
|
|
108
|
+
## Quick Start
|
|
109
|
+
|
|
110
|
+
The following example demonstrates how to evaluate k-anonymity on a Spark DataFrame.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from pyspark.sql import SparkSession
|
|
114
|
+
from anonymization_lib import KAnonymity
|
|
115
|
+
|
|
116
|
+
# Create a Spark session
|
|
117
|
+
spark = (
|
|
118
|
+
SparkSession.builder
|
|
119
|
+
.appName("AnonymizationExample")
|
|
120
|
+
.getOrCreate()
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
# Load a dataset
|
|
124
|
+
df = spark.read.parquet("data.parquet")
|
|
125
|
+
|
|
126
|
+
# Evaluate k-anonymity
|
|
127
|
+
metric = KAnonymity(
|
|
128
|
+
quasi_identifiers=["AGE", "CITY"],
|
|
129
|
+
k_threshold=3,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
result = metric.summary(df)
|
|
133
|
+
|
|
134
|
+
# Display results
|
|
135
|
+
result.show_summary()
|
|
136
|
+
result.show_violating_groups()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The resulting summary provides information about equivalence groups, privacy threshold compliance, and potentially violating groups.
|
|
140
|
+
|
|
141
|
+
## Usage Examples
|
|
142
|
+
|
|
143
|
+
### Data visualization
|
|
144
|
+
|
|
145
|
+
Generate an exploratory visualization for a selected column:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from anonymization_lib import Visualization
|
|
149
|
+
|
|
150
|
+
fig = Visualization(
|
|
151
|
+
column="AGE"
|
|
152
|
+
).transform(df)
|
|
153
|
+
|
|
154
|
+
fig.show()
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Anonymization pipeline
|
|
158
|
+
|
|
159
|
+
Combine multiple anonymization techniques into a single workflow:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from anonymization_lib import (
|
|
163
|
+
Generalization,
|
|
164
|
+
Substitution,
|
|
165
|
+
Suppression,
|
|
166
|
+
)
|
|
167
|
+
from anonymization_lib.techniques import TransformationPipeline
|
|
168
|
+
|
|
169
|
+
pipeline = [
|
|
170
|
+
Suppression(
|
|
171
|
+
columns_modes={
|
|
172
|
+
"NAME": "null",
|
|
173
|
+
"CITY": "drop",
|
|
174
|
+
}
|
|
175
|
+
),
|
|
176
|
+
Substitution(
|
|
177
|
+
column="NATIONAL_ID",
|
|
178
|
+
replacement_char="*",
|
|
179
|
+
mode="full",
|
|
180
|
+
),
|
|
181
|
+
Generalization(
|
|
182
|
+
column="AGE",
|
|
183
|
+
rules_path="data/gen_age.json",
|
|
184
|
+
),
|
|
185
|
+
Generalization(
|
|
186
|
+
column="POSTAL_CODE",
|
|
187
|
+
rules_path="data/gen_postal_code_region.json",
|
|
188
|
+
output_column="REGION",
|
|
189
|
+
),
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
df_anonymized = TransformationPipeline(
|
|
193
|
+
df,
|
|
194
|
+
pipeline,
|
|
195
|
+
)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Transformations are applied sequentially, with the output of each transformation serving as the input to the next.
|
|
199
|
+
|
|
200
|
+
### Anonymization recommendations
|
|
201
|
+
|
|
202
|
+
Analyze quasi-identifiers and obtain anonymization recommendations:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from anonymization_lib import AnonymizationAdvisor
|
|
206
|
+
|
|
207
|
+
advisor = AnonymizationAdvisor(
|
|
208
|
+
quasi_identifiers=["AGE", "REGION"],
|
|
209
|
+
k=3,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
recommendations = advisor.suggest(df_anonymized)
|
|
213
|
+
|
|
214
|
+
recommendations.get_summary_df().show()
|
|
215
|
+
recommendations.get_suggestions_df().show()
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Data export
|
|
219
|
+
|
|
220
|
+
Export an anonymized DataFrame using Spark:
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
df_anonymized.write.mode("overwrite").parquet(
|
|
224
|
+
"output/anonymized"
|
|
225
|
+
)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Development and Testing
|
|
229
|
+
|
|
230
|
+
The project uses Python's `unittest` framework for automated testing and `coverage` for measuring code coverage.
|
|
231
|
+
|
|
232
|
+
### Install development dependencies
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
pip install -e ".[local]"
|
|
236
|
+
pip install coverage
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Run tests
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
python -m unittest discover -s tests -p "test_*.py" -t .
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Generate a coverage report
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
coverage run --source=src/anonymization_lib -m unittest discover -s tests -p "test_*.py" -t .
|
|
249
|
+
coverage report -m
|
|
250
|
+
coverage html
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The HTML coverage report is generated in `htmlcov/`.
|
|
254
|
+
|
|
255
|
+
### Continuous Integration and Delivery
|
|
256
|
+
|
|
257
|
+
The project uses GitHub Actions for automated workflows.
|
|
258
|
+
|
|
259
|
+
**Continuous Integration (CI)** performs unit testing, code coverage analysis, static analysis with SonarCloud, and package building.
|
|
260
|
+
|
|
261
|
+
**Continuous Delivery (CD)** builds Wheel and source distributions when a GitHub Release is published. PyPI publishing is planned but is not currently enabled in the release workflow.
|
|
262
|
+
|
|
263
|
+
## Technologies
|
|
264
|
+
|
|
265
|
+
- **Python:** library development and testing.
|
|
266
|
+
- **Apache Spark / PySpark:** distributed data processing.
|
|
267
|
+
- **Spark Connect:** compatibility with remote Spark environments.
|
|
268
|
+
- **Plotly:** interactive data visualization.
|
|
269
|
+
- **GitHub Actions:** CI/CD automation.
|
|
270
|
+
- **SonarCloud:** static code analysis and quality monitoring.
|
|
271
|
+
|
|
272
|
+
## Project Status
|
|
273
|
+
|
|
274
|
+
**Current stable version:** v1.4.0
|
|
275
|
+
|
|
276
|
+
The library includes implemented privacy metrics, anonymization techniques, transformation pipelines, visualization utilities, data import/export, and an anonymization advisor.
|
|
277
|
+
|
|
278
|
+
## Author
|
|
279
|
+
|
|
280
|
+
Miguel Galán Fernández
|
|
281
|
+
|
|
282
|
+
## License
|
|
283
|
+
|
|
284
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "anonymization-lib"
|
|
7
|
+
version = "1.4.0"
|
|
8
|
+
description = "Library for data anonymization with Spark"
|
|
9
|
+
authors = [{ name = "Miguel" }]
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
license = "MIT"
|
|
13
|
+
|
|
14
|
+
dependencies = [
|
|
15
|
+
"numpy==2.2.6",
|
|
16
|
+
"pandas==2.3.3",
|
|
17
|
+
"plotly==7.1.0",
|
|
18
|
+
"nbformat==5.10.4",
|
|
19
|
+
"pyarrow==25.0.0"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.optional-dependencies]
|
|
23
|
+
local = [
|
|
24
|
+
"pyspark==4.2.1"
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
package-dir = {"" = "src"}
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
where = ["src"]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from .metrics import KAnonymity, LDiversity, TCloseness
|
|
2
|
+
from .techniques import Substitution, Suppression, Generalization
|
|
3
|
+
from .visualization import Visualization, AnonymizationAdvisor
|
|
4
|
+
from .io import DataExporter, DataImporter
|
|
5
|
+
|
|
6
|
+
__all__ = ["KAnonymity", "LDiversity", "TCloseness", "Substitution", "Suppression", "Generalization", "Visualization", "AnonymizationAdvisor", "DataExporter", "DataImporter"]
|
|
7
|
+
|