Skip to content

Commit

Permalink
Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardopericacho committed Jun 16, 2022
1 parent 7c5567c commit 794d24d
Show file tree
Hide file tree
Showing 16 changed files with 824 additions and 1,377 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ repos:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black

- repo: https://github.com/timothycrosley/isort
rev: 5.10.1
hooks:
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The tests can be run via the following command from the base directory:
**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)
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
-------------
Expand Down
23 changes: 4 additions & 19 deletions nrrd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,7 @@
from nrrd.reader import read, read_data, read_header
from nrrd.writer import write

__all__ = [
"read",
"read_data",
"read_header",
"write",
"format_number_list",
"format_number",
"format_matrix",
"format_optional_matrix",
"format_optional_vector",
"format_vector",
"parse_matrix",
"parse_number_auto_dtype",
"parse_number_list",
"parse_optional_matrix",
"parse_optional_vector",
"parse_vector",
"__version__",
]
__all__ = ['read', 'read_data', 'read_header', 'write', 'format_number_list', 'format_number', 'format_matrix',
'format_optional_matrix', 'format_optional_vector', 'format_vector', 'parse_matrix',
'parse_number_auto_dtype', 'parse_number_list', 'parse_optional_matrix', 'parse_optional_vector',
'parse_vector', '__version__']
2 changes: 1 addition & 1 deletion nrrd/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.3"
__version__ = '0.4.3'
1 change: 0 additions & 1 deletion nrrd/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class NRRDError(Exception):
"""Exceptions for NRRD class."""

pass
12 changes: 6 additions & 6 deletions 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 = f"{x:.17g}"
value = f'{x:.17g}'
else:
value = str(x)

Expand All @@ -54,7 +54,7 @@ def format_vector(x):
String containing NRRD vector
"""

return "(" + ",".join([format_number(y) for y in x]) + ")"
return '(' + ','.join([format_number(y) for y in x]) + ')'


def format_optional_vector(x):
Expand All @@ -79,7 +79,7 @@ def format_optional_vector(x):
# If vector is None or all elements are NaN, then return none
# Otherwise format the vector as normal
if x is None or np.all(np.isnan(x)):
return "none"
return 'none'
else:
return format_vector(x)

Expand All @@ -100,7 +100,7 @@ def format_matrix(x):
String containing NRRD matrix
"""

return " ".join([format_vector(y) for y in x])
return ' '.join([format_vector(y) for y in x])


def format_optional_matrix(x):
Expand All @@ -126,7 +126,7 @@ def format_optional_matrix(x):
String containing NRRD matrix
"""

return " ".join([format_optional_vector(y) for y in x])
return ' '.join([format_optional_vector(y) for y in x])


def format_number_list(x):
Expand All @@ -145,4 +145,4 @@ def format_number_list(x):
String containing NRRD list
"""

return " ".join([format_number(y) for y in x])
return ' '.join([format_number(y) for y in x])
24 changes: 9 additions & 15 deletions nrrd/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def parse_vector(x, dtype=None):
Vector that is parsed from the :obj:`x` string
"""

if x[0] != "(" or x[-1] != ")":
raise NRRDError("Vector should be enclosed by parentheses.")
if x[0] != '(' or x[-1] != ')':
raise NRRDError('Vector should be enclosed by parentheses.')

# Always convert to float and then truncate to integer if desired
# The reason why is parsing a floating point string to int will fail (i.e. int('25.1') will fail)
vector = np.array([float(x) for x in x[1:-1].split(",")])
vector = np.array([float(x) for x in x[1:-1].split(',')])

# If using automatic datatype detection, then start by converting to float and determining if the number is whole
# Truncate to integer if dtype is int also
Expand All @@ -41,9 +41,7 @@ def parse_vector(x, dtype=None):
elif dtype == int:
vector = vector.astype(int)
elif dtype != float:
raise NRRDError(
"dtype should be None for automatic type detection, float or int"
)
raise NRRDError('dtype should be None for automatic type detection, float or int')

return vector

Expand Down Expand Up @@ -72,7 +70,7 @@ def parse_optional_vector(x, dtype=None):
Vector that is parsed from the :obj:`x` string or :obj:`None` if :obj:`x` is 'none'
"""

if x == "none":
if x == 'none':
return None
else:
return parse_vector(x, dtype)
Expand Down Expand Up @@ -105,7 +103,7 @@ def parse_matrix(x, dtype=None):
# Get the size of each row vector and then remove duplicate sizes
# There should be exactly one value in the matrix because all row sizes need to be the same
if len(np.unique([len(x) for x in matrix])) != 1:
raise NRRDError("Matrix should have same number of elements in each row")
raise NRRDError('Matrix should have same number of elements in each row')

matrix = np.vstack(matrix)

Expand All @@ -119,9 +117,7 @@ def parse_matrix(x, dtype=None):
elif dtype == int:
matrix = matrix.astype(int)
elif dtype != float:
raise NRRDError(
"dtype should be None for automatic type detection, float or int"
)
raise NRRDError('dtype should be None for automatic type detection, float or int')

return matrix

Expand Down Expand Up @@ -159,7 +155,7 @@ def parse_optional_matrix(x):
unique_sizes = np.unique(sizes)

if len(unique_sizes) != 1 and (len(unique_sizes) != 2 or unique_sizes.min() != 0):
raise NRRDError("Matrix should have same number of elements in each row")
raise NRRDError('Matrix should have same number of elements in each row')

# Create a vector row of NaN's that matches same size of remaining vector rows
# Stack the vector rows together to create matrix
Expand Down Expand Up @@ -203,9 +199,7 @@ def parse_number_list(x, dtype=None):
elif dtype == int:
number_list = number_list.astype(int)
elif dtype != float:
raise NRRDError(
"dtype should be None for automatic type detection, float or int"
)
raise NRRDError('dtype should be None for automatic type detection, float or int')

return number_list

Expand Down
Loading

0 comments on commit 794d24d

Please sign in to comment.