Skip to content

Improve handling of pixel size#17

Merged
alanocallaghan merged 7 commits into
mainfrom
clarify-microns
May 8, 2026
Merged

Improve handling of pixel size#17
alanocallaghan merged 7 commits into
mainfrom
clarify-microns

Conversation

@alanocallaghan

@alanocallaghan alanocallaghan commented Apr 29, 2026

Copy link
Copy Markdown
Contributor
  • Allow setting pixel size when the input model specifies pixel size
  • Otherwise, allow the user to choose a downsample level

In future, could allow specifying a pixel size and downsample level, although that may be redundant.

@petebankhead

Copy link
Copy Markdown
Member

Is it in microns?

In PixelCalibration we have both getPixelWidth() and getPixelWidthUnit(), and then separately getPixelWidthMicrons() to handle our most common case where the unit is µm.

I haven't checked the possible code paths, but we would ideally deal with the scenarios where:

  • The pixel size is unknown, but we still want to encode that the model is to be run at some kind of downsample
  • The pixel size is in some other units (e.g., metres, because we're using QuPath to analyse landscapes)

Is this PR compatible with those?

@alanocallaghan

alanocallaghan commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

I operated under the assumption that it's in microns. Is there any existing code for translating SI units? edit: or indeed feet, inches, miles, parsecs or yards...

private void configurePixelSize(Model model) {
var axes = model.getInputs().getFirst().getAxes();
String axString = Axes.getAxesString(axes);
int xind = axString.indexOf("x");
int yind = axString.indexOf("y");
double xsize=0.25, ysize=0.25;
if (xind != -1 && yind != -1) {
if (axes[xind] instanceof SpaceAxes.SpaceAxis spaceAxis) {
if (spaceAxis.getUnit() != SpaceAxes.SpaceUnit.MICROMETER) {
logger.warn("Unknown space unit {}", spaceAxis.getUnit());
}
xsize = spaceAxis.getScale();
}
if (axes[yind] instanceof SpaceAxes.SpaceAxis spaceAxis) {
if (spaceAxis.getUnit() != SpaceAxes.SpaceUnit.MICROMETER) {
logger.warn("Unknown space unit {}", spaceAxis.getUnit());
}
ysize = spaceAxis.getScale();
}
}

@alanocallaghan

alanocallaghan commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

The pixel size is unknown, but we still want to encode that the model is to be run at some kind of downsample

I don't know how to reconcile this with the only downsample specification present in the bioimageio spec (and our pixel classifier spec?) being pixel size.

@petebankhead

Copy link
Copy Markdown
Member

I've tried to find and navigate the bioimage.io model spec, but became too confused before I could decipher the part on pixel sizes. But I think it's possible to include specific units, or none at all?

If the spec gives a pixel size of 2, but without units, then I'd expect that either:

  • the downsample is 2
  • the image is resized so that the pixel size of 2 of whatever units were used (possibly µm)

The first seems more intuitive to me, whereas the second seems (just about) defensible if it's substantially easier to implement with the current design, and we document the behavior somewhere.

In the case where the image doesn't contain a pixel size, then it's taken to be 1 pixel and so both options would end up using a downsample of 2 anyway.

It's annoyingly vague, but I think preferable to 'the pixel size can only be specified in µm, nothing else' (which is what I think this PR is heading towards?).

@alanocallaghan

Copy link
Copy Markdown
Contributor Author

I don't think "we're using qupath to analyze landscapes" is an expected use case for bioimageio models

PixelCalibration only supports microns and no units currently?

https://github.com/qupath/qupath/blob/31312a5708d1c396cba0a12db306358524c1e8c0/qupath-core/src/main/java/qupath/lib/images/servers/PixelCalibration.java#L32-L35

I can change the extension to support no units, but given that the only two ways to specify resolution are with just a pixelCalibration or with a pixelCalibration and a downsample, I'm not really satisfied with how you would handle that beyond the current approach of "everything is based on the currently open image" which seems borderline nonsensical to me

@alanocallaghan

alanocallaghan commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

I think it's possible to include specific units, or none at all?

Unit: https://bioimage-io.github.io/spec-bioimage-io/bioimageio_schema_latest/#oneOf_i2_oneOf_i1_inputs_items_axes_items_oneOf_i4_unit
and scale:
https://bioimage-io.github.io/spec-bioimage-io/bioimageio_schema_latest/#oneOf_i2_oneOf_i1_inputs_items_axes_items_oneOf_i4_scale

Both of which are optional.

A path forward would be to have an X and Y spinner using microns if the model's pixel size unit is microns, and a single spinner to specify a downsample otherwise. I don't want to bother converting between other units, if I'm being perfectly honest.

@petebankhead

Copy link
Copy Markdown
Member

Sounds fine, as long as the spinners are editable so that it's possible to enter anything. There's a chance qupath-fxtras might help if null causes trouble.

I also wouldn't want to get into converting units at this point; very much a problem for the future (after we've explored JSR 385 and how Bio-Formats does things).

@alanocallaghan

Copy link
Copy Markdown
Contributor Author

Right, well that should be implemented now. We don't support different pixel sizes in X and Y at the moment but I can revisit that in a future PR if we think it's something people will actually use (doubt).

I've not tested the output yet, I'll probably get around to it on Friday.

@alanocallaghan

alanocallaghan commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

I did note a separate issue with our spec: qupath/qupath-bioimageio-spec#25
in that we don't really handle optional (or required) fields very well

@alanocallaghan alanocallaghan changed the title Clarify that pixel size is microns Improve handling of pixel size Apr 29, 2026
@alanocallaghan

Copy link
Copy Markdown
Contributor Author

Tested this with different pixel size and downsample settings, seems to work well

@alanocallaghan alanocallaghan merged commit 918415e into main May 8, 2026
1 check failed
@alanocallaghan alanocallaghan deleted the clarify-microns branch May 8, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants