Skip to content

Commit

Permalink
WIP update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Feb 26, 2024
1 parent 3a7875b commit 9fd1248
Show file tree
Hide file tree
Showing 24 changed files with 478 additions and 651 deletions.
15 changes: 9 additions & 6 deletions bioimageio/core/proc_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import InitVar, dataclass, field
from typing import (
Collection,
Generic,
Hashable,
Literal,
Optional,
Expand Down Expand Up @@ -31,10 +32,12 @@
DatasetMean,
DatasetPercentile,
DatasetStd,
MeanMeasure,
Measure,
SampleMean,
SamplePercentile,
SampleStd,
StdMeasure,
)
from bioimageio.spec.model import v0_4, v0_5

Expand Down Expand Up @@ -166,7 +169,7 @@ class ScaleLinear(_SimpleOperator):
offset: Union[float, xr.DataArray] = 0.0
"""additive term"""

def apply(self, input: Tensor, stat: Stat) -> Tensor:
def _apply(self, input: Tensor, stat: Stat) -> Tensor:
return input * self.gain + self.offset

# @classmethod
Expand Down Expand Up @@ -315,8 +318,8 @@ def from_proc_descr(cls, descr: Union[v0_4.ScaleRangeDescr, v0_5.ScaleRangeDescr
return cls(
input=tensor_id,
output=tensor_id,
lower_percentile=Percentile(kwargs.min_percentile, axes=axes, tensor_id=ref_tensor),
upper_percentile=Percentile(kwargs.max_percentile, axes=axes, tensor_id=ref_tensor),
lower_percentile=Percentile(n=kwargs.min_percentile, axes=axes, tensor_id=ref_tensor),
upper_percentile=Percentile(n=kwargs.max_percentile, axes=axes, tensor_id=ref_tensor),
)

def _apply(self, input: xr.DataArray, stat: Stat) -> xr.DataArray:
Expand Down Expand Up @@ -363,16 +366,16 @@ def get_descr(self):
class ZeroMeanUnitVariance(_SimpleOperator):
"""normalize to zero mean, unit variance."""

mean: Union[SampleMean, DatasetMean]
std: Union[SampleStd, DatasetStd]
mean: MeanMeasure
std: StdMeasure

eps: float = 1e-6

def __post_init__(self):
assert self.mean.axes == self.std.axes

@property
def required_measures(self) -> Collection[Measure]:
def required_measures(self) -> Set[Union[MeanMeasure, StdMeasure]]:
return {self.mean, self.std}

@classmethod
Expand Down
34 changes: 33 additions & 1 deletion bioimageio/core/stat_calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def get_measure_calculators(


def compute_dataset_measures(
*, measures: Iterable[DatasetMeasure], dataset: Iterable[Sample]
measures: Iterable[DatasetMeasure], dataset: Iterable[Sample]
) -> Dict[DatasetMeasure, MeasureValue]:
"""compute all dataset `measures` for the given `dataset`"""
sample_calculators, calculators = get_measure_calculators(measures)
Expand All @@ -453,3 +453,35 @@ def compute_dataset_measures(
ret.update(calc.finalize().items())

return ret

def compute_sample_measures(measures: Iterable[SampleMeasure], sample: Sample) -> Dict[SampleMeasure, MeasureValue]:
"""compute all sample `measures` for the given `sample`"""
calculators, dataset_calculators = get_measure_calculators(measures)
assert not dataset_calculators
ret: Dict[SampleMeasure, MeasureValue] = {}

for calc in calculators:
ret.update(calc.compute(sample).items())

return ret


def compute_measures(measures: Iterable[Measure], dataset: Iterable[Sample]) -> Dict[Measure, MeasureValue]:
"""compute all `measures` for the given `dataset`
sample measures are computed for the last sample in `dataset`"""
sample_calculators, dataset_calculators = get_measure_calculators(measures)
ret: Dict[Measure, MeasureValue] = {}
sample = None
for sample in dataset:
for calc in dataset_calculators:
calc.update(sample)
if sample is None:
raise ValueError("empty dataset")

for calc in dataset_calculators:
ret.update(calc.finalize().items())

for calc in sample_calculators:
ret.update(calc.compute(sample).items())

return ret
11 changes: 10 additions & 1 deletion bioimageio/core/stat_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Optional, Tuple, Union
from typing import Optional, Tuple, TypeVar, Union

import xarray as xr

Expand Down Expand Up @@ -119,3 +119,12 @@ def __post_init__(self):
SampleMeasure = Union[SampleMean, SampleStd, SampleVar, SamplePercentile]
DatasetMeasure = Union[DatasetMean, DatasetStd, DatasetVar, DatasetPercentile]
Measure = Union[SampleMeasure, DatasetMeasure]

MeanMeasure = Union[SampleMean, DatasetMean]
StdMeasure = Union[SampleStd, DatasetStd]
VarMeasure = Union[SampleVar, DatasetVar]
PercentileMeasure = Union[SamplePercentile, DatasetPercentile]
MeanMeasureT = TypeVar("MeanMeasureT", bound=MeanMeasure)
StdMeasureT = TypeVar("StdMeasureT", bound=StdMeasure)
VarMeasureT = TypeVar("VarMeasureT", bound=VarMeasure)
PercentileMeasureT = TypeVar("PercentileMeasureT", bound=PercentileMeasure)
3 changes: 0 additions & 3 deletions tests/build_spec/test_add_weights.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import os

from bioimageio.core import export_resource_package, load_description, load_raw_resource_description
from bioimageio.core.resource_tests import test_model as _test_model


def _test_add_weights(model, tmp_path, base_weights, added_weights, **kwargs):
from bioimageio.core.build_spec import add_weights
Expand Down
11 changes: 5 additions & 6 deletions tests/build_spec/test_build_spec.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Optional

from marshmallow import missing

import bioimageio.spec as spec
from bioimageio.core import load_description, load_raw_resource_description
from bioimageio.core._internal.validation_visitors import resolve_source
from bioimageio.core.resource_io import nodes
from bioimageio.core.resource_tests import test_model as _test_model

# from bioimageio.core import load_description, load_raw_resource_description
# from bioimageio.core._internal.validation_visitors import resolve_source
# from bioimageio.core.resource_io import nodes
# from bioimageio.core.resource_tests import test_model as _test_model

try:
import tensorflow
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@


@fixture(scope="session")
def model_packages():
def model_packages() -> MappingProxyType[str, FilePath]:
return MappingProxyType({name: save_bioimageio_package(MODEL_SOURCES[name]) for name in load_model_packages})


Expand Down
13 changes: 7 additions & 6 deletions tests/prediction_pipeline/test_combined_processing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import numpy as np
import xarray as xr

from bioimageio.core.resource_io import nodes

def test_postprocessing_dtype(): # TODO: remove?
from bioimageio.core.common import TensorId
from bioimageio.spec.model.v0_5 import BinarizeDescr, BinarizeKwargs, OutputTensorDescr

def test_postprocessing_dtype():
from bioimageio.core.prediction_pipeline._combined_processing import CombinedProcessing
# from bioimageio.core.prediction_pipeline._combined_processing import CombinedProcessing

shape = [3, 32, 32]
axes = ("c", "y", "x")
Expand All @@ -17,12 +18,12 @@ def test_postprocessing_dtype():

for dtype in ("float32", "float64", "uint8", "uint16"):
outputs = [
nodes.OutputTensor(
"out1",
OutputTensorDescr(
id=TensorId("out1"),
data_type=dtype,
axes=axes,
shape=shape,
postprocessing=[nodes.Postprocessing("binarize", dict(threshold=threshold))],
postprocessing=[BinarizeDescr(kwargs=BinarizeKwargs(threshold=threshold))],
)
]
com_proc = CombinedProcessing.from_tensor_specs(outputs)
Expand Down
28 changes: 18 additions & 10 deletions tests/prediction_pipeline/test_device_management.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from pathlib import Path

import numpy as np
import pytest
import xarray as xr
from numpy.testing import assert_array_almost_equal

from bioimageio.core import load_description
from bioimageio.core._internal.pytest_utils import skip_on
from bioimageio.core.resource_io.nodes import Model
from bioimageio.core.utils.testing import skip_on
from bioimageio.spec import load_description
from bioimageio.spec.model.v0_4 import ModelDescr as ModelDescr04
from bioimageio.spec.model.v0_5 import ModelDescr, WeightsFormat
from bioimageio.spec.utils import load_array


class TooFewDevicesException(Exception):
pass


def _test_device_management(model_package, weight_format):
def _test_device_management(model_package: Path, weight_format: WeightsFormat):
import torch

if torch.cuda.device_count() == 0:
Expand All @@ -21,13 +24,18 @@ def _test_device_management(model_package, weight_format):
from bioimageio.core.prediction_pipeline import create_prediction_pipeline

bio_model = load_description(model_package)
assert isinstance(bio_model, Model)
assert isinstance(bio_model, (ModelDescr, ModelDescr04))
pred_pipe = create_prediction_pipeline(bioimageio_model=bio_model, weight_format=weight_format, devices=["cuda:0"])

inputs = [
xr.DataArray(np.load(str(test_tensor)), dims=tuple(spec.axes))
for test_tensor, spec in zip(bio_model.test_inputs, bio_model.inputs)
]
if isinstance(bio_model, ModelDescr04):
inputs = [
xr.DataArray(np.load(str(test_tensor)), dims=tuple(spec.axes))
for test_tensor, spec in zip(bio_model.test_inputs, bio_model.inputs)
]
else:
inputs = [
xr.DataArray(load_array(ipt.test_tensor), dims=tuple(a.id for a in ipt.axes)) for ipt in bio_model.inputs
]
with pred_pipe as pp:
outputs = pp.forward(*inputs)

Expand Down
103 changes: 0 additions & 103 deletions tests/prediction_pipeline/test_measures.py

This file was deleted.

Loading

0 comments on commit 9fd1248

Please sign in to comment.