Skip to content

Commit

Permalink
add custom exception to read rdf.yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Oct 26, 2023
1 parent 1910876 commit 7fb8265
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.xml.bind.ValidationException;

import io.bioimage.modelrunner.bioimageio.BioimageioRepo;
import io.bioimage.modelrunner.bioimageio.description.exceptions.ModelSpecsException;
import io.bioimage.modelrunner.bioimageio.description.weights.ModelWeight;
import io.bioimage.modelrunner.utils.Log;
import io.bioimage.modelrunner.utils.YAMLUtils;
Expand Down Expand Up @@ -101,9 +100,9 @@ private ModelDescriptor()
* @param modelFile
* Model descriptor file.
* @return The instance of the model descriptor.
* @throws ValidationException if any of the parameters in the rdf.yaml file does not make fit the constraints
* @throws ModelSpecsException if any of the parameters in the rdf.yaml file does not make fit the constraints
*/
public static ModelDescriptor readFromLocalFile(String modelFile) throws ValidationException
public static ModelDescriptor readFromLocalFile(String modelFile) throws ModelSpecsException
{
return readFromLocalFile(modelFile, true);
}
Expand All @@ -116,9 +115,9 @@ public static ModelDescriptor readFromLocalFile(String modelFile) throws Validat
* @param verbose
* whether to print the path to the file and the time to the console or not
* @return The instance of the model descriptor.
* @throws ValidationException if any of the parameters in the rdf.yaml file does not make fit the constraints,
* @throws ModelSpecsException if any of the parameters in the rdf.yaml file does not make fit the constraints,
*/
public static ModelDescriptor readFromLocalFile(String modelFile, boolean verbose) throws ValidationException
public static ModelDescriptor readFromLocalFile(String modelFile, boolean verbose) throws ModelSpecsException
{
// Get the date to be able to log with the time
if (verbose)
Expand All @@ -127,7 +126,7 @@ public static ModelDescriptor readFromLocalFile(String modelFile, boolean verbos
try {
yamlElements = YAMLUtils.load(modelFile);
} catch (IOException ex) {
throw new ValidationException("", ex);
throw new IllegalStateException("", ex);
}
yamlElements.put(fromLocalKey, true);
yamlElements.put(modelPathKey, new File(modelFile).getParent());
Expand All @@ -140,9 +139,9 @@ public static ModelDescriptor readFromLocalFile(String modelFile, boolean verbos
* @param yamlText
* text read from a yaml file that contains an rdf.yaml file
* @return The instance of the model descriptor.
* @throws ValidationException if any of the parameters in the rdf.yaml file does not make fit the constraints
* @throws ModelSpecsException if any of the parameters in the rdf.yaml file does not make fit the constraints
*/
public static ModelDescriptor readFromYamlTextString(String yamlText) throws ValidationException
public static ModelDescriptor readFromYamlTextString(String yamlText) throws ModelSpecsException
{
return readFromYamlTextString(yamlText, true);
}
Expand All @@ -155,9 +154,9 @@ public static ModelDescriptor readFromYamlTextString(String yamlText) throws Val
* @param verbose
* whether to print info about the rdf.yaml that is being read or not
* @return The instance of the model descriptor.
* @throws ValidationException if any of the parameters in the rdf.yaml file does not make fit the constraints
* @throws ModelSpecsException if any of the parameters in the rdf.yaml file does not make fit the constraints
*/
public static ModelDescriptor readFromYamlTextString(String yamlText, boolean verbose) throws ValidationException
public static ModelDescriptor readFromYamlTextString(String yamlText, boolean verbose) throws ModelSpecsException
{
// Convert the String of text that contains the yaml file into Map
Map<String,Object> yamlElements = YAMLUtils.loadFromString(yamlText);
Expand All @@ -178,9 +177,9 @@ public static ModelDescriptor readFromYamlTextString(String yamlText, boolean ve
* @param yamlElements
* map with the information read from a yaml file
* @return a {@link ModelDescriptor} with the info of a Bioimage.io model
* @throws ValidationException if any of the parameters in the rdf.yaml file does not make fit the constraints
* @throws ModelSpecsException if any of the parameters in the rdf.yaml file does not make fit the constraints
*/
private static ModelDescriptor buildModelDescription(Map<String, Object> yamlElements) throws ValidationException
private static ModelDescriptor buildModelDescription(Map<String, Object> yamlElements) throws ModelSpecsException
{
ModelDescriptor modelDescription = new ModelDescriptor();

Expand Down Expand Up @@ -299,7 +298,7 @@ private static ModelDescriptor buildModelDescription(Map<String, Object> yamlEle
}
catch (IOException e)
{
throw new ValidationException("Invalid model element: " + field + "->" + e.getMessage());
throw new ModelSpecsException("Invalid model element: " + field + "->" + e.getMessage());
}
}

Expand All @@ -313,9 +312,9 @@ private static ModelDescriptor buildModelDescription(Map<String, Object> yamlEle
* Every model in the bioimage.io can be run in the BioEngine as long as it is in the
* collections repo:
* https://github.com/bioimage-io/collection-bioimage-io/blob/e77fec7fa4d92d90c25e11331a7d19f14b9dc2cf/rdfs/10.5281/zenodo.6200999/6224243/rdf.yaml
* @throws MalformedURLException servers do not correspond to an actual url
* @throws ModelSpecsException servers do not correspond to an actual url
*/
private void addBioEngine() throws ValidationException {
private void addBioEngine() throws ModelSpecsException {
// TODO decide what to do with servers. Probably need permissions / Implement authentication
if (getName().equals("cellpose-python")) {
supportBioengine = true;
Expand Down Expand Up @@ -517,7 +516,7 @@ private static List<Badge> buildBadgeElements(List<?> coverElements)
}

@SuppressWarnings("unchecked")
private static List<TensorSpec> buildInputTensors(List<?> list) throws ValidationException
private static List<TensorSpec> buildInputTensors(List<?> list) throws ModelSpecsException
{
if (!(list instanceof List<?>))
return null;
Expand Down Expand Up @@ -545,7 +544,7 @@ public void setInputTensors(List<TensorSpec> inputTensors) {
}

@SuppressWarnings("unchecked")
private static List<TensorSpec> buildOutputTensors(List<?> list) throws ValidationException
private static List<TensorSpec> buildOutputTensors(List<?> list) throws ModelSpecsException
{
if (!(list instanceof List<?>))
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import java.util.List;
import java.util.Map;

import javax.xml.bind.ValidationException;

import io.bioimage.modelrunner.bioimageio.description.exceptions.ModelSpecsException;
import io.bioimage.modelrunner.utils.YAMLUtils;

/**
Expand Down Expand Up @@ -59,10 +58,10 @@ public class ShapeSpec
* @param input
* True if it is an input shape. False if it's an output shape descriptor.
* @return The shape specification instance.
* @throws ValidationException if any of the rdf.yaml does not comply the requirements
* @throws ModelSpecsException if any of the rdf.yaml does not comply the requirements
*/
@SuppressWarnings("unchecked")
public static ShapeSpec build(Object shapeElem, boolean input) throws ValidationException
public static ShapeSpec build(Object shapeElem, boolean input) throws ModelSpecsException
{
ShapeSpec shape = new ShapeSpec();
shape.input = input;
Expand Down Expand Up @@ -174,12 +173,12 @@ public float[] getScale()
* always be divisible by 0.5
* @param offset
* the output offset
* @throws ValidationException if the offset is not divisible by 0.5
* @throws ParseException if the offset is not divisible by 0.5
*/
public void setOffset(float[] offset) throws ValidationException {
public void setOffset(float[] offset) throws ModelSpecsException {
for (float ff : offset) {
if (ff % 0.5 != 0) {
throw new ValidationException("Invalid output specifications. "
throw new ModelSpecsException("Invalid output specifications. "
+ "Every output offset should be divisible by 0.5");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import java.util.Map;
import java.util.stream.IntStream;

import javax.xml.bind.ValidationException;

import io.bioimage.modelrunner.bioimageio.description.exceptions.ModelSpecsException;
import io.bioimage.modelrunner.tiling.PatchGridCalculator;
import io.bioimage.modelrunner.utils.YAMLUtils;

Expand Down Expand Up @@ -112,10 +111,10 @@ public class TensorSpec {
* @param input
* Whether it is an input (true) or an output (false) tensor.
* @return The tensor specification instance.
* @throws ValidationException if any of the fields does not fulfill the requirements
* @throws ModelSpecsException if any of the fields does not fulfill the requirements
*/
@SuppressWarnings("unchecked")
public static TensorSpec build(Map<String, Object> tensorSpecMap, boolean input) throws ValidationException
public static TensorSpec build(Map<String, Object> tensorSpecMap, boolean input) throws ModelSpecsException
{
TensorSpec tensor = new TensorSpec();
tensor.name = (String) tensorSpecMap.get("name");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*-
* #%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.exceptions;

/**
* A exception to be launched when there is any error reading the Bioimage.io rdf.yaml file
*
* @author Carlos Garcia Lopez de Haro
*/
public class ModelSpecsException extends Exception
{
private static final long serialVersionUID = 1L;

/**
* Constructor for every exception related to reading the Bioimage.io rdf.yaml specs file
* @param message
* the message that wants to be passed as the exception info.
*/
public ModelSpecsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
import java.util.Map;
import java.util.stream.Collectors;

import javax.xml.bind.ValidationException;

import io.bioimage.modelrunner.bioimageio.BioimageioRepo;
import io.bioimage.modelrunner.bioimageio.description.ModelDescriptor;
import io.bioimage.modelrunner.bioimageio.description.exceptions.ModelSpecsException;
import io.bioimage.modelrunner.bioimageio.description.weights.WeightFormat;
import io.bioimage.modelrunner.bioimageio.download.DownloadTracker;
import io.bioimage.modelrunner.bioimageio.download.DownloadTracker.TwoParameterConsumer;
Expand Down Expand Up @@ -960,10 +959,10 @@ public static boolean installEnginesForModelByNameinDir(String modelName, String
* @return true if at least one DL engine of the model weights defined in the rdf.yaml is
* successfully installed
* @throws FileNotFoundException if the rdf.yaml specs file does not exist
* @throws ValidationException if there is any error with the rdf.yaml file
* @throws ModelSpecsException if there is any error with the rdf.yaml file
* @throws IOException if there is any error creating the folder for the engine or downloading the jar files
*/
public static boolean installEnginesForModelInFolder(String modelFolder) throws FileNotFoundException, ValidationException, IOException {
public static boolean installEnginesForModelInFolder(String modelFolder) throws FileNotFoundException, ModelSpecsException, IOException {
return installEnginesinDirForModelInFolder(modelFolder, InstalledEngines.getEnginesDir(), null);
}

Expand All @@ -982,11 +981,11 @@ public static boolean installEnginesForModelInFolder(String modelFolder) throws
* @return true if at least one DL engine of the model weights defined in the rdf.yaml is
* successfully installed
* @throws FileNotFoundException if the rdf.yaml specs file does not exist
* @throws ValidationException if there is any error with the rdf.yaml file
* @throws ModelSpecsException if there is any error with the rdf.yaml file
* @throws IOException if there is any error creating the folder for the engine or downloading the jar files
*/
public static boolean installEnginesForModelInFolder(String modelFolder,
DownloadTracker.TwoParameterConsumer<String, Double> consumer) throws FileNotFoundException, ValidationException, IOException {
DownloadTracker.TwoParameterConsumer<String, Double> consumer) throws FileNotFoundException, ModelSpecsException, IOException {
return installEnginesinDirForModelInFolder(modelFolder, InstalledEngines.getEnginesDir(), consumer);
}

Expand All @@ -1004,10 +1003,10 @@ public static boolean installEnginesForModelInFolder(String modelFolder,
* @return true if at least one DL engine of the model weights defined in the rdf.yaml is
* successfully installed
* @throws FileNotFoundException if the rdf.yaml specs file does not exist
* @throws ValidationException if there is any error with the rdf.yaml file
* @throws ModelSpecsException if there is any error with the rdf.yaml file
* @throws IOException if there is any error creating the folder for the engine or downloading the jar files
*/
public static boolean installEnginesinDirForModelInFolder(String modelFolder, String enginesDir) throws FileNotFoundException, ValidationException, IOException {
public static boolean installEnginesinDirForModelInFolder(String modelFolder, String enginesDir) throws FileNotFoundException, ModelSpecsException, IOException {
return installEnginesinDirForModelInFolder(modelFolder, enginesDir, null);
}

Expand All @@ -1027,12 +1026,12 @@ public static boolean installEnginesinDirForModelInFolder(String modelFolder, St
* @return true if at least one DL engine of the model weights defined in the rdf.yaml is
* successfully installed
* @throws FileNotFoundException if the rdf.yaml specs file does not exist
* @throws ValidationException if there is any error with the rdf.yaml file
* @throws ModelSpecsException if there is any error with the rdf.yaml file
* @throws IOException if there is any error creating the folder for the engine or downloading the jar files
*/
public static boolean installEnginesinDirForModelInFolder(String modelFolder, String enginesDir,
DownloadTracker.TwoParameterConsumer<String, Double> consumer)
throws FileNotFoundException, ValidationException, IOException {
throws FileNotFoundException, ModelSpecsException, IOException {
if (new File(modelFolder, "rdf.yaml").isFile() == false)
throw new FileNotFoundException("A Bioimage.io model folder should contain its corresponding rdf.yaml file.");
ModelDescriptor descriptor =
Expand Down
Loading

0 comments on commit 7fb8265

Please sign in to comment.