openPMD/openPMD-viewer

Particle filter is not applied if dataset contains only one particle

Opened this issue · 1 comments

If particle data for a particular species contains only one particle, the select filter is not taken into account.

Take, for example, attached data which is an OpenPMD file containing a single particle dataset data/2997/particles/electrons_oxygen_7 with only one particle (all other datasets have been removed). Try reading with openPMD-viewer:

from openpmd_viewer import OpenPMDTimeSeries
ts = OpenPMDTimeSeries('hdf5/')
iteration = ts.iterations[0]
data = ts.get_particle(['uz'], species='electrons_oxygen_7', iteration = iteration, select={'uz' : [20, 150]})

Now data contains:

[array([0.4946678])]

The expected result is data should be empty as the value of 0.49 is not between 20 and 150, so this single data point should have been filtered out.

This happens because the utilities.apply_selection has the following condition which specifically ignores the filter for the case of 1 particle:

# Use select_array to reduce each quantity
for i in range(len(data_list)):
    if len(data_list[i]) > 1:  # Do not apply selection on scalar records
        data_list[i] = data_list[i][select_array]

Perhaps this condition is relevant to some other cases when the record is truly scalar. But in my case, the record is not scalar, just sometimes for some iterations and species the number of particles in species turns out to be exactly equal to 1, and then I get incorrect results. Correspondingly, for datasets with 0 or 2+ particles, filtering works correctly, so only 1 particle is problematic.

ax3l commented

Oh, good catch!

@RemiLehe and I took a look at it. The comment is a typo, it meant to say skip constant records.

Anyway, we also want to filter constant records 😅

We will correct that, thx @RemiLehe for volunteering.