You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PU电压检测代码,排除0.0值仅识别0.01-0.8异常值?

解决方案

问题背景

现有Python代码扫描文档PU电压值时,会误将0.0空值判定为低于0.8PU的异常值,导致错误触发失败提示。需要修改代码排除0.0值,仅检测0.01至0.8之间的电压异常。

修改后的代码

timeForVoltageRecovery = 11.0    
acceptableVoltagePU = 0.8
# 新增下限阈值,明确排除0.0空值
minimumVoltagePU = 0.01

for channel in data:
    if float(channel['time']) > timeForVoltageRecovery:
        isVoltageGood = True
        keys = channel.keys()
        keys.sort()
        volts = {}
        for key in keys:
            words = key.split(" ")
            if words[0] == "VOLT":
                volt_value = float(channel[key])
                # 过滤掉0.0的空值,只保留有效电压数据
                if volt_value != 0.0:
                    volts[volt_value] = key.strip()

        voltsKeys = sorted(volts.keys())
        # 取最低10个有效电压,若不足10个则取全部
        check_count = min(10, len(voltsKeys))
        for k in range(check_count):
            current_volt = voltsKeys[k]
            # 仅当电压处于0.01到0.8区间时,判定为异常
            if minimumVoltagePU <= current_volt < acceptableVoltagePU:
                isVoltageGood = False

        if isVoltageGood:
            outfile.write("\nAt time %s, this scenario passed voltage recovery criteria of 0.8 PU!\n" % (channel['time']))
        else:
            outfile.write("\n\t***** BAD: At Time %s, this scenario failed voltage recovery criteria of .8 PU :{ *****\n\n" % (channel['time']))
            outfile.write("the lowest 10 voltages (excluding 0.0) in order are:\n")
            for k in range(check_count):
                outfile.write(f"\t{volts[voltsKeys[k]]} -> {voltsKeys[k]:.2f}\n")               

        outfile.write("\n")
        break

关键修改点

  • 新增下限阈值:定义minimumVoltagePU = 0.01,明确异常值的范围边界
  • 过滤空值:在收集电压数据时直接排除0.0值,避免后续无效判断
  • 调整异常判定逻辑:仅当电压值满足0.01 ≤ 电压 < 0.8时,才标记为异常
  • 处理边界情况:当有效电压不足10个时,自动取全部有效数据进行检查和输出,避免索引越界错误

内容的提问来源于stack exchange,提问作者JMFcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 03:42:50