Skip to content

Commit

Permalink
Merge pull request #34 from catalystneuro/fix_neurotensin_valence_upd…
Browse files Browse the repository at this point in the history
…ater_updated_neuroconv

Fix custom behavior interfaces after update for neurotensin_valence
  • Loading branch information
CodyCBakerPhD authored Sep 29, 2023
2 parents 4f48faf + 3331368 commit 27be695
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,9 @@ def _load_pickle(self):
pickled = pickle.load(handle)
return pickled

def get_original_timestamps(self) -> np.ndarray:
raise NotImplementedError(
"Unable to retrieve the original unaltered timestamps for this interface! "
"Define the `get_original_timestamps` method for this interface."
)

def get_timestamps(self) -> np.ndarray:
raise NotImplementedError(
"Unable to retrieve timestamps for this interface! Define the `get_timestamps` method for this interface."
)

def align_timestamps(self, aligned_timestamps: np.ndarray):
raise NotImplementedError(
"The protocol for synchronizing the timestamps of this interface has not been specified!"
)

def run_conversion(
def add_to_nwbfile(
self,
nwbfile_path: OptionalFilePathType = None,
nwbfile: Optional[NWBFile] = None,
nwbfile: NWBFile,
metadata: Optional[dict] = None,
original_video_file_path: OptionalFilePathType = None,
labeled_video_file_path: OptionalFilePathType = None,
Expand All @@ -93,60 +76,57 @@ def run_conversion(
column_mappings: Optional[dict] = None,
overwrite: bool = False,
):
with make_or_load_nwbfile(
nwbfile_path=nwbfile_path, nwbfile=nwbfile, metadata=metadata, overwrite=overwrite, verbose=self.verbose
) as nwbfile_out:
pose_estimation_data = self._load_source_data()
pose_estimation_kwargs_from_pickle = dict()
if self.config_file_path is not None:
pose_estimation_config = self._load_pickle()
rate = pose_estimation_config["data"]["fps"]
pose_estimation_kwargs_from_pickle = dict(
scorer=pose_estimation_config["data"]["Scorer"],
# frame_dimensions are in height x width, but for NWB it should be width x height
dimensions=np.array([pose_estimation_config["data"]["frame_dimensions"][::-1]], dtype=np.uint16),
)
pose_estimation_data = self._load_source_data()
pose_estimation_kwargs_from_pickle = dict()
if self.config_file_path is not None:
pose_estimation_config = self._load_pickle()
rate = pose_estimation_config["data"]["fps"]
pose_estimation_kwargs_from_pickle = dict(
scorer=pose_estimation_config["data"]["Scorer"],
# frame_dimensions are in height x width, but for NWB it should be width x height
dimensions=np.array([pose_estimation_config["data"]["frame_dimensions"][::-1]], dtype=np.uint16),
)

pose_estimation_metadata = metadata["PoseEstimation"]
assert (
rate is not None
), "The 'rate' must be specified when the sampling frequency cannot be read from the configuration (.pickle) file."

pose_estimation_series = []
for column_name in pose_estimation_metadata:
pose_estimation_series_data = pose_estimation_data[column_name]

pose_estimation_series.append(
PoseEstimationSeries(
name=pose_estimation_metadata[column_name]["name"],
description=pose_estimation_metadata[column_name]["description"],
data=pose_estimation_series_data[["x", "y"]].values,
unit="px",
reference_frame="(0,0) corresponds to the top left corner of the cage.",
rate=rate,
starting_time=starting_time or 0.0,
confidence=pose_estimation_series_data["likelihood"].values,
confidence_definition=pose_estimation_metadata[column_name]["confidence_definition"],
)
pose_estimation_metadata = metadata["PoseEstimation"]
assert (
rate is not None
), "The 'rate' must be specified when the sampling frequency cannot be read from the configuration (.pickle) file."

pose_estimation_series = []
for column_name in pose_estimation_metadata:
pose_estimation_series_data = pose_estimation_data[column_name]

pose_estimation_series.append(
PoseEstimationSeries(
name=pose_estimation_metadata[column_name]["name"],
description=pose_estimation_metadata[column_name]["description"],
data=pose_estimation_series_data[["x", "y"]].values,
unit="px",
reference_frame="(0,0) corresponds to the top left corner of the cage.",
rate=rate,
starting_time=starting_time or 0.0,
confidence=pose_estimation_series_data["likelihood"].values,
confidence_definition=pose_estimation_metadata[column_name]["confidence_definition"],
)

# The parameters for the pose estimation container
pose_estimation_kwargs = dict(
pose_estimation_series=pose_estimation_series,
source_software="DeepLabCut",
nodes=[pose_estimation_metadata[column_name]["name"] for column_name in pose_estimation_metadata],
**pose_estimation_kwargs_from_pickle,
)

if original_video_file_path is not None:
pose_estimation_kwargs.update(original_videos=[original_video_file_path])
if labeled_video_file_path is not None:
pose_estimation_kwargs.update(labeled_videos=[labeled_video_file_path])
if edges is not None:
pose_estimation_kwargs.update(edges=np.asarray(edges, dtype=np.uint8))
# The parameters for the pose estimation container
pose_estimation_kwargs = dict(
pose_estimation_series=pose_estimation_series,
source_software="DeepLabCut",
nodes=[pose_estimation_metadata[column_name]["name"] for column_name in pose_estimation_metadata],
**pose_estimation_kwargs_from_pickle,
)

if original_video_file_path is not None:
pose_estimation_kwargs.update(original_videos=[original_video_file_path])
if labeled_video_file_path is not None:
pose_estimation_kwargs.update(labeled_videos=[labeled_video_file_path])
if edges is not None:
pose_estimation_kwargs.update(edges=np.asarray(edges, dtype=np.uint8))

# Create the container for pose estimation
pose_estimation = PoseEstimation(**pose_estimation_kwargs)
# Create the container for pose estimation
pose_estimation = PoseEstimation(**pose_estimation_kwargs)

behavior = get_module(nwbfile_out, "behavior", "Processed behavior data.")
behavior.add(pose_estimation)
behavior = get_module(nwbfile, "behavior", "Processed behavior data.")
behavior.add(pose_estimation)
Original file line number Diff line number Diff line change
Expand Up @@ -113,51 +113,38 @@ def get_metadata(self) -> dict:
metadata = dict_deep_update(metadata, metadata_from_oif_file)
return metadata

def align_timestamps(self, aligned_timestamps: np.ndarray):
pass

def get_original_timestamps(self) -> np.ndarray:
pass

def get_timestamps(self) -> np.ndarray:
pass

def run_conversion(
def add_to_nwbfile(
self,
nwbfile_path: Optional[str] = None,
nwbfile: Optional[NWBFile] = None,
nwbfile: NWBFile,
metadata: Optional[dict] = None,
conversion_options: Optional[dict] = None,
overwrite: bool = False,
):
with make_or_load_nwbfile(
nwbfile_path=nwbfile_path, nwbfile=nwbfile, metadata=metadata, overwrite=overwrite, verbose=self.verbose
) as nwbfile_out:
images = Images(name=metadata["Images"]["name"], description=metadata["Images"]["description"])
file_list = list(self.oif.series[0])

for file_ind, file in enumerate(file_list):
image = self.oif.series[0].imread(file)
image_metadata = metadata["Images"]["images"][file_ind]
images = Images(name=metadata["Images"]["name"], description=metadata["Images"]["description"])
file_list = list(self.oif.series[0])

for file_ind, file in enumerate(file_list):
image = self.oif.series[0].imread(file)
image_metadata = metadata["Images"]["images"][file_ind]

images.add_image(
GrayscaleImage(
name=image_metadata["name"],
data=H5DataIO(image.T, compression=True),
description=image_metadata["description"],
)
)

# Add composite images
if self.composite_images is not None:
for image_ind in range(self.composite_images.shape[0]):
image_metadata = metadata["Images"]["images"][len(file_list) + image_ind]
images.add_image(
GrayscaleImage(
name=image_metadata["name"],
data=H5DataIO(image.T, compression=True),
data=H5DataIO(self.composite_images[image_ind].T, compression=True),
description=image_metadata["description"],
)
)

# Add composite images
if self.composite_images is not None:
for image_ind in range(self.composite_images.shape[0]):
image_metadata = metadata["Images"]["images"][len(file_list) + image_ind]
images.add_image(
GrayscaleImage(
name=image_metadata["name"],
data=H5DataIO(self.composite_images[image_ind].T, compression=True),
description=image_metadata["description"],
)
)

nwbfile_out.add_acquisition(images)
nwbfile.add_acquisition(images)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
FilePathType,
FolderPathType,
)
from nwbinspector import inspect_nwb
from nwbinspector import inspect_nwbfile
from nwbinspector.inspector_tools import save_report, format_messages
from packaging.version import Version

Expand Down Expand Up @@ -180,15 +180,15 @@ def session_to_nwb(
session_start_time_dt = parser.parse(session_start_time)
metadata["NWBFile"].update(session_start_time=session_start_time_dt)

nwbfile_path = Path(nwbfile_path)
try:
# Run conversion
converter.run_conversion(
nwbfile_path=str(nwbfile_path), metadata=metadata, conversion_options=conversion_options
)

# Run inspection for nwbfile
nwbfile_path = Path(nwbfile_path)
results = list(inspect_nwb(nwbfile_path=nwbfile_path))
results = list(inspect_nwbfile(nwbfile_path=nwbfile_path))
report_path = nwbfile_path.parent / f"{nwbfile_path.stem}_inspector_result.txt"
save_report(
report_file_path=report_path,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-e git+https://github.com/catalystneuro/neuroconv@add_ignore_timestamps_errors_to_interface#egg=neuroconv
neuroconv[openephys,plexon,video]
git+https://github.com/neuralensemble/python-neo@master
ndx_pose>=0.1.1
oiffile==2022.9.29
easydict>=1.10
Expand Down

0 comments on commit 27be695

Please sign in to comment.