对函数中的变量使用 Tkinter 用户输入
Using Tkinter user input for variables in functions
我有两个功能。我的第一个函数创建了一个 GUI,用户可以在其中输入 8 个不同物种的最小值和最大值。我的第二个函数尝试使用这些最小值和最大值在它们各自的最小值和最大值的边界内创建 1000 种混合物的模拟,同时遵守许多不同的约束。但是,当我 运行 模拟时,我没有得到任何值。我只得到带有物种标题的 CSV 文件。我也没有得到任何有价值的错误。我的代码在下面,我不知道如何让它工作。任何帮助将非常感激。
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()
编辑:
原模拟函数的代码如下:
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)
这将以 csv 文件的形式提供以下输出:
每行加起来为 100,因此 c1 = 100-(所有其他分量的总和)
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 行。
最终编辑:
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')
问题是在您的 np.where
调用中,您的比较是在字符串值(即 c2_low
、c2_high
等中的值)和 numpy 数组之间进行的.这种比较是行不通的。您需要将这些字符串转换为浮点数,如下所示:
c2_low = float(self.c2_low.get())
我还会注意到,我认为您不需要致电 np.where
。您所做的只是确保 c2
、c3
等的值都在指定范围内。默认情况下应该是这样;当您调用 np.random.uniform
时,这些数组就是这样设置的。所以你应该能够完全取消你的 masked
变量。如果我对您的代码进行这些更改,我将得到以下结果:
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()
我已经用 Python 2.7 和 numpy 1.7.1 以及 Python 3.4 和 numpy 1.9.2(对 tkinter import 语句进行了适当的更改)以及两者都进行了测试情况下我得到一个完全填充的 CSV 文件,其中每行总和为 100。
我有两个功能。我的第一个函数创建了一个 GUI,用户可以在其中输入 8 个不同物种的最小值和最大值。我的第二个函数尝试使用这些最小值和最大值在它们各自的最小值和最大值的边界内创建 1000 种混合物的模拟,同时遵守许多不同的约束。但是,当我 运行 模拟时,我没有得到任何值。我只得到带有物种标题的 CSV 文件。我也没有得到任何有价值的错误。我的代码在下面,我不知道如何让它工作。任何帮助将非常感激。
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()
编辑:
原模拟函数的代码如下:
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)
这将以 csv 文件的形式提供以下输出:
每行加起来为 100,因此 c1 = 100-(所有其他分量的总和)
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 行。
最终编辑:
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')
问题是在您的 np.where
调用中,您的比较是在字符串值(即 c2_low
、c2_high
等中的值)和 numpy 数组之间进行的.这种比较是行不通的。您需要将这些字符串转换为浮点数,如下所示:
c2_low = float(self.c2_low.get())
我还会注意到,我认为您不需要致电 np.where
。您所做的只是确保 c2
、c3
等的值都在指定范围内。默认情况下应该是这样;当您调用 np.random.uniform
时,这些数组就是这样设置的。所以你应该能够完全取消你的 masked
变量。如果我对您的代码进行这些更改,我将得到以下结果:
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()
我已经用 Python 2.7 和 numpy 1.7.1 以及 Python 3.4 和 numpy 1.9.2(对 tkinter import 语句进行了适当的更改)以及两者都进行了测试情况下我得到一个完全填充的 CSV 文件,其中每行总和为 100。