calc—_v1

This commit is contained in:
2025-12-07 15:22:36 +08:00
parent 35a4bf640f
commit b9da6d9592
6 changed files with 462 additions and 151 deletions

View File

@@ -1,63 +1,13 @@
import os
def creat_sh(input_folder, anion, sh_file_path='analyze.sh'):
"""
创建shell脚本只处理两类CIF文件
1. 纯数字命名的CIF文件 (例如: 123.cif)
2. 数字-坐标格式的CIF文件 (例如: 123-x1y2z3.cif)
参数:
input_folder: 输入文件夹路径
anion: 阴离子类型
sh_file_path: 生成的shell脚本路径
"""
# 文件夹路径
folder_path = input_folder
import re
# 定义两种文件名模式的正则表达式
pattern1 = re.compile(r'^\d+\.cif$') # 纯数字.cif
pattern2 = re.compile(r'^\d+-x\d+y\d+z\d+\.cif$') # 数字-x数字y数字z数字.cif
# 打开SH脚本文件用于写入
with open(sh_file_path, 'w') as sh_file:
# 写入脚本头部
sh_file.write('#!/bin/bash\n')
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
# 只处理文件(不处理文件夹)
if os.path.isfile(file_path):
# 检查文件名是否匹配两种模式之一
if pattern1.match(filename) or pattern2.match(filename):
# 生成对应的命令
command = f"python ../../../tool/analyze_voronoi_nodes.py {filename} -i ../../../tool/{anion}.yaml > {filename}.txt\n"
# 将命令写入SH脚本文件
sh_file.write(command)
print(f"SH脚本已生成{sh_file_path}")
import os
def create_sh_recursive(base_folder, tool_path="tool", relative_depth=2):
"""
递归遍历文件夹为每个包含.cif文件的文件夹生成analyze.sh脚本
递归遍历文件夹,为每个包含.cif文件的文件夹生成analyze.sh脚本,
并在基础文件夹下创建一个sh_all.sh来执行所有脚本。
参数:
base_folder: 起始文件夹路径
tool_path: 工具目录的基本路径
relative_depth: 基础相对深度,用于计算正确的相对路径
"""
# 用于收集所有生成的analyze.sh脚本的相对路径
analyze_sh_paths = []
base_folder_name = os.path.basename(base_folder)
def process_folder(folder_path, current_depth=0):
print(f"处理文件夹: {folder_path}")
@@ -66,17 +16,31 @@ def create_sh_recursive(base_folder, tool_path="tool", relative_depth=2):
folder_name = os.path.basename(folder_path)
# 检查当前文件夹是否包含.cif文件
has_cif_files = any(
f.endswith('.cif') for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)))
# 注意这里我们只关心当前层级下的cif文件
cif_files = [f for f in os.listdir(folder_path) if
f.endswith('.cif') and os.path.isfile(os.path.join(folder_path, f))]
has_cif_files = len(cif_files) > 0
# 如果当前文件夹包含.cif文件生成脚本
# 如果当前文件夹包含.cif文件,生成脚本
if has_cif_files:
# 计算正确的工具路径根据深度增加../
# 计算正确的工具路径(根据深度增加../)
dots = "../" * (relative_depth + current_depth)
tool_relative_path = f"{dots}{tool_path}"
# 确定anion参数使用文件夹名
anion = folder_name
# --- 修改开始: 修正Anion识别逻辑 ---
# 如果结构是 Anion/MaterialID/file.cif此时folder_name是MaterialID
# 我们需要上一级目录的名字作为Anion (例如 'O', 'S', 'Cl') 来寻找对应的 .yaml 文件
# 简单的判断逻辑:如果当前文件夹名字主要由数字组成(或者是ID格式)且包含cif文件我们假设其父目录是Anion类型
# 或者更直接的逻辑在你的新结构中包含cif的文件夹必定是底层文件夹其父目录必定是Anion
parent_dir_name = os.path.basename(os.path.dirname(folder_path))
# 这里做一个简单的保护,如果是在第一层(比如直接在O文件夹下有cif),保持原状,否则使用父目录名
# 在新结构下cif总是在 '.../O/141/141.cif'所以anion应该是 parent_dir_name ('O')
if parent_dir_name in ['O', 'S', 'Cl', 'Br'] or folder_name not in ['O', 'S', 'Cl', 'Br']:
anion = parent_dir_name
else:
anion = folder_name
# --- 修改结束 ---
# 生成脚本文件路径
sh_file_path = os.path.join(folder_path, "analyze.sh")
@@ -84,18 +48,17 @@ def create_sh_recursive(base_folder, tool_path="tool", relative_depth=2):
# 创建脚本
with open(sh_file_path, 'w') as sh_file:
sh_file.write('#!/bin/bash\n')
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path) and filename.endswith('.cif'):
command = f"python {tool_relative_path}/analyze_voronoi_nodes.py {filename} -i {tool_relative_path}/{anion}.yaml > {filename}.txt\n"
sh_file.write(command)
for filename in cif_files:
# --- 修改开始: 输出重定向到 log.txt ---
command = f"python {tool_relative_path}/analyze_voronoi_nodes.py {filename} -i {tool_relative_path}/{anion}.yaml > log.txt\n"
# --- 修改结束 ---
sh_file.write(command)
# 将此脚本添加到收集器中
# 计算相对于基础文件夹的路径
rel_path = os.path.relpath(folder_path, base_folder)
analyze_sh_paths.append(rel_path)
print(f"生成脚本: {sh_file_path} (工具路径: {tool_relative_path})")
print(f"生成脚本: {sh_file_path} (工具路径: {tool_relative_path}, Anion: {anion})")
# 获取子文件夹列表
subdirs = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d))]
@@ -105,7 +68,7 @@ def create_sh_recursive(base_folder, tool_path="tool", relative_depth=2):
elements = folder_name.split("+")
for element in elements:
element_dir = os.path.join(folder_path, element)
# 如果对应元素的子文件夹不存在创建它
# 如果对应元素的子文件夹不存在,创建它
if not os.path.exists(element_dir):
os.makedirs(element_dir)
print(f"创建子文件夹: {element_dir}")
@@ -144,13 +107,8 @@ def create_sh_recursive(base_folder, tool_path="tool", relative_depth=2):
# 修改权限使脚本可执行
os.chmod(sh_all_path, 0o755)
print(f"生成总执行脚本: {sh_all_path}")
print("所有脚本生成完成")
# 示例调用
# create_sh_recursive("../data/after_step1")
print("所有脚本生成完成!")
if __name__ == '__main__':
# creat_sh("../data/after_step1/O","O","../data/after_step1/O/analyze.sh")
# creat_sh("../data/after_step1/S","S","../data/after_step1/S/analyze.sh")
# creat_sh("../data/after_step1/Cl","Cl","../data/after_step1/Cl/analyze.sh")
# creat_sh("../data/after_step1/Br","Br","../data/after_step1/Br/analyze.sh")
create_sh_recursive("../data/after_step1")