<jupyter_start><jupyter_text>Using DirectView and LoadBalancedView together We mentioned that you can use any number of views of any sort simultaneously.The most common model for doing this is to use a DirectView to initialise the engine namespaces(either defining functions and variables, or loading datasets) with a DirectView,and then submitting work as tasks via the LoadBalancedView.For this example, we are going to define a few functions for fetching data and getting a simple summary on some stocks,then we are going to run those functions on a series of stocks in a load-balanced way. The usual boilerplate:<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import ipyparallel as parallel

rc = parallel.Client()
all_engines = rc[:]
lbv = rc.load_balanced_view()<jupyter_output><empty_output><jupyter_text>Use matplotlib to fetch data from YahooNote that we are defining these functions on the engines<jupyter_code>%%px

from io import BytesIO

from matplotlib import finance

def get_yahoo_data(name=None, start=None, end=None):
    """return historical stock data from yahoo as a recarray."""
    fp = finance.fetch_historical_yahoo(name, start, end)
    return finance.parse_yahoo_historical_ochl(fp, asobject=True)

def relative_close(records):
    """return the reative change in closing price over the interval"""
    
    close = records['aclose']
    return (close[-1] - close[0]) / close[0]<jupyter_output><empty_output><jupyter_text>Setup the timestamps.  We want the past year<jupyter_code>%%px
import datetime
end = datetime.datetime.now()
start = end - datetime.timedelta(days=365)<jupyter_output><empty_output><jupyter_text>Our ticker of stocks to check<jupyter_code>ticker = ['AAPL', 'GOOG', 'MSFT', 'ORCL', 'AMZN', 'INTC', 'ATVI', 'EA', 'NFLX']<jupyter_output><empty_output><jupyter_text>And map a simple function (that depends on the code we have defined on the engines)onto our ticker.<jupyter_code>def year_over_year(name):
    """fetch data, and return year-over-year change (relative to first close)
 
    in percent
    """
    stock = get_yahoo_data(name, start, end)
    return relative_close(stock) * 100

amr = lbv.map(year_over_year, ticker)
amr
fig, ax = plt.subplots()
fig.set_figwidth(10)
ax.bar(range(len(amr)), amr)
ax.set_xticks(np.arange(len(amr))+0.4)
ax.set_xticklabels(ticker)
ax.axhline(0, c='k');<jupyter_output><empty_output><jupyter_text>Plotting stock data in parallel with Pandas<jupyter_code>%%px
%matplotlib inline
import matplotlib.pyplot as plt
try:
    from pandas_datareader import DataReader
except ImportError:
    from pandas.io.data import DataReader
from datetime import datetime
def plot_stock_since(name, since_when, key="Adj Close"):
    data = DataReader(name, "yahoo", since_when)
    data[key].plot()
    plt.title(name)
    plt.show()
    
start = datetime(2011,1,1)
plot_stock_since('GOOG', start)
%%px
try:
    from pandas_datareader import DataReader
except ImportError:
    from pandas.io.data import DataReader
all_engines.scatter('ticker', ticker)
%px print(ticker)
all_engines['plot_stock_since'] = plot_stock_since
%%px
from datetime import datetime
start = datetime(2011,1,1)
from pandas.io.data import DataReader

for stock in ticker:
    plot_stock_since(stock, start)
amr = lbv.map(plot_stock_since, ticker, [start]*len(ticker))
amr
%time amr.wait()
amr.display_outputs()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09748243559718969, 'fraction_numerical': 0.007611241217798595, 'mean_word_length': 5.79324055666004, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '417616', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1111, 'n_words': 338, 'path': 'parallel/tutorial/All Together.ipynb', 'repo_name': 'minrk/ipython-cse17', 'size': '3416'}