Skip to content

Commit ce1ef8f

Browse files
committed
fix dataframe format for dandi
1 parent fdd4cf0 commit ce1ef8f

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

src/ms_stim_analysis/AnalysisTables/credible_intervals.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
get_HPD_spatial_coverage,
44
get_highest_posterior_threshold,
55
)
6+
import h5py
67
import numpy as np
78
import pandas as pd
89

910
from spyglass.utils.dj_mixin import SpyglassMixin
1011
from spyglass.decoding.v1.clusterless import ClusterlessDecodingV1
1112
from spyglass.common import AnalysisNwbfile
13+
from spyglass.utils.dj_helper_fn import _resolve_external_table
1214

1315
from .ms_opto_stim_protocol import OptoStimProtocol
1416

17+
1518
schema = dj.schema("ms_credible_interval")
1619

1720

@@ -78,9 +81,7 @@ def make(self, key):
7881
spatial_coverage = get_HPD_spatial_coverage(posterior, threshold)
7982

8083
# save the results
81-
credible_df = pd.DataFrame(
82-
spatial_coverage, index=results.time, columns=["coverage"]
83-
)
84+
credible_df = pd.DataFrame({"coverage": spatial_coverage, "time": results.time})
8485

8586
analysis_file_name = AnalysisNwbfile().create(key["nwb_file_name"])
8687
key["analysis_file_name"] = analysis_file_name
@@ -107,4 +108,46 @@ def alligned_response(self, key, marks, window=0.05):
107108
def fetch1_dataframe(self) -> pd.DataFrame:
108109
if not len(nwb := self.fetch_nwb()):
109110
raise ValueError("fetch1_dataframe must be called on a single key")
110-
return nwb[0]["data"]
111+
return nwb[0]["data"].set_index("time")
112+
113+
114+
def _update_coverage_df(analysis_file_name):
115+
"""Reformats the saved coverage dataframe to be consistent with Dandi standards.
116+
117+
For one-time use on 09/24/2025.
118+
Not needed for future use.
119+
For use specifically with the CredibleInterval table.
120+
121+
Args:
122+
analysis_file_name (_type_): _description_
123+
"""
124+
if not (CredibleInterval() & {"analysis_file_name": analysis_file_name}):
125+
raise ValueError("analysis_file_name not found in CredibleInterval table")
126+
127+
path = AnalysisNwbfile().get_abs_path(analysis_file_name)
128+
grp_path = "/scratch/credible_interval"
129+
dataset_path = "/scratch/credible_interval/id"
130+
time_dataset_path = "/scratch/credible_interval/time"
131+
with h5py.File(path, "a") as file:
132+
if time_dataset_path in file:
133+
print("Time dataset already exists, skipping")
134+
return
135+
136+
# interpolate time and add as new vector
137+
grp = file[grp_path]
138+
dset = file[dataset_path]
139+
data = dset[()]
140+
new_data = np.linspace(data.min(), data.max(), len(data))
141+
grp.attrs["colnames"] = ["coverage", "time"]
142+
time_vect = grp.create_dataset(
143+
"time", data=new_data, compression="gzip", compression_opts=4
144+
)
145+
time_vect.attrs["neurodata_type"] = "VectorData"
146+
time_vect.attrs["namespace"] = "core"
147+
time_vect.attrs["description"] = " "
148+
149+
# set index to incremental values
150+
new_index = np.arange(len(data), dtype="int64")
151+
dset[:] = new_index
152+
153+
_resolve_external_table(path, analysis_file_name, location="analysis")

0 commit comments

Comments
 (0)