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: