|
| 1 | +""" |
| 2 | +convolution_wrapper.py |
| 3 | +
|
| 4 | +ctypes wrapper for convolve.c |
| 5 | +
|
| 6 | +gcc -shared -o c_convolve.so c_convolve.c |
| 7 | +""" |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import ctypes |
| 11 | + |
| 12 | +import os |
| 13 | +# os.system('gcc -shared -o c_convolve.so c_convolve.c') |
| 14 | + |
| 15 | +try: |
| 16 | + path_to_file = os.path.split(__file__)[0] |
| 17 | + |
| 18 | + _convolution = np.ctypeslib.load_library('c_convolve.so', path_to_file) |
| 19 | + args = [ctypes.c_int, |
| 20 | + ctypes.c_int, |
| 21 | + ctypes.c_int, |
| 22 | + ctypes.c_int, |
| 23 | + np.ctypeslib.ndpointer(np.int32), |
| 24 | + np.ctypeslib.ndpointer(np.int32), |
| 25 | + np.ctypeslib.ndpointer(np.int32), |
| 26 | + np.ctypeslib.ndpointer(np.int32), |
| 27 | + np.ctypeslib.ndpointer(np.float64), |
| 28 | + np.ctypeslib.ndpointer(np.float64), |
| 29 | + np.ctypeslib.ndpointer(np.float64)] |
| 30 | + _convolution.c_convolve.argtypes = args |
| 31 | + _convolution.c_convolve.restype = None |
| 32 | + |
| 33 | + def rvic_convolve(*args): |
| 34 | + """args: |
| 35 | +
|
| 36 | + nsources, |
| 37 | + noutlets, |
| 38 | + subset_length, |
| 39 | + xsize, |
| 40 | + source2outlet_ind, |
| 41 | + source_y_ind, |
| 42 | + source_x_ind, |
| 43 | + source_time_offset, |
| 44 | + unit_hydrograph, |
| 45 | + aggrunin, |
| 46 | + ring |
| 47 | + """ |
| 48 | + _convolution.c_convolve(*args) |
| 49 | + |
| 50 | + return |
| 51 | +except: |
| 52 | + print('Using brodcasting convolution method because there was a problem ' |
| 53 | + 'loading c_convolve.c') |
| 54 | + |
| 55 | + def rvic_convolve(nsources, noutlets, subset_length, xsize, |
| 56 | + source2outlet_ind, source_y_ind, source_x_ind, |
| 57 | + source_time_offset, unit_hydrograph, aggrunin, ring): |
| 58 | + # numpy brodcasting method |
| 59 | + for s, outlet in enumerate(source2outlet_ind): |
| 60 | + y = source_y_ind[s] |
| 61 | + x = source_x_ind[s] |
| 62 | + i = source_time_offset[s] |
| 63 | + j = i + subset_length |
| 64 | + ring[i:j, outlet] += np.squeeze(unit_hydrograph[:, s] * aggrunin[y, x]) |
| 65 | + |
| 66 | + # # pure python convolution |
| 67 | + # for s, outlet in enumerate(source2outlet_ind): |
| 68 | + # y = source_y_ind[s] |
| 69 | + # x = source_x_ind[s] |
| 70 | + # for i in xrange(subset_length): |
| 71 | + # j = i + source_time_offset[s] |
| 72 | + # ring[j, outlet] += (unit_hydrograph[i, s] * aggrunin[y, x]) |
| 73 | + return |
| 74 | + |
| 75 | + |
| 76 | +def test(): |
| 77 | + nsources = 20 |
| 78 | + subset_length = 10 |
| 79 | + full_time_length = 15 |
| 80 | + noutlets = 4 |
| 81 | + source2outlet_ind = np.linspace(0, noutlets, nsources, |
| 82 | + endpoint=False).astype(np.int32) |
| 83 | + |
| 84 | + ysize = 12 |
| 85 | + xsize = 15 |
| 86 | + |
| 87 | + source_y_ind = np.random.randint(0, ysize-1, nsources).astype(np.int32) |
| 88 | + source_x_ind = np.random.randint(0, xsize-1, nsources).astype(np.int32) |
| 89 | + |
| 90 | + source_time_offset = np.random.randint(0, 4, nsources).astype(np.int32) |
| 91 | + |
| 92 | + aggrunin = np.random.uniform(size=(ysize, xsize)) |
| 93 | + unit_hydrograph = np.zeros((subset_length, nsources), dtype=np.float64) |
| 94 | + unit_hydrograph[0:4, :] = 0.25 |
| 95 | + ring = np.zeros((full_time_length, noutlets), dtype=np.float64) |
| 96 | + |
| 97 | + for i in xrange(10): |
| 98 | + aggrunin = np.random.uniform(size=(ysize, xsize)) |
| 99 | + rvic_convolve(nsources, noutlets, subset_length, xsize, |
| 100 | + source2outlet_ind, source_y_ind, source_x_ind, |
| 101 | + source_time_offset, unit_hydrograph, aggrunin, ring) |
0 commit comments