Esempio n. 1
0
    def plot(self,
             save=False,
             ani_name=None,
             ImageMagickLoc=None,
             close_after_animation=True):
        if self.visualize:
            self.get_visualize()
        else:
            update, time = self.get_draw(
                close_after_animation=close_after_animation, save=save)
        for ax in self.axes:
            ax.set_xlim(self.x_lim)
            ax.set_ylim(self.y_lim)
        ani = animation.FuncAnimation(self.fig,
                                      update,
                                      time,
                                      interval=1,
                                      blit=True,
                                      repeat=close_after_animation)
        if save is True and ImageMagickLoc is not None:
            plt.rcParams['animation.convert_path'] = ImageMagickLoc
            writer = animation.ImageMagickFileWriter(fps=100)
            ani.save(ani_name if ani_name else 'gif_1.gif', writer=writer)
        else:
            # TODO(Darius): Figure out a way to get Matplotlib to close the figure nicely after animation is done
            try:
                plt.show()
            except e as Exception:  # _tkinter.TclError: invalid command name "pyimage10"
                pass

        plt.clf()
        plt.cla()
        plt.close()
Esempio n. 2
0
    def plot(self,
             save=False,
             ani_name=None,
             ImageMagickLoc=None,
             close_after_animation=True):
        if self.visualize:
            self.get_visualize()
        else:
            update, time = self.get_draw(
                close_after_animation=close_after_animation, save=save)
        for ax in self.axes:
            ax.set_xlim(self.x_lim)
            ax.set_ylim(self.y_lim)
        ani = animation.FuncAnimation(self.fig,
                                      update,
                                      time,
                                      interval=1,
                                      blit=True,
                                      repeat=close_after_animation)
        if save is True and ImageMagickLoc is not None:
            plt.rcParams['animation.convert_path'] = ImageMagickLoc
            writer = animation.ImageMagickFileWriter(fps=100)
            ani.save(ani_name if ani_name else 'gif_1.gif', writer=writer)
        else:
            try:
                plt.show()
            except e as Exception:
                pass

        plt.clf()
        plt.cla()
        plt.close()
def write_output_inundanimation(outputfile, skip):
    print("writing animated inundation file...")
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.animation as animation
    from matplotlib import pyplot as plt

    global glinundarr
    fps=5
    fig, pl= plt.subplots(figsize=(5, 5))
    #preparing empty frame
    nts=glinundarr.shape[0]
    temp=np.zeros_like(glinundarr[0,:,:]).astype(float)
    temp[:]=0
    im = pl.imshow(temp, cmap=plt.cm.RdBu)
    
    dates=pd.date_range(glrecdate[0], freq="M", periods=len(glrecdate)).strftime("%b %Y") #entire period
    
    #preparing canvas
    pl.set_yticks([])
    pl.set_xticks([])
    tx=pl.text(0.7,0.9,dates[0], transform=pl.transAxes)
    
    def plotflood(ts):
        if ts%10==0:
            print ("ts="+str(ts))
        tx.set_text(dates[ts])
        frame=np.flipud(glinundarr[ts,:,:].astype(float))
        frame[frame==0]=np.nan
        im.set_array(frame)
        return im,

    ani = animation.FuncAnimation(fig, plotflood, range(skip,nts))
    writer = animation.ImageMagickFileWriter(fps=fps)
    ani.save(outputfile, writer=writer)
Esempio n. 4
0
def gazePlotter(data,
                name=None,
                flattened=True,
                save=False,
                output_path=None,
                plot=True):
    '''
    Plots and saves the gaze pattern as animation
    :param data: gaze data vector
    :param name: name of the file to be saved
    :param flattened: boolean to know if the data vector is flat
    :param save: boolean to determine save or not
    :param output_path: string path to the output vector
    :param plot: Boolean to display the plot
    :return: none
    '''
    x = []
    y = []
    if flattened:
        for i in range(0, len(data) - 1, 2):
            x.append(data[i])
            y.append(data[i + 1])
    else:
        for point in data:
            x.append(point[0])
            y.append(point[1])
    fig = plt.figure()
    # plt.rcParams['animation.ffmpeg_path'] = 'C:/ffmpeg/ffmpeg.exe'
    plt.xlim(0, 2560)
    plt.ylim(1440, 0)
    line, = plt.plot(x, y)

    def animate(ii):
        line.set_data(x[:ii + 1], y[:ii + 1])
        return line

    writer = animation.ImageMagickFileWriter()  # for ubuntu
    # writers = animation.writers['ffmpeg']
    # writer = writers(fps=30, metadata=dict(artist='Me'), bitrate=1800)
    # writer = animation.FFMpegFileWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
    img = plt.imread(args.proj_dir + '/images/CXR129_IM-0189-1001.jpg')
    plt.imshow(img)
    ani = animation.FuncAnimation(fig, animate, frames=50, interval=100)
    if save is True:
        if not os.path.exists(output_path + str(name.split('/')[0])):
            os.makedirs(output_path + str(name.split('/')[0]))
        ani.save(output_path + str(name) + str() + '.mp4',
                 writer='ffmpeg',
                 fps=15)
    if plot is True:
        plt.show()
Esempio n. 5
0
def animo(statelist: list, size: int, export=False):
    '''Visualize the matrix states contained in statelist (as lists of lists) by using matplotlib
	
	`export` argument defaults to False and decides if the animation is shown on-screen or saved as gif'''
    global toDisplay
    global im

    stateNum = len(
        statelist
    )  #set delay between updates of figure depending on amount of states which is relative to the size
    if stateNum > 400:
        interval = 15
    elif stateNum > 200:
        interval = 40
    elif stateNum > 100:
        interval = 60
    else:
        interval = 80

    fig = plt.gcf(
    )  #maybe this and .figure both work but now that its running i aint touching it
    toDisplay = statelist  # make global... dumb TODO use OOP
    im = plt.imshow(toDisplay[0],
                    cmap=CMAP)  # set initial frame, also initialize im
    anim = animation.FuncAnimation(fig,
                                   funcAnim,
                                   frames=len(toDisplay),
                                   interval=interval,
                                   repeat=False,
                                   blit=True)
    fig.suptitle(f"L-Shape Animation {size}x{size}")
    if (export):
        if (
                len(statelist) < 100
        ):  #change speed of gif depending on amount of states ~ size of matrix
            fps = 3
        elif (len(statelist) < 1000):
            fps = 7
        else:
            fps = 30
        writer = animation.ImageMagickFileWriter(
            fps=fps
        )  #very important when using `writer='imagemagick'` shit doesn't work!!!!!
        anim.save('docs/img/recent.gif',
                  writer=writer,
                  progress_callback=printprog
                  )  #needs to have imagemagick installed :)
    else:
        plt.show()  #display matplotlib animation
Esempio n. 6
0
def animate_plot_Func(pred_isings_all_timesteps, prey_isings_all_timesteps, settings, ax, fig, rep, t, save_folder):
    ''' Uses FuncAnimation - works and currently implemented'''
    my_path = os.path.abspath(__file__)
    #mpl.rcParams["savefig.directory"] = my_path + 'tmp/'
    if settings['server_mode']:
        plt.rcParams['animation.ffmpeg_path'] = '/data-uwks159/home/jprosi/ffmpeg-4.2.1-linux-64/ffmpeg'
        #'/usr/local/bin/ffmpeg'
    elif settings['laptop_mode']:
        plt.rcParams['animation.ffmpeg_path'] = "D:/Program Files/ffmpeg-20191217-bd83191-win64-static/bin/ffmpeg.exe"

    if settings['LoadIsings']:
        path = '/save/{}/animation_loaded_gen{}/'.format(settings['loadfile'], int(settings['iter']) + rep)
        #  when loading file generation counting starts from 0 again, thats why we have to add the iteration that was loaded
    else:
        path = '/{}animation_gen{}/'.format(save_folder, rep)
    savename = 'ani-{}-{}ts-gen{}.mp4'.format(time.strftime("%Y%m%d-%H%M%S"), t, rep)

    savepath = savename
    cur_wdir = os.getcwd()
    path = cur_wdir.replace('\\','/') + path
    if not os.path.exists(path):
        os.makedirs(path)

    os.chdir(path)
    design_figure(settings, fig, ax)
    initial_plot(pred_isings_all_timesteps[0], prey_isings_all_timesteps[0], settings, ax)
    #plt.savefig('firstframe.png', dpi =100, bbox_inches = 'tight')
    if not (len(pred_isings_all_timesteps) == len(prey_isings_all_timesteps)):
        raise Exception('Length of "pred_isings_all_timesteps" not equal to "prey_isings_all_timesteps"')

    ani = animation.FuncAnimation(fig, __update_plot, fargs=[pred_isings_all_timesteps, prey_isings_all_timesteps, settings, ax, fig], interval=1, frames=len(pred_isings_all_timesteps))

    if True:
        #ffmpeg does not work on server, therefore default writer used
        Writer = animation.FFMpegFileWriter
        writer = Writer(fps=settings['animation_fps'], metadata=dict(artist='Sina Abdollahi, Jan Prosi'), bitrate=1800)
        writer.frame_format = 'png'
        ani.save(savepath, writer=writer)
    elif False:
        #Using defaul writer instead of imagemagick
        ani.save(savepath, dpi=100, writer='imagemagick', fps=settings['animation_fps']) #TODO: dpi=100 writer='imagemagick',
    elif False:
        writer = animation.ImageMagickFileWriter(fps=settings['animation_fps'], metadata=dict(artist='Sina Abdollahi, Jan Prosi'), bitrate=1800)
        ani.save('location.gif', writer=writer, dpi = 100)#
    print('\nAnimation successfully saved at {}'.format(savepath))
    os.chdir(cur_wdir)
Esempio n. 7
0
    def plot(self,
             save=False,
             ani_name=None,
             ImageMagickLoc=None,
             close_after_animation=True):

        update, time = self.get_draw(
            close_after_animation=close_after_animation, save=save)

        #for ax in self.axes:

        self.axes.set_xlim(self.x_lim)
        self.axes.set_ylim(self.y_lim)

        ani = animation.FuncAnimation(self.fig,
                                      update,
                                      time,
                                      interval=1,
                                      blit=True,
                                      repeat=close_after_animation,
                                      repeat_delay=3000)
        if save is True and ImageMagickLoc is not None:
            #plt.rcParams['savefig.bbox'] = "tight"
            #plt.rcParams['savefig.orientation'] = "landscape"
            #plt.rcParams['figure.autolayout'] = True

            plt.rcParams['animation.convert_path'] = ImageMagickLoc
            writer = animation.ImageMagickFileWriter(fps=30, bitrate=1800)
            ani.save(ani_name if ani_name else 'gif_1.gif',
                     writer=writer,
                     dpi=300)
        else:
            # TODO(Darius): Figure out a way to get Matplotlib to close the figure nicely after animation is done
            try:
                plt.show()
            except e as Exception:  # _tkinter.TclError: invalid command name "pyimage10"
                pass

        plt.clf()
        plt.cla()
        plt.close()
Esempio n. 8
0
    def visualize(self, save = False, ani_name = None):
        
        fig = plt.figure(self.fig_n)
        self.fig_n += 1
        
        period = self.complex_coord_1.size
        time   = np.arange(period)
        circles_loc_1 = np.zeros((2*(period//2-1), period), dtype = np.complex_)
        circles_rad_1 = np.zeros((2*(period//2-1)), dtype = np.float_)

        for idx, multiple in enumerate(chain(range(-(period//2)+1, 0), range(1, period//2))):
            cn_1 = self.cn(time, period, multiple, self.complex_coord_1)
            circles_rad_1[idx] = np.absolute(cn_1)
            circles_loc_1[idx, :] = self.polar_locations(time, period, multiple, cn_1)
        circles_loc_1 = circles_loc_1[np.argsort(circles_rad_1)[::-1]]  
        circles_loc_1 = np.add.accumulate(circles_loc_1, 0)

        ax = fig.add_subplot(111)
        draw, = ax.plot(0,0)
        n_text = ax.text(0.02, 0.95, 'Number of Points = 0', transform=ax.transAxes)

        def ani(i):
            draw.set_data(circles_loc_1[i, :].real, circles_loc_1[i, :].imag)
            n_text.set_text('Number of Fourier Terms = %d' % i)
            return ([])

        time = np.arange(0, period//2, 8)
        ax.set_xlim(np.amin(circles_loc_1[-1].real), np.amax(circles_loc_1[-1].real))
        ax.set_ylim(np.amax(circles_loc_1[-1].imag), np.amin(circles_loc_1[-1].imag))
        ani = animation.FuncAnimation(fig, ani, time, interval=1, blit=True)
        if save is True:
            plt.rcParams['animation.convert_path'] = 'C:\Program Files\ImageMagick-7.0.8-Q16/magick.exe'
            writer = animation.ImageMagickFileWriter(fps = 100)
            ani.save(ani_name, writer=writer)
        else:
            plt.show()
            
        plt.clf()
        plt.cla()
        plt.close()
Esempio n. 9
0
    def wind_field(self,
                   field_name,
                   u_grid,
                   v_grid,
                   start,
                   end,
                   step=1,
                   interval=1,
                   dimensions=False,
                   datapath='none'):
        """ Makes animation of field streamlines over a given period.
        param: field_name: string, model layer being animated either ocean, 250, 750.
        param: u_grid: u velocities from a u_grid call. Should correspond to field name.
        param: v_grid: v velocities from a v_grid call. Should correspond to field name.
        param: start: integer, start time of animation (column index of data file).
        param: end: integer, end time of animation (column index of data file).
        option: step: integer, step between index of frames.
        option: interval: integer, delay between animation frames in seconds.
        option: dimensions: boolean, whether to dimensionalise the animation.
        option: datapath: string, save location for video.
        """

        # Animation settings
        writer = ani.ImageMagickFileWriter()

        # Plot details determined by field being animated
        if (field_name == "phi250"):
            title = '250 hPa Wind Field'
            save = '250hPa-Wind-Field'
        elif (field_name == "phi750"):
            title = '750 hPa Wind Field'
            save = '750hPa-Wind-Field'
        elif (field_name == "phiOcean"):
            title = 'Ocean Velocity Field'
            save = 'Ocean-Velocity-Field'
        else:
            print('Please provide valid field')
            return

        # Setting up first frame
        fig, ax = plt.subplots()
        [nx, ny] = u_grid.shape[1], u_grid.shape[0]

        if (dimensions):
            x_values = np.linspace(
                0, (2 * np.pi) / n,
                nx) * L * 1.e-3  # L is dimensionalisation constant
            y_values = np.linspace(
                0, np.pi, ny) * L * 1.e-3  #1.e-3 is m to Km conversion
            u_grid[:, :, :] = u_grid[:, :, :] * (
                f0 * L)  # f0 * L is dimensionalisation constant
            v_grid[:, :, :] = v_grid[:, :, :] * (f0 * L)

        else:
            x_values = np.linspace(0, (2 * np.pi) / n, nx)
            y_values = np.linspace(0, np.pi, ny)
            speed = np.sqrt(u_grid[:, :, :]**2 + v_grid[:, :, :]**2)

        speed = np.sqrt(u_grid[:, :, :]**2 + v_grid[:, :, :]**2)
        colorange = np.linspace(speed.min(), speed.max(), 30, endpoint=True)
        xx, yy = np.meshgrid(x_values, y_values)

        # Animation function
        def frame(t):
            fig.clf()

            # Calculating time
            time_model = tw * (t + 1
                               )  # tw is write step, t is index of data file
            time_seconds = time_model / f0  # time dimension given by f0
            time_hours = time_seconds / 3600
            time_days = floor(
                time_seconds / 86400
            )  # Floor function is to prevent rounding up in formatting
            time_months = time_seconds / 2.628e6
            time_years = time_days / 365

            # Animating title
            if (dimensions):
                time_stamp = ', {:02.0f} (days) {:02.0f} (hours)'.format(
                    time_days, time_hours % 24)
            else:
                time_stamp = ', {:05.0f} (step)'.format(time_model)
            plt.title(title + time_stamp)

            # Animating contour

            ax = plt.contourf(xx, yy, speed[:, :, t], colorange, cmap=cm.Blues)

            if (dimensions):
                cbar_unit = 'ms$^{-1}$'
            else:
                cbar_unit = ''
            cbar = plt.colorbar(
                label=cbar_unit)  # assumed units, check matches param file

            ax = plt.quiver(xx, yy, u_grid[:, :, t], v_grid[:, :, t])

            # Plot details
            if (dimensions):
                plt.xlabel('x (km)')
                plt.ylabel('y (km)')
            else:
                plt.xlabel("x'")
                plt.ylabel("y'")

        animation = ani.FuncAnimation(fig,
                                      frame,
                                      range(start, end, step),
                                      interval=interval * 1e+3,
                                      blit=False)
        animation.save(datapath + '/' + save + '.gif', writer=writer)
Esempio n. 10
0
        new_val = helper(x, movement, 1)
        return (new_val, y)
    elif direction == 2:
        new_val = helper(y, movement, -1)
        return (x, new_val)
    else:
        new_val = helper(x, movement, -1)
        return (new_val, y)


def animate(i):
    global cur1, cur2, cur3
    cur1 = update(cur1, 1)
    point1.center = cur1
    cur2 = update(cur2, 2)
    point2.center = cur2
    cur3 = update(cur3, 3)
    point3.center = cur3

    return point1, point2, point3,


anim = animation.FuncAnimation(fig,
                               animate,
                               init_func=init,
                               frames=1000,
                               interval=20,
                               blit=True)
writergif = animation.ImageMagickFileWriter()
anim.save('filename.gif', writer=writergif)
Esempio n. 11
0
def plot_disk_simulation(te,
                         dx,
                         dy,
                         r,
                         tau,
                         d_star,
                         plot_data,
                         animate_plot=False,
                         save=False):
    '''
    This function creates a plot that shows the eclipsing system
    The relevant light curve, and the corresponding gradient of
    the light curve.

    Parameters
    ----------
    te : float
        duration of the eclipse.
    dx : array_like (1-D)
        Array containing all the circle centres shifted by dx in 
        the x direction.
    dy : array_like (1-D)
        Array containing all the circle centres shifted by dy in
        the y direction.
    r : array_like (1-D)
        array containing the radii of the rings to be simulated.
        these should be in increasing order and should not contain
        0.
    tau : array_like (1-D)
        array containing the opacity of the rings specified above.
        values should be between 0 and 1 and the array should have
        the same length as r.
    d_star : float
        size of the star in days.
    plot_data : array_like (2-D)
        array consisting of 4 vectors. [0] = time, [1] = light curve
        if the star was a laser beam, [2] = the light curve with a
        finite sized star, [3] = the gradients measured.
    animate_plot : boolean
        determines whether or not to make an animation of the transit

    Returns
    -------
    Plot or Animation
    
    Notes
    -----
    This function only works for a single (dx, dy) point
    '''
    # determining the angular geometry of the system
    a, b, t, i = find_ellipse_parameters(te, dx, dy)
    # creating the patch objects
    rings = vector_rings(r, tau, i, t, dy)
    # getting the limits of the data
    time = plot_data[0]
    # drawing the star path
    xy = (time[0], -d_star / 2.)
    width = time[-1] - time[0]
    star_path = patches.Rectangle(xy, width, d_star, color='g', alpha=0.5)
    ##### creating the figure #####
    # title with all the information pertaining to the disk system
    title1 = 'Eclipse of a Circumplanetary Disk \n'
    title2 = 'Star - $R_{*}$ = %.2f days, b = %.2f days \n' % (d_star / 2.,
                                                               dy[0])
    title3 = 'Disk - $i$ = %.2f$^o$, $\\phi$ = %.2f$^o$, ' % (i, t)
    title4 = '$R_{disk}$ = ' + str(r) + ' days, '
    title5 = '$\\tau$ = ' + str(tau)
    title = title1 + title2 + title3 + title4 + title5
    #creating the figure
    fig = plt.figure(figsize=(8, 10))
    fig.suptitle(title)
    ax0 = plt.subplot2grid((4, 1), (0, 0), rowspan=2)
    # plot 1 - transit
    ax0.set_xlim(time[0], time[-1])
    ax0.set_ylim(-d_star, d_star)
    ax0.set_aspect('equal')
    ax0.set_adjustable('box')
    ax0.set_ylabel('y [days]')
    ax0.axhline(y=0, color='k')
    for ring in rings:
        ax0.add_patch(ring)
    ax0.add_patch(star_path)
    # plot 2 - light curve
    ax1 = plt.subplot2grid((4, 1), (2, 0), sharex=ax0)
    ax1.set_ylim(-0.05, 1.05)
    ax1.set_xlim(time[0], time[-1])
    ax1.set_ylabel('normalised flux')
    # plot 3 - gradients
    ax2 = plt.subplot2grid((4, 1), (3, 0), sharex=ax0)
    ax2.set_ylim(-0.05, 1.05)
    ax2.set_xlim(time[0], time[-1])
    ax2.set_ylabel('gradient')
    ax2.set_xlabel('time [days]')

    def animate(i):
        '''
        This function produces the animation for the plot
        '''
        # clearing the first plot of lines
        if i == 0:
            for artist in ax1.lines + ax1.collections:
                artist.remove()
            for artist in ax2.lines + ax2.collections:
                artist.remove()
        # clearing the previous star
        try:
            stars[i - 1].remove()
            stars[i - 1].remove()
        except:
            None
        # moving the star
        star = stars[i]
        ax0.add_artist(star)
        # transmission of rings
        ax1.plot(data[0, :i], data[1, :i], 'r-')
        # light curve
        ax1.plot(data[0, :i], data[2, :i], 'g-')
        # gradient curve
        ax2.plot(data[0, :i], data[3, :i], 'g-')

    if animate_plot == True:
        stars = [
            patches.Circle((x, 0), d_star / 2., color='g', alpha=0.7)
            for x in plot_data[0]
        ]
        ani = animation.FuncAnimation(fig,
                                      animate,
                                      frames=len(time),
                                      interval=50,
                                      repeat_delay=250)
        writer = animation.ImageMagickFileWriter(fps=20)
        if save == True:
            ani.save('eclipse.gif', writer=writer)
        else:
            fig.show()
    else:
        # add star
        star = patches.Circle((time[0] + d_star, 0),
                              d_star / 2.,
                              color='g',
                              alpha=0.7)
        ax0.add_patch(star)
        # add ring transmission
        ax1.plot(time, plot_data[1], 'r-o')
        ax1.plot(time, plot_data[2], 'g-o')
        # add gradient
        ax2.plot(time, plot_data[3], 'g-o')
        if save == True:
            fig.savefig('test.png')
        else:
            fig.show()
    return None
Esempio n. 12
0
Var = transpose(walks['Variance'].value)
Sk = transpose(walks['Skewness'].value)
Ku = transpose(walks['Kurtosis'].value)


t = transpose(walks['Time'].value)
Pe = walks['Peclet'].value

walks.close()


ani = animate_stats(Me,Var,Sk,Ku,nby,nbz,Pe,t)

if (True):
     print "Saving animation to disk (may take quite a while)..."
     #
     # dpi=200 gives decent quality for the filesize. Takes about 5-10 minutes to make.
     # assume a file input '***.h5', chop off the suffix and add the '.mp4'.
     fname = sys.argv[2]
     mywriter = anim.ImageMagickFileWriter()
#     mywriter = anim.FFMpegWriter()
     ani.save(fname,dpi=120,fps=12,writer=mywriter)
else:

     pyplot.show(block=False)
# end if

print "Done."


Esempio n. 13
0
    def draw(self, n_approximations = 500, speed = 1, mode = 1, save = False , ani_name = None):
        
        fig = plt.figure(1)
        self.fig_n += 1
        # Avoiding aliasing
        n_approximations = self.complex_coord_1.size//2 if n_approximations > self.complex_coord_1.size//2  else n_approximations
            
        if self.n_images == 1 and mode == 1:
            axes = {0: fig.add_subplot(111)}
            final_points, lst_circles_lst, lst_circles_loc = self.get_one_circle_one_image(axes, n_approximations)
            con1 = con2 = con3 = con4 = None
        elif self.n_images == 1 and mode == 2:
            axes = {i: fig.add_subplot(int(j)) for i, j in zip(range(4), ("224", "222", "221", "223"))}
            final_points, lst_circles_lst, lst_circles_loc = self.get_two_circles_one_image(axes, n_approximations)
            con1 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[0], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
            con2 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[0], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)
            con3 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[2], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
            con4 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[2], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)

            axes[0].add_artist(con1)
            axes[0].add_artist(con2)
            axes[2].add_artist(con3)
            axes[2].add_artist(con4)
            axes[1].set_zorder(-1)
            axes[3].set_zorder(-1)
            
        else:
            axes = {i: fig.add_subplot(int(j)) for i, j in zip(range(4), ("224", "222", "221", "223"))}
            final_points, lst_circles_lst, lst_circles_loc = self.get_two_circles_two_images(axes, n_approximations)
            con1 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[0], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
            con2 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[0], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)
            con3 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[2], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
            con4 = ConnectionPatch(xyA=(0,0), xyB=(0,0), coordsA="data", coordsB="data",
                  axesA=axes[2], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)

            axes[0].add_artist(con1)
            axes[0].add_artist(con2)
            axes[2].add_artist(con3)
            axes[2].add_artist(con4)
            axes[1].set_zorder(-1)
            axes[3].set_zorder(-1)

        def update(i):
            nonlocal con1, con2, con3, con4
            for n, circles_lst in enumerate(lst_circles_lst):
                for idx, circle in enumerate(circles_lst):
                    circle.center = (lst_circles_loc[n][idx, i].real, lst_circles_loc[n][idx, i].imag)
                if mode == 1 and self.n_images == 1:
                    final_points[0].set_data(lst_circles_loc[0][-1, :i].real, lst_circles_loc[0][-1, :i].imag)
                elif mode == 2 and self.n_images == 1:
                    final_points[0].set_data(lst_circles_loc[0][-1, :i].real, lst_circles_loc[1][-1, :i].imag)
                    final_points[1].set_data(lst_circles_loc[1][-1, :i].real, lst_circles_loc[0][-1, :i].imag)
                    con1.remove()
                    con2.remove()
                    con3.remove()
                    con4.remove()
                    con1 = ConnectionPatch(xyA=(lst_circles_loc[0][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           xyB=(lst_circles_loc[0][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[0], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)
                    con2 = ConnectionPatch(xyA=(lst_circles_loc[0][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           xyB=(lst_circles_loc[1][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[0], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
                    con3 = ConnectionPatch(xyA=(lst_circles_loc[1][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           xyB=(lst_circles_loc[0][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[2], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)
                    con4 = ConnectionPatch(xyA=(lst_circles_loc[1][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           xyB=(lst_circles_loc[1][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[2], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
                    axes[0].add_artist(con1)
                    axes[0].add_artist(con2)
                    axes[2].add_artist(con3)
                    axes[2].add_artist(con4)

                else:
                    final_points[0].set_data(lst_circles_loc[0][-1, :i].real, lst_circles_loc[1][-1, :i].imag)
                    final_points[1].set_data(lst_circles_loc[1][-1, :i].real, lst_circles_loc[0][-1, :i].imag)
                    con1.remove()
                    con2.remove()
                    con3.remove()
                    con4.remove()
                    con1 = ConnectionPatch(xyA=(lst_circles_loc[0][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           xyB=(lst_circles_loc[0][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[0], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)
                    con2 = ConnectionPatch(xyA=(lst_circles_loc[0][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           xyB=(lst_circles_loc[1][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[0], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
                    con3 = ConnectionPatch(xyA=(lst_circles_loc[1][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           xyB=(lst_circles_loc[0][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[2], axesB=axes[1], zorder=25, fc="w", ec="darkblue", lw=2)
                    con4 = ConnectionPatch(xyA=(lst_circles_loc[1][-1, i].real, lst_circles_loc[1][-1, i].imag),
                                           xyB=(lst_circles_loc[1][-1, i].real, lst_circles_loc[0][-1, i].imag),
                                           coordsA="data", coordsB="data",
                                           axesA=axes[2], axesB=axes[3], zorder=25, fc="w", ec="darkblue", lw=2)
                    axes[0].add_artist(con1)
                    axes[0].add_artist(con2)
                    axes[2].add_artist(con3)
                    axes[2].add_artist(con4)
            return ([])

        period = self.complex_coord_1.size
        ani = animation.FuncAnimation(fig, update, np.arange(0, period, speed), interval=1, blit=True)

        if self.n_images == 1 and mode == 1:
            lim_1 = np.amin(lst_circles_loc[0][-1].real), np.amax(lst_circles_loc[0][-1].real)
            lim_2 = np.amax(lst_circles_loc[0][-1].imag), np.amin(lst_circles_loc[0][-1].imag)
        else:
            lim_1 = min(np.amin(lst_circles_loc[0][-1].real), np.amin(lst_circles_loc[1][-1].real)), max(np.amax(lst_circles_loc[0][-1].real), np.amax(lst_circles_loc[1][-1].real))
            lim_2 = max(np.amax(lst_circles_loc[0][-1].imag), np.amax(lst_circles_loc[1][-1].imag)), min(np.amin(lst_circles_loc[0][-1].imag), np.amin(lst_circles_loc[1][-1].imag))
        for key, ax in axes.items():
            ax.set_xlim(lim_1)
            ax.set_ylim(lim_2)

        if save is True:
            plt.rcParams['animation.convert_path'] = 'C:\Program Files\ImageMagick-7.0.8-Q16/magick.exe'
            writer = animation.ImageMagickFileWriter(fps = 100)
            ani.save(ani_name, writer=writer)
        else:
            plt.show()

        plt.clf()
        plt.cla()
        plt.close()
Esempio n. 14
0

def plotflood(ts):
    if ts % 10 == 0:
        print("ts=" + str(ts))
    inu = sa.iloc[ts, :]
    amap = np.zeros_like(mf).astype(float)
    amap[:] = np.nan
    for key in codes.keys():
        area = inu[codes[key]]
        sel = np.where(unitsf == key)[0]
        for x in sel:
            prob = st.norm.cdf(area, mf[x], sigmaf[x])
            if thresh > 0:
                if prob > thresh:
                    amap[x] = 1
            else:
                amap[x] = prob
    amap = np.flipud(amap.reshape(nrow, ncol))
    tx.set_text(sa.index.strftime('%Y %B')[ts])
    im.set_array(amap)
    #plt.imshow(amap)
    #plt.show()

    return im,


ani = animation.FuncAnimation(fig, plotflood, range(skip, nts))
writer = animation.ImageMagickFileWriter(fps=fps)
ani.save(output_file + ".gif", writer=writer)
Esempio n. 15
0
    def mk_animation(self,
                     field_name,
                     velocity_grid,
                     start,
                     end,
                     step=1,
                     dimensions=False,
                     datapath='none'):
        """ Makes animation of field streamlines over a given period.
        param: field_name: string, name of field being animated.
        param: velocity_grid: output of u_grid or v_grid. Should correspond to field name.
        param: start: integer, start time of animation (column index of data file).
        param: end: integer, end time of animation (column index of data file).
        option: step: integer, step time of animation (no. of indices of data file).
        option: datapath: string, save location for video.
        """
        # Animation settings
        interval = 2.e-2  # delay between frames in seconds
        writer = ani.ImageMagickFileWriter()

        # Setting up first frame
        fig, ax = plt.subplots()
        [nx, ny] = velocity_grid.shape[1], velocity_grid.shape[0]
        colorange = np.linspace(velocity_grid.min(),
                                velocity_grid.max(),
                                30,
                                endpoint=True)  # Colorbar
        if (dimensions):
            colorange = colorange * (f0 * L)

        if (dimensions):
            x_values = np.linspace(0, (2 * L * np.pi) / n,
                                   nx)  # length dimension given by L
            y_values = np.linspace(0, np.pi * L, ny)
        else:
            x_values = np.linspace(0, (2 * np.pi) / n, nx)
            y_values = np.linspace(0, np.pi, ny)

        xx, yy = np.meshgrid(x_values, y_values)

        # Animation function
        def frame(t):
            fig.clf()

            # Calculating time
            time_model = tw * (t + 1
                               )  # tw is write step, t is index of data file
            time_seconds = time_model / f0  # time dimension given by f0
            time_hours = time_seconds / 3600
            time_days = floor(
                time_seconds / 86400
            )  # Floor function is to prevent rounding up in formatting
            time_months = time_seconds / 2.628e6
            time_years = time_days / 365

            # Animating title
            if (dimensions):
                time_stamp = ', {:02.0f} (days) {:02.0f} (hours)'.format(
                    time_days, time_hours % 24)
            else:
                time_stamp = ', {:05.0f} (step)'.format(time_model)
            plt.title(title + time_stamp)

            # Animating contours
            field = velocity_grid[:, :, t]
            if (dimensions):
                field = field * (
                    f0 * L
                )  # Constant gives dimension, units dertermined by param file
            ax = plt.contourf(xx, yy, field, colorange, cmap=cm.seismic)

            # Plot details
            if (dimensions):
                plt.xlabel('x (m)')
                plt.ticklabel_format(axis='x', style='sci',
                                     scilimits=(0,
                                                0))  #Need to sort out labels
                plt.ylabel('y (m)')
                plt.yticks([])
            else:
                plt.xlabel("x'")
                plt.ylabel("y'")
            if (dimensions):
                cbar_unit = 'ms$^{-1}$'
            else:
                cbar_unit = ''
            cbar = plt.colorbar(
                format='%.1e',
                label=cbar_unit)  # assumed units, check matches param file

        animation = ani.FuncAnimation(fig,
                                      frame,
                                      range(start, end, step),
                                      interval=interval * 1e+3,
                                      blit=False)
        animation.save(datapath + '/' + field_name + '-animation.gif',
                       writer=writer)
Esempio n. 16
0
import numpy as np
from matplotlib import animation
import h5py
import argparse
import matplotlib.pyplot as plt
mywriter = animation.ImageMagickFileWriter()
plt.rcParams['image.cmap'] = 'RdBu_r'

parser = argparse.ArgumentParser(
    description='animate a field from dedalus output')
parser.add_argument('folder', type=str, help='top level folder to look in')
parser.add_argument('sim_name',
                    type=str,
                    help='simulation name in dedalus file structure')
parser.add_argument('-f', '--field', type=str, dest='field_to_animate')
parser.add_argument('-o', '--outfile', type=str, dest='outfile')
args = parser.parse_args()

# get these from args
folder = args.folder
sim_name = args.sim_name
field_to_animate = args.field_to_animate

filepath = folder + "/" + sim_name + "_s1/" + sim_name + "_s1_p0.h5"
data = h5py.File(filepath, "r")
#te = data['tasks']['total e profile'][:]
#te_1 = data['tasks']['total e'][:]
z = data['scales/z/1.0'][:]
t = data['scales']['sim_time'][:]
x = data['scales/x/1.0'][:]
Esempio n. 17
0
    def __init__(self,
                 quads,
                 quad,
                 display_obstacles=False,
                 plot_sim_trail=False,
                 plot_quad_trail=False,
                 save=False):
        self.quads = quads
        self.quad = quad
        self.display_obstacles = display_obstacles
        self.plot_sim_trail = plot_sim_trail
        self.plot_quad_trail = plot_quad_trail
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(1, 1, 1, projection='3d')
        self.fig.tight_layout(rect=[0, 0, 1, 0.97])
        #self.ax_graph = self.fig.add_subplot(2,1,2)
        self.ax.set_xlim3d([0.0, 4.0])
        self.ax.set_xlabel('X')
        self.ax.set_ylim3d([0.0, 4.0])
        self.ax.set_ylabel('Y')
        self.ax.set_zlim3d([0, 4.0])
        self.ax.set_zlabel('Z')
        self.t = 1
        self.ax.set_title('Quadcopter Simulation')
        # Store points for trails, if enabled
        self.x, self.y, self.z = [], [], []
        # Create definitions for actual trail, and simulated trails
        self.trail, = self.ax.plot([], [], [], '--', c='blue', markersize=2)
        self.sim_trail, = self.ax.plot([], [], [],
                                       '.',
                                       c='red',
                                       markersize=2,
                                       markevery=4)
        '''bounds = self.quad.get_bounds(self.t, 0.01)
        xs = bounds[:, 0]
        ys = bounds[:, 4]
        zs = bounds[:, 8]
        p1 = np.array([xs, ys, zs])[:, 0]
        p2 = np.array([xs, ys, zs])[:, 1]
        self.prism = self.ax.add_collection3d(Poly3DCollection(self.plot_prism(p1, p2), color='green', alpha=.25))
        self.prism_lines = self.ax.add_collection3d(Line3DCollection(self.plot_prism(p1, p2), color='green',
                                                                     linestyles='--', linewidths=1))
        self.points = self.ax.scatter(xs, ys, zs, s=6, c='black')'''
        self.artists = [self.sim_trail, self.trail]
        if self.display_obstacles:
            xs, ys, zs = np.indices((4, 1, 5))
            voxel = (xs < 4) & (ys < 0.5) & (zs < 5)
            self.ax.voxels(voxel, facecolors='red', edgecolor='k')

        for key in self.quads:
            self.quads[key]['l1'], = self.ax.plot([], [], [],
                                                  color='blue',
                                                  linewidth=2)
            self.quads[key]['l2'], = self.ax.plot([], [], [],
                                                  color='red',
                                                  linewidth=2)
            self.quads[key]['hub'], = self.ax.plot([], [], [],
                                                   marker='o',
                                                   color='green',
                                                   markersize=6)
            self.artists.append(self.quads[key]['l1'])
            self.artists.append(self.quads[key]['l2'])
            self.artists.append(self.quads[key]['hub'])
        self.artists = tuple(self.artists)
        self.ax.view_init(elev=30, azim=10)
        self.ani = animation.FuncAnimation(self.fig,
                                           self.update,
                                           frames=400,
                                           init_func=None,
                                           interval=5,
                                           blit=False)
        if save:
            # Interval : Amount of time, in ms between generated frames
            # Frames: Number of frames to produce
            # FPS: 1/(interval*0.001) -> multiple by number of seconds needed for number of frames
            writer = animation.ImageMagickFileWriter(fps=1 / (5 * 0.001))
            self.ani.save('test.gif', writer=writer)
        else:
            plt.show()
            os._exit(0)
Esempio n. 18
0
def make_multi_star_animation(te,
                              dx,
                              dy,
                              r,
                              tau,
                              star,
                              d_star,
                              savename='eclipse_multi_star.gif'):
    '''
    This function creates a plot that shows the eclipsing system
    The relevant light curve, and the corresponding gradient of
    the light curve.

    Parameters
    ----------
    te : float
        duration of the eclipse.
    dx : array_like (1-D)
        Array containing all the circle centres shifted by dx in 
        the x direction.
    dy : array_like (1-D)
        Array containing all the circle centres shifted by dy in
        the y direction.
    r : array_like (1-D)
        array containing the radii of the rings to be simulated.
        these should be in increasing order and should not contain
        0.
    tau : array_like (1-D)
        array containing the opacity of the rings specified above.
        values should be between 0 and 1 and the array should have
        the same length as r.
    d_star : array_like (1-D - len(d_star) <= 3)
        size of the star in days in ascending order.
    plot_data : array_like (2-D)
        array consisting of 4 vectors. [0] = time, [1] = light curve
        if the star was a laser beam, [2] = the light curve with a
        finite sized star, [3] = the gradients measured.
    animate_plot : boolean
        determines whether or not to make an animation of the transit

    Returns
    -------
    Plot or Animation
    
    Notes
    -----
    This function only works for a single (dx, dy) point
    '''
    print('setting up figure')
    # disk parameters
    a, b, tilt, inc = find_ellipse_parameters(te, dx, dy)
    ##### creating the figure #####
    # title with all the information pertaining to the disk system
    title1 = 'Eclipse of a Circumplanetary Disk \n'
    #title2 = 'Star - $R_{*}$ = %.2f days, b = %.2f days \n'%(d_star/2.,dy[0]) # needed later
    title2 = 'Disk - $i$ = %.2f$^o$, $\\phi$ = %.2f$^o$ \n' % (inc, tilt)
    title3 = '$R_{disk}$ = ' + str(np.round(r, 2)) + ' days \n '
    title4 = '$\\tau$ = ' + str(np.round(tau, 2))
    title = title1 + title2 + title3 + title4
    #creating the figure
    fig = plt.figure(figsize=(15, 8))
    fig.suptitle(title)
    n = min(len(d_star), 3)
    # create axes
    axs = np.zeros((3, 0))
    if n >= 1:
        ax0a = plt.subplot2grid((3, n), (0, 0))  #,rowspan=2)
        ax1a = plt.subplot2grid((3, n), (1, 0), sharex=ax0a)
        ax2a = plt.subplot2grid((3, n), (2, 0), sharex=ax0a)
        axsa = np.array([ax0a, ax1a, ax2a])
        axs = np.concatenate((axs, axsa[:, None]), 1)
    if n >= 2:
        ax0b = plt.subplot2grid((3, n), (0, 1))  #,rowspan=2)
        ax1b = plt.subplot2grid((3, n), (1, 1), sharex=ax0b)
        ax2b = plt.subplot2grid((3, n), (2, 1), sharex=ax0b)
        axsb = np.array([ax0b, ax1b, ax2b])
        axs = np.concatenate((axs, axsb[:, None]), 1)
    if n >= 3:
        ax0c = plt.subplot2grid((3, n), (0, 2))  #,rowspan=2)
        ax1c = plt.subplot2grid((3, n), (1, 2), sharex=ax0c)
        ax2c = plt.subplot2grid((3, n), (2, 2), sharex=ax0c)
        axsc = np.array([ax0c, ax1c, ax2c])
        axs = np.concatenate((axs, axsc[:, None]), 1)
    # open lists for the animation
    plot_data = []
    all_stars = []
    lims = [[-np.amax(d_star), np.amax(d_star)], [-0.05, 1.05], [-0.05, 1.05]]
    ylabels = ['y [days]', 'normalised flux [-]', 'gradient [-]']
    # set up for animation
    for j in range(n):
        # create ring patches
        rings = vector_rings(r, tau, inc, tilt, dy)
        # getting light curve data
        _, _, data = get_light_curve(te, dx, dy, star, d_star[j], r, tau)
        # introducing a skip factor to reduce equalize the number of frames
        if j == 0:
            frame_times = np.linspace(data[0, 0], data[0, -1], 101)

        frame_indices = frame_selection(data[0], frame_times)
        ani_data = data[:, frame_indices]
        # appending to plot_data
        plot_data.append(ani_data)
        time = ani_data[0]
        stars = [
            patches.Circle((x, 0), d_star[j] / 2., color='g', alpha=0.7)
            for x in time
        ]
        all_stars.append(stars)
        for i in range(3):
            # setting limits
            axs[i, j].set_xlim(time[0], time[-1])
            axs[i, j].set_ylim(lims[i])
            # setting labels
            if j == 0:
                axs[i, j].set_ylabel(ylabels[i])
            if i == 2:
                axs[i, j].set_xlabel('time [days]')
            # adding vector graphics
            if i == 0:
                axs[i, j].set_aspect('equal')
                axs[i, j].set_adjustable('box')
                for ring in rings:
                    axs[i, j].add_patch(ring)
                xy_rect = (time[0], -d_star[j] / 2.)
                w_rect = time[-1] - time[0]
                star_path = patches.Rectangle(xy_rect,
                                              w_rect,
                                              d_star,
                                              color='g',
                                              alpha=0.5)
                axs[i, j].axhline(y=0, color='k', ls='--')
                axs[i, j].set_title('$D_*$ = %.2f days' % d_star[j])

    def animate(f):
        '''
        This function produces the animation for the plot.
        '''
        print('frame %i/101 \r' % f),
        # clearing the plots of lines
        for j in range(n):
            if f == 0:
                for artist in axs[1, j].lines + axs[1, j].collections:
                    artist.remove()
                for artist in axs[2, j].lines + axs[2, j].collections:
                    artist.remove()
            try:
                all_stars[j][f - 1].remove()
                all_stars[j][f - 1].remove()
            except:
                None
            # moving the star
            Star = all_stars[j][f]
            axs[0, j].add_artist(Star)
            # transmission of rings
            axs[1, j].plot(plot_data[j][0, :f], plot_data[j][1, :f], 'r-')
            # light curve
            axs[1, j].plot(plot_data[j][0, :f], plot_data[j][2, :f], 'g-')
            # gradient curve
            axs[2, j].plot(plot_data[j][0, :f], plot_data[j][3, :f], 'g-')

    print('creating animation')
    ani = animation.FuncAnimation(fig, animate, interval=5, repeat_delay=250)
    writer = animation.ImageMagickFileWriter(fps=20)
    print('saving animation')
    ani.save(savename, writer=writer)
    return None
Esempio n. 19
0
def inund_calc(outputfile):
    print("Animating inundation...\n")
    import struct
    import numpy as np
    import PIL
    import sys
    import matplotlib
    matplotlib.use('Agg')
    from matplotlib import pyplot as plt
    import scipy.stats as st
    import matplotlib.animation as animation

    fps = 5

    mapdir = "./config/"
    nrow, ncol = 300, 303
    nofpix = ncol * nrow

    skip = 3
    thresh = 0.7

    print("reading inundation map parameters...")

    ncdata = Dataset(mapdir + "./m_arc.nc")
    m = ncdata.variables['Band1'][:]
    m[m < 0] = 1000

    ncdata = Dataset(mapdir + "./sigma_arc.nc")
    sigma = ncdata.variables['Band1'][:]

    ncdata = Dataset(mapdir + "./units_arc.nc")
    units = ncdata.variables['Band1'][:]
    units = units.astype("float")

    units[m <= 0] = np.nan
    units[sigma <= 0] = np.nan

    codes = {
        1: "Panhandle",
        2: "Thaoge",
        3: "Xudum",
        4: "Boro",
        5: "Khwai",
        6: "Nqoga-1a",
        7: "Selinda",
        8: "Nqoga-2a",
        9: "Mboroga"
    }

    unitsf = units.flatten()
    mf = m.flatten()
    sigmaf = sigma.flatten()

    print("done\n")

    cellnames, cellvalues = mergecells(gl.fin_sa_end)
    dates = pd.to_datetime(gl.recdate)

    sa = pd.DataFrame(cellvalues.T, index=dates, columns=cellnames)

    nts = sa.shape[0]

    print("read", nts, "time steps")
    print("will skip", skip, "time steps")

    print("done\n")

    print("processing...")

    fig, pl = plt.subplots(figsize=(5, 5))

    #preparing empty frame
    temp = np.zeros_like(units).astype(float)
    temp[:] = 0
    im = pl.imshow(temp, cmap=plt.cm.RdBu)

    #plt.show()

    #preparing canvas
    pl.set_yticks([])
    pl.set_xticks([])
    tx = pl.text(0.7,
                 0.9,
                 sa.index.strftime('%Y %B')[0],
                 transform=pl.transAxes)

    def plotflood(ts):
        if ts % 10 == 0:
            print("ts=" + str(ts))
        inu = sa.iloc[ts, :]
        amap = np.zeros_like(mf).astype(float)
        amap[:] = np.nan
        for key in codes.keys():
            area = inu[codes[key]]
            sel = np.where(unitsf == key)[0]
            for x in sel:
                prob = st.norm.cdf(area, mf[x], sigmaf[x])
                if thresh > 0:
                    if prob > thresh:
                        amap[x] = 1
                else:
                    amap[x] = prob
        amap = np.flipud(amap.reshape(nrow, ncol))
        tx.set_text(sa.index.strftime('%Y %B')[ts])
        im.set_array(amap)
        return im,

    ani = animation.FuncAnimation(fig, plotflood, range(skip, nts))
    writer = animation.ImageMagickFileWriter(fps=fps)
    ani.save(outputfile, writer=writer)
    print("done")
Esempio n. 20
0
def generate_logo(animateLogo=False,
                  animateInterval=200,
                  saveLogo=None,
                  showLogo=True,
                  dpi=300,
                  namd_start_angle=60.0):
    '''
    Generate the Hefei-NAMD logo.
    '''
    figure = plt.figure(figsize=(3.0, 4.0))
    ax = plt.subplot()
    ax.set_aspect('equal')
    ax.axis('off')

    ############################################################
    R0 = 1.0
    C0 = Circle(
        (0.0, 0.0),
        radius=R0,
        facecolor='k',
        alpha=0.7,
    )
    C1 = Circle((0.0, 0.0), radius=0.55 * R0, facecolor='w')

    ax.add_patch(C0)
    ax.add_patch(C1)

    ############################################################
    MOLCOLORS = ['r', 'b', 'b']
    R, P = get_molecule_positions(theta=-10)
    for ii in range(3):
        cc = Circle(P[ii], radius=R[ii], facecolor=MOLCOLORS[ii], alpha=0.6)
        ax.add_patch(cc)

    R, P = get_molecule_positions(theta=-120, shrink=0.15)
    P += np.array([-0.2, -0.2])
    for ii in range(3):
        cc = Circle(P[ii], radius=R[ii], facecolor=MOLCOLORS[ii], alpha=0.5)
        ax.add_patch(cc)

    R, P = get_molecule_positions(theta=120, shrink=0.1)
    P += np.array([0.2, -0.2])
    for ii in range(3):
        cc = Circle(P[ii], radius=R[ii], facecolor=MOLCOLORS[ii], alpha=0.4)
        ax.add_patch(cc)
    ############################################################

    NAMD = [x.upper() for x in 'namd']
    if animateLogo:
        namd_rot = []
        for angle in range(0, 360, 10):
            start_angle = angle / 180. * np.pi
            tmp = []
            for ii in range(4):
                tc = np.exp(1j * (start_angle + ii * np.pi / 2))
                c1 = Circle(
                    (tc.real, tc.imag),
                    radius=0.4,
                    facecolor='white',
                    # alpha=0.9
                )
                ax.add_patch(c1)
                c2 = Circle((tc.real, tc.imag),
                            radius=0.32,
                            facecolor='red',
                            alpha=0.5)
                ax.add_patch(c2)
                c3 = ax.text(
                    tc.real,
                    tc.imag - 0.05,
                    NAMD[ii],
                    ha="center",
                    va="center",
                    fontsize=52,
                    # family='monospace',
                    color='white',
                    fontweight='bold',
                    transform=ax.transData,
                    fontname='Cooper Black',
                    # bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5)
                )
                tmp += [c1, c2, c3]
            namd_rot.append(tmp)
    else:
        start_angle = namd_start_angle / 180. * np.pi
        for ii in range(4):
            tc = np.exp(1j * (start_angle + ii * np.pi / 2))
            c1 = Circle(
                (tc.real, tc.imag),
                radius=0.4,
                facecolor='white',
                # alpha=0.9
            )
            ax.add_patch(c1)
            c2 = Circle((tc.real, tc.imag),
                        radius=0.32,
                        facecolor='red',
                        alpha=0.5)
            ax.add_patch(c2)
            c3 = ax.text(
                tc.real,
                tc.imag - 0.05,
                NAMD[ii],
                ha="center",
                va="center",
                fontsize=52,
                # family='monospace',
                color='white',
                fontweight='bold',
                transform=ax.transData,
                fontname='Cooper Black',
                # bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5)
            )

    ############################################################

    ax.plot([-1.4, 1.4], [-1.45, -1.45], 'k-', lw=5.0)

    ############################################################
    TEXT = 'HEFEI'
    TPOS = np.linspace(-1.1, 1.1, 5, endpoint=True)
    TROT = [-19.05462068, 1.69593734, -11.93570325, -0.39501574, 22.31639076]
    # TROT = [-16.59700324, -7.07637502, 29.74676562,  7.06633301, 22.90883817]
    # TROT = np.random.uniform(-1, 1, 5) * 30
    # print(TROT)

    TCLR = plt.rcParams['axes.prop_cycle'].by_key()['color']

    for ii in range(5):
        h = TPOS[ii]
        v = -2.10 + np.sin(ii * np.pi / 2) * 0.2
        # v = -1.95
        T = plt.text(
            h,
            v,
            TEXT[ii],
            ha="center",
            va="center",
            fontsize=52,
            # family='monospace',
            color='white',
            alpha=0.95,
            rotation=TROT[ii],
            fontweight='bold',
            transform=ax.transData,
            fontname='Cooper Black',
            # bbox=dict(pad=0.1, lw=0.0, boxstyle='circle', facecolor='green', alpha=0.6)
        )
        # ax.plot([h,], [v], marker='o', ms=5)

        # box = T.get_window_extent().inverse_transformed(ax.transData)
        # box_w = box.x1 - box.x0
        # box_h = box.y1 - box.y0
        # RR = Rectangle(
        #     (box.x0, box.y0 + 0.1), box_w, box_h,
        #     facecolor='green'
        # )
        # ax.add_patch(RR)

        cc = Circle(
            (h, v + 0.05),
            radius=0.3,
            # facecolor='green',
            facecolor=TCLR[ii],
            alpha=0.6)
        ax.add_patch(cc)

    ax.set_xlim(-1.5, 1.5)
    ax.set_ylim(-2.55, 1.4)

    plt.tight_layout(pad=0.1)

    if animateLogo:
        ani = animation.ArtistAnimation(figure,
                                        namd_rot,
                                        interval=200,
                                        blit=True,
                                        repeat_delay=0)

        movieWrite = animation.ImageMagickFileWriter(fps=12)
        movieWrite.setup(fig=figure, outfile=saveLogo)
        ani.save(saveLogo, writer=movieWrite, dpi=120)
    else:
        plt.savefig(saveLogo, dpi=dpi)

    if showLogo:
        plt.show()
Esempio n. 21
0
def build_gif(imgs,
              interval=0.1,
              dpi=72,
              save_gif=True,
              saveto='animation.gif',
              show_gif=False,
              cmap=None):
    """Take an array or list of images and create a GIF.

    Parameters
    ----------
    imgs : np.ndarray or list
        List of images to create a GIF of
    interval : float, optional
        Spacing in seconds between successive images.
    dpi : int, optional
        Dots per inch.
    save_gif : bool, optional
        Whether or not to save the GIF.
    saveto : str, optional
        Filename of GIF to save.
    show_gif : bool, optional
        Whether or not to render the GIF using plt.
    cmap : None, optional
        Optional colormap to apply to the images.

    Returns
    -------
    ani : matplotlib.animation.ArtistAnimation
        The artist animation from matplotlib.  Likely not useful.
    """
    imgs = np.asarray(imgs)
    h, w, *c = imgs[0].shape
    fig, ax = plt.subplots(figsize=(np.round(w / dpi), np.round(h / dpi)))
    fig.subplots_adjust(bottom=0)
    fig.subplots_adjust(top=1)
    fig.subplots_adjust(right=1)
    fig.subplots_adjust(left=0)
    ax.set_axis_off()

    mpl.verbose.set_level("helpful")

    if cmap is not None:
        axs = list(map(lambda x: [ax.imshow(x, cmap=cmap)], imgs))
    else:
        axs = list(map(lambda x: [ax.imshow(x)], imgs))

    ani = animation.ArtistAnimation(fig,
                                    axs,
                                    interval=interval * 1000,
                                    repeat_delay=0,
                                    blit=False)

    if save_gif:
        try:
            writer = animation.ImageMagickFileWriter()
            ani.save(saveto, writer=writer, dpi=dpi)
        except:
            print(
                'You do not have imagemagick installed.\n\nOn OSX ' +
                'you can install this by first installing homebrew: ' +
                'http://brew.sh\nThen run: "brew install imagemagick".\n' +
                'Windows users can obtain a binary installation here: ' +
                'https://www.imagemagick.org/script/binary-releases.php\n' +
                'And Linux users should be able to install imagemagick using '
                +
                'their package manager, e.g.: sudo apt-get install imagemagick.'
            )

    if show_gif:
        plt.show()

    return ani