Skip to content

Commit

Permalink
fix problems after merging fastapi and cohort_year
Browse files Browse the repository at this point in the history
  • Loading branch information
xu-hao committed Dec 10, 2020
1 parent 07e7bca commit 5682c44
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
from time import strftime
from typing import Any
from jsonschema import ValidationError

from fastapi import FastAPI, Request
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -114,9 +115,13 @@ def wrapper(*args, request=None, **kwargs):
# run func, logging errors
try:
return_value = func(*args, **kwargs)

except ValidationError as err:
LOGGER.exception(err)
return_value = {"return value": e.message}
except Exception as err:
LOGGER.exception(err)
raise err
return_value = {"return value": str(err)}

# return tabular data, if requested
if request.headers["accept"] == "text/tabular":
Expand Down
2 changes: 0 additions & 2 deletions dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def create_indices():
itrunc = 0
def truncate(a, length=63):
logger.info("creating index " + a)
sys.stdout.flush()
sys.stderr.flush()
nonlocal itrunc
prefix = "index" + str(itrunc)
itrunc += 1
Expand Down
4 changes: 2 additions & 2 deletions features/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def select_feature_matrix(conn, table_name, year, cohort_features, cohort_year,
yb = feature_b_norm["year"]

table, table_matrices = generate_tables_from_features(table_name, cohort_features_norm, cohort_year, [(ka, ya), (kb, yb)])

selections = [
case_select2(table_matrices[yb], table_matrices[ya], kb, vb, ka, va, table_name=table_name) for vb, va in product(vbs, vas)
] + [
Expand Down Expand Up @@ -567,7 +567,7 @@ def select_feature_count(conn, table_name, year, cohort_features, cohort_year, f


def get_feature_levels(conn, table, year, feature):
s = select([table.c[feature]]).where(table.c.year == year).distinct().order_by(table.c[feature])
s = select([table.c[feature]]).where(table.c.year == year).where(table.c[feature] != None).distinct().order_by(table.c[feature])
return list(map(lambda row: row[0], conn.execute((s))))


Expand Down
7 changes: 3 additions & 4 deletions sample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from features import features
import pandas as pd
import numpy as np
from sqlalchemy import Integer, String, Enum
import sys
import argparse

Expand All @@ -18,12 +17,12 @@ def generate_data(table_name, years, n, fn):
col = f.name
levels = f.options
if levels is None:
if t == Integer:
if t == int:
df[col] = np.random.randint(10, size=n)
elif t == String:
elif t == str:
df[col] = [''.join(chr(x + 97) for x in np.random.randint(26, size=2)) for _ in range(n)]
else:
print ("error: " + col)
print ("error: " + col + " " + str(t))
else:
df[col] = np.random.choice(levels, size=n)
if df_all is None:
Expand Down
2 changes: 1 addition & 1 deletion test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
context: .
dockerfile: test/Dockerfile
image: icees-api-test:0.2.0
command: ["-s", "-vv"] # , "test/test_api.py::test_feature_association2_explicit_check_coverage_is_full"]
command: ["-s", "-vv"] # , "test/test_api.py::test_cohort_dictionary_explicit_tabular"]
environment:
WAIT: 60
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ def test_cohort_dictionary_explicit_tabular():
}
}]
resp = requests.post(prot + "://"+host+":"+str(port)+"/{0}/{1}/cohort".format(table, year), json=feature_variables, headers = json_headers, verify = False)
resp2 = requests.post(prot + "://"+host+":"+str(port)+"/{0}/{1}/cohort".format(table, year), headers = {"Accept": "text/tabular"}, verify = False)
resp2 = requests.post(prot + "://"+host+":"+str(port)+"/{0}/{1}/cohort".format(table, year), json=feature_variables, headers = {"Content-Type": "application/json", "Accept": "text/tabular"}, verify = False)

assert resp2.status_code == 200

Expand Down

0 comments on commit 5682c44

Please sign in to comment.