Skip to content

Commit 9ab276e

Browse files
authored
Combine csv example into main examples notebook (#4022)
* Combine csv example into examples.ipynb and update test accordingly * fix
1 parent a188483 commit 9ab276e

3 files changed

Lines changed: 50 additions & 132 deletions

File tree

examples/csv_output.ipynb

Lines changed: 0 additions & 80 deletions
This file was deleted.

examples/examples.ipynb

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,12 @@
120120
")"
121121
]
122122
},
123-
{
124-
"cell_type": "code",
125-
"execution_count": null,
126-
"metadata": {},
127-
"outputs": [],
128-
"source": [
129-
"# Delete temp dir\n",
130-
"temp_dir.cleanup()"
131-
]
132-
},
133123
{
134124
"cell_type": "markdown",
135125
"metadata": {},
136126
"source": [
137127
"## View key output variables\n",
138-
"Run the large tokamak scenario using `SingleRun` to set some values on the `CostModel` instance and then print them."
128+
"Using the `MFILE` we generated by running the large tokamak scenario above, we have set some values on the `CostModel` instance and can print them."
139129
]
140130
},
141131
{
@@ -144,14 +134,19 @@
144134
"metadata": {},
145135
"outputs": [],
146136
"source": [
147-
"# Define input file name relative to project dir\n",
148-
"input_rel = script_dir / \"data/large_tokamak_IN.DAT\"\n",
149-
"print(input_rel)\n",
150-
"temp_dir, temp_input_path, _ = copy_to_temp_dir(input_rel)\n",
137+
"import process.data_structure\n",
151138
"\n",
152-
"# Run process on an input file\n",
153-
"single_run = SingleRun(temp_input_path.as_posix())\n",
154-
"single_run.run()"
139+
"# Print some values on the CostModel instance\n",
140+
"print(f\"Heat transport system: {process.data_structure.cost_variables.c226:.3e} M$\")\n",
141+
"print(f\"Electrical plant equipment: {process.data_structure.cost_variables.c24:.3e} M$\")"
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"metadata": {},
147+
"source": [
148+
"## Convert to CSV format\n",
149+
"This demonstrates how you would read from a PROCESS MFILE and write specified values into a csv using the `mfile_to_csv` function"
155150
]
156151
},
157152
{
@@ -160,11 +155,28 @@
160155
"metadata": {},
161156
"outputs": [],
162157
"source": [
163-
"import process.data_structure\n",
164-
"\n",
165-
"# Print some values on the CostModel instance\n",
166-
"print(f\"Heat transport system: {process.data_structure.cost_variables.c226:.3e} M$\")\n",
167-
"print(f\"Electrical plant equipment: {process.data_structure.cost_variables.c24:.3e} M$\")"
158+
"from process.io import mfile_to_csv\n",
159+
"\n",
160+
"data_dir = Path(\"data\")\n",
161+
"\n",
162+
"# mfile_to_csv requires two inputs:\n",
163+
"# - path to the MFILE\n",
164+
"# - .json containing the variable names to include in the csv file\n",
165+
"\n",
166+
"# This routine attempts to find every variable listed in the json file\n",
167+
"# in the MFILE and writes the variable name, description and value\n",
168+
"# to the output csv.\n",
169+
"# Any listed variable that isn't in that MFILE will be skipped.\n",
170+
"# The .csv file is saved to the directory of the input file\n",
171+
"\n",
172+
"mfile_to_csv.main(\n",
173+
" args=[\n",
174+
" \"-f\",\n",
175+
" (data_dir / \"large_tokamak_1_MFILE.DAT\").as_posix(),\n",
176+
" \"-v\",\n",
177+
" (data_dir / \"mfile_to_csv_vars.json\").as_posix(),\n",
178+
" ]\n",
179+
")"
168180
]
169181
},
170182
{

tests/examples/test_examples.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,12 @@ def test_examples(examples_temp_data):
4444
"""
4545
example_notebook_location = examples_temp_data / "examples.ipynb"
4646
with testbook(example_notebook_location, execute=True, timeout=600):
47-
pass
48-
49-
50-
def test_scan(examples_temp_data):
51-
"""Run scan.ipynb notebook check no exceptions are raised and that an MFILE is created.
52-
53-
scan.ipynb intentionally produces files when running the notebook, but remove
54-
them when testing.
55-
:param examples_temp_data: temporary dir containing examples files
56-
:type examples_temp_data: Path
57-
"""
58-
scan_notebook_location = examples_temp_data / "scan.ipynb"
59-
with testbook(scan_notebook_location, execute=True, timeout=1200):
60-
# Run entire scan.ipynb notebook and assert an MFILE is created
61-
assert os.path.exists(examples_temp_data / "data/scan_example_file_MFILE.DAT")
62-
63-
64-
def test_csv(examples_temp_data):
65-
"""Run csv_output.ipynb, check no exceptions are raised, check a csv file exists and check the csv file contains data.
66-
67-
csv_output.ipynb intentionally produces files when running the notebook, but remove
68-
them when testing.
69-
:param examples_temp_data: temporary dir containing examples files
70-
:type examples_temp_data: Path
71-
"""
72-
csv_notebook_location = examples_temp_data / "csv_output.ipynb"
73-
with testbook(csv_notebook_location, execute=True, timeout=600):
7447
# Check csv file is created
7548
assert os.path.exists(examples_temp_data / "data/large_tokamak_1_MFILE.csv")
7649

7750
# Read in the csv file created by test and check it contains positive floats
7851
readcsv = pd.read_csv(examples_temp_data / "data/large_tokamak_1_MFILE.csv")
79-
values = readcsv["Value"]
80-
value_array = np.array(values)
52+
value_array = np.array(readcsv["Value"])
8153
check_float = False
8254
check_positive = False
8355
value_array_type = value_array.dtype
@@ -91,6 +63,20 @@ def test_csv(examples_temp_data):
9163
assert check_positive
9264

9365

66+
def test_scan(examples_temp_data):
67+
"""Run scan.ipynb notebook check no exceptions are raised and that an MFILE is created.
68+
69+
scan.ipynb intentionally produces files when running the notebook, but remove
70+
them when testing.
71+
:param examples_temp_data: temporary dir containing examples files
72+
:type examples_temp_data: Path
73+
"""
74+
scan_notebook_location = examples_temp_data / "scan.ipynb"
75+
with testbook(scan_notebook_location, execute=True, timeout=1200):
76+
# Run entire scan.ipynb notebook and assert an MFILE is created
77+
assert os.path.exists(examples_temp_data / "data/scan_example_file_MFILE.DAT")
78+
79+
9480
def test_plot_solutions(examples_temp_data):
9581
"""Run plot_solutions.ipynb and check no exceptions are raised.
9682

0 commit comments

Comments
 (0)