def recalculate(self): if self.func is not None and self.data_range is not None: newarray = self.func(self.data_range.x_range.low, self.data_range.x_range.high, self.data_range.y_range.low, self.data_range.y_range.high) ImageData.set_data(self, newarray) else: self._data = array([], dtype=float)
def recalculate(self): if self.func is not None and self.data_range is not None: newarray = self.func( self.data_range.x_range.low, self.data_range.x_range.high, self.data_range.y_range.low, self.data_range.y_range.high, ) ImageData.set_data(self, newarray) else: self._data = array([], dtype=float)
def set_pict(self): pd = self.plot_data if not pd: return try: imgd = ImageData.fromfile(self.pict)._data[::-1] except: imgd = ImageData() imgd.set_data(255*ones((2,2,3),dtype='uint8')) imgd = imgd._data pd.set_data("image",imgd)
def set_pict(self): pd = self.plot_data if not pd: return try: imgd = ImageData.fromfile(self.pict)._data[::-1] except: imgd = ImageData() imgd.set_data(255 * ones((2, 2, 3), dtype='uint8')) imgd = imgd._data pd.set_data("image", imgd)
def _corr_plot_default(self): diag = self.covar.diagonal() corr = self.covar / np.sqrt(np.outer(diag, diag)) N = len(diag) value_range = DataRange1D(low=-1, high=1) color_mapper = cmap(range=value_range) index = GridDataSource() value = ImageData() mapper = GridMapper(range=DataRange2D(index), y_low_pos=1.0, y_high_pos=0.0) index.set_data(xdata=np.arange(-0.5, N), ydata=np.arange(-0.5, N)) value.set_data(np.flipud(corr)) self.corr_data = value cmap_plot = CMapImagePlot(index=index, index_mapper=mapper, value=value, value_mapper=color_mapper, padding=(40, 40, 100, 40)) yaxis = PlotAxis( cmap_plot, orientation='left', tick_interval=1, tick_label_formatter=lambda x: self.header[int(N - 1 - x)], tick_generator=ShowAllTickGenerator(positions=np.arange(N))) xaxis = PlotAxis( cmap_plot, orientation='top', tick_interval=1, tick_label_formatter=lambda x: self.header[int(x)], tick_label_alignment='edge', tick_generator=ShowAllTickGenerator(positions=np.arange(N))) cmap_plot.overlays.append(yaxis) cmap_plot.overlays.append(xaxis) colorbar = ColorBar( index_mapper=LinearMapper(range=cmap_plot.value_range), plot=cmap_plot, orientation='v', resizable='v', width=10, padding=(40, 5, 100, 40)) container = HPlotContainer(bgcolor='transparent') container.add(cmap_plot) container.add(colorbar) return container
class ImageDataTestCase(UnittestTools, unittest.TestCase): def setUp(self): self.myarray = arange(15).reshape(5, 3, 1) self.data_source = ImageData(data=self.myarray) def test_init_defaults(self): data_source = ImageData() assert_array_equal(data_source.data, []) # this isn't right - #self.assertEqual(data_source.value_dimension, "scalar") #self.assertEqual(data_source.image_dimension, "image") def test_basic_setup(self): assert_array_equal(self.myarray, self.data_source.data) #self.assertEqual(self.data_source.value_dimension, "scalar") self.assertFalse(self.data_source.is_masked()) def test_set_data(self): new_array = arange(0, 30, 2).reshape(5, 3, 1) with self.assertTraitChanges(self.data_source, 'data_changed', count=1): self.data_source.set_data(new_array) assert_array_equal(new_array, self.data_source.data) self.assertEqual(self.data_source.get_bounds(), (0, 28)) def test_get_data(self): assert_array_equal(self.myarray, self.data_source.get_data()) def test_get_data_no_data(self): data_source = ImageData() self.assertIsNone(data_source.get_data()) def test_get_data_transposed(self): myarray = arange(15).reshape(5, 3, 1) data_source = ImageData(data=myarray, transposed=True) assert_array_equal(swapaxes(myarray, 0, 1), data_source.get_data()) def test_get_data_mask(self): # XXX this is probably not the right thing with self.assertRaises(NotImplementedError): data, mask = self.data_source.get_data_mask() def test_get_data_mask_no_data(self): data_source = ImageData() # XXX this is probably not the right thing with self.assertRaises(NotImplementedError): data, mask = data_source.get_data_mask() def test_bounds(self): bounds = self.data_source.get_bounds() self.assertEqual(bounds, (0, 14)) @unittest.skip('test_bounds_empty() fails in this case') def test_bounds_empty(self): data_source = ImageData() bounds = data_source.get_bounds() self.assertEqual(bounds, (0, 0)) def test_data_size(self): self.assertEqual(15, self.data_source.get_size()) def test_data_size_no_data(self): data_source = ImageData() self.assertEqual(0, data_source.get_size()) def test_get_width(self): self.assertEqual(3, self.data_source.get_width()) def test_get_width_transposed(self): myarray = arange(15).reshape(5, 3) data_source = ImageData(data=myarray, transposed=True) self.assertEqual(5, data_source.get_width()) def test_get_height(self): self.assertEqual(5, self.data_source.get_height()) def test_get_height_transposed(self): myarray = arange(15).reshape(5, 3, 1) data_source = ImageData(data=myarray, transposed=True) self.assertEqual(3, data_source.get_height()) def test_array_bounds(self): self.assertEqual(((0, 3), (0, 5)), self.data_source.get_array_bounds()) def test_array_bounds_transposed(self): myarray = arange(15).reshape(5, 3, 1) data_source = ImageData(data=myarray, transposed=True) self.assertEqual(((0, 5), (0, 3)), data_source.get_array_bounds()) def test_fromfile_png_rgb(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basn2c08.png') data_source = ImageData.fromfile(path) self.assertEqual(data_source.value_depth, 3) def test_fromfile_png_rgba(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basi6a08.png') data_source = ImageData.fromfile(path) self.assertEqual(data_source.value_depth, 4) def test_metadata(self): self.assertEqual(self.data_source.metadata, { 'annotations': [], 'selections': [] }) def test_metadata_changed(self): with self.assertTraitChanges(self.data_source, 'metadata_changed', count=1): self.data_source.metadata = {'new_metadata': True} def test_metadata_items_changed(self): with self.assertTraitChanges(self.data_source, 'metadata_changed', count=1): self.data_source.metadata['new_metadata'] = True
def _myTIC_default( self ): # create an interesting scalar field for the image plot # Eggholder function limitF = 500.0 xA = linspace(-limitF, limitF, 600) yA = linspace(-limitF, limitF, 600) ( xMG,yMG ) = meshgrid( xA,yA ) zMG = -(yMG + 47) * sin( sqrt(abs(yMG + xMG/2 + 47 ))) zMG = zMG - xMG * sin( sqrt(abs(xMG - (yMG + 47)))) # Create an ArrayPlotData object and give it this data myAPD = ArrayPlotData() myAPD.set_data( "Z", zMG ) myAPD.set_data( "X",xA ) myAPD.set_data( "Y",yA ) # Create the plot myTP = Plot( myAPD ) # contains a dict of default colormaps and their functions. We have to # pass the colormapper the data range of interest to set up the private # attributes default_colormaps.color_map_name_dict # the colormap method needs the range of the image data that we want to # plot. We first put the image data (zMG) into an ImageData object. We # then use DataRange1D on the ImageData instance to produce a DataRange1D # instance describing the ImageData data. Finally, we feed the DataRange1D # instance into the colormapper to produce a working colormapper. myID = ImageData( ) myID.set_data( zMG ) myDR1D = DataRange1D( myID ) # pick a colormap myColorMapperFn = default_colormaps.color_map_name_dict['copper'] # choose one or more modifications to the colormap function #myColorMapperFn = default_colormaps.reverse( myColorMapperFn ) #myColorMapperFn = default_colormaps.center( myColorMapperFn,500 ) #myColorMapperFn = default_colormaps.fix( myColorMapperFn,(-500,500) ) # finally, build the colormapper function myColorMapper = myColorMapperFn( myDR1D ) # add the image plot to this plot object # specify the colormap explicitly myTP.img_plot( "Z", xbounds = (xA[0],xA[-1]), ybounds = (yA[0],yA[-1]), colormap = myColorMapper, ) # add the title and padding around the plot myTP.title = "Eggholder Function" myTP.padding = 50 # grids, fonts, etc myTP.x_axis.title = "X" myTP.y_axis.title = "Y" # generate a ColorBar. pulls its colormapper from the myTP Plot object myTCB = ColorBar( plot = myTP, index_mapper = LinearMapper( range = myTP.color_mapper.range ), orientation = 'v', resizable = 'v', width = 40, padding = 30, ) # set the padding of the ColorBar to match the padding of the plot myTCB.padding_top = myTP.padding_top myTCB.padding_bottom = myTP.padding_bottom # range of the colormapper. Changes the min/max values that are mapped # to the ends of the color range. Try +/-2000 for poor contrast and +/-200 for # saturated. Asymmetrical values work as well. #myTP.color_mapper.range.low_setting = 0 #myTP.color_mapper.range.high_setting = 1000 # build up a single container for the colorbar and the image myHPC = HPlotContainer( use_backbuffer = True ) myHPC.add( myTP ) myHPC.add( myTCB ) return( myHPC )
class ImageDataTestCase(UnittestTools, unittest.TestCase): def setUp(self): self.myarray = arange(15).reshape(5, 3, 1) self.data_source = ImageData(data=self.myarray) def test_init_defaults(self): data_source = ImageData() assert_array_equal(data_source.data, []) # this isn't right - #self.assertEqual(data_source.value_dimension, "scalar") #self.assertEqual(data_source.image_dimension, "image") def test_basic_setup(self): assert_array_equal(self.myarray, self.data_source.data) #self.assertEqual(self.data_source.value_dimension, "scalar") self.assertFalse(self.data_source.is_masked()) def test_set_data(self): new_array = arange(0, 30, 2).reshape(5, 3, 1) with self.assertTraitChanges(self.data_source, 'data_changed', count=1): self.data_source.set_data(new_array) assert_array_equal(new_array, self.data_source.data) self.assertEqual(self.data_source.get_bounds(), (0, 28)) def test_get_data(self): assert_array_equal(self.myarray, self.data_source.get_data()) def test_get_data_no_data(self): data_source = ImageData() self.assertIsNone(data_source.get_data()) def test_get_data_transposed(self): myarray = arange(15).reshape(5, 3, 1) data_source = ImageData(data=myarray, transposed=True) assert_array_equal(swapaxes(myarray, 0, 1), data_source.get_data()) def test_get_data_mask(self): # XXX this is probably not the right thing with self.assertRaises(NotImplementedError): data, mask = self.data_source.get_data_mask() def test_get_data_mask_no_data(self): data_source = ImageData() # XXX this is probably not the right thing with self.assertRaises(NotImplementedError): data, mask = data_source.get_data_mask() def test_bounds(self): bounds = self.data_source.get_bounds() self.assertEqual(bounds, (0, 14)) @unittest.skip('test_bounds_empty() fails in this case') def test_bounds_empty(self): data_source = ImageData() bounds = data_source.get_bounds() self.assertEqual(bounds, (0, 0)) def test_data_size(self): self.assertEqual(15, self.data_source.get_size()) def test_data_size_no_data(self): data_source = ImageData() self.assertEqual(0, data_source.get_size()) def test_get_width(self): self.assertEqual(3, self.data_source.get_width()) def test_get_width_transposed(self): myarray = arange(15).reshape(5, 3) data_source = ImageData(data=myarray, transposed=True) self.assertEqual(5, data_source.get_width()) def test_get_height(self): self.assertEqual(5, self.data_source.get_height()) def test_get_height_transposed(self): myarray = arange(15).reshape(5, 3, 1) data_source = ImageData(data=myarray, transposed=True) self.assertEqual(3, data_source.get_height()) def test_array_bounds(self): self.assertEqual(((0, 3), (0, 5)), self.data_source.get_array_bounds()) def test_array_bounds_transposed(self): myarray = arange(15).reshape(5, 3, 1) data_source = ImageData(data=myarray, transposed=True) self.assertEqual(((0, 5), (0, 3)), data_source.get_array_bounds()) def test_fromfile_png_rgb(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basn2c08.png') data_source = ImageData.fromfile(path) self.assertEqual(data_source.value_depth, 3) def test_fromfile_png_rgba(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basi6a08.png') data_source = ImageData.fromfile(path) self.assertEqual(data_source.value_depth, 4) def test_metadata(self): self.assertEqual(self.data_source.metadata, {'annotations': [], 'selections': []}) def test_metadata_changed(self): with self.assertTraitChanges(self.data_source, 'metadata_changed', count=1): self.data_source.metadata = {'new_metadata': True} def test_metadata_items_changed(self): with self.assertTraitChanges(self.data_source, 'metadata_changed', count=1): self.data_source.metadata['new_metadata'] = True
def _myTIC_default( self ): # create an interesting scalar field for the image plot # Eggholder function limitF = 500.0 xA = linspace(-limitF, limitF, 600) yA = linspace(-limitF, limitF, 600) ( xMG,yMG ) = meshgrid( xA,yA ) zMG = -(yMG + 47) * sin( sqrt(abs(yMG + xMG/2 + 47 ))) zMG = zMG - xMG * sin( sqrt(abs(xMG - (yMG + 47)))) # Create an ArrayPlotData object and give it this data myAPD = ArrayPlotData() myAPD.set_data( "Z", zMG ) myAPD.set_data( "X",xA ) myAPD.set_data( "Y",yA ) # Create the plot self.myTP = Plot( myAPD ) # contains a dict of default colormaps and their functions. We have to # pass the colormapper the data range of interest to set up the private # attributes default_colormaps.color_map_name_dict # the colormap method needs the range of the image data that we want to # plot. We first put the image data (zMG) into an ImageData object. We # then use DataRange1D on the ImageData instance to produce a DataRange1D # instance describing the ImageData data. Finally, we feed the DataRange1D # instance into the colormapper to produce a working colormapper. myID = ImageData( ) myID.set_data( zMG ) self.myDR1D = DataRange1D( myID ) # pick an unmodified (i.e. unreversed, no ranges) colormap and build # the colormap functions myColorMapperFn = default_colormaps.color_map_name_dict[self.colormapNameTE] myColorMapper = myColorMapperFn( self.myDR1D ) # add the image plot to this plot object # specify the colormap explicitly self.myTP.img_plot( "Z", xbounds = (xA[0],xA[-1]), ybounds = (yA[0],yA[-1]), colormap = myColorMapper, ) # add the title and padding around the plot self.myTP.title = "Eggholder Function" self.myTP.padding = 50 # grids, fonts, etc self.myTP.x_axis.title = "X" self.myTP.y_axis.title = "Y" # generate a ColorBar. pulls its colormapper from the myTP Plot object self.myTCB = ColorBar( plot = self.myTP, index_mapper = LinearMapper( range = self.myTP.color_mapper.range ), orientation = 'v', resizable = 'v', width = 40, padding = 30, ) # set the padding of the ColorBar to match the padding of the plot self.myTCB.padding_top = self.myTP.padding_top self.myTCB.padding_bottom = self.myTP.padding_bottom # build up a single container for the colorbar and the image myHPC = HPlotContainer( use_backbuffer = True ) myHPC.add( self.myTP ) myHPC.add( self.myTCB ) return( myHPC )