From 869eb07edc1e27cd5e02fa0f35ffc2dafb9ca144 Mon Sep 17 00:00:00 2001
From: Manuel Spuhler <manuel.spuhler@chuv.ch>
Date: Fri, 20 Apr 2018 15:42:33 +0200
Subject: [PATCH] Exareme linear regression

---
 .../eu/hbp/mip/controllers/ExperimentApi.java |  2 +-
 .../java/eu/hbp/mip/model/Experiment.java     | 76 ++++++++++++-------
 .../resources/data/exareme_algorithms.json    | 72 +++++++++++-------
 3 files changed, 95 insertions(+), 55 deletions(-)

diff --git a/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java b/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java
index 3ede8eef8..b65416fb5 100644
--- a/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java
+++ b/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java
@@ -84,7 +84,7 @@ public class ExperimentApi extends WokenClientController {
 
         LOGGER.info("Experiment saved");
 
-        if (!experiment.isExaremeAlgorithm()) {
+        if (!experiment.isExaremeAlgorithm()._1) {
             sendExperiment(experiment);
         } else {
             String algoCode = expQuery.getAlgorithms().get(0).getCode();
diff --git a/src/main/java/eu/hbp/mip/model/Experiment.java b/src/main/java/eu/hbp/mip/model/Experiment.java
index f4880376f..377e1f00b 100644
--- a/src/main/java/eu/hbp/mip/model/Experiment.java
+++ b/src/main/java/eu/hbp/mip/model/Experiment.java
@@ -19,7 +19,7 @@ import scala.collection.JavaConversions;
 import javax.persistence.*;
 import java.lang.reflect.Type;
 import java.util.*;
-
+import scala.Tuple2;
 
 /**
  * Created by habfast on 21/04/16.
@@ -28,6 +28,10 @@ import java.util.*;
 @Table(name = "`experiment`")
 public class Experiment {
 
+    public static final String WP_K_MEANS = "K_MEANS";
+
+    public static final String WP_LINEAR_REGRESSION = "WP_LINEAR_REGRESSION";
+
     private static final Logger LOGGER = LoggerFactory.getLogger(Experiment.class);
 
     private static final Gson gson = new Gson();
@@ -134,26 +138,29 @@ public class Experiment {
     public String computeExaremeQuery(List<AlgorithmParam> params) {
         List<ExaremeQueryElement> queryElements = new LinkedList<>();
 
-        // columns
-        List<String> columns = new ArrayList<>();
-        for (Variable var : model.getQuery().getVariables()) { columns.add(var.getCode()); }
-        for (Variable var : model.getQuery().getCovariables()) { columns.add(var.getCode()); }
-        for (Variable var : model.getQuery().getGrouping()) { columns.add(var.getCode()); }
-        StringBuilder sb = new StringBuilder();
-        int i = 0;
-        for (String s : columns)
-        {
-            i++;
-            sb.append(s);
-            if (i < columns.size()) {
-                sb.append(",");
+        // Set algorithm specific queries
+        if (this.isExaremeAlgorithm()._2.equals(WP_K_MEANS)) {
+            // columns
+            List<String> columns = new ArrayList<>();
+            for (Variable var : model.getQuery().getVariables()) { columns.add(var.getCode()); }
+            for (Variable var : model.getQuery().getCovariables()) { columns.add(var.getCode()); }
+            for (Variable var : model.getQuery().getGrouping()) { columns.add(var.getCode()); }
+            StringBuilder sb = new StringBuilder();
+            int i = 0;
+            for (String s : columns)
+            {
+                i++;
+                sb.append(s);
+                if (i < columns.size()) {
+                    sb.append(",");
+                }
             }
+            ExaremeQueryElement columnsEl = new ExaremeQueryElement();
+            columnsEl.setName("columns");
+            columnsEl.setDesc("");
+            columnsEl.setValue(sb.toString());
+            queryElements.add(columnsEl);
         }
-        ExaremeQueryElement columnsEl = new ExaremeQueryElement();
-        columnsEl.setName("columns");
-        columnsEl.setDesc("");
-        columnsEl.setValue(sb.toString());
-        queryElements.add(columnsEl);
 
         // parameters
         if (params != null) {
@@ -183,7 +190,8 @@ public class Experiment {
         ExaremeQueryElement datasetsEl = new ExaremeQueryElement();
         datasetsEl.setName("dataset");
         datasetsEl.setDesc("");
-        datasetsEl.setValue(datasets.toString());
+        datasetsEl.setValue("adni,ppmi,edsd");
+//        datasetsEl.setValue(datasets.toString());
         queryElements.add(datasetsEl);
 
         return gson.toJson(queryElements);
@@ -192,6 +200,7 @@ public class Experiment {
     public JsonObject jsonify() {
         JsonObject exp = gson.toJsonTree(this).getAsJsonObject();
         JsonParser parser = new JsonParser();
+        Tuple2<Boolean, String> isExaremeAlgorithm = this.isExaremeAlgorithm();
 
         if (this.algorithms != null)
         {
@@ -210,7 +219,7 @@ public class Experiment {
         if (this.result != null && !this.hasServerError) {
             exp.remove("result");
 
-            if (!this.isExaremeAlgorithm()) {
+            if (!isExaremeAlgorithm._1) {
                 JsonArray jsonResult = parser.parse(this.result).getAsJsonArray();
                 exp.add("result", jsonResult);
             } else {
@@ -225,7 +234,14 @@ public class Experiment {
                 jsonObjectResult.add("algorithm", algoObject.get("name"));
                 jsonObjectResult.add("code", algoObject.get("code"));
 
-                jsonObjectResult.add("type", new JsonPrimitive("application/vnd.highcharts+json"));
+                // add mime-type
+                if (jsonData.get("Error") != null){
+                    jsonObjectResult.add("type", new JsonPrimitive("text/plain+error"));
+                } else if (isExaremeAlgorithm._2 == WP_K_MEANS){
+                    jsonObjectResult.add("type", new JsonPrimitive("application/vnd.highcharts+json"));
+                } else if (isExaremeAlgorithm._2 == WP_LINEAR_REGRESSION){
+                    jsonObjectResult.add("type", new JsonPrimitive("application/vnd.dataresource+json"));
+                }
 
                 jsonArrayResult.add(jsonObjectResult);
 
@@ -340,11 +356,19 @@ public class Experiment {
         this.shared = shared;
     }
 
-    public boolean isExaremeAlgorithm() {
+    public Tuple2<Boolean, String> isExaremeAlgorithm() {
+        Boolean isExareme = false;
+        String algorithm = "";
+
         String algorithms = this.algorithms;
-        Boolean isExareme =  algorithms.contains("WP_")
-                || algorithms.contains("K_MEANS");
+        if (algorithms.contains(WP_K_MEANS)) {
+            isExareme = true;
+            algorithm = WP_K_MEANS;
+        } else if (algorithms.contains(WP_LINEAR_REGRESSION)) {
+            isExareme = true;
+            algorithm = WP_LINEAR_REGRESSION;
+        }
 
-        return isExareme;
+        return new Tuple2(isExareme, algorithm);
     }
 }
diff --git a/src/main/resources/data/exareme_algorithms.json b/src/main/resources/data/exareme_algorithms.json
index f05c5079b..1e4ade443 100644
--- a/src/main/resources/data/exareme_algorithms.json
+++ b/src/main/resources/data/exareme_algorithms.json
@@ -1,36 +1,52 @@
-[{
-  "code": "glm_exareme",
-  "label": "GLM (exareme)",
-  "type": [
-    "statistics"
-  ],
-  "environment": "Exareme",
-  "description": "Linear Regression using Exareme services",
-  "constraints": {
-    "variable": {
-      "real": true,
-      "binominal": false,
-      "polynominal": false
+[
+  {
+    "code": "WP_LINEAR_REGRESSION",
+    "label": "Linear regression (Exareme)",
+    "type": ["statistics"],
+    "environment": "Exareme",
+    "description": "Linear Regression using Exareme services",
+    "parameters": [
+      {
+        "code": "x",
+        "label": "x",
+        "type": "string",
+        "description": "The expression for x-axis"
+      },
+      {
+        "code": "y",
+        "label": "y",
+        "type": "string",
+        "description": "The expression for y-axis"
+      }
+    ],
+    "constraints": {
+      "variable": {
+        "real": true,
+        "binominal": true,
+        "polynominal": true
+      }
     }
-  }
-},
+  },
   {
     "code": "K_MEANS",
-    "label": "K_MEANS (exareme)",
+    "label": "K_MEANS (Exareme)",
     "type": ["statistics"],
     "environment": "Exareme",
-    "description": "K_MEANS",
-    "parameters": [{
-      "code": "k",
-      "label": "k",
-      "default_value": 5,
-      "type": "int",
-      "constraints": {
-        "min": 1,
-        "max": null
-      },
-      "description": "The number of clusters. Typical values range from 2 to 10."
-    }],
+    "description": "k-Means using Exareme services",
+    "parameters": [
+      {
+        "code": "k",
+        "label": "k",
+        "default_value": 5,
+        "type": "int",
+        "constraints": {
+          "min": 1,
+          "max": null
+        },
+        "description":
+        "The number of clusters. Typical values range from 2 to 10."
+      }
+    ],
     "constraints": {
       "variable": {
         "real": true,
-- 
GitLab