GeoDataset: separate file enumeration from download destination
Summary
Decouple download responsibility from GeoDataset. Today files property does three jobs:
- enumerate the dataset's
files, - decide whether the dataset is downloadable (
hasattr(self, 'download')), and - act as an existence probe (
if self.files:).
Rationale
Because responsibility 3 uses files as a truthiness probe, it can't raise DatasetNotFoundError on "nothing found". So the not-found signal is pushed into every subclass, along with a repeated assert isinstance(self.paths, ...) + cast guard (35 occurrences across ~18 datasets). Removing that coupling is the goal, especially since the download itself only lives in the subclasses already (_verify / _download).
Implementation
Give each concern one home:
find_files(...)— raw "what's on disk" probe, used in downloadable datasets'_verify.files— "the dataset's files, orDatasetNotFoundError."_download_root_path— single download destination, replacing the per-subclass path guards.
This is implemented here #3832
Alternatives
- Leave
filesas-is (returns [], no raise) and keep signalling "nothing found" from each subclass - Go further: extract download into a mixin/helper so GeoDataset knows nothing about downloading. The suggested
_download_root_pathwould be a waypoint toward this.
Additional information
paths has two responsibilities, when we include the aspect of download. It has type Path | Iterable[Path] since it is mostly about reading, but a downloadable dataset requires exactly one destination directory. _download_root_path now does what each dataset subclass already did, picking the first path and warning.
Resolving it properly means deciding how a single download destination is derived. That's its own design question and belongs in a separate issue. Depending on the solution, this property might disappear, but for now it collects the scattered implementation.
Okay, I agree with parts of this in theory. files should list files, and possibly be cached if we plan to use it a lot. To be fair, files doesn't download anything, it just looks for a reason why no files were found. Your PR doesn't currently raise DatasetNotFound, is that a future plan? I like that plan.
However, _download_root_path feels unrelated to this. Can that be a separate PR? I don't love more magic private attributes, but also don't like the current code duplication. Originally we only used assert isinstance(paths, str) but when we switched from mypy to ty we required an additional cast. That may have been fixed in newer versions of ty, worth checking if we can simply delete all casts.
I've been following this discussion and would be interested in contributing to the follow-up work. I'll take a closer look at the files/find_files separation and the remaining path-handling changes to see where I can help.
Okay, I agree with parts of this in theory.
filesshould list files, and possibly be cached if we plan to use it a lot. To be fair,filesdoesn't download anything, it just looks for a reason why no files were found. Your PR doesn't currently raise DatasetNotFound, is that a future plan? I like that plan.
It does, no? https://github.com/adriantre/torchgeo/blob/3ec2f83f1ffd7f72692275bc7640d714fbbdbd75/torchgeo/datasets/geo.py#L335
However,
_download_root_pathfeels unrelated to this. Can that be a separate PR? I don't love more magic private attributes, but also don't like the current code duplication. Originally we only usedassert isinstance(paths, str)but when we switched from mypy to ty we required an additional cast. That may have been fixed in newer versions of ty, worth checking if we can simply delete all casts.
I'll have a look at this.
@SreyasM24 would definitely be great to have another pair on eyes on this!
So ty still requires the cast, unfortunately.
I have now split it up into two independent PRs in my fork. Please have a look and let me know whether it looks better.
filesraisesDatasetNotFoundError. I found it easiest to make a private method for listing that does not raise, used byfilesproperty. https://github.com/adriantre/torchgeo/pull/18- Centralise paths -> path casting for downloadable datasets by introducing
single_file()helper. Instead of_download_root_pathsuggestion from #3832. https://github.com/adriantre/torchgeo/pull/19
With this solution, GeoDataset does not know about download functionality at all.