def genData(self):
        """Generate variables holding all the dichotomies for values"""
        
        valcount = len(self.vvalues)
        computes = []
        # The VALUE function only works for numeric variables :-(
        # In ANY, all string values are considered valid.
        if self.string:
            setvars = ",".join(self.setvars)
        else:
            setvars = ",".join(["VALUE(%s)" % v for v in self.setvars])
        values = sorted(self.vvalues)
        varprefix = self.varprefix
        
        # if any generated variables already exist, they will be overwritten.
        # if they exist and are strings, the procedure will fail.
        for v in range(valcount):
            v1 = v + 1
            vname = "%(varprefix)s_%(v1)02d" % locals()
            self.generatednames.append(vname)
            val = values[v]
            vallabel = self.valuelabels.get(val, val)   # try to pick up a value label
            self.generatedvalues.append(val)
            self.generatedlabels.append(vallabel)
            if self.string:
                val = spssaux._smartquote("%s" % val)
            cmd = """COMPUTE %(vname)s = any(%(val)s, %(setvars)s).
VARIABLE LABEL %(vname)s %(vallabel)s.
VARIABLE LEVEL %(vname)s (NOMINAL).""" % locals()
            computes.append(cmd)
        spss.Submit(computes)
def savetext(filename, filemode):
    """Save the current dataset as a text file
    
    filename is the path for the file
    filemode is overwrite or append
    
    If filename is None, this is a no-op"""

    if filename is None:
        return
    fm = filemode == "overwrite" and "wb" or "ab"

    opened = False
    with spssdata.Spssdata(names=False) as curs:
        for case in curs:
            if not opened:
                opened = True
                f = open(filename, fm)
                nvar = len(case)
            lcase = []
            for item in case:
                if item is None:
                    lcase.append("")
                elif isinstance(item, basestring):
                    lcase.append(spssaux._smartquote(item.rstrip()))
                else:
                    lcase.append(str(item))
            lcase.append("\n")
            case = ",".join(lcase)
            f.write(case)
        f.close()
Пример #3
0
    def genData(self):
        """Generate variables holding all the dichotomies for values"""

        valcount = len(self.vvalues)
        computes = []
        # The VALUE function only works for numeric variables :-(
        # In ANY, all string values are considered valid.
        if self.string:
            setvars = ",".join(self.setvars)
        else:
            setvars = ",".join(["VALUE(%s)" % v for v in self.setvars])
        values = sorted(self.vvalues)
        varprefix = self.varprefix

        # if any generated variables already exist, they will be overwritten.
        # if they exist and are strings, the procedure will fail.
        for v in range(valcount):
            v1 = v + 1
            vname = "%(varprefix)s_%(v1)02d" % locals()
            self.generatednames.append(vname)
            val = values[v]
            vallabel = self.valuelabels.get(
                val, val)  # try to pick up a value label
            self.generatedvalues.append(val)
            self.generatedlabels.append(vallabel)
            if self.string:
                val = spssaux._smartquote("%s" % val)
            cmd = """COMPUTE %(vname)s = any(%(val)s, %(setvars)s).
VARIABLE LABEL %(vname)s %(vallabel)s.
VARIABLE LEVEL %(vname)s (NOMINAL).""" % locals()
            computes.append(cmd)
        spss.Submit(computes)
Пример #4
0
 def makevls(self, varname, vlinfo):
     """Return value label syntax
     
     varname is the variable to which the syntax applies
     vlinfo is the set of duples of (value, label)"""
     
     isstring = self.vardict[varname].VariableType > 0
     vls = []
     for value, label in sorted(vlinfo):
         if isstring:
             value = spssaux._smartquote(value)
         else:
             if value == int(value):
                 value = int(value)
         label = spssaux._smartquote(label)
         vls.append("%s %s" % (value, label))
     return "VALUE LABELS " + varname + "\n   " + "\n   ".join(vls) + "."
 def makevls(self, varname, vlinfo):
     """Return value label syntax
     
     varname is the variable to which the syntax applies
     vlinfo is the set of duples of (value, label)"""
     
     isstring = self.vardict[varname].VariableType > 0
     vls = []
     for value, label in sorted(vlinfo):
         if isstring:
             value = spssaux._smartquote(value)
         else:
             if value == int(value):
                 value = int(value)
         label = spssaux._smartquote(label)
         vls.append("%s %s" % (value, label))
     return "VALUE LABELS " + varname + "\n   " + "\n   ".join(vls) + "."
def getrules(rulesfilespec, varname, labels, varlabel):
    """Return a list of rules written by TREES CHAID
    
    rulesfilespec is the file containing the rules
    It should contain rules like this...
/* Node 2 */.
DO IF (SYSMIS(prevexp) OR (VALUE(prevexp) GT 5  AND  VALUE(prevexp) LE 261)).
COMPUTE nod_001 = 2.
COMPUTE pre_001 = 36410.7804232804.
END IF.
EXECUTE.

    varname is the name for the output variable
    labels is a list of labels for the values"""
    
    # rule file is written in the current Statistics encoding mode
    # but does not have a BOM
    # Try to passively ignore this.
    
    f = open(rulesfilespec, "r")
    lines = []   # accumulates generated syntax
    continuation = False   # indicates whether on a continuation line in the rules file
    code = 0   # holds the new values
    for line in f.readlines():
        line = line[:-1]  # strip trailing newline
        if line.startswith("DO IF") or continuation:
            lines.append(line)
            if not line.endswith("."):
                continuation = True
            else:
                continuation = False
        if line.startswith("END IF"):
            lines.append("""COMPUTE %(varname)s = %(code)s.""" % locals())
            code += 1
            lines.append(line)   # the END IF, which
    f.close()
    labelspec = "\n".join([str(i) + " " + spssaux._smartquote(val) for i, val in enumerate(labels)])
    if varlabel:
        varlabel = spssaux._smartquote(varlabel)
        lines.append("""VARIABLE LABEL %(varname)s %(varlabel)s.""" % locals())
    lines.append("""VALUE LABELS %(varname)s""" % locals())
    lines.append(labelspec)
    lines.append(".")
    return lines
def makehandle(dir, handle):
    """Create a file handle
    
    dir is the path for the handle
    handle is the handle name"""
    
    # Note: with V18 this could be done with an api.  Submit used instead
    # to support V17
    
    dir = spssaux._smartquote(dir)
    spss.Submit("""FILE HANDLE %(handle)s /NAME=%(dir)s.""" % locals())
def runscript(scriptname, params={}):
    """Construct a parameter dictionary and run a Python script.
    
    scriptname is the path to run.
    params is a Python dictionary of parameter names and values.
    
    The total size of the parameter dictionary is limited to 4K (after pickling).
    
    This function returns a dictionary of values set by the script via setreturnvalue.
    If the script sets no return value, the result is an empty dictionary."""
    
    fnparams = tempfile.gettempdir() + os.sep + "__SCRIPT__"
    fnreturn = tempfile.gettempdir() + os.sep + "__SCRIPTRETURN__"
    f = file(fnparams, "w+")
    # ensure file size is 4096 for *nix os's.
    f.write(1024*"0000")
    f.flush()

    shmem = mmap.mmap(f.fileno(), 4096, access=mmap.ACCESS_WRITE)
    shmem.write(pickle.dumps(params))
    f.close()
    try:
        os.remove(fnreturn)   # ensure that no stale returns file exists
    except:
        pass
    ###import wingdbstub
    spss.Submit("SCRIPT " + spssaux._smartquote(scriptname))
    shmem.close()
    
    # The _SYNC command is required in order to ensure that the script has completed
    spss.Submit("_SYNC")
    
    # The parameter file will be removed by the script if it calls getscriptparam, but
    # the following code will clean up in case the script doesn't make that call.
    try:
        os.remove(fnparams)
    except:
        pass

    # get the return value, if any
    ###import wingdbstub
    try:
        f = file(fnreturn, "r")
        shmem = mmap.mmap(f.fileno(), 4096, access=mmap.ACCESS_READ)
        ret = pickle.loads(shmem.read(4096))
        shmem.close()
        f.close()
        os.remove(fnreturn)
    except:
        ret = {}
    return ret
Пример #9
0
def runscript(scriptname, params={}):
    """Construct a parameter dictionary and run a Python script.
    
    scriptname is the path to run.
    params is a Python dictionary of parameter names and values.
    
    The total size of the parameter dictionary is limited to 4K (after pickling).
    
    This function returns a dictionary of values set by the script via setreturnvalue.
    If the script sets no return value, the result is an empty dictionary."""

    fnparams = tempfile.gettempdir() + os.sep + "__SCRIPT__"
    fnreturn = tempfile.gettempdir() + os.sep + "__SCRIPTRETURN__"
    f = open(fnparams, "w+")
    # ensure file size is 4096 for *nix os's.
    f.write(1024 * "0000")
    f.flush()

    shmem = mmap.mmap(f.fileno(), 4096, access=mmap.ACCESS_WRITE)
    shmem.write(pickle.dumps(params))
    f.close()
    try:
        os.remove(fnreturn)  # ensure that no stale returns file exists
    except:
        pass
    ###import wingdbstub
    spss.Submit("SCRIPT " + spssaux._smartquote(scriptname))
    shmem.close()

    # The _SYNC command is required in order to ensure that the script has completed
    spss.Submit("_SYNC")

    # The parameter file will be removed by the script if it calls getscriptparam, but
    # the following code will clean up in case the script doesn't make that call.
    try:
        os.remove(fnparams)
    except:
        pass

    # get the return value, if any
    ###import wingdbstub
    try:
        f = open(fnreturn, "r")
        shmem = mmap.mmap(f.fileno(), 4096, access=mmap.ACCESS_READ)
        ret = pickle.loads(shmem.read(4096))
        shmem.close()
        f.close()
        os.remove(fnreturn)
    except:
        ret = {}
    return ret
    def __init__(self, mrsetinfo, mcset, vvalues, setname, varprefix, valuelabels):
        """mrsetinfo is the structure returned by the dataset multiResposneSet api
        Set names are always in upper case
        setname is the name for the output set
        varprefix is the prefix for the generated variables
"""
        
        attributesFromDict(locals())
        if self.mrsetinfo.getVarType(mcset) == "Numeric":
            self.valuestr = ",".join([str(item) for item in vvalues])
            self.string = False
        else:
            self.valuestr = ",".join([spssaux._smartquote(item.rstrip()) for item in vvalues])
            self.string = True
        self.setvars = self.mrsetinfo.getSetVars(mcset)
        self.generatednames = []
        self.generatedlabels = []
        self.generatedvalues = []
Пример #11
0
    def __init__(self, mrsetinfo, mcset, vvalues, setname, varprefix,
                 valuelabels):
        """mrsetinfo is the structure returned by the dataset multiResposneSet api
        Set names are always in upper case
        setname is the name for the output set
        varprefix is the prefix for the generated variables
"""

        attributesFromDict(locals())
        if self.mrsetinfo.getVarType(mcset) == "Numeric":
            self.valuestr = ",".join([str(item) for item in vvalues])
            self.string = False
        else:
            self.valuestr = ",".join(
                [spssaux._smartquote(item.rstrip()) for item in vvalues])
            self.string = True
        self.setvars = self.mrsetinfo.getSetVars(mcset)
        self.generatednames = []
        self.generatedlabels = []
        self.generatedvalues = []
    def setgen(self, outputname, inputsetname):
        """construct a new MR set of the appropriate type"""
        
        # remove special values from count list and reinsert at appropriate point
        # construct new set definition
        if self.specials is None:
            self.specials = []
        for v in self.specials:
            if v in self.thismrset:
                del(self.thismrset[v])
        setvalues = sorted([(value, key) for (key, value) in self.thismrset.iteritems()], reverse = self.order == 'd')
        for j in range(len(setvalues)):
            if setvalues[j][0] == 0 and not self.other:
                if self.specialsorder == "after":
                    setvalues = setvalues[:j] + [(None, v) for v in self.specials] + setvalues[j:]
                else:
                    setvalues = [(None, v) for v in self.specials] + setvalues[:j] + setvalues[j:]
                break
        else:
            if self.specialsorder == "after":
                setvalues.extend([(None, v) for v in self.specials])
            else:
                setvalues = [(None, v) for v in self.specials] + setvalues
        # generate set
        cmd = """MRSETS /MDGROUP NAME=%(outputname)s LABEL="%(label)s" 
        CATEGORYLABELS=%(categorylabels)s
VARIABLES = %(variables)s VALUE=%(value)s
/DISPLAY NAME=[%(outputname)s]"""
        if not outputname.startswith("$"):
            outputname = "$" + outputname
        inputsetname = inputsetname.upper()
        label = self.mrsetinfo[inputsetname][0]
        variables = " ".join([item[1] for item in setvalues])
        value = self.mrsetinfo[inputsetname][2]
        if self.mrsetinfo[inputsetname][3] == "String":
            value = spssaux._smartquote(value)
        categorylabels = self.categorylabels
        spss.Submit(cmd % locals())
Пример #13
0
def weightedkappaextension(variables, wttype=1, cilevel=95):

    varnames = expandvarnames(variables)
    caption = varnames[0] + _(" vs. ") + varnames[1]
    vardict = spssaux.VariableDict(varnames)
    if len(vardict) != len(varnames):
        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel, )] = CellText.String(
            _("""An invalid variable has been specified. This command is not executed."""
              ))
        spss.EndProcedure()
    elif len(varnames) != 2:
        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel, )] = CellText.String(
            _("""Exactly two variables must be specified. This command is not executed."""
              ))
        spss.EndProcedure()
    else:
        try:
            warntext = []
            if cilevel < 50:
                warntext.append(
                    _("CILEVEL cannot be less than 50%. It has been set to 50%."
                      ))
                cilevel = 50
            if cilevel > 99.999:
                warntext.append(
                    _("CILEVEL cannot be greater than 99.999%. It has been set to 99.999%."
                      ))
                cilevel = 99.999
            if cilevel == int(cilevel):
                cilevel = int(cilevel)
            if wttype != 1:
                if wttype != 2:
                    warntext.append(
                        _("WTTYPE must be 1 or 2. It has been set to 1."))
                    wttype = 1
            varlist = varnames[0] + ' ' + varnames[1]
            spss.Submit("PRESERVE.")
            tempdir = tempfile.gettempdir()
            spss.Submit("""CD "%s".""" % tempdir)
            wtvar = spss.GetWeightVar()
            if wtvar != None:
                spss.Submit(r"""
COMPUTE %s=RND(%s).""" % (wtvar, wtvar))
                spss.Submit(r"""
EXECUTE.""")
            maxloops = 2 * spss.GetCaseCount()
            spss.Submit("""SET PRINTBACK=OFF MPRINT=OFF MXLOOPS=%s.""" %
                        maxloops)
            activeds = spss.ActiveDataset()
            if activeds == "*":
                activeds = "D" + str(random.uniform(.1, 1))
                spss.Submit("DATASET NAME %s" % activeds)
            tmpvar1 = "V" + str(random.uniform(.1, 1))
            tmpvar2 = "V" + str(random.uniform(.1, 1))
            tmpvar3 = "V" + str(random.uniform(.1, 1))
            tmpvar4 = "V" + str(random.uniform(.1, 1))
            tmpvar5 = "V" + str(random.uniform(.1, 1))
            tmpvar6 = "V" + str(random.uniform(.1, 1))
            tmpdata1 = "D" + str(random.uniform(.1, 1))
            tmpdata2 = "D" + str(random.uniform(.1, 1))
            omstag1 = "T" + str(random.uniform(.1, 1))
            omstag2 = "T" + str(random.uniform(.1, 1))
            omstag3 = "T" + str(random.uniform(.1, 1))
            omstag4 = "T" + str(random.uniform(.1, 1))
            omstag5 = "T" + str(random.uniform(.1, 1))
            omstag6 = "T" + str(random.uniform(.1, 1))
            tmpfile1 = "F" + str(random.uniform(.1, 1))
            tmpfile2 = "F" + str(random.uniform(.1, 1))
            lowlabel = _("""Lower %s%% Asymptotic CI Bound""") % cilevel
            upplabel = _("""Upper %s%% Asymptotic CI Bound""") % cilevel
            spss.Submit(r"""
DATASET COPY %s WINDOW=HIDDEN.""" % tmpdata1)
            spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata1)
            filt = spssaux.GetSHOW("FILTER", olang="english")
            if filt != "No case filter is in effect":
                filtcond = filt.strip("(FILTER)")
                select = "SELECT IF " + str(filtcond) + "."
                spss.Submit("""%s""" % select)
                spss.Submit("""EXECUTE.""")
                spss.Submit("""USE ALL.""")
            banana = spssaux.getDatasetInfo(Info="SplitFile")
            if banana != "":
                warntext.append(_("This procedure ignores split file status."))
                spss.Submit(r"""SPLIT FILE OFF.""")
            spss.Submit(r"""
COUNT %s=%s (MISSING).""" % (tmpvar1, varlist))
            spss.Submit(r"""
SELECT IF %s=0.""" % tmpvar1)
            spss.Submit(r"""
EXECUTE.""")
            validn = spss.GetCaseCount()
            if validn < 2:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
                spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                table = spss.BasePivotTable("Warnings ", "Warnings")
                table.Append(spss.Dimension.Place.row,
                             "rowdim",
                             hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel, )] = CellText.String(
                    _("""There are too few complete cases. This command is not executed."""
                      ))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
            else:
                spss.Submit(r"""
AGGREGATE
   /OUTFILE=* MODE=ADDVARIABLES
   /%s=SD(%s)
   /%s=SD(%s).""" % (tmpvar2, varnames[0], tmpvar3, varnames[1]))
                try:
                    cur = spss.Cursor(isBinary=False)
                except:
                    cur = spss.Cursor()
                datarow = cur.fetchone()
                cur.close()
                sd1 = datarow[-2]
                sd2 = datarow[-1]
                if min(sd1, sd2) == 0:
                    spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
                    spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                    table = spss.BasePivotTable("Warnings ", "Warnings")
                    table.Append(spss.Dimension.Place.row,
                                 "rowdim",
                                 hideLabels=True)
                    rowLabel = CellText.String("1")
                    table[(rowLabel, )] = CellText.String(
                        _("""All ratings are the same for at least one rater. This command is not executed."""
                          ))
                    spss.EndProcedure()
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
                else:
                    if len(warntext) > 0:
                        spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
                        if len(warntext) == 1:
                            spss.StartProcedure(_("Weighted Kappa"),
                                                "Weighted Kappa")
                            table = spss.BasePivotTable(
                                "Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row,
                                         "rowdim",
                                         hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel, )] = CellText.String("%s" %
                                                                  warntext[0])
                            spss.EndProcedure()
                        if len(warntext) == 2:
                            spss.StartProcedure(_("Weighted Kappa"),
                                                "Weighted Kappa")
                            table = spss.BasePivotTable(
                                "Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row,
                                         "rowdim",
                                         hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel, )] = CellText.String(
                                "%s \n"
                                "%s" % (warntext[0], warntext[1]))
                            spss.EndProcedure()
                        if len(warntext) == 3:
                            spss.StartProcedure(_("Weighted Kappa"),
                                                "Weighted Kappa")
                            table = spss.BasePivotTable(
                                "Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row,
                                         "rowdim",
                                         hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel, )] = CellText.String(
                                "%s \n"
                                "%s \n"
                                "%s" % (warntext[0], warntext[1], warntext[2]))
                            spss.EndProcedure()
                        spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
                    spss.Submit(r"""
DELETE VARIABLES %s %s.""" % (tmpvar2, tmpvar3))
                    spss.Submit(r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N.""" % (tmpfile1, varlist, tmpvar4))
                    spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Variables to Cases'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                    spss.Submit(r"""
VARSTOCASES
  /MAKE %s FROM %s.""" % (tmpvar5, varlist))
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
                    catdata = []
                    try:
                        cur = spss.Cursor(isBinary=False)
                    except:
                        cur = spss.Cursor()
                    while True:
                        datarow = cur.fetchone()
                        if datarow is None:
                            break
                        catdata.append(datarow[-1])
                    cur.close()
                    cats = list(set(catdata))
                    cattest = 0
                    if any(item != round(item) for item in cats):
                        cattest = 1
                        spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
                        spss.StartProcedure(_("Weighted Kappa"),
                                            "Weighted Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row,
                                     "rowdim",
                                     hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel, )] = CellText.String(
                            _("""Some ratings are not integers. This command is not executed."""
                              ))
                        spss.EndProcedure()
                        spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
                    elif min(cats) < 1.0:
                        spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
                        spss.StartProcedure(_("Weighted Kappa"),
                                            "Weighted Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row,
                                     "rowdim",
                                     hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel, )] = CellText.String(
                            _("""Some ratings are less than 1. This command is not executed."""
                              ))
                        spss.EndProcedure()
                        spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
                    else:
                        spss.Submit(r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N.""" % (tmpfile2, tmpvar5, tmpvar6))
                        spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN""" % tmpdata2)
                        spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Matrix'] 
 /DESTINATION VIEWER=NO
 /TAG='"%s"'.""" % omstag3)
                        spss.Submit(r"""
MATRIX.
GET x 
  /FILE=%s
  /VARIABLES=%s %s.
GET ratecats
  /FILE=%s
  /VARIABLES=%s.
COMPUTE size=MMAX(ratecats).
COMPUTE y=MAKE(size,size,0).
LOOP i=1 to NROW(y).
+ LOOP j=1 to NCOL(y).
+   LOOP k=1 to NROW(x).
+     DO IF (x(k,1)=i and x(k,2)=j).
+       COMPUTE y(i,j)=x(k,3).
+     END IF.
+   END LOOP.
+ END LOOP.
END LOOP.
COMPUTE wttype=%s.
COMPUTE wt=MAKE(NROW(y),NCOL(y),0).
LOOP i=1 to NROW(y).
+ LOOP j=1 to NCOL(y).
+   DO IF wttype=1.
+     COMPUTE wt(i,j)=1-(ABS(i-j)/(size-1)).
+   ELSE IF wttype=2.
+     COMPUTE wt(i,j)=1-((i-j)/(NROW(y)-1))**2.
+   END IF.
+ END LOOP.
END LOOP.
COMPUTE n=MSUM(y).
COMPUTE prop=y/n.
COMPUTE p_i=RSUM(prop).
COMPUTE p_j=CSUM(prop).
COMPUTE w_i=(wt*T(p_j))*MAKE(1,size,1).
COMPUTE w_j=MAKE(size,1,1)*(T(p_i)*wt).
COMPUTE po=MSUM(wt&*prop).
COMPUTE pe=MSUM(MDIAG(p_i)*wt*MDIAG(p_j)).
COMPUTE kstat=(po-pe)/(1-pe).
COMPUTE var0=(T(p_i)*((wt-(w_i+w_j))&**2)*T(p_j)-pe**2)/(n*(1-pe)**2).
DO IF var0>=0.
+ COMPUTE ase0=SQRT(var0).
ELSE.
+ COMPUTE ase0=-1.
END IF.
DO IF ase0>0.
+ COMPUTE z=kstat/ase0.
+ COMPUTE sig=1-CHICDF(z**2,1).
ELSE.
+ COMPUTE z=-1.
+ COMPUTE sig=-1.
END IF.
COMPUTE var1=(MSUM((prop&*((wt-(w_i+w_j)&*(1-kstat))&**2)))-(kstat-pe*(1-kstat))**2)/(n*(1-pe)**2).
DO IF var1>=0.
+ COMPUTE ase1=SQRT(var1).
ELSE.
+ COMPUTE ase1=-1.
END IF.
SAVE {wttype,kstat,ase1,z,sig,ase0}
   /OUTFILE=%s
   /VARIABLES=wttype,kstat,ase1,z,sig,ase0.
END MATRIX.""" % (tmpfile1, varlist, tmpvar4, tmpfile2, tmpvar5, wttype,
                        tmpdata2))
                        spss.Submit(r"""
OMSEND TAG=['"%s"'].""" % omstag3)
                        spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata2)
                        spss.Submit(r"""
DO IF ase0=-1.
+ RECODE z sig (-1=SYSMIS).
END IF.
EXECUTE.
DELETE VARIABLES ase0.
RECODE ase1 (-1=SYSMIS).
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase1.""" % cilevel)
                        spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase1.""" % cilevel)
                        spss.Submit(r"""
FORMATS kstat ase1 z sig lower upper (F11.3).
VARIABLE LABELS kstat %s.""" % _smartquote(_("""Kappa""")))
                        spss.Submit(r"""
VARIABLE LABELS ase1 %s.""" % _smartquote(_("""Asymptotic Standard Error""")))
                        spss.Submit(r"""
VARIABLE LABELS z %s.""" % _smartquote(_("""Z""")))
                        spss.Submit(r"""
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                        spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                        spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                        if wttype == 1:
                            spss.Submit(r"""
VARIABLE LABELS wttype %s.""" % _smartquote(_("""Linear""")))
                        if wttype == 2:
                            spss.Submit(r"""
VARIABLE LABELS wttype %s.""" % _smartquote(_("""Quadratic""")))
                        spss.Submit(r"""
EXECUTE.
""")
                        spss.Submit(r"""
OMS
  /SELECT TABLES 
  /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
  /DESTINATION VIEWER=NO
  /TAG = '"%s"'.""" % omstag4)
                        spss.Submit(r"""
OMS
  /SELECT TEXTS
  /IF COMMANDS=['Weighted Kappa'] LABELS=['Active Dataset']
  /DESTINATION VIEWER=NO
  /TAG = '"%s"'.""" % omstag5)
                        if len(warntext) > 0:
                            spss.Submit(r"""
OMS
 /SELECT HEADINGS
 /IF COMMANDS=['Weighted Kappa']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag6)
                        try:
                            cur = spss.Cursor(isBinary=False)
                        except:
                            cur = spss.Cursor()
                        data = cur.fetchone()
                        cur.close()
                        spss.StartProcedure(_("Weighted Kappa"),
                                            "Weighted Kappa")
                        table = spss.BasePivotTable(_("Weighted Kappa"),
                                                    "Kappa",
                                                    caption=caption)
                        table.SimplePivotTable(rowdim = _("Weighting"),
                           rowlabels = [CellText.String(spss.GetVariableLabel(0))],
                           coldim = "",
                           collabels = [spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3),spss.GetVariableLabel(4), \
                                             spss.GetVariableLabel(5),spss.GetVariableLabel(6)],
                           cells = [data[1],data[2],data[3],data[4],data[5],data[6]])
                        spss.EndProcedure()
                        if len(warntext) > 0:
                            spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag6)
        finally:
            try:
                spss.Submit(r"""
DATASET CLOSE %s.""" % tmpdata1)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % activeds)
                if validn >= 2:
                    if min(sd1, sd2) > 0:
                        if cattest == 0:
                            if min(cats) >= 1:
                                spss.Submit(r"""
OMSEND TAG=['"%s"' '"%s"'].""" % (omstag4, omstag5))
                                spss.Submit(r"""
DATASET CLOSE %s.""" % tmpdata2)
                                spss.Submit(r"""
ERASE FILE=%s.""" % tmpfile2)
                        spss.Submit(r"""
ERASE FILE=%s.""" % tmpfile1)
            except:
                pass
            spss.Submit(r"""
RESTORE.
""")
def recode(varlist, recodes, stringsize=None, makevaluelabels=True, copyvariablelabels=True, useinputvallabels=False,
    suffix="", prefix=""):

    vardict = spssaux.VariableDict(caseless=True)
    isutf8 = spss.PyInvokeSpss.IsUTF8mode()
    ecutf8 = codecs.getencoder("utf_8")
    inputlist, outputlist, vartype = parsevarlist(varlist, vardict)
    if len(recodes) > 1:
        raise ValueError(_("The RECODES subcommand must consist of a single, quoted specification"))
    # recodespec is a list of textual recode syntax, one item per value set
    # vldefs is a dictionary with keys the target values
    # and values the input codes
    # inputdict is a dictionary with keys the target values 
    # and values a list of the input codes
    recodespec, vldefs, inputdict = parserecodes(recodes[0], vartype, stringsize)
    valuelabelmessage = checklabelconsistency(inputlist, vardict)
    
    if stringsize:
        alter = []
        create = []
        for v in outputlist:
            try:
                if vardict[v].VariableType != stringsize:
                    alter.append(v)
            except:
                create.append(v)
        if create:
            spss.Submit("STRING %s (A%s)." % (" ".join(create), stringsize))
        if alter:
            spss.Submit("ALTER TYPE %s (A%s)" % (" ".join(alter), stringsize))
                
    spss.Submit("""RECODE %s %s INTO %s.""" % (" ".join(inputlist), " ".join(recodespec), " ".join(outputlist)))
    
    # generate variable labels if requested
    if copyvariablelabels:
        if prefix and not prefix.endswith(" "):
            prefix = prefix + " "
        if suffix and not suffix.startswith(" "):
            suffix = " " + suffix
        for vin, vout in zip(inputlist, outputlist):
            spss.Submit("""VARIABLE LABEL %s %s.""" % \
                (vout, _smartquote(prefix + vardict[vin].VariableLabel + suffix, True)))
            
    # generate value labels if requested
    # all values for given target are merged but else clause is omitted
    # VALUE LABELS syntax quotes values regardless of variable type
    # vldefs is a dictionary with keys of the output values and
    # values a string listing the input values.  If copying value labels
    # the first input variable is used as the source.
    if makevaluelabels:
        if useinputvallabels:
            vldefs = makevallabels(vldefs, inputdict, 
                vardict[inputlist[0]].ValueLabels, isutf8, ecutf8)
            
        # ensure that copy as target does not generate a value label
        copyset = set()
        for target in vldefs:
            if target.lower() == "copy":
                copyset.add(target)
        for c in copyset:
            del(vldefs[c])
        
        #spss.Submit(r"""VALUE LABELS %s %s.""" % (" ".join(outputlist), \
            #" ".join([_smartquote(val, vartype == 2) + " " + _smartquote(label, True) for val, label in vldefs.items()])))
        
        spss.Submit(r"""VALUE LABELS %s %s.""" % (" ".join(outputlist), \
            " ".join([val + " " + _smartquote(label, True) for val, label in vldefs.items()])))
    if valuelabelmessage:
        print valuelabelmessage
def fleisskappaextension(variables, cilevel=95):
    
    varnames = expandvarnames(variables)
    vardict = spssaux.VariableDict(varnames)
    if len(vardict) != len(varnames):
        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
        table = spss.BasePivotTable("Warnings ","Warnings")
        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(_("""An invalid variable has been specified. This command is not executed."""))
        spss.EndProcedure()
    elif len(varnames) < 2:
        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
        table = spss.BasePivotTable("Warnings ","Warnings")
        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(_("""At least two variables must be specified. This command is not executed."""))
        spss.EndProcedure()
        
    else:
        try:
            warntext = []
            if cilevel < 50:
                warntext.append(_("CILEVEL cannot be less than 50%. It has been reset to 50%."))
                cilevel = 50
            if cilevel > 99.999:
                warntext.append(_("CILEVEL cannot be greater than 99.999%. It has been reset to 99.999%."))
                cilevel = 99.999
            if cilevel == int(cilevel):
                cilevel = int(cilevel)
            varlist = varnames[0]
            for i in range(1,len(varnames)):
                varlist = varlist + ' ' + varnames[i]
            spss.Submit("PRESERVE.")
            tempdir = tempfile.gettempdir()
            spss.Submit("""CD "%s".""" % tempdir)
            wtvar = spss.GetWeightVar()
            if wtvar != None:
                spss.Submit(r"""
COMPUTE %s=RND(%s).""" % (wtvar, wtvar))
                spss.Submit(r"""
EXECUTE.""")
                wtdn = GetWeightSum(varnames)
            else:
                wtdn = spss.GetCaseCount()
            maxloops = wtdn + 1
            spss.Submit("""SET PRINTBACK=OFF MPRINT=OFF OATTRS=ENG MXLOOPS=%s.""" % maxloops)
            activeds = spss.ActiveDataset()
            if activeds == "*":
                activeds = "D" + str(random.uniform(.1,1))
                spss.Submit("DATASET NAME %s" % activeds)
            tmpvar1 = "V" + str(random.uniform(.1,1))
            tmpvar2 = "V" + str(random.uniform(.1,1))
            tmpvar3 = "V" + str(random.uniform(.1,1))
            tmpfile1 = "F" + str(random.uniform(.1,1))
            tmpfile2 = "F" + str(random.uniform(.1,1))
            tmpdata1 = "D" + str(random.uniform(.1,1))
            tmpdata2 = "D" + str(random.uniform(.1,1))
            tmpdata3 = "D" + str(random.uniform(.1,1))
            omstag1 = "T" + str(random.uniform(.1,1))
            omstag2 = "T" + str(random.uniform(.1,1))
            omstag3 = "T" + str(random.uniform(.1,1))
            omstag4 = "T" + str(random.uniform(.1,1))
            omstag5 = "T" + str(random.uniform(.1,1))
            omstag6 = "T" + str(random.uniform(.1,1))
            lowlabel = _("""Lower %s%% Asymptotic CI Bound""") % cilevel
            upplabel = _("""Upper %s%% Asymptotic CI Bound""") % cilevel
            spss.Submit(r"""
DATASET COPY %s WINDOW=HIDDEN.""" % tmpdata1)
            spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata1)
            filt = spssaux.GetSHOW("FILTER", olang="english")
            if filt != "No case filter is in effect":
                filtcond = filt.strip("(FILTER)")
                select = "SELECT IF " + str(filtcond) + "."
                spss.Submit("""%s""" % select)
                spss.Submit("""EXECUTE.""")
                spss.Submit("""USE ALL.""")
            banana = spssaux.getDatasetInfo(Info="SplitFile")
            if banana != "":
                warntext.append(_("This command ignores split file status."))
                spss.Submit(r"""SPLIT FILE OFF.""")   
            spss.Submit(r"""
COUNT %s=%s (MISSING).""" % (tmpvar1, varlist))
            spss.Submit(r"""
SELECT IF %s=0.""" % tmpvar1)
            spss.Submit(r"""
EXECUTE.""")
            validn=spss.GetCaseCount()
            if wtvar == None:
                spss.Submit(r"""
SAVE OUTFILE=%s.""" % tmpfile1)
            else:
                spss.Submit(r"""
DO IF %s >= 1.""" % wtvar)
                spss.Submit(r"""
+ LOOP #i=1 TO %s.""" % wtvar)
                spss.Submit(r"""
XSAVE OUTFILE=%s
  /KEEP=%s
  /DROP=%s.""" % (tmpfile1, varlist, wtvar))
                spss.Submit(r"""
+ END LOOP.
END IF.
EXECUTE.
""")   
            spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Variables to Cases'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
            spss.Submit(r"""
VARSTOCASES
  /MAKE %s FROM %s.""" % (tmpvar2, varlist))
            spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
            catdata = []
            try:
	        cur = spss.Cursor(isBinary=False)
            except:
	        cur = spss.Cursor()
            while True:
                datarow = cur.fetchone()
                if datarow is None:
                    break
                catdata.append(datarow[-1])
            cur.close()
            cats = list(set(catdata))
            ncats = len(cats)
            nraters = len(varnames)
            neededn = max(ncats, nraters)
            if validn < neededn:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                table = spss.BasePivotTable("Warnings ","Warnings")
                table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel,)] = CellText.String(_("""There are too few complete cases. This command is not executed."""))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
            elif ncats < 2:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                table = spss.BasePivotTable("Warnings ","Warnings")
                table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel,)] = CellText.String(_("""All ratings are the same. This command is not executed."""))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
            else:
                if len(warntext) > 0:
                    spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                    if len(warntext) == 1:
                        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                        table = spss.BasePivotTable("Warnings ","Warnings")
                        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String("%s" % warntext[0])
                        spss.EndProcedure()
                    if len(warntext) == 2:
                        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                        table = spss.BasePivotTable("Warnings ","Warnings")
                        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String("%s \n" "%s" % (warntext[0], warntext[1]))
                        spss.EndProcedure()
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
                spss.Submit(r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N.""" % (tmpfile2, tmpvar2, tmpvar3))
                spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN.""" % tmpdata2)
                spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN.""" % tmpdata3)
                spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Matrix'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag3)
                spss.Submit(r"""
MATRIX.
GET x 
  /FILE=%s
  /VARIABLES=%s.
GET ratecats
  /FILE=%s
  /VARIABLES=%s.
COMPUTE n=NROW(x).
COMPUTE c=NROW(ratecats).
COMPUTE y=MAKE(n,c,0).
LOOP i=1 to n.
+ LOOP j=1 to NCOL(x).
+   LOOP k=1 to c.
+     DO IF x(i,j)=ratecats(k).
+       COMPUTE y(i,k)=y(i,k)+1.
+     END IF.
+   END LOOP.
+ END LOOP.
END LOOP.
COMPUTE k=NCOL(x).
COMPUTE pe=MSUM((CSUM(y)/MSUM(y))&**2).
COMPUTE pa=MSSQ(y)/(NROW(y)*k*(k-1))-(1/(k-1)).
COMPUTE kstat=(pa-pe)/(1-pe).
COMPUTE cp=(CSSQ(y)-CSUM(y))&/((k-1)&*CSUM(y)).
COMPUTE pj=CSUM(y)/MSUM(y).
COMPUTE one=MAKE(1,NCOL(pj),1).
COMPUTE qj=one-pj.
COMPUTE kj=(cp-pj)&/qj.
COMPUTE num=2*((pj*t(qj))**2-MSUM(pj&*qj&*(qj-pj))).
COMPUTE den=n*k*(k-1)*((pj*t(qj))**2).
COMPUTE ase=SQRT(num/den).
COMPUTE z=kstat/ase.
COMPUTE sig=1-CHICDF(z**2,1).
SAVE {kstat,ase,z,sig}
   /OUTFILE=%s
   /VARIABLES=kstat,ase,z,sig.
COMPUTE asej=MAKE(1,c,SQRT(2/(n*k*(k-1)))).
COMPUTE zj=kj&/asej.
COMPUTE sigj=one-CHICDF(zj&**2,1).
SAVE {ratecats,t(cp),t(kj),t(asej),t(zj),t(sigj)}
  /OUTFILE=%s
  /VARIABLES=category,cp,kstat,ase,z,sig.
END MATRIX.""" % (tmpfile1, varlist, tmpfile2, tmpvar2, tmpdata2, tmpdata3))
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag3)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata2)
                spss.Submit(r"""
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
FORMATS kstat ase z sig lower upper (F11.3).
VARIABLE LABELS kstat %s. """ % _smartquote(_("""Kappa""")))
                spss.Submit(r"""
VARIABLE LABELS ase %s. """ % _smartquote(_("""Asymptotic Standard Error""")))
                spss.Submit(r"""
VARIABLE LABELS z %s. """ % _smartquote(_("""Z""")))
                spss.Submit(r"""
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                spss.Submit(r"""
EXECUTE.
""")
                try:
	            cur = spss.Cursor(isBinary=False)
                except:
	            cur = spss.Cursor()
                data1=cur.fetchone()
                cur.close()
                collabels1=[spss.GetVariableLabel(0),spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3), \
                                         spss.GetVariableLabel(4),spss.GetVariableLabel(5)]
                celldata1=[data1[0],data1[1],data1[2],data1[3],data1[4],data1[5]]
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata3)
                spss.Submit(r"""
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
FORMATS category (F10.0) cp kstat ase z sig lower upper (F11.3).
VARIABLE LABELS category %s. """ % _smartquote(_("""Rating Category""")))
                spss.Submit(r"""
VARIABLE LABELS cp %s. """ % _smartquote(_("""Conditional Probability""")))
                spss.Submit(r"""
VARIABLE LABELS kstat %s. """ % _smartquote(_("""Kappa""")))
                spss.Submit(r"""
VARIABLE LABELS ase %s. """ % _smartquote(_("""Asymptotic Standard Error""")))
                spss.Submit(r"""                
VARIABLE LABELS z %s. """ % _smartquote(_("""Z""")))
                spss.Submit(r""" 
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                spss.Submit(r""" 
EXECUTE.""")
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag4)
                spss.Submit(r"""
OMS
 /SELECT TEXTS
 /IF COMMANDS=['Fleiss Kappa'] LABELS=['Active Dataset']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag5)
                if len(warntext) > 0:
                    spss.Submit(r"""
OMS
 /SELECT HEADINGS
 /IF COMMANDS=['Fleiss Kappa']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag6)
                n=spss.GetCaseCount
                rlabels=[]
                data2=[]
                try:
	            cur = spss.Cursor(isBinary=False)
                except:
	            cur = spss.Cursor()
                for i in range(0,spss.GetCaseCount()):
                    datarow=cur.fetchone()
                    data2.append(datarow[1:])
                    rlabels.append(datarow[0])
                cur.close()
                def _flatten(seq):
                    for item in seq:
                        if spssaux._isseq(item):
                            for subitem in _flatten(item):
                                yield subitem
                        else:
                            yield item
                data2=[item for item in _flatten(data2)]
                spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                table1 = spss.BasePivotTable(_("Overall Kappa"),"Overall Kappa")
                table1.SimplePivotTable(rowdim = _(""),
                       rowlabels=[CellText.String("Overall")],
                       coldim="",
                       collabels=collabels1,
                       cells=celldata1)
                if any(item != round(item) for item in rlabels):
                    caption=(_("Non-integer rating category values are truncated for presentation."))
                else:
                    caption=("")
                table2=spss.BasePivotTable(_("Kappas for Individual Categories"),
                                _("Individual Category Kappa Statistics"),caption=caption)
                rowlabels=[(CellText.String("{:>9.0f}".format(rlabels[i]))) for i in range(len(rlabels))]
                collabels=[spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3), \
                      spss.GetVariableLabel(4),spss.GetVariableLabel(5),spss.GetVariableLabel(6), \
                      spss.GetVariableLabel(7)]
                table2.SimplePivotTable(rowdim=_("  Rating Category"),
                      rowlabels=rowlabels,
                      coldim="",
                      collabels=collabels,
                      cells=data2)
                spss.EndProcedure()
                if len(warntext) > 0:
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag6)
        finally:
            try:
                spss.Submit("""
DATASET CLOSE %s.""" % tmpdata1)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % activeds)
                if validn >= neededn:
                    if ncats >= 2:
                        spss.Submit("""
OMSEND TAG=['"%s"' '"%s"'].""" % (omstag4,omstag5))
                        spss.Submit("""
DATASET CLOSE %s.""" % tmpdata2)
                        spss.Submit("""
DATASET CLOSE %s.""" % tmpdata3)
                        spss.Submit("""
ERASE FILE=%s.""" % tmpfile1)
                        spss.Submit(r"""
ERASE FILE=%s.""" % tmpfile2)
            except:
                pass
            spss.Submit("""
RESTORE.
""")
def weightedkappaextension(variables, wttype=1, cilevel=95):

    varnames = expandvarnames(variables)
    caption = varnames[0] + _(" vs. ") + varnames[1]
    vardict = spssaux.VariableDict(varnames)
    if len(vardict) != len(varnames):
        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(
            _("""An invalid variable has been specified. This command is not executed.""")
        )
        spss.EndProcedure()
    elif len(varnames) != 2:
        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(
            _("""Exactly two variables must be specified. This command is not executed.""")
        )
        spss.EndProcedure()
    else:
        try:
            warntext = []
            if cilevel < 50:
                warntext.append(_("CILEVEL cannot be less than 50%. It has been set to 50%."))
                cilevel = 50
            if cilevel > 99.999:
                warntext.append(_("CILEVEL cannot be greater than 99.999%. It has been set to 99.999%."))
                cilevel = 99.999
            if cilevel == int(cilevel):
                cilevel = int(cilevel)
            if wttype != 1:
                if wttype != 2:
                    warntext.append(_("WTTYPE must be 1 or 2. It has been set to 1."))
                    wttype = 1
            varlist = varnames[0] + " " + varnames[1]
            spss.Submit("PRESERVE.")
            tempdir = tempfile.gettempdir()
            spss.Submit("""CD "%s".""" % tempdir)
            wtvar = spss.GetWeightVar()
            if wtvar != None:
                spss.Submit(
                    r"""
COMPUTE %s=RND(%s)."""
                    % (wtvar, wtvar)
                )
                spss.Submit(
                    r"""
EXECUTE."""
                )
            maxloops = 2 * spss.GetCaseCount()
            spss.Submit("""SET PRINTBACK=OFF MPRINT=OFF MXLOOPS=%s.""" % maxloops)
            activeds = spss.ActiveDataset()
            if activeds == "*":
                activeds = "D" + str(random.uniform(0.1, 1))
                spss.Submit("DATASET NAME %s" % activeds)
            tmpvar1 = "V" + str(random.uniform(0.1, 1))
            tmpvar2 = "V" + str(random.uniform(0.1, 1))
            tmpvar3 = "V" + str(random.uniform(0.1, 1))
            tmpvar4 = "V" + str(random.uniform(0.1, 1))
            tmpvar5 = "V" + str(random.uniform(0.1, 1))
            tmpvar6 = "V" + str(random.uniform(0.1, 1))
            tmpdata1 = "D" + str(random.uniform(0.1, 1))
            tmpdata2 = "D" + str(random.uniform(0.1, 1))
            omstag1 = "T" + str(random.uniform(0.1, 1))
            omstag2 = "T" + str(random.uniform(0.1, 1))
            omstag3 = "T" + str(random.uniform(0.1, 1))
            omstag4 = "T" + str(random.uniform(0.1, 1))
            omstag5 = "T" + str(random.uniform(0.1, 1))
            omstag6 = "T" + str(random.uniform(0.1, 1))
            tmpfile1 = "F" + str(random.uniform(0.1, 1))
            tmpfile2 = "F" + str(random.uniform(0.1, 1))
            lowlabel = _("""Lower %s%% Asymptotic CI Bound""") % cilevel
            upplabel = _("""Upper %s%% Asymptotic CI Bound""") % cilevel
            spss.Submit(
                r"""
DATASET COPY %s WINDOW=HIDDEN."""
                % tmpdata1
            )
            spss.Submit(
                r"""
DATASET ACTIVATE %s WINDOW=ASIS."""
                % tmpdata1
            )
            filt = spssaux.GetSHOW("FILTER", olang="english")
            if filt != "No case filter is in effect":
                filtcond = filt.strip("(FILTER)")
                select = "SELECT IF " + str(filtcond) + "."
                spss.Submit("""%s""" % select)
                spss.Submit("""EXECUTE.""")
                spss.Submit("""USE ALL.""")
            banana = spssaux.getDatasetInfo(Info="SplitFile")
            if banana != "":
                warntext.append(_("This procedure ignores split file status."))
                spss.Submit(r"""SPLIT FILE OFF.""")
            spss.Submit(
                r"""
COUNT %s=%s (MISSING)."""
                % (tmpvar1, varlist)
            )
            spss.Submit(
                r"""
SELECT IF %s=0."""
                % tmpvar1
            )
            spss.Submit(
                r"""
EXECUTE."""
            )
            validn = spss.GetCaseCount()
            if validn < 2:
                spss.Submit(
                    r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                    % omstag1
                )
                spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                table = spss.BasePivotTable("Warnings ", "Warnings")
                table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel,)] = CellText.String(
                    _("""There are too few complete cases. This command is not executed.""")
                )
                spss.EndProcedure()
                spss.Submit(
                    r"""
OMSEND TAG = ['"%s"']."""
                    % omstag1
                )
            else:
                spss.Submit(
                    r"""
AGGREGATE
   /OUTFILE=* MODE=ADDVARIABLES
   /%s=SD(%s)
   /%s=SD(%s)."""
                    % (tmpvar2, varnames[0], tmpvar3, varnames[1])
                )
                try:
                    cur = spss.Cursor(isBinary=False)
                except:
                    cur = spss.Cursor()
                datarow = cur.fetchone()
                cur.close()
                sd1 = datarow[-2]
                sd2 = datarow[-1]
                if min(sd1, sd2) == 0:
                    spss.Submit(
                        r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                        % omstag1
                    )
                    spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                    table = spss.BasePivotTable("Warnings ", "Warnings")
                    table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                    rowLabel = CellText.String("1")
                    table[(rowLabel,)] = CellText.String(
                        _("""All ratings are the same for at least one rater. This command is not executed.""")
                    )
                    spss.EndProcedure()
                    spss.Submit(
                        r"""
OMSEND TAG = ['"%s"']."""
                        % omstag1
                    )
                else:
                    if len(warntext) > 0:
                        spss.Submit(
                            r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                            % omstag1
                        )
                        if len(warntext) == 1:
                            spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                            table = spss.BasePivotTable("Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel,)] = CellText.String("%s" % warntext[0])
                            spss.EndProcedure()
                        if len(warntext) == 2:
                            spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                            table = spss.BasePivotTable("Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel,)] = CellText.String("%s \n" "%s" % (warntext[0], warntext[1]))
                            spss.EndProcedure()
                        if len(warntext) == 3:
                            spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                            table = spss.BasePivotTable("Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel,)] = CellText.String(
                                "%s \n" "%s \n" "%s" % (warntext[0], warntext[1], warntext[2])
                            )
                            spss.EndProcedure()
                        spss.Submit(
                            r"""
OMSEND TAG = ['"%s"']."""
                            % omstag1
                        )
                    spss.Submit(
                        r"""
DELETE VARIABLES %s %s."""
                        % (tmpvar2, tmpvar3)
                    )
                    spss.Submit(
                        r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N."""
                        % (tmpfile1, varlist, tmpvar4)
                    )
                    spss.Submit(
                        r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Variables to Cases'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                        % omstag2
                    )
                    spss.Submit(
                        r"""
VARSTOCASES
  /MAKE %s FROM %s."""
                        % (tmpvar5, varlist)
                    )
                    spss.Submit(
                        r"""
OMSEND TAG = ['"%s"']."""
                        % omstag2
                    )
                    catdata = []
                    try:
                        cur = spss.Cursor(isBinary=False)
                    except:
                        cur = spss.Cursor()
                    while True:
                        datarow = cur.fetchone()
                        if datarow is None:
                            break
                        catdata.append(datarow[-1])
                    cur.close()
                    cats = list(set(catdata))
                    cattest = 0
                    if any(item != round(item) for item in cats):
                        cattest = 1
                        spss.Submit(
                            r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                            % omstag1
                        )
                        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String(
                            _("""Some ratings are not integers. This command is not executed.""")
                        )
                        spss.EndProcedure()
                        spss.Submit(
                            r"""
OMSEND TAG = ['"%s"']."""
                            % omstag1
                        )
                    elif min(cats) < 1.0:
                        spss.Submit(
                            r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                            % omstag1
                        )
                        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String(
                            _("""Some ratings are less than 1. This command is not executed.""")
                        )
                        spss.EndProcedure()
                        spss.Submit(
                            r"""
OMSEND TAG = ['"%s"']."""
                            % omstag1
                        )
                    else:
                        spss.Submit(
                            r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N."""
                            % (tmpfile2, tmpvar5, tmpvar6)
                        )
                        spss.Submit(
                            r"""
DATASET DECLARE %s WINDOW=HIDDEN"""
                            % tmpdata2
                        )
                        spss.Submit(
                            r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Matrix'] 
 /DESTINATION VIEWER=NO
 /TAG='"%s"'."""
                            % omstag3
                        )
                        spss.Submit(
                            r"""
MATRIX.
GET x 
  /FILE=%s
  /VARIABLES=%s %s.
GET ratecats
  /FILE=%s
  /VARIABLES=%s.
COMPUTE size=MMAX(ratecats).
COMPUTE y=MAKE(size,size,0).
LOOP i=1 to NROW(y).
+ LOOP j=1 to NCOL(y).
+   LOOP k=1 to NROW(x).
+     DO IF (x(k,1)=i and x(k,2)=j).
+       COMPUTE y(i,j)=x(k,3).
+     END IF.
+   END LOOP.
+ END LOOP.
END LOOP.
COMPUTE wttype=%s.
COMPUTE wt=MAKE(NROW(y),NCOL(y),0).
LOOP i=1 to NROW(y).
+ LOOP j=1 to NCOL(y).
+   DO IF wttype=1.
+     COMPUTE wt(i,j)=1-(ABS(i-j)/(size-1)).
+   ELSE IF wttype=2.
+     COMPUTE wt(i,j)=1-((i-j)/(NROW(y)-1))**2.
+   END IF.
+ END LOOP.
END LOOP.
COMPUTE n=MSUM(y).
COMPUTE prop=y/n.
COMPUTE p_i=RSUM(prop).
COMPUTE p_j=CSUM(prop).
COMPUTE w_i=(wt*T(p_j))*MAKE(1,size,1).
COMPUTE w_j=MAKE(size,1,1)*(T(p_i)*wt).
COMPUTE po=MSUM(wt&*prop).
COMPUTE pe=MSUM(MDIAG(p_i)*wt*MDIAG(p_j)).
COMPUTE kstat=(po-pe)/(1-pe).
COMPUTE var0=(T(p_i)*((wt-(w_i+w_j))&**2)*T(p_j)-pe**2)/(n*(1-pe)**2).
DO IF var0>=0.
+ COMPUTE ase0=SQRT(var0).
ELSE.
+ COMPUTE ase0=-1.
END IF.
DO IF ase0>0.
+ COMPUTE z=kstat/ase0.
+ COMPUTE sig=1-CHICDF(z**2,1).
ELSE.
+ COMPUTE z=-1.
+ COMPUTE sig=-1.
END IF.
COMPUTE var1=(MSUM((prop&*((wt-(w_i+w_j)&*(1-kstat))&**2)))-(kstat-pe*(1-kstat))**2)/(n*(1-pe)**2).
DO IF var1>=0.
+ COMPUTE ase1=SQRT(var1).
ELSE.
+ COMPUTE ase1=-1.
END IF.
SAVE {wttype,kstat,ase1,z,sig,ase0}
   /OUTFILE=%s
   /VARIABLES=wttype,kstat,ase1,z,sig,ase0.
END MATRIX."""
                            % (tmpfile1, varlist, tmpvar4, tmpfile2, tmpvar5, wttype, tmpdata2)
                        )
                        spss.Submit(
                            r"""
OMSEND TAG=['"%s"']."""
                            % omstag3
                        )
                        spss.Submit(
                            r"""
DATASET ACTIVATE %s WINDOW=ASIS."""
                            % tmpdata2
                        )
                        spss.Submit(
                            r"""
DO IF ase0=-1.
+ RECODE z sig (-1=SYSMIS).
END IF.
EXECUTE.
DELETE VARIABLES ase0.
RECODE ase1 (-1=SYSMIS).
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase1."""
                            % cilevel
                        )
                        spss.Submit(
                            r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase1."""
                            % cilevel
                        )
                        spss.Submit(
                            r"""
FORMATS kstat ase1 z sig lower upper (F11.3).
VARIABLE LABELS kstat %s."""
                            % _smartquote(_("""Kappa"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS ase1 %s."""
                            % _smartquote(_("""Asymptotic Standard Error"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS z %s."""
                            % _smartquote(_("""Z"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS sig %s. """
                            % _smartquote(_("""P Value"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS lower %s. """
                            % _smartquote(_(lowlabel))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS upper %s. """
                            % _smartquote(_(upplabel))
                        )
                        if wttype == 1:
                            spss.Submit(
                                r"""
VARIABLE LABELS wttype %s."""
                                % _smartquote(_("""Linear"""))
                            )
                        if wttype == 2:
                            spss.Submit(
                                r"""
VARIABLE LABELS wttype %s."""
                                % _smartquote(_("""Quadratic"""))
                            )
                        spss.Submit(
                            r"""
EXECUTE.
"""
                        )
                        spss.Submit(
                            r"""
OMS
  /SELECT TABLES 
  /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
  /DESTINATION VIEWER=NO
  /TAG = '"%s"'."""
                            % omstag4
                        )
                        spss.Submit(
                            r"""
OMS
  /SELECT TEXTS
  /IF COMMANDS=['Weighted Kappa'] LABELS=['Active Dataset']
  /DESTINATION VIEWER=NO
  /TAG = '"%s"'."""
                            % omstag5
                        )
                        if len(warntext) > 0:
                            spss.Submit(
                                r"""
OMS
 /SELECT HEADINGS
 /IF COMMANDS=['Weighted Kappa']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                                % omstag6
                            )
                        try:
                            cur = spss.Cursor(isBinary=False)
                        except:
                            cur = spss.Cursor()
                        data = cur.fetchone()
                        cur.close()
                        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                        table = spss.BasePivotTable(_("Weighted Kappa"), "Kappa", caption=caption)
                        table.SimplePivotTable(
                            rowdim=_("Weighting"),
                            rowlabels=[CellText.String(spss.GetVariableLabel(0))],
                            coldim="",
                            collabels=[
                                spss.GetVariableLabel(1),
                                spss.GetVariableLabel(2),
                                spss.GetVariableLabel(3),
                                spss.GetVariableLabel(4),
                                spss.GetVariableLabel(5),
                                spss.GetVariableLabel(6),
                            ],
                            cells=[data[1], data[2], data[3], data[4], data[5], data[6]],
                        )
                        spss.EndProcedure()
                        if len(warntext) > 0:
                            spss.Submit(
                                r"""
OMSEND TAG = ['"%s"']."""
                                % omstag6
                            )
        finally:
            try:
                spss.Submit(
                    r"""
DATASET CLOSE %s."""
                    % tmpdata1
                )
                spss.Submit(
                    r"""
DATASET ACTIVATE %s WINDOW=ASIS."""
                    % activeds
                )
                if validn >= 2:
                    if min(sd1, sd2) > 0:
                        if cattest == 0:
                            if min(cats) >= 1:
                                spss.Submit(
                                    r"""
OMSEND TAG=['"%s"' '"%s"']."""
                                    % (omstag4, omstag5)
                                )
                                spss.Submit(
                                    r"""
DATASET CLOSE %s."""
                                    % tmpdata2
                                )
                                spss.Submit(
                                    r"""
ERASE FILE=%s."""
                                    % tmpfile2
                                )
                        spss.Submit(
                            r"""
ERASE FILE=%s."""
                            % tmpfile1
                        )
            except:
                pass
            spss.Submit(
                r"""
RESTORE.
"""
            )
def fleisskappaextension(variables, cilevel=95):

    varnames = expandvarnames(variables)
    vardict = spssaux.VariableDict(varnames)
    if len(vardict) != len(varnames):
        spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel, )] = CellText.String(
            _("""An invalid variable has been specified. This command is not executed."""
              ))
        spss.EndProcedure()
    elif len(varnames) < 2:
        spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel, )] = CellText.String(
            _("""At least two variables must be specified. This command is not executed."""
              ))
        spss.EndProcedure()

    else:
        try:
            warntext = []
            if cilevel < 50:
                warntext.append(
                    _("CILEVEL cannot be less than 50%. It has been reset to 50%."
                      ))
                cilevel = 50
            if cilevel > 99.999:
                warntext.append(
                    _("CILEVEL cannot be greater than 99.999%. It has been reset to 99.999%."
                      ))
                cilevel = 99.999
            if cilevel == int(cilevel):
                cilevel = int(cilevel)
            varlist = varnames[0]
            for i in range(1, len(varnames)):
                varlist = varlist + ' ' + varnames[i]
            spss.Submit("PRESERVE.")
            tempdir = tempfile.gettempdir()
            spss.Submit("""CD "%s".""" % tempdir)
            wtvar = spss.GetWeightVar()
            if wtvar != None:
                spss.Submit(r"""
COMPUTE %s=RND(%s).""" % (wtvar, wtvar))
                spss.Submit(r"""
EXECUTE.""")
                wtdn = GetWeightSum(varnames)
            else:
                wtdn = spss.GetCaseCount()
            maxloops = wtdn + 1
            spss.Submit(
                """SET PRINTBACK=OFF MPRINT=OFF OATTRS=ENG MXLOOPS=%s.""" %
                maxloops)
            activeds = spss.ActiveDataset()
            if activeds == "*":
                activeds = "D" + str(random.uniform(.1, 1))
                spss.Submit("DATASET NAME %s" % activeds)
            tmpvar1 = "V" + str(random.uniform(.1, 1))
            tmpvar2 = "V" + str(random.uniform(.1, 1))
            tmpvar3 = "V" + str(random.uniform(.1, 1))
            tmpfile1 = "F" + str(random.uniform(.1, 1))
            tmpfile2 = "F" + str(random.uniform(.1, 1))
            tmpdata1 = "D" + str(random.uniform(.1, 1))
            tmpdata2 = "D" + str(random.uniform(.1, 1))
            tmpdata3 = "D" + str(random.uniform(.1, 1))
            omstag1 = "T" + str(random.uniform(.1, 1))
            omstag2 = "T" + str(random.uniform(.1, 1))
            omstag3 = "T" + str(random.uniform(.1, 1))
            omstag4 = "T" + str(random.uniform(.1, 1))
            omstag5 = "T" + str(random.uniform(.1, 1))
            omstag6 = "T" + str(random.uniform(.1, 1))
            lowlabel = _("""Lower %s%% Asymptotic CI Bound""") % cilevel
            upplabel = _("""Upper %s%% Asymptotic CI Bound""") % cilevel
            spss.Submit(r"""
DATASET COPY %s WINDOW=HIDDEN.""" % tmpdata1)
            spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata1)
            filt = spssaux.GetSHOW("FILTER", olang="english")
            if filt != "No case filter is in effect":
                filtcond = filt.strip("(FILTER)")
                select = "SELECT IF " + str(filtcond) + "."
                spss.Submit("""%s""" % select)
                spss.Submit("""EXECUTE.""")
                spss.Submit("""USE ALL.""")
            banana = spssaux.getDatasetInfo(Info="SplitFile")
            if banana != "":
                warntext.append(_("This command ignores split file status."))
                spss.Submit(r"""SPLIT FILE OFF.""")
            spss.Submit(r"""
COUNT %s=%s (MISSING).""" % (tmpvar1, varlist))
            spss.Submit(r"""
SELECT IF %s=0.""" % tmpvar1)
            spss.Submit(r"""
EXECUTE.
MISSING VALUES ALL ().""")
            validn = spss.GetCaseCount()
            if wtvar == None:
                spss.Submit(r"""
SAVE OUTFILE=%s.""" % tmpfile1)
            else:
                spss.Submit(r"""
DO IF %s >= 1.""" % wtvar)
                spss.Submit(r"""
+ LOOP #i=1 TO %s.""" % wtvar)
                spss.Submit(r"""
XSAVE OUTFILE=%s
  /KEEP=%s
  /DROP=%s.""" % (tmpfile1, varlist, wtvar))
                spss.Submit(r"""
+ END LOOP.
END IF.
EXECUTE.
""")
            spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Variables to Cases'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
            spss.Submit(r"""
VARSTOCASES
  /MAKE %s FROM %s.""" % (tmpvar2, varlist))
            spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
            catdata = []
            try:
                cur = spss.Cursor(isBinary=False)
            except:
                cur = spss.Cursor()
            while True:
                datarow = cur.fetchone()
                if datarow is None:
                    break
                catdata.append(datarow[-1])
            cur.close()
            cats = list(set(catdata))
            ncats = len(cats)
            nraters = len(varnames)
            neededn = max(ncats, nraters)
            if validn < neededn:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
                table = spss.BasePivotTable("Warnings ", "Warnings")
                table.Append(spss.Dimension.Place.row,
                             "rowdim",
                             hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel, )] = CellText.String(
                    _("""There are too few complete cases. This command is not executed."""
                      ))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
            elif ncats < 2:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
                table = spss.BasePivotTable("Warnings ", "Warnings")
                table.Append(spss.Dimension.Place.row,
                             "rowdim",
                             hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel, )] = CellText.String(
                    _("""All ratings are the same. This command is not executed."""
                      ))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
            else:
                if len(warntext) > 0:
                    spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                    if len(warntext) == 1:
                        spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row,
                                     "rowdim",
                                     hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel, )] = CellText.String("%s" %
                                                              warntext[0])
                        spss.EndProcedure()
                    if len(warntext) == 2:
                        spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row,
                                     "rowdim",
                                     hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel, )] = CellText.String(
                            "%s \n"
                            "%s" % (warntext[0], warntext[1]))
                        spss.EndProcedure()
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
                spss.Submit(r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N.""" % (tmpfile2, tmpvar2, tmpvar3))
                spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN.""" % tmpdata2)
                spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN.""" % tmpdata3)
                spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Matrix'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag3)
                spss.Submit(r"""
MATRIX.
GET x 
  /FILE=%s
  /VARIABLES=%s.
GET ratecats
  /FILE=%s
  /VARIABLES=%s.
COMPUTE n=NROW(x).
COMPUTE c=NROW(ratecats).
COMPUTE y=MAKE(n,c,0).
LOOP i=1 to n.
+ LOOP j=1 to NCOL(x).
+   LOOP k=1 to c.
+     DO IF x(i,j)=ratecats(k).
+       COMPUTE y(i,k)=y(i,k)+1.
+     END IF.
+   END LOOP.
+ END LOOP.
END LOOP.
COMPUTE k=NCOL(x).
COMPUTE pe=MSUM((CSUM(y)/MSUM(y))&**2).
COMPUTE pa=MSSQ(y)/(NROW(y)*k*(k-1))-(1/(k-1)).
COMPUTE kstat=(pa-pe)/(1-pe).
COMPUTE cp=(CSSQ(y)-CSUM(y))&/((k-1)&*CSUM(y)).
COMPUTE pj=CSUM(y)/MSUM(y).
COMPUTE one=MAKE(1,NCOL(pj),1).
COMPUTE qj=one-pj.
COMPUTE kj=(cp-pj)&/qj.
COMPUTE num=2*((pj*t(qj))**2-MSUM(pj&*qj&*(qj-pj))).
COMPUTE den=n*k*(k-1)*((pj*t(qj))**2).
COMPUTE ase=SQRT(num/den).
COMPUTE z=kstat/ase.
COMPUTE sig=1-CHICDF(z**2,1).
SAVE {kstat,ase,z,sig}
   /OUTFILE=%s
   /VARIABLES=kstat,ase,z,sig.
COMPUTE asej=MAKE(1,c,SQRT(2/(n*k*(k-1)))).
COMPUTE zj=kj&/asej.
COMPUTE sigj=one-CHICDF(zj&**2,1).
SAVE {ratecats,t(cp),t(kj),t(asej),t(zj),t(sigj)}
  /OUTFILE=%s
  /VARIABLES=category,cp,kstat,ase,z,sig.
END MATRIX.""" % (tmpfile1, varlist, tmpfile2, tmpvar2, tmpdata2, tmpdata3))
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag3)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata2)
                spss.Submit(r"""
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
FORMATS kstat ase z sig lower upper (F11.3).
VARIABLE LABELS kstat %s. """ % _smartquote(_("""Kappa""")))
                spss.Submit(r"""
VARIABLE LABELS ase %s. """ % _smartquote(_("""Asymptotic Standard Error""")))
                spss.Submit(r"""
VARIABLE LABELS z %s. """ % _smartquote(_("""Z""")))
                spss.Submit(r"""
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                spss.Submit(r"""
EXECUTE.
""")
                try:
                    cur = spss.Cursor(isBinary=False)
                except:
                    cur = spss.Cursor()
                data1 = cur.fetchone()
                cur.close()
                collabels1=[spss.GetVariableLabel(0),spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3), \
                                         spss.GetVariableLabel(4),spss.GetVariableLabel(5)]
                celldata1 = [
                    data1[0], data1[1], data1[2], data1[3], data1[4], data1[5]
                ]
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata3)
                spss.Submit(r"""
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
FORMATS category (F10.0) cp kstat ase z sig lower upper (F11.3).
VARIABLE LABELS category %s. """ % _smartquote(_("""Rating Category""")))
                spss.Submit(r"""
VARIABLE LABELS cp %s. """ % _smartquote(_("""Conditional Probability""")))
                spss.Submit(r"""
VARIABLE LABELS kstat %s. """ % _smartquote(_("""Kappa""")))
                spss.Submit(r"""
VARIABLE LABELS ase %s. """ % _smartquote(_("""Asymptotic Standard Error""")))
                spss.Submit(r"""                
VARIABLE LABELS z %s. """ % _smartquote(_("""Z""")))
                spss.Submit(r""" 
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                spss.Submit(r""" 
EXECUTE.""")
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag4)
                spss.Submit(r"""
OMS
 /SELECT TEXTS
 /IF COMMANDS=['Fleiss Kappa'] LABELS=['Active Dataset']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag5)
                if len(warntext) > 0:
                    spss.Submit(r"""
OMS
 /SELECT HEADINGS
 /IF COMMANDS=['Fleiss Kappa']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag6)
                n = spss.GetCaseCount
                rlabels = []
                data2 = []
                try:
                    cur = spss.Cursor(isBinary=False)
                except:
                    cur = spss.Cursor()
                for i in range(0, spss.GetCaseCount()):
                    datarow = cur.fetchone()
                    data2.append(datarow[1:])
                    rlabels.append(datarow[0])
                cur.close()

                def _flatten(seq):
                    for item in seq:
                        if spssaux._isseq(item):
                            for subitem in _flatten(item):
                                yield subitem
                        else:
                            yield item

                data2 = [item for item in _flatten(data2)]
                spss.StartProcedure(_("Fleiss Kappa"), "Fleiss Kappa")
                table1 = spss.BasePivotTable(_("Overall Kappa"),
                                             "Overall Kappa")
                table1.SimplePivotTable(rowdim=_(""),
                                        rowlabels=[CellText.String("Overall")],
                                        coldim="",
                                        collabels=collabels1,
                                        cells=celldata1)
                if any(item != round(item) for item in rlabels):
                    caption = (_(
                        "Non-integer rating category values are truncated for presentation."
                    ))
                else:
                    caption = ("")
                table2 = spss.BasePivotTable(
                    _("Kappas for Individual Categories"),
                    _("Individual Category Kappa Statistics"),
                    caption=caption)
                rowlabels = [(CellText.String("{:>9.0f}".format(rlabels[i])))
                             for i in range(len(rlabels))]
                collabels=[spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3), \
                      spss.GetVariableLabel(4),spss.GetVariableLabel(5),spss.GetVariableLabel(6), \
                      spss.GetVariableLabel(7)]
                table2.SimplePivotTable(rowdim=_("  Rating Category"),
                                        rowlabels=rowlabels,
                                        coldim="",
                                        collabels=collabels,
                                        cells=data2)
                spss.EndProcedure()
                if len(warntext) > 0:
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag6)
        finally:
            try:
                spss.Submit("""
DATASET CLOSE %s.""" % tmpdata1)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % activeds)
                if validn >= neededn:
                    if ncats >= 2:
                        spss.Submit("""
OMSEND TAG=['"%s"' '"%s"'].""" % (omstag4, omstag5))
                        spss.Submit("""
DATASET CLOSE %s.""" % tmpdata2)
                        spss.Submit("""
DATASET CLOSE %s.""" % tmpdata3)
                        spss.Submit("""
ERASE FILE=%s.""" % tmpfile1)
                        spss.Submit(r"""
ERASE FILE=%s.""" % tmpfile2)
            except:
                pass
            spss.Submit("""
RESTORE.
""")
Пример #18
0
def recode(varlist,
           recodes,
           stringsize=None,
           makevaluelabels=True,
           copyvariablelabels=True,
           useinputvallabels=False,
           suffix="",
           prefix=""):

    vardict = spssaux.VariableDict(caseless=True)
    isutf8 = spss.PyInvokeSpss.IsUTF8mode()
    ecutf8 = codecs.getencoder("utf_8")
    inputlist, outputlist, vartype = parsevarlist(varlist, vardict)
    if len(recodes) > 1:
        raise ValueError(
            _("The RECODES subcommand must consist of a single, quoted specification"
              ))
    # recodespec is a list of textual recode syntax, one item per value set
    # vldefs is a dictionary with keys the target values
    # and values the input codes
    # inputdict is a dictionary with keys the target values
    # and values a list of the input codes
    recodespec, vldefs, inputdict = parserecodes(recodes[0], vartype,
                                                 stringsize)
    valuelabelmessage = checklabelconsistency(inputlist, vardict)

    if stringsize:
        alter = []
        create = []
        for v in outputlist:
            try:
                if vardict[v].VariableType != stringsize:
                    alter.append(v)
            except:
                create.append(v)
        if create:
            spss.Submit("STRING %s (A%s)." % (" ".join(create), stringsize))
        if alter:
            spss.Submit("ALTER TYPE %s (A%s)" % (" ".join(alter), stringsize))

    spss.Submit(
        """RECODE %s %s INTO %s.""" %
        (" ".join(inputlist), " ".join(recodespec), " ".join(outputlist)))

    # generate variable labels if requested
    if copyvariablelabels:
        if prefix and not prefix.endswith(" "):
            prefix = prefix + " "
        if suffix and not suffix.startswith(" "):
            suffix = " " + suffix
        for vin, vout in zip(inputlist, outputlist):
            spss.Submit("""VARIABLE LABEL %s %s.""" % \
                (vout, _smartquote(prefix + vardict[vin].VariableLabel + suffix, True)))

    # generate value labels if requested
    # all values for given target are merged but else clause is omitted
    # VALUE LABELS syntax quotes values regardless of variable type
    # vldefs is a dictionary with keys of the output values and
    # values a string listing the input values.  If copying value labels
    # the first input variable is used as the source.
    if makevaluelabels:
        if useinputvallabels:
            vldefs = makevallabels(vldefs, inputdict,
                                   vardict[inputlist[0]].ValueLabels, isutf8,
                                   ecutf8)

        # ensure that copy as target does not generate a value label
        copyset = set()
        for target in vldefs:
            if target.lower() == "copy":
                copyset.add(target)
        for c in copyset:
            del (vldefs[c])

        #spss.Submit(r"""VALUE LABELS %s %s.""" % (" ".join(outputlist), \
        #" ".join([_smartquote(val, vartype == 2) + " " + _smartquote(label, True) for val, label in vldefs.items()])))

        spss.Submit(r"""VALUE LABELS %s %s.""" % (" ".join(outputlist), \
            " ".join([val + " " + _smartquote(label, True) for val, label in list(vldefs.items())])))
    if valuelabelmessage:
        print(valuelabelmessage)