这是indexloc提供的服务,不要输入任何密码
Skip to content

batch - is_empty() #1125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions tianshou/data/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def alloc_by_keys_diff(
if key in meta.get_keys():
if isinstance(meta[key], Batch) and isinstance(batch[key], Batch):
alloc_by_keys_diff(meta[key], batch[key], size, stack)
elif isinstance(meta[key], Batch) and meta[key].is_empty():
elif isinstance(meta[key], Batch) and len(meta[key].get_keys()) == 0:
meta[key] = create_value(batch[key], size, stack)
else:
meta[key] = create_value(batch[key], size, stack)
Expand Down Expand Up @@ -393,7 +393,7 @@ def update(self, batch: dict | Self | None = None, **kwargs: Any) -> None:
def __len__(self) -> int:
...

def is_empty(self, recurse: bool = False) -> bool:
# def is_empty(self, recurse: bool = False) -> bool:
...

def split(
Expand Down Expand Up @@ -514,7 +514,7 @@ def __getitem__(self, index: str | IndexType) -> Any:
if len(batch_items) > 0:
new_batch = Batch()
for batch_key, obj in batch_items:
if isinstance(obj, Batch) and obj.is_empty():
if isinstance(obj, Batch) and len(obj.get_keys()) == 0:
new_batch.__dict__[batch_key] = Batch()
else:
new_batch.__dict__[batch_key] = obj[index]
Expand Down Expand Up @@ -574,13 +574,13 @@ def __iadd__(self, other: Self | Number | np.number) -> Self:
other.__dict__.values(),
strict=True,
): # TODO are keys consistent?
if isinstance(obj, Batch) and obj.is_empty():
if isinstance(obj, Batch) and len(obj.get_keys()) == 0:
continue
self.__dict__[batch_key] += value
return self
if _is_number(other):
for batch_key, obj in self.items():
if isinstance(obj, Batch) and obj.is_empty():
if isinstance(obj, Batch) and len(obj.get_keys()) == 0:
continue
self.__dict__[batch_key] += other
return self
Expand All @@ -594,7 +594,7 @@ def __imul__(self, value: Number | np.number) -> Self:
"""Algebraic multiplication with a scalar value in-place."""
assert _is_number(value), "Only multiplication by a number is supported."
for batch_key, obj in self.__dict__.items():
if isinstance(obj, Batch) and obj.is_empty():
if isinstance(obj, Batch) and len(obj.get_keys()) == 0:
continue
self.__dict__[batch_key] *= value
return self
Expand All @@ -607,7 +607,7 @@ def __itruediv__(self, value: Number | np.number) -> Self:
"""Algebraic division with a scalar value in-place."""
assert _is_number(value), "Only division by a number is supported."
for batch_key, obj in self.__dict__.items():
if isinstance(obj, Batch) and obj.is_empty():
if isinstance(obj, Batch) and len(obj.get_keys()) == 0:
continue
self.__dict__[batch_key] /= value
return self
Expand Down Expand Up @@ -722,7 +722,7 @@ def __cat(self, batches: Sequence[dict | Self], lens: list[int]) -> None:
{
batch_key
for batch_key, obj in batch.items()
if not (isinstance(obj, Batch) and obj.is_empty())
if not (isinstance(obj, Batch) and len(obj.get_keys()) == 0)
}
for batch in batches
]
Expand Down Expand Up @@ -753,7 +753,7 @@ def __cat(self, batches: Sequence[dict | Self], lens: list[int]) -> None:
if key not in batch.__dict__:
continue
value = batch.get(key)
if isinstance(value, Batch) and value.is_empty():
if isinstance(value, Batch) and len(value.get_keys()) == 0:
continue
try:
self.__dict__[key][sum_lens[i] : sum_lens[i + 1]] = value
Expand All @@ -772,7 +772,7 @@ def cat_(self, batches: BatchProtocol | Sequence[dict | BatchProtocol]) -> None:
batch_list.append(Batch(batch))
elif isinstance(batch, Batch):
# x.is_empty() means that x is Batch() and should be ignored
if not batch.is_empty():
if not len(batch.get_keys()) == 0:
batch_list.append(batch)
else:
raise ValueError(f"Cannot concatenate {type(batch)} in Batch.cat_")
Expand All @@ -783,16 +783,16 @@ def cat_(self, batches: BatchProtocol | Sequence[dict | BatchProtocol]) -> None:
# x.is_empty(recurse=True) here means x is a nested empty batch
# like Batch(a=Batch), and we have to treat it as length zero and
# keep it.
lens = [0 if batch.is_empty(recurse=True) else len(batch) for batch in batches]
lens = [0 if len(batch)==0 else len(batch) for batch in batches]
except TypeError as exception:
raise ValueError(
"Batch.cat_ meets an exception. Maybe because there is any "
f"scalar in {batches} but Batch.cat_ does not support the "
"concatenation of scalar.",
) from exception
if not self.is_empty():
if not len(self.get_keys()) == 0:
batches = [self, *list(batches)]
lens = [0 if self.is_empty(recurse=True) else len(self), *lens]
lens = [0 if len(self)==0 else len(self), *lens]
self.__cat(batches, lens)

@staticmethod
Expand All @@ -810,21 +810,21 @@ def stack_(self, batches: Sequence[dict | BatchProtocol], axis: int = 0) -> None
batch_list.append(Batch(batch))
elif isinstance(batch, Batch):
# x.is_empty() means that x is Batch() and should be ignored
if not batch.is_empty():
if not len(batch.get_keys()) == 0:
batch_list.append(batch)
else:
raise ValueError(f"Cannot concatenate {type(batch)} in Batch.stack_")
if len(batch_list) == 0:
return
batches = batch_list
if not self.is_empty():
if not len(self.get_keys()) == 0:
batches = [self, *batches]
# collect non-empty keys
keys_map = [
{
batch_key
for batch_key, obj in batch.items()
if not (isinstance(obj, BatchProtocol) and obj.is_empty())
if not (isinstance(obj, BatchProtocol) and len(obj.get_keys()) == 0)
}
for batch in batches
]
Expand Down Expand Up @@ -870,7 +870,7 @@ def stack_(self, batches: Sequence[dict | BatchProtocol], axis: int = 0) -> None
# TODO: fix code/annotations s.t. the ignores can be removed
if (
isinstance(value, BatchProtocol) # type: ignore
and value.is_empty() # type: ignore
and len(value.get_keys()) == 0 # type: ignore
):
continue # type: ignore
try:
Expand Down Expand Up @@ -930,7 +930,7 @@ def __len__(self) -> int:
# TODO: causes inconsistent behavior to batch with empty batches
# and batch with empty sequences of other type. Remove, but only after
# Buffer and Collectors have been improved to no longer rely on this
if isinstance(obj, Batch) and obj.is_empty(recurse=True):
if isinstance(obj, Batch) and len(obj) == 0:
continue
if hasattr(obj, "__len__") and (isinstance(obj, Batch) or obj.ndim > 0):
lens.append(len(obj))
Expand All @@ -940,7 +940,7 @@ def __len__(self) -> int:
return 0
return min(lens)

def is_empty(self, recurse: bool = False) -> bool:
#def is_empty(self, recurse: bool = False) -> bool:
"""Test if a Batch is empty.

If ``recurse=True``, it further tests the values of the object; else
Expand Down Expand Up @@ -971,14 +971,14 @@ def is_empty(self, recurse: bool = False) -> bool:
if not recurse:
return False
return all(
False if not isinstance(obj, Batch) else obj.is_empty(recurse=True)
False if not isinstance(obj, Batch) else len(obj) == 0
for obj in self.values()
)

@property
def shape(self) -> list[int]:
"""Return self.shape."""
if self.is_empty():
if len(self.get_keys()) == 0:
return []
data_shape = []
for obj in self.__dict__.values():
Expand Down