Skip to content

Commit 8b41705

Browse files
committed
#1007 #1071 #1050 patch incorrect structure assignments
1 parent 0d9bd47 commit 8b41705

11 files changed

+3162
-2
lines changed

MANIFEST.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
recursive-include allensdk *.py *.conf *.json *.txt *.yaml
1+
recursive-include allensdk *.py *.conf *.json *.txt *.yaml *.csv
22
include README.md LICENSE.txt Makefile *requirements.txt

allensdk/brain_observatory/ecephys/ecephys_project_api/ecephys_project_warehouse_api.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from .ecephys_project_api import EcephysProjectApi
1010
from .utilities import rma_macros, build_and_execute
1111

12+
from allensdk.brain_observatory.ecephys.ecephys_project_api.warehouse_patches import replace_bad_structure_assignments
13+
14+
1215
class EcephysProjectWarehouseApi(EcephysProjectApi):
1316

1417
movie_re = re.compile(r".*natural_movie_(?P<num>\d+).npy")
@@ -205,6 +208,7 @@ def get_channels(self, channel_ids=None, probe_ids=None):
205208
)
206209

207210
response.set_index("id", inplace=True)
211+
replace_bad_structure_assignments(response, inplace=True)
208212

209213
return response
210214

@@ -304,4 +308,4 @@ def get_unit_analysis_metrics(self, unit_ids=None, ecephys_session_ids=None, ses
304308
def default(cls, **rma_kwargs):
305309
_rma_kwargs = {"scheme": "http", "host": "api.brain-map.org"}
306310
_rma_kwargs.update(rma_kwargs)
307-
return cls(RmaEngine(**_rma_kwargs))
311+
return cls(RmaEngine(**_rma_kwargs))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)