Python: Mandelbrot explorer 0.9 now save and resume update Jan 23 /2022

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Python: Mandelbrot explorer 0.9 now save and resume update Jan 23 /2022 (/thread-17087.html)



Python: Mandelbrot explorer 0.9 now save and resume update Jan 23 /2022 - Guenter Schink - 06-09-2021 02:31 PM

edit Jan22/2022
Major changes:

1. Solid Guess
2. More information on bottom line on current progress
3. Time out
4. Auto Save / Resume
5. using "TEXTOUT_P() from HOME without to much hassle
Details and download post #15
/edit


Next exploration on Python!

Please keep in mind, that I'm a real beginner with Python and that I have no great experiences with other program languages other than a little bit of basic and pascal 30 years ago. And some RPN and RPL of course :-). The objective is to explore some specifics of Python on the Prime. The Mandelbrot set is a welcome test bed.

After my first attempts with Python on the Prime I have now completed something I'd like to share with you. It's a Mandelbrot explorer. Here https://en.wikipedia.org/wiki/Mandelbrot_set is a summary of what this set is.

This program let's you dig really deep into the fascination of the Mandelbrot set at a very amazing speed for a calculator.

My program starts with the well known picture of the Mandelbrot set. Upon completion of the first drawing it will immediately show an introductory help screen, telling you about the keys to be pressed.

When going back to the Mandelbrot display you might be disappointed a little bit by the less than perfect resolution. But this of course has a reason. The calculations for each pixel may take an awful lot of time. Therefore I decided to at first plot in "coarse" mode (a square of 4x4 pixels instead of one pixel at a time). This enables you to go rather quick (for a calculator) through the different zooms back and forth. Once you decide to have a better resolution, press [Plot] and there you go and you'll immediately recognize how much more time the fine draw needs over the coarse draw.

You zoom in with just a touch on the screen. Your hit will determine the center of the new picture enlarged by a zoom factor of initially 4. The backspace (Del) key brings you back the previous pictures up to the first one. A little square in the top left corner will tell you the current process was finished. (Beep is missing Smile )

The second row X^Y, SIN,COS,TAN,LN,LOG lets you switch between different color schemes, these are: black and white; 8, 27, 64, 125 and 216 colors of RGB

These different color schemes are really useful. While the higher color numbers show overall nicer pictures, those with only 27 or less provide you with a 3D appearance when zoomed in at specific places and iterations set to higher values. Selecting larger iterations will get you there.

[Plot] as mentioned before, the fine draw

[Num] shows the values of the current screen: Iiterations, magnification, zoom factor, co-ordinates, colors, time needed for the creation of the display.

The set starts at the co-ordinates (-2, 1) for the real part and (1.2,-1.2) for the imaginary part. Initial number of iterations(depth) is 20. The depth will increase by 10 each time you push [+]; [-] consequently will decrease the depth until a minimum of 10. [x] and [÷] will increase/decrease the iterations by 50.

[x²]/[EEX] increase/decrease the zoom factor. Best done when you had switched to the information screen [Num]. There you'll immediately see, what you've selected.

[Help] shows the introductory screen again, with the information on the keys.

[Home] takes you back to the initial screen.

[Esc] finally lets you exit. There you have the choice to restart or switch to the Home (or Cas) screen.

If you are interested in examining my program, for Windows users I'd strongly recommend "notepad++". With the "notepad++" feature of collapsing you have a great view on the simple structure which is otherwise hidden by the lines of code. And "notepad++" is a great help in tracking the various levels of indentation, that's so important with Python.

Tribute to Geoff whose Mandelbrot Program was fundamental to get the components that I had already created before in a reasonable order. I even applied a lot of the names he used, because they are so nicely descriptive.

Unpack the.zip, copy the directory"MandelExpl.hpappdir" to (most probably) "C:\Users\<some user>\Documents\HP Connectivity Kit\Content\Programs" and from there let the Ck send it to your Prime.

Please let me know of any quirks you might encounter. I'd really love to get some comments and perhaps suggestions.

Enjoy, Günter

Discussion, hints on Python specifics are very welcome


RE: Python: Mandelbrot on fire, the explorer - Guenter Schink - 07-11-2021 09:45 AM

Mandelbrot explorer update 2021-07-11 Updated in post#1

After some nice days of vacation, I returned to my Python testbed, the Mandelbrot Explorer. The basic description in post #1 still applies. But I've done some changes for a hopefully much nicer appearance. And I'll ask the moderators to move this thread to the Prime Software Library.

When you start the program, you'll recognize that the screen is quickly filled with blocs which broadly resemble the Mandelbrot set. But then the drawing is going on - on the left hand side a small progress bar is visible that tells you that the program is active still, and will finish shortly Smile

A small explanation, how it's done and where you might adjust things with ease.

The drawing is done in different stages.

CoarseDraw: this procedure draws blocs of arbitrary size. Here I choose 10 and 4 pixels high and wide to quickly show something that looks already similar to the Mandelbrot set, these values can be changed at will.

The last bloc is 2 pixels high and wide, this value must not be changed, because the procedure further on is dependent on this value.

Now we have the screen filled with blocs of 2x2 pixels each of the color of the top, left pixel. That is on each even line number (lines y start with 0) in each even column the pixel (columns x start with 0) has got the correct color. The other three pixels in the 2x2 square may be right or not.

Next step I call dithering (not quite correct, but ...) : Take the lines where y is odd, start with the column where x is odd and set each second pixel to its correct color. After that we have half of all the pixels set to the correct color and many of the others also, rather randomly.

By this procedure we avoide to do the time consuming calculation more than once for each pixel. The first two paintings of blocs (10 and 4) are negligible time-wise.

FineDraw: Last step is to get the right color for the remaining pixels. Start at the first line (y=0) set x=1 (x=0 already has the correct color) calculate the correct color for each second pixel. That is the pixels with x=odd get calculated and colored. Then in the next line (y=1) we do the same with the pixels where x=even. We continue, so that in lines where y=even all pixels where x=odd, and in lines where y=odd all pixels where x=even get calculated and colored correctly.

Why all this fuss? The Mandelbrot set does an awful lot of calculations. I start now with the maximum Iterations set to a depth of 100. While the calculator is working you have a fair view on to what is developing. By omitting the last FineDraw , the required time is only almost half of the time required for the full procedure. Yet it already shows a reasonable (in my view) appearance where you might like to go deeper and deeper, before you decide to press [Plot] for the perfect picture. The limit for magnification is ~2^13

What can be adjusted easily:

Line 16: set CompleteAllPasses=False to True, specifically for the Virtual Prime for convenience.
Line 60: for size in (10, 4, 2) don't change "2" but you may play with the other figures, even add some Smile e.g.(20, 15, 10, 8, 6, 2)
Line 190: change the initial boundaries for the set
line 191: change the initial values for the depth(Iterations), mag(which color scheme), initial ZoomFactor

It is really unbelievable how fast this Python implementation is on the Prime.

Kudos to the "Stone Soup Group" for FRACTINT and for H.-O-Peitgen and P.H.Richter for the book "The Beauty of Fractals".

Feed back is very welcome

Günter


RE: Python: Mandelbrot explorer now interactive update 14.08.2021 - Guenter Schink - 08-14-2021 01:36 PM

Next update! Version 0.8 (Link in post #1) 14.08.2021

Couldn't help but had to play around once more.

In principle, the usage hasn't changed Look at the first post), except that the program is more interactive.

At any time while the drawing is developing you can touch the screen to dive in deeper, or push <Del> to go back to the last magnification. Also the various buttons for color selection or depth increase/decrease may be pushed at any time with immediate effect.

When you choose to increase or decrease the zoom-factor, this will take effect at the next zooming in.

As a teaser I have attached 4 samples. Sample1 and Sample2 exactly show the same area, just different colors. Do you recognize how much more a 3D- effect seems to be in 1 compared to 2?

Sample3 shows a completely different area.

While the drawing develops, a progress bar at the left shows you in which line the action is. And during drawing the bottom line shows you the number of colors selected, the depth (number of iterations) the zoom factor and the actual zoom, see sample4. The progress bar and bottom line are barely visible when you run the virtual PRIME, but they help you to see that something is happening still when it takes its time on the real calc.

I'd like to emphasize once again that the various passes of drawing simply serve the goal to make something reasonable visible as early as possible. Thus you can move in deeper and deeper (and back) without having to wait until the drawing is completed. Consequently the <Plot> key has no function anymore.

I hope someone enjoys this little gimmick Smile

Günter



RE: Python: Mandelbrot explorer now interactive update 15.08.2021 - Guenter Schink - 01-23-2022 04:52 PM

Most of what's described in post#1 still applies.

While it's really fun to chew on my Mandelbrot Explorer, my major objective is to explore ways to almost seamlessly interface with the HOME environment. In this update special observation is dedicated to output of text in graphics.

major changes:

1. Solid Guess
2. More information on bottom line on current progress
3. Time out
4. Auto Save / Resume
5. using "TEXTOUT_P() from HOME without to much hassle

1. SOLID GUESS

Pass 1 calculates blocs of 4x4 pixels and assigns the color found for the top/left pixel to the entire bloc. then it checks its 3 right hand and lower neighbor blocs. Once they all have the same color this bloc is marked as completed. As the term "guess" indicates, this is not perfect but works pretty good specifically once you zoom in deeper and deeper. And it doesn't waist to much time in the "real" Mandelbrot set, The "blue sea".

Pass 1 and 2 fill those blocs which were not marked as completed. That could be done in one pass, but the "checker board" approach gives a nicer appearance.

But if you wish, there is a brute force approach with [Plot]. That takes its time specifically when large parts of the real Mandelbrot set are within the image. Try it yourself. Zoom in so that the entire screen is the "blue sea". The info screen should give an elapsed time of about 3.3 seconds( on a G2) press [Plot] and wait ... The info screen should give you an elapsed time of about 35 seconds. Quite a difference! Now try this with a depth of 300. Time t o get a coffee Smile

2. BOTTOM LINE

The extended possibilities of text output led me to redesign the bottom line. Just for fun. Once the image is finished you still can invoke it with pushing [B] or ([Mem] if that's easier to recall)

3. TIME OUT

As far as I've seen Python doesn't have a time out. Thus if you forgot that "Mandelbrot Explorer" is still running you could end up with a calculator completely out of power. This is now prevented from by taking care of the run time. The help screen, the info screen, and the manually invoked bottom info automatically time out after ~60 seconds. The major loop adds 0.2 seconds during each round, once 240 seconds are reached the program terminates. This may lead to some lag when touching the screen or pushing a button. Simply be more determined.

4. Auto SAVE / RESUME

When the program finishes, either by time out or [Esc] the current Image with its parameters (color, depth, zoomstack) is saved. Upon restart you resume where you left the last run. (Doesn't work) if you leave by pressing [On]

5. TEXTOUT_P()

The textout() function of the module hpprime is rather limited compared to the TEXTOUT_P() function of HOME. This adds optional font (size) selection, a max_width parameter and background color. As I have explained elsewhere the interface is rather cumbersome. In order to make life easier I've created a few function to serve this objective:

Code:


from hpprime import *

def string(arg):                #create a string understood by HOME environment
    return '"' + str(arg) +'"'
    
def RGB(r,g,b):                 #make RGB understood by HOME environment
    return (r*256+g)*256+b
    
def strip(arg):                 #strip parantheses from a tuple converted to string
    return str(arg)[1:-1]

def TxtOut(t,x,y,f,c,*args):    #provide an easy to handle interface to TEXTOUT_P in HOME
    if len(args)>2:             #Syntax: TxtOut(string,x,y,font,color[,max_width[,background color]]
       raise(SyntaxError("too many arguments"))
    if True in [isinstance(i,list) for i in t]:
       raise(TypeError("can't convert 'list' object to str implicitly"))
    parms=(int(x),int(y),int(f),int(c))
    if len(args)>0:parms+=(int(args[0]),) #add max_width
    if len(args)>1:parms+=(int(args[1]),) #add background color
    return eval('TEXTOUT_P('+string(t)+','+strip(parms)+')')

This function doesn't implement a choice of the graphic. It always writes to G0, and it doesn't know anything about "2D" functions. But I think it is valuable and easy to use.

Comments, suggestions