基于asammdf 删除MDF中的指定group数据

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.*"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值