cctbx_project icon indicating copy to clipboard operation
cctbx_project copied to clipboard

Treat None as NaN in flex?

Open dagewa opened this issue 3 years ago • 1 comments

NumPy translates Python None to NaN transparently:

>>> numpy.double([None,])
array([nan])

Flex does not support this

>>> flex.double([None,])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be real number, not NoneType

This came up in the context of constructing a flex array in a generator that might return None: https://github.com/dials/dials/blob/02d67d881ddbb12061d27a16e4ac4a4419e86676/util/resolution_analysis.py#L228-L230

Is the NumPy behaviour desirable? One advantage is that NaN might be handled appropriately by plotting libraries. A disadvantage might be that it is conflating concepts: None is not-aNaN. This seems worth discussing.

If the conclusion remains to avoid converting None to NaN then error messages could be tidied up and made consistent (flex.double is different from other constructors here):

>>> flex.int([None,])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type NoneType

also

>>> a=flex.double([1,])
>>> a[0] = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

dagewa avatar Jan 26 '21 17:01 dagewa

After our meeting, I dug a little deeper and it looks like flex.bool can use None and converts it to False.

>>> b = flex.bool([None, None])
>>> list(b)
[False, False]

I'll have to check the numerical flex types some more.

Maybe that's how we're using None for selections.

bkpoon avatar Jan 26 '21 18:01 bkpoon