Skip to content

implement a way to deduplicate labels easily #444

Open

Description

@gdementen

this issue is about appending some suffix (eg _1, _2) to duplicate labels, but the end goal is to make "aggregate all duplicate labels together" as easy as possible.

Activity

  1. gdementen commented on Sep 25, 2017

    @gdementen
    ContributorAuthor

    Here is the code Bernard is currently using to aggregate all duplicate labels. We need to make this easier (and more generic obviously).

    def agg_dupl(mat):
        l_c = []
        for i, p in enumerate(mat.prods):
            l_c.append((p + '_' + str(i)) if p in l_c else str(p))
        mat = mat.set_labels(x.prods, l_c)
        agg = tuple(mat.prods.startswith(code) >> code for code in mat.prods.matches('^.....$'))
        return mat.sum(agg)

    Here is a first step.

    class Axis(object):
        def deduplicate(self):
            # TODO: handle non-string labels (unsure how!? add a user-defined constant?)
            seen = set()
            labels = []
            for i, label in enumerate(self.labels):
                if label in seen:
                    label = '{}_{}'.format(label, i)
                else:
                    seen.add(label)
                labels.append(label)
            return Axis(labels, self.name)
    
    def agg_dupl(mat):    
        mat = mat.set_axes('prods', mat.prods.deduplicate())
        agg = tuple(mat.prods.startswith(code) >> code for code in mat.prods.matches('^.....$'))
        return mat.sum(agg)

    Remaining problems:

    • the aggregation relies on the fact his codes are always exactly the same length
    • we should provide a way to "deduplicate()" all axes at once

    We should be able to write something like:

    def agg_dupl(mat):
        mat = mat.deduplicate()
        agg = tuple(mat.prods.startingwith(code) >> code for code in mat.prods[~mat.prods.endswith('/DUPE/')])
        return mat.sum(agg)

    or even

    def agg_dupl(mat):
        mat = mat.deduplicate()
        agg = tuple(mat.prods.startingwith(code) >> code for code in ~mat.prods.endingwith('/DUPE/'))
        return mat.sum(agg)

    Remaining problems:

    • ideally the .deduplicate() step should not be necessary. It should be possible to easily create a positional-based agg directly.
  2. added this to the 0.30 milestone on Jan 25, 2018
  3. gdementen commented on Mar 14, 2018

    @gdementen
    ContributorAuthor

    This particular problem (aggregating all duplicates) can be solved much more elegantly by a groupby.

    agg = mat.groupby(mat.prods).sum()
    # and/or
    agg = mat.sum(mat.prods.groupunique())

    The question is then whether de-duplication has value outside of aggregating the duplicates. I can see some cases where this is true, but it is lower priority I think.

  4. self-assigned this
    on Mar 23, 2018
  5. added 2 commits that reference this issue on Jul 20, 2018
    0cc272f
    66ecfb3
  6. removed their assignment
    on Aug 23, 2018
  7. modified the milestones: 0.30, on Oct 16, 2018
  8. removed this from the milestone on Aug 1, 2019
  9. added this to the nice_to_have milestone on Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions