我有两个功能。我的第一个函数创建了一个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')
答案 0 :(得分:1)
问题在于,在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以及带有numpy 1.9.2的Python 3.4(对tkinter import语句进行了适当的更改)对此进行了测试。在这两种情况下,我得到一个完全填充的CSV文件,其中每个行总和为100。