question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add `flag_values` and `flag_meanings` to 2D masks?

See original GitHub issue

This would allow to indicate which region corresponds to which number via attrs and would work nicely with cf_xarray.

import cf_xarray
import xarray as xr

da = xr.DataArray(
    [[1, 2, 1], [1, 2, np.NaN]],
    dims=("lat", "lon"),
    attrs={
        "flag_values": [1, 2],
        "flag_meanings": "atlantic_ocean pacific_ocean",
        "standard_name": "region",
    },
  name="mask",
)

print(da.cf)

print(da.cf == "atlantic_ocean")

print(da.cf.isin(["indian_ocean", "pacific_ocean"]))

See

Open questions:

  • do we add abbrevs or names (probably abbrevs)
  • does it work for float arrays (as I encode “not belonging to any region” with NaN)?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mathausecommented, Mar 31, 2022

Also I am not 100% clear on what abbrevs vs names means in this context?

Each region has 3 “indices” - a number, an abbreviation, and a name, e.g.:

Regions:
0    Asi          Asia

But we can only attach one of the “abbreviation” and “name” to flag_meanings - but which one should it be?

Currently I am tempted to just assign the abbreviation.

Alternatively we could make it an option:

    def mask(
        self,
        lon_or_obj,
        lat=None,
        lon_name="lon",
        lat_name="lat",
        method=None,
        wrap_lon=None,
+       flag_meanings: Literal["abbrevs", "names"] ="abbrevs",
    ):

The code would then (more or less) just be:


    # abbrevs should not have spaces
    abbrevs = " ".join(abbrevs)
    
    attrs={
        "flag_values": numbers,
        "flag_meanings": abrevs,
        "standard_name": "region",
    },

    mask_2D.attrs = attrs


Another open question - if some regions are missing from the mask - do we still list them under "flag_values" and "flag_meanings"? I’d also need to test this with cf_xarray.

But overall I think this whole thing should be relatively straight-forward…

0reactions
jbuseckecommented, Apr 8, 2022

As long as we have some set way to do it, that seems totally fine to me. But there might be some spec that I am missing here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Mask Graphics in Unity. 2D Sprite Mask ... - YouTube
How to mask graphics in Unity? In this tutorial we will have a look at how we can create and use sprite masks....
Read more >
Masking BGR image using a 2D mask - python - Stack Overflow
Try to use a mask with the same shape as the image (actually, this will be a 3D mask). After generating your image_mask...
Read more >
Blue KF94 2D Mask (Large Black - Adult Size) - Be Healthy
Approved by KFDA (KF94 / KF80) · Premium 4 layers of filters · Blocks over 94% of 0.4µm size particles. · Ultra-light and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found