Skip to content

Edge Cases when binding to nearest level #175

@FloCF

Description

@FloCF

Just two tiny super nitpicky things you might consider.

I’d slightly change this function:

def _prepare(self) -> None:
level, _ = self.wsi.get_best_level_and_custom_downsample(self.downsample, tolerance=0.1)
level_downsample = int(self.wsi.level_downsamples[level])
patch_size_level = round(self.patch_size_src / level_downsample)
overlap_level = round(self.overlap_src / level_downsample)
return level, patch_size_level, overlap_level

Into something like:

  def _prepare(self) -> None:
      level, level_downsample = self.wsi.get_best_level_and_custom_downsample(self.downsample, tolerance=0.1)
      patch_size_level = round(self.patch_size_target *  level_downsample)
      # overlap_level = round(self.overlap * level_downsample) # I guess this is never used
      return level, patch_size_level #, overlap_level

Why?
In cases where you bind to the nearest level, I think the patch size at that level should exactly match the desired output patch size.
Otherwise, the level patch size might be slightly smaller than the actual patch size, leading to minor resizing artefacts.
Situations when this is triggered are what I mean:

for level, level_downsample in enumerate(level_downsamples):
if abs(level_downsample - downsample) <= tolerance:
return level, 1.0 # Exact match, no custom downsampling needed

Speaking of binding — I’d probably only “push up” cases that are just below the upper-level downsample rate, for efficiency.
If the requested downsample is already above that level, I’d stay exact, since there’s no real speed gain from binding.

So maybe change this:

for level, level_downsample in enumerate(level_downsamples):
if abs(level_downsample - downsample) <= tolerance:
return level, 1.0 # Exact match, no custom downsampling needed

Into something like this:

# If the actual downsample is just below a specific level-downsample within tolerance, push to that level for efficiency
for level, level_downsample in enumerate(level_downsamples):
  if 0 <= (level_downsample - downsample) <= tolerance:
      return level, 1.0  # Exact match, no custom downsampling needed

Let me know if you’d like me to open a PR with these changes.
I’m currently building a preprocessing pipeline myself and have been heavily inspired by your work here!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions