如何防止AssertionError停止Python代码?

如何防止AssertionError停止Python代码?,python,fuzzy,skfuzzy,Python,Fuzzy,Skfuzzy,我想做一个使用自动规则模糊控制器的系统,从过程真实数据中创建规则。 我的问题是,当我使用新数据模拟模糊控制器,并从旧数据中提取规则时,我得到一个关于清晰输出的错误,因为没有足够的规则,所以无法计算,这是完全正常的,因为我的模糊系统需要更多的规则,这就是我的观点!我想实现一个新的例程来分析清晰的输入/输出,并从这些数据中创建新的规则,之后,我想返回代码并再次模拟 是否有一个函数阻止AssertionError停止代码并重定向到另一个def或以前的代码行 我试图找到一些lib函数,该函数允许我重定向

我想做一个使用自动规则模糊控制器的系统,从过程真实数据中创建规则。 我的问题是,当我使用新数据模拟模糊控制器,并从旧数据中提取规则时,我得到一个关于清晰输出的错误,因为没有足够的规则,所以无法计算,这是完全正常的,因为我的模糊系统需要更多的规则,这就是我的观点!我想实现一个新的例程来分析清晰的输入/输出,并从这些数据中创建新的规则,之后,我想返回代码并再次模拟

是否有一个函数阻止AssertionError停止代码并重定向到另一个def或以前的代码行

我试图找到一些lib函数,该函数允许我重定向错误以停止代码,但没有成功。我想我将不得不改变skfuzzy defuzz def代码,以允许它

多谢各位

''蟒蛇


将引发
ValueError
的代码行包装在
try
中。并决定在其
子句中除了ValueError:
之外要做什么。也许
继续
-继续下一次迭代可能是合理的。

请分享所有相关代码/数据,并澄清您的问题/问题。看,我明白了!我试着用try来包装ValueError的行,但是我没有工作。我发现:~\Anaconda3\lib\site packages\skfuzzy\control\controlsystem.py在defuzz(self)587 self.var.defuzzify_method)588中除了AssertionError:->589 try:590 new_c1=[]591 new_c2=[]value错误:无法计算清晰的输出,可能是因为系统太稀疏了。检查以确保这组输入值将通过当前规则集激活每个先行项中的至少一个连接项。
step = stp_ini
k = 0
delay = stp_ini
end = stp_fim - stp_ini
Tout_sim = pd.DataFrame(columns=['val'])
Vent_sim = pd.DataFrame(columns=['val'])

start = timeit.default_timer()

for step in range(end-k):
    clear_output(wait=True)
    simulation.input['PCA1'] = comp1n[step+delay]
    simulation.input['PCA2'] = comp2n[step+delay]
    simulation.input['PCA3'] = comp3n[step+delay]
    simulation.input['PCA4'] = comp4n[step+delay]
    simulation.input['Vent'] = dataoutf.NumVentOn[step+delay]
    simulation.compute()
    Tout_sim = Tout_sim.append({'val':simulation.output['Tout']},ignore_index=True)

    stop = timeit.default_timer()
    if ((step/(stp_fim-k-1))*100) < 5:
        expected_time = "Calculating..."
    else:
        time_perc = timeit.default_timer()
        expected_time = np.round( ( (time_perc-start)/(step/(end-k-1)) )/60,2)
def defuzz(self):
    """Derive crisp value based on membership of adjective(s)."""
    if not self.sim._array_inputs:
        ups_universe, output_mf, cut_mfs = self.find_memberships()

        if len(cut_mfs) == 0:
            raise ValueError("No terms have memberships.  Make sure you "
                             "have at least one rule connected to this "
                             "variable and have run the rules calculation.")

        try:
            return defuzz(ups_universe, output_mf,
                          self.var.defuzzify_method)
        except AssertionError:
            try:
                new_c1 = []
                new_c2 = []
                new_c3 = []
                new_c4 = []
                new_vent = []
                new_tout = []
                newcondition1 = []
                newcondition2 = []
                newcondition3 = []
                newcondition4 = []
                newcondition5 = []
                newcondition6 = []

                #input
                n = 0
                for n in range(len(namespca)):
                    new_c1.append(fuzz.interp_membership(PCA1.universe, PCA1[namespcapd.name.loc[n]].mf, comp1n[step]))
                    new_c2.append(fuzz.interp_membership(PCA2.universe, PCA2[namespcapd.name.loc[n]].mf, comp2n[step]))
                    new_c3.append(fuzz.interp_membership(PCA3.universe, PCA3[namespcapd.name.loc[n]].mf, comp3n[step]))
                    new_c4.append(fuzz.interp_membership(PCA4.universe, PCA4[namespcapd.name.loc[n]].mf, comp4n[step]))

                n = 0
                for n in range(len(namesvent)):    
                    new_vent.append(fuzz.interp_membership(Vent.universe, Vent[namesventpd.name.loc[n]].mf, dataoutf.NumVentOn[step]))

                #output
                n = 0
                for n in range(len(namestemp)):
                    new_tout.append(fuzz.interp_membership(Tout.universe, Tout[namestemppd.name.loc[n]].mf, dataoutf.TsaidaHT[step]))

                #new_c1 = np.transpose(new_c1)
                new_c1_conv = pd.DataFrame(new_c1)
                #new_c2 = np.transpose(new_c2)
                new_c2_conv = pd.DataFrame(new_c2)
                #new_c3 = np.transpose(new_c3)
                new_c3_conv = pd.DataFrame(new_c3)
                #new_c4 = np.transpose(new_c4)
                new_c4_conv = pd.DataFrame(new_c4)
                #new_vent = np.transpose(new_vent)
                new_vent_conv = pd.DataFrame(new_vent)
                #new_tout = np.transpose(new_tout)
                new_tout_conv = pd.DataFrame(new_tout)

                i=0
                for i in range(pcamf):
                    newcondition1.append([new_c1_conv.idxmax(axis=0) == i])
                    newcondition2.append([new_c2_conv.idxmax(axis=0) == i])
                    newcondition3.append([new_c3_conv.idxmax(axis=0) == i])
                    newcondition4.append([new_c4_conv.idxmax(axis=0) == i])

                i=0
                for i in range(ventmf):
                    newcondition5.append([new_vent_conv.idxmax(axis=0) == i])

                i=0
                for i in range(tempmf):
                    newcondition6.append([new_tout_conv.idxmax(axis=0) == i])

                choicelistpca = namespca
                choicelistvent = namesvent
                choicelisttout = namestemp

                new_c1_rules = np.select(newcondition1, choicelistpca)
                new_c2_rules = np.select(newcondition2, choicelistpca)
                new_c3_rules = np.select(newcondition3, choicelistpca)
                new_c4_rules = np.select(newcondition4, choicelistpca)
                new_vent_rules = np.select(newcondition5, choicelistvent)
                new_tout_rules = np.select(newcondition6, choicelisttout)

                new_rules = np.vstack([new_c1_rules,new_c2_rules,new_c3_rules,new_c4_rules,new_vent_rules,new_tout_rules])
                new_rules = new_rules.T

                new_rulespd = pd.DataFrame(new_rules,columns=['PCA1','PCA2','PCA3','PCA4','Vent','Tout'])

                #Checar se a nova regra está dentro do conjunto de regras fuzzy atual
                if pd.merge(new_rulespd,AutoRules, on=['PCA1','PCA2','PCA3','PCA4','Vent','Tout'],how='inner').empty:
                    print('Nova regra não encontrada no conjunto atual de regras fuzzy!')
                else:
                    pd.merge(new_rulespd,AutoRules, on=['PCA1','PCA2','PCA3','PCA4','Vent','Tout'],how='inner')

        """except AssertionError:
            raise ValueError("Crisp output cannot be calculated, likely "
                             "because the system is too sparse. Check to "
                             "make sure this set of input values will "
                             "activate at least one connected Term in each "
                             "Antecedent via the current set of Rules.")"""
    else:
        # Calculate using array-aware version, one cut at a time.
        output = np.zeros(self.sim._array_shape, dtype=np.float64)

        it = np.nditer(output, ['multi_index'], [['writeonly', 'allocate']])

        for out in it:
            universe, mf = self.find_memberships_nd(it.multi_index)
            out[...] = defuzz(universe, mf, self.var.defuzzify_method)

        return output