@@ -232,15 +232,21 @@ def _get_trials_tasks(session_path, acquisition_description=None, sync_tasks=Non
232232 sync_kwargs = {'sync' : sync , ** sync_args }
233233
234234 # Behavior tasks
235+ protocol_numbers = set ()
235236 task_protocols = acquisition_description .get ('tasks' , [])
236237 for i , (protocol , task_info ) in enumerate (chain (* map (dict .items , task_protocols ))):
237238 collection = task_info .get ('collection' , f'raw_task_data_{ i :02} ' )
238239 task_kwargs = {'protocol' : protocol , 'collection' : collection }
239- # For now the order of protocols in the list will take precedence. If collections are numbered,
240- # check that the numbers match the order. This may change in the future.
240+ # The order of protocols in the list will take precedence unless protocol_number is present.
241+ # If collections are numbered, check that the numbers match the order.
242+ n = i
241243 if re .match (r'^raw_task_data_\d{2}$' , collection ):
242- task_kwargs ['protocol_number' ] = i
243- if int (collection .split ('_' )[- 1 ]) != i :
244+ # Protocol number may be overridden by the protocol_number key
245+ if task_info .get ('protocol_number' ) is not None :
246+ n = task_info ['protocol_number' ]
247+ task_kwargs ['protocol_number' ] = n
248+ assert not (n in protocol_numbers or protocol_numbers .add (n )), 'protocol numbers must be unique'
249+ if int (collection .split ('_' )[- 1 ]) != n :
244250 _logger .warning ('Number in collection name does not match task order' )
245251 if extractors := task_info .get ('extractors' , False ):
246252 extractors = (extractors ,) if isinstance (extractors , str ) else extractors
0 commit comments