Exemple #1
0
def _array_to_gif(arr, frame_rate):
    if len(arr.shape) == 3:
        arr = np.array([arr]*3).transpose([1,2,3,0])
    if arr.dtype == np.float:
        arr = np.round(arr * 255).astype('uint8')
    res = io.BytesIO(b"")
    write_apng(res, arr, delay=1000./frame_rate)
    return res.getvalue()
Exemple #2
0
def render_policy(env,
                  env_name,
                  algo,
                  policy_path,
                  coop=False,
                  colab=False,
                  seed=0,
                  n_episodes=1,
                  extra_configs={}):
    ray.init(num_cpus=multiprocessing.cpu_count(),
             ignore_reinit_error=True,
             log_to_driver=False)
    if env is None:
        env = make_env(env_name, coop, seed=seed)
        if colab:
            env.setup_camera(camera_eye=[0.5, -0.75, 1.5],
                             camera_target=[-0.2, 0, 0.75],
                             fov=60,
                             camera_width=1920 // 4,
                             camera_height=1080 // 4)
    test_agent, _ = load_policy(env, algo, env_name, policy_path, coop, seed,
                                extra_configs)

    if not colab:
        env.render()
    frames = []
    for episode in range(n_episodes):
        obs = env.reset()
        done = False
        while not done:
            if coop:
                # Compute the next action for the robot/human using the trained policies
                action_robot = test_agent.compute_action(obs['robot'],
                                                         policy_id='robot')
                action_human = test_agent.compute_action(obs['human'],
                                                         policy_id='human')
                # Step the simulation forward using the actions from our trained policies
                obs, reward, done, info = env.step({
                    'robot': action_robot,
                    'human': action_human
                })
                done = done['__all__']
            else:
                # Compute the next action using the trained policy
                action = test_agent.compute_action(obs)
                # Step the simulation forward using the action from our trained policy
                obs, reward, done, info = env.step(action)
            if colab:
                # Capture (render) an image from the camera
                img, depth = env.get_camera_image_depth()
                frames.append(img)
    env.disconnect()
    if colab:
        filename = 'output_%s.png' % env_name
        write_apng(filename, frames, delay=100)
        return filename
    def test_write_apng_8bit_RGBA(self):
        num_frames = 4
        w = 25
        h = 15
        np.random.seed(12345)
        seq_size = (num_frames, h, w, 4)
        seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
        f = io.BytesIO()
        numpngw.write_apng(f, seq)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents, width=w, height=h,
                                   bit_depth=8, color_type=6, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_actl(file_contents, num_frames=num_frames,
                                   num_plays=0)

        sequence_number = 0
        file_contents = check_fctl(file_contents,
                                   sequence_number=sequence_number,
                                   width=w, height=h)
        sequence_number += 1

        file_contents = check_idat(file_contents, color_type=6, bit_depth=8,
                                   interlace=0, img=seq[0])

        for k in range(1, num_frames):
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            # Check the fdAT chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"fdAT")
            actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
            self.assertEqual(actual_seq_num, sequence_number)
            sequence_number += 1
            decompressed = zlib.decompress(chunk_data[4:])
            b = np.fromstring(decompressed, dtype=np.uint8)
            lines = b.reshape(h, 4*w+1)
            expected_col0 = np.zeros(h, dtype=np.uint8)
            assert_array_equal(lines[:, 0], expected_col0)
            img2 = lines[:, 1:].reshape(h, w, 4)
            assert_array_equal(img2, seq[k])

        check_iend(file_contents)
Exemple #4
0
    def test_write_apng_8bit_RGBA(self):
        num_frames = 4
        w = 25
        h = 15
        np.random.seed(12345)
        seq_size = (num_frames, h, w, 4)
        seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
        f = io.BytesIO()
        numpngw.write_apng(f, seq)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents, width=w, height=h,
                                   bit_depth=8, color_type=6, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_actl(file_contents, num_frames=num_frames,
                                   num_plays=0)

        sequence_number = 0
        file_contents = check_fctl(file_contents,
                                   sequence_number=sequence_number,
                                   width=w, height=h)
        sequence_number += 1

        file_contents = check_idat(file_contents, color_type=6, bit_depth=8,
                                   interlace=0, img=seq[0])

        for k in range(1, num_frames):
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            # Check the fdAT chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"fdAT")
            actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
            self.assertEqual(actual_seq_num, sequence_number)
            sequence_number += 1
            decompressed = zlib.decompress(chunk_data[4:])
            b = np.frombuffer(decompressed, dtype=np.uint8)
            lines = b.reshape(h, 4*w+1)
            expected_col0 = np.zeros(h, dtype=np.uint8)
            assert_array_equal(lines[:, 0], expected_col0)
            img2 = lines[:, 1:].reshape(h, w, 4)
            assert_array_equal(img2, seq[k])

        check_iend(file_contents)
Exemple #5
0
    def test_default_image(self):
        num_frames = 2
        w = 16
        h = 8
        np.random.seed(12345)
        seq_size = (num_frames, h, w, 4)
        seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
        default_image = np.zeros((h, w, 4), dtype=np.uint8)

        f = io.BytesIO()

        numpngw.write_apng(f, seq, default_image=default_image)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents, width=w, height=h,
                                   bit_depth=8, color_type=6)

        file_contents = check_actl(file_contents, num_frames=num_frames,
                                   num_plays=0)

        sequence_number = 0

        file_contents = check_idat(file_contents, color_type=6, bit_depth=8,
                                   img=default_image)

        for k in range(0, num_frames):
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            # Check the fdAT chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"fdAT")
            actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
            self.assertEqual(actual_seq_num, sequence_number)
            sequence_number += 1
            decompressed = zlib.decompress(chunk_data[4:])
            b = np.fromstring(decompressed, dtype=np.uint8)
            lines = b.reshape(h, 4*w+1)
            expected_col0 = np.zeros(h, dtype=np.uint8)
            assert_array_equal(lines[:, 0], expected_col0)
            img2 = lines[:, 1:].reshape(h, w, 4)
            assert_array_equal(img2, seq[k])

        check_iend(file_contents)
Exemple #6
0
def animated_humanoid(fn):
    camTargetPos = [0, 0, 0]
    cameraUp = [0, 0, 1]
    cameraPos = [1, 1, 1]
    p.setGravity(0, 0, -10)
    pitch = -10.0
    roll = 0
    upAxisIndex = 2
    camDistance = 3
    pixelWidth = 1080
    pixelHeight = 790
    nearPlane = 0.01
    farPlane = 100
    fov = 90
    cubeStartPos = [0, 0, 1]
    cubeStartOrientation = p.getQuaternionFromEuler([0, 0, 0])
    yaw = 0
    frames = []
    print("Creating animated png, please wait about 5 seconds")
    for r in range(60):
        yaw += 6
        pitch = -10.0
        roll = 0
        upAxisIndex = 2
        camDistance = 4
        pixelWidth = 320
        pixelHeight = 200
        nearPlane = 0.01
        farPlane = 100
        fov = 60
        viewMatrix = p.computeViewMatrixFromYawPitchRoll(
            camTargetPos, camDistance, yaw, pitch, roll, upAxisIndex)
        aspect = pixelWidth / pixelHeight
        projectionMatrix = p.computeProjectionMatrixFOV(
            fov, aspect, nearPlane, farPlane)

        img_arr = p.getCameraImage(pixelWidth, pixelHeight, viewMatrix,
                                   projectionMatrix)
        w = img_arr[0]
        h = img_arr[1]
        rgb = img_arr[2]
        dep = img_arr[3]
        np_img_arr = np.reshape(rgb, (h, w, 4))
        frame = np_img_arr[:, :, :3]
        frames.append(frame)
    write_apng(fn, frames, delay=100)
Exemple #7
0
def render_policy(env,
                  env_name,
                  algo,
                  policy_path,
                  coop=False,
                  colab=False,
                  seed=0,
                  extra_configs={}):
    ray.init(num_cpus=multiprocessing.cpu_count(),
             ignore_reinit_error=True,
             log_to_driver=False)
    test_agent, _ = load_policy(env, algo, env_name, policy_path, coop, seed,
                                extra_configs)

    if not colab:
        env.render()
    obs = env.reset()
    frames = []
    done = False
    while not done:
        if coop:
            # Compute the next action for the robot/human using the trained policies
            action_robot = test_agent.compute_action(obs['robot'],
                                                     policy_id='robot')
            action_human = test_agent.compute_action(obs['human'],
                                                     policy_id='human')
            # Step the simulation forward using the actions from our trained policies
            obs, reward, done, info = env.step({
                'robot': action_robot,
                'human': action_human
            })
            done = done['__all__']
        else:
            # Compute the next action using the trained policy
            action = test_agent.compute_action(obs)
            # Step the simulation forward using the action from our trained policy
            obs, reward, done, info = env.step(action)
        if colab:
            # Capture (render) an image from the camera
            img, depth = env.get_camera_image_depth()
            frames.append(img)
    env.disconnect()
    if colab:
        filename = 'output_%s.png' % env_name
        write_apng(filename, frames, delay=100)
        return filename
Exemple #8
0
 def export(self, filename, delay, length=30000):
     self._run_init()
     progress = 0
     while not self.is_quit:
         try:
             self._run_update(delay)
             self._run_draw()
             progress += delay
             if progress >= length:
                 raise StopIteration
         except StopIteration:
             self.quit()
     if filename.endswith('.png'):
         write_apng(filename, self.seq, delay=delay, use_palette=True)
     elif filename.endswith('.gif'):
         # array2gif requires a transposed matrix compared to numpngw.
         seq = []
         for s in self.seq:
             seq.append(np.transpose(s, axes=[1, 0, 2]))
         write_gif(seq, filename, fps=int(1000 / delay))
     else:
         raise NotImplementedError('Unsupported file format')
Exemple #9
0
    surface.ellipse((10, 10, W - 20, H - 20), fill=color)
    del surface
    return np.array(im.resize((16, 16), Image.ANTIALIAS))


def fade(color):
    frames = []
    for i in range(25):
        frames.append(fr(color + tuple([int(i * (255.0 / 25))])))
    for i in range(25):
        frames.append(fr(color + tuple([255 - int(i * (255.0 / 25))])))
    return frames


path = "./docs/images/states/"
write_apng(path + "0.png", map(fr, [COLOR_BLUE, COLOR_BLACK]), delay=[50, 500])
write_apng(path + "1.png",
           map(fr, [COLOR_BLUE, COLOR_BLACK]),
           delay=[200, 200])
write_apng(path + "2.png",
           map(fr, [COLOR_BLYNK, COLOR_BLACK]),
           delay=[50, 500])
write_apng(path + "3.png",
           map(fr, [COLOR_BLYNK, COLOR_BLACK]),
           delay=[100, 100])
write_apng(path + "4.png", fade(COLOR_BLYNK), delay=100)
write_apng(path + "5.png",
           map(fr, [COLOR_MAGENTA, COLOR_BLACK]),
           delay=[50, 50])
write_apng(path + "6.png",
           map(fr, [COLOR_RED, COLOR_BLACK, COLOR_RED, COLOR_BLACK]),
Exemple #10
0
def run_policy(env,
               get_action,
               max_ep_len=None,
               num_episodes=100,
               render=False,
               params={},
               verbose=False):

    from upn.visualize.render import forward_env
    from numpngw import write_apng


    assert env is not None, \
        "Environment not found!\n\n It looks like the environment wasn't saved, " + \
        "and we can't run the agent in it. :( \n\n Check out the readthedocs " + \
        "page on Experiment Outputs for how to handle this situation."

    test_envs, test_env_names = [], params["test_env_names"][0]
    for name in test_env_names:
        test_envs.append(gym.make(name))

    logger = EpochLogger()
    for env_name, env in zip(test_env_names, test_envs):
        all_feats = []
        all_rews = []
        o, r, d, ep_ret, ep_len, n = env.reset(), 0, False, 0, 0, 0
        coeff = o[-env.coeff_dim:]
        acs = []
        pbar = tqdm(total=num_episodes)
        while n < num_episodes:
            #import pdb; pdb.set_trace()
            if render:
                env.render()
                time.sleep(1e-3)
            # import pdb; pdb.set_trace()
            a = get_action(o)
            acs.append(a)
            o, r, d, info = env.step(a)
            ep_ret += r
            ep_len += 1
            if "all_feats" in info.keys():
                all_feats.append(info["all_feats"])

            if d or (ep_len == max_ep_len):
                if verbose:
                    print(f"Coeff: {coeff}")
                    print(f"All feats", np.array(all_feats).sum(axis=0))
                # import pdb; pdb.set_trace()
                logger.store(**{f"{env_name}_EpRet": ep_ret})
                logger.store(**{f"{env_name}_EpLen": ep_len})
                # logger.store(EpRet=ep_ret, EpLen=ep_len)
                all_rews.append(ep_ret)
                if verbose:
                    print('Episode %d \t EpRet %.3f \t EpLen %d' %
                          (n, ep_ret, ep_len))
                print(f"{env_name}: reward {ep_ret:.03f}")
                if render:
                    frames = forward_env(env,
                                         np.array(acs),
                                         batch=False,
                                         subrender=False,
                                         resize=0.4)
                    fps = 10
                    fname = f"{env_name}_{n:02d}_rew_{ep_ret:.03f}.png"
                    #os.makedirs(osp.dirname(fname), exist_ok=True)
                    write_apng(os.path.join(args.folder, fname),
                               frames,
                               delay=1000 / fps)

                o, r, d, ep_ret, ep_len = env.reset(), 0, False, 0, 0
                o = env.reset()

                all_feats = []
                acs = []
                n += 1
                pbar.update(1)
        print(f"{env_name}: mean reward {np.mean(all_rews):.03f}")
        pbar.close()

        logger.log_tabular(f'{env_name}_EpRet', with_min_and_max=True)
        logger.log_tabular(f'{env_name}_EpLen', average_only=True)
    logger.dump_tabular()
Exemple #11
0
    plt.scatter(x, y, color='navy', s=30, marker='o', label="training examples")
    model = KernelRidge(alpha=0.01, kernel=kernel, kernel_params = {'b':degree})
    model.fit(X, y)
    y_plot = model.predict(X_plot)
    plt.plot(x_plot, y_plot, color='green', linewidth=lw,
             label="b = " + str(degree))

##    heading = 'Iteration '+str(count)
##    plt.title(heading)
    plt.legend(loc='upper right',prop={'size': 9})
    fig1 = plt.gcf()
    nfig = fig2data(fig1)
    seq.append(nfig)
##    fig1.subplots_adjust(top = 0.98, bottom = 0.1, right = 0.98, left = 0.08, hspace = 0, wspace = 0)
##    fig1.savefig('../../Illustrations/kernel-regression-' + str(count) + '.eps', format='eps', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
##    fig1.savefig('../../Illustrations/kernel-regression-' + str(count) + '.pdf', format='pdf', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
##    fig1.savefig('../../Illustrations/kernel-regression-' + str(count) + '.png', dpi=1000, bbox_inches = 'tight', pad_inches = 0)


##plt.show()

import os
## Get the directory address of current python file
curr_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_dir) ## Set the current directory as working directory

## The package used to create gif files
import numpngw
numpngw.write_apng('kernel_regression.png',seq,delay = 500)

Exemple #12
0
import numpy as np
from numpngw import write_apng


# Example 5
#
# Create an 8-bit RGB animated PNG file.

height = 20
width = 200
t = np.linspace(0, 10*np.pi, width)
seq = []
for phase in np.linspace(0, 2*np.pi, 25, endpoint=False):
    y = 150*0.5*(1 + np.sin(t - phase))
    a = np.zeros((height, width, 3), dtype=np.uint8)
    a[:, :, 0] = y
    a[:, :, 2] = y
    seq.append(a)

write_apng("example5.png", seq, delay=50, use_palette=True)
Exemple #13
0
import numpy as np
from numpngw import write_apng

# Example 5
#
# Create an 8-bit RGB animated PNG file.

height = 20
width = 200
t = np.linspace(0, 10 * np.pi, width)
seq = []
for phase in np.linspace(0, 2 * np.pi, 25, endpoint=False):
    y = 150 * 0.5 * (1 + np.sin(t - phase))
    a = np.zeros((height, width, 3), dtype=np.uint8)
    a[:, :, 0] = y
    a[:, :, 2] = y
    seq.append(a)

write_apng("example5.png", seq, delay=50, use_palette=True)
Exemple #14
0
COLOR_WHITE   = (0xF0, 0xF0, 0xE0)
COLOR_BLUE    = (0x0D, 0x36, 0xFF)
COLOR_BLYNK   = (0x2E, 0xFF, 0xB9)
COLOR_RED     = (0xFF, 0x10, 0x08)
COLOR_MAGENTA = (0xA7, 0x00, 0xFF)

def fr(color):
    im = Image.new('RGBA', (W,H))
    surface = ImageDraw.Draw(im)
    surface.ellipse((10,10,W-20,H-20), fill=color)
    del surface
    return np.array(im.resize((16,16), Image.ANTIALIAS))

def fade(color):
    frames = []
    for i in range(25):
        frames.append(fr(color + tuple([int(i*(255.0/25))])))
    for i in range(25):
        frames.append(fr(color + tuple([255-int(i*(255.0/25))])))
    return frames

path = "./docs/images/states/"
write_apng(path + "0.png", map(fr,[COLOR_BLUE, COLOR_BLACK]), delay=[50, 500])
write_apng(path + "1.png", map(fr,[COLOR_BLUE, COLOR_BLACK]), delay=[200, 200])
write_apng(path + "2.png", map(fr,[COLOR_BLYNK, COLOR_BLACK]), delay=[50, 500])
write_apng(path + "3.png", map(fr,[COLOR_BLYNK, COLOR_BLACK]), delay=[100, 100])
write_apng(path + "4.png", fade(COLOR_BLYNK), delay=100)
write_apng(path + "5.png", map(fr,[COLOR_MAGENTA, COLOR_BLACK]), delay=[50, 50])
write_apng(path + "6.png", map(fr,[COLOR_RED, COLOR_BLACK, COLOR_RED, COLOR_BLACK]), delay=[80, 100, 80, 1000])
write_apng(path + "7.png", fade(COLOR_WHITE), delay=50)
write_apng(path + "8.png", map(fr,[COLOR_WHITE, COLOR_BLACK]), delay=[100, 100])
                s=30,
                marker=2,
                label="training examples")
    plt.plot(x_plot, [fb(xp, x, degree) for xp in x_plot],
             color='blue',
             linewidth=lw,
             label="$\\hat{f}_b$, $b = " + str(degree) + "$")
    plt.plot(x_plot, sum_pdf(x_plot), label="true pdf")
    ##    plt.title(heading)

    plt.legend(loc='upper right', prop={'size': 9})
    plt.tight_layout()

    fig1 = plt.gcf()
    nfig = fig2data(fig1)
    seq.append(nfig)
##    fig1.subplots_adjust(top = 0.98, bottom = 0.1, right = 0.98, left = 0.08, hspace = 0, wspace = 0)
##    fig1.savefig('../../Illustrations/density-estimation-' + str(count) + '.eps', format='eps', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
##    fig1.savefig('../../Illustrations/density-estimation-' + str(count) + '.pdf', format='pdf', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
##    fig1.savefig('../../Illustrations/density-estimation-' + str(count) + '.png', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
#plt.show()

import os
## Get the directory address of current python file
curr_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_dir)  ## Set the current directory as working directory

## The package used to create gif files
import numpngw
numpngw.write_apng('density_estimation.png', seq, delay=500)
    def test_write_apng_bkgd(self):
        # Test creation of RGB images (color type 2), with a background color.
        w = 16
        h = 8
        np.random.seed(123)
        num_frames = 3
        for bit_depth in [8, 16]:
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 2, maxval - 3)
            dt = np.uint16 if bit_depth == 16 else np.uint8
            seq = np.random.randint(0, maxval,
                                    size=(num_frames, h, w, 3)).astype(dt)

            f = io.BytesIO()
            numpngw.write_apng(f, seq, background=bg, filter_type=0)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=2,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            file_contents = check_bkgd(file_contents, color=bg, color_type=2)

            file_contents = check_actl(file_contents, num_frames=num_frames,
                                       num_plays=0)

            sequence_number = 0
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            file_contents = check_idat(file_contents, color_type=2,
                                       bit_depth=bit_depth,
                                       interlace=0, img=seq[0])

            for k in range(1, num_frames):
                file_contents = check_fctl(file_contents,
                                           sequence_number=sequence_number,
                                           width=w, height=h)
                sequence_number += 1

                # Check the fdAT chunk.
                nxt = next_chunk(file_contents)
                chunk_type, chunk_data, file_contents = nxt
                self.assertEqual(chunk_type, b"fdAT")
                actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
                self.assertEqual(actual_seq_num, sequence_number)
                sequence_number += 1
                decompressed = zlib.decompress(chunk_data[4:])
                b = np.fromstring(decompressed, dtype=np.uint8)
                img2 = stream_to_array(b, w, h, color_type=2,
                                       bit_depth=bit_depth, interlace=0)
                assert_array_equal(img2, seq[k])

            check_iend(file_contents)
    mu1_estimate = sum([bis1[i] * x[i] for i in range(len(x))]) / sum([bis1[i] for i in range(len(x))])
    mu2_estimate = sum([bis2[i] * x[i] for i in range(len(x))]) / sum([bis2[i] for i in range(len(x))])

    sigma1_estimate = sum([bis1[i] * (x[i] - mu1_estimate)**2 for i in range(len(x))]) / sum([bis1[i] for i in range(len(x))])
    sigma2_estimate = sum([bis2[i] * (x[i] - mu2_estimate)**2 for i in range(len(x))]) / sum([bis2[i] for i in range(len(x))])

    #print mu1_estimate, mu2_estimate
    #print sigma1_estimate, sigma2_estimate

    phi1_estimate = sum([bis1[i] for i in range(len(x))])/float(len(x))
    phi2_estimate = 1.0 - phi1_estimate

    print(phi1_estimate)

    count += 1

    plt.close(count)

    if count > 50:
        break

import os
## Get the directory address of current python file
curr_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_dir) ## Set the current directory as working directory

## The package used to create gif files
import numpngw
numpngw.write_apng('GMM.png',seq,delay = 250)
Exemple #18
0
    def test_write_apng_bkgd(self):
        # Test creation of RGB images (color type 2), with a background color.
        # Also test the chromaticity argument.
        w = 16
        h = 8
        np.random.seed(123)
        num_frames = 3
        chromaticity = [[0.500, 0.750],
                        [0.125, 0.960],
                        [0.875, 0.625],
                        [0.750, 0.375]]
        for bit_depth in [8, 16]:
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 2, maxval - 3)
            dt = np.uint16 if bit_depth == 16 else np.uint8
            seq = np.random.randint(0, maxval,
                                    size=(num_frames, h, w, 3)).astype(dt)

            f = io.BytesIO()
            numpngw.write_apng(f, seq, background=bg, filter_type=0,
                               chromaticity=chromaticity)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=2,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            expected_chrm = (100000*np.array(chromaticity) + 0.5).astype(np.uint32)
            file_contents = check_chrm(file_contents, expected_chrm)

            file_contents = check_bkgd(file_contents, color=bg, color_type=2)

            file_contents = check_actl(file_contents, num_frames=num_frames,
                                       num_plays=0)

            sequence_number = 0
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            file_contents = check_idat(file_contents, color_type=2,
                                       bit_depth=bit_depth,
                                       interlace=0, img=seq[0])

            for k in range(1, num_frames):
                file_contents = check_fctl(file_contents,
                                           sequence_number=sequence_number,
                                           width=w, height=h)
                sequence_number += 1

                # Check the fdAT chunk.
                nxt = next_chunk(file_contents)
                chunk_type, chunk_data, file_contents = nxt
                self.assertEqual(chunk_type, b"fdAT")
                actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
                self.assertEqual(actual_seq_num, sequence_number)
                sequence_number += 1
                decompressed = zlib.decompress(chunk_data[4:])
                b = np.frombuffer(decompressed, dtype=np.uint8)
                img2 = stream_to_array(b, w, h, color_type=2,
                                       bit_depth=bit_depth, interlace=0)
                assert_array_equal(img2, seq[k])

            check_iend(file_contents)
    plt.yticks([-10.0, -5.0, 0.0, 5.0, 10.0])
    model = make_pipeline(PolynomialFeatures(degree), Ridge())
    model.fit(X, y)
    y_plot = model.predict(X_plot)
    plt.plot(x_plot,
             y_plot,
             color='red',
             linewidth=lw,
             label='linear regression of degree ' + str(degree))

    plt.legend(loc='best')
    plt.tight_layout()
    fig1 = plt.gcf()
    nfig = fig2data(fig1)
    seq.append(nfig)
##    fig1.subplots_adjust(top = 0.98, bottom = 0.1, right = 0.98, left = 0.08, hspace = 0, wspace = 0)
##    fig1.savefig('../../Illustrations/linear-regression-' + fit[count] + '.eps', format='eps', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
##    fig1.savefig('../../Illustrations/linear-regression-' + fit[count] + '.pdf', format='pdf', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
##    fig1.savefig('../../Illustrations/linear-regression-' + fit[count] + '.png', dpi=1000, bbox_inches = 'tight', pad_inches = 0)

#plt.show()

import os
## Get the directory address of current python file
curr_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_dir)  ## Set the current directory as working directory

## The package used to create gif files
import numpngw
numpngw.write_apng('linear_regression.png', seq, delay=500)
Exemple #20
0
# Create `seq` and `delay`, the sequence of images and the
# corresponding display times.

color = np.array([32, 48, 255])
blocksize = 24
# Images...
im3 = bits_to_image(bits3, blocksize=blocksize, color=color)
im2 = bits_to_image(bits2, blocksize=blocksize, color=color)
im1 = bits_to_image(bits1, blocksize=blocksize, color=color)
im_all = bits_to_image(bits_ones, blocksize=blocksize, color=color)
im_none = bits_to_image(bits_zeros, blocksize=blocksize, color=color)
im_box1 = bits_to_image(bits_box1, blocksize=blocksize, color=color)
im_box2 = bits_to_image(bits_box2, blocksize=blocksize, color=color)
im_dot = bits_to_image(bits_dot, blocksize=blocksize, color=color)

# The sequence of images:
seq = [
    im3, im2, im1, im_all, im_none, im_all, im_none, im_all, im_none, im_box1,
    im_box2, im_dot, im_none
]
# The time duration to display each image, in milliseconds:
delay = [1000, 1000, 1000, 333, 250, 333, 250, 333, 500, 167, 167, 167, 1000]

# Create the animated PNG file.
write_apng("example7.png",
           seq,
           delay=delay,
           default_image=im_all,
           use_palette=True)
Exemple #21
0
import numpy as np
from numpngw import write_apng

# Example 6
#
# Create an 8-bit RGB animated PNG file.


def smoother(w):
    # Return the periodic convolution of w with a 3-d Gaussian kernel.
    r = np.linspace(-3, 3, 21)
    X, Y, Z = np.meshgrid(r, r, r)
    kernel = np.exp(-0.25 * (X * X + Y * Y + Z * Z)**2)
    fw = np.fft.fftn(w)
    fkernel = np.fft.fftn(kernel, w.shape)
    v = np.fft.ifftn(fw * fkernel).real
    return v


height = 40
width = 250
num_frames = 30
np.random.seed(12345)
w = np.random.randn(num_frames, height, width, 3)
for k in range(3):
    w[..., k] = smoother(w[..., k])

seq = (255 * (w - w.min()) / w.ptp()).astype(np.uint8)

write_apng("example6.png", seq, delay=40)
Exemple #22
0
def plot_cat_prm_vecs_evo_kf(
        iter_prm_vecs,
        cat,
        kf_i,
        out_dir,
        prm_syms,
        save_png_flag,
        save_gif_flag,
        anim_secs):

    '''Plot parameter vector evolution for a given kfolds.'''

    prm_cols = 7
    prm_rows = 7

    tot_sps_per_fig = prm_cols * prm_rows

    tot_iters = iter_prm_vecs.shape[0]

    min_lim = 0  # np.nanmin(iter_prm_vecs)
    max_lim = 1  # np.nanmax(iter_prm_vecs)

    tot_figs = int(np.ceil(tot_iters / tot_sps_per_fig))

    prm_iter_ct = 0
    stop_plotting = False

    if save_gif_flag:
        opt_vecs_fig_arrs_list = []

        blines = 10  # buffer lines on all sides for gifs

    for fig_no in range(tot_figs):
        if stop_plotting:
            break

        fig = plt.figure(figsize=(19, 9), dpi=200)
        axes = GridSpec(prm_rows, prm_cols)

        for i in range(prm_rows):
            for j in range(prm_cols):

                ax = plt.subplot(axes[i, j])

                for k in range(iter_prm_vecs.shape[1]):
                    ax.plot(
                        iter_prm_vecs[prm_iter_ct, k],
                        alpha=0.005,
                        color='k')

                ax.text(
                    0.95,
                    0.95,
                    f'({prm_iter_ct})',
                    horizontalalignment='right',
                    verticalalignment='top',
                    transform=ax.transAxes)

                ax.set_xticklabels([])
                ax.set_yticklabels([])

                ax.set_ylim(min_lim, max_lim)

                prm_iter_ct += 1

                if prm_iter_ct >= tot_iters:
                    stop_plotting = True
                    break

            if stop_plotting:
                break

        plt.suptitle(
            f'Parameter vector evolution per iteration\n'
            f'Vectors per iteration: {iter_prm_vecs.shape[1]}')

        if save_png_flag:
            out_fig_name = (
                f'hbv_prm_vecs_evo_{cat}_kf_{kf_i:02d}_fig_{fig_no:02d}.png')

            plt.savefig(str(Path(out_dir, out_fig_name)), bbox_inches='tight')

        if save_gif_flag:
            fig.canvas.draw()  # calling draw is important here

            fig_arr = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
            fig_arr = fig_arr.reshape(
                fig.canvas.get_width_height()[::-1] + (3,))

            mins_arr = fig_arr[:,:,:].min(axis=2)

            not_white_idxs = np.where(mins_arr < 255)

            r_top = not_white_idxs[0].min()
            r_bot = not_white_idxs[0].max()

            c_left = not_white_idxs[1].min()
            c_rght = not_white_idxs[1].max()

            r_top -= min(blines, r_top)
            r_bot += min(blines, mins_arr.shape[0] - r_bot)

            c_left -= min(blines, c_left)
            c_rght += min(blines, mins_arr.shape[1] - c_rght)

            fig_arr = fig_arr[r_top:r_bot + 1, c_left:c_rght + 1,:]

            opt_vecs_fig_arrs_list.append(fig_arr)

        plt.close()

    if save_gif_flag:
        out_fig_name = (
            f'hbv_prm_vecs_evo_{cat}_kf_{kf_i:02d}_anim.png')

        write_apng(
            str(Path(out_dir, out_fig_name)),
            opt_vecs_fig_arrs_list,
            delay=1000,
            use_palette=True,
            num_plays=1)

        del opt_vecs_fig_arrs_list

    ##########################################################################
    if save_gif_flag:
        chull_fig_arrs_list = []

    chull_min = 0
    chull_max = 1
    n_dims = iter_prm_vecs.shape[-1]

    for opt_iter in range(iter_prm_vecs.shape[0]):

        fig = plt.figure(figsize=(15, 15), dpi=200)
        grid_axes = GridSpec(n_dims, n_dims)
        for i in range(n_dims):
            for j in range(n_dims):
                if i >= j:
                    continue

                ax = plt.subplot(grid_axes[i, j])

                ax.set_aspect('equal', 'datalim')

                ax.set_xlim(chull_min, chull_max)
                ax.set_ylim(chull_min, chull_max)

                ax.scatter(
                    iter_prm_vecs[opt_iter,:, i],
                    iter_prm_vecs[opt_iter,:, j],
                    s=2,
                    color='k',
                    alpha=0.05)

                ax.set_xticks([])
                ax.set_xticklabels([])

                ax.set_yticks([])
                ax.set_yticklabels([])

                ax.text(
                    0.95,
                    0.95,
                    f'({prm_syms[i]}, {prm_syms[j]})',
                    horizontalalignment='right',
                    verticalalignment='top',
                    transform=ax.transAxes)

        plt.suptitle(
            f'Convex hull of {n_dims}D in 2D\n'
            f'Total points: {iter_prm_vecs.shape[1]}\n'
            f'Iteration no.: {opt_iter + 1} out of {iter_prm_vecs.shape[0]}',
            x=0.4,
            y=0.5,
            va='bottom',
            ha='right')

        if save_png_flag:
            out_fig_name = (
                f'hbv_prm_vecs_chull_{cat}_kf_{kf_i:02d}_iter_'
                f'{opt_iter:02d}.png')

            plt.savefig(
                str(Path(out_dir, out_fig_name)), bbox_inches='tight')

        if save_gif_flag:
            fig.canvas.draw()  # calling draw is important here

            fig_arr = np.frombuffer(
                fig.canvas.tostring_rgb(), dtype=np.uint8)

            fig_arr = fig_arr.reshape(
                fig.canvas.get_width_height()[::-1] + (3,))

            mins_arr = fig_arr[:,:,:].min(axis=2)

            not_white_idxs = np.where(mins_arr < 255)

            r_top = not_white_idxs[0].min()
            r_bot = not_white_idxs[0].max()

            c_left = not_white_idxs[1].min()
            c_rght = not_white_idxs[1].max()

            r_top -= min(blines, r_top)
            r_bot += min(blines, mins_arr.shape[0] - r_bot)

            c_left -= min(blines, c_left)
            c_rght += min(blines, mins_arr.shape[1] - c_rght)

            fig_arr = fig_arr[r_top:r_bot + 1, c_left:c_rght + 1,:]

            chull_fig_arrs_list.append(fig_arr)

        plt.close('all')

    if save_gif_flag:
        out_fig_name = (
            f'hbv_prm_vecs_chull_{cat}_kf_{kf_i:02d}_anim.png')

        write_apng(
            str(Path(out_dir, out_fig_name)),
            chull_fig_arrs_list,
            delay=(1000 * anim_secs) / iter_prm_vecs.shape[0],
            use_palette=True,
            num_plays=1)

        del chull_fig_arrs_list
    return
Exemple #23
0
    bag = rosbag.Bag(BAG, 'r')
    print("bag file opened")
    to_skip=0
    SKIP = int(arguments['-s'])
    bridge = CvBridge()
    images=[]
    max_frame = 2
    scale = arguments['--sx'] and arguments['--sy']
    added_image_n = 0
    if scale:
        scalex, scaley = int(arguments['--sx']), int(arguments['--sy'])
    for topic, msg, t in bag.read_messages(topics=[arguments['-t']]):
        if to_skip > 0:
            to_skip -= 1
            continue
        if SKIP > 0:
            to_skip = SKIP
        cv_image = bridge.imgmsg_to_cv2(msg)
        if scale:
            cv_image = cv2.resize(cv_image, (scalex, scaley))
        images.append(cv_image)
        max_frame -= 1
        added_image_n += 1
        print ("added image {}".format(added_image_n))
        # if max_frame <= 0:
            # break
    OUT = arguments['-o']
    print ("bag finished, writing to {}".format(OUT))
    numpngw.write_apng(OUT, images, delay=arguments['-d'], use_palette=True)
    bag.close()
Exemple #24
0
import numpy as np
from numpngw import write_apng


# Example 6
#
# Create an 8-bit RGB animated PNG file.

def smoother(w):
    # Return the periodic convolution of w with a 3-d Gaussian kernel.
    r = np.linspace(-3, 3, 21)
    X, Y, Z = np.meshgrid(r, r, r)
    kernel = np.exp(-0.25*(X*X + Y*Y + Z*Z)**2)
    fw = np.fft.fftn(w)
    fkernel = np.fft.fftn(kernel, w.shape)
    v = np.fft.ifftn(fw*fkernel).real
    return v

height = 40
width = 250
num_frames = 30
np.random.seed(12345)
w = np.random.randn(num_frames, height, width, 3)
for k in range(3):
    w[..., k] = smoother(w[..., k])

seq = (255*(w - w.min())/w.ptp()).astype(np.uint8)

write_apng("example6.png", seq, delay=40)
Exemple #25
0
def loss(spendings, sales, w, b):
    N = len(spendings)
    total_error = 0.0
    for i in range(N):
        total_error += (sales[i] - (w * spendings[i] + b))**2
    return total_error / N


df = pd.read_csv("data.csv")
x = df['Spendings']
y = df['Sales']
w, b = train(x, y, 0.0, 0.0, 0.001, 16000)


def predict(x, w, b):
    return w * x + b


x_new = 23.0
y_new = predict(x_new, w, b)
print(y_new)

import os
## Get the directory address of current python file
curr_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_dir)  ## Set the current directory as working directory

## The package used to create gif files
import numpngw
numpngw.write_apng('gradient_descent.png', seq, delay=750)
        ##        fig1.savefig('../../Illustrations/kmeans-' + str(counter) + '.eps', format='eps', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
        ##        fig1.savefig('../../Illustrations/kmeans-' + str(counter) + '.pdf', format='pdf', dpi=1000, bbox_inches = 'tight', pad_inches = 0)
        ##        fig1.savefig('../../Illustrations/kmeans-' + str(counter) + '.png', dpi=1000, bbox_inches = 'tight', pad_inches = 0)

        #plt.show()

        # find new centroids as the average of examples
        new_centroids = np.array(
            [x[labels == i].mean(0) for i in range(n_clusters)])

        # check for convergence
        if np.all(centroids == new_centroids):
            break
        centroids = new_centroids

        counter += 1

    return centroids, labels


centroids, labels = find_clusters(x, 3)

import os
## Get the directory address of current python file
curr_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_dir)  ## Set the current directory as working directory

## The package used to create gif files
import numpngw
numpngw.write_apng('kmeans.png', seq, delay=500)