2

What I'm trying to do is provide a GUI for displaying some numpy arrays, and wire up IPython so you can manipulate the data however you like. I have all the IPython stuff working, the problem is how to detect that the array changed.

The obvious solution is to explicitly call some sort of refresh() but I want immediate feedback. Drawing the GUI is expensive (several seconds) so I can't redraw on a timer. However, I can look at the data on a timer, and redraw if it changed.

I saw an answer about hashing the whole ndarray which is helpful, but my arrays are a bit too large, and the hash too slow. I don't strictly need a hash, because I don't plan on caching or storing some particular array.

Can I get numpy to keep track of its own "dirty" flag? Or, is there some property that would have a different id or something after mutating the array?

Community
  • 1
  • 1
Cuadue
  • 3,769
  • 3
  • 24
  • 38
  • Can't you tell whether the array has changed by just detecting the user activity that would change it? – user2357112 Feb 25 '14 at 18:25
  • It would help, but for programmatic code that doesn't go through the IPython widget, I still have to sprinkle around explicit `render()` calls. If we can answer this question, it makes the interactive and programmatic interfaces the same, and hopefully simpler. – Cuadue Feb 25 '14 at 18:31
  • Does your code even work in such a way that showing the intermediate state of the array would be useful? Progressive rendering is nice for web pages, but for your case, it might not even be possible to update the display between the time the first bit of your results come in and the time the whole output is ready. You can just update the display when the call finishes. Or is some other thread modifying the array concurrently, not directed by user input? – user2357112 Feb 25 '14 at 18:34

1 Answers1

4

You might be able to get away with storing some checksums and recomputing them on a timer to look for changes, or just keeping around a copy and checking equality with np.all(a==b). The approaches won't scale well, though.

One solution making your array read-only, then giving the user some methods for mutation which you have instrumented to call refresh().

Another idea is to subclass ndarray, wrapping the relevant methods with calls to refresh(). This one might be tricky, as there are a lot of ways to mutate numpy arrays.

Finally, you could provide a custom IPython profile which calls refresh() after each user command. This seems like the simplest approach, as you can use the existing hooks module to insert your calls.

Community
  • 1
  • 1
perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
  • I like the `hooks` approach. I can get some code to execute *before* the IPython input, but I can't find how to execute *after*. Any idea? – Cuadue Feb 25 '14 at 18:55
  • Ah, looks like they changed the API for v1.0. I don't know enough about IPython internals to say. Perhaps you should ask that as a separate question to catch the attention of some IPython experts. – perimosocordiae Feb 25 '14 at 20:59