Skip to content

Commit 6eb05b0

Browse files
committed
move the probe registration to a public scope for iblrig use
1 parent 227c20d commit 6eb05b0

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

ibllib/ephys/spikes.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import json
44
import shutil
55
import tarfile
6+
from typing import Tuple
67

78
import numpy as np
89
from one.alf.path import get_session_path
910
import spikeglx
11+
from one.api import ONE
1012

1113
from iblutil.util import Bunch
1214
import phylib.io.alf
@@ -17,6 +19,51 @@
1719
_logger = logging.getLogger(__name__)
1820

1921

22+
def create_insertion(one: ONE, md: dict, label: str, eid: str) -> Tuple[dict, dict]:
23+
"""
24+
Create or update a probe insertion in Alyx and return description and the alyx rest record.
25+
26+
This function checks if a probe insertion with the given label already exists for the
27+
specified session. If it doesn't exist, it creates a new one. If it does, it updates
28+
the existing record. It also prepares a dictionary with essential probe details.
29+
30+
Parameters
31+
----------
32+
one : one.api.ONE
33+
An instance of the ONE API to interact with Alyx.
34+
md : dict
35+
A Bunch object containing metadata from a spikeglx meta file, including
36+
'neuropixelVersion', 'serial', and 'fileName'.
37+
label : str
38+
The label for the probe insertion (e.g., 'probe00').
39+
eid : str
40+
The unique experiment ID (UUID) for the session.
41+
42+
Returns
43+
-------
44+
tuple
45+
A tuple containing:
46+
- description (dict): A dictionary with probe details for metadata file,
47+
containing keys 'label', 'model', 'serial', 'raw_file_name'.
48+
- insertion (dict): The Alyx record for the created or updated probe insertion.
49+
"""
50+
# create json description
51+
description = {'label': label, 'model': md['neuropixelVersion'], 'serial': int(md['serial']),
52+
'raw_file_name': md['fileName']}
53+
54+
# create or update probe insertion on alyx
55+
alyx_insertion = {'session': eid, 'model': md['neuropixelVersion'], 'serial': md['serial'], 'name': label}
56+
pi = one.alyx.rest('insertions', 'list', session=eid, name=label)
57+
if len(pi) == 0:
58+
qc_dict = {'qc': 'NOT_SET', 'extended_qc': {}}
59+
alyx_insertion.update({'json': qc_dict})
60+
insertion = one.alyx.rest('insertions', 'create', data=alyx_insertion)
61+
else:
62+
insertion = one.alyx.rest('insertions', 'partial_update', data=alyx_insertion, id=pi[0]['id'])
63+
64+
return description, insertion
65+
66+
2067
def probes_description(ses_path, one):
2168
"""
2269
Aggregate probes information into ALF files
@@ -36,24 +83,6 @@ def probes_description(ses_path, one):
3683
return
3784

3885
subdirs, labels, efiles_sorted = zip(*sorted(ap_meta_files))
39-
40-
def _create_insertion(md, label, eid):
41-
42-
# create json description
43-
description = {'label': label, 'model': md.neuropixelVersion, 'serial': int(md.serial), 'raw_file_name': md.fileName}
44-
45-
# create or update probe insertion on alyx
46-
alyx_insertion = {'session': eid, 'model': md.neuropixelVersion, 'serial': md.serial, 'name': label}
47-
pi = one.alyx.rest('insertions', 'list', session=eid, name=label)
48-
if len(pi) == 0:
49-
qc_dict = {'qc': 'NOT_SET', 'extended_qc': {}}
50-
alyx_insertion.update({'json': qc_dict})
51-
insertion = one.alyx.rest('insertions', 'create', data=alyx_insertion)
52-
else:
53-
insertion = one.alyx.rest('insertions', 'partial_update', data=alyx_insertion, id=pi[0]['id'])
54-
55-
return description, insertion
56-
5786
# Ouputs the probes description file
5887
probe_description = []
5988
alyx_insertions = []
@@ -66,16 +95,16 @@ def _create_insertion(md, label, eid):
6695
nshanks = np.unique(geometry['shank'])
6796
for shank in nshanks:
6897
label_ext = f'{label}{chr(97 + int(shank))}'
69-
description, insertion = _create_insertion(md, label_ext, eid)
98+
description, insertion = create_insertion(one, md, label_ext, eid)
7099
probe_description.append(description)
71100
alyx_insertions.append(insertion)
72101
# NP2.4 meta that has already been split
73102
else:
74-
description, insertion = _create_insertion(md, label, eid)
103+
description, insertion = create_insertion(one, md, label, eid)
75104
probe_description.append(description)
76105
alyx_insertions.append(insertion)
77106
else:
78-
description, insertion = _create_insertion(md, label, eid)
107+
description, insertion = create_insertion(one, md, label, eid)
79108
probe_description.append(description)
80109
alyx_insertions.append(insertion)
81110

0 commit comments

Comments
 (0)