anonymous_generator_test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import unittest
  17. import random
  18. from federatedml.util.anonymous_generator_util import Anonymous
  19. class TeskClassifyLabelChecker(unittest.TestCase):
  20. def setUp(self):
  21. pass
  22. def test_extend_columns(self):
  23. anonymous_generator = Anonymous(role="guest", party_id=10000)
  24. schema = dict(header=["feature" + str(i) for i in range(100)])
  25. new_schema = anonymous_generator.generate_anonymous_header(schema)
  26. anonymous_header = new_schema["anonymous_header"]
  27. extend_columns1 = ["e0", "e1", "e3"]
  28. anonymous_header1 = anonymous_generator.extend_columns(anonymous_header, extend_columns1)
  29. self.assertTrue(len(anonymous_header1) == len(anonymous_header) + len(extend_columns1))
  30. for i in range(len(extend_columns1)):
  31. idx = i + len(anonymous_header)
  32. self.assertTrue(anonymous_header1[idx] == "_".join(["guest", "10000", "exp", str(i)]))
  33. extend_columns2 = ["f0", "f1", "f2", "f3"]
  34. anonymous_header2 = anonymous_generator.extend_columns(anonymous_header1, extend_columns2)
  35. self.assertTrue(len(anonymous_header2) == len(anonymous_header) + len(extend_columns1) + len(extend_columns2))
  36. for i in range(len(extend_columns2)):
  37. idx = i + len(anonymous_header) + len(extend_columns1)
  38. self.assertTrue(anonymous_header2[idx] == "_".join(
  39. ["guest", "10000", "exp", str(i + len(extend_columns1))]))
  40. def test_anonymous_header_generate_with_party_id(self):
  41. anonymous_generator = Anonymous(role="guest", party_id=10000)
  42. schema = dict()
  43. schema["header"] = ["feature" + str(i) for i in range(100)]
  44. new_schema = anonymous_generator.generate_anonymous_header(schema)
  45. anonymous_header = new_schema["anonymous_header"]
  46. self.assertTrue(len(anonymous_header) == 100)
  47. for i in range(100):
  48. self.assertTrue(anonymous_header[i] == "_".join(["guest", "10000", "x" + str(i)]))
  49. def test_anonymous_header_generate_without_party_id(self):
  50. schema = dict(header=["feature" + str(i) for i in range(100)])
  51. new_schema = Anonymous().generate_anonymous_header(schema)
  52. anonymous_header = new_schema["anonymous_header"]
  53. self.assertTrue(len(anonymous_header) == 100)
  54. for i in range(100):
  55. self.assertTrue(anonymous_header[i] == "x" + str(i))
  56. def test_generate_derived_header_without_extend(self):
  57. schema = dict(header=["feature" + str(i) for i in range(10)])
  58. new_schema = Anonymous(role="guest", party_id=10000).generate_anonymous_header(schema)
  59. anonymous_header = new_schema["anonymous_header"]
  60. derived_dict = {"feature5": ["feature5_f0", "feature5_f1", "feature5_f2", "feature5_f3"],
  61. "feature6": ["feature6_e1", "feature6_e2", "feature6_e3"]}
  62. derived_anonymous_header = Anonymous().generate_derived_header(original_header=schema["header"],
  63. original_anonymous_header=anonymous_header,
  64. derived_dict=derived_dict)
  65. for i in range(5, 9):
  66. self.assertTrue(derived_anonymous_header[i] == anonymous_header[5] + "_" + str(i - 5))
  67. for i in range(9, 12):
  68. self.assertTrue(derived_anonymous_header[i] == anonymous_header[6] + "_" + str(i - 9))
  69. def test_generate_derived_header_with_extend(self):
  70. anonymous_generator = Anonymous(role="guest", party_id=10000)
  71. schema = dict(header=["feature" + str(i) for i in range(100)])
  72. new_schema = anonymous_generator.generate_anonymous_header(schema)
  73. anonymous_header = new_schema["anonymous_header"]
  74. extend_columns1 = ["e0", "e1", "e3"]
  75. extend_header = schema["header"] + extend_columns1
  76. anonymous_header1 = anonymous_generator.extend_columns(anonymous_header, extend_columns1)
  77. derived_dict = {"e0": ["feature5_f0", "feature5_f1", "feature5_f2", "feature5_f3"],
  78. "e3": ["feature6_e1", "feature6_e2", "feature6_e3"]}
  79. derived_anonymous_header = anonymous_generator.generate_derived_header(
  80. original_header=extend_header,
  81. original_anonymous_header=anonymous_header1,
  82. derived_dict=derived_dict)
  83. for i in range(100, 104):
  84. self.assertTrue(derived_anonymous_header[i] == anonymous_header1[100] + "_" + str(i - 100))
  85. for i in range(105, 104):
  86. self.assertTrue(derived_anonymous_header[i] == anonymous_header1[102] + "_" + str(i - 105))
  87. def test_update_anonymous_header_with_role(self):
  88. schema = dict(header=["feature" + str(i) for i in range(100)])
  89. anonymous_header_without_role = Anonymous().generate_anonymous_header(schema)
  90. schema["anonymous_header"] = anonymous_header_without_role["anonymous_header"]
  91. schema = Anonymous.update_anonymous_header_with_role(schema, "guest", 10000)
  92. anonymous_header = schema["anonymous_header"]
  93. for i in range(100):
  94. self.assertTrue(anonymous_header[i] == "_".join(["guest", "10000", "x" + str(i)]))
  95. if __name__ == '__main__':
  96. unittest.main()