def AtlasPathChanged(): print("atlaspath changed") exp = ctx.field("AtlasPath").stringValue() if MLABFileManager.exists(ctx.expandFilename(exp)): updateAtlas() else: fileDialog()
def updateAtlas(newWeek=None): if MLABFileManager.exists(ctx.expandFilename( ctx.field("AtlasPath").value)): for file in os.listdir(ctx.expandFilename( ctx.field("AtlasPath").value)): if file.endswith(".nii.gz"): if not "parc" in file and not "LogicalMask" in file: if "STA%i" % ctx.field("currentWeek").value in file: print( os.path.join( ctx.expandFilename( ctx.field("AtlasPath").value), file)) ctx.field( "itkImageFileReader.fileName").setStringValue( os.path.join( ctx.expandFilename( ctx.field("AtlasPath").value), file)) if "LogicalMask" in file: if "STA%i" % ctx.field("currentWeek").value in file: ctx.field( "itkImageFileReaderMask.fileName").setStringValue( os.path.join( ctx.expandFilename( ctx.field("AtlasPath").value), file)) else: AtlasPathChanged()
def DataPathChanged(): print("DataPath changed") exp = ctx.field("DataPath").stringValue() if MLABFileManager.exists(ctx.expandFilename(exp)): updateData() else: fileDialog()
def filenameChanged(field=None): filename = ctx.expandFilename(ctx.field("filename").stringValue()) if MLABFileManager.exists(filename): if MLABFileManager.isDir(filename): MLAB.logWarning("LocalAnalyzeLoad.filename - File is a directory (" + filename + ")") else: ctx.field("AnalyzeHeader.filename").setStringValue(filename) else: if not filename == "": MLAB.logWarning("LocalAnalyzeLoad.filename - File does not exist (" + filename + ")") ctx.field("AnalyzeHeader.filename").setStringValue("") parseFile(filename)
def filenameChanged(field=None): filename = ctx.expandFilename(ctx.field("filename").stringValue()) if MLABFileManager.exists(filename): if MLABFileManager.isDir(filename): MLAB.logWarning( "LocalAnalyzeLoad.filename - File is a directory (" + filename + ")") else: ctx.field("AnalyzeHeader.filename").setStringValue(filename) else: if not filename == "": MLAB.logWarning( "LocalAnalyzeLoad.filename - File does not exist (" + filename + ")") ctx.field("AnalyzeHeader.filename").setStringValue("") parseFile(filename)
def initialize(field=None): global _styles _styles = {} cb = ctx.control("styles") cb.clearItems() cb.insertItem("") stylePath = ctx.localPath() + "/Styles/" contents = MLABFileManager.contentOfDirectory(stylePath) for file in contents: if MLABFileManager.isDir(stylePath + file): if MLABFileManager.exists(stylePath + file + "/style") and \ MLABFileManager.exists(stylePath + file + "/tags") and \ MLABFileManager.exists(stylePath + file + "/template") and \ MLABFileManager.exists(stylePath + file + "/style.png"): cb.insertItem(file) _styles[file] = stylePath + file + "/" if _styles: cb.setCurrentItem(0) ctx.field("style").setStringValue(cb.currentText()) selectStyle() global _presentations _presentations = {} cb = ctx.control("presentations") cb.clearItems() cb.insertItem("") presentationsPath = ctx.localPath() + "/Presentations/" contents = MLABFileManager.contentOfDirectory(presentationsPath) for file in contents: if MLABFileManager.isDir(presentationsPath + file): if MLABFileManager.exists(presentationsPath + file + "/config") and \ MLABFileManager.exists(presentationsPath + file + "/slides"): cb.insertItem(file) _presentations[file] = presentationsPath + file + "/"
def buildPresentation(): #set style style = ctx.field("style").stringValue() # check/delete module path localPath = ctx.localPath() + "/" stylePath = localPath + "Styles/" + style contentPath = localPath + "Presentations/" + ctx.field("presentation").stringValue() moduleName = ctx.field("module").stringValue() modulePath = localPath + "/Generated/" + moduleName # create directories MLABFileManager.mkdir(modulePath) MLABFileManager.mkdir(modulePath + "/style") MLABFileManager.mkdir(modulePath + "/data") MLABFileManager.mkdir(modulePath + "/macros") MLABFileManager.mkdir(modulePath + "/scripts") slashRe = re.compile('(.*?)[\/]{3,}',re.DOTALL) #parse style.script global _styleTags _styleTags = {} if MLABFileManager.exists(stylePath + "/tags"): tagRe = re.compile('\[(.*?)\]\s*=\s*(.*)',re.DOTALL) content = MLABFileManager.readStringFromFile(stylePath + "/tags") matches = slashRe.findall(content) for styleTag in matches: if styleTag == "": continue tagMatches = tagRe.findall(styleTag) _styleTags[tagMatches[0][0]] = tagMatches[0][1] # generate content: slides global _slideBullets _slideBullets = {} global _slideNames _slideNames = [] global _slideModules _slideModules = {} slides = "" if MLABFileManager.exists(contentPath + "/slides"): content = MLABFileManager.readStringFromFile(contentPath + "/slides") matches = slashRe.findall(content) count = 0 for slide in matches: if slide == "": continue result = parseSlide(slide,count+1) if not result == "": count += 1 slides += result # generate content: menu menu = "" if MLABFileManager.exists(contentPath + "/menu"): content = MLABFileManager.readStringFromFile(contentPath + "/menu") menuRe = re.compile('(.*?)[\/]+',re.DOTALL) matches = menuRe.findall(content) for menuItem in matches: if menuItem == "": continue menu += parseMenuItem(menuItem) # generate def file defFile = "MacroModule " + moduleName + " { externalDefinition = $(LOCAL)/" + moduleName + ".script }" MLABFileManager.writeStringToFile(modulePath + "/" + moduleName + ".def",defFile) # copy py file MLABFileManager.copy(localPath + "template_py",modulePath + "/" + moduleName + ".py") # load styles styleFile = MLABFileManager.readStringFromFile(stylePath + "/style") styleRe = re.compile('DefineStyle\s*(.*?)\s*\{(.*?)[\/]{3,}',re.DOTALL) styles = styleRe.findall(styleFile) # process slideBullets for i in range(len(styles)): styleName = styles[i][0].strip() styleDef = styles[i][1].strip() for k,v in _slideBullets.iteritems(): for i in range(len(v)): _slideBullets[k][i] = _slideBullets[k][i].replace("style = " + styleName,"style { " + styleDef) _slideBullets[k][i] = _slideBullets[k][i].replace("\n"," ") sources = "" # write slideBullets file if len(_slideBullets): bulletFile = "_bullets = {\n" num = 0 for k,v in _slideBullets.iteritems(): if num > 0: bulletFile += "," bulletFile += "'" + `k` + "':['" + "','".join(v) + "']\n" num = num + 1 bulletFile += "}" sources += "source = $(LOCAL)/scripts/bullets.py\n" MLABFileManager.writeStringToFile(modulePath + "/scripts/bullets.py",bulletFile) # process script file -> replace content tags scriptFile = MLABFileManager.readStringFromFile(stylePath + "/template") scriptFile = scriptFile.replace("template.py",moduleName + ".py") files = MLABFileManager.recursiveContentOfDirectory(contentPath + "/scripts") for file in files: if not file == "": sources += "source = $(LOCAL)/scripts/" + file.strip() + "\n" # print sources if not sources == "": scriptFile = scriptFile.replace("Commands {","Commands {\n" + sources) scriptFile = scriptFile.replace("[HEADER]","\"" + ctx.field("header").stringValue() + "\"") scriptFile = scriptFile.replace("[FOOTER]","\"" + ctx.field("footer").stringValue() + "\"") scriptFile = scriptFile.replace("[MENU]",menu) scriptFile = scriptFile.replace("[SLIDES]",slides) # process script file -> replace style tags for i in range(len(styles)): styleName = styles[i][0].strip() style = styles[i][1].strip() scriptFile = scriptFile.replace("style = " + styleName,"style { " + style) # write script file MLABFileManager.writeStringToFile(modulePath + "/" + moduleName + ".script",scriptFile) # copy style data from stylePath styleDataPath = stylePath + "/data/" if MLABFileManager.exists(styleDataPath): files = MLABFileManager.recursiveContentOfDirectory(styleDataPath) for file in files: if MLABFileManager.isDir(styleDataPath + file): MLABFileManager.mkdir(modulePath + "/style/" + file) else: MLABFileManager.copy(styleDataPath + file,modulePath + "/style/" + file) else: print(styleDataPath) # write .mlab file for module # write .def and .script files for used modules mlabFile = "//MDL v1 utf8\n network {\n watchlist = \"\"\n}\n" for k,v in _slideModules.iteritems(): macro = k.strip() mlabFile += "module " + macro + " {\n fields {\n instanceName = " + macro + "\n }\n internalFields = \"\"\n}\n" defFile = "MacroModule " + macro + " { externalDefinition = $(LOCAL)/" + macro + ".script }" scriptFile = "Interface { Inputs {} Outputs {} Parameters {} } Commands { }" MLABFileManager.writeStringToFile(modulePath + "/macros/" + macro + ".def",defFile) MLABFileManager.writeStringToFile(modulePath + "/macros/" + macro + ".script",scriptFile) mlabFile += "connections = \"\"\n" MLABFileManager.writeStringToFile(modulePath + "/" + moduleName + ".mlab",mlabFile) # copy data, macros, networks, scripts, etc from contentPath files = MLABFileManager.recursiveContentOfDirectory(contentPath) extRE = re.compile('(\.dcm|\.tiff|\.tif|\.avi)') for file in files: if file == "slides": continue if file == "menu": continue if file == "config": continue if MLABFileManager.isDir(contentPath + "/" + file): if not MLABFileManager.exists(modulePath + "/" + file): MLABFileManager.mkdir(modulePath + "/" + file) else: if not MLABFileManager.exists(modulePath + "/" + file): MLABFileManager.copy(contentPath + "/" + file,modulePath + "/" + file) else: if not len(extRE.findall(file)): MLABFileManager.copy(contentPath + "/" + file,modulePath + "/" + file)
def buildPresentation(): #set style style = ctx.field("style").stringValue() # check/delete module path localPath = ctx.localPath() + "/" stylePath = localPath + "Styles/" + style contentPath = localPath + "Presentations/" + ctx.field( "presentation").stringValue() moduleName = ctx.field("module").stringValue() modulePath = localPath + "/Generated/" + moduleName # create directories MLABFileManager.mkdir(modulePath) MLABFileManager.mkdir(modulePath + "/style") MLABFileManager.mkdir(modulePath + "/data") MLABFileManager.mkdir(modulePath + "/macros") MLABFileManager.mkdir(modulePath + "/scripts") slashRe = re.compile('(.*?)[\/]{3,}', re.DOTALL) #parse style.script global _styleTags _styleTags = {} if MLABFileManager.exists(stylePath + "/tags"): tagRe = re.compile('\[(.*?)\]\s*=\s*(.*)', re.DOTALL) content = MLABFileManager.readStringFromFile(stylePath + "/tags") matches = slashRe.findall(content) for styleTag in matches: if styleTag == "": continue tagMatches = tagRe.findall(styleTag) _styleTags[tagMatches[0][0]] = tagMatches[0][1] # generate content: slides global _slideBullets _slideBullets = {} global _slideNames _slideNames = [] global _slideModules _slideModules = {} slides = "" if MLABFileManager.exists(contentPath + "/slides"): content = MLABFileManager.readStringFromFile(contentPath + "/slides") matches = slashRe.findall(content) count = 0 for slide in matches: if slide == "": continue result = parseSlide(slide, count + 1) if not result == "": count += 1 slides += result # generate content: menu menu = "" if MLABFileManager.exists(contentPath + "/menu"): content = MLABFileManager.readStringFromFile(contentPath + "/menu") menuRe = re.compile('(.*?)[\/]+', re.DOTALL) matches = menuRe.findall(content) for menuItem in matches: if menuItem == "": continue menu += parseMenuItem(menuItem) # generate def file defFile = "MacroModule " + moduleName + " { externalDefinition = $(LOCAL)/" + moduleName + ".script }" MLABFileManager.writeStringToFile(modulePath + "/" + moduleName + ".def", defFile) # copy py file MLABFileManager.copy(localPath + "template_py", modulePath + "/" + moduleName + ".py") # load styles styleFile = MLABFileManager.readStringFromFile(stylePath + "/style") styleRe = re.compile('DefineStyle\s*(.*?)\s*\{(.*?)[\/]{3,}', re.DOTALL) styles = styleRe.findall(styleFile) # process slideBullets for i in range(len(styles)): styleName = styles[i][0].strip() styleDef = styles[i][1].strip() for k, v in _slideBullets.iteritems(): for i in range(len(v)): _slideBullets[k][i] = _slideBullets[k][i].replace( "style = " + styleName, "style { " + styleDef) _slideBullets[k][i] = _slideBullets[k][i].replace("\n", " ") sources = "" # write slideBullets file if len(_slideBullets): bulletFile = "_bullets = {\n" num = 0 for k, v in _slideBullets.iteritems(): if num > 0: bulletFile += "," bulletFile += "'" + ` k ` + "':['" + "','".join(v) + "']\n" num = num + 1 bulletFile += "}" sources += "source = $(LOCAL)/scripts/bullets.py\n" MLABFileManager.writeStringToFile(modulePath + "/scripts/bullets.py", bulletFile) # process script file -> replace content tags scriptFile = MLABFileManager.readStringFromFile(stylePath + "/template") scriptFile = scriptFile.replace("template.py", moduleName + ".py") files = MLABFileManager.recursiveContentOfDirectory(contentPath + "/scripts") for file in files: if not file == "": sources += "source = $(LOCAL)/scripts/" + file.strip() + "\n" # print sources if not sources == "": scriptFile = scriptFile.replace("Commands {", "Commands {\n" + sources) scriptFile = scriptFile.replace( "[HEADER]", "\"" + ctx.field("header").stringValue() + "\"") scriptFile = scriptFile.replace( "[FOOTER]", "\"" + ctx.field("footer").stringValue() + "\"") scriptFile = scriptFile.replace("[MENU]", menu) scriptFile = scriptFile.replace("[SLIDES]", slides) # process script file -> replace style tags for i in range(len(styles)): styleName = styles[i][0].strip() style = styles[i][1].strip() scriptFile = scriptFile.replace("style = " + styleName, "style { " + style) # write script file MLABFileManager.writeStringToFile( modulePath + "/" + moduleName + ".script", scriptFile) # copy style data from stylePath styleDataPath = stylePath + "/data/" if MLABFileManager.exists(styleDataPath): files = MLABFileManager.recursiveContentOfDirectory(styleDataPath) for file in files: if MLABFileManager.isDir(styleDataPath + file): MLABFileManager.mkdir(modulePath + "/style/" + file) else: MLABFileManager.copy(styleDataPath + file, modulePath + "/style/" + file) else: print(styleDataPath) # write .mlab file for module # write .def and .script files for used modules mlabFile = "//MDL v1 utf8\n network {\n watchlist = \"\"\n}\n" for k, v in _slideModules.iteritems(): macro = k.strip() mlabFile += "module " + macro + " {\n fields {\n instanceName = " + macro + "\n }\n internalFields = \"\"\n}\n" defFile = "MacroModule " + macro + " { externalDefinition = $(LOCAL)/" + macro + ".script }" scriptFile = "Interface { Inputs {} Outputs {} Parameters {} } Commands { }" MLABFileManager.writeStringToFile( modulePath + "/macros/" + macro + ".def", defFile) MLABFileManager.writeStringToFile( modulePath + "/macros/" + macro + ".script", scriptFile) mlabFile += "connections = \"\"\n" MLABFileManager.writeStringToFile(modulePath + "/" + moduleName + ".mlab", mlabFile) # copy data, macros, networks, scripts, etc from contentPath files = MLABFileManager.recursiveContentOfDirectory(contentPath) extRE = re.compile('(\.dcm|\.tiff|\.tif|\.avi)') for file in files: if file == "slides": continue if file == "menu": continue if file == "config": continue if MLABFileManager.isDir(contentPath + "/" + file): if not MLABFileManager.exists(modulePath + "/" + file): MLABFileManager.mkdir(modulePath + "/" + file) else: if not MLABFileManager.exists(modulePath + "/" + file): MLABFileManager.copy(contentPath + "/" + file, modulePath + "/" + file) else: if not len(extRE.findall(file)): MLABFileManager.copy(contentPath + "/" + file, modulePath + "/" + file)