From 5f83e3dfd9f1fcdd82798bccf9a9ac301278c1e1 Mon Sep 17 00:00:00 2001 From: Angel Farguell Caus Date: Thu, 5 Dec 2019 15:58:10 -0800 Subject: [PATCH] cleaning up some code and improving notnan flag --- README.md | 14 ++++++++++---- plot_svm.m | 4 ++-- process.py | 13 +++++++++---- svm.py | 16 ++++++++-------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index aff8298..551eda8 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,13 @@ Run process.py as: OR - $ python lon1,lon2,lat1,lat2 wrfout start_time days + $ python process.py lon1,lon2,lat1,lat2 start_time days Generates the fire mesh, downloads all the granules in between the dates and intersecting with the fire mesh, reads all the important information inside them and saves everything in a binary file called 'data'. It creates as well a KML file with all the fire detections called 'fire_detections.kml'. After that, it runs a postprocessing of the data creating an intermediate binary file called 'result'. Finally, it estimates the fire arrival time using SVM machine learning technique, creating an output file 'svm.mat' with the results. - The input variales are: + The input variales from 'python process.py coord start_time days' are: - - coord: string: + - coord: string: 1) link to the wrfout file of WRF-SFIRE simulation or 2) bounding box coordinates separated by commas lon1,lon2,lat1,lat2 @@ -40,7 +40,13 @@ OR - SS - second - days: number, number of days of simulation (can be decimal). -For different configurations of the run, create file called 'conf.json' using similar structure than 'conf_example.json' file. +For different configurations of the run, create file called 'conf.json' using similar structure than 'conf_example.json' file. In order to find out what are the flags, look into 'utils.py' file. + +For running SVM using different weights depending on the confidence levels, run inside JPSSdata repository: + + $ git clone https://github.com/Fergui/libsvm_weights + +and set 'dyn_pen' flag in 'conf.json' to true. There is also an example.sh bash script which needs to be run in Kinspeak or change the wrfout path to some existent wrfout file. diff --git a/plot_svm.m b/plot_svm.m index 791d7c1..26acdcb 100644 --- a/plot_svm.m +++ b/plot_svm.m @@ -63,13 +63,13 @@ else zmax = max(r.X(:,3)); figure, - S = repmat(5,sum(fire),1); + S = repmat(10,sum(fire),1); C = repmat([1,0,0],sum(fire),1); h1 = scatter3(r.X(fire,1),r.X(fire,2),r.X(fire,3),S,C,'filled'); alpha = 0.7; set(h1, 'MarkerEdgeAlpha', alpha, 'MarkerFaceAlpha', alpha) hold on - S = repmat(5,sum(ground),1); + S = repmat(10,sum(ground),1); C = repmat([0.2,0.7,0.2],sum(ground),1); h2 = scatter3(r.X(ground,1),r.X(ground,2),r.X(ground,3),S,C,'filled'); alpha = 0.3; diff --git a/process.py b/process.py index 6bc8727..fdc9051 100644 --- a/process.py +++ b/process.py @@ -313,18 +313,20 @@ print '>> Running Support Vector Machine <<' sys.stdout.flush() if dyn_pen: C = np.power(c,3)/1000. - kgam = np.sqrt(len(y))/10. + kgam = np.sqrt(len(y))/12. search = False else: - kgam = np.sqrt(len(y))/10. + kgam = np.sqrt(len(y))/12. C = kgam*1000. -F = SVM3(X,y,C=C,kgam=kgam,fire_grid=(lon,lat),**svm_settings) +tscale = 24*3600 # scale from seconds to days +it = (np.array(time_num_interval)-scale[0])/tscale # time interval in days + +F = SVM3(X,y,C=C,kgam=kgam,fire_grid=(lon,lat),it=it,**svm_settings) print '' print '>> Saving the results <<' sys.stdout.flush() -tscale = 24*3600 # scale from seconds to days # Fire arrival time in seconds from the begining of the simulation tign_g = np.array(F[2])*float(tscale)+scale[0]-time_num_interval[0] # Creating the dictionary with the results @@ -347,6 +349,9 @@ if fire_interp: values = np.ravel(tign_g) tign_g_interp = interpolate.griddata(points,values,(fxlon,fxlat)) t_interp_2 = time() + if notnan: + with np.errstate(invalid='ignore'): + tign_g_interp[np.isnan(tign_g_interp)] = np.nanmax(tign_g_interp) print 'elapsed time: %ss.' % str(abs(t_interp_2-t_interp_1)) svm.update({'fxlon_interp': np.array(fxlon), 'fxlat_interp': np.array(fxlat), diff --git a/svm.py b/svm.py index cd47a75..640b432 100644 --- a/svm.py +++ b/svm.py @@ -444,7 +444,7 @@ def frontier(clf, xx, yy, zz, bal=.5, plot_decision = False, plot_poly=False, us return F -def SVM3(X, y, C=1., kgam=1., fire_grid=None, **params): +def SVM3(X, y, C=1., kgam=1., fire_grid=None, it=None, **params): """ 3D SuperVector Machine analysis and plot @@ -712,7 +712,10 @@ def SVM3(X, y, C=1., kgam=1., fire_grid=None, **params): else: fxlon = np.divide(fire_grid[0] - xmin, xlen) fxlat = np.divide(fire_grid[1] - ymin, ylen) - it = (X2.min(),X2.max()) + if it is None: + it = (X2.min(),X2.max()) + else: + it = np.divide(np.array(it) - zmin, zlen) vnodes = vN*nnodes sdim = (fxlon.shape[0],fxlon.shape[1],vnodes) print 'fire_grid_size = %dx%dx%d = %d' % (sdim + (np.prod(sdim),)) @@ -801,12 +804,9 @@ def SVM3(X, y, C=1., kgam=1., fire_grid=None, **params): oX0, oX1, oX2 = oX[:, 0], oX[:, 1], oX[:, 2] FFx, FFy, FFz = FF[0], FF[1], FF[2] - with np.errstate(invalid='ignore'): - FFz[FFz > oX2.max()] = np.nan - - if params['notnan']: - FFz[np.isnan(FFz)] = oX2.max() - FFz = np.minimum(FFz, oX2.max()) + if params['notnan']: + with np.errstate(invalid='ignore'): + FFz[np.isnan(FFz)] = np.nanmax(FFz) FF = [FFx,FFy,FFz] # Plot the fire arrival time resulting from the SVM classification -- 2.11.4.GIT