2 import matplotlib
.pyplot
as plt
3 from mpl_toolkits
.mplot3d
import axes3d
4 import matplotlib
.colors
as colors
6 from scipy
.io
import savemat
7 from scipy
import interpolate
9 def plot_case(xx
,yy
,tign_g
,X_satellite
=None,show
=False):
11 ax
= fig
.gca(projection
='3d')
12 ax
.contour(xx
,yy
,tign_g
,30)
13 if X_satellite
is not None:
14 ax
.scatter(X_satellite
[:,0],X_satellite
[:,1],
15 X_satellite
[:,2],s
=5,color
='r')
22 plt
.savefig('syn_case.png')
24 def plot_data(X
,y
,show
=False):
25 col
= [(0, .5, 0), (.5, 0, 0)]
26 cm_GR
= colors
.LinearSegmentedColormap
.from_list('GrRd',col
,N
=2)
28 ax
= fig
.gca(projection
='3d')
29 ax
.scatter(X
[:, 0], X
[:, 1], X
[:, 2],
30 c
=y
, cmap
=cm_GR
, s
=1, alpha
=.5,
31 vmin
=y
.min(), vmax
=y
.max())
38 plt
.savefig('syn_data.png')
41 def cone_point(xx
,yy
,nx
,ny
):
44 tign_g
= np
.minimum(1e3
,10+(2e3
/cx
)*np
.sqrt(((xx
-cx
)**2+(yy
-cy
)**2)/2))
45 tsat
= (tign_g
.max()-tign_g
.min())*.5
46 tt1d
= np
.ravel(tign_g
)
47 mask
= tt1d
< tt1d
.max()
48 xx1d
= np
.ravel(xx
)[mask
]
49 yy1d
= np
.ravel(yy
)[mask
]
51 X_satellite
= np
.array([[cx
*.7,cy
*.7,tsat
]])
52 return tign_g
,xx1d
,yy1d
,tt1d
,X_satellite
54 def cone_points(xx
,yy
,nx
,ny
):
57 tign_g
= np
.minimum(1e3
,10+(2e3
/cx
)*np
.sqrt(((xx
-cx
)**2+(yy
-cy
)**2)/2))
58 tsat
= (tign_g
.max()-tign_g
.min())*.5
59 tt1d
= np
.ravel(tign_g
)
60 mask
= tt1d
< tt1d
.max()
61 xx1d
= np
.ravel(xx
)[mask
]
62 yy1d
= np
.ravel(yy
)[mask
]
65 X_satellite
= np
.c_
[np
.linspace(cx
*.7,cx
,N
+1),
66 np
.linspace(cy
*.7,cy
,N
+1),
67 np
.linspace(tsat
,tign_g
.min(),N
+1)][:-1]
68 return tign_g
,xx1d
,yy1d
,tt1d
,X_satellite
70 def slope(xx
,yy
,nx
,ny
):
71 ros
= (10,30) # rate of spread
73 s1
= 10+np
.arange(0,cx
*ros
[0],ros
[0])
74 s2
= ros
[1]+np
.arange(cx
*ros
[0],cx
*ros
[0]+(nx
-cx
)*ros
[1],ros
[1])
75 s
= np
.concatenate((s1
,s2
))
76 tign_g
= np
.reshape(np
.repeat(s
,ny
),(nx
,ny
)).T
79 tt1d
= np
.ravel(tign_g
)
81 return tign_g
,xx1d
,yy1d
,tt1d
,X_satellite
83 def preprocess_svm(xx
,yy
,tt
,epsilon
,weights
,X_satellite
=None):
84 wforecastg
,wforecastf
,wsatellite
= weights
85 for_fire
= np
.c_
[xx
.ravel(),yy
.ravel(),tt
.ravel() + epsilon
]
86 for_nofire
= np
.c_
[xx
.ravel(),yy
.ravel(),tt
.ravel() - epsilon
]
87 X_forecast
= np
.concatenate((for_nofire
,for_fire
))
88 y_forecast
= np
.concatenate((-np
.ones(len(for_nofire
)),np
.ones(len(for_fire
))))
89 c_forecast
= np
.concatenate((wforecastg
*np
.ones(len(for_nofire
)),wforecastf
*np
.ones(len(for_fire
))))
90 if X_satellite
is not None:
91 X
= np
.concatenate((X_forecast
,X_satellite
))
92 y
= np
.concatenate((y_forecast
,np
.ones(len(X_satellite
))))
93 c
= np
.concatenate((c_forecast
,wsatellite
*np
.ones(len(X_satellite
))))
100 if __name__
== "__main__":
102 # Experiments: 1) Cone with point, 2) Slope, 3) Cone with points
104 # hyperparameter settings
109 # epsilon for artificial forecast in seconds
113 # plotting data before svm?
117 xx
,yy
= np
.meshgrid(np
.arange(0,nx
,1),
120 experiments
= {1: cone_point
, 2: slope
, 3: cone_points
}
121 tign_g
,xx1d
,yy1d
,tt1d
,X_satellite
= experiments
[exp
](xx
,yy
,nx
,ny
)
123 plot_case(xx
,yy
,tign_g
,X_satellite
)
126 if X_satellite
is None:
128 X
,y
,c
= preprocess_svm(xx1d
,yy1d
,tt1d
,epsilon
,
129 (wforecastg
,wforecastf
,wsatellite
),
136 options
= {'downarti': False, 'plot_data': True,
137 'plot_scaled': True, 'plot_supports': True,
138 'plot_result': True, 'plot_decision': True,
139 'artiu': False, 'hartiu': .2,
140 'artil': False, 'hartil': .2,
142 if (wforecastg
== wforecastf
and
143 (wsatellite
== 0 or wsatellite
== wforecastg
)):
146 F
= SVM3(X
, y
, C
=c
, kgam
=kgam
, **options
)
149 # interpolation to validate
150 points
= np
.c_
[np
.ravel(F
[0]),np
.ravel(F
[1])]
151 values
= np
.ravel(F
[2])
152 zz_svm
= interpolate
.griddata(points
,values
,(xx
,yy
))
154 svm
= {'xx': xx
, 'yy': yy
, 'zz': tign_g
,
155 'zz_svm': zz_svm
, 'X': X
, 'y': y
, 'c': c
,
156 'fxlon': F
[0], 'fxlat': F
[1], 'Z': F
[2],
157 'epsilon': epsilon
, 'options': options
}
160 filename
= 'syn_fg%d_ff%d_s%d_k%d_e%d.mat' % (wforecastg
,wforecastf
,
161 wsatellite
,kgam
,epsilon
)
163 filename
= 'syn_fg%d_ff%d_k%d_e%d.mat' % (wforecastg
,wforecastf
,
165 savemat(filename
, mdict
=svm
)
166 print 'plot_svm %s' % filename