Skip to content

Commit

Permalink
create test artifact object
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Jul 11, 2023
1 parent 3a66dec commit 447a7e9
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public class ModelDescriptor
private List<String> covers;
private List<SampleImage> sample_inputs;
private List<SampleImage> sample_outputs;
private List<String> test_inputs;
private List<String> test_outputs;
private List<TestArtifact> test_inputs;
private List<TestArtifact> test_outputs;
private List<TensorSpec> input_tensors;
private List<TensorSpec> output_tensors;
private ExecutionConfig config;
Expand Down Expand Up @@ -234,10 +234,10 @@ private static ModelDescriptor buildModelDescription(Map<String, Object> yamlEle
modelDescription.documentation = (String) fieldElement;
break;
case "test_inputs":
modelDescription.test_inputs = buildUrlElements((List<?>) fieldElement);
modelDescription.test_inputs = buildTestArtifacts((List<?>) fieldElement);
break;
case "test_outputs":
modelDescription.test_outputs = buildUrlElements((List<?>) fieldElement);
modelDescription.test_outputs = buildTestArtifacts((List<?>) fieldElement);
break;
case "sample_inputs":
modelDescription.sample_inputs = buildSampleImages((List<?>) fieldElement);
Expand Down Expand Up @@ -479,6 +479,30 @@ private static List<SampleImage> buildSampleImages(Object coverElements)
return covers.stream().filter(i -> i != null).collect(Collectors.toList());
}

/**
* REturns a List<TestArtifact> of the npy artifacts that are packed in the model
* folder as input and output test objects
* @param coverElements
* data from the yaml
* @return the List<TestArtifact> with the sample images data
*/
private static List<TestArtifact> buildTestArtifacts(Object coverElements)
{
List<TestArtifact> covers = new ArrayList<>();
if ((coverElements instanceof List<?>)) {
List<?> elems = (List<?>) coverElements;
for (Object elem : elems)
{
if (!(elem instanceof String))
continue;
covers.add(TestArtifact.build((String) elem));
}
} else if ((coverElements instanceof String)) {
covers.add(TestArtifact.build((String) coverElements));
}
return covers.stream().filter(i -> i != null).collect(Collectors.toList());
}

private static List<Badge> buildBadgeElements(List<?> coverElements) throws Exception
{
if (!(coverElements instanceof List<?>))
Expand Down Expand Up @@ -948,16 +972,18 @@ public List<SampleImage> getSampleOutputs() {
/**
* @return the test_inputs
*/
public List<String> getTestInputs() {
public List<TestArtifact> getTestInputs() {
if (test_inputs == null)
test_inputs = new ArrayList<String>();
test_inputs = new ArrayList<TestArtifact>();
return test_inputs;
}

/**
* @return the test_outputs
*/
public List<String> getTestOutputs() {
public List<TestArtifact> getTestOutputs() {
if (test_outputs == null)
test_outputs = new ArrayList<TestArtifact>();
return test_outputs;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*-
* #%L
* Use deep learning frameworks from Java in an agnostic and isolated way.
* %%
* Copyright (C) 2022 - 2023 Institut Pasteur and BioImage.IO developers.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.bioimage.modelrunner.bioimageio.description;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
* Define each of the test inputs and outputs defined in the rdf.yaml file
* @author Carlos Garcia Lopez de Haro
*
*/
public class TestArtifact {
/**
* String to the sample image
*/
private String string;
/**
* URL for the sample image
*/
private URL url;
/**
* Path to the sample image in the local machine
*/
private Path path;
/**
* List of allowed extensions for the sample images
*/
private static List<String> allowedExtensions = Arrays.asList(new String[] {".npy"});

/**
* Creates a {@link TestArtifact} instance.
*
* @param sampleInputUrl
* String specified in the yaml file as a sample input or output
* @return The creates instance.
*/
public static TestArtifact build(String sampleInputUrl)
{
TestArtifact sampleInput = new TestArtifact();
if (sampleInputUrl == null)
return null;
sampleInput.string = sampleInputUrl;
if (!sampleInput.isExtensionAllowed())
return null;
sampleInput.createSampleInputURL();
sampleInput.createSampleInputPath();
sampleInput.createSampleInputPath();
return sampleInput;
}

/**
* Check if the extensio of the sample image is allowed or not
* @return true if the sample image contains a valid extension, false otherwise
*/
public boolean isExtensionAllowed() {
if (getFileExtension() == null)
return false;
if (!allowedExtensions.contains(this.getFileExtension().toLowerCase()))
return false;
return true;
}

/**
* Get the extension of the file containing the sample image
* @return the extension of teh sample image file
*/
public String getFileExtension() {
if (string == null)
return null;
if (string.lastIndexOf(".") == -1)
return null;
return string.substring(string.lastIndexOf("."));
}

/**
* Try to create an URL for the sample input if it is specified as an URL
*/
private void createSampleInputURL() {
try {
url = new URL(string);
} catch (MalformedURLException e) {
}
}

/**
* Create the Path to a sample input if it is defined in the yaml file as a local file
*/
private void createSampleInputPath() {
try {
path = Paths.get(string);
if (!path.toFile().exists())
path = null;
} catch (Exception ex) {
}
}

/**
* Add the local model path to the sample images to know where these images are
* @param p
* the path to the local model folder
*/
public void addLocalModelPath(Path p) {
if (!p.toFile().exists())
return;
String name = new File(string).getName();
Path nPath = p.resolve(name);
if (nPath.toFile().exists())
path = nPath;
}

/**
* Return the String URL of the sample image
* @return the string URL of the sample image
*/
public String getString() {
return string;
}

/**
* Return the url of the sample image
* @return the url corresponding where the sample image is in the cloud
*/
public URL getUrl() {
return url;
}

/**
* Return the local path to the sample image
* @return the local path to the sample image
*/
public Path getPath() {
return path;
}

@Override
public String toString()
{
String str = "SampleInput {";
str += " string=" + string;
if (url != null)
str += " url=" + url.toString();
if (path != null)
str += " path=" + path.toString();
str += " }";
return str;
}

/**
* Return list of allowed extensions
* @return the allowed extensions for sample images
*/
public static List<String> getAllowedExtensions(){
return allowedExtensions;
}

}

0 comments on commit 447a7e9

Please sign in to comment.