edbennett / performant-numpy

A lesson on ways to write Numpy code that is performant
Other
1 stars 2 forks source link

error running part 4/5 of the "Array broadcasts for image manipulation" exercise #2

Closed colinsauze closed 5 years ago

colinsauze commented 5 years ago

I get the following error when running part 4/5 of the Array broadcasts for image manipulation solution. Root cause of the exception claims to be values outside the 0-1 range, but I tested the image.min() and image.max() and they are in the range 0 to 1.

File I used for testing:

1170933903

I am running a somewhat dated Python 3.6.4 with Anaconda 5.1.0.

ValueError Traceback (most recent call last) ~/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in call(self, obj) 339 pass 340 else: --> 341 return printer(obj) 342 # Finally look for special method names 343 method = get_real_method(obj, self.print_method)

~/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in (fig) 236 237 if 'png' in formats: --> 238 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', kwargs)) 239 if 'retina' in formats or 'png2x' in formats: 240 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, kwargs))

~/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, kwargs) 120 121 bytes_io = BytesIO() --> 122 fig.canvas.print_figure(bytes_io, kw) 123 data = bytes_io.getvalue() 124 if fmt == 'svg':

~/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, kwargs) 2214 orientation=orientation, 2215 dryrun=True, -> 2216 kwargs) 2217 renderer = self.figure._cachedRenderer 2218 bbox_inches = self.figure.get_tightbbox(renderer)

~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, *kwargs) 505 506 def print_png(self, filename_or_obj, args, **kwargs): --> 507 FigureCanvasAgg.draw(self) 508 renderer = self.get_renderer() 509 original_dpi = renderer.dpi

~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in draw(self) 428 # if toolbar: 429 # toolbar.set_cursor(cursors.WAIT) --> 430 self.figure.draw(self.renderer) 431 finally: 432 # if toolbar:

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, *kwargs) 53 renderer.start_filter() 54 ---> 55 return draw(artist, renderer, args, **kwargs) 56 finally: 57 if artist.get_agg_filter() is not None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py in draw(self, renderer) 1297 1298 mimage._draw_list_compositing_images( -> 1299 renderer, self, artists, self.suppressComposite) 1300 1301 renderer.close_group('figure')

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite) 136 if not_composite or not has_images: 137 for a in artists: --> 138 a.draw(renderer) 139 else: 140 # Composite any adjacent images together

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, *kwargs) 53 renderer.start_filter() 54 ---> 55 return draw(artist, renderer, args, **kwargs) 56 finally: 57 if artist.get_agg_filter() is not None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe) 2435 renderer.stop_rasterizing() 2436 -> 2437 mimage._draw_list_compositing_images(renderer, self, artists) 2438 2439 renderer.close_group('axes')

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite) 136 if not_composite or not has_images: 137 for a in artists: --> 138 a.draw(renderer) 139 else: 140 # Composite any adjacent images together

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, *kwargs) 53 renderer.start_filter() 54 ---> 55 return draw(artist, renderer, args, **kwargs) 56 finally: 57 if artist.get_agg_filter() is not None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in draw(self, renderer, *args, **kwargs) 564 else: 565 im, l, b, trans = self.make_image( --> 566 renderer, renderer.get_image_magnification()) 567 if im is not None: 568 renderer.draw_image(gc, l, b, im)

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in make_image(self, renderer, magnification, unsampled) 791 return self._make_image( 792 self._A, bbox, transformed_bbox, self.axes.bbox, magnification, --> 793 unsampled=unsampled) 794 795 def _check_unsampled_image(self, renderer):

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification, unsampled, round_to_pixel_border) 482 # (of int or float) 483 # or an RGBA array of re-sampled input --> 484 output = self.to_rgba(output, bytes=True, norm=False) 485 # output is now a correctly sized RGBA array of uint8 486

~/anaconda3/lib/python3.6/site-packages/matplotlib/cm.py in to_rgba(self, x, alpha, bytes, norm) 255 if xx.dtype.kind == 'f': 256 if norm and xx.max() > 1 or xx.min() < 0: --> 257 raise ValueError("Floating point image RGB values " 258 "must be in the 0..1 range.") 259 if bytes:

ValueError: Floating point image RGB values must be in the 0..1 range.

edbennett commented 5 years ago

Weird, in my version I get a warning about values being clipped because they're outside the range, but it still renders. I've only tried with a JPEG though; I wonder if the alpha channel of the PNG makes a difference? I'll test one on my installation and see what happens

edbennett commented 5 years ago

This doesn't reproduce for me with an alpha image; are you using IPython? I'm testing using Jupyter and am planning to deliver the session using that, so I might leave this open if so. I will specifically say to use a JPEG though, because the alpha channel needs special treatment to get the desired effect in the image.

edbennett commented 5 years ago

fixed in https://github.com/edbennett/performant-numpy/commit/d2489fdd72b21f137260593aaddd788611b98fba