Skip to content
Snippets Groups Projects
Commit bba01f22 authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Merge branch '108-id_method-given-input' into 'new-year-developments'

Custom IDs

See merge request !61
parents 7755ff06 47ceefeb
No related branches found
No related tags found
3 merge requests!65Release v0.3.0,!64New year developments,!61Custom IDs
Pipeline #888204 passed
......@@ -56,6 +56,8 @@ The argument "plot_engine" defines which plot engine was used to create the figu
tagplot returns a PlotIDTransfer object that contains the tagged figures and the corresponding IDs as strings.
Optional parameters can be used to customize the tag process.
- *figure_ids*: list of str, optional
IDs that will be printed on the plot. If empty, IDs will be generated for each plot. If this option is used, an ID for each plot has to be specified. Default: [].
- *prefix* : str, optional
Will be added as prefix to the ID.
- *id_method* : str, optional
......
......@@ -29,6 +29,7 @@ FIGS_AND_IDS = tagplot(
"image",
prefix=PROJECT_ID,
id_method="time",
location="west",
qrcode=True,
)
# Required arguments: tagplot(images as list, desired plot engine)
......
......@@ -86,6 +86,9 @@ class PlotOptions:
self.position = position
self.prefix = kwargs.get("prefix", "")
self.id_method = kwargs.get("id_method", "time")
# Overwrite id_method if figure_ids were defined by the user
if self.figure_ids:
self.id_method = "custom"
self.qrcode = kwargs.get("qrcode", False)
self.qr_position = kwargs.get("qr_position", (1, 0))
self.qr_size = kwargs.get("qr_size", 100)
......
......@@ -39,6 +39,10 @@ def tagplot(
Other Parameters
----------------
figure_ids: list of str, optional
IDs that will be printed on the plot. If empty, IDs will be generated for each
plot. If this option is used, an ID for each plot has to be specified.
Default: [].
location : str, optional
Location for ID to be displayed on the plot. Default is "east".
rotation: float or int, optional
......
......@@ -56,9 +56,22 @@ def tagplot_image(plotid_object: PlotOptions) -> PlotIDTransfer:
except OSError:
warnings.warn("Font was not found.\nplotID continues with fallback font.")
for i, img in enumerate(plotid_object.figs):
img_id = plotid_object.prefix + create_id(plotid_object.id_method)
plotid_object.figure_ids.append(img_id)
if plotid_object.font:
try:
# Absolute path to font file (.ttf or .otf) has to be given
font = ImageFont.truetype(plotid_object.font, plotid_object.fontsize)
except OSError:
warnings.warn("Font was not found.\nplotID continues with fallback font.")
for j, img in enumerate(plotid_object.figs):
if plotid_object.id_method == "custom":
# If IDs were given by the user, use them
img_id: str = str(plotid_object.figure_ids[j])
else:
# Create ID with given method
img_id = plotid_object.prefix + create_id(plotid_object.id_method)
plotid_object.figure_ids.append(img_id)
img = Image.open(img)
if plotid_object.id_on_plot:
......@@ -92,7 +105,7 @@ def tagplot_image(plotid_object: PlotOptions) -> PlotIDTransfer:
),
),
)
plotid_object.figs[i] = img
plotid_object.figs[j] = img
figs_and_ids = PlotIDTransfer(plotid_object.figs, plotid_object.figure_ids)
return figs_and_ids
......@@ -50,10 +50,15 @@ def tagplot_matplotlib(plotid_object: PlotOptions) -> PlotIDTransfer:
font = matplotlib.font_manager.FontProperties(fname=font)
# Loop to create and position the IDs
for fig in plotid_object.figs:
fig_id: str = create_id(plotid_object.id_method)
fig_id = plotid_object.prefix + fig_id
plotid_object.figure_ids.append(fig_id)
for j, fig in enumerate(plotid_object.figs):
if plotid_object.id_method == "custom":
# If IDs were given by the user, use them
fig_id: str = str(plotid_object.figure_ids[j])
else:
# Create ID with given method
fig_id = create_id(plotid_object.id_method)
fig_id = plotid_object.prefix + fig_id
plotid_object.figure_ids.append(fig_id)
plt.figure(fig)
if plotid_object.id_on_plot:
......
......@@ -16,7 +16,6 @@ FIG1 = plt.figure()
IMG1 = "image1.png"
FIG2 = plt.figure()
IMG2 = "image2.jpg"
IMGS_AS_LIST = [IMG1, IMG2]
# Constants for tests
PROJECT_ID = "MR01"
......@@ -40,9 +39,10 @@ class TestTagplotImage(unittest.TestCase):
respectively.
"""
options = PlotOptions(
IMGS_AS_LIST,
[IMG1, IMG2],
ROTATION,
POSITION,
figure_ids=["test123456", "654321tset"],
prefix=PROJECT_ID,
id_method=METHOD,
qrcode=True,
......
......@@ -33,6 +33,7 @@ class TestTagplotMatplotlib(unittest.TestCase):
FIGS_AS_LIST,
ROTATION,
POSITION,
figure_ids=["test123456", "654321tset"],
prefix=PROJECT_ID,
id_method=METHOD,
qrcode=True,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment