Ejemplo n.º 1
0
def test_get_stimulus_templates(behavior_stimuli_data_fixture, remove_stimuli,
                                expected_templates):
    for stimuli in remove_stimuli:
        del (behavior_stimuli_data_fixture['items']['behavior']['stimuli']
             [stimuli])
    templates = get_stimulus_templates(behavior_stimuli_data_fixture)
    assert templates == expected_templates
Ejemplo n.º 2
0
def test_get_stimulus_templates(behavior_stimuli_data_fixture):
    templates = get_stimulus_templates(behavior_stimuli_data_fixture,
                                       grating_images_dict={})

    assert templates.image_set_name == 'test_image_set'
    assert len(templates) == 1
    assert list(templates.keys()) == ['im065']

    for img in templates.values():
        assert isinstance(img, StimulusImage)

    expected_path = os.path.join(get_resources_dir(), 'stimulus_template',
                                 'expected')

    expected_unwarped_path = os.path.join(
        expected_path, 'im065_unwarped.pkl')
    expected_unwarped = pd.read_pickle(expected_unwarped_path)

    expected_warped_path = os.path.join(
        expected_path, 'im065_warped.pkl')
    expected_warped = pd.read_pickle(expected_warped_path)

    for img_name in templates:
        img = templates[img_name]
        assert np.allclose(a=expected_unwarped,
                           b=img.unwarped, equal_nan=True)
        assert np.allclose(a=expected_warped,
                           b=img.warped, equal_nan=True)

    for img_name, img in templates.items():
        img = templates[img_name]
        assert np.allclose(a=expected_unwarped,
                           b=img.unwarped, equal_nan=True)
        assert np.allclose(a=expected_warped,
                           b=img.warped, equal_nan=True)
Ejemplo n.º 3
0
def test_get_stimulus_templates_for_gratings(behavior_stimuli_data_fixture,
                                             grating_images_dict, expected):
    templates = get_stimulus_templates(behavior_stimuli_data_fixture,
                                       grating_images_dict=grating_images_dict)

    assert templates.image_set_name == 'grating'
    assert list(templates.keys()) == ['gratings_90.0']
    assert np.allclose(templates['gratings_90.0'].warped,
                       np.array([[1, 1], [1, 1]]))
    assert np.allclose(templates['gratings_90.0'].unwarped,
                       np.array([[2, 2], [2, 2]]))
Ejemplo n.º 4
0
    def get_stimulus_templates(self) -> Dict[str, np.ndarray]:
        """Get stimulus templates (movies, scenes) for behavior session.

        Returns
        -------
        Dict[str, np.ndarray]
            A dictionary containing the stimulus images presented during the
            session. Keys are data set names, and values are 3D numpy arrays.
        """
        data = self._behavior_stimulus_file()
        return get_stimulus_templates(data)
Ejemplo n.º 5
0
    def from_stimulus_file(
            cls, stimulus_file: StimulusFile,
            limit_to_images: Optional[List] = None) -> "Templates":
        """Get stimulus templates (movies, scenes) for behavior session."""

        # TODO: Eventually the `grating_images_dict` should be provided by the
        #       BehaviorLimsExtractor/BehaviorJsonExtractor classes.
        #       - NJM 2021/2/23

        gratings_dir = "/allen/programs/braintv/production/visualbehavior"
        gratings_dir = os.path.join(gratings_dir,
                                    "prod5/project_VisualBehavior")
        grating_images_dict = {
            "gratings_0.0": {
                "warped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(gratings_dir,
                                                  "warped_grating_0.png")))),
                "unwarped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(
                        gratings_dir, "masked_unwarped_grating_0.png"))))
            },
            "gratings_90.0": {
                "warped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(gratings_dir,
                                                  "warped_grating_90.png")))),
                "unwarped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(
                        gratings_dir, "masked_unwarped_grating_90.png"))))
            },
            "gratings_180.0": {
                "warped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(gratings_dir,
                                                  "warped_grating_180.png")))),
                "unwarped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(
                        gratings_dir, "masked_unwarped_grating_180.png"))))
            },
            "gratings_270.0": {
                "warped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(gratings_dir,
                                                  "warped_grating_270.png")))),
                "unwarped": np.asarray(imageio.imread(
                    safe_system_path(os.path.join(
                        gratings_dir, "masked_unwarped_grating_270.png"))))
            }
        }

        pkl = stimulus_file.data
        t = get_stimulus_templates(pkl=pkl,
                                   grating_images_dict=grating_images_dict,
                                   limit_to_images=limit_to_images)
        return Templates(templates=t)
Ejemplo n.º 6
0
def test_add_stimulus_templates(nwbfile, behavior_stimuli_data_fixture,
                                roundtrip, roundtripper):
    stimulus_templates = get_stimulus_templates(behavior_stimuli_data_fixture,
                                                grating_images_dict={})

    nwb.add_stimulus_template(nwbfile, stimulus_templates)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    stimulus_templates_obt = obt.get_stimulus_templates()

    assert stimulus_templates_obt == stimulus_templates
Ejemplo n.º 7
0
 def get_stimulus_templates(self):
     behavior_stimulus_file = self.get_behavior_stimulus_file()
     data = pd.read_pickle(behavior_stimulus_file)
     return get_stimulus_templates(data)
Ejemplo n.º 8
0
    def get_stimulus_templates(self) -> Optional[StimulusTemplate]:
        """Get stimulus templates (movies, scenes) for behavior session.

        Returns
        -------
        StimulusTemplate or None if there are no images for the experiment
        """

        # TODO: Eventually the `grating_images_dict` should be provided by the
        #       BehaviorLimsExtractor/BehaviorJsonExtractor classes.
        #       - NJM 2021/2/23

        gratings_dir = "/allen/programs/braintv/production/visualbehavior"
        gratings_dir = os.path.join(gratings_dir,
                                    "prod5/project_VisualBehavior")
        grating_images_dict = {
            "gratings_0.0": {
                "warped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "warped_grating_0.png")))),
                "unwarped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "masked_unwarped_grating_0.png"))))
            },
            "gratings_90.0": {
                "warped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "warped_grating_90.png")))),
                "unwarped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "masked_unwarped_grating_90.png"))))
            },
            "gratings_180.0": {
                "warped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "warped_grating_180.png")))),
                "unwarped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "masked_unwarped_grating_180.png"))))
            },
            "gratings_270.0": {
                "warped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "warped_grating_270.png")))),
                "unwarped":
                np.asarray(
                    imageio.imread(
                        safe_system_path(
                            os.path.join(gratings_dir,
                                         "masked_unwarped_grating_270.png"))))
            }
        }

        pkl = self._behavior_stimulus_file()
        return get_stimulus_templates(pkl=pkl,
                                      grating_images_dict=grating_images_dict)