Skip to content

Commit

Permalink
Qa fixes (#73)
Browse files Browse the repository at this point in the history
* Removed the 'Exareme' restriction requiring all algorithm names to be in uppercase.

* Fixed a bug in which we incorrectly expected the algorithm parameter to contain the variable "default" instead of "default_value."

* Fixed a bug in which we incorrectly converted incoming str to int.
  • Loading branch information
KFilippopolitis authored Oct 31, 2023
1 parent 24bae61 commit 95f246f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public record TransformerSpecificationDTO(String name, String label, String desc
List<AlgorithmParameterSpecificationDTO> parameters) {
public TransformerSpecificationDTO(Exareme2AlgorithmSpecificationDTO.Exareme2TransformerSpecificationDTO transformerDTO) {
this(
transformerDTO.name().toUpperCase(),
transformerDTO.name(),
transformerDTO.label(),
transformerDTO.desc(),
getAlgorithmParameterSpecifications(transformerDTO.parameters())
Expand Down Expand Up @@ -128,7 +128,7 @@ private static String getParameterColumnValuesIsCategorical(String inputDataDeta

public AlgorithmSpecificationDTO(Exareme2AlgorithmSpecificationDTO exareme2Algorithm){
this(
exareme2Algorithm.name().toUpperCase(),
exareme2Algorithm.name(),
exareme2Algorithm.label(),
exareme2Algorithm.desc(),
getAlgorithmParameters(exareme2Algorithm),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonSyntaxException;
import hbp.mip.models.DTOs.ExperimentExecutionDTO;
import hbp.mip.utils.Exceptions.InternalServerError;
import hbp.mip.utils.JsonConverters;

import java.util.*;
Expand All @@ -16,13 +17,13 @@ public record Exareme2AlgorithmRequestDTO(
public Exareme2AlgorithmRequestDTO(
UUID experimentUUID,
List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO> exaremeAlgorithmRequestParamDTOs,
List<ExperimentExecutionDTO.AlgorithmExecutionDTO.TransformerExecutionDTO> exaremeTransformers
) {
List<ExperimentExecutionDTO.AlgorithmExecutionDTO.TransformerExecutionDTO> exaremeTransformers,
Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) {
this(
experimentUUID.toString(),
getInputData(exaremeAlgorithmRequestParamDTOs),
getParameters(exaremeAlgorithmRequestParamDTOs),
getPreprocessing(exaremeTransformers)
getParameters(exaremeAlgorithmRequestParamDTOs, exareme2AlgorithmSpecificationDTO),
getPreprocessing(exaremeTransformers, exareme2AlgorithmSpecificationDTO)
);
}

Expand Down Expand Up @@ -65,7 +66,7 @@ private static Exareme2InputDataRequestDTO getInputData(List<ExperimentExecution
);
}

private static Map<String, Object> getParameters(List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO> exaremeAlgorithmRequestParamDTOs) {
private static Map<String, Object> getParameters(List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO> exaremeAlgorithmRequestParamDTOs, Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) {
if (exaremeAlgorithmRequestParamDTOs == null) {
return null;
}
Expand All @@ -75,28 +76,46 @@ private static Map<String, Object> getParameters(List<ExperimentExecutionDTO.Alg

HashMap<String, Object> exareme2Parameters = new HashMap<>();
exaremeAlgorithmRequestParamDTOs.forEach(parameter -> {
if (!inputDataFields.contains(parameter.name()))
exareme2Parameters.put(parameter.name(), convertStringToProperExareme2ParameterType(parameter.value()));
if (!inputDataFields.contains(parameter.name())){
Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = exareme2AlgorithmSpecificationDTO.parameters().get(parameter.name());
exareme2Parameters.put(parameter.name(), convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto));
}
});
return exareme2Parameters;
}

private static Map<String, Object> getPreprocessing(List<ExperimentExecutionDTO.AlgorithmExecutionDTO.TransformerExecutionDTO> exaremeTransformers) {
private static Map<String, Object> getPreprocessing(List<ExperimentExecutionDTO.AlgorithmExecutionDTO.TransformerExecutionDTO> exaremeTransformers, Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) {
if (exaremeTransformers == null) {
return null;
}

HashMap<String, Object> exareme2Preprocessing = new HashMap<>();
exaremeTransformers.forEach(transformer -> {
String transformer_name = transformer.name();
HashMap<String, Object> transformerParameterDTOs = new HashMap<>();
for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO parameter : transformer.parameters())
transformerParameterDTOs.put(parameter.name(), convertStringToProperExareme2ParameterType(parameter.value()));
exareme2Preprocessing.put(transformer.name(), transformerParameterDTOs);
for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO parameter : transformer.parameters()){
String param_name = parameter.name();
Optional<Exareme2AlgorithmSpecificationDTO.Exareme2TransformerSpecificationDTO> transformerSpecificationDTO = exareme2AlgorithmSpecificationDTO.preprocessing().stream()
.filter(transformerSpec-> transformerSpec.name().equals(transformer_name))
.findFirst();
if (transformerSpecificationDTO.isEmpty()) throw new InternalServerError("Missing the transformer: " + transformer_name);

Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = transformerSpecificationDTO.get().parameters().get(param_name);
transformerParameterDTOs.put(param_name, convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto));
}
exareme2Preprocessing.put(transformer_name, transformerParameterDTOs);
});

return exareme2Preprocessing;
}

private static Object convertStringToProperExareme2ParameterTypeAccordingToSpecs(String value, Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto) {
if (paramSpecDto.enums() != null){
return value;
}
return convertStringToProperExareme2ParameterType(value);
}

private static Object convertStringToProperExareme2ParameterType(String str) {
if (isMap(str))
return JsonConverters.convertJsonStringToObject(str, Map.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hbp.mip.models.DTOs.exareme2;

import com.google.gson.annotations.SerializedName;

import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -9,7 +11,6 @@ public record Exareme2AlgorithmSpecificationDTO(
String name,
String label,
String desc,
String type,
Exareme2AlgorithmInputdataSpecificationDTO inputdata,
Map<String, Exareme2AlgorithmParameterSpecificationDTO> parameters,
List<Exareme2TransformerSpecificationDTO> preprocessing
Expand All @@ -24,7 +25,6 @@ public List<Exareme2TransformerSpecificationDTO> preprocessing() {
return Objects.requireNonNullElse(preprocessing, Collections.EMPTY_LIST);
}


public record Exareme2AlgorithmParameterSpecificationDTO(
String label,
String desc,
Expand All @@ -33,8 +33,9 @@ public record Exareme2AlgorithmParameterSpecificationDTO(
String multiple,
String min,
String max,
@SerializedName("default")
String default_value,
Exareme2AlgorithmParameterSpecificationDTO.Exareme2AlgorithmEnumDTO enums,
Exareme2AlgorithmEnumDTO enums,
Exareme2AlgorithmEnumDTO dict_keys_enums,
Exareme2AlgorithmEnumDTO dict_values_enums

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/hbp/mip/services/AlgorithmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hbp.mip.models.DTOs.AlgorithmSpecificationDTO;
import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmSpecificationDTO;
import hbp.mip.utils.CustomResourceLoader;
import hbp.mip.utils.Exareme2AlgorithmsSpecs;
import hbp.mip.utils.HTTPUtil;
import hbp.mip.utils.Logger;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -23,6 +24,7 @@ public class AlgorithmService {

private static final Gson gson = new Gson();

private final Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs;
private final CustomResourceLoader resourceLoader;

@Value("${files.disabledAlgorithms_json}")
Expand All @@ -31,7 +33,8 @@ public class AlgorithmService {
@Value("${services.exareme2.algorithmsUrl}")
private String exareme2AlgorithmsUrl;

public AlgorithmService(CustomResourceLoader resourceLoader) {
public AlgorithmService(Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs, CustomResourceLoader resourceLoader) {
this.exareme2AlgorithmsSpecs = exareme2AlgorithmsSpecs;
this.resourceLoader = resourceLoader;
}

Expand Down Expand Up @@ -77,6 +80,7 @@ private List<Exareme2AlgorithmSpecificationDTO> getExareme2Algorithms(Logger log
}

logger.debug("Fetched " + algorithms.size() + " exareme2 algorithms.");
exareme2AlgorithmsSpecs.setAlgorithms(algorithms);
return algorithms;
}

Expand Down
27 changes: 17 additions & 10 deletions src/main/java/hbp/mip/services/ExperimentService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package hbp.mip.services;

import hbp.mip.models.DAOs.ExperimentDAO;
import hbp.mip.models.DTOs.ExperimentDTO;
import hbp.mip.models.DTOs.ExperimentExecutionDTO;
import hbp.mip.models.DTOs.ExperimentsDTO;
import hbp.mip.models.DTOs.UserDTO;
import hbp.mip.models.DTOs.*;
import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmRequestDTO;
import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmSpecificationDTO;
import hbp.mip.repositories.ExperimentRepository;
import hbp.mip.repositories.ExperimentSpecifications;
import hbp.mip.utils.ClaimUtils;
import hbp.mip.utils.*;
import hbp.mip.utils.Exceptions.BadRequestException;
import hbp.mip.utils.Exceptions.InternalServerError;
import hbp.mip.utils.Exceptions.NoContent;
import hbp.mip.utils.Exceptions.UnauthorizedException;
import hbp.mip.utils.HTTPUtil;
import hbp.mip.utils.JsonConverters;
import hbp.mip.utils.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -36,6 +31,7 @@ public class ExperimentService {
private final ActiveUserService activeUserService;

private final ClaimUtils claimUtils;
private final Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs;

private final ExperimentRepository experimentRepository;

Expand All @@ -48,10 +44,12 @@ public class ExperimentService {
public ExperimentService(
ActiveUserService activeUserService,
ClaimUtils claimUtils,
Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs,
ExperimentRepository experimentRepository
) {
this.activeUserService = activeUserService;
this.claimUtils = claimUtils;
this.exareme2AlgorithmsSpecs = exareme2AlgorithmsSpecs;
this.experimentRepository = experimentRepository;
}

Expand Down Expand Up @@ -342,8 +340,9 @@ private String getExperimentDatasets(ExperimentExecutionDTO experimentExecutionD

private ExperimentAlgorithmResultDTO runExaremeAlgorithm(UUID uuid, ExperimentExecutionDTO experimentExecutionDTO, Logger logger) {
String algorithmName = experimentExecutionDTO.algorithm().name();
String algorithmEndpoint = exareme2AlgorithmsUrl + "/" + algorithmName.toLowerCase();
var exareme2AlgorithmRequestDTO = new Exareme2AlgorithmRequestDTO(uuid, experimentExecutionDTO.algorithm().parameters(), experimentExecutionDTO.algorithm().preprocessing());
String algorithmEndpoint = exareme2AlgorithmsUrl + "/" + algorithmName;
Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO = getAlgorithmSpec(algorithmName);
var exareme2AlgorithmRequestDTO = new Exareme2AlgorithmRequestDTO(uuid, experimentExecutionDTO.algorithm().parameters(), experimentExecutionDTO.algorithm().preprocessing(), exareme2AlgorithmSpecificationDTO);
String algorithmBody = convertObjectToJsonString(exareme2AlgorithmRequestDTO);
logger.debug("Exareme2 algorithm request, endpoint: " + algorithmEndpoint);
logger.debug("Exareme2 algorithm request, body: " + algorithmBody);
Expand All @@ -362,6 +361,14 @@ private ExperimentAlgorithmResultDTO runExaremeAlgorithm(UUID uuid, ExperimentEx
return new ExperimentAlgorithmResultDTO(requestResponseCode, result);
}

private Exareme2AlgorithmSpecificationDTO getAlgorithmSpec(String algorithmName){
Optional<Exareme2AlgorithmSpecificationDTO> algorithmSpecification = exareme2AlgorithmsSpecs.getAlgorithms().stream()
.filter(algorithmSpec-> algorithmSpec.name().equals(algorithmName))
.findFirst();
if (algorithmSpecification.isEmpty()) throw new InternalServerError("Missing the algorithm: " + algorithmName);
return algorithmSpecification.get();
}

record ExperimentAlgorithmResultDTO(int code, List<Object> result) {
}
}
21 changes: 21 additions & 0 deletions src/main/java/hbp/mip/utils/Exareme2AlgorithmsSpecs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hbp.mip.utils;

import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmSpecificationDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class Exareme2AlgorithmsSpecs {
private List<Exareme2AlgorithmSpecificationDTO> algorithms = new ArrayList<>();


public List<Exareme2AlgorithmSpecificationDTO> getAlgorithms() {
return algorithms;
}

public void setAlgorithms(List<Exareme2AlgorithmSpecificationDTO> algorithms) {
this.algorithms = algorithms;
}
}

0 comments on commit 95f246f

Please sign in to comment.