Skip to content

Commit

Permalink
Merge pull request #15 from thomas-saigre/12-set-automatic-tests
Browse files Browse the repository at this point in the history
12 set automatic tests
  • Loading branch information
thomas-saigre authored May 15, 2024
2 parents 6206b67 + 5c2e5ad commit 5a9a6fb
Show file tree
Hide file tree
Showing 28 changed files with 1,709 additions and 23 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- '*' # all branches

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'

build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macOS-latest]
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- uses: actions/checkout@v3
# - name: Install system dependencies
# run: sudo apt-get install -y texlive-latex-base texlive-latex-extra context python3-tk
- name: Test with tox
run: |
pip install tox
tox -- --cov tikzplotly --cov-report xml --cov-report term
- uses: codecov/[email protected]
if: ${{ matrix.python-version == '3.10' && matrix.os == 'ubuntu-latest' }}
with:
token: ${{ secrets.CODECOV_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ venv/

# Distribution / packaging
dist/

# Tox
.tox/
.coverage
coverage.xml
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/tikzplotly.svg?style=flat-square)](https://pypi.org/pypi/tikzplotly/)

[![Issue](https://img.shields.io/github/issues-raw/thomas-saigre/tikzplotly?style=flat-square)](https://github.com/thomas-saigre/tikzplotly/issues)
[![codecov](https://img.shields.io/codecov/c/github/thomas-saigre/tikzplotly.svg?style=flat-square)](https://codecov.io/gh/thomas-saigre/tikzplotly)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/thomas-saigre/tikzplotly/ci.yml?style=flat-square)


# Tikzplotly

Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "tikzplotly"
version = "0.1.5"
version = "0.1.6"
description = "Convert plotly figures to LaTeX / tikz figures"
readme = "README.md"
authors = [{name = "Thomas Saigre", email = "[email protected]"}]
Expand All @@ -13,7 +13,6 @@ classifiers = [
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand Down
3 changes: 3 additions & 0 deletions src/tests/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Run test

This tests are only for devlopment purposes.
Automatic tests are set-up in [../tests](../tests/) directory.

```bash
python3 -m tests.<test-name>
```
22 changes: 9 additions & 13 deletions src/tests/test_heatmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

import plotly
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import datetime
import numpy as np
np.random.seed(1)

def fig1():
Expand Down Expand Up @@ -168,17 +166,16 @@ def fig11(): # from @JasonGross, see issue https://github.com/thomas-saigre/t
file_directory = os.path.dirname(os.path.abspath(__file__))

functions = [
("1", fig1),
("2", fig2),
# ("3", fig3), # texttemplate not supported
("4", fig4),
("5", fig5),
("6", fig6),
# ("1", fig1),
# ("2", fig2),
# # ("3", fig3), # texttemplate not supported
# ("4", fig4),
# ("5", fig5),
# ("6", fig6),
("7", fig7),
# ("8", fig8), # Not supported
("9", fig9),
("11", fig11)

# # ("8", fig8), # Not supported
# ("9", fig9),
# ("11", fig11)
]

main_tex_content = tex_create_document(options="twocolumn", compatibility="newest")
Expand All @@ -190,7 +187,6 @@ def fig11(): # from @JasonGross, see issue https://github.com/thomas-saigre/t
for i, f in functions:
print(f"Figure {i}")
fig, title = f()
data = fig.data
save_path = os.path.join(file_directory, "outputs", "test_heatmap", "fig{}.tex".format(i))
tikzplotly.save(save_path, fig, img_name=os.path.join(file_directory, "outputs", "test_heatmap", "fig{}.png".format(i)))
main_tex_content += tex_begin_environment("figure", stack_env)
Expand Down
1 change: 0 additions & 1 deletion src/tests/test_markers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# From https://plotly.com/python/marker-style/
import plotly
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import tikzplotly
Expand Down
2 changes: 1 addition & 1 deletion src/tikzplotly/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.4"
__version__ = "0.1.6"
4 changes: 2 additions & 2 deletions src/tikzplotly/_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def draw_heatmap(data, fig, img_name, axis: Axis):
axis.add_option("colormap", get_tikz_colorscale(DEFAULT_COLORSCALE))

tmp = np.where(figure_data == None, np.nan, figure_data)
axis.add_option("point meta max", tmp.max())
axis.add_option("point meta min", tmp.min())
axis.add_option("point meta max", np.nanmax(tmp))
axis.add_option("point meta min", np.nanmin(tmp))
axis.add_option("xmin", xmin)
axis.add_option("xmax", xmax)
axis.add_option("ymin", ymax)
Expand Down
11 changes: 7 additions & 4 deletions src/tikzplotly/_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ def get_tikz_code(
if include_disclamer:
code += tex_comment(f"This file was created with tikzplotly version {__version__}.")

code += data_container.exportData()
code += "\n"
if len(data_container.data) > 0:
code += data_container.exportData()
code += "\n"

code += tex_begin_environment("tikzpicture", stack_env, options=tikz_options)

code += "\n"
for color in colors_set:
color_list = list(colors_set)
color_list.sort()
for color in color_list:
code += tex_add_color(color[0], color[1], color[2])
code += "\n"

Expand All @@ -107,7 +110,7 @@ def get_tikz_code(
return code


def save(filepath: str | Path, *args, encoding: str | None = None, **kwargs):
def save(filepath, *args, **kwargs):
"""Save a figure to a file or a stream.
Parameters
Expand Down
Empty file added tests/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Adapted from https://github.com/nschloe/tikzplotlib/blob/450712b4014799ec5f151f234df84335c90f4b9d/tests/helpers.py

import tikzplotly


# https://stackoverflow.com/a/845432/353337
def _unidiff_output(expected, actual):
import difflib

expected = expected.splitlines(1)
actual = actual.splitlines(1)
diff = difflib.unified_diff(expected, actual)
return "".join(diff)

def assert_equality(fig, target_file, **kwargs):
tikz_code = tikzplotly.get_tikz_code(fig, include_disclamer=False, **kwargs)

with open(target_file, encoding="utf-8") as f:
reference = f.read()
assert reference == tikz_code, target_file + "\n" + _unidiff_output(reference, tikz_code)
65 changes: 65 additions & 0 deletions tests/test_heatmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import os
from .helpers import assert_equality
import pathlib
import datetime

this_dir = pathlib.Path(__file__).resolve().parent
test_name = "test_heatmap"
pathlib.Path("/tmp/tikzplotly").mkdir(parents=True, exist_ok=True)

def plot_1():
fig = px.imshow([[1, 20, 30],
[20, 1, 60],
[30, 60, 1]])
return fig

def plot_2():
df = px.data.medals_wide(indexed=True)
fig = px.imshow(df)
return fig

def plot_3():
fig = go.Figure(data=go.Heatmap(
z=[[1, None, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
y=['Morning', 'Afternoon', 'Evening'],
hoverongaps = False))
return fig

def plot_4():
import plotly.graph_objects as go

programmers = ['Alex','Nicole','Sara','Etienne','Chelsea','Jody','Marianne']

base = datetime.datetime(2021, 7, 20, 19, 30, 0)
dates = base - np.arange(180) * datetime.timedelta(days=1)
np.random.seed(43)
z = np.random.poisson(size=(len(programmers), len(dates)))

fig = go.Figure(data=go.Heatmap(
z=z,
x=dates,
y=programmers,
colorscale='Viridis'))

fig.update_layout(
title='GitHub commits per day',
xaxis_nticks=36)

return fig


def test_1():
assert_equality(plot_1(), os.path.join(this_dir, test_name, test_name + "_1_reference.tex"), img_name="/tmp/tikzplotly/fig1.png")

def test_2():
assert_equality(plot_2(), os.path.join(this_dir, test_name, test_name + "_2_reference.tex"), img_name="/tmp/tikzplotly/fig2.png")

def test_3():
assert_equality(plot_3(), os.path.join(this_dir, test_name, test_name + "_3_reference.tex"), img_name="/tmp/tikzplotly/fig3.png")

def test_4():
assert_equality(plot_4(), os.path.join(this_dir, test_name, test_name + "_4_reference.tex"), img_name="/tmp/tikzplotly/fig4.png")
30 changes: 30 additions & 0 deletions tests/test_heatmap/test_heatmap_1_reference.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
\begin{tikzpicture}


\begin{axis}[
y dir=reverse,
colorbar,
colormap={mycolor}{
rgb255(0.0cm)=(13,8,135);
rgb255(0.1111111111111111cm)=(70,3,159);
rgb255(0.2222222222222222cm)=(114,1,168);
rgb255(0.3333333333333333cm)=(156,23,158);
rgb255(0.4444444444444444cm)=(189,55,134);
rgb255(0.5555555555555556cm)=(216,87,107);
rgb255(0.6666666666666666cm)=(237,121,83);
rgb255(0.7777777777777778cm)=(251,159,58);
rgb255(0.8888888888888888cm)=(253,202,38);
rgb255(1.0cm)=(240,249,33);
},
point meta max=60.0,
point meta min=1.0,
xmin=-0.5,
xmax=2.5,
ymin=-0.5,
ymax=2.5,
tick align=outside,
tick pos=left
]
\addplot+ graphics[includegraphics cmd=\pgfimage,xmin=-0.5, xmax=2.5, ymin=2.5, ymax=-0.5] {/tmp/tikzplotly/fig1.png};
\end{axis}
\end{tikzpicture}
36 changes: 36 additions & 0 deletions tests/test_heatmap/test_heatmap_2_reference.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
\begin{tikzpicture}


\begin{axis}[
y dir=reverse,
colorbar,
xtick={0,1,2},
xticklabels={gold,silver,bronze},
ytick={0,1,2},
yticklabels={South Korea,China,Canada},
colormap={mycolor}{
rgb255(0.0cm)=(13,8,135);
rgb255(0.1111111111111111cm)=(70,3,159);
rgb255(0.2222222222222222cm)=(114,1,168);
rgb255(0.3333333333333333cm)=(156,23,158);
rgb255(0.4444444444444444cm)=(189,55,134);
rgb255(0.5555555555555556cm)=(216,87,107);
rgb255(0.6666666666666666cm)=(237,121,83);
rgb255(0.7777777777777778cm)=(251,159,58);
rgb255(0.8888888888888888cm)=(253,202,38);
rgb255(1.0cm)=(240,249,33);
},
point meta max=24.0,
point meta min=8.0,
xmin=-0.5,
xmax=2.5,
ymin=-0.5,
ymax=2.5,
tick align=outside,
tick pos=left,
xlabel=medal,
ylabel=nation
]
\addplot+ graphics[includegraphics cmd=\pgfimage,xmin=-0.5, xmax=2.5, ymin=2.5, ymax=-0.5] {/tmp/tikzplotly/fig2.png};
\end{axis}
\end{tikzpicture}
33 changes: 33 additions & 0 deletions tests/test_heatmap/test_heatmap_3_reference.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
\begin{tikzpicture}


\begin{axis}[
colorbar,
xtick={0,1,2,3,4},
xticklabels={Monday,Tuesday,Wednesday,Thursday,Friday},
ytick={0,1,2},
yticklabels={Morning,Afternoon,Evening},
colormap={mycolor}{
rgb255(0.0cm)=(13,8,135);
rgb255(0.1111111111111111cm)=(70,3,159);
rgb255(0.2222222222222222cm)=(114,1,168);
rgb255(0.3333333333333333cm)=(156,23,158);
rgb255(0.4444444444444444cm)=(189,55,134);
rgb255(0.5555555555555556cm)=(216,87,107);
rgb255(0.6666666666666666cm)=(237,121,83);
rgb255(0.7777777777777778cm)=(251,159,58);
rgb255(0.8888888888888888cm)=(253,202,38);
rgb255(1.0cm)=(240,249,33);
},
point meta max=80,
point meta min=-10,
xmin=-0.5,
xmax=4.5,
ymin=-0.5,
ymax=2.5,
tick align=outside,
tick pos=left
]
\addplot+ graphics[includegraphics cmd=\pgfimage,xmin=-0.5, xmax=4.5, ymin=2.5, ymax=-0.5] {/tmp/tikzplotly/fig3.png};
\end{axis}
\end{tikzpicture}
Loading

0 comments on commit 5a9a6fb

Please sign in to comment.