Skip to content

Commit

Permalink
bump version, merge pull request #26 from AMYPAD/devel
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl authored Oct 30, 2021
2 parents 14ea116 + 601c0f3 commit f54cf4a
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 189 deletions.
32 changes: 18 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
name: Test
on: [push, pull_request]
on:
push:
pull_request:
schedule:
- cron: '15 23 * * 6' # M H d m w (Sat at 23:15)
jobs:
check:
if: github.event_name != 'pull_request' || github.head_ref != 'devel'
if: github.event_name != 'pull_request' || github.repository_owner != 'AMYPAD'
runs-on: ubuntu-latest
name: Check
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-python@v2
- name: set PYSHA
run: echo "PYSHA=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
Expand All @@ -19,7 +20,7 @@ jobs:
- name: dependencies
run: pip install -U pre-commit
- uses: reviewdog/action-setup@v1
- if: github.event_name != 'schedule'
- if: github.event_name == 'push' || github.event_name == 'pull_request'
name: comment
run: |
if [[ $EVENT == pull_request ]]; then
Expand All @@ -34,12 +35,12 @@ jobs:
EVENT: ${{ github.event_name }}
- run: pre-commit run -a --show-diff-on-failure
test:
if: github.event_name != 'pull_request' || github.head_ref != 'devel'
if: github.event_name != 'pull_request' || github.repository_owner != 'AMYPAD'
name: py${{ matrix.python }}
runs-on: ubuntu-latest
strategy:
matrix:
python: [2.7, 3.6, 3.9]
name: Test py${{ matrix.python }}
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -52,22 +53,25 @@ jobs:
run: pytest --durations-min=1
- if: startsWith(matrix.python, '2')
run: pytest
- run: codecov
- uses: codecov/codecov-action@v1
cuda_matlab:
if: github.event_name != 'pull_request' || github.head_ref != 'devel'
if: github.event_name != 'pull_request' || github.repository_owner != 'AMYPAD'
name: CUDA MATLAB py${{ matrix.python }}
runs-on: [self-hosted, python, cuda, matlab]
name: Test cuda & matlab
strategy:
matrix:
python: [3.7, 3.9]
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Run setup-python
run: setup-python -p3.7
run: setup-python -p${{ matrix.python }}
- run: pip install -U .[dev,nii,cuda,web,mbeautify]
- run: pytest --durations-min=1
- run: codecov
- uses: codecov/codecov-action@v1
- name: Post Run setup-python
run: setup-python -p3.7 -Dr
run: setup-python -p${{ matrix.python }} -Dr
if: ${{ always() }}
deploy:
needs: [check, test, cuda_matlab]
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ repos:
- flake8-comprehensions
- flake8-debugger
- flake8-string-format
- repo: https://github.com/psf/black
rev: 21.8b0
- repo: https://github.com/google/yapf
rev: v0.31.0
hooks:
- id: black
- id: yapf
args: [-i]
- repo: https://github.com/PyCQA/isort
rev: 5.9.3
hooks:
Expand Down
2 changes: 1 addition & 1 deletion miutil/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .fdio import * # NOQA
from .fdio import * # yapf: disable # NOQA

# version detector. Precedence: installed dist, git, 'UNKNOWN'
try:
Expand Down
16 changes: 6 additions & 10 deletions miutil/cuinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
def nvmlDeviceGetCudaComputeCapability(handle):
major = pynvml.c_int()
minor = pynvml.c_int()
try: # pynvml>=11
try: # pynvml>=11
get_fn = pynvml.nvml._nvmlGetFunctionPointer
except AttributeError:
get_fn = pynvml.get_func_pointer
fn = get_fn("nvmlDeviceGetCudaComputeCapability")
ret = fn(handle, pynvml.byref(major), pynvml.byref(minor))
try: # pynvml>=11
try: # pynvml>=11
check_ret = pynvml.nvml._nvmlCheckReturn
except AttributeError:
check_ret = pynvml.check_return
Expand Down Expand Up @@ -66,8 +66,7 @@ def name(dev_id=-1):

def nvcc_flags(dev_id=-1):
return "-gencode=arch=compute_{0:d}{1:d},code=compute_{0:d}{1:d}".format(
*compute_capability(dev_id)
)
*compute_capability(dev_id))


def main(*args, **kwargs):
Expand All @@ -86,12 +85,9 @@ def main(*args, **kwargs):
noargs = False
if noargs:
for dev_id in devices:
print(
"Device {:2d}:{}:compute capability:{:d}.{:d}".format(
dev_id, name(dev_id), *compute_capability(dev_id)
)
)
print("Device {:2d}:{}:compute capability:{:d}.{:d}".format(
dev_id, name(dev_id), *compute_capability(dev_id)))


if __name__ == "__main__": # pragma: no cover
if __name__ == "__main__": # pragma: no cover
main()
13 changes: 6 additions & 7 deletions miutil/fdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def extractall(fzip, dest, desc="Extracting"):
"""zipfile.Zipfile(fzip).extractall(dest) with progress"""
dest = Path(dest).expanduser()
with ZipFile(fzip) as zipf, tqdm(
desc=desc,
unit="B",
unit_scale=True,
unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
desc=desc,
unit="B",
unit_scale=True,
unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
) as pbar:
for i in zipf.infolist():
if not getattr(i, "file_size", 0): # directory
if not getattr(i, "file_size", 0): # directory
zipf.extract(i, fspath(dest))
else:
(dest / i.filename).parent.mkdir(parents=True, exist_ok=True)
Expand All @@ -78,7 +78,6 @@ def extractall(fzip, dest, desc="Extracting"):

def nsort(fnames):
"""Sort a file list, automatically detecting embedded numbers"""

def path2parts(fname):
parts = re.split(r"([0-9][0-9.]*e[-+][0-9]+|[0-9]+\.[0-9]+|[0-9]+)", fname)
parts[1::2] = map(float, parts[1::2])
Expand Down
56 changes: 20 additions & 36 deletions miutil/imio/nii.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import numbers
import os.path
import re
import sys

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

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

RE_GZ = re.compile(r"^(.+)(\.gz)$", flags=re.I)
log = logging.getLogger(__name__)
if sys.version_info[0] < 3:
string_types = basestring, # NOQA: F821
else:
string_types = str,


def file_parts(fname, regex=RE_NII_GZ):
Expand Down Expand Up @@ -86,22 +90,20 @@ def getnii(fim, nan_replace=None, output="image"):
flip = tuple(np.int8(ornt[:, 1]))

# > voxel size
voxsize = nim.header.get("pixdim")[1 : nim.header.get("dim")[0] + 1]
voxsize = nim.header.get("pixdim")[1:nim.header.get("dim")[0] + 1]
# > rearrange voxel size according to the orientation
voxsize = voxsize[np.array(trnsp)]

# > dimensions
dims = dim[1 : nim.header.get("dim")[0] + 1]
dims = dim[1:nim.header.get("dim")[0] + 1]
dims = dims[np.array(trnsp)]

# > flip y-axis and z-axis and then transpose.
# Depends if dynamic (4 dimensions) or static (3 dimensions)
if dimno == 4:
imr = np.transpose(
imr[:: -flip[0], :: -flip[1], :: -flip[2], :], (3,) + trnsp
)
imr = np.transpose(imr[::-flip[0], ::-flip[1], ::-flip[2], :], (3,) + trnsp)
elif dimno == 3:
imr = np.transpose(imr[:: -flip[0], :: -flip[1], :: -flip[2]], trnsp)
imr = np.transpose(imr[::-flip[0], ::-flip[1], ::-flip[2]], trnsp)

if output == "affine" or output == "all":
# A = nim.get_sform()
Expand All @@ -111,17 +113,8 @@ def getnii(fim, nan_replace=None, output="image"):

if output == "all":
out = {
"im": imr,
"affine": A,
"fim": fim,
"dtype": nim.get_data_dtype(),
"shape": imr.shape,
"hdr": nim.header,
"voxsize": voxsize,
"dims": dims,
"transpose": trnsp,
"flip": flip,
}
"im": imr, "affine": A, "fim": fim, "dtype": nim.get_data_dtype(), "shape": imr.shape,
"hdr": nim.header, "voxsize": voxsize, "dims": dims, "transpose": trnsp, "flip": flip}
elif output == "image":
out = imr
elif output == "affine":
Expand Down Expand Up @@ -161,11 +154,7 @@ def array2nii(im, A, fnii, descrip="", trnsp=None, flip=None, storage_as=None):
# >>as obtained from getnii(..., output='all')

# > permute the axis order in the image array
if (
isinstance(storage_as, dict)
and "transpose" in storage_as
and "flip" in storage_as
):
if (isinstance(storage_as, dict) and "transpose" in storage_as and "flip" in storage_as):

trnsp = (
storage_as["transpose"].index(0),
Expand All @@ -186,12 +175,12 @@ def array2nii(im, A, fnii, descrip="", trnsp=None, flip=None, storage_as=None):

# > perform flip of x,y,z axes after transposition into proper NIfTI order
if len(flip) == 3:
im = im[:: -flip[0], :: -flip[1], :: -flip[2], ...]
im = im[::-flip[0], ::-flip[1], ::-flip[2], ...]

res = nib.Nifti1Image(im, A)
hdr = res.header
hdr.set_sform(None, code="scanner")
hdr["cal_max"] = np.max(im) # np.percentile(im, 90) #
hdr["cal_max"] = np.max(im) # np.percentile(im, 90) #
hdr["cal_min"] = np.min(im)
hdr["descrip"] = descrip
nib.save(res, fspath(fnii))
Expand Down Expand Up @@ -266,12 +255,8 @@ def niisort(fims, memlim=True):
raise ValueError("Input image(s) must be 3D.")

out = {
"shape": _nii.shape[::-1],
"files": _fims,
"sortlist": sortlist,
"dtype": _nii.get_data_dtype(),
"N": Nim,
}
"shape": _nii.shape[::-1], "files": _fims, "sortlist": sortlist,
"dtype": _nii.get_data_dtype(), "N": Nim}

if memlim and Nfrm > 50:
imdic = getnii(_fims[0], output="all")
Expand Down Expand Up @@ -328,12 +313,11 @@ def nii_modify(nii_fd, fimout="", outpath="", fcomment="", voxel_range=None):
log.debug("output floating and affine file names:%s", fout)
fout = os.path.join(opth, fout)

if len(voxel_range) == 1: # set max value
if len(voxel_range) == 1: # set max value
im = voxel_range[0] * dctnii["im"] / np.max(dctnii["im"])
elif len(voxel_range) == 2: # set range
im = (dctnii["im"] - np.min(dctnii["im"])) * (
np.ptp(voxel_range) / np.ptp(dctnii["im"])
) + voxel_range[0]
elif len(voxel_range) == 2: # set range
im = (dctnii["im"] - np.min(dctnii["im"])) * (np.ptp(voxel_range) /
np.ptp(dctnii["im"])) + voxel_range[0]
else:
return None

Expand Down
Loading

0 comments on commit f54cf4a

Please sign in to comment.