Setting the Data Set to a 2D matrix

Hello,

I am fairly new to ignition, but have a lot of previous programming experience. I have created master, a variable that parses through an XML file to create a 2D array. I am now trying to show that 2D array in a table in ignition. I know the array is correct as I can get it to output to an xml file. However, I am unable to figure out how to take an empty table and set the data set to my matrix. I believe I can also do this row by row or column by column, but would prefer not to as the matrix is rather large.

#TODO Add it to the table
table = event.source.parent.getComponent("PwrTable")
#event.source.parent.getComponent('PwrTable').data = system.dataset.clearDataset(table.data)
master = system.dataset.toDataSet(master)
event.source.data = master 
#system.db.refresh(table, "PwrTable")
#table = system.dataset.addColumn(table, scaninfo_label, "Scanned Info Labels", str)

With the few lines not commented out above, I get the following error:

Traceback (most recent call last):

  File "<event:actionPerformed>", line 47, in <module>

TypeError: toDataSet(): 1st arg can't be coerced to com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet
Ignition v7.9.6 (b2018012914)
Java: Oracle Corporation 1.8.0_211

Thank you in advance for your help.

Are you sure that you want to try and convert 'master' to a dataset? Based on the small sample of code, I can't tell what master is configured as. I'm thinking based on the previous line it should be 'table'

I think you are correct. I am trying to input my matrix into the table. However, I thought I had to set it to the data set first to do so.

Really need to see some more code, to understand what variable type you end up with after the parsing through the XML.

Either way, assuming that master is a dataset type, then all you need to do is set the table’s data property to the dataset.

table.data = master

You have this script in an event what component is the script on?

If the data that you are working with is in fact an array then you will also need to have a header list to properly create the dataset.

headers = ['x','y']
data = [[10,15],[15,20],[20,25]]

#the number of columns in headers must match the number of columns in each row of data
dsExample = system.dataset.toDataSet(headers,data)

#will create a dataset with the following format
#     x     |     y
#    10    |    15
#    15    |    20
#    20    |    25

I have included the code below to help. Thank you for your assistance.

path = system.file.openFile(".xml")
#TODO Error Proofing with no file selected
if path is not None:
	data = system.file.readFileAsString(path)
	system.tag.writeToTag("FA_Intern_Brittany_Playground/Text2", data)

		
	tree = ET.parse(path)
	root = tree.getroot()
 	
	w = 100
	h = 100
	#creates a list containing 10 lists, each of 15 items, all set to 0
	master = [['' for x in range(w)] for y in range (h)]
	c = 0
	r = -1
 	
	for child in tree.getiterator():
		tag = child.tag
		text = str(child.text)
		text = str(text).replace(',', ';')
		if not '\n' in text and not '\t' in text:
			#print(tag, text)
			if 'Name' in tag:
			#	print("NAME")
				c += 2
				r = 0
			#print(r, c)
			r += 1
			master[r][c] = tag
			
			c += 1
			master[r][c] = text
			c -= 1
			
	#TODO Add it to the table
	table = event.source.parent.getComponent("PwrTable")
	#event.source.parent.getComponent('PwrTable').data = system.dataset.clearDataset(table.data)
	#master = system.dataset.toDataSet(master)
	#event.source.data = master 
	#sys	tem.db.refresh(table, "PwrTable")
	#table = system.dataset.addColumn(table, scaninfo_label, "Scanned Info Labels", str)
 	
	#Exporting to an CSV file
	#print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in master]))
	
	thisfilename = os.path.split(path)
	thisfilename = thisfilename[1]
	thisfilename = thisfilename.strip('.xml')
	thisfilename = thisfilename + '.csv'
 	
	f = open(os.path.join(os.path.expanduser('~'), 'Documents', thisfilename), 'w')
			
	for r in range(h):
		for c in range(w):
			#if master[r][c] != '':
			f.write(master[r][c]+',')
		f.write("\n")
	f.close()

So it looks like the variable master is indeed a list of lists, which is just fine. All you’re missing is a list of headers.

Something like

headers = ['Col ' + str(col) for col in range(w)]

table.data = system.dataset.toDataSet(headers,master)
2 Likes

Thank you so much for your help!

The system.dataset.toDataSet() function converts from a normal DataSet to a PyDataSet, which is a wrapper class that makes working with datasets more Python-esque.

Firstly, no, toPyDataSet() will do that. And secondly, what does this have to do with anything? I saw your other flagged post on another ancient topic (4yrs old) with another seemingly random reply...

1 Like