Skip to content

Commit

Permalink
make modeldescriptor reader more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Oct 9, 2024
1 parent 09c579c commit 8f43fce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,10 +120,6 @@ protected static ModelDescriptorV04 buildModelDescription(Map<String, Object> 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;
Expand Down Expand Up @@ -195,12 +190,11 @@ protected static ModelDescriptorV04 buildModelDescription(Map<String, Object> 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<String, Object>) bio).get("nickname"));
modelDescription.addBioEngine();
if (modelDescription.localModelPath == null)
return modelDescription;
Expand Down Expand Up @@ -284,6 +278,13 @@ private static String findOldID(Map<String, Object> yamlElements) {
Map<String, Object> configMap = (Map<String, Object>) 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) {
Expand Down Expand Up @@ -518,7 +519,7 @@ public String getName()
*/
public String getNickname()
{
return nickname;
return this.newModelID;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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<String, Object> yamlElements) {

if (yamlElements.get("config") != null && yamlElements.get("config") instanceof Map) {
Map<String, Object> configMap = (Map<String, Object>) yamlElements.get("config");
if (configMap.get("bioimageio") != null && configMap.get("bioimageio") instanceof Map) {
Map<String, Object> bioimageMap = (Map<String, Object>) 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:
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 8f43fce

Please sign in to comment.