Skip to content

Commit

Permalink
bump version, merge pull request #9 from AMYPAD/devel
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl authored Nov 29, 2020
2 parents 5654e31 + 47ed925 commit cd553f7
Show file tree
Hide file tree
Showing 21 changed files with 297 additions and 45 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/comment-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
types: [created]
pull_request_review_comment:
types: [created]

jobs:
tag: # /tag <tagname> <commit>
if: startsWith(github.event.comment.body, '/tag ')
Expand All @@ -21,7 +20,6 @@ jobs:
post = (context.eventName == "issue_comment"
? github.reactions.createForIssueComment
: github.reactions.createForPullRequestReviewComment)
if (!["admin", "write"].includes(perm.data.permission)){
post({
owner: context.repo.owner, repo: context.repo.repo,
Expand Down
20 changes: 18 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ jobs:
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: set PYSHA
run: echo "PYSHA=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PYSHA }}|${{ hashFiles('.pre-commit-config.yaml') }}
- run: pip install -U .[dev]
- run: python setup.py sdist bdist_wheel
- run: twine check dist/*
- run: flake8 .
- uses: reviewdog/action-setup@v1
- if: github.event_name != 'schedule'
run: |
set -o pipefail
pre-commit run -a flake8 | \
reviewdog -f=pep8 -name=flake8 -tee -reporter=github-check -filter-mode nofilter
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- if: |
! startsWith(matrix.python, '2')
run: black --check .
run: pre-commit run -a --show-diff-on-failure black
- run: pre-commit run -a --show-diff-on-failure isort
test:
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -59,6 +73,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: casperdcl/deploy-pypi@v1
with:
build: true
Expand Down
32 changes: 32 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- flake8-debugger
- flake8-string-format
repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
- hooks:
- id: black
repo: https://github.com/psf/black
rev: 19.10b0
- hooks:
- id: isort
repo: https://github.com/timothycrosley/isort
rev: 5.6.4
9 changes: 9 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- id: mbeautify
name: mbeautify
description: '`mbeautify` is a command-line utility for enforcing style consistency across Matlab projects.'
entry: mbeautify
language: python
additional_dependencies: ['.[mbeautify]']
types: [text]
files: \.(m)$
require_serial: true
2 changes: 1 addition & 1 deletion LICENCE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Copyright 2020 AMYPAD

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may not use this project except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Expand Down
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Install
Intended for inclusion in requirements for other packages.
The package name is ``miutil``. Extra install options include:

- cuda

- provides `miutil.cuinfo <https://github.com/AMYPAD/miutil/blob/master/miutil/cuinfo.py>`_

- mbeautify

- provides `miutil.mlab.beautify <https://github.com/AMYPAD/miutil/blob/master/miutil/mlab/beautify.py>`_

- nii

- provides `miutil.imio.nii <https://github.com/AMYPAD/miutil/blob/master/miutil/imio/nii.py>`_
Expand All @@ -25,9 +33,9 @@ The package name is ``miutil``. Extra install options include:

- includes a useful 3D multi-volume ``imscroll`` function which depends only on ``matplotlib``

- cuda
- web

- provides `miutil.cuinfo <https://github.com/AMYPAD/miutil/blob/master/miutil/cuinfo.py>`_
- provides `miutil.web <https://github.com/AMYPAD/miutil/blob/master/miutil/web.py>`_


To install extras and their dependencies,
Expand Down
5 changes: 3 additions & 2 deletions miutil/cuinfo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env python3
"""CUDA helpers
Usage:
miutil.cuinfo [options]
cuinfo [options]
Options:
-n, --num-devices : print number of devices (ignores `-d`)
-f, --nvcc-flags : print out flags for use nvcc compilation
-d ID, --dev-id ID : select device ID [default: None:int] for all
"""
from argopt import argopt
import pynvml
from argopt import argopt

__all__ = ["num_devices", "compute_capability", "memory", "name", "nvcc_flags"]

Expand Down
10 changes: 8 additions & 2 deletions miutil/fdio.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import logging
from contextlib import contextmanager
from os import makedirs, path
from shutil import rmtree
from tempfile import mkdtemp

log = logging.getLogger(__name__)


def create_dir(pth):
"""Equivalent of `mkdir -p`"""
if not path.exists(pth):
makedirs(pth)
if not path.isdir(pth):
try:
makedirs(pth)
except Exception as exc:
log.warn("cannot create:%s:%s" % (pth, exc))


def hasext(fname, ext):
Expand Down
6 changes: 3 additions & 3 deletions miutil/imio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
RE_NPYZ = re.compile(r"^(.+)(\.np[yz])$", flags=re.I)


def imread(fname):
def imread(fname, *args, **kwargs):
"""Read any supported filename"""
if RE_NII_GZ.search(fname):
from .nii import getnii

return getnii(fname)
return getnii(fname, *args, **kwargs)
elif RE_NPYZ.search(fname):
import numpy as np

res = np.load(fname)
res = np.load(fname, *args, **kwargs)
if hasattr(res, "keys") and len(res.keys()) == 1:
res = res[list(res.keys())[0]]
return res
Expand Down
4 changes: 2 additions & 2 deletions miutil/imio/nii.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""NIfTI I/O"""
from six import string_types
import gzip
import logging
import numbers
Expand All @@ -8,9 +7,10 @@

import nibabel as nib
import numpy as np
from six import string_types

from . import RE_NII_GZ
from ..fdio import create_dir, hasext
from . import RE_NII_GZ

RE_GZ = re.compile(r"^(.+)(\.gz)$", flags=re.I)
log = logging.getLogger(__name__)
Expand Down
10 changes: 5 additions & 5 deletions miutil/mlab.py → miutil/mlab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging
import re
import sys
from ast import literal_eval
from functools import lru_cache
from os import getenv, path
from subprocess import CalledProcessError, STDOUT, check_output
from subprocess import STDOUT, CalledProcessError, check_output
from textwrap import dedent
import logging
import re
import sys

from .fdio import tmpdir
from ..fdio import tmpdir

__all__ = ["get_engine"]
IS_WIN = any(sys.platform.startswith(i) for i in ["win32", "cygwin"])
Expand Down
53 changes: 53 additions & 0 deletions miutil/mlab/beautify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
"""Usage:
mbeautify <mfile>...
Arguments:
<mfile> : Path to `*.m` file
"""
import logging
from functools import lru_cache, wraps
from os import path
from zipfile import ZipFile

from argopt import argopt
from tqdm.contrib import tmap

from ..web import get_file
from . import get_engine

log = logging.getLogger(__name__)
MBEAUTIFIER_REV = "aad307c62532cd7c852aab9bac44fd6443b90bef"


@lru_cache()
@wraps(get_engine)
def ensure_mbeautifier(*args, **kwargs):
eng = get_engine(*args, **kwargs)
fn = get_file(
"MBeautifier-aad307c.zip",
"https://github.com/davidvarga/MBeautifier/archive/%s.zip" % MBEAUTIFIER_REV,
)
outpath = path.join(path.dirname(fn), "MBeautifier-%s" % MBEAUTIFIER_REV)
if not path.exists(outpath):
with ZipFile(fn) as fd:
fd.extractall(path=path.dirname(outpath))
assert path.exists(outpath), "Error extracting"
log.debug("adding wrappers (%s) to MATLAB path", outpath)
eng.addpath(outpath, nargout=0)
return eng


def main(*args, **kwargs):
args = argopt(__doc__).parse_args(*args, **kwargs)
logging.basicConfig(level=logging.INFO)
eng = ensure_mbeautifier()
formatter = eng.MBeautify.formatFileNoEditor

for fn in tmap(path.abspath, args.mfile):
log.debug("file:%s", fn)
formatter(fn, fn, nargout=0)


if __name__ == "__main__": # pragma: no cover
main()
35 changes: 31 additions & 4 deletions miutil/plot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
from os import path
from textwrap import dedent

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

from .imio import imread

show = plt.show # convenience: for use after `imscroll`


def apply_cmap(**kwargs):
"""Apply different cmaps to inputs an average the results.
Args:
**kwargs: map named cmap to ndarray
>>> vol1 = np.random.random((10, 10, 10))
>>> vol2 = np.random.random((10, 10, 10))
>>> res = apply_cmap(magma=vol1, bone=vol2)
>>> assert res.shape == (10, 10, 10, 4) # RGBA
>>> imscroll(res[None]) # (1, 10, 10, 10, 4) for (N, Z, Y, X, RGBA)
"""
res = None
for v in kwargs.values():
if res is None:
res = np.zeros(v.shape + (4,), dtype=np.float32)
else:
assert v.shape == res.shape[:-1], "all inputs must have same shape"
for k, v in kwargs.items():
cmap = getattr(cm, k)
res += cmap(v)
res /= len(kwargs)
return res


class imscroll:
"""
Expand Down Expand Up @@ -137,10 +167,7 @@ def _on_click(self, event):
]
else:
z = ndi.map_coordinates(
arr,
np.vstack((x, y)),
order=self.order,
mode="nearest",
arr, np.vstack((x, y)), order=self.order, mode="nearest"
)
self.picked = []
self.key["control"] = False
Expand Down
Loading

0 comments on commit cd553f7

Please sign in to comment.