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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#-----------------------------------------------------------------------------
# Name: wxFrame2.py
# Purpose: Data output frame (wx.grid.Grid)
#
# Author: <Flavio Codeco Coelho>
#
# Created: 2003/02/04
# RCS-ID: $Id: wxFrame2.py,v 1.3 2004/01/13 10:51:44 fccoelho Exp $
# Copyright: (c) 2003-6 Flavio Codeco Coelho <fccoelho@fiocruz.br>
# Licence: This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#-----------------------------------------------------------------------------
#Boa:Frame:wxFrame2
import wxversion
wxversion.select('2.8')
import wx
import wx.grid
from numpy import *
import icones,cwt
import PlotFigure as PF
from pylab import show
def create(parent):
return wxFrame2(parent)
[wxID_WXFRAME2, wxID_WXFRAME2GRID1, wxID_WXFRAME2PANEL1,
wxID_WXFRAME2STATUSBAR1, wxID_WXFRAME2TOOLBAR1,
] = [wx.NewId() for _init_ctrls in range(5)]
[wxID_WXFRAME2TOOLBAR1CWT, wxID_WXFRAME2TOOLBAR1PLOT,
wxID_WXFRAME2TOOLBAR1PSD, wxID_WXFRAME2TOOLBAR1TOOLS0,
] = [wx.NewId() for _init_coll_toolBar1_Tools in range(4)]
class wxFrame2(wx.Frame):
def _init_coll_panelSizer_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.panel1, 1, border=0, flag=wx.EXPAND)
def _init_coll_sheetBSizer_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.grid1, 1, border=0, flag=wx.EXPAND)
def _init_coll_toolBar1_Tools(self, parent):
# generated method, don't edit
parent.AddTool(bitmap=icones.getSaveBitmap(),
id=wxID_WXFRAME2TOOLBAR1TOOLS0, isToggle=False,
longHelpString='Save this table', pushedBitmap=wx.NullBitmap,
shortHelpString='Save As')
parent.DoAddTool(bitmap=icones.getplotBitmap(),
bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME2TOOLBAR1PLOT,
kind=wx.ITEM_NORMAL, label='plotSel',
longHelp='Plot selected column', shortHelp='Plot')
parent.DoAddTool(bitmap=icones.getSpectrumBitmap(),
bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME2TOOLBAR1PSD,
kind=wx.ITEM_NORMAL, label='Spectrogram',
longHelp='Spectrogram display', shortHelp='Spectrogram')
parent.DoAddTool(bitmap=icones.getCWTBitmap(),
bmpDisabled=wx.NullBitmap, id=wxID_WXFRAME2TOOLBAR1CWT,
kind=wx.ITEM_NORMAL, label='Continuous Wavelet Transform',
longHelp='Continuous Wavelet Transform',
shortHelp='Wavelet Transform')
self.Bind(wx.EVT_TOOL, self.OnToolbar1tools0Tool,
id=wxID_WXFRAME2TOOLBAR1TOOLS0)
self.Bind(wx.EVT_TOOL, self.OnToolBar1Tools1Tool,
id=wxID_WXFRAME2TOOLBAR1PLOT)
self.Bind(wx.EVT_TOOL, self.OnToolBar1PsdTool,
id=wxID_WXFRAME2TOOLBAR1PSD)
self.Bind(wx.EVT_TOOL, self.OnToolBar1CwtTool,
id=wxID_WXFRAME2TOOLBAR1CWT)
parent.Realize()
def _init_coll_statusBar1_Fields(self, parent):
# generated method, don't edit
parent.SetFieldsCount(1)
parent.SetStatusText(number=0, text='Status')
parent.SetStatusWidths([-1])
def _init_sizers(self):
# generated method, don't edit
self.panelSizer = wx.BoxSizer(orient=wx.VERTICAL)
self.sheetBSizer = wx.BoxSizer(orient=wx.VERTICAL)
self._init_coll_panelSizer_Items(self.panelSizer)
self._init_coll_sheetBSizer_Items(self.sheetBSizer)
self.grid1.SetSizer(self.sheetBSizer)
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_WXFRAME2, name='', parent=prnt,
pos=wx.Point(778, 556), size=wx.Size(686, 491),
style=wx.DEFAULT_FRAME_STYLE, title='Output Table')
self.SetClientSize(wx.Size(686, 491))
self.SetAutoLayout(True)
self.SetToolTipString('')
self.statusBar1 = wx.StatusBar(id=wxID_WXFRAME2STATUSBAR1,
name='statusBar1', parent=self, style=0)
self._init_coll_statusBar1_Fields(self.statusBar1)
self.SetStatusBar(self.statusBar1)
self.toolBar1 = wx.ToolBar(id=wxID_WXFRAME2TOOLBAR1, name='toolBar1',
parent=self, pos=wx.Point(0, 0), size=wx.Size(204, 48),
style=wx.TB_HORIZONTAL | wx.NO_BORDER)
self.toolBar1.SetToolTipString('')
self.SetToolBar(self.toolBar1)
self.panel1 = wx.Panel(id=wxID_WXFRAME2PANEL1, name='panel1',
parent=self, pos=wx.Point(1, 1), size=wx.Size(684, 464),
style=wx.TAB_TRAVERSAL)
self.grid1 = wx.grid.Grid(id=wxID_WXFRAME2GRID1, name='grid1',
parent=self.panel1, pos=wx.Point(0, 0),size=wx.Size(684, 464),
style=0)
self.grid1.SetAutoLayout(True)
self.grid1.SetMinSize(wx.Size(684, 464))
self.grid1.SetToolTipString(u'Data grid')
self.grid1.SetThemeEnabled(True)
self._init_coll_toolBar1_Tools(self.toolBar1)
self._init_sizers()
def __init__(self, parent):
self._init_ctrls(parent)
self.filename = None
def OnToolbar1tools0Tool(self, event):
dlg = wx.FileDialog(self, "Save Data As", ".", "", "*.dat", wx.SAVE)
try:
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
f = open(filename,'w')
for i in xrange(self.table.GetNumberRows()):
if i == 0:
for k in xrange(self.grid1.GetNumberCols()):
f.write(self.table.GetColLabelValue(k)+',')
f.write('\n')
for j in xrange(self.table.GetNumberCols()):
f.write(self.grid1.GetCellValue(i,j)+',')
f.write('\n')
f.close()
finally:
dlg.Destroy()
def fillGrid(self,data):
"""
this function will fill the spreadsheet
"""
self.table = TableBase(self.grid1,data)
self.grid1.SetTable(self.table,True)
def OnToolBar1Tools1Tool(self, event):
"""
Plot Selected columns
"""
sel = self.grid1.GetSelectedCols()
data = self.createDataMatrix()
leg = tuple([self.table.GetColLabelValue(i) for i in self.grid1.GetSelectedCols()])
y = []
for i in sel:
y.append(data[:,i])
p=PF.create(None)
p.SetTitle('Selected Variables')
p.plot(data[:,0],y,leg)
p.Show()
def createDataMatrix(self):
"""
create a Numeric array with the contents of the spreadsheet
"""
rows = self.grid1.GetNumberRows()
cols = self.grid1.GetNumberCols()
data = zeros((rows-2,cols),float)
for i in xrange(rows-2):
for j in xrange(cols):
data[i,j] = float(self.grid1.GetCellValue(i+1,j))
print data, data.shape, data[0,0]
return data
def OnToolBar1PsdTool(self, event):
"""
Plots the spectrogram of a vector
"""
sel = self.grid1.GetSelectedCols()
if len(sel)!=1:
dlg = wx.MessageDialog(self, 'You have selected %s columns.\nPlease select only one.'%len(sel),
'Selection Error', wx.OK | wx.ICON_INFORMATION)
try:
dlg.ShowModal()
finally:
dlg.Destroy()
return
coln = self.grid1.GetSelectedCols()[0]#number of the selected column
#print coln, type(coln)
coll = self.grid1.GetNumberRows()#height of sel. column.
name = self.table.GetColLabelValue(coln)
y = zeros(coll,float)
for i in xrange(coll):
try:
y[i] = float(self.grid1.GetCellValue(i,coln))
except: print i
p=PF.create(None)
p.SetTitle('Spectrogram')
p.plotSpecg(y,name)
p.Show()
q=PF.create(None)
q.SetTitle('Power Spectrum')
q.plotSpec(y,name)
q.Show()
def OnToolBar1CwtTool(self, event):
"""
Plots the continuous wavelet transform of a given series.
"""
origin = 'image'
interpolation = 'bilinear'
sel = self.grid1.GetSelectedCols()
if len(sel)!=1:
dlg = wx.MessageDialog(self, 'You have selected %s columns.\nPlease select only one.'%len(sel),
'Selection Error', wx.OK | wx.ICON_INFORMATION)
try:
dlg.ShowModal()
finally:
dlg.Destroy()
return
coln = self.grid1.GetSelectedCols()[0]#number of the selected column
#print coln, type(coln)
coll = self.grid1.GetNumberRows()#height of sel. column.
name = self.table.GetColLabelValue(coln)
y = zeros(coll,float)
for i in xrange(coll):
try:
y[i] = float(self.grid1.GetCellValue(i,coln))
except: print i
c = cwt.cwt(y,nvoice=8,wavelet="Morlet",oct=2,scale=4)
cwt.imageCWT(c,title='%s Continuous Wavelet Transform\nMorlet wavelet'% name,origin=origin,interpolation=interpolation)
show()
class TableBase(wx.grid.PyGridTableBase):
"""
This class will store a numpy array to fill
the grid
"""
def __init__(self, parentgrid, data):
wx.grid.PyGridTableBase.__init__(self)
self.data = data
self.grid = parentgrid
self.collabels = range(data.shape[1]+6)
def GetNumberRows(self):
return self.data.shape[0]
def GetNumberCols(self):
return self.data.shape[1]
def GetValue(self, row, col):
return self.data[row][col]
def GetColLabelValue(self, col):
return self.collabels[col]
def SetColLabelValue(self, col, label):
self.collabels[col]= label
if __name__ == '__main__':
app = wx.PySimpleApp()
wx.InitAllImageHandlers()
frame = create(None)
frame.Show()
app.MainLoop()
|