Unicorn HAT HD - stars.py not working

When trying to run the example script stars.py, instead of any actual display, I’m given:

Unicorn HAT HD: Stars

This example simulates a wooshing star field.

Press Ctrl+C to exit!


Traceback (most recent call last):
  File "stars.py", line 41, in <module>
    unicornhathd.set_pixel(stars[i][0], stars[i][1], v, v, v)
  File "/usr/lib/python2.7/dist-packages/unicornhathd/__init__.py", line 79, in set_pixel
    _buf[x][y] = r, g, b
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Any thoughts as to how I can resolve this?

Are you running it in Python 2 or 3? Some files written for Python 2 will error when run in Python 3. It’s caught me out a few times.

Either one. Neither works. The above was pasted from trying Python 2. Here’s the Python 3 output:

Unicorn HAT HD: Stars

This example simulates a wooshing star field.

Press Ctrl+C to exit!


Traceback (most recent call last):
  File "stars.py", line 41, in <module>
    unicornhathd.set_pixel(stars[i][0], stars[i][1], v, v, v)
  File "/usr/lib/python3/dist-packages/unicornhathd/__init__.py", line 79, in set_pixel
    _buf[x][y] = r, g, b
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Looks like random.uniform returns a float, but set_pixel expects an integer.

Try changing this line:

unicornhathd.set_pixel(stars[i][0], stars[i][1], v, v, v)

to:

unicornhathd.set_pixel(int(stars[i][0]), int(stars[i][1]), v, v, v)

Thank you very much! That fixed it. Even though the answer was right there in the error, as was the mention of the line to fix, I don’t remember enough about Python for making these changes, so thanks for taking the time. Time to sign up for a Python course, I suppose…

Do you know if these fixes eventually get implemented into GitHub (or wherever) so that everyone who installs it gets a working copy?

Looking into them now- bizarrely neither Python 2 nor Python 3 fail in the same way for me locally.

What do you see if you run:

import numpy
print(numpy.__version__)

I’ll probably update the library to coerce any input values to int().

I’m betting we’re running into: https://github.com/mapbox/rasterio/issues/1002 since my local numpy version is only 1.8.2

You’re right! Mine is version 1.12.1.

I’ve pushed up a fix to the library that should resolve it without having to worry about casting to int within the examples.