renamed module
This commit is contained in:
@@ -5,8 +5,8 @@ from pathlib import Path
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from shared.data_store import connect_minio
|
from shared.data_store import connect_minio
|
||||||
from shared.database import connect_mongodb
|
from shared.mongodb import connect_mongodb
|
||||||
from shared.database.classes import VisualCommunication
|
from shared.mongodb.classes import VisualCommunication
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from shared.data_store import connect_minio
|
from shared.data_store import connect_minio
|
||||||
from shared.database import connect_mongodb, get_visual_communication
|
from shared.mongodb import connect_mongodb, get_visual_communication
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ from pathlib import Path
|
|||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from shared.database import connect_mongodb
|
from shared.mongodb import connect_mongodb
|
||||||
from shared.database.classes import VisualCommunication
|
from shared.mongodb.classes import VisualCommunication
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# prepare env vars
|
# prepare env vars
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ from dotenv import load_dotenv
|
|||||||
from pymongo.errors import DuplicateKeyError
|
from pymongo.errors import DuplicateKeyError
|
||||||
|
|
||||||
from shared.data_store import connect_minio
|
from shared.data_store import connect_minio
|
||||||
from shared.database import connect_mongodb
|
from shared.mongodb import connect_mongodb
|
||||||
from shared.database.classes import VisualCommunication
|
from shared.mongodb.classes import VisualCommunication
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from dotenv import load_dotenv
|
|||||||
from pymongo.errors import DuplicateKeyError
|
from pymongo.errors import DuplicateKeyError
|
||||||
|
|
||||||
from shared.data_store import connect_minio
|
from shared.data_store import connect_minio
|
||||||
from shared.database import VisualCommunication, connect_mongodb
|
from shared.mongodb import VisualCommunication, connect_mongodb
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ from pathlib import Path
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from shared.data_store import connect_minio
|
from shared.data_store import connect_minio
|
||||||
from shared.database import connect_mongodb, upsert_prediction
|
from shared.mongodb import connect_mongodb, upsert_prediction
|
||||||
from shared.database.classes import VisualCommunication
|
from shared.mongodb.classes import VisualCommunication
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from shared.database import connect_mongodb, count_documents
|
from shared.mongodb import connect_mongodb, count_documents
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# prepare env vars
|
# prepare env vars
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from shared.database import connect_mongodb, count_documents
|
from shared.mongodb import connect_mongodb, count_documents
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# prepare env vars
|
# prepare env vars
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from dotenv import load_dotenv
|
|||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
from shared.data_store import connect_minio, put
|
from shared.data_store import connect_minio, put
|
||||||
from shared.database import VisualCommunication, connect_mongodb
|
from shared.mongodb import VisualCommunication, connect_mongodb
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
from web_ui.src.main import NECESSARY_ENV_VAR_LIST
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
"""Database classes module content."""
|
"""Database classes module content."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from .dataset import Dataset
|
from .dataset import Dataset
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
"""Definition of database Dataset class."""
|
"""Definition of database Dataset class."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
from datetime import datetime
|
from datetime import UTC, datetime
|
||||||
from datetime import UTC
|
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, Field
|
||||||
from pydantic import Field
|
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
|
|
||||||
class Dataset(BaseModel):
|
class Dataset(BaseModel):
|
||||||
"""Database Dataset model."""
|
"""Database Dataset model."""
|
||||||
|
|
||||||
create_time: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
create_time: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
||||||
train_names: list[str]
|
train_names: list[str]
|
||||||
test_names: list[str]
|
test_names: list[str]
|
||||||
@@ -44,13 +44,9 @@ class Dataset(BaseModel):
|
|||||||
num_test = round(num_total * fraction_map['test'])
|
num_test = round(num_total * fraction_map['test'])
|
||||||
# split data
|
# split data
|
||||||
validation_name_list = random.choices(name_list, k=num_validation)
|
validation_name_list = random.choices(name_list, k=num_validation)
|
||||||
name_list = [
|
name_list = [name for name in name_list if name not in validation_name_list]
|
||||||
name for name in name_list if name not in validation_name_list
|
|
||||||
]
|
|
||||||
test_name_list = random.choices(name_list, k=num_test)
|
test_name_list = random.choices(name_list, k=num_test)
|
||||||
train_name_list = [
|
train_name_list = [name for name in name_list if name not in test_name_list]
|
||||||
name for name in name_list if name not in test_name_list
|
|
||||||
]
|
|
||||||
# instantiate object
|
# instantiate object
|
||||||
dataset = Dataset(
|
dataset = Dataset(
|
||||||
train_names=train_name_list,
|
train_names=train_name_list,
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
"""Definition of database exception."""
|
"""Definition of database exception."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
"""Definition of function to count documents in database."""
|
"""Definition of function to count documents in database."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
@@ -8,10 +9,8 @@ def count_documents(
|
|||||||
collection: Collection,
|
collection: Collection,
|
||||||
only_with_annotation: bool = False,
|
only_with_annotation: bool = False,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""
|
"""Get the total number of documents in database that matches the
|
||||||
Get the total number of documents
|
filters."""
|
||||||
in database that matches the filters.
|
|
||||||
"""
|
|
||||||
assert isinstance(collection, Collection)
|
assert isinstance(collection, Collection)
|
||||||
assert isinstance(only_with_annotation, bool)
|
assert isinstance(only_with_annotation, bool)
|
||||||
# build query
|
# build query
|
||||||
+5
-5
@@ -1,12 +1,12 @@
|
|||||||
"""Definition of function to get visual communication from database."""
|
"""Definition of function to get visual communication from database."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
from shared.database import NoDocumentFoundException
|
from shared.mongodb import NoDocumentFoundException, VisualCommunication
|
||||||
from shared.database import VisualCommunication
|
|
||||||
|
|
||||||
|
|
||||||
def get_visual_communication(
|
def get_visual_communication(
|
||||||
@@ -22,16 +22,16 @@ def get_visual_communication(
|
|||||||
data = collection.aggregate(
|
data = collection.aggregate(
|
||||||
pipeline=[
|
pipeline=[
|
||||||
{
|
{
|
||||||
'$match': query, # find using filters
|
'$match': query, # find using filters
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'$sample': {
|
'$sample': {
|
||||||
'size': 1, # get one random
|
'size': 1, # get one random
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
data_list = list(data) # read data from cursor object
|
data_list = list(data) # read data from cursor object
|
||||||
if len(data_list) == 0:
|
if len(data_list) == 0:
|
||||||
raise NoDocumentFoundException()
|
raise NoDocumentFoundException()
|
||||||
vis_com = VisualCommunication.model_validate(data_list[0])
|
vis_com = VisualCommunication.model_validate(data_list[0])
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
"""
|
"""Definition of function to list names of all visual communication documents
|
||||||
Definition of function to list names
|
in database."""
|
||||||
of all visual communication documents in database.
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
@@ -4,7 +4,7 @@ import logging
|
|||||||
|
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
from shared.database import Dataset
|
from shared.mongodb import Dataset
|
||||||
|
|
||||||
|
|
||||||
def save_dataset(
|
def save_dataset(
|
||||||
@@ -22,7 +22,7 @@ if __name__ == '__main__':
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
load_dotenv('local.env')
|
load_dotenv('local.env')
|
||||||
from shared.database import connect_mongodb, list_names
|
from shared.mongodb import connect_mongodb, list_names
|
||||||
|
|
||||||
# connect to database
|
# connect to database
|
||||||
collection, db, client = connect_mongodb()
|
collection, db, client = connect_mongodb()
|
||||||
+4
-8
@@ -2,22 +2,18 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
from shared.database import VisualCommunication
|
from shared.mongodb import VisualCommunication
|
||||||
|
|
||||||
|
|
||||||
def upsert_visual_communication(
|
def upsert_visual_communication(
|
||||||
collection: Collection,
|
collection: Collection,
|
||||||
visual_communication_list: list[VisualCommunication],
|
visual_communication_list: list[VisualCommunication],
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""Upsert VisualCommunication object in the database.
|
||||||
Upsert VisualCommunication object in the database.
|
|
||||||
Returns bool stating success.
|
Returns bool stating success.
|
||||||
"""
|
"""
|
||||||
response = collection.insert_many(
|
response = collection.insert_many(
|
||||||
[
|
[vis_com.model_dump() for vis_com in visual_communication_list],
|
||||||
vis_com.model_dump()
|
|
||||||
for vis_com
|
|
||||||
in visual_communication_list
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
return response.acknowledged
|
return response.acknowledged
|
||||||
+24
-35
@@ -1,28 +1,28 @@
|
|||||||
"""Definition of init_app function."""
|
"""Definition of init_app function."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import dash_bootstrap_components as dbc
|
import dash_bootstrap_components as dbc
|
||||||
from dash import ALL
|
from dash import ALL, Dash, Input, Output, State
|
||||||
from dash import Dash
|
|
||||||
from dash import Input
|
|
||||||
from dash import Output
|
|
||||||
from dash import State
|
|
||||||
from dash_auth import BasicAuth
|
from dash_auth import BasicAuth
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
from pymongo.collection import Collection
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
from .layout import app_layout
|
|
||||||
from shared.data_store import delete as delete_from_minio
|
from shared.data_store import delete as delete_from_minio
|
||||||
from shared.database import count_documents
|
|
||||||
from shared.database import get_visual_communication
|
|
||||||
from shared.database import NoDocumentFoundException
|
|
||||||
from shared.database import upsert_annotation
|
|
||||||
from shared.database import VisualCommunication
|
|
||||||
from shared.dto import ModelData
|
from shared.dto import ModelData
|
||||||
|
from shared.mongodb import (
|
||||||
|
NoDocumentFoundException,
|
||||||
|
VisualCommunication,
|
||||||
|
count_documents,
|
||||||
|
get_visual_communication,
|
||||||
|
upsert_annotation,
|
||||||
|
)
|
||||||
|
|
||||||
|
from .layout import app_layout
|
||||||
|
|
||||||
|
|
||||||
def init_app(
|
def init_app(
|
||||||
@@ -101,10 +101,8 @@ def init_app(
|
|||||||
collection=mongo_collection,
|
collection=mongo_collection,
|
||||||
only_with_annotation=True,
|
only_with_annotation=True,
|
||||||
)
|
)
|
||||||
limit = int(num_total/20)
|
limit = int(num_total / 20)
|
||||||
label_str = f"{
|
label_str = f'{num_handled}/{num_total}' if num_handled >= limit else ''
|
||||||
num_handled
|
|
||||||
}/{num_total}" if num_handled >= limit else ''
|
|
||||||
return num_handled, num_total, label_str
|
return num_handled, num_total, label_str
|
||||||
|
|
||||||
# define callback: upload image
|
# define callback: upload image
|
||||||
@@ -122,9 +120,7 @@ def init_app(
|
|||||||
"""Upload image to database through web ui."""
|
"""Upload image to database through web ui."""
|
||||||
try:
|
try:
|
||||||
# stop if no input
|
# stop if no input
|
||||||
assert (
|
assert (content_list is not None) and (
|
||||||
content_list is not None
|
|
||||||
) and (
|
|
||||||
filename_list is not None
|
filename_list is not None
|
||||||
), 'nothing to upload'
|
), 'nothing to upload'
|
||||||
# handle input
|
# handle input
|
||||||
@@ -159,7 +155,9 @@ def init_app(
|
|||||||
client=minio_client,
|
client=minio_client,
|
||||||
object_name=vis_com.object_name,
|
object_name=vis_com.object_name,
|
||||||
)
|
)
|
||||||
assert len(failed_filename_list) == 0, f"failed uploading:{
|
assert (
|
||||||
|
len(failed_filename_list) == 0
|
||||||
|
), f"failed uploading:{
|
||||||
'\n'.join(failed_filename_list)
|
'\n'.join(failed_filename_list)
|
||||||
}"
|
}"
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
@@ -208,30 +206,19 @@ def init_app(
|
|||||||
logging.info('saving annotations to database: %s', vis_com_name)
|
logging.info('saving annotations to database: %s', vis_com_name)
|
||||||
try:
|
try:
|
||||||
# extract option keys
|
# extract option keys
|
||||||
annotation_keys = [
|
annotation_keys = [elem['index'] for elem in annotation_keys]
|
||||||
elem['index']
|
|
||||||
for elem in annotation_keys
|
|
||||||
]
|
|
||||||
# ensure all options are set
|
# ensure all options are set
|
||||||
logging.info(annotation_keys)
|
logging.info(annotation_keys)
|
||||||
for option, value in zip(annotation_keys, annotation_values):
|
for option, value in zip(annotation_keys, annotation_values):
|
||||||
if value is None:
|
if value is None:
|
||||||
raise ValueError(f"{option} is not set")
|
raise ValueError(f"{option} is not set")
|
||||||
# prepare data to save
|
# prepare data to save
|
||||||
annotation_keys = [
|
annotation_keys = [elem.replace(' ', '_') for elem in annotation_keys]
|
||||||
elem.replace(' ', '_')
|
|
||||||
for elem
|
|
||||||
in annotation_keys
|
|
||||||
]
|
|
||||||
annotation_values = [
|
annotation_values = [
|
||||||
elem.replace(' ', '_').lower()
|
elem.replace(' ', '_').lower() for elem in annotation_values
|
||||||
for elem
|
|
||||||
in annotation_values
|
|
||||||
]
|
]
|
||||||
annotation_map = {
|
annotation_map = {
|
||||||
key: value
|
key: value for key, value in zip(annotation_keys, annotation_values)
|
||||||
for key, value
|
|
||||||
in zip(annotation_keys, annotation_values)
|
|
||||||
}
|
}
|
||||||
# instantiate ModelOutputs object
|
# instantiate ModelOutputs object
|
||||||
annotations = ModelData.from_annotations(**annotation_map)
|
annotations = ModelData.from_annotations(**annotation_map)
|
||||||
@@ -273,8 +260,10 @@ def init_app(
|
|||||||
response[2] = image_src
|
response[2] = image_src
|
||||||
response[3] = annotation_values
|
response[3] = annotation_values
|
||||||
logging.info(
|
logging.info(
|
||||||
'finished getting visual communication: %s', vis_com_name,
|
'finished getting visual communication: %s',
|
||||||
|
vis_com_name,
|
||||||
)
|
)
|
||||||
return tuple(response)
|
return tuple(response)
|
||||||
|
|
||||||
logging.info('initialised app')
|
logging.info('initialised app')
|
||||||
return app
|
return app
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from shared.data_store import connect_minio
|
from shared.data_store import connect_minio
|
||||||
from shared.database import connect_mongodb
|
from shared.mongodb import connect_mongodb
|
||||||
from shared.utils import check_env, setup_logging
|
from shared.utils import check_env, setup_logging
|
||||||
|
|
||||||
from .app import init_app
|
from .app import init_app
|
||||||
|
|||||||
Reference in New Issue
Block a user