Skip to content
Snippets Groups Projects
Commit 20ea1395 authored by nugget's avatar nugget
Browse files

Fix merge conflicts.

parent a60887a4
No related branches found
No related tags found
2 merge requests!12v.0.1 for alpha release,!8First OOP implementation of tagplot.py.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Tag your plot with an ID.
For publishing the tagged plot along your research data have a look at the
module publish.
Functions:
TagPlot(figure object, string) -> list
"""
import warnings
from TagPlot_matplotlib import TagPlot_matplotlib
def TagPlot(figs, engine, prefix='', method=1, location='east'):
"""
Tag your figure/plot with an ID.
After determining the plot engine, TagPlot calls the corresponding
function which tags the plot.
Parameters
----------
figs : list
Figures that should be tagged.
engine : string
Plot engine which should be used to tag the plot.
prefix : string
Will be added as prefix to the ID.
method : int, optional
Method for creating the ID. Create an ID by Unix time is referenced
as 1, create a random ID with method=2. The default is 1.
location : string, optional
Location for ID to be displayed on the plot. Default is 'east'.
Raises
------
RuntimeWarning
DESCRIPTION.
Returns
-------
list
The resulting list contains two lists each with as many entries as
figures were given. The first list contains the tagged figures.
The second list contains the corresponding IDs as strings.
"""
# %% Validate inputs
if isinstance(prefix, str):
pass
else:
raise TypeError("Prefix is not a string.")
if isinstance(figs, list):
pass
else:
raise TypeError("Figures are not a list.")
# TODO: Rename method to more specific name (maybe id_method?).
# TODO: Change method key from integer to (more meaningful) string.
try:
method = int(method)
except ValueError:
raise TypeError('The chosen ID method is not an integer.')
if isinstance(location, str):
pass
else:
raise TypeError("Location is not a string.")
match location:
case 'north':
rotation = 0
position = (0.35, 0.975)
case 'east':
rotation = 90
position = (0.975, 0.35)
case 'south':
rotation = 0
position = (0.35, 0.015)
case 'west':
rotation = 90
position = (0.025, 0.35)
case 'southeast':
rotation = 0
position = (0.75, 0.015)
case 'custom':
# TODO: Get rotation and position from user input & check if valid
rotation = 0
position = (0.5, 0.5)
case _:
warnings.warn(f'Location "{location}" is not a defined location,'
' TagPlot uses location "east" instead.')
rotation = 90
position = (0.975, 0.35)
if engine in ('matplotlib', 'pyplot'):
return TagPlot_matplotlib(figs, prefix, method, rotation, position)
else:
raise ValueError(f'The plot engine "{engine}" is not supported.')
# Following code only works for python >= 3.10
# match engine:
# case 'matplotlib' | 'pyplot':
# return TagPlot_matplotlib(figs, prefix, method)
# case _:
# raise ValueError(
# f'The plot engine "{engine}" is not supported.')
Test0.tiff

5.67 MiB

Test1.tiff

5.67 MiB

...@@ -13,7 +13,7 @@ from numpy import random ...@@ -13,7 +13,7 @@ from numpy import random
# import h5py as h5 # import h5py as h5
# import matplotlib # import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from TagPlot import TagPlot from tagplot import PlotOptions
from publish import publish from publish import publish
# %% Project ID # %% Project ID
...@@ -44,8 +44,11 @@ plt.plot(x, y_2, color=color1) ...@@ -44,8 +44,11 @@ plt.plot(x, y_2, color=color1)
fig = [fig1, fig2] fig = [fig1, fig2]
# %% TagPlot # %% TagPlot
[figs, ID] = TagPlot(fig, plot_engine, prefix=ProjectID, p1 = PlotOptions(fig, plot_engine, prefix=ProjectID,
method='2', location='west') method='2', location='east')
[figs, ID] = p1.tagplot()
# [figs, ID] = PlotOptions(fig, plot_engine, prefix=ProjectID,
# method='2', location='west')
# %% Figure als tiff-Datei abspeichern # %% Figure als tiff-Datei abspeichern
for i, figure in enumerate(figs): for i, figure in enumerate(figs):
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import warnings
from tagplot_matplotlib import tagplot_matplotlib
class PlotOptions:
def __init__(self, figs, engine, prefix='', method=1, location='east'):
self.figs = figs
self.engine = engine
self.prefix = prefix
self.method = method
self.location = location
def tagplot(self):
"""
Tag your figure/plot with an ID.
After determining the plot engine, TagPlot calls the corresponding
function which tags the plot.
Parameters
----------
figs : list
Figures that should be tagged.
prefix : string
Will be added as prefix to the ID.
engine : string
Plot engine which should be used to tag the plot.
method : int, optional
Method for creating the ID. Create an ID by Unix time is referenced
as 1, create a random ID with method=2. The default is 1.
location : string, optional
Location for ID to be displayed on the plot. Default is 'east'.
Raises
------
RuntimeWarning
DESCRIPTION.
Returns
-------
list
The resulting list contains two lists each with as many entries as
figures were given. The first list contains the tagged figures.
The second list contains the corresponding IDs as strings.
"""
# %% Validate inputs
if isinstance(self.prefix, str):
pass
else:
raise TypeError("Prefix is not a string.")
if isinstance(self.figs, list):
pass
else:
raise TypeError("Figures are not a list.")
# TODO: Rename method to more specific name (maybe id_method?).
# TODO: Change method key from integer to (more meaningful) string.
try:
self.method = int(self.method)
except ValueError:
raise TypeError('The chosen ID method is not an integer.')
if isinstance(self.location, str):
pass
else:
raise TypeError("Location is not a string.")
# TODO: Implement backwards combatibility with if-clauses
match self.location:
case 'north':
rotation = 0
position = (0.35, 0.975)
case 'east':
rotation = 90
position = (0.975, 0.35)
case 'south':
rotation = 0
position = (0.35, 0.015)
case 'west':
rotation = 90
position = (0.025, 0.35)
case 'southeast':
rotation = 0
position = (0.75, 0.015)
case 'custom':
# TODO: Get rotation and position from user input & check if valid
rotation = 0
position = (0.5, 0.5)
case _:
warnings.warn(f'Location "{self.location}" is not a defined '
'location, TagPlot uses location "east" '
'instead.')
rotation = 90
position = (0.975, 0.35)
if self.engine in ('matplotlib', 'pyplot'):
return tagplot_matplotlib(self.figs, self.prefix, self.method,
rotation, position)
else:
raise ValueError(f'The plot engine "{self.engine}" '
'is not supported.')
# Following code only works for python >= 3.10
# match engine:
# case 'matplotlib' | 'pyplot':
# return TagPlot_matplotlib(figs, prefix, method)
# case _:
# raise ValueError(
# f'The plot engine "{engine}" is not supported.')
...@@ -11,7 +11,7 @@ import matplotlib.pyplot as plt ...@@ -11,7 +11,7 @@ import matplotlib.pyplot as plt
import create_id import create_id
def TagPlot_matplotlib(figs, prefix, method, rotation, position): def tagplot_matplotlib(figs, prefix, method, rotation, position):
""" """
Add IDs to figures with matplotlib. Add IDs to figures with matplotlib.
......
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
''' '''
Unittests for TagPlot Unittests for tagplot
''' '''
import unittest import unittest
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from TagPlot import TagPlot from tagplot import tagplot
# %% Create data # %% Create data
...@@ -36,37 +36,37 @@ plot_engine = "matplotlib" ...@@ -36,37 +36,37 @@ plot_engine = "matplotlib"
method = 1 method = 1
class TestTagPlot(unittest.TestCase): class Test_tagplot(unittest.TestCase):
def test_figures(self): def test_figures(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot('fig', ProjectID, plot_engine, method) tagplot('fig', ProjectID, plot_engine, method)
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot(fig1, plot_engine, prefix=ProjectID) tagplot(fig1, plot_engine, prefix=ProjectID)
def test_prefix(self): def test_prefix(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot(fig, plot_engine, 3, method) tagplot(fig, plot_engine, 3, method)
def test_plotengine(self): def test_plotengine(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
TagPlot(fig, 1, ProjectID, method) tagplot(fig, 1, ProjectID, method)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
TagPlot(fig, 'xyz', ProjectID, method) tagplot(fig, 'xyz', ProjectID, method)
def test_idmethod(self): def test_idmethod(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot(fig, plot_engine, ProjectID, method='(0,1)') tagplot(fig, plot_engine, ProjectID, method='(0,1)')
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot(fig, plot_engine, ProjectID, method='h') tagplot(fig, plot_engine, ProjectID, method='h')
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot(fig, plot_engine, ProjectID, method='[0,1]') tagplot(fig, plot_engine, ProjectID, method='[0,1]')
def test_location(self): def test_location(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot(fig, plot_engine, ProjectID, method, location=1) tagplot(fig, plot_engine, ProjectID, method, location=1)
with self.assertWarns(Warning): with self.assertWarns(Warning):
TagPlot(fig, plot_engine, ProjectID, method, location='up') tagplot(fig, plot_engine, ProjectID, method, location='up')
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -5,7 +5,7 @@ Unittests for TagPlot_matplotlib ...@@ -5,7 +5,7 @@ Unittests for TagPlot_matplotlib
""" """
import unittest import unittest
from TagPlot_matplotlib import TagPlot_matplotlib from tagplot_matplotlib import tagplot_matplotlib
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.figure import Figure from matplotlib.figure import Figure
...@@ -38,15 +38,15 @@ rotation = 90 ...@@ -38,15 +38,15 @@ rotation = 90
position = (0.975, 0.35) position = (0.975, 0.35)
class TestTagPlot_matplotlib(unittest.TestCase): class Test_tagplot_matplotlib(unittest.TestCase):
def test_mplfigures(self): def test_mplfigures(self):
[figs, ID] = TagPlot_matplotlib( [figs, ID] = tagplot_matplotlib(
fig, ProjectID, method, rotation, position) fig, ProjectID, method, rotation, position)
self.assertIsInstance(figs[0], Figure) self.assertIsInstance(figs[0], Figure)
self.assertIsInstance(figs[1], Figure) self.assertIsInstance(figs[1], Figure)
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
TagPlot_matplotlib(3, ProjectID, method, rotation, position) tagplot_matplotlib(3, ProjectID, method, rotation, position)
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment