Python Forum
Data written in loop not available afterwards
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data written in loop not available afterwards
#1
Greetings folks,

I am still fairly new to python and have a small problem with a function I have written:

    def evaluate_tests(test_results):
        evaluation = {};
        n = 0;
        for tests in test_results:
            test_eval = {};
            test_eval['error'] = [];
            test_eval['incomplete_nodes'] = [];
            test_eval['finished_fbc'] = 0;
            test_eval['finished_mfcn'] = 0;
            sum_fbc_error = 0;
            sum_mfcn_error = 0;
            sum_fbc_in = 0;
            sum_mfcn_in = 0;
            for test_result in tests:
                fbc = test_result['find_best_coverage'];
                mfcn = test_result['max_fully_covered_nodes'];
                print("fbc['incomplete_nodes']");
                print(fbc['incomplete_nodes']);
                print("mfcn['incomplete_nodes']");
                print(mfcn['incomplete_nodes']);
                print("mfcn fbc incomplete notes difference");
                print(abs(len(fbc['incomplete_nodes']) - len(mfcn['incomplete_nodes'])));
                test_eval['error'].append(abs(fbc['errors'] - mfcn['errors']));
                test_eval['incomplete_nodes'].append(abs(len(fbc['incomplete_nodes']) - len(mfcn['incomplete_nodes'])));
                test_eval['finished_fbc'] += 1;
                test_eval['finished_mfcn'] += 1;
                sum_fbc_error += fbc['errors'];
                sum_mfcn_error += mfcn['errors'];
                sum_fbc_in += len(fbc['incomplete_nodes']);
                sum_mfcn_in += len(mfcn['incomplete_nodes']);
                print("error difference fbc mfcn");
                print(test_eval['error']);
                print("test_eval['incomplete_nodes']");
                print(test_eval['incomplete_nodes']);
All initiated variables of the dictionary

test_eval['error'] = [];
test_eval['incomplete_nodes'] = [];
test_eval['finished_fbc'] = 0;
test_eval['finished_mfcn'] = 0;
will be filled with data inside the loop (the print command shows that to me) but are empty outside (or 0) of the loop (as they were initialised), since I initialised them before the loop I actually expected to be available also after the loop finished - can somebody tell me how to fix it (and what I did wrong)?

Thank you very much in advance!
Reply
#2
I assume you mean the first loop? Move them outside the first loop, that shouldn't change anything except that they'll keep their values.
Reply
#3
Actually I meant the second loop - but for some reason half of the function went missing - no idea why...

    def evaluate_tests(test_results):
        evaluation = {};
        n = 0;
        for tests in test_results:
            test_eval = {};
            test_eval['error'] = [];
            test_eval['incomplete_nodes'] = [];
            test_eval['finished_fbc'] = 0;
            test_eval['finished_mfcn'] = 0;
            sum_fbc_error = 0;
            sum_mfcn_error = 0;
            sum_fbc_in = 0;
            sum_mfcn_in = 0;
            for test_result in tests:
                fbc = test_result['find_best_coverage'];
                mfcn = test_result['max_fully_covered_nodes'];
                print("fbc['incomplete_nodes']");
                print(fbc['incomplete_nodes']);
                print("mfcn['incomplete_nodes']");
                print(mfcn['incomplete_nodes']);
                print("mfcn fbc incomplete notes difference");
                print(abs(len(fbc['incomplete_nodes']) - len(mfcn['incomplete_nodes'])));
                test_eval['error'].append(abs(fbc['errors'] - mfcn['errors']));
                test_eval['incomplete_nodes'].append(abs(len(fbc['incomplete_nodes']) - len(mfcn['incomplete_nodes'])));
                test_eval['finished_fbc'] += 1;
                test_eval['finished_mfcn'] += 1;
                sum_fbc_error += fbc['errors'];
                sum_mfcn_error += mfcn['errors'];
                sum_fbc_in += len(fbc['incomplete_nodes']);
                sum_mfcn_in += len(mfcn['incomplete_nodes']);
                print("test_eval['error']");
                print(test_eval['error']);
                print("test_eval['incomplete_nodes']");
                print(test_eval['incomplete_nodes']);

            print("test_eval");
            print(test_eval);
            test_eval['avg_fbc_error'] = sum_fbc_error / len(test_results);
            test_eval['avg_mfcn_error'] = sum_mfcn_error / len(test_results);
            test_eval['avg_fbc_incomplete_nodes'] = sum_fbc_in / len(test_results);
            test_eval['avg_mfcn_incomplete_nodes'] = sum_mfcn_in / len(test_results);
            print("test_eval['error']");
            print(test_eval['error']);
            print("test_eval['incomplete_nodes']");
            print(test_eval['incomplete_nodes']);
            test_eval['avg_error'] = sum(test_eval['error']) / len(test_eval['error']);
            test_eval['avg_incomplete_nodes'] = sum(test_eval['incomplete_nodes']) / len(test_eval['incomplete_nodes']);
            test_eval['finished'] = test_eval['finished_fbc'] + test_eval['finished_mfcn'];
            evaluation[n] = test_eval;
            n += 1;

        return evaluation;
already
print("test_eval");
print(test_eval);
showed me that "test_eval" is after the loop set to the initial values while it is inside the loop extended with values e.g. this is a print output - the end of the output of the inner loop and when the loop ends the output afterwards:

test_eval['error']
[155, 34, 171, 37, 161, 52]
test_eval['incomplete_nodes']
[1, 0, 0, 0, 0, 0]
test_eval
{'error': [], 'incomplete_nodes': [], 'finished_fbc': 0, 'finished_mfcn': 0}
test_eval['error']
[]
test_eval['incomplete_nodes']
[]
Reply
#4
Also, why do you have semi-colons at the end of your lines? Those just aren't necessary.
Reply
#5
@ndc85430
I know - but they also shouldn't hurt - I am just used to put them there - old habit

Could you maybe also help me a little with my scope problem from above - I really don't know why the content of the variables goes missing and how to fix my solution

(That the code isn't pretty may be true - but in the end it is single purpose code to evaluate AMPL test results)
Reply
#6
When you need further information to help me with my problem please feel free to ask for it. I just thought I provided everything necessary but maybe the problem itself is a lot more complicated then I thought.

@SheeppOSU this would change a lot and make it non functional but since you couldn't see the whole function (due to some mistake from my side) you couldn't know - I add each "test_eval" dictionary to the "evaluation" dictionary and then create a new "test_eval" dictionary in the next loop.
Reply
#7
When asking a question you should try to write the shortest working program that demonstrates the problem you are trying to solve. Often this step will provide the insight you need to solve the problem yourself, and if it doesn't, at least others can do a quick copy/paste run and see what happens.

I tried to run your snippet, but that forced me to make fake test results. Since the only information I have about the test results is how you access the data in your program (which could be the problem) I had to reverse engineer something. This took me a about half an hour. Then I ran the program and didn't see any problem. Is my program wrong? Are the errors showing up somewhere else? Did I misinterpret your posts? I don't know and because of that I cannot help you
Reply
#8
I really sorry that it cost you that much time :/

I found that the function itself seems to be just fine. When I split the input list into two parts it works fine can't explain it (yet) - will look more detailed into it why this happens as soon as I have the time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have written a program that outputs data based on GPS signal kalle 1 1,133 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  2 or more data to be written in a row plumberpy 2 1,452 Nov-25-2021, 07:44 AM
Last Post: Gribouillis
  Formatting file data written to file. melblanc 2 2,254 Jan-15-2020, 03:27 PM
Last Post: melblanc
  csv data not written into avro file rajeshdatla2000 2 2,975 Aug-12-2017, 01:38 PM
Last Post: Bass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020