Skip to content

IntervalSet dropping metadata when index or slicing is not sorted #560

@lilytong0919

Description

@lilytong0919

I understand that the IntervalSet will drop metadata when there are changes in the content of the intervals. However, in the case of slicing and indexing in a reverse or unsorted indices, I think it would be more reasonable for the IntervalSet to be able to handle it properly and keep the metadata.

import pynapple as nap
import numpy as np
import pandas as pd

# Create test IntervalSet with metadata columns
start_times = np.array([0, 5, 10, 15, 20, 25, 30])
end_times = np.array([2, 8, 12, 18, 23, 28, 35])

# Create the IntervalSet
iset = nap.IntervalSet(start=start_times, end=end_times)

# Add metadata columns
iset['label'] = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
iset['amplitude'] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
iset['frequency_mean'] = [100, 200, 300, 400, 500, 600, 700]

print("Original IntervalSet:")
print(iset)
print()

# Test slicing with unsorted indices
print("=" * 50)
print("UNSORTED INDEX SLICING TESTS")
print("=" * 50)

# 1. Unsorted list of indices
print("\n1. Unsorted indices [4, 1, 6, 2]:")
unsorted_idx = [4, 1, 6, 2]
print(iset[unsorted_idx])

# same indices but sorted
print("\n same indices but sorted")
sorted_idx = np.sort(unsorted_idx)
print(iset[sorted_idx])

# 10. Negative step slice
start = 5
end = 1
print(f"Negative step slice (iset[{start}:{end}:-1]):")
print(iset[start:end:-1])

# Positive step slice
print(f"Positive step slice (iset[{end+1}:{start+1}:1]):")
print(iset[end+1:start+1:1])

My proprosal is to potentially change the behavior of the __get_item__() function we inheritated from the superclass so that when indexing/slicing make sure the keys are are already sorted before we apply them.

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