Skip to content

Commit d4e2e09

Browse files
author
Nicolas Legrand
authored
Update the test suite for cardioception.reports.report (#66)
1 parent c5c4ba3 commit d4e2e09

File tree

8 files changed

+1571
-1563
lines changed

8 files changed

+1571
-1563
lines changed

MANIFEST.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ include requirements.txt
77
include cardioception/HRD/Images/*
88
include cardioception/HRD/Sounds/*
99
include cardioception/HBC/Images/*
10-
include cardioception/HBC/Sounds/*
11-
include cardioception/notebooks/data/HBC/*
12-
include cardioception/notebooks/data/HRD/*
10+
include cardioception/HBC/Sounds/*

cardioception/notebooks/HeartBeatCounting.ipynb

Lines changed: 672 additions & 673 deletions
Large diffs are not rendered by default.

cardioception/notebooks/HeartRateDiscrimination.ipynb

Lines changed: 829 additions & 828 deletions
Large diffs are not rendered by default.

cardioception/reports.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import subprocess
55
from os import PathLike
6+
from pathlib import Path
67
from typing import Optional, Union
78

89
import numpy as np
@@ -247,8 +248,8 @@ def report(result_path: str, report_path: Optional[str] = None, task: str = "HRD
247248

248249
if report_path is None:
249250
report_path = result_path
250-
temp_notebook = os.path.join(report_path, "temp.ipynb")
251-
htmlreport = os.path.join(report_path, f"{task}_report.html")
251+
temp_notebook = Path(report_path, "temp.ipynb")
252+
htmlreport = Path(report_path, f"{task}_report.html")
252253

253254
if task == "HRD":
254255
template = "HeartRateDiscrimination.ipynb"
@@ -258,11 +259,11 @@ def report(result_path: str, report_path: Optional[str] = None, task: str = "HRD
258259
execute_notebook(
259260
pkg_resources.resource_filename("cardioception.notebooks", template),
260261
temp_notebook,
261-
parameters=dict(resultPath=result_path, reportPath=report_path),
262+
parameters=dict(resultPath=str(result_path), reportPath=str(report_path)),
262263
)
263264
command = (
264265
"jupyter nbconvert --to html --execute "
265266
+ f"--TemplateExporter.exclude_input=True {temp_notebook} --output {htmlreport}"
266267
)
267-
subprocess.call(command)
268+
subprocess.call(command, shell=True)
268269
os.remove(temp_notebook)

docs/source/examples/templates/HeartBeatCounting.ipynb

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,29 @@
8686
{
8787
"cell_type": "code",
8888
"execution_count": 4,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"# ensure that the paths are pathlib instance in case they are passed through cardioception.reports.report\n",
93+
"resultPath = Path(resultPath)\n",
94+
"reportPath = Path(reportPath)"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 5,
89100
"metadata": {
90101
"id": "8cYkrDMvKdaY"
91102
},
92103
"outputs": [],
93104
"source": [
94105
"# Search files ending with \"final.txt\" - This is the main data frame that is saved at the end of the task\n",
95-
"results_df = [file for file in Path(Path.cwd(), \"data\", \"HBC\").glob('*final.txt')]"
106+
"results_df = [file for file in Path(resultPath).glob('*final.txt')]"
96107
]
97108
},
98109
{
99110
"cell_type": "code",
100-
"execution_count": 5,
111+
"execution_count": 6,
101112
"metadata": {
102113
"colab": {
103114
"base_uri": "https://localhost:8080/",
@@ -205,7 +216,7 @@
205216
"5 5 23 Count 25 5 2.635"
206217
]
207218
},
208-
"execution_count": 5,
219+
"execution_count": 6,
209220
"metadata": {},
210221
"output_type": "execute_result"
211222
}
@@ -218,7 +229,7 @@
218229
},
219230
{
220231
"cell_type": "code",
221-
"execution_count": 6,
232+
"execution_count": 7,
222233
"metadata": {
223234
"id": "xN2uZaX6Kdaa"
224235
},
@@ -228,7 +239,7 @@
228239
"ppg = {}\n",
229240
"for i in range(6):\n",
230241
" ppg[str(i)] = np.load(\n",
231-
" [file for file in Path(Path.cwd(), \"data\", \"HBC\").glob(f'*_{i}.npy')][0]\n",
242+
" [file for file in resultPath.glob(f'*_{i}.npy')][0]\n",
232243
" )"
233244
]
234245
},
@@ -263,7 +274,7 @@
263274
},
264275
{
265276
"cell_type": "code",
266-
"execution_count": 7,
277+
"execution_count": 8,
267278
"metadata": {
268279
"colab": {
269280
"base_uri": "https://localhost:8080/",
@@ -508,7 +519,7 @@
508519
},
509520
{
510521
"cell_type": "code",
511-
"execution_count": 8,
522+
"execution_count": 9,
512523
"metadata": {
513524
"id": "uXUtLU1lKdad"
514525
},
@@ -521,7 +532,7 @@
521532
},
522533
{
523534
"cell_type": "code",
524-
"execution_count": 9,
535+
"execution_count": 10,
525536
"metadata": {
526537
"colab": {
527538
"base_uri": "https://localhost:8080/",
@@ -651,7 +662,7 @@
651662
"5 0.916667 "
652663
]
653664
},
654-
"execution_count": 9,
665+
"execution_count": 10,
655666
"metadata": {},
656667
"output_type": "execute_result"
657668
}
@@ -662,7 +673,7 @@
662673
},
663674
{
664675
"cell_type": "code",
665-
"execution_count": 10,
676+
"execution_count": 11,
666677
"metadata": {
667678
"id": "-PuGeDAkKdaf"
668679
},

docs/source/examples/templates/HeartRateDiscrimination.ipynb

Lines changed: 30 additions & 17 deletions
Large diffs are not rendered by default.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ def get_requirements():
4040
package_data={
4141
"cardioception.HBC": ["*.wav", "*.png"],
4242
"cardioception.HRD": ["*.wav", "*.png"],
43-
"cardioception.notebooks": ["*.ipynb", "*.npy", "*.txt"],
43+
"cardioception.notebooks": ["*.ipynb"],
4444
},
4545
)

tests/test_reports.py

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
from unittest import TestCase
66
import pandas as pd
77
from pathlib import Path
8-
import cardioception
98
from cardioception.reports import report, preprocessing
109

1110

12-
class TestHRD(TestCase):
11+
class TestReports(TestCase):
1312
def test_preprocessing(self):
1413
"""Test the preprocessing function"""
1514
# load the main result data frame
16-
results = pd.read_csv(Path(
17-
Path(cardioception.__file__).parent, "notebooks", "data", "HRD", "HRD_final.txt"
18-
)
19-
)
20-
15+
results = pd.read_csv(
16+
"https://raw.githubusercontent.com/embodied-computation-group/Cardioception/master/docs/source/examples/templates/data/HRD/HRD_final.txt"
17+
)
2118
preprocessing(results=results)
2219

2320

@@ -27,30 +24,18 @@ def test_report(self):
2724
#####
2825
# HRD
2926
#####
30-
hrd_resultsPath = os.path.join(
31-
os.path.dirname(cardioception.__file__), "notebooks", "data", "HRD"
32-
)
33-
34-
hrd_reportPath = os.path.join(
35-
os.path.dirname(cardioception.__file__), "notebooks", "data", "HRD"
36-
)
37-
report(resultPath=hrd_resultsPath, reportPath=hrd_reportPath)
38-
39-
os.remove(os.path.join(hrd_reportPath, "HRD_report.html"))
27+
hrd_results_path = Path(Path.cwd(), "docs", "source", "examples", "templates", "data", "HRD")
28+
hrd_report_path = Path.cwd()
29+
report(result_path=hrd_results_path, report_path=hrd_report_path)
30+
#os.remove(Path(hrd_report_path, "HRD_report.html"))
4031

4132
#####
4233
# HBC
4334
#####
44-
hbc_resultsPath = os.path.join(
45-
os.path.dirname(cardioception.__file__), "notebooks", "data", "HBC"
46-
)
47-
48-
hbc_reportPath = os.path.join(
49-
os.path.dirname(cardioception.__file__), "notebooks", "data", "HBC"
50-
)
51-
report(resultPath=hbc_resultsPath, reportPath=hbc_reportPath, task="HBC")
52-
53-
os.remove(os.path.join(hbc_reportPath, "HBC_report.html"))
35+
hbc_results_path = Path(Path.cwd(), "docs", "source", "examples", "templates", "data", "HBC")
36+
hbc_report_path = Path.cwd()
37+
report(result_path=hbc_results_path, report_path=hbc_report_path, task="HBC")
38+
#os.remove(Path(hbc_report_path, "HBC_report.html"))
5439

5540

5641
if __name__ == "__main__":

0 commit comments

Comments
 (0)