Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra fiber photometry metadata #26

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,15 @@ def add_wheel_events(self, nwbfile, metadata):

def add_analysis(self, nwbfile: NWBFile) -> None:
event_types_table = EventTypesTable(
name="EventTypes",
name="PeakFluorescenceEventTypes",
description="Contains the type of events.",
)

event_names_mapping = dict(
peaksG="Large transient peaks for green fluorescence",
peaksR="Large transient peaks for red fluorescence",
peaksGRun="Large transient peaks occurring during running periods for green fluorescence",
peaksRRun="Large transient peaks occurring during running periods for red fluorescence",
peaksG="Large transient peaks for Fiber 1 fluorescence",
peaksR="Large transient peaks for Fiber 2 fluorescence",
peaksGRun="Large transient peaks occurring during running periods for Fiber 1 fluorescence",
peaksRRun="Large transient peaks occurring during running periods for Fiber 2 fluorescence",
)

for event_name, event_type_description in event_names_mapping.items():
Expand All @@ -397,7 +397,7 @@ def add_analysis(self, nwbfile: NWBFile) -> None:
return

events = EventsTable(
name="Events",
name="PeakFluorescenceEvents",
description="Contains the onset times of events.",
target_tables={"event_type": event_types_table},
)
Expand Down Expand Up @@ -430,7 +430,7 @@ def add_analysis(self, nwbfile: NWBFile) -> None:

peak_events_table = events.from_dataframe(
peak_events_to_add,
name="Events",
name="PeakFluorescenceEvents",
table_description="Contains the onset times of large fluorescence peaks.",
)
peak_events_table.event_type.table = event_types_table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Literal

import numpy as np
from hdmf.common import DynamicTableRegion
from neuroconv.tools import get_module
from pynwb import NWBFile
from ndx_fiber_photometry import (
Expand Down Expand Up @@ -61,8 +60,30 @@ def add_fiber_photometry_series(
fiber_photometry_table_metadata = fiber_photometry_metadata["FiberPhotometryTable"]
fiber_photometry_table = FiberPhotometryTable(**fiber_photometry_table_metadata)
fiber_photometry_table.add_column(
name="fiber_depth_in_mm", description="The depth of the optical fiber in the unit of millimeters."
name="fiber_depth_in_mm",
description="The depth of the optical fiber in the unit of millimeters.",
)
fiber_photometry_table.add_column(
name="baseline_fluorescence",
description="The baseline fluorescence value for each fiber.",
)
fiber_photometry_table.add_column(
name="normalized_fluorescence",
description="The normalized fluorescence value for each fiber.",
)
fiber_photometry_table.add_column(
name="recording_target_type",
description="Defines whether recordings are made in axons vs cell bodies.",
)
fiber_photometry_table.add_column(
name="signal_to_noise_ratio",
description="The signal to noise ratio for each fiber.",
)
fiber_photometry_table.add_column(
name="cross_correlation_with_acceleration",
description="The cross-correlation between ΔF/F and acceleration.",
)

fiber_photometry_lab_meta_data = FiberPhotometry(
name="FiberPhotometry",
fiber_photometry_table=fiber_photometry_table,
Expand All @@ -80,17 +101,26 @@ def add_fiber_photometry_series(
raise ValueError(f"Fiber metadata for '{fiber_to_add}' not found.")

location = fiber_metadata["location"]
coordinates = fiber_metadata["coordinates"]
fiber_depth_in_mm = fiber_metadata["fiber_depth_in_mm"]

trace_description = trace_metadata["description"]
trace_description += f" from {location} region at {fiber_depth_in_mm / 1000} meters depth."
trace_metadata["description"] = trace_description

fiber_metadata = {
k: v for k, v in fiber_metadata.items() if k not in ["location", "coordinates", "fiber_depth_in_mm"]
all_attributes = set(fiber_metadata.keys())
optical_fiber_attributes = {
"name",
"description",
"manufacturer",
"model",
"numerical_aperture",
"core_diameter_in_um",
}
add_photometry_device(nwbfile, device_metadata=fiber_metadata, device_type="OpticalFiber")
extra_fiber_attributes = all_attributes - optical_fiber_attributes
extra_fiber_metadata = {k: v for k, v in fiber_metadata.items() if k in extra_fiber_attributes}
optical_fiber_metadata = {k: v for k, v in fiber_metadata.items() if k in optical_fiber_attributes}

add_photometry_device(nwbfile, device_metadata=optical_fiber_metadata, device_type="OpticalFiber")

indicator_to_add = trace_metadata["indicator"]
indicator_metadata = next(
Expand Down Expand Up @@ -163,9 +193,7 @@ def add_fiber_photometry_series(
add_photometry_device(nwbfile, device_metadata=emission_filter_metadata, device_type="BandOpticalFilter")

fiber_photometry_table.add_row(
location=location,
coordinates=coordinates,
fiber_depth_in_mm=fiber_depth_in_mm,
**extra_fiber_metadata,
indicator=nwbfile.devices[indicator_to_add],
optical_fiber=nwbfile.devices[fiber_to_add],
excitation_source=nwbfile.devices[excitation_source_to_add],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,58 @@ def process_extra_metadata(
location_fiber_2 = processed_photometry_data["chR"].lower()
allen_location_fiber_1 = allen_location_mapping.get(location_fiber_1, None)
allen_location_fiber_2 = allen_location_mapping.get(location_fiber_2, None)
is_dual_fiber = allen_location_fiber_1 is not None and allen_location_fiber_2 is not None

# Update the metadata for the first fiber
fiber_photometry_metadata = extra_metadata["Ophys"]["FiberPhotometry"]
fibers_metadata = fiber_photometry_metadata["OpticalFibers"]

recording_target_type_mapping = {1: "axons in striatum", 0: "cell bodies in SNc"}

if allen_location_fiber_1 is not None:
fiber_description = fibers_metadata[0]["description"]
fiber_description += f"from the {allen_location_fiber_1} brain region."
fiber_description += f" from the {allen_location_fiber_1} brain region."

recording_target_type = recording_target_type_mapping.get(processed_photometry_data["exStr"][0], "unknown")

fibers_metadata[0].update(
description=fiber_description,
coordinates=processed_photometry_data["RecLocGmm"],
fiber_depth_in_mm=processed_photometry_data["depthG"],
location=allen_location_fiber_1,
label="chGreen", # the name of the channel in the .mat file
recording_target_type=recording_target_type,
baseline_fluorescence=processed_photometry_data["base"][0]
if is_dual_fiber
else processed_photometry_data["base"],
normalized_fluorescence=processed_photometry_data["norm"][0]
if is_dual_fiber
else processed_photometry_data["norm"],
signal_to_noise_ratio=processed_photometry_data["sig2noise"][0],
cross_correlation_with_acceleration=processed_photometry_data["Acc405"][0],
)

if allen_location_fiber_2 is not None:
fiber_2_description = fibers_metadata[1]["description"]
fiber_2_description += f" from the {allen_location_fiber_2} brain region."

recording_target_type = recording_target_type_mapping.get(processed_photometry_data["exStr"][1], "unknown")

fibers_metadata[1].update(
description=fiber_2_description,
coordinates=processed_photometry_data["RecLocRmm"],
fiber_depth_in_mm=processed_photometry_data["depthR"],
location=allen_location_fiber_2,
label="chRed", # the name of the channel in the .mat file
recording_target_type=recording_target_type,
baseline_fluorescence=processed_photometry_data["base"][1]
if is_dual_fiber
else processed_photometry_data["base"],
normalized_fluorescence=processed_photometry_data["norm"][1]
if is_dual_fiber
else processed_photometry_data["norm"],
signal_to_noise_ratio=processed_photometry_data["sig2noise"][1],
cross_correlation_with_acceleration=processed_photometry_data["Acc405"][1],
)

# keep only those fibers metadata that have location information
Expand Down
Loading