我如何在python dm-script中遍历TagGroups

问题描述

如何在python的中使用TagGroup的未知结构?


TagGroup被广泛用于 DigitalMicrograph 中。但是我如何在python中穿越它们呢?如何获得所有可用的标签名称

在正常的dm-script中,有TagGroupGetTagLabel()TagGroupGetTagType()之类的功能。可以使用for循环来检查结构。但是python包装器类Py_TagGroup中没有单个函数获取有关标签的信息。同样,这些示例也从未处理过未知的TagGroup


我有一个非常简单的问题:我想使用(显示修改,检查,保存...)图像中的标签。但是我不知道标签名称

img = camera.AcquireImage()
tg = img.GetTagGroup()

但是现在呢?我对此TagGroup一无所知。 TagGroup是不可迭代的,因此我无法在for循环中使用它们。我可以得到它们的长度,但只能访问索引处的数据。而且只有我知道数据类型。我既无法获取类型,也无法获取标签


再说一遍:如何在中的python中穿越TagGroup?如何获得结构?

解决方法

Python API只是DM-script API的一个子集,遗憾的是初始版本缺少一些重要的命令。如果您觉得缺少某个特定命令,请通过Gatan的“问题报告”表格将其作为功能请求报告。

GMS 3.4.2似乎不包含执行所需操作的必要命令(在Python中)

顺便说一句,tagGroups 可迭代的。

taggroup tg = NewTagGroup()
tg.TagGroupSetTagAsString("a","A")
tg.TagGroupSetTagAsString("b","B")
tg.TagGroupSetTagAsString("d","d")
tg.TagGroupSetTagAsString("c","C")

string str
tg.tagGroupGetIndexedTagAsString(2,str)
Result("\n str:" + str)

tg.TagGroupOpenBrowserWindow("",0)
,

GitHUB上(版本≥1.1.4)的Python模块execdmscript支持将Py_TagGroup s转换为python dictlist s。因此,以下代码对我有用:

import execdmscript

img = camera.AcquireImage()
tg = img.GetTagGroup()

# convert the Py_TagGroup object to a python dict
tg = execdmscript.convert_from_taggroup(tg)

for key in tg:
    print(key,tg[key])

免责声明:我是execdmscript模块的作者。对于此问题,目前仍没有简便的解决方案 1 ,因此,我相信这是最好的解决方案。


1 execdmscript.convert_from_taggroup()为此使用了持久标记和dm-script代码。它涉及将Py_TagGroup保存到全局标签。由于dm-script代码可以穿越TagGroup,因此通过执行python解释器中的TagGroup代码来分析dm-script的结构。结果将再次保存到持久性标签中。然后,使用python从全局标签中读取结构,并可以从中创建dictlist