-
Notifications
You must be signed in to change notification settings - Fork 1
/
NumpyFileViewer.py
172 lines (160 loc) · 7.62 KB
/
NumpyFileViewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
"""
Draws a colormapped image plot
- Left-drag pans the plot.
- Mousewheel up and down zooms the plot in and out.
- Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow
and alt-right-arrow moves you forwards and backwards through the "zoom
history".
"""
# Major library imports
from numpy import exp, linspace, meshgrid, pi, sin, load, save
from enthought.enable.example_support import DemoFrame, demo_main
# Enthought library imports
from enthought.enable.api import Component, ComponentEditor, Window
from enthought.traits.api import HasTraits, Instance, File, Str
from enthought.traits.ui.api import Item, Group, View
# Chaco imports
from enthought.chaco.api import ArrayPlotData, jet, Plot, ColorBar, HPlotContainer, LinearMapper, PlotGraphicsContext
from enthought.chaco.tools.api import PanTool, ZoomTool
from enthought.chaco.tools.api import TraitsTool
from enthought.chaco.tools import toolbars
from enthought.chaco.tools.api import SaveTool, RangeSelection, RangeSelectionOverlay
from enthought.traits.ui.menu import Action, CloseAction, Menu, \
MenuBar, NoButtons, Separator
from enthought.chaco.scales.formatters import BasicFormatter
from enthought.chaco.tools.image_inspector_tool import ImageInspectorTool, ImageInspectorOverlay
import easygui
#===============================================================================
# # Create the Chaco plot.
#===============================================================================
def _create_plot_component(file_name):
# Create a scalar field to colormap
z = load(file_name)
pd = ArrayPlotData()
pd.set_data("imagedata", z)
# Create the plot
plot = Plot(pd)
plot.bgcolor = 'gray'
img_plot = plot.img_plot("imagedata",
name='my_plot',
colormap=jet,
hide_grids=True)[0]
#cont_plot=plot.contour_plot('imagedata', type='line', name='countour')
# Tweak some of the plot properties
#plot.title = file_name
plot.padding = 40
plot.padding_right=20
# Attach some tools to the plot
plot.tools.append(PanTool(plot))
#plot.tools.append(TraitsTool(plot))
zoom = ZoomTool(component=img_plot, tool_mode="box", always_on=False)
img_plot.overlays.append(zoom)
#plot.y_axis.tick_label_formatter = lambda x: "%.3e" % x
#plot.x_axis.tick_label_formatter = lambda x: "%.3e" % x
#plot.tools.append(SaveTool(plot))
# Right now, some of the tools are a little invasive, and we need the
# actual CMapImage object to give to them
my_plot = img_plot#plot.plots["my_plot"][0]
colormap = my_plot.color_mapper
colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
color_mapper=colormap,
plot=my_plot,
orientation='v',
resizable='v',
width=25,
padding=0)
colorbar.origin = 'bottom left'
#colorbar._axis.tick_label_formatter = lambda x: '%.0e'%(x*10e6) + u' [\u00b5' + 'Watt]'
colorbar._axis.tick_label_formatter = lambda x: ('%.0e'%(x*1e6))
colorbar._axis.orientation = 'right'
colorbar._axis.title = u'Intensity [\u00b5W]'
colorbar.padding_top = plot.padding_top
colorbar.padding_bottom = plot.padding_bottom
colorbar.padding_right = 100
# create a range selection for the colorbar
range_selection = RangeSelection(component=colorbar)
colorbar.tools.append(range_selection)
colorbar.overlays.append(RangeSelectionOverlay(component=colorbar,
border_color="white",
alpha=0.5,
fill_color="lightgray"))
range_selection.listeners.append(my_plot)
imgtool = ImageInspectorTool(img_plot)
img_plot.tools.append(imgtool)
plot.overlays.append(ImageInspectorOverlay(component=img_plot, image_inspector=imgtool))
# we also want to the range selection to inform the cmap plot of
# the selection, so set that up as well
# Create a container to position the plot and the colorbar side-by-side
container = HPlotContainer(use_backbuffer = True)
container.add(plot)
container.add(colorbar)
container.bgcolor = "white"
container.tools.append(SaveTool(container))
container.tools.append(TraitsTool(container))
#my_plot.set_value_selection((-1.3, 6.9))
return container
#===============================================================================
# Attributes to use for the plot view.
size=(800,600)
title="Basic Colormapped Image Plot"
#===============================================================================
# # Demo class that is used by the demo.py application.
#===============================================================================
class NumpyFileViewer(HasTraits):
plot = Instance(Component)
file_name=Str('measure_3.npy')
_save_file = File('default.npy', filter=['Numpy files (*.npy)| *.npy'])
_load_file = File('.npy', filter=['Numpy files (*.npy) | *.npy', 'All files (*.*) | *.*'])
view = View(
Group(
Item('plot', editor=ComponentEditor(size=size), show_label=False),orientation = "vertical"),
menubar=MenuBar(Menu(Separator(),
Action(name="Load Numpy-data", action="load_file"), # action= ... calls the function, given in the string
Action(name="Save Plot", action="save_plot"),
Separator(),
CloseAction,
name="File")),
resizable=True, title=title
)
def _plot_default(self):
try:
return _create_plot_component(self.file_name)
except:
print 'file not found'
#=======================================================================
# try:
# return _create_plot_component(self.file_name)
# except:
# print 'file not found'
#=======================================================================
def load_file(self):
"""
Callback for the 'Load Image' menu option.
"""
tmp = easygui.fileopenbox(title = "Choose your file",default="*.npy")
print self.file_name
if tmp:
try:
self.file_name = tmp
self.plot=self._plot_default()
except:
print 'Loading file failed'
def save_plot(self):
from easygui import filesavebox
file_string=str(self.file_name.rstrip('.npy')+'.png')
tmp = filesavebox(title = "Save to", default=file_string)
if tmp:
try:
self._save_file = tmp + '.png'
win_size = self.plot.outer_bounds
plot_gc = PlotGraphicsContext(win_size)
plot_gc.render_component(self.plot)
plot_gc.save(self._save_file)
except:
print 'Saving failed'
# Create a graphics context of the right size
if __name__ == '__main__':
demo = NumpyFileViewer()
demo.configure_traits(view='view')