from loguru import logger import re import numpy as np import pandas as pd def…
- User
from loguru import logger import re import numpy as np import pandas as pd
def pcd2array(path: str): """ convert pcd file to array data :param path: the save path of pcd file """ with open(path, "rb") as f: res = f.read()
key = b'binary' if key not in res: logger.error("not found key value(ascii/binary) in header, please check header!!!") return [], [] header = (res.split(key)[0] + key).decode("utf-8") header_keys = ["FIELDS", "SIZE", "TYPE", "POINTS"] for header_key in header_keys: if header_key not in header: logger.error(f"{header_key} not in pcd file!!!") return [], [] # get field info from header field = re.search(r'FIELDS\s(.+)', header).group(1).strip().split() # get size and type info from header size = re.search(r'SIZE\s(.+)', header).group(1).strip().split() size = np.array(size, dtype=np.int32) type = re.search(r'TYPE\s(.+)', header).group(1).strip().lower().split() # DATA binary \n\n xxxxx res = res[len(header):].lstrip(b' ')[1:] if len(res) == 0: logger.error("pcd binary file is abnormal, only find header, please check!!!") return [], field dt_point_list = [] for i in range(len(type)): dt_point_list.append(f'{type[i]}{str(size[i])}') dt_point = { "names": field, "formats": dt_point_list[:], } pcd_dataframe = pd.DataFrame(np.frombuffer(res, dtype=dt_point)).astype(float) return pcd_dataframe.drop(pcd_dataframe.columns[-1], axis=1)master = pcd2array("./2025-06-20-15-10_first.pcd") slave = pcd2array("./2025-06-20-15-10_second.pcd") slave_id = slave.loc[1, "frame_id"] master = master[master["frame_id"] == slave_id] master.reset_index(drop=True, inplace=True) print(slave.equals(master)) 如果不同,增加不同的打印
Assistant
我来帮你修改代码,在比较两个DataFrame不同时打印出具体的差异信息:
from loguru import logger import re import numpy as np import pandas as pd def pcd2array(path: str): """ convert pcd file to array data :param path: the save path of pcd file """ with open(path, "rb") as f: res = f.read() key = b'binary' if key not in res: logger.error("not found key value(ascii/binary) in header, please check header!!!") return [], [] header = (res.split(key)[0] + key).decode("utf-8") header_keys = ["FIELDS", "SIZE", "TYPE", "POINTS"] for header_key in header_keys: if header_key not in header: logger.error(f"{header_key} not in pcd file!!!") return [], [] # get field info from header field = re.search(r'FIELDS\s(.+)', header).group(1).strip().split() # get size and type info from header size = re.search(r'SIZE\s(.+)', header).group(1).strip().split() size = np.array(size, dtype=np.int32) type = re.search(r'TYPE\s(.+)', header).group(1).strip().lower().split() # DATA binary \n\n xxxxx res = res[len(header):].lstrip(b' ')[1:] if len(res) == 0: logger.error("pcd binary file is abnormal, only find header, please check!!!") return [], field dt_point_list = [] for i in range(len(type)): dt_point_list.append(f'{type[i]}{str(size[i])}') dt_point = { "names": field, "formats": dt_point_list[:], } pcd_dataframe = pd.DataFrame(np.frombuffer(res, dtype=dt_point)).astype(float) return pcd_dataframe.drop(pcd_dataframe.columns[-1], axis=1) master = pcd2array("./2025-06-20-15-10_first.pcd") slave = pcd2array("./2025-06-20-15-10_second.pcd") slave_id = slave.loc[1, "frame_id"] master = master[master["frame_id"] == slave_id] master.reset_index(drop=True, inplace=True) # 比较两个DataFrame if slave.equals(master): print("两个DataFrame完全相同") else: print("两个DataFrame不同,差异如下:") # 检查形状是否相同 if slave.shape != master.shape: print(f"形状不同 - slave: {slave.shape}, master: {master.shape}") # 检查列是否相同 if not slave.columns.equals(master.columns): print(f"列不同:") print(f" slave列: {list(slave.columns)}") print(f" master列: {list(master.columns)}") print(f" 只在slave中的列: {set(slave.columns) - set(master.columns)}") print(f" 只在master中的列: {set(master.columns) - set(slave.columns)}") # 如果形状和列都相同,比较具体的值 if slave.shape == master.shape and slave.columns.equals(master.columns): # 找出不同的位置 diff_mask = (slave != master) | (slave.isna() != master.isna()) # 统计每列的差异数量 diff_counts = diff_mask.sum() if diff_counts.any(): print("\n每列的差异数量:") for col in diff_counts[diff_counts > 0].index: print(f" {col}: {diff_counts[col]} 个差异") # 显示前几个差异的具体值 print("\n前10个差异的具体值:") diff_indices = np.where(diff_mask.any(axis=1))[0][:10] for idx in diff_indices: print(f"\n行索引 {idx}:") for col in slave.columns: if diff_mask.loc[idx, col]: print(f" {col}: slave={slave.loc[idx, col]}, master={master.loc[idx, col]}") if len(diff_indices) < len(np.where(diff_mask.any(axis=1))[0]): print(f"\n... 还有 {len(np.where(diff_mask.any(axis=1))[0]) - len(diff_indices)} 行存在差异")这个修改后的代码会在两个DataFrame不同时提供详细的差异信息,包括:
- 形状是否相同
- 列名是否相同,以及具体的差异
- 每列有多少个不同的值
- 前10个差异的具体位置和值
- 总共有多少行存在差异
这样你就能清楚地看到两个PCD文件在过滤后的具体差异了。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.