Пример #1
0
def get_images_dict(pkl) -> Dict:
    """
    Gets the dictionary of images that were presented during an experiment
    along with image set metadata and the image specific metadata. This
    function uses the path to the image pkl file to read the images and their
    metadata from the pkl file and return this dictionary.
    Parameters
    ----------
    pkl: The pkl file containing the data for the stimuli presented during
         experiment

    Returns
    -------
    Dict:
        A dictionary containing keys images, metadata, and image_attributes.
        These correspond to paths to image arrays presented, metadata
        on the whole set of images, and metadata on specific images,
        respectively.

    """
    # Sometimes the source is a zipped pickle:
    pkl_stimuli = pkl["items"]["behavior"]["stimuli"]
    metadata = {'image_set': pkl_stimuli["images"]["image_path"]}

    # Get image file name;
    # These are encoded case-insensitive in the pickle file :/
    filename = convert_filepath_caseinsensitive(metadata['image_set'])

    image_set = load_pickle(open(filename, 'rb'))
    images = []
    images_meta = []

    ii = 0
    for cat, cat_images in image_set.items():
        for img_name, img in cat_images.items():
            meta = dict(
                image_category=cat.decode("utf-8"),
                image_name=img_name.decode("utf-8"),
                orientation=np.NaN,
                phase=np.NaN,
                spatial_frequency=np.NaN,
                image_index=ii,
            )

            images.append(img)
            images_meta.append(meta)

            ii += 1

    images_dict = dict(
        metadata=metadata,
        images=images,
        image_attributes=images_meta,
    )

    return images_dict
Пример #2
0
def get_stimulus_metadata(pkl) -> pd.DataFrame:
    """
    Gets the stimulus metadata for each type of stimulus presented during
    the experiment. The metadata is return for gratings, images, and omitted
    stimuli.
    Parameters
    ----------
    pkl: the pkl file containing the information about what stimuli were
         presented during the experiment

    Returns
    -------
    pd.DataFrame:
        The dataframe containing a row for every stimulus that was presented
        during the experiment. The row contains the following data,
        image_category, image_name, image_set, phase, spatial_frequency,
        orientation, and image index.

    """
    stimuli = pkl['items']['behavior']['stimuli']
    if 'images' in stimuli:
        images = get_images_dict(pkl)
        stimulus_index_df = pd.DataFrame(images['image_attributes'])
        image_set_filename = convert_filepath_caseinsensitive(
            images['metadata']['image_set'])
        stimulus_index_df['image_set'] = get_image_set_name(
            image_set_path=image_set_filename)
    else:
        stimulus_index_df = pd.DataFrame(columns=[
            'image_name', 'image_category', 'image_set', 'phase',
            'spatial_frequency', 'image_index'
        ])

    # get the grating metadata will be empty if gratings are absent
    grating_df = get_gratings_metadata(stimuli,
                                       start_idx=len(stimulus_index_df))
    stimulus_index_df = stimulus_index_df.append(grating_df,
                                                 ignore_index=True,
                                                 sort=False)

    # Add an entry for omitted stimuli
    omitted_df = pd.DataFrame({
        'image_category': ['omitted'],
        'image_name': ['omitted'],
        'image_set': ['omitted'],
        'orientation': np.NaN,
        'phase': np.NaN,
        'spatial_frequency': np.NaN,
        'image_index': len(stimulus_index_df)
    })
    stimulus_index_df = stimulus_index_df.append(omitted_df,
                                                 ignore_index=True,
                                                 sort=False)
    stimulus_index_df.set_index(['image_index'], inplace=True, drop=True)
    return stimulus_index_df
Пример #3
0
    def __init__(self, image_set_name: str, images: List[StimulusImage]):
        """
        Parameters
        ----------
        image_set_name:
            the name of the image set
        images
            List of images
        """
        self._image_set_name = image_set_name

        image_set_name = convert_filepath_caseinsensitive(image_set_name)
        self._image_set_filepath = image_set_name

        self._images: Dict[str, StimulusImage] = {}

        for image in images:
            self._images[image.name] = image
Пример #4
0
def get_stimulus_templates(
        pkl: dict,
        grating_images_dict: Optional[dict] = None
) -> Optional[StimulusTemplate]:
    """
    Gets images presented during experiments from the behavior stimulus file
    (*.pkl)

    Parameters
    ----------
    pkl : dict
        Loaded pkl dict containing data for the presented stimuli.
    grating_images_dict : Optional[dict]
        Because behavior pkl files do not contain image versions of grating
        stimuli, they must be obtained from an external source. The
        grating_images_dict is a nested dictionary where top level keys
        correspond to grating image names (e.g. 'gratings_0.0',
        'gratings_270.0') as they would appear in table returned by
        get_gratings_metadata(). Sub-nested dicts are expected to have 'warped'
        and 'unwarped' keys where values are numpy image arrays
        of aforementioned warped or unwarped grating stimuli.

    Returns
    -------
    StimulusTemplate:
        StimulusTemplate object containing images that were presented during
        the experiment

    """

    pkl_stimuli = pkl['items']['behavior']['stimuli']
    if 'images' in pkl_stimuli:
        images = get_images_dict(pkl)
        image_set_filepath = images['metadata']['image_set']
        image_set_name = get_image_set_name(image_set_path=image_set_filepath)
        image_set_name = convert_filepath_caseinsensitive(image_set_name)

        return StimulusTemplateFactory.from_unprocessed(
            image_set_name=image_set_name,
            image_attributes=images['image_attributes'],
            images=images['images'])
    elif 'grating' in pkl_stimuli:
        if (grating_images_dict is None) or (not grating_images_dict):
            raise RuntimeError("The 'grating_images_dict' param MUST "
                               "be provided to get stimulus templates "
                               "because this pkl data contains "
                               "gratings presentations.")
        gratings_metadata = get_gratings_metadata(pkl_stimuli).to_dict(
            orient='records')

        unwarped_images = []
        warped_images = []
        for image_attrs in gratings_metadata:
            image_name = image_attrs['image_name']
            grating_imgs_sub_dict = grating_images_dict[image_name]
            unwarped_images.append(grating_imgs_sub_dict['unwarped'])
            warped_images.append(grating_imgs_sub_dict['warped'])

        return StimulusTemplateFactory.from_processed(
            image_set_name='grating',
            image_attributes=gratings_metadata,
            unwarped=unwarped_images,
            warped=warped_images)
    else:
        warnings.warn(
            "Could not determine stimulus template images from pkl file. "
            f"The pkl stimuli nested dict "
            "(pkl['items']['behavior']['stimuli']) contained neither "
            "'images' nor 'grating' but instead: "
            f"'{pkl_stimuli.keys()}'")
        return None