วิธีการสื่อสารระหว่าง dm-script และ python ใน Digital Micrograph
ฉันจะสื่อสารระหว่างdm-script
รหัสและpython
รหัสได้อย่างไรทั้งที่ดำเนินการใน Digital Micrograph
ในกรณีเฉพาะของฉันฉันมีกล่องโต้ตอบที่ซับซ้อน sice ไดอะล็อกจะไม่ได้รับการสนับสนุนในpython
แต่ผมเขียนไดอะล็อกของฉันในdm-script
dm-script
ปัญหาตอนนี้คือการส่งผ่านจากกล่องโต้ตอบไปยังรหัส python
พิจารณาตัวอย่างต่อไปนี้:
import DigitalMicrograph as DM
script = """string field_value = "";
class ButtonDialog : UIFrame{
void field_changed(object self, TagGroup field){
field_value = field.DLGGetStringValue();
}
object init(object self){
TagGroup dlg, dlg_items, field;
dlg = DLGCreateDialog("Type in a number", dlg_items);
dlg.DLGAddElement(DLGCreateLabel("Number:"));
field = DLGCreateIntegerField(0, 10, "field_changed");
dlg.DLGAddElement(field);
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();
"""
DM.ExecuteScriptString(script)
# how do I get this?
field_value = ""
คำตอบ
DM-scripts เรียกสคริปต์ Python
คำตอบนี้แสดงการสื่อสารระหว่าง Python และ DM-script แต่สร้างด้านตรงข้าม นอกจากนี้ยังใช้ tagGroup แบบถาวรเป็น transfer-proxy
ส่งผ่านค่าอย่างง่ายเข้าและออกจาก Python จากสคริปต์ DM
// //////////////////////////////////////////////////
// Helper methods
// Build the DM-String representing the PythonScript
// which is then called
// //////////////////////////////////////////////////
string AddPythonValueIN( number val, string valName, string valType )
{
string py
py += "\n#Get value DM->Python via global tags" + "\n"
py += "success, " + valName + " = DM.GetPersistentTagGroup().GetTagAs" + valType + "('Python_temp:in:" + valName + "')" + "\n"
py += "if False == success:" + "\n"
py += " print( 'Error. No value passed in by DM-script sucessfully.' )" + "\n"
py += " exit(0)" + "\n"
return py
}
string AddPythonValueIN( string val, string valName, string valType )
{
string py
py += "\n#Get value DM->Python via global tags" + "\n"
py += "success, " + valName + " = DM.GetPersistentTagGroup().GetTagAs" + valType + "('Python_temp:in:" + valName + "')" + "\n"
py += "if False == success:" + "\n"
py += " print( 'Error. No value passed in by DM-script sucessfully.' )" + "\n"
py += " exit(0)" + "\n"
return py
}
string AddPythonValueOut( string valName, string valType )
{
string py
py += "\n#Get value Python->DM via global tags" + "\n"
py += "DM.GetPersistentTagGroup().SetTagAs" + valType + "('Python_temp:out:" + valName + "'," + valName + ")" + "\n"
return py
}
string AddPythonPrintModifyPrint()
{
string py
py += "\n#Print InValue, modify, print OutValue" + "\n"
py += "print('InValue',InValue)"+ "\n"
py += "OutValue = InValue * 2" + "\n"
py += "print('OutValue',OutValue)"+ "\n"
return py
}
// //////////////////////////////////////////////////
// Example calls
// Demonstrating value passing from DM script to a
// called Python snippet. TagTypes need to be
// explicit in Python.
// //////////////////////////////////////////////////
void PythonValueInAndOut_Float()
{
number value = 13.2
GetPersistentTagGroup().TagGroupSetTagAsFloat("Python_temp:in:InValue",value)
Result( " DM-Script passed value in: " + value + "\n")
string py = "#Script to demonstrate value passing in Hybrid scripts. DM calling Python." + "\n"
py += AddPythonValueIN( value, "InValue", "Float" )
py += AddPythonPrintModifyPrint()
py += AddPythonValueOut( "OutValue", "Float" )
Result( "\n PYTHON CODE EXECUTES\n --------------------- \n\n" )
ExecutePythonScriptString( py, 1 )
Result( "\n --------------------- \n PYTHON CODE FINISHED \n\n" )
if ( !GetPersistentTagGroup().TagGroupGetTagAsFloat("Python_temp:out:OutValue",value))
Throw("Python value passed out not found.")
Result( "DM-Script received value: " + value + "\n")
GetPersistentTagGroup().TagGroupDeleteTagWithLabel("Python_temp")
}
void PythonValueInAndOut_Double()
{
number value = 13.2
GetPersistentTagGroup().TagGroupSetTagAsDouble("Python_temp:in:InValue",value)
Result( " DM-Script passed value in: " + value + "\n")
string py = "#Script to demonstrate value passing in Hybrid scripts. DM calling Python." + "\n"
py += AddPythonValueIN( value, "InValue", "Double" )
py += AddPythonPrintModifyPrint()
py += AddPythonValueOut( "OutValue", "Double" )
Result( "\n PYTHON CODE EXECUTES\n --------------------- \n\n" )
ExecutePythonScriptString( py, 1 )
Result( "\n --------------------- \n PYTHON CODE FINISHED \n\n" )
if ( !GetPersistentTagGroup().TagGroupGetTagAsDouble("Python_temp:out:OutValue",value))
Throw("Python value passed out not found.")
Result( "DM-Script received value: " + value + "\n")
GetPersistentTagGroup().TagGroupDeleteTagWithLabel("Python_temp")
}
void PythonValueInAndOut_String()
{
string value = "hallo"
GetPersistentTagGroup().TagGroupSetTagAsString("Python_temp:in:InValue",value)
Result( " DM-Script passed value in: " + value + "\n")
string py = "#Script to demonstrate value passing in Hybrid scripts. DM calling Python." + "\n"
py += AddPythonValueIN( value, "InValue", "String" )
py += AddPythonPrintModifyPrint()
py += AddPythonValueOut( "OutValue", "String" )
Result( "\n PYTHON CODE EXECUTES\n --------------------- \n\n" )
ExecutePythonScriptString( py, 1 )
Result( "\n --------------------- \n PYTHON CODE FINISHED \n\n" )
if ( !GetPersistentTagGroup().TagGroupGetTagAsString("Python_temp:out:OutValue",value))
Throw("Python value passed out not found.")
Result( "DM-Script received value: " + value + "\n")
GetPersistentTagGroup().TagGroupDeleteTagWithLabel("Python_temp")
}
ClearResults()
PythonValueInAndOut_float()
PythonValueInAndOut_double()
PythonValueInAndOut_string()
การส่งผ่านข้อมูลรูปภาพเข้าและออกจาก Python จากสคริปต์ DM
// Images and other objects which are in memory can be most easily passed
// by passing their ID value and then finding them by ID.
// However, this does not work if the object would go out of scope at the
// end of a script (like an image which is not displayed)
//
// To pass a NumpyArray out of Python without showing an image, one can
// instead write the array to the global tags directly, together with the
// information needed to read the tags out into a DM-image
void PythonImageInOut( )
{
image img := RealImage("test",4,5,5)
img = icol
Result( " DM-Script passed image ["+img.ImageGetLabel()+"] in.\n")
string py
py += "\n#Get image DM->Python via imageLabel" + "\n"
py += "img = DM.FindImageByID("+ img.ImageGetID()+")" + "\n"
py += "if None == img:" + "\n"
py += " print( 'Error.No image passed in by DM-script sucessfully.' )" + "\n"
py += " exit(0)" + "\n"
py += "\n"
py += "print('Image from DM: Label = ', img.GetLabel())" + "\n"
py += "print('Image from DM: Name = ', img.GetName())" + "\n"
py += "print('Image from DM: Value = \\n', img.GetNumArray())" + "\n"
py += "del img #ALWAYS delete DM objects in Python scripts" + "\n"
py += "\n"
py += "\n#Create DM image in Python and pass out as ID via global tags" + "\n"
py += "import numpy as np" + "\n"
py += "data = np.arange(48).reshape(6,8)" + "\n"
py += "outImg = DM.CreateImage(data.copy(order='C'))" + "\n"
py += "DM.GetPersistentTagGroup().SetTagAsLong('Python_temp:out:ID',outImg.GetID())" + "\n"
py += "print('Create new image with ID:',outImg.GetID())"+"\n"
py += "outImg.ShowImage() #Image needs to be shown or it will not stay in memory" + "\n"
py += "del outImg #ALWAYS delete DM objects in Python scripts" + "\n"
Result( "\n PYTHON CODE EXECUTES\n --------------------- \n\n" )
ExecutePythonScriptString( py, 1 )
Result( "\n --------------------- \n PYTHON CODE FINISHED \n\n" )
number imgID
if ( !GetPersistentTagGroup().TagGroupGetTagAsLong("Python_temp:out:ID",imgID))
Throw("Python imageID value passed out not found.")
image outImg := FindImageByID(imgID)
if ( !outImg.ImageIsValid() )
Throw("Python imageID value passed out but no matching image found.")
Result( "DM-Script received image: " + outImg.ImageGetLabel()+ "\n")
GetPersistentTagGroup().TagGroupDeleteTagWithLabel("Python_temp")
}
void PythonArrayOut( )
{
string py
py += "#Passing Numpy array values via global tags" + "\n"
py += "import numpy as np" + "\n"
py += "data = np.arange(6*8*3).reshape(6,8,3)" + "\n"
py += "outImg = DM.CreateImage(data.copy(order='C'))" + "\n"
py += "DM.GetPersistentTagGroup().SetTagAsArray('Python_temp:out:array',outImg)" + "\n"
py += "DM.GetPersistentTagGroup().SetTagAsLong('Python_temp:out:type',outImg.GetDataType())" + "\n"
py += "for i in range(data.ndim,0,-1):" + "\n"
py += " DM.GetPersistentTagGroup().SetTagAsLong('Python_temp:out:dim:' + str(i-1),data.shape[i-1])" + "\n"
py += "\n"
py += "del outImg #ALWAYS delete DM objects in Python scripts" + "\n"
Result( "\n PYTHON CODE EXECUTES\n --------------------- \n\n" )
ExecutePythonScriptString( py, 1 )
Result( "\n --------------------- \n PYTHON CODE FINISHED \n\n" )
number dataType
if ( !GetPersistentTagGroup().TagGroupGetTagAsLong("Python_temp:out:type",dataType))
Throw("Python dataType value passed out not found.")
TagGroup dimTG
if ( !GetPersistentTagGroup().TagGroupGetTagAsTagGroup("Python_temp:out:dim",dimTG))
Throw("Python dimensions passed out not found.")
number nDim = dimTG.TagGroupCountTags()
number n0, n1, n2, n3, n4
dimTG.TagGroupGetIndexedTagAsLong(0,n0)
dimTG.TagGroupGetIndexedTagAsLong(1,n1)
dimTG.TagGroupGetIndexedTagAsLong(2,n2)
dimTG.TagGroupGetIndexedTagAsLong(3,n3)
dimTG.TagGroupGetIndexedTagAsLong(4,n4)
image outImg
if ( 1 == nDim )
outImg := NewImage("Test", dataType, n0 )
else if ( 2 == nDim )
outImg := NewImage("Test", dataType, n0, n1 )
else if ( 3 == nDim )
outImg := NewImage("Test", dataType, n0, n1, n2 )
else if ( 4 == nDim )
outImg := NewImage("Test", dataType, n0, n1, n2, n3 )
else if ( 5 == nDim )
outImg := NewImage("Test", dataType, n0, n1, n2, n3, n4 )
if ( !GetPersistentTagGroup().TagGroupGetTagAsArray("Python_temp:out:array",outImg))
Throw("Python array data passed out not found.")
Result("\n DM recreated image from array values: " + outImg.ImageGetLabel()+ "\n")
GetPersistentTagGroup().TagGroupDeleteTagWithLabel("Python_temp")
}
clearresults()
Result("\n\nEXAMPLE Python<->DM image passing\n")
PythonImageInOut()
Result("\n\nEXAMPLE Python-->DM array passing\n")
PythonArrayOut( )
เพื่อประสานข้อมูลระหว่างdm-script
และpython
ขณะที่ทั้งคู่กำลังทำงานอยู่ในกรณีเดียวกัน (และด้ายในตัวอย่างนี้สามารถแก้ไขได้สำหรับหัวข้อที่แตกต่างกันเกินไป) หนึ่งสามารถใช้แท็กถาวร
dm-script
คือการตั้งค่าแท็กถาวร python
จากนั้นสามารถอ่านแท็กถาวรอีกครั้ง
อัปเดต: โมดูล Python
เนื่องจากข้อ จำกัด ที่กล่าวถึงด้านล่างและโค้ดที่เพิ่มขึ้นเรื่อย ๆ และไม่สามารถอ่านได้ฉันจึงตัดสินใจเขียนโมดูล python ของตัวเองสำหรับสิ่งนี้ ด้วยexecdmscriptหนึ่งสามารถรันโค้ด dm-script ด้วยการซิงโครไนซ์ตัวแปรได้อย่างง่ายดายจาก Digital Micrograph
ลองดูตัวอย่างต่อไปนี้:
from execdmscript import exec_dmscript
# some script to execute
dmscript = """
number input;
number s = GetNumber("Enter a number between " + min + " and " + max + ".", init, input);"
"""
# variables that will be defined for the dm-scripts (and readable later on in python)
sv = {"min": 1, "max": 10, "init": 2}
# variables the dm-script defines and that should be readable in the python file
rv = {"input": "number", "s": "number"}
with exec_dmscript(dmscript, readvars=rv, setvars=sv) as script:
if script["s"]:
print(script["input"])
else:
print("User pressed cancel.")
สิ่งนี้จะซ่อนการบันทึกและปัญหาเกี่ยวกับ dm-script ทั้งหมด (ดังกล่าวด้านล่าง) จะช่วยให้การใช้งานlist
และdict
s และประเภทพื้นฐานชอบbool
, str
, และint
float
ทุกประเภทและค่าสามารถใช้ได้ในรูปแบบ pythonic โดยไม่จำเป็นต้องสนใจประเภท dm-script สามารถย้าย dm-script ไปยังไฟล์แยกเพื่อล้างโค้ดได้มากขึ้น
โปรดทราบว่าสำหรับการแก้จุดบกพร่องมีสวิทช์ที่จะบันทึกรหัสที่จะได้รับexec_dmscript(debug=True, debug_file="path/to/file")
debug_file
จากนั้นไฟล์นี้สามารถดำเนินการใน GMS ด้วยตนเองซึ่งจะแสดงข้อผิดพลาด
ข้อจำกัดความรับผิดชอบ : ดังที่ได้กล่าวไว้ฉันเป็นผู้เขียนexecdmscriptโมดูล ฉันคิดว่านี่เป็นทางออกที่ดีที่สุดสำหรับกรณีนี้ อันที่จริงคำถามนี้เป็นเหตุผลที่ฉันเขียนโมดูล
ประเภทข้อมูลพื้นฐาน
สำหรับประเภทข้อมูลพื้นฐานเช่นstring
และnumber
สามารถสร้างและเพิ่มรหัสด้วยมือได้ ตัวอย่างต่อไปนี้แสดงแนวคิด:
import DigitalMicrograph as DM
import time
sync_id = "sync-{}".format(int(time.time() * 100))
dmscript = """
number input;
number s = GetNumber("Enter a number between {min} and {max}.", {init}, input);
TagGroup p = GetPersistentTagGroup();
p.TagGroupSetTagAsBoolean("{id}-success", s);
p.TagGroupSetTagAsLong("{id}-input", input);
"""
DM.ExecuteScriptString(dmscript.format(min=1, max=10, init=2, id=sync_id))
# cannot save TagGroups to variables, check below
s, success = DM.GetPersistentTagGroup().GetTagAsBoolean("{}-success".format(sync_id))
if s and success:
# cannot save TagGroups to variables, check below
s, input = DM.GetPersistentTagGroup().GetTagAsLong("{}-input".format(sync_id))
if s:
print(input)
else:
print("Did not find 'input' in persistent tags.")
elif not s:
print("Did not find 'success' in persistent tags.")
else:
print("User clicked cancel.")
# cannot save TagGroups to variables, check below
# delete tag, otherwise the persistent tags gets filled with garbage
DM.GetPersistentTagGroup().DeleteTagWithLabel("{}-success".format(sync_id))
DM.GetPersistentTagGroup().DeleteTagWithLabel("{}-input".format(sync_id))
อย่างที่เห็นมีข้อเสียบางประการของวิธีนี้ มีรหัสจำนวนมากที่สร้างการตั้งค่าและการซิงโครไนซ์สำหรับการป้อนตัวเลขเพียงตัวเดียว สิ่งนี้ทำให้โค้ดยุ่ง นอกจากนี้คุณอาจต้องการบันทึกรหัส dm-script ในไฟล์แยกต่างหากซึ่งจะเพิ่มการเปิดไฟล์ python การดีบักโค้ด dm-script ก็ทำได้ยากในโค้ดนี้เช่นกัน และสุดท้ายมีข้อ จำกัด เกี่ยวกับ TagGroup ที่เดินทางในฝั่ง python (ตรวจสอบด้านล่าง)
ข้อ จำกัด
อัปเดต : โมดูล python ที่กล่าวถึงด้านล่างสามารถจัดการกับTagGrou
s และTagList
s ได้เนื่องจากมันบันทึกไว้ในลักษณะเชิงเส้นและมีคีย์และประเภทแยก
โปรดทราบว่าTagGroup
การซิงโครไนซ์นั้นยากมาก TagGroup
ต้องไม่กำหนดให้กับตัวแปร ทันทีที่มีก็จะไม่สามารถใช้งานได้อีกต่อไป สิ่งนี้สามารถแสดงได้ด้วยรหัสที่ง่ายมากต่อไปนี้:
import DigitalMicrograph as DM
# create group for the example
group1 = DM.NewTagGroup()
group1.SetTagAsString("test-string", "test content")
print("Group 1 valid:", group1.IsValid(), "(type:", type(group1), ")")
# save to persistent tags
DM.GetPersistentTagGroup().SetTagAsTagGroup("test", group1)
# get the group without assigning to a variable, works
s, group2 = DM.GetPersistentTagGroup().GetTagAsTagGroup("test")
print("Group 2 success:", s, ", valid:", group2.IsValid(), "(type:", type(group2), ")")
# assign one parent group to a variable, doesn't work
tags = DM.GetPersistentTagGroup()
s, group3 = tags.GetTagAsTagGroup("test")
print("Group 3 success:", s, ", valid:", group3.IsValid(), "(type:", type(group3), ")")
รหัสนี้จะสร้างผลลัพธ์
Group 1 valid: True (type: <class 'DigitalMicrograph.Py_TagGroup'> )
Group 2 success: True , valid: True (type: <class 'DigitalMicrograph.Py_TagGroup'> )
Group 3 success: True , valid: False (type: <class 'DigitalMicrograph.Py_TagGroup'> )
นี่แสดงว่าคุณไม่สามารถกำหนดTagGroup
s ให้กับตัวแปร python
ความคิดแบบอะซิงโครนัส
สิ่งนี้สามารถขยายได้สำหรับแอปพลิเคชันแบบอะซิงโครนัส dm-script
และpython
การดำเนินงานที่สามารถตั้งกระทู้ที่สังเกตแท็กถาวร (โปรดทราบว่าคุณสามารถทำเครื่องหมายTagGroup
s ว่าแก้ไขโดยใช้TagGroupMarkAsChanged()
) แท็กที่แตกต่างกันโดยเฉพาะ จากนั้นทั้งสองสามารถเพิ่มคำสั่งและ / หรือข้อมูลลงในแท็ก "อินสแตนซ์" อื่นสามารถอ่านและประมวลผลได้
ต้องเข้ารหัสด้วยตนเอง สิ่งนี้ (ปัจจุบัน) ไม่รวมอยู่ในexecdmscript
โมดูล