import pickle
import dill


def unpickle_custom_operators(filename):
    """Load the custom operators from pickled file

    Args:
        filename (str): string indicates the path to custom operator pickled file

    Return:
        Lists of nodal, pool, operator set

    """

    nodal_set = []
    pool_set = []
    activation_set = []

    with open(filename, 'rb') as fid:
        operators = pickle.load(fid)

    pickled_func = operators['pickled_func']

    for nodal in operators['nodal_set']:
        if isinstance(nodal, str):
            nodal_set.append(nodal)
        else:
            nodal_set.append(dill.loads(pickled_func[nodal]))

    for pool in operators['pool_set']:
        if isinstance(pool, str):
            pool_set.append(pool)
        else:
            pool_set.append(dill.loads(pickled_func[pool]))

    for activation in operators['activation_set']:
        if isinstance(activation, str):
            activation_set.append(activation)
        else:
            activation_set.append(dill.loads(pickled_func[activation]))

    return nodal_set, pool_set, activation_set