ome / openmicroscopy

OME (Open Microscopy Environment) develops open-source software and data format standards for the storage and manipulation of biological light microscopy data. A joint project between universities, research establishments and industry in Europe and the USA, OME has over 20 active researchers with strong links to the microscopy community. Funded by private and public research grants, OME has been a major force on the international microscopy stage since 2000.
https://www.openmicroscopy.org/omero
GNU General Public License v2.0
200 stars 102 forks source link

Histogram tests: fix tests and cover more pixel types #6350

Closed sbesson closed 1 year ago

sbesson commented 1 year ago

Companion PR to https://github.com/ome/omero-server/pull/164 which fixes the bin assignment in the getHistorgram API. The bug fix exposes the fact that the underlying integration tests were incorrect

This PR fixes the assertions for the existing tests (uint8 and uint16) and expands the coverage to also test signed pixel types as well as 32 and 64 bit pixel types.

As ground truth, the Python snippet below returns the expected non-zero bin counts for all scenarios

import numpy

def get_histogram(a, c):
    data = numpy.histogram(
        a[:,:,c],
        bins=256,
        range=[a[:,:,c].min(), a[:,:,c].max()])[0]
    print(f"Channel: {c}")
    for i in numpy.nditer(numpy.nonzero(data)):
        print(f" {i}: {data[i]}")

print("Pixel type: uint8")
a = numpy.zeros((10, 10, 1), dtype=numpy.uint8)
a[0, :, 0] = 63
a[1, :, 0] = 127
get_histogram(a, 0)

print("Pixel type: int8")
a = numpy.zeros((10, 10, 1), dtype=numpy.int8)
a[0, :, 0] = -128
a[1, :, 0] = 63
get_histogram(a, 0)

print("Pixel type: uint16")
a = numpy.zeros((10, 10, 2), dtype=numpy.uint16)
a[0, :, 0] = 12800
a[1, :, 0] = 25600
a[0, :, 1] = 25600
a[1, :, 1] = 51200
get_histogram(a, 0)
get_histogram(a, 1)

print("Pixel type: int16")
a = numpy.zeros((10, 10, 2), dtype=numpy.int16)
a[0, :, 0] = -12800
a[1, :, 0] = 25600
a[0, :, 1] = -6400
a[1, :, 1] = 12800
get_histogram(a, 0)
get_histogram(a, 1)

print("Pixel type: float")
a = numpy.zeros((10, 10, 2), dtype=numpy.float32)
a[0, :, 0] = -.1
a[1, :, 0] = .1
a[0, :, 1] = -100.0
a[1, :, 1] = 100.0
get_histogram(a, 0)
get_histogram(a, 1)

print("Pixel type: double")
a = numpy.zeros((10, 10, 2), dtype=numpy.float64)
a[0, :, 0] = -.1
a[1, :, 0] = .1
a[0, :, 1] = -100.0
a[1, :, 1] = 100.0
get_histogram(a, 0)
get_histogram(a, 1)
sbesson commented 1 year ago

This PR is now in a fairly good state, should cover a wider range of pixel types and can be used to review the server histogram fixes proposed in https://github.com/ome/omero-server/pull/164.

jburel commented 1 year ago

Tests are green. Corresponding PR has now been merged