| 81 | self.label = 'pattern' |
| 82 | |
| 83 | def map_values(self, base_dir=None, base_label=None, **kwargs): |
| 84 | if base_dir is None: |
| 85 | base_dir = self.base_dir |
| 86 | |
| 87 | pattern = self.pattern |
| 88 | exclude = self.exclude |
| 89 | |
| 90 | glob_pattern = pattern.replace('{id}','*') |
| 91 | |
| 92 | if base_dir is not None: |
| 93 | if not base_dir.endswith('/'): |
| 94 | base_dir += '/' |
| 95 | glob_pattern = os.path.join(base_dir, glob_pattern) |
| 96 | |
| 97 | x = sorted(glob.glob(glob_pattern, recursive=True)) |
| 98 | |
| 99 | if base_dir is not None: |
| 100 | x = [os.path.relpath(xx, base_dir) for xx in x] |
| 101 | |
| 102 | if exclude: |
| 103 | x = [file for file in x if not fnmatch(file, exclude)] |
| 104 | |
| 105 | if '{id}' in pattern: |
| 106 | ids = [parse(pattern.replace('*','{other}'), file).named['id'] for file in x] |
| 107 | else: |
| 108 | ids = None |
| 109 | |
| 110 | if base_dir is not None: |
| 111 | x = [os.path.join(base_dir, file) for file in x] |
| 112 | |
| 113 | if len(x) == 0: |
| 114 | raise Exception(f'No filepaths found that match {glob_pattern}') |
| 115 | |
| 116 | self.values = x |
| 117 | self.ids = ids |
| 118 | |
| 119 | if self.label is None: |
| 120 | if base_label is not None: |
| 121 | self.label = base_label |
| 122 | else: |
| 123 | self.label = 'pattern' |
| 124 | |
| 125 | def __getitem__(self, idx): |
| 126 | return {self.label: ants.image_read(self.values[idx])} |