From a8d174c7cbef1f7e04511f5027df3de1f73fae5c Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph@weston.cloud> Date: Wed, 12 Jun 2019 10:26:09 +0200 Subject: [PATCH] disallow incompatible types for size/color specifications Raise a TypeError when a site size/color is provided as an array and the system is a kwant.Builder, as the site ordering for the latter is not well defined. A function should be used instead. --- kwant/plotter.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kwant/plotter.py b/kwant/plotter.py index d3bb4493..222af796 100644 --- a/kwant/plotter.py +++ b/kwant/plotter.py @@ -911,7 +911,11 @@ def plot(sys, num_lead_cells=2, unit='nn', raise ValueError('Invalid value of unit argument.') # make all specs proper: either constant or lists/np.arrays: - def make_proper_site_spec(spec, fancy_indexing=False): + def make_proper_site_spec(spec_name, spec, fancy_indexing=False): + if _p.isarray(spec) and isinstance(syst, builder.Builder): + raise TypeError('{} cannot be an array when plotting' + ' a Builder; use a function instead.' + .format(spec_name)) if callable(spec): spec = [spec(i[0]) for i in sites if i[1] is None] if (fancy_indexing and _p.isarray(spec) @@ -933,7 +937,8 @@ def plot(sys, num_lead_cells=2, unit='nn', spec = np.asarray(spec, dtype='object') return spec - site_symbol = make_proper_site_spec(site_symbol) + + site_symbol = make_proper_site_spec('site_symbol', site_symbol) if site_symbol is None: site_symbol = defaults['site_symbol'][dim] # separate different symbols (not done in 3D, the separation # would mess up sorting) @@ -967,10 +972,10 @@ def plot(sys, num_lead_cells=2, unit='nn', # Unknown finalized system, no sites access. site_color = defaults['site_color'][dim] - site_size = make_proper_site_spec(site_size, fancy_indexing) - site_color = make_proper_site_spec(site_color, fancy_indexing) - site_edgecolor = make_proper_site_spec(site_edgecolor, fancy_indexing) - site_lw = make_proper_site_spec(site_lw, fancy_indexing) + site_size = make_proper_site_spec('site_size', site_size, fancy_indexing) + site_color = make_proper_site_spec('site_color', site_color, fancy_indexing) + site_edgecolor = make_proper_site_spec('site_edgecolor', site_edgecolor, fancy_indexing) + site_lw = make_proper_site_spec('site_lw', site_lw, fancy_indexing) hop_color = make_proper_hop_spec(hop_color) hop_lw = make_proper_hop_spec(hop_lw) -- GitLab