Skip to content

Commit

Permalink
Override show() to save figures during CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Sep 5, 2024
1 parent b0ab38d commit 57d067d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
18 changes: 7 additions & 11 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,27 @@ jobs:
EXAMPLE="examples/${{ matrix.example }}"
EXAMPLE_DIR=$(dirname "${EXAMPLE}")
EXAMPLE_FILE=$(basename "${EXAMPLE}")
FIGURE_DIR=$(realpath figs/)
echo "ARTIFACT_NAME=${EXAMPLE_FILE}" >> $GITHUB_ENV
echo "FIGURE_DIR=${FIGURE_DIR}" >> $GITHUB_ENV
mkdir -p "${FIGURE_DIR}"
python rhodium/test/plot_to_savefig.py "${EXAMPLE}" "${FIGURE_DIR}"
pushd "${EXAMPLE_DIR}"
if [ -f "Makefile" ]; then
make
fi
IMAGES_DIR=$(realpath images/)
export RHODIUM_NO_PROMPT=true
export RHODIUM_FIGURE_OUTPUT="${IMAGES_DIR}"
export LD_LIBRARY_PATH="$(pwd):${LD_LIBRARY_PATH}"
python "${EXAMPLE_FILE}"
if [ -d "images/" ]; then
mv images/ "${FIGURE_DIR}"
fi
echo "IMAGES_DIR=${IMAGES_DIR}" >> $GITHUB_ENV
echo "ARTIFACT_NAME=${EXAMPLE_FILE}" >> $GITHUB_ENV
- name: Upload figures
uses: actions/upload-artifact@v4
with:
name: "${{ env.ARTIFACT_NAME }}"
path: "${{ env.FIGURE_DIR }}"
path: "${{ env.IMAGES_DIR }}"
if-no-files-found: ignore
19 changes: 19 additions & 0 deletions rhodium/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
# You should have received a copy of the GNU General Public License
# along with Rhodium. If not, see <http://www.gnu.org/licenses/>.
import os
import mplcursors
import matplotlib as mpl
import matplotlib.pyplot as plt
Expand All @@ -29,6 +30,23 @@
from .model import Response
from .brush import BrushSet, apply_brush, color_brush, brush_color_map, color_indices

# When set, override the plt.show() method to save. Intended for CI
# or headless mode.
_figure_output_envvar = "RHODIUM_FIGURE_OUTPUT"

if os.getenv(_figure_output_envvar):
def show_override(*args, **kwargs):
directory = os.getenv(_figure_output_envvar)

if not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)

filename = os.path.join(directory, f"figure_{plt.gcf().number}.png")
plt.savefig(filename)
print(f"{_figure_output_envvar} set, saving figure to {filename}")

plt.show = show_override

def _combine_keys(*args):
result = []
result_set = set()
Expand Down Expand Up @@ -423,6 +441,7 @@ def kdeplot(model, data, x, y,
cmap=["Reds", "Blues", "Oranges", "Greens", "Greys"],
**kwargs):
df = data.as_dataframe()
plt.figure()

if brush is None:
sns.kdeplot(x=df[x],
Expand Down
30 changes: 0 additions & 30 deletions rhodium/test/plot_to_savefig.py

This file was deleted.

0 comments on commit 57d067d

Please sign in to comment.