Skip to content

Commit

Permalink
debug:improve test coverage between matlab and python cases
Browse files Browse the repository at this point in the history
typo

complete rebase
  • Loading branch information
scivision committed Jul 19, 2019
1 parent 9d9becc commit 9653136
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 16 deletions.
10 changes: 8 additions & 2 deletions ncarglow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def simple(time: datetime, glat: float, glon: float,
stderr=subprocess.DEVNULL,
universal_newlines=True)

return glowparse(dat)
iono = glowparse(dat)
iono.attrs['geomag'] = ip

return iono


def ebins(time: datetime, glat: float, glon: float,
Expand Down Expand Up @@ -87,9 +90,12 @@ def glowparse(raw: str) -> xarray.Dataset:
dat = np.genfromtxt(table, skip_header=2, max_rows=NALT)
alt_km = dat[:, 0]

states = ['Tn', 'O', 'N2', 'N', 'NO', 'NeIn', 'NeOut', 'ionrate',
states = ['Tn', 'O', 'N2', 'NO', 'NeIn', 'NeOut', 'ionrate',
'O+', 'O2+', 'NO+', 'N2D', 'pedersen', 'hall']

if len(states) != dat.shape[1]-1:
raise ValueError('did not read raw output from GLOW correctly, please file a bug report.')

d: dict = {k: ('alt_km', v) for (k, v) in zip(states, dat[:, 1:].T)}
iono = xarray.Dataset(d, coords={'alt_km': alt_km})
# %% VER
Expand Down
10 changes: 6 additions & 4 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def test_simple():
pytest.skip('CI internet FTP issue')

assert iono['alt_km'].size == Nbins
assert iono.alt_km[32] == approx(101.8)
assert iono['Tn'][32] == approx(188.)
assert iono['ver'].loc[:, '5577'][32] == approx(20.45)
assert iono['ionrate'][32] == approx(335.)
i = 32
assert iono.alt_km[i] == approx(101.8)
assert iono['Tn'][i] == approx(188.)
assert iono['ver'].loc[:, '5577'][i] == approx(20.45)
assert iono['ionrate'][i] == approx(335.)
assert iono['hall'][i].item() == approx(6.98e-05)


def test_ebins():
Expand Down
62 changes: 62 additions & 0 deletions tests/test_basic.py.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python
import ncarglow as glow
import pytest
from pytest import approx
from datetime import datetime
import numpy as np


def test_simple():
time = datetime(2015, 12, 13, 10, 0, 0)
glat = 65.1
glon = -147.5
# %% flux [erg cm-2 s-1 == mW m-2 s-1]
Q = 1
# %% characteristic energy [eV]
Echar = 100e3
# %% Number of energy bins
Nbins = 250

try:
iono = glow.simple(time, glat, glon, Q, Echar, Nbins)
except ConnectionError:
pytest.skip('CI internet FTP issue')

assert iono['alt_km'].size == Nbins
assert iono.alt_km[32] == approx(101.8)
assert iono['Tn'][32] == approx(188.)
assert iono['ver'].loc[:, '5577'][32] == approx(20.45)
<<<<<<< HEAD
assert iono['ionrate'][32] == approx(473.)
=======
assert iono['ionrate'][32] == approx(0.488)
>>>>>>> db1f403... upgrade API for use as subproject


def test_ebins():
time = datetime(2015, 12, 13, 10, 0, 0)
glat = 65.1
glon = -147.5
# %% Energy grid [eV]
Emin = 0.1
Emax = 1e6
E0 = 100e3
Nbins = 250
# %% monoenergetic beam
Ebins = np.logspace(np.log10(Emin), np.log10(Emax), Nbins).astype(np.float32)
Phitop = np.zeros_like(Ebins)
Phitop[abs(Ebins-E0).argmin()] = 1.
Phitop = Phitop.astype(np.float32)
# %% run glow
try:
iono = glow.ebins(time, glat, glon, Ebins, Phitop)
except ConnectionError:
pytest.skip('CI internet FTP issue')

assert iono['alt_km'].size == Nbins
assert iono['Tn'][32] == approx(188.0)
assert iono['ver'].loc[:, '5577'][32] == approx(0.04)


if __name__ == '__main__':
pytest.main(['-r', 'a', '-v', __file__])
23 changes: 23 additions & 0 deletions tests/test_build.py.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
import pytest
import ncarglow.build as build
from pathlib import Path

R = Path(__file__).parent


@pytest.mark.parametrize('build_sys', ['cmake', 'meson'])
def test_build(build_sys, tmp_path):
build.build(build_sys, R.parent, tmp_path)


<<<<<<< HEAD
=======
def test_bad(tmp_path):
with pytest.raises(ValueError):
build.build('fake', R.parent, tmp_path)


>>>>>>> db1f403... upgrade API for use as subproject
if __name__ == '__main__':
pytest.main(['-r', 'a', '-v', '-s', __file__])
2 changes: 1 addition & 1 deletion tests/test_matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_matlab_build(build_sys, tmp_path):

@pytest.mark.skipif(not MATLAB, reason="Matlab not available")
def test_matlab_api():
subprocess.check_call([MATLAB, '-batch', 'test_mod'],
subprocess.check_call([MATLAB, '-batch', 'runtests'],
cwd=R, timeout=60)


Expand Down
56 changes: 56 additions & 0 deletions tests/test_matlab.py.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python
r"""
On Windows with Octave >= 5, you may need to add to PATH like:
C:\Octave\Octave-5.1.0.0\mingw64\bin
to get at octave.exe instead of octave.vbs
"""
from pathlib import Path
import subprocess
import pytest
import shutil

R = Path(__file__).parent
Rcode = R.parent / 'matlab'

OCTAVE = shutil.which('octave-cli')
MATLAB = shutil.which('matlab')


@pytest.mark.skipif(not OCTAVE, reason="GNU Octave not available")
@pytest.mark.parametrize('build_sys', ['cmake', 'meson'])
def test_octave_build(build_sys, tmp_path):
subprocess.check_call([OCTAVE, '--eval',
<<<<<<< HEAD
f"setup('{build_sys}', '{R.parent}', '{tmp_path}')"],
=======
f"build('{build_sys}', '{R.parent}', '{tmp_path}')"],
>>>>>>> db1f403... upgrade API for use as subproject
cwd=Rcode, timeout=120)


@pytest.mark.skipif(not MATLAB, reason="Matlab not available")
@pytest.mark.parametrize('build_sys', ['cmake', 'meson'])
def test_matlab_build(build_sys, tmp_path):
subprocess.check_call([MATLAB, '-batch',
<<<<<<< HEAD
f"setup('{build_sys}', '{R.parent}', '{tmp_path}')"],
=======
f"build('{build_sys}', '{R.parent}', '{tmp_path}')"],
>>>>>>> db1f403... upgrade API for use as subproject
cwd=Rcode, timeout=120)


@pytest.mark.skipif(not MATLAB, reason="Matlab not available")
def test_matlab_api():
subprocess.check_call([MATLAB, '-batch', 'test_mod'],
cwd=R, timeout=60)


@pytest.mark.skipif(not OCTAVE, reason='octave not found')
def test_octave_api():
subprocess.check_call([OCTAVE, 'test_mod.m'],
cwd=R, timeout=60)


if __name__ == '__main__':
pytest.main(['-r', 'a', '-v', '-s', __file__])
19 changes: 10 additions & 9 deletions tests/test_mod.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
time = datenum(2015,12,13,10,0,0);
glat = 65.1;
glon = -147.5;
Ap = 4;
f107 = 100;
f107a = 100;
f107p = 100;
Ap = 7;
f107 = 118.7;
f107a = 78.7;
f107p = 113.1;
Q = 1;
Echar = 100e3;
Nbins = 250;
Expand All @@ -15,9 +15,10 @@

iono = glow(time, glat, glon, f107a, f107, f107p, Ap, Q, Echar, Nbins);

i = 32-1;
i = 32+1;

assert(abs(iono.altkm(i) - 101.8) < 0.1, 'wrong altitude?')
assert(abs(iono.Tn(i) - 188.) < 0.01, 'Tn error')
assert(abs(iono.A5577(i) - 20.45) < 0.0001, 'A5577 error')
assert(abs(iono.ionrate(i) - 335.) < 0.01, 'ionrate error')
assert(abs(iono.altkm(i) - 101.8) < 0.1, ['alt_km: expected 101.8, but got: ', num2str(iono.altkm(i))])
assert(abs(iono.Tn(i) - 188.) < 0.01, ['Tn: expected 188., but got: ', num2str(iono.Tn(i))])
assert(abs(iono.A5577(i) - 20.45) < 0.0001, ['A5577: expected 20.45, but got: ', num2str(iono.A5577(i))])
assert(abs(iono.ionrate(i) - 335.) < 0.01, ['ionrate: expected 335. but got: ', num2str(iono.ionrate(i))])
assert(abs(iono.hall(i) - 6.98e-5) < 1e-7, ['hall: expected 6.98e-5 but got: ', num2str(iono.hall(i))])
32 changes: 32 additions & 0 deletions tests/test_mod.m.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%% simple
time = datenum(2015,12,13,10,0,0);
glat = 65.1;
glon = -147.5;
Ap = 4;
f107 = 100;
f107a = 100;
f107p = 100;
Q = 1;
Echar = 100e3;
Nbins = 250;

cwd = fileparts(mfilename('fullpath'));
addpath([cwd, filesep, '..', filesep, 'matlab'])

iono = glow(time, glat, glon, f107a, f107, f107p, Ap, Q, Echar, Nbins);

<<<<<<< HEAD
i = 32;

assert(abs(iono.altkm(i) - 99.8) < 0.1, 'wrong altitude?')
assert(abs(iono.Tn(i) - 188.) < 0.01, 'Tn error')
assert(abs(iono.A5577(i) - 27.32) < 0.0001, 'A5577 error')
assert(abs(iono.ionrate(i) - 473.) < 0.01, 'ionrate error')
=======
i = 32-1;

assert(abs(iono.altkm(i) - 101.8) < 0.1, 'wrong altitude?')
assert(abs(iono.Tn(i) - 188.) < 0.01, 'Tn error')
assert(abs(iono.A5577(i) - 20.45) < 0.0001, 'A5577 error')
assert(abs(iono.ionrate(i) - 0.488) < 0.01, 'ionrate error')
>>>>>>> db1f403... upgrade API for use as subproject

0 comments on commit 9653136

Please sign in to comment.