Closed luhtzia closed 5 months ago
@luhtzia Please try to sleep
after calling v.frame=int(n)
(this is non-blocking)
@hainm that doesn't make a difference, unfortunately - still identical images are being saved
@hainm that doesn't make a difference, unfortunately - still identical images are being saved
@luhtzia Can you please provide a full example (with input files if needed) so I can reproduce and play with it? thanks
@hainm thank you - I uploaded everything as an example here: https://seafile.rlp.net/d/56a4a0caa4764ba9a146/
def generate_images(v=view_b):
l=[]
for n in np.arange(0,20,5):
print(n)
v.clear()
v.frame=int(n)
time.sleep(5)
for item in ALL_BTB:
view_b.add_cartoon(item.residues,color="green") #coordinates
for item in ALL_BACK:
view_b.add_cartoon(item.residues,color="orange") #coordinates
for item in ALL_PEI1:
view_b.add_surface(item.residues, color="lightblue",opacity=0.1,depthWrite=True)
for item in ALL_PEI2:
view_b.add_surface(item.residues, color="lightgrey",opacity=0.1,depthWrite=True)
v.add_unitcell()
v.center()
im0 = v.render_image()
time.sleep(5)
l.append(im0)
im0=None
v.clear()
for im in l:
while not im.value:
time.sleep(0.1)
for n, im in zip('ABCD', l):
with open(f'figure_{n}.png', 'wb') as fh:
fh.write(im.value)
import threading
thread = threading.Thread(
target=generate_images,
)
thread.daemon = True
thread.start()
however if I let this run while moving to a different tab the files are saved only once I return to the tap and I get the issue with identical figures: W,X,Y,Z (see example folder). Is there a solution such that I can execute this in the background?
in all cases the view is a little different in every image saved - how can I define and fix this?
however if I let this run while moving to a different tab the files are saved only once I return to the tap and I get the issue with identical figures: W,X,Y,Z (see example folder). Is there a solution such that I can execute this in the background?
What does "moving to a different tab" mean? does this mean your execute another Python code in another tab. This won't work for python. Basically, we are using thread
for run the rendering code. This code has sleep
period, and when you run code in another tab, this will block the code execution from the generate_images
, so nothing will be actually done. So you will end up get the same image as the first one. There is no currently solution for this and I don't know (yet) how to run thing in background. Working with asynchronous code is hard and I am not good at that.
in all cases the view is a little different in every image saved - how can I define and fix this?
I think it's because you call center
multiple times. May be try to only call one right after creating the view?
Basically add center
here
And remove it from here:
PS: try to sleep less first, instead of 5s
.
@hainm no with moving to a different tab I meant moving away from the tab where I run the jupyter lab to e.g. writing an email in a different tab, not executing any other python code. I figured out this is related to Firefox though and I can do that when using Chromium, also the rendering of the same image over and over again seems to be related to Firefox. Im using Chromium now and I run a test case with a more realistic example (see test_rendering.jpynb in the provided folder), but unlike in example_rendering.ipynb it is not saving any files. I like nglview very much - but I've been spending a vast amount of time trying to get a rendering in order to create a movie out of my views. Is what you're saying that I need to make sure that there is no other python process running on my machine at the same time?
Is what you're saying that I need to make sure that there is no other python process running on my machine at the same time?
I mean about try not to run another python code in the same notebook you are using while rendering.
I figured out this is related to Firefox
I see. I am using Chrome.
but I've been spending a vast amount of time trying to get a rendering in order to create a movie out of my views.
Good luck with this. If you have a good idea, feel free to share. thanks.
@hainm thank you - I will have a look, are you seeing an obvious difference between test_rendering.jpynb and example_rendering.ipynb here: https://seafile.rlp.net/d/56a4a0caa4764ba9a146/ ? what is the current status of the movie maker ansatz?
@luhtzia it's due to your Python code, IMO. lab=str(int(n))
doesn't work. I think this is a limitation of using thread to execute the function. The error won't be raised if having a code error.
Good luck with debugging.
Version report print(ipywidgets.version)' 8.1.0 print(nglview.version)' 3.0.6 -Environment Python 3.11.2 IPython : 8.14.0 ipykernel : 6.25.0 ipywidgets : 8.1.0 jupyter_client : 8.3.0 jupyter_core : 5.3.1 jupyter_server : 2.7.0 jupyterlab : 4.0.3 nbclient : 0.8.0 nbconvert : 7.7.3 nbformat : 5.9.2 notebook : 7.0.1 qtconsole : 5.4.3 traitlets : 5.9.0
I'm trying to save different frames of a view as files following the approach suggested in the FAQ https://github.com/nglviewer/nglview/blob/master/docs/FAQ.md#how-to-make-nglview-view-object-write-png-file -I give an example of how this works well for two individual frames (code example A) but not as I loop through a series of frames (code example B) -in example B I experience the issue is that the same image is saved over and over again - how can I avoid this or what would be a good way to save (not download!) larger amounts of frames of a particular view as image files in order to manually render a movie? -I also experienced that I need to run or refresh the first cell immediately before running the second in order for example A to work -here are the examples: In both examples I setup an initial view in first cell
first cell
examples of the second cell
example A (working)
works
example B (not working)
doesn't work with above