Skip to content

Commit

Permalink
Officially rely on numpy as a dependency.
Browse files Browse the repository at this point in the history
This is unfortunately necessary as the base language doesn't support
typed arrays, so we need to include it to just become equivalent to R.
  • Loading branch information
LTLA committed Nov 7, 2023
1 parent a1cbc26 commit a9c07db
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ python_requires = >=3.8
# For more information, check out https://semver.org/.
install_requires =
importlib-metadata; python_version<"3.8"
numpy


[options.packages.find]
Expand All @@ -66,7 +67,6 @@ testing =
setuptools
pytest
pytest-cov
numpy

[options.entry_points]
# Add here console scripts like:
Expand Down
42 changes: 10 additions & 32 deletions src/biocutils/normalize_subscript.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
from typing import Optional, Sequence, Tuple, Union
import numpy


def _raise_int(idx: int, length):
raise IndexError(
"subscript ("
+ str(idx)
+ ") out of range for vector-like object of length "
+ str(length)
)
raise IndexError("subscript (" + str(idx) + ") out of range for vector-like object of length " + str(length))


has_numpy = False
try:
import numpy

has_numpy = True
except Exception:
pass
def _is_scalar_bool(sub):
return isinstance(sub, bool) or isinstance(sub, numpy.bool_)


def normalize_subscript(
Expand Down Expand Up @@ -64,15 +55,13 @@ def normalize_subscript(
specifying the subscript elements, and (ii) a boolean indicating whether
``sub`` was a scalar.
"""
if isinstance(sub, bool) or (
has_numpy and isinstance(sub, numpy.bool_)
): # before ints, as bools are ints.
if _is_scalar_bool(sub): # before ints, as bools are ints.
if sub:
return [0], True
else:
return [], False

if isinstance(sub, int) or (has_numpy and isinstance(sub, numpy.generic)):
if isinstance(sub, int) or isinstance(sub, numpy.integer):
if sub < -length or sub >= length:
_raise_int(sub, length)
if sub < 0 and non_negative_only:
Expand All @@ -81,11 +70,7 @@ def normalize_subscript(

if isinstance(sub, str):
if names is None:
raise IndexError(
"failed to find subscript '"
+ sub
+ "' for vector-like object with no names"
)
raise IndexError("failed to find subscript '" + sub + "' for vector-like object with no names")
return [names.index(sub)], True

if isinstance(sub, slice):
Expand Down Expand Up @@ -121,12 +106,7 @@ def normalize_subscript(

can_return_early = True
for x in sub:
if (
isinstance(x, str)
or isinstance(x, bool)
or (has_numpy and isinstance(x, numpy.bool_))
or (x < 0 and non_negative_only)
):
if isinstance(x, str) or _is_scalar_bool(x) or (x < 0 and non_negative_only):
can_return_early = False
break

Expand All @@ -144,7 +124,7 @@ def normalize_subscript(
has_strings.add(x)
string_positions.append(len(output))
output.append(None)
elif isinstance(x, bool) or (has_numpy and isinstance(x, numpy.bool_)):
elif _is_scalar_bool(x):
if x:
output.append(i)
elif x < 0:
Expand All @@ -158,9 +138,7 @@ def normalize_subscript(

if len(has_strings):
if names is None:
raise IndexError(
"cannot find string subscripts for vector-like object with no names"
)
raise IndexError("cannot find string subscripts for vector-like object with no names")

mapping = {}
for i, y in enumerate(names):
Expand Down
11 changes: 3 additions & 8 deletions src/biocutils/print_wrapped_table.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Optional, Sequence
import numpy

from .subset import subset

Expand Down Expand Up @@ -153,12 +154,6 @@ def print_type(x) -> str:
String containing the class of the object.
"""
cls = type(x).__name__

import sys

if "numpy" in sys.modules:
numpy = sys.modules["numpy"]
if isinstance(x, numpy.ndarray):
return cls + "[" + x.dtype.name + "]"

if isinstance(x, numpy.ndarray):
return cls + "[" + x.dtype.name + "]"
return cls

0 comments on commit a9c07db

Please sign in to comment.