Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julkaar9 committed Dec 23, 2022
1 parent 8842965 commit 8f9336c
Show file tree
Hide file tree
Showing 20 changed files with 403 additions and 91 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests

on:
push:
branches:
- [ main ]
pull_request:
branches:
- [ main ]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
legacy/*

tests/data/ccodes.csv
tests/data/cn_dict.csv
tests/data/data2.csv
tests/data/mapdata.csv
tests/test.py
tests/example4.py
examples/data/ccodes.csv
examples/data/cn_dict.csv
examples/data/data2.csv
examples/data/mapdata.csv
examples/test.py
examples/example4.py
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Pynimate

[![PyPI](https://img.shields.io/pypi/v/pynimate?color=orange)](https://pypi.org/project/pynimate/)
[![License](https://img.shields.io/pypi/l/pynimate?color=green)](https://github.com/julkaar9/pynimate/blob/main/LICENSE)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Python package for statistical data animations.
![](https://github.com/julkaar9/pynimate/blob/gh-pages/assets/example3.gif)
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/datafier.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
merge_init_into_class: true
members:
- add_var
- intepolateEven
- interpolate_even
- get_prepared_data
- get_top_cols
- get_bar_colors
Expand Down
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions examples/data/map.csv

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions tests/example2.py → examples/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
}
).set_index("time")
cnv = nim.Canvas(figsize=(12.8, 7.2))
bar = nim.Barplot(df, "%Y-%m-%d", "2d")
bar = nim.Barplot(df, "%Y-%m-%d", "2d", 0.1)
bar.set_time(callback=lambda i, datafier: datafier.data.index[i].year)
cnv.add_plot(bar)
cnv.animate()
# cnv.save(
# "exm2",
# 24,
# "gif",
# )
cnv.save(
"exm2",
24,
"gif",
)
plt.show()
2 changes: 1 addition & 1 deletion tests/example3.py → examples/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def post_update(ax, i, datafier, bar_attr):
cnv.add_plot(bar)
cnv.animate()
# plt.show()
cnv.save("example3", 24, "gif")
cnv.save("example3", 24, "mp4")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
requires-python = ">=3.9"

[project.optional-dependencies]
dev = ["black", "isort", "pip-tools"]
dev = ["black", "isort", "pip-tools", "pytest"]

[project.urls]
Homepage = "https://github.com/julkaar9/pynimate"
Expand Down
56 changes: 3 additions & 53 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,20 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --extra=dev pyproject.toml
# pip-compile pyproject.toml
#
black==22.10.0
# via pynimate (pyproject.toml)
build==0.9.0
# via pip-tools
bumpver==2022.1119
# via pynimate (pyproject.toml)
click==8.1.3
# via
# black
# bumpver
# pip-tools
colorama==0.4.6
# via
# build
# bumpver
# click
contourpy==1.0.6
# via matplotlib
cycler==0.11.0
# via matplotlib
fonttools==4.38.0
# via matplotlib
isort==5.10.1
# via pynimate (pyproject.toml)
kiwisolver==1.4.4
# via matplotlib
lexid==2021.1006
# via bumpver
matplotlib==3.6.2
# via
# pynimate (pyproject.toml)
# seaborn
mypy-extensions==0.4.3
# via black
numpy==1.23.4
# via
# contourpy
Expand All @@ -46,25 +24,13 @@ numpy==1.23.4
# pynimate (pyproject.toml)
# seaborn
packaging==21.3
# via
# build
# matplotlib
# via matplotlib
pandas==1.5.1
# via
# pynimate (pyproject.toml)
# seaborn
pathlib2==2.3.7.post1
# via bumpver
pathspec==0.10.2
# via black
pep517==0.13.0
# via build
pillow==9.3.0
# via matplotlib
pip-tools==6.10.0
# via pynimate (pyproject.toml)
platformdirs==2.5.4
# via black
pyparsing==3.0.9
# via
# matplotlib
Expand All @@ -78,20 +44,4 @@ pytz==2022.6
seaborn==0.12.1
# via pynimate (pyproject.toml)
six==1.16.0
# via
# pathlib2
# python-dateutil
toml==0.10.2
# via bumpver
tomli==2.0.1
# via
# black
# build
typing-extensions==4.4.0
# via black
wheel==0.38.4
# via pip-tools

# The following packages are considered to be unsafe in a requirements file:
# pip
# setuptools
# via python-dateutil
2 changes: 1 addition & 1 deletion src/pynimate/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def set_text(
>>> lambda i, datafier: datafier.data.index[i]
```
"""
assert text or callback, "Both text and callback cannot be None"
assert text or callback, "Both text and callback cannot be None"
self.text_collection[key] = (
callback,
{
Expand Down
24 changes: 2 additions & 22 deletions src/pynimate/datafier.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,6 @@ def add_var(self, row_var: pd.DataFrame = None, col_var: pd.DataFrame = None):
self.row_var = self.interpolateEven(row_var, self.ip_freq) if row_var else None
self.col_var = col_var

# def prepared_data(
# self,
# ) -> tuple[pd.DataFrame, pd.DataFrame, dict[str, str]]:
# """_summary_

# Returns
# -------
# tuple[pd.DataFrame, pd.DataFrame, dict[str, str]]
# Tuple containing the following data
# ```
# pd.DataFrame: The actual data interpolated
# pd.DataFrame: Dataframe containing bar ranks
# dict[str, str]: Dict containing column to color mapping
# ```
# """
# self.data.columns = list(self.data.columns)
# self.df_ranks.columns = list(self.df_ranks.columns)

# return self.data, self.df_ranks, self.bar_colors

def interpolate(
self, data: pd.DataFrame, freq: str, method: str = "linear"
) -> pd.DataFrame:
Expand All @@ -138,7 +118,7 @@ def interpolate(
)
return data

def interpolateEven(
def interpolate_even(
self, data: pd.DataFrame, freq: str, method: str = "linear"
) -> pd.DataFrame:
"""Interpolates the given dataframe according to the frequency
Expand Down Expand Up @@ -209,7 +189,7 @@ def get_prepared_data(
df_ranks = self.n_bars + 1 - df_ranks
data.replace(np.nan, 0, inplace=True)
df_ranks.replace(np.nan, -1, inplace=True)
data = self.interpolateEven(data, freq=self.ip_freq)
data = self.interpolate_even(data, freq=self.ip_freq)
df_ranks = df_ranks.reindex(data.index)
# calculate the no of nans in each interval
# see https://stackoverflow.com/questions/69951782/pandas-interpolate-with-condition
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sys
sys.path.append('.')
37 changes: 37 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os

dir_path = os.path.dirname(os.path.realpath(__file__))

import pytest
import pandas as pd


@pytest.fixture
def sample_bar_data1() -> pd.DataFrame:
return pd.DataFrame(
{
"time": ["1960-01-01", "1961-01-01", "1962-01-01"],
"Afghanistan": [1, 2, 3],
"Angola": [2, 3, 4],
"Albania": [1, 2, 5],
"USA": [5, 3, 4],
"Argentina": [1, 4, 5],
}
).set_index("time")


@pytest.fixture
def sample_bar_data2() -> pd.DataFrame:
return pd.DataFrame(
{
"time": ["2012", "2013", "2014"],
"col1": [1, 2, 3],
"col2": [3, 2, 1],
}
).set_index("time")


@pytest.fixture
def map_data() -> pd.DataFrame:
map_data = pd.read_csv(dir_path + "/data/map.csv").set_index("time")
return map_data
84 changes: 84 additions & 0 deletions tests/test_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from pynimate.bar import Barplot
import pytest


def test_barplot_set_bar_color_list(sample_bar_data1):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar_colors_list = [
"#2a9d8f",
"#e9c46a",
"#e76f51",
"#a7c957",
"#e5989b",
]
bar_colors = {
"Afghanistan": "#2a9d8f",
"Angola": "#e9c46a",
"Albania": "#e76f51",
"USA": "#a7c957",
"Argentina": "#e5989b",
}
bar.set_bar_color(bar_colors_list)
assert bar.datafier.bar_colors == bar_colors


def test_barplot_set_bar_color_dict(sample_bar_data1):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar_colors = {
"Afghanistan": "#2a9d8f",
"Angola": "#e9c46a",
"Albania": "#e76f51",
"USA": "#a7c957",
"Argentina": "#e5989b",
}
bar.set_bar_color(bar_colors)
assert bar.datafier.bar_colors == bar_colors


def test_barplot_set_bar_color_error_length(sample_bar_data1):
with pytest.raises(AssertionError):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar_colors = [
"#2a9d8f",
"#e9c46a",
"#e76f51",
"#a7c957",
]
bar.set_bar_color(bar_colors)
assert bar.datafier.bar_colors == bar_colors


def test_barplot_set_bar_color_error_col_mismatch(sample_bar_data1):
with pytest.raises(AssertionError):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar_colors = {
"India": "#2a9d8f",
"Angola": "#e9c46a",
"Albania": "#e76f51",
"USA": "#a7c957",
"Argentina": "#e5989b",
}
bar.set_bar_color(bar_colors)
assert bar.datafier.bar_colors == bar_colors


def test_barplot_set_test_error_empty_text(sample_bar_data1):
with pytest.raises(AssertionError):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar.set_text("text1")


def test_barplot_set_test_error_text_priority(sample_bar_data1):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar.set_text("text1", text="Test", callback=lambda *args: "Test")
assert "s" not in bar.text_collection["text1"][1]


def test_barplot_remove_text(sample_bar_data1):
bar = Barplot(sample_bar_data1, "%Y-%m-%d", "3MS")
bar.set_text("text1", text="Test1")
bar.set_text("text2", text="Test2")
bar.set_text("text3", text="Test3")

bar.remove_text(["text1", "text2"])
assert list(bar.text_collection.keys()) == ["time", "text3"]
Loading

0 comments on commit 8f9336c

Please sign in to comment.