Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting setting offscreen default & enable running of example on headless server #197

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
34 changes: 34 additions & 0 deletions .github/workflows/test_examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test examples

on:
pull_request:
branches: ["main"]

jobs:
test_examples:
runs-on: ubuntu-latest

steps:
- name: checkout code
uses: actions/checkout@v4
- name: enable headless render
run: |
sudo apt-get update
sudo apt-get install -y libgl1-mesa-glx libgl1-mesa-dev xvfb
set -x
export DISPLAY=:99.0
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
set +x
exec "$@"
- name: Set up python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"

- name: install gustaf
run: |
pip install .[dev]
- name: run examples
run: |
cd examples
python3 run_all_examples.py
4 changes: 2 additions & 2 deletions examples/create_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import gustaf as gus


def main():
def example():
mesh_faces_box = gus.create.faces.box(
bounds=[[0, 0], [2, 2]], resolutions=[2, 3]
)
Expand Down Expand Up @@ -65,4 +65,4 @@ def main():


if __name__ == "__main__":
main()
example()
7 changes: 6 additions & 1 deletion examples/export_meshio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import gustaf as gus

if __name__ == "__main__":

def example():
export_path = Path("export")

# define coordinates
Expand Down Expand Up @@ -55,3 +56,7 @@
# Export only tetrahedra
gus.io.meshio.export(tet, export_path / "export_meshio.vtu")
gus.io.meshio.export(tet.to_faces(), export_path / "export_meshio.stl")


if __name__ == "__main__":
example()
6 changes: 3 additions & 3 deletions examples/interfaces/nutils_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import gustaf as gus


def main():
def example():
# Simulation parameters
degree = 1
btype = "std"
Expand Down Expand Up @@ -80,7 +80,7 @@ def main():
deformation = lhs.reshape(m.vertices.shape)
m.vertices += deformation

gus.show.show_vedo(
gus.show.show(
[m_in, "gustaf_input-mesh"],
[m, "gustaf_mesh-with-lhs"],
[gustaf_mesh, "gustaf_mesh-bezier"],
Expand Down Expand Up @@ -143,4 +143,4 @@ def define_mesh():


if __name__ == "__main__":
main()
example()
4 changes: 2 additions & 2 deletions examples/mixd_to_nutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import gustaf as gus


def main():
def example():
# Creates a gustaf volume.
mesh = create_mesh()

Expand Down Expand Up @@ -63,4 +63,4 @@ def create_mesh():


if __name__ == "__main__":
main()
example()
46 changes: 41 additions & 5 deletions examples/run_all_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,60 @@
"""

import glob
import importlib
import os
import pathlib
import subprocess
import sys

import gustaf

if __name__ == "__main__":
gustaf.settings.DEFAULT_OFFSCREEN = True
files_not_completed = []
examples_root_dir = pathlib.Path(__file__).parent.absolute()
current_dir = pathlib.Path(os.getcwd()).absolute()

for file in glob.glob(str(pathlib.Path(__file__).parent) + "/*.py"):
if file.split("/")[-1] in [
for file_ in glob.glob(str(examples_root_dir) + "/*.py"):
if file_.split("/")[-1] in [
"run_all_examples.py",
"load_sample_file.py",
]:
continue # i like pathlib so i convert the path into it
file = pathlib.Path(file_)
print(
f"file: {file}",
f"file.parent: {file.parent}",
f"current_dir: {current_dir}",
f"file.stem: {file.stem}",
)
# change directory to the file's directory so that imports are
# relative to the file's directory
os.chdir(file.parent)
# ignore this file
if "run_all_examples.py" in str(file):
continue
print(f"Calling {file}")
# import and call the file
try:
proc_return = subprocess.run([sys.executable, file], check=True)
except subprocess.CalledProcessError:
# add the file's directory to the path so importlib can
# import it
sys.path.insert(0, f"{file.parent}{os.sep}")
example = importlib.import_module(file.stem)
except Exception as e:
print(f"Failed to import {file}.")
print(f"Error: {e}")
files_not_completed.append(file)
continue
try:
# run example
example.example()
except Exception as e:
print(f"Failed to call {file}.")
print(f"Error: {e}")
files_not_completed.append(file)
continue
# change directory back to the original directory
os.chdir(current_dir)
if len(files_not_completed) > 0:
print(
f"Failed to call {len(files_not_completed)} files: "
Expand Down
7 changes: 6 additions & 1 deletion examples/show_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import gustaf as gus

if __name__ == "__main__":

def example():
# define coordinates
v = np.array(
[
Expand Down Expand Up @@ -42,3 +43,7 @@

# show
edges.show()


if __name__ == "__main__":
example()
7 changes: 6 additions & 1 deletion examples/show_faces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import gustaf as gus

if __name__ == "__main__":

def example():
# define coordinates
v = np.array(
[
Expand Down Expand Up @@ -60,3 +61,7 @@
# show
tri.show()
quad.show()


if __name__ == "__main__":
example()
7 changes: 6 additions & 1 deletion examples/show_gmsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import gustaf
from gustaf import io

if __name__ == "__main__":

def example():
mesh_file_tri = pathlib.Path("faces/tri/2DChannelTria.msh")
mesh_file_quad = pathlib.Path("faces/quad/2DChannelQuad.msh")
mesh_file_tetra = pathlib.Path("volumes/tet/3DBrickTet.msh")
Expand Down Expand Up @@ -39,3 +40,7 @@
*[[msh.__class__.__name__, msh] for msh in loaded_mesh_default],
title="3D mesh with tetrahedrons",
)


if __name__ == "__main__":
example()
7 changes: 6 additions & 1 deletion examples/show_scalarbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import gustaf as gus

if __name__ == "__main__":

def example():
f = gus.Faces()
f.vertices = [
[1.16670818, 0.116044],
Expand Down Expand Up @@ -223,3 +224,7 @@
"nlabels": 12,
}
gus.show.show([f, "3D scalarbar"], [f_2d, "2D scalarbar"])


if __name__ == "__main__":
example()
7 changes: 6 additions & 1 deletion examples/show_vertices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import gustaf as gus

if __name__ == "__main__":

def example():
# define coordinates
v = np.array(
[
Expand All @@ -27,3 +28,7 @@
vert.show_options["r"] = 10
vert.show_options["vertex_ids"] = True
vert.show()


if __name__ == "__main__":
example()
7 changes: 6 additions & 1 deletion examples/show_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import gustaf as gus

if __name__ == "__main__":

def example():
# define coordinates
v = np.array(
[
Expand Down Expand Up @@ -55,3 +56,7 @@
# set vertex_data to plot
hexa.show_options["data"] = "arange"
hexa.show()


if __name__ == "__main__":
example()
7 changes: 6 additions & 1 deletion examples/shrink_elements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gustaf as gus

if __name__ == "__main__":

def example():
# create 2x3x4 test hexa element
v_res = [2, 3, 4]
vertices = gus.create.vertices.raster(
Expand Down Expand Up @@ -37,3 +38,7 @@
direct_toedges,
],
)


if __name__ == "__main__":
example()
4 changes: 4 additions & 0 deletions gustaf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
}

NTHREADS = 1

DEFAULT_OFFSCREEN = False
#: If True, the visualization will default to offscreen. This is useful for
#: running tests on CI/CD servers.
4 changes: 2 additions & 2 deletions gustaf/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from gustaf import utils
from gustaf import settings, utils

try:
from gustaf.helpers.notebook import K3DPlotterN
Expand Down Expand Up @@ -70,7 +70,7 @@ def show(*args, **kwargs):
"""
# vedo plotter parameter
N = len(args)
offs = kwargs.get("offscreen", False)
offs = kwargs.get("offscreen", settings.DEFAULT_OFFSCREEN)
interact = kwargs.get("interactive", True)
plt = kwargs.get("vedoplot", None)
skip_clear = kwargs.get("skip_clear", False)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test = [
"pytest",
"funi>=0.0.1",
"napf>=0.0.5",
"vedo>=2023.5.0",
]
dev = [
"pytest",
Expand Down
Loading