Skip to content

Commit

Permalink
[Lang] Add warning message when converting dynamic snode to numpy (#6853
Browse files Browse the repository at this point in the history
)

This PR adds a warning message when the user tries to call the
`to_numpy` method of a dynamic snode.

close: #6841

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
neozhaoliang and pre-commit-ci[bot] authored Dec 14, 2022
1 parent ef15e8f commit 5ed0dc4
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/taichi/lang/field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import taichi.lang
from taichi._lib import core as _ti_core
from taichi._logging import warn
from taichi.lang import impl
from taichi.lang.exception import TaichiSyntaxError
from taichi.lang.util import (in_python_scope, python_scope, to_numpy_type,
Expand Down Expand Up @@ -289,6 +290,10 @@ def fill(self, val):
def to_numpy(self, dtype=None):
"""Converts this field to a `numpy.ndarray`.
"""
if self.parent()._snode.ptr.type == _ti_core.SNodeType.dynamic:
warn(
"You are trying to convert a dynamic snode to a numpy array, be aware that inactive items in the snode will be converted to zeros in the resulting array."
)
if dtype is None:
dtype = to_numpy_type(self.dtype)
import numpy as np # pylint: disable=C0415
Expand Down

0 comments on commit 5ed0dc4

Please sign in to comment.