diff --git a/examples/example_image1.png b/examples/example_image1.png new file mode 100644 index 0000000000000000000000000000000000000000..fe0d91339e7e24fbdf4a01aeee61634c28643026 Binary files /dev/null and b/examples/example_image1.png differ diff --git a/examples/example_image2.png b/examples/example_image2.png new file mode 100644 index 0000000000000000000000000000000000000000..ca9bf477a3e9ab3720bdff6b39887425145749ec Binary files /dev/null and b/examples/example_image2.png differ diff --git a/examples/image_example.py b/examples/image_example.py new file mode 100644 index 0000000000000000000000000000000000000000..29f173d7c7174459f0d8ec95be3fdcd893caead6 --- /dev/null +++ b/examples/image_example.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +""" +Example workflow for integrating plotID with jpg or png images. + +With tagplot() an ID can be generated and printed on the plot. To export the +plot along with the corresponding research data and the plot generating +script use the function publish(). +""" + +# %% Import modules +from plotid.tagplot import tagplot +from plotid.publish import publish + +# %% Set optional Project ID, which will be placed in front of the generated ID +PROJECT_ID = "MR05_" + +# %% Read example images +IMG1 = 'example_image1.png' +IMG2 = 'example_image2.png' + +# %% TagPlot + +# If multiple images should be tagged, they must be provided as list. +IMGS_AS_LIST = [IMG1, IMG2] + +# Example for how to use tagplot with image files +FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID, + id_method='time', location='west') +# Required arguments: tagplot(images as list, desired plot engine) + + +# %% Publish + +# Export your tagged images, copy the research data that generated the images, +# specify the destination folder and give a name for the exported image files. + +publish(FIGS_AND_IDS, ['../README.md', '../docs', '../LICENSE'], + './data', 'my_image') +# Required arguments: publish(output of tagplot(), list of files, +# path to destination folder, name(s) for the resulting images) diff --git a/plotid/example.py b/examples/matplotlib_example.py similarity index 60% rename from plotid/example.py rename to examples/matplotlib_example.py index 1bfe3ab7e7830b655350ca32bc9f41635248fb3d..7dbe6b7c7ab4f368042ad020931999ee65f415a7 100644 --- a/plotid/example.py +++ b/examples/matplotlib_example.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -Example workflow for integrating plotID. +Example workflow for integrating plotID with matplotlib figures. With tagplot() an ID can be generated and printed on the plot. To export the plot along with the corresponding research data and the plot generating @@ -24,36 +24,29 @@ y_2 = np.sin(x) + 2 # %% Create sample figures # 1. figure -IMG1 = 'image1.png' FIG1 = plt.figure() plt.plot(x, y, color='black') plt.plot(x, y_2, color='yellow') -plt.savefig(IMG1) # 2. figure -IMG2 = 'image2.png' FIG2 = plt.figure() plt.plot(x, y, color='blue') plt.plot(x, y_2, color='red') -plt.savefig(IMG2) # %% TagPlot # If multiple figures should be tagged, figures must be provided as list. FIGS_AS_LIST = [FIG1, FIG2] -IMGS_AS_LIST = [IMG1, IMG2] # Example for how to use tagplot with matplotlib figures -# [TAGGED_FIGS, ID] = tagplot(FIGS_AS_LIST, 'matplotlib', location='west', -# id_method='random', prefix=PROJECT_ID) +FIGS_AND_IDS = tagplot(FIGS_AS_LIST, 'matplotlib', location='west', + id_method='random', prefix=PROJECT_ID) +# Required arguments: tagplot(images as list, desired plot engine) -# Example for how to use tagplot with image files -FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID, - id_method='time', location='west') # %% Publish -# Arguments: Source directory or files as list, destination directory, figures, -# plots or images. publish(FIGS_AND_IDS, ['../README.md', '../docs', '../LICENSE'], - '/home/chief/Dokumente/fst/plotid_python/data/', 'Bild') + './data', 'my_plot') +# Required arguments: publish(output of tagplot(), list of files, +# path to destination folder, name(s) for the resulting images) diff --git a/plotid/publish.py b/plotid/publish.py index de95c9c9d6993d90bbc664d829e9051ed77042fb..8d55b26a2ba838a6fab46d19bb08025277119203 100644 --- a/plotid/publish.py +++ b/plotid/publish.py @@ -146,7 +146,7 @@ class PublishOptions: """ # Export plot figure to picture. plot_paths = save_plot(self.figure, self.plot_names) - + print(plot_paths) match self.data_storage: case 'centralized': self.centralized_data_storage() @@ -194,9 +194,9 @@ class PublishOptions: raise ValueError(f'The data storage method {self.data_storage}' ' is not available.') - print(f'Publish was successful.\nYour plot(s) {plot_paths},\nyour' - f' data {self.src_datapaths}\nand your script {sys.argv[0]}\n' - f'were copied to {self.dst_path}\nin {self.data_storage} mode.') + print(f'Publish was successful.\nYour plot(s), your' + f' data and your\nscript {sys.argv[0]}' + f'\nwere copied to {self.dst_path}.') def centralized_data_storage(self): """ @@ -240,10 +240,18 @@ class PublishOptions: # Copy script that calls this function to folder shutil.copy2(sys.argv[0], destination) - # Copy plot files to folder + if os.path.isfile(pic_path): + # Copy plot file to folder shutil.copy2(pic_path, destination) + # Remove by plotID exported .tmp plot os.remove(pic_path) + # Remove .tmp. from file name in destinaion + name_tmp, orig_ext = os.path.splitext(pic_path) + orig_name, _ = os.path.splitext(name_tmp) + final_file_path = orig_name + orig_ext + os.rename(os.path.join(destination, pic_path), + os.path.join(destination, final_file_path)) def publish(figs_and_ids, src_datapath, dst_path, plot_name, **kwargs): diff --git a/plotid/save_plot.py b/plotid/save_plot.py index 2c045854c15fc8b65ac7bc01ec3640fadc933ed7..5cf6f3b57a44f39e139d10af27d7bdcc4d18ae02 100644 --- a/plotid/save_plot.py +++ b/plotid/save_plot.py @@ -57,10 +57,10 @@ def save_plot(figures, plot_names, extension='png'): for i, fig in enumerate(figures): if isinstance(fig, matplotlib.figure.Figure): plt.figure(fig) - plot_path.append(plot_names[i] + '.' + extension) + plot_path.append(plot_names[i] + '.tmp.' + extension) plt.savefig(plot_path[i]) elif all(x in str(type(fig)) for x in ['PIL', 'ImageFile']): - plot_path.append(plot_names[i] + '.' + extension) + plot_path.append(plot_names[i] + '.tmp.' + extension) fig.save(plot_path[i]) else: raise TypeError(f'Figure number {i} is not a valid figure object.') diff --git a/tests/test_publish.py b/tests/test_publish.py index 58611fb421e6c69ee5efd6fe2093e1c78d3036f7..7a9c5d87f0971d22ea6f83fea010368f068e09eb 100644 --- a/tests/test_publish.py +++ b/tests/test_publish.py @@ -219,8 +219,8 @@ class TestPublish(unittest.TestCase): with self.assertRaises(RuntimeError): publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME) assert not os.path.isdir(invisible_path1) - os.remove('test_picture1.png') - os.remove('test_picture2.png') + os.remove('test_picture1.tmp.png') + os.remove('test_picture2.tmp.png') def test_plot_names(self): """ Test if Error is raised if plot_name is not a string. """ diff --git a/tests/test_save_plot.py b/tests/test_save_plot.py index fbb9f22f8b4d2a7198e82601f673183cc57d7190..a9f9f059c549f781480a2d51d87d523e68099a32 100644 --- a/tests/test_save_plot.py +++ b/tests/test_save_plot.py @@ -32,7 +32,7 @@ class TestSavePlot(unittest.TestCase): """ plot_paths = save_plot(FIGURE, [PLOT_NAME], extension='jpg') self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '.jpg') + os.remove(PLOT_NAME + '.tmp.jpg') def test_save_plot_image_png(self): """ @@ -42,7 +42,7 @@ class TestSavePlot(unittest.TestCase): img1 = Image.open(IMG1) plot_paths = save_plot(img1, [PLOT_NAME]) self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '.png') + os.remove(PLOT_NAME + '.tmp.png') def test_save_plot_image_jpg(self): """ @@ -53,8 +53,8 @@ class TestSavePlot(unittest.TestCase): imgs_as_list = [img2, img2] plot_paths = save_plot(imgs_as_list, [PLOT_NAME], extension='jpg') self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '1.jpg') - os.remove(PLOT_NAME + '2.jpg') + os.remove(PLOT_NAME + '1.tmp.jpg') + os.remove(PLOT_NAME + '2.tmp.jpg') def test_more_figs_than_names(self): """ @@ -64,8 +64,8 @@ class TestSavePlot(unittest.TestCase): with self.assertWarns(Warning): save_plot([FIGURE, FIGURE, FIGURE], [PLOT_NAME]) for i in (1, 2, 3): - assert os.path.isfile(PLOT_NAME + f'{i}.png') - os.remove(PLOT_NAME + f'{i}.png') + assert os.path.isfile(PLOT_NAME + f'{i}.tmp.png') + os.remove(PLOT_NAME + f'{i}.tmp.png') def test_more_names_than_figs(self): """ Test if Error is raised if more names than figures are given. """ @@ -84,7 +84,7 @@ class TestSavePlot(unittest.TestCase): """ with self.assertRaises(TypeError): save_plot([FIGURE, 'figure', FIGURE], 'PLOT_NAME', extension='jpg') - os.remove('PLOT_NAME1.jpg') + os.remove('PLOT_NAME1.tmp.jpg') def tearDown(self): os.remove(IMG1)