Example #1
0
def loessMA(m, windowSize, axis=0, approxMasked=True, verbose=False, callback=None):
    """Returns a new array with values at the given axis smoothed by loess;
    if approxMasked==True: the masked values are approximated by loess;
    assumes equidistant spacing of points on the given axis.
    """
    assert 0 < windowSize <= m.shape[axis]+0.1, "0 < windowSize[%s] <= 1 OR windowSize in range(1.1,m.shape[axis]+1) expected, got %f" % ("%", windowSize)
    m = MA.asarray(m)
    if m.dtype.char <> Numeric.Float:
        m = m.astype(Numeric.Float)
    shp_other = list(m.shape)
    shp_other.pop(axis)
    # get a transposed and reshaped mask and data from m; if m.mask() == None, construct a new array of zeros
    mask = Numeric.reshape(Numeric.transpose(MA.getmaskarray(m), [axis] + range(0,axis) + range(axis+1,len(m.shape))), (m.shape[axis], Numeric.multiply.reduce(shp_other)))
    data = MA.reshape(MA.transpose(m, [axis] + range(0,axis) + range(axis+1,len(m.shape))), (m.shape[axis], Numeric.multiply.reduce(shp_other)))
    maskInv = -1*(mask-1)
    xall = Numeric.arange(data.shape[0])
    xallList = xall.tolist()
    for ii in Numeric.compress(Numeric.add.reduce(maskInv,0) > 1, range(data.shape[1])):    # run loess if the profile contains more than 2 values
        try:
            data[:,ii] = MA.array(statc.loess(zip(MA.compress(maskInv[:,ii], xall).tolist(), MA.compress(maskInv[:,ii], data[:,ii]).tolist()), xallList, windowSize))[:,1]
        except:
            if verbose:
                print "Warning: loessMA: could not loess axis %i index %i" % (axis, ii)
        if callback:
            callback()
    if not approxMasked:
        data = MA.array(data, mask=mask)
    return MA.transpose(MA.reshape(data, [m.shape[axis]] + shp_other), [axis] + range(0,axis) + range(axis+1,len(m.shape)))
    def refreshPureVisibleOutcomes(self):
        if self.dataHasDiscreteClass:
            return
        keys=self.hdata.keys()
        if self.variableContinuous:
            keys.sort()
        self.clear()
        self.tips.removeAll()
        cn=0
        for key in keys:
            ckey = PolygonCurve(pen=QPen(Qt.black), brush=QBrush(Qt.gray))
            ckey.attach(self)
            ckey.setRenderHint(QwtPlotItem.RenderAntialiased, self.useAntialiasing)
            if self.variableContinuous:
                ckey.setData([key, key + self.subIntervalStep, key + self.subIntervalStep, key],[0, 0, self.hdata[key], self.hdata[key]])
                ff="%."+str(self.data.domain[self.attributeName].numberOfDecimals+1)+"f"
                text = "N(%s in ("+ff+","+ff+"])=<b>%i</b>"
                text = text%(str(self.attributeName), key, key+self.subIntervalStep, self.hdata[key])
                self.tips.addToolTip(key+self.subIntervalStep/2.0, self.hdata[key]/2.0, text, self.subIntervalStep/2.0, self.hdata[key]/2.0)
            else:
                tmpx = cn - (self.barSize/2.0)/100.0
                tmpx2 = cn + (self.barSize/2.0)/100.0
                ckey.setData([tmpx, tmpx2, tmpx2, tmpx], [0, 0, self.hdata[key], self.hdata[key]])
                text = "N(%s=%s)=<b>%i</b>"%(str(self.attributeName), str(key), self.hdata[key])
                self.tips.addToolTip(cn, self.hdata[key]/2.0, text, (self.barSize/2.0)/100.0, self.hdata[key]/2.0)
                cn+=1

        if self.dataHasClass and not self.dataHasDiscreteClass and self.showContinuousClassGraph:
            self.enableYRaxis(1)
            self.setAxisAutoScale(QwtPlot.yRight)
            self.setYRaxisTitle(str(self.data.domain.classVar.name))
            if self.variableContinuous:
                equiDist = orange.EquiDistDiscretization(numberOfIntervals = self.numberOfBars)
                d_variable = equiDist(self.attributeName, self.data)
                d_data=self.data.select([d_variable, self.data.domain.classVar])
                c=orange.ContingencyAttrClass(d_variable, d_data)
                XY=[(key+self.subIntervalStep/2.0, val.average()) for key, val in zip(keys, c.values()) if val.cases]
                if len(XY) >= 2:
                    XY=statc.loess(XY, 10, 4.0, 1)
            else:
                d_data=orange.ContingencyAttrClass(self.attributeName, self.data)
                XY=[(i, dist.average()) for i, dist in zip(range(len(d_data.values())), d_data.values()) if dist.cases]
            key = self.addCurve(QwtPlot.xBottom, QwtPlot.yRight)
            key.setData([a[0] for a in XY], [a[1] for a in XY])
            if self.variableContinuous:
                key.setPen(QPen(Qt.black))
            else:
                key.setStyle(QwtPlotCurve.Dots)
                key.setSymbol(QwtSymbol(QwtSymbol.Diamond, QBrush(Qt.color0), QPen(Qt.black, 2), QSize(7,7)))
        else:
            self.enableYRaxis(0)
            self.setAxisScale(QwtPlot.yRight, 0.0, 1.0, 0.1)

        self.probCurveKey = self.addCurve(QwtPlot.xBottom, QwtPlot.yRight)
        self.probCurveUpperCIKey = self.addCurve(QwtPlot.xBottom, QwtPlot.yRight)
        self.probCurveLowerCIKey = self.addCurve(QwtPlot.xBottom, QwtPlot.yRight)

        self.replot()
    def refreshPureVisibleOutcomes(self):
        if self.dataHasDiscreteClass:
            return
        keys=self.hdata.keys()
        if self.variableContinuous:
            keys.sort()
        self.clear()
        self.tips.removeAll()
        cn=0
        for key in keys:
            ckey = PolygonCurve(pen=self.color(OWPalette.Data), brush=QBrush(self.color(OWPalette.Grid)))
            ckey.attach(self)
            if self.variableContinuous:
                ckey.setData([key, key + self.subIntervalStep, key + self.subIntervalStep, key],[0, 0, self.hdata[key], self.hdata[key]])
                ff="%."+str(self.data.domain[self.attributeName].numberOfDecimals+1)+"f"
                text = "N(%s in ("+ff+","+ff+"])=<b>%i</b>"
                text = text % (Qt.escape(str(self.attributeName)), key, key+self.subIntervalStep, self.hdata[key])
                self.tips.addToolTip(key+self.subIntervalStep/2.0, self.hdata[key]/2.0, text, self.subIntervalStep/2.0, self.hdata[key]/2.0)
            else:
                tmpx = cn - (self.barSize/2.0)/100.0
                tmpx2 = cn + (self.barSize/2.0)/100.0
                ckey.setData([tmpx, tmpx2, tmpx2, tmpx], [0, 0, self.hdata[key], self.hdata[key]])
                text = "N(%s=%s)=<b>%i</b>"%(Qt.escape(str(self.attributeName)), Qt.escape(str(key)), self.hdata[key])
                self.tips.addToolTip(cn, self.hdata[key]/2.0, text, (self.barSize/2.0)/100.0, self.hdata[key]/2.0)
                cn+=1

        if self.dataHasClass and not self.dataHasDiscreteClass and self.showContinuousClassGraph:
            self.enableYRaxis(1)
#            self.setAxisAutoScale(yRight)
            self.setYRaxisTitle(str(self.data.domain.classVar.name))
            if self.variableContinuous:
                equiDist = orange.EquiDistDiscretization(numberOfIntervals = self.numberOfBars)
                d_variable = equiDist(self.attributeName, self.data)
                d_data=self.data.select([d_variable, self.data.domain.classVar])
                c=orange.ContingencyAttrClass(d_variable, d_data)
                XY=[(key+self.subIntervalStep/2.0, val.average()) for key, val in zip(keys, c.values()) if val.cases]
                XY=statc.loess(XY, 10, 4.0, 1)
            else:
                d_data=orange.ContingencyAttrClass(self.attributeName, self.data)
                XY=[(i, dist.average()) for i, dist in zip(range(len(d_data.values())), d_data.values()) if dist.cases]
            key = self.addCurve(xBottom, yRight)
            key.setData([a[0] for a in XY], [a[1] for a in XY])
            key.set_color(self.color(OWPalette.Data))
            if not self.variableContinuous:
                key.set_symbol(OWPoint.Diamond)
                key.set_point_size(7)
        else:
            self.enableYRaxis(0)
            self.setAxisScale(yRight, -0.05, 1.05, 0.1)

        self.probCurveKey = self.addCurve(xBottom, yRight)
        self.probCurveUpperCIKey = self.addCurve(xBottom, yRight)
        self.probCurveLowerCIKey = self.addCurve(xBottom, yRight)

        self.replot()
Example #4
0
def loessMA(m,
            windowSize,
            axis=0,
            approxMasked=True,
            verbose=False,
            callback=None):
    """Returns a new array with values at the given axis smoothed by loess;
    if approxMasked==True: the masked values are approximated by loess;
    assumes equidistant spacing of points on the given axis.
    """
    assert 0 < windowSize <= m.shape[
        axis] + 0.1, "0 < windowSize[%s] <= 1 OR windowSize in range(1.1,m.shape[axis]+1) expected, got %f" % (
            "%", windowSize)
    m = MA.asarray(m)
    if m.dtype.char <> Numeric.Float:
        m = m.astype(Numeric.Float)
    shp_other = list(m.shape)
    shp_other.pop(axis)
    # get a transposed and reshaped mask and data from m; if m.mask() == None, construct a new array of zeros
    mask = Numeric.reshape(
        Numeric.transpose(MA.getmaskarray(m), [axis] + range(0, axis) +
                          range(axis + 1, len(m.shape))),
        (m.shape[axis], Numeric.multiply.reduce(shp_other)))
    data = MA.reshape(
        MA.transpose(m,
                     [axis] + range(0, axis) + range(axis + 1, len(m.shape))),
        (m.shape[axis], Numeric.multiply.reduce(shp_other)))
    maskInv = -1 * (mask - 1)
    xall = Numeric.arange(data.shape[0])
    xallList = xall.tolist()
    for ii in Numeric.compress(
            Numeric.add.reduce(maskInv, 0) > 1, range(data.shape[1])
    ):  # run loess if the profile contains more than 2 values
        try:
            data[:, ii] = MA.array(
                statc.loess(
                    zip(
                        MA.compress(maskInv[:, ii], xall).tolist(),
                        MA.compress(maskInv[:, ii], data[:, ii]).tolist()),
                    xallList, windowSize))[:, 1]
        except:
            if verbose:
                print "Warning: loessMA: could not loess axis %i index %i" % (
                    axis, ii)
        if callback:
            callback()
    if not approxMasked:
        data = MA.array(data, mask=mask)
    return MA.transpose(MA.reshape(data, [m.shape[axis]] + shp_other), [axis] +
                        range(0, axis) + range(axis + 1, len(m.shape)))
Example #5
0
    def refreshPureVisibleOutcomes(self):
        if self.dataHasDiscreteClass:
            return
        keys = self.hdata.keys()
        if self.variableContinuous:
            keys.sort()
        self.clear()
        self.tips.removeAll()
        cn = 0
        for key in keys:
            ckey = PolygonCurve(pen=QPen(Qt.black), brush=QBrush(Qt.gray))
            ckey.attach(self)
            ckey.setRenderHint(QwtPlotItem.RenderAntialiased,
                               self.useAntialiasing)
            if self.variableContinuous:
                ckey.setData([
                    key, key + self.subIntervalStep,
                    key + self.subIntervalStep, key
                ], [0, 0, self.hdata[key], self.hdata[key]])
                ff = "%." + str(self.data.domain[
                    self.attributeName].numberOfDecimals + 1) + "f"
                text = "N(%s in (" + ff + "," + ff + "])=<b>%i</b>"
                text = text % (str(self.attributeName), key,
                               key + self.subIntervalStep, self.hdata[key])
                self.tips.addToolTip(key + self.subIntervalStep / 2.0,
                                     self.hdata[key] / 2.0, text,
                                     self.subIntervalStep / 2.0,
                                     self.hdata[key] / 2.0)
            else:
                tmpx = cn - (self.barSize / 2.0) / 100.0
                tmpx2 = cn + (self.barSize / 2.0) / 100.0
                ckey.setData([tmpx, tmpx2, tmpx2, tmpx],
                             [0, 0, self.hdata[key], self.hdata[key]])
                text = "N(%s=%s)=<b>%i</b>" % (str(
                    self.attributeName), str(key), self.hdata[key])
                self.tips.addToolTip(cn, self.hdata[key] / 2.0, text,
                                     (self.barSize / 2.0) / 100.0,
                                     self.hdata[key] / 2.0)
                cn += 1

        if self.dataHasClass and not self.dataHasDiscreteClass and self.showContinuousClassGraph:
            self.enableYRaxis(1)
            self.setAxisAutoScale(QwtPlot.yRight)
            self.setYRaxisTitle(str(self.data.domain.classVar.name))
            if self.variableContinuous:
                equiDist = orange.EquiDistDiscretization(
                    numberOfIntervals=self.numberOfBars)
                d_variable = equiDist(self.attributeName, self.data)
                d_data = self.data.select(
                    [d_variable, self.data.domain.classVar])
                c = orange.ContingencyAttrClass(d_variable, d_data)
                XY = [(key + self.subIntervalStep / 2.0, val.average())
                      for key, val in zip(keys, c.values()) if val.cases]
                if len(XY) >= 2:
                    XY = statc.loess(XY, 10, 4.0, 1)
            else:
                d_data = orange.ContingencyAttrClass(self.attributeName,
                                                     self.data)
                XY = [(i, dist.average()) for i, dist in zip(
                    range(len(d_data.values())), d_data.values())
                      if dist.cases]
            key = self.addCurve(QwtPlot.xBottom, QwtPlot.yRight)
            key.setData([a[0] for a in XY], [a[1] for a in XY])
            if self.variableContinuous:
                key.setPen(QPen(Qt.black))
            else:
                key.setStyle(QwtPlotCurve.Dots)
                key.setSymbol(
                    QwtSymbol(QwtSymbol.Diamond, QBrush(Qt.color0),
                              QPen(Qt.black, 2), QSize(7, 7)))
        else:
            self.enableYRaxis(0)
            self.setAxisScale(QwtPlot.yRight, 0.0, 1.0, 0.1)

        self.probCurveKey = self.addCurve(QwtPlot.xBottom, QwtPlot.yRight)
        self.probCurveUpperCIKey = self.addCurve(QwtPlot.xBottom,
                                                 QwtPlot.yRight)
        self.probCurveLowerCIKey = self.addCurve(QwtPlot.xBottom,
                                                 QwtPlot.yRight)

        self.replot()