<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def reduce_to_parent_states(models):
 """Remove all models of states that have a state model with parent relation in the list The function filters the list of models, so that for no model in the list, one of it (grand-)parents is also in the list. E.g. if the input models consists of a hierarchy state with two of its child states, the resulting list only contains the hierarchy state. :param set models: The set of selected models :return: The reduced set of selected models :rtype: set """

    models = set(models)  # Ensure that models is a set and that we do not operate on the parameter itself
    models_to_remove = set()
    # check all models
    for model in models:
        parent_m = model.parent
        # check if any (grand-)parent is already in the selection, if so, remove the child
        while parent_m is not None:
            if parent_m in models:
                models_to_remove.add(model)
                break
            parent_m = parent_m.parent
    for model in models_to_remove:
        models.remove(model)
    if models_to_remove:
        logger.debug("The selection has been reduced, as it may not contain elements whose children are also selected")
    return models