Skip to content

Commit

Permalink
mayavi offscreen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KristoforMaynard committed Sep 28, 2019
1 parent 92b602e commit 0b8201f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 46 deletions.
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ env:
- PYTHON=2.6 DEPS="minimal"
- PYTHON=2.7 DEPS="" FLAKE=1 PREPARE_DOCS=1
- PYTHON=3.3 DEPS=""
# - PYTHON=3.4 DEPS="" # is this necessary if 3.3 and 3.6 are tested?
# - PYTHON=3.5 DEPS="" # is this necessary if 3.3 and 3.6 are tested?
# - PYTHON=3.4 DEPS="" # is this necessary if 3.3 and 3.7 are tested?
# - PYTHON=3.5 DEPS="" # is this necessary if 3.3 and 3.7 are tested?
# - PYTHON=3.6 DEPS="" # is this necessary if 3.3 and 3.7 are tested?
- PYTHON=3.7 DEPS="mayavi" FLAKE=1 ETS_TOOLKIT='qt' QT_API='pyqt5'

git:
Expand Down Expand Up @@ -230,12 +231,20 @@ before_script:

script:
- |
echo "ETS_TOOLKIT> ${ETS_TOOLKIT}"
echo "QT_API> ${QT_API}"
if [ "${PREPARE_DOCS}" == "1" ]; then
make check;
else
make instcheck;
fi
- |
du -hs tests/plots/mvi-*.png
# if [ -f tests/plots/mvi-000.png ]; then
# curl --upload-file tests/plots/mvi-000.png https://transfer.sh/mvi-000.png
# fi
- |
if [[ $TRAVIS_BRANCH =~ ${deployable}$ && "${DEPLOY_TESTS}" == "true" ]]; then
make deploy-summary;
Expand Down
29 changes: 20 additions & 9 deletions tests/misc_prepare_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,24 @@
import viscid


offscreen_vlab = None
_global_ns = dict()


def get_mvi_fig():
from viscid.plot import vlab
try:
fig = _global_ns['figure']
vlab.clf()
except KeyError:
if offscreen_vlab is None:
raise RuntimeError("offscreen_vlab must be set before calling "
"get_mvi_fig(...)")
vlab.mlab.options.offscreen = offscreen_vlab
fig = vlab.figure(size=[1200, 800])
_global_ns['figure'] = fig
return vlab, fig

def do_test(lines, scalars, show=False, txt=""):
viscid.logger.info('--> ' + txt)
title = txt + '\n' + "\n".join(textwrap.wrap("scalars = {0}".format(scalars),
Expand All @@ -39,15 +54,7 @@ def do_test(lines, scalars, show=False, txt=""):

try:
from mayavi import mlab
from viscid.plot import vlab

try:
fig = _global_ns['figure']
vlab.clf()
except KeyError:
fig = vlab.figure(size=[1200, 800], offscreen=not show,
bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
_global_ns['figure'] = fig
vlab, _ = get_mvi_fig()

vlab.clf()
vlab.plot_lines3d(lines, scalars=scalars)
Expand All @@ -60,13 +67,17 @@ def do_test(lines, scalars, show=False, txt=""):
pass

def _main():
global offscreen_vlab

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--show", "--plot", action="store_true")
args = viscid.vutil.common_argparse(parser, default_verb=0)

viscid.logger.setLevel(viscid.logging.DEBUG)
args.show = False

offscreen_vlab = not args.show

cotr = viscid.Cotr(dip_tilt=20.0, dip_gsm=15.0) # pylint: disable=not-callable
b = viscid.make_dipole(m=cotr.get_dipole_moment(), n=(32, 32, 32))

Expand Down
3 changes: 2 additions & 1 deletion tests/test_mvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def _main():
pp = f3d["pp"]
e = f3d["e_cc"]

vlab.figure(size=(1280, 800), offscreen=not args.show)
vlab.mlab.options.offscreen = not args.show
vlab.figure(size=(1280, 800))

##########################################################
# make b a dipole inside 3.1Re and set e = 0 inside 4.0Re
Expand Down
3 changes: 2 additions & 1 deletion tests/test_quasi_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def _main():
except ImportError:
xfail("Mayavi not installed")

vlab.figure(size=[1200, 800], offscreen=not args.show)
vlab.mlab.options.offscreen = not args.show
vlab.figure(size=[1200, 800])

inds = np.argsort(xi_dat)[-64:]
inds = np.concatenate([inds, np.arange(len(xi_dat))[::71]])
Expand Down
24 changes: 15 additions & 9 deletions tests/test_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from viscid import sample_dir


offscreen_vlab = None
_global_ns = dict()


Expand All @@ -23,15 +24,19 @@
viscid.readers.openggcm.GGCMGrid.mhd_to_gse_on_read = "auto"


def get_mvi_fig(offscreen=False):
def get_mvi_fig():
from viscid.plot import vlab
try:
fig = _global_ns['figure']
vlab.clf()
except KeyError:
fig = vlab.figure(size=[1200, 800], offscreen=offscreen)
if offscreen_vlab is None:
raise RuntimeError("offscreen_vlab must be set before calling "
"get_mvi_fig(...)")
vlab.mlab.options.offscreen = offscreen_vlab
fig = vlab.figure(size=[1200, 800])
_global_ns['figure'] = fig
return fig
return vlab, fig

def run_test(fld, seeds, plot2d=True, plot3d=True, add_title="",
view_kwargs=None, show=False, scatter_mpl=False, mesh_mvi=True):
Expand Down Expand Up @@ -69,9 +74,8 @@ def run_test(fld, seeds, plot2d=True, plot3d=True, add_title="",
try:
if not plot3d:
raise ImportError
from viscid.plot import vlab

_ = get_mvi_fig(offscreen=not show)
vlab, _ = get_mvi_fig()

try:
if mesh_mvi:
Expand All @@ -96,6 +100,8 @@ def run_test(fld, seeds, plot2d=True, plot3d=True, add_title="",


def _main():
global offscreen_vlab

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--notwo", dest='notwo', action="store_true")
parser.add_argument("--nothree", dest='nothree', action="store_true")
Expand All @@ -109,6 +115,8 @@ def _main():
# plot3d = True
# args.show = True

offscreen_vlab = not args.show

img = np.load(os.path.join(sample_dir, "logo.npy"))
x = np.linspace(-1, 1, img.shape[0])
y = np.linspace(-1, 1, img.shape[1])
Expand Down Expand Up @@ -244,8 +252,7 @@ def _main():
pass
if plot3d:
try:
from viscid.plot import vlab
_ = get_mvi_fig(offscreen=not args.show)
vlab, _ = get_mvi_fig()
vlab.points3d(knots[0], knots[1], knots[2],
color=(1.0, 1.0, 0), scale_mode='none',
scale_factor=0.04)
Expand Down Expand Up @@ -296,8 +303,7 @@ def _main():
try:
if not plot3d:
raise ImportError
from viscid.plot import vlab
_ = get_mvi_fig(offscreen=not args.show)
vlab, _ = get_mvi_fig()
mesh = vlab.mesh_from_seeds(sheet_seed, scalars=vx_sheet,
clim=(-400, 400))
vlab.plot_earth_3d(crd_system=b)
Expand Down
39 changes: 21 additions & 18 deletions tests/test_streamline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@
import viscid


offscreen_vlab = None
_global_ns = dict()


def get_mvi_fig():
from viscid.plot import vlab
try:
fig = _global_ns['figure']
vlab.clf()
except KeyError:
if offscreen_vlab is None:
raise RuntimeError("offscreen_vlab must be set before calling "
"get_mvi_fig(...)")
vlab.mlab.options.offscreen = offscreen_vlab
fig = vlab.figure(size=[1200, 800])
_global_ns['figure'] = fig
return vlab, fig

def run_test(_fld, _seeds, plot2d=True, plot3d=True, title='', show=False,
**kwargs):
lines, topo = viscid.calc_streamlines(_fld, _seeds, **kwargs)
Expand Down Expand Up @@ -44,15 +59,7 @@ def run_test(_fld, _seeds, plot2d=True, plot3d=True, title='', show=False,
try:
if not plot3d:
raise ImportError
from viscid.plot import vlab

try:
fig = _global_ns['figure']
vlab.clf()
except KeyError:
fig = vlab.figure(size=[1200, 800], offscreen=not show,
bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
_global_ns['figure'] = fig
vlab, _ = get_mvi_fig()

fld_mag = np.log(viscid.magnitude(_fld))
try:
Expand Down Expand Up @@ -113,6 +120,8 @@ def set_violin_colors(v):


def _main():
global offscreen_vlab

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--notwo", dest='notwo', action="store_true")
parser.add_argument("--nothree", dest='nothree', action="store_true")
Expand All @@ -122,6 +131,8 @@ def _main():
plot2d = not args.notwo
plot3d = not args.nothree

offscreen_vlab = not args.show

# #################################################
# viscid.logger.info("Testing field lines on 2d field...")
B = viscid.make_dipole(twod=True)
Expand Down Expand Up @@ -410,15 +421,7 @@ def _calc_rel_diff(_lsp, _ideal_lsp, _d):
try:
if not plot3d:
raise ImportError
from viscid.plot import vlab

try:
fig = _global_ns['figure']
vlab.clf()
except KeyError:
fig = vlab.figure(size=[1200, 800], offscreen=not args.show,
bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
_global_ns['figure'] = fig
vlab, _ = get_mvi_fig()

for i, method in zip(count(), methods):
# if i in (3, 4):
Expand Down
4 changes: 2 additions & 2 deletions viscid/cotr.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,8 @@ def _main():
import viscid
from viscid.plot import vlab

vlab.figure(size=(768, 768), fgcolor=(0, 0, 0), bgcolor=(1, 1, 1),
offscreen=True)
vlab.mlab.options.offscreen = True
vlab.figure(size=(768, 768), fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))

def _plot_time_range(times, figname):
for i, t in enumerate(times):
Expand Down
16 changes: 12 additions & 4 deletions viscid/plot/vlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,13 @@ def figure(*args, **kwargs):
if size:
kwargs['size'] = size

if offscreen:
viscid.logger.warning("Since some backends are more picky than others,\n"
"it is recomended that you rather use\n"
" vlab.mlab.options.offscreen = True\n"
"near the top of __main__ to create an offscreen "
"figure.")

fig = None

if global_fig:
Expand Down Expand Up @@ -1336,10 +1343,11 @@ def resize(size, figure=None):
if mlab.options.offscreen:
figure.scene.set_size(size)
elif figure.scene.off_screen_rendering:
viscid.logger.warning("viscid.plot.vlab.resize doesn't work for "
"figures that are off-screened this way. Try "
"creating the figure with viscid.plot.vlab."
"figure(size=(w, h), offscreen=True)")
viscid.logger.warning("viscid.plot.vlab.resize doesn't work for\n"
"figures that are off-screened this way. I\n"
"suggest you use\n"
" mayavi.mlab.options.offscreen = True\n"
"near the beginning of __main__")
else:
try:
_ets_config = mayavi.ETSConfig
Expand Down

0 comments on commit 0b8201f

Please sign in to comment.