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.

Conflicting sizes for region and dataset when trying to create mask

See original GitHub issue

Hi! I’ve been trying to create a mask for a dataset:

basins = regionmask.defined_regions.natural_earth.ocean_basins_50
mask = basins.mask(cmip6_compiled['CMIP.MPI-M.MPI-ESM1-2-HR.historical.Omon.gn'], lon_name='longitude', lat_name='latitude')

but this error appears:


ValueError                                Traceback (most recent call last)
/tmp/ipykernel_1285/730184074.py in <module>
      1 basins = regionmask.defined_regions.natural_earth.ocean_basins_50
----> 2 mask = basins.mask(cmip6_compiled['CMIP.MPI-M.MPI-ESM1-2-HR.historical.Omon.gn'], lon_name='longitude', lat_name='latitude')

/srv/conda/envs/notebook/lib/python3.9/site-packages/regionmask/core/regions.py in mask(self, lon_or_obj, lat, lon_name, lat_name, method, wrap_lon)
    282     ):
    283 
--> 284         return _mask_2D(
    285             outlines=self.polygons,
    286             lon_bounds=self.bounds_global[::2],

/srv/conda/envs/notebook/lib/python3.9/site-packages/regionmask/core/mask.py in _mask_2D(outlines, lon_bounds, numbers, lon_or_obj, lat, lon_name, lat_name, method, wrap_lon)
    198 ):
    199 
--> 200     mask = _mask(
    201         outlines=outlines,
    202         lon_bounds=lon_bounds,

/srv/conda/envs/notebook/lib/python3.9/site-packages/regionmask/core/mask.py in _mask(outlines, lon_bounds, numbers, lon_or_obj, lat, lon_name, lat_name, method, wrap_lon)
    183         )
    184 
--> 185     return mask_to_dataarray(mask, lon_or_obj, lat_orig, lon_name, lat_name)
    186 
    187 

/srv/conda/envs/notebook/lib/python3.9/site-packages/regionmask/core/mask.py in mask_to_dataarray(mask, lon_or_obj, lat, lon_name, lat_name)
    323         dims = ds.dims.keys()
    324 
--> 325     return ds.assign(region=(dims, mask)).region
    326 
    327 

/srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/dataset.py in assign(self, variables, **variables_kwargs)
   5226         results = data._calc_assign_results(variables)
   5227         # ... and then assign
-> 5228         data.update(results)
   5229         return data
   5230 

/srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/dataset.py in update(self, other)
   4206         Dataset.merge
   4207         """
-> 4208         merge_result = dataset_update_method(self, other)
   4209         return self._replace(inplace=True, **merge_result._asdict())
   4210 

/srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/merge.py in dataset_update_method(dataset, other)
    982     # TODO: benbovy - flexible indexes: fix this (it only works with pandas indexes)
    983     indexes = {key: PandasIndex(dataset.coords[key]) for key in dataset.xindexes.keys()}
--> 984     return merge_core(
    985         [dataset, other],
    986         priority_arg=1,

/srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/merge.py in merge_core(objects, compat, join, combine_attrs, priority_arg, explicit_coords, indexes, fill_value)
    638     assert_unique_multiindex_level_names(variables)
    639 
--> 640     dims = calculate_dimensions(variables)
    641 
    642     coord_names, noncoord_names = determine_coords(coerced)

/srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/dataset.py in calculate_dimensions(variables)
    206                 last_used[dim] = k
    207             elif dims[dim] != size:
--> 208                 raise ValueError(
    209                     f"conflicting sizes for dimension {dim!r}: "
    210                     f"length {size} on {k!r} and length {dims[dim]} on {last_used!r}"

ValueError: conflicting sizes for dimension 'i': length 404 on 'region' and length 802 on {'i': 'i', 'j': 'j'}

I’m not sure, but maybe the i and j variables are getting switched? Here are the dimensions for the dataset:

xarray.Dataset
Dimensions: i: 802 j: 404 lev: 40 bnds: 2 member_id: 1 time: 1980 vertices: 4

And these are the package versions that I used:

regionmask='0.8.1.dev11+g3fab767'
xarray='0.19.0'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mathausecommented, Nov 26, 2021

Thanks again for the error report - I am very happy I could fix this before releasing!

1reaction
mathausecommented, Nov 24, 2021

Let’s keep this open until I can fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create 3D boolean masks — regionmask 0.7.0 documentation
As mentioned, mask is a boolean xarray.Dataset with shape region x lat x lon . It contains region (= numbers ) as dimension...
Read more >
Managing and animating shape paths and masks in After Effects
To specify the size of Bezier direction handles and vertices for masks and shapes, choose Edit > Preferences > General (Windows) or After ......
Read more >
Collectivism predicts mask use during COVID-19 - PNAS
Analyzing a dataset of 367,109 individuals in 29 coun- tries, Study 2 revealed that mask usage was higher in more collec-.
Read more >
python Xarray lenght mismatch - Stack Overflow
I'm learning the Xarray library and I'm trying to create Xarrays for 3D data. The following snippet does not work:
Read more >
Indexing and selecting data - Xarray
As xarray objects can store coordinates corresponding to each dimension of an array, label-based indexing similar to pandas.DataFrame.loc is also possible. In ...
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