Skip to content

Commit

Permalink
CLI: Click tests for CLI commands (#132)
Browse files Browse the repository at this point in the history
* initial commit, cite test is working

* black and flake8 fixes

* to keep changes, info command test wip

* to  keep changes

* cli info command test implemented

* handle files test implemented

* black fixes

* test denoise saveload on cli is wip

* to keep changes

* to keep changes

* tests passed

Co-authored-by: acs-ws <asolak@ku.edu.tr>
  • Loading branch information
AhmetCanSolak and acs-ws committed May 2, 2022
1 parent 06b8826 commit 7c2ae4c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
101 changes: 80 additions & 21 deletions aydin/cli/test/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,80 @@
# from click.testing import CliRunner
#
# from aydin.cli.cli import update
#
#
# def test_update():
# runner = CliRunner()
# result = runner.invoke(update)
# assert result.exit_code == 0


# def test_noise2self():
# runner = CliRunner()
# result = runner.invoke(noise2self, [])
# assert result.exit_code == 1
#
#
# def test_info():
# runner = CliRunner()
# result = runner.invoke(info, [])
# assert result.exit_code == 1
from click.testing import CliRunner

from aydin.cli.cli import cli, handle_files
from aydin.io.datasets import examples_single
from aydin.util.log.log import Log


def test_info():
Log.override_test_exclusion = True
Log.force_click_echo = True

image_path = examples_single.generic_lizard.get_path()

runner = CliRunner()
result = runner.invoke(cli, ['info', image_path])

assert result.exit_code == 0
assert "Reading" in result.output
assert "Metadata" in result.output
assert "batch" in result.output


def test_cite():
runner = CliRunner()
result = runner.invoke(cli, ['cite'])

assert result.exit_code == 0
assert "10.5281/zenodo.5654826" in result.output


def test_handle_files():
file_list = [
examples_single.generic_lizard.get_path(),
examples_single.noisy_fountain.get_path(),
]
filepaths, image_arrays, metadatas = handle_files(file_list, slicing="")

assert filepaths == file_list

assert image_arrays[0].shape == examples_single.generic_lizard.get_array().shape
assert image_arrays[0].dtype == examples_single.generic_lizard.get_array().dtype
assert image_arrays[1].shape == examples_single.noisy_fountain.get_array().shape
assert image_arrays[1].dtype == examples_single.noisy_fountain.get_array().dtype

assert metadatas[0].shape == examples_single.generic_lizard.get_array().shape
assert metadatas[0].dtype == examples_single.generic_lizard.get_array().dtype
assert metadatas[1].shape == examples_single.noisy_fountain.get_array().shape
assert metadatas[1].dtype == examples_single.noisy_fountain.get_array().dtype


def test_denoise():
image_path = examples_single.noisy_fountain.get_path()

# Denoise
runner = CliRunner()
result = runner.invoke(cli, ['denoise', image_path])
assert result.exit_code == 0

# TODO: turn this into a saveload testcase
# Denoise with the pre-trained model
# result = runner.invoke(cli, ['denoise', '--model-path=', '--use-model', image_path])
# assert result.exit_code == 0

# denoised = denoised.clip(0, 1)
#
# psnr_noisy = psnr(noisy, image)
# ssim_noisy = ssim(noisy, image)
# print("noisy", psnr_noisy, ssim_noisy)
#
# psnr_denoised = psnr(denoised, image)
# ssim_denoised = ssim(denoised, image)
# print("denoised", psnr_denoised, ssim_denoised)
#
# assert psnr_denoised > psnr_noisy and ssim_denoised > ssim_noisy
# assert psnr_denoised > psnr_noisy and ssim_denoised > ssim_noisy
#
# # if the line below fails, then the parameters of the image the lgbm regressohave been broken.
# # do not change the number below, but instead, fix the problem -- most likely a parameter.
#
# assert psnr_denoised > min_psnr and ssim_denoised > min_ssim
8 changes: 7 additions & 1 deletion aydin/util/log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
from contextlib import contextmanager

import click


class Log:
"""
Expand All @@ -19,6 +21,7 @@ class Log:
max_depth = math.inf
log_elapsed_time = True
override_test_exclusion = False
force_click_echo = False

# Define special characters:
__vl__ = '│' # 'Vertical Line'
Expand All @@ -44,7 +47,10 @@ def __init__(self):
@staticmethod
def native_print(*args, sep=' ', end='\n', file=sys.__stdout__):
if Log.enable_output:
print(*args, sep=sep, end=end, file=file)
if Log.force_click_echo:
click.echo(*args)
else:
print(*args, sep=sep, end=end, file=file)

if Log.guiEnabled and Log.gui_callback is not None:
result = ""
Expand Down

0 comments on commit 7c2ae4c

Please sign in to comment.