Quantipy / quantipy

Python for people data
MIT License
66 stars 14 forks source link

Batch.adds_as_main(keep_main=False) needed #1191

Closed alextanski closed 5 years ago

alextanski commented 5 years ago

This should turn additional batches into main ones with the option to destroy the parent or keep all of them.

@nitsrek ^^ 🤦‍♂️

alextanski commented 5 years ago

better name might be as_main(keep_parent=False) after all.

alextanski commented 5 years ago
def _typed_batches(dataset, kind='main'):
    """
    """
    verbose = dataset._verbose_infos
    dataset._verbose_infos = False
    batches = dataset.batches()
    if kind == 'main':
        typed_batches = [b for b in batches if not dataset.get_batch(b).additional]
    elif kind == 'add':
        typed_batches = [b for b in batches if dataset.get_batch(b).additional]
    dataset._verbose_infos = verbose
    return typed_batches 

def _adds_per_mains(bmeta, reverse=False):
    """
    """
    adds_per_mains = {bname: bdef['additions'] for bname, bdef in bmeta.items() if bdef['additions']}
    if not reverse:
        return adds_per_mains
    else:
        rev = {}
        adds = _typed_batches(dataset, 'add')
        for add in adds:
            for m, a in adds_per_mains.items():
                if add in a:
                    if add in rev:
                        rev[add].append(m)
                    else:
                        rev[add] = [m]
        return rev

def as_main(batch, keep=False):
    """
    """
    if not batch.additional: return None
    batch.additional = False
    bmeta = batch._meta['sets']['batches']
    parent = _adds_per_mains(bmeta, True)[batch.name]
    for p in parent:
        if not keep:
            del bmeta[p]
        else:
            bmeta[p]['additions'].remove(batch.name)
    batch._update()
alextanski commented 5 years ago

this is done.