Boundary modes in scipy.ndimage.laplace Boundary modes in scipy.ndimage.laplace numpy numpy

Boundary modes in scipy.ndimage.laplace


I digged a bit into scipy source code.What I found makes me think it won’t be easy to add your custom mode using only python scypi/numpy : you would end up re-coding most of all the backbone code of scipy in python.

The mode you are referring to is managed deep down into the underlying scipy’s C code.You will need to change a C enum value, update the source file accordingly with your custom mode logics, and produce a new package.

That might be an issue/evolution to open on github: either allow a custom build array method, or implement new modes. Or 1 ticket for each.

See here. Quoting :

/* Extend a line in memory to implement boundary conditions: */int NI_ExtendLine(double *buffer, npy_intp line_length,                  npy_intp size_before, npy_intp size_after,                  NI_ExtendMode extend_mode, double extend_value){    double *first = buffer + size_before;    double *last = first + line_length;    double *src, *dst, val;    switch (extend_mode) {        /* aaaaaaaa|abcd|dddddddd */        case NI_EXTEND_NEAREST:            src = first;            dst = buffer;            val = *src;            while (size_before--) {                *dst++ = val;            }            src = last - 1;            dst = last;            val = *src;            while (size_after--) {                *dst++ = val;            }            break;        /* abcdabcd|abcd|abcdabcd */        case NI_EXTEND_WRAP:            src = last - 1;            dst = first - 1;            while (size_before--) {                *dst-- = *src--;            }            src = first;            dst = last;            while (size_after--) {                *dst++ = *src++;            }            break;        /* abcddcba|abcd|dcbaabcd */        case NI_EXTEND_REFLECT:            src = first;            dst = first - 1;            while (size_before && src < last) {                *dst-- = *src++;                --size_before;            }            src = last - 1;            while (size_before--) {                *dst-- = *src--;            }            src = last - 1;            dst = last;            while (size_after && src >= first) {                *dst++ = *src--;                --size_after;            }            src = first;            while (size_after--) {                *dst++ = *src++;            }            break;        /* cbabcdcb|abcd|cbabcdcb */        case NI_EXTEND_MIRROR:            src = first + 1;            dst = first - 1;            while (size_before && src < last) {                *dst-- = *src++;                --size_before;            }            src = last - 2;            while (size_before--) {                *dst-- = *src--;            }            src = last - 2;            dst = last;            while (size_after && src >= first) {                *dst++ = *src--;                --size_after;            }            src = first + 1;            while (size_after--) {                *dst++ = *src++;            }            break;        /* kkkkkkkk|abcd]kkkkkkkk */        case NI_EXTEND_CONSTANT:            val = extend_value;            dst = buffer;            while (size_before--) {                *dst++ = val;            }            dst = last;            while (size_after--) {                *dst++ = val;            }            break;        default:            PyErr_Format(PyExc_RuntimeError,                         "mode %d not supported", extend_mode);            return 0;    }    return 1;}

Please note that I’m absolutely no expert in scipy, so maybe I’m missing something huge, or there are very documented reasons that explain what are the proposed modes.


EDIT 2019-Ap-24 some documentation hereI don't get all the details, but from what I understand:

  • It refers to academic work of Dr. Philippe Thevenez as potential source for some part of algorithms.
  • The ticket seems to indicate that some other modes may be developed in the future.

This ticket also deals with new modes.

Given the open dates of tickets, it may take some time before it comes.