0

I am sitting in front of this problem for the last 2 hours and can't figure out what the problem is. I have a function which calculates something based on csv input. When I print the "spalte_ideal_sammlung" out of the function it returns the correct array: ['y36', 'y11', 'y2', 'y33'] but when I try to call these functions via my unittest I get this array as a return from the function: ['y36', 'y11', 'y2', 'y33', 'y36', 'y11', 'y2', 'y33']. Any idea why I get this return instead of the correct one with 4 strings inside? The variable spalte_ideal_sammlung has been defined as global in my main function in the main.py.

def main():  # Mittels unsere Main-Funktion rufen wir die definierte Funktion auf


    global spalte_ideal_sammlung #Abspeichern unserer gefundenen idealen Spalten
    spalte_ideal_sammlung = []

My function in my main.py:

class LeastSquare(Berechnung):  # Klasse LeastSquare erbt von Berechnung
    # Überschreibe die Methode least_square_berechnung aus der Basisklasse Berechnung
    def least_square_berechnung(self):
        #pass
        # Initialisiere eine Liste, die später die LS-Werte für die Idealen-Funktionen enthält
        ls_ideal = []
        # Initialisiere eine Liste, welche die Spaltenbezeichnung für die Y-Werte der Trainingsdaten enthält. Die erste Spalte enthält die x-Werte.
        trainingsdaten = ["y1", "y2", "y3", "y4"]
        # Wir starten ab der zweiten Spalte, weil ab hier die y werte liegen. Erste Spalte sind die X-Werte
        for columns_train in training.columns[1:]:
            mse = 9999
            # Durchlaufe alle Spalten der Idealen-Funktionen
            for columns_ideal in ideal.columns[1:]:
                #Berechnung MSE
                mse_zw = (mean_squared_error(training[columns_train], ideal[columns_ideal]))
                if mse_zw < mse:
                    mse = mse_zw
                    spalte_ideal = ideal[columns_ideal].name
            # Gefundene Werte werden ans Array übergeben.
            # Da der MeanSquaredError nicht dem verlangten SquaredError entspricht, überführen wir diesen durch den Faktor 400 in Wert der gesuchten Abweichung.
            ls_ideal.append(mse * len(ideal))
            spalte_ideal_sammlung.append(spalte_ideal)

        for i, y, k in zip(ls_ideal, spalte_ideal_sammlung, trainingsdaten):
            print("Für den Trainingsdatensatz " + str(k) + " lautet der LeastSquare " + str(
                i) + " die Ideale Funktion lautet " + str(y))

        return spalte_ideal_sammlung

I call the function out of my unittes.py (I did not start with the unit test because the return is not what I expected. Its definitely my fault but I can't find the mistake:

import unittest
import main

class MyTestCase(unittest.TestCase):
    def test_something(self):

        instanz1 = main.LeastSquare()
        print(instanz1.least_square_berechnung())
        


if __name__ == '__main__':
    unittest.main()

Really appreciate any help!

6
  • 1
    seems you are creating a new instance of LeastSquare try passing the instance of LeastSquare to your unit test self.assertEqual(instanz1.least_square_berechnung(), ['y36', 'y11', 'y2', 'y33']) Commented Dec 24, 2022 at 14:28
  • @Samuel yes but when i print the instanz1.least_square_berechnung it returns the "wrong" array with 8 strings inside. Commented Dec 24, 2022 at 14:32
  • 1
    could be problem with calling the list more than once which causes it to append more than once in your instance you could add this and check if it works main.spalte_ideal_sammlung = [] self.assertEqual(instanz1.least_square_berechnung(), ['y36', 'y11', 'y2', 'y33']) Commented Dec 24, 2022 at 14:36
  • @Samuel you are right! That definetly works! Thanks a lot :-) Commented Dec 24, 2022 at 14:53
  • 1
    il rewrite in answer so you can accept :D Commented Dec 24, 2022 at 16:02

1 Answer 1

1

could be problem with calling the list more than once which causes it to append more than once in your instance you could add this and check if it works

main.spalte_ideal_sammlung = []
self.assertEqual(instanz1.least_square_berechnung(), ['y36', 'y11', 'y2', 'y33'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.