增加扩胞逻辑

This commit is contained in:
2025-12-14 18:11:00 +08:00
parent 83647c2218
commit 9bde3e1229
3 changed files with 60 additions and 39 deletions

View File

@@ -217,17 +217,20 @@ class DatabaseAnalyzer:
# 更新 _compute_statistics 方法
def _compute_statistics(self, report: DatabaseReport):
"""计算统计数据(含扩胞分析)"""
"""计算统计数据"""
for info in report.all_structures:
# 确保 info 不是 None
if info is None:
report.invalid_files += 1
continue
# 检查有效性
if info.is_valid:
report.valid_files += 1
else:
report.invalid_files += 1
continue
if not info.contains_target_cation:
continue
continue # 无效文件不继续统计
report.cation_containing_count += 1

View File

@@ -131,6 +131,7 @@ class StructureInspector:
Returns:
StructureInfo: 分析结果
"""
import os
info = StructureInfo(
file_path=file_path,
file_name=os.path.basename(file_path)
@@ -139,11 +140,16 @@ class StructureInspector:
# 尝试读取结构
try:
structure = Structure.from_file(file_path)
info.is_valid = True
except Exception as e:
info.error_message = str(e)
info.is_valid = False
info.error_message = f"读取CIF失败: {str(e)}"
return info
# 结构读取成功,标记为有效
info.is_valid = True
# 后续分析用 try-except 包裹,确保即使分析出错也能返回基本信息
try:
# 基础信息
info.elements = {str(el) for el in structure.composition.elements}
info.num_sites = structure.num_sites
@@ -168,10 +174,17 @@ class StructureInspector:
info.has_oxidation_states = self._check_oxidation_states(structure)
# 检查共占位(核心分析)
try:
self._analyze_partial_occupancy(structure, info)
except Exception as e:
# 共占位分析失败,记录但继续
pass
# 检查水分子
try:
info.has_water_molecule = self._check_water_molecule(structure)
except:
info.has_water_molecule = False
# 检查放射性元素
info.has_radioactive_elements = bool(
@@ -181,6 +194,11 @@ class StructureInspector:
# 判断可处理性
self._evaluate_processability(info)
except Exception as e:
# 分析过程出错,但文件本身是有效的
# 保留 is_valid = True但记录错误
info.error_message = f"分析过程出错: {str(e)}"
return info
def _check_oxidation_states(self, structure: Structure) -> bool:

View File

@@ -27,17 +27,17 @@ def analyze_single_file(args: Tuple[str, str, set]) -> Optional[StructureInfo]:
target_cation=target_cation,
target_anions=target_anions
)
return inspector.inspect(file_path)
result = inspector.inspect(file_path)
return result
except Exception as e:
# 返回一个标记失败的结果
# 返回一个标记失败的结果(而不是 None
return StructureInfo(
file_path=file_path,
file_name=os.path.basename(file_path),
is_valid=False,
error_message=str(e)
error_message=f"Worker异常: {str(e)}"
)
def structure_info_to_dict(info: StructureInfo) -> dict:
"""
将 StructureInfo 转换为可序列化的字典