【问题标题】:How do I travel through TagGroups in python dm-script如何在 python dm-script 中通过 TagGroups
【发布时间】:2020-09-29 10:42:32
【问题描述】:

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


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

在正常的dm-script 中有TagGroupGetTagLabel()TagGroupGetTagType() 之类的函数。可以使用for-loop 来检查结构。但是python 包装类Py_TagGroup 中没有一个函数可以获取有关标签的信息。此外,这些示例从不处理未知的TagGroups。


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

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

但是现在呢?我对这个TagGroup一无所知。 TagGroups 不可迭代,因此我无法在 for-loops 中使用它们。我可以得到它们的长度,但我只能访问索引处的数据。只有当我知道数据类型时。我既不能得到类型也不能得到标签。


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

【问题讨论】:

    标签: dm-script dm-script python tags traversal dm-script


    【解决方案1】:

    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)
    

    【讨论】:

    • 使用 iterable 我的意思是 python iterable(或者更好的 python mappings),所以对象可以直接在 for 循环中使用。 Py_TagGroup 既没有 __iter__ 也没有 __getitem__ 函数,因此在 python 术语中它是不可迭代的。我应该更明确地说明我在谈论 python 术语,对此感到抱歉:)
    【解决方案2】:

    GitHUB 上的 Python 模块 execdmscript(版本 ≥ 1.1.4)支持将 Py_TagGroups 转换为 python dicts 或 lists。所以下面的代码对我有用:

    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,所以我相信这是最好的解决方案。


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      相关资源
      最近更新 更多