Package: anonypyx
AnonyPyxThis is a fork of the python libraryAnonyPyproviding data anonymization techniques.
AnonyPyx adds further algorithms (see below) and introduces a declarative interface.
If you consider migrating from AnonyPy, keep in mind that AnonyPyx is not compatible with its original API.Featurespartion-based anonymization algorithm Mondrian [1] supportingk-anonymityl-diversityt-closenessmicroclustering based anonymization algorithm MDAV-Generic [2] supportingk-anonymityinteroperability with pandas data framessupports both continuous and categorical attributesimage anonymization via the k-Same family of algorithmsInstallpipinstallanonypyxUsageDisclaimer: 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 anonymized data set when shuffling is not used.Mondrian:importanonypyximportpandasaspd# 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)forcolumnin("sex","zip code","diagnosis"):df[column]=df[column].astype("category")# Step 2: Prepare anonymizeranonymizer=anonypyx.Anonymizer(df,k=3,l=2,algorithm="Mondrian",feature_columns=["age","sex","zip code"],sensitive_column="diagnosis")# Step 3: Anonymize data (this might take a while for large data sets)anonymized_records=anonymizer.anonymize()# Print results:anonymized_df=pd.DataFrame(anonymized_records)print(anonymized_df)Output:agesexzipcodediagnosiscount019-33female10023,33139,96850diabetes1119-33female10023,33139,96850diarrhea1219-33female10023,33139,96850flu1350-92male,intersex02139,20001,94130cancer1450-92male,intersex02139,20001,94130flu1550-92male,intersex02139,20001,94130stroke1MDAV-generic:# Step 2: Prepare anonymizeranonymizer=anonypyx.Anonymizer(df,k=3,algorithm="MDAV-generic",feature_columns=["age","sex","zip code"],sensitive_column="diagnosis")k-Same-Eigen:importanonypyximportnumpyasnpimportcv2fromosimportlistdirfromos.pathimportisfile,join# Step 1: Load images into single numpy array# images are loaded in grayscale# every image must have the same height and widthpath_to_dir='directory/containing/images/'height=120width=128files=[fforfinlistdir(path_to_dir)ifisfile(join(path_to_dir,f))]images=[cv2.imread(join(path_to_dir,f),flags=cv2.IMREAD_GRAYSCALE)forfinlistdir(path_to_dir)ifisfile(join(path_to_dir,f))]images=np.array(images)# Step 2: Prepare anonymizeranonymizer=anonypyx.kSame(images,width,height,k=5,variant='eigen')# Step 3: Anonymizationanonymized,mapping=anonymizer.anonymize()# Display the first image and its anonymized versionsample_image=np.concatenate((images[0],anonymized[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()ContributingClone the repository:gitclonehttps://github.com/questforwisdom/anonypyx.gitSet a virtual python environment up and install dependencies:python-mvenvvenvsourcevenv/bin/activate
pipinstall-rrequirements.txtRun tests:pytestChangelog0.2.0added the microaggregation algorithm MDAV-generic [2]added the Anonymizer class as the new APIremoved Preserver class which was superseded by Anonymizer0.2.1 - 0.2.3minor bugfixes0.2.4added k-Same family of algorithms for image anonymization [3]added the microaggregation algorithm used by k-SameReferences[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.