Determine how many times dask computed something
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
add a comment |
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
add a comment |
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
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
python dask
edited Dec 15 '18 at 1:06
djhoese
asked Nov 13 '18 at 20:49
djhoesedjhoese
2,1831832
2,1831832
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 17 '18 at 14:49
MRocklinMRocklin
26.1k1470129
26.1k1470129
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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