Skip to content

Commit

Permalink
Noisy cast for numerical titles (fixes fossasia#353)
Browse files Browse the repository at this point in the history
Summary:
Added a noisy cast for the case of numerical titles ONLY, this means that things like `opts={'title'=torch.tensor(10)}` are not covered here and are catched instead by an assertion error. A direct cast for these cases would depend on the string representation of the tensor/ndarray, which I don't think is generally desired. However adding a special cast for the case of scalar values  might be worth it.
I also didn't know if/where to put tests other than the demo, which I don't think is a good idea since its better that users give strings as titles from the beginning.
Finally, I've never used lua before, a quick glance to the api showed me that the data is sent to the tornado server which unpacks the lua values into python, however I didn't quite get if it then calls the corresponding function in python or not.
Closes fossasia#360

Differential Revision: D8102332

Pulled By: JackUrb

fbshipit-source-id: 521b97e670484f7d567b5e2c9d54a2bc79175503
  • Loading branch information
RicCu authored and facebook-github-bot committed May 22, 2018
1 parent fbec551 commit d5dc9fb
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions py/visdom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ def loadfile(filename):
return str


def _title2str(opts):
if opts.get('title'):
if isnum(opts.get('title')):
title = str(opts.get('title'))
logger.warn('Numerical title %s has been casted to a string' % \
title)
opts['title'] = title
return opts
else:
return opts


def _scrub_dict(d):
if type(d) is dict:
return {k: _scrub_dict(v) for k, v in list(d.items())
Expand Down Expand Up @@ -225,6 +237,9 @@ def _assert_opts(opts):
assert isnum(opts.get('fps')), 'fps should be a number'
assert opts.get('fps') > 0, 'fps must be greater than 0'

if opts.get('title'):
assert isstr(opts.get('title')), 'title should be a string'


def pytorch_wrap(fn):
def result(*args, **kwargs):
Expand Down Expand Up @@ -499,6 +514,7 @@ def text(self, text, win=None, env=None, opts=None, append=False):
No specific `opts` are currently supported.
"""
opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)
data = [{'content': text, 'type': 'text'}]

Expand All @@ -521,6 +537,7 @@ def svg(self, svgstr=None, svgfile=None, win=None, env=None, opts=None):
`opts`.
"""
opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)

if svgfile is not None:
Expand All @@ -537,6 +554,7 @@ def matplot(self, plot, opts=None, env=None, win=None):
any plot-specific `opts`.
"""
opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)

# write plot to SVG buffer:
Expand Down Expand Up @@ -564,6 +582,7 @@ def image(self, img, win=None, env=None, opts=None):
uint8 in [0, 255].
"""
opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)
opts['width'] = opts.get('width', img.shape[img.ndim - 1])
opts['height'] = opts.get('height', img.shape[img.ndim - 2])
Expand Down Expand Up @@ -658,6 +677,7 @@ def audio(self, tensor=None, audiofile=None, win=None, env=None, opts=None):
"""
opts = {} if opts is None else opts
opts['sample_frequency'] = opts.get('sample_frequency', 44100)
_title2str(opts)
_assert_opts(opts)
assert tensor is not None or audiofile is not None, \
'should specify audio tensor or file'
Expand Down Expand Up @@ -700,6 +720,7 @@ def video(self, tensor=None, videofile=None, win=None, env=None, opts=None):
"""
opts = {} if opts is None else opts
opts['fps'] = opts.get('fps', 25)
_title2str(opts)
_assert_opts(opts)
assert tensor is not None or videofile is not None, \
'should specify video tensor or file'
Expand Down Expand Up @@ -854,6 +875,7 @@ def scatter(self, X, Y=None, win=None, env=None, opts=None, update=None,
assert len(L) == X.shape[0], \
'textlabels and X should have same shape'

_title2str(opts)
_assert_opts(opts)

if opts.get('legend'):
Expand Down Expand Up @@ -975,6 +997,7 @@ def line(self, Y, X=None, win=None, env=None, opts=None, update=None,
opts['fillarea'] = opts.get('fillarea', False)
opts['mode'] = 'lines+markers' if opts.get('markers') else 'lines'

_title2str(opts)
_assert_opts(opts)

if Y.ndim == 1:
Expand Down Expand Up @@ -1009,6 +1032,7 @@ def heatmap(self, X, win=None, env=None, opts=None):
opts['xmin'] = opts.get('xmin', np.asscalar(X.min()))
opts['xmax'] = opts.get('xmax', np.asscalar(X.max()))
opts['colormap'] = opts.get('colormap', 'Viridis')
_title2str(opts)
_assert_opts(opts)

if opts.get('columnnames') is not None:
Expand Down Expand Up @@ -1072,6 +1096,7 @@ def bar(self, X, Y=None, win=None, env=None, opts=None):
opts = {} if opts is None else opts
opts['stacked'] = opts.get('stacked', False)

_title2str(opts)
_assert_opts(opts)

if opts.get('rownames') is not None:
Expand Down Expand Up @@ -1117,6 +1142,7 @@ def histogram(self, X, win=None, env=None, opts=None):

opts = {} if opts is None else opts
opts['numbins'] = opts.get('numbins', min(30, len(X)))
_title2str(opts)
_assert_opts(opts)

minx, maxx = X.min(), X.max()
Expand Down Expand Up @@ -1147,6 +1173,7 @@ def boxplot(self, X, win=None, env=None, opts=None):
X = X[:, None]

opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)

if opts.get('legend') is not None:
Expand Down Expand Up @@ -1194,6 +1221,7 @@ def _surface(self, X, stype, win=None, env=None, opts=None):
opts['xmin'] = opts.get('xmin', X.min())
opts['xmax'] = opts.get('xmax', X.max())
opts['colormap'] = opts.get('colormap', 'Viridis')
_title2str(opts)
_assert_opts(opts)

data = [{
Expand Down Expand Up @@ -1273,6 +1301,7 @@ def quiver(self, X, Y, gridX=None, gridY=None,
opts = {} if opts is None else opts
opts['mode'] = 'lines'
opts['arrowheads'] = opts.get('arrowheads', True)
_title2str(opts)
_assert_opts(opts)

# normalize vectors to unit length:
Expand Down Expand Up @@ -1365,6 +1394,7 @@ def stem(self, X, Y=None, win=None, env=None, opts=None):

opts = {} if opts is None else opts
opts['mode'] = 'lines'
_title2str(opts)
_assert_opts(opts)

return self.scatter(X=data, Y=labels, opts=opts, win=win, env=env)
Expand All @@ -1384,6 +1414,7 @@ def pie(self, X, win=None, env=None, opts=None):
'X cannot contain negative values'

opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)

data = [{
Expand Down Expand Up @@ -1411,6 +1442,7 @@ def mesh(self, X, Y=None, win=None, env=None, opts=None):
- `opts.opacity`: opacity of polygons (`number` between 0 and 1)
"""
opts = {} if opts is None else opts
_title2str(opts)
_assert_opts(opts)

X = np.asarray(X)
Expand Down

0 comments on commit d5dc9fb

Please sign in to comment.