def recurse_json(mlkit_tree, xgb_tree_json, tree_id, node_id, feature_map,
        force_32bit_float):
    """Traverse through the tree and append to the tree spec.
    """
    relative_hit_rate = None

    try:
        relative_hit_rate = xgb_tree_json['cover']
    except KeyError:
        pass


    # Fill node attributes
    if 'leaf' not in xgb_tree_json:
        branch_mode = 'BranchOnValueLessThan'
        split_name = xgb_tree_json['split']
        feature_index = split_name if not feature_map else feature_map[split_name]

        # xgboost internally uses float32, but the parsing from json pulls it out
        # as a 64bit double.  To trigger the internal float32 detection in the
        # tree ensemble compiler, we need to explicitly cast it to a float 32
        # value, then back to the 64 bit float that protobuf expects.  This is
        # controlled with the force_32bit_float flag.
        feature_value = xgb_tree_json['split_condition']

        if force_32bit_float:
            feature_value = float(_np.float32(feature_value))


        true_child_id = xgb_tree_json['yes']
        false_child_id = xgb_tree_json['no']

        # Get the missing value behavior correct
        missing_value_tracks_true_child = False

        try:
            if xgb_tree_json['missing'] == true_child_id:
                missing_value_tracks_true_child = True
        except KeyError:
            pass

        mlkit_tree.add_branch_node(tree_id, node_id, feature_index,
                feature_value, branch_mode, true_child_id, false_child_id,
                relative_hit_rate = relative_hit_rate,
                missing_value_tracks_true_child = missing_value_tracks_true_child)

    else:
        value = xgb_tree_json["leaf"]

        if force_32bit_float:
            value = float(_np.float32(value))

        mlkit_tree.add_leaf_node(tree_id, node_id, value,
                relative_hit_rate = relative_hit_rate)

    # Now recurse
    if "children" in xgb_tree_json:
        for child in xgb_tree_json["children"]:
            recurse_json(mlkit_tree, child, tree_id, child['nodeid'], feature_map, force_32bit_float)
def recurse_json ( mlkit_tree , xgb_tree_json , tree_id , node_id , feature_map , force_32bit_float ) : relative_hit_rate = None try : relative_hit_rate = xgb_tree_json [ 'cover' ] except KeyError : pass if 'leaf' not in xgb_tree_json : branch_mode = 'BranchOnValueLessThan' split_name = xgb_tree_json [ 'split' ] feature_index = split_name if not feature_map else feature_map [ split_name ] feature_value = xgb_tree_json [ 'split_condition' ] if force_32bit_float : feature_value = float ( _np . float32 ( feature_value ) ) true_child_id = xgb_tree_json [ 'yes' ] false_child_id = xgb_tree_json [ 'no' ] missing_value_tracks_true_child = False try : if xgb_tree_json [ 'missing' ] == true_child_id : missing_value_tracks_true_child = True except KeyError : pass mlkit_tree . add_branch_node ( tree_id , node_id , feature_index , feature_value , branch_mode , true_child_id , false_child_id , relative_hit_rate = relative_hit_rate , missing_value_tracks_true_child = missing_value_tracks_true_child ) else : value = xgb_tree_json [ "leaf" ] if force_32bit_float : value = float ( _np . float32 ( value ) ) mlkit_tree . add_leaf_node ( tree_id , node_id , value , relative_hit_rate = relative_hit_rate ) if "children" in xgb_tree_json : for child in xgb_tree_json [ "children" ] : recurse_json ( mlkit_tree , child , tree_id , child [ 'nodeid' ] , feature_map , force_32bit_float )
Traverse through the tree and append to the tree spec.