diff --git a/TagPlot.py b/TagPlot.py deleted file mode 100644 index 0dce6ffa015bd122b9bb2779fcd3f2d7b2951791..0000000000000000000000000000000000000000 --- a/TagPlot.py +++ /dev/null @@ -1,111 +0,0 @@ -#!/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.') diff --git a/Test0.tiff b/Test0.tiff deleted file mode 100644 index ad7fd93e68abbd8a34c3bfa7a0aa5faf2c3e4598..0000000000000000000000000000000000000000 Binary files a/Test0.tiff and /dev/null differ diff --git a/Test1.tiff b/Test1.tiff deleted file mode 100644 index 19a3618f596fd052f5a5e46b242219bd0a18e1f5..0000000000000000000000000000000000000000 Binary files a/Test1.tiff and /dev/null differ diff --git a/example.py b/example.py index 4b10f8414b3d793a43acd8d6d633a7b53dcc49e2..a915acd37b0368fd732d87888ae6b6e61f610521 100644 --- a/example.py +++ b/example.py @@ -13,7 +13,7 @@ from numpy import random # import h5py as h5 # import matplotlib import matplotlib.pyplot as plt -from TagPlot import TagPlot +from tagplot import PlotOptions from publish import publish # %% Project ID @@ -44,8 +44,11 @@ plt.plot(x, y_2, color=color1) fig = [fig1, fig2] # %% TagPlot -[figs, ID] = TagPlot(fig, plot_engine, prefix=ProjectID, - method='2', location='west') +p1 = PlotOptions(fig, plot_engine, prefix=ProjectID, + 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 for i, figure in enumerate(figs): diff --git a/tagplot.py b/tagplot.py new file mode 100644 index 0000000000000000000000000000000000000000..97f42c5800a6861a6856580f5b6e45d87127eb1e --- /dev/null +++ b/tagplot.py @@ -0,0 +1,114 @@ +#!/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.') diff --git a/TagPlot_matplotlib.py b/tagplot_matplotlib.py similarity index 94% rename from TagPlot_matplotlib.py rename to tagplot_matplotlib.py index 184f6c454ffc8bbfc269c1bbe674e41824c6409a..31b9ec4ac22e5933900acbc2076b4b474a62b345 100644 --- a/TagPlot_matplotlib.py +++ b/tagplot_matplotlib.py @@ -11,7 +11,7 @@ import matplotlib.pyplot as plt 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. diff --git a/tests/test_TagPlot.py b/tests/test_tagplot.py similarity index 64% rename from tests/test_TagPlot.py rename to tests/test_tagplot.py index 5ab3991eb854a7bbd91481b64c64f2f5811fc8d3..25c1ee2aa59bef2305bba0227938e8f8bb743ecd 100644 --- a/tests/test_TagPlot.py +++ b/tests/test_tagplot.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' -Unittests for TagPlot +Unittests for tagplot ''' import unittest import numpy as np import matplotlib.pyplot as plt -from TagPlot import TagPlot +from tagplot import tagplot # %% Create data @@ -36,37 +36,37 @@ plot_engine = "matplotlib" method = 1 -class TestTagPlot(unittest.TestCase): +class Test_tagplot(unittest.TestCase): def test_figures(self): with self.assertRaises(TypeError): - TagPlot('fig', ProjectID, plot_engine, method) + tagplot('fig', ProjectID, plot_engine, method) with self.assertRaises(TypeError): - TagPlot(fig1, plot_engine, prefix=ProjectID) + tagplot(fig1, plot_engine, prefix=ProjectID) def test_prefix(self): with self.assertRaises(TypeError): - TagPlot(fig, plot_engine, 3, method) + tagplot(fig, plot_engine, 3, method) def test_plotengine(self): with self.assertRaises(ValueError): - TagPlot(fig, 1, ProjectID, method) + tagplot(fig, 1, ProjectID, method) with self.assertRaises(ValueError): - TagPlot(fig, 'xyz', ProjectID, method) + tagplot(fig, 'xyz', ProjectID, method) def test_idmethod(self): with self.assertRaises(TypeError): - TagPlot(fig, plot_engine, ProjectID, method='(0,1)') + tagplot(fig, plot_engine, ProjectID, method='(0,1)') with self.assertRaises(TypeError): - TagPlot(fig, plot_engine, ProjectID, method='h') + tagplot(fig, plot_engine, ProjectID, method='h') with self.assertRaises(TypeError): - TagPlot(fig, plot_engine, ProjectID, method='[0,1]') + tagplot(fig, plot_engine, ProjectID, method='[0,1]') def test_location(self): with self.assertRaises(TypeError): - TagPlot(fig, plot_engine, ProjectID, method, location=1) + tagplot(fig, plot_engine, ProjectID, method, location=1) with self.assertWarns(Warning): - TagPlot(fig, plot_engine, ProjectID, method, location='up') + tagplot(fig, plot_engine, ProjectID, method, location='up') if __name__ == '__main__': diff --git a/tests/test_TagPlot_matplotlib.py b/tests/test_tagplot_matplotlib.py similarity index 82% rename from tests/test_TagPlot_matplotlib.py rename to tests/test_tagplot_matplotlib.py index a3a3995060bdca4ed54145c25515c55a3bab8383..19e30760792eea7cd41cf46d43c158c36c3179dc 100644 --- a/tests/test_TagPlot_matplotlib.py +++ b/tests/test_tagplot_matplotlib.py @@ -5,7 +5,7 @@ Unittests for TagPlot_matplotlib """ import unittest -from TagPlot_matplotlib import TagPlot_matplotlib +from tagplot_matplotlib import tagplot_matplotlib import numpy as np import matplotlib.pyplot as plt from matplotlib.figure import Figure @@ -38,15 +38,15 @@ rotation = 90 position = (0.975, 0.35) -class TestTagPlot_matplotlib(unittest.TestCase): +class Test_tagplot_matplotlib(unittest.TestCase): def test_mplfigures(self): - [figs, ID] = TagPlot_matplotlib( + [figs, ID] = tagplot_matplotlib( fig, ProjectID, method, rotation, position) self.assertIsInstance(figs[0], Figure) self.assertIsInstance(figs[1], Figure) with self.assertRaises(TypeError): - TagPlot_matplotlib(3, ProjectID, method, rotation, position) + tagplot_matplotlib(3, ProjectID, method, rotation, position) if __name__ == '__main__':