def test_bar_chart(): """Make sure some dummy data generates a ``bokeh`` plot""" # Make a toy dataframe data = DataFrame({ 'meow': { 'foo': 12, 'bar': 23, 'baz': 2 }, 'mix': { 'foo': 45, 'bar': 31, 'baz': 23 }, 'deliver': { 'foo': 62, 'bar': 20, 'baz': 9 } }) data = data.reset_index() # And generate a figure plt = bar_chart(data, 'index') assert str(type(plt)) == "<class 'bokeh.plotting.figure.Figure'>"
def jwst_inventory(instruments=JWST_INSTRUMENT_NAMES, dataproducts=['image', 'spectrum', 'cube'], caom=False, plot=False): """Gather a full inventory of all JWST data in each instrument service by instrument/dtype Parameters ---------- instruments: sequence The list of instruments to count dataproducts: sequence The types of dataproducts to count caom: bool Query CAOM service plot: bool Return a pie chart of the data Returns ------- astropy.table.table.Table The table of record counts for each instrument and mode """ logging.info('Searching database...') # Iterate through instruments inventory, keywords = [], {} for instrument in instruments: ins = [instrument] for dp in dataproducts: count = instrument_inventory(instrument, dataproduct=dp, caom=caom) ins.append(count) # Get the total ins.append(sum(ins[-3:])) # Add it to the list inventory.append(ins) # Add the keywords to the dict keywords[instrument] = instrument_keywords(instrument, caom=caom) logging.info( 'Completed database search for {} instruments and {} data products.'. format(instruments, dataproducts)) # Make the table all_cols = ['instrument'] + dataproducts + ['total'] table = pd.DataFrame(inventory, columns=all_cols) # Plot it if plot: # Determine plot location and names output_dir = get_config()['outputs'] if caom: output_filename = 'database_monitor_caom' else: output_filename = 'database_monitor_jwst' # Make the plot plt = bar_chart(table, 'instrument', dataproducts, title="JWST Inventory") # Save the plot as full html html_filename = output_filename + '.html' outfile = os.path.join(output_dir, 'monitor_mast', html_filename) output_file(outfile) save(plt) set_permissions(outfile) logging.info( 'Saved Bokeh plots as HTML file: {}'.format(html_filename)) # Save the plot as components plt.sizing_mode = 'stretch_both' script, div = components(plt) div_outfile = os.path.join(output_dir, 'monitor_mast', output_filename + "_component.html") with open(div_outfile, 'w') as f: f.write(div) f.close() set_permissions(div_outfile) script_outfile = os.path.join(output_dir, 'monitor_mast', output_filename + "_component.js") with open(script_outfile, 'w') as f: f.write(script) f.close() set_permissions(script_outfile) logging.info( 'Saved Bokeh components files: {}_component.html and {}_component.js' .format(output_filename, output_filename)) # Melt the table table = pd.melt(table, id_vars=['instrument'], value_vars=dataproducts, value_name='files', var_name='dataproduct') return table, keywords