From c166420909a7c91bd488d0f877b97b61c6888448 Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Tue, 20 Feb 2024 19:55:53 +0100 Subject: [PATCH] updated database interface --- src/database/__init__.py | 5 +++ src/database/classes.py | 69 ++++++++++++++++++++++++++++++++++++++++ src/database/database.py | 22 +++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 src/database/__init__.py create mode 100644 src/database/classes.py create mode 100644 src/database/database.py diff --git a/src/database/__init__.py b/src/database/__init__.py new file mode 100644 index 0000000..4083b77 --- /dev/null +++ b/src/database/__init__.py @@ -0,0 +1,5 @@ +from .classes import ( + ModelOutputs, + VisualCommunication +) +from .database import connect \ No newline at end of file diff --git a/src/database/classes.py b/src/database/classes.py new file mode 100644 index 0000000..620142a --- /dev/null +++ b/src/database/classes.py @@ -0,0 +1,69 @@ +from __future__ import annotations +from pydantic import BaseModel, field_validator +from PIL import Image +from io import BytesIO +from pathlib import Path + +from src.model_experiential import ExperientialModelOutput +from src.model_interpersonal import ( + ContactModelOutput, + AngleModelOutput, + PointOfViewModelOutput, + DistanceModelOutput, + ModalityLightingModelOutput, + ModalityColorModelOutput, + ModalityDepthModelOutput +) +from src.model_textual import ( + InformationValueModelOutput, + FramingModelOutput, + SalienceModelOutput +) + +class ModelOutputs(BaseModel): + experiential: ExperientialModelOutput + contact: ContactModelOutput + angle: AngleModelOutput + point_of_view: PointOfViewModelOutput + distance: DistanceModelOutput + modality_lighting: ModalityLightingModelOutput + modality_color: ModalityColorModelOutput + modality_depth: ModalityDepthModelOutput + information_value: InformationValueModelOutput + framing: FramingModelOutput + salience: SalienceModelOutput + + +class VisualCommunication(BaseModel): + name: str + image: bytes + annotation: ModelOutputs | None = None + prediction: ModelOutputs | None = None + + @classmethod + def classname(cls) -> str: + """Return classname.""" + return cls.__name__ + + @classmethod + def from_file(cls, path: Path) -> VisualCommunication: + """Instantiate from file.""" + name = path.stem + image = Image.open(path) + return VisualCommunication(name=name, image=image) + + @field_validator("image", mode="before") + @classmethod + def convert_to_bytes(cls, raw: Image.Image | BytesIO | bytes) -> bytes: + if isinstance(raw, Image.Image): + raw = raw.tobytes() + if isinstance(raw, BytesIO): + raw = raw.read() + return raw + + def __repr__(self) -> str: + return f"{self.classname()}(name='{self.name}')" + + @property + def image(self) -> Image.Image: + return Image.open(BytesIO(self.image)) diff --git a/src/database/database.py b/src/database/database.py new file mode 100644 index 0000000..8f5b429 --- /dev/null +++ b/src/database/database.py @@ -0,0 +1,22 @@ +from pymongo import MongoClient +from dotenv import load_dotenv +import os + +def connect(): + """Connect to MongoDB.""" + # load env vars + load_dotenv() + necessary_env_vars = [ + "MONGO_HOST", + "MONGO_DB", + "MONGO_COLLECTION" + ] + for env_var in necessary_env_vars: + assert env_var in os.environ, f"{env_var} not found" + # connect to database + client = MongoClient(os.getenv("MONGO_HOST")) + db = client[os.getenv("MONGO_DB")] + collection = db[os.getenv("MONGO_COLLECTION")] + return collection, db, client + +