from pathlib import Path
import logging
import re
import yaml
from asammdf import MDF
# ===========================
# Read config
# ===========================
base_dir = Path(__file__).resolve().parent
config_file = base_dir / "config.yaml"
with config_file.open(encoding="utf-8") as f:
cfg = yaml.safe_load(f)
def resolve_path(value):
path = Path(value)
return path if path.is_absolute() else base_dir / path
input_file = resolve_path(cfg["input_file"])
output_file = resolve_path(cfg["output_file"])
log_file = resolve_path(cfg["log_file"])
flt = cfg["filter"]
logging.basicConfig(
level=logging.INFO,
filename=str(log_file),
filemode="w",
format="%(asctime)s %(message)s",
)
logger = logging.getLogger(__name__)
# ===========================
# Read MDF
# ===========================
if not input_file.is_file():
raise FileNotFoundError(f"Input MF4 file does not exist: {input_file}")
output_file.parent.mkdir(parents=True, exist_ok=True)
src = MDF(input_file)
logger.info("Input File : %s", input_file)
# ===========================
# Build group blacklist
# ===========================
remove_groups = set()
if flt["enable_group_filter"]:
for begin, end in flt.get("remove_group_range") or []:
remove_groups.update(range(begin, end + 1))
remove_groups.update(flt.get("remove_groups") or [])
# ===========================
# Filter groups without rebuilding signals
# ===========================
kept_groups = []
for group_index, group in enumerate(src.groups):
cg = group.channel_group
acq_name = getattr(cg, "acq_name", "") or ""
if isinstance(acq_name, bytes):
acq_name = acq_name.decode(errors="ignore")
remove = False
reason = ""
# --------------------
# group id
# --------------------
if group_index in remove_groups:
remove = True
reason = "Group ID"
# --------------------
# acq contains
# --------------------
if (
not remove
and flt["enable_acq_filter"]
):
for s in flt.get("remove_acq_contains") or []:
if s in acq_name:
remove = True
reason = f"Contains [{s}]"
break
# --------------------
# regex
# --------------------
if (
not remove
and flt["enable_acq_filter"]
):
for pattern in flt.get("remove_acq_regex") or []:
if re.search(pattern, acq_name):
remove = True
reason = f"Regex [{pattern}]"
break
# --------------------
if remove:
logger.info(
"REMOVE Group %-4d %s (%s)",
group_index,
acq_name,
reason,
)
continue
logger.info(
"KEEP Group %-4d %s",
group_index,
acq_name,
)
kept_groups.append(group)
src.groups = kept_groups
# ===========================
src.save(str(output_file), overwrite=True)
logger.info("Done. Output File: %s", output_file)
print(f"Done. Output file: {output_file}")
配置文件yaml
input_file: D:\TEST.MF4
output_file: D:\filtered.MF4
log_file: filter.log
filter:
# 是否启用Group ID过滤
enable_group_filter: true
remove_group_range:
- [319,391]
remove_groups:
# - 500
# - 501
# 是否启用Acq Name过滤
enable_acq_filter: false
remove_acq_contains:
- "TEMP_DEBUG"
remove_acq_regex:
- "^on signal.*"

1058

被折叠的 条评论
为什么被折叠?



