-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanonymizer.go
More file actions
485 lines (387 loc) · 13.2 KB
/
Copy pathanonymizer.go
File metadata and controls
485 lines (387 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (C) 2022 CGI France
//
// This file is part of SIGO.
//
// SIGO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SIGO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SIGO. If not, see <http://www.gnu.org/licenses/>.
package sigo
import (
"os"
"github.com/cgi-fr/jsonline/pkg/cast"
"github.com/rs/zerolog/log"
)
const (
laplace = "laplace"
gaussian = "gaussian"
)
type bounds struct {
down, up float64
}
func NewNoAnonymizer() NoAnonymizer { return NoAnonymizer{} }
func NewGeneralAnonymizer() GeneralAnonymizer {
return GeneralAnonymizer{boundsValues: make(map[string]map[string]bounds)}
}
func NewAggregationAnonymizer(typeAgg string) AggregationAnonymizer {
return AggregationAnonymizer{typeAggregation: typeAgg, values: make(map[string]map[string]float64)}
}
func NewCodingAnonymizer() CodingAnonymizer {
return CodingAnonymizer{}
}
func NewNoiseAnonymizer(mechanism string) NoiseAnonymizer {
return NoiseAnonymizer{typeNoise: mechanism}
}
func NewSwapAnonymizer() SwapAnonymizer {
return SwapAnonymizer{swapValues: make(map[string]map[string][]float64)}
}
func NewReidentification(args []string) Reidentification {
return Reidentification{
masked: make(map[string][]map[string]interface{}),
unique: make(map[string]bool),
sensitive: make(map[string]map[string]interface{}),
stats: make(map[string]map[string]map[string]float64),
sensitivesFields: args,
}
}
type (
NoAnonymizer struct{}
GeneralAnonymizer struct {
// groupMap map[Cluster]map[string]string
// map of cluster -> qi -> bounds
boundsValues map[string]map[string]bounds
}
AggregationAnonymizer struct {
typeAggregation string
values map[string]map[string]float64
}
CodingAnonymizer struct{}
NoiseAnonymizer struct {
typeNoise string
}
SwapAnonymizer struct {
swapValues map[string]map[string][]float64
}
AnonymizedRecord struct {
original Record
mask map[string]interface{}
}
Reidentification struct {
masked map[string][]map[string]interface{}
unique map[string]bool
sensitive map[string]map[string]interface{}
stats map[string]map[string]map[string]float64
sensitivesFields []string
}
)
func (ar AnonymizedRecord) QuasiIdentifer() ([]float64, error) {
return ar.original.QuasiIdentifer()
}
func (ar AnonymizedRecord) Sensitives() []interface{} {
return ar.original.Sensitives()
}
func (ar AnonymizedRecord) Row() map[string]interface{} {
original := ar.original.Row()
for k, v := range ar.mask {
original[k] = v
}
return original
}
// Anonymize returns the original record, there is no anonymization.
func (a NoAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
mask := map[string]interface{}{}
for _, q := range qi {
mask[q] = rec.Row()[q]
}
return AnonymizedRecord{original: rec, mask: mask}
}
// Anonymize returns the record anonymize with the method general
// the record takes the bounds of the cluster.
func (a GeneralAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
mask := map[string]interface{}{}
if a.boundsValues[clus.ID()] == nil {
a.ComputeGeneralization(clus, qi)
}
for _, key := range qi {
mask[key] = []float64{a.boundsValues[clus.ID()][key].down, a.boundsValues[clus.ID()][key].up}
}
return AnonymizedRecord{original: rec, mask: mask}
}
// ComputeGeneralization calculates the min and max values of the cluster for each qi.
func (a GeneralAnonymizer) ComputeGeneralization(clus Cluster, qi []string) {
values := listValues(clus, qi)
boundsVal := make(map[string]bounds)
for _, key := range qi {
var b bounds
b.down = Min(values[key])
b.up = Max(values[key])
boundsVal[key] = b
}
a.boundsValues[clus.ID()] = boundsVal
}
// Anonymize returns the record anonymized with the method meanAggregarion or medianAggregation
// the record takes the aggregated values of the cluster.
func (a AggregationAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
mask := map[string]interface{}{}
if a.values[clus.ID()] == nil {
a.ComputeAggregation(clus, qi)
}
for _, key := range qi {
mask[key] = a.values[clus.ID()][key]
}
return AnonymizedRecord{original: rec, mask: mask}
}
// ComputeAggregation calculates the mean (method meanAggreagtion)
// or median (method medianAggregation) value of the cluster for each qi.
func (a AggregationAnonymizer) ComputeAggregation(clus Cluster, qi []string) {
values := listValues(clus, qi)
valAggregation := make(map[string]float64)
for _, key := range qi {
switch a.typeAggregation {
case "mean":
valAggregation[key] = Mean(values[key])
case "median":
valAggregation[key] = Median(values[key])
}
}
a.values[clus.ID()] = valAggregation
}
// Anonymize returns the record anonymized with the method outlier
// if the record is in the interval [Q1;Q3] then we don't change its value
// if the record is > Q3 then it takes the Q3 value
// if the record is < Q1 then it takes the Q1 value.
func (a CodingAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
values := listValues(clus, qi)
mask := map[string]interface{}{}
for i, key := range qi {
vals := values[key]
q := Quartile(vals)
bottom := q.Q1
top := q.Q3
recVals, err := rec.QuasiIdentifer()
if err != nil {
log.Err(err).Msg("Cannot cast quasi-identifier to float64")
log.Warn().Int("return", 1).Msg("End SIGO")
os.Exit(1)
}
val := recVals[i]
switch {
case val < bottom:
mask[key] = bottom
case val > top:
mask[key] = top
default:
mask[key] = val
}
}
return AnonymizedRecord{original: rec, mask: mask}
}
// Anonymize returns the record anonymized with the method laplaceNoise or gaussianNoise
// the record takes as value the original value added to a Laplacian or Gaussian noise
// the anonymized value stays within the bounds of the cluster.
func (a NoiseAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
values := listValues(clus, qi)
mask := map[string]interface{}{}
for i, key := range qi {
recVals, err := rec.QuasiIdentifer()
if err != nil {
log.Err(err).Msg("Cannot cast quasi-identifier to float64")
log.Warn().Int("return", 1).Msg("End SIGO")
os.Exit(1)
}
val := recVals[i]
laplaceVal := Scaling(val, values[key], laplace)
gaussianVal := Scaling(val, values[key], gaussian)
var randomVal float64
for {
switch a.typeNoise {
case laplace:
randomVal = Rescaling(laplaceVal+LaplaceNumber(), values[key], laplace)
case gaussian:
randomVal = Rescaling(gaussianVal+GaussianNumber(0, 1), values[key], gaussian)
}
if (randomVal > Min(values[key]) && randomVal < Max(values[key])) || Min(values[key]) == Max(values[key]) {
break
}
}
mask[key] = randomVal
}
return AnonymizedRecord{original: rec, mask: mask}
}
func (a SwapAnonymizer) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
mask := map[string]interface{}{}
// cluster value swapping
if a.swapValues[clus.ID()] == nil {
a.Swap(clus, qi)
}
var idx int
// retrieve the position (idx) of the record in the cluster
for i, r := range clus.Records() {
if rec == r {
idx = i
}
}
for _, key := range qi {
// retrieve the swapped value
mask[key] = a.swapValues[clus.ID()][key][idx]
}
return AnonymizedRecord{original: rec, mask: mask}
}
func (a SwapAnonymizer) Swap(clus Cluster, qi []string) {
// retrieve the cluster values for each qi
values := listValues(clus, qi)
swapVal := make(map[string][]float64)
for _, key := range qi {
// values permutation
swapVal[key] = Shuffle(values[key])
}
a.swapValues[clus.ID()] = swapVal
}
// Anonymize on object Reidentification re-identifies the original data using the anonymized data.
func (r Reidentification) Anonymize(rec Record, clus Cluster, qi, s []string) Record {
mask := map[string]interface{}{}
// initialize re-identification object: groups the anonymized data,
// checks if the sensitive data is not unique in the cluster,
// checks if the anonymized data of the cluster have the same qi value,
// and computes the mean and the standard deviation of the cluster
if r.masked[clus.ID()] == nil {
r.InitReidentification(clus, qi, s)
}
original, err := cast.ToInt64(rec.Sensitives()[0])
if err != nil {
log.Err(err).Msg("Cannot cast original value")
log.Warn().Int("return", 1).Msg("End SIGO")
os.Exit(1)
}
// re-identification of the original data
if original.(int64) == 1 {
for _, q := range qi {
mask[q] = rec.Row()[q]
}
for _, sensitive := range r.sensitivesFields {
// if in a cluster the sensitive data is unique then we can re-identify the individuals
if r.sensitive[clus.ID()][sensitive] != nil {
mask[r.sensitivesFields[0]] = r.sensitive[clus.ID()][sensitive]
mask["similarity"] = 1
} else if !r.unique[clus.ID()] {
// else if the masked data are not unique, then the distances are computed
// (if they are unique, impossible to re-identify)
scores := r.ComputeSimilarity(rec, clus, qi, s)
sim, sens := TopSimilarity(scores)
mask[r.sensitivesFields[0]] = sens
mask["similarity"] = sim
}
}
}
return AnonymizedRecord{original: rec, mask: mask}
}
// InitReidentification initialize the re-identification object.
func (r Reidentification) InitReidentification(clus Cluster, qi []string, s []string) {
// map containing the anonymized data
maskedData := []map[string]interface{}{}
// slice containing the records of cluster
data := []map[string]interface{}{}
// map containing for each sensitive attribute the list of sensitive data
sensitivesData := make(map[string][]interface{})
for _, rec := range clus.Records() {
data = append(data, rec.Row())
original, err := cast.ToInt64(rec.Sensitives()[0])
if err != nil {
log.Err(err).Msg("Cannot cast original value")
log.Warn().Int("return", 1).Msg("End SIGO")
os.Exit(1)
}
if original.(int64) == 0 {
maskedData = append(maskedData, rec.Row())
for _, s := range r.sensitivesFields {
sensitivesData[s] = append(sensitivesData[s], rec.Row()[s])
}
}
}
if len(maskedData) == 0 {
log.Error().Msg("Clusters with only original data, pay attention to the l-diversity parameter ")
log.Warn().Int("return", 1).Msg("End SIGO")
os.Exit(1)
}
// groups all the anonymized records
r.masked[clus.ID()] = maskedData
// indicates if the cluster contains unique masked data
r.unique[clus.ID()] = Unique(maskedData, qi)
// checks if the sensitive data is well represented
uniqueSensitive := IsUnique(sensitivesData)
tmp := make(map[string]interface{})
for _, s := range r.sensitivesFields {
if uniqueSensitive[s] {
tmp[s] = sensitivesData[s][0]
} else {
tmp[s] = nil
}
}
r.sensitive[clus.ID()] = tmp
// computes the mean and standard deviation of each cluster
r.ComputeStatistics(data, clus, s)
}
// ComputeStatistics computes the mean and standart deviation for cluster clus.
func (r Reidentification) ComputeStatistics(data []map[string]interface{}, clus Cluster, s []string) {
statistics := make(map[string]map[string]float64)
for key, val := range ListValues(data, append(s, r.sensitivesFields...)) {
stats := make(map[string]float64)
stats["mean"] = Mean(SliceToFloat64(val))
stats["std"] = Std(SliceToFloat64(val))
statistics[key] = stats
}
r.stats[clus.ID()] = statistics
}
// Statistics returns the statistics of the q attribute of the cluster with path idCluster.
func (r Reidentification) Statistics(idCluster string, q string) (mean float64, std float64) {
return r.stats[idCluster][q]["mean"], r.stats[idCluster][q]["std"]
}
// ComputeSimilarity computes the similarity score between the record rec and the anonymized cluster data.
func (r Reidentification) ComputeSimilarity(rec Record, clus Cluster,
qi []string, s []string,
) map[float64]interface{} {
scores := make(map[float64]interface{})
x := make(map[string]interface{})
for _, q := range qi {
mean, std := r.Statistics(clus.ID(), q)
x[q] = Scale(rec.Row()[q], mean, std)
}
X := MapItoMapF(x)
for _, row := range r.masked[clus.ID()] {
y := make(map[string]interface{})
for _, q := range qi {
mean, std := r.Statistics(clus.ID(), q)
y[q] = Scale(row[q], mean, std)
}
Y := MapItoMapF(y)
// Compute similarity
score := Similarity(ComputeDistance("", X, Y))
scores[score] = row[r.sensitivesFields[0]]
}
return scores
}
// Returns the list of values present in the cluster for each qi.
func listValues(clus Cluster, qi []string) (mapValues map[string][]float64) {
mapValues = make(map[string][]float64)
for _, record := range clus.Records() {
for i, key := range qi {
recVals, err := record.QuasiIdentifer()
if err != nil {
log.Err(err).Msg("Cannot cast quasi-identifier to float64")
log.Warn().Int("return", 1).Msg("End SIGO")
os.Exit(1)
}
val := recVals[i]
mapValues[key] = append(mapValues[key], val)
}
}
return mapValues
}