def to_data(ftr, port=0):
    """Extract data from filter.

    Parameters
    ----------
    ftr : vtkAlgorithm or :class:`.BSAlgorithm`
        Input filter.
    port : int, optional
        Port to get data from. When port is -1, refers to all ports.
        Default is 0.

    Returns
    -------
    data : BSDataObject or list of BSDataObject
        Returns the output of the filter. If port is -1 and number of output
        ports > 1, then return list of outputs.

    Notes
    -----
    Filters are automatically updated to get the output.
    """
    list_ports = [port] if port > -1 else range(ftr.nop)
    n_ports = len(list_ports)
    out = [None] * n_ports
    for i, port_id in enumerate(list_ports):
        ftr.Update(port_id)
        out[i] = ftr.GetOutputDataObject(port_id)

    if port > -1:
        return out[0]
    return out