diff --git a/examples/csv_output.ipynb b/examples/csv_output.ipynb deleted file mode 100644 index 2825f0451c..0000000000 --- a/examples/csv_output.ipynb +++ /dev/null @@ -1,80 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Output to csv\n", - "\n", - "Routine to read from a PROCESS MFILE and write specified values into a csv.\n", - "\n", - "Input files:\n", - "- MFILE.DAT as output from PROCESS\n", - "- .json variable list as defined by user (defaults to local `mfile_to_csv_vars.json`)\n", - "\n", - "Instructions:\n", - "- from command line: `python mfile_to_csv.py -f -v `\n", - "- from this Jupyter notebook: run the cell below\n", - "\n", - "Output file:\n", - "- .csv will be saved to the directory of the input file" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [], - "source": [ - "from pathlib import Path\n", - "\n", - "from process.io import mfile_to_csv\n", - "\n", - "# Project directory for example result file and default .json list;\n", - "# not needed if you replace both target filepaths below.\n", - "data_dir = Path(\"data\")\n", - "# Replace this path/to/MFILE.DAT with your target file:\n", - "mfilename = data_dir / \"large_tokamak_1_MFILE.DAT\"\n", - "\n", - "# Either replace this with your own path/to/file.json target,\n", - "# or add your required variables into the identified file:\n", - "varfilename = data_dir / \"mfile_to_csv_vars.json\"\n", - "# This routine attempts to find every variable in the given list and\n", - "# writes the variable name, description and value to the output csv.\n", - "# Any listed variable that isn't in that MFILE will be skipped.\n", - "\n", - "# call to function:\n", - "mfile_to_csv.main(args=[\"-f\", str(mfilename), \"-v\", str(varfilename)])" - ] - } - ], - "metadata": { - "celltoolbar": "Slideshow", - "kernelspec": { - "display_name": "env", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/examples/examples.ipynb b/examples/examples.ipynb index 3e9bedbb9a..5c18a58e6d 100644 --- a/examples/examples.ipynb +++ b/examples/examples.ipynb @@ -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." ] }, { @@ -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" ] }, { @@ -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", + ")" ] }, { diff --git a/tests/examples/test_examples.py b/tests/examples/test_examples.py index f3c76be4b8..d97015af65 100644 --- a/tests/examples/test_examples.py +++ b/tests/examples/test_examples.py @@ -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 @@ -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.