def synthesize(name=None, dir=None, mode=None): proc = "synthesize" args = srclib.arguments(name, dir, mode, proc) # get the arguments if not args: return 1 path = args[0] datain = args[1] dataout = args[2] rc = srclib.metadata(datain, path) # get the metadata if rc != 0: return rc msg = None rc = srclib.execute("iveset", path, mode, None) # execute iveset if rc != 0: msg = "Abnormal termination of iveset" else: if not os.path.exists(path + ".inp"): # get the data msg = "Missing " + path + ".inp file" else: f = open(path + ".inp", "r") cmd = f.read() f.close() spss.Submit(cmd) if not mode: os.remove(path + ".inp") rc = srclib.execute("impute", path, mode, None) # execute impute if rc != 0: msg = "Abnormal termination of synthesize" else: if dataout: # output the synthesized data rc = srclib.execute("putdata", path, mode, None) # execute putdata if rc != 0: msg = "Abnormal termination of putdata" else: if not os.path.exists(dataout + ".out"): # write the synthesized data msg = "Missing " + dataout + ".out file" else: f = open(dataout + ".out", "r") cmd = f.read() f.close() spss.Submit(cmd) # execute the command if not mode: os.remove(dataout + ".imp") os.remove(dataout + ".out") if msg: if os.path.exists(path + ".log"): # copy the log print f = open(path + ".log", "r") print f.read() f.close() print msg # print the error message else: if not os.path.exists(path + ".lst"): # copy the listing msg = "Missing " + path + ".lst file" else: print f = open(path + ".lst", "r") print f.read() f.close()
def bbdesign(name=None, dir=None, mode=None): proc = "bbdesign" args = srclib.arguments(name, dir, mode, proc) # get the arguments if not args: return 1 path = args[0] datain = args[1] dataout = args[2] rc = srclib.metadata(datain, path) # get the metadata if rc != 0: return rc msg = None args = [] # execute putdata args.append("/setup") rc = srclib.execute("bbdesign", path, mode, args) # execute bbdesign setup if rc != 0: msg = "Abnormal termination of bbdesign" else: if not os.path.exists(path + ".inp"): # get the data msg = "Missing " + path + ".inp file" else: f = open(path + ".inp", "r") cmd = f.read() f.close() spss.Submit(cmd) if not mode: os.remove(path + ".inp") rc = srclib.execute("bbdesign", path, mode, None) # execute bbdesign go if rc != 0: msg = "Abnormal termination of bbdesign" else: if os.path.exists(path + ".out"): # write the samples f = open(path + ".out", "r") cmd = f.read() f.close() spss.Submit(cmd) # execute the command if not mode: os.remove(path + ".out") if msg: if os.path.exists(path + ".log"): # copy the log print f = open(path + ".log", "r") print f.read() f.close() print msg # print the error message else: if not os.path.exists(path + ".lst"): # copy the listing msg = "Missing " + path + ".lst file" else: print f = open(path + ".lst", "r") print f.read() f.close()
def search(name=None, dir=None, mode=None): proc = "search" args = srclib.arguments(name, dir, mode, proc) # get the arguments if not args: return 1 path = args[0] datain = args[1] dataout = args[2] rc = srclib.metadata(datain, path) # get the metadata if rc != 0: return rc msg = None rc = srclib.execute("srchset", path, mode, None) # execute srchset if rc != 0: msg = "Abnormal termination of srchset" else: if not os.path.exists(path + ".inp"): # get the data msg = "Missing " + path + ".inp file" else: f = open(path + ".inp", "r") cmd = f.read() f.close() spss.Submit(cmd) if not mode: os.remove(path + ".inp") rc = srclib.execute("search", path, mode, None) # execute search if rc != 0: msg = "Abnormal termination of search" else: if dataout: srclib.residuals(datain, dataout, path, mode) # output the residuals if msg: if os.path.exists(path + ".log"): # copy the log print f = open(path + ".log", "r") print f.read() f.close() print msg # print the error message else: if not os.path.exists(path + ".lst"): # copy the listing msg = "Missing " + path + ".lst file" else: print f = open(path + ".lst", "r") print f.read() f.close()
def combine(name=None, dir=None, mode=None): proc = "combine" args = srclib.arguments(name, dir, mode, proc) # get the arguments if not args: return 1 path = args[0] datain = args[1] dataout = args[2] setup = args[3] var = None # variable list m = re.search(r"(^|;)\s*var[\w]*\s+([^;]+);", setup, flags=re.IGNORECASE) # supplied? if m: var = m.group(2).strip() var = var.strip() if not re.match(r"^[\w -]+$", var): # check it sys.Exit("Invalid variable list.") # invalid filenames = datain.strip() # build the file names list filelist = [] while filenames != "": # loop through file names m = re.match(r'^"([^"]+)"(\s+.*)?$', filenames) # double quotes if not m: m = re.match(r"^'([^']+)'(\s+.*)?$", filenames) # single quotes if not m: m = re.match(r"^([^\s]+)(\s+.*)?$", filenames) # no quotes if m.group(2): filenames = m.group(2).strip() # trim the remaining names else: filenames = "" file = m.group(1).strip() # extract the file name m = re.match(r"^[~\.\\/:\w -]+$", file) # check it if not m: sys.Exit("Invalid datain.") # invalid if not re.match(r"\.sav$", file, flags=re.IGNORECASE): # add .sav file += ".sav" filelist.append(file) # append it to the list #dataout if not re.match(r"\.sav$", dataout, flags=re.IGNORECASE): # add .sav dataout += ".sav" beg = 0 # merge the files count = 0 for file in filelist: # loop through the input files if count > 0: # not the first file cmd = "save outfile='{0}'.\n".format( dataout) # save the intermediate file spss.Submit(cmd) cmd = "get file='{0}'.\n".format(file) # get the input file spss.Submit(cmd) cmd = "compute OBS_ = $casenum.\n" # compute the observation number cmd += "compute OBS_ = OBS_ + {0}.\n".format(beg) cmd += "execute.\n" spss.Submit(cmd) beg += spss.GetCaseCount() # increment the case count if count > 0: # not the first file cmd = "match files file=* /file='{0}' /by OBS_.\n".format( dataout) # match the input and intermediate files spss.Submit(cmd) count += 1 if var: # variable list cmd = "save outfile='{0}' /keep={1}.\n".format( dataout, var) # subset variables and save else: cmd = "save outfile='{0}'.\n".format(dataout) # save the output file spss.Submit(cmd)
def regress(name=None, dir=None, mode=None): proc = "regress" args = srclib.arguments(name, dir, mode, proc) # get the arguments if not args: return 1 path = args[0] datain = args[1] dataout = args[2] rc = srclib.metadata(datain, path) # get the metadata if rc != 0: return rc msg = None rc = srclib.execute("iveset", path, mode, None) # execute iveset if rc != 0: msg = "Abnormal termination of iveset" else: if not os.path.exists(path + ".ctl"): # get the method msg = "Missing " + path + ".ctl file" else: f = open(path + ".ctl", "rb") method = struct.unpack('i', f.read(4))[0] f.close() if not os.path.exists(path + ".inp"): # get the data msg = "Missing " + path + ".inp file" else: f = open(path + ".inp", "r") cmd = f.read() f.close() spss.Submit(cmd) if not mode: os.remove(path + ".inp") if method & 1: # imputation rc = srclib.execute("impute", path, mode, None) # execute impute if rc != 0: msg = "Abnormal termination of impute" else: if dataout: # output the imputed data rc = srclib.execute("putdata", path, mode, None) # execute putdata if rc != 0: msg = "Abnormal termination of putdata" else: if not os.path.exists( dataout + ".out"): # write the imputed data msg = "Missing " + dataout + ".out file" else: f = open(dataout + ".out", "r") cmd = f.read() f.close() spss.Submit(cmd) # execute the command if not mode: os.remove(dataout + ".imp") os.remove(dataout + ".out") if not msg: rc = srclib.execute("setdata", path, mode, None) # execute setdata if rc != 0: msg = "Abnormal termination of setdata" else: # no imputation if os.path.exists(path + ".agg"): # remove the .agg file os.remove(path + ".agg") if not msg: rc = srclib.execute("regress", path, mode, None) # execute regress if rc != 0: msg = "Abnormal termination of regress" else: if os.path.exists(path + ".out"): # write the estimates, etc. f = open(path + ".out", "r") cmd = f.read() f.close() spss.Submit(cmd) # execute the command if not mode: os.remove(path + ".out") if msg: if os.path.exists(path + ".log"): # copy the log print f = open(path + ".log", "r") print f.read() f.close() print msg # print the error message else: if not os.path.exists(path + ".lst"): # copy the listing msg = "Missing " + path + ".lst file" else: print f = open(path + ".lst", "r") print f.read() f.close()