Delete specified items from data dictionary to release memory. It will remove the key-values and copy the others to construct a new dictionary.
| 669 | |
| 670 | |
| 671 | class DeleteItemsd(MapTransform): |
| 672 | """ |
| 673 | Delete specified items from data dictionary to release memory. |
| 674 | It will remove the key-values and copy the others to construct a new dictionary. |
| 675 | """ |
| 676 | |
| 677 | backend = [TransformBackends.TORCH, TransformBackends.NUMPY] |
| 678 | |
| 679 | def __init__(self, keys: KeysCollection, sep: str = ".", use_re: Sequence[bool] | bool = False) -> None: |
| 680 | """ |
| 681 | Args: |
| 682 | keys: keys of the corresponding items to delete, can be "A{sep}B{sep}C" |
| 683 | to delete key `C` in nested dictionary, `C` can be regular expression. |
| 684 | See also: :py:class:`monai.transforms.compose.MapTransform` |
| 685 | sep: the separator tag to define nested dictionary keys, default to ".". |
| 686 | use_re: whether the specified key is a regular expression, it also can be |
| 687 | a list of bool values, mapping them to `keys`. |
| 688 | """ |
| 689 | super().__init__(keys) |
| 690 | self.sep = sep |
| 691 | self.use_re = ensure_tuple_rep(use_re, len(self.keys)) |
| 692 | |
| 693 | def __call__(self, data): |
| 694 | |
| 695 | def _delete_item(keys, d, use_re: bool = False): |
| 696 | key = keys[0] |
| 697 | if len(keys) > 1: |
| 698 | d[key] = _delete_item(keys[1:], d[key], use_re) |
| 699 | return d |
| 700 | return {k: v for k, v in d.items() if (use_re and not re.search(key, f"{k}")) or (not use_re and k != key)} |
| 701 | |
| 702 | d = dict(data) |
| 703 | for key, use_re in zip(cast(Sequence[str], self.keys), self.use_re): |
| 704 | d = _delete_item(key.split(self.sep), d, use_re) |
| 705 | |
| 706 | return d |
| 707 | |
| 708 | |
| 709 | class SelectItemsd(MapTransform): |
no outgoing calls
searching dependent graphs…