Skip to content

Commit

Permalink
Merge pull request #70 from alxndrkalinin/code-formatting
Browse files Browse the repository at this point in the history
add black and flake8 to pre-commit and github actions
  • Loading branch information
alxndrkalinin authored Jul 14, 2022
2 parents 3c907ea + e6073f5 commit 643d3e7
Show file tree
Hide file tree
Showing 36 changed files with 165 additions and 88 deletions.
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[flake8]
ignore = E203, E501, W503
exclude =
.git,
__pycache__,
docs/conf.py,
old,
build,
dist
max-line-length = 88
max-complexity = 12
select = B,C,E,F,W,T4,B9
41 changes: 41 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Code formatting

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
format:
name: Black formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Setup Python
uses: actions/setup-python@master
with:
python-version: 3.7
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install Black
run: pip install black[jupyter]==22.3
- name: Run Black
run: black --config=black.toml --check .

lint:
name: Flake8 linting
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install linters
run: pip install flake8==4.0.1 flake8-docstrings==1.6.0
- name: Run Flake8
run: flake8
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: requirements-txt-fixer
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
args: [--config=black.toml]
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-docstrings==1.6.0]
25 changes: 25 additions & 0 deletions black.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Example configuration for Black.

# NOTE: you have to use single-quoted strings in TOML for regular expressions.
# It's the equivalent of r-strings in Python. Multiline strings are treated as
# verbose regular expressions by Black. Use [ ] to denote a significant space
# character.

[tool.black]
target-version = ['py35', 'py36', 'py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| .ipynb_checkpoints
)/
'''
1 change: 1 addition & 0 deletions cytominer_eval/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Information about cytominer_eval."""
__project__ = "cytominer_eval"
__author__ = "Gregory Way"
__version__ = "0.1"
Expand Down
3 changes: 3 additions & 0 deletions cytominer_eval/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Calculation of quality metrics for perturbation profiling experiments."""
from .evaluate import evaluate
from cytominer_eval import __about__
from cytominer_eval.__about__ import __version__

__all__ = [evaluate, __about__, __version__]
2 changes: 0 additions & 2 deletions cytominer_eval/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
The primary entrypoint into quickly evaluating profile quality.
"""
import warnings
import numpy as np
import pandas as pd
from typing import List, Union

Expand Down
9 changes: 9 additions & 0 deletions cytominer_eval/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@
from .mp_value import mp_value
from .enrichment import enrichment
from .hitk import hitk

__all__ = [
replicate_reproducibility,
precision_recall,
grit,
mp_value,
enrichment,
hitk,
]
4 changes: 1 addition & 3 deletions cytominer_eval/operations/enrichment.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""Function to calculate the enrichment score for a given similarity matrix.
"""
"""Function to calculate the enrichment score for a given similarity matrix."""
import numpy as np
import pandas as pd
from typing import List, Union
import scipy

from cytominer_eval.utils.operation_utils import assign_replicates
from cytominer_eval.utils.transform_utils import set_pair_ids, assert_melt


def enrichment(
Expand Down
1 change: 0 additions & 1 deletion cytominer_eval/operations/grit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
with respect to:
- Similarity to control perturbations
"""
import numpy as np
import pandas as pd
from typing import List

Expand Down
5 changes: 2 additions & 3 deletions cytominer_eval/operations/hitk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Function to calculate the hits at k list and scores for a given similarity matrix.
"""
"""Function to calculate the hits at k list and scores for a given similarity matrix."""
import pandas as pd
from typing import List, Union

Expand Down Expand Up @@ -82,7 +81,7 @@ def hitk(

# make a list of the ranks of correct connection (hits), ie where the group_replicate is true
hits_list = similarity_melted_with_rank[
similarity_melted_with_rank["group_replicate"] == True
similarity_melted_with_rank["group_replicate"]
]["rank"].tolist()

# calculate the scores at each percentage
Expand Down
1 change: 0 additions & 1 deletion cytominer_eval/operations/mp_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
doi: 10.1177/1087057112469257
"""

import numpy as np
import pandas as pd
from typing import List

Expand Down
5 changes: 1 addition & 4 deletions cytominer_eval/operations/precision_recall.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""
Functions to calculate precision and recall at a given k
"""
"""Functions to calculate precision and recall at a given k."""

import numpy as np
import pandas as pd
from typing import List, Union

Expand Down
4 changes: 1 addition & 3 deletions cytominer_eval/operations/replicate_reproducibility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Functions to calculate replicate reproducibility
"""
"""Functions to calculate replicate reproducibility."""

import numpy as np
import pandas as pd
from typing import List

Expand Down
1 change: 1 addition & 0 deletions cytominer_eval/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test cytominer-eval."""
16 changes: 12 additions & 4 deletions cytominer_eval/tests/test_evaluate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test evaluate method of cytominer-eval."""
import os
import pytest
import pathlib
import tempfile
import numpy as np
import pandas as pd
from math import isclose
Expand Down Expand Up @@ -115,7 +115,11 @@ def test_evaluate_replicate_reprod_return_cor_true():

assert np.round(med_cor_df.similarity_metric.max(), 3) == 0.949
assert sorted(med_cor_df.columns.tolist()) == sorted(
["Metadata_gene_name", "Metadata_pert_name", "similarity_metric",]
[
"Metadata_gene_name",
"Metadata_pert_name",
"similarity_metric",
]
)


Expand Down Expand Up @@ -211,7 +215,9 @@ def test_evaluate_grit():
top_result = (
grit_results_df.sort_values(by="grit", ascending=False)
.reset_index(drop=True)
.iloc[0,]
.iloc[
0,
]
)
assert np.round(top_result.grit, 4) == 2.3352
assert top_result.group == "PTK2"
Expand All @@ -237,7 +243,9 @@ def test_evaluate_grit():
top_result = (
grit_results_df.sort_values(by="grit", ascending=False)
.reset_index(drop=True)
.iloc[0,]
.iloc[
0,
]
)

assert np.round(top_result.grit, 4) == 0.9990
Expand Down
1 change: 0 additions & 1 deletion cytominer_eval/tests/test_operations/test_enrichment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import random
import pytest
import pathlib
import tempfile
import numpy as np
Expand Down
6 changes: 1 addition & 5 deletions cytominer_eval/tests/test_operations/test_grit.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import os
import random
import pytest
import pathlib
import tempfile
import numpy as np
import pandas as pd
from pandas.testing import assert_frame_equal

from sklearn.preprocessing import StandardScaler

from cytominer_eval.operations import grit
from cytominer_eval.transform import metric_melt

Expand Down Expand Up @@ -186,7 +182,7 @@ def test_grit_summary_metric():
)

with pytest.raises(ValueError) as ve:
output = grit(
grit(
similarity_melted_df=similarity_melted_df,
control_perts=control_perts,
profile_col=profile_col,
Expand Down
3 changes: 1 addition & 2 deletions cytominer_eval/tests/test_operations/test_hitk.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ def test_hitk_list():


def test_number_of_hits():
"""Calculates the number of indexes based off the MOA value count in the original df
"""
"""Calculates the number of indexes based off the MOA value count in the original df"""
s = sum([n * (n - 1) for n in df["Metadata_moa"].value_counts()])
assert s == len(index_list)

Expand Down
1 change: 0 additions & 1 deletion cytominer_eval/tests/test_operations/test_mp_value.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import pytest
import pathlib
import tempfile
import numpy as np
import pandas as pd
from math import isclose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import tempfile
import numpy as np
import pandas as pd
import pandas.api.types as ptypes

from cytominer_eval.transform import metric_melt
from cytominer_eval.operations import replicate_reproducibility
Expand Down Expand Up @@ -58,7 +57,7 @@ def test_replicate_reproducibility():
def test_replicate_reproducibility_uniquerows():
with pytest.raises(AssertionError) as err:
replicate_groups = ["Metadata_pert_well"]
output = replicate_reproducibility(
replicate_reproducibility(
similarity_melted_df=similarity_melted_df,
replicate_groups=replicate_groups,
quantile_over_null=0.95,
Expand Down
15 changes: 6 additions & 9 deletions cytominer_eval/tests/test_transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
import random
import pytest
import pathlib
import tempfile
import numpy as np
import pandas as pd
import pandas.api.types as ptypes

from cytominer_eval.transform.transform import get_pairwise_metric, process_melt
from cytominer_eval.transform import metric_melt

random.seed(123)
tmpdir = tempfile.gettempdir()

example_file = "SQ00015054_normalized_feature_select.csv.gz"
example_file = pathlib.Path(
Expand Down Expand Up @@ -40,11 +37,11 @@

def test_get_pairwise_metric():
with pytest.raises(ValueError) as ve:
output = get_pairwise_metric(df, similarity_metric="pearson")
get_pairwise_metric(df, similarity_metric="pearson")
assert "check input features" in str(ve.value)

with pytest.raises(AssertionError) as ve:
output = get_pairwise_metric(feature_df, similarity_metric="not supported")
get_pairwise_metric(feature_df, similarity_metric="not supported")
assert "not supported not supported" in str(ve.value)

result_df = get_pairwise_metric(feature_df, similarity_metric="pearson")
Expand All @@ -56,7 +53,7 @@ def test_get_pairwise_metric():

def test_process_melt():
with pytest.raises(AssertionError) as ve:
output = process_melt(df=feature_df, meta_df=meta_df)
process_melt(df=feature_df, meta_df=meta_df)
assert "Matrix must be symmetrical" in str(ve.value)

melted_df = process_melt(df=pairwise_metric_df, meta_df=meta_df)
Expand All @@ -82,7 +79,7 @@ def test_metric_melt():
assert result_df.shape[0] == 73536

with pytest.raises(AssertionError) as ve:
output = metric_melt(
metric_melt(
df,
features,
meta_features,
Expand All @@ -92,13 +89,13 @@ def test_metric_melt():
assert "MISSING not supported. Select one of" in str(ve.value)

with pytest.raises(AssertionError) as ve:
output = metric_melt(
metric_melt(
df, features + ["NOT SUPPORTED"], meta_features, similarity_metric="pearson"
)
assert "Profile feature not found" in str(ve.value)

with pytest.raises(AssertionError) as ve:
output = metric_melt(
metric_melt(
df, features, meta_features + ["NOT SUPPORTED"], similarity_metric="pearson"
)
assert "Metadata feature not found" in str(ve.value)
Expand Down
6 changes: 1 addition & 5 deletions cytominer_eval/tests/test_utils/test_assert_melt.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import os
import random
import pytest
import pathlib
import tempfile
import numpy as np
import pandas as pd
import pandas.api.types as ptypes

from cytominer_eval.transform import metric_melt
from cytominer_eval.utils.operation_utils import assign_replicates
Expand Down Expand Up @@ -55,7 +51,7 @@ def test_assert_melt():

for dummy_metric in dummy_metrics:
with pytest.raises(AssertionError) as ve:
output = assert_melt(result, eval_metric=dummy_metric)
assert_melt(result, eval_metric=dummy_metric)
assert (
"Stop! The eval_metric provided in 'metric_melt()' is incorrect!"
in str(ve.value)
Expand Down
Loading

0 comments on commit 643d3e7

Please sign in to comment.