def test_bidsimagefile_get_image(): path = "synthetic/sub-01/ses-01/func/sub-01_ses-01_task-nback_run-01_bold.nii.gz" path = path.split('/') path = os.path.join(get_test_data_path(), *path) bf = BIDSImageFile(path, None) assert bf.get_image() is not None assert bf.get_image().shape == (64, 64, 64, 64)
def get_matching_images( image_to_match: BIDSImageFile, bids_dataset: BIDSLayout, matching_entities: list = None, required_entities: dict = None, ): """ Returns a list of images from the BIDS dataset that has the specified required_entities and has the same value for entities listed in matching_entities as the image_to_match. Example: for an image "sub-123_ses-1_T1w.nii" with matching_entities ['ses'] and required_entities {'suffix': 'FLAIR'}, the image "sub-123_ses-1_FLAIR.nii" would match, but "sub-123_ses-2_FLAIR.nii" would not. Parameters ---------- required_entities: dict Entity-value dictionary that are required. matching_entities: list List of entities that must match, if present, between the previous image and the one to fetch. image_to_match: BIDSImageFile Image to use as reference for matching_entities. bids_dataset: BIDSLayout BIDS dataset from which to fetch the new image. Returns ------- list [BIDSImageFile] BIDS image file matching the input specifications. Empty if there are no matches. """ if matching_entities is None: matching_entities = [] if required_entities is None: required_entities = {} ents_to_match = {} im_entities = image_to_match.get_entities() for k in matching_entities: if k in im_entities.keys(): ents_to_match[k] = im_entities[k] potential_matches = bids_dataset.get(**required_entities, **ents_to_match) # Go through each potential image; remove those that don't match potential_idx = [] for idx, potential_im in enumerate(potential_matches): potential_im_ents = potential_im.get_entities() for entity, value in ents_to_match.items(): if (entity not in potential_im_ents.keys() or value != potential_im_ents[entity]): continue else: if potential_im != image_to_match: potential_idx.append(idx) return [potential_matches[i] for i in potential_idx]