How do I travel through TagGroups in python dm-script

157 views Asked by At

How do I use unknown structures of TagGroups in in python?


TagGroups are widely used in DigitalMicrograph. But how do I travel through them in python? How do I get all available tag names?

In normal dm-script there are functions like TagGroupGetTagLabel() and TagGroupGetTagType(). One can use a for-loop to check the structure. But there is not a single function in the python wrapper class Py_TagGroup to get information about the labels. Also the examples never deal with unknown TagGroups.


I have a very simple problem: I want to use (show, modify, check, save, ...) the tags from an image. But I don't know the tag names.

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

But now what? I don't know anything about this TagGroup. TagGroups are not iterable so I cannot use them in for-loops. I can get their length but I can only access data at indices. And that only if I know the datatype. Neither can I get the type nor the label.


So again: How can I travel through TagGroups in python in ? How can I get the structure?

2

There are 2 answers

0
miile7 On BEST ANSWER

The Python module execdmscript on GitHUB with (version ≥ 1.1.4) supports converting Py_TagGroups to python dicts or lists. So the following code works for me:

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])

Disclaimer: I am the author of the execdmscript module. Still there is no easy solution1 for this problem at the moment so I am sure this is the best solution.


1execdmscript.convert_from_taggroup() uses the persistent tags and dm-script code for this. It involves saving the Py_TagGroup to the global tags. Since dm-script code can travel through TagGroups, the strucutre of the TagGroup is analyzed by executing dm-script code from the python interpreter. The results are saved to the persistent tags again. Then the structure is read from the global tags with python and a dict or list can be created from it.

1
BmyGuest On

The Python API is only a subset of the DM-script API and initial releases unfortunately lacked some important commands. If you feel a particular command missing, please report it as feature request at Gatan's "issue reporting" form.

GMS 3.4.2 does not seem to contain the necessary commands to do what you want (in Python)

BTW, tagGroups are iterable.

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)