def rename_feature(spec, current_name, new_name, rename_inputs=True,
                   rename_outputs=True):
    """
    Rename a feature in the specification.

    Parameters
    ----------
    spec: Model_pb
        The specification containing the feature to rename.

    current_name: str
        Current name of the feature. If this feature doesn't exist, the rename
        is a no-op.

    new_name: str
        New name of the feature.

    rename_inputs: bool
        Search for `current_name` only in the input features (i.e ignore output
        features)

    rename_outputs: bool
        Search for `current_name` only in the output features (i.e ignore input
        features)

    Examples
    --------
    .. sourcecode:: python

        # In-place rename of spec
        >>> coremltools.utils.rename_feature(spec, 'old_feature', 'new_feature_name')
    """
    from coremltools.models import MLModel

    if not rename_inputs and not rename_outputs:
        return

    changed_input = False
    changed_output = False

    if rename_inputs:
        for input in spec.description.input:
            if input.name == current_name:
                input.name = new_name
                changed_input = True

    if rename_outputs:
        for output in spec.description.output:
            if output.name == current_name:
                output.name = new_name
                changed_output = True

        if spec.description.predictedFeatureName == current_name:
            spec.description.predictedFeatureName = new_name

        if spec.description.predictedProbabilitiesName == current_name:
            spec.description.predictedProbabilitiesName = new_name

    if not changed_input and not changed_output:
        return

    # Rename internally in NN model
    nn = None
    for nn_type in ['neuralNetwork','neuralNetworkClassifier','neuralNetworkRegressor']:
        if spec.HasField(nn_type):
            nn = getattr(spec,nn_type)

    if nn is not None:
        for layer in nn.layers:
            if rename_inputs:
                for index,name in enumerate(layer.input):
                    if name == current_name:
                        layer.input[index] = new_name
                if rename_outputs:
                    for index,name in enumerate(layer.output):
                        if name == current_name:
                            layer.output[index] = new_name

    # Rename internally for feature vectorizer
    if spec.HasField('featureVectorizer') and rename_inputs:
        for input in spec.featureVectorizer.inputList:
            if input.inputColumn == current_name:
                input.inputColumn = new_name
                changed_input = True

    # Rename for pipeline models
    pipeline = None
    if spec.HasField('pipeline'):
        pipeline = spec.pipeline
    elif spec.HasField('pipelineClassifier'):
        pipeline = spec.pipelineClassifier.pipeline
    elif spec.HasField('pipelineRegressor'):
        pipeline = spec.pipelineRegressor.pipeline

    if pipeline is not None:
        for index,model in enumerate(pipeline.models):
            rename_feature(model,
                           current_name,
                           new_name,
                           rename_inputs or (index != 0),
                           rename_outputs or (index < len(spec.pipeline.models)))
def rename_feature ( spec , current_name , new_name , rename_inputs = True , rename_outputs = True ) : from coremltools . models import MLModel if not rename_inputs and not rename_outputs : return changed_input = False changed_output = False if rename_inputs : for input in spec . description . input : if input . name == current_name : input . name = new_name changed_input = True if rename_outputs : for output in spec . description . output : if output . name == current_name : output . name = new_name changed_output = True if spec . description . predictedFeatureName == current_name : spec . description . predictedFeatureName = new_name if spec . description . predictedProbabilitiesName == current_name : spec . description . predictedProbabilitiesName = new_name if not changed_input and not changed_output : return nn = None for nn_type in [ 'neuralNetwork' , 'neuralNetworkClassifier' , 'neuralNetworkRegressor' ] : if spec . HasField ( nn_type ) : nn = getattr ( spec , nn_type ) if nn is not None : for layer in nn . layers : if rename_inputs : for index , name in enumerate ( layer . input ) : if name == current_name : layer . input [ index ] = new_name if rename_outputs : for index , name in enumerate ( layer . output ) : if name == current_name : layer . output [ index ] = new_name if spec . HasField ( 'featureVectorizer' ) and rename_inputs : for input in spec . featureVectorizer . inputList : if input . inputColumn == current_name : input . inputColumn = new_name changed_input = True pipeline = None if spec . HasField ( 'pipeline' ) : pipeline = spec . pipeline elif spec . HasField ( 'pipelineClassifier' ) : pipeline = spec . pipelineClassifier . pipeline elif spec . HasField ( 'pipelineRegressor' ) : pipeline = spec . pipelineRegressor . pipeline if pipeline is not None : for index , model in enumerate ( pipeline . models ) : rename_feature ( model , current_name , new_name , rename_inputs or ( index != 0 ) , rename_outputs or ( index < len ( spec . pipeline . models ) ) )
Rename a feature in the specification.

    Parameters
    ----------
    spec: Model_pb
        The specification containing the feature to rename.

    current_name: str
        Current name of the feature. If this feature doesn't exist, the rename
        is a no-op.

    new_name: str
        New name of the feature.

    rename_inputs: bool
        Search for `current_name` only in the input features (i.e ignore output
        features)

    rename_outputs: bool
        Search for `current_name` only in the output features (i.e ignore input
        features)

    Examples
    --------
    .. sourcecode:: python

        # In-place rename of spec
        >>> coremltools.utils.rename_feature(spec, 'old_feature', 'new_feature_name')