forked from LGSInnovations/jupyter-sigplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigplot.py
505 lines (403 loc) · 17.3 KB
/
sigplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/env python
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)
import errno
import os
import sys
import ipywidgets as widgets
import numpy as np
import requests
from traitlets import (
Unicode,
Bool,
Dict,
Float,
)
from ._version import __version__ as version_string
from IPython.display import display
_py3k = sys.version_info[0] == 3
if _py3k:
StringType = (str, bytes)
else:
StringType = (basestring,)
class Plot(widgets.DOMWidget):
"""Name and version information required by widgets"""
_view_module_version = Unicode(version_string)
_view_name = Unicode('SigPlotView').tag(sync=True)
_model_name = Unicode('SigPlotModel').tag(sync=True)
_view_module = Unicode('jupyter_sigplot').tag(sync=True)
_model_module = Unicode('jupyter_sigplot').tag(sync=True)
"""The command and arguments that will get sent"""
command_and_arguments = Dict().tag(sync=True)
"""The plot_options dictionary in the JS
sigplot.Plot(dom_element, plot_options)"""
plot_options = Dict().tag(sync=True)
"""Progress information for the client"""
progress = Float().tag(sync=True)
done = Bool(False).tag(sync=True)
"""Sequence of callables used by ``_prepare_file_input``
to resolve relative pathnames"""
path_resolvers = []
def __init__(self, *args, **kwargs):
super(Plot, self).__init__(**kwargs)
# Where to look for data, and where to cache/symlink remote resources
# that the server or client cannot access directly. Note that changing
# the kernel's current directory affects data_dir if it is set as a
# relative path.
self.data_dir = kwargs.pop('data_dir', '')
if 'path_resolvers' in kwargs:
# Don't use pop()+default because we don't want to override class-
# level values when not specified here, and we do want to allow
# specifying None to remove any resolvers.
#
# Note that instance-level resolvers will override class-level
# resolvers per Python semantics.
self.path_resolvers = kwargs.pop('path_resolvers')
# Whatever's left is meant for sigplot.js's ``sigplot.Plot``
self.plot_options = kwargs
display(self)
def __getattr__(self, attr):
"""Enables a "thin-wrapper" around sigplot.Plot (JS)
methods
Note: ``__getattr__`` is only called if ``attr`` is not
an attribute of ``self``
:param attr: An attribute or method
:type attr: str
:return: A function wrapper around ``self.send_command``
:rtype: function
:raises AttributeError: if ``attr`` is not an attribute of
``self`` or in ``available_commands``
"""
if attr in self.available_commands:
# if it doesn't have the attribute,
# but ``attr`` exists as sigplot.Plot methods
# that we've whitelisted in ``self.available_commands``,
# send that command (``attr``) and the arguments
# provded to the client to handle (via ``send_command``)
def wrapper(*args, **kwargs):
command = attr
arguments = args
self.send_command(command, list(arguments), **kwargs)
else:
# if ``attr`` is not an attribute of ``self``
# AND does not exist on the client, throw the standard
# ``AttributeError``
raise AttributeError(attr)
return wrapper
@property
def available_commands(self):
"""Available commands from Sigplot.js that Jupyter-SigPlot can call"""
return [
'change_settings',
'overlay_href',
'overlay_array',
]
def send_command(self, command, arguments, **_):
"""Sends the Notebook client (JS) the SigPlot.js
command and relevant arguments.
:param command: Command for the ``sigplot.Plot`` JS
object to run
:type command: str
:param arguments: The tuple of the positional arguments and
keyword arguments for SigPlot to run
:type arguments: list(Any)
:Example:
>>> from jupyter_sigplot.sigplot import Plot
>>> plt = Plot()
>>> plt.send_command('overlay_href', ['foo.tmp'])
"""
# lower the command, just so we're normalized
command = command.lower()
# we need to convert the array argument to numpy arrays
if command == 'overlay_array':
array = np.array(arguments[0])
# make sure np.dtype is something sigplot can plot
if not np.issubdtype(array.dtype, np.number):
raise TypeError(
'Array passed to overlay_array must be numeric type'
)
arguments[0] = memoryview(array.astype(np.float32))
# cause the sync to happen
self.sync_command_and_arguments({
"command": command,
"arguments": arguments,
})
elif command == 'overlay_href':
# we still need to download the hrefs locally
# to avoid CORS
href = arguments[0]
href_list = _prepare_href_input(
href,
self.data_dir,
self.progress,
self.path_resolvers
)
for href in href_list:
arguments[0] = href
# cause the sync to happen
# TODO: Figure out why the list comp works
# but passing `arguments` doesn't;
# perhaps it's an addressing issue?
self.sync_command_and_arguments({
"command": command,
"arguments": [arg for arg in arguments],
})
else:
# cause the sync to happen
self.sync_command_and_arguments({
"command": command,
"arguments": arguments,
})
def sync_command_and_arguments(self, command_and_arguments):
"""
:param command_and_arguments:
:type command_and_arguments: dict
:return:
"""
self.command_and_arguments = command_and_arguments
# End of class SigPlot
###########################################################################
def _require_dir(d):
"""Creates the path ``d`` similar to ``mkdir -p``
:param d: Path to create
:type d: Union[str, Path]
:raises: OSError if an error occurs (aside from
the path already existing)
:Examples:
>>> from jupyter_sigplot.sigplot import _require_dir
>>> import os
>>> import shutil
>>> path = '/tmp/foo/bar/baz/1/2/3'
>>> _require_dir(path)
>>> os.path.exists(path)
True
>>> shutil.rmtree('/tmp/foo')
>>> os.path.exists(path)
False
"""
if d == '':
# makedirs fails on ''
d = '.'
try:
os.makedirs(d)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def _local_name_for_href(url, local_dir):
"""Generate a name for the given url under directory ``local_dir``
:param url: URL that we'll be downloading to some local directory
``local_dir``
:type url: str
:param local_dir: Local directory where URL
:type local_dir: str
:return: A path under ``local_dir`` suitable for storing the contents
of ``url``
:rtype: str or Path
.. note:: Different ``url`` values may map to the same local path
"""
# This function has no side effects, unlike its primary caller,
# _prepare_http_input . The goal is to make testing easier.
if not isinstance(url, StringType):
raise TypeError("url must be of type str (%r has type %s)" %
(url, type(url)))
if not isinstance(local_dir, StringType):
raise TypeError("local_dir must be of type str (%r has type %s)" %
(local_dir, type(local_dir)))
if not url:
raise ValueError("Path %r is not a valid filename" % url)
# TODO (sat 2018-11-07): Note that a URL with a query string
# will result in an odd filename. Better to split the URL
# more completely, perhaps with urlparse.urlsplit followed by
# this split on '/'
basename = url.split('/')[-1]
local_path = os.path.join(local_dir, basename)
# TODO (sat 2018-11-07): Either deconflict this path, or
# decide explicitly that we don't need to
return local_path
def _prepare_http_input(url, local_dir, progress=None):
"""Given a URI, fetch the named resource to a file in ``local_dir``,
to avoid CORS issues.
:param url: URL that we'll be downloading to some local directory
``local_dir``
:type url: str
:param local_dir: Local directory to where URL will be downloaded
:type local_dir: str
:param progress: Progress traitlet that will sync with the client
:type progress: Optional[traitlets.Float]
:return: A filename in the local filesystem, under <local_dir>
"""
_require_dir(local_dir)
# get where the ``url`` will be downloaded to under ``local_dir``
local_fname = _local_name_for_href(url, local_dir)
# `stream=True` lets us stream over the response
r = requests.get(url, stream=True)
# get the total file size
total_size = int(r.headers.get('content-length', 0))
# we'll want to iterate over the file by chunks
block_size = 1024
# how much we've written locally (kernel-side)
wrote = 0
# "stream" the remote asset to ``local_file``
with open(local_fname, 'wb') as f:
for data in r.iter_content(block_size):
# keep track of how much we've written
f.write(data)
wrote += len(data)
# update the ``progress`` traitlet, which
# we will handle on the client-side in some loading
# notification (e.g., loading bar via tqdm?, spinny wheel, etc.)
if progress is not None:
progress = wrote / total_size
# TODO: Make sure we do the right thing if ``local_dir`` is an
# absolute path, doesn't exist, etc.
#
# The client side of the widget will automatically look for a path
# relative to ``local_dir``
return local_fname
def _unravel_path(path, resolvers=None):
"""Expand user directories and environment variables in ``path``,
then run through callables in ``resolvers``. Does NOT call
``realpath`` / ``abspath`` unless that happens to be in ``resolvers``.
:param path: Path to be unraveled
:type path: str or Path
:param resolvers: Optional list of functions to be applied to ``path``,
e.g., ``os.path.abspath`` or ``os.path.realpath``
:type resolvers: Optional[List[function]]
:return: The unraveled path
:rtype: str
:Example:
>>> from jupyter_sigplot.sigplot import _unravel_path
>>>
"""
unraveled = os.path.expanduser(os.path.expandvars(path))
if resolvers:
for f in resolvers:
unraveled = f(unraveled)
return unraveled
def _local_name_for_file(file_path, local_dir):
"""Generate a name for the given file path under ``local_dir``.
:param file_path: File that will be renamed and placed under ``local_dir``
:type file_path: str
:param local_dir: Directory where ``file_path`` will be placed
:type local_dir: str
:return: tuple (path, is_local) where
``path`` is a path starting at ``local_dir`` suitable for storing
a link to, or the contents of, ``file_path``
``is_local`` is a bool, true iff ``file_path`` is already a
descendant of ``local_dir``
:rtype: Tuple[str, bool]
.. note:: Different ``fname`` values may map to the same local path
.. note:: Expects arguments to already be unraveled.
"""
if not isinstance(file_path, StringType):
raise TypeError("fpath must be of type str (%r has type %s)" %
(file_path, type(file_path)))
if not isinstance(local_dir, StringType):
raise TypeError("local_dir must be of type str (%r has type %s)" %
(local_dir, type(local_dir)))
if not file_path:
raise ValueError("Path %r is not a valid filename" % file_path)
abs_file_path = os.path.realpath(file_path)
abs_local_dir = os.path.realpath(local_dir)
# A bit clunky but works okay for now
if abs_file_path.startswith(abs_local_dir):
is_local = True
local_relative_path = abs_file_path[len(abs_local_dir + os.path.sep):]
else:
is_local = False
local_relative_path = os.path.basename(abs_file_path)
return os.path.join(local_dir, local_relative_path), is_local
def _prepare_file_input(orig_file_name, local_dir, resolvers=None):
"""Given an arbitrary filename, determine whether that file is a child of
``local_dir``. If not, create a symlink under ``local_dir`` that points
to the original file, so the Jupyter server can resolve it.
:param orig_file_name: Some arbitrary file that we want to put under
``local_dir``
:type orig_file_name: str
:param local_dir: The directory where we'll either check if
``orig_file_name`` exists under, or where we'll
link ``orig_file_name``
:type local_dir: str
:param resolvers: sequence of callables to be applied, in order, to
``orig_file_name``. Could be used to normalize case,
look up bare paths in system-specific search paths, etc.
``orig_file_name`` will have environment variables and
user directories expanded before it is given to the
first resolver.
:type resolvers: Optional[Sequence[function]]
:return: A filename in the local filesystem, under ``local_dir``
:rtype: str
"""
input_path = _unravel_path(orig_file_name, resolvers=resolvers)
# TODO: Handle errors more thoroughly
# * unable to make local path
# * symlink already exists, to wrong target
# * original file does not exist / has bad perms
_require_dir(local_dir)
# TODO (sat 2018-11-07): Do the right thing if ``local_dir`` is absolute
local_fname, is_local = _local_name_for_file(input_path, local_dir)
if not is_local:
try:
# Note that ``_unravel_path`` keeps relative names like ../foo.tmp
# will as is, only applying user specifications like ~someone,
# environment variables, and any explicit resolvers. This is
# intended to make links as human-comprehensible as possible.
os.symlink(input_path, local_fname)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return local_fname
def _split_inputs(orig_inputs):
"""Given an input specification containing one or more filesystem paths and
URIs separated by '|', return a list of individual inputs.
* Skips blank entries
* Removes blank space around entries
:param orig_inputs: One or more filesystem paths and/or
URIs separated by '|'
:type orig_inputs: str
:return: List of the individual inputs specified in ``orig_inputs``
:rtype: list(str)
:Example:
>>> from sigplot import _split_inputs
>>> _split_inputs('foo|bar')
['foo', 'bar']
"""
# (sat 2018-11-19): If we want to support direct list inputs, this is the
# place to handle that.
return [ii.strip() for ii in orig_inputs.split('|') if ii.strip()]
def _prepare_href_input(orig_inputs, local_dir, progress=None, resolvers=None):
"""Given an input specification containing one or more filesystem paths and
URIs separated by '|', prepare each one according to its type.
:param orig_inputs: One or more filesystem paths
and/or URIs separated by '|'
:type orig_inputs: str
:param local_dir: Directory where the ``orig_inputs`` will end up
:type local_dir: Optional[str]
:param progress: Optional progress traitlet to provide
feedback to the client
:type progress: Optional[traitlets.Float]
:param resolvers: sequence of callables to be applied, in order, to
``orig_file_name``. Could be used to normalize case,
look up bare paths in system-specific search paths, etc.
``orig_file_name`` will have environment variables and
user directories expanded before it is given to the
first resolver.
:type resolvers: Optional[Sequence[function]]
:return: A list of filenames in the local filesystem, under ``local_dir``
:rtype: list(str)
"""
prepared = []
for oi in _split_inputs(orig_inputs):
if oi.startswith("http"):
pi = _prepare_http_input(oi, local_dir, progress=progress)
else:
# TODO: This `resolvers` argument feels like a bad factoring,
# since only one branch uses it; may want to move
# _prepare_href_input to a class member or else replace
# with a split+dispatch idiom at the point of call.
pi = _prepare_file_input(oi, local_dir, resolvers)
prepared.append(pi)
return prepared