pre-commit renamed files to PEP standard and removed unused imports
pipeline / Test (push) Failing after 51s

This commit is contained in:
Brian Bjarke Jensen
2024-02-25 14:26:54 +01:00
parent ea28f23304
commit ce063822b5
12 changed files with 230 additions and 215 deletions
+40 -42
View File
@@ -1,29 +1,27 @@
from __future__ import annotations from __future__ import annotations
from pydantic import BaseModel, field_validator, field_serializer
from PIL import Image import logging
from base64 import b64encode
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from base64 import b64encode
import logging
from typing import List
from src.model_experiential import ( from model_experiential import (
VisualSyntaxModelOutput VisualSyntaxModelOutput,
)
from src.model_interpersonal import (
ContactModelOutput,
AngleModelOutput,
PointOfViewModelOutput,
DistanceModelOutput,
ModalityLightingModelOutput,
ModalityColorModelOutput,
ModalityDepthModelOutput
)
from src.model_textual import (
InformationValueModelOutput,
FramingModelOutput,
SalienceModelOutput
) )
from model_interpersonal import AngleModelOutput
from model_interpersonal import ContactModelOutput
from model_interpersonal import DistanceModelOutput
from model_interpersonal import ModalityColorModelOutput
from model_interpersonal import ModalityDepthModelOutput
from model_interpersonal import ModalityLightingModelOutput
from model_interpersonal import PointOfViewModelOutput
from model_textual import FramingModelOutput
from model_textual import InformationValueModelOutput
from model_textual import SalienceModelOutput
from PIL import Image
from pydantic import BaseModel
from pydantic import field_serializer
from pydantic import field_validator
class NoDocumentFoundException(Exception): class NoDocumentFoundException(Exception):
@@ -44,7 +42,7 @@ class ModelOutputs(BaseModel):
salience: SalienceModelOutput salience: SalienceModelOutput
@classmethod @classmethod
def list_fields(cls) -> List[str]: def list_fields(cls) -> list[str]:
"""List options that are stored as attributes.""" """List options that are stored as attributes."""
return list(cls.model_fields.keys()) return list(cls.model_fields.keys())
@@ -71,32 +69,32 @@ class ModelOutputs(BaseModel):
modality_depth: str, modality_depth: str,
information_value: str, information_value: str,
framing: str, framing: str,
salience: str salience: str,
) -> ModelOutputs: ) -> ModelOutputs:
"""Instantiate from annotation.""" """Instantiate from annotation."""
kwargs = { kwargs = {
"visual_syntax": VisualSyntaxModelOutput 'visual_syntax': VisualSyntaxModelOutput
.from_choice(visual_syntax), .from_choice(visual_syntax),
"contact": ContactModelOutput 'contact': ContactModelOutput
.from_choice(contact), .from_choice(contact),
"angle": AngleModelOutput 'angle': AngleModelOutput
.from_choice(angle), .from_choice(angle),
"point_of_view": PointOfViewModelOutput 'point_of_view': PointOfViewModelOutput
.from_choice(point_of_view), .from_choice(point_of_view),
"distance": DistanceModelOutput 'distance': DistanceModelOutput
.from_choice(distance), .from_choice(distance),
"modality_lighting": ModalityLightingModelOutput 'modality_lighting': ModalityLightingModelOutput
.from_choice(modality_lighting), .from_choice(modality_lighting),
"modality_color": ModalityColorModelOutput 'modality_color': ModalityColorModelOutput
.from_choice(modality_color), .from_choice(modality_color),
"modality_depth": ModalityDepthModelOutput 'modality_depth': ModalityDepthModelOutput
.from_choice(modality_depth), .from_choice(modality_depth),
"information_value": InformationValueModelOutput 'information_value': InformationValueModelOutput
.from_choice(information_value), .from_choice(information_value),
"framing": FramingModelOutput 'framing': FramingModelOutput
.from_choice(framing), .from_choice(framing),
"salience": SalienceModelOutput 'salience': SalienceModelOutput
.from_choice(salience) .from_choice(salience),
} }
return cls(**kwargs) return cls(**kwargs)
@@ -123,17 +121,17 @@ class VisualCommunication(BaseModel):
image.load() image.load()
return VisualCommunication(name=name, image=image) return VisualCommunication(name=name, image=image)
@field_serializer("image") @field_serializer('image')
def serialize_image(image: Image.Image) -> bytes: # type: ignore def serialize_image(image: Image.Image) -> bytes: # type: ignore
buffer = BytesIO() buffer = BytesIO()
image.save(buffer, format="JPEG") image.save(buffer, format='JPEG')
return buffer.getvalue() return buffer.getvalue()
@field_validator("image", mode="before") @field_validator('image', mode='before')
@classmethod @classmethod
def convert_to_image( def convert_to_image(
cls, cls,
image: Image.Image | BytesIO | bytes image: Image.Image | BytesIO | bytes,
) -> Image.Image: ) -> Image.Image:
if isinstance(image, bytes): if isinstance(image, bytes):
image = BytesIO(image) image = BytesIO(image)
@@ -148,12 +146,12 @@ class VisualCommunication(BaseModel):
"""Convert image to be displayed on webpage.""" """Convert image to be displayed on webpage."""
# convert images to bytes string # convert images to bytes string
buffer = BytesIO() buffer = BytesIO()
self.image.save(buffer, format="png") self.image.save(buffer, format='png')
img_enc = b64encode(buffer.getvalue()).decode("utf-8") img_enc = b64encode(buffer.getvalue()).decode('utf-8')
return f"data:image/png;base64, {img_enc}" return f"data:image/png;base64, {img_enc}"
def generate_random_prediction(self, force: bool = False) -> None: def generate_random_prediction(self, force: bool = False) -> None:
"""Generate random prediction values.""" """Generate random prediction values."""
if not force and self.prediction is not None: if not force and self.prediction is not None:
logging.warning("set force=True to overwrite existing values.") logging.warning('set force=True to overwrite existing values.')
self.prediction = ModelOutputs.from_random() self.prediction = ModelOutputs.from_random()
+19 -17
View File
@@ -1,23 +1,25 @@
import logging from __future__ import annotations
from dotenv import load_dotenv
from pathlib import Path
import os
from src.web import app import logging
import os
from pathlib import Path
from dotenv import load_dotenv
from web import app
# prepare optional local setup # prepare optional local setup
env_path = Path(__file__).parent.parent / "local.env" env_path = Path(__file__).parent.parent / 'local.env'
load_dotenv(env_path) load_dotenv(env_path)
# ensure env vars set # ensure env vars set
necesasary_var_list = { necesasary_var_list = {
"MONGO_HOST", 'MONGO_HOST',
"MONGO_DB", 'MONGO_DB',
"MONGO_COLLECTION", 'MONGO_COLLECTION',
"MONGO_USER", 'MONGO_USER',
"MONGO_PASSWORD", 'MONGO_PASSWORD',
"DASH_AUTH_USERNAME", 'DASH_AUTH_USERNAME',
"DASH_AUTH_PASSWORD" 'DASH_AUTH_PASSWORD',
} }
for env_var in necesasary_var_list: for env_var in necesasary_var_list:
# ensure env var set # ensure env var set
@@ -38,12 +40,12 @@ fmt = (
datefmt = '%Y-%m-%d %H:%M:%S' datefmt = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO) logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO)
logging.info("initialized app") logging.info('initialized app')
server = app.server server = app.server
if __name__ == "__main__": if __name__ == '__main__':
# prepare local env vars # prepare local env vars
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# run app # run app
app.run(debug=True) app.run(debug=True)
logging.info("started app") logging.info('started app')
+46 -42
View File
@@ -1,29 +1,33 @@
from dash import Dash, Input, Output, State, ALL from __future__ import annotations
import dash_bootstrap_components as dbc
from dash_auth import BasicAuth
import logging import logging
from typing import List
from pydantic import ValidationError
import os 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_auth import BasicAuth
from database import connect
from database import get_visual_communication
from database import ModelOutputs
from database import NoDocumentFoundException
from database import upsert_annotations
from pydantic import ValidationError
from .layout import app_layout from .layout import app_layout
from src.database import (
connect,
get_visual_communication,
NoDocumentFoundException,
upsert_annotations,
ModelOutputs
)
# setup app # setup app
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = "visual critical discourse analysis".title() app.title = 'visual critical discourse analysis'.title()
app.layout = app_layout app.layout = app_layout
server = app.server server = app.server
# setup authentication # setup authentication
AUTH_DICT = { AUTH_DICT = {
os.getenv("DASH_AUTH_USERNAME"): os.getenv("DASH_AUTH_PASSWORD") os.getenv('DASH_AUTH_USERNAME'): os.getenv('DASH_AUTH_PASSWORD'),
} }
BasicAuth(app, AUTH_DICT) BasicAuth(app, AUTH_DICT)
@@ -33,58 +37,58 @@ collection, db, client = connect()
# define callbacks # define callbacks
@app.callback( @app.callback(
Output("alert-element", "is_open"), Output('alert-element', 'is_open'),
Output("alert-element", "children"), Output('alert-element', 'children'),
Input("alert-message", "data") Input('alert-message', 'data'),
) )
def show_alert( def show_alert(
msg: str | None msg: str | None,
): ):
if msg is None or msg == "": if msg is None or msg == '':
return False, "" return False, ''
logging.info(f"updated alert message: {msg}") logging.info(f"updated alert message: {msg}")
return True, msg return True, msg
@app.callback( @app.callback(
Output("alert-message", "data"), Output('alert-message', 'data'),
Output("vis-com-name", "data"), Output('vis-com-name', 'data'),
Output("image-container", "src"), Output('image-container', 'src'),
Output({"type": "annotation", "index": ALL}, "value"), Output({'type': 'annotation', 'index': ALL}, 'value'),
Input("next-button", "n_clicks"), Input('next-button', 'n_clicks'),
State("vis-com-name", "data"), State('vis-com-name', 'data'),
State("image-container", "src"), State('image-container', 'src'),
State({"type": "annotation", "index": ALL}, "id"), State({'type': 'annotation', 'index': ALL}, 'id'),
State({"type": "annotation", "index": ALL}, "value"), State({'type': 'annotation', 'index': ALL}, 'value'),
prevent_initial_call=True, prevent_initial_call=True,
) )
def cycle_visual_communication_data( def cycle_visual_communication_data(
n_clicks: int, n_clicks: int,
vis_com_name: str, vis_com_name: str,
image_src: str, image_src: str,
annotation_keys: List, annotation_keys: list,
annotation_values: List, annotation_values: list,
): ):
logging.info("began cycling visual communication data") logging.info('began cycling visual communication data')
global collection global collection
# prepare default response # prepare default response
response = [ response = [
"", '',
vis_com_name, vis_com_name,
image_src, image_src,
annotation_values annotation_values,
] ]
# check if next-button clicked # check if next-button clicked
if n_clicks == 0: if n_clicks == 0:
logging.info("stopping early: next-button has not yet been clicked") logging.info('stopping early: next-button has not yet been clicked')
return response return response
# check if visual communication name is set # check if visual communication name is set
if len(vis_com_name) > 0: if len(vis_com_name) > 0:
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"] elem['index']
for elem in annotation_keys for elem in annotation_keys
] ]
# ensure all options are set # ensure all options are set
@@ -114,7 +118,7 @@ def cycle_visual_communication_data(
upsert_annotations( upsert_annotations(
collection=collection, collection=collection,
vis_com_name=vis_com_name, vis_com_name=vis_com_name,
annotations=annotations annotations=annotations,
) )
except (ValueError, ValidationError) as exc: except (ValueError, ValidationError) as exc:
msg = f"failed saving annotation: {exc}" msg = f"failed saving annotation: {exc}"
@@ -122,12 +126,12 @@ def cycle_visual_communication_data(
response[0] = msg response[0] = msg
return tuple(response) return tuple(response)
# get new visual communication # get new visual communication
logging.info("trying to get new visual communication") logging.info('trying to get new visual communication')
try: try:
# get data # get data
vis_com = get_visual_communication( vis_com = get_visual_communication(
collection=collection, collection=collection,
with_annotation=False with_annotation=False,
) )
# set variables # set variables
vis_com_name = vis_com.name vis_com_name = vis_com.name
@@ -139,7 +143,7 @@ def cycle_visual_communication_data(
# reset annotations # reset annotations
annotation_values = [None for elem in annotation_values] annotation_values = [None for elem in annotation_values]
except NoDocumentFoundException: except NoDocumentFoundException:
msg = "no unannotated data in database" msg = 'no unannotated data in database'
logging.warning(msg) logging.warning(msg)
response[0] = msg response[0] = msg
return tuple(response) return tuple(response)
@@ -147,5 +151,5 @@ def cycle_visual_communication_data(
response[1] = vis_com_name response[1] = vis_com_name
response[2] = image_src response[2] = image_src
response[3] = annotation_values response[3] = annotation_values
logging.info("finished getting visual communication: %s", vis_com_name) logging.info('finished getting visual communication: %s', vis_com_name)
return tuple(response) return tuple(response)
+47 -50
View File
@@ -1,27 +1,24 @@
from dash import html, dcc from __future__ import annotations
import dash_mantine_components as dmc import dash_mantine_components as dmc
from typing import List from dash import dcc
from dash import html
from src.model_experiential import ( from model_experiential import (
VisualSyntaxModelOutput VisualSyntaxModelOutput,
)
from src.model_interpersonal import (
ContactModelOutput,
AngleModelOutput,
PointOfViewModelOutput,
DistanceModelOutput,
ModalityLightingModelOutput,
ModalityColorModelOutput,
ModalityDepthModelOutput
)
from src.model_textual import (
InformationValueModelOutput,
FramingModelOutput,
SalienceModelOutput
) )
from model_interpersonal import AngleModelOutput
from model_interpersonal import ContactModelOutput
from model_interpersonal import DistanceModelOutput
from model_interpersonal import ModalityColorModelOutput
from model_interpersonal import ModalityDepthModelOutput
from model_interpersonal import ModalityLightingModelOutput
from model_interpersonal import PointOfViewModelOutput
from model_textual import FramingModelOutput
from model_textual import InformationValueModelOutput
from model_textual import SalienceModelOutput
def generate_option_labels(model) -> List[str]: def generate_option_labels(model) -> list[str]:
"""Generate presentable list of attributes from an OutputModel.""" """Generate presentable list of attributes from an OutputModel."""
labels = [ labels = [
label.replace('_', ' ').title() label.replace('_', ' ').title()
@@ -34,8 +31,8 @@ def generate_visual_syntax_options_map():
"""Generate map of titles and options for visual syntax labels.""" """Generate map of titles and options for visual syntax labels."""
options_map = {} options_map = {}
# add experiential labels # add experiential labels
options_map["visual syntax"] = generate_option_labels( options_map['visual syntax'] = generate_option_labels(
VisualSyntaxModelOutput VisualSyntaxModelOutput,
) )
return options_map return options_map
@@ -44,20 +41,20 @@ def generate_interpersonal_options_map():
"""Generate map of titles and options for interpersonal labels.""" """Generate map of titles and options for interpersonal labels."""
options_map = {} options_map = {}
# add interpersonal labels # add interpersonal labels
options_map["contact"] = generate_option_labels(ContactModelOutput) options_map['contact'] = generate_option_labels(ContactModelOutput)
options_map["angle"] = generate_option_labels(AngleModelOutput) options_map['angle'] = generate_option_labels(AngleModelOutput)
options_map["point of view"] = generate_option_labels( options_map['point of view'] = generate_option_labels(
PointOfViewModelOutput PointOfViewModelOutput,
) )
options_map["distance"] = generate_option_labels(DistanceModelOutput) options_map['distance'] = generate_option_labels(DistanceModelOutput)
options_map["modality lighting"] = generate_option_labels( options_map['modality lighting'] = generate_option_labels(
ModalityLightingModelOutput ModalityLightingModelOutput,
) )
options_map["modality color"] = generate_option_labels( options_map['modality color'] = generate_option_labels(
ModalityColorModelOutput ModalityColorModelOutput,
) )
options_map["modality depth"] = generate_option_labels( options_map['modality depth'] = generate_option_labels(
ModalityDepthModelOutput ModalityDepthModelOutput,
) )
return options_map return options_map
@@ -66,11 +63,11 @@ def generate_textual_options_map():
"""Generate map of titles and options for textual labels.""" """Generate map of titles and options for textual labels."""
options_map = {} options_map = {}
# add textual labels # add textual labels
options_map["information value"] = generate_option_labels( options_map['information value'] = generate_option_labels(
InformationValueModelOutput InformationValueModelOutput,
) )
options_map["framing"] = generate_option_labels(FramingModelOutput) options_map['framing'] = generate_option_labels(FramingModelOutput)
options_map["salience"] = generate_option_labels(SalienceModelOutput) options_map['salience'] = generate_option_labels(SalienceModelOutput)
return options_map return options_map
@@ -78,11 +75,11 @@ def generate_textual_options_map():
experiential_map = generate_visual_syntax_options_map() experiential_map = generate_visual_syntax_options_map()
experiential_container = dmc.Col( experiential_container = dmc.Col(
children=[ children=[
html.H4("experiential".title()), html.H4('experiential'.title()),
], span=5 ], span=5,
) )
for title, options in experiential_map.items(): for title, options in experiential_map.items():
id_dict = {"type": "annotation", "index": title.replace('_', '-')} id_dict = {'type': 'annotation', 'index': title.replace('_', '-')}
experiential_container.children.append( experiential_container.children.append(
dmc.Container([ dmc.Container([
html.B(title.title()), html.B(title.title()),
@@ -90,17 +87,17 @@ for title, options in experiential_map.items():
options=options, options=options,
id=id_dict, id=id_dict,
), ),
]) ]),
) )
# prepare interpersonal container # prepare interpersonal container
interpersonal_map = generate_interpersonal_options_map() interpersonal_map = generate_interpersonal_options_map()
interpersonal_container = dmc.Col( interpersonal_container = dmc.Col(
children=[ children=[
html.H4("interpersonal".title()), html.H4('interpersonal'.title()),
], span=3 ], span=3,
) )
for title, options in interpersonal_map.items(): for title, options in interpersonal_map.items():
id_dict = {"type": "annotation", "index": title.replace('_', '-')} id_dict = {'type': 'annotation', 'index': title.replace('_', '-')}
interpersonal_container.children.append( interpersonal_container.children.append(
dmc.Container([ dmc.Container([
html.B(title.title()), html.B(title.title()),
@@ -108,17 +105,17 @@ for title, options in interpersonal_map.items():
options=options, options=options,
id=id_dict, id=id_dict,
), ),
]) ]),
) )
# prepare textual container # prepare textual container
textual_map = generate_textual_options_map() textual_map = generate_textual_options_map()
textual_container = dmc.Col( textual_container = dmc.Col(
children=[ children=[
html.H4("textual".title()), html.H4('textual'.title()),
], span=4 ], span=4,
) )
for title, options in textual_map.items(): for title, options in textual_map.items():
id_dict = {"type": "annotation", "index": title.replace('_', '-')} id_dict = {'type': 'annotation', 'index': title.replace('_', '-')}
textual_container.children.append( textual_container.children.append(
dmc.Container([ dmc.Container([
html.B(title), html.B(title),
@@ -126,7 +123,7 @@ for title, options in textual_map.items():
options=options, options=options,
id=id_dict, id=id_dict,
), ),
]) ]),
) )
labels_element = dmc.Grid( labels_element = dmc.Grid(
@@ -134,5 +131,5 @@ labels_element = dmc.Grid(
experiential_container, experiential_container,
interpersonal_container, interpersonal_container,
textual_container, textual_container,
] ],
) )
@@ -1,13 +1,15 @@
from __future__ import annotations
from pathlib import Path from pathlib import Path
from src.database import VisualCommunication from database import VisualCommunication
if __name__ == "__main__": if __name__ == '__main__':
# get list of image paths # get list of image paths
test_dir = Path(__file__).parent test_dir = Path(__file__).parent
img_dir = test_dir / "imgs" img_dir = test_dir / 'imgs'
img_path_list = [path for path in img_dir.glob("*.jpeg") if path.is_file()] img_path_list = [path for path in img_dir.glob('*.jpeg') if path.is_file()]
print(img_path_list) print(img_path_list)
# instantiate data object # instantiate data object
vis_com_list = [ vis_com_list = [
@@ -16,6 +18,7 @@ if __name__ == "__main__":
in img_path_list in img_path_list
] ]
# generate random predictions # generate random predictions
[vis_com.generate_random_prediction() for vis_com in vis_com_list] for vis_com in vis_com_list:
vis_com.generate_random_prediction()
for vis_com in vis_com_list: for vis_com in vis_com_list:
print(vis_com) print(vis_com)
@@ -1,18 +1,18 @@
from pathlib import Path from __future__ import annotations
from dotenv import load_dotenv
import os import os
from pathlib import Path
from src.database import ( from database import connect
connect, from database import get_visual_communication
get_visual_communication from dotenv import load_dotenv
)
if __name__ == "__main__": if __name__ == '__main__':
# prepare env vars # prepare env vars
env_path = Path(__file__).parent.parent / "local.env" env_path = Path(__file__).parent.parent / 'local.env'
assert env_path.exists() assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# connect to database # connect to database
collection, db, client = connect() collection, db, client = connect()
print(client.server_info()) print(client.server_info())
@@ -1,15 +1,18 @@
from pathlib import Path from __future__ import annotations
from dotenv import load_dotenv
import os import os
from pathlib import Path
from src.database import VisualCommunication, connect from database import connect
from database import VisualCommunication
from dotenv import load_dotenv
if __name__ == "__main__": if __name__ == '__main__':
# prepare env vars # prepare env vars
env_path = Path(__file__).parent.parent / "local.env" env_path = Path(__file__).parent.parent / 'local.env'
assert env_path.exists() assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# connect to database # connect to database
collection, db, client = connect() collection, db, client = connect()
print(client.server_info()) print(client.server_info())
@@ -17,7 +20,7 @@ if __name__ == "__main__":
data = None data = None
for data in collection.find().limit(3): for data in collection.find().limit(3):
if data is None: if data is None:
print("no document found") print('no document found')
break break
vis_com: VisualCommunication = VisualCommunication.model_validate(data) vis_com: VisualCommunication = VisualCommunication.model_validate(data)
print(repr(vis_com)) print(repr(vis_com))
@@ -1,15 +1,18 @@
from __future__ import annotations
import os
from pathlib import Path from pathlib import Path
from database import connect
from database import VisualCommunication
from dotenv import load_dotenv from dotenv import load_dotenv
from pymongo.errors import DuplicateKeyError from pymongo.errors import DuplicateKeyError
import os
from src.database import VisualCommunication, connect if __name__ == '__main__':
if __name__ == "__main__":
# get list of image paths # get list of image paths
test_dir = Path(__file__).parent test_dir = Path(__file__).parent
img_dir = test_dir / "imgs" img_dir = test_dir / 'imgs'
img_path_list = [path for path in img_dir.glob("*.jpeg") if path.is_file()] img_path_list = [path for path in img_dir.glob('*.jpeg') if path.is_file()]
print(img_path_list) print(img_path_list)
# instantiate data object # instantiate data object
vis_com_list = [ vis_com_list = [
@@ -20,10 +23,10 @@ if __name__ == "__main__":
for vis_com in vis_com_list: for vis_com in vis_com_list:
print(repr(vis_com)) print(repr(vis_com))
# prepare env vars # prepare env vars
env_path = test_dir.parent / "local.env" env_path = test_dir.parent / 'local.env'
assert env_path.exists() assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# connect to database # connect to database
collection, db, client = connect() collection, db, client = connect()
print(client.server_info()) print(client.server_info())
@@ -32,6 +35,6 @@ if __name__ == "__main__":
try: try:
result = collection.insert_one(vis_com.model_dump()) result = collection.insert_one(vis_com.model_dump())
except DuplicateKeyError as exc: except DuplicateKeyError as exc:
print("ignoring:\n", exc) print('ignoring:\n', exc)
else: else:
print(f"inserted document: {result}") print(f"inserted document: {result}")
@@ -1,7 +1,9 @@
from src.database import ModelOutputs from __future__ import annotations
from database import ModelOutputs
if __name__ == "__main__": if __name__ == '__main__':
# instantiate data object # instantiate data object
vis_com_list = [ vis_com_list = [
ModelOutputs.from_random() ModelOutputs.from_random()
@@ -1,15 +1,15 @@
from pathlib import Path from __future__ import annotations
from dotenv import load_dotenv
import os
import logging import logging
import os
from pathlib import Path
from src.database import ( from database import connect
VisualCommunication, from database import upsert_predictions
connect, from database import VisualCommunication
upsert_predictions from dotenv import load_dotenv
)
if __name__ == "__main__": if __name__ == '__main__':
# setup logging # setup logging
fmt = ( fmt = (
'%(asctime)s | ' '%(asctime)s | '
@@ -22,8 +22,8 @@ if __name__ == "__main__":
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO) logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO)
# get list of image paths # get list of image paths
test_dir = Path(__file__).parent test_dir = Path(__file__).parent
img_dir = test_dir / "imgs" img_dir = test_dir / 'imgs'
img_path_list = [path for path in img_dir.glob("*.jpeg") if path.is_file()] img_path_list = [path for path in img_dir.glob('*.jpeg') if path.is_file()]
# instantiate data object # instantiate data object
vis_com_list = [ vis_com_list = [
VisualCommunication.from_file(path) VisualCommunication.from_file(path)
@@ -31,16 +31,19 @@ if __name__ == "__main__":
in img_path_list in img_path_list
] ]
# generate random predictions # generate random predictions
[vis_com.generate_random_prediction() for vis_com in vis_com_list] for vis_com in vis_com_list:
vis_com.generate_random_prediction()
# prepare env vars # prepare env vars
env_path = test_dir.parent / "local.env" env_path = test_dir.parent / 'local.env'
assert env_path.exists() assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# connect to database # connect to database
collection, db, client = connect() collection, db, client = connect()
# upload visual communication # upload visual communication
for vis_com in vis_com_list: for vis_com in vis_com_list:
if vis_com.prediction is None:
continue
upsert_predictions( upsert_predictions(
collection=collection, collection=collection,
vis_com_name=vis_com.name, vis_com_name=vis_com.name,
@@ -1,18 +1,18 @@
from pathlib import Path from __future__ import annotations
from dotenv import load_dotenv
import os import os
from pathlib import Path
from src.database import ( from database import connect
connect, from database import total_annotated
total_annotated from dotenv import load_dotenv
)
if __name__ == "__main__": if __name__ == '__main__':
# prepare env vars # prepare env vars
env_path = Path(__file__).parent.parent / "local.env" env_path = Path(__file__).parent.parent / 'local.env'
assert env_path.exists() assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# connect to database # connect to database
collection, db, client = connect() collection, db, client = connect()
# get visual communication # get visual communication
@@ -1,18 +1,18 @@
from pathlib import Path from __future__ import annotations
from dotenv import load_dotenv
import os import os
from pathlib import Path
from src.database import ( from database import connect
connect, from database import total_documents
total_documents from dotenv import load_dotenv
)
if __name__ == "__main__": if __name__ == '__main__':
# prepare env vars # prepare env vars
env_path = Path(__file__).parent.parent / "local.env" env_path = Path(__file__).parent.parent / 'local.env'
assert env_path.exists() assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
os.environ["MONGO_HOST"] = "localhost" os.environ['MONGO_HOST'] = 'localhost'
# connect to database # connect to database
collection, db, client = connect() collection, db, client = connect()
# get visual communication # get visual communication