Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BaseTableBuilder and CountsBuilder to the public API #282

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cumulus_library/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Package metadata"""

from .base_table_builder import BaseTableBuilder
from .base_utils import StudyConfig
from .statistics.counts import CountsBuilder
from .study_manifest import StudyManifest

__all__ = ["StudyConfig", "StudyManifest"]
__all__ = ["BaseTableBuilder", "CountsBuilder", "StudyConfig", "StudyManifest"]
__version__ = "3.0.0"
6 changes: 3 additions & 3 deletions cumulus_library/studies/core/builder_condition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library.studies.core.core_templates import core_templates
from cumulus_library.template_sql import base_templates, sql_utils

Expand All @@ -12,7 +12,7 @@
}


class CoreConditionBuilder(base_table_builder.BaseTableBuilder):
class CoreConditionBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating Condition tables..."

def denormalize_codes(self):
Expand Down Expand Up @@ -75,7 +75,7 @@ def denormalize_codes(self):
def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
):
self.denormalize_codes()
Expand Down
6 changes: 3 additions & 3 deletions cumulus_library/studies/core/builder_documentreference.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library.studies.core.core_templates import core_templates
from cumulus_library.template_sql import sql_utils

Expand All @@ -14,13 +14,13 @@
}


class CoreDocumentreferenceBuilder(base_table_builder.BaseTableBuilder):
class CoreDocumentreferenceBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating DocumentReference tables..."

def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
):
self.queries = sql_utils.denormalize_complex_objects(
Expand Down
6 changes: 3 additions & 3 deletions cumulus_library/studies/core/builder_encounter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass

from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library.studies.core.core_templates import core_templates
from cumulus_library.template_sql import sql_utils

Expand Down Expand Up @@ -34,7 +34,7 @@ def __post_init__(self):
self.target_table = f"core__encounter_dn_{self.column_hierarchy[-1][0]}"


class CoreEncounterBuilder(base_table_builder.BaseTableBuilder):
class CoreEncounterBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating Encounter tables..."

def denormalize_codes(self, database):
Expand Down Expand Up @@ -108,7 +108,7 @@ def denormalize_codes(self, database):
def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
):
self.denormalize_codes(config.db)
Expand Down
6 changes: 3 additions & 3 deletions cumulus_library/studies/core/builder_medicationrequest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module for generating core medicationrequest table"""

from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library.studies.core.core_templates import core_templates
from cumulus_library.template_sql import sql_utils

Expand All @@ -19,13 +19,13 @@
}


class MedicationRequestBuilder(base_table_builder.BaseTableBuilder):
class MedicationRequestBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating MedicationRequest table..."

def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
) -> None:
"""Constructs queries related to medication requests
Expand Down
6 changes: 3 additions & 3 deletions cumulus_library/studies/core/builder_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass

from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library.studies.core.core_templates import core_templates
from cumulus_library.template_sql import sql_utils

Expand Down Expand Up @@ -36,13 +36,13 @@ def __post_init__(self):
self.target_table = f"core__observation_{table_suffix}"


class ObservationBuilder(base_table_builder.BaseTableBuilder):
class ObservationBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating Observation tables..."

def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
):
"""constructs queries related to patient extensions of interest
Expand Down
8 changes: 4 additions & 4 deletions cumulus_library/studies/core/builder_patient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Module for extracting US core extensions from patient records"""

from cumulus_library import base_utils, databases
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library
from cumulus_library import databases
from cumulus_library.studies.core.core_templates import core_templates
from cumulus_library.template_sql import base_templates, sql_utils

Expand All @@ -15,7 +15,7 @@
}


class PatientBuilder(BaseTableBuilder):
class PatientBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating Patient tables..."

@staticmethod
Expand Down Expand Up @@ -57,7 +57,7 @@ def make_extension_query(
def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
):
"""constructs queries related to patient extensions of interest
Expand Down
4 changes: 2 additions & 2 deletions cumulus_library/studies/core/builder_prereq_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import sqlparse

from cumulus_library import base_table_builder
import cumulus_library


class CorePrereqTableBuilder(base_table_builder.BaseTableBuilder):
class CorePrereqTableBuilder(cumulus_library.BaseTableBuilder):
display_text = "Creating core prerequisite tables..."

def prepare_queries(self, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions cumulus_library/studies/core/count_core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pathlib import Path

from cumulus_library.statistics import counts
import cumulus_library


class CoreCountsBuilder(counts.CountsBuilder):
class CoreCountsBuilder(cumulus_library.CountsBuilder):
display_text = "Creating core counts..."

def count_core_condition(self, duration: str = "month"):
Expand Down Expand Up @@ -119,6 +119,6 @@ def prepare_queries(self, *args, **kwargs):
]


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
builder = CoreCountsBuilder()
builder.write_counts(f"{Path(__file__).resolve().parent}/count_core.sql")
7 changes: 4 additions & 3 deletions cumulus_library/studies/discovery/code_detection.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Module for generating encounter codeableConcept table"""

from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library import base_utils
from cumulus_library.studies.discovery import code_definitions
from cumulus_library.studies.discovery.discovery_templates import discovery_templates
from cumulus_library.template_sql import sql_utils


class CodeDetectionBuilder(base_table_builder.BaseTableBuilder):
class CodeDetectionBuilder(cumulus_library.BaseTableBuilder):
display_text = "Selecting unique code systems..."

def _check_coding_against_db(self, code_source, database):
Expand Down Expand Up @@ -35,7 +36,7 @@ def _check_codes_in_fields(self, code_sources: list[dict], database) -> dict:
def prepare_queries(
self,
*args,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
**kwargs,
):
"""Constructs queries related to condition codeableConcept
Expand Down
6 changes: 3 additions & 3 deletions cumulus_library/studies/vocab/vocab_icd_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

import pandas

from cumulus_library import base_table_builder, base_utils
import cumulus_library
from cumulus_library.template_sql import base_templates


class VocabIcdRunner(base_table_builder.BaseTableBuilder):
class VocabIcdRunner(cumulus_library.BaseTableBuilder):
display_text = "Creating ICD vocab..."

def prepare_queries(
self,
config: base_utils.StudyConfig,
config: cumulus_library.StudyConfig,
*args,
**kwargs,
):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_dedicated_schema/module1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleOneRunner(BaseTableBuilder):
class ModuleOneRunner(cumulus_library.BaseTableBuilder):
display_text = "module1"

def prepare_queries(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_dedicated_schema/module2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleTwoRunner(BaseTableBuilder):
class ModuleTwoRunner(cumulus_library.BaseTableBuilder):
display_text = "module2"

def prepare_queries(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_counts_valid/module1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.statistics.counts import CountsBuilder
import cumulus_library


class ModuleOneRunner(CountsBuilder):
class ModuleOneRunner(cumulus_library.CountsBuilder):
display_text = "module1"

def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_counts_valid/module2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.statistics.counts import CountsBuilder
import cumulus_library


class ModuleTwoRunner(CountsBuilder):
class ModuleTwoRunner(cumulus_library.CountsBuilder):
display_text = "module2"

def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_local_template/module1.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from study_python_local_template import local_template

from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleOneRunner(BaseTableBuilder):
class ModuleOneRunner(cumulus_library.BaseTableBuilder):
display_text = "module1"

def prepare_queries(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_no_subclass/module1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleOneRunner(BaseTableBuilder):
class ModuleOneRunner(cumulus_library.BaseTableBuilder):
display_text = "Module 1"

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_s3/module1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleOneRunner(BaseTableBuilder):
class ModuleOneRunner(cumulus_library.BaseTableBuilder):
display_text = "module1"

def prepare_queries(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_s3/module2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleTwoRunner(BaseTableBuilder):
class ModuleTwoRunner(cumulus_library.BaseTableBuilder):
display_text = "module2"

def prepare_queries(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_valid/module1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleOneRunner(BaseTableBuilder):
class ModuleOneRunner(cumulus_library.BaseTableBuilder):
display_text = "module1"

def prepare_queries(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_data/study_python_valid/module2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cumulus_library.base_table_builder import BaseTableBuilder
import cumulus_library


class ModuleTwoRunner(BaseTableBuilder):
class ModuleTwoRunner(cumulus_library.BaseTableBuilder):
display_text = "module2"

def prepare_queries(self, *args, **kwargs):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pathlib
from unittest import mock

import pytest

from cumulus_library import cli, databases
from cumulus_library.studies.discovery.discovery_templates import discovery_templates
from tests import conftest
Expand Down Expand Up @@ -144,3 +146,18 @@ def test_get_system_pairs():
],
)
assert query == expected


@mock.patch.dict(os.environ, clear=True)
@mock.patch("cumulus_library.studies.discovery.code_definitions.code_list", new=[{}])
def test_bad_code_definition(tmp_path):
with pytest.raises(KeyError, match="Expected table_name and column_hierarchy keys"):
cli.main(
cli_args=conftest.duckdb_args(
[
"build",
"--target=discovery",
],
tmp_path,
)
)
Loading