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

This PR addressed in operator in cohort (#308) and cohort constraint in features endpoint (#305) #309

Merged
merged 6 commits into from
Mar 27, 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
28 changes: 16 additions & 12 deletions icees_api/features/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def op_dict(k, v, table_=None):
if k_op_val_dict['operator'] == '>':
value = value + 1


# v is a dict with "operator" key; other keys depend on the "operator" value
operations = {
">": lambda: value > simplify_value(v["value"], v["operator"]),
Expand All @@ -68,7 +67,9 @@ def op_dict(k, v, table_=None):
"<>": lambda: value != simplify_value(v["value"], v["operator"]),
"between": lambda: between(value, simplify_value(v["value_a"], v["operator"]),
simplify_value(v["value_b"], v["operator"])),
"in": lambda: value.in_([simplify_value(val, v["operator"]) for val in v["values"]])
"in": lambda: value.in_(
[simplify_value(val, v["operator"]) for val in v["values"]]
) if table_ is not None else value in [simplify_value(val, v["operator"]) for val in v["values"]]
}
return operations[v["operator"]]()

Expand Down Expand Up @@ -104,7 +105,6 @@ def case_select2(table1, table2, k, v, k2, v2):
def select_cohort(conn, table_name, year, cohort_features, cohort_id=None):
"""Select cohort."""
cohort_features_norm = normalize_features(year, cohort_features)

gen_table, _, pk = generate_tables_from_features(table_name, cohort_features_norm, year, [])
s = select([func.count(column(pk).distinct())]).select_from(gen_table)
n = conn.execute(s).scalar()
Expand Down Expand Up @@ -189,14 +189,11 @@ def get_cohort_by_id(conn, table_name, year, cohort_id):
}


def get_cohort_features(conn, table_name, year, cohort_features, cohort_year):
def get_cohort_features(conn, table_name, feats, year, cohort_features, cohort_year):
"""Get cohort features."""
rs = []
for k in get_features(conn, table_name):
# k = f.name
# levels = f.options
# if levels is None:
levels = get_feature_levels(k)
for k in feats:
levels = get_feature_levels(k, year=year, cohort_feat_dict=cohort_features)
ret = select_feature_count_all_values(
conn,
table_name,
Expand Down Expand Up @@ -290,9 +287,9 @@ def generate_tables_from_features(
cohort_features,
cohort_year,
columns,
primary_key='PatientId'
):
"""Generate tables from features."""
primary_key = table_name[0].upper() + table_name[1:] + "Id"
table_ = table(table_name, column(primary_key))

table_cohorts = []
Expand Down Expand Up @@ -1025,7 +1022,6 @@ def compute_multivariate_table(conn, table_name, year, cohort_id, feature_variab
cohort_meta = get_features_by_id(conn, table_name, cohort_id)
if cohort_meta is None:
raise ValueError("Input cohort_id invalid. Please try again.")

cohort_features, cohort_year = cohort_meta
feat_len = len(feature_variables)
if feat_len < 3:
Expand All @@ -1036,7 +1032,8 @@ def compute_multivariate_table(conn, table_name, year, cohort_id, feature_variab
feat_constraint_list = get_operator_and_value(get_feature_levels(feature_variables[0], year=year,
cohort_feat_dict=cohort_features),
feature_variables[0], append_feature_variable=True)

if not feat_constraint_list:
raise HTTPException(status_code=400, detail=f"{feature_variables[0]} is not a valid feature variable")
index = 1
while index + 2 <= feat_len:
feature_as = [
Expand All @@ -1047,6 +1044,8 @@ def compute_multivariate_table(conn, table_name, year, cohort_id, feature_variab
feature_variables[index])
}
]
if not feature_as[0]['feature_qualifiers']:
raise HTTPException(status_code=400, detail=f"{feature_variables[index]} is not a valid feature variable")
feature_bs = [
{
"feature_name": feature_variables[index + 1],
Expand All @@ -1056,6 +1055,9 @@ def compute_multivariate_table(conn, table_name, year, cohort_id, feature_variab
feature_variables[index + 1])
}
]
if not feature_bs[0]['feature_qualifiers']:
raise HTTPException(status_code=400,
detail=f"{feature_variables[index + 1]} is not a valid feature variable")
# add more constraints to feat_constraint_list as needed depending on feature_a and feature_b levels
more_constraint_list = []
for feature_constraint in feat_constraint_list:
Expand All @@ -1081,6 +1083,8 @@ def compute_multivariate_table(conn, table_name, year, cohort_id, feature_variab
feature_qualifiers = get_operator_and_value(get_feature_levels(feature_variables[index], year=year,
cohort_feat_dict=cohort_features),
feature_variables[index])
if not feature_qualifiers:
raise HTTPException(status_code=400, detail=f"{feature_variables[index]} is not a valid feature variable")
more_constraint_list = []
for feature_constraint in feat_constraint_list:
for fq in feature_qualifiers:
Expand Down
10 changes: 10 additions & 0 deletions icees_api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,25 @@ def features(
if cohort_meta is None:
return_value = "Input cohort_id invalid. Please try again."
else:
feature_list = sql.get_features(conn, table)
cohort_features, cohort_year = cohort_meta
# compute frequency for each feature constraint
if cohort_features:
# compute frequency on the cohort view
table = sql.create_cohort_view(conn, table, cohort_features)

return_value = sql.get_cohort_features(
conn,
table,
feature_list,
year,
cohort_features,
cohort_year,
)

if cohort_features:
sql.drop_cohort_view(conn, cohort_features)

return {"return value": return_value}


Expand Down
23 changes: 9 additions & 14 deletions test/api/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,17 @@ def test_cohort_dictionary():
} in resp_json1["return value"]


feature_variables = [
{
"feature_name": "AgeStudyStart",
"feature_qualifier": {
"operator": "=",
"value": "0-2"
}
}, {
"feature_name": "AvgDailyPM2.5Exposure",
"feature_qualifier": {
"operator": "=",
"value": 1
feature_variables = {
"AgeStudyStart": {
"operator": "=",
"value": "0-2"
},
"year": 2011
"AvgDailyPM2.5Exposure": {
"operator": "=",
"value": 1
},
"year": {"operator": "=", "value": 2011}
}
]


@load_data(
Expand Down
Loading