예제 #1
0
    def test_rainflow_csv(self):
        """tests rainflow functions on a contrived problem"""
        input_csv = 'test.csv'
        n = 700
        n1 = n // 3
        casenames = {
            ('normal', 0, n1),
            ('impulse', n1, n - 1),
        }

        x = np.linspace(0., 3.14 * 5, num=n)
        y = np.sin(x) * np.cos(201 * x)
        z = np.sin(x) * np.cos(201 * x) * np.tan(x)
        A = np.vstack([y, z])
        np.savetxt(input_csv, A.T, delimiter=',')
        features = {
            0: 'fillet',
            1: 'groove',
        }
        rainflow_from_csv(input_csv,
                          casenames,
                          features,
                          write_csvs=True,
                          delimiter=',',
                          xmax=None,
                          legend_alpha=1.0,
                          debug=False)

        stress_filename = 'stress.csv'
        eids = list(features.keys())
        min_max_stresses_dict = rainflow_from_arrays(A.T,
                                                     features=None,
                                                     casenames=None,
                                                     nfeatures_max=None,
                                                     filter_stresses=True)
        write_min_max_stress_dict(stress_filename, min_max_stresses_dict, eids)

        min_max_stresses_dict = rainflow_from_arrays(A.T,
                                                     features=features,
                                                     casenames=casenames,
                                                     nfeatures_max=None,
                                                     filter_stresses=True)
        write_min_max_stress_dict(stress_filename, min_max_stresses_dict, eids)

        failed = remove_files(
            'test.csv',
            'feature0_impulse_fillet.csv',
            'feature0_normal_fillet.csv',
            'feature1_impulse_groove.csv',
            'feature1_normal_groove.csv',
            'fillet.png',
            'groove.png',
            'stress.csv',
        )
        if failed:  # pragma: no cover
            print(failed)
예제 #2
0
def write_gif(gif_filename: str,
              png_filenames: List[str],
              time: float = 2.0,
              onesided: bool = True,
              nrepeat: int = 0,
              delete_images: bool = False,
              make_gif: bool = True) -> bool:
    """
    Makes an animated gif

    Parameters
    ----------
    gif_filename : str
        path to the output gif & png folder
    png_filenames : List[str]
        the pictures to make the gif from
    time : float; default=2.0
        the runtime of the gif (seconds)
    onesided : bool; default=True
        should the animation go up and back down
        True : the video will use images [0...N]
        False : the video will use images [0...N...0]
    nrepeat : int; default=0
        0 : loop infinitely
        1 : loop 1 time
        2 : loop 2 times
    delete_images : bool; default=False
        cleanup the png files at the end
    make_gif : bool; default=True
        actually make the gif at the end

    Returns
    -------
    success : bool
        was the gif made

    """
    if not IS_IMAGEIO:
        return False

    #assert fps >= 1, fps
    nframes = len(png_filenames)
    assert nframes > 0, png_filenames

    # duration : float
    # frame time (seconds)
    duration = time / nframes

    gif_dirname = os.path.dirname(os.path.abspath(gif_filename))
    if not os.path.exists(gif_dirname):
        os.makedirs(gif_dirname)

    #if not onesided:
    # drop the duplicate middle frame
    # >>> a = [1, 2, 3, 4, 5]
    # >>> a + a[-2::-1]
    # [1, 2, 3, 4, 5, 4, 3, 2, 1]
    #png_filenames = png_filenames + png_filenames[-2::-1]

    if make_gif and IS_IMAGEIO:
        images = []
        for png_filename in png_filenames:
            if not isinstance(png_filename,
                              str) and os.path.exists(png_filename):
                raise TypeError(f'png_filename={png_filename!r} is invalid')
            imagei = imageio.imread(png_filename)
            images.append(imagei)
        if nrepeat is True:
            nrepeat = 0
        try:
            imageio.mimsave(gif_filename,
                            images,
                            duration=duration,
                            loop=nrepeat)
        except IOError:  # file is open
            raise IOError('%s is likely open' % gif_filename)

    if delete_images:
        remove_files(png_filenames)
    return True