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

Fix merge conflicts.

parent 20ea1395
No related branches found
No related tags found
2 merge requests!12v.0.1 for alpha release,!8First OOP implementation of tagplot.py.
Test0.tiff

5.67 MiB

Test1.tiff

5.67 MiB

......@@ -9,7 +9,7 @@ import time
import uuid
def create_id(method):
def create_id(id_method):
"""
Create an Identifier (str).
......@@ -20,16 +20,16 @@ def create_id(method):
-------
ID
"""
if method == 1:
if id_method == 1:
ID = time.time() # UNIX Time
ID = hex(int(ID)) # convert time to hexadecimal
time.sleep(0.5) # break for avoiding duplicate IDs
elif method == 2:
elif id_method == 2:
ID = str(uuid.uuid4()) # creates a random UUID
ID = ID[0:8] # only use first 8 numbers
else:
raise ValueError(
f'Your chosen ID method "{method}" is not supported.\n'
f'Your chosen ID method "{id_method}" is not supported.\n'
'At the moment these methods are available:\n'
'"1": Unix time converted to hexadecimal\n'
'"2": Random UUID')
......
data/Bild.png

137 KiB

This diff is collapsed.
import matplotlib.pyplot as plt
def set_FST_template(project_directory):
plt.style.use(f'{project_directory}\\Code\\''FST.mplstyle')
class Colors():
def __init__(self):
self.rgb_colors = {
'blue' : (0, 0.31, 0.45),
'red' : (0.91, 0.31, 0.24),
'green' : (0.69, 0.8, 0.31),
'black' : (0, 0, 0),
'orange' : (0.93, 0.48, 0.20),
'petrol' : (0.31, 0.71, 0.58),
'grey' : (0.54, 0.54, 0.54),
'yellow' : (0.99, 0.79, 0)
}
def get_rgb(self, color_name):
if color_name in self.rgb_colors.keys():
return self.rgb_colors[color_name]
else:
print(f"Error: Color '{color_name}' does not exist.\nAvailable colors are: {list(self.rgb_colors.keys())}")
def get_color_names(self):
return list(self.rgb_colors.keys())
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 20 12:22:33 2021
example workflow for integrating plotIDs
@author: Richter
"""
# %% Module einbinden
import sys
import numpy as np
from numpy import random
# import h5py as h5
# import matplotlib
import matplotlib.pyplot as plt
from tagplot import PlotOptions, tagplot
from publish import publish
from fcn_help.FST_colors import Colors
plt.style.use('fcn_help/FST.mplstyle')
# %matplotlib qt
# %matplotlib inline
# %% Pfade hinzufügen
sys.path.append("fcn_core")
sys.path.append("fcn_help")
# %% Project ID
ProjectID = "MR04_"
# %% Plot engine
plot_engine = "matplotlib"
# %% Create sample data
x = np.linspace(0, 10, 100)
y = random.rand(100) + 2
y_2 = np.sin(x) + 2
# %% Create figure
# Colors
colors = Colors() # create instance from class
color_list = colors.get_color_names()
# 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)
# 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
# p1 = PlotOptions(fig, plot_engine, prefix=ProjectID,
# method='2', location='east')
# [figs, ID] = p1.tagplot()
[figs, ID] = tagplot(fig, plot_engine, prefix=ProjectID,
id_method='2', location='west')
# %% Figure als tiff-Datei abspeichern
for i, figure in enumerate(figs):
name = "Test"+str(i)+".tiff"
figure.savefig(name)
# %% Publish
publish('fcn_help', '/home/chief/Dokumente/fst/plotid_python/data',
fig1, 'Bild', 'individual')
......@@ -13,7 +13,7 @@ from numpy import random
# import h5py as h5
# import matplotlib
import matplotlib.pyplot as plt
from tagplot import PlotOptions
from tagplot import PlotOptions, tagplot
from publish import publish
# %% Project ID
......@@ -44,11 +44,11 @@ plt.plot(x, y_2, color=color1)
fig = [fig1, fig2]
# %% TagPlot
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')
# p1 = PlotOptions(fig, plot_engine, prefix=ProjectID,
# method='2', location='east')
# [figs, ID] = p1.tagplot()
[figs, ID] = tagplot(fig, plot_engine, prefix=ProjectID,
id_method='2', location='west')
# %% Figure als tiff-Datei abspeichern
for i, figure in enumerate(figs):
......
......@@ -2,19 +2,11 @@
# -*- coding: utf-8 -*-
import warnings
from plotoptions import PlotOptions
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):
def tagplot(figs, engine, prefix='', id_method=1, location='east'):
"""
Tag your figure/plot with an ID.
......@@ -29,9 +21,9 @@ class PlotOptions:
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.
id_method : int, optional
id_method for creating the ID. Create an ID by Unix time is referenced
as 1, create a random ID with id_method=2. The default is 1.
location : string, optional
Location for ID to be displayed on the plot. Default is 'east'.
......@@ -48,30 +40,28 @@ class PlotOptions:
The second list contains the corresponding IDs as strings.
"""
# %% Validate inputs
if isinstance(self.prefix, str):
if isinstance(prefix, str):
pass
else:
raise TypeError("Prefix is not a string.")
if isinstance(self.figs, list):
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.
# TODO: Change id_method key from integer to (more meaningful) string.
try:
self.method = int(self.method)
id_method = int(id_method)
except ValueError:
raise TypeError('The chosen ID method is not an integer.')
raise TypeError('The chosen ID id_method is not an integer.')
if isinstance(self.location, str):
if isinstance(location, str):
pass
else:
raise TypeError("Location is not a string.")
# TODO: Implement backwards combatibility with if-clauses
match self.location:
match location:
case 'north':
rotation = 0
position = (0.35, 0.975)
......@@ -92,23 +82,18 @@ class PlotOptions:
rotation = 0
position = (0.5, 0.5)
case _:
warnings.warn(f'Location "{self.location}" is not a defined '
warnings.warn(f'Location "{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,
option_container = PlotOptions(figs, prefix, id_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.')
match engine:
case 'matplotlib' | 'pyplot':
return tagplot_matplotlib(option_container)
case _:
raise ValueError(
f'The plot engine "{engine}" is not supported.')
......@@ -11,7 +11,7 @@ import matplotlib.pyplot as plt
import create_id
def tagplot_matplotlib(figs, prefix, method, rotation, position):
def tagplot_matplotlib(plotid_object):
"""
Add IDs to figures with matplotlib.
......@@ -20,10 +20,10 @@ def tagplot_matplotlib(figs, prefix, method, rotation, position):
TagPlot can tag multiple figures at once
"""
# Check if figs is a valid figure or a list of valid figures
if isinstance(figs, matplotlib.figure.Figure):
if isinstance(plotid_object.figs, matplotlib.figure.Figure):
pass
elif isinstance(figs, list):
for figure in figs:
elif isinstance(plotid_object.figs, list):
for figure in plotid_object.figs:
if isinstance(figure, matplotlib.figure.Figure):
pass
else:
......@@ -34,14 +34,15 @@ def tagplot_matplotlib(figs, prefix, method, rotation, position):
IDs = []
# Loop to create and position the IDs
for fig in figs:
ID = create_id.create_id(method)
ID = prefix + str(ID)
for fig in plotid_object.figs:
ID = create_id.create_id(plotid_object.id_method)
ID = plotid_object.prefix + str(ID)
IDs.append(ID)
plt.figure(fig.number)
plt.figtext(x=position[0], y=position[1], s=ID, ha='left', wrap=True,
rotation=rotation, fontsize=fontsize, color=color)
plt.figtext(x=plotid_object.position[0], y=plotid_object.position[1],
s=ID, ha='left', wrap=True,
rotation=plotid_object.rotation,
fontsize=fontsize, color=color)
fig.tight_layout()
return [figs, IDs]
return [plotid_object.figs, IDs]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment