大家好,
(JMP 17 Pro 用户)当我尝试将数据表对象传递给 Python 并将它们检索回 JSL 时遇到了一个奇怪的问题,特别是在使用 Application Builder 时。
由于这是我第一次使用 Application Builder,因此我通过标准 JSL 脚本编写了我想要在应用程序中实现的行为的草稿(就上下文而言,目标是应用程序允许用户选择目录/文件,然后让 JMP 生成图表进行分析)。该行为在标准 JSL 脚本中有效,但在 Application Builder 中无效。
这是我的独立(非应用程序构建器)脚本:
// Initialize Python within JMP
PyConnect = Python Connect();
Names Default to Here( 1 );
// Allow user to manually pick directory and file
directory = Pick Directory ("Select a directory.");
chosen_file = Pick File("Select a file", directory);
// Open the selected file, then send to Python
dt1 = Open(chosen_file, Table Contains Column Headers(0));
Python Send(dt1);
// Close the dirty data table to save space/resources
Close(dt1, NoSave);
show(dt1, "first");
// Clean the table by removing all references to "#"
Python Submit("\[
dt1 = dt1[~dt1["Column 1"].str.contains("#")]
]\");
// Retrieve the processed table from Python and open in JMP
// Terminate Python connection
dt1 = Python Get(dt1);
Python Term();
show(dt1, "second");该脚本的输出如下:
dt1 = DataTable("D424TUZ0.Inspec Meas 01");
"first";
dt1 = DataTable("dt1");
"second";(不起作用)这是我的应用程序生成器脚本:
executePress=Function({thisBox}, {Default Local},
// This function is called when the button is pressed
py = Python Connect();
dt1 = Open(::file, Table Contains Column Headers(0));
Close(dt1, NoSave);
show(dt1, "first");
// Clean the table by removing all references to "#"
Python Submit("\[
dt1 = dt1[~dt1["Column 1"].str.contains("#")]
]\");
dt1 = Python Get(dt1);
Python Term();
show(dt1, "second");
);以及令人不安的输出:
dt1 = DataTable("D424TUZ0.Inspec Meas 01");
"first";
/**********/
dt1 = dt1[~dt1["Column 1"].str.contains("#")]
/**********/
dt1 = .;
"second";TLDR:在我的独立脚本中,通过dt1 = Python Get(dt1);从 Python 中的清理中检索数据表后,我得到了一个对象dt1 = DataTable("dt1"); 。但是在 JMP Application Builder 脚本中,我得到了dt1 = .; (这对我来说表明该对象不存在?)。
当我尝试使用dt1 << New Data Window 时,我第一次意识到这一点;阅读此线程让我意识到我可能根本没有使用对象: https://community.jmp.com/t5/Discussions/Error-in-running-script-Send-Expects-Scriptable-Object-in-access/td-p/718245
抱歉,帖子太长了。这是一个奇怪的错误,在此先感谢大家对这个问题的帮助!
我在您的应用程序生成器脚本中没有看到任何Python Send(dt1);
另请注意,JMP 18 改变了Python Send(dt) 的操作。在 JMP 18 及更高版本中,您将获得 jmp.DataTable 对象,而不是 pandas.DataFrame。并且该 DataTable 对象不再是数据的副本,而是对数据表的实时访问,您可以直接在 Python 中对实时表进行处理。有关通过import jmp包支持的 Python API 的示例,请参阅 JMP 18 中的脚本索引。
如果您需要从 jmp.DataTable 转到 pandas.DataFrame,$SAMPLE_SCRIPTS/Python 目录中有几个示例。dt2pandas.py 的作用与 JMP 14-17 完全相同,将 DataTable 作为临时 CSV 文件保存到磁盘,然后告诉 pandas 对文件执行 read_csv()。JMP2pandas.py 是一个示例脚本,展示了如何使用 jmp.DataTable 对象的功能并在内存中创建 pandas.DataFrame。
Python Get( dt ) 识别新的 jmp.DataTable 对象,而 Python Get( df ) 将识别 DataFrame 并使用 CSV 导入,与 JMP 14-17 相同
非常感谢您提供的详细解决方案,这非常有帮助。我不敢相信我错过了这么小的事情。