LumAPI 1.1.3__tar.gz → 1.1.5__tar.gz

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.
@@ -4,7 +4,7 @@
4
4
  from .lumapi import (
5
5
  lumapi, lumerical, LumFuncBase,
6
6
  FDTD, MODE, DEVICE, INTERCONNECT,
7
- savemat, loadmat,
7
+ savemat, loadmat, save_h5, load_h5,
8
8
  create_cmap, set_colorbar_range,
9
9
  Estimate_focal, Kirchhoff,
10
10
  RayleighSommerfeld_Scalar, RayleighSommerfeld_Vector, AngularSpectrum_Vector
@@ -14,7 +14,7 @@ from .lumapi import (
14
14
  __all__ = [
15
15
  'lumapi', 'lumerical', 'LumFuncBase',
16
16
  'FDTD', 'MODE', 'DEVICE', 'INTERCONNECT',
17
- 'savemat', 'loadmat',
17
+ 'savemat', 'loadmat', 'save_h5', 'load_h5',
18
18
  'create_cmap', 'set_colorbar_range',
19
19
  'Estimate_focal', 'Kirchhoff',
20
20
  'RayleighSommerfeld_Scalar', 'RayleighSommerfeld_Vector', 'AngularSpectrum_Vector'
@@ -9,7 +9,7 @@ import re, platform
9
9
  current_dir = os.path.dirname(os.path.abspath(__file__))
10
10
  CONFIG_PATH = os.path.join(current_dir, 'config.json')
11
11
 
12
- # ******************mat处理函数******************
12
+ # ******************数据处理函数******************
13
13
  def savemat(filename, data_dict, version='v7.3', auto_transpose=True):
14
14
  """
15
15
  将字典数据写入 MATLAB .mat 文件。
@@ -49,7 +49,8 @@ def savemat(filename, data_dict, version='v7.3', auto_transpose=True):
49
49
 
50
50
  if version == 'v7.3':
51
51
  import h5py
52
- with h5py.File(filename, 'w') as f:
52
+ # 预留 512 字节的 userblock 空间给 MATLAB 特征头
53
+ with h5py.File(filename, 'w', userblock_size=512) as f:
53
54
  for key, data_array in processed_dict.items():
54
55
 
55
56
  # 自动转置处理
@@ -67,6 +68,22 @@ def savemat(filename, data_dict, version='v7.3', auto_transpose=True):
67
68
  f.create_dataset(key, data=mat_complex)
68
69
  else:
69
70
  f.create_dataset(key, data=data_array)
71
+
72
+ # 受限于底层 HDF5 C 语言库的运行机制,wb模式会清空文件,追加a模式会报错,只能先h5py保存后with open打开写入文件头
73
+ # HDF5 文件写入完成后,以读写模式打开并注入 MATLAB 文件头
74
+ with open(filename, 'r+b') as f:
75
+ # 前 116 字节为文本描述,不足部分用空格补齐
76
+ header_str = 'MATLAB 7.3 MAT-file, created by LumAPI custom script'
77
+ header_bytes = header_str.encode('ascii').ljust(116, b' ')
78
+
79
+ # 接下来的 8 字节为子系统数据偏移量(全 0 即可)
80
+ subsys_offset = b'\x00' * 8
81
+
82
+ # 最后 4 字节为版本号和字节序:0x0200 表示 v7.3,'IM' 表示小端序 (Little Endian)
83
+ version_and_endian = b'\x02\x00IM'
84
+
85
+ f.seek(0)
86
+ f.write(header_bytes + subsys_offset + version_and_endian)
70
87
 
71
88
  elif version == 'v7':
72
89
  import scipy.io
@@ -157,6 +174,100 @@ def loadmat(filename, auto_transpose=True, squeeze_me=True):
157
174
 
158
175
  return data_dict
159
176
 
177
+ def save_h5(filename, data_dict, compression=True):
178
+ """
179
+ 将字典数据写入标准 HDF5 文件,支持复数处理和压缩。
180
+
181
+ 参数 (Parameters):
182
+ ------------------
183
+ filename : str
184
+ 输出的 .h5 文件路径和名称。
185
+ data_dict : dict
186
+ 需要写入的数据字典。
187
+ compression : bool, 可选 (默认: True)
188
+ 是否进行数据压缩,使文件更小
189
+
190
+ 返回 (Returns):
191
+ ---------------
192
+ bool
193
+ 写入成功返回 True。
194
+ """
195
+ import h5py
196
+ # 自动补全后缀
197
+ if not filename.endswith('.h5') and not filename.endswith('.hdf5'):
198
+ filename += '.h5'
199
+
200
+ with h5py.File(filename, 'w') as f:
201
+ for key, val in data_dict.items():
202
+ data = np.asarray(val)
203
+
204
+ # 处理数据类型:将整型转为双精度浮点
205
+ if np.issubdtype(data.dtype, np.integer):
206
+ data = data.astype(np.float64)
207
+
208
+ # 压缩设置:对于大数据非常有帮助
209
+ dataset_args = {}
210
+ if compression and data.ndim > 0:
211
+ dataset_args = {"compression": "gzip", "compression_opts": 4}
212
+
213
+ if np.iscomplexobj(data):
214
+ # 采用复合类型存储复数
215
+ complex_dt = np.dtype([('real', data.real.dtype), ('imag', data.imag.dtype)])
216
+ mat_complex = np.empty(data.shape, dtype=complex_dt)
217
+
218
+ # 兼容 0D 标量复数和多维复数数组的赋值
219
+ if data.ndim == 0:
220
+ mat_complex['real'] = data.real
221
+ mat_complex['imag'] = data.imag
222
+ else:
223
+ mat_complex['real'] = data.real
224
+ mat_complex['imag'] = data.imag
225
+
226
+ dset = f.create_dataset(key, data=mat_complex, **dataset_args)
227
+ dset.attrs['is_complex'] = 1
228
+ else:
229
+ f.create_dataset(key, data=data, **dataset_args)
230
+
231
+ # print(f"[成功] 数据已保存至标准 HDF5: {filename}")
232
+ return True
233
+
234
+ def load_h5(filename):
235
+ """
236
+ 从标准 HDF5 文件读取数据,并自动恢复复数结构。
237
+
238
+ 参数 (Parameters):
239
+ ------------------
240
+ filename : str
241
+ 读取的 .h5 文件路径和名称。
242
+
243
+ 返回 (Returns):
244
+ ---------------
245
+ dict
246
+ 读取的数据字典。
247
+ """
248
+ import h5py
249
+ if not os.path.exists(filename):
250
+ raise FileNotFoundError(f"找不到文件: {filename}")
251
+
252
+ data_dict = {}
253
+ try:
254
+ with h5py.File(filename, 'r') as f:
255
+ for key in f.keys():
256
+ data = np.array(f[key])
257
+
258
+ # 自动检测并恢复复数
259
+ # 检查是否存在属性标记,或检查复合类型字段
260
+ is_complex = f[key].attrs.get('is_complex', 0)
261
+ has_fields = data.dtype.names is not None and 'real' in data.dtype.names
262
+
263
+ if is_complex or has_fields:
264
+ data = data['real'] + 1j * data['imag']
265
+
266
+ data_dict[key] = data
267
+ return data_dict
268
+ except Exception as e:
269
+ print(f"错误:读取 H5 文件失败 - {str(e)}")
270
+ return None
160
271
 
161
272
  # *****************绘图增强函数******************
162
273
  def create_cmap(color_list, cmap_name="custom_cmap"):
@@ -1002,7 +1113,12 @@ def detect_version(lumerical_root):
1002
1113
 
1003
1114
  def get_lumapi_path(lumerical_root, version):
1004
1115
  """从Lumerical根路径和版本获取lumapi.py路径"""
1005
- return os.path.join(lumerical_root, version, "api", "python", "lumapi.py")
1116
+ base = os.path.join(lumerical_root, version)
1117
+ p1 = os.path.join(base, "Lumerical", "api", "python", "lumapi.py")
1118
+ if os.path.exists(p1): return p1
1119
+ p2 = os.path.join(base, "api", "python", "lumapi.py")
1120
+ if os.path.exists(p2): return p2
1121
+ return None
1006
1122
 
1007
1123
  def validate_path(lumerical_root: str, version: str = None) -> object:
1008
1124
  """验证Lumerical路径有效性并返回lumapi对象
@@ -1031,7 +1147,7 @@ def validate_path(lumerical_root: str, version: str = None) -> object:
1031
1147
  # 获取lumapi.py的完整路径
1032
1148
  lumapi_path = get_lumapi_path(lumerical_root, version)
1033
1149
 
1034
- if not os.path.exists(lumapi_path):
1150
+ if not lumapi_path:
1035
1151
  print(f"错误:在指定路径未找到 lumapi.py 文件(查找路径:{lumapi_path})")
1036
1152
  return None
1037
1153
 
@@ -4,6 +4,8 @@ import numpy as np
4
4
  # ================= 自定义功能函数 =================
5
5
  def savemat(filename: str, data_dict: Dict[str, Any], version: str = 'v7.3', auto_transpose: bool = True) -> bool: ...
6
6
  def loadmat(filename: str, auto_transpose: bool = True, squeeze_me: bool = True) -> Dict[str, Any]: ...
7
+ def save_h5(filename, data_dict, compression=True) -> bool: ...
8
+ def load_h5(filename) -> Dict[str, Any]: ...
7
9
 
8
10
  def create_cmap(color_list: list, cmap_name: str = "custom_cmap") -> Any: ...
9
11
  def set_colorbar_range(mappable: Any, vmin: float, vmax: float) -> None: ...
@@ -58,6 +58,8 @@ import numpy as np
58
58
  # ================= mat保存读取函数 =================
59
59
  def savemat(filename: str, data_dict: Dict[str, Any], version: str = 'v7.3', auto_transpose: bool = True) -> bool: ...
60
60
  def loadmat(filename: str, auto_transpose: bool = True, squeeze_me: bool = True) -> Dict[str, Any]: ...
61
+ def save_h5(filename, data_dict, compression=True) -> bool: ...
62
+ def load_h5(filename) -> Dict[str, Any]: ...
61
63
 
62
64
  # ==================== 绘图函数 ====================
63
65
  def create_cmap(color_list: list, cmap_name: str = "custom_cmap") -> Any: ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: LumAPI
3
- Version: 1.1.3
3
+ Version: 1.1.5
4
4
  Summary: Lumerical Python API 自动化配置工具
5
5
  Author-email: JaniQuiz <janiquiz@163.com>
6
6
  Maintainer-email: JaniQuiz <janiquiz@163.com>
@@ -22,12 +22,12 @@ Requires-Dist: h5py
22
22
  Requires-Dist: tqdm
23
23
  Dynamic: license-file
24
24
 
25
- # Lumerical FDTD Python 自用脚本整理
25
+ # LumAPI:Lumerical Python接口的轻量级封装与增强库
26
26
 
27
27
  本项目旨在简化 Lumerical FDTD 软件与 Python 环境的交互流程,提供自动化的路径配置工具,并封装了matlab数据文件.mat的读取和写入,以及常用的光学仿真后处理算法(如近场至远场变换)。
28
28
 
29
29
  ## 📖 核心功能
30
- * **自动化环境配置**:自动识别系统中的 Lumerical 安装路径及版本。
30
+ * **自动化环境配置**:自动识别系统中的 Lumerical 安装路径及版本,支持`Lumerical`安装和`Ansys`安装两种路径。
31
31
  * **灵活集成**:支持将 API 库集成至特定 Python 解释器或独立项目目录。
32
32
  * **便捷配置**:支持图形化界面和命令行界面两种配置方式。
33
33
  * **API 增强**:支持原本接口,并对原本接口进行优化。
@@ -55,7 +55,7 @@ Dynamic: license-file
55
55
  - [矢量角谱(Angular Spectrum)矢量衍射积分函数验证](docs/AngularSpectrum_Vector.md)
56
56
 
57
57
  ## 📜 许可证
58
- 本库遵循[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html)开源许可证
58
+ 本仓库遵循[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html)开源许可证
59
59
 
60
60
  ## 📧 联系作者
61
61
  - **作者**: [JaniQuiz](https://github.com/JaniQuiz)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: LumAPI
3
- Version: 1.1.3
3
+ Version: 1.1.5
4
4
  Summary: Lumerical Python API 自动化配置工具
5
5
  Author-email: JaniQuiz <janiquiz@163.com>
6
6
  Maintainer-email: JaniQuiz <janiquiz@163.com>
@@ -22,12 +22,12 @@ Requires-Dist: h5py
22
22
  Requires-Dist: tqdm
23
23
  Dynamic: license-file
24
24
 
25
- # Lumerical FDTD Python 自用脚本整理
25
+ # LumAPI:Lumerical Python接口的轻量级封装与增强库
26
26
 
27
27
  本项目旨在简化 Lumerical FDTD 软件与 Python 环境的交互流程,提供自动化的路径配置工具,并封装了matlab数据文件.mat的读取和写入,以及常用的光学仿真后处理算法(如近场至远场变换)。
28
28
 
29
29
  ## 📖 核心功能
30
- * **自动化环境配置**:自动识别系统中的 Lumerical 安装路径及版本。
30
+ * **自动化环境配置**:自动识别系统中的 Lumerical 安装路径及版本,支持`Lumerical`安装和`Ansys`安装两种路径。
31
31
  * **灵活集成**:支持将 API 库集成至特定 Python 解释器或独立项目目录。
32
32
  * **便捷配置**:支持图形化界面和命令行界面两种配置方式。
33
33
  * **API 增强**:支持原本接口,并对原本接口进行优化。
@@ -55,7 +55,7 @@ Dynamic: license-file
55
55
  - [矢量角谱(Angular Spectrum)矢量衍射积分函数验证](docs/AngularSpectrum_Vector.md)
56
56
 
57
57
  ## 📜 许可证
58
- 本库遵循[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html)开源许可证
58
+ 本仓库遵循[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html)开源许可证
59
59
 
60
60
  ## 📧 联系作者
61
61
  - **作者**: [JaniQuiz](https://github.com/JaniQuiz)
@@ -1,9 +1,9 @@
1
- # Lumerical FDTD Python 自用脚本整理
1
+ # LumAPI:Lumerical Python接口的轻量级封装与增强库
2
2
 
3
3
  本项目旨在简化 Lumerical FDTD 软件与 Python 环境的交互流程,提供自动化的路径配置工具,并封装了matlab数据文件.mat的读取和写入,以及常用的光学仿真后处理算法(如近场至远场变换)。
4
4
 
5
5
  ## 📖 核心功能
6
- * **自动化环境配置**:自动识别系统中的 Lumerical 安装路径及版本。
6
+ * **自动化环境配置**:自动识别系统中的 Lumerical 安装路径及版本,支持`Lumerical`安装和`Ansys`安装两种路径。
7
7
  * **灵活集成**:支持将 API 库集成至特定 Python 解释器或独立项目目录。
8
8
  * **便捷配置**:支持图形化界面和命令行界面两种配置方式。
9
9
  * **API 增强**:支持原本接口,并对原本接口进行优化。
@@ -31,7 +31,7 @@
31
31
  - [矢量角谱(Angular Spectrum)矢量衍射积分函数验证](docs/AngularSpectrum_Vector.md)
32
32
 
33
33
  ## 📜 许可证
34
- 本库遵循[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html)开源许可证
34
+ 本仓库遵循[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html)开源许可证
35
35
 
36
36
  ## 📧 联系作者
37
37
  - **作者**: [JaniQuiz](https://github.com/JaniQuiz)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "LumAPI"
7
- version = "1.1.3"
7
+ version = "1.1.5"
8
8
  authors = [
9
9
  {name = "JaniQuiz", email = "janiquiz@163.com"},
10
10
  ]
@@ -0,0 +1,22 @@
1
+ from LumAPI import *
2
+ import numpy as np
3
+
4
+ # x = np.linspace(0, 1, 100)
5
+ # y = np.linspace(0, 1, 100)
6
+ # X, Y = np.meshgrid(x, y)
7
+ # E = np.ones_like(X)
8
+
9
+ # data = {
10
+ # 'x': x,
11
+ # 'y': y,
12
+ # 'E': E
13
+ # }
14
+ # savemat('test.mat', data)
15
+
16
+ fdtd = lumapi.FDTD()
17
+ fdtd.addrect()
18
+
19
+ fdtd.close()
20
+
21
+
22
+
lumapi-1.1.3/test/test.py DELETED
@@ -1,18 +0,0 @@
1
- from LumAPI import *
2
-
3
- x = np.linspace(0, 1, 100)
4
- y = np.linspace(0, 1, 100)
5
- X, Y = np.meshgrid(x, y)
6
- E = np.ones_like(X)
7
-
8
- data = {
9
- 'x': x,
10
- 'y': y,
11
- 'E': E
12
- }
13
- savemat('test.mat', data)
14
-
15
-
16
-
17
-
18
-
File without changes
File without changes
File without changes
File without changes
File without changes