added function to upsert visual communiation data
This commit is contained in:
+52
-33
@@ -1,57 +1,58 @@
|
|||||||
from pymongo.collection import Collection
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .classes import (
|
from pymongo.collection import Collection
|
||||||
VisualCommunication,
|
|
||||||
NoDocumentFoundException,
|
from .classes import ModelOutputs
|
||||||
ModelOutputs
|
from .classes import NoDocumentFoundException
|
||||||
)
|
from .classes import VisualCommunication
|
||||||
|
|
||||||
|
|
||||||
def total_documents(
|
def total_documents(
|
||||||
collection: Collection
|
collection: Collection,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Get total number of documents in database."""
|
"""Get total number of documents in database."""
|
||||||
return collection.count_documents(filter={})
|
return collection.count_documents(filter={})
|
||||||
|
|
||||||
|
|
||||||
def total_annotated(
|
def total_annotated(
|
||||||
collection: Collection
|
collection: Collection,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Get total number of annotated documents in database."""
|
"""Get total number of annotated documents in database."""
|
||||||
query = {
|
query = {
|
||||||
"annotation": {
|
'annotation': {
|
||||||
"$ne": None
|
'$ne': None,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
return collection.count_documents(filter=query)
|
return collection.count_documents(filter=query)
|
||||||
|
|
||||||
|
|
||||||
def get_visual_communication(
|
def get_visual_communication(
|
||||||
collection: Collection,
|
collection: Collection,
|
||||||
with_annotation: bool = False
|
with_annotation: bool = False,
|
||||||
) -> VisualCommunication:
|
) -> VisualCommunication:
|
||||||
"""Get a random visual communication from the database."""
|
"""Get a random visual communication from the database."""
|
||||||
query = {}
|
query = {}
|
||||||
if with_annotation:
|
if with_annotation:
|
||||||
query["annotation"] = {"$ne": None}
|
query['annotation'] = {'$ne': None}
|
||||||
else:
|
else:
|
||||||
query["annotation"] = {"$eq": None}
|
query['annotation'] = {'$eq': None}
|
||||||
data = collection.aggregate([
|
data = collection.aggregate([
|
||||||
{
|
{
|
||||||
"$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:
|
||||||
logging.error("failed getting visual communication")
|
logging.error('failed getting visual communication')
|
||||||
raise NoDocumentFoundException()
|
raise NoDocumentFoundException()
|
||||||
logging.info("finished")
|
logging.info('finished')
|
||||||
return VisualCommunication.model_validate(data_list[0])
|
return VisualCommunication.model_validate(data_list[0])
|
||||||
|
|
||||||
|
|
||||||
@@ -62,20 +63,20 @@ def upsert_predictions(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Upsert prediction data in the database."""
|
"""Upsert prediction data in the database."""
|
||||||
query = {
|
query = {
|
||||||
"name": vis_com_name
|
'name': vis_com_name,
|
||||||
}
|
}
|
||||||
update = {
|
update = {
|
||||||
"$set": {
|
'$set': {
|
||||||
"prediction": predictions.model_dump()
|
'prediction': predictions.model_dump(),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
res = collection.update_one(
|
res = collection.update_one(
|
||||||
filter=query,
|
filter=query,
|
||||||
update=update,
|
update=update,
|
||||||
upsert=True,
|
upsert=True,
|
||||||
)
|
)
|
||||||
logging.debug("upserted document: %s", res)
|
logging.debug('upserted document: %s', res)
|
||||||
logging.info("finished")
|
logging.info('finished')
|
||||||
|
|
||||||
|
|
||||||
def upsert_annotations(
|
def upsert_annotations(
|
||||||
@@ -85,17 +86,35 @@ def upsert_annotations(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Upserts annotation data in the database."""
|
"""Upserts annotation data in the database."""
|
||||||
query = {
|
query = {
|
||||||
"name": vis_com_name
|
'name': vis_com_name,
|
||||||
}
|
}
|
||||||
update = {
|
update = {
|
||||||
"$set": {
|
'$set': {
|
||||||
"annotation": annotations.model_dump()
|
'annotation': annotations.model_dump(),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
res = collection.update_one(
|
res = collection.update_one(
|
||||||
filter=query,
|
filter=query,
|
||||||
update=update,
|
update=update,
|
||||||
upsert=True,
|
upsert=True,
|
||||||
)
|
)
|
||||||
logging.info("upserted document: %s", res)
|
logging.info('upserted document: %s', res)
|
||||||
logging.info("finished")
|
logging.info('finished')
|
||||||
|
|
||||||
|
|
||||||
|
def upsert_visual_communication(
|
||||||
|
collection: Collection,
|
||||||
|
visual_communication_list: list[VisualCommunication],
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
],
|
||||||
|
)
|
||||||
|
return response.acknowledged
|
||||||
|
|||||||
Reference in New Issue
Block a user