grst / nbimporter

Import ipython notebooks as modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Errors thrown from imported notebook are unduly hard to interpret

githubpsyche opened this issue · comments

Hi, as a devoted jupyter user, I quite like nbimporter. Thanks for producing this tool.

In general, when an error gets thrown from a function defined in an imported notebook, it's harder than usual to tell what's going on - in general it seems that the traceback points at the wrong line numbers in the original notebook or perhaps at the wrong parts of the lines?

Here's an error message when I raised an error in an imported notebook:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-40123f3eaf8a> in <module>()
     29             for i in range(round(fractionthreshold*len(potentialrecordings))):
     30                 if not stored(potentialrecordings[i], cliptype['path']):
---> 31                     recordingpath, stats = recordPlay(potentialrecordings[i])
     32                     time.sleep(3)
     33 

C:\Users\me\Dropbox\automatedcurator\replaycontrols.ipynb in recordPlay(specs)
     20    "cell_type": "markdown",
     21    "metadata": {},
---> 22    "source": [
     23     "### Dependencies"
     24    ]

C:\Users\me\Dropbox\automatedcurator\replaycontrols.ipynb in setTime(target)
    169     "# build list of currently stored champ icons\n",
    170     "iconpath = 'images/champions/'\n",
--> 171     "storedicons = {icon[:-4]: Image.open(os.path.join(iconpath, icon))\n",
    172     "               for icon in os.listdir(iconpath) if '.png' in icon}\n",
    173     "\n",

C:\Users\me\Dropbox\automatedcurator\replaycontrols.ipynb in confirmTime()
    158     "\n",
    159     "# images of pause button\n",
--> 160     "pauseimage = Image.open('images/pause.png')\n",
    161     "activepauseimage = Image.open('images/active_pause.png')\n",
    162     "pressedpauseimage = Image.open('images/pressed_pause.png')\n",

C:\Users\me\Dropbox\automatedcurator\replaycontrols.ipynb in imtime(im)
     47     "# for basic mathematical computations\n",
     48     "import math\n",
---> 49     "\n",
     50     "# for reading and controlling operating system\n",
     51     "import os\n",

C:\Users\me\Dropbox\automatedcurator\replaycontrols.ipynb in <lambda>(storedtime)
     47     "# for basic mathematical computations\n",
     48     "import math\n",
---> 49     "\n",
     50     "# for reading and controlling operating system\n",
     51     "import os\n",

C:\Users\me\Dropbox\automatedcurator\replaycontrols.ipynb in imsimilarity(im1, im2)
     13    "metadata": {},
     14    "source": [
---> 15     "## Initial Parameters and Dependencies\n",
     16     "Don't forget to set HUD toggle to middle button! And resolution to 1920 x 1080!"
     17    ]

c:\programdata\anaconda3\lib\site-packages\PIL\ImageOps.py in invert(image)
    387     for i in range(256):
    388         lut.append(255-i)
--> 389     return _lut(image, lut)
    390 
    391 

c:\programdata\anaconda3\lib\site-packages\PIL\ImageOps.py in _lut(image, lut)
     46 
     47 def _lut(image, lut):
---> 48     if image.mode == "P":
     49         # FIXME: apply to lookup table, not image data
     50         raise NotImplementedError("mode P support coming soon")

AttributeError: 'tuple' object has no attribute 'mode'

And now here's the same one when the relevant functions are all in the same notebook instead of dispersed between two and united using nbimporter:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-40123f3eaf8a> in <module>()
     29             for i in range(round(fractionthreshold*len(potentialrecordings))):
     30                 if not stored(potentialrecordings[i], cliptype['path']):
---> 31                     recordingpath, stats = recordPlay(potentialrecordings[i])
     32                     time.sleep(3)
     33 

<ipython-input-6-402a0f88ebc4> in recordPlay(specs)
     20 
     21     # set the time to target time
---> 22     settime = setTime(targettime)
     23     for i in range(7):
     24         if settime:

<ipython-input-5-8b87335c4179> in setTime(target)
    169         click(timelinestart + targetposition, timeliney)
    170         waituntil(delayAfterClick, isPlaying)
--> 171         currenttime = confirmTime()
    172 
    173     # next, wait for and select estimated location of target timepoint

<ipython-input-5-8b87335c4179> in confirmTime()
    158 
    159     # return best guess of current time
--> 160     return imtime(currentScreen().crop(timebox))
    161 
    162 def setTime(target):

<ipython-input-4-b758543f265b> in imtime(im)
     47 def imtime(im):
     48     "match current timestamp with stored images to guess current time"
---> 49     match = max(storedtimes, key=lambda storedtime: imsimilarity(im, storedtime))[1]
     50     return tuple(match.split('-'))
     51 

<ipython-input-4-b758543f265b> in <lambda>(storedtime)
     47 def imtime(im):
     48     "match current timestamp with stored images to guess current time"
---> 49     match = max(storedtimes, key=lambda storedtime: imsimilarity(im, storedtime))[1]
     50     return tuple(match.split('-'))
     51 

<ipython-input-4-b758543f265b> in imsimilarity(im1, im2)
     13 def imsimilarity(im1, im2):
     14     "measure similarity of two Image objects"
---> 15     return compare_ssim(np.array(ImageOps.invert(im1).convert('LA')), np.array(ImageOps.invert(im2).convert('LA')), multichannel=True)
     16 
     17 def isLoading(im):

c:\programdata\anaconda3\lib\site-packages\PIL\ImageOps.py in invert(image)
    387     for i in range(256):
    388         lut.append(255-i)
--> 389     return _lut(image, lut)
    390 
    391 

c:\programdata\anaconda3\lib\site-packages\PIL\ImageOps.py in _lut(image, lut)
     46 
     47 def _lut(image, lut):
---> 48     if image.mode == "P":
     49         # FIXME: apply to lookup table, not image data
     50         raise NotImplementedError("mode P support coming soon")

AttributeError: 'tuple' object has no attribute 'mode'

I'm not a confident github user so I don't know if this is the right way to report the issue, but it seemed worth at least bringing up. I could probably write up a simpler example of what I'm complaining about if need be. In general, though, in the latter case where I'm not relying on nbimporter, I'm getting very exact and clear feedback on where in my code the error is happening; a problem with nbimporter keeps this from happening successfully when I rely on it. This limits nbimporter as an development/prototyping aid.

Hi,

thanks for reporting the issue.
I don't support this project anymore, as I now believe that importing from notebooks is a bad idea.
See the README for more details.

However, if you/someone creates a pull-request that addresses the issue, I am happy to merge it.

Cheers,
Gregor

Cool, glad to know you'll merge it if I add a fix.