From 8f43fcebd05522c53c23be023d63a0b24db9e673 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Wed, 9 Oct 2024 12:34:15 +0200 Subject: [PATCH] make modeldescriptor reader more robust --- .../description/ModelDescriptor.java | 5 ++++ .../description/ModelDescriptorV04.java | 19 ++++++------- .../description/ModelDescriptorV05.java | 27 ++++++++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptor.java b/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptor.java index 80dd4dd0..3aa0d4ef 100644 --- a/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptor.java +++ b/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptor.java @@ -70,6 +70,11 @@ public interface ModelDescriptor { */ public String getModelID(); + /** + * @return The ID of this model. + */ + public String getNickname(); + /** * @return The creation timestamp of this model. */ diff --git a/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV04.java b/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV04.java index 8a9ee23a..d0e88927 100644 --- a/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV04.java +++ b/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV04.java @@ -47,7 +47,6 @@ public class ModelDescriptorV04 implements ModelDescriptor { private String format_version; private String name; - private String nickname; private String timestamp; private String description; private String type; @@ -121,10 +120,6 @@ protected static ModelDescriptorV04 buildModelDescription(Map ya case "description": modelDescription.description = (String) fieldElement; break; - case "id": - modelDescription.newModelID = findID(yamlElements); - modelDescription.modelID = findOldID(yamlElements); - break; case "authors": modelDescription.authors = buildAuthorElements((List) fieldElement); break; @@ -195,12 +190,11 @@ protected static ModelDescriptorV04 buildModelDescription(Map ya throw new ModelSpecsException("Invalid model element: " + field + "->" + e.getMessage()); } } + modelDescription.newModelID = findID(yamlElements); + modelDescription.modelID = findOldID(yamlElements); modelDescription.addSampleAndTestImages(yamlElements); - Object bio = modelDescription.config.getSpecMap().get("bioimageio"); - if ((bio != null) && (bio instanceof Map)) - modelDescription.nickname = (String) (((Map) bio).get("nickname")); modelDescription.addBioEngine(); if (modelDescription.localModelPath == null) return modelDescription; @@ -284,6 +278,13 @@ private static String findOldID(Map yamlElements) { Map configMap = (Map) yamlElements.get("config"); if (configMap.get("_conceptdoi") != null && configMap.get("_conceptdoi") instanceof String) { return (String) configMap.get("_conceptdoi"); + } else if (configMap.get("_id") != null && configMap.get("_id") instanceof String) { + String id = (String) configMap.get("_id"); + if (id.length() - id.replace("/", "").length() >= 2 + && id.substring(id.indexOf("/") + 1).indexOf("/") - id.indexOf("/") > 2 ) + return id.substring(0, id.indexOf("/") + id.substring(id.indexOf("/") + 1).indexOf("/") + 1); + else + return id; } } if (yamlElements.get("id") != null && yamlElements.get("id") instanceof String) { @@ -518,7 +519,7 @@ public String getName() */ public String getNickname() { - return nickname; + return this.newModelID; } /** diff --git a/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV05.java b/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV05.java index 0d0cf70a..51cc9c30 100644 --- a/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV05.java +++ b/src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV05.java @@ -152,7 +152,7 @@ protected void buildModelDescription() throws ModelSpecsException // TODO createAttachments(); break; case "covers": - // TODO createCovers(); + covers = castListStrings(yamlElements.get(field)); break; case "inputs": input_tensors = buildInputTensors((List) yamlElements.get(field)); @@ -182,12 +182,29 @@ protected void buildModelDescription() throws ModelSpecsException throw new ModelSpecsException("Invalid model element: " + field + "->" + e.getMessage()); } } + if (modelID == null) { + modelID = findID(yamlElements); + } addBioEngine(); if (localModelPath == null) return; // TODO SpecialModels.checkSpecialModels(null); } + @SuppressWarnings("unchecked") + private static String findID(Map yamlElements) { + + if (yamlElements.get("config") != null && yamlElements.get("config") instanceof Map) { + Map configMap = (Map) yamlElements.get("config"); + if (configMap.get("bioimageio") != null && configMap.get("bioimageio") instanceof Map) { + Map bioimageMap = (Map) configMap.get("bioimageio"); + if (bioimageMap.get("nickname") != null) + return (String) bioimageMap.get("nickname"); + } + } + return (String) yamlElements.get("id"); + } + /** * Every model in the bioimage.io can be run in the BioEngine as long as it is in the * collections repo: @@ -380,6 +397,14 @@ public String getModelID() return modelID; } + /** + * @return The nickname of this model, for v0.5 is the same as the id. + */ + public String getNickname() + { + return modelID; + } + /** * @return The creation timestamp of this model. */