Chaco min/max values when plotting image

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Chaco min/max values when plotting image

Tiago Pereira
Hi all,

Sorry for the barrage of questions. I am plotting an image with chaco in
the following way:


pd = ArrayPlotData()
pd.set_data("imagedata", my_image)
plot = Plot(pd)
plot.img_plot("imagedata",interpolation='nearest',colormap=gist_heat)[0]


How can I set the luminance of the image so that it is not defined by
the max and min of my_image? In matplotlib's imshow this is set by the
vmin/vmax keywords. But looking in chaco's examples and source code I
could not find how to do this.

Also, what is the easiest way to revert a colormap? Matplotlib has the
the _r versions of colormaps, is there a simple recipe for chaco?


Thanks again,

Tiago
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chaco min/max values when plotting image

Pietro Berkes


On Fri, Jul 13, 2012 at 2:50 AM, Tiago Pereira <[hidden email]> wrote:
Hi all,

Sorry for the barrage of questions. I am plotting an image with chaco in
the following way:


pd = ArrayPlotData()
pd.set_data("imagedata", my_image)
plot = Plot(pd)
plot.img_plot("imagedata",interpolation='nearest',colormap=gist_heat)[0]

You can create an appropriate color mapper like this:

color_range = DataRange1D(low=0.2, high=0.8)
color_map = gist_heat(color_range)
plot.img_plot("imagedata",interpolation='nearest',colormap=color_map)[0]



How can I set the luminance of the image so that it is not defined by
the max and min of my_image? In matplotlib's imshow this is set by the
vmin/vmax keywords. But looking in chaco's examples and source code I
could not find how to do this.

Also, what is the easiest way to revert a colormap? Matplotlib has the
the _r versions of colormaps, is there a simple recipe for chaco?


Use chaco.default_colormaps.reverse:

  import chaco
  from chaco.default_colormaps import Spectral, reverse
 
  # this is were you revert the 'Spectral' colormap factory
  reversed_spectral = reverse(Spectral)

  from chaco.api import DataRange1D
  range = DataRange1D(min=1.0, max=30.0)

  color_map = Spectral(data_range)
  reversed_color_map = reversed_spectral(data_range)
  print color_map.color_bands
  print reversed_color_map.color_bands



Thanks again,

Tiago
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chaco min/max values when plotting image

Robert Kern
On Fri, Jul 13, 2012 at 8:52 AM, Pietro Berkes <[hidden email]> wrote:

>
>
> On Fri, Jul 13, 2012 at 2:50 AM, Tiago Pereira <[hidden email]> wrote:
>>
>> Hi all,
>>
>> Sorry for the barrage of questions. I am plotting an image with chaco in
>> the following way:
>>
>>
>> pd = ArrayPlotData()
>> pd.set_data("imagedata", my_image)
>> plot = Plot(pd)
>> plot.img_plot("imagedata",interpolation='nearest',colormap=gist_heat)[0]
>
>
> You can create an appropriate color mapper like this:
>
> color_range = DataRange1D(low=0.2, high=0.8)
> color_map = gist_heat(color_range)
> plot.img_plot("imagedata",interpolation='nearest',colormap=color_map)[0]

  from chaco.default_colormaps import fix, gist_heat

  color_map = fix(gist_heat, (0.2, 0.8)

That also works.

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

How can I catch the list changed even?

Robert Unguran
Hallo,

I have a view with a list, and I would like to do further steps if the
content of the list has changed

class MCLidarSysView( HasTraits ):
     system_list = List( Systems )
     ....
    def _system_list_changed( self ):
         print "here"


but it's not working?

What should id do?

cheers,
Robert
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How can I catch the list changed even?

Fred
Le 13/07/2012 10:15, Robert Unguran a écrit :

> Hallo,
>
> I have a view with a list, and I would like to do further steps if the
> content of the list has changed
>
> class MCLidarSysView( HasTraits ):
>       system_list = List( Systems )
>       ....
>      def _system_list_changed( self ):
>           print "here"
>
>
> but it's not working?
>
> What should id do?
Use _system_list_items_changed(self, event) instead.

--
Fred
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How can I catch the list changed even?

Robert Kern
On Fri, Jul 13, 2012 at 9:21 AM, Fred <[hidden email]> wrote:

> Le 13/07/2012 10:15, Robert Unguran a écrit :
>> Hallo,
>>
>> I have a view with a list, and I would like to do further steps if the
>> content of the list has changed
>>
>> class MCLidarSysView( HasTraits ):
>>       system_list = List( Systems )
>>       ....
>>      def _system_list_changed( self ):
>>           print "here"
>>
>>
>> but it's not working?
>>
>> What should id do?
>
> Use _system_list_items_changed(self, event) instead.

For more information:

http://docs.enthought.com/traits/traits_user_manual/notification.html?highlight=_items

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How can I catch the list changed even?

Robert Unguran
On 07/13/2012 11:02 AM, Robert Kern wrote:

> On Fri, Jul 13, 2012 at 9:21 AM, Fred <[hidden email]> wrote:
>> Le 13/07/2012 10:15, Robert Unguran a écrit :
>>> Hallo,
>>>
>>> I have a view with a list, and I would like to do further steps if the
>>> content of the list has changed
>>>
>>> class MCLidarSysView( HasTraits ):
>>>        system_list = List( Systems )
>>>        ....
>>>       def _system_list_changed( self ):
>>>            print "here"
>>>
>>>
>>> but it's not working?
>>>
>>> What should id do?
>> Use _system_list_items_changed(self, event) instead.
> For more information:
>
> http://docs.enthought.com/traits/traits_user_manual/notification.html?highlight=_items
>
Nice description, Thanks.

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How can I catch the list changed even?

Robert Unguran
Hi,

I have one more question,

how can I add custom image to a table editor? For example I have the following  code and when I would like to display custom image in the my_image column

class SystemInfoColumn ( ObjectColumn ):

    # Override some default settings for the column:   
    horizontal_alignment = 'center'  
  
   
# The 'players' trait table editor:
system_editor = TableEditor(
    sortable = True,
    configurable = False,
    deletable = True,
    sort_model = True,
    auto_size = False,
    #auto_size = False,
    columns = [ CheckboxColumn( name = 'synchronize', width = 0.05 ),
                 SystemColumn( name = 'ip', editable = False ),
                 SystemColumn( name = 'system', editable = False , width = 0.20 ),
                 SystemColumn( name = 'lastsync', editable = False, width = 0.10, label = "Last synchronization time" ),
                 ]   
    )

class SysInfoAdapter( TableEditor ):
    sortable = True,
    configurable = False,
    #auto_size = False,
    columns = [  SystemInfoColumn( name = 'sensor', editable = False, width = 0.30 , horizontal_alignment = 'left' ),
                 SystemInfoColumn( name = 'value', editable = False , width = 0.02 ),
                 SystemInfoColumn( name = 'min_value', editable = False , width = 0.02 ),
                 SystemInfoColumn( name = 'max_value', editable = False , width = 0.02 ),
                 SystemInfoColumn( name = 'status', editable = False, width = 0.02 ),
                SystemInfoColumn( name = 'my_image', editable = False, width = 0.02 ),
               ]   
  
   
class SystemStatusInfo ( HasTraits ):   
    sensor = Str
    multiplier = Float
    command = tuple
    value = Str
    min_value = Float
    max_value = Float  
    status = Int   
    unit = Str
    my_image = ????

class MCLidarSysView( HasTraits ):    
    system_status_list = List( SystemStatusInfo )
    ....



self.system_status_list.append( SystemStatusInfo( sensor = row[0], \
                                                            command = command, multiplier = float( row[2] ), \
                                                            unit = row[3], min_value = float( row[4] ), \
                                                            max_value = float( row[5] ), value = '0' , my_image=????) )


Thanks,
Robert.



On 07/13/2012 11:26 AM, Robert Unguran wrote:
On 07/13/2012 11:02 AM, Robert Kern wrote:
On Fri, Jul 13, 2012 at 9:21 AM, Fred [hidden email] wrote:
Le 13/07/2012 10:15, Robert Unguran a écrit :
Hallo,

I have a view with a list, and I would like to do further steps if the
content of the list has changed

class MCLidarSysView( HasTraits ):
       system_list = List( Systems )
       ....
      def _system_list_changed( self ):
           print "here"


but it's not working?

What should id do?
Use _system_list_items_changed(self, event) instead.
For more information:

http://docs.enthought.com/traits/traits_user_manual/notification.html?highlight=_items

Nice description, Thanks.




_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chaco min/max values when plotting image

Tiago Pereira
In reply to this post by Pietro Berkes
Thank you Pietro (and Robert) for your quick replies.

On 13/7/12 12:52 AM, Pietro Berkes wrote:
> You can create an appropriate color mapper like this:
>
> color_range = DataRange1D(low=0.2, high=0.8)
> color_map = gist_heat(color_range)
> plot.img_plot("imagedata",interpolation='nearest',colormap=color_map)[0]

You suggestion for the luminance scaling works. Ultimately what I want
is to be able to change 'low' and 'high' dynamically, but your
suggestion seems to involve recalculating the colormap every 'low' and
'high' change. Isn't this going to be slow?

With some experimentation I found that this works:

img_plot = plot.img_plot("imagedata", colormap=gist_heat)[0]

img_plot.value_range.low = self.vmin
img_plot.value_range.high = self.vmax

And here self.vmin/vmax are traits, which when changed will update
img_plot.value.range:

def _vmin_changed(self):
    self.img_plot.value_range.low = self.vmin

This seems to be quite fast in updating the plot. But I'm not sure if
this is a good approach. Ideally, I would love to get
value_range.low/high directly exposed in img_plot, so I could do
something like:

img_plot = plot.img_plot("imagedata", colormap=gist_heat,
                         value_range_low = my_trait)[0]

And avoid having to set _vmin_changed/_vmax_changed. Is there a way to
do this? (Getting img_plot.value_range.high < img_plot.value_range.low
also has the advantage of reversing the colormap!)

Thanks,

Tiago

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chaco min/max values when plotting image

Robert Kern
On Fri, Jul 13, 2012 at 3:54 PM, Tiago Pereira <[hidden email]> wrote:

> Thank you Pietro (and Robert) for your quick replies.
>
> On 13/7/12 12:52 AM, Pietro Berkes wrote:
>> You can create an appropriate color mapper like this:
>>
>> color_range = DataRange1D(low=0.2, high=0.8)
>> color_map = gist_heat(color_range)
>> plot.img_plot("imagedata",interpolation='nearest',colormap=color_map)[0]
>
> You suggestion for the luminance scaling works. Ultimately what I want
> is to be able to change 'low' and 'high' dynamically, but your
> suggestion seems to involve recalculating the colormap every 'low' and
> 'high' change. Isn't this going to be slow?

The colormap does not get recalculated. The cached color array does
not change when the data range does. The image data will get be
renormalized and passed through the colormap though.

> With some experimentation I found that this works:
>
> img_plot = plot.img_plot("imagedata", colormap=gist_heat)[0]
>
> img_plot.value_range.low = self.vmin
> img_plot.value_range.high = self.vmax
>
> And here self.vmin/vmax are traits, which when changed will update
> img_plot.value.range:
>
> def _vmin_changed(self):
>     self.img_plot.value_range.low = self.vmin
>
> This seems to be quite fast in updating the plot. But I'm not sure if
> this is a good approach. Ideally, I would love to get
> value_range.low/high directly exposed in img_plot, so I could do
> something like:
>
> img_plot = plot.img_plot("imagedata", colormap=gist_heat,
>                          value_range_low = my_trait)[0]
>
> And avoid having to set _vmin_changed/_vmax_changed. Is there a way to
> do this? (Getting img_plot.value_range.high < img_plot.value_range.low
> also has the advantage of reversing the colormap!)

Look at the .sync_trait() method:

[~]
|9> from traits.api import *

[~]
|10> from chaco.api import DataRange1D

[~]
|11> class A(HasTraits):
...>     vmin = Float(0.0)
...>     vmax = Float(1.0)
...>

[~]
|12> d = DataRange1D()

[~]
|13> HasTraits.sync_trait?
Type:       instancemethod
String Form:<unbound method HasTraits.sync_trait>
File:       /Users/rkern/git/et/traits/traits/has_traits.py
Definition: HasTraits.sync_trait(self, trait_name, object, alias=None,
mutual=True, remove=False)
Docstring:
Synchronizes the value of a trait attribute on this object with a
trait attribute on another object.

Parameters
----------
name : string
    Name of the trait attribute on this object
object : object
    The object with which to synchronize
alias : string
    Name of the trait attribute on *other*; if None or omitted, same
    as *name*.
mutual : Boolean or integer
    Indicates whether synchronization is mutual (True or non-zero)
    or one-way (False or zero)
remove : Boolean or integer
    Indicates whether sychronization is being added (False or zero)
    or removed (True or non-zero)

Description
-----------
In mutual synchronization, any change to the value of the specified
trait attribute of either object results in the same value being
assigned to the corresponding trait attribute of the other object.
In one-way synchronization, any change to the value of the attribute
on this object causes the corresponding trait attribute of *object* to
be updated, but not vice versa.

[~]
|23> d = DataRange1D()

[~]
|24> a = A()

[~]
|25> a.sync_trait('vmin', d, 'low_setting', mutual=False)

[~]
|26> d.low_setting
0.0

[~]
|27> a.sync_trait('vmax', d, 'high_setting', mutual=False)

[~]
|28> d.high_setting
1.0

[~]
|29> a.vmin = 0.5

[~]
|30> d.low_setting
0.5

[~]
|31> d.low
0.5

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chaco min/max values when plotting image

Tiago Pereira
On 13/07/12 8:05 AM, Robert Kern wrote:
> Look at the .sync_trait() method:

Thanks! That was what I was looking for. In my previous example, I
actually did it the other way around (sync'd the img_plot.value_range
with local attributes:

vmin = Range()
vmax = Range()

img_plot = plot.img_plot("imagedata", colormap=gist_heat)[0]

img_plot.value_range.sync_trait('low', self, 'vmin', mutual=True)
img_plot.value_range.sync_trait('high', self, 'vmax', mutual=True)

(and then request a redraw when vmin/vmax change.)

And this seems to be reasonably fast.

Cheers,

Tiago
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chaco min/max values when plotting image

Adam Hughes
Sync_traits is a great method especially when mutualness is not desired (eg mutual=False).

I use it a lot in situations when you have master and secondary parameters.  For example, you have global settings like "x-units" that the user can change on a whim, but you also have sections of the program that can be changed by the user without initiating a global change.  It's good for exploratory analysis when you want the user to be able to change parameters on the fly, but occasionally, apply changes globally.


On Fri, Jul 13, 2012 at 2:49 PM, Tiago Pereira <[hidden email]> wrote:
On 13/07/12 8:05 AM, Robert Kern wrote:
> Look at the .sync_trait() method:

Thanks! That was what I was looking for. In my previous example, I
actually did it the other way around (sync'd the img_plot.value_range
with local attributes:

vmin = Range()
vmax = Range()

img_plot = plot.img_plot("imagedata", colormap=gist_heat)[0]

img_plot.value_range.sync_trait('low', self, 'vmin', mutual=True)
img_plot.value_range.sync_trait('high', self, 'vmax', mutual=True)

(and then request a redraw when vmin/vmax change.)

And this seems to be reasonably fast.

Cheers,

Tiago
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev