diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5ef0029b6b1aa805d65afec7d6f7a14bb5572e5a..7a57b4a2018e68619f7a4512fe8f9ef349090637 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,7 +48,7 @@ PEP8: Pylint: stage: linting # allow_failure: true - script: find src/plotid/ tests/ -type f -name '*.py' | xargs pylint -rn --fail-under=9 # Find all python files and check the code with pylint. + script: find . -type f -name '*.py' | xargs pylint -rn --fail-under=9 # Find all python files and check the code with pylint. test: stage: testing diff --git a/src/plotid/create_id.py b/src/plotid/create_id.py index ff759e3fd4855bac3586aa261badbb64c535b13b..fa7febcde918750391e4c6c1c38ad61fe99047e1 100644 --- a/src/plotid/create_id.py +++ b/src/plotid/create_id.py @@ -20,17 +20,18 @@ def create_id(id_method): ------- figure_id """ - if id_method == 1: - figure_id = time.time() # UNIX Time - figure_id = hex(int(figure_id)) # convert time to hexadecimal - time.sleep(0.5) # break for avoiding duplicate IDs - elif id_method == 2: - figure_id = str(uuid.uuid4()) # creates a random UUID - figure_id = figure_id[0:8] # only use first 8 numbers - else: - raise ValueError( - 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') + match id_method: + case 'time': + figure_id = time.time() # UNIX Time + figure_id = hex(int(figure_id)) # convert time to hexadecimal + time.sleep(0.5) # break for avoiding duplicate IDs + case 'random': + figure_id = str(uuid.uuid4()) # creates a random UUID + figure_id = figure_id[0:8] # only use first 8 numbers + case _: + raise ValueError( + f'Your chosen ID method "{id_method}" is not supported.\n' + 'At the moment these methods are available:\n' + '"time": Unix time converted to hexadecimal\n' + '"random": Random UUID') return figure_id diff --git a/src/plotid/example.py b/src/plotid/example.py index f8156f2acf233a057dc16fa6b9fbfefa03d2a1c9..491c986003196a98009dff80ee452a2258144fd4 100644 --- a/src/plotid/example.py +++ b/src/plotid/example.py @@ -9,55 +9,50 @@ the function publish. # %% Import modules import numpy as np -from numpy import random # import h5py as h5 -# import matplotlib import matplotlib.pyplot as plt from tagplot import tagplot from publish import publish -# %% Project ID -ProjectID = "MR04_" +# %% Set Project ID +PROJECT_ID = "MR04_" -# %% Plot engine -plot_engine = "matplotlib" +# %% Choose Plot engine +PLOT_ENGINE = "matplotlib" # %% Create sample data x = np.linspace(0, 10, 100) -y = random.rand(100) + 2 +y = np.random.rand(100) + 2 y_2 = np.sin(x) + 2 -# %% Create figure +# %% Create figures -# Create plot -color1 = 'black' -color2 = 'yellow' -# 1.Figure -fig1 = plt.figure() -plt.plot(x, y, color=color1) -plt.plot(x, y_2, color=color2) +# 1. figure +FIG1 = plt.figure() +plt.plot(x, y, color='black') +plt.plot(x, y_2, color='yellow') -# 2.Figure -fig2 = plt.figure() -plt.plot(x, y, color=color2) -plt.plot(x, y_2, color=color1) - -fig = [fig1, fig2] +# 2. figure +FIG2 = plt.figure() +plt.plot(x, y, color='blue') +plt.plot(x, y_2, color='red') # %% 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) +# If multiple figures should be tagged, figures must be provided as list. +FIGS_AS_LIST = [FIG1, FIG2] + +[TAGGED_FIGS, ID] = tagplot(FIGS_AS_LIST, PLOT_ENGINE, prefix=PROJECT_ID, + id_method='random', location='west') + +# %% Save figure as tiff-file, but publish also exports the plot to a picture +# file in the destination folder. +for i, figure in enumerate(TAGGED_FIGS): + NAME = "Test"+str(i)+".tiff" + figure.savefig(NAME) # %% Publish -# Arguments: Source directory, destination directory, figure, plot (which is -# already saved as picture, publish-mode). +# Arguments: Source directory, destination directory, figure, plot name, +# publish-mode). publish('../../tests', '/home/chief/Dokumente/fst/plotid_python/data', - fig1, 'Bild', 'individual') + FIG1, 'Bild', 'individual') diff --git a/src/plotid/plotoptions.py b/src/plotid/plotoptions.py index 53ffe2a8ae8f1e65fddfd66662127f0f8cf6abb9..6503422c73fe66f0fcd5a5fe9f44cdbd5ded1c7c 100644 --- a/src/plotid/plotoptions.py +++ b/src/plotid/plotoptions.py @@ -46,24 +46,19 @@ class PlotOptions: Returns ------- - 0, if all checks succeeded. + 0, if all checks succeed. """ # %% Validate inputs + # Input validation for figs is done in submodules tagplot_$engine.py if isinstance(self.prefix, str): pass else: raise TypeError("Prefix is not a string.") - if isinstance(self.figs, list): + if isinstance(self.id_method, str): pass else: - raise TypeError("Figures are not a list.") - - # TODO: Change id_method key from integer to (more meaningful) string. - try: - self.id_method = int(self.id_method) - except ValueError: - raise TypeError('The chosen ID id_method is not an integer.') + raise TypeError('The chosen id_method is not a string.') return 0 diff --git a/src/plotid/publish.py b/src/plotid/publish.py index 77486c82c5e465a771d89af3d03138c77337bece..34628258450ea7283cc8d6d93915d8d292980ad1 100644 --- a/src/plotid/publish.py +++ b/src/plotid/publish.py @@ -55,10 +55,6 @@ def publish(src_datapath, dst_path, figure, plot_name, data_storage): raise FileNotFoundError('The specified destination directory ' 'does not exist.') - # Check if handed over figure is not empty. - if not figure: - raise TypeError('No figure was given. ') - # If dst dir already exists ask user if it should be overwritten or not. if os.path.isdir(dst_path): warnings.warn(f'Folder "{dst_dirname}" already exists – ' @@ -87,9 +83,10 @@ def publish(src_datapath, dst_path, figure, plot_name, data_storage): # Does nothing, not implemented yet pass case 'individual': - # Copy data to invisible folder + # Copy all files to destination directory print('Copying data has been started. Depending on the size of ' 'your data this may take a while...') + # Copy data to invisible folder shutil.copytree(src_datapath, dst_path_invisible) # Copy script that calls this function to folder @@ -107,4 +104,3 @@ def publish(src_datapath, dst_path, figure, plot_name, data_storage): print(f'Publish was successful.\nYour plot "{plot_path}",\n' f'your data "{src_datapath}"\nand your script "{sys.argv[0]}"\n' f'were copied to {dst_path}\nin {data_storage} mode.') - return diff --git a/src/plotid/tagplot.py b/src/plotid/tagplot.py index e075dc66c6294a27d3856afa19f2a715bf6b1190..d0258ff0eff15be8830317168d0c7487edbbf9a3 100644 --- a/src/plotid/tagplot.py +++ b/src/plotid/tagplot.py @@ -15,7 +15,7 @@ from plotoptions import PlotOptions from tagplot_matplotlib import tagplot_matplotlib -def tagplot(figs, engine, prefix='', id_method=1, location='east'): +def tagplot(figs, engine, prefix='', id_method='time', location='east'): """ Tag your figure/plot with an ID. diff --git a/src/plotid/tagplot_matplotlib.py b/src/plotid/tagplot_matplotlib.py index b558b3c8c4e8f2f3c1d9a244dcc0050815359810..f527fb250e80f012787830c558b5a33a693b4831 100644 --- a/src/plotid/tagplot_matplotlib.py +++ b/src/plotid/tagplot_matplotlib.py @@ -26,7 +26,7 @@ def tagplot_matplotlib(plotid_object): 'of PlotOptions.') # Check if figs is a valid figure or a list of valid figures if isinstance(plotid_object.figs, matplotlib.figure.Figure): - pass + plotid_object.figs = [plotid_object.figs] elif isinstance(plotid_object.figs, list): for figure in plotid_object.figs: if isinstance(figure, matplotlib.figure.Figure): diff --git a/tests/runner_tests.py b/tests/runner_tests.py index 20beec95206fe1dac53ae04765e9c50c295369d4..613dde132f8d03b502c6042cb6281abecc265270 100644 --- a/tests/runner_tests.py +++ b/tests/runner_tests.py @@ -6,9 +6,9 @@ Includes starting all tests and measuring the code coverage. """ import sys +import os import unittest import coverage -import os path = os.path.abspath('src/plotid') sys.path.append(path) diff --git a/tests/test_create_id.py b/tests/test_create_id.py index d4560624afba6a7a5c19141abc241b52f842b6dd..0310ca471786ad3dc9103bacc593d8ccd081ea87 100644 --- a/tests/test_create_id.py +++ b/tests/test_create_id.py @@ -15,8 +15,8 @@ class TestCreateID(unittest.TestCase): def test_existence(self): """Test if create_id returns a string.""" - self.assertIsInstance(cid.create_id(1), str) - self.assertIsInstance(cid.create_id(2), str) + self.assertIsInstance(cid.create_id('time'), str) + self.assertIsInstance(cid.create_id('random'), str) def test_errors(self): """ Test if Errors are raised when id_method is wrong. """ @@ -27,8 +27,8 @@ class TestCreateID(unittest.TestCase): def test_length(self): """ Test if figure_id has the correct length. """ - self.assertEqual(len(cid.create_id(1)), 10) - self.assertEqual(len(cid.create_id(2)), 8) + self.assertEqual(len(cid.create_id('time')), 10) + self.assertEqual(len(cid.create_id('random')), 8) if __name__ == '__main__': diff --git a/tests/test_publish.py b/tests/test_publish.py index 86c3192524ff1dbc567ff12b8c0be9ca73c32907..f157600208776dade4851fe194c4e0b0b68e60e3 100644 --- a/tests/test_publish.py +++ b/tests/test_publish.py @@ -109,11 +109,6 @@ class TestPublish(unittest.TestCase): os.mkdir(INVISIBLE_PATH) publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'individual') - def test_picture(self): - """ Test if Error is raised if fig is not a valid figure object. """ - with self.assertRaises(TypeError): - publish(SRC_DIR, DST_PATH, 'fig', PIC_NAME, 'individual') - def test_data_storage(self): """ Test if Error is raised when unsupported storage method was chosen. diff --git a/tests/test_tagplot.py b/tests/test_tagplot.py index d0e5af2e21d2dd954fbb7650e9fe4ac017f5d3a9..cb71137911a71cccc28e348985a6f8973273f664 100644 --- a/tests/test_tagplot.py +++ b/tests/test_tagplot.py @@ -15,7 +15,7 @@ FIGS_AS_LIST = [FIG1, FIG2] PROJECT_ID = "MR01" PLOT_ENGINE = "matplotlib" -METHOD = 1 +METHOD = 'time' class TestTagplot(unittest.TestCase): @@ -23,40 +23,31 @@ class TestTagplot(unittest.TestCase): Class for all unittests of the tagplot module. """ - def test_figures(self): - """ - Test if Errors are raised when the provided figure is not given - as list. - """ - with self.assertRaises(TypeError): - tagplot('fig', PLOT_ENGINE, PROJECT_ID, METHOD) - with self.assertRaises(TypeError): - tagplot(FIG1, PLOT_ENGINE, prefix=PROJECT_ID) - def test_prefix(self): """ Test if Error is raised if prefix is not a string. """ with self.assertRaises(TypeError): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, 3, METHOD) + tagplot(FIGS_AS_LIST, PLOT_ENGINE, 3, METHOD, location='southeast') def test_plotengine(self): """ Test if Errors are raised if the provided plot engine is not supported. """ with self.assertRaises(ValueError): - tagplot(FIGS_AS_LIST, 1, PROJECT_ID, METHOD) + tagplot(FIGS_AS_LIST, 1, PROJECT_ID, METHOD, location='north') with self.assertRaises(ValueError): - tagplot(FIGS_AS_LIST, 'xyz', PROJECT_ID, METHOD) + tagplot(FIGS_AS_LIST, 'xyz', PROJECT_ID, METHOD, location='south') def test_idmethod(self): """ - Test if Errors are raised if the id_method is not an integer. + Test if Errors are raised if the id_method is not an string. """ with self.assertRaises(TypeError): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, PROJECT_ID, id_method='(0,1)') + tagplot(FIGS_AS_LIST, PLOT_ENGINE, PROJECT_ID, id_method=(0, 1), + location='west') with self.assertRaises(TypeError): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, PROJECT_ID, id_method='h') + tagplot(FIGS_AS_LIST, PLOT_ENGINE, PROJECT_ID, id_method=1) with self.assertRaises(TypeError): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, PROJECT_ID, id_method='[0,1]') + tagplot(FIGS_AS_LIST, PLOT_ENGINE, PROJECT_ID, id_method=[0, 1]) def test_location(self): """ diff --git a/tests/test_tagplot_matplotlib.py b/tests/test_tagplot_matplotlib.py index 50040d444f8303cb4414db71cb2892c7f284e4bc..4a6abf265ff2c5fc6f62e23c0e7a61e6db5a00e8 100644 --- a/tests/test_tagplot_matplotlib.py +++ b/tests/test_tagplot_matplotlib.py @@ -17,7 +17,7 @@ FIGS_AS_LIST = [FIG1, FIG2] # Constants for tests PROJECT_ID = "MR01" -METHOD = 1 +METHOD = 'time' ROTATION = 90 POSITION = (0.975, 0.35) @@ -35,6 +35,15 @@ class TestTagplotMatplotlib(unittest.TestCase): self.assertIsInstance(figs[0], Figure) self.assertIsInstance(figs[1], Figure) + def test_single_mplfigure(self): + """ + Test of returned objects. Check if matplotlib figures are returned, + if a single matplot figure is given (not as a list). + """ + options = PlotOptions(FIG1, PROJECT_ID, METHOD, ROTATION, POSITION) + [figs, _] = tagplot_matplotlib(options) + self.assertIsInstance(figs[0], Figure) + def test_mplerror(self): """ Test if Error is raised if wrong type of figures is given. """ options = PlotOptions(3, PROJECT_ID, METHOD, ROTATION, POSITION)