#!/usr/bin/env python # coding: utf-8 # ## A series of tests of the SLIP package. # In[1]: get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') import os import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt fopts = {'fontsize':18} # In[2]: from SLIP import Image, imread # In[3]: help(Image) # ## initializing the SLIP object # # The SLIP object may be initialized by giving some parameters. These could be given using # - nothing # In[4]: im = Image() # default is 'https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py' print(im.pe) # - a dictionary (dimensions ``N_X`` and ``N_Y`` and ``mask_exponent`` are mandatory) # In[5]: im = Image({'N_X':32, 'N_Y':64}) print(im.pe) # - a ``ndarray`` (dimensions ``N_X`` and ``N_Y`` are guessed from this array) # In[6]: im = Image(np.empty((32, 64))) print(im.pe) # - a string indicating the path of a image file # In[7]: im = Image('database/lena64.png') print(im.pe) # - a string indicating the URL to a image file # In[8]: im = Image('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png') print(im.pe) # - a string indicating the path of a file containing a dictionary # In[9]: im = Image('file://default_param.py') im = Image('default_param.py') print(im.pe.keys()) # - a string indicating the URL of a file containing a dictionary # In[10]: im = Image('https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py') print(im.pe.keys()) # - a NeuroTools object # In[11]: from NeuroTools.parameters import ParameterSet from SLIP import Image im = Image(ParameterSet({'N_X':128, 'N_Y':256})) # The parameters may be handled using the properties of the ``ParameterSet`` object. # ## resizing the SLIP object # # The SLIP object may be adapted to a new size # # - nothing # In[12]: help(im.set_size) # In[13]: im = Image() # default is {'N_X':128, 'N_Y':128} print(im.pe) # In[14]: im.set_size((512, 234)) print(im.pe) # In[15]: im.set_size(np.ones((512, 234))) print(im.pe) # In[16]: im.set_size('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png') print(im.pe) # ## testing logging # # we use https://docs.python.org/3.4/library/logging.html to handle logging of events # In[17]: im = Image() print('Verbosity level=', im.pe.verbose) # In[18]: im.log.debug(' > this should not appear') im.log.info(' > this should not appear') im.log.error(' > this *should* appear') Logging Levels -------------- The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost. +--------------+---------------+ | Level | Numeric value | +==============+===============+ | ``CRITICAL`` | 50 | +--------------+---------------+ | ``ERROR`` | 40 | +--------------+---------------+ | ``WARNING`` | 30 | +--------------+---------------+ | ``INFO`` | 20 | +--------------+---------------+ | ``DEBUG`` | 10 | +--------------+---------------+ | ``NOTSET`` | 0 | +--------------+---------------+ # In[19]: im.pe.verbose = 15 im.init_logging() im.log.debug(' > this should not appear') im.log.info(' > this *should* appear') im.log.error(' > this *should* appear') # ## basic parameter check # # TODO # In[20]: try: Image({'N_X':128, 'N_Y':-12, 'mask_exponent':3}) except Exception as e: print(e) # ## testing reading an image file # # Note that if the image is of a different size, the size of the ``Image`` object is adapted: # In[21]: im = Image() print(im.pe) image = im.imread('database/lena64.png') print(im.pe) # In[22]: image = im.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png') print(im.pe) # ## testing showing an image file # # # In[23]: 'N_X' in im.pe.keys() # In[24]: im = Image() print(im.pe) image = im.imread('database/lena64.png') _ = im.imshow(image) # In[25]: image = im.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png') _ = im.imshow(image) # In[26]: _ = im.show_spectrum(image) # ## testing import and cropping # In[27]: axs = [] im = Image({'N_X':32, 'N_Y':128, 'seed':None, 'do_mask':False}) im.pe.datapath = 'database/' for name_database in ['serre07_targets', 'serre07_distractors']: fig = plt.figure(figsize=(14, 3.5)) for _ in range(4): image, filename, croparea = im.patch(name_database, ) ax = fig.add_subplot(1, 4, _+1) im.imshow(image, fig=fig, ax=ax) fig.show() # ## Handling a database # In[28]: im = Image({'N_X':64, 'N_Y':128, 'seed':None, 'do_mask':False, 'N_image':10}) im.pe.datapath = 'database/' help(im.make_imagelist) # extract one image list from a database imagelist = im.make_imagelist('serre07_targets') print('Number of images:', len(imagelist)) print('First image:', imagelist[0]) # In[29]: help(im.get_imagelist) im.pe.figpath, im.pe.matpath = '/tmp/fig', '/tmp/mat' # store one image list from a database imagelist = im.get_imagelist('classifier', 'serre07_targets') get_ipython().system('ls -l /tmp/mat') print('Number of images:', len(imagelist)) print('First image:', imagelist[0]) # In[30]: im = Image({'N_X':128, 'N_Y':128, 'seed':None, 'do_mask':False, 'N_image':10}) im.pe.figpath, im.pe.matpath = '/tmp/fig', '/tmp/mat' # now we can access again this stored list get_ipython().system('ls -l /tmp/mat') imagelist = im.get_imagelist('classifier', 'serre07_targets') print('Number of images:', len(imagelist)) print('First image:', imagelist[0]) # ## checking basic translation mechanism # defining a reference test image; check the axis labels for a (x,y) translation # In[31]: im = Image({'N_X':128, 'N_Y':256}) image = np.zeros((im.pe.N_X, im.pe.N_Y)) image[im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4, im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4] = 1 # white square on the right image[im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4, im.pe.N_X//4:im.pe.N_X//2] = -1 # black square on its left _ = im.imshow(image, axis=True) # translating the image by an integer by rolling indices # # In[32]: print(im.pe.N_X//8., im.pe.N_Y//4.) _ = im.imshow(np.roll(np.roll(image, np.int(im.pe.N_X//8.), axis=0), np.int(im.pe.N_X//4.), axis=1)) # remember **axis conventions** are: x going down, y going right with the origin on top # translating the image down # In[33]: _ = im.imshow(im.translate(image, [im.pe.N_X//8., 0.])) # translating the image right # In[34]: _ = im.imshow(im.translate(image, [0., im.pe.N_Y//4.])) # translating the image on both axis # In[35]: _ = im.imshow(im.translate(image, [im.pe.N_X//8., im.pe.N_Y//4.])) # translating the image over the torus # In[36]: _ = im.imshow(im.translate(image, [im.pe.N_X//2., im.pe.N_Y//4.])) # [Back to top](#SLIP) # ## some checks that should produce gray images (= null error) # null translation gives the same image # In[37]: _ = im.imshow(image - im.translate(image, [0., 0.]), norm=False) # the lg.translate function is invertible # In[38]: _ = im.imshow(image - im.translate(im.translate(image, [1.64, -2.233]), [-1.64, 2.233]), norm=False) # also true for bigger translations (we separate integer part from fractional part in the translation) # In[39]: _ = im.imshow(image - im.translate(im.translate(image, [182.64, -286.233]), [-182.64, 286.233], preshift=True), norm=False) # but not always true when we don't separate # In[40]: _ = im.imshow(image - im.translate(im.translate(image, [182.64, -286.233]), [-182.64, 286.233], preshift=False), norm=False) # the lg.translate function is periodic # In[41]: _ = im.imshow(image - im.translate(image, [-45*im.pe.N_X, 5*im.pe.N_Y]), norm=False) # lg.translate function is associative # In[42]: _ = im.imshow(im.translate(image, [1., .0]) - im.translate(im.translate(image, [.5, .0]), [.5, .0]), norm=False) # In[43]: _ = im.imshow(im.translate(image, [2., .0]) - im.translate(im.translate(image, [1.5, -1.0]), [.5, 1.0]), norm=False) # ## filtering # # Check out the discussion @ https://laurentperrinet.github.io/sciblog/posts/2017-09-20-the-fastest-2d-convolution-in-the-world.html # ## whitening # # Here, we test the whitening on different images. # In[44]: axs = [] im = Image('default_param.py') im.pe.datapath = 'database/' for name_database in ['serre07_targets', 'serre07_distractors']: for _ in range(4): fig = plt.figure(figsize=(14, 7)) image, filename, croparea = im.patch(name_database, do_whitening=False) ax = fig.add_subplot(1, 2, 1) fig , ax = im.imshow(image, fig=fig, ax=ax) ax = fig.add_subplot(1, 2, 2) image, filename, croparea = im.patch(name_database, filename=filename, croparea=croparea, do_whitening=True) fig , ax = im.imshow(image, fig=fig, ax=ax) plt.show() # [Back to top](#SLIP) # ## more book keeping # In[48]: get_ipython().run_line_magic('load_ext', 'watermark') get_ipython().run_line_magic('watermark', '-i -h -m -v -p numpy,matplotlib,scipy,imageio,SLIP -r -g -b') # [Back to top](#SLIP)