flake8 compliant

This commit is contained in:
Brian Bjarke Jensen
2024-02-24 23:42:55 +01:00
parent 993c9a122b
commit fa2230d5cf
23 changed files with 157 additions and 106 deletions
+31 -15
View File
@@ -25,6 +25,11 @@ from src.model_textual import (
SalienceModelOutput
)
class NoDocumentFoundException(Exception):
pass
class ModelOutputs(BaseModel):
visual_syntax: VisualSyntaxModelOutput
contact: ContactModelOutput
@@ -70,17 +75,28 @@ class ModelOutputs(BaseModel):
) -> ModelOutputs:
"""Instantiate from annotation."""
kwargs = {
"visual_syntax": VisualSyntaxModelOutput.from_choice(visual_syntax),
"contact": ContactModelOutput.from_choice(contact),
"angle": AngleModelOutput.from_choice(angle),
"point_of_view": PointOfViewModelOutput.from_choice(point_of_view),
"distance": DistanceModelOutput.from_choice(distance),
"modality_lighting": ModalityLightingModelOutput.from_choice(modality_lighting),
"modality_color": ModalityColorModelOutput.from_choice(modality_color),
"modality_depth": ModalityDepthModelOutput.from_choice(modality_depth),
"information_value": InformationValueModelOutput.from_choice(information_value),
"framing": FramingModelOutput.from_choice(framing),
"salience": SalienceModelOutput.from_choice(salience)
"visual_syntax": VisualSyntaxModelOutput
.from_choice(visual_syntax),
"contact": ContactModelOutput
.from_choice(contact),
"angle": AngleModelOutput
.from_choice(angle),
"point_of_view": PointOfViewModelOutput
.from_choice(point_of_view),
"distance": DistanceModelOutput
.from_choice(distance),
"modality_lighting": ModalityLightingModelOutput
.from_choice(modality_lighting),
"modality_color": ModalityColorModelOutput
.from_choice(modality_color),
"modality_depth": ModalityDepthModelOutput
.from_choice(modality_depth),
"information_value": InformationValueModelOutput
.from_choice(information_value),
"framing": FramingModelOutput
.from_choice(framing),
"salience": SalienceModelOutput
.from_choice(salience)
}
return cls(**kwargs)
@@ -115,7 +131,10 @@ class VisualCommunication(BaseModel):
@field_validator("image", mode="before")
@classmethod
def convert_to_image(cls, image: Image.Image | BytesIO | bytes) -> Image.Image:
def convert_to_image(
cls,
image: Image.Image | BytesIO | bytes
) -> Image.Image:
if isinstance(image, bytes):
image = BytesIO(image)
if isinstance(image, BytesIO):
@@ -138,6 +157,3 @@ class VisualCommunication(BaseModel):
if not force and self.prediction is not None:
logging.warning("set force=True to overwrite existing values.")
self.prediction = ModelOutputs.from_random()
class NoDocumentFoundException(Exception):
pass
+17 -5
View File
@@ -20,7 +20,9 @@ def total_annotated(
) -> int:
"""Get total number of annotated documents in database."""
query = {
"annotation": { "$ne": None }
"annotation": {
"$ne": None
}
}
return collection.count_documents(filter=query)
@@ -36,8 +38,14 @@ def get_visual_communication(
else:
query["annotation"] = None
data = collection.aggregate([
{ "$match": query }, # find using filters
{ "$sample": { "size": 1 } } # get one random
{
"$match": query # find using filters
},
{
"$sample": {
"size": 1 # get one random
}
}
])
data = list(data) # read data from cursor object
if len(data) == 0:
@@ -58,7 +66,9 @@ def upsert_predictions(
"name": vis_com_name
}
update = {
"$set": { "prediction": predictions.model_dump() }
"$set": {
"prediction": predictions.model_dump()
}
}
res = collection.update_one(
filter=query,
@@ -79,7 +89,9 @@ def upsert_annotations(
"name": vis_com_name
}
update = {
"$set": { "annotation": annotations.model_dump() }
"$set": {
"annotation": annotations.model_dump()
}
}
res = collection.update_one(
filter=query,
+2 -3
View File
@@ -1,9 +1,10 @@
import logging
from dotenv import load_dotenv
from pathlib import Path
import logging
import os
from src.web import app
# prepare optional local setup
env_path = Path(__file__).parent.parent / "local.env"
load_dotenv(env_path)
@@ -38,8 +39,6 @@ datefmt = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO)
logging.info("initialized app")
from src.web import app
server = app.server
if __name__ == "__main__":
-23
View File
@@ -1,24 +1 @@
from .classes import VisualSyntaxModelOutput
# CLASS_NAME_LIST = Literal[
# "non transactional action",
# "non transactional reaction",
# "unidirectional transactional action",
# "unidirectional transactional reaction",
# "bidirectional transactional action",
# "bidirectional transactional reaction",
# "conversion",
# "speech process",
# "classification overt taxonomy",
# "analytical exhaustive",
# "analytical disarranged",
# "analytical temporal",
# "analytical distributed",
# "anaytical topological",
# "analytical exploded",
# "analytical inclusive",
# "symbolic suggestive",
# "symbolic attributive"
# ]
+8 -2
View File
@@ -6,6 +6,7 @@ import random
class OptionNotSetException(Exception):
pass
class ModelOutput(BaseModel):
@classmethod
@@ -31,7 +32,8 @@ class ModelOutput(BaseModel):
raise ValidationError()
assert isinstance(option, str), "option is not a string"
allowed_options_list = cls.list_fields()
assert option in allowed_options_list, f"{option} is not among allowed fields {allowed_options_list}"
assert option in allowed_options_list, \
f"{option} is not among allowed fields {allowed_options_list}"
kwargs = {field: 0 for field in cls.list_fields()}
kwargs[option] = 1
return cls(**kwargs)
@@ -39,7 +41,11 @@ class ModelOutput(BaseModel):
def __repr__(self) -> str:
model_dict = self.model_dump()
model_repr_str = f"{self.classname()}("
model_repr_str += ", ".join([f"{field}={value:.3f}" for field, value in model_dict.items()])
model_repr_str += ", ".join([
f"{field}={value:.3f}"
for field, value
in model_dict.items()
])
model_repr_str += ")"
return model_repr_str
+7 -2
View File
@@ -28,7 +28,8 @@ class ModelOutput(BaseModel):
raise ValidationError()
assert isinstance(option, str)
allowed_options_list = cls.list_fields()
assert option in allowed_options_list, f"{option} is not among allowed fields {allowed_options_list}"
assert option in allowed_options_list, \
f"{option} is not among allowed fields {allowed_options_list}"
kwargs = {field: 0 for field in cls.list_fields()}
kwargs[option] = 1
return cls(**kwargs)
@@ -36,7 +37,11 @@ class ModelOutput(BaseModel):
def __repr__(self) -> str:
model_dict = self.model_dump()
model_repr_str = f"{self.classname()}("
model_repr_str += ", ".join([f"{field}={value:.3f}" for field, value in model_dict.items()])
model_repr_str += ", ".join([
f"{field}={value:.3f}"
for field, value
in model_dict.items()
])
model_repr_str += ")"
return model_repr_str
+7 -2
View File
@@ -28,7 +28,8 @@ class ModelOutput(BaseModel):
raise ValidationError()
assert isinstance(option, str)
allowed_options_list = cls.list_fields()
assert option in allowed_options_list, f"{option} is not among allowed fields {allowed_options_list}"
assert option in allowed_options_list, \
f"{option} is not among allowed fields {allowed_options_list}"
kwargs = {field: 0 for field in cls.list_fields()}
kwargs[option] = 1
return cls(**kwargs)
@@ -36,7 +37,11 @@ class ModelOutput(BaseModel):
def __repr__(self) -> str:
model_dict = self.model_dump()
model_repr_str = f"{self.classname()}("
model_repr_str += ", ".join([f"{field}={value:.3f}" for field, value in model_dict.items()])
model_repr_str += ", ".join([
f"{field}={value:.3f}"
for field, value
in model_dict.items()
])
model_repr_str += ")"
return model_repr_str
+4 -2
View File
@@ -30,6 +30,7 @@ BasicAuth(app, AUTH_DICT)
# connect to database
collection, db, client = connect()
# define callbacks
@app.callback(
Output("alert-element", "is_open"),
@@ -44,6 +45,7 @@ def show_alert(
logging.info(f"updated alert message: {msg}")
return True, msg
@app.callback(
Output("alert-message", "data"),
Output("vis-com-name", "data"),
@@ -108,7 +110,7 @@ def cycle_visual_communication_data(
}
# instantiate ModelOutputs object
annotations = ModelOutputs.from_annotations(**annotations)
# save data to
# save data to database
upsert_annotations(
collection=collection,
vis_com_name=vis_com_name,
@@ -137,7 +139,7 @@ def cycle_visual_communication_data(
# reset annotations
annotation_values = [None for elem in annotation_values]
except NoDocumentFoundException:
msg = f"no unannotated data in database"
msg = "no unannotated data in database"
logging.warning(msg)
response[0] = msg
return tuple(response)
+1 -1
View File
@@ -1,4 +1,4 @@
from dash import html, dcc
from dash import html
import dash_bootstrap_components as dbc
-2
View File
@@ -1,6 +1,4 @@
import dash_mantine_components as dmc
from dash import dcc, html
from typing import List
from .image import image_element
from .inputs import inputs_element
+23 -6
View File
@@ -20,6 +20,7 @@ from src.model_textual import (
SalienceModelOutput
)
def generate_option_labels(model) -> List[str]:
"""Generate presentable list of attributes from an OutputModel."""
labels = [
@@ -28,35 +29,51 @@ def generate_option_labels(model) -> List[str]:
]
return labels
def generate_visual_syntax_options_map():
"""Generate map of titles and options for visual syntax labels."""
options_map = {}
# add experiential labels
options_map["visual syntax"] = generate_option_labels(VisualSyntaxModelOutput)
options_map["visual syntax"] = generate_option_labels(
VisualSyntaxModelOutput
)
return options_map
def generate_interpersonal_options_map():
"""Generate map of titles and options for interpersonal labels."""
options_map = {}
# add interpersonal labels
options_map["contact"] = generate_option_labels(ContactModelOutput)
options_map["angle"] = generate_option_labels(AngleModelOutput)
options_map["point of view"] = generate_option_labels(PointOfViewModelOutput)
options_map["point of view"] = generate_option_labels(
PointOfViewModelOutput
)
options_map["distance"] = generate_option_labels(DistanceModelOutput)
options_map["modality lighting"] = generate_option_labels(ModalityLightingModelOutput)
options_map["modality color"] = generate_option_labels(ModalityColorModelOutput)
options_map["modality depth"] = generate_option_labels(ModalityDepthModelOutput)
options_map["modality lighting"] = generate_option_labels(
ModalityLightingModelOutput
)
options_map["modality color"] = generate_option_labels(
ModalityColorModelOutput
)
options_map["modality depth"] = generate_option_labels(
ModalityDepthModelOutput
)
return options_map
def generate_textual_options_map():
"""Generate map of titles and options for textual labels."""
options_map = {}
# add textual labels
options_map["information value"] = generate_option_labels(InformationValueModelOutput)
options_map["information value"] = generate_option_labels(
InformationValueModelOutput
)
options_map["framing"] = generate_option_labels(FramingModelOutput)
options_map["salience"] = generate_option_labels(SalienceModelOutput)
return options_map
# prepare experiential container
experiential_map = generate_visual_syntax_options_map()
experiential_container = dmc.Col(
-1
View File
@@ -1,4 +1,3 @@
from dash import dcc
import dash_mantine_components as dmc
from .stores import stores_element
+4 -2
View File
@@ -5,8 +5,10 @@ import os
storage_type = "session"
if "ENV" in os.environ and os.getenv("ENV") == "DEV":
storage_type = "memory"
logging.info(f"ENV=DEV -> dcc.Stores changed to storage_type={storage_type}")
logging.info(
"ENV=DEV -> dcc.Stores changed to storage_type=%s",
storage_type
)
stores_element = html.Div(
children=[
+5 -1
View File
@@ -10,7 +10,11 @@ if __name__ == "__main__":
img_path_list = [path for path in img_dir.glob("*.jpeg") if path.is_file()]
print(img_path_list)
# instantiate data object
vis_com_list = [VisualCommunication.from_file(path) for path in img_path_list]
vis_com_list = [
VisualCommunication.from_file(path)
for path
in img_path_list
]
# generate random predictions
[vis_com.generate_random_prediction() for vis_com in vis_com_list]
for vis_com in vis_com_list:
+5 -1
View File
@@ -12,7 +12,11 @@ if __name__ == "__main__":
img_path_list = [path for path in img_dir.glob("*.jpeg") if path.is_file()]
print(img_path_list)
# instantiate data object
vis_com_list = [VisualCommunication.from_file(path) for path in img_path_list]
vis_com_list = [
VisualCommunication.from_file(path)
for path
in img_path_list
]
for vis_com in vis_com_list:
print(repr(vis_com))
# prepare env vars
+5 -4
View File
@@ -3,10 +3,11 @@ from src.database import ModelOutputs
if __name__ == "__main__":
# instantiate data object
annotation = {
}
vis_com_list = [ModelOutputs.from_annotation(path) for path in img_path_list]
vis_com_list = [
ModelOutputs.from_random()
for i
in range(3)
]
# generate random predictions
[vis_com.generate_random_prediction() for vis_com in vis_com_list]
for vis_com in vis_com_list:
+5 -1
View File
@@ -25,7 +25,11 @@ if __name__ == "__main__":
img_dir = test_dir / "imgs"
img_path_list = [path for path in img_dir.glob("*.jpeg") if path.is_file()]
# instantiate data object
vis_com_list = [VisualCommunication.from_file(path) for path in img_path_list]
vis_com_list = [
VisualCommunication.from_file(path)
for path
in img_path_list
]
# generate random predictions
[vis_com.generate_random_prediction() for vis_com in vis_com_list]
# prepare env vars