feature_utils.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 numpy
  17. def get_component_output_data_line(src_key, src_value, schema=None, all_extend_header=None):
  18. if not all_extend_header:
  19. for inst in ["inst_id", "label", "weight"]:
  20. all_extend_header[inst] = ""
  21. data_line = [src_key]
  22. is_str = False
  23. if hasattr(src_value, "is_instance"):
  24. for inst in ["inst_id", "label", "weight"]:
  25. if getattr(src_value, inst) is not None:
  26. data_line.append(getattr(src_value, inst))
  27. if inst == "inst_id" and schema:
  28. all_extend_header[inst] = schema.get("match_id_name")
  29. else:
  30. if not all_extend_header[inst]:
  31. all_extend_header[inst] = inst
  32. elif inst == "inst_id" and schema.get("match_id_name"):
  33. data_line.append(None)
  34. elif inst == "label" and schema.get("label_name"):
  35. data_line.append(None)
  36. data_line.extend(dataset_to_list(src_value.features))
  37. elif isinstance(src_value, str):
  38. data_line.extend([value for value in src_value.split(',')])
  39. is_str = True
  40. else:
  41. data_line.extend(dataset_to_list(src_value))
  42. return data_line, is_str, all_extend_header
  43. def generate_header(all_extend_header, schema):
  44. extend_header = []
  45. for inst in ["inst_id", "label", "weight"]:
  46. if all_extend_header.get(inst):
  47. extend_header.append(all_extend_header[inst])
  48. if not all_extend_header.get(inst) and inst == "inst_id" and schema.get("match_id_name"):
  49. extend_header.append(schema.get("match_id_name"))
  50. if not all_extend_header.get(inst) and inst == "label" and schema.get("label_name"):
  51. extend_header.append(inst)
  52. return extend_header
  53. def get_deserialize_value(src_value, id_delimiter):
  54. extend_header = []
  55. if hasattr(src_value, "is_instance"):
  56. v_list = []
  57. for inst in ["inst_id", "label", "weight"]:
  58. if getattr(src_value, inst) is not None:
  59. v_list.append(getattr(src_value, inst))
  60. extend_header.append(inst)
  61. v_list.extend(dataset_to_list(src_value.features))
  62. v_list = list(map(str, v_list))
  63. deserialize_value = id_delimiter.join(v_list)
  64. elif isinstance(src_value, str):
  65. deserialize_value = src_value
  66. else:
  67. deserialize_value = id_delimiter.join(list(map(str, dataset_to_list(src_value))))
  68. return deserialize_value, extend_header
  69. def dataset_to_list(src):
  70. if isinstance(src, numpy.ndarray):
  71. return src.tolist()
  72. elif isinstance(src, list):
  73. return src
  74. elif hasattr(src, "is_sparse_vector"):
  75. vector = [0] * src.get_shape()
  76. for idx, v in src.get_all_data():
  77. vector[idx] = v
  78. return vector
  79. else:
  80. return [src]