Skip to content

Commit

Permalink
test model renames
Browse files Browse the repository at this point in the history
  • Loading branch information
shapiromatron committed May 3, 2024
1 parent a46ded4 commit 86c82e7
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 92 deletions.
20 changes: 10 additions & 10 deletions scripts/bmds-online-33-api.ipynb → scripts/bmds-online-api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# BMDS online verison 3.3 - API\n",
"# BMDS Online API\n",
"\n",
"A work in progress to demonstrate how to use the BMDS Online API."
]
Expand All @@ -16,9 +16,10 @@
"outputs": [],
"source": [
"import json\n",
"import requests\n",
"import time\n",
"from pathlib import Path\n",
"import time"
"\n",
"import requests"
]
},
{
Expand Down Expand Up @@ -62,8 +63,8 @@
" \"incidences\": [0, 0, 3, 2, 3],\n",
" \"model_type\": \"DM\",\n",
" \"column_names\": {\n",
" \"ns\": \"N\", \n",
" \"doses\": \"Dose\", \n",
" \"ns\": \"N\",\n",
" \"doses\": \"Dose\",\n",
" \"incidences\": \"Incidence\"\n",
" },\n",
" \"dataset_name\": \"Dataset #1\",\n",
Expand Down Expand Up @@ -124,8 +125,7 @@
" print(\"Polling...\")\n",
" time.sleep(1)\n",
" resp = requests.get(poll_url)\n",
" output = resp.json()\n",
" "
" output = resp.json()\n"
]
},
{
Expand Down Expand Up @@ -334,7 +334,7 @@
"source": [
"excel_url = root + output['word_url']\n",
"resp = requests.get(excel_url)\n",
"Path('/Users/shapiromatron/Desktop/demo.docx').write_bytes(resp.content)"
"Path('~/Desktop/demo.docx').write_bytes(resp.content)"
]
},
{
Expand All @@ -356,7 +356,7 @@
"source": [
"word_url = root + output['excel_url']\n",
"resp = requests.get(word_url)\n",
"Path('/Users/shapiromatron/Desktop/demo.xlsx').write_bytes(resp.content)"
"Path('~/Desktop/demo.xlsx').write_bytes(resp.content)"
]
},
{
Expand Down Expand Up @@ -393,7 +393,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
34 changes: 17 additions & 17 deletions tests/analysis/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def check_error(response: dict, type: str, loc: list, msg: str):

@pytest.mark.django_db
class TestAnalysisViewSet:
def test_auth(self, bmds3_complete_continuous):
def test_auth(self, complete_continuous):
"""
Check API auth cases.
Expand All @@ -33,7 +33,7 @@ def test_auth(self, bmds3_complete_continuous):
read_url = analysis.get_api_url()
payload = {
"editKey": analysis.password,
"data": bmds3_complete_continuous,
"data": complete_continuous,
}

# client; no CSRF
Expand Down Expand Up @@ -142,43 +142,43 @@ def test_patch_partial(self):
assert response.status_code == 200
assert response.json()["inputs"] == payload["data"]

def test_patch_complete_continuous(self, bmds3_complete_continuous):
def test_patch_complete_continuous(self, complete_continuous):
client = APIClient()
analysis = Analysis.objects.create()
url = analysis.get_api_patch_inputs_url()

# complete bmds3 continuous
payload = {"editKey": analysis.password, "data": bmds3_complete_continuous}
# complete continuous
payload = {"editKey": analysis.password, "data": complete_continuous}
response = client.patch(url, payload, format="json")
assert response.status_code == 200
assert response.json()["inputs"] == payload["data"]

def test_optional_recommender(self, bmds3_complete_continuous):
def test_optional_recommender(self, complete_continuous):
client = APIClient()
analysis = Analysis.objects.create()
url = analysis.get_api_patch_inputs_url()

# complete bmds3 continuous
payload = {"editKey": analysis.password, "data": bmds3_complete_continuous}
# complete continuous
payload = {"editKey": analysis.password, "data": complete_continuous}
del payload["data"]["recommender"]
response = client.patch(url, payload, format="json")
assert response.status_code == 200
assert response.json()["inputs"] == payload["data"]

def test_patch_complete_dichotomous(self, bmds3_complete_dichotomous):
def test_patch_complete_dichotomous(self, complete_dichotomous):
client = APIClient()
analysis = Analysis.objects.create()
url = analysis.get_api_patch_inputs_url()

# complete bmds3 dichotomous
payload = {"editKey": analysis.password, "data": bmds3_complete_dichotomous}
# complete dichotomous
payload = {"editKey": analysis.password, "data": complete_dichotomous}
response = client.patch(url, payload, format="json")
assert response.status_code == 200
assert response.json()["inputs"] == payload["data"]

def test_execute(self, bmds3_complete_dichotomous):
def test_execute(self, complete_dichotomous):
client = APIClient()
analysis = Analysis.objects.create(inputs=bmds3_complete_dichotomous)
analysis = Analysis.objects.create(inputs=complete_dichotomous)
assert analysis.started is None
url = analysis.get_api_execute_url()

Expand All @@ -196,9 +196,9 @@ def test_execute(self, bmds3_complete_dichotomous):
assert response.data["has_errors"] is False
assert bmd == pytest.approx(164.3, rel=0.05)

def test_reset_execute(self, bmds3_complete_dichotomous):
def test_reset_execute(self, complete_dichotomous):
client = APIClient()
analysis = Analysis.objects.create(inputs=bmds3_complete_dichotomous)
analysis = Analysis.objects.create(inputs=complete_dichotomous)
analysis.execute()
url = analysis.get_api_execute_reset_url()

Expand All @@ -215,9 +215,9 @@ def test_reset_execute(self, bmds3_complete_dichotomous):
assert response.data["has_errors"] is False
assert response.data["outputs"] == {}

def test_model_selection(self, bmds3_complete_dichotomous):
def test_model_selection(self, complete_dichotomous):
client = APIClient()
analysis = Analysis.objects.create(inputs=bmds3_complete_dichotomous)
analysis = Analysis.objects.create(inputs=complete_dichotomous)
analysis.execute()

url = analysis.get_api_url() + "select-model/"
Expand Down
44 changes: 22 additions & 22 deletions tests/analysis/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@


class TestAnalysisSession:
def test_default_dichotomous(self, bmds3_complete_dichotomous):
def test_default_dichotomous(self, complete_dichotomous):
# assure a default dataset can be created
data = deepcopy(bmds3_complete_dichotomous)
data = deepcopy(complete_dichotomous)
session = AnalysisSession.create(data, 0, 0)
assert len(session.frequentist.models) == 1
assert len(session.bayesian.models) == 1

def test_default_continuous(self, bmds3_complete_continuous):
def test_default_continuous(self, complete_continuous):
# assure a default dataset can be created
data = deepcopy(bmds3_complete_continuous)
data = deepcopy(complete_continuous)
session = AnalysisSession.create(data, 0, 0)
assert len(session.frequentist.models) == 1
assert len(session.bayesian.models) == 1

def test_default_continuous_individual(self, bmds3_complete_continuous_individual):
def test_default_continuous_individual(self, complete_continuous_individual):
# assure a default dataset can be created
data = deepcopy(bmds3_complete_continuous_individual)
data = deepcopy(complete_continuous_individual)
session = AnalysisSession.create(data, 0, 0)
assert len(session.frequentist.models) == 1
assert len(session.bayesian.models) == 1

def test_prior_classes(self, bmds3_complete_dichotomous):
def test_prior_classes(self, complete_dichotomous):
# assure a default dataset can be created
data = deepcopy(bmds3_complete_dichotomous)
data = deepcopy(complete_dichotomous)
data["models"] = {
"frequentist_restricted": ["Gamma"],
"frequentist_unrestricted": ["Gamma"],
Expand All @@ -50,8 +50,8 @@ def test_prior_classes(self, bmds3_complete_dichotomous):
)
assert session.bayesian.models[0].settings.priors.prior_class is PriorClass.bayesian

def test_exponential_unpacking(self, bmds3_complete_continuous):
data = deepcopy(bmds3_complete_continuous)
def test_exponential_unpacking(self, complete_continuous):
data = deepcopy(complete_continuous)
data["models"] = {
"frequentist_restricted": ["Exponential"],
"bayesian": [{"model": "Exponential", "prior_weight": 1}],
Expand All @@ -64,7 +64,7 @@ def test_exponential_unpacking(self, bmds3_complete_continuous):
assert session.bayesian.models[0].bmd_model_class.id == ContinuousModelIds.c_exp_m3
assert session.bayesian.models[1].bmd_model_class.id == ContinuousModelIds.c_exp_m5

def test_multistage_permutations(self, bmds3_complete_dichotomous):
def test_multistage_permutations(self, complete_dichotomous):
def _expected_degree(session, n: int):
assert session.bayesian is None
assert len(session.frequentist.models) == n
Expand All @@ -74,14 +74,14 @@ def _expected_degree(session, n: int):
assert degrees == set(list(range(1, n + 1)))

# degree = 1
data = deepcopy(bmds3_complete_dichotomous)
data = deepcopy(complete_dichotomous)
data["models"] = {"frequentist_restricted": ["Multistage"]}
data["dataset_options"][0]["degree"] = 1
session = AnalysisSession.create(data, 0, 0)
_expected_degree(session, 1)

# degree = 2
data = deepcopy(bmds3_complete_dichotomous)
data = deepcopy(complete_dichotomous)
data["models"] = {"frequentist_restricted": ["Multistage"]}
data["dataset_options"][0]["degree"] = 2
session = AnalysisSession.create(data, 0, 0)
Expand All @@ -90,7 +90,7 @@ def _expected_degree(session, n: int):
# 3 dose-groups; degree = N-1; expected 2
for num_doses in range(3, 8):
expected_degree = min(max(num_doses - 1, 2), 8)
data = deepcopy(bmds3_complete_dichotomous)
data = deepcopy(complete_dichotomous)
data["datasets"] = [
{
"dtype": "D",
Expand All @@ -107,7 +107,7 @@ def _expected_degree(session, n: int):
_expected_degree(session, expected_degree)

# degree = N -1, bayesian, fixed at degree == 2
data = deepcopy(bmds3_complete_dichotomous)
data = deepcopy(complete_dichotomous)
data["models"] = {"bayesian": [{"model": "Multistage", "prior_weight": 1}]}
data["dataset_options"][0]["degree"] = 0
session = AnalysisSession.create(data, 0, 0)
Expand All @@ -117,9 +117,9 @@ def _expected_degree(session, n: int):
assert model.bmd_model_class.id == DichotomousModelIds.d_multistage
assert model.settings.degree == 2

def test_polynomial_unpacking(self, bmds3_complete_continuous):
def test_polynomial_unpacking(self, complete_continuous):
# test linear; degree 0
data = deepcopy(bmds3_complete_continuous)
data = deepcopy(complete_continuous)
data["models"] = {"frequentist_unrestricted": ["Linear"]}
data["dataset_options"][0]["degree"] = 0
session = AnalysisSession.create(data, 0, 0)
Expand All @@ -128,7 +128,7 @@ def test_polynomial_unpacking(self, bmds3_complete_continuous):
assert session.bayesian is None

# test polynomial; degree 2
data = deepcopy(bmds3_complete_continuous)
data = deepcopy(complete_continuous)
data["models"] = {"frequentist_unrestricted": ["Polynomial"]}
data["dataset_options"][0]["degree"] = 2
session = AnalysisSession.create(data, 0, 0)
Expand All @@ -137,7 +137,7 @@ def test_polynomial_unpacking(self, bmds3_complete_continuous):
assert session.bayesian is None

# test polynomial; degree 3
data = deepcopy(bmds3_complete_continuous)
data = deepcopy(complete_continuous)
data["models"] = {"frequentist_unrestricted": ["Polynomial"]}
data["dataset_options"][0]["degree"] = 3
session = AnalysisSession.create(data, 0, 0)
Expand All @@ -147,7 +147,7 @@ def test_polynomial_unpacking(self, bmds3_complete_continuous):
assert session.bayesian is None

# test linear + polynomial; degree 3
data = deepcopy(bmds3_complete_continuous)
data = deepcopy(complete_continuous)
data["models"] = {"frequentist_unrestricted": ["Linear", "Polynomial"]}
data["dataset_options"][0]["degree"] = 3
session = AnalysisSession.create(data, 0, 0)
Expand All @@ -158,8 +158,8 @@ def test_polynomial_unpacking(self, bmds3_complete_continuous):
assert session.bayesian is None

# disttype 3 Linear and power are not added
def test_disttype(self, bmds3_complete_continuous):
data = deepcopy(bmds3_complete_continuous)
def test_disttype(self, complete_continuous):
data = deepcopy(complete_continuous)
data["models"] = {
"frequentist_restricted": ["Exponential", "Hill", "Linear", "Power"],
}
Expand Down
34 changes: 17 additions & 17 deletions tests/analysis/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def write_excel(data: dict, path: Path):


@pytest.mark.django_db()
class TestBmds3Execution:
def test_continuous(self, bmds3_complete_continuous, data_path, rewrite_data_files):
analysis = Analysis.objects.create(inputs=bmds3_complete_continuous)
class TestExecution:
def test_continuous(self, complete_continuous, data_path, rewrite_data_files):
analysis = Analysis.objects.create(inputs=complete_continuous)

assert analysis.is_finished is False
assert analysis.has_errors is False
Expand All @@ -38,13 +38,13 @@ def test_continuous(self, bmds3_complete_continuous, data_path, rewrite_data_fil
assert len(df) == 3

if rewrite_data_files:
write_excel(df, data_path / "continuous.xlsx")
(data_path / "continuous.docx").write_bytes(docx.getvalue())
write_excel(df, data_path / "reports/continuous.xlsx")
(data_path / "reports/continuous.docx").write_bytes(docx.getvalue())

def test_continuous_individual(
self, bmds3_complete_continuous_individual, data_path, rewrite_data_files
self, complete_continuous_individual, data_path, rewrite_data_files
):
analysis = Analysis.objects.create(inputs=bmds3_complete_continuous_individual)
analysis = Analysis.objects.create(inputs=complete_continuous_individual)

assert analysis.is_finished is False
assert analysis.has_errors is False
Expand All @@ -66,11 +66,11 @@ def test_continuous_individual(
assert len(df) == 3

if rewrite_data_files:
write_excel(df, data_path / "continuous_individual.xlsx")
(data_path / "continuous_individual.docx").write_bytes(docx.getvalue())
write_excel(df, data_path / "reports/continuous_individual.xlsx")
(data_path / "reports/continuous_individual.docx").write_bytes(docx.getvalue())

def test_dichotomous(self, bmds3_complete_dichotomous, data_path, rewrite_data_files):
analysis = Analysis.objects.create(inputs=bmds3_complete_dichotomous)
def test_dichotomous(self, complete_dichotomous, data_path, rewrite_data_files):
analysis = Analysis.objects.create(inputs=complete_dichotomous)

assert analysis.is_finished is False
assert analysis.has_errors is False
Expand All @@ -92,8 +92,8 @@ def test_dichotomous(self, bmds3_complete_dichotomous, data_path, rewrite_data_f
assert len(df) == 3

if rewrite_data_files:
write_excel(df, data_path / "dichotomous.xlsx")
(data_path / "dichotomous.docx").write_bytes(docx.getvalue())
write_excel(df, data_path / "reports/dichotomous.xlsx")
(data_path / "reports/dichotomous.docx").write_bytes(docx.getvalue())

def test_nested_dichotomous(self, bmds_complete_nd, data_path, rewrite_data_files):
analysis = Analysis.objects.create(inputs=bmds_complete_nd)
Expand All @@ -118,8 +118,8 @@ def test_nested_dichotomous(self, bmds_complete_nd, data_path, rewrite_data_file
assert len(df) == 3

if rewrite_data_files:
write_excel(df, data_path / "nested_dichotomous.xlsx")
(data_path / "nested_dichotomous.docx").write_bytes(docx.getvalue())
write_excel(df, data_path / "reports/nested_dichotomous.xlsx")
(data_path / "reports/nested_dichotomous.docx").write_bytes(docx.getvalue())

def test_multitumor(self, bmds_complete_mt, data_path, rewrite_data_files):
analysis = Analysis.objects.create(inputs=bmds_complete_mt)
Expand All @@ -144,5 +144,5 @@ def test_multitumor(self, bmds_complete_mt, data_path, rewrite_data_files):
assert len(df) == 3

if rewrite_data_files:
write_excel(df, data_path / "multitumor.xlsx")
(data_path / "multitumor.docx").write_bytes(docx.getvalue())
write_excel(df, data_path / "reports/multitumor.xlsx")
(data_path / "reports/multitumor.docx").write_bytes(docx.getvalue())
Loading

0 comments on commit 86c82e7

Please sign in to comment.