Skip to content

Commit 7784e5d

Browse files
author
Gsaes
committed
Readme+test
1 parent 155c24a commit 7784e5d

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
.. |Commits| image:: https://img.shields.io/github/commits-since/Quantmetry/qolmat/latest/main
2424
.. _Commits: https://github.com/Quantmetry/qolmat/commits/main
2525

26-
.. image:: https://github.com/Quantmetry/qolmat/tree/main/docs/images/logo.png
26+
.. image:: https://raw.githubusercontent.com/Quantmetry/qolmat/main/docs/images/logo.png
2727
:align: center
2828

2929
Qolmat - The Tool for Data Imputation
@@ -95,7 +95,7 @@ For this demonstration, let us create artificial holes in our dataset.
9595
plt.savefig('readme1.png')
9696
plt.show()
9797
98-
.. image:: https://github.com/Quantmetry/qolmat/tree/main/docs/images/readme1.png
98+
.. image:: https://raw.githubusercontent.com/Quantmetry/qolmat/main/docs/images/readme1.png
9999
:align: center
100100

101101
To impute missing data, there are several methods that can be imported with ``from qolmat.imputations import imputers``.
@@ -191,7 +191,7 @@ We can observe the benchmark results.
191191
plt.savefig('readme3.png')
192192
plt.show()
193193
194-
.. image:: https://github.com/Quantmetry/qolmat/tree/main/docs/images/readme2.png
194+
.. image:: https://raw.githubusercontent.com/Quantmetry/qolmat/main/docs/images/readme2.png
195195
:align: center
196196

197197
Finally, we keep the best ``TSMLE`` imputor we represent.
@@ -206,7 +206,7 @@ Finally, we keep the best ``TSMLE`` imputor we represent.
206206
plt.plot(df_with_nan['y'],'.b')
207207
plt.show()
208208
209-
.. image:: https://github.com/Quantmetry/qolmat/tree/main/docs/images/readme3.png
209+
.. image:: https://raw.githubusercontent.com/Quantmetry/qolmat/main/docs/images/readme3.png
210210
:align: center
211211

212212

qolmat/utils/utils.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ def display_bar_table(data: pd.DataFrame, ylabel: Optional[str] = "", path: Opti
3737
plt.show()
3838

3939

40-
def progress_bar(iteration, total, prefix="", suffix="", decimals=1, length=100, fill="█"):
40+
def progress_bar(
41+
iteration: int,
42+
total: int,
43+
prefix: str = "",
44+
suffix: str = "",
45+
decimals: int = 1,
46+
length: int = 100,
47+
fill: str = "█",
48+
):
4149
"""Call in a loop to create terminal progress bar
4250
4351
Parameters
@@ -46,27 +54,40 @@ def progress_bar(iteration, total, prefix="", suffix="", decimals=1, length=100,
4654
current iteration
4755
total : int
4856
total iterations
49-
prefix : str, optional
57+
prefix : str
5058
prefix string, by default ""
51-
suffix : str, optional
59+
suffix : str
5260
suffix string, by default ""
53-
decimals : int, optional
61+
decimals : int
5462
positive number of decimals in percent complete, by default 1
55-
length : int, optional
63+
length : int
5664
character length of bar, by default 100
57-
fill : str, optional
65+
fill : str
5866
bar fill character, by default "█"
5967
"""
6068
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
6169
filled_length = int(length * iteration // total)
6270
bar = fill * filled_length + "-" * (length - filled_length)
6371
print(f"\r{prefix} |{bar}| {percent}% {suffix}", end="\r")
64-
# Print New Line on Complete
6572
if iteration == total:
6673
print()
6774

6875

6976
def acf(values: pd.Series, lag_max: int = 30) -> pd.Series:
77+
"""Correlation series of dataseries
78+
79+
Parameters
80+
----------
81+
values : pd.Series
82+
dataseries
83+
lag_max : int, optional
84+
the maximum lag, by default 30
85+
86+
Returns
87+
-------
88+
pd.Series
89+
correlation series of value
90+
"""
7091
acf = pd.Series(0, index=range(lag_max))
7192
for lag in range(lag_max):
7293
acf[lag] = values.corr(values.shift(lag))

tests/utils/test_exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from qolmat.utils import exceptions
3+
4+
5+
def test_utils_exception_init():
6+
try:
7+
raise exceptions.KerasExtraNotInstalled()
8+
except exceptions.KerasExtraNotInstalled as e:
9+
assert (
10+
str(e)
11+
== """Please install keras xx.xx.xx
12+
pip install qolmat[keras]"""
13+
)

tests/utils/test_utils.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
import matplotlib as mpl
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
import pandas as pd
6+
import pytest
7+
from qolmat.utils import utils
8+
from pytest_mock.plugin import MockerFixture
9+
from io import StringIO
10+
11+
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
12+
13+
14+
@pytest.mark.parametrize("df", [df])
15+
def test_utils_utils_display_bar_table(df: pd.DataFrame, mocker: MockerFixture) -> None:
16+
mock_save = mocker.patch("matplotlib.pyplot.savefig")
17+
mocker.patch("matplotlib.pyplot.show")
18+
utils.display_bar_table(data=df, ylabel="Counts", path="output/barplot")
19+
y_label_text = plt.gca().get_ylabel()
20+
assert mock_save.call_count == 1
21+
assert plt.gcf() is not None
22+
assert plt.gca() is not None
23+
assert y_label_text == "Counts"
24+
25+
26+
@pytest.mark.parametrize("iteration, total", [(1, 1)])
27+
def test_utils_utils_display_progress_bar(iteration: int, total: int, capsys) -> None:
28+
captured_output = StringIO()
29+
sys.stdout = captured_output
30+
utils.progress_bar(
31+
iteration, total, prefix="Progress", suffix="Complete", decimals=1, length=2, fill="█"
32+
)
33+
captured_output.seek(0)
34+
output = captured_output.read().strip()
35+
sys.stdout = sys.__stdout__
36+
37+
output_expected = "Progress |██| 100.0% Complete"
38+
assert output == output_expected
39+
40+
41+
@pytest.mark.parametrize("values, lag_max", [(pd.Series([1, 2, 3, 4, 5]), 3)])
42+
def test_utils_utils_acf(values, lag_max):
43+
result = utils.acf(values, lag_max)
44+
result_expected = pd.Series([1.0, 1.0, 1.0])
45+
pd.testing.assert_series_equal(result, result_expected, atol=0.001)

0 commit comments

Comments
 (0)