pyvista.core._validation.validate.validate_array

pyvista.core._validation.validate.validate_array#

validate_array(
arr: float | ArrayLike[float],
/,
*,
must_have_shape: _ShapeLike | list[_ShapeLike] | None = None,
must_have_ndim: int | VectorLike[int] | None = None,
must_have_dtype: npt.DTypeLike | None = None,
must_have_length: int | VectorLike[int] | None = None,
must_have_min_length: int | None = None,
must_have_max_length: int | None = None,
must_be_nonnegative: bool = False,
must_be_finite: bool = False,
must_be_real: bool = True,
must_be_integer: bool = False,
must_be_sorted: bool | dict[str, Any] = False,
must_be_in_range: VectorLike[float] | None = None,
strict_lower_bound: bool = False,
strict_upper_bound: bool = False,
reshape_to: int | tuple[int, ...] | None = None,
broadcast_to: int | tuple[int, ...] | None = None,
dtype_out: npt.DTypeLike = None,
as_any: bool = True,
copy: bool = False,
to_list: bool = False,
to_tuple: bool = False,
name: str = 'Array',
)[ソース]#

数値配列が特定の要件を満たしているかチェックし,検証します.

配列が数値であること,特定の形状,データ型を持っていること,ソートされていること,整数のようであること,有限であることなど,特定の要件を満たす値を持っていることを確認するために,配列を検証します.

配列の出力は,形を変えたり,ブロードキャストしたり,ネストされたタプルやリスト配列としてキャストしたり,特定のデータ型にキャストすることもできます.

パラメータ:
arrarray_like

np.ndarray に変換できる任意の形式で,検証される配列.これにはリスト,タプルのリスト,タプル,タプルのタプル,リストのタプル,ndarrayが含まれます.

must_have_shapeint | tuple[int, ...] | list[int, tuple[int, ...]], optional

Check if the array has a specific shape. Specify a single shape or a list of any allowable shapes. If an integer, the array must be 1-dimensional with that length. Use a value of -1 for any dimension where its size is allowed to vary. Use () to allow scalar values (i.e. 0-dimensional). Set to None if the array can have any shape (default).

must_have_ndimint | VectorLike[int], optional

Check if the array has the specified number of dimension(s). Specify a single dimension or a sequence of allowable dimensions. If a sequence, the array must have at least one of the specified number of dimensions.

must_have_dtypeDTypeLike | list[DTypeLike, ...], optional

Check if the array's data-type has the given dtype. Specify a np.dtype object or dtype-like base class which the array's data must be a subtype of. If a list, the array's data must be a subtype of at least one of the specified dtypes.

must_have_lengthint | VectorLike[int], optional

Check if the array has the given length. If multiple values are given, the array's length must match one of the values.

注釈

配列の長さは,( reshapeNone でない場合)配列の形状を変更した後,( broadcast_toNone でない場合)ブロードキャスト後に決定されます.したがって, length の値は配列の新しい形状を考慮する必要があります.

must_have_min_lengthint, optional

Check if the array's length is this value or greater.

must_have_max_lengthint, optional

Check if the array' length is this value or less.

must_be_nonnegativebool, default: False

Check if all elements of the array are nonnegative.

must_be_finitebool, default: False

Check if all elements of the array are finite, i.e. not infinity and not Not a Number (NaN).

must_be_realbool, default: True

Check if the array has real numbers, i.e. its data type is integer or floating.

must_be_integerbool, default: False

Check if the array's values are integer-like (i.e. that np.all(arr, np.floor(arr))).

must_be_sortedbool | dict, default: False

Check if the array's values are sorted. If True, the check is performed with default parameters:

  • ascending=True: 配列は昇順にソートされなければなりません.

  • strict=False: 同じ値を持つ連続した要素が許可されます.

  • axis=-1: ソートは配列の最後の軸に沿ってチェックされます.

To check for descending order, enforce strict ordering, or to check along a different axis, use a dict with keyword arguments that will be passed to check_sorted.

must_be_in_rangeVectorLike[float], optional

Check if the array's values are all within a specific range. Range must be array-like with two elements specifying the minimum and maximum data values allowed, respectively. By default, the range endpoints are inclusive, i.e. values must be >= minimum and <= maximum. Use strict_lower_bound and/or strict_upper_bound to further restrict the allowable range.

..note

Use ``np.inf`` to check for open intervals, e.g.:

* ``[-np.inf, upper_bound]`` to check if values are less
  than (or equal to)  ``upper_bound``
* ``[lower_bound, np.inf]`` to check if values are greater
  than (or equal to) ``lower_bound``
strict_lower_boundbool, default: False

must_be_in_range で指定された範囲の下限を厳密にします.つまり,配列の値は指定された最小値よりも厳密に大きくなければなりません.

strict_upper_boundbool, default: False

must_be_in_range で指定された範囲の上限を厳密にします.つまり,配列の値は指定された最大値よりも厳密に小さくなければなりません.

reshape_toint | tuple[int, ...], optional

np.reshape() を使用して,出力配列を新しい形状に整形します.この形状は元の形状と互換性がなければなりません.もし整数であれば,結果はその長さの1次元配列になります.形状の次元は-1でも構いません.

broadcast_toint | tuple[int, ...], optional

np.broadcast_to() で配列を指定したシェイプの読み取り専用ビューにブロードキャストします.ブロードキャストは整形後に行われます( reshape_toNone でない場合).

dtype_outDTypeLike, optional

返される配列のデータ型を設定します.デフォルトでは,データ型は入力データから推測されます.以下のようになります.

as_anybool, default: True

np.ndarray のサブクラスがコピーを作成せずに通過できるようにします.

copybool, default: False

もし True ならば,配列のコピーを返します.配列が以下の場合は常にコピーが返されます:

  • ネストされたシーケンス である

  • np.ndarray のサブクラスであり, as_anyFalse

また, dtype_out 要件を満たすためにコピーを作成することもできます.

to_listbool, default: False

検証済みの配列を list またはネストされた list として返します.スカラ値は常に Number ( int または float ) として返されます. to_tuple=True の場合は無効です.

to_tuplebool, default: False

検証済みの配列を tuple またはネストされた tuple として返します.スカラ値は常に Number ( int または float ) として返されます.

namestr, default: "Array"

エラーメッセージのバリデーションチェックに失敗した場合に使用する変数名.

戻り値:
array_like

検証済みの配列.返されるオブジェクトは次のとおりです:

  • np.ndarray のインスタンス(デフォルト),または

  • 入れ子になった list ( to_list=True の場合),または

  • 入れ子になった tuple ( to_tuple=True の場合),または

  • 入力がスカラーの場合は Number (つまり int または float ) .

参考

validate_number

単一の数字に特化した関数 .

validate_array3

3要素配列に特化した関数 .

validate_arrayN

一次元配列に特化した関数 .

validate_arrayNx3

Nx3次元配列に特化した関数 .

validate_data_range

データ範囲に特化した関数.

1次元配列が少なくとも2つの長さを持ち,単調増加(つまり厳密な昇順)であり,ある範囲内にあることを検証します.

>>> from pyvista import _validation
>>> array_in = (1, 2, 3, 5, 8, 13)
>>> rng = (0, 20)
>>> _validation.validate_array(
...     array_in,
...     must_have_shape=(-1),
...     must_have_min_length=2,
...     must_be_sorted=dict(strict=True),
...     must_be_in_range=rng,
... )
array([ 1,  2,  3,  5,  8, 13])