UnitMatchPy 3.4.0__py3-none-any.whl → 3.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
UnitMatchPy/utils.py CHANGED
@@ -448,13 +448,13 @@ def fill_missing_pos(KS_dir, n_channels):
448
448
  outside the brain). UnitMatch requires the full channel_pos array, so this
449
449
  function reconstructs positions for the missing hardware channels.
450
450
 
451
- Strategy: channel_map.npy maps the N_active entries in channel_positions.npy to
452
- their hardware-channel indices (0 … n_channels-1). For each x-column found in
453
- the known positions a linear model y = slope * hw_channel_index + intercept is
454
- fit using the known (hw_index, y) pairs. Missing channels are then assigned to
455
- the column given by hw_index % n_cols and their y is predicted by that model.
456
- This correctly handles any number of missing channels, from a single gap to the
457
- majority of the probe being outside the brain.
451
+ Strategy (mirrors MATLAB fillmissingprobe.m): for each x-column that has known
452
+ positions, find the most similar column (one whose y-positions are a subset or
453
+ superset of the current column's y-positions). The y-positions present in that
454
+ reference column but absent from the current one are the missing positions.
455
+ The corresponding hardware-channel indices are inferred from the stride between
456
+ consecutive known channel indices in the same column, then matched against the
457
+ actual NaN entries. This makes no assumptions about round-robin column ordering.
458
458
 
459
459
  Parameters
460
460
  ----------
@@ -484,58 +484,96 @@ def fill_missing_pos(KS_dir, n_channels):
484
484
  print('Likely to be correctly filled')
485
485
  return channel_pos
486
486
 
487
- # --- Geometry inference via per-column linear regression ---
488
- # Assumes channels fill x-columns in round-robin order (ch % n_cols → column),
489
- # which holds for all Neuropixels probes. For probes with a different layout the
490
- # validation step below will detect the mismatch and fall back to off-probe
491
- # placeholders rather than silently returning wrong positions.
492
- x_unique = np.unique(pos[:, 0])
487
+ # --- MATLAB-style cross-column geometry inference ---
488
+
489
+ # Column assignment: ch_order[i] = column index for hardware channel i, -1 if unknown.
490
+ x_unique, ch_order_known = np.unique(channel_pos[~nan_mask, 0], return_inverse=True)
491
+ ch_order = np.full(n_channels, -1, dtype=int)
492
+ ch_order[~nan_mask] = ch_order_known
493
493
  n_cols = len(x_unique)
494
494
 
495
- # Estimate a global y-step as a fallback for columns with only one known channel.
496
- all_y_diffs = []
497
- for x in x_unique:
498
- yj = np.sort(pos[pos[:, 0] == x, 1])
499
- if len(yj) > 1:
500
- all_y_diffs.extend(np.diff(yj).tolist())
501
- y_step_global = float(np.median(all_y_diffs)) if all_y_diffs else 20.0
502
-
503
- col_models = {} # col_index -> (slope, intercept)
504
- for col_i, x in enumerate(x_unique):
505
- col_mask = pos[:, 0] == x
506
- ch_indices = channel_map[col_mask].astype(float)
507
- y_vals = pos[col_mask, 1].astype(float)
508
- if len(ch_indices) >= 2:
509
- slope, intercept = np.polyfit(ch_indices, y_vals, 1)
510
- # Validate: max residual must be well below one y-step.
511
- residuals = np.abs(np.polyval([slope, intercept], ch_indices) - y_vals)
512
- if np.max(residuals) > 0.4 * y_step_global:
513
- # Column assignment by ch % n_cols is inconsistent with the known
514
- # positions — this probe does not use round-robin column ordering.
515
- print('Warning: probe geometry does not follow a round-robin column '
516
- 'pattern (max fit residual {:.1f} µm). Cannot infer positions '
517
- 'for inactive channels; off-probe placeholders will be used. '
518
- 'Supply the full channel_positions manually if needed.'.format(
519
- float(np.max(residuals))))
520
- x_fill = float(np.nanmedian(channel_pos[:, 0]))
521
- y_fill = float(np.nanmin(channel_pos[:, 1])) - 10000.0
522
- channel_pos[nan_mask] = [x_fill, y_fill]
523
- return channel_pos
495
+ # Sorted unique y-positions per column (known channels only).
496
+ y_unique = [np.unique(channel_pos[ch_order == col_i, 1]) for col_i in range(n_cols)]
497
+
498
+ miss_idx = np.where(nan_mask)[0]
499
+ missing_chan = [] # (x, y) pairs for missing positions
500
+ probable_ch_idx = [] # hardware channel indices for those positions
501
+ count_id = 0
502
+
503
+ for xid in range(n_cols):
504
+ y_col = y_unique[xid]
505
+
506
+ # Find the most similar column: one whose y-set is a subset or superset of y_col.
507
+ same_idx = [
508
+ j for j in range(n_cols) if j != xid and (
509
+ np.all(np.isin(y_col, y_unique[j])) or
510
+ np.all(np.isin(y_unique[j], y_col))
511
+ )
512
+ ]
513
+
514
+ if same_idx:
515
+ # Prefer the column whose size is closest to y_col.
516
+ sizes = np.array([len(y_unique[j]) for j in same_idx])
517
+ y_ref = y_unique[same_idx[int(np.argmin(np.abs(sizes - len(y_col))))]]
524
518
  else:
525
- # Single known channel: use global y-step scaled by column stride.
526
- slope = y_step_global / n_cols
527
- intercept = y_vals[0] - slope * ch_indices[0]
528
- col_models[col_i] = (slope, intercept)
529
-
530
- # Assign each inactive hardware channel to its column (ch % n_cols) and
531
- # predict its y position from the column's linear model.
532
- for ch_idx in np.where(nan_mask)[0]:
533
- col_i = ch_idx % n_cols
534
- slope, intercept = col_models[col_i]
535
- channel_pos[ch_idx] = [x_unique[col_i], slope * ch_idx + intercept]
536
-
537
- # Safety net: if any positions are still NaN (unexpected column layout),
538
- # place them well off-probe so they do not interfere with active channels.
519
+ y_ref = np.array([])
520
+
521
+ # If the reference column itself has gaps, complete it via its minimum step.
522
+ if len(y_ref) > 1 and len(np.unique(np.diff(y_ref))) > 1:
523
+ step = float(np.min(np.diff(y_ref)))
524
+ y_ref = np.arange(y_ref[0], y_ref[-1] + step / 2, step)
525
+
526
+ # Skip this column if there are no missing y-positions.
527
+ if len(y_ref) == 0 or not np.any(~np.isin(y_ref, y_col)):
528
+ continue
529
+
530
+ missing_y = y_ref[~np.isin(y_ref, y_col)]
531
+ for y in missing_y:
532
+ missing_chan.append([x_unique[xid], y])
533
+
534
+ # Hardware channel indices already known in this column (sorted ascending).
535
+ these_chs = np.sort(np.where(ch_order == xid)[0])
536
+
537
+ if len(these_chs) > 1:
538
+ step_sz = int(np.min(np.diff(these_chs)))
539
+ else:
540
+ step_sz = n_cols # fallback: stride equals number of columns
541
+
542
+ # Potential missing indices: extend the known range downward by 2 steps
543
+ # (mirrors MATLAB: min-2*step : step : max).
544
+ potential_chans = np.arange(
545
+ these_chs[0] - step_sz * 2,
546
+ these_chs[-1] + 1,
547
+ step_sz,
548
+ dtype=int,
549
+ )
550
+ potential_chans = potential_chans[(potential_chans >= 0) & (potential_chans < n_channels)]
551
+
552
+ this_chans = miss_idx[np.isin(miss_idx, potential_chans)]
553
+ if len(this_chans) == 0:
554
+ # Fallback: take the next unassigned NaN channel.
555
+ if count_id < len(miss_idx):
556
+ this_chans = np.array([miss_idx[count_id]], dtype=int)
557
+ else:
558
+ this_chans = np.array([], dtype=int)
559
+
560
+ probable_ch_idx.extend(this_chans.tolist())
561
+ count_id += 1
562
+
563
+ # Apply inferred positions.
564
+ if probable_ch_idx:
565
+ idx_arr = np.array(probable_ch_idx)
566
+ if np.any(np.bincount(idx_arr, minlength=n_channels) > 1):
567
+ raise RuntimeError('Channel map filling in failed: duplicate channel assignments.')
568
+ missing_arr = np.array(missing_chan)
569
+ if len(idx_arr) == len(missing_arr):
570
+ channel_pos[idx_arr] = missing_arr
571
+ else:
572
+ print('Warning: mismatch between inferred positions ({}) and channel '
573
+ 'indices ({}); skipping partial fill.'.format(
574
+ len(missing_arr), len(idx_arr)))
575
+
576
+ # Safety net: place any still-NaN channels well off-probe.
539
577
  still_nan = np.isnan(channel_pos[:, 0])
540
578
  if np.any(still_nan):
541
579
  x_fill = float(np.nanmedian(channel_pos[:, 0]))
@@ -634,7 +672,7 @@ def paths_from_KS(KS_dirs, param=None, custom_raw_waveform_paths=None, custom_bo
634
672
  path_tmp = os.path.join(KS_dirs[i], 'channel_positions.npy')
635
673
  pos_tmp = np.load(path_tmp)
636
674
  if pos_tmp.shape[0] != n_channels[i]:
637
- print('Attmepting to fill in missing channel positions')
675
+ print('Attempting to fill in missing channel positions')
638
676
  pos_tmp = fill_missing_pos(KS_dirs[i], n_channels[i])
639
677
 
640
678
  # Want 3-D positions, however at the moment code only needs 2-D so add 1's to 0 axis position
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: UnitMatchPy
3
- Version: 3.4.0
3
+ Version: 3.4.1
4
4
  Summary: Automatic matching of neurons across sessions
5
5
  Author: Enny van Beest, Célian Bimbard, Suyash Agarwal, Wentao Qiu
6
6
  License: CC BY-NC-SA 4.0 International
@@ -13,16 +13,16 @@ Classifier: Operating System :: OS Independent
13
13
  Requires-Python: <3.13,>=3.9
14
14
  Description-Content-Type: text/markdown
15
15
  License-File: LICENSE
16
- Requires-Dist: numpy<3.0,>=1.21
17
- Requires-Dist: scipy<2.0,>=1.13
18
- Requires-Dist: pandas<3.0,>=2.2
19
- Requires-Dist: matplotlib<4.0,>=3.9
16
+ Requires-Dist: numpy<2.0,>=1.21
17
+ Requires-Dist: scipy<2.0,>=1.9
18
+ Requires-Dist: pandas<3.0,>=1.5
19
+ Requires-Dist: matplotlib<4.0,>=3.6
20
20
  Requires-Dist: joblib<2.0,>=1.2
21
21
  Requires-Dist: tqdm<5.0,>=4.64
22
- Requires-Dist: h5py<4.0,>=3.11
23
- Requires-Dist: scikit-learn<2.0,>=1.5
22
+ Requires-Dist: h5py<4.0,>=3.8
23
+ Requires-Dist: scikit-learn<2.0,>=1.2
24
24
  Requires-Dist: mtscomp
25
- Requires-Dist: torch<3.0,>=2.3
25
+ Requires-Dist: torch<3.0,>=2.1
26
26
  Requires-Dist: tensorboard
27
27
  Requires-Dist: seaborn
28
28
  Requires-Dist: matplotlib_venn
@@ -53,7 +53,7 @@ This repository contains:
53
53
 
54
54
  ## Versions
55
55
 
56
- - **UnitMatchPy version:** `3.3.1` (from `pyproject.toml`)
56
+ - **UnitMatchPy version:** `3.4.1` (from `pyproject.toml`)
57
57
  - **DeepUnitMatch:** code lives under `DeepUnitMatch/` (see **DeepUnitMatch (DUM)** section)
58
58
 
59
59
  ## System requirements
@@ -11,7 +11,7 @@ UnitMatchPy/overlord.py,sha256=-TJi1ZnDWYdNlI-b27MW4-9BmzO94P9m0K6WC9yRKfY,6371
11
11
  UnitMatchPy/param_functions.py,sha256=XKj3zpyjJIcIvO4gavWu0F6wMlMawOjEG2csK9gvjdw,17108
12
12
  UnitMatchPy/run.py,sha256=pA0lQntiEQt8jTuxfC1eoIdESCkWttyBw3eheuk5hMA,5350
13
13
  UnitMatchPy/save_utils.py,sha256=ZPlI60JYKt5cmZPiTYI2sPeA1OeKdgcWjeAT5yG4QyA,18672
14
- UnitMatchPy/utils.py,sha256=5owvqRm0PmOFG__JcvFANDiY0JzjHJ2tLMAYmNncg7I,43752
14
+ UnitMatchPy/utils.py,sha256=VaLBeTz6HF5vmMx2hxWxjxO4VEoSlf5-CUQQV7vsATM,44651
15
15
  UnitMatchPy/TkinterTheme/awthemes-10.4.0/LICENSE,sha256=QLti2X2WgO5MQ5_zSH1BpTVF2KHSCliQa9HG1s4skQk,2215
16
16
  UnitMatchPy/TkinterTheme/awthemes-10.4.0/README.txt,sha256=OszYA8mn7QelmAYtz0bHi6sf9YOvrXLGgmuCx3bRoJ4,15419
17
17
  UnitMatchPy/TkinterTheme/awthemes-10.4.0/awdark.tcl,sha256=bGeLj0wiuOkXQR3R7lP9FpkdE1-mAJruUATnxdCmy0c,2024
@@ -279,8 +279,8 @@ UnitMatchPy/TkinterTheme/awthemes-10.4.0/i/awthemes/treeview/solid/tree-arrow-do
279
279
  UnitMatchPy/TkinterTheme/awthemes-10.4.0/i/awthemes/treeview/solid/tree-arrow-right-n-base.svg,sha256=rhgTHm-xIGRkRz9sCSwNNZ3uKfDrK78lroyYbBJAZsk,2386
280
280
  UnitMatchPy/TkinterTheme/awthemes-10.4.0/i/awthemes/treeview/solid/tree-arrow-right-n.svg,sha256=8x1rGSDH8eyFeQDJ-glAc63K1hl-UveaJTNBkDmP6vA,2497
281
281
  UnitMatchPy/TkinterTheme/awthemes-10.4.0/i/awthemes/treeview/solid/tree-arrow-right-sn.svg,sha256=8x1rGSDH8eyFeQDJ-glAc63K1hl-UveaJTNBkDmP6vA,2497
282
- unitmatchpy-3.4.0.dist-info/licenses/LICENSE,sha256=WAMqgXMZUjcGirAtQplIvDIz2OaFjRud39xH47vD2Ec,20844
283
- unitmatchpy-3.4.0.dist-info/METADATA,sha256=NpIrZuiuqBnFMtWhLAAjhF2G8FzXuD1bglx1YxEspVM,5425
284
- unitmatchpy-3.4.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
285
- unitmatchpy-3.4.0.dist-info/top_level.txt,sha256=EjaC3TLtFxUHy5xVS71T_IrmNpDUVUlXf-7GO32tuDI,12
286
- unitmatchpy-3.4.0.dist-info/RECORD,,
282
+ unitmatchpy-3.4.1.dist-info/licenses/LICENSE,sha256=WAMqgXMZUjcGirAtQplIvDIz2OaFjRud39xH47vD2Ec,20844
283
+ unitmatchpy-3.4.1.dist-info/METADATA,sha256=895MkPNQ1uP96GL2ni-UNbhjOWN86UQ__wpCFxoW9Sw,5423
284
+ unitmatchpy-3.4.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
285
+ unitmatchpy-3.4.1.dist-info/top_level.txt,sha256=EjaC3TLtFxUHy5xVS71T_IrmNpDUVUlXf-7GO32tuDI,12
286
+ unitmatchpy-3.4.1.dist-info/RECORD,,