def _get_ds_factors(ds):
    """
        return factors in the order of ds.factors, ds.metadata['factors'] or []
    """
    # set to ds.factors if provided
    factors = ds.factors or []
    # otherwise, taking from ds.metadata if factorvalue is provided
    if not factors and 'factors' in ds.metadata:
        if 'factorvalue' in list(ds.metadata['factors'][0].values())[0]:
            factors = [list(e.values())[0]['factorvalue'] for e in ds.metadata['factors']]
    return factors