Exemple #1
0
def cforange_attribute_distance(input_dict):
    import orange
    import orngInteract
    inputdata = input_dict['dataset']
    discretizedData = None
    classInteractions = int(input_dict['classInteractions'])
    atts = inputdata.domain.attributes
    if len(atts) < 2:
        return None
    matrix = orange.SymMatrix(len(atts))
    matrix.setattr('items', atts)
    if classInteractions < 3:
        if inputdata.domain.hasContinuousAttributes():
            if discretizedData is None:
                try:
                    discretizedData = orange.Preprocessor_discretize(
                        inputdata,
                        method=orange.EquiNDiscretization(numberOfIntervals=4))
                except orange.KernelException, ex:
                    return None
            data = discretizedData
        else:
            data = inputdata

        # This is ugly (no shit)
        if not data.domain.classVar:
            if classInteractions == 0:
                classedDomain = orange.Domain(
                    data.domain.attributes,
                    orange.EnumVariable("foo", values=["0", "1"]))
                data = orange.ExampleTable(classedDomain, data)
            else:
                return None

        im = orngInteract.InteractionMatrix(data, dependencies_too=1)
        off = 1
        if classInteractions == 0:
            diss, labels = im.exportChi2Matrix()
            off = 0
        elif classInteractions == 1:
            (diss, labels) = im.depExportDissimilarityMatrix(
                jaccard=1)  # 2-interactions
        else:
            (diss, labels) = im.exportDissimilarityMatrix(
                jaccard=1)  # 3-interactions

        for i in range(len(atts) - off):
            for j in range(i + 1):
                matrix[i + off, j] = diss[i][j]
Exemple #2
0
    def computeMatrix(self):
        self.error()
        if self.data:
            atts = self.data.domain.attributes
            matrix = orange.SymMatrix(len(atts))
            matrix.setattr('items', atts)

            if self.classInteractions < 3:
                if self.data.domain.hasContinuousAttributes():
                    if self.discretizedData is None:
                        self.discretizedData = orange.Preprocessor_discretize(
                            self.data,
                            method=orange.EquiNDiscretization(
                                numberOfIntervals=4))
                    data = self.discretizedData
                else:
                    data = self.data

                # This is ugly, but: Aleks' code which computes Chi2 requires the class attribute because it prepares
                # some common stuff for all measures. If we want to use his code, we need the class variable, so we
                # prepare a fake one
                if not data.domain.classVar:
                    if self.classInteractions == 0:
                        classedDomain = orange.Domain(
                            data.domain.attributes,
                            orange.EnumVariable("foo", values=["0", "1"]))
                        data = orange.ExampleTable(classedDomain, data)
                    else:
                        self.error(
                            "The selected distance measure requires a data set with a class attribute"
                        )
                        return None

                im = orngInteract.InteractionMatrix(data, dependencies_too=1)
                off = 1
                if self.classInteractions == 0:
                    diss, labels = im.exportChi2Matrix()
                    off = 0
                elif self.classInteractions == 1:
                    (diss, labels) = im.depExportDissimilarityMatrix(
                        jaccard=1)  # 2-interactions
                else:
                    (diss, labels) = im.exportDissimilarityMatrix(
                        jaccard=1)  # 3-interactions

                for i in range(len(atts) - off):
                    for j in range(i + 1):
                        matrix[i + off, j] = diss[i][j]

            else:
                if self.classInteractions == 3:
                    for a1 in range(len(atts)):
                        for a2 in range(a1):
                            matrix[a1, a2] = orange.PearsonCorrelation(
                                a1, a2, self.data, 0).p
                else:
                    import numpy, statc
                    m = self.data.toNumpyMA("A")[0]
                    averages = numpy.ma.average(m, axis=0)
                    filleds = [
                        list(numpy.ma.filled(m[:, i], averages[i]))
                        for i in range(len(atts))
                    ]
                    for a1, f1 in enumerate(filleds):
                        for a2 in range(a1):
                            matrix[a1, a2] = statc.spearmanr(f1,
                                                             filleds[a2])[1]

            return matrix
        else:
            return None
Exemple #3
0
    elif color_mode == 3:
        _colorize = _bw2  # grayscale for dendrograms
    elif color_mode == 4:
        _colorize = RdBu()
    elif color_mode == 5:
        _colorize = RdBu(darken=1)
    elif color_mode == 6:
        _colorize = _bw3  # plain grayscale
    return _colorize


if __name__ == "__main__":
    import orange, orngInteract
    import warnings
    warnings.filterwarnings("ignore", module="orngInteract")

    tab = orange.ExampleTable('zoo.tab')
    im = orngInteract.InteractionMatrix(tab)
    (diss, labels) = im.exportDissimilarityMatrix()
    c = GDHClustering(diss)
    canvas = c.dendrogram(labels)
    canvas.getImage().save("c_zoo.png")
    ViewImage(canvas.getImage())

    (dissx, labels, gains) = im.exportDissimilarityMatrix(show_gains=0,
                                                          color_coding=1,
                                                          color_gains=1)
    canvas = c.matrix(labels, dissx, att_colors=gains)
    canvas.getImage().save("m_zoo.png")
    ViewImage(canvas.getImage())
Exemple #4
0
    def updateNewData(self, data):
        self.data = data
        self.interactionMatrix = orngInteract.InteractionMatrix(data, dependencies_too=1)

        # save discretized data and repair invalid names
        for attr in self.interactionMatrix.discData.domain.attributes:
            attr.name = attr.name.replace("ED_","")
            attr.name = attr.name.replace("D_","")
            attr.name = attr.name.replace("M_","")

        self.interactionList = []
        entropy = self.interactionMatrix.entropy
        if entropy == 0.0: return

        ################################
        # create a sorted list of total information
        for ((val,(val2, attrIndex1, attrIndex2))) in self.interactionMatrix.list:
            gain1 = self.interactionMatrix.gains[attrIndex1] / entropy
            gain2 = self.interactionMatrix.gains[attrIndex2] / entropy
            total = (val/entropy) + gain1 + gain2
            self.interactionList.append((total, (gain1, gain2, attrIndex1, attrIndex2)))
        self.interactionList.sort()
        self.interactionList.reverse()

        f = open('interaction.dot','w')
        self.interactionMatrix.exportGraph(f, significant_digits=3,positive_int=8,negative_int=8,absolute_int=0,url=1)
        f.flush()
        f.close()
        
        self.graph = self.interactionMatrix.exportNetwork(significant_digits=3,positive_int=8,negative_int=8,absolute_int=0)
        
        # execute dot and save otuput to pipes
        (pipePngOut, pipePngIn) = os.popen2("dot interaction.dot -Tpng", "b")
        (pipePlainOut, pipePlainIn) = os.popen2("dot interaction.dot -Tismap", "t")
        textPng = ""
        #textPng = pipePngIn.read()
        textPlainList = pipePlainIn.readlines()
        pipePngIn.close()
        pipePlainIn.close()
        pipePngOut.close()
        pipePlainOut.close()
        os.remove('interaction.dot')

        # if the output from the pipe was empty, then the software isn't installed correctly
        if len(textPng) == 0:
            pass
            #self.error(0, "This widget needs 'graphviz' software package. You can freely download it from the web.")
        
         # create a picture
        pixmap = QPixmap()
        #pixmap.loadFromData(textPng)
        canvasPixmap = self.canvasR.addPixmap(pixmap)
        width = canvasPixmap.pixmap().width()
        height = canvasPixmap.pixmap().height()

        # hide all rects
        for rects in list(self.rectIndices.values()):
            rects.hide()

        self.rectIndices = {}       # QRect rectangles
        self.rectNames   = {}       # info about rectangle names (attributes)
        self.lines = []             # dict of form (rectName1, rectName2):(labelQPoint, [p1QPoint, p2QPoint, ...])

        self.parseGraphData(data, textPlainList, width, height)
        self.initLists(data)   # add all attributes found in .dot file to shown list
        self.showInteractionRects(data) # use interaction matrix to fill the left canvas with rectangles

        self.updateCanvas()

        self.send("Examples", data)
        self.send("Network", self.graph)
Exemple #5
0
    def updateNewData(self, data):
        if not data:
            return
        self.data = data
        self.interactionMatrix = orngInteract.InteractionMatrix(
            data, dependencies_too=1)

        # save discretized data and repair invalid names
        for attr in self.interactionMatrix.discData.domain.attributes:
            attr.name = attr.name.replace("ED_", "")
            attr.name = attr.name.replace("D_", "")
            attr.name = attr.name.replace("M_", "")

        self.interactionList = []
        entropy = self.interactionMatrix.entropy
        if entropy == 0.0: return

        ################################
        # create a sorted list of total information
        for ((val, (val2, attrIndex1,
                    attrIndex2))) in self.interactionMatrix.list:
            gain1 = self.interactionMatrix.gains[attrIndex1] / entropy
            gain2 = self.interactionMatrix.gains[attrIndex2] / entropy
            total = (val / entropy) + gain1 + gain2
            self.interactionList.append(
                (total, (gain1, gain2, attrIndex1, attrIndex2)))
        self.interactionList.sort()
        self.interactionList.reverse()

        import tempfile, os
        fhandle, fname = tempfile.mkstemp('dot')
        os.close(fhandle)
        f = open(fname, "wt")
        self.interactionMatrix.exportGraph(f,
                                           significant_digits=3,
                                           positive_int=8,
                                           negative_int=8,
                                           absolute_int=0,
                                           url=1)
        f.close()
        del f

        # execute dot
        import subprocess
        try:
            textPng = subprocess.Popen(["dot", fname, "-Tpng"],
                                       stdout=subprocess.PIPE).communicate()[0]
            textPlainList = subprocess.Popen(
                ["dot", fname, "-Tismap"],
                stdout=subprocess.PIPE).communicate()[0].splitlines()
        except OSError as ex:
            textPng = ""
            textPlainList = []

        os.remove(fname)

        # if the output from the pipe was empty, then the software isn't installed correctly
        if len(textPng) == 0:
            self.error(
                0,
                "This widget needs 'graphviz' software package. You can freely download it from the web."
            )

        # create a picture
        pixmap = QPixmap()
        pixmap.loadFromData(textPng)
        if hasattr(self, "canvasPixmap"):
            self.canvasPixmap.setPixmap(pixmap)
        else:
            self.canvasPixmap = self.canvasR.addPixmap(pixmap)

        width = self.canvasPixmap.pixmap().width()
        height = self.canvasPixmap.pixmap().height()

        self.canvasR.setSceneRect(
            QRectF(0, 0, width, height).adjusted(-10, -10, 10, 10))
        #self.canvasR.resize(width, height)

        # hide all rects
        for rects in self.rectIndices.values():
            rects.hide()

        self.rectIndices = {}  # QRect rectangles
        self.rectNames = {}  # info about rectangle names (attributes)
        self.lines = [
        ]  # dict of form (rectName1, rectName2):(labelQPoint, [p1QPoint, p2QPoint, ...])

        self.parseGraphData(data, textPlainList, width, height)
        self.initLists(
            data)  # add all attributes found in .dot file to shown list
        self.showInteractionRects(
            data
        )  # use interaction matrix to fill the left canvas with rectangles

        #self.canvasL.update()
        #self.canvasR.update()

        self.send("Examples", data)
Exemple #6
0
    def updateNewData(self, data):
        if not data:
            return
        self.data = data
        self.interactionMatrix = orngInteract.InteractionMatrix(data, dependencies_too=1)

        # save discretized data and repair invalid names
        for attr in self.interactionMatrix.discData.domain.attributes:
            attr.name = attr.name.replace("ED_","")
            attr.name = attr.name.replace("D_","")
            attr.name = attr.name.replace("M_","")

        self.interactionList = []
        entropy = self.interactionMatrix.entropy
        if entropy == 0.0: return

        ################################
        # create a sorted list of total information
        for ((val,(val2, attrIndex1, attrIndex2))) in self.interactionMatrix.list:
            gain1 = self.interactionMatrix.gains[attrIndex1] / entropy
            gain2 = self.interactionMatrix.gains[attrIndex2] / entropy
            total = (val/entropy) + gain1 + gain2
            self.interactionList.append((total, (gain1, gain2, attrIndex1, attrIndex2)))
        self.interactionList.sort()
        self.interactionList.reverse()

        import tempfile, os
        fhandle, fname = tempfile.mkstemp('.dot')
        os.close(fhandle)
        f = file(fname, "wt")
        self.interactionMatrix.exportGraph(f, significant_digits=3,positive_int=8,negative_int=8,absolute_int=0,url=1)
        f.flush()
        f.close()
        del f

        import subprocess
        def run_dot(dotfile, format):
            # execute dot command to retrieve an image in 'format'
            # and an ismap formated dot output.
            # Using temp files instead of direct pipe output due to EINTR
            # errors
            image_handle, image_name = tempfile.mkstemp('.' + format)
            os.close(image_handle)
            
            ismap_handle, ismap_name = tempfile.mkstemp('.ismap')
            os.close(ismap_handle)
            
#            print image_name
#            subprocess.check_call(" ".join(["dot", fname, "-T" + format, "-o" + image_name]), shell=True)
#            subprocess.check_call(" ".join(["dot", fname, "-Tismap", "-o" + ismap_name]), shell=True)
            subprocess.check_call(["dot", fname, "-T" + format, "-o" + image_name])
            subprocess.check_call(["dot", fname, "-Tismap", "-o" + ismap_name])

            
            image_data = open(image_name, "rb").read()
            ismap_data = open(ismap_name, "rb").read()
            
            os.remove(image_name)
            os.remove(ismap_name)
            
            return image_data, ismap_data

        self.error([0, 1])
        ismap = ""
        image = ""
        pixmap = QPixmap()
        try:
            # testing different formats, sometimes the png output from 'dot'
            # fails to load in Qt (no idea why).
            for format in ["png", "gif", "jpeg", "svg"]: 
                image, ismap = run_dot(fname, format)
                qimage = QImage.fromData(QByteArray(image), format)
                pixmap = QPixmap.fromImage(qimage)
                if not pixmap.isNull():
                    break
        except OSError, ex:
            self.error(0, "This widget needs 'graphviz' software package. You can freely download it from the web.")