Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomsaraiva committed Oct 16, 2023
2 parents 6ca50cc + f3e249c commit d62beb0
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 243 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test = [
"pytest-sugar>=0.9.6",
"pytest-cython>=0.2.0",
"nanopyx[jupyter]",
"numba"
]
jupyter = [
"nbformat>=4.2.0",
Expand Down
17 changes: 3 additions & 14 deletions src/nanopyx/__liquid_engine__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,6 @@ def _store_results(self, arg_repr, arg_score, run_type, t2run):
Stores the results of a run
"""

# Re-read the benchmark file in case it has been updated
try:
with open(self._benchmark_filepath) as f:
self._benchmarks = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError:
self._benchmarks = self._benchmarks

# Check if the run type has been run, and if not create empty info
run_type_benchs = self._benchmarks[run_type]
if arg_repr not in run_type_benchs:
Expand All @@ -284,19 +277,15 @@ def _store_results(self, arg_repr, arg_score, run_type, t2run):
c = run_type_benchs[arg_repr]

assert c[0] == arg_score, "arg_score mismatch"

# if run failed, t2run is np.inf
if np.isinf(t2run):
c.append(np.nan)
else:
c.append(t2run)

c.append(t2run)

self._dump_run_times()

def _dump_run_times(
self,
):
"""We might need to wrap this into a multiprocessing.Queue if we find it blocking"""
# TODO We might need to wrap this into a multiprocessing.Queue if we find it blocking
with open(self._benchmark_filepath, "w") as f:
yaml.dump(self._benchmarks, f)

Expand Down
9 changes: 6 additions & 3 deletions src/nanopyx/core/analysis/cross_correlation_elastic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from math import sqrt
from skimage.filters import gaussian
from scipy.interpolate import interp2d
from scipy.interpolate import bisplrep, bisplev # replacement for interp2d see: https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff

from .ccm import calculate_ccm_from_ref
from .estimate_shift import GetMaxOptimizer
Expand Down Expand Up @@ -252,8 +252,11 @@ def calculate_translation_mask_vector_field(

y_translation = np.array(y_translation)
x_translation = np.array(x_translation)
y_interp = interp2d(y_translation[:, 0], y_translation[:, 1], y_translation[:, 2])
x_interp = interp2d(x_translation[:, 0], x_translation[:, 1], x_translation[:, 2])

y_spline = bisplrep(y_translation[:, 0], y_translation[:, 1], y_translation[:, 2],kx=1, ky=1, s=0) # bspline representation
x_spline = bisplrep(x_translation[:, 0], x_translation[:, 1], x_translation[:, 2],kx=1, ky=1, s=0)
x_interp = lambda x, y: bisplev(x, y, x_spline).T # bspline evaluation
y_interp = lambda x, y: bisplev(x, y, y_spline).T

translation_matrix = np.zeros((height, width * 2))
translation_matrix_x = np.zeros((height, width))
Expand Down
74 changes: 33 additions & 41 deletions src/nanopyx/core/transform/_le_interpolation_bicubic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class ShiftAndMagnify(LiquidEngine):
:return: The shifted and magnified image
"""
image = check_image(image)
shift_row = value2array(shift_row, image.shape[0])
shift_col = value2array(shift_col, image.shape[0])
return self._run(image, shift_row, shift_col, magnification_row, magnification_col, run_type=run_type)
# tag-end

Expand All @@ -70,8 +68,6 @@ class ShiftAndMagnify(LiquidEngine):
:rtype: [[run_time, run_type_name, return_value], ...]
"""
image = check_image(image)
shift_row = value2array(shift_row, image.shape[0])
shift_col = value2array(shift_col, image.shape[0])
return super().benchmark(image, shift_row, shift_col, magnification_row, magnification_col)
# tag-end

Expand Down Expand Up @@ -109,8 +105,8 @@ class ShiftAndMagnify(LiquidEngine):
self.get_work_group(dc, (n_slices, image.shape[1]*magnification_row, image.shape[2]*magnification_col)),
input_opencl,
output_opencl,
np.float32(shift_row[0]),
np.float32(shift_col[0]),
np.float32(shift_row),
np.float32(shift_col),
np.float32(magnification_row),
np.float32(magnification_col)).wait()

Expand All @@ -127,7 +123,7 @@ class ShiftAndMagnify(LiquidEngine):
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftAndMagnify._run_unthreaded
def _run_unthreaded(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float magnification_row, float magnification_col) -> np.ndarray:
def _run_unthreaded(self, float[:,:,:] image, float shift_row, float shift_col, float magnification_row, float magnification_col) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand All @@ -144,16 +140,16 @@ class ShiftAndMagnify(LiquidEngine):
with nogil:
for f in range(nFrames):
for j in range(colsM):
col = j / magnification_col - shift_col[f]
col = j / magnification_col - shift_col
for i in range(rowsM):
row = i / magnification_row - shift_row[f]
row = i / magnification_row - shift_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftAndMagnify._run_unthreaded; replace("_run_unthreaded", "_run_threaded"); replace("range(colsM)", "prange(colsM)")
def _run_threaded(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float magnification_row, float magnification_col) -> np.ndarray:
def _run_threaded(self, float[:,:,:] image, float shift_row, float shift_col, float magnification_row, float magnification_col) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand All @@ -170,16 +166,16 @@ class ShiftAndMagnify(LiquidEngine):
with nogil:
for f in range(nFrames):
for j in prange(colsM):
col = j / magnification_col - shift_col[f]
col = j / magnification_col - shift_col
for i in range(rowsM):
row = i / magnification_row - shift_row[f]
row = i / magnification_row - shift_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftAndMagnify._run_unthreaded; replace("_run_unthreaded", "_run_threaded_static"); replace("range(colsM)", 'prange(colsM, schedule="static")')
def _run_threaded_static(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float magnification_row, float magnification_col) -> np.ndarray:
def _run_threaded_static(self, float[:,:,:] image, float shift_row, float shift_col, float magnification_row, float magnification_col) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand All @@ -196,16 +192,16 @@ class ShiftAndMagnify(LiquidEngine):
with nogil:
for f in range(nFrames):
for j in prange(colsM, schedule="static"):
col = j / magnification_col - shift_col[f]
col = j / magnification_col - shift_col
for i in range(rowsM):
row = i / magnification_row - shift_row[f]
row = i / magnification_row - shift_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftAndMagnify._run_unthreaded; replace("_run_unthreaded", "_run_threaded_dynamic"); replace("range(colsM)", 'prange(colsM, schedule="dynamic")')
def _run_threaded_dynamic(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float magnification_row, float magnification_col) -> np.ndarray:
def _run_threaded_dynamic(self, float[:,:,:] image, float shift_row, float shift_col, float magnification_row, float magnification_col) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand All @@ -222,16 +218,16 @@ class ShiftAndMagnify(LiquidEngine):
with nogil:
for f in range(nFrames):
for j in prange(colsM, schedule="dynamic"):
col = j / magnification_col - shift_col[f]
col = j / magnification_col - shift_col
for i in range(rowsM):
row = i / magnification_row - shift_row[f]
row = i / magnification_row - shift_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftAndMagnify._run_unthreaded; replace("_run_unthreaded", "_run_threaded_guided"); replace("range(colsM)", 'prange(colsM, schedule="guided")')
def _run_threaded_guided(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float magnification_row, float magnification_col) -> np.ndarray:
def _run_threaded_guided(self, float[:,:,:] image, float shift_row, float shift_col, float magnification_row, float magnification_col) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand All @@ -248,9 +244,9 @@ class ShiftAndMagnify(LiquidEngine):
with nogil:
for f in range(nFrames):
for j in prange(colsM, schedule="guided"):
col = j / magnification_col - shift_col[f]
col = j / magnification_col - shift_col
for i in range(rowsM):
row = i / magnification_row - shift_row[f]
row = i / magnification_row - shift_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
Expand Down Expand Up @@ -287,8 +283,6 @@ class ShiftScaleRotate(LiquidEngine):
:return: The shifted, magnified and rotated image
"""
image = check_image(image)
shift_row = value2array(shift_row, image.shape[0])
shift_col = value2array(shift_col, image.shape[0])
return self._run(image, shift_row, shift_col, scale_row, scale_col, angle, run_type=run_type)
# tag-end

Expand All @@ -312,8 +306,6 @@ class ShiftScaleRotate(LiquidEngine):
:rtype: [[run_time, run_type_name, return_value], ...]
"""
image = check_image(image)
shift_row = value2array(shift_row, image.shape[0])
shift_col = value2array(shift_col, image.shape[0])
return super().benchmark(image, shift_row, shift_col, scale_row, scale_col, angle)
# tag-end

Expand Down Expand Up @@ -367,11 +359,11 @@ class ShiftScaleRotate(LiquidEngine):
input_opencl.release()
output_opencl.release()

return image_out
return np.asarray(image_out, dtype=np.float32)
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftScaleRotate._run_unthreaded
def _run_unthreaded(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
def _run_unthreaded(self, float[:,:,:] image, float shift_row, float shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand Down Expand Up @@ -399,15 +391,15 @@ class ShiftScaleRotate(LiquidEngine):
for f in range(nFrames):
for j in range(cols):
for i in range(rows):
col = (a*(j-center_col-shift_col[f])+b*(i-center_row-shift_row[f])) + center_col
row = (c*(j-center_col-shift_col[f])+d*(i-center_row-shift_row[f])) + center_row
col = (a*(j-center_col-shift_col)+b*(i-center_row-shift_row)) + center_col
row = (c*(j-center_col-shift_col)+d*(i-center_row-shift_row)) + center_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftScaleRotate._run_unthreaded; replace("_run_unthreaded", "_run_threaded"); replace("range(colsM)", "prange(colsM)")
def _run_threaded(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
def _run_threaded(self, float[:,:,:] image, float shift_row, float shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand Down Expand Up @@ -435,15 +427,15 @@ class ShiftScaleRotate(LiquidEngine):
for f in range(nFrames):
for j in range(cols):
for i in range(rows):
col = (a*(j-center_col-shift_col[f])+b*(i-center_row-shift_row[f])) + center_col
row = (c*(j-center_col-shift_col[f])+d*(i-center_row-shift_row[f])) + center_row
col = (a*(j-center_col-shift_col)+b*(i-center_row-shift_row)) + center_col
row = (c*(j-center_col-shift_col)+d*(i-center_row-shift_row)) + center_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftScaleRotate._run_unthreaded; replace("_run_unthreaded", "_run_threaded_static"); replace("range(colsM)", 'prange(colsM, schedule="static")')
def _run_threaded_static(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
def _run_threaded_static(self, float[:,:,:] image, float shift_row, float shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand Down Expand Up @@ -471,15 +463,15 @@ class ShiftScaleRotate(LiquidEngine):
for f in range(nFrames):
for j in range(cols):
for i in range(rows):
col = (a*(j-center_col-shift_col[f])+b*(i-center_row-shift_row[f])) + center_col
row = (c*(j-center_col-shift_col[f])+d*(i-center_row-shift_row[f])) + center_row
col = (a*(j-center_col-shift_col)+b*(i-center_row-shift_row)) + center_col
row = (c*(j-center_col-shift_col)+d*(i-center_row-shift_row)) + center_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftScaleRotate._run_unthreaded; replace("_run_unthreaded", "_run_threaded_dynamic"); replace("range(colsM)", 'prange(colsM, schedule="dynamic")')
def _run_threaded_dynamic(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
def _run_threaded_dynamic(self, float[:,:,:] image, float shift_row, float shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand Down Expand Up @@ -507,15 +499,15 @@ class ShiftScaleRotate(LiquidEngine):
for f in range(nFrames):
for j in range(cols):
for i in range(rows):
col = (a*(j-center_col-shift_col[f])+b*(i-center_row-shift_row[f])) + center_col
row = (c*(j-center_col-shift_col[f])+d*(i-center_row-shift_row[f])) + center_row
col = (a*(j-center_col-shift_col)+b*(i-center_row-shift_row)) + center_col
row = (c*(j-center_col-shift_col)+d*(i-center_row-shift_row)) + center_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
# tag-end

# tag-copy: _le_interpolation_nearest_neighbor.ShiftScaleRotate._run_unthreaded; replace("_run_unthreaded", "_run_threaded_guided"); replace("range(colsM)", 'prange(colsM, schedule="guided")')
def _run_threaded_guided(self, float[:,:,:] image, float[:] shift_row, float[:] shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
def _run_threaded_guided(self, float[:,:,:] image, float shift_row, float shift_col, float scale_row, float scale_col, float angle) -> np.ndarray:
cdef int nFrames = image.shape[0]
cdef int rows = image.shape[1]
cdef int cols = image.shape[2]
Expand Down Expand Up @@ -543,8 +535,8 @@ class ShiftScaleRotate(LiquidEngine):
for f in range(nFrames):
for j in range(cols):
for i in range(rows):
col = (a*(j-center_col-shift_col[f])+b*(i-center_row-shift_row[f])) + center_col
row = (c*(j-center_col-shift_col[f])+d*(i-center_row-shift_row[f])) + center_row
col = (a*(j-center_col-shift_col)+b*(i-center_row-shift_row)) + center_col
row = (c*(j-center_col-shift_col)+d*(i-center_row-shift_row)) + center_row
_image_out[f, i, j] = _c_interpolate(&_image_in[f, 0, 0], row, col, rows, cols)

return image_out
Expand Down
Loading

0 comments on commit d62beb0

Please sign in to comment.