Skip to content

Commit ac2c411

Browse files
authored
Merge pull request #423 from MEDomics-UdeS/scalability
New tool: row tagging prior to v1.2.0
2 parents b52b396 + dddd44d commit ac2c411

5 files changed

Lines changed: 537 additions & 126 deletions

File tree

go_server/blueprints/input/input.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ func AddHandleFunc() {
2121
Utils.CreateHandleFunc(prePath+"/create_tags/", handleCreateTags)
2222
Utils.CreateHandleFunc(prePath+"/delete_tag_from_column/", handleDeleteTagFromColumn)
2323
Utils.CreateHandleFunc(prePath+"/handle_pkl/", handlePKL)
24+
Utils.CreateHandleFunc(prePath+"/create_group_DB/", handleCreateGroupDB)
25+
Utils.CreateHandleFunc(prePath+"/delete_row_tag_DB/", handleDeleteRowTagDB)
2426
}
2527

26-
2728
// handleMerge handles the request to merge the datasets for the DB
2829
// It returns the response from the python script
2930
func handleMergeDB(jsonConfig string, id string) (string, error) {
@@ -60,7 +61,6 @@ func handleCleanDB(jsonConfig string, id string) (string, error) {
6061
return response, nil
6162
}
6263

63-
6464
// handleComputeEigenvaluesDB handles the request to compute the eigenvalues for the DB
6565
// It returns the response from the python script
6666
func handleComputeEigenvaluesDB(jsonConfig string, id string) (string, error) {
@@ -73,7 +73,6 @@ func handleComputeEigenvaluesDB(jsonConfig string, id string) (string, error) {
7373
return response, nil
7474
}
7575

76-
7776
// handleCreatePCADB handles the request to compute pca for the db
7877
// It returns the response from the python script
7978
func handleCreatePCADB(jsonConfig string, id string) (string, error) {
@@ -86,7 +85,6 @@ func handleCreatePCADB(jsonConfig string, id string) (string, error) {
8685
return response, nil
8786
}
8887

89-
9088
// handleApplyPCA handles the request to compute pca with DB
9189
// It returns the response from the python script
9290
func handleApplyPCADB(jsonConfig string, id string) (string, error) {
@@ -99,7 +97,6 @@ func handleApplyPCADB(jsonConfig string, id string) (string, error) {
9997
return response, nil
10098
}
10199

102-
103100
// handleComputeCorrelations handles the request to compute correlations for the DB
104101
// It returns the response from the python script
105102
func handleComputeCorrelationsDB(jsonConfig string, id string) (string, error) {
@@ -112,7 +109,6 @@ func handleComputeCorrelationsDB(jsonConfig string, id string) (string, error) {
112109
return response, nil
113110
}
114111

115-
116112
// handleComputeSpearman handles the request to compute Spearman for the DB
117113
// It returns the response from the python script
118114
func handleComputeSpearmanDB(jsonConfig string, id string) (string, error) {
@@ -125,7 +121,6 @@ func handleComputeSpearmanDB(jsonConfig string, id string) (string, error) {
125121
return response, nil
126122
}
127123

128-
129124
// handleProgress handles the request to get the progress of the experiment
130125
// It returns the progress of the experiment
131126
func handleProgress(jsonConfig string, id string) (string, error) {
@@ -174,3 +169,27 @@ func handlePKL(jsonConfig string, id string) (string, error) {
174169
}
175170
return response, nil
176171
}
172+
173+
// handleCreateGroupDB handles the request to create the group for the DB
174+
// It returns the response from the python script
175+
func handleCreateGroupDB(jsonConfig string, id string) (string, error) {
176+
log.Println("Create Group DB", id)
177+
response, err := Utils.StartPythonScripts(jsonConfig, "../pythonCode/modules/input/create_group_DB.py", id)
178+
Utils.RemoveIdFromScripts(id)
179+
if err != nil {
180+
return "", err
181+
}
182+
return response, nil
183+
}
184+
185+
// deleteRowTagDB handles the request to delete the row tag for the DB
186+
// It returns the response from the python script
187+
func handleDeleteRowTagDB(jsonConfig string, id string) (string, error) {
188+
log.Println("Delete Row Tag DB", id)
189+
response, err := Utils.StartPythonScripts(jsonConfig, "../pythonCode/modules/input/delete_row_tag_DB.py", id)
190+
Utils.RemoveIdFromScripts(id)
191+
if err != nil {
192+
return "", err
193+
}
194+
return response, nil
195+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import json
2+
import sys
3+
import numpy as np
4+
import os
5+
import pandas as pd
6+
from pathlib import Path
7+
sys.path.append(
8+
str(Path(os.path.dirname(os.path.abspath(__file__))).parent.parent))
9+
from med_libs.GoExecutionScript import GoExecutionScript, parse_arguments
10+
from med_libs.server_utils import go_print
11+
12+
# To deal with the DB
13+
from pymongo import MongoClient
14+
import math
15+
16+
# to deal with the ObjectID
17+
from bson import ObjectId
18+
19+
json_params_dict, id_ = parse_arguments()
20+
go_print("running script.py:" + id_)
21+
22+
23+
24+
class GoExecScriptCreateGroupDB(GoExecutionScript):
25+
"""
26+
This class is used to execute the create group DB script.
27+
28+
Args:
29+
json_params: The input json params
30+
_id: The id of the page that made the request if any
31+
"""
32+
33+
def __init__(self, json_params: dict, _id: str = None):
34+
super().__init__(json_params, _id)
35+
self.results = {"data": "nothing to return"}
36+
37+
def _custom_process(self, json_config: dict) -> dict:
38+
"""
39+
This function is used to create the group DB.
40+
41+
Args:
42+
json_config: The input JSON params.
43+
"""
44+
45+
collectionName = json_config["collectionName"]
46+
groupName = json_config["groupName"]
47+
data = json_config["data"]
48+
db_name = json_config["databaseName"]
49+
50+
print("Starting creating group DB script")
51+
52+
# Connect to MongoDB
53+
client = MongoClient('localhost', 54017)
54+
db = client[db_name]
55+
56+
# Create or get the 'row_tags' collection
57+
row_tags_collection = db["row_tags"]
58+
59+
# Create a document to hold the group information
60+
group_document = {
61+
"collectionName": collectionName,
62+
"data": [{"row": row, "groupNames": [groupName]} for row in data] # Store groupName as a list
63+
}
64+
65+
# Check if a document for this collectionName already exists
66+
existing_document = row_tags_collection.find_one({"collectionName": collectionName})
67+
68+
if existing_document:
69+
# Prepare to update existing document
70+
for i, new_entry in enumerate(group_document["data"]):
71+
existing_row = existing_document["data"][i]
72+
# Check if the row already exists
73+
if existing_row["row"] == new_entry["row"]:
74+
# If so, add the new group name to the list
75+
existing_group_names = existing_row.get("groupNames", [])
76+
# Only add the group name if it's not already present
77+
if groupName not in existing_group_names:
78+
existing_group_names.append(groupName)
79+
# Update the existing document
80+
row_tags_collection.update_one(
81+
{"collectionName": collectionName, "data.row": new_entry["row"]},
82+
{"$set": {"data.$.groupNames": existing_group_names}}
83+
)
84+
else:
85+
# If the row does not exist, insert it as a new entry
86+
row_tags_collection.update_one(
87+
{"collectionName": collectionName},
88+
{"$addToSet": {"data": new_entry}} # Use $addToSet to avoid duplicates
89+
)
90+
else:
91+
# Insert the new document
92+
row_tags_collection.insert_one(group_document)
93+
return {"data": f"Created new group '{groupName}' for collection '{collectionName}'."}
94+
95+
return {"data": f"Updated group '{groupName}' for collection '{collectionName}'."}
96+
97+
98+
script = GoExecScriptCreateGroupDB(json_params_dict, id_)
99+
script.start()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import json
2+
import sys
3+
import numpy as np
4+
import os
5+
import pandas as pd
6+
from pathlib import Path
7+
sys.path.append(
8+
str(Path(os.path.dirname(os.path.abspath(__file__))).parent.parent))
9+
from med_libs.GoExecutionScript import GoExecutionScript, parse_arguments
10+
from med_libs.server_utils import go_print
11+
12+
# To deal with the DB
13+
from pymongo import MongoClient
14+
import math
15+
16+
# to deal with the ObjectID
17+
from bson import ObjectId
18+
19+
json_params_dict, id_ = parse_arguments()
20+
go_print("running script.py:" + id_)
21+
22+
23+
24+
class GoExecScriptDeleteRowTagDB(GoExecutionScript):
25+
"""
26+
This class is used to execute the delete row tag DB script.
27+
28+
Args:
29+
json_params: The input json params
30+
_id: The id of the page that made the request if any
31+
"""
32+
33+
def __init__(self, json_params: dict, _id: str = None):
34+
super().__init__(json_params, _id)
35+
self.results = {"data": "nothing to return"}
36+
37+
def _custom_process(self, json_config: dict) -> dict:
38+
tagToDelete = json_config["tagToDelete"]
39+
db_name = json_config["databaseName"]
40+
rowId = json_config["rowId"]
41+
42+
client = MongoClient('localhost', 54017)
43+
db = client[db_name]
44+
row_tags_collection = db["row_tags"]
45+
46+
document = row_tags_collection.find_one()
47+
if document is None:
48+
raise Exception("No document found in the DB")
49+
50+
row = next((item for item in document["data"] if item["row"]["_id"] == rowId), None)
51+
if row is None:
52+
raise Exception("Row not found in the DB")
53+
54+
groupNames = row["groupNames"]
55+
if tagToDelete in groupNames:
56+
groupNames.remove(tagToDelete)
57+
row_tags_collection.update_one(
58+
{"_id": document["_id"]},
59+
{"$set": {"data.$[elem].groupNames": groupNames}},
60+
array_filters=[{"elem.row._id": rowId}]
61+
)
62+
else:
63+
raise Exception("Tag not found in the row")
64+
65+
return {"data": "Tag deleted successfully"}
66+
67+
script = GoExecScriptDeleteRowTagDB(json_params_dict, id_)
68+
script.start()

0 commit comments

Comments
 (0)