Skip to content

Commit

Permalink
Raise SpaceWarpingFailedError if Point/PointSet warping results in …
Browse files Browse the repository at this point in the history
…NaN or in between unsupported spaces
  • Loading branch information
AhmetNSimsek committed Oct 25, 2024
1 parent 1f35f6d commit 1e6020a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 5 additions & 4 deletions siibra/locations/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from ..commons import logger
from ..retrieval.requests import HttpRequest
from ..exceptions import SpaceWarpingFailedError

from urllib.parse import quote
import re
Expand Down Expand Up @@ -129,7 +130,7 @@ def warp(self, space):
if spaceobj == self.space:
return self
if any(_ not in location.Location.SPACEWARP_IDS for _ in [self.space.id, spaceobj.id]):
raise ValueError(
raise SpaceWarpingFailedError(
f"Cannot convert coordinates between {self.space.id} and {spaceobj.id}"
)
url = "{server}/transform-point?source_space={src}&target_space={tgt}&x={x}&y={y}&z={z}".format(
Expand All @@ -141,9 +142,9 @@ def warp(self, space):
z=self.coordinate[2],
)
response = HttpRequest(url, lambda b: json.loads(b.decode())).get()
if any(map(np.isnan, response['target_point'])):
logger.info(f'Warping {str(self)} to {spaceobj.name} resulted in NaN')
return None
if np.any(np.isnan(response['target_point'])):
raise SpaceWarpingFailedError(f'Warping {str(self)} to {spaceobj.name} resulted in NaN')

return self.__class__(
coordinatespec=tuple(response["target_point"]),
space=spaceobj.id,
Expand Down
7 changes: 6 additions & 1 deletion siibra/locations/pointset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from ..retrieval.requests import HttpRequest
from ..commons import logger
from ..exceptions import SpaceWarpingFailedError

from typing import List, Union, Tuple
import numbers
Expand Down Expand Up @@ -149,7 +150,7 @@ def warp(self, space, chunksize=1000):
if spaceobj == self.space:
return self
if any(_ not in location.Location.SPACEWARP_IDS for _ in [self.space.id, spaceobj.id]):
raise ValueError(
raise SpaceWarpingFailedError(
f"Cannot convert coordinates between {self.space.id} and {spaceobj.id}"
)

Expand Down Expand Up @@ -178,6 +179,10 @@ def warp(self, space, chunksize=1000):
).data
tgt_points.extend(list(response["target_points"]))

# TODO: consider using np.isnan(np.dot(arr, arr)). see https://stackoverflow.com/a/45011547
if np.any(np.isnan(response['target_points'])):
raise SpaceWarpingFailedError(f'Warping {str(self)} to {spaceobj.name} resulted in NaN')

return self.__class__(coordinates=tuple(tgt_points), space=spaceobj, labels=self.labels)

def transform(self, affine: np.ndarray, space=None):
Expand Down

0 comments on commit 1e6020a

Please sign in to comment.