<jupyter_start><jupyter_text>We saw in this [journal entry](http://wiki.noahbrenowitz.com/doku.php?id=journal:2018-10:day-2018-10-24run_110) that multiple-step trained neural network gives a very imbalanced estimate, but the two-step trained neural network gives a good answer. Where do these two patterns disagree?<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import xarray as xr
import click
import torch
from uwnet.model import call_with_xr
import holoviews as hv
from holoviews.operation import decimate
hv.extension('bokeh')


def column_integrate(data_array, mass):
    return (data_array * mass).sum('z')


def compute_apparent_sources(model_path, ds):
    model = torch.load(model_path)
    
    return call_with_xr(model, ds, drop_times=0)

def get_single_location(ds, location=(32,0)):
    y, x = location
    return ds.isel(y=slice(y,y+1), x=slice(x,x+1))

def dict_to_dataset(datasets, dim='key'):
    """Concatenate a dict of datasets along a new axis"""
    keys, values = zip(*datasets.items())
    idx = pd.Index(keys, name=dim)
    return xr.concat(values, dim=idx)

def dataarray_to_table(dataarray):
    return dataarray.to_dataset('key').to_dataframe().reset_index()

def get_apparent_sources(model_paths, data_path):
    ds = xr.open_dataset(data_path)
    location = get_single_location(ds, location=(32,0))
    sources = {training_strategy: compute_apparent_sources(model_path, location)
              for training_strategy, model_path in model_paths.items()}
    return dict_to_dataset(sources)
model_paths = {
    'multi': '../models/113/3.pkl',
    'single': '../models/110/3.pkl'
}

data_path = "../data/processed/training.nc"

sources = get_apparent_sources(model_paths, data_path)<jupyter_output><empty_output><jupyter_text>Apparent moistening and heatingHere we scatter plot the apparent heating and moistening:<jupyter_code>%%opts Scatter[width=500, height=500, color_index='z'](cmap='viridis', alpha=.2)
%%opts Curve(color='black')

lims = (-30, 40)
df = dataarray_to_table(sources.QT)
moisture_source = hv.Scatter(df, kdims=["multi", "single"]).groupby('z').redim.range(multi=lims, single=lims) \
        *hv.Curve((lims, lims))

lims = (-30, 40)
df = dataarray_to_table(sources.SLI)
heating = hv.Scatter(df, kdims=["multi", "single"]).groupby('z').redim.range(multi=lims, single=lims) \
            *hv.Curve((lims, lims))


moisture_source.relabel("Moistening (g/kg/day)") + heating.relabel("Heating (K/day)")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13758523866827116, 'fraction_numerical': 0.01965503409546731, 'mean_word_length': 5.427835051546392, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '10407', 'license': 'MIT', 'n_tokens_mistral': 919, 'n_tokens_neox': 884, 'n_words': 181, 'path': 'notebooks/2018-10-25-compare-multiple-step-vs-single-step.ipynb', 'repo_name': 'kbren/uwnet', 'size': '2493'}