1
+ import os
2
+
3
+ import pandas as pd
4
+ import numpy as np
5
+
6
+
7
+ def structure_assignment_fname_map ():
8
+ return {
9
+ 733744647 : "732592105_probeB_733744647.csv" ,
10
+ 733744649 : "732592105_probeC_733744649.csv" ,
11
+ 733744651 : "732592105_probeD_733744651.csv" ,
12
+ 733744653 : "732592105_probeE_733744653.csv" ,
13
+ 733744655 : "732592105_probeF_733744655.csv" ,
14
+ 805579698 : "799864342_probeA_805579698.csv" ,
15
+ 832129149 : "829720705_probeA_832129149.csv" ,
16
+ 837761710 : "835479236_probeC_837761710.csv"
17
+ }
18
+
19
+
20
+ def load_structure_assignments (probe_ids = None ):
21
+
22
+ fnames = structure_assignment_fname_map ()
23
+ dirname = os .path .join (os .path .dirname (__file__ ), "data" )
24
+
25
+ output = []
26
+ for probe_id , fname in fnames .items ():
27
+ if probe_ids is not None and probe_id not in probe_ids :
28
+ continue
29
+
30
+ full_path = os .path .join (dirname , fname )
31
+ df = pd .read_csv (full_path )
32
+
33
+ df ["ecephys_probe_id" ] = np .zeros ([len (df )], dtype = int ) + probe_id
34
+ output .append (df )
35
+
36
+ return pd .concat (output )
37
+
38
+
39
+ def replace_bad_structure_assignments (
40
+ channels ,
41
+ structure_id_key = "structure_id" ,
42
+ structure_acronym_key = "structure_acronym" ,
43
+ inplace = False
44
+ ):
45
+ if not inplace :
46
+ channels = channels .copy ()
47
+
48
+ probes = set (channels ["ecephys_probe_id" ].unique ().tolist ())
49
+
50
+ assignments = load_structure_assignments ()
51
+ reassigned_probes = set (assignments ["ecephys_probe_id" ].unique ().tolist ())
52
+
53
+ reassigned = channels [channels ["ecephys_probe_id" ].isin (reassigned_probes )]
54
+ reassigned = pd .merge (
55
+ reassigned .reset_index (), assignments ,
56
+ how = "left" ,
57
+ left_on = ["ecephys_probe_id" , "local_index" ],
58
+ right_on = ["ecephys_probe_id" , "local_index" ],
59
+ suffixes = ["_original" , "_corrected" ]
60
+ )
61
+ reassigned .set_index ("id" , inplace = True )
62
+
63
+ reassigned .drop (columns = [
64
+ f"{ structure_id_key } _original" ,
65
+ f"{ structure_acronym_key } _original"
66
+ ], inplace = True )
67
+ reassigned .rename (columns = {
68
+ f"{ structure_id_key } _corrected" : structure_id_key ,
69
+ f"{ structure_acronym_key } _corrected" : structure_acronym_key
70
+ }, inplace = True )
71
+
72
+ channels .loc [reassigned .index .values , [structure_id_key , structure_acronym_key ]] = \
73
+ reassigned .loc [:, [structure_id_key , structure_acronym_key ]]
74
+
75
+ if not inplace :
76
+ return reassigned
0 commit comments