io_check.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from fate_arch.computing import is_table
  17. from federatedml.util import LOGGER
  18. def assert_io_num_rows_equal(func):
  19. def _func(*args, **kwargs):
  20. input_count = None
  21. all_args = []
  22. all_args.extend(args)
  23. all_args.extend(kwargs.values())
  24. for arg in all_args:
  25. if is_table(arg):
  26. input_count = arg.count()
  27. break
  28. result = func(*args, **kwargs)
  29. if input_count is not None and is_table(result):
  30. output_count = result.count()
  31. LOGGER.debug(f"num row of input: {input_count} -> num row of output: {output_count}")
  32. if input_count != output_count:
  33. raise EnvironmentError(
  34. f"num row of input({input_count}) not equals to num row of output({output_count})")
  35. return result
  36. return _func
  37. def check_with_inst_id(data_instances):
  38. instance = data_instances.first()[1]
  39. if type(instance).__name__ == "Instance" and instance.with_inst_id:
  40. return True
  41. return False
  42. def check_is_instance(data_instances):
  43. instance = data_instances.first()[1]
  44. if type(instance).__name__ == "Instance":
  45. return True
  46. return False
  47. def assert_match_id_consistent(func):
  48. def _func(*args, **kwargs):
  49. input_with_inst_id = None
  50. all_args = []
  51. all_args.extend(args)
  52. all_args.extend(kwargs.values())
  53. for arg in all_args:
  54. if is_table(arg):
  55. input_with_inst_id = check_with_inst_id(arg)
  56. break
  57. result = func(*args, **kwargs)
  58. if input_with_inst_id is not None and is_table(result):
  59. if check_is_instance(result):
  60. result_with_inst_id = check_with_inst_id(result)
  61. LOGGER.debug(
  62. f"Input with match id: {input_with_inst_id} -> output with match id: {result_with_inst_id}")
  63. if input_with_inst_id and not result_with_inst_id:
  64. raise EnvironmentError(
  65. f"Input with match id: {input_with_inst_id} -> output with match id: {result_with_inst_id},"
  66. f"func: {func}")
  67. return result
  68. return _func