kevin-toolbox-dev 1.4.0__py3-none-any.whl → 1.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kevin_toolbox/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "1.4.0"
1
+ __version__ = "1.4.1"
2
2
 
3
3
 
4
4
  import os
@@ -12,5 +12,5 @@ os.system(
12
12
  os.system(
13
13
  f'python {os.path.split(__file__)[0]}/env_info/check_validity_and_uninstall.py '
14
14
  f'--package_name kevin-toolbox-dev '
15
- f'--expiration_timestamp 1741603427 --verbose 0'
15
+ f'--expiration_timestamp 1742651885 --verbose 0'
16
16
  )
@@ -1,11 +1,20 @@
1
1
  import os
2
+ import numpy as np
2
3
  from sklearn.metrics import confusion_matrix
3
4
  import matplotlib.pyplot as plt
4
5
  import seaborn as sns
5
6
  from kevin_toolbox.patches.for_os.path import replace_illegal_chars
6
7
 
7
8
 
8
- def plot_confusion_matrix(data_s, title, gt_name, pd_name, label_to_value_s=None, output_dir=None, **kwargs):
9
+ def plot_confusion_matrix(data_s, title, gt_name, pd_name, label_to_value_s=None, output_dir=None,
10
+ replace_zero_division_with=0, **kwargs):
11
+ """
12
+ 计算并绘制混淆矩阵
13
+
14
+ 参数:
15
+ replace_zero_division_with: <float> 对于在normalize时引发除0错误的矩阵元素,使用何种值进行替代
16
+ 建议使用 np.nan 或者 0
17
+ """
9
18
  paras = {
10
19
  "dpi": 200,
11
20
  "normalize": None, # "true", "pred", "all",
@@ -15,14 +24,28 @@ def plot_confusion_matrix(data_s, title, gt_name, pd_name, label_to_value_s=None
15
24
 
16
25
  value_set = set(data_s[gt_name]).union(set(data_s[pd_name]))
17
26
  if label_to_value_s is None:
18
- label_to_value_s = dict()
19
- for i in value_set.difference(set(label_to_value_s.values())):
20
- label_to_value_s[f'{i}'] = i
21
-
22
- assert set(label_to_value_s.values()).issuperset(value_set)
27
+ label_to_value_s = {f'{i}': i for i in value_set}
28
+ else:
29
+ # assert all(i in value_set for i in label_to_value_s.values())
30
+ pass
23
31
  # 计算混淆矩阵
24
32
  cfm = confusion_matrix(y_true=data_s[gt_name], y_pred=data_s[pd_name], labels=list(label_to_value_s.values()),
25
33
  normalize=paras["normalize"])
34
+ # replace with nan
35
+ if paras["normalize"] is not None:
36
+ if paras["normalize"] == "all":
37
+ if cfm.sum() == 0:
38
+ cfm[cfm == 0] = replace_zero_division_with
39
+ else:
40
+ check_axis = 1 if paras["normalize"] == "true" else 0
41
+ temp = np.sum(cfm, axis=check_axis, keepdims=False)
42
+ for i in range(len(temp)):
43
+ if temp[i] == 0:
44
+ if check_axis == 0:
45
+ cfm[:, i] = replace_zero_division_with
46
+ else:
47
+ cfm[i, :] = replace_zero_division_with
48
+
26
49
  # 绘制混淆矩阵热力图
27
50
  plt.clf()
28
51
  plt.figure(figsize=(8, 6))
@@ -49,14 +72,13 @@ def plot_confusion_matrix(data_s, title, gt_name, pd_name, label_to_value_s=None
49
72
 
50
73
 
51
74
  if __name__ == '__main__':
52
- import numpy as np
53
-
54
75
  # 示例真实标签和预测标签
55
76
  y_true = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2, 5])
56
77
  y_pred = np.array([0, 2, 1, 0, 2, 1, 0, 1, 1, 5])
57
78
 
58
79
  plot_confusion_matrix(data_s={'a': y_true, 'b': y_pred},
59
80
  title='test', gt_name='a', pd_name='b',
60
- label_to_value_s={"A": 5, "B": 0, "C": 1, "D": 2},
81
+ label_to_value_s={"A": 5, "B": 0, "C": 1, "D": 2, "E": 3},
61
82
  # output_dir=os.path.join(os.path.dirname(__file__), "temp"),
62
- normalize="true")
83
+ replace_zero_division_with=-1,
84
+ normalize="all")
@@ -11,7 +11,8 @@ DEFAULT_DISPLAY_MODE_S = {
11
11
 
12
12
 
13
13
  def _show_table_by_columns(matrix, doc_dir, table_name, **kwargs):
14
- with st.expander(label=table_name, expanded=True):
14
+ tab, _ = st.tabs([table_name, "[click to hide table]"])
15
+ with tab:
15
16
  for row in matrix:
16
17
  col_ls = st.columns(len(row))
17
18
  for col, i in zip(col_ls, row):
@@ -21,7 +22,7 @@ def _show_table_by_columns(matrix, doc_dir, table_name, **kwargs):
21
22
 
22
23
  METHOD_S = {
23
24
  "by_columns": _show_table_by_columns,
24
- "by_markdown": lambda text, **kwargs: st.markdown(text=text)
25
+ "by_markdown": lambda text, **kwargs: st.markdown(text)
25
26
  }
26
27
 
27
28
 
@@ -52,7 +53,7 @@ def show_table(text, doc_dir=None, display_mode_s=None):
52
53
  method(text=part, matrix=table_s["matrix"], doc_dir=doc_dir, table_name=f'Table {idx}')
53
54
  else:
54
55
  # 是表格,且内部无图片,则直接显示
55
- show_image(part)
56
+ show_image(text=part, doc_dir=None)
56
57
 
57
58
  # 另一种显示表格的方式是通过 data_editor 来显示,但是对图片的显示效果不好
58
59
  # TODO 可以选择是通过 data_editor 还是 columns,或者原始格式(对本地图片不处理或者使用 base64 代替)来显示表格
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.1
2
+ Name: kevin-toolbox-dev
3
+ Version: 1.4.1
4
+ Summary: 一个常用的工具代码包集合
5
+ Home-page: https://github.com/cantbeblank96/kevin_toolbox
6
+ Download-URL: https://github.com/username/your-package/archive/refs/tags/v1.0.0.tar.gz
7
+ Author: kevin hsu
8
+ Author-email: xukaiming1996@163.com
9
+ License: MIT
10
+ Keywords: mathematics,pytorch,numpy,machine-learning,algorithm
11
+ Platform: UNKNOWN
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3
15
+ Requires-Python: >=3.6
16
+ Description-Content-Type: text/markdown
17
+ Requires-Dist: torch (>=1.2.0)
18
+ Requires-Dist: numpy (>=1.19.0)
19
+ Provides-Extra: plot
20
+ Requires-Dist: matplotlib (>=3.0) ; extra == 'plot'
21
+ Provides-Extra: rest
22
+ Requires-Dist: pytest (>=6.2.5) ; extra == 'rest'
23
+ Requires-Dist: line-profiler (>=3.5) ; extra == 'rest'
24
+
25
+ # kevin_toolbox
26
+
27
+ 一个通用的工具代码包集合
28
+
29
+
30
+
31
+ 环境要求
32
+
33
+ ```shell
34
+ numpy>=1.19
35
+ pytorch>=1.2
36
+ ```
37
+
38
+ 安装方法:
39
+
40
+ ```shell
41
+ pip install kevin-toolbox --no-dependencies
42
+ ```
43
+
44
+
45
+
46
+ [项目地址 Repo](https://github.com/cantbeblank96/kevin_toolbox)
47
+
48
+ [使用指南 User_Guide](./notes/User_Guide.md)
49
+
50
+ [免责声明 Disclaimer](./notes/Disclaimer.md)
51
+
52
+ [版本更新记录](./notes/Release_Record.md):
53
+
54
+ - v 1.4.1 (2024-09-23)【bug fix】【new feature】
55
+ - patches
56
+ - for_streamlit.markdown
57
+ - 【bug fix】fix bug in show_table(),将原来的使用 st.expander 去包裹表格,改为使用 st.tabs 去包裹表格,避免在 streamlit<=1.38.0 下(截止2024-09-23最新版本),因为 st.expander 嵌套使用而造成的报错。具体参看:https://docs.streamlit.io/develop/api-reference/layout/st.expander
58
+ - 【bug fix】fix bug in show_table(),修复在 line 56 和 line 25 中对 show_image() 和 st.markdown 的函数参数写错,导致在显示无图表格时反而报错的问题。
59
+ - 增加了测试用例。
60
+
61
+ - for_matplotlib.common_charts
62
+ - 【new feature】 add para replace_zero_division_with to plot_confusion_matrix(),新增参数 replace_zero_division_with 用于指定在normalize时引发除0错误的矩阵元素要使用何种值进行替代。
63
+ - 增加了测试用例。
64
+
65
+
66
+
67
+
@@ -1,4 +1,4 @@
1
- kevin_toolbox/__init__.py,sha256=-Q49VzPSum4_2Fr48x0D8r7urVTtPiLEFQ7Gus40Rkw,410
1
+ kevin_toolbox/__init__.py,sha256=7isptekqTWuS1t1NRWNgtllHGRDc1eNX2UVtujkt5NM,410
2
2
  kevin_toolbox/computer_science/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  kevin_toolbox/computer_science/algorithm/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
4
4
  kevin_toolbox/computer_science/algorithm/cache_manager/__init__.py,sha256=p2hddkZ1HfYF9-m2Hx-o9IotwQHd4QwDCePy2ADpTDA,41
@@ -281,7 +281,7 @@ kevin_toolbox/patches/for_matplotlib/color/generate_color_list.py,sha256=TZm-TkO
281
281
  kevin_toolbox/patches/for_matplotlib/color/get_format.py,sha256=l_vX8DUsWHNzLwveuF60TLcbQ_P7PvVt1yH_7FjElDs,312
282
282
  kevin_toolbox/patches/for_matplotlib/common_charts/__init__.py,sha256=etey2r0LO4PTLnH3VzcRKFe7IHP9I5TMW3DEz3sQx2c,270
283
283
  kevin_toolbox/patches/for_matplotlib/common_charts/plot_bars.py,sha256=crS1h79Dz6gGOnqhjuuN2o5pl8CekhCenx9lRz5KPiI,1887
284
- kevin_toolbox/patches/for_matplotlib/common_charts/plot_confusion_matrix.py,sha256=RVTsQhC04KDOg8wUO8sB66bBG07DKAC2I072UhUTr_o,2256
284
+ kevin_toolbox/patches/for_matplotlib/common_charts/plot_confusion_matrix.py,sha256=KtmUAlKs3_ALFRKAEi0OAXj6SyG5L7LMmoSgOxKvvVs,3213
285
285
  kevin_toolbox/patches/for_matplotlib/common_charts/plot_distribution.py,sha256=stuyaULWM_vVW3r9WrpzGqA8rohQrdNKT3Agsbobqck,2396
286
286
  kevin_toolbox/patches/for_matplotlib/common_charts/plot_lines.py,sha256=j2GBT_E9tvQhLN2ynCknuBl1MjD6q2TZeNYGvm2IVRA,2034
287
287
  kevin_toolbox/patches/for_matplotlib/common_charts/plot_scatters.py,sha256=whO36bmixjwtsjCS6Ah6zEGJAlJyGcD-wmV3dA6u7mk,1658
@@ -324,7 +324,7 @@ kevin_toolbox/patches/for_streamlit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQ
324
324
  kevin_toolbox/patches/for_streamlit/markdown/__init__.py,sha256=ZWNRNA7yn3LD_YMjBuUHrXcxDcG4iswIZtCJVCnRVB0,93
325
325
  kevin_toolbox/patches/for_streamlit/markdown/show.py,sha256=uSkArSUv8N05TFWsIpXa8f15uhN1Lpm0ZHZst_IytgY,327
326
326
  kevin_toolbox/patches/for_streamlit/markdown/show_image.py,sha256=8njiSDiPWWRNwevvpgipxZS3My7bGHp9j0dxLiut_x8,1546
327
- kevin_toolbox/patches/for_streamlit/markdown/show_table.py,sha256=yW1wrIngbb8xCA5iuDN_jgeet_iaV5PhA1y8OgQbDYg,3218
327
+ kevin_toolbox/patches/for_streamlit/markdown/show_table.py,sha256=mZu37G9lqtpSEP62YLv88rDw-OSe8BCFkmSa2UQt6fY,3251
328
328
  kevin_toolbox/patches/for_test/__init__.py,sha256=sFr2VZD1zk8Vtjq2_F8uE4xNovJF6yDY8j1YND5XAw0,49
329
329
  kevin_toolbox/patches/for_test/check_consistency.py,sha256=cerf4NywkvWYMvuJUjimfRRVU7D9vL30jTAX0NxxRoM,9422
330
330
  kevin_toolbox/patches/for_torch/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -342,7 +342,7 @@ kevin_toolbox/patches/for_torch/math/get_y_at_x.py,sha256=bfoVcasZ_tMdhR_1Me0Jli
342
342
  kevin_toolbox/patches/for_torch/math/my_around.py,sha256=ptpU3ids50gwf663EpHbw7raj9tNrDGBFZ5t_uMNH14,1378
343
343
  kevin_toolbox/patches/for_torch/nn/__init__.py,sha256=aJs3RMqRzQmd8KKDmQW9FxwCqS5yfPqEdg-m0PwlQro,39
344
344
  kevin_toolbox/patches/for_torch/nn/lambda_layer.py,sha256=KUuLiX_Dr4bvRmpAaCW5QTDWDcnMPRnw0jg4NNXTFhM,223
345
- kevin_toolbox_dev-1.4.0.dist-info/METADATA,sha256=jn9h9g9cLt9ai5kkd2Y8aDkTiv31Qg5Ia63M9lQfF4w,4041
346
- kevin_toolbox_dev-1.4.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
347
- kevin_toolbox_dev-1.4.0.dist-info/top_level.txt,sha256=S5TeRGF-PwlhsaUEPTI-f2vWrpLmh3axpyI6v-Fi75o,14
348
- kevin_toolbox_dev-1.4.0.dist-info/RECORD,,
345
+ kevin_toolbox_dev-1.4.1.dist-info/METADATA,sha256=b3yGqO3ykWWJRAx1ChCx9N_v_ezAfbIwchtFwrgtt3U,2234
346
+ kevin_toolbox_dev-1.4.1.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
347
+ kevin_toolbox_dev-1.4.1.dist-info/top_level.txt,sha256=S5TeRGF-PwlhsaUEPTI-f2vWrpLmh3axpyI6v-Fi75o,14
348
+ kevin_toolbox_dev-1.4.1.dist-info/RECORD,,
@@ -1,88 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: kevin-toolbox-dev
3
- Version: 1.4.0
4
- Summary: 一个常用的工具代码包集合
5
- Home-page: https://github.com/cantbeblank96/kevin_toolbox
6
- Download-URL: https://github.com/username/your-package/archive/refs/tags/v1.0.0.tar.gz
7
- Author: kevin hsu
8
- Author-email: xukaiming1996@163.com
9
- License: MIT
10
- Keywords: mathematics,pytorch,numpy,machine-learning,algorithm
11
- Platform: UNKNOWN
12
- Classifier: License :: OSI Approved :: MIT License
13
- Classifier: Programming Language :: Python
14
- Classifier: Programming Language :: Python :: 3
15
- Requires-Python: >=3.6
16
- Description-Content-Type: text/markdown
17
- Requires-Dist: torch (>=1.2.0)
18
- Requires-Dist: numpy (>=1.19.0)
19
- Provides-Extra: plot
20
- Requires-Dist: matplotlib (>=3.0) ; extra == 'plot'
21
- Provides-Extra: rest
22
- Requires-Dist: pytest (>=6.2.5) ; extra == 'rest'
23
- Requires-Dist: line-profiler (>=3.5) ; extra == 'rest'
24
-
25
- # kevin_toolbox
26
-
27
- 一个通用的工具代码包集合
28
-
29
-
30
-
31
- 环境要求
32
-
33
- ```shell
34
- numpy>=1.19
35
- pytorch>=1.2
36
- ```
37
-
38
- 安装方法:
39
-
40
- ```shell
41
- pip install kevin-toolbox --no-dependencies
42
- ```
43
-
44
-
45
-
46
- [项目地址 Repo](https://github.com/cantbeblank96/kevin_toolbox)
47
-
48
- [使用指南 User_Guide](./notes/User_Guide.md)
49
-
50
- [免责声明 Disclaimer](./notes/Disclaimer.md)
51
-
52
- [版本更新记录](./notes/Release_Record.md):
53
-
54
- - v 1.4.0 (2024-09-11)【bug fix】【new feature】【incompatible change】
55
- - patches
56
- - for_matplotlib.common_charts
57
- - 【bug fix】fix bug in plot_confusion_matrix() for paras label_to_value_s,删除了对参数 label_to_value_s 的不合理的检验,并且支持更加自由的 label_to_value_s 设置,比如允许 label_to_value_s 中缺少 data_s 中已有的 label_idx,或者含有部分 data_s 中未见的 label_idx。
58
- - 【bug fix】fix bug in plot_lines(),对接受的输入 data_s 进行 copy,避免后续操作引起输入意外改变。
59
- - 增加测试用例。
60
- - for_streamlit.markdown
61
- - 【new feature】add show_image(),对 st.markdown 中图片显示部分的改进,能够正确显示本地的图片。
62
- - 【new feature】add show_table(),对 st.markdown 中表格显示部分的改进,支持以下多种方式来显示表格。
63
- - 【new feature】add show(),st.markdown 的改进版。
64
- - 增加测试用例。
65
- - data_flow.file
66
- - markdown【incompatible change】【new feature】
67
- - 【refactor and modify】
68
- - generate_table() ==> table.generate_table()
69
- - generate_link() ==> link.generate_link()
70
- - save_images_in_ndl() ==> utils.save_images_in_ndl()
71
- - parse_table() ,可以使用 table.convert_format() 来代替实现表格格式的转换
72
- - table
73
- - 【new feature】新增 Table_Format、get_format()、convert_format() 用于表格格式的转换,目前支持以下几种格式:
74
- - simple_dict 简易字典模式:`{<title>: <list of value>, ...}`
75
- - complete_dict 完整字典模式:`{<index>: {"title": <title>, "values": <list of value>}, ...}`
76
- - matrix 矩阵形式:`{"matrix": [[...], [...], ...], "orientation":...(, "chunk_nums":..., "chunk_size":...)}`
77
- - 【new feature】add find_tables(),查找文本中的表格
78
- - 当 b_compact_format 设为 True,此时返回 table_ls,其中每个元素是一个 MATRIX 格式的表格
79
- - 当 b_compact_format 设置为 False,此时返回 (table_ls, part_slices_ls, table_idx_ls),其中 part_slices_ls 是表格和表格前后文本在 text 中对应的 slice,而 table_idx_ls 指出了 part_slices_ls 中第几个元素对应的是表格,table_idx_ls 与 table_ls 依次对应。
80
- - 【new feature】add padding_misaligned_values(), 将标题下长度不相等的 values 补齐
81
- - convert
82
- - 格式转换相关的函数,包括 complete_to_matrix() 和 matrix_to_complete()
83
- - link
84
- - 【new feature】add find_links(),查找文本中的链接。与find_tables()类似,也支持 b_compact_format 参数。
85
- - 增加测试用例。
86
-
87
-
88
-