Determine how many times dask computed something










1















Question



I'm wondering if it is possible with dask (specifically dask arrays) to know if and when something has been computed. I'm thinking of unit tests wanting to know how many times dask computed an array. Similar to mock objects knowing how many times they were called. Does something like this exist already? If not, is there a better way than making a custom callback? If this doesn't exist, is it something the dask core devs would be interested in adding to core dask for testing?



Any help is much appreciated.



Details



Say I have a function which takes in an xarray DataArray, does some stuff to it, and returns it. There are some cases where dask arrays are implicitly converted to numpy arrays including a new dask-user not knowing the best dask-friendly way to do something. I would like to write my unit tests to make sure that I or another contributor doesn't accidentally hurt the performance of a function. This is especially important considering test data is often a simplified/small version of real world cases and the performance hit of computing a dask array multiple times may not be seen in these cases.



Edit: Solution



Here is what I ended up doing as a simple solution based on MRocklin's answer.



class CustomScheduler(object):
def __init__(self, max_computes=1):
self.max_computes = max_computes
self.total_computes = 0

def __call__(self, dsk, keys, **kwargs):
self.total_computes += 1
if self.total_computes > self.max_computes:
raise RuntimeError("Too many dask computations were scheduled: ".format(self.total_computes))
return dask.get(dsk, keys, **kwargs)


I then use it like this:



with dask.config.set(scheduler=CustomScheduler(0)):
# dask array stuff









share|improve this question




























    1















    Question



    I'm wondering if it is possible with dask (specifically dask arrays) to know if and when something has been computed. I'm thinking of unit tests wanting to know how many times dask computed an array. Similar to mock objects knowing how many times they were called. Does something like this exist already? If not, is there a better way than making a custom callback? If this doesn't exist, is it something the dask core devs would be interested in adding to core dask for testing?



    Any help is much appreciated.



    Details



    Say I have a function which takes in an xarray DataArray, does some stuff to it, and returns it. There are some cases where dask arrays are implicitly converted to numpy arrays including a new dask-user not knowing the best dask-friendly way to do something. I would like to write my unit tests to make sure that I or another contributor doesn't accidentally hurt the performance of a function. This is especially important considering test data is often a simplified/small version of real world cases and the performance hit of computing a dask array multiple times may not be seen in these cases.



    Edit: Solution



    Here is what I ended up doing as a simple solution based on MRocklin's answer.



    class CustomScheduler(object):
    def __init__(self, max_computes=1):
    self.max_computes = max_computes
    self.total_computes = 0

    def __call__(self, dsk, keys, **kwargs):
    self.total_computes += 1
    if self.total_computes > self.max_computes:
    raise RuntimeError("Too many dask computations were scheduled: ".format(self.total_computes))
    return dask.get(dsk, keys, **kwargs)


    I then use it like this:



    with dask.config.set(scheduler=CustomScheduler(0)):
    # dask array stuff









    share|improve this question


























      1












      1








      1








      Question



      I'm wondering if it is possible with dask (specifically dask arrays) to know if and when something has been computed. I'm thinking of unit tests wanting to know how many times dask computed an array. Similar to mock objects knowing how many times they were called. Does something like this exist already? If not, is there a better way than making a custom callback? If this doesn't exist, is it something the dask core devs would be interested in adding to core dask for testing?



      Any help is much appreciated.



      Details



      Say I have a function which takes in an xarray DataArray, does some stuff to it, and returns it. There are some cases where dask arrays are implicitly converted to numpy arrays including a new dask-user not knowing the best dask-friendly way to do something. I would like to write my unit tests to make sure that I or another contributor doesn't accidentally hurt the performance of a function. This is especially important considering test data is often a simplified/small version of real world cases and the performance hit of computing a dask array multiple times may not be seen in these cases.



      Edit: Solution



      Here is what I ended up doing as a simple solution based on MRocklin's answer.



      class CustomScheduler(object):
      def __init__(self, max_computes=1):
      self.max_computes = max_computes
      self.total_computes = 0

      def __call__(self, dsk, keys, **kwargs):
      self.total_computes += 1
      if self.total_computes > self.max_computes:
      raise RuntimeError("Too many dask computations were scheduled: ".format(self.total_computes))
      return dask.get(dsk, keys, **kwargs)


      I then use it like this:



      with dask.config.set(scheduler=CustomScheduler(0)):
      # dask array stuff









      share|improve this question
















      Question



      I'm wondering if it is possible with dask (specifically dask arrays) to know if and when something has been computed. I'm thinking of unit tests wanting to know how many times dask computed an array. Similar to mock objects knowing how many times they were called. Does something like this exist already? If not, is there a better way than making a custom callback? If this doesn't exist, is it something the dask core devs would be interested in adding to core dask for testing?



      Any help is much appreciated.



      Details



      Say I have a function which takes in an xarray DataArray, does some stuff to it, and returns it. There are some cases where dask arrays are implicitly converted to numpy arrays including a new dask-user not knowing the best dask-friendly way to do something. I would like to write my unit tests to make sure that I or another contributor doesn't accidentally hurt the performance of a function. This is especially important considering test data is often a simplified/small version of real world cases and the performance hit of computing a dask array multiple times may not be seen in these cases.



      Edit: Solution



      Here is what I ended up doing as a simple solution based on MRocklin's answer.



      class CustomScheduler(object):
      def __init__(self, max_computes=1):
      self.max_computes = max_computes
      self.total_computes = 0

      def __call__(self, dsk, keys, **kwargs):
      self.total_computes += 1
      if self.total_computes > self.max_computes:
      raise RuntimeError("Too many dask computations were scheduled: ".format(self.total_computes))
      return dask.get(dsk, keys, **kwargs)


      I then use it like this:



      with dask.config.set(scheduler=CustomScheduler(0)):
      # dask array stuff






      python dask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 15 '18 at 1:06







      djhoese

















      asked Nov 13 '18 at 20:49









      djhoesedjhoese

      2,1831832




      2,1831832






















          1 Answer
          1






          active

          oldest

          votes


















          1














          There are a variety of ways to trigger on execution.



          One would be to specify a custom scheduler:



          def my_scheduler(dsk, keys, **kwargs):
          print('computing!')
          return dask.get(dsk, keys, **kwargs)

          with dask.config.set(scheduler=my_scheduler):
          ...


          Custom callbacks, like what you suggest are also pretty easy to implement.



          If you're using dask array exclusively then you could look at array plugins



          There are a variety of other approaches used in the test suite.






          share|improve this answer






















            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%2f53289286%2fdetermine-how-many-times-dask-computed-something%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            There are a variety of ways to trigger on execution.



            One would be to specify a custom scheduler:



            def my_scheduler(dsk, keys, **kwargs):
            print('computing!')
            return dask.get(dsk, keys, **kwargs)

            with dask.config.set(scheduler=my_scheduler):
            ...


            Custom callbacks, like what you suggest are also pretty easy to implement.



            If you're using dask array exclusively then you could look at array plugins



            There are a variety of other approaches used in the test suite.






            share|improve this answer



























              1














              There are a variety of ways to trigger on execution.



              One would be to specify a custom scheduler:



              def my_scheduler(dsk, keys, **kwargs):
              print('computing!')
              return dask.get(dsk, keys, **kwargs)

              with dask.config.set(scheduler=my_scheduler):
              ...


              Custom callbacks, like what you suggest are also pretty easy to implement.



              If you're using dask array exclusively then you could look at array plugins



              There are a variety of other approaches used in the test suite.






              share|improve this answer

























                1












                1








                1







                There are a variety of ways to trigger on execution.



                One would be to specify a custom scheduler:



                def my_scheduler(dsk, keys, **kwargs):
                print('computing!')
                return dask.get(dsk, keys, **kwargs)

                with dask.config.set(scheduler=my_scheduler):
                ...


                Custom callbacks, like what you suggest are also pretty easy to implement.



                If you're using dask array exclusively then you could look at array plugins



                There are a variety of other approaches used in the test suite.






                share|improve this answer













                There are a variety of ways to trigger on execution.



                One would be to specify a custom scheduler:



                def my_scheduler(dsk, keys, **kwargs):
                print('computing!')
                return dask.get(dsk, keys, **kwargs)

                with dask.config.set(scheduler=my_scheduler):
                ...


                Custom callbacks, like what you suggest are also pretty easy to implement.



                If you're using dask array exclusively then you could look at array plugins



                There are a variety of other approaches used in the test suite.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 '18 at 14:49









                MRocklinMRocklin

                26.1k1470129




                26.1k1470129





























                    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%2f53289286%2fdetermine-how-many-times-dask-computed-something%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

                    Darth Vader #20

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Ondo