如何在try循环中标记不满足语句的条目

2024-10-05 13:16:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力确定涨跌幅度;信号的下降时间,如下所示:PMT Signal

目标是找到信号的下降和上升时间。我尝试的是在信号的平坦部分指定90%的电平,并指定0-10%作为信号的最小值。以下是我尝试过的:

def下降时间(自身、通道、校准通道数据):

    try:
        up_limit = np.where(calChannel_data >= np.percentile(calChannel_data, 90, axis = 0) )
        low_limit = np.where(calChannel_data == np.min(calChannel_data))
        midpoint = np.where( np.logical_and(calChannel_data >= np.percentile(calChannel_data, 45, axis = 0), \
                                        calChannel_data <= np.percentile(calChannel_data, 55, axis = 0)))
        before_fall =up_limit[0][ np.where(up_limit[0] <= low_limit[0][0])]
        after_fall = up_limit[0][np.where(up_limit[0]>= low_limit[0][0])]
    except IndexError:
        print("the criteria was not met") 
    else:
        return  ch, up_limit[0], low_limit[0][0], midpoint[0], before_fall, after_fall
    finally: 
        print("the end")

但是,它并不能完全工作,因为有部分信号不满足try中的条件:部分代码。有没有办法标记任何不满足条件的实例并阻止其停止


Tags: data信号npwherelowlimituptry
1条回答
网友
1楼 · 发布于 2024-10-05 13:16:28

你总是可以嵌套try,比如

try:
    try:
        up_limit = np.where(calChannel_data >= np.percentile(calChannel_data, 90, axis = 0) )
    except:
       up_limit = "Failed" # or whatever
    # ... similarly for the others
    low_limit = np.where(calChannel_data == np.min(calChannel_data))
    midpoint = np.where( np.logical_and(calChannel_data >= np.percentile(calChannel_data, 45, axis = 0), \
                                    calChannel_data <= np.percentile(calChannel_data, 55, axis = 0)))
    before_fall =up_limit[0][ np.where(up_limit[0] <= low_limit[0][0])]
    after_fall = up_limit[0][np.where(up_limit[0]>= low_limit[0][0])]
except IndexError:
    print("the criteria was not met") 
else:
    return  ch, up_limit[0], low_limit[0][0], midpoint[0], before_fall, after_fall
finally: 
    print("the end")

相关问题 更多 >

    热门问题