|
| 1 | +package hbp.mip.algorithm; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.google.gson.Gson; |
| 6 | +import com.google.gson.reflect.TypeToken; |
| 7 | +import hbp.mip.experiment.ExperimentExecutionDTO; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.lang.reflect.Type; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +class AlgorithmSpecificationDTOTest { |
| 19 | + private final Gson gson = new Gson(); |
| 20 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 21 | + |
| 22 | + @Test |
| 23 | + void deserializesOutlierDictionaryMetadataAndPreprocessingOrder() throws Exception { |
| 24 | + String payload = """ |
| 25 | + [ |
| 26 | + { |
| 27 | + "name": "outlier_report", |
| 28 | + "label": "Outlier Report", |
| 29 | + "desc": "Detect outliers.", |
| 30 | + "inputdata": { |
| 31 | + "data_model": { "label": "Data model", "desc": "", "types": ["text"] }, |
| 32 | + "datasets": { "label": "Datasets", "desc": "", "types": ["text"] }, |
| 33 | + "y": { "label": "Y", "desc": "", "types": ["real"], "required": true, "multiple": true } |
| 34 | + }, |
| 35 | + "parameters": { |
| 36 | + "folds": { |
| 37 | + "label": "Folds", |
| 38 | + "types": ["dict"], |
| 39 | + "required": false, |
| 40 | + "multiple": false, |
| 41 | + "dict_keys_enums": { |
| 42 | + "type": "input_var_names", |
| 43 | + "source": ["x", "y"] |
| 44 | + }, |
| 45 | + "dict_values_type": "real" |
| 46 | + } |
| 47 | + }, |
| 48 | + "preprocessing": [ |
| 49 | + { |
| 50 | + "name": "outlier_winsorizer", |
| 51 | + "label": "Outlier Winsorizer", |
| 52 | + "order": 2, |
| 53 | + "parameters": {} |
| 54 | + } |
| 55 | + ] |
| 56 | + } |
| 57 | + ] |
| 58 | + """; |
| 59 | + |
| 60 | + Type algorithmListType = new TypeToken<List<AlgorithmSpecificationDTO>>() { |
| 61 | + }.getType(); |
| 62 | + List<AlgorithmSpecificationDTO> algorithms = gson.fromJson(payload, algorithmListType); |
| 63 | + |
| 64 | + AlgorithmSpecificationDTO algorithm = algorithms.getFirst(); |
| 65 | + AlgorithmSpecificationDTO.AlgorithmParameterSpecificationDTO folds = algorithm.parameters().get("folds"); |
| 66 | + |
| 67 | + assertThat(folds.dict_values_type()).isEqualTo("real"); |
| 68 | + assertThat(folds.dict_keys_enums().type()).isEqualTo("input_var_names"); |
| 69 | + assertThat(folds.dict_keys_enums().source()).containsExactly("x", "y"); |
| 70 | + assertThat(algorithm.preprocessing().getFirst().order()).isEqualTo(2); |
| 71 | + |
| 72 | + JsonNode serialized = objectMapper.readTree(objectMapper.writeValueAsString(algorithm)); |
| 73 | + assertThat(serialized.at("/parameters/folds/dict_values_type").asText()).isEqualTo("real"); |
| 74 | + assertThat(serialized.at("/preprocessing/0/order").asInt()).isEqualTo(2); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void serializesNestedOutlierPreprocessingUnchangedForExaflowRequest() throws Exception { |
| 79 | + Map<String, Object> outlierWinsorizer = new LinkedHashMap<>(); |
| 80 | + outlierWinsorizer.put("strategies", Map.of("age", "iqr")); |
| 81 | + outlierWinsorizer.put("tails", Map.of("age", "both")); |
| 82 | + outlierWinsorizer.put("folds", Map.of("age", 1.5)); |
| 83 | + |
| 84 | + Map<String, Object> preprocessing = Map.of("outlier_winsorizer", outlierWinsorizer); |
| 85 | + ExperimentExecutionDTO.AlgorithmExecutionDTO algorithmExecutionDTO = |
| 86 | + new ExperimentExecutionDTO.AlgorithmExecutionDTO( |
| 87 | + "linear_regression", |
| 88 | + new AlgorithmRequestDTO.InputDataRequestDTO( |
| 89 | + "dm:1", |
| 90 | + List.of("ds1"), |
| 91 | + List.of("outcome"), |
| 92 | + List.of("age"), |
| 93 | + null |
| 94 | + ), |
| 95 | + Map.of(), |
| 96 | + preprocessing |
| 97 | + ); |
| 98 | + |
| 99 | + AlgorithmRequestDTO request = AlgorithmRequestDTO.create( |
| 100 | + UUID.fromString("00000000-0000-0000-0000-000000000001"), |
| 101 | + algorithmExecutionDTO |
| 102 | + ); |
| 103 | + |
| 104 | + JsonNode outlierNode = objectMapper.readTree(objectMapper.writeValueAsString(request)) |
| 105 | + .at("/preprocessing/outlier_winsorizer"); |
| 106 | + |
| 107 | + assertThat(outlierNode.at("/strategies/age").asText()).isEqualTo("iqr"); |
| 108 | + assertThat(outlierNode.at("/tails/age").asText()).isEqualTo("both"); |
| 109 | + assertThat(outlierNode.at("/folds/age").asDouble()).isEqualTo(1.5); |
| 110 | + } |
| 111 | +} |
0 commit comments