<jupyter_start><jupyter_text>Retrieve metric metadata from SQLite db and metric data from .npz filesNow we are going to make some plots of the output of the previous notebook.  **Important:**  As in the last notebook, in the next cell you need to update the `your_username` variable with **Your Username** (between the single quotes).  After you have done that, in principle, you should be able to run the notebook all at once instead of cell by cell.<jupyter_code>#Please enter your SciServer username between the single quotes below!
your_username = ''
# import matplotlib to show plots inline.
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import glob
import os<jupyter_output><empty_output><jupyter_text>Import the sims_maf modules needed.<jupyter_code># import maf python modules
import lsst.sims.maf.db as db
import lsst.sims.maf.metrics as metrics
import lsst.sims.maf.slicers as slicers
import lsst.sims.maf.stackers as stackers
import lsst.sims.maf.plots as plots
import lsst.sims.maf.metricBundles as metricBundles
# import convenience functions
from opsimUtils import *<jupyter_output><empty_output><jupyter_text>1. Read result SQLite db and read in metric dataEach time you run MAF on an opsim simulation, MAF produces two types of files:1. `*.db`: A SQLite database storing metadata and summary statistics for each metric run on the opsims.2. `*.npz`: A file storing metric values (e.g., those used to generated plots and histograms) per metric and per opsim. The name of this file is also stored in the above `*.db` files.If you followed the [Introduction](./Introduction.ipynb) notebook, metadata for metrics run on each opsim will be stored in separate files (using the convention 'runName_result.db', otherwise all in one 'resultDb_sqlite.db' by default). You can easily retrieve the metadata from those files using the provided conveience functions.**Note:** You can either provide the path to the directory where all the resulting SQLite databases are stored or hard code the paths to each individual result database. To read results stored in files, we need the paths to two directories:1. `resultDbPath`: The path to the directory where the `runName_result.db` metadata databases are stored.2. `metricDataPath`: The path to the directory where the actual metric data (`*.npz` files) are stored.<jupyter_code>if your_username == '': # do NOT put your username here
    raise Exception('Please provide your username! See the top of the notebook.')

# user provided paths
resultDbPath = '/home/idies/workspace/Storage/{}/persistent/MAFOutput'.format(your_username)
metricDataPath = '/home/idies/workspace/Storage/{}/persistent/MAFOutput/MetricData/'.format(your_username)
# get a dictionary of resultDb from given directory
resultDbs = getResultsDbs(resultDbPath)

# the following line will be useful if you did not run MAF on all 75 opsims
runNames = list(resultDbs.keys())
# retrieve metricBundles for each opsim run and store them in a dictionary
bundleDicts = {}

for runName in resultDbs:
    bundleDicts[runName] = bundleDictFromDisk(resultDbs[runName], runName, metricDataPath)<jupyter_output>Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSIDE=64, approximate resolution 54.967783 arcminutes
Healpix slicer using NSID[...]<jupyter_text>BundleDict Keys:If you have gone through all the previous notebooks ([Introduction.ipynb](./00_Introduction.ipynb)) and [Multiple_Opsims.ipynb](./01_Multiple_Opsims.ipynb), you should be familiar with the term 'bundleDict' and 'metricBundle'. A metricBundle is a combination of metric (e.g., CoaddM5), slicer (e.g., healpixSlicer) and sql constraint (e.g., filter = 'g') and a bundleDict is a dictionary of multiple metricBundles. Each item in the `bundleDicts` shown in the cell above is one bundleDict. While reconstructing the bundleDict, we give each metricBundle a tuple key in the format of ('metricId', 'metricName'). metricId is unique while metricName is not, that is you can run same metric using differerent sql constraints. To make sure that we are comparing apples to apples, the `plotHist()` function and `plotSky()` function, which will be introducted below, require the tuple metricKey instead of just the metric name.<jupyter_code># we see the available metric keys
list(bundleDicts[runNames[0]].keys())<jupyter_output><empty_output><jupyter_text>The 2nd and the 3rd have the same metric name, but we can see the difference between the two by retrieving the full metadata using `get_metricMetadata()`. Note that the keys of `bundleDicts`, `resultDbs` are opSim run names.<jupyter_code>print('{}: '.format(runNames[0])) # show the opsim run name where the metric metadata comes from
get_metricMetadata(resultDbs[runNames[0]], metricName='CoaddM5')<jupyter_output>dcr_nham1_ugri_v1.5_10yrs:<jupyter_text>2. Compare opSims using summary statistics You can retrieve all summary statistics stored in the metadata using function `getSummary()`. This function will return a dictionary, each item is an array of summary statistics for the required metric. You can pass `pandas = True` to this function to get a pandas dataframe instead.<jupyter_code>summary = getSummary(resultDbs, 'CoaddM5', 'Rms', pandas=True)
summary<jupyter_output><empty_output><jupyter_text>Next, we use a simple function to generate a bar plot for comparing opsim using the summary statistics.<jupyter_code>plotSummaryBar(resultDbs, 'CoaddM5', 'Rms', slicerName = 'HealpixSlicer')<jupyter_output><empty_output><jupyter_text>Two convenience functions, `get_metricNames` and `get_summaryStatName`, are provided to check what metrics are available and and what summary statistics were run on a given metric.<jupyter_code>get_metricNames(resultDbs[runNames[0]])
getSummaryStatNames(resultDbs[runNames[0]], 'CoaddM5')<jupyter_output><empty_output><jupyter_text>3. Generate plots (hist/sky) using metric dataThe following plotting functions, `plotHist()` and `plotSky()`, are simply an extension to the code provided in the [Introduction](./00_Introduction.ipynb) notebook. These functions are provided for quick visualization, you can easily make better looking plots by using the metric values loaded into metricbundles directly. For how to access the metric values, please look at the [Introduction](./00_Introduction.ipynb) notebook.  Note that both functions require you to provide a **tuple** metric key<jupyter_code># plot hist for both metric of all opsims in one figure
plotHist(bundleDicts, (1, 'Max airmass'))
plotHist(bundleDicts, (2, 'CoaddM5'))
plotHist(bundleDicts, (3, 'CoaddM5'))
# Make sky plots
plotSky(bundleDicts, (1, 'Max airmass'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07372049854150092, 'fraction_numerical': 0.022540440201538052, 'mean_word_length': 6.095954844778928, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '447062', 'license': 'MIT', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2104, 'n_words': 896, 'path': 'Scripts_NBs/02_View_Results.ipynb', 'repo_name': 'RichardsGroup/LSST_OpSim', 'size': '7542'}