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