Skip to content
Closed
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.

50 changes: 39 additions & 11 deletions examples/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,6 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Delete temp dir\n",
"temp_dir.cleanup()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -150,7 +140,7 @@
"temp_dir, temp_input_path, _ = copy_to_temp_dir(input_rel)\n",
"\n",
"# Run process on an input file\n",
"single_run = SingleRun(temp_input_path.as_posix())\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed (back)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had rebase trouble with this (not entirely sure why, it kept glitching out). Simplest solution was for me to redo the initial rebase and have made a new pr where everything is correct now #4022

"single_run = SingleRun(str(temp_input_path))\n",
"single_run.run()"
]
},
Expand All @@ -167,6 +157,44 @@
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
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