|
| 1 | +import pandas as pd |
| 2 | +from encord.objects import OntologyStructure |
| 3 | +from sqlalchemy import MetaData, Table, create_engine, select, text |
| 4 | +from sqlalchemy.engine import Engine |
| 5 | + |
| 6 | + |
| 7 | +def get_active_engine(path_to_db: str) -> Engine: |
| 8 | + return create_engine(f"sqlite:///{path_to_db}") |
| 9 | + |
| 10 | + |
| 11 | +class ActiveProject: |
| 12 | + def __init__(self, engine: Engine, project_name: str): |
| 13 | + self._engine = engine |
| 14 | + self._metadata = MetaData(bind=self._engine) |
| 15 | + self._project_name = project_name |
| 16 | + |
| 17 | + active_project = Table("active_project", self._metadata, autoload_with=self._engine) |
| 18 | + stmt = select(active_project.c.project_hash).where(active_project.c.project_name == f"{self._project_name}") |
| 19 | + |
| 20 | + with self._engine.connect() as connection: |
| 21 | + result = connection.execute(stmt).fetchone() |
| 22 | + |
| 23 | + if result is not None: |
| 24 | + self._project_hash = result[0] |
| 25 | + else: |
| 26 | + self._project_hash = None |
| 27 | + |
| 28 | + def get_ontology(self) -> OntologyStructure: |
| 29 | + active_project = Table("active_project", self._metadata, autoload_with=self._engine) |
| 30 | + |
| 31 | + stmt = select(active_project.c.project_ontology).where(active_project.c.project_hash == f"{self._project_hash}") |
| 32 | + |
| 33 | + with self._engine.connect() as connection: |
| 34 | + result = connection.execute(stmt).fetchone() |
| 35 | + |
| 36 | + if result is not None: |
| 37 | + ontology = result[0] |
| 38 | + else: |
| 39 | + ontology = None |
| 40 | + |
| 41 | + return OntologyStructure.from_dict(ontology) |
| 42 | + |
| 43 | + def get_prediction_metrics(self) -> pd.DataFrame: |
| 44 | + active_project_prediction = Table("active_project_prediction", self._metadata, autoload_with=self._engine) |
| 45 | + stmt = select(active_project_prediction.c.prediction_hash).where( |
| 46 | + active_project_prediction.c.project_hash == f"{self._project_hash}" |
| 47 | + ) |
| 48 | + |
| 49 | + with self._engine.connect() as connection: |
| 50 | + result = connection.execute(stmt).fetchone() |
| 51 | + |
| 52 | + if result is not None: |
| 53 | + prediction_hash = result[0] |
| 54 | + else: |
| 55 | + prediction_hash = None |
| 56 | + |
| 57 | + active_project_prediction_analytics = Table( |
| 58 | + "active_project_prediction_analytics", self._metadata, autoload_with=self._engine |
| 59 | + ) |
| 60 | + |
| 61 | + stmt = select( |
| 62 | + [ |
| 63 | + active_project_prediction_analytics.c.feature_hash, |
| 64 | + active_project_prediction_analytics.c.metric_area, |
| 65 | + active_project_prediction_analytics.c.metric_area_relative, |
| 66 | + active_project_prediction_analytics.c.metric_aspect_ratio, |
| 67 | + active_project_prediction_analytics.c.metric_brightness, |
| 68 | + active_project_prediction_analytics.c.metric_contrast, |
| 69 | + active_project_prediction_analytics.c.metric_sharpness, |
| 70 | + active_project_prediction_analytics.c.metric_red, |
| 71 | + active_project_prediction_analytics.c.metric_green, |
| 72 | + active_project_prediction_analytics.c.metric_blue, |
| 73 | + active_project_prediction_analytics.c.metric_label_border_closeness, |
| 74 | + active_project_prediction_analytics.c.metric_label_confidence, |
| 75 | + text( |
| 76 | + """CASE |
| 77 | + WHEN feature_hash == match_feature_hash THEN 1 |
| 78 | + ELSE 0 |
| 79 | + END AS true_positive |
| 80 | + """ |
| 81 | + ), |
| 82 | + ] |
| 83 | + ).where(active_project_prediction_analytics.c.prediction_hash == prediction_hash) |
| 84 | + |
| 85 | + with self._engine.begin() as conn: |
| 86 | + df = pd.read_sql(stmt, conn) |
| 87 | + |
| 88 | + return df |
0 commit comments