From 5682c447db86bff4939bca60d8cf6be37aecdf0d Mon Sep 17 00:00:00 2001 From: Hao Xu Date: Wed, 9 Dec 2020 23:50:36 -0500 Subject: [PATCH] fix problems after merging fastapi and cohort_year --- app.py | 7 ++++++- dbutils.py | 2 -- features/model.py | 4 ++-- sample.py | 7 +++---- test/docker-compose.yml | 2 +- test/test_api.py | 2 +- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 609bf220..05f2084b 100644 --- a/app.py +++ b/app.py @@ -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 @@ -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": diff --git a/dbutils.py b/dbutils.py index c77aef9d..8e7cbc66 100644 --- a/dbutils.py +++ b/dbutils.py @@ -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 diff --git a/features/model.py b/features/model.py index eac51fd5..8e925d29 100644 --- a/features/model.py +++ b/features/model.py @@ -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) ] + [ @@ -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)))) diff --git a/sample.py b/sample.py index 7c70fbaa..7f4b1f23 100644 --- a/sample.py +++ b/sample.py @@ -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 @@ -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: diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 5034dd37..826bf8c4 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -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: diff --git a/test/test_api.py b/test/test_api.py index 016df0ad..e98fa917 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -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