Skip to content

Commit

Permalink
STYLE: Transition to ruff for code formatting
Browse files Browse the repository at this point in the history
Transition to `ruff` for code formatting in line with the rest of the
`nipreps` repositories.

Drop the flake8 `W503` rule, which is not implemented by `ruff`.

Add a GHA workflow to ensure that contributions not complying with the
style are detected.

Format the source code accordingly.
  • Loading branch information
jhlegarreta committed May 11, 2024
1 parent a8b3927 commit d8f3beb
Show file tree
Hide file tree
Showing 39 changed files with 424 additions and 388 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/contrib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Contribution checks
on: [push, pull_request]

defaults:
run:
shell: bash

jobs:
stable:
name: Run ruff
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0
- name: Set up Python 3
uses: actions/setup-python@v5
with:
python-version: 3

- name: Install and run pre-commit hooks
uses: pre-commit/[email protected]
4 changes: 2 additions & 2 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Christopher J. Markiewicz <[email protected]>
Christopher J. Markiewicz <[email protected]> <[email protected]>
Christopher J. Markiewicz <[email protected]> <[email protected]>
Conrad Ma <[email protected]>
Conrad Ma <[email protected]> Conrad
Conrad Ma <[email protected]> 394822740
Conrad Ma <[email protected]> Conrad
Conrad Ma <[email protected]> 394822740
Conrad Ma <[email protected]> <[email protected]>
Conrad Ma <[email protected]> <[email protected]>
Dylan Nielson <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion .maint/PIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ If you are participating because of your affiliation with another organization (
| **Lastname** | **Name** | **Handle** | **ORCID** | **Affiliation** | **Position** |
| --- | --- | --- | --- | --- | --- |
| Esteban | Oscar | @oesteban | 0000-0001-8435-6191 | Department of Radiology, Lausanne University Hospital and University of Lausanne | -1 |
| Poldrack | Russell A. | @poldrack | 0000-0001-6755-0259 | Department of Psychology, Stanford University, CA, USA | -2 |
| Poldrack | Russell A. | @poldrack | 0000-0001-6755-0259 | Department of Psychology, Stanford University, CA, USA | -2 |
2 changes: 1 addition & 1 deletion .maint/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
click
fuzzywuzzy
fuzzywuzzy
70 changes: 24 additions & 46 deletions .maint/update_authors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
"""Update and sort the creators list of the zenodo record."""

import json
import sys
from pathlib import Path
import json

import click
from fuzzywuzzy import fuzz, process

Expand Down Expand Up @@ -36,10 +38,7 @@ def read_md_table(md_text):
retval = []
for line in md_text.splitlines():
if line.strip().startswith("| --- |"):
keys = (
k.replace("*", "").strip()
for k in prev.split("|")
)
keys = (k.replace("*", "").strip() for k in prev.split("|"))
keys = [k.lower() for k in keys if k]
continue
elif not keys:
Expand All @@ -60,19 +59,13 @@ def sort_contributors(entries, git_lines, exclude=None, last=None):
last = last or []
sorted_authors = sorted(entries, key=lambda i: i["name"])

first_last = [
" ".join(val["name"].split(",")[::-1]).strip() for val in sorted_authors
]
first_last_excl = [
" ".join(val["name"].split(",")[::-1]).strip() for val in exclude or []
]
first_last = [" ".join(val["name"].split(",")[::-1]).strip() for val in sorted_authors]
first_last_excl = [" ".join(val["name"].split(",")[::-1]).strip() for val in exclude or []]

unmatched = []
author_matches = []
for ele in git_lines:
matches = process.extract(
ele, first_last, scorer=fuzz.token_sort_ratio, limit=2
)
matches = process.extract(ele, first_last, scorer=fuzz.token_sort_ratio, limit=2)
# matches is a list [('First match', % Match), ('Second match', % Match)]
if matches[0][1] > 80:
val = sorted_authors[first_last.index(matches[0][0])]
Expand Down Expand Up @@ -152,8 +145,9 @@ def cli():
@cli.command()
@click.option("-z", "--zenodo-file", type=click.Path(exists=True), default=".zenodo.json")
@click.option("-m", "--maintainers", type=click.Path(exists=True), default=".maint/MAINTAINERS.md")
@click.option("-c", "--contributors", type=click.Path(exists=True),
default=".maint/CONTRIBUTORS.md")
@click.option(
"-c", "--contributors", type=click.Path(exists=True), default=".maint/CONTRIBUTORS.md"
)
@click.option("--pi", type=click.Path(exists=True), default=".maint/PIs.md")
@click.option("-f", "--former-file", type=click.Path(exists=True), default=".maint/FORMER.md")
def zenodo(
Expand All @@ -176,15 +170,13 @@ def zenodo(
)

zen_contributors, miss_contributors = sort_contributors(
_namelast(read_md_table(Path(contributors).read_text())),
data,
exclude=former
_namelast(read_md_table(Path(contributors).read_text())), data, exclude=former
)

zen_pi = _namelast(
sorted(
read_md_table(Path(pi).read_text()),
key=lambda v: (int(v.get("position", -1)), v.get("lastname"))
key=lambda v: (int(v.get("position", -1)), v.get("lastname")),
)
)

Expand All @@ -194,8 +186,7 @@ def zenodo(
misses = set(miss_creators).intersection(miss_contributors)
if misses:
print(
"Some people made commits, but are missing in .maint/ "
f"files: {', '.join(misses)}",
"Some people made commits, but are missing in .maint/ " f"files: {', '.join(misses)}",
file=sys.stderr,
)

Expand All @@ -214,15 +205,14 @@ def zenodo(
if isinstance(creator["affiliation"], list):
creator["affiliation"] = creator["affiliation"][0]

Path(zenodo_file).write_text(
"%s\n" % json.dumps(zenodo, indent=2)
)
Path(zenodo_file).write_text("%s\n" % json.dumps(zenodo, indent=2))


@cli.command()
@click.option("-m", "--maintainers", type=click.Path(exists=True), default=".maint/MAINTAINERS.md")
@click.option("-c", "--contributors", type=click.Path(exists=True),
default=".maint/CONTRIBUTORS.md")
@click.option(
"-c", "--contributors", type=click.Path(exists=True), default=".maint/CONTRIBUTORS.md"
)
@click.option("--pi", type=click.Path(exists=True), default=".maint/PIs.md")
@click.option("-f", "--former-file", type=click.Path(exists=True), default=".maint/FORMER.md")
def publication(
Expand All @@ -232,9 +222,8 @@ def publication(
former_file,
):
"""Generate the list of authors and affiliations for papers."""
members = (
_namelast(read_md_table(Path(maintainers).read_text()))
+ _namelast(read_md_table(Path(contributors).read_text()))
members = _namelast(read_md_table(Path(maintainers).read_text())) + _namelast(
read_md_table(Path(contributors).read_text())
)

hits, misses = sort_contributors(
Expand All @@ -246,15 +235,12 @@ def publication(
pi_hits = _namelast(
sorted(
read_md_table(Path(pi).read_text()),
key=lambda v: (int(v.get("position", -1)), v.get("lastname"))
key=lambda v: (int(v.get("position", -1)), v.get("lastname")),
)
)

pi_names = [pi["name"] for pi in pi_hits]
hits = [
hit for hit in hits
if hit["name"] not in pi_names
] + pi_hits
hits = [hit for hit in hits if hit["name"] not in pi_names] + pi_hits

def _aslist(value):
if isinstance(value, (list, tuple)):
Expand All @@ -281,27 +267,19 @@ def _aslist(value):

if misses:
print(
"Some people made commits, but are missing in .maint/ "
f"files: {', '.join(misses)}",
"Some people made commits, but are missing in .maint/ " f"files: {', '.join(misses)}",
file=sys.stderr,
)

print("Authors (%d):" % len(hits))
print(
"%s."
% "; ".join(
[
"%s \\ :sup:`%s`\\ " % (i["name"], idx)
for i, idx in zip(hits, aff_indexes)
]
)
% "; ".join(["%s \\ :sup:`%s`\\ " % (i["name"], idx) for i, idx in zip(hits, aff_indexes)])
)

print(
"\n\nAffiliations:\n%s"
% "\n".join(
["{0: >2}. {1}".format(i + 1, a) for i, a in enumerate(affiliations)]
)
% "\n".join(["{0: >2}. {1}".format(i + 1, a) for i, a in enumerate(affiliations)])
)


Expand Down
51 changes: 31 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
# To install the git pre-commit hook run:
# pre-commit install
# To update the pre-commit hooks run:
# pre-commit install-hooks

exclude: |
(?x)^(
docs/_static/.*|
nireports/tests/data/.*|
nireports/assembler/data/tests/work/.*|
nireports/assembler/data/tests/crashfile.txt|
nireports/assembler/data/.*\.tpl
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-toml
- id: check-case-conflict
- id: check-docstring-first
- id: check-merge-conflict
- id: check-vcs-permalinks
- id: pretty-format-json
args: ['--autofix']
- repo: https://github.com/psf/black
rev: 22.3.0
- id: trailing-whitespace
- id: end-of-file-fixer
- id: debug-statements
- id: check-yaml
- id: check-json
- id: check-toml
- id: check-case-conflict
- id: check-docstring-first
- id: check-merge-conflict
- id: check-vcs-permalinks
- id: pretty-format-json
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
- id: ruff
args: [ --fix ]
- id: ruff-format
62 changes: 31 additions & 31 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
{
"title": "NiReports: the NiPreps' reporting and visualization tools",
"description": "<p>The visual report system (VRS) of <em>NiPreps</em>.</p>",
"creators": [
{
"orcid": "0000-0001-8435-6191",
"affiliation": "Lausanne University Hospital and University of Lausanne, Lausanne, Switzerland",
"name": "Oscar Esteban"
},
{
"orcid": "0000-0002-3896-6906",
"affiliation": "Department of Radiology, Lausanne University Hospital and University of Lausanne, Switzerland",
"name": "Elodie Savary"
},
{
"orcid": "0000-0003-3715-7012",
"affiliation": "Department of Neuroimaging, Institute of Psychiatry, Psychology and Neuroscience, King's College London, London, UK",
"name": "Eilidh MacNicol"
},
{
"orcid": "0000-0002-1668-9629",
"affiliation": "Department of Radiology, Lausanne University Hospital and University of Lausanne, Switzerland",
"name": "C\u00e9line Provins"
}
],
"contributors": [
{
"orcid": "0000-0002-6533-164X",
"affiliation": "Department of Psychology, Stanford University, CA, USA",
"name": "Christopher J. Markiewicz",
"orcid": "0000-0002-6533-164X",
"type": "Researcher"
},
{
"orcid": "0000-0001-7159-1387",
"affiliation": "Sagol School of Neuroscience, Tel Aviv University, Tel Aviv, Israel",
"name": "Zvi Baratz",
"orcid": "0000-0001-7159-1387",
"type": "Researcher"
},
{
"orcid": "0000-0002-7252-7771",
"affiliation": "Department of Psychology, Stanford University, CA, USA",
"name": "Mathias Goncalves",
"orcid": "0000-0002-7252-7771",
"type": "Researcher"
},
{
"orcid": "0000-0003-3007-1056",
"affiliation": "Department of Psychology, Stanford University, CA, USA",
"name": "Ross W. Blair",
"orcid": "0000-0003-3007-1056",
"type": "Researcher"
},
{
"orcid": "0000-0003-4613-6643",
"affiliation": "Section on Clinical and Computational Psychiatry, National Institute of Mental Health, Bethesda, MD, USA",
"name": "Dylan Nielson",
"orcid": "0000-0003-4613-6643",
"type": "Researcher"
},
{
"orcid": "0000-0001-8435-6191",
"affiliation": "Department of Radiology, Lausanne University Hospital and University of Lausanne",
"name": "Oscar Esteban",
"orcid": "0000-0001-8435-6191",
"type": "Researcher"
},
{
"orcid": "0000-0001-6755-0259",
"affiliation": "Department of Psychology, Stanford University, CA, USA",
"name": "Russell A. Poldrack",
"orcid": "0000-0001-6755-0259",
"type": "Researcher"
}
],
"creators": [
{
"affiliation": "Lausanne University Hospital and University of Lausanne, Lausanne, Switzerland",
"name": "Oscar Esteban",
"orcid": "0000-0001-8435-6191"
},
{
"affiliation": "Department of Radiology, Lausanne University Hospital and University of Lausanne, Switzerland",
"name": "Elodie Savary",
"orcid": "0000-0002-3896-6906"
},
{
"affiliation": "Department of Neuroimaging, Institute of Psychiatry, Psychology and Neuroscience, King's College London, London, UK",
"name": "Eilidh MacNicol",
"orcid": "0000-0003-3715-7012"
},
{
"affiliation": "Department of Radiology, Lausanne University Hospital and University of Lausanne, Switzerland",
"name": "C\u00e9line Provins",
"orcid": "0000-0002-1668-9629"
}
],
"description": "<p>The visual report system (VRS) of <em>NiPreps</em>.</p>",
"keywords": [
"neuroimaging",
"visualization",
"nipreps"
],
"license": "Apache-2.0",
"title": "NiReports: the NiPreps' reporting and visualization tools",
"upload_type": "software"
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Contributing to *nireports*

*nireports* is a project of the [*NiPreps* Community, which specifies the contributing guidelines](https://www.nipreps.org/community/).
*nireports* is a project of the [*NiPreps* Community, which specifies the contributing guidelines](https://www.nipreps.org/community/).
Loading

0 comments on commit d8f3beb

Please sign in to comment.