コンテンツにスキップ

sensorutils.core


to_frames(src, window_size, stride, stride_mode='index')

np.ndarrayをフレーム分けするプリミティブな実装で,stride_modeで分割アルゴリズムを指定することが可能.

to_frames関数はto_frames_using_index関数,to_frames_using_nptricks関数,to_frames_using_reshape関数を適応的に使い分ける.

使い分けは以下の通り.

  • window_size == stride -> to_frames_using_reshape
  • window_size != stride and stride_mode == 'index' -> to_frames_using_index
  • window_size != stride and stride_mode == 'nptrick' -> to_frames_using_nptricks

Parameters:

Name Type Description Default
src ndarray

splited source.

required
window_size int

sliding window size.

required
stride int

stride is int more than 0.

required
stride_mode str

'index' or 'nptrick'.

it is used to_frames_* method when window_size != stride.

'index'

Returns:

Type Description
ndarray

a shape of frames is (num_frames, window_size, *src.shape[1:]), where num_frames is (src.shape[0] - window_size) // stride + 1.


to_frames_using_reshape(src, window_size)

np.ndarrayをフレーム分けするプリミティブな実装で,ウィンドウサイズとストライド幅が同じ場合に利用することが可能.

分割にnp.reshapeを使用しており,非常に高速なsliding-window処理を実行可能.

Parameters:

Name Type Description Default
src ndarray

splited source.

required
window_size int

sliding window size. stride = window_size.

required

Returns:

Type Description
ndarray

a shape of frames is (num_frames, window_size, *src.shape[1:]), where num_frames is (src.shape[0] - window_size) // window_size + 1.


to_frames_using_index(src, window_size, stride)

np.ndarray をフレーム分けするプリミティブな実装で,分割にindexingを使用している.

Parameters:

Name Type Description Default
src ndarray

splited source.

required
window_size int

sliding window size.

required
stride int

stride is int more than 0.

required

Returns:

Type Description
ndarray

a shape of frames is (num_frames, window_size, *src.shape[1:]), where num_frames is (src.shape[0] - window_size) // stride + 1.


to_frames_using_nptricks(src, window_size, stride)

np.ndarray をフレーム分けするプリミティブな実装で,分割にnp.lib.stride_tricks.as_strided関数を使用しており,indexingを使用するto_frames_using_indexより高速である.

Parameters:

Name Type Description Default
src ndarray

splited source.

required
window_size int

sliding window size.

required
stride int

stride is int more than 0.

required

Returns:

Type Description
ndarray

a shape of frames is (num_frames, window_size, *src.shape[1:]), where num_frames is (src.shape[0] - window_size) // stride + 1.


split_using_sliding_window(segment, **options)

可変サイズのsegmentからsliding-window方式で一定サイズのフレームを抽出する.

各shapeは以下のようになる.

  • segment: (segment_size, ch)
  • frames: (num_frames, window_size, ch)

segmentの第2軸(axis=1)以降のshapeは任意であり, 例えばshapeが(segment_size, ch1, ch2)のデータをsegmentとして入力すると, (num_frames, window_size, ch1, ch2)のデータを取得することができる.

Parameters:

Name Type Description Default
segments np.ndarray

分割対象のデータ

required
window_size int, default=512

フレーム分けするサンプルサイズ

required
stride int, default=None

strideがNoneはwindow_sizeが指定される.

required
ftrim int

最初のftrimサンプルをとばす(default=5).

required
btrim int

最後のbtrimサンプルをとばす(default=5).

required
return_error_value None

エラーの時の返り値

required

Returns:

Type Description
ndarray

sliding-window方式で抽出下フレーム

失敗したらreturn_error_valueで指定された値を返す.


split_using_target(src, target)

targetのデータを元にsrcの分割を行う.

Parameters:

Name Type Description Default
src ndarray

分割するデータ

required
target ndarray

ラベルデータ(一次元配列)

required

Examples:

>>> tgt = np.array([0, 0, 1, 1, 2, 2, 1])
>>> src = np.array([1, 2, 3, 4, 5, 6, 7])
>>> splited = split_from_target(src, tgt)
>>>
>>> # splited == {
>>> #    0: [np.array([1, 2])],
>>> #    1: [np.array([3, 4]), np.array([7])],
>>> #    2: [np.array([5, 6])]
>>> # }

Returns:

Type Description
Dict[int, List[numpy.ndarray]]

keyはラベル,valueはデータのリスト.


interpolate(src, rate, kind='linear', axis=-1)

interpolation function.

Use scipy.interpolate.interp1d in this function.

Parameters:

Name Type Description Default
src ndarray

interpolation source.

required
rate int

rate.

required
kind str

Specifies the kind of interpolation as a string (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, ‘next’, where ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order; ‘previous’ and ‘next’ simply return the previous or next value of the point) or as an integer specifying the order of the spline interpolator to use. Default is ‘linear’.

'linear'
axis int

Specifies the axis of y along which to interpolate. Interpolation defaults to the last axis of y.

-1

Examples:

Linear interpolation

1
2
[0, 2, 4] - x2 -> [0, 1, 2, 3, 4]
[0, 3, 6] - x3 -> [0, 1, 2, 3, 4, 5, 6]

Returns:

Type Description
ndarray

shape[axis] - 1 == rate * ft.shape[axis]


pickle_dump(obj, path)

object dump using pickle.

Parameters:

Name Type Description Default
obj Any

any object.

required
path Union[str, pathlib.Path]

save path.

required

pickle_load(path)

object load using pickle.

Parameters:

Name Type Description Default
path Path

a saved object pickle path.

required