openPMD / openPMD-viewer

:snake: Python visualization tools for openPMD files
https://openpmd-viewer.readthedocs.io/
Other
67 stars 50 forks source link

warning when reading particle data #396

Closed PrometheusPi closed 1 year ago

PrometheusPi commented 1 year ago

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.

max-lehman14 commented 1 year ago

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')
RemiLehe commented 1 year ago

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!

PrometheusPi commented 1 year ago

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

RemiLehe commented 1 year ago

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.

RemiLehe commented 1 year ago

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

RemiLehe commented 1 year ago

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 1 year ago

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 1 year ago

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
RemiLehe commented 1 year ago

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

ax3l commented 1 year ago

Thanks, happy to review!

PrometheusPi commented 1 year ago

Thanks for fixing this warning.