def renameFids(lfn): """First argument should be a command "rn", "col" or "sep". These functions were used in creation of the standard fiducials. rn calls "guessFiducialNames (from ccbcv align) to attempt to name the fiducial lines in a file as sagital, coronal, and transverse, rather than the arbitrary names they may have. col collects the fiducials in the list of arguments into a single "combined fiducials" file. sep opperates on a file generated by col (the argument list must have exactly one file name after the command sep), and splits this into files containing xhair, transverse, sagital, and coronal lines. """ import ccbcv.align as al import mien.parsers.nmpml as nmp if lfn[1] == "rn": for n in lfn[2:]: if "_renamed" in n: continue if "_fubar" in n: continue print (n) nn, ext = os.path.splitext(n) nn = nn + "_renamed" + ext doc = io.read(n) try: al.guessFiducialNames(doc) io.write(doc, nn) except: print ("failed") raise elif lfn[1] == "col": ndoc = nmp.blankDocument() for n in lfn[2:]: doc = io.read(n) els = [] for e in doc.elements: if e.name() in ["xhair", "transverse", "sagital", "coronal"]: ne = e.clone() snum = aname.match(n) nn = ne.name() + "_" + "_".join(snum.groups()) ne.setName(nn) ndoc.newElement(ne) io.write(ndoc, "combined_fiducials.nmpml") elif lfn[1] == "sep": cf = io.read(lfn[2]) for n in ["xhair", "transverse", "sagital", "coronal"]: els = [e for e in cf.elements if e.name().startswith(n)] ndoc = nmp.blankDocument() for e in els: ndoc.newElement(e) nn = n + "_fiducials.nmpml" io.write(ndoc, nn)
import re aname = re.compile("([SML])\.(\d+)\.10\.(\d+)") if sys.argv[1]=='rn': for n in sys.argv[2:]: if "_renamed" in n: continue if "_fubar" in n: continue print(n) nn, ext = os.path.splitext(n) nn = nn+"_renamed"+ext doc = io.read(n) try: al.guessFiducialNames(doc) io.write(doc, nn) except: print("failed") raise elif sys.argv[1]=="col": ndoc = nmp.blankDocument() for n in sys.argv[2:]: doc = io.read(n) els = [] for e in doc.elements: if e.name() in ["xhair", "transverse", "sagital", "coronal"]: ne = e.clone() snum = aname.match(n) nn = ne.name()+"_"+"_".join(snum.groups()) ne.setName(nn)