Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Sep 19, 2022
1 parent 761efc5 commit 4335361
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
14 changes: 8 additions & 6 deletions python/lance/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
import lance
from lance.types import (
Box2dArray,
Box3dArray,
Box2dType,
Box3dArray,
Box3dType,
Image,
ImageArray,
ImageBinary,
ImageType,
ImageUri,
LabelArray,
LabelType,
Point2dType, Box3dType, Point3dType,
Point2dType,
Point3dType,
)

if platform.system() != "Linux":
Expand Down Expand Up @@ -62,8 +64,7 @@ def test_image_array():


def test_image_array_chunks():
images = [pa.array(["uri1", "uri2"]),
pa.array(["uri3", "uri4"])]
images = [pa.array(["uri1", "uri2"]), pa.array(["uri3", "uri4"])]
chunks = pa.chunked_array(images, pa.string())
arr = ImageArray.from_pandas(chunks)
assert isinstance(arr, pa.ChunkedArray)
Expand Down Expand Up @@ -120,8 +121,9 @@ def _check_points(ext_arr, reshaped, ndims):

def _check_size(ext_arr, reshaped, ndims):
actual_sizes = ext_arr._box_sizes()
expected_size = ((reshaped[:, ndims] - reshaped[:, 0] + 1) *
(reshaped[:, ndims + 1] - reshaped[:, 1] + 1))
expected_size = (reshaped[:, ndims] - reshaped[:, 0] + 1) * (
reshaped[:, ndims + 1] - reshaped[:, 1] + 1
)
if ndims == 3:
expected_size *= reshaped[:, ndims + 2] - reshaped[:, 2] + 1
assert np.all(actual_sizes == expected_size)
Expand Down
8 changes: 3 additions & 5 deletions python/lance/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import pyarrow as pa
from pyarrow import ArrowKeyError

from lance.types.base import (
Point2dType, Point3dType, Polygon2dType, Polygon3dType
)
from lance.types.box import Box2dArray, Box3dArray, Box2dType, Box3dType
from lance.types.base import Point2dType, Point3dType, Polygon2dType, Polygon3dType
from lance.types.box import Box2dArray, Box2dType, Box3dArray, Box3dType
from lance.types.image import (
Image,
ImageArray,
Expand All @@ -43,7 +41,7 @@ def register_extension_types():
Point3dType(),
Box2dType(),
Box3dType(),
LabelType()
LabelType(),
]
for t in types:
try:
Expand Down
6 changes: 2 additions & 4 deletions python/lance/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class Polygon2dType(LanceType):

def __init__(self):
super(Polygon2dType, self).__init__(
pa.list_(pa.list_(pa.float64(), list_size=2)),
"polygon2d"
pa.list_(pa.list_(pa.float64(), list_size=2)), "polygon2d"
)

@property
Expand All @@ -100,8 +99,7 @@ class Polygon3dType(LanceType):

def __init__(self):
super(Polygon3dType, self).__init__(
pa.list_(pa.list_(pa.float64(), list_size=3)),
"polygon3d"
pa.list_(pa.list_(pa.float64(), list_size=3)), "polygon3d"
)

@property
Expand Down
36 changes: 19 additions & 17 deletions python/lance/types/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import abstractmethod, ABC
from abc import ABC, abstractmethod
from typing import Union

import numpy as np
Expand Down Expand Up @@ -76,7 +76,6 @@ def __arrow_ext_serialize__(self):


class BoxNdArray(pa.ExtensionArray, ABC):

@property
@abstractmethod
def ndims(self):
Expand Down Expand Up @@ -141,31 +140,32 @@ def get_max(self, axis: Union[int, str]):

def _sanitize_axis(self, axis):
if isinstance(axis, str):
axis = {'x': 0, 'y': 1, 'z': 2}[axis]
axis = {"x": 0, "y": 1, "z": 2}[axis]
if axis < 0 or axis >= self.ndims:
raise ValueError(f"Axis {axis} is greater the number of box "
f"dimensions in this array")
raise ValueError(
f"Axis {axis} is greater the number of box " f"dimensions in this array"
)
return axis

@property
def xmin(self) -> np.ndarray:
"""Return a numpy array of the min X-coord of each box"""
return self.get_min('x')
return self.get_min("x")

@property
def ymin(self) -> np.ndarray:
"""Return a numpy array of the min Y-coord of each box"""
return self.get_min('y')
return self.get_min("y")

@property
def xmax(self) -> np.ndarray:
"""Return a numpy array of the max X-coord of each box"""
return self.get_max('x')
return self.get_max("x")

@property
def ymax(self) -> np.ndarray:
"""Return a numpy array of the max Y-coord of each box"""
return self.get_max('y')
return self.get_max("y")

def _box_sizes(self):
sizes = self.get_length(0)
Expand Down Expand Up @@ -194,12 +194,14 @@ def iou(self, others: "BoxNdArray") -> np.ndarray:
else:
size_others = others._box_sizes()

min_inter = [np.maximum(self.get_min(axis)[:, np.newaxis],
others.get_min(axis))
for axis in range(self.ndims)]
max_inter = [np.minimum(self.get_max(axis)[:, np.newaxis],
others.get_max(axis))
for axis in range(self.ndims)]
min_inter = [
np.maximum(self.get_min(axis)[:, np.newaxis], others.get_min(axis))
for axis in range(self.ndims)
]
max_inter = [
np.minimum(self.get_max(axis)[:, np.newaxis], others.get_max(axis))
for axis in range(self.ndims)
]
intersection = np.maximum(max_inter[0] - min_inter[0] + 1, 0)
for i in range(1, self.ndims):
intersection *= np.maximum(max_inter[i] - min_inter[i] + 1, 0)
Expand Down Expand Up @@ -233,9 +235,9 @@ def volume(self) -> np.ndarray:
@property
def zmin(self) -> np.ndarray:
"""Return a numpy array of the min Z-coord of each box"""
return self.get_min('z')
return self.get_min("z")

@property
def zmax(self) -> np.ndarray:
"""Return a numpy array of the max Z-coord of each box"""
return self.get_max('z')
return self.get_max("z")
18 changes: 12 additions & 6 deletions python/lance/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def arrow_dtype(self):
def display(self, **kwargs):
"""Return the jupyter compatible viz for this image"""
import base64

from IPython.display import Image as IPyImage

with self.open() as fobj:
Expand All @@ -173,6 +174,7 @@ class ImageBinary(Image):
"""
An In-memory Image
"""

DTYPE = ImageBinaryType()

def __init__(self, data: bytes):
Expand Down Expand Up @@ -215,6 +217,7 @@ class ImageUri(Image):
"""
An externalized image represented by its uri
"""

DTYPE = ImageUriType()

def __init__(self, uri: str):
Expand Down Expand Up @@ -306,12 +309,14 @@ def from_pandas(obj, mask=None, type=None, safe=True, memory_pool=None):
if isinstance(first, Image):
return ImageArray.from_images(obj, mask, type, safe, memory_pool)
elif isinstance(first, bytes):
storage = pa.array(obj, mask=mask, type=pa.binary(),
safe=safe, memory_pool=memory_pool)
storage = pa.array(
obj, mask=mask, type=pa.binary(), safe=safe, memory_pool=memory_pool
)
return ImageArray.from_pandas(storage)
elif isinstance(first, str):
storage = pa.array(obj, mask=mask, type=pa.string(),
safe=safe, memory_pool=memory_pool)
storage = pa.array(
obj, mask=mask, type=pa.string(), safe=safe, memory_pool=memory_pool
)
return ImageArray.from_pandas(storage)

return pa.ExtensionArray.from_pandas(
Expand Down Expand Up @@ -346,8 +351,9 @@ def from_images(images, type=None, mask=None, safe=True, memory_pool=None):
memory_pool=memory_pool,
)
else:
storage = pa.array([], type=type, mask=mask,
safe=safe, memory_pool=memory_pool)
storage = pa.array(
[], type=type, mask=mask, safe=safe, memory_pool=memory_pool
)
return pa.ExtensionArray.from_storage(type, storage)


Expand Down

0 comments on commit 4335361

Please sign in to comment.