Skip to content

Conversation

Baharis
Copy link
Member

@Baharis Baharis commented May 16, 2025

Context

If you work with Instamatic and tqdm, or you tried to run some calibration from the main GUI which communicates using the instamatic.tools.printer with '\r\x1b[K' return sequence, you probably ran into the same problem I have. While both these commands produce quickly-changing strings in the command-line terminal, e.g. progress bars, the carriage return does not work correctly in the GUI console. According to my research, it should not: tkinter.Text was never designed with carriage return or ANSI escape codes in mind. As a result, after counting merely to 25, it starts looking like that:

image

This PR improves the behavior of the carriage return symbol '\r': any time console's Writer class now gets a message with '\r', it will immediately strip everything on its left, remove the carriage return, remove any other ANSI escape codes, delete the last message in the terminal, and print the new message in its place. As a result, both tqdm and printer now work completely correctly in the GUI! You can even update a line yourself by passing a string that starts with '\r'. Below you will see the beam shift calibration running completely correctly in the GUI console:

image

This has an added benefit (i.e. my original reason to look at it): it makes the GUI faster! For very long lines, tkinter can take surprisingly long to calculate line wrapping. As an example, I ran a 15 x 15 = 225-point beam shift calibration on 2 PCs, my work PC1 and the microscope-handling PC2. After ~200 points, writing to the console on PC2 took longer than the rest of the calibration step!

image

The only caveat with this PR is it makes the GUI actively remove lines from the console. This means that if the user clicks somewhere in the console during tqdm or printer loop, the console might start removing random lines and appending new info at the end. Previously it would only write in a completely wrong place. Given that the console itself is very wonky and user can just as well remove data by hand (because it is a normal text field), I am not particularly concerned with it.

Note: you can test the feature using Advanced tab -> run custom script -> 'showcase_movie.py', left in src/instamatic/config/scripts/showcase_movie.py after PR #121. I can see not time improvements there, but the caret return works as intended.

Bug fixes

  • Carriage return '\r' is now correctly handled by the Instamatic GUI, making tqdm and printer output much nicer;
  • ANSI escape sentences are not handled by console and thus removed instead of being printed as text.

@Baharis Baharis self-assigned this May 16, 2025
@Baharis Baharis requested a review from stefsmeets May 16, 2025 18:04
@Baharis Baharis added the bug label May 16, 2025
Copy link
Member

@stefsmeets stefsmeets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, nice fix! Somewhat surprising that this seems more efficient also.

@Baharis
Copy link
Member Author

Baharis commented May 21, 2025

I was also surprised. In the showcase_movie.py, on my work computer there is little to no difference, but on the older PC turning on tqdm or reverting this fix can make a 100-frame 0.1s movie take up to ~25 seconds if I remember correctly.
If there are no further comments I believe this is ready to be merged, I can do it later.

@Baharis Baharis merged commit a46596a into instamatic-dev:main May 21, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants