Ejemplo n.º 1
0
def extalg_plot(gi, imgkey=None):

    gi.set_plotting_params(gi)

    if imgkey and imgkey == 'rainrate':

        colormapper = cm.ScalarMappable(norm=colors.NoNorm(), cmap=get_cmap('frozen_cmap'))
        currimg = colormapper.to_rgba(normalize(gi.image['rainrate']['frozen']))
        # imshow expects upside down arrays, but we should not store them upside down
        # so flipud here
        gi.basemap.imshow(np.flipud(gi.image['rainrate']['frozen']))

        colormapper = cm.ScalarMappable(norm=colors.NoNorm(), cmap=get_cmap('liquid_cmap'))
        currimg = colormapper.to_rgba(normalize(gi.image['rainrate']['liquid']))
        # imshow expects upside down arrays, but we should not store them upside down
        # so flipud here
        gi.basemap.imshow(np.flipud(gi.image['rainrate']['liquid']))

        ##if 'float' in str(gi.image[imgkey].dtype):
        ##    pass
        ##else:
        ##    gi.image[imgkey].dtype = 'float64'
        ##gi.basemap.imshow(np.flipud(currimg), ax=gi.axes, interpolation='nearest')


    if gi.is_final:
        gi.finalize()
Ejemplo n.º 2
0
def extalg_plot(gi, imgkey=None):

    gi.set_plotting_params(gi)

    if imgkey and imgkey == 'rainrate':

        colormapper = cm.ScalarMappable(norm=colors.NoNorm(),
                                        cmap=get_cmap('frozen_cmap'))
        currimg = colormapper.to_rgba(normalize(
            gi.image['rainrate']['frozen']))
        gi.basemap.imshow(gi.image['rainrate']['frozen'])

        colormapper = cm.ScalarMappable(norm=colors.NoNorm(),
                                        cmap=get_cmap('liquid_cmap'))
        currimg = colormapper.to_rgba(normalize(
            gi.image['rainrate']['liquid']))
        gi.basemap.imshow(gi.image['rainrate']['liquid'])

        ##if 'float' in str(gi.image[imgkey].dtype):
        ##    pass
        ##else:
        ##    gi.image[imgkey].dtype = 'float64'
        ##gi.basemap.imshow(currimg, ax=gi.axes, interpolation='nearest')

    if gi.is_final:
        gi.finalize()
Ejemplo n.º 3
0
Archivo: p1.py Proyecto: iagopicos/VA
def showImage(inputImage, outputImage):
    pyplot.subplot(1, 2, 1)
    pyplot.imshow(inputImage, cmap='gray', norm=colors.NoNorm())
    pyplot.title('Original')
    pyplot.subplot(1, 2, 2)
    pyplot.imshow(outputImage, cmap='gray', norm=colors.NoNorm())
    pyplot.title('Modified')
    pyplot.show()
Ejemplo n.º 4
0
    def _process_colors(self):
        """
        Color argument processing for contouring.

        Note that we base the color mapping on the contour levels,
        not on the actual range of the Z values.  This means we
        don't have to worry about bad values in Z, and we always have
        the full dynamic range available for the selected levels.

        The color is based on the midpoint of the layer, except for
        an extended end layers.
        """
        self.monochrome = self.cmap.monochrome
        if self.colors is not None:
            i0, i1 = 0, len(self.layers)
            if self.extend in ('both', 'min'):
                i0 = -1
            if self.extend in ('both', 'max'):
                i1 = i1 + 1
            self.cvalues = range(i0, i1)
            self.set_norm(colors.NoNorm())
        else:
            self.cvalues = self.layers
        if not self.norm.scaled():
            self.set_clim(self.vmin, self.vmax)
        if self.extend in ('both', 'max', 'min'):
            self.norm.clip = False
        self.set_array(self.layers)
Ejemplo n.º 5
0
 def confirm_slot(self):
     cmap = plt.get_cmap(self.colormaps_combox.currentText())
     vmin = float(self.min_lineedit.text()
                  ) if self.min_lineedit.text() != '' else None
     vmax = float(self.max_lineedit.text()
                  ) if self.max_lineedit.text() != '' else None
     clip = self.clip_checkbox.isChecked()
     if self.collection:
         vmax = self.collection.norm.vmax if vmax is None else vmax
         vmin = self.collection.norm.vmin if vmin is None else vmin
     if clip:
         vmin, vmax = None, None
     if self.norms_combox.currentText() == 'Normalize':
         norm = mcolors.Normalize(vmin, vmax, clip)
     if self.norms_combox.currentText() == 'LogNorm':
         norm = mcolors.LogNorm(vmin, vmax, clip)
     if self.norms_combox.currentText() == 'NoNorm':
         norm = mcolors.NoNorm(vmin, vmax, clip)
     # self.ax.set_autoscale_on(True)
     if not self.colorbar:
         self.collection = self.ax.collections[
             self.collections_combox.currentIndex()]
         self.collection.cmap = cmap
         self.collection.norm = norm
         self.colorbar = self.canvas.figure.colorbar(
             self.ax.collections[self.collections_combox.currentIndex()],
             ax=self.ax)
     else:
         self.collection.autoscale()
         self.collection.cmap = cmap
         self.collection.norm = norm
         self.colorbar.update_normal(self.collection)
         # self.colorbar.norm=norm
     self.canvas.draw_idle()
Ejemplo n.º 6
0
def save_to_file(pred_mask, display_image, gt_mask, i, save_dir):
    plt.switch_backend('agg')
    plt.figure()
    plt.subplot(131)
    plt.imshow(display_image)
    plt.axis('off')
    plt.title('original image')

    NUM_CLASSES = 11
    cmap = discrete_cmap(NUM_CLASSES, 'Paired')
    norm = colors.NoNorm(vmin=0, vmax=NUM_CLASSES)

    plt.subplot(132)
    plt.imshow(display_image)
    plt.imshow(gt_mask, alpha=0.8, cmap=cmap, norm=norm)
    plt.axis('off')
    plt.title('real mask')

    plt.subplot(133)
    plt.imshow(display_image)
    plt.imshow(pred_mask, alpha=0.8, cmap=cmap, norm=norm)
    plt.axis('off')
    plt.title('predicted mask')
    plt.savefig(os.path.join(save_dir, str(i) + '.png'))
    plt.clf()
Ejemplo n.º 7
0
Archivo: p1.py Proyecto: iagopicos/VA
def showHistogram(inputImage, outputImage, bins=[256, 256]):
    pyplot.subplot(2, 2, 1)
    pyplot.imshow(inputImage, cmap='gray', norm=colors.NoNorm())
    pyplot.title('Original image')

    pyplot.subplot(2, 2, 2)
    pyplot.hist(inputImage.ravel(), bins[0], [0, bins[0]])
    pyplot.title('Original hist')

    pyplot.subplot(2, 2, 3)
    pyplot.imshow(outputImage, cmap='gray', norm=colors.NoNorm())
    pyplot.title('Modified')

    pyplot.subplot(2, 2, 4)
    pyplot.hist(outputImage.ravel(), 255, [0, bins[1]])
    pyplot.title('Modified')
    pyplot.show()
Ejemplo n.º 8
0
def plt_images(images, rows, cols):
    fig = plt.figure(figsize=(cols,rows))
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.05, hspace=0.05)
    for i, x in enumerate(images[:cols * rows]):
        plt.subplot(rows, cols, i + 1)
        plt.axis('off')
        plt.imshow(x, cmap='gray', norm=colors.NoNorm())
    return fig
Ejemplo n.º 9
0
 def overlay(self, image):
     self._overlay = image
     if image is None:
         self.ax.images.remove(self._overlay_plot)
         self._overlay_plot = None
     elif self._overlay_plot is None:
         props = dict(cmap=self.cmap, alpha=self.alpha,
                      norm=mcolors.NoNorm(), animated=True)
         self._overlay_plot = self.ax.imshow(image, **props)
     else:
         self._overlay_plot.set_data(image)
     self.redraw()
Ejemplo n.º 10
0
def full_multi_viewer(img, seg_mask, prediction):
    remove_keymap_conflicts({'j', 'k'})
    fig, (ax_img, ax_seg, ax_pre) = plt.subplots(1, 3)
    ax_img.volume = img
    ax_seg.volume = seg_mask
    ax_pre.volume = prediction
    ax_img.index = ax_img.volume.shape[1] // 2
    ax_seg.index = ax_seg.volume.shape[1] // 2
    ax_pre.index = ax_pre.volume.shape[1] // 2
    ax_img.imshow(ax_img.volume[:, ax_img.index, :], cmap='gray')
    ax_img.set_title('MRI Scan')
    ax_seg.imshow(ax_seg.volume[:, ax_seg.index, :],
                  cmap='gray',
                  norm=clr.NoNorm())
    ax_seg.set_title('Ground Truth')
    ax_pre.imshow(ax_pre.volume[:, ax_pre.index, :],
                  cmap='gray',
                  norm=clr.NoNorm())
    ax_pre.set_title('Prediction')
    fig.canvas.mpl_connect('key_press_event', process_key)
    plt.show()
Ejemplo n.º 11
0
def set_colormap(color=None,
                 boundaries=[],
                 normalization='linear',
                 color_bad='w',
                 **kwargs):

    if color is None or boundaries == []:
        return {}

    if isinstance(boundaries, np.ndarray):
        boundaries = np.atleast_2d(boundaries)
        ncolors = len(np.unique(boundaries, axis=0))
        boundaries = np.linalg.norm(boundaries, axis=1)
    else:
        try:
            ncolors = len(set(boundaries))
        except TypeError:
            ncolors = 1

    if ncolors == 1 or normalization == 'none':
        cmap = matplotlib.cm.get_cmap(color)
        norm = colors.NoNorm(None, None)
    elif normalization == 'linear':
        cmap = matplotlib.cm.get_cmap(color)
        norm = colors.Normalize(min(boundaries), max(boundaries))
    elif normalization == 'discrete':
        cmap = matplotlib.cm.get_cmap(color, ncolors)
        norm = colors.BoundaryNorm(boundaries, ncolors)
    elif normalization == "log":
        cmap = matplotlib.cm.get_cmap(color)
        norm = colors.LogNorm(max(min(boundaries), 1e-14), max(boundaries))
    else:
        cmap = matplotlib.cm.get_cmap(color)
        norm = colors.NoNorm(None, None)

    cmap.set_bad(color=color_bad)

    return {'cmap': cmap, 'norm': norm}
Ejemplo n.º 12
0
    def image(self):
        if not hasattr(self, '_image'):
            img_dts = {}
            pname = self.product.name
            sname = self.sector.name
            img_dts['start_fullsingleimg' + sname + pname] = datetime.utcnow()
            log.info('Creating basic image.')
            print_mem_usage(self.logtag + 'gimgbeforeregister', True)

            #Register data to area definition
            #Performing prior to color gun generation in order to allow
            #   for multiple data resolutions

            gun = self.product.images['img']
            img_dts['start_clrgunsingle' + sname + pname] = datetime.utcnow()
            self._image = np.flipud(create_color_gun(self.registered_data,
                                                     gun))
            img_dts['end_clrgunsingle' + sname + pname] = datetime.utcnow()

            # add flexibility:  if Boundary is added in the productfile.mxl
            #     the discrete colorbar is selected.
            if len(self.product.colorbars
                   ) > 0 and self.product.colorbars[0].norm == 'Boundary':
                #ticks = [float(i) for i in self.product.colorbars[0].bounds.split(' ')]
                #bounds = [ticks[0]-1] + ticks + [ticks[-1]+1]
                colormapper = cm.ScalarMappable(norm=None,
                                                cmap=get_cmap(
                                                    self.product.cmap))
            else:
                colormapper = cm.ScalarMappable(norm=colors.NoNorm(),
                                                cmap=get_cmap(
                                                    self.product.cmap))

            img_dts['start_torgbasingle' + sname + pname] = datetime.utcnow()
            self._image = colormapper.to_rgba(self.image)
            img_dts['end_torgbasingle' + sname + pname] = datetime.utcnow()
            log.info(sname + pname + ' Done Creating single channel image.')
            img_dts['end_fullsingleimg' + sname + pname] = datetime.utcnow()

            for sttag in sorted(img_dts.keys()):
                if 'start_' in sttag:
                    tag = sttag.replace('start_', '')
                    try:
                        log.info('process image time %-40s: ' % tag +
                                 str(img_dts['end_' + tag] -
                                     img_dts['start_' + tag]))
                    except:
                        log.info('WARNING! No end time for ' + sttag)
        return self._image
Ejemplo n.º 13
0
def patch_viewer(patch):
    remove_keymap_conflicts({'j', 'k'})
    fig, (ax_img, ax_seg) = plt.subplots(1, 2)
    ax_img.volume = patch[..., 0]
    ax_seg.volume = patch[..., 1]
    ax_img.index = ax_img.volume.shape[1] // 2
    ax_seg.index = ax_seg.volume.shape[1] // 2
    ax_img.imshow(ax_img.volume[:, ax_img.index, :], cmap='gray')
    ax_img.set_title('MRI Scan')
    ax_seg.imshow(ax_seg.volume[:, ax_seg.index, :],
                  cmap='gray',
                  norm=clr.NoNorm())
    ax_seg.set_title('Ground Truth')
    fig.canvas.mpl_connect('key_press_event', process_key)
    plt.show()
Ejemplo n.º 14
0
def X_singlePlot(imIn, ax, label):
    if (imIn.getTypeAsString() == "RGB"):
        im3ch = sp.Image()
        sp.splitChannels(imIn, im3ch)
        imArr = im3ch.getNumArray()
        imArr = np.rot90(imArr, -1)
        imArr = np.fliplr(imArr)
        ax.imshow(imArr)
    else:
        imInArr = imIn.getNumArray()
        imInArr = np.rot90(imInArr, -1)
        imInArr = np.fliplr(imInArr)
        if not label:
            ax.imshow(imInArr, cmap=cm.Greys_r, norm=cl.NoNorm())
        else:
            ax.imshow(imInArr, cmap=randcmap)
Ejemplo n.º 15
0
def imshow(Is, titles, savePath=None):
    plt.figure(facecolor='w', figsize=(16, 16))
    n = len(Is)
    sq = int(np.ceil(n**0.5))
    r, c = int(np.ceil(n / sq)), sq
    for i in range(n):
        plt.subplot(n, c, i + 1)
        plt.imshow(Is[i], norm=colors.NoNorm(), cmap='gray')
        plt.axis('off')
        plt.title(titles[i])
        if savePath:
            cv2.imwrite(os.path.join(savePath, titles[i] + '.png'), Is[i])


#             plt.imsave(os.path.join(savePath,titles[i]),Is[i],cmap='gray')
    plt.tight_layout()
Ejemplo n.º 16
0
 def display(self, img_id, do_de_normalize=False):
     img, _, masks = self[img_id]
     if do_de_normalize:
         img = de_normalize(img)
     display_image = np.transpose(img.numpy(), (1, 2, 0))
     plt.figure()
     plt.subplot(121)
     plt.imshow(display_image)
     plt.axis('off')
     plt.title('original image')
     plt.subplot(122)
     plt.imshow(display_image)
     cmap = discrete_cmap(self.numClasses, 'Paired')
     norm = colors.NoNorm(vmin=0, vmax=self.numClasses)
     plt.imshow(masks, alpha=0.8, cmap=cmap, norm=norm)
     plt.axis('off')
     plt.title('annotated image')
     plt.show()
Ejemplo n.º 17
0
def visualize_mask(trainer, loader, number):
    total = 0
    to_return = []
    for data, mask_gt, gt_visual in loader:
        if total < number:
            data = data.cuda()
            batch_size = data.size()[0]
            total += batch_size
            mask_pred = convert_to_mask(trainer._gen(data))
            for i in range(len(data)):
                img = de_normalize(data[i].detach().cpu().numpy())
                gt_mask = gt_visual[i].detach().cpu().numpy()
                pred_mask = np.argmax(mask_pred[i].detach().cpu().numpy(),
                                      axis=0)
                to_return.append((img, gt_mask, pred_mask))
                display_image = np.transpose(img, (1, 2, 0))
                plt.figure()

                plt.subplot(131)
                plt.imshow(display_image)
                plt.axis('off')
                plt.title('original image')

                cmap = discrete_cmap(NUM_CLASSES, 'Paired')
                norm = colors.NoNorm(vmin=0, vmax=NUM_CLASSES)

                plt.subplot(132)
                plt.imshow(display_image)
                plt.imshow(gt_mask, alpha=0.8, cmap=cmap, norm=norm)
                plt.axis('off')
                plt.title('real mask')

                plt.subplot(133)
                plt.imshow(display_image)
                plt.imshow(pred_mask, alpha=0.8, cmap=cmap, norm=norm)
                plt.axis('off')
                plt.title('predicted mask')
                plt.show()
        else:
            break
    return to_return
Ejemplo n.º 18
0
def smilToNumpyPlotNoNorm(imIn, labelImage=False):
    if (imIn.getTypeAsString() == "RGB"):
        imIn3ch = sp.Image()
        sp.splitChannels(imIn, imIn3ch)
        imInArr = imIn3ch.getNumArray()
        imInArr = np.rot90(imInArr, -1)
        imInArr = np.fliplr(imInArr)
        plt.figure()
        plt.imshow(imInArr)
        plt.show()
    else:
        imInArr = imIn.getNumArray()
        imInArr = np.rot90(imInArr, -1)
        imInArr = np.fliplr(imInArr)
        if not labelImage:
            plt.figure()
            plt.imshow(imInArr, cmap="gray", norm=cl.NoNorm())
            plt.show()
        else:
            plt.figure()
            plt.imshow(imInArr, cmap=randcmap)
            plt.show()
Ejemplo n.º 19
0
    def image(self):
        if not hasattr(self, '_image'):
            img_dts = {}
            pname = self.product.name
            sname = self.sector.name
            img_dts['start_fullsingleimg' + sname + pname] = datetime.utcnow()
            log.info('Creating basic image.')
            print_mem_usage(self.logtag + 'gimgbeforeregister', True)

            #Register data to area definition
            #Performing prior to color gun generation in order to allow
            #   for multiple data resolutions

            gun = self.product.images['img']

            img_dts['start_clrgunsingle' + sname + pname] = datetime.utcnow()
            self._image = np.flipud(create_color_gun(self.registered_data,
                                                     gun))
            img_dts['end_clrgunsingle' + sname + pname] = datetime.utcnow()
            colormapper = cm.ScalarMappable(norm=colors.NoNorm(),
                                            cmap=get_cmap(self.product.cmap))
            img_dts['start_torgbasingle' + sname + pname] = datetime.utcnow()
            self._image = colormapper.to_rgba(self.image)
            img_dts['end_torgbasingle' + sname + pname] = datetime.utcnow()
            log.info(sname + pname + ' Done Creating single channel image.')
            img_dts['end_fullsingleimg' + sname + pname] = datetime.utcnow()

            for sttag in sorted(img_dts.keys()):
                if 'start_' in sttag:
                    tag = sttag.replace('start_', '')
                    try:
                        log.info('process image time %-40s: ' % tag +
                                 str(img_dts['end_' + tag] -
                                     img_dts['start_' + tag]) + ' ' +
                                 socket.gethostname())
                    except:
                        log.info('WARNING! No end time for ' + sttag)
        return self._image
Ejemplo n.º 20
0
cropax = axarr[1].imshow(x, cmap=plt.get_cmap('gray'))
axarr[1].set_title('Cropped Image')

dx = np.arange(-np.floor(dsize[1] / 2), np.floor(dsize[1] / 2) + 1, dtype=int)
dy = np.arange(-np.floor(dsize[0] / 2), np.floor(dsize[0] / 2) + 1, dtype=int)
[dpx, dpy] = np.meshgrid(dx, dy)
dpx = dpx.reshape(-1, 1)
dpy = dpy.reshape(-1, 1)
dp = np.hstack((dpx, dpy))
N = dpx.size

all_patches = np.ones((N * dsize[0], dsize[1]))
all_patchax = axarr[2].imshow(all_patches,
                              cmap=plt.get_cmap('gray'),
                              aspect='auto',
                              norm=colors.NoNorm())
axarr[2].set_title('Concatenation of Sub-Images (X)')

X = np.zeros((N, N))
Y = np.zeros((N, 1))

sigma = 5


def init():
    return [cropax, patch, all_patchax]


def animate(i):
    global X, Y, dp, gnd_p, sigma, all_patches, patch, cropax, all_patchax, N
Ejemplo n.º 21
0
def genplots(save=False, folder="images", **kwargs):
    if save:
        import os
        folder = os.path.join(os.getcwd(), folder)
        if not os.path.exists(folder):
            os.mkdir(folder)

    for key in ('price', 'size_class', 'ratings_avg', 'brand', 'refresh',
                'is_3d', 'tv_type', 'resolution'):
        data[key] = []
        for a in attrs:
            try:
                if key in ('price', 'size_class', 'ratings_avg', 'refresh'):
                    if a[key]:
                        precision = 0
                        accuracy = 1
                        if key in ['price', 'refresh']:
                            precision = -1
                        elif key in ['size_class']:
                            accuracy = 2
                        elif key in ['ratings_avg']:
                            precision = 0
                        data[key].append(
                            round(float(a[key]) / accuracy, precision) *
                            accuracy)
                    else:
                        data[key].append(NaN)
                else:
                    if hasattr(a[key], 'strip'):
                        data[key].append(a[key].strip(u'\x99'))
                    else:
                        data[key].append(a[key])
            except KeyError:
                print(
                    u"Couldn't find key: %s for product with attributes: %s" %
                    (key, a))
        unique = sorted(list(set(data[key])))
        data[key + "map"] = array([unique.index(item) for item in data[key]])
        data[key] = array(data[key])

        largelims = None
        zoomlims = None

        if 'price' in data and 'size_class' in data and not largelims and not zoomlims:
            xbuf = 5
            ybuf = 100
            largelims = {
                'xmax': max(data['size_class']) + xbuf,
                'xmin': min(data['size_class']) - xbuf,
                'ymax': max(data['price']) + ybuf,
                'ymin': min(data['price']) - ybuf
            }

            xmed = median(data['size_class'])
            xsd = std(data['size_class'][:, ~isnan(data['size_class'])])
            ymed = median(data['price'])
            ysd = std(data['price'][:, ~isnan(data['price'])])
            zoomlims = {
                'xmax': xmed + xsd / 2,
                'xmin': xmed - xsd / 2,
                'ymax': ymed + ysd / 2,
                'ymin': ymed - ysd / 2
            }

        if key not in ('price', 'size_class') and largelims and zoomlims:
            largename = key + "large"
            pyplot.figure(largename, figsize=(20, 15))
            pyplot.xlim(xmax=largelims['xmax'], xmin=largelims['xmin'])
            pyplot.ylim(ymax=largelims['ymax'], ymin=largelims['ymin'])
            pyplot.title(largename)
            zoomname = key + "zoom"
            pyplot.figure(zoomname, figsize=(20, 15))
            pyplot.xlim(xmax=zoomlims['xmax'], xmin=zoomlims['xmin'])
            pyplot.ylim(ymax=zoomlims['ymax'], ymin=zoomlims['ymin'])
            pyplot.title(zoomname)

            #TODO: better nan handling
            if key in ['price', 'size_class', 'ratings_avg', 'refresh']:
                values = data[key][:, ~isnan(data[key])]
                prices = data['price'][:, ~isnan(data[key])]
                sizes = data['size_class'][:, ~isnan(data[key])]
            else:
                values = data[key][:, ~equal(data[key], None)]
                prices = data['price'][:, ~equal(data[key], None)]
                sizes = data['size_class'][:, ~equal(data[key], None)]
            for value in set(values):
                if len(values[:, equal(values, value)]) > 2:
                    print "graphing with key: %s and value %s" % (
                        key, unicode(value))
                    pyplot.figure(largename)
                    color = (float(unique.index(value))) / (float(len(unique)))
                    edgecolor = color  #max(0,color-.2)
                    if kwargs['cmap']:
                        edgecolor = kwargs['cmap'](int(
                            round(float(kwargs['cmap'].N) * float(edgecolor))))
                        color = kwargs['cmap'](int(
                            round(float(kwargs['cmap'].N) * color)))
                    else:
                        edgecolor = str(edgecolor)
                        color = str(color)

                    cursizes = sizes[:, values == value]
                    curprices = prices[:, values == value]
                    rectlarge = pyplot.Rectangle(
                        (min(cursizes) - 1, min(curprices) - 10),
                        max(cursizes) - min(cursizes) + 2,
                        max(curprices) - min(curprices) + 20,
                        edgecolor=edgecolor,
                        linewidth=10,
                        facecolor=color,
                        alpha=.1)
                    pyplot.scatter(cursizes,
                                   curprices,
                                   c=color,
                                   label=unicode(value),
                                   norm=colors.NoNorm(),
                                   **kwargs)  #c=data[key+'map'],,cmap=cm.gray)
                    pyplot.gca().add_patch(rectlarge)
                    pyplot.gca().text(
                        max(largelims['xmin'] + 1,
                            min(cursizes) + 2),
                        min(largelims['ymax'] - 100,
                            max(curprices) - 300), unicode(value))
                    pyplot.figure(zoomname)
                    rectzoom = pyplot.Rectangle(
                        (min(cursizes) - 1, min(curprices) - 10),
                        max(cursizes) - min(cursizes) + 2,
                        max(curprices) - min(curprices) + 20,
                        edgecolor=edgecolor,
                        linewidth=10,
                        facecolor=color,
                        alpha=.1)
                    pyplot.scatter(cursizes,
                                   curprices,
                                   c=color,
                                   label=unicode(value),
                                   **kwargs)
                    pyplot.gca().add_patch(rectzoom)
                    pyplot.gca().text(
                        min(cursizes) + 1,
                        min(zoomlims['ymax'] - 100, max(curprices)),
                        unicode(value))
                else:
                    print "rejecting key: %s and value %s" % (key,
                                                              unicode(value))

            pyplot.figure(largename)
            pyplot.legend()
            if save:
                pyplot.savefig("%s/%s.png" % (folder, largename))

            pyplot.figure(zoomname)
            pyplot.legend()

            if save:
                pyplot.savefig("%s/%s.png" % (folder, zoomname))
Ejemplo n.º 22
0
    def clabel(self, *args, **kwargs):
        """
        call signature::

          clabel(cs, **kwargs)

        adds labels to line contours in *cs*, where *cs* is a
        :class:`~matplotlib.contour.ContourSet` object returned by
        contour.

        ::

          clabel(cs, v, **kwargs)

        only labels contours listed in *v*.

        Optional keyword arguments:

          *fontsize*:
            See http://matplotlib.sf.net/fonts.html

          *colors*:
            - if *None*, the color of each label matches the color of
              the corresponding contour

            - if one string color, e.g. *colors* = 'r' or *colors* =
              'red', all labels will be plotted in this color

            - if a tuple of matplotlib color args (string, float, rgb, etc),
              different labels will be plotted in different colors in the order
              specified

          *inline*:
            controls whether the underlying contour is removed or
            not. Default is *True*.

          *inline_spacing*:
            space in pixels to leave on each side of label when
            placing inline.  Defaults to 5.  This spacing will be
            exact for labels at locations where the contour is
            straight, less so for labels on curved contours.

          *fmt*:
            a format string for the label. Default is '%1.3f'
            Alternatively, this can be a dictionary matching contour
            levels with arbitrary strings to use for each contour level
            (i.e., fmt[level]=string)

          *manual*:
            if *True*, contour labels will be placed manually using
            mouse clicks.  Click the first button near a contour to
            add a label, click the second button (or potentially both
            mouse buttons at once) to finish adding labels.  The third
            button can be used to remove the last label added, but
            only if labels are not inline.  Alternatively, the keyboard
            can be used to select label locations (enter to end label
            placement, delete or backspace act like the third mouse button,
            and any other key will select a label location).

          *rightside_up*:
            if *True* (default), label rotations will always be plus
            or minus 90 degrees from level.

          *use_clabeltext*:
            if *True* (default is False), ClabelText class (instead of
            matplotlib.Text) is used to create labels. ClabelText
            recalculates rotation angles of texts during the drawing time,
            therefore this can be used if aspect of the axes changes.

        .. plot:: mpl_examples/pylab_examples/contour_demo.py
        """
        """
        NOTES on how this all works:

        clabel basically takes the input arguments and uses them to
        add a list of "label specific" attributes to the ContourSet
        object.  These attributes are all of the form label* and names
        should be fairly self explanatory.

        Once these attributes are set, clabel passes control to the
        labels method (case of automatic label placement) or
        BlockingContourLabeler (case of manual label placement).
        """

        fontsize = kwargs.get('fontsize', None)
        inline = kwargs.get('inline', 1)
        inline_spacing = kwargs.get('inline_spacing', 5)
        self.labelFmt = kwargs.get('fmt', '%1.3f')
        _colors = kwargs.get('colors', None)

        self._use_clabeltext = kwargs.get('use_clabeltext', False)

        # Detect if manual selection is desired and remove from argument list
        self.labelManual = kwargs.get('manual', False)

        self.rightside_up = kwargs.get('rightside_up', True)

        if len(args) == 0:
            levels = self.levels
            indices = range(len(self.levels))
        elif len(args) == 1:
            levlabs = list(args[0])
            indices, levels = [], []
            for i, lev in enumerate(self.levels):
                if lev in levlabs:
                    indices.append(i)
                    levels.append(lev)
            if len(levels) < len(levlabs):
                msg = "Specified levels " + str(levlabs)
                msg += "\n don't match available levels "
                msg += str(self.levels)
                raise ValueError(msg)
        else:
            raise TypeError("Illegal arguments to clabel, see help(clabel)")
        self.labelLevelList = levels
        self.labelIndiceList = indices

        self.labelFontProps = font_manager.FontProperties()
        if fontsize == None:
            font_size = int(self.labelFontProps.get_size_in_points())
        else:
            if type(fontsize) not in [int, float, str]:
                raise TypeError("Font size must be an integer number.")
                # Can't it be floating point, as indicated in line above?
            else:
                if type(fontsize) == str:
                    font_size = int(self.labelFontProps.get_size_in_points())
                else:
                    self.labelFontProps.set_size(fontsize)
                    font_size = fontsize
        self.labelFontSizeList = [font_size] * len(levels)

        if _colors == None:
            self.labelMappable = self
            self.labelCValueList = np.take(self.cvalues, self.labelIndiceList)
        else:
            cmap = colors.ListedColormap(_colors, N=len(self.labelLevelList))
            self.labelCValueList = range(len(self.labelLevelList))
            self.labelMappable = cm.ScalarMappable(cmap=cmap,
                                                   norm=colors.NoNorm())

        #self.labelTexts = []   # Initialized in ContourSet.__init__
        #self.labelCValues = [] # same
        self.labelXYs = []

        if self.labelManual:
            print 'Select label locations manually using first mouse button.'
            print 'End manual selection with second mouse button.'
            if not inline:
                print 'Remove last label by clicking third mouse button.'

            blocking_contour_labeler = BlockingContourLabeler(self)
            blocking_contour_labeler(inline, inline_spacing)
        else:
            self.labels(inline, inline_spacing)

        # Hold on to some old attribute names.  These are depricated and will
        # be removed in the near future (sometime after 2008-08-01), but keeping
        # for now for backwards compatibility
        self.cl = self.labelTexts
        self.cl_xy = self.labelXYs
        self.cl_cvalues = self.labelCValues

        self.labelTextsList = cbook.silent_list('text.Text', self.labelTexts)
        return self.labelTextsList
Ejemplo n.º 23
0
    def _i_mtv(self, data, wcs, title, isMask):
        """Internal routine to display an Image or Mask on a DS9 display"""

        title = str(title) if title else ""
        dataArr = data.getArray()

        if isMask:
            maskPlanes = data.getMaskPlaneDict()
            nMaskPlanes = max(maskPlanes.values()) + 1

            planes = {}  # build inverse dictionary
            for key in maskPlanes:
                planes[maskPlanes[key]] = key

            planeList = range(nMaskPlanes)

            maskArr = np.zeros_like(dataArr, dtype=np.int32)

            colorNames = ['black']
            colorGenerator = self.display.maskColorGenerator(omitBW=True)
            for p in planeList:
                color = self.display.getMaskPlaneColor(
                    planes[p]) if p in planes else None

                if not color:  # none was specified
                    color = next(colorGenerator)
                elif color.lower() == afwDisplay.IGNORE:
                    color = 'black'  # we'll set alpha = 0 anyway

                colorNames.append(color)
            #
            # Set the maskArr image to be an index into our colour map (cmap; see below)
            #
            for i, p in enumerate(planeList):
                color = colorNames[i]
                maskArr[(dataArr & (1 << p)) !=
                        0] += i + 1  # + 1 as we set colorNames[0] to black

            #
            # Convert those colours to RGBA so we can have per-mask-plane transparency
            # and build a colour map
            #
            colors = mpColors.to_rgba_array(colorNames)
            colors[0][3] = 0.0  # it's black anyway
            for i, p in enumerate(planeList):
                if colorNames[i + 1] == 'black':
                    alpha = 0.0
                else:
                    alpha = 1 - self._getMaskTransparency(planes[p] if p in
                                                          planes else None)

                colors[i + 1][3] = alpha

            dataArr = maskArr
            cmap = mpColors.ListedColormap(colors)
            norm = mpColors.NoNorm()
        else:
            cmap = pyplot.cm.gray
            norm = self._normalize

        ax = self._figure.gca()
        bbox = data.getBBox()
        mappable = ax.imshow(
            dataArr,
            origin='lower',
            interpolation='nearest',
            extent=(bbox.getBeginX() - 0.5, bbox.getEndX() - 0.5,
                    bbox.getBeginY() - 0.5, bbox.getEndY() - 0.5),
            cmap=cmap,
            norm=norm)

        if not isMask:
            self._mappable = mappable

        self._figure.canvas.draw_idle()
Ejemplo n.º 24
0
    def __init__(self,
                 labels=None,
                 seed=None,
                 alpha=150,
                 index_direct=True,
                 min_val=0,
                 max_val=255,
                 min_any=0,
                 background=None,
                 dup_for_neg=False,
                 symmetric_colors=False,
                 cmap_labels=None):
        """Generate discrete colormap for labels using 
        :func:``discrete_colormap``.
        
        Args:
            labels: Labels of integers for which a distinct color should be 
                mapped to each unique label. Defults to None, in which case 
                no colormap will be generated.
            seed: Seed for randomizer to allow consistent colormap between 
                runs; defaults to None.
            alpha: Transparency leve; defaults to 150 for semi-transparent.
            index_direct: True if the colormap will be indexed directly, which 
                assumes that the labels will serve as indexes to the colormap 
                and should span sequentially from 0, 1, 2, ...; defaults to 
                True. If False, a colormap will be generated for the full 
                range of integers between the lowest and highest label values, 
                inclusive, with a :obj:`colors.BoundaryNorm`, which may
                incur performance cost.
            min_val (int): Minimum value for random numbers; defaults to 0.
            max_val (int): Maximum value for random numbers; defaults to 255.
            min_any (int, float): Minimum value above which at least one value
                must be in each set of RGB values; defaults to 0
            background: Tuple of (backround_label, (R, G, B, A)), where 
                background_label is the label value specifying the background, 
                and RGBA value will replace the color corresponding to that 
                label. Defaults to None.
            dup_for_neg: True to duplicate positive labels as negative 
                labels to recreate the same set of labels as for a 
                mirrored labels map. Defaults to False.
            symmetric_colors (bool): True to make symmetric colors, assuming
                symmetric labels centered on 0; defaults to False.
            cmap_labels (List[str]): Sequence of colors as Matplotlib color
                strings or RGB(A) hex (eg "#0fab24ff") strings.
        """
        self.norm = None
        self.cmap_labels = None
        self.img_labels = None
        self.symmetric_colors = symmetric_colors

        if labels is None: return
        labels_unique = np.unique(labels)
        if dup_for_neg and np.sum(labels_unique < 0) == 0:
            # for labels that are only >= 0, duplicate the pos portion
            # as neg so that images with or without negs use the same colors
            labels_unique = np.append(
                -1 * labels_unique[labels_unique > 0][::-1], labels_unique)
        num_colors = len(labels_unique)

        labels_offset = 0
        if index_direct:
            # assume label vals increase by 1 from 0 until num_colors; store
            # sorted labels sequence to translate labels based on index
            self.norm = colors.NoNorm()
            self.img_labels = labels_unique
        else:
            # use labels as bounds for each color, including wide bounds
            # for large gaps between successive labels; offset bounds to
            # encompass each label and avoid off-by-one errors that appear
            # when viewing images with additional extreme labels; float32
            # gives unsymmetric colors for large values in mirrored atlases
            # despite remaining within range for unclear reasons, fixed by
            # using float64 instead
            labels_offset = 0.5
            bounds = labels_unique.astype(np.float64)
            bounds -= labels_offset
            # number of boundaries should be one more than number of labels to
            # avoid need for interpolation of boundary bin numbers and
            # potential merging of 2 extreme labels
            bounds = np.append(bounds, [bounds[-1] + 1])
            # TODO: may have occasional colormap inaccuracies from this bug:
            # https://github.com/matplotlib/matplotlib/issues/9937;
            self.norm = colors.BoundaryNorm(bounds, num_colors)
        if cmap_labels is None:
            # auto-generate colors for the number of labels
            self.cmap_labels = discrete_colormap(num_colors,
                                                 alpha,
                                                 False,
                                                 seed,
                                                 min_val,
                                                 max_val,
                                                 min_any,
                                                 symmetric_colors,
                                                 jitter=20,
                                                 mode=DiscreteModes.RANDOMN)
        else:
            # generate RGBA colors from supplied color strings
            self.cmap_labels = colors.to_rgba_array(cmap_labels) * max_val
        if background is not None:
            # replace background label color with given color
            bkgdi = np.where(labels_unique == background[0] - labels_offset)
            if len(bkgdi) > 0 and bkgdi[0].size > 0:
                self.cmap_labels[bkgdi[0][0]] = background[1]
        #print(self.cmap_labels)
        self.make_cmap()
Ejemplo n.º 25
0
axarr[0].set_title('Image')

cropax = axarr[1].imshow(x, cmap=plt.get_cmap('gray'))
axarr[1].set_title('Cropped Image')

dx = np.arange(-np.floor(dsize[1]/2), np.floor(dsize[1]/2)+1, dtype=int)
dy = np.arange(-np.floor(dsize[0]/2), np.floor(dsize[0]/2)+1, dtype=int)
[dpx, dpy] = np.meshgrid(dx, dy)
dpx = dpx.reshape(-1, 1)
dpy = dpy.reshape(-1, 1)
dp = np.hstack((dpx, dpy))
N = dpx.size

all_patches = np.ones((N*dsize[0], dsize[1]))
all_patchax = axarr[2].imshow(all_patches, cmap=plt.get_cmap('gray'),
                              aspect='auto', norm=colors.NoNorm())
axarr[2].set_title('Concatenation of Sub-Images (X)')

X = np.zeros((N, N))
Y = np.zeros((N, 1))

sigma = 5


def init():
    return [cropax, patch, all_patchax]


def animate(i):
    global X, Y, dp, gnd_p, sigma, all_patches, patch, cropax, all_patchax, N
Ejemplo n.º 26
0
    def _i_mtv(self, data, wcs, title, isMask):
        """Internal routine to display an Image or Mask on a DS9 display"""

        title = str(title) if title else ""
        dataArr = data.getArray()

        if isMask:
            maskPlanes = data.getMaskPlaneDict()
            nMaskPlanes = max(maskPlanes.values()) + 1

            planes = {}  # build inverse dictionary
            for key in maskPlanes:
                planes[maskPlanes[key]] = key

            planeList = range(nMaskPlanes)

            maskArr = np.zeros_like(dataArr, dtype=np.int32)

            colorNames = ['black']
            colorGenerator = self.display.maskColorGenerator(omitBW=True)
            for p in planeList:
                color = self.display.getMaskPlaneColor(
                    planes[p]) if p in planes else None

                if not color:  # none was specified
                    color = next(colorGenerator)
                elif color.lower() == afwDisplay.IGNORE:
                    color = 'black'  # we'll set alpha = 0 anyway

                colorNames.append(color)
            #
            # Convert those colours to RGBA so we can have per-mask-plane
            # transparency and build a colour map
            #
            # Pixels equal to 0 don't get set (as no bits are set), so leave
            # them transparent and start our colours at [1] --
            # hence "i + 1" below
            #
            colors = mpColors.to_rgba_array(colorNames)
            alphaChannel = 3  # the alpha channel; the A in RGBA
            colors[0][alphaChannel] = 0.0  # it's black anyway
            for i, p in enumerate(planeList):
                if colorNames[i + 1] == 'black':
                    alpha = 0.0
                else:
                    alpha = 1 - self._getMaskTransparency(planes[p] if p in
                                                          planes else None)

                colors[i + 1][alphaChannel] = alpha

            cmap = mpColors.ListedColormap(colors)
            norm = mpColors.NoNorm()
        else:
            cmap = self._image_colormap
            norm = self._normalize

        ax = self._figure.gca()
        bbox = data.getBBox()
        extent = (bbox.getBeginX() - 0.5, bbox.getEndX() - 0.5,
                  bbox.getBeginY() - 0.5, bbox.getEndY() - 0.5)

        with pyplot.rc_context(dict(interactive=False)):
            if isMask:
                for i, p in reversed(list(enumerate(planeList))):
                    if colors[i +
                              1][alphaChannel] == 0:  # colors[0] is reserved
                        continue

                    bitIsSet = (dataArr & (1 << p)) != 0
                    if bitIsSet.sum() == 0:
                        continue

                    maskArr[
                        bitIsSet] = i + 1  # + 1 as we set colorNames[0] to black

                    if not self._fastMaskDisplay:  # we draw each bitplane separately
                        ax.imshow(maskArr,
                                  origin='lower',
                                  interpolation='nearest',
                                  extent=extent,
                                  cmap=cmap,
                                  norm=norm)
                        maskArr[:] = 0

                if self._fastMaskDisplay:  # we only draw the lowest bitplane
                    ax.imshow(maskArr,
                              origin='lower',
                              interpolation='nearest',
                              extent=extent,
                              cmap=cmap,
                              norm=norm)
            else:
                mappable = ax.imshow(dataArr,
                                     origin='lower',
                                     interpolation='nearest',
                                     extent=extent,
                                     cmap=cmap,
                                     norm=norm)
                self._mappable = mappable

        self._figure.canvas.draw_idle()
Ejemplo n.º 27
0
def main_kohonen(args):
    """testing the map from extero to proprio sensor subspaces

    it is general in that if the map is underdetermined (dim_e < dim_p),
    the map can cope with multiple solutions and samples from the joint density

    in-situ Hebbian linked SOMs (Miikkulainen) using lmjohns3's kohonen python lib"""
    
    from kohonen.kohonen import Map, Parameters, ExponentialTimeseries, ConstantTimeseries
    from kohonen.kohonen import Gas, GrowingGas, GrowingGasParameters, Filter
    import matplotlib.colors as mcolors
    from mpl_toolkits.mplot3d import Axes3D

    # extract datadir from datfile
    datadir = "/".join(args.datafile.split("/")[:-1])
    # load offline data from active inference experiment
    # EP = np.load("EP_1000.npy") # [:200]
    # EP = np.load("EP.npy") # [:500]
    # EP = np.load("EP.npy")[500:600]
    EP = np.load(args.datafile)

    som_e_save = datadir + "/" + "som_filter_e.pkl"
    som_p_save = datadir + "/" + "som_filter_p.pkl"
    som_e2p_link_save = datadir + "/" + "som_e2p_link.npy"
    
    # learning rate proxy
    ET = ExponentialTimeseries

    # som argument dict
    def kwargs(shape=(10, 10), z=0.001, dimension=2, lr_init = 1.0, neighborhood_size = 1):
        return dict(dimension=dimension,
                    shape=shape,
                    neighborhood_size = neighborhood_size,
                    learning_rate=ET(-1e-4, lr_init, 0.01),
                    noise_variance=z)
    
    mapsize = 4
    # FIXME: make neighborhood_size decrease with time
    
    # SOM exteroceptive stimuli 2D input
    kw_e = kwargs(shape = (mapsize, mapsize), dimension = dim_e, lr_init = 0.5, neighborhood_size = 0.6)
    som_e = Map(Parameters(**kw_e))

    # SOM proprioceptive stimuli 3D input
    kw_p = kwargs(shape = (int(mapsize * 1.5), int(mapsize * 1.5)), dimension = dim_p, lr_init = 0.5, neighborhood_size = 0.7)
    som_p = Map(Parameters(**kw_p))
    
    # kw = kwargs((64, ))
    # som_e = Gas(Parameters(**kw))
    # kw = kwargs((64, ))
    # kw['growth_interval'] = 7
    # kw['max_connection_age'] = 17
    # som_e = GrowingGas(GrowingGasParameters(**kw))
    
    # kw_f_e = kwargs(shape = (mapsize, mapsize), dimension = 2, neighborhood_size = 0.75, lr_init = 1.0)
    # filter_e = Filter(Map(Parameters(**kw_f_e)))
    
    # create "filter" using existing SOM_e, filter computes activation on distance
    filter_e = Filter(som_e, history=lambda: 0.0)
    filter_e.reset()

    # kw_f_p = kwargs(shape = (mapsize * 3, mapsize * 3), dimension = 3, neighborhood_size = 0.5, lr_init = 0.1)
    # filter_p = Filter(Map(Parameters(**kw_f_p)), history=lambda: 0.01)
    
    # create "filter" using existing SOM_p, filter computes activation on distance
    filter_p = Filter(som_p, history=lambda: 0.0)
    filter_p.reset()

    # Hebbian links
    # hebblink_som    = np.random.uniform(-1e-4, 1e-4, (np.prod(som_e._shape), np.prod(som_p._shape)))
    # hebblink_filter = np.random.uniform(-1e-4, 1e-4, (np.prod(filter_e.map._shape), np.prod(filter_p.map._shape)))
    hebblink_som    = np.zeros((np.prod(som_e._shape), np.prod(som_p._shape)))
    hebblink_filter = np.zeros((np.prod(filter_e.map._shape), np.prod(filter_p.map._shape)))

    # # plot initial weights as images
    # pl.subplot(211)
    # pl.imshow(hebblink_som + 0.5, interpolation="none", norm=mcolors.NoNorm())
    # pl.colorbar()
    # pl.subplot(212)
    # pl.imshow(hebblink_filter + 0.5, interpolation="none", norm=mcolors.NoNorm())
    # pl.colorbar()
    # pl.show()

    fig = pl.figure()
    numepisodes_som = 10 # 30
    numepisodes_hebb = 10 # 30
    numsteps = EP.shape[0]
    
    ################################################################################
    # check for trained SOM
    if not os.path.exists(som_e_save) and \
        not os.path.exists(som_p_save):
    
        ################################################################################
        # train the SOMs on the input
        # som_e_barbase = np.linspace(-2, 2, np.prod(filter_e.map._shape))
        # som_p_barbase = np.linspace(-2, 2, np.prod(filter_p.map._shape))
        
        pl.ion()
        # pl.figure()
        # pl.show()
        for j in range(numepisodes_som):
            for i in range(numsteps):
                e = EP[i,:dim_e]
                p = EP[i,dim_e:]

                # don't learn twice
                # som_e.learn(e)
                # som_p.learn(p)
                filter_e.learn(e)
                filter_p.learn(p)
                # print np.argmin(som_e.distances(e)) # , som_e.distances(e)
    
                # on plot interval, update plot :)
                if i % 50 == 0:
                    ax = fig.add_subplot(121)
                    # pl.subplot(121)
                    ax.cla()
                    ax.plot(filter_e.map.neurons[:,:,0], filter_e.map.neurons[:,:,1], "ko", alpha=0.5, ms=10)
                    # data
                    ax.plot(e[0], e[1], "bx", alpha=0.7, ms=12)
                    # winner
                    # ax.plot(som_e.neurons[w_e[0],w_e[1],0], som_e.neurons[w_e[0],w_e[1],1], "ro", alpha=0.7, ms=12)
                    # ax.bar(som_e_barbase, filter_e.distances(e).flatten(), width=0.01, bottom = -2, alpha=0.1)
                    # ax.stem(filter_e.distances(e), "k", alpha=0.1)
                    ax.set_xlim((-2, 2))
                    ax.set_ylim((-2, 2))
                    ax.set_aspect(1)
                    ax.text(-1.0, 1.2, "%d / %d, eta = %f" % ((j * numsteps) + i, numepisodes_som * numsteps, som_e._learning_rate()))
    
                    ax = fig.add_subplot(122) #, projection='3d')
                    # pl.subplot(122)
                    ax.cla()
                    ax.plot(filter_p.map.neurons[:,:,0], filter_p.map.neurons[:,:,1], "ko", alpha=0.5, ms=10)
                    # ax.bar(som_p_barbase, filter_p.distances(p).flatten(), width=0.01, bottom = -2, alpha=0.1)
                    ax.set_xlim((-2, 2))
                    ax.set_ylim((-2, 2))
                    pl.gca().set_aspect(1)

                    pl.pause(0.01)
                    pl.draw()
                    # print som_e.neurons
                    
        ################################################################################
        # save SOMs
        # print "filter.map shapes", filter_e.map._shape, filter_p.map._shape
        cPickle.dump(filter_e.map, open(som_e_save, "wb"))
        cPickle.dump(filter_p.map, open(som_p_save, "wb"))
    
    else:
        print "loading existing SOMs"
        filter_e.map = cPickle.load(open(som_e_save, "rb"))
        filter_p.map = cPickle.load(open(som_p_save, "rb"))
        # filter_e.reset()
        # filter_p.reset()
        # print "filter.map shapes", filter_e.map._shape, filter_p.map._shape
        j = numepisodes_som - 1
        i = numsteps - 1

    # sys.exit()
        
    ################################################################################
    # plot final SOM configurations
    pl.ioff()
    # pl.subplot(121)
    ax = fig.add_subplot(121)
    ax.cla()
    ax.plot(filter_e.map.neurons[:,:,0], filter_e.map.neurons[:,:,1], "ko", alpha=0.5, ms=10)
    ax.plot(EP[:,0], EP[:,1], "r.", alpha=0.3, label="data_e")
    ax.set_xlim((-2, 2))
    ax.set_ylim((-2, 2))
    ax.set_aspect(1)
    ax.text(-0.8, 0.7, "%d, eta = %f" % ((j * EP.shape[0]) + i, som_e._learning_rate()))
    ax.legend()
    # pl.subplot(224)
    ax = fig.add_subplot(122, projection='3d')
    ax.scatter(filter_p.map.neurons[:,:,0], filter_p.map.neurons[:,:,1], filter_p.map.neurons[:,:,2], "ko", alpha=0.5)#, ms=10)
    ax.plot(EP[:,2], EP[:,3], EP[:,4], "r.", alpha=0.3, ms=5)
    #ax.set_xlim((-2, 2))
    #ax.set_ylim((-2, 2))
    #ax.set_zlim((-2, 2))
    pl.gca().set_aspect(1)
    pl.show()
    
    ################################################################################
    # fix the SOMs with learning rate constant 0
    CT = ConstantTimeseries
    filter_e.map.learning_rate = CT(0.0)
    filter_p.map.learning_rate = CT(0.0)
        
    ################################################################################
    # now train Hebbian associations on fixed SOM

    use_activity = True
    
    # Hebbian learning rate
    if use_activity:
        et = ExponentialTimeseries(-1e-4, 0.8, 0.001)
        # et = ConstantTimeseries(0.5)
    else:
        et = ConstantTimeseries(1e-5)
    e_shape = (np.prod(filter_e.map._shape), 1)
    p_shape = (np.prod(filter_p.map._shape), 1)

    z_err_coef = 0.99
    z_err_norm_ = 1
    Z_err_norm  = np.zeros((numepisodes_hebb*numsteps,1))
    Z_err_norm_ = np.zeros((numepisodes_hebb*numsteps,1))
    W_norm      = np.zeros((numepisodes_hebb*numsteps,1))

    if not os.path.exists(som_e2p_link_save):
        for j in range(numepisodes_hebb):
            # while 
            for i in range(numsteps):
                e = EP[i,:dim_e]
                p = EP[i,dim_e:]

                # just activate
                filter_e.learn(e)
                filter_p.learn(p)

                # fetch data induced activity
                if use_activity:
                    p_    = filter_p.activity.reshape(p_shape)
                else:
                    p_    = filter_p.distances(p).flatten().reshape(p_shape)
                
                # # get winner coord and flattened index
                # w_e_idx = som_e.winner(e)
                # # w_p_idx = som_p.winner(p)
                # w_e = som_e.flat_to_coords(w_e_idx)
                # # w_p = som_p.flat_to_coords(w_p_idx)
    
                # # get winner coord and flattened index
                # w_f_e_idx = filter_e.winner(e)
                # # w_f_p_idx = filter_p.winner(p)
                # w_f_e = filter_e.flat_to_coords(w_f_e_idx)
                # # w_f_p = filter_p.flat_to_coords(w_f_p_idx)
            
                # print som_e.neuron(w_e), som_p.neuron(w_p)

                # 
                # hebblink_som[w_e_idx,w_p_idx] += 0.01
            
                # hebblink_filter[w_f_e_idx,w_f_p_idx] += 0.01
            
                # print "filter_e.activity", filter_e.activity
                # print "filter_p.activity", filter_p.activity
                # print "np.outer(filter_e.activity.flatten(), filter_p.activity.flatten())", np.outer(filter_e.activity.flatten(), filter_p.activity.flatten()).shape
            
                # compute prediction for p using e activation and hebbian weights
                if use_activity:
                    p_bar = np.dot(hebblink_filter.T, filter_e.activity.reshape(e_shape))
                else:
                    p_bar = np.dot(hebblink_filter.T, filter_e.distances(e).flatten().reshape(e_shape))

                # inject activity prediction
                p_bar_sum = p_bar.sum()
                if p_bar_sum > 0:
                    p_bar_normed = p_bar / p_bar_sum
                else:
                    p_bar_normed = np.zeros(p_bar.shape)
            
                        
                # print np.linalg.norm(p_), np.linalg.norm(p_bar)
            
                # clip p_bar positive             
                # p_bar = np.clip(p_bar, 0, np.inf)
    
                # compute prediction error: data induced activity - prediction
                z_err = p_ - p_bar
                # z_err = p_bar - p_
                z_err_norm = np.linalg.norm(z_err, 2)
                if j == 0 and i == 0:
                    z_err_norm_ = z_err_norm
                else:
                    z_err_norm_ = z_err_coef * z_err_norm_ + (1 - z_err_coef) * z_err_norm
                w_norm = np.linalg.norm(hebblink_filter)

                logidx = (j*numsteps) + i
                Z_err_norm [logidx] = z_err_norm
                Z_err_norm_[logidx] = z_err_norm_
                W_norm     [logidx] = w_norm
            
                # z_err = p_bar - filter_p.activity.reshape(p_bar.shape)
                # print "p_bar.shape", p_bar.shape
                # print "filter_p.activity.flatten().shape", filter_p.activity.flatten().shape
                if i % 100 == 0:
                    print "iter %d/%d: z_err.shape = %s, |z_err| = %f, |W| = %f, |p_bar_normed| = %f" % (logidx, (numepisodes_hebb*numsteps), z_err.shape, z_err_norm_, w_norm, np.linalg.norm(p_bar_normed))
                    # print 
            
                # d_hebblink_filter = et() * np.outer(filter_e.activity.flatten(), filter_p.activity.flatten())
                if use_activity:
                    d_hebblink_filter = et() * np.outer(filter_e.activity.flatten(), z_err)
                else:
                    d_hebblink_filter = et() * np.outer(filter_e.distances(e), z_err)
                hebblink_filter += d_hebblink_filter
        np.save(som_e2p_link_save, hebblink_filter)

        ################################################################################
        # show final Hebbian weights
        pl.subplot(411)
        pl.imshow(hebblink_som + 0.5, interpolation="none", norm=mcolors.NoNorm())
        pl.colorbar()
        pl.subplot(412)
        # pl.imshow(hebblink_filter + 0.5, interpolation="none", norm=mcolors.NoNorm())
        pl.imshow(hebblink_filter, interpolation="none")
        pl.colorbar()
        pl.subplot(413)
        pl.plot(Z_err_norm, linewidth=0.5)
        pl.plot(Z_err_norm_)
        pl.gca().set_yscale("log")
        pl.subplot(414)
        pl.plot(W_norm)
        pl.show()
    else:
        print "Loading existing Hebbian link"
        hebblink_filter = np.load(som_e2p_link_save)
    


    # do prediction using activation propagation from extero map to proprio map via hebbian links
    from explauto import Environment
    environment = Environment.from_configuration('simple_arm', 'low_dimensional')
    environment.noise = 0.

    sampling_search_num = 100
    
    P_ = np.zeros((EP.shape[0], dim_p))    
    E_ = np.zeros((EP.shape[0], dim_e))
    # e2p_w_p_weights = filter_p.neuron(filter_p.flat_to_coords(filter_p.sample(1)[0]))
    for i in range(EP.shape[0]):
        e = EP[i,:dim_e]
        p = EP[i,dim_e:]
        # print np.argmin(som_e.distances(e)), som_e.distances(e)
        filter_e.learn(e)
        # print "filter_e.winner(e)", filter_e.winner(e)
        # filter_p.learn(p)
        # print "filter_e.activity.shape", filter_e.activity.shape
        # import pdb; pdb.set_trace()
        if use_activity:
            e2p_activation = np.dot(hebblink_filter.T, filter_e.activity.reshape((np.prod(filter_e.map._shape), 1)))
            filter_p.activity = np.clip((e2p_activation / np.sum(e2p_activation)).reshape(filter_p.map._shape), 0, np.inf)
        else:
            e2p_activation = np.dot(hebblink_filter.T, filter_e.distances(e).flatten().reshape(e_shape))
        # print "e2p_activation.shape, np.sum(e2p_activation)", e2p_activation.shape, np.sum(e2p_activation)
        # print "filter_p.activity.shape", filter_p.activity.shape
        # print "np.sum(filter_p.activity)", np.sum(filter_p.activity), (filter_p.activity >= 0).all()

        # filter_p.learn(p)
        emode = 0 # 1, 2
        if i % 1 == 0:
            if emode == 0:
                e2p_w_p_weights_ = []
                for k in range(sampling_search_num):
                    e2p_w_p_weights = filter_p.neuron(filter_p.flat_to_coords(filter_p.sample(1)[0]))
                    e2p_w_p_weights_.append(e2p_w_p_weights)
                pred = np.array(e2p_w_p_weights_)
                # print "pred", pred
                pred_err = np.linalg.norm(pred - p, 2, axis=1)
                # print "np.linalg.norm(e2p_w_p_weights - p, 2)", np.linalg.norm(e2p_w_p_weights - p, 2)
                e2p_w_p = np.argmin(pred_err)
                print "pred_err", e2p_w_p, pred_err[e2p_w_p]
                e2p_w_p_weights = e2p_w_p_weights_[e2p_w_p]
            elif emode == 1:
                if use_activity:
                    e2p_w_p = np.argmax(e2p_activation)
                else:
                    e2p_w_p = np.argmin(e2p_activation)
                e2p_w_p_weights = filter_p.neuron(filter_p.flat_to_coords(e2p_w_p))
                    
            elif emode == 2:
                e2p_w_p = filter_p.winner(p)
                e2p_w_p_weights = filter_p.neuron(filter_p.flat_to_coords(e2p_w_p))
        P_[i] = e2p_w_p_weights
        E_[i] = environment.compute_sensori_effect(P_[i])

        # print "e * hebb", e2p_w_p, e2p_w_p_weights
        
    pl.ioff()

    err_extero = EP[:,:dim_e] - E_
    print "final error MSE", np.mean(np.square(err_extero))
    
    pl.subplot(211)
    pl.title("Extero measured and predicted by E2P with P prediction reexecuted through system")
    pl.plot(EP[:,:dim_e])
    pl.plot(E_)
    pl.subplot(212)
    pl.title("Proprio measured and predicted by E2P")
    pl.plot(EP[:,dim_e:])
    pl.plot(P_)
    
    # pl.subplot(412)
    # pl.title("Proprio dim 1")
    # pl.plot(EP[:,2])
    # pl.plot(P_[:,0])
    # pl.subplot(413)
    # pl.title("Proprio dim 2")
    # pl.plot(EP[:,3])
    # pl.plot(P_[:,1])
    # pl.subplot(414)
    # pl.title("Proprio dim 3")
    # pl.plot(EP[:,4])
    # pl.plot(P_[:,2])
    pl.show()
Ejemplo n.º 28
0
    def _i_mtv(self, data, wcs, title, isMask):
        """Internal routine to display an Image or Mask on a DS9 display"""

        title = str(title) if title else ""
        dataArr = data.getArray()

        if isMask:
            maskPlanes = data.getMaskPlaneDict()
            nMaskPlanes = max(maskPlanes.values()) + 1

            planes = {}                      # build inverse dictionary
            for key in maskPlanes:
                planes[maskPlanes[key]] = key

            planeList = range(nMaskPlanes)

            maskArr = np.zeros_like(dataArr, dtype=np.int32)

            colors = ['black']
            colorGenerator = self.display.maskColorGenerator(omitBW=True)
            for p in planeList:
                color = self.display.getMaskPlaneColor(planes[p]) if p in planes else None

                if not color:            # none was specified
                    color = next(colorGenerator)

                colors.append(color)
            #
            # Set the maskArr image to be an index into our colour map (cmap; see below)
            #
            for i, p in enumerate(planeList):
                color = colors[i]
                if color.lower() == "ignore":
                    continue

                maskArr[(dataArr & (1 << p)) != 0] += i + 1 # + 1 as we set colors[0] to black

            #
            # Convert those colours to RGBA so we can have per-mask-plane transparency
            # and build a colour map
            #
            colors = mpColors.to_rgba_array(colors)
            colors[0][3] = 0.0          # it's black anyway
            for i, p in enumerate(planeList):
                colors[i + 1][3] = 1 - self._getMaskTransparency(planes[p] if p in planes else None)

            dataArr = maskArr
            cmap = mpColors.ListedColormap(colors)
            norm = mpColors.NoNorm()
        else:
            cmap = pyplot.cm.gray
            norm = self._normalize

        ax = self._figure.gca()
        bbox = data.getBBox()
        ax.imshow(dataArr, origin='lower', interpolation='nearest',
                  extent=(bbox.getBeginX() - 0.5, bbox.getEndX() - 0.5,
                          bbox.getBeginY() - 0.5, bbox.getEndY() - 0.5),
                  cmap=cmap, norm=norm)

        if False:
            if evData:
                axes = self._figure.get_axes()[0]
                myText = axes.text(0.05, 1.05, 'Press "return" to show intensity here',
                                   transform=axes.transAxes, va='top')
                
                global eventHandlers
                eventHandlers[self._figure] = EventHandler((evData, myText), self._figure)
                
        self._figure.canvas.draw_idle()
Ejemplo n.º 29
0
    lim = axes.get_xlim() + axes.get_ylim()
    for bar in bars:
        bar.set_zorder(1)
        bar.set_facecolor("none")
        x, y = bar.get_xy()
        w, h = bar.get_width(), bar.get_height()
        a_min = 0.4
        inverse = 1 - a_min
        maximum = a_min + (inverse * w / max(y_val))
        z = create_gradient_array(color, alpha_min=a_min, alpha_max=maximum)
        z = np.rot90(z)
        payload = dict(extent=[x, x + w, y, y + h],
                       aspect="auto",
                       zorder=0,
                       norm=mcolors.NoNorm(vmin=0, vmax=1))
        axes.imshow(z, **payload)

    axes.axis(lim)
    return save_matplotlib(fig, axes)


def save_matplotlib(fig, axes):
    fig.delaxes(axes)
    fig.add_axes(axes)
    buffer = io.BytesIO()
    fig.savefig(buffer, transparent=True, bbox_inches="tight")
    axes.clear()
    fig.clf()
    plt.close(fig)
    return buffer
Ejemplo n.º 30
0
    def clabel(self, *args, **kwargs):
        """
        clabel(CS, **kwargs) - add labels to line contours in CS,
               where CS is a ContourSet object returned by contour.

        clabel(CS, V, **kwargs) - only label contours listed in V

        keyword arguments:

        * fontsize = None: as described in http://matplotlib.sf.net/fonts.html

        * colors = None:

           - a tuple of matplotlib color args (string, float, rgb, etc),
             different labels will be plotted in different colors in the order
             specified

           - one string color, e.g. colors = 'r' or colors = 'red', all labels
             will be plotted in this color

           - if colors == None, the color of each label matches the color
             of the corresponding contour

        * inline = True: controls whether the underlying contour is removed
                     (inline = True) or not (False)

        * fmt = '%1.3f': a format string for the label

        """
        fontsize = kwargs.get('fontsize', None)
        inline = kwargs.get('inline', 1)
        self.fmt = kwargs.get('fmt', '%1.3f')
        _colors = kwargs.get('colors', None)



        if len(args) == 0:
            levels = self.levels
            indices = range(len(self.levels))
        elif len(args) == 1:
            levlabs = list(args[0])
            indices, levels = [], []
            for i, lev in enumerate(self.levels):
                if lev in levlabs:
                    indices.append(i)
                    levels.append(lev)
            if len(levels) < len(levlabs):
                msg = "Specified levels " + str(levlabs)
                msg += "\n don't match available levels "
                msg += str(self.levels)
                raise ValueError(msg)
        else:
            raise TypeError("Illegal arguments to clabel, see help(clabel)")
        self.label_levels = levels
        self.label_indices = indices

        self.fp = font_manager.FontProperties()
        if fontsize == None:
            font_size = int(self.fp.get_size_in_points())
        else:
            if type(fontsize) not in [int, float, str]:
                raise TypeError("Font size must be an integer number.")
                # Can't it be floating point, as indicated in line above?
            else:
                if type(fontsize) == str:
                    font_size = int(self.fp.get_size_in_points())
                else:
                    self.fp.set_size(fontsize)
                    font_size = fontsize
        self.fslist = [font_size] * len(levels)

        if _colors == None:
            self.label_mappable = self
            self.label_cvalues = npy.take(self.cvalues, self.label_indices)
        else:
            cmap = colors.ListedColormap(_colors, N=len(self.label_levels))
            self.label_cvalues = range(len(self.label_levels))
            self.label_mappable = cm.ScalarMappable(cmap = cmap,
                                                 norm = colors.NoNorm())

        #self.cl = []   # Initialized in ContourSet.__init__
        #self.cl_cvalues = [] # same
        self.cl_xy = []

        self.labels(inline)

        for label in self.cl:
            self.ax.add_artist(label)

        self.label_list =  cbook.silent_list('text.Text', self.cl)
        return self.label_list