Coverage for graphica/plugins/example_plugin/__init__.py: 41.2%

15 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-12 16:38 +0000

1""" 

2Graphica のプラグインのサンプル。 

3 

4- 曲線フィットの選択肢に「二重指数減衰」を追加する 

5- 「プラグイン」メニューに、選択中のデータセットの点数を表示する項目を追加する 

6 

7プラグインに要るのは、このフォルダの __init__.py(register(api) を定義する)と plugin.json だけ。 

8本体とのやりとりは、callback に渡る窓口(ctx: PluginContext)だけを使う。 

9""" 

10import numpy as np 

11 

12 

13def _double_exp_func(x, a, b, c, d): 

14 """y = a*exp(-b*x) + c*exp(-d*x)""" 

15 return a * np.exp(-b * x) + c * np.exp(-d * x) 

16 

17 

18def _double_exp_p0(x_data, y_data): 

19 amplitude = float(np.nanmax(np.abs(y_data))) or 1.0 

20 return [amplitude, 1.0, amplitude, 0.1] 

21 

22 

23def _show_dataset_point_count(ctx): 

24 dataset = ctx.current_dataset() 

25 if dataset is None: 

26 ctx.show_message("データセットが選択されていません。") 

27 return 

28 ctx.show_message(f"「{dataset.name}」の表示中の点数: {len(dataset.visible_df)}") 

29 

30 

31def register(api): 

32 api.register_fit_function( 

33 "二重指数減衰 (y = a*exp(-bx) + c*exp(-dx))", 

34 _double_exp_func, 

35 ["a", "b", "c", "d"], 

36 p0=_double_exp_p0, 

37 ) 

38 api.register_menu_action("選択中データセットの点数を表示", _show_dataset_point_count)