From ed0ac0be69050bbc691c3ed375fcc041ff861d70 Mon Sep 17 00:00:00 2001
From: Christian Schiffer <c.schiffer@fz-juelich.de>
Date: Fri, 9 Apr 2021 08:01:22 +0200
Subject: [PATCH] Implemented changes for import of predictions

---
 atlas_server/src/app.py      |  10 ++-
 microdraw/src/atlas_ui.js    | 142 +++++++++++++++++++----------------
 microdraw/src/microdraw.html |  31 +++++---
 3 files changed, 107 insertions(+), 76 deletions(-)

diff --git a/atlas_server/src/app.py b/atlas_server/src/app.py
index e8bd0ec..771e16b 100644
--- a/atlas_server/src/app.py
+++ b/atlas_server/src/app.py
@@ -523,7 +523,9 @@ class AnnotationImportExport(Resource):
 class PredictionAnnotationExport(Resource):
     @annotation_namespace.doc("Retrieve annotations based on predictions of a specific task")
     def get(self, project_id, task_id):
+        import os
         import requests
+        from atlaslib.files import get_section_number_from_string
         from .config import ATLAS_CONTROLLER_URL
 
         # Determine
@@ -535,11 +537,15 @@ class PredictionAnnotationExport(Resource):
             return
         result = result.json()
 
+        annotations = {}
+        for item in result:
+            section = get_section_number_from_string(os.path.basename(item["path"]))
+            annotations[section] = item["annotations"]
+
         return {
             "project_id": project_id,
             "task_id": task_id,
-            "path": result["path"],
-            "annotations": result["annotations"],
+            "annotations": annotations,
         }
 
 
diff --git a/microdraw/src/atlas_ui.js b/microdraw/src/atlas_ui.js
index 657ff7b..03b5477 100644
--- a/microdraw/src/atlas_ui.js
+++ b/microdraw/src/atlas_ui.js
@@ -3,7 +3,7 @@ if (debug) {
 }
 
 var atlasRegions = [];
- var atlasServerApiUrl = "http://medpc076.ime.kfa-juelich.de:5000/api"
+var atlasServerApiUrl = "http://medpc076.ime.kfa-juelich.de:5000/api"
 //var atlasServerApiUrl = "http://localhost:5000/api"
 // var serverHost = "medpc076.ime.kfa-juelich.de"
 // var serverPort = 80
@@ -42,21 +42,24 @@ function Task() {
 }
 
 function AnnotationImport() {
-    this.src_host = "imedv02.ime.kfa-juelich.de";
-    // This is fixed for now. Always import into running instance.
-    this.dst_host = "atlasui_microdraw-mariadb_1";
-    this.src_user = null;
-    this.dst_user = null;
-    this.src_path = null;
-    this.dst_path = null;
-    this.annotation_items = [];
-    this.loading_state = false;
+	this.src_host = "imedv02.ime.kfa-juelich.de";
+	// This is fixed for now. Always import into running instance.
+	this.dst_host = "atlasui_microdraw-mariadb_1";
+	this.src_user = null;
+	this.dst_user = null;
+	this.src_path = null;
+	this.dst_path = null;
+	this.annotation_items = [];
+	this.loading_state = false;
+	this.project_import = null;
+	this.src_project = 0;
+	this.src_task = 0;
 }
 
 function AnnotationImportItem(section, annotations) {
-    this.section = section;
-    this.annotations = annotations;
-    this.loading_state = false;
+	this.section = section;
+	this.annotations = annotations;
+	this.loading_state = false;
 }
 
 function compareRegions(region0, region1) {
@@ -581,33 +584,42 @@ var app = new Vue({
 			window.location.href = target_link;
 		},
 		initializeAnnotationImport: function() {
-		    if (typeof(myOrigin.user) == "object") {
-                this.annotation_import.src_user = null;
-                this.annotation_import.dst_user = null;
-		    } else {
-                this.annotation_import.src_user = myOrigin.user;
-                this.annotation_import.dst_user = myOrigin.user;
-		    }
-		    this.annotation_import.src_path = myOrigin.source;
-		    this.annotation_import.dst_path = myOrigin.source;
+			if (typeof(myOrigin.user) == "object") {
+				this.annotation_import.src_user = null;
+				this.annotation_import.dst_user = null;
+			} else {
+				this.annotation_import.src_user = myOrigin.user;
+				this.annotation_import.dst_user = myOrigin.user;
+			}
+			this.annotation_import.src_path = myOrigin.source;
+			this.annotation_import.dst_path = myOrigin.source;
 		},
 		getAnnotations: function() {
-		    console.log("get annotations");
+			console.log("get annotations");
 
-		    let user = this.annotation_import.src_user;
+			let user = this.annotation_import.src_user;
 
-		    if (user === null) {
-		        alert("Please enter a source user before retrieving annotations.");
-		        return;
-		    }
+			this.annotation_import.annotation_items = [];
 
-		    let host = encodeURIComponent(btoa(this.annotation_import.src_host));
-		    let path = encodeURIComponent(btoa(this.annotation_import.src_path));
-		    this.annotation_import.annotation_items = [];
+			let endpoint = null;
+			if (this.annotation_import.project_import) {
+				// Import predictions from a specific project
+				let project = this.annotation_import.src_project;
+				let task = this.annotation_import.src_task;
+				endpoint = `${atlasServerApiUrl}/predictions/${project}/tasks/${task}`;
+			} else {
+				// Import predictions from a specific user
+				if (user === null) {
+					alert("Please enter a source user before retrieving annotations.");
+					return;
+				}
+				let host = encodeURIComponent(btoa(this.annotation_import.src_host));
+				let path = encodeURIComponent(btoa(this.annotation_import.src_path));
+				endpoint = `${atlasServerApiUrl}/annotations/${host}/${user}/${path}`;
+			}
 
-			let endpoint = `${atlasServerApiUrl}/annotations/${host}/${user}/${path}`;
 			let _this = this;
-		    this.annotation_import.loading_state = true;
+			this.annotation_import.loading_state = true;
 			$.ajax({
 				url: endpoint,
 				type: "GET",
@@ -619,40 +631,40 @@ var app = new Vue({
 					_this.annotation_items = [];
 					console.log(annotations);
 					for (let key in annotations) {
-					    let value = annotations[key];
-					    _this.annotation_import.annotation_items.push(new AnnotationImportItem(key, value));
+						let value = annotations[key];
+						_this.annotation_import.annotation_items.push(new AnnotationImportItem(key, value));
 					}
 				},
 				error: function (jqXHR, textStatus, errorThrown) {
 					post_message(`Error getting annotations status: ${textStatus} ${errorThrown}`);
 				},
 				complete: function(data) {
-				    _this.annotation_import.loading_state = false;
+					_this.annotation_import.loading_state = false;
 				}
 			});
 		},
 		importAll: function(overwrite) {
-		    if (!this.checkUser()) {
-		        return;
-		    }
-		    for (let annotation in this.annotation_import.annotation_items) {
-		        this.importAnnotation(this.annotation_import.annotation_items[annotation], overwrite);
-		    }
+			if (!this.checkUser()) {
+				return;
+			}
+			for (let annotation in this.annotation_import.annotation_items) {
+				this.importAnnotation(this.annotation_import.annotation_items[annotation], overwrite);
+			}
 		},
 		importAnnotation: function(annotation, overwrite) {
-		    if (!this.checkUser()) {
-		        return;
-		    }
-		    console.log(annotation);
+			if (!this.checkUser()) {
+				return;
+			}
+			console.log(annotation);
 
-		    let user = this.annotation_import.dst_user;
-            console.log(`Importing annotation to user ${this.annotation_import.dst_user} at ${this.annotation_import.dst_host}`);
-		    let host = encodeURIComponent(btoa(this.annotation_import.dst_host));
-		    let path = encodeURIComponent(btoa(this.annotation_import.dst_path));
+			let user = this.annotation_import.dst_user;
+			console.log(`Importing annotation to user ${this.annotation_import.dst_user} at ${this.annotation_import.dst_host}`);
+			let host = encodeURIComponent(btoa(this.annotation_import.dst_host));
+			let path = encodeURIComponent(btoa(this.annotation_import.dst_path));
 
 			let endpoint = `${atlasServerApiUrl}/annotations/${host}/${user}/${path}`;
 			if (overwrite) {
-			    endpoint = `${endpoint}?overwrite=True`;
+				endpoint = `${endpoint}?overwrite=True`;
 			}
 			console.log(endpoint);
 			let _this = this;
@@ -660,7 +672,7 @@ var app = new Vue({
 			data[annotation.section] = annotation.annotations;
 
 			// Disable buttons while importing
-		    annotation.loading_state = true;
+			annotation.loading_state = true;
 			$.ajax({
 				url: endpoint,
 				type: "POST",
@@ -670,30 +682,30 @@ var app = new Vue({
 				crossDomain: true,
 				success: function (data) {
 					// Remove annotation from list
-		            _this.removeImportAnnotation(annotation);
+					_this.removeImportAnnotation(annotation);
 				},
 				error: function (jqXHR, textStatus, errorThrown) {
 					post_message(`Error importing annotation: ${textStatus} ${errorThrown}`);
 				},
 				complete: function(data) {
-				    annotation.loading_state = false;
+					annotation.loading_state = false;
 				}
 			});
 		},
 		checkUser: function() {
-		    let user_ok = this.annotation_import.dst_user !== null && this.annotation_import.dst_user !== ""
-		    if (!user_ok) {
-		        alert("Please enter a target user before importing annotations.");
-		        return false;
-		    }
-		    return true;
+			let user_ok = this.annotation_import.dst_user !== null && this.annotation_import.dst_user !== ""
+			if (!user_ok) {
+				alert("Please enter a target user before importing annotations.");
+				return false;
+			}
+			return true;
 		},
 		removeImportAnnotation: function(annotation) {
-		    for (let idx in this.annotation_import.annotation_items) {
-		        if (this.annotation_import.annotation_items[idx] === annotation) {
-		            this.annotation_import.annotation_items.splice(idx, 1);
-		        }
-		    }
+			for (let idx in this.annotation_import.annotation_items) {
+				if (this.annotation_import.annotation_items[idx] === annotation) {
+					this.annotation_import.annotation_items.splice(idx, 1);
+				}
+			}
 		}
 	},
 	created: function () {
diff --git a/microdraw/src/microdraw.html b/microdraw/src/microdraw.html
index 0effafe..99cc822 100755
--- a/microdraw/src/microdraw.html
+++ b/microdraw/src/microdraw.html
@@ -527,31 +527,44 @@
 						<div class="modal-body">
 							<div class="form">
 								<div class="form-row">
-									<label class="col-form-label">Source host</label>
+									<label class="col-form-label col-2">Source host</label>
 									<div class="col-4">
 										<input v-model="annotation_import.src_host" class="form-control" type="text"
 											   data-toggle="tooltip" data-placement="top" title="Host of the machine to retrieve annotations from. If you do not know what this is, leave the default value untouched.">
 									</div>
+									<div class="col-4">
+										<input type="checkbox" v-model="annotation_import.project_import" id="atlas_ui_import_form_project_import"
+											   data-toggle="tooltip" data-placement="top" title="Switch between import from user and import from predictions of specific task.">
+										<label for="atlas_ui_import_form_project_import">Import predictions</label>
+									</div>
 								</div>
 								<div class="form-row">
-									<label class="col-form-label">Source user</label>
+									<label class="col-form-label col-2" v-if="annotation_import.project_import">Project ID</label>
+									<label class="col-form-label col-2" v-else>Source user</label>
 									<div class="col-4">
-										<input v-model="annotation_import.src_user" type="text" class="form-control"
+										<input type="number" min="0" v-model="annotation_import.src_project" type="text" class="form-control" v-if="annotation_import.project_import"
+										data-toggle="tooltip" data-placement="top" title="ID of project to import predictions from.">
+										<input v-model="annotation_import.src_user" type="text" class="form-control" v-else
 										data-toggle="tooltip" data-placement="top" title="Microdraw user on source host to retrieve annotations from.">
 									</div>
-									<label class="col-form-label">Destination user</label>
+									<label class="col-form-label col-2">Destination user</label>
 									<div class="col-4">
 										<input v-model="annotation_import.dst_user" type="text" class="form-control"
 										data-toggle="tooltip" data-placement="top" title="Microdraw user to import annotations to.">
 									</div>
 								</div>
+
 								<div class="form-row">
-									<label class="col-form-label">Source path</label>
+									<label class="col-form-label col-2" v-if="annotation_import.project_import">Task ID</label>
+									<label class="col-form-label col-2" v-else>Source path</label>
+
 									<div class="col-4">
-										<input v-model="annotation_import.src_path" type="text" class="form-control"
+										<input type="number" min="0" v-model="annotation_import.src_task" type="text" class="form-control" v-if="annotation_import.project_import"
+										data-toggle="tooltip" data-placement="top" title="ID of task to import predictions from.">
+										<input v-model="annotation_import.src_path" type="text" class="form-control" v-else
 										data-toggle="tooltip" data-placement="top" title="Microdraw path to import annotations from.">
 									</div>
-									<label class="col-form-label">Destination path</label>
+									<label class="col-form-label col-2">Destination path</label>
 									<div class="col-4">
 										<input v-model="annotation_import.dst_path" type="text" class="form-control"
 										data-toggle="tooltip" data-placement="top" title="Microdraw path to import annotations to.">
@@ -581,10 +594,10 @@
 												<input v-model="ann_item.section" class="form-control" type="text" readonly />
 											</div>
 											<label class="col-2 col-form-label text-right">Annotations</label>
-											<div class="col-1">
+											<div class="col-2">
 												<input v-model="ann_item.annotations.Regions.length" class="form-control" type="text" readonly />
 											</div>
-											<div class="col-3">
+											<div class="col-2">
 											</div>
 											<div class="col-3">
 												<div class="btn-group" role="group">
-- 
GitLab