1

I have two functions. My first function creates a GUI where the user inputs min and max values for 8 different species. My second function attempts to use those min and max values to create a simulation of 1000 mixtures within the boundaries of their respective min and max values whilst abiding by a number of different constraints. However, when I run the simulation I get no values. I only get the CSV file with the headings of the species. I also get no valuable error. My code is below and I am out of ideas of how to make this work. Any help would be much appreciated.

import Tkinter 
import pandas as pd
import numpy as np

class simulation_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.grid()

    def initialize(self):

        self.c2_low =Tkinter.StringVar()
        self.c3_low =Tkinter.StringVar()
        self.ic4_low =Tkinter.StringVar()
        self.nc4_low =Tkinter.StringVar()
        self.ic5_low =Tkinter.StringVar()
        self.nc5_low =Tkinter.StringVar()
        self.neoc5_low =Tkinter.StringVar()
        self.n2_low = Tkinter.StringVar()

        self.c2_high =Tkinter.StringVar()
        self.c3_high =Tkinter.StringVar()
        self.ic4_high =Tkinter.StringVar()
        self.nc4_high =Tkinter.StringVar()
        self.ic5_high =Tkinter.StringVar()
        self.nc5_high =Tkinter.StringVar()
        self.neoc5_high=Tkinter.StringVar()
        self.n2_high = Tkinter.StringVar()

        self.entry = Tkinter.Entry(self, textvariable = self.c2_low).grid(column=0,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c2_high).grid(column=0,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c3_low).grid(column=0,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c3_high).grid(column=0,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic4_low).grid(column=1,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic4_high).grid(column=1,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc4_low).grid(column=1,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc4_high).grid(column=1,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic5_low).grid(column=0,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic5_high).grid(column=0,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc5_low).grid(column=0,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc5_high).grid(column=0,row=8,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.neoc5_low).grid(column=1,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.neoc5_high).grid(column=1,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.n2_low).grid(column=1,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.n2_high).grid(column=1,row=8,sticky='EW')

        self.resizable(False,False)


        button = Tkinter.Button(self,text=u"simulate", command =self.simulation)
        button.grid(column=3,row=9)

    def simulation(self):


        sample_runs =10000     # Sample Population needs to be higher than exporting population
        export_runs = 1000    # How many samples we actually take

        c2_low = self.c2_low.get()
        c2_high = self.c2_high.get()
        c3_low = self.c3_low.get()
        c3_high = self.c3_high.get()
        ic4_low = self.ic4_low.get()
        ic4_high =self.ic4_high.get()
        nc4_low =self.nc4_low.get()
        nc4_high = self.nc4_high.get()  
        ic5_low = self.ic5_low.get()
        ic5_high = self.ic5_high.get()
        nc5_low = self.nc5_low.get()
        nc5_high = self.nc5_high.get()
        neoc5_low = self.neoc5_low.get()
        neoc5_high = self.neoc5_high.get()
        n2_low = self.n2_low.get()
        n2_high = self.n2_high.get()

        c2 = np.random.uniform(c2_low,c2_high,sample_runs)
        c3 = np.random.uniform(c3_low,c3_high, sample_runs)
        ic4 = np.random.uniform(ic4_low,ic4_high,sample_runs)
        nc4 = np.random.uniform(nc4_low,nc4_high,sample_runs)
        ic5 = np.random.uniform(ic5_low,ic5_high,sample_runs)
        nc5 = np.random.uniform(nc5_low,nc5_high,sample_runs)
        neoc5 = np.random.uniform(neoc5_low ,neoc5_high,sample_runs)
        n2 = np.random.uniform(n2_low, n2_high,sample_runs)

        # SETS CONSTRAINTS BASED ON RANGES

        masked = np.where((c3>=c3_low) & (c3<=c3_high) & (c2>=c2_low) & (c2<= c2_high) & (ic4>=ic4_low) & 
        (ic4<= ic4_high) & (nc4>= nc4_low) & (nc4<= nc4_high) & (ic5>= ic5_low) & (ic5<= ic5_high)& (nc5>= nc5_low)& 
        (nc5<= nc5_high)& (neoc5>= neoc5_low)& (neoc5<=neoc5_high) & (n2>=n2_low) & (n2<= n2_high))

        # MASKED CREATES AN INDEX (Where constraints are held) FOR LOOKING THROUGH DATA

        c2 = c2[masked][:export_runs]
        c3 = c3[masked][:export_runs]
        ic4 = ic4[masked][:export_runs]
        nc4 = nc4[masked][:export_runs]
        ic5 = ic5[masked][:export_runs]
        nc5 = nc5[masked][:export_runs]
        neoc5 = neoc5[masked][:export_runs]
        n2 = n2[masked][:export_runs]

        # DETERMINES CONC FROM METHANE BY BALANCE

        c1 = 100-c2-c3-nc4-ic4-nc5-ic5-neoc5-n2

        #CREATES A SERIES FOR EACH COMPONENET AND ADDS COLUMNS TO A FINAL DATAFRAME 

        c1_ser = pd.Series(c1)
        c2_ser = pd.Series(c2)
        c3_ser = pd.Series(c3)
        ic4_ser = pd.Series(ic4)
        nc4_ser = pd.Series(nc4)
        ic5_ser = pd.Series(ic5)
        nc5_ser = pd.Series(nc5)
        neoc5_ser = pd.Series(neoc5)
        n2_ser = pd.Series(n2)


        #EXPORTS DATAFRAME TO .CSV FILE NAMED LNG_DATA

        df = pd.DataFrame([c1_ser, c2_ser, c3_ser, ic4_ser, nc4_ser, ic5_ser, nc5_ser, neoc5_ser, n2_ser]).T
        df.columns = ['C1','C2','C3','nC4','iC4','nC5','iC5','neoC5','N2']
        df.to_csv(path to directory you want the saved file)

if __name__ == "__main__":
    app = simulation_tk(None)
    app.title('Simulation')
    app.mainloop()

EDIT:

The code for the original simulation function is below:

import numpy as np
import pandas as pd
import time

def LNG_SIMULATION(no_of_simulations):

    t0 = time.time()

    # SET COMPOSITION RANGES HERE:

    c2_low    =0;    c2_high   =14 
    c3_low    =0;    c3_high   =4
    nc4_low   =0;   nc4_high   =1.5
    ic4_low   =0;   ic4_high   =1.2
    nc5_low   =0;   nc5_high   =0.1
    ic5_low   =0;   ic5_high   =0.1
    neoc5_low =0; neoc5_high   =0.01
    n2_low    =0;    n2_high   =1.5

    # PRODUCES A RANDOM UNIFORM DISTRIBUTION BETWEEN LOW AND HIGH * runs

    sample_runs =10000     # Sample Population needs to be higher than exporting population
    export_runs = no_of_simulations    # How many samples we actually take

    c2 = np.random.uniform(c2_low,c2_high,sample_runs)
    c3 = np.random.uniform(c3_low,c3_high, sample_runs)
    ic4 = np.random.uniform(ic4_low,ic4_high,sample_runs)
    nc4 = np.random.uniform(nc4_low,nc4_high,sample_runs)
    ic5 = np.random.uniform(ic5_low,ic5_high,sample_runs)
    nc5 = np.random.uniform(nc5_low,nc5_high,sample_runs)
    neoc5 = np.random.uniform(neoc5_low,neoc5_high,sample_runs)
    n2 = np.random.uniform(n2_low, n2_high,sample_runs)

    # SETS CONSTRAINTS BASED ON RANGES

    masked = np.where((c3>=0) & (c3<=4) & (c2>=0) & (c2<=14) & (ic4>=0) & 
    (ic4<=1.5) & (nc4>=0) & (nc4<=1.2) & (ic5>=0) & (ic5<=0.1)& (nc5>=0)& 
    (nc5<=0.1)& (neoc5>=0)& (neoc5<=0.01) & (n2>=0) & (n2<=1.5))

    # MASKED CREATES AN INDEX (Where constraints are held) FOR LOOKING THROUGH DATA

    c2 = c2[masked][:export_runs]
    c3 = c3[masked][:export_runs]
    ic4 = ic4[masked][:export_runs]
    nc4 = nc4[masked][:export_runs]
    ic5 = ic5[masked][:export_runs]
    nc5 = nc5[masked][:export_runs]
    neoc5 = neoc5[masked][:export_runs]
    n2 = n2[masked][:export_runs]

    # DETERMINES CONC FROM METHANE BY BALANCE

    c1 = 100-c2-c3-nc4-ic4-nc5-ic5-neoc5-n2

    #CREATES A SERIES FOR EACH COMPONENET AND ADDS COLUMNS TO A FINAL DATAFRAME 

    c1_ser = pd.Series(c1)
    c2_ser = pd.Series(c2)
    c3_ser = pd.Series(c3)
    ic4_ser = pd.Series(ic4)
    nc4_ser = pd.Series(nc4)
    ic5_ser = pd.Series(ic5)
    nc5_ser = pd.Series(nc5)
    neoc5_ser = pd.Series(neoc5)
    n2_ser = pd.Series(n2)

    print np.min(c1); print np.max(c1) # Check for methane range

    #EXPORTS DATAFRAME TO .CSV FILE NAMED LNG_DATA

    df = pd.DataFrame([c1_ser, c2_ser, c3_ser, ic4_ser, nc4_ser, ic5_ser, nc5_ser, neoc5_ser, n2_ser]).T
    df.columns = ['C1','C2','C3','nC4','iC4','nC5','iC5','neoC5','N2']
    df.to_csv(filepath)

    t1 = time.time()
    tfinal = t1-t0, 'seconds'
    print tfinal


LNG_SIMULATION(1000) 

this gives the following output as a csv file:

each row adds up to 100, hence the c1 = 100- (sum of all the other components)

C1  C2  C3  nC4 iC4 nC5 iC5 neoC5   N2
0   82.85372539 12.99851014 2.642744858 0.129878248 0.800397967 0.002835756 0.01996335  0.00665644  0.545287856
1   97.53896049 1.246468861 0.00840227  0.616819596 0.340552181 0.093463733 0.0415282   0.002044789 0.11175988
2   96.06680372 1.005440722 0.427965685 0.944281965 0.354424967 0.029694142 0.046906668 0.001961002 1.122521133
3   92.152083   4.558717345 1.850648013 0.060053009 0.802721707 0.055533032 0.013490485 0.008897805 0.497855601
4   81.68486996 13.21690811 2.478113198 0.825638261 0.963227282 0.02162254  0.03812538  0.006329348 0.765165918
5   86.4237313  9.387647074 2.729233511 0.562534986 0.786110737 0.050537327 0.026122606 0.000290321 0.033792141
6   95.11319788 2.403944121 0.467770537 0.229967177 0.220494035 0.073742963 0.007893607 0.007473005 1.475516673
7   92.501114   2.677293658 2.742409857 0.608661787 0.237898432 0.073326044 0.030292277 0.002908029 1.126095919
8   89.83876672 5.850123215 2.598266005 0.060712896 0.29401403  0.037017143 0.048577495 0.001888549 1.270633946
9   84.14677099 13.9234657  0.214404288 0.535574576 0.677735065 0.061556983 0.015255684 0.006789481 0.418447232
10  94.73390493 2.302821233 1.478361587 0.500991046 0.022823156 0.030764131 0.024351373 0.009064709 0.896917832

1000 rows.

FINAL EDIT:

        self.entry = Tkinter.Entry(self, textvariable = self.c2_low).grid(column=0,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c2_high).grid(column=1,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c3_low).grid(column=0,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c3_high).grid(column=1,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic4_low).grid(column=0,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic4_high).grid(column=1,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc4_low).grid(column=0,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc4_high).grid(column=1,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic5_low).grid(column=0,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic5_high).grid(column=1,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc5_low).grid(column=0,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc5_high).grid(column=1,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.neoc5_low).grid(column=0,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.neoc5_high).grid(column=1,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.n2_low).grid(column=0,row=8,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.n2_high).grid(column=1,row=8,sticky='EW')
4
  • Note: I know the simulation works because I have run it in isolation outside the class function. Commented Dec 14, 2015 at 14:33
  • What does "I get no values" literally mean? Is the code that is trying to get the values failing? Is it succeeding but the values are None? Is it succeeding but the values are an empty string? Are you getting the values from the GUI ok, but your calculations are returning nothin? Commented Dec 14, 2015 at 14:37
  • Okay, so what I mean by this is that the cells in the CSV file are empty. The headings are present but the cells are empty. I know the simulation works 'without the GUI user input' , so the problem has to be somewhere in between getting the user input data and trying to use in the simulation. Commented Dec 14, 2015 at 18:00
  • 1
    what debugging have you done? The first step should always be to validate your assumptions. Print out the values you get from the GUI before you feed them into your function. Are they they the values you expect? Are they the right datatype? Commented Dec 14, 2015 at 18:07

1 Answer 1

1

The problem is that in your np.where call, your comparison is being performed between string values (i.e., the values in c2_low, c2_high, etc.) and numpy arrays. That comparison won't work. You need to convert those strings to floats, like so:

c2_low = float(self.c2_low.get())

I'll also note that I don't think you need your call to np.where. All you're doing there is making sure that the values of c2, c3, etc. all lie within the specified ranges. That should be true by default; those arrays were set up that way when you called np.random.uniform. So you should be able to do away with your masked variable altogether. If I make those changes to your code, I'm left with this:

import Tkinter as Tkinter
import pandas as pd
import numpy as np

class simulation_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.grid()

    def initialize(self):

        self.c2_low =Tkinter.StringVar()
        self.c3_low =Tkinter.StringVar()
        self.ic4_low =Tkinter.StringVar()
        self.nc4_low =Tkinter.StringVar()
        self.ic5_low =Tkinter.StringVar()
        self.nc5_low =Tkinter.StringVar()
        self.neoc5_low =Tkinter.StringVar()
        self.n2_low = Tkinter.StringVar()

        self.c2_high =Tkinter.StringVar()
        self.c3_high =Tkinter.StringVar()
        self.ic4_high =Tkinter.StringVar()
        self.nc4_high =Tkinter.StringVar()
        self.ic5_high =Tkinter.StringVar()
        self.nc5_high =Tkinter.StringVar()
        self.neoc5_high=Tkinter.StringVar()
        self.n2_high = Tkinter.StringVar()

        self.entry = Tkinter.Entry(self, textvariable = self.c2_low).grid(column=0,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c2_high).grid(column=0,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c3_low).grid(column=0,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.c3_high).grid(column=0,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic4_low).grid(column=1,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic4_high).grid(column=1,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc4_low).grid(column=1,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc4_high).grid(column=1,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic5_low).grid(column=0,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.ic5_high).grid(column=0,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc5_low).grid(column=0,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.nc5_high).grid(column=0,row=8,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.neoc5_low).grid(column=1,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.neoc5_high).grid(column=1,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.n2_low).grid(column=1,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = self.n2_high).grid(column=1,row=8,sticky='EW')

        self.resizable(False,False)


        button = Tkinter.Button(self,text=u"simulate", command =self.simulation)
        button.grid(column=3,row=9)

    def simulation(self):


        sample_runs =10000     # Sample Population needs to be higher than exporting population
        export_runs = 1000    # How many samples we actually take

        c2_low = float(self.c2_low.get())
        c2_high = float(self.c2_high.get())
        c3_low = float(self.c3_low.get())
        c3_high = float(self.c3_high.get())
        ic4_low = float(self.ic4_low.get())
        ic4_high = float(self.ic4_high.get())
        nc4_low = float(self.nc4_low.get())
        nc4_high = float(self.nc4_high.get())
        ic5_low = float(self.ic5_low.get())
        ic5_high = float(self.ic5_high.get())
        nc5_low = float(self.nc5_low.get())
        nc5_high = float(self.nc5_high.get())
        neoc5_low = float(self.neoc5_low.get())
        neoc5_high = float(self.neoc5_high.get())
        n2_low = float(self.n2_low.get())
        n2_high = float(self.n2_high.get())

        c2 = np.random.uniform(c2_low,c2_high,sample_runs)
        c3 = np.random.uniform(c3_low,c3_high, sample_runs)
        ic4 = np.random.uniform(ic4_low,ic4_high,sample_runs)
        nc4 = np.random.uniform(nc4_low,nc4_high,sample_runs)
        ic5 = np.random.uniform(ic5_low,ic5_high,sample_runs)
        nc5 = np.random.uniform(nc5_low,nc5_high,sample_runs)
        neoc5 = np.random.uniform(neoc5_low ,neoc5_high,sample_runs)
        n2 = np.random.uniform(n2_low, n2_high,sample_runs)

        # SETS CONSTRAINTS BASED ON RANGES

        # masked = np.where((c3>=c3_low) & (c3<=c3_high) & (c2>=c2_low) & (c2<= c2_high) & (ic4>=ic4_low) &
        #                   (ic4<= ic4_high) & (nc4>= nc4_low) & (nc4<= nc4_high) & (ic5>= ic5_low) & (ic5<= ic5_high)& (nc5>= nc5_low)&
        #                   (nc5<= nc5_high)& (neoc5>= neoc5_low)& (neoc5<=neoc5_high) & (n2>=n2_low) & (n2<= n2_high))

        # MASKED CREATES AN INDEX (Where constraints are held) FOR LOOKING THROUGH DATA

        c2 = c2[:export_runs]
        c3 = c3[:export_runs]
        ic4 = ic4[:export_runs]
        nc4 = nc4[:export_runs]
        ic5 = ic5[:export_runs]
        nc5 = nc5[:export_runs]
        neoc5 = neoc5[:export_runs]
        n2 = n2[:export_runs]

        # DETERMINES CONC FROM METHANE BY BALANCE

        c1 = 100-c2-c3-nc4-ic4-nc5-ic5-neoc5-n2

        #CREATES A SERIES FOR EACH COMPONENET AND ADDS COLUMNS TO A FINAL DATAFRAME

        c1_ser = pd.Series(c1)
        c2_ser = pd.Series(c2)
        c3_ser = pd.Series(c3)
        ic4_ser = pd.Series(ic4)
        nc4_ser = pd.Series(nc4)
        ic5_ser = pd.Series(ic5)
        nc5_ser = pd.Series(nc5)
        neoc5_ser = pd.Series(neoc5)
        n2_ser = pd.Series(n2)


        #EXPORTS DATAFRAME TO .CSV FILE NAMED LNG_DATA

        df = pd.DataFrame([c1_ser, c2_ser, c3_ser, ic4_ser, nc4_ser, ic5_ser, nc5_ser, neoc5_ser, n2_ser]).T
        df.columns = ['C1','C2','C3','nC4','iC4','nC5','iC5','neoC5','N2']
        df.to_csv('output.csv')

if __name__ == "__main__":
    app = simulation_tk(None)
    app.title('Simulation')
    app.mainloop()

I've tested this with Python 2.7 and numpy 1.7.1 as well as Python 3.4 with numpy 1.9.2 (with an appropriate change to the tkinter import statement) and in both cases I get a fully populated CSV file where each row sums to 100.

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately this didn't work. I still get empty cells in my CSV file.
Can you edit your original post to describe the values you're using as input? After adding a cast to floats for lines 61-76 and running this in Python 2.7, I get a populated CSV file with 1001 lines and 10 columns (whether they're correct or not I can't say). I'm using alternating values of 10 and 20 as input (i.e., c2_low = 10, c2_high = 20, c3_low = 10...)
I've added additional detail to my answer, as well as a copy of the source that I'm running. What version of Python/numpy are you running? If you inspect the values of c2_low, c2_high before using them to create the arrays, are the values what you expect? What about the arrays c1, c2...? Do those contain what you'd expect? I'm going to echo @BryanOakley above: some time spent with the debugger (or even print statements) would pay off here.
I've run the code you have proposed. However, output is not what I want. The 'masked' section ensures the constraints are met. It's quite hard to explain. But, if you see my EDIT post you can see how all the numbers are non-zero and random, yet all add to 100.
The problem has been solved with some debugging as @BryanOakley suggested. The issue was the Entries were assigned to the wrong labels. I've edited my post for the correct code. Thanks.

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.