|
| 1 | +from typing import Dict |
1 | 2 | import re |
| 3 | +import numpy as np |
2 | 4 |
|
3 | 5 | description_dict = { |
4 | 6 | # key is a regex and value is returned on match |
@@ -55,25 +57,82 @@ def get_expt_description(session_type: str) -> str: |
55 | 57 | return match.popitem()[1] |
56 | 58 |
|
57 | 59 |
|
58 | | -def get_task_parameters(data): |
| 60 | +def get_task_parameters(data: Dict) -> Dict: |
| 61 | + """ |
| 62 | + Read task_parameters metadata from the behavior stimulus pickle file. |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + data: dict |
| 67 | + The nested dict read in from the behavior stimulus pickle file. |
| 68 | + All of the data expected by this method lives under |
| 69 | + data['items']['behavior'] |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + dict |
| 74 | + A dict containing the task_parameters associated with this session. |
| 75 | + """ |
59 | 76 | behavior = data["items"]["behavior"] |
| 77 | + stimuli = behavior['stimuli'] |
| 78 | + config = behavior["config"] |
| 79 | + doc = config["DoC"] |
60 | 80 |
|
61 | 81 | task_parameters = {} |
| 82 | + |
62 | 83 | task_parameters['blank_duration_sec'] = \ |
63 | | - [float(x) for x in behavior['config']['DoC']['blank_duration_range']] |
64 | | - task_parameters['stimulus_duration_sec'] = \ |
65 | | - behavior['config']['DoC']['stimulus_window'] |
| 84 | + [float(x) for x in doc['blank_duration_range']] |
| 85 | + |
| 86 | + if 'images' in stimuli: |
| 87 | + stim_key = 'images' |
| 88 | + elif 'grating' in stimuli: |
| 89 | + stim_key = 'grating' |
| 90 | + else: |
| 91 | + msg = "Cannot get stimulus_duration_sec\n" |
| 92 | + msg += "'images' and/or 'grating' not a valid " |
| 93 | + msg += "key in pickle file under " |
| 94 | + msg += "['items']['behavior']['stimuli']\n" |
| 95 | + msg += f"keys: {list(stimuli.keys())}" |
| 96 | + raise RuntimeError(msg) |
| 97 | + |
| 98 | + stim_duration = stimuli[stim_key]['flash_interval_sec'] |
| 99 | + |
| 100 | + # from discussion in |
| 101 | + # https://github.com/AllenInstitute/AllenSDK/issues/1572 |
| 102 | + # |
| 103 | + # 'flash_interval' contains (stimulus_duration, gray_screen_duration) |
| 104 | + # (as @matchings said above). That second value is redundant with |
| 105 | + # 'blank_duration_range'. I'm not sure what would happen if they were |
| 106 | + # set to be conflicting values in the params. But it looks like |
| 107 | + # they're always consistent. It should always be (0.25, 0.5), |
| 108 | + # except for TRAINING_0 and TRAINING_1, which have statically |
| 109 | + # displayed stimuli (no flashes). |
| 110 | + |
| 111 | + if stim_duration is None: |
| 112 | + stim_duration = np.NaN |
| 113 | + else: |
| 114 | + stim_duration = stim_duration[0] |
| 115 | + |
| 116 | + task_parameters['stimulus_duration_sec'] = stim_duration |
| 117 | + |
66 | 118 | task_parameters['omitted_flash_fraction'] = \ |
67 | 119 | behavior['params'].get('flash_omit_probability', float('nan')) |
68 | 120 | task_parameters['response_window_sec'] = \ |
69 | | - [float(x) for x in behavior["config"]["DoC"]["response_window"]] |
70 | | - task_parameters['reward_volume'] = \ |
71 | | - behavior["config"]["reward"]["reward_volume"] |
72 | | - task_parameters['stage'] = behavior["params"]["stage"] |
| 121 | + [float(x) for x in doc["response_window"]] |
| 122 | + task_parameters['reward_volume'] = config["reward"]["reward_volume"] |
| 123 | + task_parameters['auto_reward_volume'] = doc['auto_reward_volume'] |
| 124 | + task_parameters['session_type'] = behavior["params"]["stage"] |
73 | 125 | task_parameters['stimulus'] = next(iter(behavior["stimuli"])) |
74 | | - task_parameters['stimulus_distribution'] = \ |
75 | | - behavior["config"]["DoC"]["change_time_dist"] |
76 | | - task_parameters['task'] = behavior["config"]["behavior"]["task_id"] |
| 126 | + task_parameters['stimulus_distribution'] = doc["change_time_dist"] |
| 127 | + |
| 128 | + task_id = config['behavior']['task_id'] |
| 129 | + if 'DoC' in task_id: |
| 130 | + task_parameters['task'] = 'change detection' |
| 131 | + else: |
| 132 | + msg = "metadata_processing.get_task_parameters does not " |
| 133 | + msg += f"know how to parse 'task_id' = {task_id}" |
| 134 | + raise RuntimeError(msg) |
| 135 | + |
77 | 136 | n_stimulus_frames = 0 |
78 | 137 | for stim_type, stim_table in behavior["stimuli"].items(): |
79 | 138 | n_stimulus_frames += sum(stim_table.get("draw_log", [])) |
|
0 commit comments