Hi everyone,
I have a dataset with two columns. Column1 has string values that I need to convert into integers in order to use both columns on a chart. Is this possible?
I was thinking about creating a Custom Property (dataset) on the Root Container that is bound to the dataset and use a propertyChange event to fire a script to loop through the dataset, cast all the values in Column1 to an integer and then push the new dataset to another Custom Property. I am having a hard time with the script for this and seem to be overcomplicating it.
It would be nice if there was an Expression language to cast an entire columnâs value of a dataset to integer, etc. Looks like Expression can only handle one.
[quote=âframebuilderâ]It would be nice if there was an Expression language to cast an entire columnâs value of a dataset to integer, etc. Looks like Expression can only handle one.[/quote]Youâre in luck!
The view() expression function can do this with one line of pseudo-SQL:view("SELECT columnname1, int(columnname2) as newcolumnname",
{Root Container.mySourceDataset})⌠with appropriate substitutions for your case.
Phil,
I tried installing the module but the Designer hangs at âInitializing Scriptingâ. If I uninstall, the Designer launches fine. This is on 7.7.6 with version 1.6.1 (b1510110139) Simulation Aids. A Gateway restart has not helped.
[quote=âframebuilderâ]Phil,
I tried installing the module but the Designer hangs at âInitializing Scriptingâ. If I uninstall, the Designer launches fine. This is on 7.7.6 with version 1.6.1 (b1510110139) Simulation Aids. A Gateway restart has not helped.[/quote]Thanks for the heads-up. I hadnât had a chance to play with 7.7.6 yet. Will fix.
[quote=âpturmelâ][quote=âframebuilderâ]Phil,
I tried installing the module but the Designer hangs at âInitializing Scriptingâ. If I uninstall, the Designer launches fine. This is on 7.7.6 with version 1.6.1 (b1510110139) Simulation Aids. A Gateway restart has not helped.[/quote]Thanks for the heads-up. I hadnât had a chance to play with 7.7.6 yet. Will fix.[/quote]
Ah, just noticed your build number. You should upgrade to the latest, b1511032223. Iâll go fix the link in the post.
Jordan,
I was wondering if someone would catch that. Unfortunately, it has to be a string because it comes from another datasource that we canât modify.
Hereâs what I came up with so far by playing with a Table as the output (Iâll move to a chart later). It seems to be working for the one string column and converting to int, but I canât figure out how to build the next column which is already an int.
[quote=âJordanCClarkâ]Also, I like Philâs solution a lot! Just wanna be clear on that one, as itâs a good all-in-one tool.
On the other hand, doing it the âlong wayâ is a good way to build your coding skills.
Just shows there are a lot of skinned cats⌠[/quote]The simulation aids module can help you learn these algorithms, too â if you can express the problem in pseudo-SQL to get a result from view(), you can turn on its âdebugâ mode to see the python it created to solve your problem. Which you can then cut, paste, study, and customize further.
data = event.source.parent.CycleTimes
table = event.source.parent.getComponent("Table")
rows = []
headers = ["OP100 CT", "Line PC"]
for row in range(data.rowCount):
oneRow = [int(data.getValueAt(row, "OP100 Cycle Time2")), data.getValueAt(row, "Line Production Count")]
rows.append(oneRow)
data = system.dataset.toDataSet(headers,rows)
table.data = data
Phil,
Iâm going to try out the Simulation Aids module as well, but I have a question. You mention it works with Expressionsâdoes that mean when I go to bind a chartâs data, I can use the Expression button and I will see a new option on the right side that has view() and some others? Iâm getting excited.
Hereâs how to implement the solution using a MutablePyDataSet:
table = event.source.parent.getComponent('Table')
data = pa.dataset.toData(table.data)
for row in data:
row["Col 1"] = int(row["Col 1"])
table.data = data.toDataSet()MutablePyDataSet is a swiss army knife for Datasets.
To show off the various things you can do with a MutablePyDataSet, hereâs another way it could be done:table = event.source.parent.getComponent('Table')
data = pa.dataset.toData(table.data)
data.setColumn("OP100 Cycle Time2",map(int,data.columns["OP100 Cycle Time2"]))
table.data = data.toDataSet()1. The âpa.dataset.toDataâ function converts the dataset to a MutablePyDataSet.
data.columns[âOP100 Cycle Time2â] gets the entire column as a list.
map(int,data.columns[âOP100 Cycle Time2â]) creates a new list from the âOP100 Cycle Time2â column list but with each value converted to an int.
data.setColumn(âOP100 Cycle Time2â,map(str,data.columns[âOP100 Cycle Time2â])) sets a new column at position âOP100 Cycle Time2â, using the result of map(int,data.columns[âOP100 Cycle Time2â]) as the new column.
[quote=âframebuilderâ]Iâm going to try out the Simulation Aids module as well, but I have a question. You mention it works with Expressionsâdoes that mean when I go to bind a chartâs data, I can use the Expression button and I will see a new option on the right side that has view() and some others? Iâm getting excited.[/quote]Yes. In the f(x) menu.
The view() function and the objectScript() function are the most versatile for general use. The rest of the module really is tailored to simulation of not-yet-available processes.
Expression functions have the big advantage that they run in the designer without having to ask for preview mode, making some design tasks much quicker and/or more intuitive. The demo project has several live examples.
Hi Phil,
I have the view() function working with a PowerTable (thank you btw), but when I try grab a piece of information in a Text Field Component all I get is Dataset [1R x 1C]. Is there a way return the value rather than the dataset info?
I have a dataset tag that pulls data from our ERP system. Working with PowerTables getting specific information seems pretty easy, however, grabbing specific information into other components doesnât work the way that I would like. Iâm new to Python so my going is a little slow.
I tried the following:
view({Root Container.txtStatusPath.text}, {Performance/Performance_Detail})[0,0]
view({Root Container.txtStatusPath.text}, {Performance/Performance_Detail}[0,0])
view({Root Container.txtStatusPath.text}, {Performance/Performance_Detail[0,0]})
None of these worked.
Iâm trying to learn about working with datasets as much as I can now.
Careful! Ignition's expression language isn't python. view() is part of the expression language--it just uses python for its internals for for the expressions nested within the pseudo-SQL.
Anyways, this one should have worked, since view() returns a dataset: