Hello,
I'm currently working on a little soft to plot some data from CSV files. I've got two problems.
The first one is on CSV opening in procFile method. When I try to open a file with a path which own "é" characters for example, it raises an error. I've spent quite a lot of time trying to find a solution but none of the one tested works. More over, in some simples tests with IDLE it did work well like :
import csv
filename = 'C:\Users\Sébastien\Documents\Python\FourChannels\igbt_0_1.csv'
wav = csv.reader(open(filename, 'rb'))
In my soft the error is "UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 10: ordinal not in range(128)"
The second problem occurs when I trying to store an array return from procFile(). I would like to store my data in "self.data" created at the beginning of setupUi() but it raise an exception
"self.data[0] = self.procFile(path)
AttributeError: data"
I simulated a simple Class in IDLE which do the same and it did work well.
class Myclass(object):
def func1(self):
print 'func1'
def func2(self):
self.data = [[0],[1],[2],[3],[4],[5]]
print 'func2'
def func3(self):
return [1,2,3,4]
def func4(self):
self.data.append(self.func3())
def func5(self):
self.data[2] = self.func3()
obj = Myclass()
obj.func2()
obj.func5()
obj.data
[[0], [1], [1, 2, 3, 4], [3], [4], [5]]
There is two file needed to run this script, FourChannels.py:
#!/usr/bin/env python
# -*- coding: latin-1 -*-
from matplotlibwidget import MatplotlibWidget
from PyQt4 import QtCore, QtGui
import ui_FourChannels
import sys
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
ui = ui_FourChannels.Ui_FourChannels()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
and ui_FourChannels.py :
#!/usr/bin/env python
# -*- coding: latin-1 -*-
# Created: Mon Jul 06 09:52:02 2009
# by: PyQt4 UI code generator 4.4.3
from PyQt4 import QtCore, QtGui
from matplotlibwidget import MatplotlibWidget
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from pylab import arange,sin
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import pylab
from numpy import *
import csv
import os
from scipy import signal
class Ui_FourChannels(QMainWindow):
def setupUi(self, FourChannels):
self.data = [[0],[1],[2],[3],[4],[5]]
FourChannels.setObjectName("FourChannels")
FourChannels.resize(1414, 822)
self.centralwidget = QtGui.QWidget(FourChannels)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(2, 1, 1400, 771))
self.widget.setObjectName("widget")
self.gridLayout = QtGui.QGridLayout(self.widget)
self.gridLayout.setObjectName("gridLayout")
self.mplCh1 = MatplotlibWidget(self.widget)
self.mplCh1.setObjectName("mplCh1")
self.gridLayout.addWidget(self.mplCh1, 0, 0, 1, 4)
spacerItem = QtGui.QSpacerItem(37, 378, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 0, 4, 3, 1)
self.mplCh2 = MatplotlibWidget(self.widget)
self.mplCh2.setObjectName("mplCh2")
self.gridLayout.addWidget(self.mplCh2, 0, 5, 1, 3)
spacerItem1 = QtGui.QSpacerItem(37, 378, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 0, 8, 3, 1)
self.mplPower = MatplotlibWidget(self.widget)
self.mplPower.setObjectName("mplPower")
self.gridLayout.addWidget(self.mplPower, 0, 9, 1, 4)
self.ch1Button = QtGui.QPushButton("&Ch1")
self.connect(self.ch1Button, SIGNAL('clicked()'), self.pushedCh1)
self.ch1Button.setObjectName("ch1Button")
self.gridLayout.addWidget(self.ch1Button, 1, 0, 1, 2)
self.ch1lineEdit = QtGui.QLineEdit(self.widget)
self.ch1lineEdit.setObjectName("ch1lineEdit")
self.gridLayout.addWidget(self.ch1lineEdit, 1, 2, 1, 1)
self.drawCh1Button = QtGui.QPushButton(self.widget)
self.drawCh1Button.setObjectName("drawCh1Button")
self.connect(self.drawCh1Button, SIGNAL('clicked()'), self.pushedDrawCh1)
self.gridLayout.addWidget(self.drawCh1Button, 1, 3, 1, 1)
self.ch2Button = QtGui.QPushButton("&Ch2")
self.connect(self.ch2Button, SIGNAL('clicked()'), self.pushedCh2)
self.ch2Button.setObjectName("ch2Button")
self.gridLayout.addWidget(self.ch2Button, 1, 5, 1, 1)
self.ch2lineEdit = QtGui.QLineEdit(self.widget)
self.ch2lineEdit.setObjectName("ch2lineEdit")
self.gridLayout.addWidget(self.ch2lineEdit, 1, 6, 1, 1)
self.drawCh2Button = QtGui.QPushButton(self.widget)
self.drawCh2Button.setObjectName("drawCh2Button")
self.connect(self.drawCh2Button, SIGNAL('clicked()'), self.pushedDrawCh2)
self.gridLayout.addWidget(self.drawCh2Button, 1, 7, 1, 1)
self.powerButton = QtGui.QPushButton(self.widget)
self.powerButton.setObjectName("powerButton")
self.gridLayout.addWidget(self.powerButton, 1, 9, 1, 1)
self.powlineEdit = QtGui.QLineEdit(self.widget)
self.powlineEdit.setObjectName("powlineEdit")
self.gridLayout.addWidget(self.powlineEdit, 1, 10, 1, 2)
self.drawPowerButton = QtGui.QPushButton(self.widget)
self.drawPowerButton.setObjectName("drawPowerButton")
self.connect(self.drawPowerButton, SIGNAL('clicked()'), self.pushedDrawPower)
self.gridLayout.addWidget(self.drawPowerButton, 1, 12, 1, 1)
spacerItem2 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem2, 2, 0, 1, 1)
spacerItem3 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem3, 2, 2, 1, 1)
spacerItem4 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem4, 2, 5, 1, 1)
spacerItem5 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem5, 2, 6, 1, 1)
self.mplCh3 = MatplotlibWidget(self.widget)
self.mplCh3.setObjectName("mplCh3")
self.gridLayout.addWidget(self.mplCh3, 3, 0, 1, 4)
spacerItem6 = QtGui.QSpacerItem(37, 318, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem6, 3, 4, 2, 1)
self.mplCh4 = MatplotlibWidget(self.widget)
self.mplCh4.setObjectName("mplCh4")
self.gridLayout.addWidget(self.mplCh4, 3, 5, 1, 3)
spacerItem7 = QtGui.QSpacerItem(37, 328, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem7, 3, 8, 2, 1)
self.mplImpedance = MatplotlibWidget(self.widget)
self.mplImpedance.setObjectName("mplImpedance")
self.gridLayout.addWidget(self.mplImpedance, 3, 9, 1, 4)
self.ch3Button = QtGui.QPushButton("&Ch3")
self.connect(self.ch3Button, SIGNAL('clicked()'), self.pushedCh3)
self.ch3Button.setObjectName("ch3Button")
self.gridLayout.addWidget(self.ch3Button, 4, 0, 1, 2)
self.ch3lineEdit = QtGui.QLineEdit(self.widget)
self.ch3lineEdit.setObjectName("ch3lineEdit")
self.gridLayout.addWidget(self.ch3lineEdit, 4, 2, 1, 1)
self.drawCh3Button = QtGui.QPushButton(self.widget)
self.drawCh3Button.setObjectName("drawCh3Button")
self.connect(self.drawCh3Button, SIGNAL('clicked()'), self.pushedDrawCh3)
self.gridLayout.addWidget(self.drawCh3Button, 4, 3, 1, 1)
self.ch4Button = QtGui.QPushButton("&Ch4")
self.connect(self.ch4Button, SIGNAL('clicked()'), self.pushedCh4)
self.ch4Button.setObjectName("ch4Button")
self.gridLayout.addWidget(self.ch4Button, 4, 5, 1, 1)
self.ch4lineEdit = QtGui.QLineEdit(self.widget)
self.ch4lineEdit.setObjectName("ch4lineEdit")
self.gridLayout.addWidget(self.ch4lineEdit, 4, 6, 1, 1)
self.drawCh4Button = QtGui.QPushButton(self.widget)
self.drawCh4Button.setObjectName("drawCh4Button")
self.connect(self.drawCh4Button, SIGNAL('clicked()'), self.pushedDrawCh4)
self.gridLayout.addWidget(self.drawCh4Button, 4, 7, 1, 1)
self.impedanceButton = QtGui.QPushButton(self.widget)
self.impedanceButton.setObjectName("impedanceButton")
self.gridLayout.addWidget(self.impedanceButton, 4, 9, 1, 2)
self.impedancelineEdit = QtGui.QLineEdit(self.widget)
self.impedancelineEdit.setObjectName("impedancelineEdit")
self.gridLayout.addWidget(self.impedancelineEdit, 4, 11, 1, 1)
self.drawImpedanceButton = QtGui.QPushButton(self.widget)
self.drawImpedanceButton.setObjectName("drawImpedanceButton")
self.connect(self.drawImpedanceButton, SIGNAL('clicked()'), self.pushedDrawImpedance)
self.gridLayout.addWidget(self.drawImpedanceButton, 4, 12, 1, 1)
spacerItem8 = QtGui.QSpacerItem(1295, 13, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem8, 5, 1, 1, 12)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.drawAllButton = QtGui.QPushButton(self.widget)
self.drawAllButton.setObjectName("drawAllButton")
self.horizontalLayout.addWidget(self.drawAllButton)
self.clearAllButton = QtGui.QPushButton(self.widget)
self.clearAllButton.setObjectName("clearAllButton")
self.horizontalLayout.addWidget(self.clearAllButton)
self.gridLayout.addLayout(self.horizontalLayout, 6, 0, 1, 8)
FourChannels.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(FourChannels)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1414, 22))
self.menubar.setObjectName("menubar")
FourChannels.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(FourChannels)
self.statusbar.setObjectName("statusbar")
FourChannels.setStatusBar(self.statusbar)
self.retranslateUi(FourChannels)
QtCore.QObject.connect(self.clearAllButton, QtCore.SIGNAL("clicked()"), self.ch4lineEdit.clear)
QtCore.QObject.connect(self.clearAllButton, QtCore.SIGNAL("clicked()"), self.ch3lineEdit.clear)
QtCore.QObject.connect(self.clearAllButton, QtCore.SIGNAL("clicked()"), self.ch2lineEdit.clear)
QtCore.QObject.connect(self.clearAllButton, QtCore.SIGNAL("clicked()"), self.ch1lineEdit.clear)
#~ QtCore.QObject.connect(self.ch1Button, QtCore.SIGNAL("clicked()"), self.ch1lineEdit.paste)
#~ QtCore.QObject.connect(self.ch2Button, QtCore.SIGNAL("clicked()"), self.ch2lineEdit.paste)
#~ QtCore.QObject.connect(self.ch3Button, QtCore.SIGNAL("clicked()"), self.ch3lineEdit.paste)
#~ QtCore.QObject.connect(self.ch4Button, QtCore.SIGNAL("clicked()"), self.ch4lineEdit.paste)
QtCore.QObject.connect(self.ch1Button, QtCore.SIGNAL("clicked()"), self.ch1lineEdit.update)
QtCore.QObject.connect(self.ch2Button, QtCore.SIGNAL("clicked()"), self.ch2lineEdit.update)
QtCore.QObject.connect(self.ch3Button, QtCore.SIGNAL("clicked()"), self.ch3lineEdit.update)
QtCore.QObject.connect(self.ch4Button, QtCore.SIGNAL("clicked()"), self.ch4lineEdit.update)
QtCore.QObject.connect(self.clearAllButton, QtCore.SIGNAL("clicked()"), self.powlineEdit.clear)
QtCore.QObject.connect(self.clearAllButton, QtCore.SIGNAL("clicked()"), self.impedancelineEdit.clear)
QtCore.QObject.connect(self.impedanceButton, QtCore.SIGNAL("clicked()"), self.impedancelineEdit.paste)
QtCore.QObject.connect(self.powerButton, QtCore.SIGNAL("clicked()"), self.powlineEdit.paste)
QtCore.QMetaObject.connectSlotsByName(FourChannels)
def retranslateUi(self, FourChannels):
FourChannels.setWindowTitle(QtGui.QApplication.translate("FourChannels", "DrawAcquisitions", None, QtGui.QApplication.UnicodeUTF8))
self.ch1Button.setText(QtGui.QApplication.translate("FourChannels", "Ch1", None, QtGui.QApplication.UnicodeUTF8))
self.drawCh1Button.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.ch2Button.setText(QtGui.QApplication.translate("FourChannels", "Ch2", None, QtGui.QApplication.UnicodeUTF8))
self.drawCh2Button.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.powerButton.setText(QtGui.QApplication.translate("FourChannels", "Power", None, QtGui.QApplication.UnicodeUTF8))
self.drawPowerButton.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.ch3Button.setText(QtGui.QApplication.translate("FourChannels", "Ch3", None, QtGui.QApplication.UnicodeUTF8))
self.drawCh3Button.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.ch4Button.setText(QtGui.QApplication.translate("FourChannels", "Ch4", None, QtGui.QApplication.UnicodeUTF8))
self.drawCh4Button.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.impedanceButton.setText(QtGui.QApplication.translate("FourChannels", "Impedance", None, QtGui.QApplication.UnicodeUTF8))
self.drawImpedanceButton.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.drawAllButton.setText(QtGui.QApplication.translate("FourChannels", "Draw", None, QtGui.QApplication.UnicodeUTF8))
self.clearAllButton.setText(QtGui.QApplication.translate("FourChannels", "Clear all", None, QtGui.QApplication.UnicodeUTF8))
def pushedCh1(self):
path = QFileDialog.getOpenFileName(self,"Select CSV", QtCore.QDir.homePath(), ("CSV (*.csv)"))
self.ch1lineEdit.setText(path)
self.data[0] = self.procFile(path)
return path
def pushedCh2(self):
path = QFileDialog.getOpenFileName(self,"Select CSV", QtCore.QDir.homePath(), ("CSV (*.csv)"))
self.ch2lineEdit.setText(path)
self.data[1] = self.procFile(path)
return path
def pushedCh3(self):
path = QFileDialog.getOpenFileName(self,"Select CSV", QtCore.QDir.homePath(), ("CSV (*.csv)"))
self.ch3lineEdit.setText(path)
self.data[2] = self.procFile(path)
return path
def pushedCh4(self):
path = QFileDialog.getOpenFileName(self,"Select CSV", QtCore.QDir.homePath(), ("CSV (*.csv)"))
self.ch4lineEdit.setText(path)
self.data[3] = self.procFile(path)
return path
def pushedDrawCh1(self):
self.mplCh1.axes.plot(self.data[0][0],self.data[0][1])
self.mplCh1.axes.set_xlim(min(x),max(x))
self.mplCh1.axes.set_ylim(min(y),max(y))
self.mplCh1.axes.draw()
def pushedDrawCh2(self):
self.mplCh2.axes.plot(self.data[1][0],self.data[1][1])
self.mplCh2.axes.set_xlim(min(x),max(x))
self.mplCh2.axes.set_ylim(min(y),max(y))
self.mplCh2.axes.draw()
def pushedDrawCh3(self):
self.mplCh3.axes.plot(self.data[2][0],self.data[2][1])
self.mplCh3.axes.set_xlim(min(x),max(x))
self.mplCh3.axes.set_ylim(min(y),max(y))
self.mplCh3.axes.draw()
def pushedDrawCh4(self):
self.mplCh4.axes.plot(self.data[3][0],self.data[3][1])
self.mplCh4.axes.set_xlim(min(x),max(x))
self.mplCh4.axes.set_ylim(min(y),max(y))
self.mplCh4.axes.draw()
def pushedDrawPower(self):
self.mplPower.axes.plot(self.data[4][0],self.data[4][1])
self.mplPower.axes.set_xlim(min(x),max(x))
self.mplPower.axes.set_ylim(min(y),max(y))
self.mplPower.axes.draw()
def pushedDrawImpedance(self):
self.mplImpedance.axes.plot(self.data[5][0],self.data[5][1])
self.mplImpedance.axes.set_xlim(min(x),max(x))
self.mplImpedance.axes.set_ylim(min(y),max(y))
self.mplImpedance.axes.draw()
def redraw(self):
nop()
return 'okDraw'
def procFile(self,filename):
array0 = []
array1 = []
#~ wav = csv.reader(open(filename, 'rb',encoding='latin-1'))
wav = csv.reader(open(filename, 'rb'))
self.data = (list(wav))[2:]# les deux premieres lignes sont du texte
if len(self.data[0]) > 500000:
redux = True
else :
redux = False
for stuff in self.data:
array0.append(float(stuff[0]))
array1.append(float(stuff[1]))
del(self.data)
if redux == True:
meandata0 = [ sum(array0[i:i+7:1])/8 for i in range(0,len(array0),8)]
del(array0)
meandata1 = [ sum(array1[i:i+7:1])/8 for i in range(0,len(array1),8)]
del(array1)
meandata1 = signal.medfilt(meandata1)
return [meandata0,meandata1]
return [array0,array1]
def appendPuissance(Unum,Inum):
P = []
P = [self.data[Unum][1][i] * self.data[Inum][1][i] for i in range(0,len(self.data[Unum][0]))]
self.data.append([self.data[0][0],P])
def appendImpedance(Unum, Inum):
I = []
for i in range(0,len(self.data[Inum][1])):
if self.data[Inum][1][i] !=0:
I.append(self.data[Inum][1][i])
else :
I.append(0.01)
U = self.data[Unum][1]
self.data.append([U,I])
return self.data
I'm using Py(x,y) with python 2.5.4 and QT4 designer.
Here is an example of csv file I'm using to test my program :
x-axis,1
second,Ampere
-1e-04,1.875
-9.99875e-05,3.4375
-9.9975e-05,1.875
-9.99625e-05,0.3125
-9.995e-05,1.875
-9.99375e-05,3.4375
-9.9925e-05,0.3125
-9.99125e-05,3.4375
-9.99e-05,1.875
-9.98875e-05,1.875
-9.9875e-05,3.4375
-9.98625e-05,0.3125
-9.985e-05,3.4375
-9.98375e-05,3.4375
-9.9825e-05,1.875
-9.98125e-05,1.875
-9.98e-05,3.4375
-9.97875e-05,1.875
-9.9775e-05,0.3125
-9.97625e-05,0.3125
-9.975e-05,1.875
-9.97375e-05,1.875
-9.9725e-05,1.875
-9.97125e-05,0.3125
-9.97e-05,3.4375
-9.96875e-05,0.3125
-9.9675e-05,1.875
I would be grateful if someone could help me with these two issues.
Thank you