-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
misc.py
80 lines (62 loc) · 2.34 KB
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import random
import re
from pathlib import Path
from typing import Union
from flask import current_app
from asreview.utils import asreview_path
def current_app_is_authenticated():
return current_app.config.get("AUTHENTICATION_ENABLED")
def get_project_id(project):
"""Get a project id from either a Project model
(authenticated app) or an ASReviewProject object
(unauthenticated app)."""
id = None
if current_app_is_authenticated():
id = project.project_id
else:
id = project.config["id"]
return id
def read_project_file(project):
"""Loads the data from the project.json file."""
id = get_project_id(project)
with open(asreview_path() / id / "project.json", "r") as f:
data = json.load(f)
return data
def manipulate_project_file(project, key, value):
"""Updates key value pairs in the project.json file."""
id = get_project_id(project)
data = read_project_file(project)
data[key] = value
with open(asreview_path() / id / "project.json", "w+") as f:
json.dump(data, f)
return True
return False
def _extract_stem(path: Union[str, Path]):
"""Extracts a stem from a path or URL containing a filename."""
return Path(re.split(":|/", str(path))[-1]).stem
def extract_filename_stem(upload_data):
"""Helper function to get the stem part of a filename from a
Path or URL contaning a filename."""
# upload data is a dict with a single key value pair
value = list(upload_data.values())[0]
# split this value on either / or :
return _extract_stem(value)
def choose_project_algorithms():
"""Randomly chooses a model plus the appropriate feature
extraction, query strategy and balance strategy."""
model = random.choice(["svm", "nb", "logistic"])
feature_extraction = random.choice(["tfidf"])
data = {
"model": model,
"feature_extraction": feature_extraction,
"query_strategy": random.choice(
["cluster", "max", "max_random", "max_uncertainty", "random", "uncertainty"]
),
"balance_strategy": random.choice(["double", "simple", "undersample"]),
}
return data
def get_folders_in_asreview_path():
"""This function returns the amount of folders located
in the asreview folder."""
return [f for f in asreview_path().glob("*") if f.is_dir()]