Skip to content
Snippets Groups Projects
Commit 49a2844e authored by nugget's avatar nugget
Browse files

More unittests, refactor for-loops.

parent 1ec032c1
Branches
Tags
2 merge requests!12v.0.1 for alpha release,!1Implement basic function to switch between plot engines.
......@@ -34,10 +34,10 @@ def TagPlot(figs, prefix, engine, method=1):
DESCRIPTION.
"""
# %% Validate inputs
if prefix:
if isinstance(prefix, str):
pass
else:
raise RuntimeWarning("No prefix set.")
raise TypeError("Prefix is not a string.")
if figs:
pass
......@@ -53,5 +53,4 @@ def TagPlot(figs, prefix, engine, method=1):
case 'matplotlib' | 'pyplot':
return TagPlot_matplotlib(figs, prefix, method)
case _:
raise ValueError(f'The plot engine "{engine}"'
' is not supported.')
raise ValueError(f'The plot engine "{engine}" is not supported.')
......@@ -4,6 +4,7 @@ import warnings
import CreateID
from fcn_help.FST_colors import Colors
import matplotlib.pyplot as plt
import matplotlib
#plt.style.use('./fcn_help/FST.mplstyle')
......@@ -15,9 +16,18 @@ def TagPlot_matplotlib(figs, prefix, method):
as Tag (Property of figure).
TagPlot can tag multiple figures at once
"""
# isinstance(figs, t) # Check if figure input is valid
location = 'nothing'
# Check if figs is a valid figure or a list of valid figures
if isinstance(figs, matplotlib.figure.Figure):
pass
elif isinstance(figs, list):
for i in range(len(figs)):
if isinstance(figs[i], matplotlib.figure.Figure):
pass
else:
raise TypeError('Figure is not a valid matplotlib-figure.')
location = 'east'
# TODO: Implement backwards combatibility with if-clauses
match location:
......@@ -50,18 +60,15 @@ def TagPlot_matplotlib(figs, prefix, method):
color = 'grey'
# Loop to create and position the IDs
for i in range(len(figs)):
# TODO: loop fails, when only one figure is given
for fig in figs:
ID = CreateID.CreateID(method)
# ID = prefix + '_' +'Test'+str(i)
ID = prefix + '_' + str(ID)
# print(ID)
fig = figs[i]
# print(type(fig))
plt.figure(fig.number)
plt.figtext(x=position[0], y=position[1], s=ID, ha='left', wrap=True,
rotation=rotation, fontsize=fontsize, color=color)
fig.tight_layout()
# plt.draw()
# TODO: Return all IDs, not only the last one
return(figs, ID)
Image diff could not be displayed: it is too large. Options to address this: view the blob.
Image diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -4,19 +4,16 @@ Created on Tue Jul 20 12:22:33 2021
example workflow for integrating plotIDs
@author: Richter
"""
# %% Code und Konsole aufräumen
# %reset
# %matplotlib
# %clear
# %% Module einbinden
from TagPlot import TagPlot
from fcn_help.FST_colors import Colors
import sys
import numpy as np
from numpy import random
# import h5py as h5
# import matplotlib
import matplotlib.pyplot as plt
from TagPlot import TagPlot
from fcn_help.FST_colors import Colors
plt.style.use('fcn_help/FST.mplstyle')
# %matplotlib qt
# %matplotlib inline
......@@ -26,46 +23,42 @@ sys.path.append("fcn_help")
# %% Project ID
ProjectID = "MR01"
ProjectID = "MR04"
# %% Plot engine
plot_engine = "matplotlib"
# %% Daten erzeugen
# %% Create sample data
x = np.linspace(0, 10, 100)
y = random.rand(100) + 2
y_2 = np.sin(x) + 2
# %% Daten speichern
dataset1 = "sample_data.npy"
np.save(dataset1, x, y, y_2)
# %% figure erstellen
# Farben
# %% Create figure
# Colors
colors = Colors() # create instance from class
color_list = colors.get_color_names()
# Plot erstellen
# Create plot
color1 = colors.get_rgb('black')
color2 = colors.get_rgb('yellow')
# 1.Figure
fig1 = plt.figure()
plt.plot(x, y, color=color1)
plt.plot(x, y_2, color=color2)
# plt.draw()
# 2.Figure
plt.clf # Close figure
fig2 = plt.figure()
plt.plot(x, y, color=color2)
plt.plot(x, y_2, color=color1)
# plt.draw()
plt.clf # Close figure
fig = [fig1, fig2]
# %% TagPlot
[figs, ID] = TagPlot(fig, ProjectID, plot_engine, method='1')
[figs, ID] = TagPlot(fig, ProjectID, plot_engine, method='2')
# %% Figure als tiff-Datei abspeichern
for i in range(len(figs)):
for i, figure in enumerate(figs):
name = "Test"+str(i)+".tiff"
figs[i].savefig(name)
figure.savefig(name)
......@@ -10,8 +10,8 @@ import CreateID
class TestCreateID(unittest.TestCase):
def test_existence(self):
self.assertIsNotNone(CreateID.CreateID(1))
self.assertIsNotNone(CreateID.CreateID(2))
self.assertIsInstance(CreateID.CreateID(1), str)
self.assertIsInstance(CreateID.CreateID(2), str)
def test_errors(self):
with self.assertRaises(ValueError):
......
......@@ -5,58 +5,57 @@ Unittests for TagPlot
'''
import unittest
from TagPlot import TagPlot
from fcn_help.FST_colors import Colors
import sys
import numpy as np
from numpy import random
import matplotlib.pyplot as plt
plt.style.use('../fcn_help/FST.mplstyle')
from matplotlib.figure import Figure
sys.path.append("fcn_core")
sys.path.append("fcn_help")
ProjectID = "MR01"
# %% Plot engine
plot_engine = "matplotlib"
# %% Daten erzeugen
# %% Create data
x = np.linspace(0, 10, 100)
y = random.rand(100) + 2
y = np.random.rand(100) + 2
y_2 = np.sin(x) + 2
# %% figure erstellen
colors = Colors() # create instance from class
color_list = colors.get_color_names()
# Plot erstellen
color1 = colors.get_rgb('black')
color2 = colors.get_rgb('yellow')
# %% Create figure
color1 = 'black'
color2 = 'yellow'
# 1.Figure
fig1 = plt.figure()
plt.plot(x, y, color=color1)
plt.plot(x, y_2, color=color2)
plt.clf # Close figure
# 2.Figure
plt.clf # Close figure
fig2 = plt.figure()
plt.plot(x, y, color=color2)
plt.plot(x, y_2, color=color1)
plt.clf # Close figure
fig = [fig1, fig2]
# %% TagPlot
[figs, ID] = TagPlot(fig, ProjectID, plot_engine, method=1)
# Constants for tests
ProjectID = "MR01"
plot_engine = "matplotlib"
method = 1
class TestTagPlot(unittest.TestCase):
def test_figures(self):
[figs, ID] = TagPlot(fig, ProjectID, plot_engine, method)
self.assertIsInstance(figs[0], Figure)
self.assertIsInstance(figs[1], Figure)
with self.assertRaises(TypeError):
TagPlot('fig', ProjectID, plot_engine, method)
def test_prefix(self):
with self.assertRaises(TypeError):
TagPlot(fig, 3, plot_engine, method)
def test_plotengine(self):
with self.assertRaises(ValueError):
TagPlot(fig, ProjectID, 3, method)
TagPlot(fig, ProjectID, 'xyz', method)
def test_idmethod(self):
with self.assertRaises(TypeError):
TagPlot(fig, ProjectID, plot_engine, method='(0,1)')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment