torchgeo icon indicating copy to clipboard operation
torchgeo copied to clipboard

GeoDataset: separate file enumeration from download destination

Open adriantre opened this issue 3 months ago • 4 comments

Summary

Decouple download responsibility from GeoDataset. Today files property does three jobs:

  1. enumerate the dataset's files,
  2. decide whether the dataset is downloadable (hasattr(self, 'download')), and
  3. 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, or DatasetNotFoundError."
  • _download_root_path — single download destination, replacing the per-subclass path guards.

This is implemented here #3832

Alternatives

  • Leave files as-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_path would 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.

adriantre avatar Aug 07 '26 12:08 adriantre

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.

adamjstewart avatar Aug 10 '26 16:08 adamjstewart

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.

SreyasM24 avatar Aug 11 '26 09:08 SreyasM24

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.

It does, no? https://github.com/adriantre/torchgeo/blob/3ec2f83f1ffd7f72692275bc7640d714fbbdbd75/torchgeo/datasets/geo.py#L335

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'll have a look at this.

@SreyasM24 would definitely be great to have another pair on eyes on this!

adriantre avatar Aug 11 '26 09:08 adriantre

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.

  1. files raises DatasetNotFoundError. I found it easiest to make a private method for listing that does not raise, used by files property. https://github.com/adriantre/torchgeo/pull/18
  2. Centralise paths -> path casting for downloadable datasets by introducing single_file() helper. Instead of _download_root_path suggestion from #3832. https://github.com/adriantre/torchgeo/pull/19

With this solution, GeoDataset does not know about download functionality at all.

adriantre avatar Aug 11 '26 12:08 adriantre