Skip to content

[Bug] Underflow: Negative value in van_rossum_distance function #679

@xxcStella

Description

@xxcStella

Describe the bug

When uses the van_rossum_distance funtion in elephant.spike_train_dissimilarity, it may generate "nan" result and give error like "RuntimeWarning: invalid value encountered in sqrt return np.sqrt(XXX"). This is because there is no zero-checking function before squaring root, making it possible to square root an extremely small number (e.g. 1e-16).

To Reproduce

  1. Give a spike train with small values:
  import neo, quantities as pq
  from elephant.spike_train_dissimilarity import van_rossum_distance

  tau = 0.1 * pq.s
  a = neo.SpikeTrain([0.1782, 0.2286, 0.2804, 0.4972, 0.5504]*pq.s, t_start=0*pq.s, t_stop=4*pq.s)
  print(van_rossum_distance([a, a], tau)[0, 1])
nan
RuntimeWarning: invalid value encountered in sqrt return np.sqrt(vr_dist)

Expected behavior

In theory, the result should be 0. But the value is nan and the code gives error: RuntimeWarning: invalid value encountered in sqrt return np.sqrt(vr_dist)

Solution
Before the return in van_rossum_distance function, add vr_dist[vr_dist < 0] = 0.0. This can clip any value lower than 0.

Environment

  • OS: Windows 11
  • How you installed elephant (conda, pip, source): pip
  • Python version: 3.8.20
  • neo python package version: 0.13.3
  • elephant python package version: 1.1.1 (latest)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions