forked from SpikeInterface/probeinterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.py
More file actions
824 lines (651 loc) · 26.4 KB
/
Copy pathio.py
File metadata and controls
824 lines (651 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
"""
Read/write probe info using a variety of formats:
* probeinterface (.json)
* PRB (.prb)
* CSV (.csv)
* mearec (.h5)
* ironclust/jrclust (.mat)
* Neurodata Without Borders (.nwb)
"""
from pathlib import Path
import re
import json
from collections import OrderedDict
from packaging.version import parse
import numpy as np
from . import __version__
from .probe import Probe
from .probegroup import ProbeGroup
from .utils import import_safely
def _probeinterface_format_check_version(d):
"""
Check format version of probeinterface JSON file
"""
pass
def read_probeinterface(file: str | Path) -> ProbeGroup:
"""
Read probeinterface JSON-based format.
Parameters
----------
file: Path or str
The file path
Returns
--------
probegroup : ProbeGroup object
"""
file = Path(file)
with open(file, "r", encoding="utf8") as f:
d = json.load(f)
# check version
_probeinterface_format_check_version(d)
# create probegroup from dict
return ProbeGroup.from_dict(d)
def write_probeinterface(file: str | Path, probe_or_probegroup: Probe | ProbeGroup):
"""
Write a probeinterface JSON file.
The format handles several probes in one file.
Parameters
----------
file : Path or str
The file path
probe_or_probegroup : Probe or ProbeGroup object
If probe is given a probegroup is created anyway
"""
if isinstance(probe_or_probegroup, Probe):
probe = probe_or_probegroup
probegroup = ProbeGroup()
probegroup.add_probe(probe)
elif isinstance(probe_or_probegroup, ProbeGroup):
probegroup = probe_or_probegroup
else:
raise TypeError(
f"write_probeinterface : needs a probe or probegroup you "
f"entered an object of type: {type(probe_or_probegroup)}"
)
file = Path(file)
d = OrderedDict()
d["specification"] = "probeinterface"
d["version"] = __version__
d.update(probegroup.to_dict(array_as_list=True))
with open(file, "w", encoding="utf8") as f:
json.dump(d, f, indent=4)
tsv_label_map_to_BIDS = {
"contact_ids": "contact_id",
"probe_ids": "probe_id",
"contact_shapes": "contact_shape",
"shank_ids": "shank_id",
"si_units": "xyz_units",
}
tsv_label_map_to_probeinterface = {v: k for k, v in tsv_label_map_to_BIDS.items()}
def read_BIDS_probe(folder: str | Path, prefix: str | None = None) -> ProbeGroup:
"""
Read to BIDS probe format.
This requires a probes.tsv and a contacts.tsv file
and potentially corresponding files in JSON format.
Parameters
----------
folder: Path or str
The folder to scan for probes and contacts files
prefix : str
Prefix of the probes and contacts files
Returns
--------
probegroup : ProbeGroup object
"""
pd = import_safely("pandas")
folder = Path(folder)
probes = {}
probegroup = ProbeGroup()
# Identify source files for probes and contacts information
if prefix is None:
probes_files = [f for f in folder.iterdir() if f.name.endswith("probes.tsv")]
contacts_files = [f for f in folder.iterdir() if f.name.endswith("contacts.tsv")]
if len(probes_files) != 1 or len(contacts_files) != 1:
raise ValueError("Did not find one probes.tsv and one contacts.tsv file")
probes_file = probes_files[0]
contacts_file = contacts_files[0]
else:
probes_file = folder / f"{prefix}_probes.tsv"
contacts_file = folder / f"{prefix}_contacts.tsv"
for file in [probes_file, contacts_file]:
if not file.exists():
raise ValueError(f"Source file does not exist ({file})")
# Step 1: READING CONTACTS.TSV
converters = {
"x": float,
"y": float,
"z": float,
"contact_shapes": str,
"probe_index": int,
"probe_id": str,
"shank_id": str,
"contact_id": str,
"radius": float,
"width": float,
"height": float,
}
df = pd.read_csv(contacts_file, sep="\t", header=0, keep_default_na=False, converters=converters) # dtype=str,
df.replace(to_replace={"n/a": ""}, inplace=True)
df.rename(columns=tsv_label_map_to_probeinterface, inplace=True)
if "probe_ids" not in df:
raise ValueError("probes.tsv file does not contain probe_id column")
if "contact_ids" not in df:
raise ValueError("contacts.tsv file does not contain contact_id column")
for probe_id in df["probe_ids"].unique():
df_probe = df[df["probe_ids"] == probe_id].copy()
# adding default values required by probeinterface if not present in
# source files
if "contact_shapes" not in df_probe:
df_probe["contact_shapes"] = "circle"
df_probe["radius"] = 1
print(
f"There is no contact shape provided for probe {probe_id}, a "
f"dummy circle with 1um radius will be used."
)
if "x" not in df_probe:
df_probe["x"] = np.arange(len(df_probe.index), dtype=float)
print(
f"There is no x coordinate provided for probe {probe_id}, a " f"dummy linear x coordinate will be used."
)
if "y" not in df_probe:
df_probe["y"] = 0.0
print(
f"There is no y coordinate provided for probe {probe_id}, a "
f"dummy constant y coordinate will be used."
)
if "si_units" not in df_probe:
df_probe["si_units"] = "um"
print(f"There is no SI unit provided for probe {probe_id}, a " f"dummy SI unit (um) will be used")
# create probe object and register with probegroup
probe = Probe.from_dataframe(df=df_probe)
probe.annotate(probe_id=probe_id)
probes[str(probe_id)] = probe
probegroup.add_probe(probe)
ignore_annotations = [
"probe_ids",
"contact_ids",
"contact_shapes",
"x",
"y",
"z",
"shank_ids",
"si_units",
"device_channel_indices",
"radius",
"width",
"height",
"probe_num",
"device_channel_indices",
]
df_others = df_probe.drop(ignore_annotations, axis=1, errors="ignore")
for col_name in df_others.columns:
probe.annotate(**{col_name: df_probe[col_name].values})
# Step 2: READING PROBES.TSV
df = pd.read_csv(probes_file, sep="\t", header=0, keep_default_na=False, dtype=str)
df.replace(to_replace={"n/a": ""}, inplace=True)
if "probe_id" not in df:
raise ValueError(f"{probes_file} file does not contain probe_id column")
for row_idx, row in df.iterrows():
probe_id = row["probe_id"]
if probe_id not in probes:
print(
f"Probe with id {probe_id} is present in probes.tsv but not "
f"in contacts.tsv file. Ignoring entry in probes.tsv."
)
continue
probe = probes[probe_id]
probe.annotate(**dict(row.items()))
# for string based annotations use '' instead of None as default
for string_annotation in ["name", "manufacturer"]:
if probe.annotations.get(string_annotation, None) is None:
probe.annotations[string_annotation] = ""
# Step 3: READING PROBES.JSON (optional)
probes_dict = {}
probe_json = probes_file.with_suffix(".json")
if probe_json.exists():
with open(probe_json, "r") as f:
probes_dict = json.load(f)
if "ProbeId" in probes_dict:
for probe_id, probe_info in probes_dict["ProbeId"].items():
probe = probes[probe_id]
for probe_param, param_value in probe_info.items():
if probe_param == "contour":
probe.probe_planar_contour = np.array(param_value)
elif probe_param == "units":
if probe.si_units is None:
probe.si_units = param_value
elif probe.si_units != param_value:
raise ValueError(f"Inconsistent si_units for probe " f"{probe_id}")
else:
probe.annotate(**{probe_param: param_value})
# Step 4: READING CONTACTS.JSON (optional)
contacts_dict = {}
contact_json = contacts_file.with_suffix(".json")
if contact_json.exists():
with open(contact_json, "r") as f:
contacts_dict = json.load(f)
if "ContactId" in contacts_dict:
# collect all contact parameters used in this file
contact_params = [k for v in contacts_dict["ContactId"].values() for k in v.keys()]
contact_params = np.unique(contact_params)
# collect contact information for each probe_id
for probe in probes.values():
contact_ids = probe.contact_ids
for contact_param in contact_params:
# collect parameters across contact ids to add to probe
value_list = [contacts_dict["ContactId"][str(c)].get(contact_param, None) for c in contact_ids]
probe.annotate(**{contact_param: value_list})
return probegroup
def write_BIDS_probe(folder: str | Path, probe_or_probegroup: Probe | ProbeGroup, prefix: str = ""):
"""
Write to probe and contact formats as proposed
for ephy BIDS extension (tsv & json based).
The format handles several probes in one file.
Parameters
----------
folder : Path or str
The folder name.
probe_or_probegroup : Probe or ProbeGroup
If probe is given a probegroup is created anyway.
prefix : str
A prefix to be added to the filenames
"""
pd = import_safely("pandas")
if isinstance(probe_or_probegroup, Probe):
probe = probe_or_probegroup
probegroup = ProbeGroup()
probegroup.add_probe(probe)
elif isinstance(probe_or_probegroup, ProbeGroup):
probegroup = probe_or_probegroup
else:
raise TypeError(
f"probe_or_probegroup has to be" "of type Probe or ProbeGroup " f"not type: {type(probe_or_probegroup)}"
)
folder = Path(folder)
# ensure that prefix and file type indicator are separated by an underscore
if prefix != "" and prefix[-1] != "_":
prefix = prefix + "_"
probes = probegroup.probes
# Step 1: GENERATION OF PROBE.TSV
# ensure required keys (probe_id, probe_type) are present
if any("probe_id" not in p.annotations for p in probes):
probegroup.auto_generate_probe_ids()
for probe in probes:
if "probe_id" not in probe.annotations:
raise ValueError(
"Export to BIDS probe format requires "
"the probe id to be specified as an annotation "
"(probe_id). You can do this via "
"`probegroup.auto_generate_ids."
)
if "type" not in probe.annotations:
raise ValueError(
"Export to BIDS probe format requires " "the probe type to be specified as an " "annotation (type)"
)
# extract all used annotation keys
keys_by_probe = [list(p.annotations) for p in probes]
keys_concatenated = np.concatenate(keys_by_probe)
annotation_keys = np.unique(keys_concatenated)
# generate a tsv table capturing probe information
index = range(len([p.annotations["probe_id"] for p in probes]))
df = pd.DataFrame(index=index)
for annotation_key in annotation_keys:
df[annotation_key] = [p.annotations[annotation_key] for p in probes]
df["n_shanks"] = [len(np.unique(p.shank_ids)) for p in probes]
# Note: in principle it would also be possible to add the probe width and
# depth here based on the probe contour information. However this would
# require an alignment of the probe within the coordinate system.
# substitute empty values by BIDS default and create tsv file
df.fillna("n/a", inplace=True)
df.replace(to_replace="", value="n/a", inplace=True)
df.to_csv(folder.joinpath(prefix + "probes.tsv"), sep="\t", index=False)
# Step 2: GENERATION OF PROBE.JSON
probes_dict = {}
for probe in probes:
probe_id = probe.annotations["probe_id"]
probes_dict[probe_id] = {
"contour": probe.probe_planar_contour.tolist(),
"units": probe.si_units,
}
probes_dict[probe_id].update(probe.annotations)
with open(folder.joinpath(prefix + "probes.json"), "w", encoding="utf8") as f:
json.dump({"ProbeId": probes_dict}, f, indent=4)
# Step 3: GENERATION OF CONTACTS.TSV
# ensure required contact identifiers are present
for probe in probes:
if probe.contact_ids is None:
raise ValueError(
"Contacts must have unique contact ids "
"and not None for export to BIDS probe format."
"Use `probegroup.auto_generate_contact_ids`."
)
df = probegroup.to_dataframe()
index = range(sum([p.get_contact_count() for p in probes]))
df.rename(columns=tsv_label_map_to_BIDS, inplace=True)
df["probe_id"] = [p.annotations["probe_id"] for p in probes for _ in p.contact_ids]
df["coordinate_system"] = ["relative cartesian"] * len(index)
channel_indices = []
for probe in probes:
if probe.device_channel_indices:
channel_indices.extend(probe.device_channel_indices)
else:
channel_indices.extend([-1] * probe.get_contact_count())
df["device_channel_indices"] = channel_indices
df.fillna("n/a", inplace=True)
df.replace(to_replace="", value="n/a", inplace=True)
df.to_csv(folder.joinpath(prefix + "contacts.tsv"), sep="\t", index=False)
# Step 4: GENERATING CONTACTS.JSON
contacts_dict = {}
for probe in probes:
for cidx, contact_id in enumerate(probe.contact_ids):
cdict = {"contact_plane_axes": probe.contact_plane_axes[cidx].tolist()}
contacts_dict[contact_id] = cdict
with open(folder.joinpath(prefix + "contacts.json"), "w", encoding="utf8") as f:
json.dump({"ContactId": contacts_dict}, f, indent=4)
def read_prb(file: str | Path) -> ProbeGroup:
"""
Read a PRB file and return a ProbeGroup object.
Since PRB does not handle contact shapes, contacts are set to be circle of 5um radius.
Same for the probe shape, where an auto shape is created.
PRB format does not contain any information about the channel of the probe
Only the channel index on device is given.
Parameters
----------
file : Path or str
The file path
Returns
--------
probegroup : ProbeGroup object
"""
file = Path(file).absolute()
assert file.is_file(), "'file given is not of type file"
with file.open("r") as f:
contents = f.read()
contents = re.sub(r"range\(([\d,]*)\)", r"list(range(\1))", contents)
prb = {}
exec(contents, None, prb)
prb = {k.lower(): v for (k, v) in prb.items()}
if "channel_groups" not in prb:
raise ValueError("This file is not a standard PRB file")
probegroup = ProbeGroup()
for i, group in prb["channel_groups"].items():
probe = Probe(ndim=2, si_units="um")
chans = np.array(group["channels"], dtype="int64")
positions = np.array([group["geometry"][c] for c in chans], dtype="float64")
probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5})
probe.create_auto_shape(probe_type="tip")
probe.set_device_channel_indices(chans)
probegroup.add_probe(probe)
return probegroup
def read_maxwell(file: str | Path, well_name: str = "well000", rec_name: str = "rec0000") -> Probe:
"""
Read a maxwell file and return a Probe object. The Maxwell file format can be
either Maxone (and thus just the file name is needed), or MaxTwo. In case
of the latter, you need to explicitly specify what is the well number of
interest (well000 by default), and the recording session (since there can
be several. Default is rec0000)
Parameters
----------
file : Path or str
The file name
well_name : str
If MaxTwo file format, the well_name to extract the mapping from
(default is well000)
rec_name : str
If MaxTwo file format, the recording session to extract the mapping
from (default is rec0000)
Returns
--------
probe : Probe object
"""
file = Path(file).absolute()
assert file.is_file()
h5py = import_safely("h5py")
my_file = h5py.File(file, mode="r")
if "mapping" in my_file.keys():
mapping = my_file["mapping"][:]
else:
mapping = my_file["wells"][well_name][rec_name]["settings"]["mapping"][:]
prb = {"channel_groups": {1: {}}}
channels = list(mapping["channel"])
electrodes = list(mapping["electrode"])
x_pos = list(mapping["x"])
y_pos = list(mapping["y"])
geometry = {}
for c, x, y in zip(channels, x_pos, y_pos):
geometry[c] = [x, y]
my_file.close()
prb["channel_groups"][1]["geometry"] = geometry
prb["channel_groups"][1]["channels"] = channels
probe = Probe(ndim=2, si_units="um", manufacturer="Maxwell Biosystems")
chans = np.array(prb["channel_groups"][1]["channels"], dtype="int64")
positions = np.array([prb["channel_groups"][1]["geometry"][c] for c in chans], dtype="float64")
probe.set_contacts(positions=positions, shapes="rect", shape_params={"width": 5.45, "height": 9.3})
probe.annotate_contacts(electrode=electrodes)
probe.set_planar_contour(([-12.5, -12.5], [3845, -12.5], [3845, 2095], [-12.5, 2095]))
probe.set_device_channel_indices(np.arange(positions.shape[0]))
return probe
def read_3brain(file: str | Path, mea_pitch: float = None, electrode_width: float = None) -> Probe:
"""
Read a 3brain file and return a Probe object. The 3brain file format can be
either an .h5 file or a .brw
Parameters
----------
file : Path or str
The file name
mea_pitch : float
The inter-electrode distance (pitch) between electrodes in um, if
`None` it is tried to be inferred from the chip model in the file or
set to 42
electrode_width : float
The width of the electrodes in um, if `None` it is tried to be inferred
from the chip model in the file or set to 21
Returns
--------
probe : Probe object
Notes
-----
In case of multiple wells, the function will return the probe of the first
plate.
"""
file = Path(file).absolute()
assert file.is_file()
h5py = import_safely("h5py")
rf = h5py.File(file, "r")
if "3BRecInfo" in rf.keys(): # brw v3.x
# get channel positions
channels = rf["3BRecInfo/3BMeaStreams/Raw/Chs"][:]
rows = channels["Row"] - 1
cols = channels["Col"] - 1
if mea_pitch is None:
mea_pitch = 42
if electrode_width is None:
electrode_width = 21
else: # brw v4.x
num_channels = None
for key in rf:
if key.startswith("Well_"):
num_channels = len(rf[key]["StoredChIdxs"])
break
assert num_channels is not None, "No Well found in the file"
num_channels_x = num_channels_y = int(np.sqrt(num_channels))
assert num_channels_x * num_channels_y == num_channels, (
"Electrode configuration is not a square. Cannot determine "
f"configuration of the MEA plate with {num_channels} channels."
)
rows = np.repeat(range(num_channels_x), num_channels_y)
cols = np.tile(range(num_channels_y), num_channels_x)
if mea_pitch is None or electrode_width is None:
experiment_settings = json.JSONDecoder().decode(rf["ExperimentSettings"][0].decode())
model = experiment_settings["MeaPlate"]["Model"].lower()
# see https://www.3brain.com/products/single-well/hd-mea
# see https://www.3brain.com/products/multiwell/coreplate-multiwell
if mea_pitch is None:
if model.startswith("accura") or model.startswith("coreplate"):
mea_pitch = 60
elif model.startswith("stimulo"):
mea_pitch = 81
else: # Arena, Prime
mea_pitch = 42
if electrode_width is None:
if model.startswith("coreplate"):
electrode_width = 25
else: # Accura, Arena, Prime, Stimulo
electrode_width = 21
positions = np.vstack((rows, cols)).T * mea_pitch
probe = Probe(ndim=2, si_units="um", manufacturer="3Brain")
probe.set_contacts(positions=positions, shapes="square", shape_params={"width": electrode_width})
probe.annotate_contacts(row=rows)
probe.annotate_contacts(col=cols)
probe.create_auto_shape(probe_type="rect", margin=mea_pitch)
probe.set_device_channel_indices(np.arange(positions.shape[0]))
return probe
def write_prb(
file: str,
probegroup: ProbeGroup,
total_nb_channels: int | None = None,
radius: float | None = None,
group_mode: str = "by_probe",
):
"""
Write ProbeGroup into a prb file.
This format handles:
* multi Probe with channel group index key
* channel positions with "geometry"
* device_channel_indices with "channels "key
Note: much information is lost in the PRB format:
* contact shape
* shape
* channel index
Note:
* "total_nb_channels" is needed by spyking-circus
* "radius" is needed by spyking-circus
* "graph" is not handled
Parameters
----------
file: str
The name of the file to be written
probegroup: ProbeGroup
The Probegroup to be used for writing
total_nb_channels: int | None, default None
***to do
radius: float | None, default None
*** to do
group_mode: str
One of "by_probe" or "by_shank
"""
assert group_mode in ("by_probe", "by_shank")
if len(probegroup.probes) == 0:
raise ValueError("The probe group must have at least one probe")
for probe in probegroup.probes:
if probe.device_channel_indices is None:
raise ValueError("For PRB format device_channel_indices must be set")
with open(file, "w") as f:
if total_nb_channels is not None:
f.write(f"total_nb_channels = {total_nb_channels}\n")
if radius is not None:
f.write(f"radius = {radius}\n")
f.write("channel_groups = {\n")
if group_mode == "by_probe":
loop = enumerate(probegroup.probes)
elif group_mode == "by_shank":
shanks = []
for probe in probegroup.probes:
shanks.extend(probe.get_shanks())
loop = enumerate(shanks)
for group_ind, probe_or_shank in loop:
f.write(f" {group_ind}:\n")
f.write(" {\n")
channels = probe_or_shank.device_channel_indices
keep = channels >= 0
# channels -1 are not wired
chans = list(channels[keep])
f.write(f" 'channels': {chans},\n")
f.write(" 'geometry': {\n")
for c in range(probe_or_shank.get_contact_count()):
if not keep[c]:
continue
pos = list(probe_or_shank.contact_positions[c, :])
f.write(f" {channels[c]}: {pos},\n")
f.write(" }\n")
f.write(" },\n")
f.write("}\n")
def read_csv(file: str | Path):
"""
Return a 2 or 3 columns csv file with contact positions
"""
raise NotImplementedError
def write_csv(file, probe):
"""
Write contact positions into a 2 or 3 columns csv file
"""
raise NotImplementedError
def read_mearec(file: str | Path) -> Probe:
"""
Read probe position, and contact shape from a MEArec file.
See https://mearec.readthedocs.io/en/latest/ and https://doi.org/10.1007/s12021-020-09467-7 for implementation.
Parameters
----------
file : Path or str
The file path
Returns
-------
probe : Probe object
"""
file = Path(file).absolute()
assert file.is_file()
h5py = import_safely("h5py")
f = h5py.File(file, "r")
positions = f["channel_positions"][()]
electrodes_info = f["info"]["electrodes"]
electrodes_info_keys = electrodes_info.keys()
mearec_description = None
mearec_name = None
if "electrode_name" in electrodes_info_keys:
mearec_name = electrodes_info["electrode_name"][()]
mearec_name = mearec_name.decode("utf-8") if isinstance(mearec_name, bytes) else mearec_name
if "description" in electrodes_info_keys:
description = electrodes_info["description"][()]
mearec_description = description.decode("utf-8") if isinstance(description, bytes) else description
probe = Probe(ndim=2, si_units="um", model_name=mearec_name)
plane = "yz" # default
if "plane" in electrodes_info_keys:
plane = electrodes_info["plane"][()]
plane = plane.decode("utf-8") if isinstance(plane, bytes) else plane
plane_to_columns = {"xy": [0, 1], "xz": [0, 2], "yz": [1, 2]}
columns = plane_to_columns[plane]
positions_2d = positions[()][:, columns]
shape = None
if "shape" in electrodes_info_keys:
shape = electrodes_info["shape"][()]
shape = shape.decode("utf-8") if isinstance(shape, bytes) else shape
size = None
if "shape" in electrodes_info_keys:
size = electrodes_info["size"][()]
shape_params = {}
if shape is not None and size is not None:
if shape == "circle":
shape_params = {"radius": size}
elif shape == "square":
shape_params = {"width": 2 * size}
elif shape == "rect":
shape_params = {{"width": 2 * size[0], "height": 2 * size[1]}}
# create contacts
probe.set_contacts(positions_2d, shapes=shape, shape_params=shape_params)
# add MEArec annotations
annotations = dict(mearec_name=mearec_name, mearec_description=mearec_description)
probe.annotate(**annotations)
# set device indices
if electrodes_info["sortlist"][()] not in (b"null", "null"):
channel_indices = electrodes_info["sortlist"][()]
else:
channel_indices = np.arange(positions.shape[0], dtype="int64")
probe.set_device_channel_indices(channel_indices)
# create auto shape
probe.create_auto_shape(probe_type="tip")
return probe
def read_nwb(file):
"""
Read probe position from an NWB file
"""
raise NotImplementedError