pythonscipyscipy-spatial

Management of scipy.spatial.distance.cdist output


Good morning everyone, if I am writing something in a wrong way please let me know. However I am using the afore mentioned method in python to get pairwise CA-CA distances of two sets of residues in a pdb file, sometimes there are two CA due to incertainties and this will change the output from:

[[X]]

to:

[[X X]]

where X are floats, sometimes the output is [[]] and some other times is [], how can manage all these exceptions in a smart way? tried to convert those to string and .strip something here and there, didn't really work, at the moment the error I get is the following:

if distmap[0][0]:

IndexError: index 0 is out of bounds for axis 0 with size 0

Which should be due to the [] case, I need to append these values to a list in a str(round(float(X),2)) format. The blank cases I would prefer to not be reported as 0 but for istance an insanely huge number such as 100, or not reported at all. Please let me know if you need more info and if I need to change the form of my question


Solution

  • Can you try the following:

    if distmap.size == 0:
        # Do something when distmap is empty
    

    You can also use distmap.shape to identify its dimension.