Dask equivalent of numpy (convolve + hstack)?










0















I currently have a function that computes a sliding sum across a 1-D numpy array (vector) using convolve and hstack. I would like to create an equivalent function using dask, but the various ways I've tried so far have not worked out.



What I'm trying to do is to compute a "sliding sum" of n numbers of an array, unless any of the numbers are NaN in which case the sum should also be NaN. The (n - 1) elements of the result should also be NaN, since no wrap around/reach behind is assumed.



For example:



input vector: [3, 4, 6, 2, 1, 3, 5, np.NaN, 8, 5, 6] 
n: 3
result: [NaN, NaN, 13, 12, 9, 6, 9, NaN, NaN, NaN, 19]


or



input vector: [1, 5, 7, 2, 3, 4, 9, 6, 3, 8]
n: 4
result: [NaN, NaN, NaN, 15, 17, 16, 18, 22, 22, 26]


The function I currently have for this using numpy functions:



def sum_to_scale(values, scale):

# don't bother if the number of values to sum is 1 (will result in duplicate array)
if scale == 1:
return values

# get the valid sliding summations with 1D convolution
sliding_sums = np.convolve(values, np.ones(scale), mode="valid")

# pad the first (n - 1) elements of the array with NaN values
return np.hstack(([np.NaN] * (scale - 1), sliding_sums))


How can I do the above using the dask array API (and/or dask_image.ndfilters) to achieve the same functionality?



Thanks in advance for any suggestions or insight.










share|improve this question




























    0















    I currently have a function that computes a sliding sum across a 1-D numpy array (vector) using convolve and hstack. I would like to create an equivalent function using dask, but the various ways I've tried so far have not worked out.



    What I'm trying to do is to compute a "sliding sum" of n numbers of an array, unless any of the numbers are NaN in which case the sum should also be NaN. The (n - 1) elements of the result should also be NaN, since no wrap around/reach behind is assumed.



    For example:



    input vector: [3, 4, 6, 2, 1, 3, 5, np.NaN, 8, 5, 6] 
    n: 3
    result: [NaN, NaN, 13, 12, 9, 6, 9, NaN, NaN, NaN, 19]


    or



    input vector: [1, 5, 7, 2, 3, 4, 9, 6, 3, 8]
    n: 4
    result: [NaN, NaN, NaN, 15, 17, 16, 18, 22, 22, 26]


    The function I currently have for this using numpy functions:



    def sum_to_scale(values, scale):

    # don't bother if the number of values to sum is 1 (will result in duplicate array)
    if scale == 1:
    return values

    # get the valid sliding summations with 1D convolution
    sliding_sums = np.convolve(values, np.ones(scale), mode="valid")

    # pad the first (n - 1) elements of the array with NaN values
    return np.hstack(([np.NaN] * (scale - 1), sliding_sums))


    How can I do the above using the dask array API (and/or dask_image.ndfilters) to achieve the same functionality?



    Thanks in advance for any suggestions or insight.










    share|improve this question


























      0












      0








      0








      I currently have a function that computes a sliding sum across a 1-D numpy array (vector) using convolve and hstack. I would like to create an equivalent function using dask, but the various ways I've tried so far have not worked out.



      What I'm trying to do is to compute a "sliding sum" of n numbers of an array, unless any of the numbers are NaN in which case the sum should also be NaN. The (n - 1) elements of the result should also be NaN, since no wrap around/reach behind is assumed.



      For example:



      input vector: [3, 4, 6, 2, 1, 3, 5, np.NaN, 8, 5, 6] 
      n: 3
      result: [NaN, NaN, 13, 12, 9, 6, 9, NaN, NaN, NaN, 19]


      or



      input vector: [1, 5, 7, 2, 3, 4, 9, 6, 3, 8]
      n: 4
      result: [NaN, NaN, NaN, 15, 17, 16, 18, 22, 22, 26]


      The function I currently have for this using numpy functions:



      def sum_to_scale(values, scale):

      # don't bother if the number of values to sum is 1 (will result in duplicate array)
      if scale == 1:
      return values

      # get the valid sliding summations with 1D convolution
      sliding_sums = np.convolve(values, np.ones(scale), mode="valid")

      # pad the first (n - 1) elements of the array with NaN values
      return np.hstack(([np.NaN] * (scale - 1), sliding_sums))


      How can I do the above using the dask array API (and/or dask_image.ndfilters) to achieve the same functionality?



      Thanks in advance for any suggestions or insight.










      share|improve this question
















      I currently have a function that computes a sliding sum across a 1-D numpy array (vector) using convolve and hstack. I would like to create an equivalent function using dask, but the various ways I've tried so far have not worked out.



      What I'm trying to do is to compute a "sliding sum" of n numbers of an array, unless any of the numbers are NaN in which case the sum should also be NaN. The (n - 1) elements of the result should also be NaN, since no wrap around/reach behind is assumed.



      For example:



      input vector: [3, 4, 6, 2, 1, 3, 5, np.NaN, 8, 5, 6] 
      n: 3
      result: [NaN, NaN, 13, 12, 9, 6, 9, NaN, NaN, NaN, 19]


      or



      input vector: [1, 5, 7, 2, 3, 4, 9, 6, 3, 8]
      n: 4
      result: [NaN, NaN, NaN, 15, 17, 16, 18, 22, 22, 26]


      The function I currently have for this using numpy functions:



      def sum_to_scale(values, scale):

      # don't bother if the number of values to sum is 1 (will result in duplicate array)
      if scale == 1:
      return values

      # get the valid sliding summations with 1D convolution
      sliding_sums = np.convolve(values, np.ones(scale), mode="valid")

      # pad the first (n - 1) elements of the array with NaN values
      return np.hstack(([np.NaN] * (scale - 1), sliding_sums))


      How can I do the above using the dask array API (and/or dask_image.ndfilters) to achieve the same functionality?



      Thanks in advance for any suggestions or insight.







      dask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 3:41







      James Adams

















      asked Nov 14 '18 at 3:31









      James AdamsJames Adams

      3,301125283




      3,301125283






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292792%2fdask-equivalent-of-numpy-convolve-hstack%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292792%2fdask-equivalent-of-numpy-convolve-hstack%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線