Hey folks,
Apologies for the formatting.
I have a batch of CSV files that I would like to read in to a dataset, find the line of best fit equation, then use this equation to generate a new dataset at regular intervals.
I have a functioning script, however I’m encountering issues reading certain CSVs in the batch. I’m unsure why this is occurring, as the CSVs are all sourced in the same manner and appear to be formatted the same when I inspect them.
The error I am receiving is as follows:
Traceback (most recent call last):
File "<event:actionPerformed>", line 46, in CSV
ValueError: invalid literal for __float__: 1.#QNAN00
Ignition v7.9.10 (b2018112821)
Java: Azul Systems, Inc. 9.0.7.2
And for what it’s worth, here’s the script I am running - testing on buttons and with tables for the time being.
def CSV (esp = event.source.parent):
import string
import system
import csv
import shared
DataOK = 1
csvData =[]
csvRow =[]
# get CSV
path = system.file.openFile('csv')
if path != None:
# open & read csv
f=open(path,'rb')
reader = csv.reader(f)
#process file
rowNum = 0
for row in reader:
#First pass: Header row
if rowNum == 0:
header = row
#Next Passess, Process rest of Data
else:
if DataOK:
csvData.append(row)
rowNum +=1
# Close CSV File
f.close()
# make a dataset and start manipulating it
ds = system.dataset.toDataSet(header, csvData)
data = system.dataset.toPyDataSet(ds)
#Set up column lists
Volume = []
Level = []
Tank = 0
The following lines are where the error occurs
for row in data:
#Volume
Volume.append(float(row["Volume"]))
#Level
Level.append(float(row["Raw Level"]))
#Tank
Tank = float(row["Tank Number"])
#Line of best fit function
#Produced a list with the constants from the line
lobf = shared.math.polyfit(Level, Volume, 3, False)
#Find max level
maxLevel = int(max(Level))
i=0
dataOutput = []
Headers = ["Tank", "Level", "Volume"]
#Calculate new dataset using line of best fit
#i^3(volume)+i^2(volume)+i(volume)+(y-int)
for i in range(maxLevel):
#i squared / cubed
values = [i, i]
i2 = system.math.product(values)
values = [i2, i]
i3 = system.math.product(values)
#3rd order
values = [i3, lobf[3]]
p3 = system.math.product(values)
#2nd order
values = [i2, lobf[2]]
p2 = system.math.product(values)
#first order
values = [i, lobf[1]]
p1 = system.math.product(values)
#y intercept
values = [lobf[0], p1, p2, p3]
sum = system.math.sum(values)
dataOutput.append([int(Tank), i, sum])
i+=1
# Push data to Table on this screen
# assign display labels
tankChart = system.dataset.toDataSet(Headers, dataOutput)
def writeData(tankChart = tankChart, header = header, csvData = csvData, esp = esp):
# data read in
Table = esp.getComponent('Table')
Table.data = system.dataset.toDataSet(header, csvData)
system.db.refresh(Table, "data")
# data output from equation
Table1 = esp.getComponent('Table 1')
Table1.data = tankChart
system.db.refresh(Table1, "data")
system.util.invokeLater(writeData)
try:
system.util.invokeAsynchronous(CSV)
except:
system.gui.errorBox( "An unexpected error importing the CSV", "Error!")
Finally, this is the CSV format.
RowNum,Date,Time,Tank Number,Level,Raw Level,Volume,Temp,Flow Rate,Chart Num
32068,02/05/2019,03:33:35,913.0000000,254.0000000,254.3789978,2042.1732178,47.4560165,0.0000000,61.0000000
32069,02/05/2019,03:33:35,913.0000000,254.0000000,254.3791046,2042.1732178,47.4560165,0.0000000,61.0000000