Source code for hed.tools.analysis.file_dictionary
""" A file dictionary keyed by entity indices. """importosfromhed.errors.exceptionsimportHedFileError
[docs]classFileDictionary:""" A file dictionary keyed by entity pair indices. Notes: - The entities are identified as 0, 1, ... depending on order in the base filename. - The entity key-value pairs are assumed separated by '_' unless a separator is provided. """
[docs]def__init__(self,collection_name,file_list,key_indices=(0,2),separator='_'):""" Create a dictionary with full paths as values. Parameters: collection_name (str): Name of the file collection for reference. file_list (list, None): List containing full paths of files of interest. key_indices (tuple, None): List of order of key-value pieces to assemble for the key. separator (str): Character used to separate pieces of key name. Notes: - This dictionary is used for cross listing BIDS style files for different studies. - Examples: If key_indices is (0, 2), the key generated for /tmp/sub-001_task-FaceCheck_run-01_events.tsv is sub_001_run-01. """self.collection_name=collection_nameself._file_dict={}self.create_file_dict(file_list,key_indices,separator)
@propertydefname(self):""" Name of this dictionary. """returnself.collection_name@propertydefkey_list(self):""" Keys in this dictionary. """returnlist(self._file_dict.keys())@propertydeffile_dict(self):""" Dictionary of path values in this dictionary. """returnself._file_dict@propertydeffile_list(self):""" List of path values in this dictionary. """returnlist(self._file_dict.values())
[docs]defcreate_file_dict(self,file_list,key_indices,separator):""" Create new dict based on key indices. Parameters: file_list (list): Paths of the files to include. key_indices (tuple): A tuple of integers representing order of entities for key. separator (str): The separator used between entities to form the key. """ifkey_indices:self._file_dict=self.make_file_dict(file_list,key_indices=key_indices,separator=separator)
[docs]defget_file_path(self,key):""" Return file path corresponding to key. Parameters: key (str): Key used to retrieve the file path. Returns: str: File path. """returnself._file_dict.get(key,None)
[docs]defiter_files(self):""" Iterator over the files in this dictionary. Yields: - str: Key into the dictionary. - file: File path. """forkey,fileinself._file_dict.items():yieldkey,file
[docs]defkey_diffs(self,other_dict):""" Return symmetric key difference with another dict. Parameters: other_dict (FileDictionary) A file dictionary object. Returns: list: The symmetric difference of the keys in this dictionary and the other one. """diffs=set(self._file_dict.keys()).symmetric_difference(set(other_dict.file_dict.keys()))returnlist(diffs)
[docs]defoutput_files(self,title=None,logger=None):""" Return a string with the output of the list. Parameters: title (None, str): Optional title. logger (HedLogger): Optional HED logger for recording. Returns: str: The dictionary in string form. Notes: - The logger is updated if available. """output_list=[]iftitle:output_list.append(f"{title} ({len(self.key_list)} files)")forkey,valueinself._file_dict.items():basename=os.path.basename(self.get_file_path(key))output_list.append(f"{key}: {basename}")iflogger:logger.add(key,f"{self.name}: {basename}")return"\n".join(output_list)
[docs]@staticmethoddefmake_file_dict(file_list,key_indices=(0,2),separator='_'):""" Return a dictionary of files using entity keys. Parameters: file_list (list): Paths to files to use. key_indices (tuple): Positions of entities to use for key. separator (str): Separator character used to construct key. Returns: dict: Key is based on key indices and value is a full path. """file_dict={}forthe_fileinfile_list:the_file=os.path.realpath(the_file)base=os.path.basename(the_file)key=FileDictionary.make_key(base,indices=key_indices,separator=separator)ifkeyinfile_dict:raiseHedFileError("NonUniqueFileKeys",f"dictionary key {key} is associated with {the_file} and {file_dict[key]}","")file_dict[key]=the_filereturnfile_dict
[docs]@staticmethoddefmake_key(key_string,indices=(0,2),separator='_'):""" Create a key from specified entities. Parameters: key_string (str): The string from which to extract the key (usually a filename or path). indices (tuple): Positions of entity pairs to use as key. separator (str): Separator between entity pairs in the created key. Returns: str: The created key. """key_value=''pieces=key_string.split(separator)forindexinlist(indices):key_value+=pieces[index]+separatorreturnkey_value[:-1]