Skip to content

Commit

Permalink
Remove hydra use query api (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrit110 authored Oct 8, 2023
1 parent a8886c5 commit 9b9d544
Show file tree
Hide file tree
Showing 18 changed files with 1,527 additions and 1,407 deletions.
55 changes: 27 additions & 28 deletions cyclops/query/base.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
"""Base querier class."""

import logging
import os
from functools import partial
from typing import Any, Callable, Dict, List, Optional

import yaml
from hydra import compose, initialize
from omegaconf import OmegaConf
from sqlalchemy import MetaData
from sqlalchemy.sql.selectable import Subquery

from cyclops.query import ops as qo
from cyclops.query.interface import QueryInterface
from cyclops.query.orm import Database
from cyclops.query.orm import Database, DatasetQuerierConfig
from cyclops.query.util import (
DBSchema,
_to_subquery,
get_attr_name,
)
from cyclops.utils.file import join as join_path
from cyclops.utils.log import setup_logging


Expand Down Expand Up @@ -81,9 +76,18 @@ class DatasetQuerier:
Parameters
----------
config_overrides
Override configuration parameters, specified as kwargs.
database
Name of database.
user
Username for database.
password
Password for database.
dbms
Database management system.
host
Hostname of database.
port
Port of database.
Notes
-----
Expand All @@ -98,26 +102,21 @@ class DatasetQuerier:

def __init__(
self,
**config_overrides: Dict[str, Any],
database: str,
user: str,
password: str,
dbms: str = "postgresql",
host: str = "localhost",
port: int = 5432,
) -> None:
overrides = []
if config_overrides:
config_file = join_path(os.path.dirname(__file__), "configs", "config.yaml")
with open(config_file, "r", encoding="utf-8") as file:
config_keys = list(yaml.safe_load(file).keys())
for key, value in config_overrides.items():
if key in config_keys:
overrides.append(f"{key}={value}")
else:
overrides.append(f"+{key}={value}")
with initialize(
version_base=None,
config_path="configs",
job_name="DatasetQuerier",
):
config = compose(config_name="config", overrides=overrides)
LOGGER.debug(OmegaConf.to_yaml(config))

config = DatasetQuerierConfig(
database=database,
user=user,
password=password,
dbms=dbms,
host=host,
port=port,
)
self.db = Database(config)
if not self.db.is_connected:
LOGGER.error("Database is not connected, cannot run queries.")
Expand Down
6 changes: 0 additions & 6 deletions cyclops/query/configs/config.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions cyclops/query/eicu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import logging
from typing import Any, Dict

from cyclops.query.base import DatasetQuerier
from cyclops.utils.log import setup_logging
Expand All @@ -18,17 +17,3 @@

class EICUQuerier(DatasetQuerier):
"""EICU dataset querier."""

def __init__(self, **config_overrides: Dict[str, Any]) -> None:
"""Initialize.
Parameters
----------
**config_overrides
Override configuration parameters, specified as kwargs.
"""
overrides = {}
if config_overrides:
overrides = config_overrides
super().__init__(**overrides)
15 changes: 0 additions & 15 deletions cyclops/query/gemini.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""GEMINI query API."""

import logging
from typing import Any, Dict

from sqlalchemy import select
from sqlalchemy.sql.expression import union_all
Expand All @@ -24,20 +23,6 @@
class GEMINIQuerier(DatasetQuerier):
"""GEMINI dataset querier."""

def __init__(self, **config_overrides: Dict[str, Any]) -> None:
"""Initialize.
Parameters
----------
**config_overrides
Override configuration parameters, specified as kwargs.
"""
overrides = {}
if config_overrides:
overrides = config_overrides
super().__init__(**overrides)

def ip_admin(
self,
) -> QueryInterface:
Expand Down
15 changes: 0 additions & 15 deletions cyclops/query/mimiciii.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import logging
from typing import Any, Dict

import cyclops.query.ops as qo
from cyclops.query.base import DatasetQuerier
Expand All @@ -21,20 +20,6 @@
class MIMICIIIQuerier(DatasetQuerier):
"""MIMIC-III dataset querier."""

def __init__(self, **config_overrides: Dict[str, Any]) -> None:
"""Initialize.
Parameters
----------
**config_overrides
Override configuration parameters, specified as kwargs.
"""
overrides = {}
if config_overrides:
overrides = config_overrides
super().__init__(**overrides)

def diagnoses(
self,
) -> QueryInterface:
Expand Down
15 changes: 0 additions & 15 deletions cyclops/query/mimiciv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import logging
from typing import Any, Dict

from sqlalchemy import Integer, func, select

Expand All @@ -24,20 +23,6 @@
class MIMICIVQuerier(DatasetQuerier):
"""MIMICIV dataset querier."""

def __init__(self, **config_overrides: Dict[str, Any]) -> None:
"""Initialize.
Parameters
----------
**config_overrides
Override configuration parameters, specified as kwargs.
"""
overrides = {}
if config_overrides:
overrides = config_overrides
super().__init__(**overrides)

def patients(
self,
) -> QueryInterface:
Expand Down
33 changes: 16 additions & 17 deletions cyclops/query/omop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""OMOP query API."""

import logging
from typing import Any, Dict, List, Optional, Union
from typing import List, Optional, Union

from sqlalchemy.sql.selectable import Subquery

Expand Down Expand Up @@ -58,24 +58,23 @@ class OMOPQuerier(DatasetQuerier):

def __init__(
self,
schema_name: str,
**config_overrides: Dict[str, Any],
database: str,
user: str,
password: str,
dbms: str = "postgresql",
host: str = "localhost",
port: int = 5432,
schema_name: str = "omop",
) -> None:
"""Initialize.
Parameters
----------
schema_name
Name of database schema.
**config_overrides
Override configuration parameters, specified as kwargs.
"""
super().__init__(
database=database,
user=user,
password=password,
dbms=dbms,
host=host,
port=port,
)
self.schema_name = schema_name
overrides = {}
if config_overrides:
overrides = config_overrides
super().__init__(**overrides)

def map_concept_ids_to_name(
self,
Expand Down
50 changes: 40 additions & 10 deletions cyclops/query/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import logging
import os
import socket
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Union
from urllib.parse import quote_plus

import dask.dataframe as dd
import pandas as pd
import pyarrow.csv as pv
import pyarrow.parquet as pq
from datasets import Dataset
from omegaconf import DictConfig
from sqlalchemy import MetaData, create_engine, inspect
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -43,32 +44,61 @@ def _get_db_url(
user: str,
pwd: str,
host: str,
port: str,
port: int,
database: str,
) -> str:
"""Combine to make Database URL string."""
return f"{dbms}://{user}:{pwd}@{host}:{port}/{database}"
return f"{dbms}://{user}:{quote_plus(pwd)}@{host}:{str(port)}/{database}"


@dataclass
class DatasetQuerierConfig:
"""Configuration for the dataset querier.
Attributes
----------
dbms
Database management system.
host
Hostname of database.
port
Port of database.
database
Name of database.
user
Username for database.
password
Password for database.
"""

database: str
user: str
password: str
dbms: str = "postgresql"
host: str = "localhost"
port: int = 5432


class Database:
"""Database class.
Attributes
----------
config: argparse.Namespace
Configuration stored in object.
engine: sqlalchemy.engine.base.Engine
config
Configuration stored in a dataclass.
engine
SQL extraction engine.
inspector: sqlalchemy.engine.reflection.Inspector
inspector
Module for schema inspection.
session: sqlalchemy.orm.session.Session
session
Session for ORM.
is_connected: bool
is_connected
Whether the database is setup, connected and ready to run queries.
"""

def __init__(self, config: DictConfig) -> None:
def __init__(self, config: DatasetQuerierConfig) -> None:
"""Instantiate.
Parameters
Expand Down
Loading

0 comments on commit 9b9d544

Please sign in to comment.