QCElemental icon indicating copy to clipboard operation
QCElemental copied to clipboard

Accesing qc.models.Molecule.show()

Open VHchavez opened this issue 2 years ago • 5 comments

Hello, We are using Psi4 within ChemCompute at the latest MolSSI workshop in Texas. We can visualize geometries with NGLViewer using the show method of the qc.models.Molecule class. Currently, we define a function to use as a shortcut:

def molshow(psi4geo):
    molplot = qc.models.Molecule.show(psi4geo)
    return molplot

where psi4geo is a psi4.core.Molecule

A quick set of questions:

  1. Is there a better way of doing this that we are missing?
  2. How hard would it be to add this functionality to the Psi4 molecule, e.g., psi4.core.Molecule.show(). I'm uncertain of much interplay exists between the two Classes.

I'd be happy to work on a PR, but any advice would be appreciated :) Thanks.

VHchavez avatar Jun 16 '22 04:06 VHchavez

I'm not quite following your code. What is psi4geo -- str, psi4 Mol, etc.? The below works. I don't see how you're creating the qcel.models.Molecule object in your snippet.

import qcelemental as qcel

psi4geo = """
He 0 0 0
"""

molplot = qcel.models.Molecule.from_data(psi4geo).show()

print(molplot)

loriab avatar Jun 16 '22 14:06 loriab

Ope, sorry about that. It is a psi4 geometry:

import psi4
# Using the function definition from above. 
benzene = psi4.geometry("pubchem:benzene")
molshow(benzene)

VHchavez avatar Jun 16 '22 14:06 VHchavez

Ok, at first inspection, I didn't think your molshow would work at all. Inspecting further, psi4geo is taking the argument of self, and since the only operations on self in the show fn is to_string, and psi4.core.Molecule also has a to_string, your fn works.

Therefore, I think you could make a psi4molinstance.show() work by attaching it to the class like these other mol extensions. https://github.com/psi4/psi4/blob/master/psi4/driver/molutil.py#L227 It'd be good to also have a test case in the psi4 tests and a "using" fn for nglview in /tests/pytests/addons.py

loriab avatar Jun 17 '22 05:06 loriab

Oh wow, that's pretty nifty! I've never added functions in this fashion. It works just fine 🐶.

Questions:

  1. To add the "using" test, do I need to simply add NGLview as a new key here?
  2. What would be an appropriate test? Assert that an nglview.widget.NGLWidget object gets returned?

Thanks.

VHchavez avatar Jun 21 '22 02:06 VHchavez

To add the "using" test, do I need to simply add NGLview as a new key here?

Yes, that should work fine.

What would be an appropriate test? Assert that an nglview.widget.NGLWidget object gets returned?

Agreed, just check the type returned.

loriab avatar Jun 23 '22 19:06 loriab

@VHchavez has your issue been fully addressed? Can I close now? Just doing some cleanup on old issues :) Thanks!

coltonbh avatar Mar 29 '23 05:03 coltonbh

@VHchavez thank you!

coltonbh avatar Mar 29 '23 05:03 coltonbh