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

Easily matching brain regions to sort groups #221

Closed
edeno opened this issue Apr 19, 2022 · 6 comments · Fixed by #910
Closed

Easily matching brain regions to sort groups #221

edeno opened this issue Apr 19, 2022 · 6 comments · Fixed by #910
Assignees
Labels
enhancement New feature or request

Comments

@edeno
Copy link
Collaborator

edeno commented Apr 19, 2022

It seems like the specific brain region names are listed as targed_location in the nwb files and this isn't available anywhere. @jguides has some code to match these two, which will help for future analyses. We should consider adding that code to spyglass and make sure it is easy to match up brain region and sort groups both for clustered units and individual electrodes.

@edeno
Copy link
Collaborator Author

edeno commented Apr 19, 2022

Another note that it is a little confusing why targeted_location is not in the ElectrodeGroup table or the BrainRegion table. Also in the BrainRegion table, subregion_name and subsubregion_name are never used.

@edeno edeno added the enhancement New feature or request label May 8, 2023
@samuelbray32
Copy link
Collaborator

@edeno , I'm unclear on what subregion and subsubregion were intended to be used for. One option is to put targeted_location in subregion during population, but wanted to see if that would be intuitive

@edeno
Copy link
Collaborator Author

edeno commented Jan 2, 2024

targeted_location refers to the experimenters intended target location because histology hasn't been done yet to determine the true location (with subregion and subsubregion). Therefore, putting targeted_location in subregion does not make sense. As a note, I don't think there has been a case where the histology has been done before the creation of the NWB file for us.

@samuelbray32
Copy link
Collaborator

Ok, so we want to leave the subregion keys unused as is with respect to this. In that case, can either:

  1. Make a utility function that goes into the nwb file and returns the set of targeted location(s) of electrodes in a group. More generically, it could return a dataframe of positional information for a list of electrodes.
  2. Add new key targeted_location to the BrainRegion table with this info. Would be a bit more tedious to edit existing database, but doable

@samuelbray32
Copy link
Collaborator

Example of the utility function solution:

from pynwb import NWBHDF5IO

def get_sort_group_targeted_locations(key: dict, nwbfile=None):
    """return targeted location(s) for a given sort group

    Parameters
    ----------
    key : dict
        restriction dict for SortGroup
    nwbfile : NWBFile, optional
        opened NWBFile object--speeds up multiple calls to this function, by default None

    Returns
    -------
    list[str]
        list of targeted locations for the given sort group
    """
    sort_group_electrodes = (SortGroup().SortGroupElectrode & key).fetch('electrode_id')
    if nwbfile is None:
        nwb_file_name = key['nwb_file_name']
        with NWBHDF5IO(f'/stelmo/nwb/raw/{nwb_file_name}', 'r') as io:
            nwbfile = io.read()
            electrode_df = nwbfile.electrodes.to_dataframe()
            groups = set([electrode_df[electrode_df.index == id].group_name.values[0] for id in sort_group_electrodes])
            targeted_location = [nwbfile.electrode_groups[group].targeted_location for group in groups]
    else:
        electrode_df = nwbfile.electrodes.to_dataframe()
        groups = set([electrode_df[electrode_df.index == id].group_name.values[0] for id in sort_group_electrodes])
        targeted_location = [nwbfile.electrode_groups[group].targeted_location for group in groups]
    return targeted_location

from tqdm import tqdm
def get_sort_groups_in_targeted_location(nwb_file_name: str, targeted_location: str):
    """return sort groups that target a given region

    Parameters
    ----------
    nwb_file_name : str
        the name of the session
    region_name : str
        the name of the region you want

    Returns
    -------
    list[int]
        The sort group ids that target the given region
    """
    sort_groups = (SortGroup() & {"nwb_file_name": nwb_file_name}).fetch('sort_group_id')
    on_target_sort_groups = []
    with NWBHDF5IO(f'/stelmo/nwb/raw/{nwb_file_name}', 'r') as io:
        nwbfile = io.read()
        for sort_group in tqdm(sort_groups):
            key = dict(nwb_file_name=nwb_file_name, sort_group_id=sort_group)
            targeted_locations = get_sort_group_targeted_locations(key, nwbfile=nwbfile)
            if targeted_locations == [targeted_location]:
                on_target_sort_groups.append(sort_group)
    return sort_groups

@edeno
Copy link
Collaborator Author

edeno commented Feb 3, 2024

Should implement a function in SortedSpikesGroup to retrieve brain area per spike_times.

Similar for LFP grouping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants