Skip to content

Commit

Permalink
lazy import for xarray
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodrut committed Jan 9, 2025
1 parent 871d080 commit 5d7b27a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions requirements/datasets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pycocotools==2.0.8
pyvista==0.44.2
scikit-image==0.25.0
scipy==1.14.1
xarray==2024.11.0
netcdf4==1.7.2
3 changes: 3 additions & 0 deletions tests/datasets/test_glacier_mapping_alps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

from torchgeo.datasets import DatasetNotFoundError, GlacierMappingAlps

pytest.importorskip('xarray', minversion='2024.11.0')
pytest.importorskip('netCDF4', minversion='1.7.2')


class TestGlacierMappingAlps:
@pytest.fixture(
Expand Down
9 changes: 7 additions & 2 deletions torchgeo/datasets/glacier_mapping_alps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import numpy as np
import pandas as pd
import torch
import xarray as xr
from matplotlib.figure import Figure
from torch import Tensor

from .errors import DatasetNotFoundError
from .geo import NonGeoDataset
from .utils import download_and_extract_archive, download_url, extract_archive
from .utils import (
download_and_extract_archive,
download_url,
extract_archive,
lazy_import,
)


class GlacierMappingAlps(NonGeoDataset):
Expand Down Expand Up @@ -220,6 +224,7 @@ def __getitem__(self, index: int) -> dict[str, Tensor]:
* the cloud and shadow mask
* the additional features (DEM, derived features, etc.) if required
"""
xr = lazy_import('xarray')
nc = xr.open_dataset(
self.fp_patches[index], decode_coords='all', mask_and_scale=True
)
Expand Down

0 comments on commit 5d7b27a

Please sign in to comment.