Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CAREamics CI Script #114

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions scripts/check_compatibility_careamics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import argparse
from pathlib import Path

import numpy as np
import pydantic
from careamics import __version__ as CAREAMICS_VERSION
from careamics import CAREamist
from careamics.model_io.bmz_io import load_from_bmz
from bioimageio.spec import load_model_description
from bioimageio.spec.generic.v0_2 import AttachmentsDescr

from .script_utils import CompatibilityReportDict, check_tool_compatibility


def check_compatibility_careamics_impl(
rdf_url: str,
sha256: str,
) -> CompatibilityReportDict:
"""Create a `CompatibilityReport` for a resource description.

Args:
rdf_url: URL to the rdf.yaml file
sha256: SHA-256 value of **rdf_url** content
"""
model_desc = load_model_description(rdf_url)
if isinstance(model_desc.attachments, AttachmentsDescr):
attachment_file_names = [
Path(file.path).name
for file in model_desc.attachments.files
if file.path is not None
]
elif isinstance(model_desc.attachments, list):
attachment_file_names = [
Path(attachment.source.path).name
for attachment in model_desc.attachments
if attachment.source.path is not None
]
else:
# TODO: confirm all types of attachments (type checker still complaining)
melisande-c marked this conversation as resolved.
Show resolved Hide resolved
report = CompatibilityReportDict(
status="failed",
error=None,
details="Could not process attachments.",
)
return report

# check type is tagged as CAREamics
if ("CAREamics" not in model_desc.tags) and ("careamics" not in model_desc.tags):
report = CompatibilityReportDict(
status="not-applicable",
error=None,
details="'Model' resource not tagged with 'CAREamics' or 'careamics'.",
)
# check config file is present in attachments
# TODO: update to careamics.yaml once files have been updated
elif not "config.yml" in attachment_file_names:
report = CompatibilityReportDict(
status="failed",
error=None,
details="CAREamics config file is not present in attachments.",
)
# download and test
else:
try:
model, config = load_from_bmz(rdf_url)
except (ValueError, pydantic.ValidationError) as e:
report = CompatibilityReportDict(
status="failed",
error="Error: {}".format(e),
details=("Could not load CAREamics configuration or model."),
)
return report

# no failure mode as config is already a Configuration object
careamist = CAREamist(config)
# TODO (CAREamics): make a model loading method, why doesn't this exist
careamist.model = model

# get input tensor
# TODO: type checker complaining because of difference between v0.4 and v0.5
# test_tensor attribute does not exist for v0.4,
# how can the tensor path be accessed?
input_path = model_desc.inputs[0].test_tensor.download().path
input_array = np.load(input_path)
melisande-c marked this conversation as resolved.
Show resolved Hide resolved

try:
_ = careamist.predict(
source=input_array,
data_type="array",
axes="SCZYX" if "Z" in config.data_config.axes else "SCYX",
)
except Exception as e:
report = CompatibilityReportDict(
status="failed",
error="Error: {}".format(e),
details="Calling prediction failed.",
)
return report

report = CompatibilityReportDict(
status="passed",
error=None,
details="CAREamics compatibility checks completed successfully!",
)

return report


def check_compatibility_careamics(all_version_path: Path, output_folder: Path) -> None:
"""CAREamics compatibility check."""
check_tool_compatibility(
"CAREamics",
CAREAMICS_VERSION,
all_version_path=all_version_path,
output_folder=output_folder,
check_tool_compatibility_impl=check_compatibility_careamics_impl,
applicable_types={"model"},
)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
_ = parser.add_argument("all_versions", type=Path)
_ = parser.add_argument("output_folder", type=Path)

args = parser.parse_args()
check_compatibility_careamics(args.all_versions, args.output_folder)