Skip to content
Snippets Groups Projects
Commit 6a381c5c authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Export plots to adjusted file names which include '.tmp.' and remove 'tmp'...

Export plots to adjusted file names which include '.tmp.' and remove 'tmp' afterwards to avoid overwriting original pictures.
parent 8f1764e0
No related branches found
No related tags found
8 merge requests!41Include latest changes in main branch,!37Merge dev upstream changes into improve/metadata,!34Include architecture diagram in docs,!32SAST implementation,!27Update documentation and version number,!26Merge !23, !24, !25 into main,!25Rework examples,!23Resolve "PNG file is removed from working directory upon export"
Pipeline #803461 waiting for manual action
......@@ -44,7 +44,7 @@ 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',
# FIGS_AND_IDS = tagplot(FIGS_AS_LIST, 'matplotlib', location='west',
# id_method='random', prefix=PROJECT_ID)
# Example for how to use tagplot with image files
......@@ -56,4 +56,4 @@ FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID,
# plots or images.
publish(FIGS_AND_IDS, ['../README.md', '../docs', '../LICENSE'],
'/home/chief/Dokumente/fst/plotid_python/data/', 'Bild')
'/home/chief/Dokumente/fst/plotid_python/data/', 'image')
......@@ -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):
......
......
......@@ -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.')
......
......
......@@ -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. """
......
......
......@@ -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)
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment