Skip to content

Commit

Permalink
cicd: add Ruff and Black (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson authored Sep 20, 2023
1 parent e116661 commit 46e91c5
Show file tree
Hide file tree
Showing 32 changed files with 1,747 additions and 1,707 deletions.
11 changes: 0 additions & 11 deletions .flake8

This file was deleted.

16 changes: 4 additions & 12 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: python3 -m pip install ".[dev]"
- name: black
uses: psf/black@stable

- name: Check style
run: python3 -m flake8 gene/ tests/ setup.py
- name: ruff
uses: chartboost/ruff-action@v1

test:
runs-on: ubuntu-latest
Expand Down
26 changes: 17 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.4.0
hooks:
- id: flake8
additional_dependencies: [flake8-docstrings]
- id: check-added-large-files
- id: detect-private-key
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.4.0
hooks:
- id: check-added-large-files
- id: detect-private-key
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
language_version: python3.11
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.290
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
6 changes: 3 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fastapi = "*"
uvicorn = "*"
click = "*"
boto3 = "*"
"ga4gh.vrs" = {version = "~=2.0.0.dev0"}
"ga4gh.vrs" = {version = "==2.0.0.dev0"}

[dev-packages]
gene = {editable = true, path = "."}
Expand All @@ -18,8 +18,8 @@ gffutils = "*"
psycopg = {version = "*", extras=["binary"]}
pytest = "*"
pre-commit = "*"
flake8 = "*"
flake8-docstrings = "*"
black = "*"
ruff = "*"
pytest-cov = "*"
mock = "*"
ipykernel = "*"
Expand Down
45 changes: 31 additions & 14 deletions docs/scripts/generate_normalize_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
Embeddable HTML for the normalization figure should be deposited in the correct
location, at docs/source/_static/html/normalize_her2.html.
"""
from typing import Dict
import json
from typing import Dict

import gravis as gv

Expand All @@ -17,17 +17,20 @@
from gene.query import QueryHandler
from gene.schemas import UnmergedNormalizationService


COLORS = [
"#F8766D",
"#00BA38",
"#00B9E3",
]

GENE = 'OTX2P1'
GENE = "OTX2P1"


def create_gjgf(result: UnmergedNormalizationService) -> Dict:
"""Create gravis input.
:param result: result from Unmerged Normalization search
"""
graph = {
"graph": {
"label": f"Reference Network for Search Term '{GENE}'",
Expand All @@ -37,8 +40,8 @@ def create_gjgf(result: UnmergedNormalizationService) -> Dict:
"arrow_size": 15,
"node_size": 15,
"node_label_size": 20,
"edge_size": 2
}
"edge_size": 2,
},
}
}

Expand All @@ -48,7 +51,7 @@ def create_gjgf(result: UnmergedNormalizationService) -> Dict:
"metadata": {
"color": COLORS[i],
"hover": f"{match.concept_id}\n{match.symbol}\n<i>{match.label}</i>", # noqa: E501
"click": f"<p color='black'>{json.dumps(match.model_dump(), indent=2)}</p>" # noqa: E501
"click": f"<p color='black'>{json.dumps(match.model_dump(), indent=2)}</p>", # noqa: E501
}
}
for xref in match.xrefs:
Expand All @@ -58,8 +61,10 @@ def create_gjgf(result: UnmergedNormalizationService) -> Dict:

included_edges = []
for edge in graph["graph"]["edges"]:
if edge["target"] in graph["graph"]["nodes"] and \
edge["source"] in graph["graph"]["nodes"]:
if (
edge["target"] in graph["graph"]["nodes"]
and edge["source"] in graph["graph"]["nodes"]
):
included_edges.append(edge)
graph["graph"]["edges"] = included_edges

Expand All @@ -75,20 +80,32 @@ def create_gjgf(result: UnmergedNormalizationService) -> Dict:
return graph


def gen_norm_figure():
def gen_norm_figure() -> None:
"""Generate normalized graph figure for docs."""
q = QueryHandler(create_db())
result = q.normalize_unmerged(GENE)

graph = create_gjgf(result)

fig = gv.d3(
data=graph, graph_height=200, node_hover_neighborhood=True,
use_links_force=True, links_force_distance=0.01, links_force_strength=0.01,
node_label_font="arial"
data=graph,
graph_height=200,
node_hover_neighborhood=True,
use_links_force=True,
links_force_distance=0.01,
links_force_strength=0.01,
node_label_font="arial",
)
fig.export_html(
(APP_ROOT.parents[0] / "docs" / "source" / "_static" / "html" / f"normalize_{GENE.lower()}.html").absolute(), # noqa: E501
overwrite=True
(
APP_ROOT.parents[0]
/ "docs"
/ "source"
/ "_static"
/ "html"
/ f"normalize_{GENE.lower()}.html"
).absolute(), # noqa: E501
overwrite=True,
)


Expand Down
3 changes: 1 addition & 2 deletions docs/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ When running the web server, enable hot-reloading on new code changes: ::
Style
-----

Code style is managed by `flake8 <https://github.com/PyCQA/flake8>`_ and should be checked via pre-commit hook before commits. Final QC is applied with GitHub Actions to every pull request.

Code style is managed by `Ruff <https://github.com/astral-sh/ruff>`_ and `Black <https://github.com/psf/black>`_, and should be checked via pre-commit hook before commits. Final QC is applied with GitHub Actions to every pull request.

Tests
-----
Expand Down
40 changes: 23 additions & 17 deletions gene/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""The VICC library for normalizing genes."""
from pathlib import Path
import logging
from os import environ
from pathlib import Path

from .version import __version__ # noqa: F401


APP_ROOT = Path(__file__).resolve().parents[0]

logging.basicConfig(
filename="gene.log",
format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s")
filename="gene.log", format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s"
)
logger = logging.getLogger("gene")
logger.setLevel(logging.DEBUG)
logger.handlers = []
Expand All @@ -28,32 +27,39 @@
)


class DownloadException(Exception):
class DownloadException(Exception): # noqa: N818
"""Exception for failures relating to source file downloads."""

def __init__(self, *args, **kwargs):
"""Initialize exception."""
super().__init__(*args, **kwargs)

from gene.schemas import ( # noqa: E402
NamespacePrefix,
RefType,
SourceIDAfterNamespace,
SourceName,
)

from gene.schemas import SourceName, NamespacePrefix, SourceIDAfterNamespace, RefType # noqa: E402, E501
ITEM_TYPES = {k.lower(): v.value for k, v in RefType.__members__.items()}

# Sources we import directly (HGNC, Ensembl, NCBI)
SOURCES = {source.value.lower(): source.value
for source in SourceName.__members__.values()}
SOURCES = {
source.value.lower(): source.value for source in SourceName.__members__.values()
}

# Set of sources we import directly
XREF_SOURCES = {src.lower() for src in SourceName.__members__}

# use to fetch source name from schema based on concept id namespace
# e.g. {"hgnc": "HGNC"}
PREFIX_LOOKUP = {v.value: SourceName[k].value
for k, v in NamespacePrefix.__members__.items()
if k in SourceName.__members__.keys()}
PREFIX_LOOKUP = {
v.value: SourceName[k].value
for k, v in NamespacePrefix.__members__.items()
if k in SourceName.__members__.keys()
}

# use to generate namespace prefix from source ID value
# e.g. {"ensg": "ensembl"}
NAMESPACE_LOOKUP = {v.value.lower(): NamespacePrefix[k].value
for k, v in SourceIDAfterNamespace.__members__.items()
if v.value != ""}
NAMESPACE_LOOKUP = {
v.value.lower(): NamespacePrefix[k].value
for k, v in SourceIDAfterNamespace.__members__.items()
if v.value != ""
}
Loading

0 comments on commit 46e91c5

Please sign in to comment.