Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fmt and lint checks #118

Merged
merged 3 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Lint

on: [push, pull_request]

jobs:
run-hooks:
name: Run pre-commit hooks
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.7

- name: Install Python dependencies
run: pip install pre-commit

- name: Run pre-commit hooks
run: pre-commit run --all-files
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: coverage run -m unittest discover -v nrrd/tests

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v2
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ MANIFEST
docs/build/

# coverage.py files
.coverage
.coverage
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exclude: "docs"

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer

- repo: https://github.com/asottile/pyupgrade
rev: v2.34.0
hooks:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/timothycrosley/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
args: ["--config=setup.cfg"]
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include AUTHORS
include README.md
include LICENSE
include LICENSE
14 changes: 9 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

pynrrd
======
pynrrd is a pure-Python module for reading and writing `NRRD <http://teem.sourceforge.net/nrrd/>`_ files into and
pynrrd is a pure-Python module for reading and writing `NRRD <http://teem.sourceforge.net/nrrd/>`_ files into and
from numpy arrays.

Dependencies
Expand All @@ -46,7 +46,7 @@ Install via pip and GitHub
.. code-block:: bash

pip install git+https://github.com/mhe/pynrrd.git

Install from source (recommended for contributing to pynrrd)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For developers that want to contribute to pynrrd, you can clone the pynrrd repository and install it using the following commands:
Expand Down Expand Up @@ -74,20 +74,24 @@ The tests can be run via the following command from the base directory:

python -m unittest discover -v nrrd/tests

**Format and Lint code**

This repository uses pre-commit hooks to run format and lint the code and they are enforced in CI. See [pre-commit](https://pre-commit.com)

Example usage
-------------
.. code-block:: python

import numpy as np
import nrrd

# Some sample numpy data
data = np.zeros((5,4,3,2))
filename = 'testdata.nrrd'

# Write to a NRRD file
nrrd.write(filename, data)

# Read the data back from file
readdata, header = nrrd.read(filename)
print(readdata.shape)
Expand Down
2 changes: 1 addition & 1 deletion nrrd/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def format_number(x):
# floating point number.
# The g option is used rather than f because g precision uses significant digits while f is just the number of
# digits after the decimal. (NRRD C implementation uses g).
value = '{:.17g}'.format(x)
value = f'{x:.17g}'
else:
value = str(x)

Expand Down
21 changes: 11 additions & 10 deletions nrrd/reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
import bz2
import os
import re
Expand Down Expand Up @@ -321,9 +320,9 @@ def read_data(header, fh=None, filename=None, index_order='F'):
Filename of the header file. Only necessary if data is detached from the header. This is used to get the
absolute data path.
index_order : {'C', 'F'}, optional
Specifies the index order of the resulting data array. Either 'C' (C-order) where the dimensions are ordered from
slowest-varying to fastest-varying (e.g. (z, y, x)), or 'F' (Fortran-order) where the dimensions are ordered
from fastest-varying to slowest-varying (e.g. (x, y, z)).
Specifies the index order of the resulting data array. Either 'C' (C-order) where the dimensions are ordered
from slowest-varying to fastest-varying (e.g. (z, y, x)), or 'F' (Fortran-order) where the dimensions are
ordered from fastest-varying to slowest-varying (e.g. (x, y, z)).

Returns
-------
Expand Down Expand Up @@ -452,8 +451,8 @@ def read_data(header, fh=None, filename=None, index_order='F'):
fh.close()

if total_data_points != data.size:
raise NRRDError('Size of the data does not equal the product of all the dimensions: {0}-{1}={2}'
.format(total_data_points, data.size, total_data_points - data.size))
raise NRRDError(f'Size of the data does not equal the product of all the dimensions: '
f'{total_data_points}-{data.size}={total_data_points - data.size}')

# In the NRRD header, the fields are specified in Fortran order, i.e, the first index is the one that changes
# fastest and last index changes slowest. This needs to be taken into consideration since numpy uses C-order
Expand All @@ -475,7 +474,9 @@ def read(filename, custom_field_map=None, index_order='F'):
See :ref:`user-guide:Reading NRRD files` for more information on reading NRRD files.

.. note::
Users should be aware that the `index_order` argument needs to be consistent between `nrrd.read` and `nrrd.write`. I.e., reading an array with `index_order='F'` will result in a transposed version of the original data and hence the writer needs to be aware of this.
Users should be aware that the `index_order` argument needs to be consistent between `nrrd.read` and
`nrrd.write`. I.e., reading an array with `index_order='F'` will result in a transposed version of the
original data and hence the writer needs to be aware of this.

Parameters
----------
Expand All @@ -485,9 +486,9 @@ def read(filename, custom_field_map=None, index_order='F'):
Dictionary used for parsing custom field types where the key is the custom field name and the value is a
string identifying datatype for the custom field.
index_order : {'C', 'F'}, optional
Specifies the index order of the resulting data array. Either 'C' (C-order) where the dimensions are ordered from
slowest-varying to fastest-varying (e.g. (z, y, x)), or 'F' (Fortran-order) where the dimensions are ordered
from fastest-varying to slowest-varying (e.g. (x, y, z)).
Specifies the index order of the resulting data array. Either 'C' (C-order) where the dimensions are ordered
from slowest-varying to fastest-varying (e.g. (z, y, x)), or 'F' (Fortran-order) where the dimensions are
ordered from fastest-varying to slowest-varying (e.g. (x, y, z)).

Returns
-------
Expand Down
1 change: 0 additions & 1 deletion nrrd/tests/data/test1d_ascii.nrrd
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ encoding: ASCII
25
26
27

1 change: 0 additions & 1 deletion nrrd/tests/data/test2d_ascii.nrrd
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ encoding: ASCII
19 20 21
22 23 24
25 26 27

1 change: 0 additions & 1 deletion nrrd/tests/data/test_customFields.nrrd
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ double matrix:= (1.2,0.3,0) (0,1.5,0) (0,-0.55,1.6)
25
26
27

9 changes: 5 additions & 4 deletions nrrd/tests/test_formatting.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import sys

# Required specifically in each module so that searches happen at the parent directory for importing modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

import numpy as np
from nrrd.tests.util import *

import nrrd
from nrrd.tests.util import *

# Required specifically in each module so that searches happen at the parent directory for importing modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
bernardopericacho marked this conversation as resolved.
Show resolved Hide resolved


class TestFieldFormatting(unittest.TestCase):
Expand Down
7 changes: 4 additions & 3 deletions nrrd/tests/test_parsing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

import numpy as np
from nrrd.tests.util import *

import nrrd
from nrrd.tests.util import *

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
bernardopericacho marked this conversation as resolved.
Show resolved Hide resolved


class TestFieldParsing(unittest.TestCase):
Expand Down
Loading