Skip to content

Commit

Permalink
Address comments on release from Marina
Browse files Browse the repository at this point in the history
Add project codes to stim processing.
Switch trials_id calculation to not require stimulus_block.
Remove x,y,heigh,width
Update change log
Update cells table.
  • Loading branch information
morriscb committed Oct 24, 2023
1 parent ecae373 commit a89ea22
Show file tree
Hide file tree
Showing 10 changed files with 133,300 additions and 133,176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
import pandas as pd
import numpy as np
import warnings

from allensdk.api.warehouse_cache.cache import Cache
from allensdk.brain_observatory.behavior.behavior_ophys_experiment import \
Expand All @@ -25,6 +26,10 @@
.project_cache_base import ProjectCacheBase


class UpdatedStimulusPresentationTableWarning(UserWarning):
pass


class VBOLimsCache(Cache):
"""
A class that ineherits from the warehouse Cache and provides
Expand Down Expand Up @@ -121,6 +126,22 @@ def __init__(
version=version,
cache=cache)

warnings.warn(
message="As of AllenSDK version 2.16.0, the latest Visual "
"Behavior Ophys data has be significantly updated from "
"previous releases. Specifically the user will need to "
"update all processing of the stimulus_presentations "
"tables. These tables now include multiple stimulus types "
"delineated by the columns `stimulus_block` and "
"`stimulus_block_name`. The previous are stored in the "
"block name containing 'change_detection' and can be "
"accessed in the pandas table by using: "
"`stimulus_presentations["
"stimulus_presentations.stimulus_block_name.str.contains"
"('change_detection')]`",
category=UpdatedStimulusPresentationTableWarning
)

@classmethod
def cloud_api_class(cls):
return BehaviorProjectCloudApi
Expand Down Expand Up @@ -346,7 +367,7 @@ def get_natural_movie_template(self, n_workers=None) -> pd.DataFrame:
frames as presented to the mouse. The DataFrame is indexed with the
same frame index as shown in the stimulus presentation table.
The processing of the movie requires signicant processing and its
The processing of the movie requires significant processing and its
return size is very large so take care in requesting this data.
Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def sanitize_data_columns(


class BehaviorProjectCloudApi(BehaviorProjectBase, ProjectCloudApiBase):
MANIFEST_COMPATIBILITY = ["0.0.0", "2.0.0"]
MANIFEST_COMPATIBILITY = ["1.0.0", "2.0.0"]

def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,7 @@ def _get_ophys_cells_table(self):
SELECT
cr.id as cell_roi_id,
cr.cell_specimen_id,
cr.ophys_experiment_id,
cr.x,
cr.y,
cr.height,
cr.width
cr.ophys_experiment_id
FROM cell_rois AS cr
JOIN ophys_cell_segmentation_runs AS ocsr
ON ocsr.id=cr.ophys_cell_segmentation_run_id
Expand Down
105 changes: 85 additions & 20 deletions allensdk/brain_observatory/behavior/behavior_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
from allensdk.brain_observatory.behavior.data_objects.trials.trials import (
Trials,
)
from allensdk.brain_observatory.behavior.stimulus_processing import (
compute_trials_id_for_stimulus,
)
from allensdk.brain_observatory.sync_dataset import Dataset as SyncDataset
from allensdk.core import (
DataObject,
Expand Down Expand Up @@ -1072,8 +1069,16 @@ def stimulus_presentations(self) -> pd.DataFrame:
for a given duration, typically 250 ms) and whose columns are
presentation characteristics.
Adds trials_id to the stimulus table if the column is not already
present.
For VBO, the latest release adds additional
stimuli to the table divided into stimulus blocks. The block previously
released is the one whose stimulus block name starts with
"change_detection". Users can access the this block via in the pandas
table with:
`stimulus_presentations[
stimulus_presentations.stimulus_block_name.str.contains(
'change_detection'
)
]`
Returns
-------
Expand All @@ -1083,40 +1088,89 @@ def stimulus_presentations(self) -> pd.DataFrame:
and whose columns are presentation characteristics.
dataframe columns:
stimulus_block: (int)
Index of the the different stimuli presented to the mouse
in order.
stimulus_block_name [VBO only]: (int)
Name of the individual stimulus blocks describing briefly
what was shown to the mouse.
stimulus_presentations_id [index]: (int)
identifier for a stimulus presentation
(presentation of an image)
image_index [VBO only]: (int)
image index (0-7) for a given session,
corresponding to each image name
image_name: (string)
Name of the image presented to the mouse if applicable.
This is indexes in to the stimulus_templates object.
movie_frame_index [VBO only]: (int)
Index into the natural movie presented. Use
the method get_raw_natural_movie or if you the image
stretched and warped to the monitor use
get_natural_movie_template. (Warning this is a very compute
intensive process).
duration: (float)
duration of an image presentation (flash)
in seconds (stop_time - start_time). NaN if omitted
start_frame: (int)
image presentation start frame
end_frame: (float)
image presentation end frame
start_time: (float)
image presentation start time in seconds
end_time: (float)
image presentation end time in seconds
image_index: (int)
image index (0-7) for a given session,
corresponding to each image name
start_frame: (int)
image presentation start frame
end_frame: (int)
image presentation end frame
is_change: (bool)
True if the image stimulus has changed. <NA> if not
an image stimulus block.
is_image_novel: (bool)
True if the image stimulus is novel. <NA> if not
an image stimulus block.
omitted: (bool)
True if no image was shown for this stimulus
presentation
presentation. <NA> if not an image stimulus block.
movie_repeat [VBO only]: (int)
Number of times the movie has been repeated during the
natural movie block. -99 if not a movie block.
flashes_since_change: (bool)
Number of times the image has been flashed in between when
it has changed.
trials_id: (int)
Id to match to the table Index of the trials table.
If not present, it will be added.
active: (bool)
Stimulus block overlaps with the trials.
is_sham_change: (bool)
True if the stimulus overlaps with a catch trial.
stimulus_index [VBN only]: (int)
0 for gabor_20_deg_250ms, 1 for flash_250ms. -99 otherwise
position_x [VBN only]: (float)
X position of gabor grating when shown. NaN if not
applicable.
position_y [VBN only]: (float)
Y position of gabor grating when shown. NaN if not
applicable.
color [VBN only]: (float)
Value of the color int flash_250ms stimulus. Either -1 or
1. NaN if not applicable.
rewarded [VBN only]: (bool)
Merge of trials data. True if the mouse was rewarded during
the stimulus. <NA> if not applicable to stimulus block.
orientation [VBN only]: (float)
Orientation of the gabor grating in degrees.
temporal_frequency [VBN only]: (float)
Temporal_frequency of the gabor grating. NaN if not
applicable.
spatial_frequency [VBN only]: (float)
spatial_frequency of the gabor grating. NaN if not
applicable.
contrast [VBN only]: float
Contrast value of the gabor grating. NaN if not applicable.
"""
table = self._stimuli.presentations.value
table = table.drop(columns=["image_set", "index"], errors="ignore")
table = table.rename(columns={"stop_time": "end_time"})

if (
"trials_id" not in table.columns
and "stimulus_block" in table.columns
):
table["trials_id"] = compute_trials_id_for_stimulus(
table, self.trials
)
return table

@property
Expand Down Expand Up @@ -1279,6 +1333,17 @@ def trials(self) -> pd.DataFrame:
change_image_name: (string)
name of image that is changed to at the change time,
on go trials
change_time: (float)
Time in the session at which the change occurred.
response_latency [VBO only]: (float)
Delay between change and first lick in seconds. NaN if
no change occurred.
change_frame: (int)
Frame in the session at which the change occurred. -99 if
no change.
change_time_no_display_delay [VBN only]: (float)
Time of the change in seconds, before the display lag is
accounted for and applied.
"""
return self._trials.data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(
[
"flashes_since_change",
"image_index",
"movie_repeat",
"movie_frame_index",
"repeat",
"stimulus_index",
Expand Down
21 changes: 11 additions & 10 deletions allensdk/brain_observatory/behavior/stimulus_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,6 @@ def compute_trials_id_for_stimulus(
index=stim_pres_table.index,
name="trials_id",
).astype("int")
# Return input frame if the stimulus_block or active is not available.
if (
"stimulus_block" not in stim_pres_table.columns
or "active" not in stim_pres_table.columns
):
return trials_ids
active_sorted = stim_pres_table.active

# Find stimulus blocks that start within a trial. Copy the trial_id
# into our new trials_ids series. For some sessions there are gaps in
Expand All @@ -757,12 +750,20 @@ def compute_trials_id_for_stimulus(
max_trials_stop = trials_table.stop_time.max()
for idx, trial in trials_table.iterrows():
stim_mask = (
(stim_pres_table.start_time > trial.start_time)
& (stim_pres_table.start_time < max_trials_stop)
& (~stim_pres_table.image_name.isna())
(stim_pres_table.start_time > trial.start_time)
& (stim_pres_table.start_time < max_trials_stop)
& (~stim_pres_table.image_name.isna())
)
trials_ids[stim_mask] = idx

# Return input frame if the stimulus_block or active is not available.
if (
"stimulus_block" not in stim_pres_table.columns
or "active" not in stim_pres_table.columns
):
return trials_ids
active_sorted = stim_pres_table.active

# The code below finds all stimulus blocks that contain images/trials
# and attempts to detect blocks that are identical to copy the associated
# trials_ids into those blocks. In the parlance of the data this is
Expand Down
8 changes: 8 additions & 0 deletions allensdk/brain_observatory/ophys/project_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
#
#####################################################################

# Last three codes are projects that have experiments that were pulled into
# the data release. The mouse in these codes had most of its sessions
# reassigned to one of the first 4 codes. This is a patch to allow these
# projects to get the proper stimulus presentation tables with correct
# stimulus_block_names.
PROJECT_CODES = [
"VisualBehavior",
"VisualBehaviorTask1B",
"VisualBehaviorMultiscope",
"VisualBehaviorMultiscope4areasx2d",
"DevelopmentMultiscope4areasx2d",
"MesoscopeDevelopment",
"VisBIntTestDatacube"
]

NUM_STRUCTURES_DICT = {
Expand Down
Loading

0 comments on commit a89ea22

Please sign in to comment.