You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I would like to be able to provide better type hints for my monai code. The DataLoader and Dataset classes in monai inherit from torch but hide the fact that in torch these are generic classes in torch. For example, in torch I can define a dataset like:
This means that elsewhere in the code I can have a better idea what the data will look like. This kind of thing isn't possible if I'm using the monai code.
Describe the solution you'd like
I think you could solve this by doing something like the following (for Dataset - you'd need to do something similar in DataLoader):
importcollections.abcfromtypingimportAny, Mapping, Sequence, TypeVar, Union, overloadimportnumpyasnpimporttorchfromtorch.utils.dataimportDatasetas_TorchDatasetfromtorch.utils.dataimportSubsetas_TorchSubsetNdarrayOrTensor=Union[np.ndarray, torch.Tensor]
T=TypeVar(
"T",
bound=NdarrayOrTensor|Sequence[NdarrayOrTensor] |Mapping[Any, NdarrayOrTensor],
)
classDataset(_TorchDataset[T]):
# Leave the rest of the class as-is
...
@overloaddef__getitem__(self, index: slice) ->_TorchSubset[T]:
...
@overloaddef__getitem__(self, index: Sequence[int]) ->_TorchSubset[T]:
...
@overloaddef__getitem__(self, index: int) ->T:
...
def__getitem__(self, index: int|slice|Sequence[int]) ->T|_TorchSubset[T]:
""" Returns a `Subset` if `index` is a slice or Sequence, a data item otherwise. """ifisinstance(index, slice):
# dataset[:42]start, stop, step=index.indices(len(self))
indices=range(start, stop, step)
return_TorchSubset(dataset=self, indices=indices)
ifisinstance(index, collections.abc.Sequence):
# dataset[[1, 3, 4]]return_TorchSubset(dataset=self, indices=index)
returnself._transform(index)
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
I would like to be able to provide better type hints for my monai code. The
DataLoader
andDataset
classes in monai inherit from torch but hide the fact that in torch these are generic classes in torch. For example, in torch I can define a dataset like:This means that elsewhere in the code I can have a better idea what the data will look like. This kind of thing isn't possible if I'm using the monai code.
Describe the solution you'd like
I think you could solve this by doing something like the following (for Dataset - you'd need to do something similar in DataLoader):
The text was updated successfully, but these errors were encountered: