openPMD/openPMD-viewer

warning when reading particle data

Closed this issue · 11 comments

When I read openPMD-api PIConGPU data with the openPMD viewer, I get the following warning

<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast

when executing

from openpmd_viewer import OpenPMDTimeSeries 

ts = OpenPMDTimeSeries("run07/simOutput/openPMD/")
x, y, z = ts.get_particle( var_list=['x','y', 'z'], iteration=0, species='e') 

The position values look OK.

I am using openPMD-viewer 1.7.0 installed via conda on ipython 8.15.0 and python 3.11.5.

I got a more detailed error message:

envs/openPMD/lib/python3.10/site-packages/numpy/core/numeric.py:330: RuntimeWarning: invalid value encountered in cast
  multiarray.copyto(a, fill_value, casting='unsafe')

Thanks for reporting this issue @PrometheusPi.
Would you be able to post an openPMD file that exhibits this problem, so that I can try to reproduce it?

Thanks!

here is a minimal time series that causes the warning openPMD.zip

OK, thanks a lot.
When extracting the traceback, I see that it comes from these lines:

  File "/Users/rlehe/miniconda3/envs/jupyterlab/lib/python3.11/site-packages/openpmd_viewer/openpmd_timeseries/data_reader/io_reader/particle_reader.py", line 88, in read_species_data
    offset = get_data(series, species['positionOffset'][component_name])
  File "/Users/rlehe/miniconda3/envs/jupyterlab/lib/python3.11/site-packages/openpmd_viewer/openpmd_timeseries/data_reader/io_reader/utilities.py", line 67, in get_data
    data = np.full(record_component.shape, np.nan, record_component.dtype)

i.e. when reading positionOffset, openPMD-viewer first creates an array of nan, which is later filled with the actual data. The issue here, though, is that positionOffset seems to be of type int32 in files created by PIConGPU, and thus np.full(record_component.shape, np.nan, record_component.dtype) is trying to cast np.nan to int32, which triggers the warning.

Maybe the fix is to first check the type of record_component, and then create an array of 0 if it is int-like, and an array of np.nan if it is float-like.

Any opinion @ax3l ? I think that the lines of code involved here were introduced in this PR:
https://github.com/openPMD/openPMD-viewer/pull/334/files

Note that the same warning can be obtained outside of openPMD-viewer, by doing:

import numpy as np
x = np.full( (10,), np.nan, np.int32 )
ax3l commented

Thanks for the traceback, yes I just reproduced it as well for int dtypes.
I would change it to NaN for float dtypes and zero (as invalid value) for all others types.

ax3l commented

We need to update two locations, because np.full_like also warns:

x = np.full([2,3], 0, dtype=np.int32)

In [10]: y = np.full_like(x, np.nan)
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast

OK, sounds good. I can do a corresponding PR.

ax3l commented

Thanks, happy to review!

Thanks for fixing this warning.