def main(argv): # Create an OptionParser for reading optional parameters parser = OptionParser(version='0.10') parser.prog = 'Model Runner Plots Test' print 'Running ' + parser.prog + ' ' + parser.version options, args = parser.parse_args(argv) log = False # Use a hardcoded value for the input filename, probably adding in an option argument later # for more flexibility filename = 'output_test/BigHorn_Temperature.txt' # Create the data frames and plots from the input file (df, df_closest_fit) = ModelRunnerPlots.create_dataframes(filename, log) # Get the plot and create a matplotlib figure. df_plot = ModelRunnerPlots.create_line_plot('Temperature', 'Temperature in Farenheit', df=df, log=log, value_vars=list(df.columns - ['year'])) df_plot.savefig('graph_test.png', format='png') # Create our GUI componenets. Note, the data frame and plot must be created beforehand for the GUI to work properly. root = Tkinter.Tk() root.title(parser.prog + ' ' + parser.version) root.grid() model_runner_gui = ModelRunnerGraphGUI.GraphFrame(root, df=df, plot=df_plot) model_runner_gui.set_grid() # Run the window main loop root.mainloop()
def create_plots(output_files_list, log, gui_frame=None): print('Number of plots in output: ' + str(len(output_files_list))) for output_file_and_job in output_files_list: output_file = output_file_and_job[0] # NOTE: Skip files with "Avg" in the filename. The problem occurs when creating line graphs with intervals like: # 1950-1959, 1951-1960, 1952-1961, etc. This is because the y-value is a range associated with a discrete range of data, # but not a continuous range. Would a bar chart be more applicable? if output_file.find('Avg') != -1: print('Could not find the output file!') continue if log: logging.info('Creating plots for file: ' + output_file) else: print('Creating plots for file: ' + output_file) #(df, df_closest_fit) = ModelRunnerPlots.create_dataframes(output_file, log) datatype = output_file[output_file.rindex('_')+1:output_file.rindex('.')] df = ModelRunnerPlots.create_dataframes(output_file, datatype, log) plot_title = create_plot_title(output_file, log) window_title = None if len(output_file_and_job) == 2: window_title = create_plot_title(output_file, log) + ': ' + output_file_and_job[1] else: window_title = plot_title if log: logging.info('Plot Title: ' + plot_title) all_plot = ModelRunnerPlots.create_line_plot(plot_title, get_plot_y_label(output_file, log), df, log, value_vars=list(df.columns - ['year'])) #closest_fit_plot = ModelRunnerPlots.create_line_plot(create_plot_title(output_file, log) + ' Closest Fit', get_plot_y_label(output_file, log), df_closest_fit, log, value_vars=list(df_closest_fit.columns - ['year'])) # Create two instances of graph windows for each graph if gui_frame != None: print('Creating plots!') #plot_title = create_plot_title(output_file, log) y_label = get_plot_y_label(output_file, log) #all_plot_gui = ModelRunnerGraphGUI.GraphWindow(parent=None, df=df, plot=all_plot, plot_title=plot_title, y_label=y_label) #closest_fit_plot_gui = ModelRunnerGraphGUI.GraphWindow(parent=None, df=df_closest_fit, plot=closest_fit_plot, plot_title=plot_title + ' Closest Fit', y_label=y_label) gui_frame.add_plot({'df': df, 'plot': all_plot, 'plot_title': plot_title, 'y_label': y_label, 'window_title': window_title, 'output_file': output_file}) #print 'Adding plots' # Or, just save the files if a GUI is not being used else: print('Just outputting image files!') plot_base_fname = output_file[0:output_file.rindex('.')] all_plot_fname = plot_base_fname + '_all_plot.png' all_plot.savefig(all_plot_fname, format='png') if log: logging.info('Created a plot file of all output columns called ' + all_plot_fname) closest_fit_plot_fname = plot_base_fname + '_closest_fit_plot.png' closest_fit_plot.savefig(closest_fit_plot_fname, format='png') if log: logging.info('Created a plot file of the best fitting output called ' + closest_fit_plot_fname)
def create_plots(output_files_list, log, gui_frame=None): # print 'Number of plots in output: ' + str(len(output_files_list)) for output_file_and_job in output_files_list: output_file = output_file_and_job[0] # NOTE: Skip files with "Avg" in the filename. The problem occurs when creating line graphs with intervals like: # 1950-1959, 1951-1960, 1952-1961, etc. This is because the y-value is a range associated with a discrete range of data, # but not a continuous range. Would a bar chart be more applicable? if output_file.find("Avg") != -1: continue if log: logging.info("Creating plots for file: " + output_file) else: print ("Creating plots for file: " + output_file) # (df, df_closest_fit) = ModelRunnerPlots.create_dataframes(output_file, log) datatype = output_file[output_file.rindex("_") + 1 : output_file.rindex(".")] df = ModelRunnerPlots.create_dataframes(output_file, datatype, log) plot_title = create_plot_title(output_file, log) window_title = None if len(output_file_and_job) == 2: window_title = create_plot_title(output_file, log) + ": " + output_file_and_job[1] else: window_title = plot_title if log: logging.info("Plot Title: " + plot_title) all_plot = ModelRunnerPlots.create_line_plot( plot_title, get_plot_y_label(output_file, log), df, log, value_vars=list(df.columns - ["year"]) ) # closest_fit_plot = ModelRunnerPlots.create_line_plot(create_plot_title(output_file, log) + ' Closest Fit', get_plot_y_label(output_file, log), df_closest_fit, log, value_vars=list(df_closest_fit.columns - ['year'])) # Create two instances of graph windows for each graph if gui_frame != None: # plot_title = create_plot_title(output_file, log) y_label = get_plot_y_label(output_file, log) # all_plot_gui = ModelRunnerGraphGUI.GraphWindow(parent=None, df=df, plot=all_plot, plot_title=plot_title, y_label=y_label) # closest_fit_plot_gui = ModelRunnerGraphGUI.GraphWindow(parent=None, df=df_closest_fit, plot=closest_fit_plot, plot_title=plot_title + ' Closest Fit', y_label=y_label) gui_frame.add_plot( { "df": df, "plot": all_plot, "plot_title": plot_title, "y_label": y_label, "window_title": window_title, "output_file": output_file, } ) # print 'Adding plots' # Or, just save the files if a GUI is not being used else: plot_base_fname = output_file[0 : output_file.rindex(".")] all_plot_fname = plot_base_fname + "_all_plot.png" all_plot.savefig(all_plot_fname, format="png") if log: logging.info("Created a plot file of all output columns called " + all_plot_fname) closest_fit_plot_fname = plot_base_fname + "_closest_fit_plot.png" closest_fit_plot.savefig(closest_fit_plot_fname, format="png") if log: logging.info("Created a plot file of the best fitting output called " + closest_fit_plot_fname)