@@ -1422,3 +1422,113 @@ def test_sel_with_open_ended_slice_stop(self):
14221422 # slice(10, None) means from 10 to maximum
14231423 result = ds_indexed .sel (abs_time = slice (10 , None ))
14241424 assert result .sizes ["trial" ] >= 1
1425+
1426+ def test_slice_with_step_sorted (self ):
1427+ """Slice with step on sorted coordinate."""
1428+ ds = trial_based_dataset (n_trials = 3 , trial_length = 5.0 , sample_rate = 10 )
1429+ ds_indexed = ds .set_xindex (["abs_time" ], NDIndex )
1430+
1431+ # Verify the coordinate is sorted
1432+ idx = ds_indexed .xindexes ["abs_time" ]
1433+ assert idx ._nd_coords ["abs_time" ].is_sorted
1434+
1435+ # Select with step on a range that uses the sorted path
1436+ # This triggers the sorted path (is_sorted=True) with step handling
1437+ result = ds_indexed .sel (abs_time = slice (0 , 5 , 2 ), method = "nearest" )
1438+ assert result .sizes ["rel_time" ] > 0
1439+
1440+ def test_slice_no_values_in_range_sorted (self ):
1441+ """Sorted path with no values in range returns empty slices."""
1442+ ds = trial_based_dataset (n_trials = 3 , trial_length = 5.0 , sample_rate = 10 )
1443+ ds_indexed = ds .set_xindex (["abs_time" ], NDIndex )
1444+
1445+ # Select a range that doesn't exist (negative times)
1446+ result = ds_indexed .sel (abs_time = slice (- 100 , - 50 ))
1447+ # Should return empty
1448+ assert result .sizes ["trial" ] == 0 or result .sizes ["rel_time" ] == 0
1449+
1450+ def test_slice_no_values_in_range_unsorted (self ):
1451+ """Unsorted path with no values in range returns empty slices."""
1452+ # Create unsorted 2D coordinate
1453+ values = np .random .randn (5 , 5 ) + 100 # All positive values > 100
1454+
1455+ ds = xr .Dataset (
1456+ {"data" : (("y" , "x" ), np .ones ((5 , 5 )))},
1457+ coords = {
1458+ "y" : np .arange (5 ),
1459+ "x" : np .arange (5 ),
1460+ "coord" : (("y" , "x" ), values ),
1461+ },
1462+ )
1463+ ds_indexed = ds .set_xindex (["coord" ], NDIndex )
1464+
1465+ # Select a range that doesn't exist
1466+ result = ds_indexed .sel (coord = slice (- 100 , - 50 ))
1467+ assert result .sizes ["y" ] == 0 or result .sizes ["x" ] == 0
1468+
1469+ def test_unsorted_nearest_with_step (self ):
1470+ """Unsorted coordinate with method='nearest' and step."""
1471+ # Create unsorted 2D coordinate
1472+ np .random .seed (42 )
1473+ values = np .random .randn (5 , 10 )
1474+
1475+ ds = xr .Dataset (
1476+ {"data" : (("y" , "x" ), np .ones ((5 , 10 )))},
1477+ coords = {
1478+ "y" : np .arange (5 ),
1479+ "x" : np .arange (10 ),
1480+ "coord" : (("y" , "x" ), values ),
1481+ },
1482+ )
1483+ ds_indexed = ds .set_xindex (["coord" ], NDIndex )
1484+
1485+ # Select with step and nearest on unsorted coord
1486+ result = ds_indexed .sel (coord = slice (- 1 , 1 , 2 ), method = "nearest" )
1487+ assert result .sizes ["x" ] > 0
1488+
1489+ def test_trim_outer_slice_method_range (self ):
1490+ """trim_outer method with range selection."""
1491+ ds = trial_based_dataset (n_trials = 3 , trial_length = 5.0 , sample_rate = 10 )
1492+ ds_indexed = ds .set_xindex (["abs_time" ], NDIndex , slice_method = "trim_outer" )
1493+
1494+ # Select a range spanning multiple trials
1495+ result = ds_indexed .sel (abs_time = slice (2 , 8 ))
1496+ assert result .sizes ["trial" ] >= 1
1497+ assert result .sizes ["rel_time" ] >= 1
1498+
1499+ def test_trim_outer_with_step (self ):
1500+ """trim_outer method with step in slice."""
1501+ ds = trial_based_dataset (n_trials = 3 , trial_length = 5.0 , sample_rate = 10 )
1502+ ds_indexed = ds .set_xindex (["abs_time" ], NDIndex , slice_method = "trim_outer" )
1503+
1504+ # Select with step
1505+ result = ds_indexed .sel (abs_time = slice (0 , 10 , 3 ))
1506+ assert result .sizes ["rel_time" ] > 0
1507+
1508+ def test_sel_masked_metadata_with_sorted_nearest (self ):
1509+ """sel_masked with returns='metadata' and method='nearest' on sorted."""
1510+ from linked_indices import nd_sel
1511+
1512+ ds = trial_based_dataset (n_trials = 3 , trial_length = 5.0 , sample_rate = 10 )
1513+ ds_indexed = ds .set_xindex (["abs_time" ], NDIndex )
1514+
1515+ # Use metadata mode with nearest on sorted data
1516+ result = nd_sel (
1517+ ds_indexed ,
1518+ abs_time = slice (2.5 , 7.5 ),
1519+ method = "nearest" ,
1520+ returns = "metadata" ,
1521+ )
1522+
1523+ assert "in_abs_time_range" in result .coords
1524+
1525+ def test_empty_coord_raises_on_nearest (self ):
1526+ """Finding nearest in empty coordinate raises ValueError."""
1527+ # Create a minimal dataset and get the index
1528+ ds = trial_based_dataset (n_trials = 1 , trial_length = 1.0 , sample_rate = 10 )
1529+ ds_indexed = ds .set_xindex (["abs_time" ], NDIndex )
1530+ idx = ds_indexed .xindexes ["abs_time" ]
1531+
1532+ # Try to find nearest in empty array
1533+ with pytest .raises (ValueError , match = "Cannot find nearest in empty array" ):
1534+ idx ._find_nearest_index (np .array ([]), 0.5 )
0 commit comments