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

GeoDataset: allow a mix of str and pathlib paths #2270

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/datasets/test_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ def test_files_property_deterministic(self) -> None:
CustomGeoDataset(paths=paths1).files == CustomGeoDataset(paths=paths2).files
)

def test_files_property_mix_str_and_pathlib(self, tmp_path: Path) -> None:
foo = tmp_path / 'foo.txt'
bar = tmp_path / 'bar.txt'
foo.touch()
bar.touch()
ds = CustomGeoDataset(paths=[str(foo), bar])
assert ds.files == [str(bar), str(foo)]


class TestRasterDataset:
naip_dir = os.path.join('tests', 'data', 'naip')
Expand Down
6 changes: 3 additions & 3 deletions torchgeo/datasets/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def res(self, new_res: float) -> None:
self._res = new_res

@property
def files(self) -> list[Path]:
def files(self) -> list[str]:
"""A list of all files in the dataset.

Returns:
Expand All @@ -306,15 +306,15 @@ def files(self) -> list[Path]:
paths = self.paths

# Using set to remove any duplicates if directories are overlapping
files: set[Path] = set()
files: set[str] = set()
for path in paths:
if os.path.isdir(path):
pathname = os.path.join(path, '**', self.filename_glob)
files |= set(glob.iglob(pathname, recursive=True))
elif (os.path.isfile(path) or path_is_vsi(path)) and fnmatch.fnmatch(
str(path), f'*{self.filename_glob}'
):
files.add(path)
files.add(str(path))
elif not hasattr(self, 'download'):
warnings.warn(
f"Could not find any relevant files for provided path '{path}'. "
Expand Down