Ejemplo n.º 1
0
    def _autoscale(self, vars, datamin=None, datamax=None):
        from fipy.tools import numerix

        if datamin is None:
            datamin = 1e300
            for var in vars:
                datamin = min(datamin, numerix.nanmin(var))

        if datamax is None:
            from fipy.tools import numerix
            datamax = -1e300
            for var in vars:
                datamax = max(datamax, numerix.nanmax(var))

        return datamin, datamax
Ejemplo n.º 2
0
    def _autoscale(self, vars, datamin=None, datamax=None):
        from fipy.tools import numerix

        if datamin is None:
            datamin = 1e300
            for var in vars:
                datamin = min(datamin, numerix.nanmin(var))

        if datamax is None:
            from fipy.tools import numerix
            datamax = -1e300
            for var in vars:
                datamax = max(datamax, numerix.nanmax(var))

        return datamin, datamax
    def plot(self, matrix, RHSvector, log='auto'):
        import tempfile
        import os

        if "print" in os.environ['FIPY_DISPLAY_MATRIX'].lower().split():
            print("-" * 75)
            print(self.title)
            print("-" * 75)
            print("L:")
            print(matrix)
            print("b:", RHSvector)

        (f, mtxName) = tempfile.mkstemp(suffix='.mtx')
        matrix.exportMmf(mtxName)
        mtx = mmio.mmread(mtxName)
        os.remove(mtxName)

        pyplot.ion()

        c = mtx.tocoo()
        y = c.row
        x = c.col
        z = c.data
        N = matrix._shape[0]

        b = RHSvector
        if numerix.shape(b) == ():
            b = numerix.zeros((N, ), 'l')

        if len(z) == 0:
            y = numerix.zeros((1, ), 'l')
            x = numerix.zeros((1, ), 'l')
            z = numerix.zeros((1, ), 'l')

        def signed_to_logs(v):
            return (numerix.where(v > 0, numerix.log10(v), numerix.nan),
                    numerix.where(v < 0, numerix.log10(-v), numerix.nan))

        def logs_to_signed(v, plus, minus):
            v = numerix.where(v > 0, plus, -minus)
            v = numerix.where(numerix.isnan(v), 0., v)

            return v

        zPlus, zMinus = signed_to_logs(z)
        bPlus, bMinus = signed_to_logs(b)

        logs = (zPlus, zMinus, bPlus, bMinus)

        log = ((log == True)
               or (log == 'auto' and
                   (numerix.nanmax(numerix.concatenate(logs)) -
                    numerix.nanmin(numerix.concatenate(logs)) > 2)))

        if log:
            zMin = numerix.nanmin(numerix.concatenate(logs))
            zMax = numerix.nanmax(numerix.concatenate(logs))

            zMin -= 0.5

            numdec = numerix.floor(zMax) - numerix.ceil(zMin)
            if numdec < 0:
                zMax += 0.5

            for v in logs:
                v -= zMin

            zRange = zMax - zMin

            if zRange == 0:
                zRange = numerix.nanmax(zPlus) + 1

            z = logs_to_signed(z, zPlus, zMinus)
            b = logs_to_signed(b, bPlus, bMinus)

            fmt = SignedLogFormatter(threshold=zMin)
            loc = SignedLogLocator(threshold=zMin)

        else:
            zRange = max(abs(numerix.concatenate((z, b))))

            if zRange == 0:
                zRange = 1

            fmt = None
            loc = None

        pyplot.ioff()

        fig = pyplot.figure(self.id)
        fig.clf()

        usetex = rcParams['text.usetex']
        rcParams['text.usetex'] = False

        cmap = cm.RdBu

        norm = Normalize(vmin=-zRange, vmax=zRange)

        x0 = self.margin
        L_ax = fig.add_axes([
            x0 / self.aspect, self.margin, self.L_width / self.aspect,
            self.L_width
        ])
        L_ax.text(0.5,
                  -0.1,
                  "L",
                  transform=L_ax.transAxes,
                  horizontalalignment='center',
                  verticalalignment='baseline')

        x0 += self.L_width + self.buffer
        c_ax = fig.add_axes([
            x0 / self.aspect, self.margin, self.c_width / self.aspect,
            self.L_width
        ])

        x0 += self.c_width + self.buffer
        b_ax = fig.add_axes([
            x0 / self.aspect, self.margin, self.b_width / self.aspect,
            self.L_width
        ],
                            sharey=L_ax)
        b_ax.text(0.5,
                  -0.1,
                  "b",
                  transform=b_ax.transAxes,
                  horizontalalignment='center',
                  verticalalignment='baseline')

        def scatterRectangles(x, y, z, norm=None, cmap=None):
            patches = [
                Rectangle(numerix.array([X - 0.5, Y - 0.5]),
                          1.,
                          1.,
                          edgecolor='none') for X, Y in zip(x, y)
            ]

            collection = PatchCollection(patches,
                                         norm=norm,
                                         cmap=cmap,
                                         edgecolors='none')
            collection.set_array(z)

            return collection

        L_ax.add_collection(
            scatterRectangles(x=x, y=y, z=z, norm=norm, cmap=cmap))

        b_ax.add_collection(
            scatterRectangles(x=numerix.zeros((N, ), 'l'),
                              y=numerix.arange(N),
                              z=b,
                              norm=norm,
                              cmap=cmap))

        ColorbarBase(ax=c_ax,
                     cmap=cmap,
                     norm=norm,
                     orientation='vertical',
                     format=fmt,
                     ticks=loc)

        pyplot.setp((b_ax.get_xticklabels(), b_ax.get_yticklabels(),
                     b_ax.get_xticklines(), b_ax.get_yticklines()),
                    visible=False)

        L_ax.set_xlim(xmin=-0.5, xmax=N - 0.5)
        L_ax.set_ylim(ymax=-0.5, ymin=N - 0.5)

        b_ax.set_xlim(xmin=-0.5, xmax=0.5)
        b_ax.set_ylim(ymax=-0.5, ymin=N - 0.5)

        fig.suptitle(self.title, x=0.5, y=0.95, fontsize=14)

        pyplot.draw()

        rcParams['text.usetex'] = usetex
Ejemplo n.º 4
0
    def plot(self, matrix, RHSvector, log='auto'):
        import tempfile
        import os

        if "print" in os.environ['FIPY_DISPLAY_MATRIX'].lower().split():
            print("-"*75)
            print(self.title)
            print("-"*75)
            print("L:")
            print(matrix)
            print("b:", RHSvector)

        (f, mtxName) = tempfile.mkstemp(suffix='.mtx')
        matrix.exportMmf(mtxName)
        mtx = mmio.mmread(mtxName)
        os.remove(mtxName)

        pyplot.ion()

        c = mtx.tocoo()
        y = c.row
        x = c.col
        z = c.data
        N = matrix._shape[0]

        b = RHSvector
        if numerix.shape(b) == ():
            b = numerix.zeros((N,), 'l')

        if len(z) == 0:
            y = numerix.zeros((1,), 'l')
            x = numerix.zeros((1,), 'l')
            z = numerix.zeros((1,), 'l')

        def signed_to_logs(v):
            return (numerix.where(v > 0, numerix.log10(v), numerix.nan),
                    numerix.where(v < 0, numerix.log10(-v), numerix.nan))

        def logs_to_signed(v, plus, minus):
            v = numerix.where(v > 0, plus, -minus)
            v = numerix.where(numerix.isnan(v), 0., v)

            return v

        zPlus, zMinus = signed_to_logs(z)
        bPlus, bMinus = signed_to_logs(b)

        logs = (zPlus, zMinus, bPlus, bMinus)

        log = ((log == True)
               or (log == 'auto'
                   and (numerix.nanmax(numerix.concatenate(logs))
                        - numerix.nanmin(numerix.concatenate(logs)) > 2)))

        if log:
            zMin = numerix.nanmin(numerix.concatenate(logs))
            zMax = numerix.nanmax(numerix.concatenate(logs))

            zMin -= 0.5

            numdec = numerix.floor(zMax) - numerix.ceil(zMin)
            if numdec < 0:
                zMax += 0.5

            for v in logs:
                v -= zMin

            zRange = zMax - zMin

            if zRange == 0:
                zRange = numerix.nanmax(zPlus) + 1

            z = logs_to_signed(z, zPlus, zMinus)
            b = logs_to_signed(b, bPlus, bMinus)

            fmt = SignedLogFormatter(threshold=zMin)
            loc = SignedLogLocator(threshold=zMin)

        else:
            zRange = max(abs(numerix.concatenate((z, b))))

            if zRange == 0:
                zRange = 1

            fmt = None
            loc = None


        pyplot.ioff()

        fig = pyplot.figure(self.id)
        fig.clf()

        usetex = rcParams['text.usetex']
        rcParams['text.usetex'] = False

        cmap = cm.RdBu

        norm = Normalize(vmin=-zRange, vmax=zRange)

        x0 = self.margin
        L_ax = fig.add_axes([x0 / self.aspect, self.margin, self.L_width / self.aspect, self.L_width])
        L_ax.text(0.5, -0.1, "L",
                  transform=L_ax.transAxes, horizontalalignment='center', verticalalignment='baseline')

        x0 += self.L_width + self.buffer
        c_ax = fig.add_axes([x0 / self.aspect, self.margin, self.c_width / self.aspect, self.L_width])

        x0 += self.c_width + self.buffer
        b_ax = fig.add_axes([x0 / self.aspect, self.margin, self.b_width / self.aspect, self.L_width],
                            sharey=L_ax)
        b_ax.text(0.5, -0.1, "b",
                  transform=b_ax.transAxes, horizontalalignment='center', verticalalignment='baseline')



        def scatterRectangles(x, y, z, norm=None, cmap=None):
            patches = [Rectangle(numerix.array([X - 0.5, Y - 0.5]), 1., 1.,
                                 edgecolor='none') for X, Y in zip(x, y)]

            collection = PatchCollection(patches, norm=norm, cmap=cmap,
                                         edgecolors='none')
            collection.set_array(z)

            return collection

        L_ax.add_collection(scatterRectangles(x=x, y=y, z=z,
                                              norm=norm, cmap=cmap))

        b_ax.add_collection(scatterRectangles(x=numerix.zeros((N,), 'l'), y=numerix.arange(N), z=b,
                                              norm=norm, cmap=cmap))

        ColorbarBase(ax=c_ax, cmap=cmap, norm=norm, orientation='vertical',
                     format=fmt, ticks=loc)

        pyplot.setp((b_ax.get_xticklabels(),
                     b_ax.get_yticklabels(),
                     b_ax.get_xticklines(),
                     b_ax.get_yticklines()), visible=False)

        L_ax.set_xlim(xmin=-0.5, xmax=N-0.5)
        L_ax.set_ylim(ymax=-0.5, ymin=N-0.5)

        b_ax.set_xlim(xmin=-0.5, xmax=0.5)
        b_ax.set_ylim(ymax=-0.5, ymin=N-0.5)

        fig.suptitle(self.title, x=0.5, y=0.95, fontsize=14)

        pyplot.draw()

        rcParams['text.usetex'] = usetex
    def plot(self, matrix, RHSvector, log='auto'):
        import tempfile
        import os
        
        (f, mtxName) = tempfile.mkstemp(suffix='.mtx')
        matrix.exportMmf(mtxName)
        mtx = mmio.mmread(mtxName)
##         f.close()
        os.remove(mtxName)
        
        pylab.ion()
        
        c = mtx.tocoo()
        y = c.row
        x = c.col
        z = c.data
        
        b = RHSvector
        
        if len(z) == 0:
            y = zeros((1,))
            x = zeros((1,))
            z = zeros((1,))

        zPlus = where(z > 0, log10(z), nan)
        zMinus = where(z < 0, log10(-z), nan)
        bPlus = where(b > 0, log10(b), nan)
        bMinus = where(b < 0, log10(-b), nan)

        if (log == True
            or (log == 'auto' 
                and (max(zPlus) - min(zPlus) > 2
                     or max(zMinus) - min(zMinus) > 2
                     or max(bPlus) - min(bPlus) > 2
                     or max(bMinus) - min(bMinus) > 2))):
            log = True
        else:
            log = False
            
        if log:
            zMin = nanmin((nanmin(zPlus), nanmin(zMinus), nanmin(bPlus), nanmin(bMinus)))
            zMax = nanmax((nanmax(zPlus), nanmax(zMinus), nanmax(bPlus), nanmax(bMinus)))
##             zThreshold = 0.5 # (zMax - zMin) / 5.
            
            zMin -= 0.5
            
            numdec = math.floor(zMax)-math.ceil(zMin)
            if numdec < 0:
                zMax += 0.5
            
            zPlus -= zMin
            zMinus -= zMin
            bPlus -= zMin
            bMinus -= zMin
            zRange = zMax - zMin
            
            if zRange == 0:
                zRange = nanmax(zPlus) + 1

            z = where(z > 0, zPlus, -zMinus)
            z = where(isnan(z), 0., z)
            b = where(b > 0, bPlus, -bMinus)
            b = where(isnan(b), 0., b)

            fmt = SignedLogFormatter(threshold=zMin)
            loc = SignedLogLocator(threshold=zMin)
            
        else:
            zRange = max(max(abs(z)), max(abs(b)))
        
            if zRange == 0:
                zRange = 1

            fmt = None
            loc = None
            

        N = matrix._getShape()[0]
        saveSize = pylab.rcParams['figure.figsize']
        size = pylab.rcParams['figure.dpi'] **2 * saveSize[0] * saveSize[1] / N**2

        pylab.ioff()
        
        pylab.figure(self.id)
        pylab.clf()

        pylab.delaxes()
        ax1 = pylab.axes([self.margin, self.margin, self.width, self.width])
        
        Mscat = pylab.scatter(x, y, c=z, 
                              vmin=-zRange, vmax=zRange, edgecolors='none', 
                              cmap=pylab.get_cmap('RdBu'), marker='s', s=size)
                             
        ax2 = pylab.axes([self.width + self.margin, self.margin, (self.width / self.aspect) / N, self.width], 
                         sharey=ax1)

        bscat = pylab.scatter(zeros((N,)), arange(N), c=b, 
                              vmin=-zRange, vmax=zRange, edgecolors='none', 
                              cmap=pylab.get_cmap('RdBu'), marker='s', s=size)

        pylab.setp((ax2.get_xticklabels(),
                    ax2.get_yticklabels(),
                    ax2.get_xticklines(),
                    ax2.get_yticklines()), visible=False)
        
        pylab.axes(ax1)
        pylab.axis([-0.5, N - 0.5, N - 0.5, -0.5])

        pylab.colorbar(format=fmt, ticks=loc)

        pylab.title(self.title)

        pylab.draw()