diff --git a/beamtime/README.md b/README.md similarity index 100% rename from beamtime/README.md rename to README.md diff --git a/beamtime/plotting.py b/beamtime/plotting.py index 500f532..946a4e4 100644 --- a/beamtime/plotting.py +++ b/beamtime/plotting.py @@ -1,13 +1,11 @@ import beamtime.auxillary as aux import matplotlib.pyplot as plt -from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,AutoMinorLocator) -from mpl_toolkits.axes_grid.inset_locator import (inset_axes, InsetPosition, mark_inset) +from matplotlib.ticker import (MultipleLocator) import importlib import matplotlib.patches as mpatches from matplotlib.lines import Line2D import matplotlib.lines as mlines -from cycler import cycler import itertools diff --git a/beamtime/test/__init__.py b/beamtime/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/beamtime/test/pytest.ini b/beamtime/test/pytest.ini new file mode 100644 index 0000000..c317621 --- /dev/null +++ b/beamtime/test/pytest.ini @@ -0,0 +1,9 @@ +# pytest.ini + +[pytest] +minversion = 6.0 +testpaths = + . + +filterwarnings = + ignore::DeprecationWarning diff --git a/beamtime/test/test_auxillary.py b/beamtime/test/test_auxillary.py new file mode 100644 index 0000000..668b792 --- /dev/null +++ b/beamtime/test/test_auxillary.py @@ -0,0 +1,78 @@ +import beamtime.auxillary as aux +import os + +def test_swap_values(): + + + dict = {'test1': 1, 'test2': 2} + key1 = 'test1' + key2 = 'test2' + + oldval1 = dict[key1] + oldval2 = dict[key2] + + new_dict = aux.swap_values(dict=dict, key1=key1, key2=key2) + + assert (dict[key1] == oldval2) and (dict[key2] == oldval1) + + +def test_ceil() -> None: + + assert aux.ceil(1.05, 0.5) == 1.5 + assert aux.ceil(1.05, 1) == 2.0 + assert aux.ceil(1.1, 0.2) == 1.2 + + +def test_floor() -> None: + + assert aux.floor(2.02, 1) == 2.0 + assert aux.floor(2.02, 0.01) == 2.02 + assert aux.floor(2.013, 0.01) == 2.01 + + + +def test_options() -> None: + + + options = {} + required_options = ['test1', 'test2', 'test3', 'test4'] + default_options = { + 'test1': 1, + 'test2': 2, + 'test3': 3, + 'test4': 4, + 'test5': 5, + } + + + options = aux.update_options(options=options, required_options=required_options, default_options=default_options) + + assert options['test1'] == default_options['test1'] + assert len(options.items()) == len(required_options) + assert 'test5' not in options.keys() + + +def test_save_options() -> None: + + options = {'test1': 1, 'test2': 2} + path = 'tmp.dat' + + aux.save_options(options, path) + + assert os.path.isfile(path) + + os.remove(path) + + +def test_load_options() -> None: + + options = {'test1': 1, 'test2': 2} + path = 'tmp.dat' + + aux.save_options(options, path) + + loaded_options = aux.load_options(path) + + assert (loaded_options['test1'] == 1) and (loaded_options['test2'] == 2) + + os.remove(path) \ No newline at end of file diff --git a/beamtime/test/test_plotting.py b/beamtime/test/test_plotting.py new file mode 100644 index 0000000..3b9ccda --- /dev/null +++ b/beamtime/test/test_plotting.py @@ -0,0 +1,132 @@ +import beamtime.plotting as btp +from cycler import cycler +import itertools +import numpy as np + +import matplotlib.pyplot as plt + + +def test_generate_colours() -> None: + + assert type(btp.generate_colours('black', kind='single')) == itertools.cycle + + palettes = [('qualitative', 'Dark2_8')] + colour_cycle = btp.generate_colours(palettes) + + assert type(colour_cycle) == itertools.cycle + + + # Test that it actually loaded 8 colours when given a set of 8 colours to + + same_colour = None + for i in range(10): + colour = next(colour_cycle) + if i == 0: + first_colour = colour + + if colour == first_colour: + repeat_colour_index = i + + + assert repeat_colour_index == 8 + + + + +def test_update_rc_params() -> None: + + rc_params = { + 'lines.linewidth': 100 + } + + prev_params = plt.rcParams['lines.linewidth'] + + # Update run commands if any is passed (will pass an empty dictionary if not passed) + btp.update_rc_params(rc_params) + + new_params = plt.rcParams['lines.linewidth'] + + assert new_params == 100 + assert prev_params != new_params + + + # Reset run commands + plt.rcdefaults() + + + +def test_scale_figure() -> None: + + width, height = 1, 1 + + format_params = { + 'upscaling_factor': 2, + 'compress_width': 1, + 'compress_height': 1 + } + + width1, height1 = btp.scale_figure(format_params=format_params, width=width, height=height) + + assert width1 == 2 and height1 == 2 + + format_params = { + 'upscaling_factor': 1, + 'compress_width': 0.5, + 'compress_height': 1 + } + + width2, height2 = btp.scale_figure(format_params=format_params, width=width, height=height) + + assert width2 == 0.5 and height2 == 1 + + format_params = { + 'upscaling_factor': 2, + 'compress_width': 0.5, + 'compress_height': 0.2 + } + + width2, height2 = btp.scale_figure(format_params=format_params, width=width, height=height) + + assert width2 == 1 and height2 == 0.4 + + +def test_determine_width() -> None: + + conversion_cm_inch = 0.3937008 # cm to inch + + format_params = { + 'column_type': 'single', + 'single_column_width': 5, + 'double_column_width': 10, + 'width_ratio': '1:1' + } + + assert np.round(btp.determine_width(format_params),6) == np.round(5*conversion_cm_inch,6) + + format_params['column_type'] = 'double' + + assert np.round(btp.determine_width(format_params), 6) == np.round(10*conversion_cm_inch, 6) + + + format_params['column_type'] = 'single' + format_params['width_ratio'] = '1:2' + + assert np.round(btp.determine_width(format_params), 6) == np.round(2.5*conversion_cm_inch, 6) + +def test_determine_height() -> None: + + + width = 1 + + format_params = { + 'aspect_ratio': '1:1' + } + + assert btp.determine_height(format_params=format_params, width=width) == 1 + + format_params['aspect_ratio'] = '3:1' + + assert (btp.determine_height(format_params=format_params, width=width) - 0.333333333333333) < 10e-7 + + assert True + diff --git a/beamtime/test/xrd/test_io.py b/beamtime/test/xrd/test_io.py new file mode 100644 index 0000000..e69de29 diff --git a/beamtime/test/xrd/test_plot.py b/beamtime/test/xrd/test_plot.py new file mode 100644 index 0000000..e69de29 diff --git a/beamtime/feature_list.txt b/feature_list.txt similarity index 100% rename from beamtime/feature_list.txt rename to feature_list.txt diff --git a/beamtime/reqirements2.txt b/reqirements2.txt similarity index 100% rename from beamtime/reqirements2.txt rename to reqirements2.txt diff --git a/beamtime/requirements.txt b/requirements.txt similarity index 100% rename from beamtime/requirements.txt rename to requirements.txt