antarctica / MeshiPhi

MIT License
1 stars 0 forks source link

Dataloaders can't handle slices of xarray datasets that are empty #30

Closed hjabbot closed 4 months ago

hjabbot commented 7 months ago

I assume this crash happens when the split depth is too high such that the dataloader has no data within the sliced region. Config that crashes

Traceback (most recent call last):
  File "/users/polarroute/staging_pipeline/pipeline/venv/bin/create_mesh", line 33, in <module>
    sys.exit(load_entry_point('meshiphi', 'console_scripts', 'create_mesh')())
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/utils.py", line 231, in wrapper
    res = func(*args, **kwargs)
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/cli.py", line 100, in create_mesh_cli
    cg = MeshBuilder(config).build_environmental_mesh()
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/mesh_generation/mesh_builder.py", line 553, in build_environmental_mesh
    self.split_to_depth(self.mesh.get_max_split_depth())
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/mesh_generation/mesh_builder.py", line 537, in split_to_depth
    should_split = cellbox.should_split(index+1)
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/mesh_generation/cellbox.py", line 174, in should_split
    hom_cond = data_loader.get_hom_condition(
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/dataloaders/scalar/abstract_scalar.py", line 677, in get_hom_condition
    dps = self.trim_datapoints(bounds)[self.data_name]
  File "/users/polarroute/staging_pipeline/MeshiPhi/meshiphi/dataloaders/scalar/abstract_scalar.py", line 324, in trim_datapoints
    if data.lat.min() >  bounds.get_lat_min() and \
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/xarray/core/_aggregations.py", line 1579, in min
    return self.reduce(
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/xarray/core/dataarray.py", line 3667, in reduce
    var = self.variable.reduce(func, dim, axis, keep_attrs, keepdims, **kwargs)
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/xarray/core/variable.py", line 1957, in reduce
    data = func(self.data, **kwargs)
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/xarray/core/duck_array_ops.py", line 378, in f
    return func(values, axis=axis, **kwargs)
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/xarray/core/nanops.py", line 66, in nanmin
    return nputils.nanmin(a, axis=axis)
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/xarray/core/nputils.py", line 157, in f
    result = getattr(npmodule, name)(values, axis=axis, **kwargs)
  File "<__array_function__ internals>", line 200, in nanmin
  File "/users/polarroute/staging_pipeline/pipeline/venv/lib/python3.8/site-packages/numpy/lib/nanfunctions.py", line 343, in nanmin
    res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
ValueError: zero-size array to reduction operation fmin which has no identity
SamuelHall700 commented 7 months ago

The config attached contains a dataloader for amsr2 north, though the region of the config is entirely within the southern hemisphere. Removing the dataloader for amsr2 north fixes the issue locally and the mesh built just fine image I've attached an updated config file here: amsr_southern.config.json hopefully this can be swapped out in the pipeline and then everything should run smoothly again! @hjabbot can you test and close the issue if it all works?

hjabbot commented 7 months ago

Yup, well spotted :) Thanks for the debug; this is a non-issue

JimCircadian commented 6 months ago

Hey @hjabbot / @SamuelHall700

I'm seeing this issue having installed from main, with my configuration being largely the same as the one Sam recommends (a bit simpler, adapted from the pipeline repo.)

test_local.config.json

 create_mesh /home/jambyr/code/amop/polarroute-pipeline/configs/environment_configs/test_local.config.json -o /home/jambyr/code/amop/polarroute-pipeline/outputs/test_local/2024-03-15/test_local.mesh.json
+ tee /home/jambyr/code/amop/polarroute-pipeline/logs/test_local_2024-03-15.out
[15-03-24 15:50:15 :INFO    ] - create_mesh 1.1.6
[15-03-24 15:50:15 :INFO    ] - Initialising Mesh Builder
[15-03-24 15:50:15 :INFO    ] - Initialising mesh...
[15-03-24 15:50:15 :INFO    ] - Initialising cellboxes...
[15-03-24 15:50:15 :INFO    ] - Initialising gebco dataloader
/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/backends/plugins.py:71: RuntimeWarning: Engine 'cfgrib' loading failed:
Cannot find the ecCodes library
  warnings.warn(f"Engine {name!r} loading failed:\n{ex}", RuntimeWarning)
[15-03-24 15:50:15 :INFO    ] -     Files read:
[15-03-24 15:50:15 :INFO    ] -         datastore/bathymetry/gebco/gebco_global.nc
[15-03-24 15:50:17 :INFO    ] -     Mercator data range (roughly) covers 100% of initial boundary
[15-03-24 15:50:17 :INFO    ] -     Trimming data to initial boundary: (0, -40) to (85, 50)
[15-03-24 15:50:17 :INFO    ] - Initialising amsr dataloader
[15-03-24 15:50:17 :INFO    ] -     Files read:
[15-03-24 15:50:17 :INFO    ] -         datastore/sic/amsr2/south/asi-AMSR2-s6250-20240312-v5.4.nc
[15-03-24 15:50:17 :INFO    ] -         datastore/sic/amsr2/south/asi-AMSR2-s6250-20240313-v5.4.nc
[15-03-24 15:50:17 :INFO    ] -         datastore/sic/amsr2/south/asi-AMSR2-s6250-20240314-v5.4.nc
[15-03-24 15:50:17 :INFO    ] -     Reprojecting data from EPSG:3412 to EPSG:4326
[15-03-24 15:50:20 :INFO    ] -     Mercator data range (roughly) covers 100% of initial boundary
[15-03-24 15:50:20 :INFO    ] -     Trimming data to initial boundary: (0, -40) to (85, 50)
[15-03-24 15:50:20 :INFO    ] - Initialising vector_grf dataloader
[15-03-24 15:50:20 :INFO    ] -     Mercator data range (roughly) covers 100% of initial boundary
[15-03-24 15:50:20 :INFO    ] -     Trimming data to initial boundary: (0, -40) to (85, 50)
[15-03-24 15:50:20 :INFO    ] - Assigning data sources to cellboxes...
[15-03-24 15:50:20 :INFO    ] - Initialising neighbour graph...
[15-03-24 15:50:20 :INFO    ] - Splitting cellboxes...
 Processing amsr data: 1/3 |███▎      |  33%, [00:01 elapsed]  
Traceback (most recent call last):                       
  File "/home/jambyr/code/amop/venv/bin/create_mesh", line 33, in <module>
    sys.exit(load_entry_point('meshiphi', 'console_scripts', 'create_mesh')())
  File "/home/jambyr/code/amop/meshiphi/meshiphi/utils.py", line 231, in wrapper
    res = func(*args, **kwargs)
  File "/home/jambyr/code/amop/meshiphi/meshiphi/cli.py", line 100, in create_mesh_cli
    cg = MeshBuilder(config).build_environmental_mesh()
  File "/home/jambyr/code/amop/meshiphi/meshiphi/mesh_generation/mesh_builder.py", line 553, in build_environmental_mesh
    self.split_to_depth(self.mesh.get_max_split_depth())
  File "/home/jambyr/code/amop/meshiphi/meshiphi/mesh_generation/mesh_builder.py", line 537, in split_to_depth
    should_split = cellbox.should_split(index+1)
  File "/home/jambyr/code/amop/meshiphi/meshiphi/mesh_generation/cellbox.py", line 174, in should_split
    hom_cond = data_loader.get_hom_condition(
  File "/home/jambyr/code/amop/meshiphi/meshiphi/dataloaders/scalar/abstract_scalar.py", line 677, in get_hom_condition
    dps = self.trim_datapoints(bounds)[self.data_name]
  File "/home/jambyr/code/amop/meshiphi/meshiphi/dataloaders/scalar/abstract_scalar.py", line 324, in trim_datapoints
    if data.lat.min() >  bounds.get_lat_min() and \
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/core/_aggregations.py", line 1579, in min
    return self.reduce(
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/core/dataarray.py", line 3667, in reduce
    var = self.variable.reduce(func, dim, axis, keep_attrs, keepdims, **kwargs)
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/core/variable.py", line 1957, in reduce
    data = func(self.data, **kwargs)
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/core/duck_array_ops.py", line 378, in f
    return func(values, axis=axis, **kwargs)
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/core/nanops.py", line 66, in nanmin
    return nputils.nanmin(a, axis=axis)
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/xarray/core/nputils.py", line 157, in f
    result = getattr(npmodule, name)(values, axis=axis, **kwargs)
  File "<__array_function__ internals>", line 200, in nanmin
  File "/home/jambyr/code/amop/venv/lib/python3.8/site-packages/numpy/lib/nanfunctions.py", line 343, in nanmin
    res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
ValueError: zero-size array to reduction operation fmin which has no identity
                                    + echo 'Finished generating meshes'
Finished generating meshes          
JimCircadian commented 6 months ago

@hjabbot has validated that this will be fixed when 0.2.x is merged

EDIT: also didn't notice that my latitude sign was wrong, so understand the original reason for closure. Nice now that it'll be fixed to be tolerant rather than produce a deep message. 😉