Skip to content

Commit

Permalink
Merge pull request #7 from Bayer-Group/cleanup
Browse files Browse the repository at this point in the history
clean up
  • Loading branch information
a-hartens authored Nov 21, 2024
2 parents 1f6c266 + 04be36f commit 918c9f0
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 236 deletions.
2 changes: 1 addition & 1 deletion docs/api/phenotypes/arithmetic_phenotype.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# ArithmeticPhenotype

::: phenex.phenotypes.arithmetic_phenotype
::: phenex.phenotypes.computation_graph_phenotypes.ArithmeticPhenotype
2 changes: 1 addition & 1 deletion docs/api/phenotypes/logic_phenotype.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# LogicPhenotype

::: phenex.phenotypes.logic_phenotype
::: phenex.phenotypes.computation_graph_phenotypes.LogicPhenotype
3 changes: 3 additions & 0 deletions docs/api/phenotypes/multiple_occurrences_phenotype.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MultipleOccurrencesPhenotype

::: phenex.phenotypes.multiple_occurrences_phenotype.MultipleOccurrencesPhenotype
2 changes: 1 addition & 1 deletion docs/api/phenotypes/score_phenotype.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# ScorePhenotype

::: phenex.phenotypes.score_phenotype
::: phenex.phenotypes.computation_graph_phenotypes.ScorePhenotype
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
"metadata": {},
"outputs": [],
"source": [
"from phenex.core.tables import Codelist\n",
"from phenex.codelists.codelists import Codelist\n",
"\n",
"# Create a codelist for Atrial Fibrillation\n",
"af_codelist = Codelist(\n",
" name = 'atrial_fibrillation', \n",
" codes = {\n",
" codelist = {\n",
" 'ICD10CM': \n",
" ['I48.0', 'I48.1', 'I48.11', 'I48.19', 'I48.2', 'I48.20', 'I48.21', 'I48.91'], \n",
" 'ICD9CM': \n",
Expand All @@ -66,7 +66,7 @@
"# Create a codelist for electrocardiogram (ECG)\n",
"ecg_codelist = Codelist(\n",
" name = 'electrocardiogram', \n",
" codes = {\n",
" codelist = {\n",
" 'CPT': ['93000','93005','93010','93040','93041','93042']\n",
" }\n",
")"
Expand Down Expand Up @@ -114,7 +114,7 @@
"metadata": {},
"outputs": [],
"source": [
"from phenex.phenotypes import CodelistPhenotype\n",
"from phenex.phenotypes.codelist_phenotype import CodelistPhenotype\n",
"# Ex.1 \n",
"# Which patients had an atrial fibrillation diagnosis at **any time** in the data source?\n",
"af_phenotype = CodelistPhenotype(\n",
Expand Down
15 changes: 5 additions & 10 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ nav:
- Home: index.md
- Installation: installation.md
- Tutorials:
- Cohort Basics:
- Optum Data Format: tutorials/cohort_basics/Optum_Format.ipynb
- OMOP CDM Data Format: tutorials/cohort_basics/OMOP_CDM_Format.ipynb
- Custom Data Structures: tutorials/cohort_basics/Custom_Data_Structure.ipynb
- Phenotypes:
- CodelistPhenotype: tutorials/phenotypes/CodelistPhenotype_Tutorial.ipynb
- MeasurementPhenotype: tutorials/phenotypes/MeasurementPhenotype_Tutorial.ipynb
- ArithmeticPhenotype: tutorials/phenotypes/ArithmeticPhenotype_Tutorial.ipynb
- LogicPhenotype: tutorials/phenotypes/LogicPhenotype_Tutorial.ipynb
- CodelistPhenotype: tutorials/CodelistPhenotype_Tutorial.ipynb
- MeasurementPhenotype: tutorials/MeasurementPhenotype_Tutorial.ipynb
- ArithmeticPhenotype: tutorials/ArithmeticPhenotype_Tutorial.ipynb
- LogicPhenotype: tutorials/LogicPhenotype_Tutorial.ipynb
- API:
- Phenotypes:
- Phenotype: api/phenotypes/phenotype.md
- CodelistPhenotype: api/phenotypes/codelist_phenotype.md
- MultipleOccurrencePhenotype: api/phenotypes/multiple_occurrences_phenotype.md
- MeasurementPhenotype: api/phenotypes/measurement_phenotype.md
- AgePhenotype: api/phenotypes/age_phenotype.md
- SexPhenotype: api/phenotypes/sex_phenotype.md
- DeathPhenotype: api/phenotypes/death_phenotype.md
- ContinuousCoveragePhenotype: api/phenotypes/continuous_coverage_phenotype.md
- AgePhenotype: api/phenotypes/age_phenotype.md
- ArithmeticPhenotype: api/phenotypes/arithmetic_phenotype.md
- LogicPhenotype: api/phenotypes/logic_phenotype.md
- ScorePhenotype: api/phenotypes/score_phenotype.md
Expand Down
5 changes: 5 additions & 0 deletions phenex/phenotypes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from .phenotype import Phenotype

from .codelist_phenotype import CodelistPhenotype
from .age_phenotype import AgePhenotype
from .sex_phenotype import SexPhenotype
from .multiple_occurrences_phenotype import MultipleOccurrencesPhenotype
from .measurement_phenotype import MeasurementPhenotype
from .death_phenotype import DeathPhenotype
from .computation_graph_phenotypes import (
ScorePhenotype,
ArithmeticPhenotype,
LogicPhenotype,
)
from .cohort import Cohort

# from .age_phenotype import AgePhenotype
# from .multiple_occurrences_phenotype import MultipleOccurrencesPhenotype
Expand Down
24 changes: 14 additions & 10 deletions phenex/phenotypes/age_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@ class AgePhenotype(Phenotype):
based on their date of birth and an optional anchor phenotype. The age is computed in years and can
be filtered within a specified range.
Parameters:
name: Name of the phenotype, default is 'age'.
min_age: Minimum age for filtering, in years.
max_age: Maximum age for filtering, in years.
domain: Domain of the phenotype, default is 'PERSON'.
anchor_phenotype: An optional anchor phenotype to calculate relative age.
Attributes:
DAYS_IN_YEAR (float): Number of days in a year, used for age calculation.
name (str): Name of the phenotype, default is 'age'.
min_age (float, optional): Minimum age for filtering, in years.
max_age (float, optional): Maximum age for filtering, in years.
domain (str): Domain of the phenotype, default is 'PERSON'.
anchor_phenotype (Phenotype, optional): An optional anchor phenotype to calculate relative age.
time_range_filter (RelativeTimeRangeFilter): A filter to apply the age range constraints.
children (list): List of dependent phenotypes.
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
_execute(tables: Dict[str, Table]) -> PhenotypeTable:
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
Example:
```
>>> age_phenotype = AgePhenotype(min_age=18, max_age=65, anchor_phenotype=some_anchor_phenotype)
>>> age_phenotype = AgePhenotype(
min_age=18,
max_age=65,
anchor_phenotype=some_anchor_phenotype
)
>>> result_table = age_phenotype.execute(tables)
>>> display(result_table)
```
Expand Down
Empty file.
34 changes: 19 additions & 15 deletions phenex/phenotypes/codelist_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@

class CodelistPhenotype(Phenotype):
"""
CodelistPhenotype filters a CodeTable based on a specified codelist and other optional filters
such as date range and relative time range.
CodelistPhenotype filters a CodeTable based on a specified codelist and other optional filters such as date range and relative time range.
Parameters:
name: The name of the phenotype.
domain: The domain of the phenotype.
codelist: The codelist used for filtering.
use_code_type: Whether to use the code type in filtering. Default is True.
date_range: A date range filter to apply.
relative_time_range: A relative time range filter or a list of filters to apply.
return_date: Specifies whether to return the 'first', 'last', or 'nearest' event date. Default is 'first'.
Attributes:
name (str): The name of the phenotype.
domain (str): The domain of the phenotype.
codelist (Codelist): The codelist used for filtering.
use_code_type (bool): Whether to use the code type in filtering. Default is True.
date_range (DateRangeFilter, optional): A date range filter to apply.
relative_time_range (Union[RelativeTimeRangeFilter, List[RelativeTimeRangeFilter]], optional): A relative time range filter or a list of filters to apply.
return_date (str): Specifies whether to return the 'first', 'last', or 'nearest' event date. Default is 'first'.
table (PhenotypeTable): The resulting phenotype table after filtering.
children (list): List of child phenotypes.
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
execute(tables: dict) -> PhenotypeTable:
Executes the filtering process on the provided tables and returns the filtered phenotype table.
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
Example:
```python
from phenex.codelists import Codelist
codelist = Codelist(name="example_codelist", codes=[...])
codelist = Codelist(
name="example_codelist",
codelist=[...])
date_range = DateRangeFilter(start_date="2020-01-01", end_date="2020-12-31")
date_range = DateRangeFilter(
start_date="2020-01-01",
end_date="2020-12-31")
phenotype = CodelistPhenotype(
name="example_phenotype",
Expand Down
17 changes: 11 additions & 6 deletions phenex/phenotypes/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ class Cohort(Phenotype):
The Cohort class represents a cohort of individuals based on specified entry criteria,
inclusions, exclusions, and baseline characteristics. It extends the Phenotype class.
Parameters:
entry_criterion: The primary phenotype used to define the cohort.
inclusions: A list of phenotypes that must be included in the cohort.
exclusions: A list of phenotypes that must be excluded from the cohort.
characteristics: A list of phenotypes representing baseline characteristics of the cohort.
Attributes:
table (Table): The table representing the cohort.
entry_criterion (Phenotype): The primary phenotype used to define the cohort.
inclusions (List[Phenotype]): A list of phenotypes that must be included in the cohort.
exclusions (List[Phenotype]): A list of phenotypes that must be excluded from the cohort.
characteristics (List[Phenotype]): A list of phenotypes representing baseline characteristics of the cohort.
children (List[Phenotype]): A list of all phenotypes involved in defining the cohort.
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
"""

table = None
Expand Down
98 changes: 65 additions & 33 deletions phenex/phenotypes/computation_graph_phenotypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ class ComputationGraphPhenotype(Phenotype):
| ScorePhenotype | boolean | value |
+---------------------+-------------+------------+
Parameters:
expression: The arithmetic expression to be evaluated composed of phenotypes combined by python arithmetic operations.
return_date: The date to be returned for the phenotype. Can be "first", "last", or a Phenotype object.
operate_on: The column to operate on. Can be "boolean" or "value".
populate: The column to populate. Can be "boolean" or "value".
reduce: Whether to reduce the phenotype table to only include rows where the boolean column is True. This is only relevant if populate is "boolean".
Attributes:
expression (ComputationGraph): The arithmetic expression to be evaluated composed of phenotypes combined by python arithmetic operations.
return_date (Union[str, Phenotype]): The date to be returned for the phenotype. Can be "first", "last", or a Phenotype object.
operate_on (str): The column to operate on. Can be "boolean" or "value".
populate (str): The column to populate. Can be "boolean" or "value".
reduce (bool): Whether to reduce the phenotype table to only include rows where the boolean column is True. This is only relevant if populate is "boolean".
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
"""

def __init__(
Expand Down Expand Up @@ -175,19 +182,33 @@ class ScorePhenotype(ComputationGraphPhenotype):
--> See the comparison table of CompositePhenotype classes
Parameters:
expression: The arithmetic expression to be evaluated composed of phenotypes combined by python arithmetic operations.
return_date: The date to be returned for the phenotype. Can be "first", "last", or a Phenotype object.
name: The name of the phenotype.
Attributes:
expression (ComputationGraph): The arithmetic expression to be evaluated composed of phenotypes combined by python arithmetic operations.
return_date (Union[str, Phenotype]): The date to be returned for the phenotype. Can be "first", "last", or a Phenotype object.
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
# Create component phenotypes individually
>> hypertension = Phenotype(Codelist('hypertension'))
>> chf = Phenotype(Codelist('chf'))
>> age_gt_45 = AgePhenotype(min_age=GreaterThan(45))
# Create the ScorePhenotype that defines a score which is 2*age + 1 if hypertension or chf are present, respectively. Notice that the boolean column of the component phenotypes are used for calculation and the value column is populated of the ScorePhenotype table.
>> pt = ScorePhenotype(
expression = 2 * age_gt_45 + hypertension + chf,
)
Methods:
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
Example:
```python
# Create component phenotypes individually
hypertension = Phenotype(Codelist('hypertension'))
hf = Phenotype(Codelist('chf'))
age_gt_45 = AgePhenotype(min_age=GreaterThan(45))
# Create the ScorePhenotype that defines a score which is 2*age + 1 if
# hypertension or chf are present, respectively. Notice that the boolean
# column of the component phenotypes are used for calculation and the value
# column is populated of the ScorePhenotype table.
pt = ScorePhenotype(
expression = 2 * age_gt_45 + hypertension + chf,
)
```
"""

def __init__(
Expand All @@ -209,17 +230,29 @@ class ArithmeticPhenotype(ComputationGraphPhenotype):
ArithmeticPhenotype is a composite phenotype that performs arithmetic operations using the **value** column of its component phenotypes and populations the **value** column. It should be used for calculating values such as BMI, GFR or converting units.
--> See the comparison table of CompositePhenotype classes
Attributes:
Parameters:
expression (ComputationGraph): The arithmetic expression to be evaluated composed of phenotypes combined by python arithmetic operations.
return_date (Union[str, Phenotype]): The date to be returned for the phenotype. Can be "first", "last", or a Phenotype object.
name: The name of the phenotype.
# Create component phenotypes individually
>> height = MeasurementPhenotype(Codelist('height'))
>> weight = MeasurementPhenotype(Codelist('weight'))
# Create the ArithmeticPhenotype that defines a score which is 2*age + 1 if hypertension or chf are present, respectively. Notice that the boolean column of the component phenotypes are used for calculation and the value column is populated of the ScorePhenotype table.
>> bmi = ArithmeticPhenotype(
expression = weight / height**2,
)
Attributes:
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
Example:
```python
# Create component phenotypes individually
height = MeasurementPhenotype(Codelist('height'))
weight = MeasurementPhenotype(Codelist('weight'))
# Create the ArithmeticPhenotype that defines the BMI score
bmi = ArithmeticPhenotype(
expression = weight / height**2,
)
```
"""

def __init__(
Expand All @@ -242,18 +275,17 @@ class LogicPhenotype(ComputationGraphPhenotype):
--> See the comparison table of CompositePhenotype classes
# Create component phenotypes individually
>> hypertension = Phenotype(Codelist('hypertension'))
>> chf = Phenotype(Codelist('chf'))
>> age_gt_45 = AgePhenotype(min_age=GreaterThan(45))
# Create the LogicPhenotype that returns the patients who are agreater than 45 years old and have both hypertension and chf. Notice that the logical operation operates on the boolean columns of the component phenotypes and populations the boolean column of the LogicPhenotype table.
>> pt = ScorePhenotype(
expression = age_gt_45 | hypertension | chf,
)
Attributes:
Parameters:
expression (ComputationGraph): The logical expression to be evaluated composed of phenotypes combined by python arithmetic operations.
return_date (Union[str, Phenotype]): The date to be returned for the phenotype. Can be "first", "last", or a Phenotype object.
name: The name of the phenotype.
Attributes:
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
"""

def __init__(
Expand Down
9 changes: 6 additions & 3 deletions phenex/phenotypes/death_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ class DeathPhenotype(Phenotype):
DeathPhenotype is a class that represents a death-based phenotype. It filters individuals
who have died and returns their date of death.
Attributes:
Parameters:
name (str): Name of the phenotype, default is 'death'.
domain (str): Domain of the phenotype, default is 'PERSON'.
children (list): List of dependent phenotypes.
Attributes:
table (PhenotypeTable): The resulting phenotype table after filtering (None until execute is called)
Methods:
_execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the filtered individuals.
execute(tables: Dict[str, Table]) -> PhenotypeTable:
Executes the phenotype calculation and returns a table with the computed age.
"""

def __init__(
Expand Down
Loading

0 comments on commit 918c9f0

Please sign in to comment.