Message Boards Message Boards

0
|
1092 Views
|
8 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Skipped instances using Dynamic

Posted 1 year ago

When I execute the following sample code using a large Map operation and many Graphics3D objects, the end result (i=20) is shown without showing the intermediate results (i=1,i=2,..., i=19). I want to show each and every step. Can someone help?

Dynamic[a]
For[i = 1, i <= 20, i++,
b = RandomReal[{-1, 1}, {10000, 3}];
TextStr = {Text["i=" <> ToString[i], {0, 0, 5}]};
b3g = Map[Sphere[#, 1] &, b];
a = Graphics3D[{TextStr, b3g}, 
PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}];
]

As a comparison, the sample code below without a large Map operation and with little Graphics3D objects, each and every individual step is visible.

Dynamic[c]
For[i = 1, i <= 20, i++,
d = RandomReal[{-1, 1}, {10000000, 3}];
TextStr = {Text["i=" <> ToString[i], {0, 0, 5}]};
c = Graphics3D[{TextStr, Sphere[d[[1]], 1]}, 
PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}];
]
POSTED BY: John Lee
8 Replies
Posted 1 year ago

Eric, thanks again for the comment.

but now you're happy with completely externalizing the results for offline inspection.

If you run the last code example I uploaded, you may be able to monitor the Graphics3D output in real time at every intermediate step (i=1,2,3,...,20) as they are evaluated. Yes they are saved to the SSD as png files also but the important point is that it enables real-time screen monitoring without skipping any of the results and I am happy with that. If I save all the steps' outputs (hundreds or thousands of them) to an array and use ListAnimate, it may take up too much memory for my computer to handle so I rather decided to use For loop to keep updating the vector array variable as the calculation goes on, rather than keep appending it to another giant array of arrays. I am not as fluent as most of the experts here including you in Mathematica and I learned a lot from your comments. I guess that the problem might be ultimately solved if Dynamic can have an option to increase its graphical rendering priority as you implied, or there is a Pause-like function that waits until Dynamic finishes updating all of its graphical output.

POSTED BY: John Lee
Posted 1 year ago

Well now you need to inspect the results outside of Mathematica (unless you're going to just import them back in again). What I showed you earlier (the one that ultimately used ListAnimate) gathered all of the graphics objects into a list (which I named spheres), so you have all of the things you want to inspect all together in one structure within Mathematica itself.

I'm not arguing that my suggestions is right or best. I'm just saying you seem to be jumping around a lot: first you want to inspect in real time, but now you're happy with completely externalizing the results for offline inspection. I'm obviously not invested in how you choose to build your system, but I do feel like maybe we're missing an opportunity to get at the real root of the problem.

POSTED BY: Eric Rimbey
Posted 1 year ago

Eric, thanks for the suggestion again. I inserted a png file Export command in place of the Pause as shown below and it just works fine and shows each and every step now. Probably the png Export requires the output of the updated Graphics3D object a enforcing it to wait until the graphics update is finished internally, or it simply took just enough time :).

a = Graphics3D[{}];
Dynamic[a]
For[i = 1, i <= 20, i++, b = RandomReal[{-1, 1}, {10000, 3}];
 TextStr = {Text["i=" <> ToString[i], {0, 0, 5}]};
 b3g = Map[Sphere[#, 1] &, b];
 a = Graphics3D[{TextStr, b3g}, 
   PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}];
 Export["D:\\Downloads\\a" <> ToString[i] <> ".png", a]
 ]
POSTED BY: John Lee
Posted 1 year ago

I would expect that Dynamic is given lower priority for computation time compare to the "main thread". So I think this approach is generally doomed to fail.

I wanted to check each and every single cycle result real time on screen as the calculation goes on

I guess I still don't understand enough of your context. If the computations are going at "machine speed" with no pauses, then how could you even mentally process what you're seeing if you monitor the computations "in real time"?

If you want to inspect every step of the computation, your best bet is to save them and then review them after the computation is complete. If you want to watch things progress as the computation is happening, then I think you're going to need some pauses.

Probably Monitor would be better than Dynamic, but I don't think you'll be able to avoid using pauses. If you're adamant about no pauses, you could insert a dialog, maybe something like this:

For[i = 1, i <= 2, i++, b = RandomReal[{-1, 1}, {10000, 3}];
 TextStr = {Text["i=" <> ToString[i], {0, 0, 5}]};
 b3g = Map[Sphere[#, 1] &, b];
 ChoiceDialog[
  Graphics3D[{TextStr, b3g}, 
   PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}]];]

But I'm not really familiar with the best monitoring/debugging techniques, so hopefully someone else will jump in. I'm also not sure I understand what you really need, so if there's more info/context you can provide, maybe that would help.

POSTED BY: Eric Rimbey
Posted 1 year ago

Eric, thanks for your suggestions.

I have some explanation about the code examples and my goal. The large evaluation of RandomReal simulates a chunk of somewhat complicated calculations involving a large array of vectors on a triangular lattice without showing too much details but ends up using a similar total time O(1 sec) per cycle when combined with the Map step. The 10,000 instances of Spheres in the first example also simulates the large Graphics3D object made up of many 3d Arrows used to display the intermediate status of the vector arrays. I wanted to check each and every single cycle result real time on screen as the calculation goes on, but as shown in the first example, many cycles (i=1,2,...,19) are skipped totally, unlike the much lighter second example which shows the whole progression without skipping.

If I insert Pause at the end of the For execution part as shown in the code example below, it shows only i=20 when Pause[0] is used (on my computer), shows i=5,7,9,11,13,15,17,19,20 when Pause[1] is used, shows i=2,3,4,5,6,7,8,...,19,20 when Pause[2] is used.

Dynamic[a]
For[i = 1, i <= 20, i++, b = RandomReal[{-1, 1}, {50000, 3}];
TextStr = {Text["i=" <> ToString[i], {0, 0, 5}]};
b3g = Map[Sphere[#, 1] &, b];
a = Graphics3D[{TextStr, b3g}, 
PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}];
Pause[0.2];
]

My guess is that the Graphics3D object of each cycle is thrown at the graphics handler of Mathematica responsible for Dynamic at a pace too fast for it to process and spit out to the screen in time before it receives the next Graphics3D object due to the sheer number of 3D Arrows contained in it. All I need is that, without using Pause and tuning its time atgument to match the speed of the graphics handler, simply do the calculation and the visualization one after the other as if they are performed by a single computing thread.

POSTED BY: John Lee
Posted 1 year ago

You could do something like this instead:

centers = RandomReal[{-1, 1}, {20, 3}];
spheres = MapIndexed[
   Graphics3D[{Text["i=" <> ToString[#2[[1]]], {0, 0, 5}], 
      Sphere[#1]}, PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}] &, 
   centers];
ListAnimate[spheres]
POSTED BY: Eric Rimbey
Posted 1 year ago

Why are you generating 10 million random triples when you only need 20?

POSTED BY: Eric Rimbey
Posted 1 year ago

If you figured out something that DOES show each step, then what is your question? Sounds like you answered your own question.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract