Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage questions about nglview: Please ask here first [1] #785

Open
hainm opened this issue Apr 22, 2019 · 33 comments

Comments

Projects
None yet
9 participants
@hainm
Copy link
Collaborator

commented Apr 22, 2019

For any questions relating to how/why/ ... Or anything you don't know where to ask.
For bug, suggestion, ... please open seperate issue

PS: QA-0: #589

@njbruce

This comment has been minimized.

Copy link

commented Apr 25, 2019

If I create a sphere with eg:

view = nglview.NGLWidget()
view.shape.add_sphere([0.0, 0.0, 0.0], [0,0,1], 10.0)

is there a way of setting the opacity of the sphere?

Thanks in advance!

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 25, 2019

hi, there's no API yet. But you can try with below code (using the latest version).

shape

shape.ipynb.txt

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 25, 2019

ah, there's actually an API for it but you have to play with it with more complicated case (mixing structure, shape, ...).

shape1

@njbruce

This comment has been minimized.

Copy link

commented Apr 26, 2019

ah, there's actually an API for it but you have to play with it with more complicated case (mixing structure, shape, ...).

Great, thanks!

@jbloom

This comment has been minimized.

Copy link

commented Apr 28, 2019

I'm trying to figure out the best way to set the orientation. Here is my current procedure. It works. However, can you let me know if this is indeed the best way. The reason that I wasn't sure is that it uses two private methods:

  1. Manually set the orientation to whatever I want by rotating the object.

  2. Get that orientation (as a 16-number list) with:

        mat = view._camera_orientation
    
  3. In the future, set that orientation with:

       view._set_camera_orientation(mat)
    
  4. If I want to make sure it is also centered, call:

      view.center()
    
@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 28, 2019

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 29, 2019

view._set_camera_orientation(mat)

@jbloom I've checked and we should use view.control.orient(...) function.

view.control.orient(mat) # from view._camera_orientation
@mktonycho

This comment has been minimized.

Copy link

commented May 14, 2019

Hi everyone,
I am using gro and xtc files from gromacs and visualize them using nglview within Jupyter notebook.
It is unclear why rendered and downloaded images from nglview package contain white slits as you may be able to notice them on the second picture. It's not too big of a problem. Thank you for your help in advance!

The version of packages is as follows:
nglview version = 1.1.9
mdanalysis version = 0.19.2

nglview_prob

@jbloom

This comment has been minimized.

Copy link

commented May 14, 2019

I also had @mktonycho's problem with vertical lines, although they go away if I use antialias=True with render_image.

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 14, 2019

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 14, 2019

It is unclear why rendered and downloaded images from nglview package contain white slits as you may be able to notice them on the second picture. It's not too big of a problem.

@mktonycho should be fixed in master branch (by upgrading to NGL 0.36).

@mktonycho

This comment has been minimized.

Copy link

commented May 16, 2019

@jbloom @hainm Thank you so much for your help!!! I will try again with the new version!

@rasmusthog

This comment has been minimized.

Copy link

commented May 29, 2019

Hi! Not sure if this should be submitted as a separate issue, or if there is something I'm not doing correctly that can easily be fixed on my side.

I'm trying to run nglview in a Jupyter Notebook through ASE using nglview.show_ase(atoms) where atoms is the ASE Atoms-object I want to view.

When I make this call, I get a permission error is thrown (pasted below).

Appreciate any help I can get here.


PermissionError Traceback (most recent call last)
in
----> 1 nglview.show_ase(co)

C:\Anaconda3\lib\site-packages\nglview\show.py in show_ase(ase_atoms, **kwargs)
84 """
85 structure = ASEStructure(ase_atoms)
---> 86 return NGLWidget(structure, **kwargs)
87
88

C:\Anaconda3\lib\site-packages\nglview\widget.py in init(self, structure, representations, parameters, **kwargs)
201 else:
202 if structure is not None:
--> 203 self.add_structure(structure, **kwargs)
204
205 if representations:

C:\Anaconda3\lib\site-packages\nglview\widget.py in add_structure(self, structure, **kwargs)
1089 raise ValueError(
1090 f'{structure} is not an instance of Structure')
-> 1091 self._load_data(structure, **kwargs)
1092 self._ngl_component_ids.append(structure.id)
1093 if self.n_components > 1:

C:\Anaconda3\lib\site-packages\nglview\widget.py in _load_data(self, obj, **kwargs)
1196 if not is_url:
1197 if hasattr(obj, 'get_structure_string'):
-> 1198 blob = obj.get_structure_string()
1199 kwargs2['ext'] = obj.ext
1200 passing_buffer = True

C:\Anaconda3\lib\site-packages\nglview\adaptor.py in get_structure_string(self)
102
103 def get_structure_string(self):
--> 104 return _get_structure_string(self._ase_atoms.write)
105
106

C:\Anaconda3\lib\site-packages\nglview\adaptor.py in _get_structure_string(write_method, suffix)
29 def _get_structure_string(write_method, suffix='.pdb'):
30 with NamedTemporaryFile(suffix=suffix) as fh:
---> 31 write_method(fh.name)
32 return fh.read().decode()
33

C:\Anaconda3\lib\site-packages\ase\atoms.py in write(self, filename, format, **kwargs)
1852 """
1853 from ase.io import write
-> 1854 write(filename, self, format, **kwargs)
1855
1856 def iterimages(self):

C:\Anaconda3\lib\site-packages\ase\io\formats.py in write(filename, images, format, parallel, append, **kwargs)
380 io = get_ioformat(format)
381
--> 382 _write(filename, fd, format, io, images, parallel=parallel, append=append, **kwargs)
383
384

C:\Anaconda3\lib\site-packages\ase\io\formats.py in _write(filename, fd, format, io, images, parallel, append, **kwargs)
411 if append:
412 mode = mode.replace('w','a')
--> 413 fd = open_with_compression(filename, mode)
414 io.write(fd, images, **kwargs)
415 if open_new:

C:\Anaconda3\lib\site-packages\ase\io\formats.py in open_with_compression(filename, mode)
306
307 if compression is None:
--> 308 return open(filename, mode)
309 elif compression == 'gz':
310 import gzip

PermissionError: [Errno 13] Permission denied: 'C:\cygwin64\tmp\tmp7fatov62.pdb'

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 29, 2019

hi @rasmusthog, you can do either

  • make sure your C:\cygwin64\tmp writable by python
  • patch the get_structure_string function to write file to your current folder.

something like

def get_structure_string(self):
    print("HELLO")
    fname = 'tmp.pdb'
    self._ase_atoms.write(fname)
    with open(fname) as fh:
        return fh.read()
    
nv.adaptor.ASEStructure.get_structure_string = get_structure_string
@jaygn

This comment has been minimized.

Copy link

commented May 29, 2019

Hi,

I'm trying to get nglview up and running and I'm probably overlooking something simple...

I created an environment with conda and installed juypter, pytraj and nglview. Everything installed smoothly. When I run the tutorial, I see the expected output from the first cell (nglview version = 1.1.7, pytraj version = 2.0.4)

However, after the third cell, (view._display_image()), I get a broken image icon. I tried running the same command sequence in ipython and got <IPython.core.display.Image object> as the output, but again, no image. I've tried with both python 2.7 and 3.7, both locally and remotely, and with the tutorial files and my own and I get the same behavior in all scenarios. The notebook is running on ubuntu 18.04.

Thanks for your help

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 29, 2019

@jaygn Please post your screenshot.
(But I guess you have to run render_image and _display_image in different cell, make sure there's a small delay between commands).

But you can try the latest version via (v2.1.0), you can see the rendered image without using _display_image.

example: #808

@jaygn

This comment has been minimized.

Copy link

commented May 29, 2019

Thanks @hainm. Here's the screenshot. I tried both with render_image and _display_image in the different and the same cells. I get the same behavior when I run the tutorial notebook in place or if I copy the commands to a new notebook and run it there.

Screen Shot 2019-05-29 at 12 54 10 PM

I tried updating to 2.1.0 and I still don't see the image, although now it's trying to display after the render_image step as you said.

Screen Shot 2019-05-29 at 1 07 35 PM

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 29, 2019

sorry for your inconvenience. Can you try

  • close your notebook
  • activate nglview-js-widgets (I've heard that you don't need that with the latest notebook, not sure)
jupyter-nbextension install nglview --py --sys-prefix
jupyter-nbextension enable nglview --py --sys-prefix
  • open your notebook and try again.
@jaygn

This comment has been minimized.

Copy link

commented May 29, 2019

That worked! It looks great now. Thanks so much for the prompt help!

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 29, 2019

thanks for trying.

@rasmusthog

This comment has been minimized.

Copy link

commented May 30, 2019

Thanks, @hainm!

I have now modified the permissions (changed the ownership of the tmp-folder to my user instead of the local Administrator account), but I'm still getting the same error. Also tried changing the permissions so that the tmp-folder is globally readable (chmod -R 777), but still the same result.

This is how the folder looks from a ls -l:

drwxrwxrwx+ 1 [my username] [computer name]+None 0 May 30 10:27 tmp

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 30, 2019

sorry @rasmusthog that I can't not help further with the permission error because I don't have any windows machine to test (I am mainly working with linux and macos). But please try the patch I mentioned. cheers.

@garyo

This comment has been minimized.

Copy link

commented May 31, 2019

I'm embedding NGL. I'm loading a model like this (in an async typescript function):

  let obj = await stage.loadFile(name, { defaultRepresentation: false })
  obj.setDefaultAssembly('BU1') 
  representations.forEach((elt, idx) => {
    obj.addRepresentation(elt[0] as NGLViewer.StructureRepresentationType, elt[1])
  })
  obj.autoView()

but when I dump the Three.js scene (stage.viewer.scene) immediately after this, the graphics objects don't exist yet. I think they're getting created in the background.
Is there any callback or hook so I can know when the world is fully loaded?

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 31, 2019

@garyo

This comment has been minimized.

Copy link

commented May 31, 2019

Thanks for the example -- but I don't see there how to invoke a callback once all the graphics objects are loaded. If you dump the scene at the end of that example, just after o.autoView(), it will not have any meshes. Actually in your simple example it may, but if you use a 'surface' representation or something else that requires a bg task to compute, it won't be there yet.

@garyo

This comment has been minimized.

Copy link

commented May 31, 2019

Looking at the source, in representation.ts representation.make() takes an optional callback to call when it's done, but nobody calls it with a callback arg as far as I can see.

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented May 31, 2019

@garyo I see. Do you mind opening the issue in NGL repo (we are in nglview repo now)? There are many experts there.

https://github.com/arose/ngl/issues

@hainm hainm changed the title Usage questions: Please ask here first [1] Usage questions about nglview: Please ask here first [1] May 31, 2019

@garyo

This comment has been minimized.

Copy link

commented May 31, 2019

ok, done -- thanks

@yamasakih

This comment has been minimized.

Copy link

commented Jun 3, 2019

Hi,

I want to ask if I can display hydrogen bonds with nglview.

I thought the add_distance or add_contact method played that role but is it not?

Thanks for your help.

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 4, 2019

@yamasakih yes, you can use add_contact for that (v.add_contact(hydrogen_bond=True))

You can try the NGL website for different types of contacts (Choose "Examples" -> "representation/contact"). cheers.

@yamasakih

This comment has been minimized.

Copy link

commented Jun 4, 2019

It worked! Thanks so much for your prompt help!

@tonyyzy

This comment has been minimized.

Copy link

commented Jun 17, 2019

Hi,

[EDIT] I have found view._set_size() works.

I tried to use view.layout.height = '600px' to adjust the view height but it doesn't work. The widget height is increased but the viewport(?) height is not changed. The width control works. Is there any other ways I can change the height?

Screen Shot 2019-06-17 at 12 40 37

Thanks!

@hainm

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 17, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.