Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option in Array.set_labels to ignore missing labels #1090

Open
gdementen opened this issue Oct 23, 2023 · 2 comments
Open

add an option in Array.set_labels to ignore missing labels #1090

gdementen opened this issue Oct 23, 2023 · 2 comments

Comments

@gdementen
Copy link
Contributor

When applying the same map (for example to translate several arrays for final reporting), it is very tedious to have to compute the correct mapping for each array (intersect the global mapping keys with the axis labels).

>>> arr = ndtest(3)
>>> arr.set_labels({'a1': 'A1', 'a3': 'A3'})
ValueError: 'a3' is not a valid label for any axis:
 a [3]: 'a0' 'a1' 'a2'

Since this could hide errors and it sort-of-conflicts with #906, we cannot do this by default. Having an option would be nice though.

@gdementen
Copy link
Contributor Author

FWIW, I needed this for dc2019

@gdementen
Copy link
Contributor Author

gdementen commented Jun 3, 2024

I needed this for the promes project too.

Here is some preliminary code to do so:

def set_labels(arr: la.Array, labelmaps: dict) -> la.Array:
    def new_labels(axis: la.Axis, labelmap: dict) -> list:
        return [labelmap.get(label, label) for label in axis.labels]

    return arr.set_labels({axis: new_labels(axis, labelmaps[axis.id])
                           for axis in arr.axes if axis.id in labelmaps})

In dc2024 (and possibly 2019), I used different code to be able to rename the axis at the same time, by using a special __name__ label but I am unsure it is a good idea to support this in LArray itself.

Test code:

>>> arr = ndtest((2, 3))
>>> set_labels(arr, {'b': {'b1': 'B1', 'b3': 'B3'}, 'c': {'c1': 'C1'}})
a\b  b0  B1  b2
 a0   0   1   2
 a1   3   4   5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant