I'm trying to find the distances between residues of a protein in pymol using a python script, which calls the pymol command cmd.get_distance. However, sometimes there are multiple atom assignments, which causes an error:
GetDistance-Error: Selection 2 doesn't contain a single atom/vertex.
I want to skip over sites that have this problem so I'm trying to use try/pass:
try:
cmd.get_distance(atom2)
except GetDistance-Error:
pass
However, it tells me that there is no such error message:
NameError: global name 'GetDistance' is not defined.
How do I tell it to pass this error? isn't GetDistance-Error the error?
I stumbled over the same issue. I want to catch the error from pymols cmd.get_distance. In my case, one (or both) of the selections does not contain an atom at all (instead of 2).
This answer will not clarify handling error in pymol scripting but resolve the issue itself.
So instead of catching the error, you could check before if there is exactly 1 atom in your selection.
Erroneous code:
PyMOL>dist = cmd.get_distance("///A/44/CA","///A/60/CA")
GetDistance-Error: Selection 1 doesn't contain a single atom/vertex.
Optimized code:
PyMOL>p1 = cmd.select("p1","///A/44/CA")
PyMOL>p2 = cmd.select("p2","///A/60/CA")
PyMOL>dist = "N/A"
PyMOL>if p1 == 1 and p2 == 1: dist = cmd.get_distance("p1","p2")
So instead of catching the error, you check the requirements (one atom per selection) for the get_distance command. If you run a continuous code, the dist = "N/A" is needed, otherwise you will falsely get the last assignment of the distance.
Related
One of the challenges I'm having is getting a stack trace or examining variables which should be in scope when an error occurs. However I am not finding this is the case. For example if a piece of code fields, I'd like to be able to see where in the loop in fails. However despite using %debug, I can never get any values out.
For example my code:
if a[field].to_list()[0] == b[field].to_list()[0]:
result = True
fails, and I'd like to know what the value of field is. But I can never find a way to make this work using %debug.
Maybe this try and except block helps you
for field in fields:
try:
if a[field].to_list()[0] == b[field].to_list()[0]:
result = True
except Exception as e:
print(field)
print(e)
I'm trying to plot netCDF data using cartopy + matplotlib (contourf) call, however on some occasions the data generate invalid polygon instances through contourf and the following error is thrown (Sample), followed by other exceptions:
TopologyException: side location conflict at -83.68749999996696 36.937499999989356
When running the code using contour (not contourf), the plot works, and I can see the invalid geometry in the contours. I am able to address this issue in the contourf call by NAN-ing the value of the point at the location of the side-location conflict.
aLatName = "PRISM_Latitude"
aLonName = "PRISM_Longitude"
tLat = 36.937499999989356
tLon = -83.68749999996696
aLat = np.abs(array[aLatName] - tLat)
aLon = np.abs(array[aLonName] - tLon)
c = np.maximum(aLon, aLat)
([xloc], [yloc]) = np.where(c == np.min(c))
array["data"][yloc, xloc] = np.nan
I'm wondering if there is a way to capture the coordinates of the side location conflict in a try-except block so I can then try the nan approach to fix the issue.
5/30 Edit: I did some rabbit hole digging through the call stack to find out more information and it seems like the error message shown above is not a python exception being thrown but an exception being thrown in C++ and then passed along through the python interpreter, specifically the GEOS library (https://github.com/libgeos/geos/blob/70b1662e9b8f5118f9eef26529e9eeb9eb466544/src/geomgraph/EdgeEndStar.cpp#L312). From what I know about C++ / Python exceptions, I cannot directly "catch" this exception in python unless it exists as a python object already so it looks like this is a dead end, but perhaps this may provide some information to others who may be stuck with a similar problem.
I am writing a python tool that will store attributes of the stereoscopic camera in a scene into a JSON file.
I use this code to get the selected plane with the attributes I need to store
import maya.OpenMaya as om
selected = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selected)
obj = om.MObject()
selected.getDependNode(0,obj)
sel = om.MFnDependencyNode(obj).name()
if sel != "npl_Near_ZP":
cmds.confirmDialog(title= 'Error:', message = 'Error: Please Select the correct plane', button =['OK'])
else:
jsonCreate(sel)
It was working perfectly before but now when I try to the script, I get the following runtime error and I 've sort of hit a wall here cause I don't understand what the problem is
Error: RuntimeError: file S:\Maya_2018_DI\build\Release\runTime\Python\Lib\site-packages\maya\OpenMaya.py line 18296: (kInvalidParameter): Index not in valid range #
I m still new to Maya so any help will be greatly appreciated
update I restarted my computer and the script is now running as it should. Can anyone explain this behaviour in case it happens again
I tried writing the code for a problem, but the module won't run. It says invalid syntax, but it's not highlighting anything.
The code: http://pastebin.com/cJVNBcYE
The problem: http://pastebin.com/p8E0E0Nj
I don't understand why it's not working.
I have numDealers set as a variable so that info can be entered in the program. The arrays are all defined. I have index=0 and x=1 to set up the loop for the numDealer arrays for sales and commission. I have another array=index section to calculate commissions. And then I have the prints set up.
Why isn't the program working? I don't understand.
Please post code in future, with a full traceback of the error. However:
else print(sales[index]) and print(comm[index])
should be:
else:
print(sales[index]) and print(comm[index])
i.e. you are missing a colon
I'm a bit puzzled by the and. It means that the second print will only be executed if the first fails (unlikely). Did you mean:
else:
print(sales[index])
print(comm[index])
?
By the way, it appears you are not using arrays but lists. The Python standard library includes a module called array https://docs.python.org/3/library/array.html which you do not appear to be using. So don't have a list called array, that collides with the standard library module name, and can cause no end of confusion.
def travel():
travel.s=0
travel.frate=[]
travel.tr=[]
def accomodation():
print"""specialises
1.place 1
a.hotel 1
b.hotel 2
Hotel1:ac/non ac rooms
Ac.for ac...
Noac.for non ac...."""
hd=[5000,6000]
hg=[4000,7000]
TAc=[1000]
Nac=[400]
ch5=input("Enter your choice")
fav=raw_input("ENter hotel choice")
mode=raw_input("Enter ac/no ac")
if(ch5==1):
for i in hd:
frate=hd[i]+TAc
else:
frate=hd[i]+Nac
if(ch5==2):
for i in range(0,2,1):
frate=hg[i]+TAc
else:
frate=hg[i]+Nac
accomodation()
travel()
When i run the program , i get the error as List Index out of range.but in hd and hg list,there are only two elements, so index number will be from 0 right?? Is there anything i should import?? I even gave statements like this:
travel.frate=travel.hg[i]+TAc
but it still doesn't come.Thank you for your effort.
the indentation is proper now,but the output is still not coming.
The specific issue you are encountering is caused by these lines here:
if(ch5==1):
for i in hd:
frate=hd[i]+TAc
hd[i] looks at hd[5000] and hd[6000] which will throw an IndexError.
But if you fix that, you're going to run into another error, because
frate=hd[i]+TAc
tries to add a list TAc to an integer hd[i], which is an unsupported operation. You probably need frate=hd[i]+TAc[0], or even better, make TAc a number rather than a list. This issue occurs elsewhere in your code as well.
Finally, while not causing explicit issues in your code right now, there are two other problems:
ch5=input("Enter your choice") is dangerous since input tells Python to run whatever code snippet the user enters. In this case, much safer to do ch5=int(raw_input("Enter your choice"))
for i in range(0,2,1): is really for i in range(2): - only use the starting index and jump parameters if you need to change them from their default, namely 0 and 1.
There are other issues like variable scope (you're expecting travel.frate to be modified from within accommodation, but that's not happening) and defining variables in functions like travel.frate (not invalid syntax, but definitely strange) but those are probably better addressed outside of this question.