gamsjupyter01.gms : Test GAMS Jupyter Notebooks

Description

Contributor: Clemens Westphal, September 2020)


Small Model of Type : GAMS


Category : GAMS Test library


Main file : gamsjupyter01.gms

$TITLE 'Test GAMS Jupyter Notebooks' (GAMSJUPYTER01,SEQ=836)

$ontext
Contributor: Clemens Westphal, September 2020)
$offtext

$set srcRoot %gams.sysdir%api%system.dirsep%python%system.dirsep%examples%system.dirsep%magic%system.dirsep%
$set testDir %sysenv.GTESTDIR%%system.dirsep%%sysenv.MODTESTDIR%%system.dirsep%gamsjupyter01

$if not setenv JUPYTER $exit

$onEmbeddedCode Python:
import os
import shutil

def _patch_sysdir(file_in, file_out, old, new):
    with open(file_in, 'r', encoding='utf-8') as f:
        s = f.read().replace(old, new)
    with open(file_out, 'w', encoding='utf-8') as f:
        f.write(s)

def patch_sysdir(file_in, file_out):
    _patch_sysdir(
        file_in,
        file_out,
        '"%reload_ext gams.magic"',
        r'"%reload_ext gams.magic\n", "import os\n", "gams.reset(os.environ.get(\"GTESTDIR\", None))"'
    )

def patch_sysdir_py(file_in, file_out):
    _patch_sysdir(
        file_in,
        file_out,
        'gams = GamsInteractive()',
        r'gams = GamsInteractive(); import os; gams.reset(os.environ.get("GTESTDIR", None))'
    )

test_dir = r"%testDir%"
os.makedirs(test_dir, exist_ok=True)

patch_sysdir(r"%srcRoot%Introduction.ipynb", os.path.join(test_dir, "Introduction.ipynb"))
patch_sysdir(r"%srcRoot%Millco.ipynb", os.path.join(test_dir, "Millco.ipynb"))
patch_sysdir(r"%srcRoot%Polyomino.ipynb", os.path.join(test_dir, "Polyomino.ipynb"))
patch_sysdir(r"%srcRoot%Pickstock.ipynb", os.path.join(test_dir, "Pickstock.ipynb"))
patch_sysdir(r"%srcRoot%NordhausDice.ipynb", os.path.join(test_dir, "NordhausDice.ipynb"))

# copy Pentominos.png required by Polyomino.ipynb
shutil.copy(r"%srcRoot%Pentominos.png", test_dir)

patch_sysdir_py(r"%srcRoot%millco.py", os.path.join(test_dir, "millco.py"))
patch_sysdir_py(r"%srcRoot%nordhausdice.py", os.path.join(test_dir, "nordhausdice.py"))
patch_sysdir_py(r"%srcRoot%pickstock.py", os.path.join(test_dir, "pickstock.py"))

$offEmbeddedCode


$call.checkErrorLevel cd . && "%sysenv.JUPYTER%" nbconvert --to notebook --execute "%testDir%%system.dirsep%Introduction.ipynb" > Introduction.log 2>&1
$call.checkErrorLevel cd . && "%sysenv.JUPYTER%" nbconvert --to notebook --execute "%testDir%%system.dirsep%Millco.ipynb" > Millco.log 2>&1
$ifThen dExist "%gams.sysdir%GMSPython"
$  call.checkErrorLevel cd . && "%gams.sysdir%GMSPython%system.dirsep%python" "%testDir%%system.dirsep%millco.py" > millco_py.log 2>&1
$endIf

$if %DEMOSIZE% == 1 $exit

$call.checkErrorLevel cd . && "%sysenv.JUPYTER%" nbconvert --to notebook --execute "%testDir%%system.dirsep%Polyomino.ipynb" > Polyomino.log 2>&1
$call.checkErrorLevel cd . && "%sysenv.JUPYTER%" nbconvert --to notebook --execute "%testDir%%system.dirsep%Pickstock.ipynb" > Pickstock.log 2>&1
$call.checkErrorLevel cd . && "%sysenv.JUPYTER%" nbconvert --to notebook --execute "%testDir%%system.dirsep%NordhausDice.ipynb" > NordhausDice.log 2>&1
$ifThen dExist "%gams.sysdir%GMSPython"
$  call.checkErrorLevel cd . && "%gams.sysdir%GMSPython%system.dirsep%python" "%testDir%%system.dirsep%pickstock.py" > pickstock_py.log 2>&1
$endIf
$ifThen dExist "%gams.sysdir%GMSPython"
$  call.checkErrorLevel cd . && "%gams.sysdir%GMSPython%system.dirsep%python" "%testDir%%system.dirsep%nordhausdice.py" > nordhausdice_py.log 2>&1
$endIf