Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions examples/csv_output.ipynb

This file was deleted.

58 changes: 35 additions & 23 deletions examples/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,12 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Delete temp dir\n",
"temp_dir.cleanup()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## View key output variables\n",
"Run the large tokamak scenario using `SingleRun` to set some values on the `CostModel` instance and then print them."
"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."
]
},
{
Expand All @@ -144,14 +134,19 @@
"metadata": {},
"outputs": [],
"source": [
"# Define input file name relative to project dir\n",
"input_rel = script_dir / \"data/large_tokamak_IN.DAT\"\n",
"print(input_rel)\n",
"temp_dir, temp_input_path, _ = copy_to_temp_dir(input_rel)\n",
"import process.data_structure\n",
"\n",
"# Run process on an input file\n",
"single_run = SingleRun(temp_input_path.as_posix())\n",
"single_run.run()"
"# Print some values on the CostModel instance\n",
"print(f\"Heat transport system: {process.data_structure.cost_variables.c226:.3e} M$\")\n",
"print(f\"Electrical plant equipment: {process.data_structure.cost_variables.c24:.3e} M$\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert to CSV format\n",
"This demonstrates how you would read from a PROCESS MFILE and write specified values into a csv using the `mfile_to_csv` function"
]
},
{
Expand All @@ -160,11 +155,28 @@
"metadata": {},
"outputs": [],
"source": [
"import process.data_structure\n",
"\n",
"# Print some values on the CostModel instance\n",
"print(f\"Heat transport system: {process.data_structure.cost_variables.c226:.3e} M$\")\n",
"print(f\"Electrical plant equipment: {process.data_structure.cost_variables.c24:.3e} M$\")"
"from process.io import mfile_to_csv\n",
"\n",
"data_dir = Path(\"data\")\n",
"\n",
"# mfile_to_csv requires two inputs:\n",
"# - path to the MFILE\n",
"# - .json containing the variable names to include in the csv file\n",
"\n",
"# This routine attempts to find every variable listed in the json file\n",
"# in the MFILE and writes the variable name, description and value\n",
"# to the output csv.\n",
"# Any listed variable that isn't in that MFILE will be skipped.\n",
"# The .csv file is saved to the directory of the input file\n",
"\n",
"mfile_to_csv.main(\n",
" args=[\n",
" \"-f\",\n",
" (data_dir / \"large_tokamak_1_MFILE.DAT\").as_posix(),\n",
" \"-v\",\n",
" (data_dir / \"mfile_to_csv_vars.json\").as_posix(),\n",
" ]\n",
")"
]
},
{
Expand Down
44 changes: 15 additions & 29 deletions tests/examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,12 @@ def test_examples(examples_temp_data):
"""
example_notebook_location = examples_temp_data / "examples.ipynb"
with testbook(example_notebook_location, execute=True, timeout=600):
pass


def test_scan(examples_temp_data):
"""Run scan.ipynb notebook check no exceptions are raised and that an MFILE is created.

scan.ipynb intentionally produces files when running the notebook, but remove
them when testing.
:param examples_temp_data: temporary dir containing examples files
:type examples_temp_data: Path
"""
scan_notebook_location = examples_temp_data / "scan.ipynb"
with testbook(scan_notebook_location, execute=True, timeout=1200):
# Run entire scan.ipynb notebook and assert an MFILE is created
assert os.path.exists(examples_temp_data / "data/scan_example_file_MFILE.DAT")


def test_csv(examples_temp_data):
"""Run csv_output.ipynb, check no exceptions are raised, check a csv file exists and check the csv file contains data.

csv_output.ipynb intentionally produces files when running the notebook, but remove
them when testing.
:param examples_temp_data: temporary dir containing examples files
:type examples_temp_data: Path
"""
csv_notebook_location = examples_temp_data / "csv_output.ipynb"
with testbook(csv_notebook_location, execute=True, timeout=600):
# Check csv file is created
assert os.path.exists(examples_temp_data / "data/large_tokamak_1_MFILE.csv")

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


def test_scan(examples_temp_data):
"""Run scan.ipynb notebook check no exceptions are raised and that an MFILE is created.

scan.ipynb intentionally produces files when running the notebook, but remove
them when testing.
:param examples_temp_data: temporary dir containing examples files
:type examples_temp_data: Path
"""
scan_notebook_location = examples_temp_data / "scan.ipynb"
with testbook(scan_notebook_location, execute=True, timeout=1200):
# Run entire scan.ipynb notebook and assert an MFILE is created
assert os.path.exists(examples_temp_data / "data/scan_example_file_MFILE.DAT")


def test_plot_solutions(examples_temp_data):
"""Run plot_solutions.ipynb and check no exceptions are raised.

Expand Down