Skip to content

Commit

Permalink
refactor lexicon service
Browse files Browse the repository at this point in the history
  • Loading branch information
khansadaoudi committed May 24, 2024
1 parent 555f7d0 commit 73eaadd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 87 deletions.
63 changes: 19 additions & 44 deletions app/lexicon/controller.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import json

from flask_login import current_user
from flask import request
from flask import Response
from flask_restx import Namespace, Resource, reqparse
from flask_restx import Namespace, Resource

from app.lexicon.interface import LexiconItemInterface
from app.lexicon.schema import LexiconItemSchema
from app.utils.grew_utils import grew_request
from app.utils.grew_utils import GrewService

api = Namespace(
"LexiconV2", description="Endpoints for dealing with samples of project"
Expand All @@ -18,44 +16,22 @@ class LexiconResource(Resource):
"Lexicon"

def post(self, project_name: str):
parser = reqparse.RequestParser()
parser.add_argument(name="samplenames", type=str, action="append")
parser.add_argument(name="features", type=str,action="append")
parser.add_argument(name="lexiconType", type=str)
parser.add_argument(name="prune", type=int)
args = parser.parse_args()

sample_names = args.get("samplenames")

args = request.get_json()
sample_ids = args.get("samplenames")
features = args.get("features")
lexicon_type=args.get("lexiconType")
prune=args.get("prune")

if lexicon_type == 'user':
user_ids = { "one": [current_user.username] }
elif lexicon_type == 'user_recent':
user_ids = { "one": [current_user.username, "__last__"] }
elif lexicon_type == 'recent':
user_ids = { "one": ["__last__"] }
elif lexicon_type == 'validated':
user_ids = { "one": ["validated"] }
elif lexicon_type=='all':
user_ids = "all"

prune = (None, prune) [prune != 0]
reply = grew_request(
"getLexicon",
data={"project_id": project_name, "sample_ids": json.dumps(sample_names), "user_ids": json.dumps(user_ids), "features": json.dumps(features),"prune":prune},
)
lexicon_type = args.get("lexiconType")
prune = args.get("prune")

reply = GrewService.get_lexicon(project_name, sample_ids, lexicon_type, '',prune, features)
return reply["data"]


@api.route("/<string:project_name>/lexicon/export-json")
@api.route("lexicon/export-json")
class LexiconExportJson(Resource):
def post(self, project_name: str):
parser = reqparse.RequestParser()
parser.add_argument(name="data", type=dict, action="append")
args = parser.parse_args()
def post(self):

args = request.get_json()
lexicon = args.get("data")
for element in lexicon:
del element["key"]
Expand All @@ -64,17 +40,16 @@ def post(self, project_name: str):
return resp


@api.route("/<string:project_name>/lexicon/export-tsv")
@api.route("/lexicon/export-tsv")
class LexiconExportTsv(Resource):
def post(self, project_name: str):
parser = reqparse.RequestParser()
parser.add_argument(name="data", type=dict, action="append")
args = parser.parse_args()
def post(self):

args = request.get_json()
lexicon = args.get("data")

features=list(lexicon[0]["feats"].keys())
features = list(lexicon[0]["feats"].keys())
header = "\t".join(features)+"\tfrequence"
line_tsv=header+'\n'
line_tsv = header+'\n'

for item in lexicon:
line_tsv += "\t".join(str(value) for key, value in item["feats"].items())
Expand Down
30 changes: 0 additions & 30 deletions app/lexicon/interface.py

This file was deleted.

13 changes: 0 additions & 13 deletions app/lexicon/schema.py

This file was deleted.

17 changes: 17 additions & 0 deletions app/utils/grew_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ def get_relation_table(project_id: str, sample_ids, user_type, other_user):
)
return reply

@staticmethod
def get_lexicon(project_name: str, sample_ids, user_type, other_user, prune, features):

user_ids = GrewService.get_user_ids(user_type, other_user)
prune = (None, prune) [prune != 0]
reply = grew_request(
"getLexicon",
data={
"project_id": project_name,
"sample_ids": json.dumps(sample_ids),
"user_ids": json.dumps(user_ids),
"features": json.dumps(features),
"prune":prune
},
)
return reply

@staticmethod
def get_user_ids(user_type: str, other_user: str):
if user_type == 'user':
Expand Down

0 comments on commit 73eaadd

Please sign in to comment.