def plot_NoteAndSens(): # Plot time series of note (volume) and sensors of specific file # TODO: save it to the folder # files = next(os.walk(directory))[1] # for f in files: f = 'two_scales_1' A = Analyze(filename=f) A.plotKeyAndGlove('basic', savename=f)
def fit_sparse(self, feature_opt='1storder', variableCoef=False, variableCoefOrder=0, variableCoefBasis='simple_polynomial', \ LassoType='Lasso', RegCoef=0.00001, cv=None, criterion=None, maxiter=10000, tolerance=0.00001, use_rfe=False, normalize=True, rfe_iter=10, rfe_alpha=None, print_rfeiter=False, shuffle=False, nzthresh=1e-200, basefile='', adjustgrid={}, save=True, comments='', checkExistence=True): # Make Metadata and Check its existence metadata = self.makeMetadata(basefile, adjustgrid, feature_opt, self.trainratio, variableCoef, variableCoefOrder, variableCoefBasis, \ LassoType, cv, criterion, use_rfe, rfe_alpha, nzthresh, maxiter, comments) datahandler = DataIO(self.scase, directory=LEARNDIR, basefile=basefile) if checkExistence: exists, filename = datahandler.checkMetadataInDir( metadata, ignore_prop='nzthresh') if exists: return filename + '.txt' # Make features and training set F = Features(scase=self.scase, option=feature_opt, variableCoef=variableCoef, variableCoefOrder=variableCoefOrder, variableCoefBasis=variableCoefBasis) self.featurelist, self.labels, self.featurenames = F.makeFeatures( self.grid, self.fu, self.ICparams) Xtrain, ytrain, Xtest, ytest = self.makeTTsets(self.featurelist, self.labels, shuffle=shuffle, threshold=nzthresh) # Choose optimization algorithm lin = self.choose_optimizer(LassoType=LassoType, RegCoef=RegCoef, cv=cv, criterion=criterion, maxiter=maxiter, tolerance=tolerance, normalize=normalize) # Train model with Lasso # Choose to use Recursive Feature Elimination or not if use_rfe: lin, rem_feature_idx = self.train_rfe(lin, Xtrain, ytrain, rfe_iter=rfe_iter, rfe_alpha=rfe_alpha, print_rfeiter=print_rfeiter) Xtrain = Xtrain[:, rem_feature_idx] Xtest = Xtest[:, rem_feature_idx] coefficients = lin.coef_ else: lin, rem_feature_idx = self.train_single(lin, Xtrain, ytrain) coefficients = lin.coef_[rem_feature_idx] # Outputs output = {} # Compute Erros and Scores output['trainRMSE'] = np.sqrt( mean_squared_error(ytrain, lin.predict(Xtrain))) output['testRMSE'] = np.sqrt( mean_squared_error(ytest, lin.predict(Xtest))) output['trainScore'] = lin.score(Xtrain, ytrain) output['testScore'] = lin.score(Xtest, ytest) rem_featurenames = [self.featurenames[i] for i in rem_feature_idx] output['featurenames'] = rem_featurenames output['coef'] = coefficients.tolist() # Might not work for RFE !! output['n_iter'] = lin.n_iter_ # Different optimizers have different outputs if LassoType == 'LassoLarsIC': output['alpha'] = lin.alpha_.tolist() output['criterion_path'] = lin.criterion_.tolist() elif LassoType == 'LassoCV': output['alpha'] = lin.alpha_.tolist() output['alpha_mse_path'] = lin.mse_path_.mean(axis=1).tolist() output['alpha_path'] = lin.alphas_.tolist() output['dual_gap'] = lin.dual_gap_ elif LassoType in {'LassoLarsCV', 'LarsCV'}: output['alpha'] = lin.alpha_ output['alpha_mse_path'] = lin.mse_path_.mean( axis=1).tolist() # Average along CV folds output['cv_alpha_path'] = lin.cv_alphas_.tolist( ) # Goes with mse_path output['coef_path'] = lin.coef_path_.tolist() output['alpha_path'] = lin.alphas_.tolist() # Goes with coef_path elif LassoType == 'Lasso': output['alpha'] = RegCoef # Printing if self.verbose: A = Analyze() A.print_results(output, metadata) # Saving filename = datahandler.saveSolution(output, metadata, fileformat='.txt') return filename
# Save Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) print('files = [') for f in filename_vec: print("\'"+f+"\',") print(']') ## PLOT A = Analyze() savename = 'Burgers_MC_CDF' A.plotRMSEandCoefs(output_vec, MCcountvec, '$N_{MC}$, Number of Realizations', threshold=0.01, set_grid=False, cdf=True, invert_sign=True, savename='Burgers_MC_CDF') ############## ##Function of Regularization - For MC = num_realizations (MAX) ## WTF?? # fig, ax = plt.subplots(1, 2) # alphas, mse = A.getRegMseDependence_single(output_vec[-1]) # ax[0].plot(alphas, mse) # ax[0].set_xlabel('Regularization Coefficient') # ax[0].set_ylabel('MSE') # alphas, coefficients, feats = A.getCoefRegDependence(output_vec[-1], threshold=0.0) # for i in range(len(feats)):
output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) output_VEC.append(output_vec) metadata_VEC.append(metadata_vec) filename_VEC.append(filename_vec) print('files = [') for f in filename_vec: print("\'" + f + "\',") print(']') ## PLOT A = Analyze() savename = 'advectreact_rfe' + "_closecomp_" + LassoType + "_" + str(coeforder) xlabel = 'RFE Threshold' feature_opt_vec = ['Closure Prob.', 'Full Eqn.'] linestyles = ['solid', 'dashed', 'dashdot', 'dotted'] marker = ['o', 'v', 's', '*'] #, '^', '>', '<', 'x', 'D', '1', '.', '2', '3', '4'] styles = [[l, m] for l in linestyles for m in marker] print(styles) print(len(styles)) fig, ax = plt.subplots(1, 2, figsize=(13, 5.5)) variable = rfe_alpha_vec leg0 = []
def plotRun(self): dataplotting = Analyze(self.directory, self.filename) dataplotting.plotKeyAndGlove('basic') dataplotting.plot_sampling_frequency()
metadata_vec.append(metadata) filename_vec.append(filename) output_VEC.append(output_vec) metadata_VEC.append(metadata_vec) filename_VEC.append(filename_vec) print('files = [') for f in filename_vec: print("\'"+f+"\',") print(']') ## PLOT A = Analyze() savename = 'advectreact_rfe' + "_ordercomp_" + LassoType + "_" + feature_opt xlabel = 'RFE Threshold' linestyles = ['solid', 'dashed', 'dashdot', 'dotted'] marker = ['o', 'v', 's', '*']#, '^', '>', '<', 'x', 'D', '1', '.', '2', '3', '4'] styles = [[l, m] for l in linestyles for m in marker] fig, ax = plt.subplots(1, 2, figsize=(13, 5.5)) variable = rfe_alpha_vec leg0 = [] leg1 = [] for i, outvec in enumerate(output_VEC): trainRMSE, testRMSE = A.getTrainTestDependence(outvec)
# READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) print('files = [') for f in filename_vec: print("\'" + f + "\',") print(']') ## PLOT A = Analyze() portion_from_boundary = [m[0] for m in muvec] savename = 'advectreact_boundary' # ax = A.plotRMSEandCoefs(output_vec, portion_from_boundary, 'Distance from $U=0$ (Domain Fraction)', \ # set_grid=False, cdf=False, threshold=0.01, invert_sign=True, savename=savename) ##### variable = portion_from_boundary xlabel = 'Distance from $U=0$ (Domain Fraction)' fig, ax = plt.subplots(1, 2, figsize=(9, 4)) # Coefficients Dependence Multi featarray, relevant_feats = A.getCoefDependence(output_vec, threshold=0.01,
# READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) print('files = [') for f in filename_vec: print("\'" + f + "\',") print(']') ## PLOT A = Analyze() savename = 'advectreact_rde' A.plotRMSEandCoefs(output_vec, rfe_alpha_vec, 'Percentage Distance from Boundary', threshold=0.01, invert_sign=True, savename=savename) # Plot boundary s = 8 V = Visualize(grid) V.plot_fu3D(fu) V.plot_fu(fu, dim='t', steps=s) V.plot_fu(fu, dim='x', steps=s) V.show()
# READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) print('files = [') for f in filename_vec: print("\'" + f + "\',") print(']') ## PLOT A = Analyze() savename = 'advectreact_MC' + "_" + LassoType + "_" + str(mu[0]).split('.')[1] A.plotRMSEandCoefs(output_vec, MCcountvec, 'Number of Realizations', threshold=0.01, invert_sign=True, use_logx=False, set_grid=True, savename=savename) ## PLOT 3D # s = 8 # V = Visualize(grid) # V.plot_fu3D(fu)
scase=case, trainratio=trainratio, verbose=True) filename = difflearn.fit_sparse(feature_opt=feature_opt, variableCoef=variableCoef, variableCoefBasis=variableCoefBasis, \ variableCoefOrder=coeforder, use_rfe=use_rfe, rfe_alpha=rfe_alpha, nzthresh=nzthresh, maxiter=maxiter, \ LassoType=LassoType, RegCoef=RegCoef, cv=cv, criterion=criterion, print_rfeiter=print_rfeiter, shuffle=shuffle, \ basefile=savenamepdf, adjustgrid=adjustgrid, save=save, normalize=normalize, comments=comments) # READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) print('files = [') for f in filename_vec: print("\'" + f + "\',") print(']') ## PLOT A = Analyze() savename = 'advectreact_randadv_closure' A.barRMSEandCoefs(output_vec, feature_opt_vec, 'Method', threshold=0.01, invert_sign=True, savename=savename)
trainratio=trainratio, verbose=printlearning) filename = difflearn.fit_sparse(feature_opt=feature_opt, variableCoef=variableCoef, variableCoefBasis=variableCoefBasis, \ variableCoefOrder=coeforder, use_rfe=use_rfe, rfe_alpha=rfe_alpha, nzthresh=nzthresh, maxiter=maxiter, \ LassoType=LassoType, RegCoef=RegCoef, cv=cv, criterion=criterion, print_rfeiter=print_rfeiter, shuffle=shuffle, \ basefile=savenamepdf, adjustgrid=adjustgrid, save=save, normalize=normalize, comments=comments) # READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) ## PLOT # Error function of MC A = Analyze() savename = 'Burgers_rfe' A.plotRMSEandCoefs(output_vec, rfe_alpha_vec, 'RFE Threshold', threshold=0.01, invert_sign=True, savename=savename) plt.show() # Plot Coefficients as a function of t0
basefile=savenamepdf, adjustgrid=adjustgrid, save=save, normalize=normalize, comments=comments) # READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) ## PLOT # Error function of MC # fig = plt.figure() A = Analyze() savename = 'Burgers_shock' portion = [(t[1] - 0.5) / (t[1] - t[0]) for t in mtvec] A.plotRMSEandCoefs(output_vec, portion, '$p_s$, Time Portion in Shock Region', threshold=0.01, invert_sign=True, cdf=True, set_grid=False, savename='Burgers_shock') # trainRMSE, testRMSE = A.getTrainTestDependence(output_vec) # t0 = [t[0] for t in mtvec] # plt.plot(t0, testRMSE, linwidth=3)
# READ Learning D = DataIO(case, directory=LEARNDIR) output, metadata = D.readLearningResults(filename) output_vec.append(output) metadata_vec.append(metadata) filename_vec.append(filename) print('files = [') for f in filename_vec: print("\'" + f + "\',") print(']') ## PLOT A = Analyze() savename = 'advectreact_rfe' + "_" + feature_opt + "_" + LassoType + "_" + str( coeforder) A.plotRMSEandCoefs(output_vec, rfe_alpha_vec, 'RFE Threshold', threshold=0.001, use_logx=True, set_grid=True, invert_sign=True, savename=savename, show=True) except: print("\n\n\n************************\n\n\n") print("Exception Happened for ", feature_opt, " ", LassoType,