|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""PyMilo chain for cross decomposition models.""" |
| 3 | +from ..transporters.transporter import Command |
| 4 | + |
| 5 | +from ..transporters.general_data_structure_transporter import GeneralDataStructureTransporter |
| 6 | +from ..transporters.preprocessing_transporter import PreprocessingTransporter |
| 7 | + |
| 8 | +from ..pymilo_param import SKLEARN_CROSS_DECOMPOSITION_TABLE |
| 9 | +from ..exceptions.serialize_exception import PymiloSerializationException, SerializationErrorTypes |
| 10 | +from ..exceptions.deserialize_exception import PymiloDeserializationException, DeserializationErrorTypes |
| 11 | + |
| 12 | +from ..utils.util import get_sklearn_type |
| 13 | + |
| 14 | +from traceback import format_exc |
| 15 | + |
| 16 | +CROSS_DECOMPOSITION_CHAIN = { |
| 17 | + "PreprocessingTransporter": PreprocessingTransporter(), |
| 18 | + "GeneralDataStructureTransporter": GeneralDataStructureTransporter(), |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def is_cross_decomposition(model): |
| 23 | + """ |
| 24 | + Check if the input model is a sklearn's cross decomposition model. |
| 25 | +
|
| 26 | + :param model: is a string name of a cross decomposition or a sklearn object of it |
| 27 | + :type model: any object |
| 28 | + :return: check result as bool |
| 29 | + """ |
| 30 | + if isinstance(model, str): |
| 31 | + return model in SKLEARN_CROSS_DECOMPOSITION_TABLE |
| 32 | + else: |
| 33 | + return get_sklearn_type(model) in SKLEARN_CROSS_DECOMPOSITION_TABLE.keys() |
| 34 | + |
| 35 | + |
| 36 | +def transport_cross_decomposition(request, command, is_inner_model=False): |
| 37 | + """ |
| 38 | + Return the transported (Serialized or Deserialized) model. |
| 39 | +
|
| 40 | + :param request: given cross decomposition model to be transported |
| 41 | + :type request: any object |
| 42 | + :param command: command to specify whether the request should be serialized or deserialized |
| 43 | + :type command: transporter.Command |
| 44 | + :param is_inner_model: determines whether it is an inner model of a super ml model |
| 45 | + :type is_inner_model: boolean |
| 46 | + :return: the transported request as a json string or sklearn cross decomposition model |
| 47 | + """ |
| 48 | + if not is_inner_model: |
| 49 | + _validate_input(request, command) |
| 50 | + |
| 51 | + if command == Command.SERIALIZE: |
| 52 | + try: |
| 53 | + return serialize_cross_decomposition(request) |
| 54 | + except Exception as e: |
| 55 | + raise PymiloSerializationException( |
| 56 | + { |
| 57 | + 'error_type': SerializationErrorTypes.VALID_MODEL_INVALID_INTERNAL_STRUCTURE, |
| 58 | + 'error': { |
| 59 | + 'Exception': repr(e), |
| 60 | + 'Traceback': format_exc(), |
| 61 | + }, |
| 62 | + 'object': request, |
| 63 | + }) |
| 64 | + |
| 65 | + elif command == Command.DESERIALIZE: |
| 66 | + try: |
| 67 | + return deserialize_cross_decomposition(request, is_inner_model) |
| 68 | + except Exception as e: |
| 69 | + raise PymiloDeserializationException( |
| 70 | + { |
| 71 | + 'error_type': SerializationErrorTypes.VALID_MODEL_INVALID_INTERNAL_STRUCTURE, |
| 72 | + 'error': { |
| 73 | + 'Exception': repr(e), |
| 74 | + 'Traceback': format_exc()}, |
| 75 | + 'object': request}) |
| 76 | + |
| 77 | + |
| 78 | +def serialize_cross_decomposition(cross_decomposition_object): |
| 79 | + """ |
| 80 | + Return the serialized json string of the given cross decomposition model. |
| 81 | +
|
| 82 | + :param cross_decomposition_object: given model to be get serialized |
| 83 | + :type cross_decomposition_object: any sklearn cross decomposition model |
| 84 | + :return: the serialized json string of the given cross decomposition model |
| 85 | + """ |
| 86 | + for transporter in CROSS_DECOMPOSITION_CHAIN: |
| 87 | + CROSS_DECOMPOSITION_CHAIN[transporter].transport( |
| 88 | + cross_decomposition_object, Command.SERIALIZE) |
| 89 | + return cross_decomposition_object.__dict__ |
| 90 | + |
| 91 | + |
| 92 | +def deserialize_cross_decomposition(cross_decomposition, is_inner_model=False): |
| 93 | + """ |
| 94 | + Return the associated sklearn cross decomposition model. |
| 95 | +
|
| 96 | + :param cross_decomposition: given json string of a cross decomposition model to get deserialized to associated sklearn cross decomposition model |
| 97 | + :type cross_decomposition: obj |
| 98 | + :param is_inner_model: determines whether it is an inner linear model of a super ml model |
| 99 | + :type is_inner_model: boolean |
| 100 | + :return: associated sklearn cross decomposition model |
| 101 | + """ |
| 102 | + raw_model = None |
| 103 | + data = None |
| 104 | + if is_inner_model: |
| 105 | + raw_model = SKLEARN_CROSS_DECOMPOSITION_TABLE[cross_decomposition["type"]]() |
| 106 | + data = cross_decomposition["data"] |
| 107 | + else: |
| 108 | + raw_model = SKLEARN_CROSS_DECOMPOSITION_TABLE[cross_decomposition.type]() |
| 109 | + data = cross_decomposition.data |
| 110 | + |
| 111 | + for transporter in CROSS_DECOMPOSITION_CHAIN: |
| 112 | + CROSS_DECOMPOSITION_CHAIN[transporter].transport( |
| 113 | + cross_decomposition, Command.DESERIALIZE, is_inner_model) |
| 114 | + for item in data: |
| 115 | + setattr(raw_model, item, data[item]) |
| 116 | + return raw_model |
| 117 | + |
| 118 | + |
| 119 | +def _validate_input(model, command): |
| 120 | + """ |
| 121 | + Check if the provided inputs are valid in relation to each other. |
| 122 | +
|
| 123 | + :param model: a sklearn cross decomposition model or a json string of it, serialized through the pymilo export. |
| 124 | + :type model: obj |
| 125 | + :param command: command to specify whether the request should be serialized or deserialized |
| 126 | + :type command: transporter.Command |
| 127 | + :return: None |
| 128 | + """ |
| 129 | + if command == Command.SERIALIZE: |
| 130 | + if is_cross_decomposition(model): |
| 131 | + return |
| 132 | + else: |
| 133 | + raise PymiloSerializationException( |
| 134 | + { |
| 135 | + 'error_type': SerializationErrorTypes.INVALID_MODEL, |
| 136 | + 'object': model |
| 137 | + } |
| 138 | + ) |
| 139 | + elif command == Command.DESERIALIZE: |
| 140 | + if is_cross_decomposition(model.type): |
| 141 | + return |
| 142 | + else: |
| 143 | + raise PymiloDeserializationException( |
| 144 | + { |
| 145 | + 'error_type': DeserializationErrorTypes.INVALID_MODEL, |
| 146 | + 'object': model |
| 147 | + } |
| 148 | + ) |
0 commit comments