Skip to content

Commit 8f43fce

Browse files
committed
make modeldescriptor reader more robust
1 parent 09c579c commit 8f43fce

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public interface ModelDescriptor {
7070
*/
7171
public String getModelID();
7272

73+
/**
74+
* @return The ID of this model.
75+
*/
76+
public String getNickname();
77+
7378
/**
7479
* @return The creation timestamp of this model.
7580
*/

src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV04.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public class ModelDescriptorV04 implements ModelDescriptor
4747
{
4848
private String format_version;
4949
private String name;
50-
private String nickname;
5150
private String timestamp;
5251
private String description;
5352
private String type;
@@ -121,10 +120,6 @@ protected static ModelDescriptorV04 buildModelDescription(Map<String, Object> ya
121120
case "description":
122121
modelDescription.description = (String) fieldElement;
123122
break;
124-
case "id":
125-
modelDescription.newModelID = findID(yamlElements);
126-
modelDescription.modelID = findOldID(yamlElements);
127-
break;
128123
case "authors":
129124
modelDescription.authors = buildAuthorElements((List<?>) fieldElement);
130125
break;
@@ -195,12 +190,11 @@ protected static ModelDescriptorV04 buildModelDescription(Map<String, Object> ya
195190
throw new ModelSpecsException("Invalid model element: " + field + "->" + e.getMessage());
196191
}
197192
}
193+
modelDescription.newModelID = findID(yamlElements);
194+
modelDescription.modelID = findOldID(yamlElements);
198195

199196
modelDescription.addSampleAndTestImages(yamlElements);
200197

201-
Object bio = modelDescription.config.getSpecMap().get("bioimageio");
202-
if ((bio != null) && (bio instanceof Map))
203-
modelDescription.nickname = (String) (((Map<String, Object>) bio).get("nickname"));
204198
modelDescription.addBioEngine();
205199
if (modelDescription.localModelPath == null)
206200
return modelDescription;
@@ -284,6 +278,13 @@ private static String findOldID(Map<String, Object> yamlElements) {
284278
Map<String, Object> configMap = (Map<String, Object>) yamlElements.get("config");
285279
if (configMap.get("_conceptdoi") != null && configMap.get("_conceptdoi") instanceof String) {
286280
return (String) configMap.get("_conceptdoi");
281+
} else if (configMap.get("_id") != null && configMap.get("_id") instanceof String) {
282+
String id = (String) configMap.get("_id");
283+
if (id.length() - id.replace("/", "").length() >= 2
284+
&& id.substring(id.indexOf("/") + 1).indexOf("/") - id.indexOf("/") > 2 )
285+
return id.substring(0, id.indexOf("/") + id.substring(id.indexOf("/") + 1).indexOf("/") + 1);
286+
else
287+
return id;
287288
}
288289
}
289290
if (yamlElements.get("id") != null && yamlElements.get("id") instanceof String) {
@@ -518,7 +519,7 @@ public String getName()
518519
*/
519520
public String getNickname()
520521
{
521-
return nickname;
522+
return this.newModelID;
522523
}
523524

524525
/**

src/main/java/io/bioimage/modelrunner/bioimageio/description/ModelDescriptorV05.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected void buildModelDescription() throws ModelSpecsException
152152
// TODO createAttachments();
153153
break;
154154
case "covers":
155-
// TODO createCovers();
155+
covers = castListStrings(yamlElements.get(field));
156156
break;
157157
case "inputs":
158158
input_tensors = buildInputTensors((List<?>) yamlElements.get(field));
@@ -182,12 +182,29 @@ protected void buildModelDescription() throws ModelSpecsException
182182
throw new ModelSpecsException("Invalid model element: " + field + "->" + e.getMessage());
183183
}
184184
}
185+
if (modelID == null) {
186+
modelID = findID(yamlElements);
187+
}
185188
addBioEngine();
186189
if (localModelPath == null)
187190
return;
188191
// TODO SpecialModels.checkSpecialModels(null);
189192
}
190193

194+
@SuppressWarnings("unchecked")
195+
private static String findID(Map<String, Object> yamlElements) {
196+
197+
if (yamlElements.get("config") != null && yamlElements.get("config") instanceof Map) {
198+
Map<String, Object> configMap = (Map<String, Object>) yamlElements.get("config");
199+
if (configMap.get("bioimageio") != null && configMap.get("bioimageio") instanceof Map) {
200+
Map<String, Object> bioimageMap = (Map<String, Object>) configMap.get("bioimageio");
201+
if (bioimageMap.get("nickname") != null)
202+
return (String) bioimageMap.get("nickname");
203+
}
204+
}
205+
return (String) yamlElements.get("id");
206+
}
207+
191208
/**
192209
* Every model in the bioimage.io can be run in the BioEngine as long as it is in the
193210
* collections repo:
@@ -380,6 +397,14 @@ public String getModelID()
380397
return modelID;
381398
}
382399

400+
/**
401+
* @return The nickname of this model, for v0.5 is the same as the id.
402+
*/
403+
public String getNickname()
404+
{
405+
return modelID;
406+
}
407+
383408
/**
384409
* @return The creation timestamp of this model.
385410
*/

0 commit comments

Comments
 (0)