def get_ids_from_path(self, path=None, patient_id_getter=None, case_id_getter=None, slide_id_getter=None, classification_labels_getter=None): presetter(variables={'path': path}, cls=self) precheck(types=['path'], path={'path': self.path}, cls=self) if not isinstance(path, list): path = [path] if not patient_id_getter: patient_id = self.get_patient_id(self.path) if patient_id_getter: patient_id = patient_id_getter(self.path) if not case_id_getter: case_id = [None] * len(path) if case_id_getter: case_id = case_id_getter(self.path) if not slide_id_getter: slide_id = [None] * len(path) if slide_id_getter: slide_id = slide_id_getter(self.path) if not classification_labels_getter: classification_labels = [None] * len(path) if classification_labels_getter: classification_labels = classification_labels_getter(self.path) return self.path, patient_id, case_id, slide_id, classification_labels
def create_named_object_from_path( self, path: List[Union[pathlib.Path, str]] = None, patient_id_getter: Callable = None, case_id_getter: Callable = None, slide_id_getter: Callable = None, classification_labels_getter: Callable = None): """ Arguments: path: List of paths patient_id_getter: Callable that takes a path as input and returns the corresponding patient id case_id_getter: Callable that takes a path as input and returns the corresponding case id slide_id_getter: Callable that takes a path as input and returns the corresponding slide id classification_labels_getter: Callable that takes a path as input and returns the corresponding labels Returns: List of wsi_processing_pipeline.preprocessing.objects.NamedObject """ presetter(variables={'path': path}, cls=self) precheck(types=['path'], path={'path': self.path}, cls=self) paths, patient_id, case_id, slide_id, classification_labels = self.get_ids_from_path( path=self.path, patient_id_getter=patient_id_getter, case_id_getter=case_id_getter, slide_id_getter=slide_id_getter, classification_labels_getter=classification_labels_getter) named_objects = self.create_named_object(paths, patient_id, case_id, slide_id, classification_labels) return named_objects
def get_path_parts_and_indices(self, path=None): presetter(variables={'path': path}, cls=self) precheck(types=['path'], path={'path': self.path}, classes=self) if isinstance(self.path, list): path_parts = [] res = [] for parts in self.path: parts = list(parts.parts) path_parts.append(parts) res.append({i: e for i, e in enumerate(parts)}) else: path_parts = list(self.path.parts) res = ({i: e for i, e in enumerate(path_parts)}) return res
def num2lbs(self, classification_labels=None, keys=None): presetter(variables={ 'classification_labels': classification_labels, 'keys': keys, }, cls=self) precheck(types=['str-or-int'], str_or_int={ 'classification_labels': self.classification_labels, 'keys': self.keys }, cls=self) if self.keys == [None]: num2lbs = { i: self.classification_labels[i] for i in range(0, len(self.classification_labels)) } elif self.keys != []: zipbObj = zip(self.keys, self.classification_labels) # Create a dictionary from zip object num2lbs = dict(zipbObj) return num2lbs
def label_func(self, names=None, classification_labels=None, keys=None, return_list=False, return_name=False, match=False, multicategory=False): presetter(variables={ 'classification_labels': classification_labels, 'keys': keys, 'names': names, }, cls=self) precheck(types=['str-or-int', 'bool'], str_or_int={ 'classification_labels': self.classification_labels, 'keys': self.keys, }, bools={ 'return_list': return_list, 'return_name': return_name, 'match': match, 'multicategory': multicategory }, cls=self) if isinstance(self.keys, NoneType): self.keys = [self.keys] if multicategory: raise NotImplementedError else: if not any(isinstance(el, list) for el in names): if return_list: if return_name: res = [[name] if name in self.classification_labels else [NoneType] for name in names] else: res = [[self.lbs2num()[name]] if name in self.classification_labels else [NoneType] for name in names] else: if return_name: res = [ name if name in self.classification_labels else NoneType for name in names ] else: res = [ self.lbs2num()[name] if name in self.classification_labels else NoneType for name in names ] elif any(isinstance(el, list) for el in names): raise NotImplementedError else: raise AssertionError return res
def get_names_from_paths(self, paths=None, path_parts=None, n_split_returns=None, splitter=None, joiner=None, unique=False, regex=None, rm_correctors=None, keep_split=False, match=False): ''' ''' presetter(variables={ 'path': paths, 'path_parts': path_parts, 'n_split_returns': n_split_returns, 'splitter': splitter, 'joiner': joiner, 'rm_correctors': rm_correctors }, cls=self) precheck(types=['path', 'string', 'integer', 'bool'], path={'path': self.path}, string={ 'splitter': self.splitter, 'joiner': self.joiner, 'rm_correctors': self.rm_correctors }, integer={ 'path_parts': self.path_parts, 'n_split_returns': self.n_split_returns }, bools={ 'unique': unique, 'keep_split': keep_split, 'match': match }, cls=self) paths = [ Path(p) if not isinstance(p, pathlib.PosixPath) else p for p in self.path ] if unique: result = list( set([ self.get_name_from_path( path=ps, path_parts=self.path_parts, splitter=self.splitter, joiner=self.joiner, n_split_returns=self.n_split_returns, unique=unique, regex=regex, rm_correctors=self.rm_correctors, keep_split=keep_split, match=match) for ps in paths ])) else: result = [ self.get_name_from_path(path=ps, path_parts=self.path_parts, splitter=self.splitter, joiner=self.joiner, n_split_returns=self.n_split_returns, unique=unique, regex=regex, rm_correctors=rm_correctors, keep_split=keep_split, match=match) for ps in paths ] return result
def get_name_from_path(self, path=None, path_parts=None, n_split_returns=None, splitter=None, joiner=None, unique=False, rm_correctors=None, keep_split=False, regex=None, match=False): '''Function that takes "path": pathlib or string object or a list "path_parts: integer or a list of integers defining parts of split path" "n_split_returns: Number of splitted parts to return of the modified path defined as integer or list of integers "splitter" and "joiner" strings for splitting and joining the path_parts "unique": boolean for getting modified processed paths as set or pure list: may contain duplicate entries "rm_correctors" string or list of strings to strip of off the modified processed path adjusted with the parameter "remove:"boolean whether to just strip the rm_corrector off or to leave out the complete path_part containing the rm_corrector "regex": is a regular expressions pattern used with fastai's class RegexLabeller--> this overwrites the other methods!! additionally ajustable with "match": boolean whether re.match or re.search is used within the RegexLabeller returns: a string object modified from initial path_parts ''' presetter(variables={ 'path': path, 'path_parts': path_parts, 'n_split_returns': n_split_returns, 'splitter': splitter, 'joiner': joiner, 'rm_correctors': rm_correctors }, cls=self) precheck(types=['path', 'string', 'integer', 'bool'], path={'path': self.path}, string={ 'splitter': self.splitter, 'joiner': self.joiner, 'rm_correctors': self.rm_correctors }, integer={ 'path_parts': self.path_parts, 'n_split_returns': self.n_split_returns }, bools={ 'unique': unique, 'keep_split': keep_split, 'match': match }, cls=self) if isinstance(self.joiner, list): if len(self.splitter) == 1: self.joiner = self.joiner[0] else: raise NotImplementedError if isinstance(self.splitter, list): if len(self.splitter) == 1: self.splitter = self.splitter[0] else: raise NotImplementedError if isinstance(path, list) or isinstance(path, fastcore.foundation.L): return self.get_names_from_paths( paths=self.path, path_parts=self.path_parts, splitter=self.splitter, joiner=self.joiner, n_split_returns=self.n_split_returns, unique=unique, rm_correctors=self.rm_correctors, regex=regex, keep_split=keep_split) else: if regex: f = RegexLabeller(regex, match=match) return f(str(self.path)) elif self.func_path_to_patient_id is not None: return self.func_path_to_patient_id(path) elif self.func_path_to_case_id is not None: return self.func_path_to_case_id(path) elif self.func_path_to_slide_id is not None: return self.func_path_to_slide_id(path) else: try: return_path = self.joiner.join([ self.path.parts[i] for i in self.path_parts ]).split(self.splitter) if not isinstance(self.rm_correctors, NoneType): if isinstance(self.rm_correctors, list): #if not any(isinstance(el, NoneType) for el in self.rm_correctors): parts_to_check = [ return_path[i] for i in self.n_split_returns ] return_path, n_split_returns = self.rm_corrector( parts_to_check, keep_split=keep_split) return_path = str( Path( self.joiner.join([ return_path[i] for i in n_split_returns ])).with_suffix('')) else: return_path = str( Path( self.joiner.join([ return_path[i] for i in self.n_split_returns ])).with_suffix('')) except: raise IndexError( 'You may want to use the "get_path_parts_and_indices" function \n' 'to precheck your path and to get the parameters "path_parts" splitter", \n' '"joiner" and "n_split_returns" right!') return return_path