Пример #1
0
def line_plot(carr, ax=None, ylabel=None, figsize=None, title=None, **kwargs):
    import matplotlib.pyplot as plt
    import numpy as np

    # fix missing \text support
    from expresso.pycas import latex as rlatex
    latex = lambda x: rlatex(x).replace(r'\text', r'\mathrm')

    fig = None
    if ax == None:
        fig, ax = plt.subplots(figsize=figsize)

    if title:
        ax.set_title(title)

    e = get_unitless_bounds(carr)[0]

    prefix, factor = get_metric_prefix(e[:2])

    lines = ax.plot(
        np.linspace(
            float(e[0]) / factor,
            float(e[1]) / factor, carr.data.shape[0]), carr.data, **kwargs)
    ax.set_xlabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[0]), prefix, latex(e[2])))
    if ylabel: ax.set_ylabel(ylabel)

    if fig:
        plt.show()

    return lines[0]
Пример #2
0
def line_plot(carr,ax = None,ylabel = None,figsize = None,title = None,**kwargs):
    import matplotlib.pyplot as plt
    import numpy as np

    # fix missing \text support
    from expresso.pycas import latex as rlatex
    latex = lambda x:rlatex(x).replace(r'\text',r'\mathrm')

    fig = None
    if ax == None:
        fig, ax = plt.subplots(figsize=figsize)
    
    if title:
        ax.set_title(title)
    
    e = get_unitless_bounds(carr)[0]
    
    prefix,factor = get_metric_prefix(e[:2])
    
    lines = ax.plot(np.linspace(float(e[0])/factor,float(e[1])/factor,carr.data.shape[0]),carr.data, **kwargs)
    ax.set_xlabel("$%s$ [$%s %s$]" % (latex(carr.axis[0]),prefix,latex(e[2])))
    if ylabel: ax.set_ylabel(ylabel)

    if ax == None:
        plt.show()

    return lines[0]
Пример #3
0
    def callback(r,m):
        from IPython.display import display_latex

        print "appy rule: %s" % r

        lt = pc.latex(r.search.subs(m,evaluate=False)),\
             pc.latex(r.replacement.subs(m,evaluate=False)),\
             r"\;\text{ if }\;%s" % pc.latex(r.condition.subs(m,evaluate=False)) if r.condition is not None else ''

        display_latex(r"$$%s \rightarrow %s%s$$" % lt,raw=True)
Пример #4
0
    def callback(r,m):
        from IPython.display import display_latex
        import sys

        print("appy rule: %s" % r)

        lt = pc.latex(r.search.subs(m,evaluate=False)),\
             pc.latex(r.replacement.subs(m,evaluate=False)),\
             r"\;\text{ if }\;%s" % pc.latex(r.condition.subs(m,evaluate=False)) if r.condition is not None else ''

        display_latex(r"$$%s \rightarrow %s%s$$" % lt,raw=True)
        sys.stdout.flush()
Пример #5
0
def get_plot_coordinates(array):
    import numpy as np
    from expresso.pycas import latex
    e = get_unitless_bounds(array)[0]
    prefix, factor = get_metric_prefix(e[:2])
    return np.linspace(
        float(e[0]) / factor,
        float(e[1]) / factor, array.shape[0]), prefix + ' ' + latex(e[2])
Пример #6
0
def image_plot(carr, ax=None, figsize=None, title=None, **kwargs):
    import matplotlib.pyplot as plt

    # fix missing \text support
    from expresso.pycas import latex as rlatex
    latex = lambda x: rlatex(x).replace(r'\text', r'\mathrm')

    fig = None
    if ax == None:
        fig, ax = plt.subplots(figsize=figsize)
    if title:
        ax.set_title(title)

    e = get_unitless_bounds(carr)

    xprefix, xfactor = get_metric_prefix(e[1][:2])
    yprefix, yfactor = get_metric_prefix(e[0][:2])

    extent = [
        float(e[1][0]) / xfactor,
        float(e[1][1]) / xfactor,
        float(e[0][0]) / yfactor,
        float(e[0][1]) / yfactor
    ]

    if 'aspect' not in kwargs:
        kwargs['aspect'] = 'auto'
    if 'origin' not in kwargs:
        kwargs['origin'] = 'lower'

    image = ax.imshow(carr.data, extent=extent, **kwargs)
    ax.set_ylabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[0]), yprefix, latex(e[0][2])))
    ax.set_xlabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[1]), xprefix, latex(e[1][2])))

    if fig:
        fig.colorbar(image)
        plt.show()

    return image
Пример #7
0
def image_plot(carr,ax = None,figsize = None,title = None, **kwargs):
    import matplotlib.pyplot as plt

    # fix missing \text support
    from expresso.pycas import latex as rlatex
    latex = lambda x:rlatex(x).replace(r'\text',r'\mathrm')

    fig = None
    if ax == None:
        fig, ax = plt.subplots(figsize=figsize)
    if title:
        ax.set_title(title)
    
    e = get_unitless_bounds(carr)

    xprefix,xfactor = get_metric_prefix(e[1][:2])
    yprefix,yfactor = get_metric_prefix(e[0][:2])
        
    extent = [float(e[1][0])/xfactor,float(e[1][1])/xfactor,float(e[0][0])/yfactor,float(e[0][1])/yfactor]

    if 'aspect' not in kwargs:
        kwargs['aspect'] = 'auto'
    if 'origin' not in kwargs:
        kwargs['origin'] = 'lower'

    image = ax.imshow(carr.data, extent=extent, **kwargs )
    ax.set_ylabel("$%s$ [$%s %s$]" % (latex(carr.axis[0]),yprefix,latex(e[0][2])))
    ax.set_xlabel("$%s$ [$%s %s$]" % (latex(carr.axis[1]),xprefix,latex(e[1][2])))
    
    if fig:
        fig.colorbar(image)

    if ax == None:
        plt.show()

    return image
Пример #8
0
def poynting_streamplot_with_start_points(carr,
                                          k,
                                          start_points,
                                          color='w',
                                          ax=None,
                                          figsize=None,
                                          title=None,
                                          arrowsize=5,
                                          arrowpositions=[0.1, 0.9],
                                          set_limits=True,
                                          mask=None,
                                          support=None,
                                          settings=None,
                                          dxdy=None,
                                          **kwargs):
    import matplotlib.pyplot as plt
    import numpy as np
    import expresso.pycas as pc
    from pypropagate.phase_gradient import phase_gradient

    def streamlines(x, y, U, V, start_points):
        import scipy as sci
        import scipy.integrate
        import scipy.interpolate
        import numpy as np

        N = len(start_points)
        flat_start_values = np.array(start_points).flatten()

        dt = np.sqrt((x[1] - x[0])**2 + (y[1] - y[0])**2)
        NT = max(len(x), len(y))
        norm = np.sqrt(U**2 + V**2) * dt

        XY = (y, x)
        uinterp = sci.interpolate.RegularGridInterpolator(XY,
                                                          U / norm,
                                                          fill_value=0,
                                                          bounds_error=False)
        vinterp = sci.interpolate.RegularGridInterpolator(XY,
                                                          V / norm,
                                                          fill_value=0,
                                                          bounds_error=False)

        dx = [0]

        def interpolated_velocity(t, x):
            x = x.reshape((N, 2))
            res = np.stack([vinterp(x), uinterp(x)]).transpose().flatten()
            dx[0] = res
            return res

        integrator = sci.integrate.ode(interpolated_velocity).set_integrator(
            'dopri5')
        integrator.set_initial_value(flat_start_values, 0)

        integrated = np.zeros((NT, N, 2), dtype=float)
        integrated[0] = start_points

        for i in range(1, NT):
            if not integrator.successful():
                print "error"
                break
            integrated[i] = integrator.integrate(i * dt).reshape((N, 2))
            if np.all(dx[0] == 0):
                integrated = integrated[:i + 1]
                break

        return integrated

    e = get_unitless_bounds(carr)

    xprefix, xfactor = get_metric_prefix(e[1][:2])
    yprefix, yfactor = get_metric_prefix(e[0][:2])

    extent = [
        float(e[1][0]) / xfactor,
        float(e[1][1]) / xfactor,
        float(e[0][0]) / yfactor,
        float(e[0][1]) / yfactor
    ]

    x = np.linspace(extent[0], extent[1], carr.shape[1])
    y = np.linspace(extent[2], extent[3], carr.shape[0])

    if dxdy is None:
        gx, gy = phase_gradient(carr)
        gx /= xfactor * (x[0] - x[1])
        gy /= yfactor * (y[0] - y[1])
        gx += float(carr.evaluate(k * e[1][2]))
    else:
        gx, gy = dxdy

    gx *= yfactor / xfactor

    if support is not None:
        if mask is not None:
            raise ValueError('provide either support or mask arguments')

        if isinstance(support, pc.Expression):
            mask = pc.Not(support)
        else:
            mask = support.copy()
            mask.data = np.logical_not(support.data)

    if mask is not None:
        import expresso.pycas

        if isinstance(mask, pc.Expression):
            if settings == None:
                raise ValueError('no settings argument provided')
            mask = expression_for_array(mask, carr, settings)

        gx = np.ma.array(gx.data, mask=mask.data)
        gy = np.ma.array(gy.data, mask=mask.data)

    fig = None
    if ax == None:
        fig, ax = plt.subplots(figsize=figsize)
    if title:
        ax.set_title(title)

    start_points = [[
        float(carr.evaluate(sp[1] / (yfactor * e[0][2]))),
        float(carr.evaluate(sp[0] / (xfactor * e[1][2])))
    ] for sp in start_points]
    stream = streamlines(x, y, gx.data, gy.data, start_points)

    for i in range(stream.shape[1]):
        ax.plot(stream[:, i, 1], stream[:, i, 0], '-', color=color, **kwargs)

        import scipy
        xmin, xmax = stream[:, i, 1].min(), stream[:, i, 1].max()
        dx = (xmax - xmin) / len(stream[:, i, 1]) / 10
        xpositions = [xmin + ap * (xmax - xmin) for ap in arrowpositions]
        ypositions = scipy.interp(xpositions + [xp - dx for xp in xpositions],
                                  stream[:, i, 1], stream[:, i, 0])
        for i in range(len(arrowpositions)):
            size = arrowsize
            x0, y0, y1 = xpositions[i], ypositions[i], ypositions[
                i + len(arrowpositions)]
            props = dict(color=color,
                         width=0,
                         headwidth=size,
                         headlength=2 * size,
                         linewidth=0)
            ax.annotate("",
                        xy=(x0, y0),
                        xycoords='data',
                        xytext=(x0 - dx, y1),
                        textcoords='data',
                        arrowprops=props)

    if set_limits:
        ax.set_xlim(extent[0], extent[1])
        ax.set_ylim(extent[2], extent[3])

    from expresso.pycas import latex as rlatex
    latex = lambda x: rlatex(x).replace(r'\text', r'\mathrm')
    ax.set_ylabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[0]), yprefix, latex(e[0][2])))
    ax.set_xlabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[1]), xprefix, latex(e[1][2])))

    if fig:
        plt.show()

    return stream
Пример #9
0
def poynting_streamplot(carr,
                        k,
                        ax=None,
                        figsize=None,
                        title=None,
                        set_limits=True,
                        mask=None,
                        support=None,
                        settings=None,
                        dxdy=None,
                        **kwargs):
    import matplotlib.pyplot as plt
    import numpy as np
    import expresso.pycas as pc
    from .phase_gradient import phase_gradient

    e = get_unitless_bounds(carr)

    xprefix, xfactor = get_metric_prefix(e[1][:2])
    yprefix, yfactor = get_metric_prefix(e[0][:2])

    extent = [
        float(e[1][0]) / xfactor,
        float(e[1][1]) / xfactor,
        float(e[0][0]) / yfactor,
        float(e[0][1]) / yfactor
    ]

    x = np.linspace(extent[0], extent[1], carr.shape[1])
    y = np.linspace(extent[2], extent[3], carr.shape[0])

    if dxdy is None:
        gx, gy = [g.data for g in phase_gradient(carr)]
        gx /= xfactor * (x[0] - x[1])
        gy /= yfactor * (y[0] - y[1])
        gx += float(carr.evaluate(k * e[1][2]))
    else:
        gx, gy = dxdy

    gx *= yfactor / xfactor

    if support is not None:
        if mask is not None:
            raise ValueError('provide either support or mask arguments')

        if isinstance(support, pc.Expression):
            mask = pc.Not(support)
        else:
            mask = support.copy()
            mask.data = np.logical_not(support.data)

    if mask is not None:
        import expresso.pycas

        if isinstance(mask, pc.Expression):
            if settings == None:
                raise ValueError('no settings argument provided')
            mask = expression_for_array(mask, carr, settings)

        gx = np.ma.array(gx, mask=mask.data)
        gy = np.ma.array(gy, mask=mask.data)

    fig = None
    if ax == None:
        fig, ax = plt.subplots(figsize=figsize)
    if title:
        ax.set_title(title)

    stream = ax.streamplot(x, y, gx, gy, **kwargs)

    if set_limits:
        ax.set_xlim(extent[0], extent[1])
        ax.set_ylim(extent[2], extent[3])

    from expresso.pycas import latex as rlatex
    latex = lambda x: rlatex(x).replace(r'\text', r'\mathrm')
    ax.set_ylabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[0]), yprefix, latex(e[0][2])))
    ax.set_xlabel("$%s$ [$%s %s$]" %
                  (latex(carr.axis[1]), xprefix, latex(e[1][2])))

    if fig:
        plt.show()

    return stream
Пример #10
0
def get_plot_coordinates(array):
    import numpy as np
    from expresso.pycas import latex
    e = get_unitless_bounds(array)[0]
    prefix,factor = get_metric_prefix(e[:2])
    return np.linspace(float(e[0])/factor,float(e[1])/factor,array.shape[0]), prefix + ' ' + latex(e[2])