diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1735d5bf687c4ed56a0a05c17214877389fb107d..6f23dbf965ef4af0ca998f5e1867cb741ced0437 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,7 +19,7 @@ PEP8: stage: linting script: - pip install flake8 - - flake8 --count . + - flake8 --count --max-line-length=88 . Pylint: stage: linting @@ -27,6 +27,19 @@ Pylint: - pip install pylint - find . -type f -name '*.py' | xargs pylint -rn --rcfile='plotid/.pylintrc' # Find all python files and check the code with pylint +Autoformatting: + stage: linting + script: + - pip install black + - black --check --verbose --diff --color . + +Typechecker: + stage: linting + script: + - pip install mypy + - mypy --ignore-missing-imports --strict plotid tests examples + - allow_failure: true + test: stage: testing tags: diff --git a/docs/source/conf.py b/docs/source/conf.py index 09b075d408632deae4561c8e3fb9d6e660000f78..dfec1e430ace2b700542e35042c0d1d4da97d67b 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -14,15 +14,16 @@ import os import sys -sys.path.insert(0, os.path.abspath('../..')) + +sys.path.insert(0, os.path.abspath("../..")) from plotid import __version__, __author__ # noqa: E402 # -- Project information ----------------------------------------------------- -project = 'plotID' -copyright = '2022, ' + __author__ +project = "plotID" +copyright = "2022, " + __author__ author = __author__ # The full version, including alpha/beta/rc tags @@ -34,16 +35,12 @@ release = __version__ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -extensions = [ - 'sphinx.ext.napoleon', - 'autoapi.extension', - "myst_parser" -] -autoapi_type = 'python' -autoapi_dirs = ['../../plotid', '../../tests'] +extensions = ["sphinx.ext.napoleon", "autoapi.extension", "myst_parser"] +autoapi_type = "python" +autoapi_dirs = ["../../plotid", "../../tests"] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. @@ -56,10 +53,10 @@ exclude_patterns = [] # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -html_theme = 'sphinx_rtd_theme' -html_logo = '_static/plotID.jpg' +html_theme = "sphinx_rtd_theme" +html_logo = "_static/plotID.jpg" # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] diff --git a/examples/image_example.py b/examples/image_example.py index 29f173d7c7174459f0d8ec95be3fdcd893caead6..4c4693369b250799a536a4f410783abf6f5d32ba 100644 --- a/examples/image_example.py +++ b/examples/image_example.py @@ -15,8 +15,8 @@ from plotid.publish import publish PROJECT_ID = "MR05_" # %% Read example images -IMG1 = 'example_image1.png' -IMG2 = 'example_image2.png' +IMG1 = "example_image1.png" +IMG2 = "example_image2.png" # %% TagPlot @@ -24,8 +24,9 @@ IMG2 = 'example_image2.png' 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') +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) @@ -34,7 +35,6 @@ FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID, # 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') +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/examples/matplotlib_example.py b/examples/matplotlib_example.py index 0929036eb4308ea0afbf0a8cca93954eda6c6ed0..eddfab4f3de0d513836a93126f22187451ed14fe 100644 --- a/examples/matplotlib_example.py +++ b/examples/matplotlib_example.py @@ -25,13 +25,13 @@ y_2 = np.sin(x) + 2 # 1. figure FIG1 = plt.figure() -plt.plot(x, y, color='black') -plt.plot(x, y_2, color='yellow') +plt.plot(x, y, color="black") +plt.plot(x, y_2, color="yellow") # 2. figure FIG2 = plt.figure() -plt.plot(x, y, color='blue') -plt.plot(x, y_2, color='red') +plt.plot(x, y, color="blue") +plt.plot(x, y_2, color="red") # %% tagplot @@ -39,13 +39,13 @@ plt.plot(x, y_2, color='red') FIGS_AS_LIST = [FIG1, FIG2] # Example for how to use tagplot with matplotlib figures -FIGS_AND_IDS = 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) # %% Publish -publish(FIGS_AND_IDS, ['../README.md', '../docs', '../LICENSE'], - 'data', 'my_plot') +publish(FIGS_AND_IDS, ["../README.md", "../docs", "../LICENSE"], "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/__init__.py b/plotid/__init__.py index bedc85e6ce852383f2cc7bcd3e8f173c73ce330a..e572fc044d3a808bc423cb488588097f4b1a40ce 100644 --- a/plotid/__init__.py +++ b/plotid/__init__.py @@ -10,5 +10,5 @@ research data, the plot is based on. Additionally, the script that created the plot will also be copied to the directory. """ -__version__ = '0.2.2' -__author__ = 'Institut Fluidsystemtechnik within nfdi4ing at TU Darmstadt' +__version__ = "0.2.2" +__author__ = "Institut Fluidsystemtechnik within nfdi4ing at TU Darmstadt" diff --git a/plotid/create_id.py b/plotid/create_id.py index f28d94ae18e32fd835561a9cdbbc942f7b6660bc..61c1cc03be360848a3986bd00a6e7862721454ba 100644 --- a/plotid/create_id.py +++ b/plotid/create_id.py @@ -27,19 +27,20 @@ def create_id(id_method): figure_id : str """ match id_method: - case 'time': + case "time": figure_id = time.time() # UNIX Time figure_id = hex(int(figure_id)) # convert time to hexadecimal str - time.sleep(1) # break for avoiding duplicate IDs - case 'random': + time.sleep(1) # 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 + 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' + "At the moment these methods are available:\n" '"time": Unix time converted to hexadecimal\n' - '"random": Random UUID') + '"random": Random UUID' + ) return figure_id diff --git a/plotid/plotoptions.py b/plotid/plotoptions.py index f85d2300afbc0b71ffd8d40edf84e47b7db71dc6..79f576a9e4603cc056dee355bccfeb3d34d5f847 100644 --- a/plotid/plotoptions.py +++ b/plotid/plotoptions.py @@ -45,12 +45,12 @@ class PlotOptions: def __init__(self, figs, rotation, position, **kwargs): self.figs = figs - self.figure_ids = kwargs.get('figure_ids', []) + self.figure_ids = kwargs.get("figure_ids", []) self.rotation = rotation self.position = position - self.prefix = kwargs.get('prefix', '') - self.id_method = kwargs.get('id_method', 'time') - self.qrcode = kwargs.get('qrcode', False) + self.prefix = kwargs.get("prefix", "") + self.id_method = kwargs.get("id_method", "time") + self.qrcode = kwargs.get("qrcode", False) def __str__(self): """Representation if an object of this class is printed.""" @@ -76,7 +76,7 @@ class PlotOptions: raise TypeError("Prefix is not a string.") if not isinstance(self.id_method, str): - raise TypeError('The chosen id_method is not a string.') + raise TypeError("The chosen id_method is not a string.") # Store figs in a list, even if it is only one. if not isinstance(self.figs, list): @@ -145,14 +145,19 @@ def validate_list(list_var, elt_type=str, is_file=False): if isinstance(list_var, list): for elt in list_var: if not isinstance(elt, elt_type): - raise TypeError(f'The list of {list_var} contains an ' - f'object which is not of type {elt_type}.') + raise TypeError( + f"The list of {list_var} contains an " + f"object which is not of type {elt_type}." + ) if is_file: # Check if directory and files exist if not os.path.exists(elt): - raise FileNotFoundError('The specified directory' - f'/file {elt} does not exist.') + raise FileNotFoundError( + "The specified directory" f"/file {elt} does not exist." + ) else: - raise TypeError(f'The specified {list_var} are neither a ' - f'{elt_type} nor a list of {elt_type}.') + raise TypeError( + f"The specified {list_var} are neither a " + f"{elt_type} nor a list of {elt_type}." + ) return list_var diff --git a/plotid/publish.py b/plotid/publish.py index cf5a1d3f829074dcceff13d030cf5663ffa47468..d10d0223c6c23c259363fe5a147502aa31b5a99c 100644 --- a/plotid/publish.py +++ b/plotid/publish.py @@ -33,18 +33,16 @@ class PublishOptions: Export the plot and copy specified files to the destiantion folder. """ - def __init__(self, figs_and_ids, src_datapaths, dst_path, plot_names, - **kwargs): + def __init__(self, figs_and_ids, src_datapaths, dst_path, plot_names, **kwargs): if not isinstance(figs_and_ids, PlotIDTransfer): - raise RuntimeError('figs_and_ids is not an instance of ' - 'PlotIDTransfer.') + raise RuntimeError("figs_and_ids is not an instance of " "PlotIDTransfer.") self.figure = figs_and_ids.figs self.figure_ids = figs_and_ids.figure_ids self.src_datapaths = src_datapaths self.dst_path = os.path.abspath(dst_path) self.plot_names = plot_names - self.data_storage = kwargs.get('data_storage', 'individual') + self.data_storage = kwargs.get("data_storage", "individual") self.dst_path_head, self.dst_dirname = os.path.split(self.dst_path) def __str__(self): @@ -75,23 +73,25 @@ class PublishOptions: self.plot_names = validate_list(self.plot_names) if not os.path.isfile(sys.argv[0]): - raise FileNotFoundError('Cannot copy original python script. ' - 'Running publish from a shell is not ' - 'possible.') + raise FileNotFoundError( + "Cannot copy original python script. " + "Running publish from a shell is not " + "possible." + ) # Check if self.src_datapaths are strings and existing files. - self.src_datapaths = validate_list(self.src_datapaths, - is_file=True) + self.src_datapaths = validate_list(self.src_datapaths, is_file=True) # Check if destination directory is allowed path if not os.path.exists(self.dst_path_head): - raise FileNotFoundError('The specified destination directory ' - f'{self.dst_path_head} does not exist.') + raise FileNotFoundError( + "The specified destination directory " + f"{self.dst_path_head} does not exist." + ) # Check if data_storage is a string if not isinstance(self.data_storage, str): - raise TypeError('The specified data_storage method is not a ' - 'string.') + raise TypeError("The specified data_storage method is not a " "string.") def export(self): """ @@ -112,55 +112,66 @@ class PublishOptions: # Export plot figure to picture. plot_paths = save_plot(self.figure, self.plot_names) match self.data_storage: - case 'centralized': + case "centralized": self.centralized_data_storage() - case 'individual': + case "individual": for i, plot in enumerate(plot_paths): try: # Create folder with ID as name - dst_path = os.path.join(self.dst_path, - self.figure_ids[i]) - dst_path_invisible = os.path.join(self.dst_path, '.' - + self.figure_ids[i]) + dst_path = os.path.join(self.dst_path, self.figure_ids[i]) + dst_path_invisible = os.path.join( + self.dst_path, "." + self.figure_ids[i] + ) # If dir with the same ID already exists ask user # if it should be overwritten. if os.path.isdir(dst_path): - warnings.warn(f'Folder "{dst_path}" already exists' - ' – plot has already been published.' - ) - overwrite_dir = input('Do you want to overwrite ' - 'the existing folder? ' - '(yes/no[default])\n') - if overwrite_dir in ('yes', 'y', 'Yes', 'YES'): + warnings.warn( + f'Folder "{dst_path}" already exists' + " – plot has already been published." + ) + overwrite_dir = input( + "Do you want to overwrite " + "the existing folder? " + "(yes/no[default])\n" + ) + if overwrite_dir in ("yes", "y", "Yes", "YES"): shutil.rmtree(dst_path) else: - raise RuntimeError('publish has finished ' - 'without an export.\nRerun ' - 'tagplot if you need a new' - ' ID or consider ' - 'overwriting.') + raise RuntimeError( + "publish has finished " + "without an export.\nRerun " + "tagplot if you need a new" + " ID or consider " + "overwriting." + ) self.individual_data_storage(dst_path_invisible, plot) # If export was successful, make the directory visible os.rename(dst_path_invisible, dst_path) except FileExistsError as exc: - delete_dir = input('There was an error while ' - 'publishing the data. Should the ' - 'partially copied data at ' - f'{dst_path_invisible} be' - ' removed? (yes/no[default])\n') - if delete_dir in ('yes', 'y', 'Yes', 'YES'): + delete_dir = input( + "There was an error while " + "publishing the data. Should the " + "partially copied data at " + f"{dst_path_invisible} be" + " removed? (yes/no[default])\n" + ) + if delete_dir in ("yes", "y", "Yes", "YES"): shutil.rmtree(dst_path_invisible) - raise RuntimeError('Publishing was unsuccessful. ' - 'Try re-running publish.') from exc + raise RuntimeError( + "Publishing was unsuccessful. " "Try re-running publish." + ) from exc case _: - raise ValueError(f'The data storage method {self.data_storage}' - ' is not available.') + raise ValueError( + f"The data storage method {self.data_storage}" " is not available." + ) - print(f'Publish was successful.\nYour plot(s), your' - f' data and your\nscript {sys.argv[0]}' - f'\nwere copied to {self.dst_path}.') + 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): """ @@ -192,8 +203,10 @@ class PublishOptions: """ # Copy all files to destination directory - print('Copying data has been started. Depending on the size of' - ' your data this may take a while...') + print( + "Copying data has been started. Depending on the size of" + " your data this may take a while..." + ) os.makedirs(destination) # Copy data to destination folder for path in self.src_datapaths: @@ -214,8 +227,10 @@ class PublishOptions: 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)) + 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): @@ -249,7 +264,8 @@ def publish(figs_and_ids, src_datapath, dst_path, plot_name, **kwargs): None. """ - publish_container = PublishOptions(figs_and_ids, src_datapath, dst_path, - plot_name, **kwargs) + publish_container = PublishOptions( + figs_and_ids, src_datapath, dst_path, plot_name, **kwargs + ) publish_container.validate_input() publish_container.export() diff --git a/plotid/save_plot.py b/plotid/save_plot.py index 1fa7063c80fc26974b7ba7dbf17c1393f16f2d44..d587156baad5c5dd6983514fcd659e3a7dce954b 100644 --- a/plotid/save_plot.py +++ b/plotid/save_plot.py @@ -12,7 +12,7 @@ import matplotlib import matplotlib.pyplot as plt -def save_plot(figures, plot_names, extension='png'): +def save_plot(figures, plot_names, extension="png"): """ Export plot(s). @@ -35,34 +35,36 @@ def save_plot(figures, plot_names, extension='png'): figures = [figures] # PIL has different classes for different file formats. Therefore, the # type is checked to contain 'PIL' and 'ImageFile'. - if all(x in str(type(figures)) for x in ['PIL', 'ImageFile']): + if all(x in str(type(figures)) for x in ["PIL", "ImageFile"]): figures = [figures] if not isinstance(figures, list): - raise TypeError('Figures are not given as list.') + raise TypeError("Figures are not given as list.") if isinstance(plot_names, str): plot_names = [plot_names] if len(plot_names) < len(figures): - warnings.warn('There are more figures than plot names. The first name' - ' will be taken for all plots with an appended number.') + warnings.warn( + "There are more figures than plot names. The first name" + " will be taken for all plots with an appended number." + ) first_name = plot_names[0] plot_names = [None] * len(figures) for i, _ in enumerate(plot_names): - plot_names[i] = first_name + f'{i+1}' + plot_names[i] = first_name + f"{i+1}" elif len(plot_names) > len(figures): - raise IndexError('There are more plot names than figures.') + raise IndexError("There are more plot names than figures.") plot_path = [] for i, fig in enumerate(figures): if isinstance(fig, matplotlib.figure.Figure): plt.figure(fig) - plot_path.append(plot_names[i] + '.tmp.' + 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] + '.tmp.' + extension) + elif all(x in str(type(fig)) for x in ["PIL", "ImageFile"]): + 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.') + raise TypeError(f"Figure number {i} is not a valid figure object.") return plot_path diff --git a/plotid/tagplot.py b/plotid/tagplot.py index b0f365ed8f0d493add69b06ea1ac5b2df0f04701..da83b3ec1756727ffc14642ff0a464cf08d85f9e 100644 --- a/plotid/tagplot.py +++ b/plotid/tagplot.py @@ -16,7 +16,7 @@ from plotid.tagplot_matplotlib import tagplot_matplotlib from plotid.tagplot_image import tagplot_image -def tagplot(figs, engine, location='east', **kwargs): +def tagplot(figs, engine, location="east", **kwargs): """ Tag your figure/plot with an ID. @@ -65,28 +65,30 @@ def tagplot(figs, engine, location='east', **kwargs): raise TypeError("Location is not a string.") match location: - case 'north': + case "north": rotation = 0 position = (0.35, 0.975) - case 'east': + case "east": rotation = 90 position = (0.975, 0.35) - case 'south': + case "south": rotation = 0 position = (0.35, 0.015) - case 'west': + case "west": rotation = 90 position = (0.025, 0.35) - case 'southeast': + case "southeast": rotation = 0 position = (0.75, 0.015) - case 'custom': + case "custom": # TODO: Get rotation and position from user input & check if valid pass case _: - warnings.warn(f'Location "{location}" is not a defined ' - 'location, TagPlot uses location "east" ' - 'instead.') + warnings.warn( + f'Location "{location}" is not a defined ' + 'location, TagPlot uses location "east" ' + "instead." + ) rotation = 90 position = (0.975, 0.35) @@ -94,10 +96,9 @@ def tagplot(figs, engine, location='east', **kwargs): option_container.validate_input() match engine: - case 'matplotlib' | 'pyplot': + case "matplotlib" | "pyplot": return tagplot_matplotlib(option_container) - case 'image' | 'fallback': + case "image" | "fallback": return tagplot_image(option_container) case _: - raise ValueError( - f'The plot engine "{engine}" is not supported.') + raise ValueError(f'The plot engine "{engine}" is not supported.') diff --git a/plotid/tagplot_image.py b/plotid/tagplot_image.py index 5569097ea41f97fd6cd74e6cbe7edd92f04a5990..7c0c6adda887cfad33e8a70423512119ee5d0f28 100644 --- a/plotid/tagplot_image.py +++ b/plotid/tagplot_image.py @@ -28,15 +28,16 @@ def tagplot_image(plotid_object): """ # Check if plotid_object is a valid instance of PlotOptions if not isinstance(plotid_object, PlotOptions): - raise TypeError('The given options container is not an instance' - 'of PlotOptions.') + raise TypeError( + "The given options container is not an instance" "of PlotOptions." + ) # Check if figs is a list of files for img in plotid_object.figs: if not isinstance(img, str): - raise TypeError('Name of the image is not a string.') + raise TypeError("Name of the image is not a string.") if not os.path.isfile(img): - raise TypeError('File does not exist.') + raise TypeError("File does not exist.") # Check if figs is a valid file is done by pillow internally color = (128, 128, 128) # grey @@ -47,18 +48,23 @@ def tagplot_image(plotid_object): plotid_object.figure_ids.append(img_id) img = Image.open(img) - img_txt = Image.new('L', font.getsize(img_id)) + img_txt = Image.new("L", font.getsize(img_id)) draw_txt = ImageDraw.Draw(img_txt) draw_txt.text((0, 0), img_id, font=font, fill=255) txt = img_txt.rotate(plotid_object.rotation, expand=1) - img.paste(ImageOps.colorize(txt, (0, 0, 0), color), - (int(img.width*plotid_object.position[0]), - int(img.height*(1-plotid_object.position[1]))), txt) + img.paste( + ImageOps.colorize(txt, (0, 0, 0), color), + ( + int(img.width * plotid_object.position[0]), + int(img.height * (1 - plotid_object.position[1])), + ), + txt, + ) if plotid_object.qrcode: qrcode = create_qrcode(img_id) qrcode.thumbnail((100, 100), Image.ANTIALIAS) - img.paste(qrcode, box=(img.width-100, img.height-100)) + img.paste(qrcode, box=(img.width - 100, img.height - 100)) plotid_object.figs[i] = img figs_and_ids = PlotIDTransfer(plotid_object.figs, plotid_object.figure_ids) diff --git a/plotid/tagplot_matplotlib.py b/plotid/tagplot_matplotlib.py index d9fe383d5d5d0e6d5aedb8bd03f94d86746739ae..883178ba2b3950b7e5758c2c8385bfbd820d5786 100644 --- a/plotid/tagplot_matplotlib.py +++ b/plotid/tagplot_matplotlib.py @@ -30,16 +30,17 @@ def tagplot_matplotlib(plotid_object): """ # Check if plotid_object is a valid instance of PlotOptions if not isinstance(plotid_object, PlotOptions): - raise TypeError('The given options container is not an instance' - 'of PlotOptions.') + raise TypeError( + "The given options container is not an instance" "of PlotOptions." + ) # Check if figs is a list of valid figures for figure in plotid_object.figs: if not isinstance(figure, matplotlib.figure.Figure): - raise TypeError('Figure is not a valid matplotlib-figure.') + raise TypeError("Figure is not a valid matplotlib-figure.") - fontsize = 'small' - color = 'grey' + fontsize = "small" + color = "grey" # Loop to create and position the IDs for fig in plotid_object.figs: @@ -48,15 +49,21 @@ def tagplot_matplotlib(plotid_object): plotid_object.figure_ids.append(fig_id) plt.figure(fig) - plt.figtext(x=plotid_object.position[0], y=plotid_object.position[1], - s=fig_id, ha='left', wrap=True, - rotation=plotid_object.rotation, - fontsize=fontsize, color=color) + plt.figtext( + x=plotid_object.position[0], + y=plotid_object.position[1], + s=fig_id, + ha="left", + wrap=True, + rotation=plotid_object.rotation, + fontsize=fontsize, + color=color, + ) if plotid_object.qrcode: qrcode = create_qrcode(fig_id) qrcode.thumbnail((100, 100), Image.ANTIALIAS) - fig.figimage(qrcode, fig.bbox.xmax - 100, 0, cmap='bone') + fig.figimage(qrcode, fig.bbox.xmax - 100, 0, cmap="bone") fig.tight_layout() figs_and_ids = PlotIDTransfer(plotid_object.figs, plotid_object.figure_ids) diff --git a/tests/runner_tests.py b/tests/runner_tests.py index ce944d6f7379a4a71b7921a6afb358728dc63e1c..81eb90753ceb2cc08c0e0a70b9717e2774eb9da1 100644 --- a/tests/runner_tests.py +++ b/tests/runner_tests.py @@ -10,14 +10,14 @@ import os import unittest import coverage -path = os.path.abspath('plotid') +path = os.path.abspath("plotid") sys.path.append(path) cov = coverage.Coverage() cov.start() loader = unittest.TestLoader() -tests = loader.discover('.') +tests = loader.discover(".") testRunner = unittest.runner.TextTestRunner(verbosity=2) result = testRunner.run(tests) diff --git a/tests/test_create_id.py b/tests/test_create_id.py index 2f8e4b2b669b49a8f295ba7f60799e89804b02b5..c494a265fc9bd5fb15804ec67aef1f545114d80c 100644 --- a/tests/test_create_id.py +++ b/tests/test_create_id.py @@ -17,26 +17,25 @@ class TestCreateID(unittest.TestCase): def test_existence(self): """Test if create_id returns a string.""" - self.assertIsInstance(cid.create_id('time'), str) - self.assertIsInstance(cid.create_id('random'), 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. """ + """Test if Errors are raised when id_method is wrong.""" with self.assertRaises(ValueError): cid.create_id(3) with self.assertRaises(ValueError): - cid.create_id('h') + cid.create_id("h") def test_length(self): - """ Test if figure_id has the correct length. """ - self.assertEqual(len(cid.create_id('time')), 10) - self.assertEqual(len(cid.create_id('random')), 8) + """Test if figure_id has the correct length.""" + self.assertEqual(len(cid.create_id("time")), 10) + self.assertEqual(len(cid.create_id("random")), 8) def test_qrcode(self): """Test if qrcode returns a image.""" - self.assertIsInstance(create_qrcode('test_ID'), - qrcode.image.pil.PilImage) + self.assertIsInstance(create_qrcode("test_ID"), qrcode.image.pil.PilImage) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_plotoptions.py b/tests/test_plotoptions.py index 87b4f306f5781172f54f209397e3a463185d968b..4889c9fe98ef3eced0f6f1adeaed863095fc8f43 100644 --- a/tests/test_plotoptions.py +++ b/tests/test_plotoptions.py @@ -20,42 +20,47 @@ class TestTagplot(unittest.TestCase): """ Test if input validation runs successful. """ - PlotOptions('FIG', ROTATION, POSITION, prefix='xyz', - id_method='random').validate_input() + PlotOptions( + "FIG", ROTATION, POSITION, prefix="xyz", id_method="random" + ).validate_input() def test_prefix(self): - """ Test if Error is raised if prefix is not a string. """ + """Test if Error is raised if prefix is not a string.""" with self.assertRaises(TypeError): - PlotOptions(['FIG'], ROTATION, POSITION, prefix=3).validate_input() + PlotOptions(["FIG"], ROTATION, POSITION, prefix=3).validate_input() def test_data_storage(self): - """ Test if Error is raised if id_method is not a string. """ + """Test if Error is raised if id_method is not a string.""" with self.assertRaises(TypeError): - PlotOptions(['FIG'], ROTATION, POSITION, - id_method=4).validate_input() + PlotOptions(["FIG"], ROTATION, POSITION, id_method=4).validate_input() def test_str_plotoptions(self): """ Test if the string representation of a PlotOptions object is correct. """ - plot_obj = PlotOptions('FIG', ROTATION, POSITION, prefix='xyz', - id_method='random') - self.assertEqual(str(plot_obj), - "<class 'plotid.plotoptions.PlotOptions'>: {'figs': " - "'FIG', 'figure_ids': [], 'rotation': 270, 'position'" - ": (100, 200), 'prefix': 'xyz', 'id_method': " - "'random', 'qrcode': False}") + plot_obj = PlotOptions( + "FIG", ROTATION, POSITION, prefix="xyz", id_method="random" + ) + self.assertEqual( + str(plot_obj), + "<class 'plotid.plotoptions.PlotOptions'>: {'figs': " + "'FIG', 'figure_ids': [], 'rotation': 270, 'position'" + ": (100, 200), 'prefix': 'xyz', 'id_method': " + "'random', 'qrcode': False}", + ) def test_str_plotidtransfer(self): """ Test if the string representation of a PlotIDTransfer object is correct. """ - transfer_obj = PlotIDTransfer('FIG', []) - self.assertEqual(str(transfer_obj), - "<class 'plotid.plotoptions.PlotIDTransfer'>: " - "{'figs': 'FIG', 'figure_ids': []}") + transfer_obj = PlotIDTransfer("FIG", []) + self.assertEqual( + str(transfer_obj), + "<class 'plotid.plotoptions.PlotIDTransfer'>: " + "{'figs': 'FIG', 'figure_ids': []}", + ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_publish.py b/tests/test_publish.py index 7d2ff4954d4fe6cd21e6756c9c1fc57b72bf181f..7bacf8c136eecff5eea6a8f72049f18ca42e922c 100644 --- a/tests/test_publish.py +++ b/tests/test_publish.py @@ -16,18 +16,18 @@ from plotid.publish import publish, PublishOptions from plotid.plotoptions import PlotIDTransfer -SRC_DIR = 'test_src_folder' -SRC_FILES = ['test_file1.txt', 'test_file2.jpg', 'test_file3.exe'] -PIC_NAME = 'test_picture' -DST_DIR = 'test_dst_folder' -DST_PARENT_DIR = 'test_parent' +SRC_DIR = "test_src_folder" +SRC_FILES = ["test_file1.txt", "test_file2.jpg", "test_file3.exe"] +PIC_NAME = "test_picture" +DST_DIR = "test_dst_folder" +DST_PARENT_DIR = "test_parent" DST_PATH = os.path.join(DST_PARENT_DIR, DST_DIR) FIG = plt.figure(figsize=[6.4, 4.8], dpi=100) FIG2 = plt.figure(figsize=[6.4, 4.8], dpi=100) FIGS_AS_LIST = [FIG, FIG2] -IDS_AS_LIST = ['MR05_0x63203c6f', 'MR05_0x63203c70'] +IDS_AS_LIST = ["MR05_0x63203c6f", "MR05_0x63203c70"] FIGS_AND_IDS = PlotIDTransfer(FIGS_AS_LIST, IDS_AS_LIST) -PIC_NAME_LIST = [PIC_NAME, 'second_picture'] +PIC_NAME_LIST = [PIC_NAME, "second_picture"] class TestPublish(unittest.TestCase): @@ -36,30 +36,40 @@ class TestPublish(unittest.TestCase): """ def setUp(self): - """ Generate source and destination directory and source files. """ + """Generate source and destination directory and source files.""" os.makedirs(SRC_DIR, exist_ok=True) os.makedirs(DST_PARENT_DIR, exist_ok=True) for file in SRC_FILES: - open(file, 'w', encoding='utf-8').close() + open(file, "w", encoding="utf-8").close() # Skip test if tests are run from command line. - @unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called ' - 'from a Python script. Therefore, the script cannot be ' - 'copied.') + @unittest.skipIf( + not os.path.isfile(sys.argv[0]), + "Publish is not called " + "from a Python script. Therefore, the script cannot be " + "copied.", + ) def test_publish(self): """ Test publish and check if an exported picture file exists. The destination path is given with trailing slash. """ - publish(PlotIDTransfer(FIG, 'testID'), SRC_DIR, DST_PATH + '/', - PIC_NAME, data_storage='individual') - assert os.path.isfile(os.path.join(DST_PATH, 'testID', - PIC_NAME + '.png')) + publish( + PlotIDTransfer(FIG, "testID"), + SRC_DIR, + DST_PATH + "/", + PIC_NAME, + data_storage="individual", + ) + assert os.path.isfile(os.path.join(DST_PATH, "testID", PIC_NAME + ".png")) # Skip test if tests are run from command line. - @unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called ' - 'from a Python script. Therefore, the script cannot be ' - 'copied.') + @unittest.skipIf( + not os.path.isfile(sys.argv[0]), + "Publish is not called " + "from a Python script. Therefore, the script cannot be " + "copied.", + ) def test_publish_multiple_figs(self): """ Test publish with multiple figures and check if all exported picture @@ -67,8 +77,7 @@ class TestPublish(unittest.TestCase): """ publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME_LIST) for i, name in enumerate(PIC_NAME_LIST): - assert os.path.isfile(os.path.join(DST_PATH, IDS_AS_LIST[i], - name + '.png')) + assert os.path.isfile(os.path.join(DST_PATH, IDS_AS_LIST[i], name + ".png")) def test_figs_and_ids(self): """ @@ -76,14 +85,14 @@ class TestPublish(unittest.TestCase): PlotIDTransfer object. """ with self.assertRaises(RuntimeError): - publish('FIGS_AND_IDS', SRC_DIR, DST_PATH, PIC_NAME_LIST) + publish("FIGS_AND_IDS", SRC_DIR, DST_PATH, PIC_NAME_LIST) def test_wrong_ids(self): - """ Test if Error is raised if IDs are of wrong type. """ + """Test if Error is raised if IDs are of wrong type.""" with self.assertRaises(TypeError): publish(PlotIDTransfer(FIG, 3), SRC_DIR, DST_PATH, PIC_NAME) with self.assertRaises(TypeError): - publish(PlotIDTransfer(FIG, ['i', 4]), SRC_DIR, DST_PATH, PIC_NAME) + publish(PlotIDTransfer(FIG, ["i", 4]), SRC_DIR, DST_PATH, PIC_NAME) def test_publish_multiple_src_files(self): """ @@ -92,8 +101,13 @@ class TestPublish(unittest.TestCase): """ files_and_dir = list(SRC_FILES) files_and_dir.append(SRC_DIR) - publish(FIGS_AND_IDS, files_and_dir, DST_PATH, PIC_NAME_LIST, - data_storage='individual') + publish( + FIGS_AND_IDS, + files_and_dir, + DST_PATH, + PIC_NAME_LIST, + data_storage="individual", + ) for identifier in IDS_AS_LIST: for file in SRC_FILES: path = os.path.join(DST_PATH, identifier) @@ -101,12 +115,12 @@ class TestPublish(unittest.TestCase): assert os.path.isfile(os.path.join(path, file)) def test_src_directory(self): - """ Test if Error is raised when source directory does not exist.""" + """Test if Error is raised when source directory does not exist.""" with self.assertRaises(FileNotFoundError): - publish(FIGS_AND_IDS, 'not_existing_folder', DST_PATH, PIC_NAME) + publish(FIGS_AND_IDS, "not_existing_folder", DST_PATH, PIC_NAME) def test_src_directory_type(self): - """ Test if Error is raised when source directory is not a string.""" + """Test if Error is raised when source directory is not a string.""" with self.assertRaises(TypeError): publish(FIGS_AND_IDS, [SRC_DIR, 4], DST_PATH, PIC_NAME) with self.assertRaises(TypeError): @@ -118,8 +132,7 @@ class TestPublish(unittest.TestCase): destination dir does not exist. """ with self.assertRaises(FileNotFoundError): - publish(FIGS_AND_IDS, SRC_DIR, 'not_existing_folder/data', - PIC_NAME) + publish(FIGS_AND_IDS, SRC_DIR, "not_existing_folder/data", PIC_NAME) def test_script_exists(self): """ @@ -137,30 +150,46 @@ class TestPublish(unittest.TestCase): python = "python" with self.assertRaises(CalledProcessError): - run([python, "-c", - "import matplotlib.pyplot as plt\n" - "from plotid.publish import publish\n" - "from plotid.plotoptions import PlotIDTransfer\n" - "publish(PlotIDTransfer(plt.figure(), 'testID2')," - " 'test_src_folder', 'test_parent/test_dst_folder'," - " 'test_picture')"], - capture_output=True, check=True) - process = run([python, "-c", - "import matplotlib.pyplot as plt\n" - "from plotid.publish import publish\n" - "from plotid.plotoptions import PlotIDTransfer\n" - "publish(PlotIDTransfer(plt.figure(), 'testID2'), " - "'test_src_folder', 'test_parent/test_dst_folder', " - "'test_picture')"], - capture_output=True) - assert ("FileNotFoundError: Cannot copy original python script. " - "Running publish from a shell" - " is not possible.") in process.stderr.decode() + run( + [ + python, + "-c", + "import matplotlib.pyplot as plt\n" + "from plotid.publish import publish\n" + "from plotid.plotoptions import PlotIDTransfer\n" + "publish(PlotIDTransfer(plt.figure(), 'testID2')," + " 'test_src_folder', 'test_parent/test_dst_folder'," + " 'test_picture')", + ], + capture_output=True, + check=True, + ) + process = run( + [ + python, + "-c", + "import matplotlib.pyplot as plt\n" + "from plotid.publish import publish\n" + "from plotid.plotoptions import PlotIDTransfer\n" + "publish(PlotIDTransfer(plt.figure(), 'testID2'), " + "'test_src_folder', 'test_parent/test_dst_folder', " + "'test_picture')", + ], + capture_output=True, + ) + assert ( + "FileNotFoundError: Cannot copy original python script. " + "Running publish from a shell" + " is not possible." + ) in process.stderr.decode() # Skip test if tests are run from command line. - @unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called ' - 'from a Python script. Therefore, the script cannot be ' - 'copied.') + @unittest.skipIf( + not os.path.isfile(sys.argv[0]), + "Publish is not called " + "from a Python script. Therefore, the script cannot be " + "copied.", + ) def test_dst_already_exists_yes(self): """ Test if publish succeeds if the user wants to overwrite an existing @@ -169,14 +198,18 @@ class TestPublish(unittest.TestCase): os.mkdir(DST_PATH) os.mkdir(os.path.join(DST_PATH, IDS_AS_LIST[0])) # Mock user input as 'yes' - with patch('builtins.input', return_value='yes'): - publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME, - data_storage='individual') + with patch("builtins.input", return_value="yes"): + publish( + FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME, data_storage="individual" + ) # Skip test if tests are run from command line. - @unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called ' - 'from a Python script. Therefore, the script cannot be ' - 'copied.') + @unittest.skipIf( + not os.path.isfile(sys.argv[0]), + "Publish is not called " + "from a Python script. Therefore, the script cannot be " + "copied.", + ) def test_dst_already_exists_no(self): """ Test if publish exits with error if the user does not want to overwrite @@ -186,14 +219,17 @@ class TestPublish(unittest.TestCase): os.mkdir(os.path.join(DST_PATH, IDS_AS_LIST[0])) os.mkdir(os.path.join(DST_PATH, IDS_AS_LIST[1])) # Mock user input as 'no' - with patch('builtins.input', return_value='no'): + with patch("builtins.input", return_value="no"): with self.assertRaises(RuntimeError): publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME) # Skip test if tests are run from command line. - @unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called ' - 'from a Python script. Therefore, the script cannot be ' - 'copied.') + @unittest.skipIf( + not os.path.isfile(sys.argv[0]), + "Publish is not called " + "from a Python script. Therefore, the script cannot be " + "copied.", + ) def test_dst_already_exists_empty(self): """ Test if publish exits with error if the user does not want to overwrite @@ -203,15 +239,19 @@ class TestPublish(unittest.TestCase): os.mkdir(os.path.join(DST_PATH, IDS_AS_LIST[0])) os.mkdir(os.path.join(DST_PATH, IDS_AS_LIST[1])) # Mock user input as empty (no should be default). - with patch('builtins.input', return_value=''): + with patch("builtins.input", return_value=""): with self.assertRaises(RuntimeError): - publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME, - data_storage='individual') + publish( + FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME, data_storage="individual" + ) # Skip test if tests are run from command line. - @unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called ' - 'from a Python script. Therefore, the script cannot be ' - 'copied.') + @unittest.skipIf( + not os.path.isfile(sys.argv[0]), + "Publish is not called " + "from a Python script. Therefore, the script cannot be " + "copied.", + ) def test_dst_invisible_already_exists(self): """ Test if after a crash of publish the partially copied data is @@ -219,33 +259,37 @@ class TestPublish(unittest.TestCase): To mock this, the invisible directory already exists. """ os.mkdir(DST_PATH) - invisible_path1 = os.path.join(DST_PATH, '.' + IDS_AS_LIST[0]) + invisible_path1 = os.path.join(DST_PATH, "." + IDS_AS_LIST[0]) os.mkdir(invisible_path1) # Mock user input as 'yes' for deleting the partial copied data - with patch('builtins.input', side_effect=['yes', 'yes']): + with patch("builtins.input", side_effect=["yes", "yes"]): 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.tmp.png') - os.remove('test_picture2.tmp.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. """ + """Test if Error is raised if plot_name is not a string.""" with self.assertRaises(TypeError): - publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, 7.6, - data_storage='individual') + publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, 7.6, data_storage="individual") with self.assertRaises(TypeError): publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, ()) with self.assertRaises(TypeError): - publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, ['test', 3]) + publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, ["test", 3]) def test_data_storage(self): """ Test if Error is raised when unsupported storage method was chosen. """ with self.assertRaises(ValueError): - publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME, - data_storage='none_existing_method') + publish( + FIGS_AND_IDS, + SRC_DIR, + DST_PATH, + PIC_NAME, + data_storage="none_existing_method", + ) with self.assertRaises(TypeError): publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME, data_storage=3) with self.assertRaises(TypeError): @@ -264,15 +308,16 @@ class TestPublish(unittest.TestCase): " 640x480 with 0 Axes>], 'figure_ids': " "['MR05_0x63203c6f', 'MR05_0x63203c70'], " "'src_datapaths': 'test_src_folder'", - str(publish_obj)) + str(publish_obj), + ) def tearDown(self): - """ Delete all files created in setUp. """ + """Delete all files created in setUp.""" shutil.rmtree(SRC_DIR) shutil.rmtree(DST_PARENT_DIR) for file in SRC_FILES: os.remove(file) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_save_plot.py b/tests/test_save_plot.py index a9f9f059c549f781480a2d51d87d523e68099a32..33e54d83e4eef303cc92566fec18308a85e9910f 100644 --- a/tests/test_save_plot.py +++ b/tests/test_save_plot.py @@ -11,9 +11,9 @@ from PIL import Image from plotid.save_plot import save_plot FIGURE = plt.figure() -PLOT_NAME = 'PLOT_NAME' -IMG1 = 'image1.png' -IMG2 = 'image2.jpg' +PLOT_NAME = "PLOT_NAME" +IMG1 = "image1.png" +IMG2 = "image2.jpg" class TestSavePlot(unittest.TestCase): @@ -30,9 +30,9 @@ class TestSavePlot(unittest.TestCase): Test if save_plot succeeds with valid arguments using the matplotlib engine. """ - plot_paths = save_plot(FIGURE, [PLOT_NAME], extension='jpg') + plot_paths = save_plot(FIGURE, [PLOT_NAME], extension="jpg") self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '.tmp.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 + '.tmp.png') + os.remove(PLOT_NAME + ".tmp.png") def test_save_plot_image_jpg(self): """ @@ -51,10 +51,10 @@ class TestSavePlot(unittest.TestCase): """ img2 = Image.open(IMG2) imgs_as_list = [img2, img2] - plot_paths = save_plot(imgs_as_list, [PLOT_NAME], extension='jpg') + plot_paths = save_plot(imgs_as_list, [PLOT_NAME], extension="jpg") self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '1.tmp.jpg') - os.remove(PLOT_NAME + '2.tmp.jpg') + os.remove(PLOT_NAME + "1.tmp.jpg") + os.remove(PLOT_NAME + "2.tmp.jpg") def test_more_figs_than_names(self): """ @@ -64,18 +64,18 @@ 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}.tmp.png') - os.remove(PLOT_NAME + f'{i}.tmp.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. """ + """Test if Error is raised if more names than figures are given.""" with self.assertRaises(IndexError): save_plot([FIGURE, FIGURE], [PLOT_NAME, PLOT_NAME, PLOT_NAME]) def test_wrong_fig_type(self): - """ Test if Error is raised when not a figure object is given. """ + """Test if Error is raised when not a figure object is given.""" with self.assertRaises(TypeError): - save_plot('figure', 'PLOT_NAME', extension='jpg') + save_plot("figure", "PLOT_NAME", extension="jpg") def test_wrong_fig_in_list(self): """ @@ -83,13 +83,13 @@ class TestSavePlot(unittest.TestCase): valid figures. """ with self.assertRaises(TypeError): - save_plot([FIGURE, 'figure', FIGURE], 'PLOT_NAME', extension='jpg') - os.remove('PLOT_NAME1.tmp.jpg') + save_plot([FIGURE, "figure", FIGURE], "PLOT_NAME", extension="jpg") + os.remove("PLOT_NAME1.tmp.jpg") def tearDown(self): os.remove(IMG1) os.remove(IMG2) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_tagplot.py b/tests/test_tagplot.py index 0eb60acf3929ad122f205ed0414e76832171fede..3bcccc344b54fadef4234e0a91fbcbb4ab539eaf 100644 --- a/tests/test_tagplot.py +++ b/tests/test_tagplot.py @@ -13,13 +13,13 @@ from plotid.tagplot import tagplot FIG1 = plt.figure() FIG2 = plt.figure() FIGS_AS_LIST = [FIG1, FIG2] -IMG1 = 'image1.png' -IMG2 = 'image2.jpg' +IMG1 = "image1.png" +IMG2 = "image2.jpg" IMGS_AS_LIST = [IMG1, IMG2] PROJECT_ID = "MR01" PLOT_ENGINE = "matplotlib" -METHOD = 'time' +METHOD = "time" class TestTagplot(unittest.TestCase): @@ -36,30 +36,34 @@ class TestTagplot(unittest.TestCase): Test if tagplot runs successful. """ tagplot(FIGS_AS_LIST, PLOT_ENGINE, prefix=PROJECT_ID, id_method=METHOD) - tagplot(IMGS_AS_LIST, 'image', location='north') + tagplot(IMGS_AS_LIST, "image", location="north") def test_prefix(self): - """ Test if Error is raised if prefix is not a string. """ + """Test if Error is raised if prefix is not a string.""" with self.assertRaises(TypeError): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, location='southeast', - prefix=3, id_method=METHOD) + tagplot( + FIGS_AS_LIST, + PLOT_ENGINE, + location="southeast", + prefix=3, + id_method=METHOD, + ) 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, location='north') + tagplot(FIGS_AS_LIST, 1, location="north") with self.assertRaises(ValueError): - tagplot(FIGS_AS_LIST, 'xyz', location='south') + tagplot(FIGS_AS_LIST, "xyz", location="south") def test_idmethod(self): """ Test if Errors are raised if the id_method is not an string. """ with self.assertRaises(TypeError): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, id_method=(0, 1), - location='west') + tagplot(FIGS_AS_LIST, PLOT_ENGINE, id_method=(0, 1), location="west") with self.assertRaises(TypeError): tagplot(FIGS_AS_LIST, PLOT_ENGINE, id_method=1) with self.assertRaises(TypeError): @@ -72,12 +76,12 @@ class TestTagplot(unittest.TestCase): with self.assertRaises(TypeError): tagplot(FIGS_AS_LIST, PLOT_ENGINE, location=1) with self.assertWarns(Warning): - tagplot(FIGS_AS_LIST, PLOT_ENGINE, location='up') + tagplot(FIGS_AS_LIST, PLOT_ENGINE, location="up") def tearDown(self): os.remove(IMG1) os.remove(IMG2) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_tagplot_image.py b/tests/test_tagplot_image.py index 2c236c57bccd97748def00665fcf509d10a343b8..a14b4b9ea545812b04fce5900a8039423db302dd 100644 --- a/tests/test_tagplot_image.py +++ b/tests/test_tagplot_image.py @@ -13,14 +13,14 @@ from plotid.plotoptions import PlotOptions FIG1 = plt.figure() -IMG1 = 'image1.png' +IMG1 = "image1.png" FIG2 = plt.figure() -IMG2 = 'image2.jpg' +IMG2 = "image2.jpg" IMGS_AS_LIST = [IMG1, IMG2] # Constants for tests PROJECT_ID = "MR01" -METHOD = 'time' +METHOD = "time" ROTATION = 90 POSITION = (0.975, 0.35) @@ -39,14 +39,18 @@ class TestTagplotImage(unittest.TestCase): Test of returned objects. Check if they are png and jpg files, respectively. """ - options = PlotOptions(IMGS_AS_LIST, ROTATION, POSITION, - prefix=PROJECT_ID, id_method=METHOD, qrcode=True) + options = PlotOptions( + IMGS_AS_LIST, + ROTATION, + POSITION, + prefix=PROJECT_ID, + id_method=METHOD, + qrcode=True, + ) options.validate_input() figs_and_ids = tagplot_image(options) - self.assertIsInstance(figs_and_ids.figs[0], - PngImagePlugin.PngImageFile) - self.assertIsInstance(figs_and_ids.figs[1], - JpegImagePlugin.JpegImageFile) + self.assertIsInstance(figs_and_ids.figs[0], PngImagePlugin.PngImageFile) + self.assertIsInstance(figs_and_ids.figs[1], JpegImagePlugin.JpegImageFile) def test_single_image(self): """ @@ -56,20 +60,20 @@ class TestTagplotImage(unittest.TestCase): options = PlotOptions(IMG1, ROTATION, POSITION) options.validate_input() figs_and_ids = tagplot_image(options) - self.assertIsInstance(figs_and_ids.figs[0], - PngImagePlugin.PngImageFile) + self.assertIsInstance(figs_and_ids.figs[0], PngImagePlugin.PngImageFile) def test_image_not_str(self): - """ Test if Error is raised if wrong type of image is given. """ - options = PlotOptions(3, ROTATION, POSITION, - prefix=PROJECT_ID, id_method=METHOD) + """Test if Error is raised if wrong type of image is given.""" + options = PlotOptions( + 3, ROTATION, POSITION, prefix=PROJECT_ID, id_method=METHOD + ) options.validate_input() with self.assertRaises(TypeError): tagplot_image(options) def test_image_not_file(self): - """ Test if Error is raised if the image file does not exist. """ - options = PlotOptions('xyz', ROTATION, POSITION) + """Test if Error is raised if the image file does not exist.""" + options = PlotOptions("xyz", ROTATION, POSITION) options.validate_input() with self.assertRaises(TypeError): tagplot_image(options) @@ -79,12 +83,12 @@ class TestTagplotImage(unittest.TestCase): Test if Error is raised if not an instance of PlotOptions is passed. """ with self.assertRaises(TypeError): - tagplot_image('wrong_object') + tagplot_image("wrong_object") def tearDown(self): os.remove(IMG1) os.remove(IMG2) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_tagplot_matplotlib.py b/tests/test_tagplot_matplotlib.py index 87cc22bfbce4422105aabdb9f1286f5e55e608a5..3bda534dfec8e16646b5ee06dfb30586a75c77ac 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 = 'time' +METHOD = "time" ROTATION = 90 POSITION = (0.975, 0.35) @@ -28,9 +28,15 @@ class TestTagplotMatplotlib(unittest.TestCase): """ def test_mplfigures(self): - """ Test of returned objects. Check if they are matplotlib figures. """ - options = PlotOptions(FIGS_AS_LIST, ROTATION, POSITION, - prefix=PROJECT_ID, id_method=METHOD, qrcode=True) + """Test of returned objects. Check if they are matplotlib figures.""" + options = PlotOptions( + FIGS_AS_LIST, + ROTATION, + POSITION, + prefix=PROJECT_ID, + id_method=METHOD, + qrcode=True, + ) options.validate_input() figs_and_ids = tagplot_matplotlib(options) self.assertIsInstance(figs_and_ids.figs[0], Figure) @@ -47,7 +53,7 @@ class TestTagplotMatplotlib(unittest.TestCase): self.assertIsInstance(figs_and_ids.figs[0], Figure) def test_mplerror(self): - """ Test if Error is raised if wrong type of figures is given. """ + """Test if Error is raised if wrong type of figures is given.""" options = PlotOptions(3, ROTATION, POSITION) options.validate_input() with self.assertRaises(TypeError): @@ -58,8 +64,8 @@ class TestTagplotMatplotlib(unittest.TestCase): Test if Error is raised if not an instance of PlotOptions is passed. """ with self.assertRaises(TypeError): - tagplot_matplotlib('wrong_object') + tagplot_matplotlib("wrong_object") -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()