diff --git a/Screen/process_txt.py b/Screen/process_txt.py new file mode 100644 index 0000000..4b67fe2 --- /dev/null +++ b/Screen/process_txt.py @@ -0,0 +1,95 @@ +# -*- coding: utf-8 -*- + +import os +import re +import sys +import csv + + +def extract_data_from_folder(folder_path): + """ + 读取文件夹中所有txt文件,提取指定信息,并写入一个CSV文件。 + (已修正正则表达式) + + :param folder_path: 包含txt文件的文件夹路径。 + """ + # 检查文件夹是否存在 + if not os.path.isdir(folder_path): + print(f"错误:文件夹 '{folder_path}' 不存在。") + return + + # 定义要提取数据的正则表达式 + # 1. 对应 "Percolation diameter (A): 1.234" + pattern1 = re.compile(r"Percolation diameter \(A\): ([\d\.]+)") + + # 2. 对应 "the minium of d 1.23 #" (已根据您的反馈修正) + pattern2 = re.compile(r"the minium of d\s*([\d\.]+)\s*#") + + # 3. 对应 "Maximum node length detected: 5.67 A" + pattern3 = re.compile(r"Maximum node length detected: ([\d\.]+) A") + + # 存储所有提取到的数据 + all_data = [] + + # 遍历文件夹中的所有文件,使用 sorted() 确保处理顺序一致 + for filename in sorted(os.listdir(folder_path)): + if filename.endswith(".txt"): + txt_path = os.path.join(folder_path, filename) + + try: + with open(txt_path, 'r', encoding='utf-8') as file: + content = file.read() + + # 使用修正后的正则表达式查找数据 + match1 = pattern1.search(content) + match2 = pattern2.search(content) + match3 = pattern3.search(content) + + # 提取匹配到的值,如果未匹配到则为空字符串 '' + val1 = match1.group(1) if match1 else '' + val2 = match2.group(1) if match2 else '' + val3 = match3.group(1) if match3 else '' + + # 获取文件名(不含.txt后缀) + base_filename = os.path.splitext(filename)[0] + + # 将这一行的数据添加到总列表中 + all_data.append([base_filename, val1, val2, val3]) + + except Exception as e: + print(f"处理文件 {filename} 时发生错误: {e}") + + # 如果没有找到任何txt文件或数据,则不创建csv + if not all_data: + print("未在文件夹中找到任何 .txt 文件或未能提取任何数据。") + return + + # 根据文件夹名确定CSV文件名 + folder_name = os.path.basename(os.path.normpath(folder_path)) + csv_filename = f"{folder_name}.csv" + + # 写入CSV文件 + try: + with open(csv_filename, 'w', newline='', encoding='utf-8') as csvfile: + writer = csv.writer(csvfile) + + # 写入表头 + headers = ['filename', 'Percolation_diameter_A', 'minium_of_d', 'Max_node_length_A'] + writer.writerow(headers) + + # 写入所有数据 + writer.writerows(all_data) + + print(f"数据成功写入到文件: {csv_filename}") + + except Exception as e: + print(f"写入CSV文件 {csv_filename} 时发生错误: {e}") + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("用法: python your_script_name.py ") + sys.exit(1) + + input_folder = sys.argv[1] + extract_data_from_folder(input_folder) diff --git a/softBV/run_softBV.py b/softBV/run_softBV.py new file mode 100644 index 0000000..8770add --- /dev/null +++ b/softBV/run_softBV.py @@ -0,0 +1,158 @@ +import os +import sys +import subprocess +import re +import csv +from pathlib import Path + +# --- 配置区 --- +# softBV.x 可执行文件的路径,请根据你的实际情况修改 +# 使用 Path.home() 来自动获取用户家目录,比 '~' 更可靠 +SOFTBV_EXECUTABLE = Path.home() / "tool/softBV-GUI_linux/bin/softBV.x" + +# 固定的命令参数 +MOBILE_ION = "Li" +ION_VALENCE = "1" +SF_VALUE = "1000" + +# 输出CSV文件的名称 +OUTPUT_CSV_FILE = "conductivity_results.csv" + + +# --- 配置区结束 --- + +def check_executable(): + """检查 softBV.x 文件是否存在且可执行""" + if not SOFTBV_EXECUTABLE.is_file(): + print(f"错误: 可执行文件未找到: {SOFTBV_EXECUTABLE}") + print("请检查脚本中的 SOFTBV_EXECUTABLE 路径是否正确。") + sys.exit(1) + if not os.access(SOFTBV_EXECUTABLE, os.X_OK): + print(f"错误: 文件存在但不可执行: {SOFTBV_EXECUTABLE}") + print("请使用 'chmod +x' 命令赋予其执行权限。") + sys.exit(1) + + +def parse_conductivity(output_text): + """ + 从 softBV.x 的输出文本中解析电导率(conductivity)。 + 使用正则表达式来匹配 'MD: conductivity = ' 格式的行。 + """ + # 正则表达式解释: + # ^ - 匹配行首 + # \s* - 匹配任意数量的空白字符 + # MD: conductivity - 匹配字面字符串 + # \s*=\s* - 匹配等号,前后可以有任意空白 + # ([-\d.eE+]+) - 捕获组1: 匹配并捕获一个或多个数字、点、'e'/'E'、'+'/'-' (科学计数法) + pattern = re.compile(r"^\s*MD: conductivity\s*=\s*([-\d.eE+]+)", re.MULTILINE) + match = pattern.search(output_text) + + if match: + # 返回捕获到的第一个组 (也就是数值部分) + return match.group(1) + else: + # 如果没有找到匹配的行,返回 None + return None + + +def main(): + """主函数""" + # 1. 检查命令行参数 + if len(sys.argv) != 2: + print(f"用法: python {sys.argv[0]} ") + print(f"例如: python {sys.argv[0]} Br") + sys.exit(1) + + target_folder = Path(sys.argv[1]) + + # 检查目标文件夹是否存在 + if not target_folder.is_dir(): + print(f"错误: 文件夹 '{target_folder}' 不存在。") + sys.exit(1) + + # 检查 softBV.x 是否配置正确 + check_executable() + + # 2. 查找所有 .cif 文件 + cif_files = sorted(list(target_folder.glob("*.cif"))) + + if not cif_files: + print(f"警告: 在文件夹 '{target_folder}' 中没有找到任何 .cif 文件。") + return + + print(f"在 '{target_folder}' 中找到 {len(cif_files)} 个 .cif 文件,开始处理...") + + results = [] # 用于存储 (文件名, 电导率) + + # 3. 遍历文件,执行命令并提取数据 + for cif_file in cif_files: + print(f" -> 正在处理: {cif_file} ...", end="", flush=True) + + # 构建命令 + command = [ + str(SOFTBV_EXECUTABLE), + "--md", + str(cif_file), + MOBILE_ION, + ION_VALENCE, + SF_VALUE + ] + + try: + # 执行命令并捕获输出 + # capture_output=True 将 stdout 和 stderr 分别捕获 + # text=True (或 encoding='utf-8') 将输出解码为字符串 + # check=True 如果命令返回非零退出码 (表示错误),则会抛出异常 + process_result = subprocess.run( + command, + capture_output=True, + text=True, + check=True, + timeout=300 # 设置一个超时时间(例如300秒),防止程序卡死 + ) + + # 从标准输出中解析数据 + conductivity = parse_conductivity(process_result.stdout) + + if conductivity is not None: + results.append((cif_file.name, conductivity)) + print(f" 成功, conductivity = {conductivity}") + else: + print(" 失败 (无法在输出中找到conductivity)") + # 你也可以选择将错误信息记录下来 + # results.append((cif_file.name, "Error: Not Found")) + + except subprocess.CalledProcessError as e: + # 命令执行失败 + print(f" 失败 (命令执行错误)") + print(f" 错误信息: {e.stderr.strip()}") + # results.append((cif_file.name, "Error: Execution Failed")) + except subprocess.TimeoutExpired: + print(f" 失败 (命令执行超时)") + # results.append((cif_file.name, "Error: Timeout")) + except Exception as e: + # 其他未知错误 + print(f" 失败 (发生未知错误: {e})") + # results.append((cif_file.name, f"Error: {e}")) + + # 4. 将结果写入CSV文件 + if not results: + print("没有成功处理任何文件,不生成CSV。") + return + + print(f"\n处理完成,正在将 {len(results)} 条结果写入 {OUTPUT_CSV_FILE} ...") + + try: + with open(OUTPUT_CSV_FILE, 'w', newline='', encoding='utf-8') as csvfile: + writer = csv.writer(csvfile) + # 写入表头 + writer.writerow(['filename', 'conductivity']) + # 写入数据 + writer.writerows(results) + print("CSV文件已成功生成!") + except IOError as e: + print(f"错误: 无法写入CSV文件: {e}") + + +if __name__ == "__main__": + main() \ No newline at end of file