从“主程序”获取所有预定义变量的子程序

时间:2013-12-11 15:49:21

标签: python function pyqt scikit-learn

我很难用这个: 因为我用pyqt编程GUI我想构建我的工作:

我的GUI上有几个按钮,可以通过scikitlearn从我的计算中调用“不同的子程序”。

我有一个用于预测的按钮“PRED”,另一个用于某些名为“PLOT”的图表

当点击这些按钮时,用

调用python“计算程序”
class MyDia(QtGui.QDialog, Dlg): 
    def __init__(self): 
        QtGui.QDialog.__init__(self) 
        self.setupUi(self)

        self.connect(self.buttonOPLOT, 
                QtCore.SIGNAL("clicked()"), self.onPLOT)    
        self.connect(self.buttonPRED, 
                QtCore.SIGNAL("clicked()"), self.onPRED)
    def onPRED
        if self.button_1.checkState(): 
            a=1
        if self.button_2.checkState(): 
            a=2
        query=np.zeros((1,18))
        for i in range(0,18,1):
            try:
                query[0,i]= float(self.tableWidget.item(0,i).text())

        ### when user has made his choices the data goes do this
        from sk_calc import main, pred
        main() #after main, "pred" should be called with some definitions that 
        have been made in "main"
        pred(a) #a is some parameter of a regression (i try to keep it easy)

目前我在不同的文件中使用不同的“计算”程序“sk_plot和sk_pred” - 目标是仅更改一个...其中“main”在特定作业之前运行(PRED或PLOT ...)< / p>

独特的计算程序应该“看起来”/结构类似于:

def main():
    import numpy as np
    import #all modules from scikitlearn

    DATA=np.genfromtxt(direc+"\some.csv",delimiter=";",dtype=float ,skip_header=2, usecols=range(0,22)) #reading in a csv file with my data

    features=DATA[:,4:22]#the "X" of my DATA
    targets=DATA[:,1]#the "Y" of my DATA

    svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a) #Regression using the DATA #a comes from user click
    svr_rbf.fit(features, targets).predict(features)# method of scikit-learn

        def pred():
            Prediction=svr_rbf.predict(query)
            #query is defined by the user in the gui typing in some values 
            print(Pred_ic)

        def plot():
            #... something different using pylab but ALSO DATA features and targets

你看,我想要一些代码(主要)运行单独点击whick按钮 之后,应执行“计算程序”的一部分,其中包含变量和在main()中定义的数据。

我是否为此使用课程?如果是的话,我必须记住什么?这有什么步骤......

1 个答案:

答案 0 :(得分:1)

你是正确的,类是构建代码的好方法。

一个类可以维护自己的状态,并且具有可以通过方法和属性操作的预定义行为。

但是,我不打算提供有关使用类的一般建议,因为这是stackoverflow的主题,它专注于特定的编程问题。如果你想了解更多,只需要对这个主题的python书籍/教程进行网络搜索 - 那里有很多好的。

相反,我会尽力重新构建您的问题中的代码以使用类。以下代码仅用于插图意味着是一个完整的,可运行的示例。希望有足够的提示让您了解如何继续:

<强> gui.py

import numpy as np
import sk_calc

class MyDia(QtGui.QDialog, Dlg):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.buttonOPLOT.clicked.connect(self.onPLOT)
        self.buttonPRED.clicked.connect(self.onPRED)

    def onPRED(self):
        if self.button_1.isChecked():
            a = 1
        elif self.button_2.isChecked():
            a = 2
        else:
            a = 0
        query = np.zeros((1,18))
        # ... etc

        # when user has made his choices the data goes do this

        # create an instance of the Calc class, passing in
        # parameters from the gui
        calc = sk_calc.Calc(a)

        # call methods of the instance, passing in parameters
        # from the gui, and receiving returned values
        prediction = calc.pred(query)

        # calc.plot() ... etc

<强> sk_calc.py

import numpy as np
from sklearn.svm import SVR
# import other stuff from scikitlearn

DEFAULT_CSVPATH = 'path/to/some/file.csv'

class Calc(object):
    def __init__(self, a, csvpath=None):
        if csvpath is None:
            csvpath = DEFAULT_CSVPATH
        # reading in a csv file with my data
        self.data = np.genfromtxt(
            csvpath , delimiter=';', dtype=float,
            skip_header=2, usecols=range(0,22))

        self.features = data[:,4:22] # the "X" of my DATA
        self.targets = data[:,1]     # the "Y" of my DATA

        # Regression using the DATA, a comes from user click
        self.svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a)

        # method of scikit-learn
        self.svr_rbf.fit(features, targets).predict(features)

    def pred(self, query):
        # query is defined by the user in the gui typing in some values
        prediction = self.svr_rbf.predict(query)
        return prediction

    def plot(self):
        # ... use pylab with DATA features and targets
        # self.data ...
        # self.features ...