diff --git a/tensorforce-PPO-IEET.ipynb b/tensorforce-PPO-IEET.ipynb index 0167064..9309c86 100644 --- a/tensorforce-PPO-IEET.ipynb +++ b/tensorforce-PPO-IEET.ipynb @@ -14,8 +14,8 @@ "execution_count": 1, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.212639Z", - "start_time": "2017-11-11T08:57:18.125754Z" + "end_time": "2017-11-12T04:19:37.423032Z", + "start_time": "2017-11-12T04:19:35.616652Z" }, "collapsed": true }, @@ -56,8 +56,8 @@ "execution_count": 2, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.281388Z", - "start_time": "2017-11-11T08:57:19.214908Z" + "end_time": "2017-11-12T04:19:37.516583Z", + "start_time": "2017-11-12T04:19:37.428893Z" }, "collapsed": true }, @@ -73,8 +73,8 @@ "execution_count": 3, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.310831Z", - "start_time": "2017-11-11T08:57:19.282954Z" + "end_time": "2017-11-12T04:19:40.291461Z", + "start_time": "2017-11-12T04:19:40.259250Z" }, "collapsed": true }, @@ -91,15 +91,15 @@ "execution_count": 4, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.340928Z", - "start_time": "2017-11-11T08:57:19.312693Z" + "end_time": "2017-11-12T04:19:40.501765Z", + "start_time": "2017-11-12T04:19:40.471795Z" } }, "outputs": [ { "data": { "text/plain": [ - "'./outputs/tensorforce-PPO-prioritised/tensorforce-PPO-crypto-20171105_06-50-31.model'" + "'./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31'" ] }, "execution_count": 4, @@ -114,25 +114,34 @@ "batch_size=128\n", "import datetime\n", "ts = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')\n", - "save_path = './outputs/tensorforce-PPO-prioritised/tensorforce-PPO-crypto-%s.model' % ts\n", - "save_path = './outputs/tensorforce-PPO-prioritised/tensorforce-PPO-crypto-20171105_06-50-31.model'\n", + "save_path = './outputs/tensorforce-PPO/tensorforce_PPO_crypto-%s' % ts\n", + "save_path = './outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31'\n", "save_path" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.388816Z", - "start_time": "2017-11-11T08:57:19.343609Z" + "end_time": "2017-11-12T04:19:40.999732Z", + "start_time": "2017-11-12T04:19:40.967934Z" } }, "outputs": [ { "data": { "text/plain": [ - "'logs/tensorforce-PPO-crypto-20171105_06-50-31/run-20171111_08-57-19'" + "'logs/tensorforce_PPO_crypto_20171105_06-50-31/run-20171112_04-19-40'" ] }, "execution_count": 5, @@ -166,8 +175,8 @@ "execution_count": 6, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.460430Z", - "start_time": "2017-11-11T08:57:19.390375Z" + "end_time": "2017-11-12T04:19:41.642642Z", + "start_time": "2017-11-12T04:19:41.548893Z" }, "collapsed": true }, @@ -181,8 +190,8 @@ "execution_count": 7, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.564124Z", - "start_time": "2017-11-11T08:57:19.464441Z" + "end_time": "2017-11-12T04:19:41.869824Z", + "start_time": "2017-11-12T04:19:41.746284Z" } }, "outputs": [], @@ -257,8 +266,8 @@ "execution_count": 8, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.598441Z", - "start_time": "2017-11-11T08:57:19.565641Z" + "end_time": "2017-11-12T04:19:42.782046Z", + "start_time": "2017-11-12T04:19:42.650024Z" }, "collapsed": true }, @@ -274,11 +283,11 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 14, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.748803Z", - "start_time": "2017-11-11T08:57:19.600186Z" + "end_time": "2017-11-12T04:20:24.504699Z", + "start_time": "2017-11-12T04:20:24.446934Z" } }, "outputs": [], @@ -293,29 +302,16 @@ " output_mode='EIIE',\n", ")\n", "env.seed(0)\n", - "environment = CustomOpenAIGym('CryptoPortfolioEIIE-v0', env)\n", - "\n", - "df_test = pd.read_hdf('./data/poloniex_30m.hf',key='test')\n", - "env_test = EnvWrapper(\n", - " df=df_test,\n", - " steps=40, \n", - " scale=True, \n", - " trading_cost=0.0025, \n", - " window_length=window_length,\n", - " output_mode='EIIE',\n", - ")\n", - "env_test.seed(0)\n", - "\n", - "environment_test = CustomOpenAIGym('CryptoPortfolioEIIETest-v0', env_test)" + "environment = CustomOpenAIGym('CryptoPortfolioEIIE-v0', env)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:19.823448Z", - "start_time": "2017-11-11T08:57:19.750421Z" + "end_time": "2017-11-12T04:20:24.955032Z", + "start_time": "2017-11-12T04:20:24.920794Z" } }, "outputs": [ @@ -325,7 +321,7 @@ "(3, 51, 3)" ] }, - "execution_count": 10, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -355,11 +351,11 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 16, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:33.186760Z", - "start_time": "2017-11-11T08:57:19.824998Z" + "end_time": "2017-11-12T04:20:26.232444Z", + "start_time": "2017-11-12T04:20:26.197473Z" }, "collapsed": true }, @@ -385,11 +381,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:33.584950Z", - "start_time": "2017-11-11T08:57:33.188396Z" + "end_time": "2017-11-12T04:20:27.530745Z", + "start_time": "2017-11-12T04:20:27.101913Z" }, "collapsed": true }, @@ -530,11 +526,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:33.623776Z", - "start_time": "2017-11-11T08:57:33.587003Z" + "end_time": "2017-11-12T04:20:27.788540Z", + "start_time": "2017-11-12T04:20:27.750707Z" }, "collapsed": true }, @@ -573,30 +569,69 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 19, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:37.897187Z", - "start_time": "2017-11-11T08:57:33.625546Z" + "end_time": "2017-11-12T04:20:35.334501Z", + "start_time": "2017-11-12T04:20:28.522247Z" }, "scrolled": true }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "INFO:tensorflow:Create CheckpointSaverHook.\n" + ] + }, { "name": "stderr", "output_type": "stream", "text": [ - "WARNING:PPOAgent:Configuration values not accessed: summary_labels, summary_frequency, task_index, distributions, local_model, cluster_spec, replica_model, parameter_server, summary_logdir\n", - "[2017-11-11 16:57:37,867] Configuration values not accessed: summary_labels, summary_frequency, task_index, distributions, local_model, cluster_spec, replica_model, parameter_server, summary_logdir\n" + "INFO:tensorflow:Create CheckpointSaverHook.\n", + "[2017-11-12 12:20:32,888] Create CheckpointSaverHook.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "INFO:tensorflow:Restoring parameters from ./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31/model.ckpt-1820000\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:tensorflow:Restoring parameters from ./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31/model.ckpt-1820000\n", + "[2017-11-12 12:20:34,362] Restoring parameters from ./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31/model.ckpt-1820000\n", + "WARNING:PPOAgent:Configuration values not accessed: summary_logdir, distributions, parameter_server, local_model, cluster_spec, replica_model, summary_labels, summary_frequency, task_index\n", + "[2017-11-12 12:20:34,641] Configuration values not accessed: summary_logdir, distributions, parameter_server, local_model, cluster_spec, replica_model, summary_labels, summary_frequency, task_index\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "INFO:tensorflow:Saving checkpoints for 1820000 into ./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31/model.ckpt.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:tensorflow:Saving checkpoints for 1820000 into ./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31/model.ckpt.\n", + "[2017-11-12 12:20:34,671] Saving checkpoints for 1820000 into ./outputs/tensorforce-PPO/tensorforce_PPO_crypto_20171105_06-50-31/model.ckpt.\n" ] }, { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 14, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -626,7 +661,7 @@ " # PPOAgent\n", " step_optimizer=dict(\n", " type='adam',\n", - " learning_rate = 3e-5, # float of learning rate (alpha). (3e-4 in paper 1e-3 (atari) and 3e-4 in baselines)\n", + " learning_rate = 1e-3, # float of learning rate (alpha). (3e-4 in paper 1e-3 (atari) and 3e-4 in baselines)\n", " ),\n", " optimization_steps=4,\n", " \n", @@ -634,10 +669,14 @@ " # https://github.com/reinforceio/tensorforce/blob/master/tensorforce/models/model.py#L33\n", " scope='ppo',\n", " discount = 0.97, # float of discount factor (gamma).\n", + " saver_spec = dict(\n", + " directory=save_path, \n", + " steps=100000, \n", + "# basename=os.path.basename(save_path)\n", + " ),\n", " \n", " # DistributionModel\n", " distributions=dict(action=dict(type='gaussian', mean=0.25, log_stddev=np.log(5e-2))),\n", - "# distributions=dict(type='beta', min_value, max_value, alpha=0.0, beta=0.0)\n", " entropy_regularization=0.01, # 0 and 0.01 in baselines\n", " \n", " # PGModel\n", @@ -648,7 +687,7 @@ " layers_spec=network_spec\n", "# update_batch_size=512,\n", " ), # string indicating the baseline value function (currently 'linear' or 'mlp').\n", - " baseline_optimizer=dict(type='adam', learning_rate=0.01),\n", + " baseline_optimizer=dict(type='adam', learning_rate=0.003),\n", " gae_lambda=0.5,\n", " normalize_rewards=False,\n", " \n", @@ -669,7 +708,8 @@ " task_index=0,\n", " device=None,\n", " local_model=False,\n", - " replica_model=False,)\n", + " replica_model=False,\n", + ")\n", "\n", "# I want to use a gaussian dist instead of beta, we will apply post processing to scale everything\n", "actions_spec = environment.actions.copy()\n", @@ -686,22 +726,6 @@ "agent" ] }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "ExecuteTime": { - "end_time": "2017-11-11T08:57:37.928843Z", - "start_time": "2017-11-11T08:57:37.898942Z" - }, - "collapsed": true - }, - "outputs": [], - "source": [ - "from tensorforce.execution import Runner\n", - "runner = Runner(agent=agent, environment=environment)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -713,11 +737,11 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 20, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:57:37.962817Z", - "start_time": "2017-11-11T08:57:37.931246Z" + "end_time": "2017-11-12T04:20:35.402423Z", + "start_time": "2017-11-12T04:20:35.337389Z" }, "collapsed": true }, @@ -741,26 +765,33 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "ExecuteTime": { - "end_time": "2017-11-11T08:58:14.121992Z", - "start_time": "2017-11-11T08:57:37.965113Z" + "start_time": "2017-11-12T04:20:29.952Z" + }, + "collapsed": true + }, + "outputs": [], + "source": [ + "from tensorforce.execution import Runner\n", + "runner = Runner(agent=agent, environment=environment)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "start_time": "2017-11-12T04:20:30.154Z" }, "scrolled": true }, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Widget Javascript not detected. It may not be installed or enabled properly.\n" - ] - }, { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "70cd1b78806646beb606f3c218fbe935" + "model_id": "13880b8b2e03414ca09005a18632a58b" } }, "metadata": {}, @@ -770,2403 +801,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "TensorBoardLogger started. Run `tensorboard --logdir=/media/oldhome/wassname/Documents/projects/rl-portfolio-gh/rl-portfolio-management/logs/tensorforce-PPO-crypto-20171105_06-50-31` to visualize\n", - "ep reward: -0.00004773 [-0.00032387, 0.00027298], portfolio_value: 0.9247 mdd=-2.42% sharpe=-2.0635, expl= 99.60% eps=10 weights={'DASHBTC': 0.6171000003814697, 'LTCBTC': 0.24709999561309814, 'XMRBTC': 0.08590000122785568}\n" - ] - }, - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", - " button.click(method_name, toolbar_event);\n", - " button.mouseover(tooltip, toolbar_mouse_event);\n", - " nav_element.append(button);\n", - " }\n", - "\n", - " // Add the status bar.\n", - " var status_bar = $('');\n", - " nav_element.append(status_bar);\n", - " this.message = status_bar[0];\n", - "\n", - " // Add the close button to the window.\n", - " var buttongrp = $('
');\n", - " var button = $('');\n", - " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", - " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", - " buttongrp.append(button);\n", - " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", - " titlebar.prepend(buttongrp);\n", - "}\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(el){\n", - " var fig = this\n", - " el.on(\"remove\", function(){\n", - "\tfig.close_ws(fig, {});\n", - " });\n", - "}\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(el){\n", - " // this is important to make the div 'focusable\n", - " el.attr('tabindex', 0)\n", - " // reach out to IPython and tell the keyboard manager to turn it's self\n", - " // off when our div gets focus\n", - "\n", - " // location in version 3\n", - " if (IPython.notebook.keyboard_manager) {\n", - " IPython.notebook.keyboard_manager.register_events(el);\n", - " }\n", - " else {\n", - " // location in version 2\n", - " IPython.keyboard_manager.register_events(el);\n", - " }\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._key_event_extra = function(event, name) {\n", - " var manager = IPython.notebook.keyboard_manager;\n", - " if (!manager)\n", - " manager = IPython.keyboard_manager;\n", - "\n", - " // Check for shift+enter\n", - " if (event.shiftKey && event.which == 13) {\n", - " this.canvas_div.blur();\n", - " // select the cell after this one\n", - " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", - " IPython.notebook.select(index + 1);\n", - " }\n", - "}\n", - "\n", - "mpl.figure.prototype.handle_save = function(fig, msg) {\n", - " fig.ondownload(fig, null);\n", - "}\n", - "\n", - "\n", - "mpl.find_output_cell = function(html_output) {\n", - " // Return the cell and output element which can be found *uniquely* in the notebook.\n", - " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", - " // IPython event is triggered only after the cells have been serialised, which for\n", - " // our purposes (turning an active figure into a static one), is too late.\n", - " var cells = IPython.notebook.get_cells();\n", - " var ncells = cells.length;\n", - " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", - " data = data.data;\n", - " }\n", - " if (data['text/html'] == html_output) {\n", - " return [cell, data, j];\n", - " }\n", - " }\n", - " }\n", - " }\n", - "}\n", - "\n", - "// Register the function which deals with the matplotlib target/channel.\n", - "// The kernel may be null if the page has been refreshed.\n", - "if (IPython.notebook.kernel != null) {\n", - " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", - "}\n" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('