Skip to content

Commit

Permalink
Upgrade pre-commit configuration and autofix (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas authored Jul 29, 2022
1 parent 1124e0c commit 2cd3efa
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 84 deletions.
86 changes: 52 additions & 34 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,50 +1,68 @@
ci:
skip: [pylint]

repos:
- repo: https://github.com/myint/autoflake
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/myint/autoflake
rev: v1.4
hooks:
- id: autoflake
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- --expand-star-imports
- --remove-duplicate-keys
- --remove-unused-variables
- repo: https://github.com/ambv/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: check-merge-conflict
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-json
- id: check-xml
- id: check-yaml
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
- repo: https://github.com/PyCQA/isort
rev: 5.7.0
hooks:
- id: isort
- repo: local
hooks:
- id: pylint
- --in-place
- --remove-all-unused-imports
- --expand-star-imports
- --remove-duplicate-keys
- --remove-unused-variables
- repo: https://github.com/asottile/pyupgrade
rev: v2.34.0
hooks:
- id: pyupgrade
exclude: tests/testdata
args: [--py36-plus]
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
exclude: tests/testdata
- repo: https://github.com/Pierre-Sassoulas/black-disable-checker/
rev: v1.1.0
hooks:
- id: black-disable-checker
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear, flake8-typing-imports==1.12.0]
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.800
args: ["-rn", "-sn"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.961
hooks:
- id: mypy
- id: mypy
name: mypy
entry: mypy
language: python
"types": [python]
args: ["--ignore-missing-imports", "--scripts-are-modules"]
require_serial: true
additional_dependencies: []
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
args: [--prose-wrap=always, --print-width=88]
38 changes: 23 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ plt.savefig('customers-goods.png', bbox_inches='tight') # to save
![Customer goods](pysankey/customers-goods.png)

Similar to seaborn, you can pass a matplotlib `Axes` to `sankey` function:

```python
import pandas as pd
from pysankey import sankey
Expand Down Expand Up @@ -169,20 +170,27 @@ plt.show()

## Important informations

Use of `figureName`, `closePlot`, `figSize` in `sankey()` is deprecated and will be remove in a future version.
This is done so matplotlib is used more transparently as this [issue](https://github.com/anazalea/pySankey/issues/26#issue-429312025) on the original github repo suggested.
Use of `figureName`, `closePlot`, `figSize` in `sankey()` is deprecated and will be
remove in a future version. This is done so matplotlib is used more transparently as
this [issue](https://github.com/anazalea/pySankey/issues/26#issue-429312025) on the
original github repo suggested.

Now, `sankey` does less of the customization and let the user do it to their liking by returning a matplotlib `Axes` object, which mean the user also has access to the `Figure` to customise.
Then they can choose what to do with it - showing it, saving it with much more flexibility.
Now, `sankey` does less of the customization and let the user do it to their liking by
returning a matplotlib `Axes` object, which mean the user also has access to the
`Figure` to customise. Then they can choose what to do with it - showing it, saving it
with much more flexibility.

### Recommended changes to your code
- To save a figure, one can simply do:
```python
plt.savefig("<figureName>.png", bbox_inches="tight", dpi=150)
```

- The `closePlot` is not needed anymore because without `plt.show()` after `sankey()`, no plot is displayed.
You can still do `plt.close()` to be sure to not display this plot if you display other plots afterwards.
- To save a figure, one can simply do:

```python
plt.savefig("<figureName>.png", bbox_inches="tight", dpi=150)
```

- The `closePlot` is not needed anymore because without `plt.show()` after `sankey()`,
no plot is displayed. You can still do `plt.close()` to be sure to not display this
plot if you display other plots afterwards.

- You can modify the sankey size by changing the one from the matplotlib figure.
```python
Expand All @@ -195,14 +203,14 @@ Then they can choose what to do with it - showing it, saving it with much more f

### Lint

pylint pysankey
pylint pysankey

### Testing

python -m unittest
python -m unittest

### Coverage

coverage run -m unittest
coverage html
# Open htmlcov/index.html in a navigator
coverage run -m unittest
coverage html
# Open htmlcov/index.html in a navigator
11 changes: 4 additions & 7 deletions pysankey/sankey.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
r"""
Produces simple Sankey Diagrams with matplotlib.
@author: Anneya Golob & marcomanz & pierre-sassoulas & jorwoods & vgalisson
Expand Down Expand Up @@ -29,7 +28,7 @@


class PySankeyException(Exception):
""" Generic PySankey Exception. """
"""Generic PySankey Exception."""


class NullsInFrame(PySankeyException):
Expand Down Expand Up @@ -57,9 +56,7 @@ def check_data_matches_labels(labels, data, side):
msg = "Labels: " + ",".join(labels) + "\n"
if len(data) < 20:
msg += "Data: " + ",".join(data)
raise LabelMismatch(
"{0} labels and data do not match.{1}".format(side, msg)
)
raise LabelMismatch(f"{side} labels and data do not match.{msg}")


def sankey(
Expand Down Expand Up @@ -159,7 +156,7 @@ def sankey(

def save_image(figureName):
if figureName is not None:
fileName = "{}.png".format(figureName)
fileName = f"{figureName}.png"
plt.savefig(fileName, bbox_inches="tight", dpi=150)
LOGGER.info("Sankey diagram generated in '%s'", fileName)

Expand Down Expand Up @@ -386,7 +383,7 @@ def plot_strips(


def _get_positions_and_total_widths(df, labels, side):
""" Determine positions of label patches and total widths"""
"""Determine positions of label patches and total widths"""
widths = defaultdict()
for i, label in enumerate(labels):
labelWidths = {}
Expand Down
8 changes: 3 additions & 5 deletions pysankey/tests/generic_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import os
import unittest

Expand All @@ -20,15 +18,15 @@ def setUpClass(cls):
cls.colorDict = ""

def tearDown(self):
path = "{}.png".format(self.figure_name)
path = f"{self.figure_name}.png"
# Comment this to check the resulting image
if os.path.exists(path):
os.remove(path)


class TestFruit(GenericTest):

""" Base test to test with the data in fruit.txt """
"""Base test to test with the data in fruit.txt"""

def setUp(self):
self.figure_name = "fruit"
Expand All @@ -47,7 +45,7 @@ def setUp(self):

class TestCustomerGood(GenericTest):

""" Base test to test with the data in customers-goods.csv """
"""Base test to test with the data in customers-goods.csv"""

def setUp(self):
self.figure_name = "customer-good"
Expand Down
14 changes: 8 additions & 6 deletions pysankey/tests/test_create_dataframe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import pandas as pd

from pysankey.sankey import create_datadrame
Expand All @@ -7,7 +6,7 @@

class TestCreateDataframeCustomerGood(TestCustomerGood):

""" Tests the create_dataframe function on the data in customers-goods.csv"""
"""Tests the create_dataframe function on the data in customers-goods.csv"""

def test_dataframe_correct_type(self):
dataframe = create_datadrame(
Expand All @@ -20,8 +19,8 @@ def test_dataframe_correct_type(self):

def test_sorted_dataframe(self):
"""
Tests that if we pass a sorted dataframe, it doesn't change the values due to an index
mismatch
Tests that if we pass a sorted dataframe, it doesn't change the values due to
an index mismatch.
"""
# Pass the data as is
dataframe = create_datadrame(
Expand All @@ -40,5 +39,8 @@ def test_sorted_dataframe(self):
rightWeight=data_sorted["revenue"],
)

# Check that the values are still the same if we sort both dataframes the same way
assert (dataframe.sort_values(by="leftWeight").values == dataframe_sorted.values).all()
# Check that the values are still the same if we sort both dataframes
# the same way
assert (
dataframe.sort_values(by="leftWeight").values == dataframe_sorted.values
).all()
6 changes: 2 additions & 4 deletions pysankey/tests/test_deprecated.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-

from pysankey import sankey
from pysankey.tests.generic_test import TestFruit


class TestErrorCase(TestFruit):

""" Test sankey's deprecation warnings. """
"""Test sankey's deprecation warnings."""

def test_deprecated_parameters(self):
""" Test if deprecation warnings are correctly triggered """
"""Test if deprecation warnings are correctly triggered"""
with self.assertWarns(DeprecationWarning):
sankey(
self.data["true"],
Expand Down
10 changes: 4 additions & 6 deletions pysankey/tests/test_error_case.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# -*- coding: utf-8 -*-

from pysankey import LabelMismatch, NullsInFrame, sankey
from pysankey.tests.generic_test import TestFruit


class TestErrorCase(TestFruit):

""" Test sankey's error case. """
"""Test sankey's error case."""

def test_bad_color_labels(self):
""" sankey raise a value error when there is not enough color info"""
"""sankey raise a value error when there is not enough color info"""
bad_color_dict = {"apple": "#f71b1b", "orange": "#f78c1b"}
with self.assertRaises(ValueError) as value_error:
sankey(self.data["true"], self.data["predicted"], colorDict=bad_color_dict)
self.assertIn(": lime, blueberry, banana, kiwi", str(value_error.exception))

def test_label_mismatch(self):
""" sankey raises a LabelMismatch when data doesn't match the labels"""
"""sankey raises a LabelMismatch when data doesn't match the labels"""
with self.assertRaises(LabelMismatch):
sankey(
self.data["true"],
Expand All @@ -26,6 +24,6 @@ def test_label_mismatch(self):
)

def test_nulls_in_frame(self):
""" sankey raises a NullsInFrame when left or right data is null"""
"""sankey raises a NullsInFrame when left or right data is null"""
with self.assertRaises(NullsInFrame):
sankey([None], self.data["predicted"])
6 changes: 2 additions & 4 deletions pysankey/tests/test_readme.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

from pysankey import sankey
Expand All @@ -8,7 +6,7 @@

class TestReadmeFruit(TestFruit):

""" Test use case from README with the data in fruit.txt"""
"""Test use case from README with the data in fruit.txt"""

def test_no_fail_readme(self):
ax = sankey(
Expand All @@ -23,7 +21,7 @@ def test_no_fail_readme(self):

class TestReadmeCustomerGood(TestCustomerGood):

""" Test use case from README with the data in customer-goods.csv"""
"""Test use case from README with the data in customer-goods.csv"""

def test_no_fail_readme(self):
weight = self.data["revenue"].values[1:].astype(float)
Expand Down
2 changes: 0 additions & 2 deletions pysankey/tests/test_sankey.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

from pysankey import sankey
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import setuptools

with open("README.md", "r", encoding="UTF-8") as fh:
with open("README.md", encoding="UTF-8") as fh:
long_description = fh.read()

TEST_REQUIRES = ["pylint", "coverage", "coveralls", "pre-commit", "pytest-cov"]
Expand Down

0 comments on commit 2cd3efa

Please sign in to comment.