Skip to content

Commit

Permalink
Add standard pre-commit config + linting CI job (#17)
Browse files Browse the repository at this point in the history
* Add standard pre-commit config

* Add GH actions linting job
  • Loading branch information
dstansby authored Jun 24, 2022
1 parent 1682591 commit 0a2a6da
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ name: tests
on: [push, pull_request]

jobs:
linting:
runs-on: ubuntu-latest
steps:
- uses: brainglobe/actions/lint@v1
test:
name: ${{ matrix.platform }} py${{ matrix.python-version }}
runs-on: ${{ matrix.platform }}
Expand Down
38 changes: 29 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
repos:
- repo: https://github.com/python/black
rev: 20.8b1
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: black
pass_filenames: true
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: flake8
pass_filenames: true
# exclude: __init__.py
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-toml
- id: end-of-file-fixer
- id: mixed-line-ending
args: [--fix=lf]
- id: requirements-txt-fixer
- id: trailing-whitespace
exclude: setup.cfg # bump2version fix
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.931
hooks:
- id: mypy
additional_dependencies:
- types-setuptools
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Visualise cellfinder and brainreg results with napari


## Installation
This package is likely already installed
(e.g. with cellfinder, brainreg or another napari plugin), but if you want to
install it again, either use the napari plugin install GUI or you can
This package is likely already installed
(e.g. with cellfinder, brainreg or another napari plugin), but if you want to
install it again, either use the napari plugin install GUI or you can
install `brainglobe-napari-io` via [pip]:

pip install brainglobe-napari-io
Expand All @@ -26,7 +26,7 @@ install `brainglobe-napari-io` via [pip]:
### brainreg
#### Sample space
Drag your [brainreg](https://github.com/brainglobe/brainreg) output directory (the one with the log file) onto the napari window.

Various images should then open, including:
* `Registered image` - the image used for registration, downsampled to atlas resolution
* `atlas_name` - e.g. `allen_mouse_25um` the atlas labels, warped to your sample brain
Expand All @@ -42,8 +42,8 @@ _N.B. If you use a high resolution atlas (such as `allen_mouse_10um`), then the


#### Atlas space
`napari-brainreg` also comes with an additional plugin, for visualising your data
in atlas space.
`napari-brainreg` also comes with an additional plugin, for visualising your data
in atlas space.

This is typically only used in other software, but you can enable it yourself:
* Open napari
Expand All @@ -61,9 +61,9 @@ This is typically only used in other software, but you can enable it yourself:
* Load your raw data (drag and drop the data directories into napari, one at a time)
* Drag and drop your cellfinder output directory into napari.

The plugin will then load your detected cells (in yellow) and the rejected cell
candidates (in blue). If you carried out registration, then these results will be
overlaid (similarly to the loading brainreg data, but transformed to the
The plugin will then load your detected cells (in yellow) and the rejected cell
candidates (in blue). If you carried out registration, then these results will be
overlaid (similarly to the loading brainreg data, but transformed to the
coordinate space of your raw data).

![load_data](https://raw.githubusercontent.com/brainglobe/brainglobe-napari-io/master/resources/load_data.gif)
Expand Down
5 changes: 3 additions & 2 deletions brainglobe_napari_io/brainreg/reader_dir.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import json
import tifffile
import os
from pathlib import Path

import tifffile
from bg_atlasapi.bg_atlas import BrainGlobeAtlas

from .utils import is_brainreg_dir, load_additional_downsampled_channels
Expand Down
5 changes: 3 additions & 2 deletions brainglobe_napari_io/brainreg/reader_dir_standard_space.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import json
import tifffile
import os
from pathlib import Path

import tifffile
from bg_atlasapi.bg_atlas import BrainGlobeAtlas

from .utils import is_brainreg_dir, load_additional_downsampled_channels
Expand Down
8 changes: 3 additions & 5 deletions brainglobe_napari_io/brainreg/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

import tifffile


Expand Down Expand Up @@ -45,12 +46,9 @@ def load_additional_downsampled_channels(
):

print(
f"Found additional downsampled image: {file.name}, "
f"adding to viewer"
)
name = file.name.strip(search_string).strip(extension) + (
" (downsampled)"
f"Found additional downsampled image: {file.name}, " f"adding to viewer"
)
name = file.name.strip(search_string).strip(extension) + (" (downsampled)")
layers.append(
(
tifffile.imread(file),
Expand Down
26 changes: 8 additions & 18 deletions brainglobe_napari_io/cellfinder/reader_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
Replace code below accordingly. For complete documentation see:
https://napari.org/docs/plugins/for_plugin_developers.html
"""
import json
import os
import sys
import json
import bg_space as bgs
from pathlib import Path

from ..brainreg.reader_dir import reader_function as brainreg_reader
import bg_space as bgs

from ..brainreg.reader_dir import reader_function as brainreg_reader
from .utils import load_cells


Expand Down Expand Up @@ -110,16 +110,12 @@ def reader_function(path, point_size=15, opacity=0.6, symbol="ring"):
)

else:
layers = load_cells_from_file(
path, layers, point_size, opacity, symbol
)
layers = load_cells_from_file(path, layers, point_size, opacity, symbol)

return layers


def load_cells_from_file(
path, layers, point_size, opacity, symbol, channel=None
):
def load_cells_from_file(path, layers, point_size, opacity, symbol, channel=None):
classified_cells_path = path / "points" / "cell_classification.xml"
layers = load_cells(
layers,
Expand All @@ -139,9 +135,7 @@ def load_registration(layers, registration_directory, metadata):
registration_layers = remove_downsampled_images(registration_layers)
atlas = get_atlas(registration_layers)

registration_layers = scale_reorient_layers(
registration_layers, atlas, metadata
)
registration_layers = scale_reorient_layers(registration_layers, atlas, metadata)
layers.extend(registration_layers)
return layers

Expand Down Expand Up @@ -181,13 +175,9 @@ def reorient_registration_layers(layers, atlas, metadata):
return new_layers


def reorient_registration_layer(
layer, atlas_orientation, raw_data_orientation
):
def reorient_registration_layer(layer, atlas_orientation, raw_data_orientation):
layer = list(layer)
layer[0] = bgs.map_stack_to(
atlas_orientation, raw_data_orientation, layer[0]
)
layer[0] = bgs.map_stack_to(atlas_orientation, raw_data_orientation, layer[0])
layer = tuple(layer)
return layer

Expand Down
1 change: 0 additions & 1 deletion brainglobe_napari_io/cellfinder/reader_xml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path

from xml.etree import ElementTree

from .utils import load_cells
Expand Down
4 changes: 2 additions & 2 deletions brainglobe_napari_io/cellfinder/writer_xml.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Tuple, Dict, Any
from typing import Any, Dict, List, Tuple

from imlib.IO.cells import save_cells
from imlib.cells.cells import Cell
from imlib.IO.cells import save_cells
from napari.types import FullLayerData
from napari.utils.notifications import show_info

Expand Down
11 changes: 3 additions & 8 deletions brainglobe_napari_io/plugins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from brainglobe_napari_io.cellfinder.reader_xml import cellfinder_read_xml
from brainglobe_napari_io.cellfinder.reader_dir import cellfinder_read_dir

from brainglobe_napari_io.brainreg.reader_dir import brainreg_read_dir

from brainglobe_napari_io.cellfinder.writer_xml import (
cellfinder_write_xml,
cellfinder_write_multiple_xml,
)
from brainglobe_napari_io.cellfinder.reader_dir import cellfinder_read_dir
from brainglobe_napari_io.cellfinder.reader_xml import cellfinder_read_xml
from brainglobe_napari_io.cellfinder.writer_xml import write_multiple_points_to_xml
10 changes: 5 additions & 5 deletions brainglobe_napari_io/tests/test_cellfinder_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_reader_xml():
assert reader_xml.cellfinder_read_xml(str(xml_file.resolve())) is not None
layers = reader_xml.xml_reader(xml_file)

for l in layers:
assert len(l) == 3
assert isinstance(l[0], np.ndarray)
assert isinstance(l[1], dict)
assert isinstance(l[2], str)
for layer in layers:
assert len(layer) == 3
assert isinstance(layer[0], np.ndarray)
assert isinstance(layer[1], dict)
assert isinstance(layer[2], str)
5 changes: 1 addition & 4 deletions brainglobe_napari_io/tests/test_cellfinder_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import numpy as np

from brainglobe_napari_io.cellfinder import (
reader_xml,
writer_xml,
)
from brainglobe_napari_io.cellfinder import reader_xml, writer_xml

test_data_dir = pathlib.Path(__file__) / ".." / "data"

Expand Down
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[tool.black]
target-version = ['py36', 'py37', 'py38']
target-version = ['py36', 'py37', 'py38', 'py39', 'py310']
skip-string-normalization = false
line-length = 79
exclude = '''
(
/(
Expand All @@ -18,4 +17,8 @@ exclude = '''
| examples
)/
)
'''
'''


[tool.isort]
profile = "black"
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ commit = True
tag = True
tag_name = {new_version}
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<rc>\d+))?
serialize =
serialize =
{major}.{minor}.{patch}-{release}{rc}
{major}.{minor}.{patch}

[bumpversion:part:release]
optional_value = prod
first_value = rc
values =
values =
rc
prod

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from setuptools import setup, find_packages
from os import path

from setuptools import find_packages, setup

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
long_description = f.read()
Expand Down

0 comments on commit 0a2a6da

Please sign in to comment.