-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'refs/remotes/origin/master'
- Loading branch information
Showing
12 changed files
with
948 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
C code to be compiled on setup goes here. | ||
|
||
fast_median.cpp is a very fast median filter for 1d and 2d ndarrays | ||
with float or double data. The glue code is in fast_median.py | ||
|
||
compile using the Makefile in src/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import ctypes | ||
import numpy | ||
from numpy.ctypeslib import ndpointer | ||
from scipy.ndimage import median_filter | ||
|
||
HAVE_MEDIAN_SO = False | ||
try: | ||
import platform | ||
from os import path, environ | ||
s = platform.system() | ||
resources_dir = environ.get('LVMDRP_LIB_DIR') or path.join(path.dirname(__file__), 'src') | ||
if s=='Linux': | ||
resources_dir = path.join(resources_dir, 'fast_median.so') | ||
#print(resources_dir) | ||
lib = ctypes.cdll.LoadLibrary(resources_dir) | ||
elif s=='Darwin': | ||
resources_dir = path.join(resources_dir, 'fast_median.dylib') | ||
#print(resources_dir) | ||
lib = ctypes.cdll.LoadLibrary(resources_dir) | ||
else: | ||
raise Exception('Unknown platform: '+s) | ||
|
||
#template <typename T> | ||
#void median_filter_2d(int x, int y, int hx, int hy, int blockhint, const T* in, T* out); | ||
lib.median_filter_2d_float.argtypes = (ctypes.c_int, ctypes.c_int, | ||
ctypes.c_int, ctypes.c_int, | ||
ctypes.c_int, | ||
ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"), | ||
ndpointer(ctypes.c_float, flags="C_CONTIGUOUS")) | ||
lib.median_filter_2d_float.restype = None | ||
lib.median_filter_2d_double.argtypes = (ctypes.c_int, ctypes.c_int, | ||
ctypes.c_int, ctypes.c_int, | ||
ctypes.c_int, | ||
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), | ||
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS")) | ||
lib.median_filter_2d_double.restype = None | ||
|
||
lib.median_filter_1d_float.argtypes = (ctypes.c_int, | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"), | ||
ndpointer(ctypes.c_float, flags="C_CONTIGUOUS")) | ||
lib.median_filter_1d_float.restype = None | ||
lib.median_filter_1d_double.argtypes = (ctypes.c_int, | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), | ||
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS")) | ||
lib.median_filter_1d_double.restype = None | ||
|
||
HAVE_MEDIAN_SO = True | ||
except Exception: | ||
print(Exception) | ||
pass | ||
|
||
print('HAVE_MEDIAN_SO ',HAVE_MEDIAN_SO) | ||
|
||
if HAVE_MEDIAN_SO is True: | ||
def fast_median_filter_2d(input, size=None): | ||
assert len(input.shape) == 2, 'fast_median_filter_2d requires 2-dimensional input' | ||
assert len(size) == 2, 'fast_median_filter_2d requires two element size tuple, list or array' | ||
out = numpy.empty(input.shape, dtype=input.dtype) | ||
# Note we have to flip the shape and box x/y to match python's indexing | ||
if input.dtype==numpy.float32: | ||
lib.median_filter_2d_float(input.shape[1], input.shape[0], size[1], size[0], 0, input, out) | ||
elif input.dtype==numpy.float64: | ||
lib.median_filter_2d_double(input.shape[1], input.shape[0], size[1], size[0], 0, input, out) | ||
else: | ||
raise TypeError('median_filter_2d not implemented for type '+str(input.dtype)) | ||
return out | ||
|
||
def fast_median_filter_1d(input, size=None): | ||
assert len(input.shape) == 1, 'fast_median_filter_1d requires 1-dimensional input' | ||
out = numpy.empty(input.shape, dtype=input.dtype) | ||
if input.dtype==numpy.float32: | ||
lib.median_filter_1d_float(input.shape[0], size, 0, input, out) | ||
elif input.dtype==numpy.float64: | ||
lib.median_filter_1d_double(input.shape[0], size, 0, input, out) | ||
else: | ||
raise TypeError('median_filter_1d not implemented for type '+str(input.dtype)) | ||
return out | ||
else: | ||
def fast_median_filter_2d(input, size=None): | ||
return median_filter(input, size=size) | ||
def fast_median_filter_1d(input, size=None): | ||
return median_filter(input, size=size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
UNAME_S := $(shell uname -s) | ||
ifeq ($(UNAME_S),Linux) | ||
FLAGS = -fPIC -shared -O3 -march=native | ||
TRG = fast_median.so | ||
COMPILER = g++ | ||
endif | ||
ifeq ($(UNAME_S),Darwin) | ||
FLAGS = -dynamiclib -current_version 1.0 -compatibility_version 1.0 -O3 -march=native | ||
TRG = fast_median.dylib | ||
COMPILER = clang++ | ||
endif | ||
|
||
all: fast_median.cpp fast_median.hpp | ||
$(COMPILER) $(FLAGS) -o $(TRG) fast_median.cpp |
Oops, something went wrong.