Skip to content Skip to sidebar Skip to footer

Subprogram Which Takes All The Predefined Variables From A "main Program"

i have a hard time with this: As i am programming GUIs with pyqt i want to structure my work: I have several buttons on my GUI that should call 'different subprograms' from my calc

Solution 1:

You are correct that classes are a good way to structure your code.

A class can maintain its own state, and has pre-defined behaviour that can be manipulated through methods and properties.

However, I am not going to give general advice about using classes, because that is off-topic for stackoverflow, which focuses on specific programming problems. If you want to know more, just do a web-search for python books/tutorials on the subject - there are dozens of good ones out there.

Instead, I will do my best to re-structure the code in your question to use a class. The code below is for illustration purposes only. It is not meant to be a complete, runnable example. Hopefully there are enough hints there to give you an idea of how to proceed:

gui.py:

import numpy as np
import sk_calc

classMyDia(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)

    defonPRED(self):
        if self.button_1.isChecked():
            a = 1elif self.button_2.isChecked():
            a = 2else:
            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'classCalc(object):
    def__init__(self, a, csvpath=None):
        if csvpath isNone:
            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)

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

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

Post a Comment for "Subprogram Which Takes All The Predefined Variables From A "main Program""